Add serialization API to StaticNetFilteringEngine (#3806)

This commit is contained in:
Manish Jethani 2021-08-10 02:22:41 +05:30 committed by GitHub
parent b54bf554a8
commit d9adf5a6fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 19 deletions

View File

@ -182,6 +182,25 @@ async function useLists(lists, options = {}) {
/******************************************************************************/ /******************************************************************************/
class MockStorage {
constructor(serialized) {
this.map = new Map(serialized);
}
async put(assetKey, content) {
this.map.set(assetKey, content);
return ({ assetKey, content });
}
async get(assetKey) {
return ({ assetKey, content: this.map.get(assetKey) });
}
*[Symbol.iterator]() {
yield* this.map;
}
}
const fctx = new FilteringContext(); const fctx = new FilteringContext();
let snfeInstance = null; let snfeInstance = null;
@ -194,7 +213,7 @@ class StaticNetFilteringEngine {
} }
async useLists(lists) { async useLists(lists) {
return useLists(lists); await useLists(lists);
} }
matchRequest(details) { matchRequest(details) {
@ -213,7 +232,18 @@ class StaticNetFilteringEngine {
return compileList(...args); return compileList(...args);
} }
static async create({ noPSL } = {}) { async serialize() {
const storage = new MockStorage();
await snfe.toSelfie(storage, 'path');
return JSON.stringify([...storage]);
}
async deserialize(serialized) {
const storage = new MockStorage(JSON.parse(serialized));
await snfe.fromSelfie(storage, 'path');
}
static async create({ noPSL = false } = {}) {
const instance = new StaticNetFilteringEngine(); const instance = new StaticNetFilteringEngine();
if ( noPSL !== true && !pslInit() ) { if ( noPSL !== true && !pslInit() ) {

View File

@ -42,23 +42,7 @@ function fetch(listName) {
}); });
} }
async function main() { function runTests(engine) {
try {
const result = await enableWASM();
if ( result !== true ) {
console.log('Failed to enable all WASM code paths');
}
} catch(ex) {
console.log(ex);
}
const engine = await StaticNetFilteringEngine.create();
await engine.useLists([
fetch('easylist').then(raw => ({ name: 'easylist', raw })),
fetch('easyprivacy').then(raw => ({ name: 'easyprivacy', raw })),
]);
let result = 0; let result = 0;
// Tests // Tests
@ -91,6 +75,35 @@ async function main() {
if ( result !== 0 ) { if ( result !== 0 ) {
console.log(engine.toLogData()); console.log(engine.toLogData());
} }
}
async function main() {
try {
const result = await enableWASM();
if ( result !== true ) {
console.log('Failed to enable all WASM code paths');
}
} catch(ex) {
console.log(ex);
}
const engine = await StaticNetFilteringEngine.create();
await engine.useLists([
fetch('easylist').then(raw => ({ name: 'easylist', raw })),
fetch('easyprivacy').then(raw => ({ name: 'easyprivacy', raw })),
]);
runTests(engine);
const serialized = await engine.serialize();
engine.useLists([]);
runTests(engine);
await engine.deserialize(serialized);
runTests(engine);
process.exit(); process.exit();
} }