From cb72211795c546b25831a5b419529984ae46701e Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sat, 31 Jul 2021 08:38:33 -0400 Subject: [PATCH] Move orphanizeString() into text-utils module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Another small step toward the goal of reducing dependency on `µb`. Related issue: - https://github.com/uBlockOrigin/uBlock-issues/issues/1664 text-iterators module has been renamed text-utils to better reflect its content. --- platform/browser/main.js | 2 +- platform/nodejs/main.js | 2 +- src/js/benchmarks.js | 2 +- src/js/dynamic-net-filtering.js | 2 +- src/js/hnswitches.js | 2 +- src/js/pagestore.js | 3 ++- src/js/redirect-engine.js | 9 ++++++--- src/js/storage.js | 14 +++++++------- src/js/{text-iterators.js => text-utils.js} | 17 ++++++++++++++++- src/js/url-net-filtering.js | 2 +- src/js/utils.js | 8 -------- 11 files changed, 37 insertions(+), 26 deletions(-) rename src/js/{text-iterators.js => text-utils.js} (85%) diff --git a/platform/browser/main.js b/platform/browser/main.js index aaacfe9a2..5beee3c46 100644 --- a/platform/browser/main.js +++ b/platform/browser/main.js @@ -28,7 +28,7 @@ import './lib/punycode.js'; import globals from './js/globals.js'; import { FilteringContext } from './js/filtering-context.js'; -import { LineIterator } from './js/text-iterators.js'; +import { LineIterator } from './js/text-utils.js'; import { StaticFilteringParser } from './js/static-filtering-parser.js'; import { staticNetFilteringEngine } from './js/static-net-filtering.js'; diff --git a/platform/nodejs/main.js b/platform/nodejs/main.js index b8a1830d8..171ec8636 100644 --- a/platform/nodejs/main.js +++ b/platform/nodejs/main.js @@ -31,7 +31,7 @@ import './lib/publicsuffixlist/publicsuffixlist.js'; import globals from './js/globals.js'; import snfe from './js/static-net-filtering.js'; import { FilteringContext } from './js/filtering-context.js'; -import { LineIterator } from './js/text-iterators.js'; +import { LineIterator } from './js/text-utils.js'; import { StaticFilteringParser } from './js/static-filtering-parser.js'; import { diff --git a/src/js/benchmarks.js b/src/js/benchmarks.js index 4514f28c5..fc7f987d1 100644 --- a/src/js/benchmarks.js +++ b/src/js/benchmarks.js @@ -30,8 +30,8 @@ import scriptletFilteringEngine from './scriptlet-filtering.js'; import staticNetFilteringEngine from './static-net-filtering.js'; import µb from './background.js'; import { FilteringContext } from './filtering-context.js'; +import { LineIterator } from './text-utils.js'; import { sessionFirewall } from './dynamic-net-filtering.js'; -import { LineIterator } from './text-iterators.js'; import { domainFromHostname, diff --git a/src/js/dynamic-net-filtering.js b/src/js/dynamic-net-filtering.js index 926036964..5470c98f2 100644 --- a/src/js/dynamic-net-filtering.js +++ b/src/js/dynamic-net-filtering.js @@ -28,7 +28,7 @@ import '../lib/punycode.js'; import globals from './globals.js'; -import { LineIterator } from './text-iterators.js'; +import { LineIterator } from './text-utils.js'; import { decomposeHostname, diff --git a/src/js/hnswitches.js b/src/js/hnswitches.js index 63178ca89..876d4b835 100644 --- a/src/js/hnswitches.js +++ b/src/js/hnswitches.js @@ -28,7 +28,7 @@ import '../lib/punycode.js'; import globals from './globals.js'; -import { LineIterator } from './text-iterators.js'; +import { LineIterator } from './text-utils.js'; import { decomposeHostname } from './uri-utils.js'; /******************************************************************************/ diff --git a/src/js/pagestore.js b/src/js/pagestore.js index 5d9871cf0..48e7e4e1a 100644 --- a/src/js/pagestore.js +++ b/src/js/pagestore.js @@ -27,6 +27,7 @@ import contextMenu from './contextmenu.js'; import logger from './logger.js'; import staticNetFilteringEngine from './static-net-filtering.js'; import µb from './background.js'; +import { orphanizeString } from './text-utils.js'; import { redirectEngine } from './redirect-engine.js'; import { sessionFirewall } from './dynamic-net-filtering.js'; import { sessionSwitches } from './hnswitches.js'; @@ -468,7 +469,7 @@ const PageStore = class { ) { return false; } - this.title = µb.orphanizeString(details.title.slice(0, 128)); + this.title = orphanizeString(details.title.slice(0, 128)); return true; } diff --git a/src/js/redirect-engine.js b/src/js/redirect-engine.js index 58a6ec467..fc39f54ce 100644 --- a/src/js/redirect-engine.js +++ b/src/js/redirect-engine.js @@ -24,8 +24,11 @@ /******************************************************************************/ import io from './assets.js'; -import µb from './background.js'; -import { LineIterator } from './text-iterators.js'; + +import { + LineIterator, + orphanizeString, +} from './text-utils.js'; /******************************************************************************/ @@ -409,7 +412,7 @@ RedirectEngine.prototype.resourcesFromString = function(text) { // No more data, add the resource. const name = this.aliases.get(fields[0]) || fields[0]; const mime = fields[1]; - const content = µb.orphanizeString( + const content = orphanizeString( fields.slice(2).join(encoded ? '' : '\n') ); this.resources.set( diff --git a/src/js/storage.js b/src/js/storage.js index 76c1f3e93..9525440b6 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -36,7 +36,6 @@ import staticFilteringReverseLookup from './reverselookup.js'; import staticNetFilteringEngine from './static-net-filtering.js'; import µb from './background.js'; import { hostnameFromURI } from './uri-utils.js'; -import { LineIterator } from './text-iterators.js'; import { permanentFirewall } from './dynamic-net-filtering.js'; import { permanentSwitches } from './hnswitches.js'; import { permanentURLFiltering } from './url-net-filtering.js'; @@ -50,6 +49,11 @@ import { CompiledListWriter, } from './static-filtering-io.js'; +import { + LineIterator, + orphanizeString, +} from './text-utils.js'; + /******************************************************************************/ µb.getBytesInUse = async function() { @@ -898,10 +902,6 @@ self.addEventListener('hiddenSettingsChanged', ( ) => { // https://github.com/gorhill/uBlock/issues/3406 // Lower minimum update period to 1 day. -// https://bugs.chromium.org/p/v8/issues/detail?id=2869 -// orphanizeString is to work around String.slice() potentially causing -// the whole raw filter list to be held in memory just because we cut out -// the title as a substring. µb.extractFilterListMetadata = function(assetKey, raw) { const listEntry = this.availableFilterLists[assetKey]; @@ -914,13 +914,13 @@ self.addEventListener('hiddenSettingsChanged', ( ) => { let matches = head.match(/(?:^|\n)(?:!|# )[\t ]*Title[\t ]*:([^\n]+)/i); const title = matches && matches[1].trim() || ''; if ( title !== '' && title !== listEntry.title ) { - listEntry.title = this.orphanizeString(title); + listEntry.title = orphanizeString(title); io.registerAssetSource(assetKey, { title }); } matches = head.match(/(?:^|\n)(?:!|# )[\t ]*Homepage[\t ]*:[\t ]*(https?:\/\/\S+)\s/i); const supportURL = matches && matches[1] || ''; if ( supportURL !== '' && supportURL !== listEntry.supportURL ) { - listEntry.supportURL = this.orphanizeString(supportURL); + listEntry.supportURL = orphanizeString(supportURL); io.registerAssetSource(assetKey, { supportURL }); } } diff --git a/src/js/text-iterators.js b/src/js/text-utils.js similarity index 85% rename from src/js/text-iterators.js rename to src/js/text-utils.js index e2986ddae..7082c1ac1 100644 --- a/src/js/text-iterators.js +++ b/src/js/text-utils.js @@ -23,6 +23,17 @@ /******************************************************************************/ +// https://bugs.chromium.org/p/v8/issues/detail?id=2869 +// orphanizeString is to work around String.slice() potentially causing +// the whole raw filter list to be held in memory just because we cut out +// the title as a substring. + +function orphanizeString(s) { + return JSON.parse(JSON.stringify(s)); +} + +/******************************************************************************/ + class LineIterator { constructor(text, offset) { this.text = text; @@ -89,4 +100,8 @@ class FieldIterator { /******************************************************************************/ -export { LineIterator, FieldIterator }; +export { + FieldIterator, + LineIterator, + orphanizeString, +}; diff --git a/src/js/url-net-filtering.js b/src/js/url-net-filtering.js index 8b4d56a67..2b452c645 100644 --- a/src/js/url-net-filtering.js +++ b/src/js/url-net-filtering.js @@ -23,7 +23,7 @@ /******************************************************************************/ -import { LineIterator } from './text-iterators.js'; +import { LineIterator } from './text-utils.js'; import { decomposeHostname } from './uri-utils.js'; /******************************************************************************* diff --git a/src/js/utils.js b/src/js/utils.js index 5e996c684..1aefa59c0 100644 --- a/src/js/utils.js +++ b/src/js/utils.js @@ -141,14 +141,6 @@ import µb from './background.js'; /******************************************************************************/ -// TODO: evaluate using TextEncoder/TextDecoder - -µb.orphanizeString = function(s) { - return JSON.parse(JSON.stringify(s)); -}; - -/******************************************************************************/ - µb.fireDOMEvent = function(name) { if ( window instanceof Object &&