Skip to content

Commit

Permalink
Improve 'm3u-prune' — content is not pruned if contains carriage return
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamWr committed Sep 6, 2023
1 parent a1997f8 commit 678a302
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- issue with not pruning in `m3u-prune` scriptlet if file contains carriage return
[#354](https://github.com/AdguardTeam/Scriptlets/issues/354)
- issue with not overriding value in `set-constant` (only partially, for cases where single scriptlet is used)
[#330](https://github.com/AdguardTeam/Scriptlets/issues/330)

Expand Down
4 changes: 4 additions & 0 deletions src/scriptlets/m3u-prune.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ export function m3uPrune(source, propsToRemove, urlToMatch = '') {
const pruneM3U = (text) => {
let lines = text.split(/\n\r|\n|\r/);

// Remove empty elements from array
// https://github.com/AdguardTeam/Scriptlets/issues/354
lines = lines.filter((l) => !!l);

if (text.includes(COMCAST_AD_MARKER.VMAP_AD_BREAK)) {
lines = pruneVmapBlock(lines);
return lines.filter((l) => !!l).join('\n');
Expand Down
22 changes: 22 additions & 0 deletions tests/scriptlets/m3u-prune.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const M3U8_OBJECTS_PATH_03 = './test-files/manifestM3U8-03.m3u8';
// From sbs.com - https://github.com/AdguardTeam/AdguardFilters/issues/88692
// https://regex101.com/r/Kxtnng/1
const M3U8_OBJECTS_PATH_04 = './test-files/manifestM3U8-04.m3u8';
// https://github.com/AdguardTeam/Scriptlets/issues/354
const M3U8_OBJECTS_CR = './test-files/manifestM3U8-carriage-return.m3u8';

const nativeFetch = fetch;
const nativeXhrOpen = XMLHttpRequest.prototype.open;
const nativeConsole = console.log;
Expand Down Expand Up @@ -355,6 +358,25 @@ if (!isSupported) {
done();
});

test('fetch - remove ads carriage return', async (assert) => {
const M3U8_PATH = M3U8_OBJECTS_CR;
const MATCH_DATA = 'advert.com';

runScriptlet(name, [MATCH_DATA]);

const done = assert.async();

const response = await fetch(M3U8_PATH);
const responseM3U8 = await response.text();

assert.notOk(
responseM3U8.includes('advert.com'),
'check if "advert.com" has been removed',
);
assert.strictEqual(window.hit, 'FIRED', 'hit function fired');
done();
});

test('xhr - no prune 1', async (assert) => {
const METHOD = 'GET';
const M3U8_PATH = M3U8_OBJECTS_PATH_01;
Expand Down
15 changes: 15 additions & 0 deletions tests/scriptlets/test-files/manifestM3U8-carriage-return.m3u8
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#EXTM3U
#EXT-X-VERSION:4
#EXT-X-TARGETDURATION:6
#EXT-X-MEDIA-SEQUENCE:1
#EXT-X-PLAYLIST-TYPE:VOD
#EXTINF:5.005,
https://advert.com/foo.ts
#EXT-X-DISCONTINUITY
#EXTINF:6,
https://example.com/segment1.ts
#EXTINF:6,
https://example.com/segment2.ts
#EXTINF:6,
https://example.com/segment3.ts
#EXT-X-DISCONTINUITY

0 comments on commit 678a302

Please sign in to comment.