Skip to content

Commit

Permalink
Fix npm test suite
Browse files Browse the repository at this point in the history
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().
  • Loading branch information
gorhill committed Oct 5, 2024
1 parent 3b53d8e commit 4169340
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 64 deletions.
1 change: 1 addition & 0 deletions platform/nodejs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
91 changes: 52 additions & 39 deletions platform/npm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/js/biditrie.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/js/hntrie.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down
29 changes: 8 additions & 21 deletions src/js/static-net-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -3193,7 +3193,7 @@ const urlTokenizer = new (class {
}

toSelfie() {
return this.knownTokens;
return this.knownTokens.slice();
}

fromSelfie(selfie) {
Expand Down Expand Up @@ -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(),
Expand All @@ -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();
Expand Down Expand Up @@ -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);
};

/******************************************************************************/
Expand Down

0 comments on commit 4169340

Please sign in to comment.