Skip to content

Commit 4169340

Browse files
committed
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().
1 parent 3b53d8e commit 4169340

File tree

5 files changed

+65
-64
lines changed

5 files changed

+65
-64
lines changed

platform/nodejs/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ class StaticNetFilteringEngine {
218218
}
219219

220220
filterQuery(details) {
221+
fctx.redirectURL = undefined;
221222
const directives = snfe.filterQuery(fctx.fromDetails(details));
222223
if ( directives === undefined ) { return; }
223224
return { redirectURL: fctx.redirectURL, directives };

platform/npm/package-lock.json

Lines changed: 52 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/biditrie.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,12 +582,12 @@ class BidiTrieContainer {
582582
}
583583

584584
toSelfie() {
585-
const buf32 = this.buf32.subarray(0, this.buf32[CHAR1_SLOT] + 3 >>> 2);
585+
const buf32 = this.buf32.slice(0, this.buf32[CHAR1_SLOT] + 3 >>> 2);
586586
return { buf32, checksum: i32Checksum(buf32) };
587587
}
588588

589589
fromSelfie(selfie) {
590-
if ( selfie instanceof Object === false ) { return false; }
590+
if ( typeof selfie !== 'object' || selfie === null ) { return false; }
591591
if ( selfie.buf32 instanceof Uint32Array === false ) { return false; }
592592
if ( selfie.checksum !== i32Checksum(selfie.buf32) ) { return false; }
593593
const byteLength = selfie.buf32.length << 2;

src/js/hntrie.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,12 +452,12 @@ class HNTrieContainer {
452452
}
453453

454454
toSelfie() {
455-
const buf32 = this.buf32.subarray(0, this.buf32[CHAR1_SLOT] + 3 >>> 2);
455+
const buf32 = this.buf32.slice(0, this.buf32[CHAR1_SLOT] + 3 >>> 2);
456456
return { buf32, checksum: i32Checksum(buf32) };
457457
}
458458

459459
fromSelfie(selfie) {
460-
if ( selfie instanceof Object === false ) { return false; }
460+
if ( typeof selfie !== 'object' || selfie === null ) { return false; }
461461
if ( selfie.buf32 instanceof Uint32Array === false ) { return false; }
462462
if ( selfie.checksum !== i32Checksum(selfie.buf32) ) { return false; }
463463
this.needle = '';

src/js/static-net-filtering.js

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ const filterDataReset = ( ) => {
495495
filterDataWritePtr = 2;
496496
};
497497
const filterDataToSelfie = ( ) =>
498-
filterData.subarray(0, filterDataWritePtr);
498+
filterData.slice(0, filterDataWritePtr);
499499

500500
const filterDataFromSelfie = selfie => {
501501
if ( selfie instanceof Int32Array === false ) { return false; }
@@ -3193,7 +3193,7 @@ const urlTokenizer = new (class {
31933193
}
31943194

31953195
toSelfie() {
3196-
return this.knownTokens;
3196+
return this.knownTokens.slice();
31973197
}
31983198

31993199
fromSelfie(selfie) {
@@ -4779,7 +4779,7 @@ StaticNetFilteringEngine.prototype.toSelfie = function() {
47794779
processedFilterCount: this.processedFilterCount,
47804780
acceptedCount: this.acceptedCount,
47814781
discardedCount: this.discardedCount,
4782-
bitsToBucket: this.bitsToBucket,
4782+
bitsToBucket: new Map(this.bitsToBucket),
47834783
urlTokenizer: urlTokenizer.toSelfie(),
47844784
destHNTrieContainer: destHNTrieContainer.toSelfie(),
47854785
origHNTrieContainer: origHNTrieContainer.toSelfie(),
@@ -4789,20 +4789,13 @@ StaticNetFilteringEngine.prototype.toSelfie = function() {
47894789
};
47904790
};
47914791

4792-
StaticNetFilteringEngine.prototype.serialize = async function() {
4793-
const selfie = [];
4794-
const storage = {
4795-
put(name, data) {
4796-
selfie.push([ name, data ]);
4797-
}
4798-
};
4799-
await this.toSelfie(storage, '');
4800-
return JSON.stringify(selfie);
4792+
StaticNetFilteringEngine.prototype.serialize = function() {
4793+
return this.toSelfie();
48014794
};
48024795

48034796
/******************************************************************************/
48044797

4805-
StaticNetFilteringEngine.prototype.fromSelfie = async function(selfie) {
4798+
StaticNetFilteringEngine.prototype.fromSelfie = function(selfie) {
48064799
if ( typeof selfie !== 'object' || selfie === null ) { return; }
48074800

48084801
this.reset();
@@ -4835,14 +4828,8 @@ StaticNetFilteringEngine.prototype.fromSelfie = async function(selfie) {
48354828
return true;
48364829
};
48374830

4838-
StaticNetFilteringEngine.prototype.unserialize = async function(s) {
4839-
const selfie = new Map(JSON.parse(s));
4840-
const storage = {
4841-
async get(name) {
4842-
return { content: selfie.get(name) };
4843-
}
4844-
};
4845-
return this.fromSelfie(storage, '');
4831+
StaticNetFilteringEngine.prototype.unserialize = function(selfie) {
4832+
return this.fromSelfie(selfie);
48464833
};
48474834

48484835
/******************************************************************************/

0 commit comments

Comments
 (0)