diff --git a/CHANGELOG.md b/CHANGELOG.md index e6335b0..296a6a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [2.2.0] - 2024-03-26 + +### Changed + +- rawFilter is returned as a string, so that new lines can be preserved. + +[2.2.0]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.1.2...v2.2.0 ## [2.1.2] - 2024-03-25 diff --git a/__tests__/filters-downloader.test.ts b/__tests__/filters-downloader.test.ts index 9eb8176..961d159 100644 --- a/__tests__/filters-downloader.test.ts +++ b/__tests__/filters-downloader.test.ts @@ -37,7 +37,7 @@ describe('FiltersDownloader', () => { const { filter, rawFilter } = await FiltersDownloader.downloadWithRaw(url, { rawFilter: prevFilter }); expect(filter).toEqual(curFilter.trim().split(/[\r\n]+/)); // since there are no any includes, they are the same - expect(rawFilter).toEqual(curFilter.trim().split(/[\r\n]+/)); + expect(rawFilter).toEqual(curFilter); }); it('applies conditions after patches', async () => { @@ -55,13 +55,11 @@ describe('FiltersDownloader', () => { '||example.net^', '||included.com^', ]; - const expectedRawFilter = [ - '! Title: Diff Updates Simple Example List', - '! Version: v1.0.2', - '! Diff-Path: patches/v1.0.1-s-1702460925-5.patch', - '||example.net^', - '!#include ./filter_2.txt', - ]; + const expectedRawFilter = `! Title: Diff Updates Simple Example List +! Version: v1.0.2 +! Diff-Path: patches/v1.0.1-s-1702460925-5.patch +||example.net^ +!#include ./filter_2.txt`; expect(filter).toEqual(expectedFilter); expect(rawFilter).toEqual(expectedRawFilter); }); @@ -102,13 +100,12 @@ describe('FiltersDownloader', () => { '||example.net^', '||included.com^', ]; - const expectedRawFilter = [ - '! Title: Diff Updates Simple Example List', - '! Version: v1.0.2', - '! Diff-Path: patches/v1.0.1-s-1702460925-5.patch', - '||example.net^', - '!#include filter_2.txt', - ]; + const expectedRawFilter = `! Title: Diff Updates Simple Example List +! Version: v1.0.2 +! Diff-Path: patches/v1.0.1-s-1702460925-5.patch +||example.net^ +!#include filter_2.txt +`; expect(filter).toEqual(expectedFilter); expect(rawFilter).toEqual(expectedRawFilter); // Assert that each URL was fetched only once @@ -161,12 +158,11 @@ describe('FiltersDownloader', () => { '||example.net^', '||included.com^', ]; - const expectedRawFilter = [ - '! Title: Simple Example List', - '! Version: v1.0.2', - '||example.net^', - '!#include filter_2.txt', - ]; + const expectedRawFilter = `! Title: Simple Example List +! Version: v1.0.2 +||example.net^ +!#include filter_2.txt +`; expect(filter).toEqual(expectedFilter); expect(rawFilter).toEqual(expectedRawFilter); // Assert that each URL was fetched only once @@ -221,12 +217,11 @@ describe('FiltersDownloader', () => { '! Version: v1.0.0', '||included2.com^', ]; - const expectedRawFilter = [ - '! Title: Simple Example List', - '! Version: v1.0.2', - '||example.net^', - '!#include filter_2.txt', - ]; + const expectedRawFilter = `! Title: Simple Example List +! Version: v1.0.2 +||example.net^ +!#include filter_2.txt +`; expect(filter).toEqual(expectedFilter); expect(rawFilter).toEqual(expectedRawFilter); // Assert that each URL was fetched only once diff --git a/package.json b/package.json index c041655..e3f8cbc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@adguard/filters-downloader", - "version": "2.1.2", + "version": "2.2.0", "description": "Compiles filters source files", "main": "dist/index.js", "browser": "dist/index.browser.js", diff --git a/src/filters-downloader-creator.ts b/src/filters-downloader-creator.ts index ec845cd..295982b 100644 --- a/src/filters-downloader-creator.ts +++ b/src/filters-downloader-creator.ts @@ -168,7 +168,7 @@ export interface DownloadResult { /** * The raw filter, which is the filter before any conditions are applied or inclusions resolved. */ - rawFilter: string[]; + rawFilter: string; /** * Flag to indicate if the patch update failed. @@ -598,15 +598,12 @@ const FiltersDownloaderCreator = (FileDownloadWrapper: IFileDownloader): IFilter }; /** - * Normalizes filter content. And splits it by lines. - * @param file Filter content. - * + * Splits filter by lines. + * @param filter Filter to split. * @returns Array of strings. */ - const normalizeFilterContent = (file: string): string[] => { - return file - .trim() - .split(/[\r\n]+/); + const splitFilter = (filter: string): string[] => { + return filter.trim().split(/[\r\n]+/); }; /** @@ -631,20 +628,20 @@ const FiltersDownloaderCreator = (FileDownloadWrapper: IFileDownloader): IFilter ? `${filterUrlOrigin}/${url}` : url; - const file = await FileDownloadWrapper.getExternalFile(filterUrl); + const rawFilter = await FileDownloadWrapper.getExternalFile(filterUrl); if (downloadOptions && downloadOptions.validateChecksum) { - if (!isValidChecksum(file, downloadOptions.validateChecksumStrict)) { + if (!isValidChecksum(rawFilter, downloadOptions.validateChecksumStrict)) { throw new Error('Invalid checksum'); } } - const filter = normalizeFilterContent(file); + const filter = splitFilter(rawFilter); if (!downloadOptions?.resolveDirectives) { return { filter, - rawFilter: filter, + rawFilter, }; } @@ -658,7 +655,7 @@ const FiltersDownloaderCreator = (FileDownloadWrapper: IFileDownloader): IFilter return { filter: includesResult, - rawFilter: filter, + rawFilter, }; }; @@ -702,20 +699,20 @@ const FiltersDownloaderCreator = (FileDownloadWrapper: IFileDownloader): IFilter : url; const origin = getFilterUrlOrigin(urlToLoad, filterOrigin); - const rawFilterContent = await FileDownloadWrapper.getLocalFile(urlToLoad, origin); + const rawFilter = await FileDownloadWrapper.getLocalFile(urlToLoad, origin); if (downloadOptions && downloadOptions.validateChecksum) { - if (!isValidChecksum(rawFilterContent, downloadOptions.validateChecksumStrict)) { + if (!isValidChecksum(rawFilter, downloadOptions.validateChecksumStrict)) { throw new Error('Invalid checksum'); } } - const filterContent = normalizeFilterContent(rawFilterContent); + const filterContent = splitFilter(rawFilter); if (!downloadOptions?.resolveDirectives) { return { filter: filterContent, - rawFilter: filterContent, + rawFilter, }; } @@ -725,7 +722,7 @@ const FiltersDownloaderCreator = (FileDownloadWrapper: IFileDownloader): IFilter const includesResult = await resolveIncludes(conditionsResult, urlOrigin, downloadOptions.definedExpressions); return { filter: includesResult, - rawFilter: filterContent, + rawFilter, }; }; @@ -799,11 +796,12 @@ const FiltersDownloaderCreator = (FileDownloadWrapper: IFileDownloader): IFilter * @returns A Promise that resolves to the result of resolving the includes. */ async function resolveConditionsAndIncludes( - rawFilter: string[], + rawFilter: string, options: DownloadWithRawOptions, filterUrlOrigin?: string, ): Promise { - const resolvedConditionsResult = resolveConditions(rawFilter, options.definedExpressions); + const filter = splitFilter(rawFilter); + const resolvedConditionsResult = resolveConditions(filter, options.definedExpressions); return resolveIncludes( resolvedConditionsResult, filterUrlOrigin, @@ -847,15 +845,6 @@ const FiltersDownloaderCreator = (FileDownloadWrapper: IFileDownloader): IFilter }; } - /** - * Splits filter by lines. - * @param filter Filter to split. - * @returns Array of strings. - */ - const splitFilter = (filter: string): string[] => { - return filter.trim().split(/[\r\n]+/); - }; - /** * Downloads filter rules from a URL without resolving pre-processor directives. * @@ -897,7 +886,7 @@ const FiltersDownloaderCreator = (FileDownloadWrapper: IFileDownloader): IFilter if (e instanceof UnacceptableResponseError) { return { filter: splitFilter(options.rawFilter), - rawFilter: splitFilter(options.rawFilter), + rawFilter: options.rawFilter, isPatchUpdateFailed: true, }; } @@ -916,19 +905,19 @@ const FiltersDownloaderCreator = (FileDownloadWrapper: IFileDownloader): IFilter if (rawFilter === options.rawFilter) { return { filter: splitFilter(options.rawFilter), - rawFilter: splitFilter(options.rawFilter), + rawFilter: options.rawFilter, }; } const resolveResult = await resolveConditionsAndIncludes( - splitFilter(rawFilter), + rawFilter, options, filterUrlOrigin, ); return { filter: resolveResult, - rawFilter: splitFilter(rawFilter), + rawFilter, }; };