From 41693407b27d517486741ff19c2774cb55fd1319 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sat, 5 Oct 2024 11:32:59 -0400 Subject: [PATCH] Fix npm test suite Ensure serialization returns copy of data rather than live references to data. This allows to immediately deserialize() the result of serialize(). Also, adjust code to modified behavior of filterQuery(). --- platform/nodejs/index.js | 1 + platform/npm/package-lock.json | 91 +++++++++++++++++++--------------- src/js/biditrie.js | 4 +- src/js/hntrie.js | 4 +- src/js/static-net-filtering.js | 29 +++-------- 5 files changed, 65 insertions(+), 64 deletions(-) diff --git a/platform/nodejs/index.js b/platform/nodejs/index.js index 1d39a7d11292e..7bc808e89941f 100644 --- a/platform/nodejs/index.js +++ b/platform/nodejs/index.js @@ -218,6 +218,7 @@ class StaticNetFilteringEngine { } filterQuery(details) { + fctx.redirectURL = undefined; const directives = snfe.filterQuery(fctx.fromDetails(details)); if ( directives === undefined ) { return; } return { redirectURL: fctx.redirectURL, directives }; diff --git a/platform/npm/package-lock.json b/platform/npm/package-lock.json index b819bb0fddb68..3a1790e27a906 100644 --- a/platform/npm/package-lock.json +++ b/platform/npm/package-lock.json @@ -242,12 +242,15 @@ } }, "node_modules/braces": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", - "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==", + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.3.tgz", + "integrity": "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==", "dev": true, "dependencies": { - "fill-range": "^7.0.1" + "fill-range": "^7.1.1" + }, + "engines": { + "node": ">=8" } }, "node_modules/browser-stdout": { @@ -683,12 +686,15 @@ } }, "node_modules/fill-range": { - "version": "7.0.1", - "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", - "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==", + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz", + "integrity": "sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==", "dev": true, "dependencies": { "to-regex-range": "^5.0.1" + }, + "engines": { + "node": ">=8" } }, "node_modules/find-up": { @@ -892,7 +898,10 @@ "version": "7.0.0", "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", - "dev": true + "dev": true, + "engines": { + "node": ">=0.12.0" + } }, "node_modules/is-path-inside": { "version": "3.0.3", @@ -1062,15 +1071,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/make-dir/node_modules/semver": { - "version": "6.3.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz", - "integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==", - "dev": true, - "bin": { - "semver": "bin/semver.js" - } - }, "node_modules/minimatch": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", @@ -1398,6 +1398,15 @@ "resolved": "git+ssh://git@github.com/mjethani/scaling-palm-tree.git#15cf1ab37e038771e1ff8005edc46d95f176739f", "dev": true }, + "node_modules/semver": { + "version": "6.3.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz", + "integrity": "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==", + "dev": true, + "bin": { + "semver": "bin/semver.js" + } + }, "node_modules/serialize-javascript": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-6.0.0.tgz", @@ -1481,6 +1490,9 @@ "dev": true, "dependencies": { "is-number": "^7.0.0" + }, + "engines": { + "node": ">=8.0" } }, "node_modules/type-check": { @@ -1537,10 +1549,13 @@ } }, "node_modules/word-wrap": { - "version": "1.2.3", - "resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.3.tgz", - "integrity": "sha512-Hz/mrNwitNRh/HUAtM/VT/5VH+ygD6DV7mYKZAtHOrbs8U7lvPS6xf7EJKMF0uW1KJCl0H701g3ZGus+muE5vQ==", - "dev": true + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.5.tgz", + "integrity": "sha512-BN22B5eaMMI9UMtjrGd5g5eCYPpCPDUy0FJXbYsaT5zYxjFOckS53SQDE3pWkVoWpHXVb3BrYcEN4Twa55B5cA==", + "dev": true, + "engines": { + "node": ">=0.10.0" + } }, "node_modules/workerpool": { "version": "6.2.1", @@ -1818,12 +1833,12 @@ } }, "braces": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", - "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==", + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.3.tgz", + "integrity": "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==", "dev": true, "requires": { - "fill-range": "^7.0.1" + "fill-range": "^7.1.1" } }, "browser-stdout": { @@ -2202,9 +2217,9 @@ } }, "fill-range": { - "version": "7.0.1", - "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", - "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==", + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz", + "integrity": "sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==", "dev": true, "requires": { "to-regex-range": "^5.0.1" @@ -2532,14 +2547,6 @@ "dev": true, "requires": { "semver": "^6.0.0" - }, - "dependencies": { - "semver": { - "version": "6.3.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz", - "integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==", - "dev": true - } } }, "minimatch": { @@ -2808,6 +2815,12 @@ "dev": true, "from": "scaling-palm-tree@github:mjethani/scaling-palm-tree#15cf1ab37e038771e1ff8005edc46d95f176739f" }, + "semver": { + "version": "6.3.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz", + "integrity": "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==", + "dev": true + }, "serialize-javascript": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-6.0.0.tgz", @@ -2935,9 +2948,9 @@ } }, "word-wrap": { - "version": "1.2.3", - "resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.3.tgz", - "integrity": "sha512-Hz/mrNwitNRh/HUAtM/VT/5VH+ygD6DV7mYKZAtHOrbs8U7lvPS6xf7EJKMF0uW1KJCl0H701g3ZGus+muE5vQ==", + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.5.tgz", + "integrity": "sha512-BN22B5eaMMI9UMtjrGd5g5eCYPpCPDUy0FJXbYsaT5zYxjFOckS53SQDE3pWkVoWpHXVb3BrYcEN4Twa55B5cA==", "dev": true }, "workerpool": { diff --git a/src/js/biditrie.js b/src/js/biditrie.js index 06566d2e1ac98..2fa0495062fc6 100644 --- a/src/js/biditrie.js +++ b/src/js/biditrie.js @@ -582,12 +582,12 @@ class BidiTrieContainer { } toSelfie() { - const buf32 = this.buf32.subarray(0, this.buf32[CHAR1_SLOT] + 3 >>> 2); + const buf32 = this.buf32.slice(0, this.buf32[CHAR1_SLOT] + 3 >>> 2); return { buf32, checksum: i32Checksum(buf32) }; } fromSelfie(selfie) { - if ( selfie instanceof Object === false ) { return false; } + if ( typeof selfie !== 'object' || selfie === null ) { return false; } if ( selfie.buf32 instanceof Uint32Array === false ) { return false; } if ( selfie.checksum !== i32Checksum(selfie.buf32) ) { return false; } const byteLength = selfie.buf32.length << 2; diff --git a/src/js/hntrie.js b/src/js/hntrie.js index 4f89a99642633..5885e6e21175d 100644 --- a/src/js/hntrie.js +++ b/src/js/hntrie.js @@ -452,12 +452,12 @@ class HNTrieContainer { } toSelfie() { - const buf32 = this.buf32.subarray(0, this.buf32[CHAR1_SLOT] + 3 >>> 2); + const buf32 = this.buf32.slice(0, this.buf32[CHAR1_SLOT] + 3 >>> 2); return { buf32, checksum: i32Checksum(buf32) }; } fromSelfie(selfie) { - if ( selfie instanceof Object === false ) { return false; } + if ( typeof selfie !== 'object' || selfie === null ) { return false; } if ( selfie.buf32 instanceof Uint32Array === false ) { return false; } if ( selfie.checksum !== i32Checksum(selfie.buf32) ) { return false; } this.needle = ''; diff --git a/src/js/static-net-filtering.js b/src/js/static-net-filtering.js index f5b1841752368..ac4b373df616d 100644 --- a/src/js/static-net-filtering.js +++ b/src/js/static-net-filtering.js @@ -495,7 +495,7 @@ const filterDataReset = ( ) => { filterDataWritePtr = 2; }; const filterDataToSelfie = ( ) => - filterData.subarray(0, filterDataWritePtr); + filterData.slice(0, filterDataWritePtr); const filterDataFromSelfie = selfie => { if ( selfie instanceof Int32Array === false ) { return false; } @@ -3193,7 +3193,7 @@ const urlTokenizer = new (class { } toSelfie() { - return this.knownTokens; + return this.knownTokens.slice(); } fromSelfie(selfie) { @@ -4779,7 +4779,7 @@ StaticNetFilteringEngine.prototype.toSelfie = function() { processedFilterCount: this.processedFilterCount, acceptedCount: this.acceptedCount, discardedCount: this.discardedCount, - bitsToBucket: this.bitsToBucket, + bitsToBucket: new Map(this.bitsToBucket), urlTokenizer: urlTokenizer.toSelfie(), destHNTrieContainer: destHNTrieContainer.toSelfie(), origHNTrieContainer: origHNTrieContainer.toSelfie(), @@ -4789,20 +4789,13 @@ StaticNetFilteringEngine.prototype.toSelfie = function() { }; }; -StaticNetFilteringEngine.prototype.serialize = async function() { - const selfie = []; - const storage = { - put(name, data) { - selfie.push([ name, data ]); - } - }; - await this.toSelfie(storage, ''); - return JSON.stringify(selfie); +StaticNetFilteringEngine.prototype.serialize = function() { + return this.toSelfie(); }; /******************************************************************************/ -StaticNetFilteringEngine.prototype.fromSelfie = async function(selfie) { +StaticNetFilteringEngine.prototype.fromSelfie = function(selfie) { if ( typeof selfie !== 'object' || selfie === null ) { return; } this.reset(); @@ -4835,14 +4828,8 @@ StaticNetFilteringEngine.prototype.fromSelfie = async function(selfie) { return true; }; -StaticNetFilteringEngine.prototype.unserialize = async function(s) { - const selfie = new Map(JSON.parse(s)); - const storage = { - async get(name) { - return { content: selfie.get(name) }; - } - }; - return this.fromSelfie(storage, ''); +StaticNetFilteringEngine.prototype.unserialize = function(selfie) { + return this.fromSelfie(selfie); }; /******************************************************************************/