mirror of https://github.com/gorhill/uBlock.git
Add serialization API to StaticNetFilteringEngine (#3806)
This commit is contained in:
parent
b54bf554a8
commit
d9adf5a6fb
|
@ -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() ) {
|
||||||
|
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue