From 86aeae75dfd125f6e4cb7a22521b4badaf54837d Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sun, 6 Oct 2024 17:03:18 -0400 Subject: [PATCH] Further fix npm documentation, minor code review --- platform/nodejs/README.md | 18 ++++++++++++++---- platform/nodejs/index.js | 7 +++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/platform/nodejs/README.md b/platform/nodejs/README.md index bd5775abb..db6c5d775 100644 --- a/platform/nodejs/README.md +++ b/platform/nodejs/README.md @@ -90,10 +90,20 @@ if ( snfe.matchRequest({ } ``` -It is possible to pre-parse filter lists and save the intermediate results for -later use -- useful to speed up the loading of filter lists. This will be -documented eventually, but if you feel adventurous, you can look at the code -and use this capability now if you figure out the details. +Once all the filter lists are loaded into the static network filtering engine, +you can serialize the content of the engine into a JS string: + +```js +const serializedData = await snfe.serialize(); +``` + +You can save and later use that JS string to fast-load the content of the +static network filtering engine without having to parse and compile the lists: + +```js +const snfe = await StaticNetFilteringEngine.create(); +await snfe.deserialize(serializedData); +``` --- diff --git a/platform/nodejs/index.js b/platform/nodejs/index.js index fbc17da85..d50fb4e19 100644 --- a/platform/nodejs/index.js +++ b/platform/nodejs/index.js @@ -182,8 +182,7 @@ async function useLists(lists, options = {}) { // Populate filtering engine with resolved filter lists const promises = []; for ( const list of lists ) { - const promise = list instanceof Promise ? list : Promise.resolve(list); - promises.push(promise.then(list => consumeList(list))); + promises.push(Promise.resolve(list).then(list => consumeList(list)); } useLists.promise = Promise.all(promises); @@ -249,12 +248,12 @@ class StaticNetFilteringEngine { return compileList(...args); } - serialize() { + async serialize() { const data = snfe.serialize(); return s14e.serialize(data, { compress: true }); } - deserialize(serialized) { + async deserialize(serialized) { const data = s14e.deserialize(serialized); return snfe.unserialize(data); }