From 818cb2d801d9a486ee010133a67a824856951ee6 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sun, 6 Oct 2024 11:15:08 -0400 Subject: [PATCH] Fix npm test suite Related commits: - https://github.com/gorhill/uBlock/commit/02cba63331 - https://github.com/gorhill/uBlock/commit/41693407b2 --- .gitignore | 1 + platform/nodejs/.eslintrc.json | 38 ++++++++++++++++++++++++ platform/nodejs/index.js | 53 ++++++++++++++++++++-------------- platform/npm/package.json | 2 +- src/js/s14e-serializer.js | 35 +++++++++++++++++----- tools/make-nodejs.sh | 1 + 6 files changed, 101 insertions(+), 29 deletions(-) create mode 100644 platform/nodejs/.eslintrc.json diff --git a/.gitignore b/.gitignore index 1f064b847..2c0e57151 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ *.bak *.pem __pycache__/ +node_modules/ /dist/build/ /tmp/ diff --git a/platform/nodejs/.eslintrc.json b/platform/nodejs/.eslintrc.json new file mode 100644 index 000000000..5f7c6b58c --- /dev/null +++ b/platform/nodejs/.eslintrc.json @@ -0,0 +1,38 @@ +{ + "root": true, + "env": { + "es2021": true, + "node": true + }, + "extends": "eslint:recommended", + "parserOptions": { + "ecmaVersion": 12, + "sourceType": "module" + }, + "rules": { + "eqeqeq": [ "warn", "always" ], + "indent": [ + "warn", + 4, + { + "ArrayExpression": "first", + "CallExpression": { "arguments": "first" }, + "MemberExpression": "off", + "ObjectExpression": "off", + "ignoreComments": true, + "ignoredNodes": [ + "AssignmentExpression:has(Literal)" + ] + } + ], + "getter-return": "off", + "no-control-regex": "off", + "no-empty": [ "error", { "allowEmptyCatch": true } ], + "no-promise-executor-return": [ "error" ], + "no-template-curly-in-string": [ "error" ], + "no-unreachable-loop": [ "error" ], + "no-useless-backreference": [ "error" ], + "no-useless-escape": "off", + "require-atomic-updates": [ "warn" ] + } +} diff --git a/platform/nodejs/index.js b/platform/nodejs/index.js index 7bc808e89..fbc17da85 100644 --- a/platform/nodejs/index.js +++ b/platform/nodejs/index.js @@ -19,31 +19,40 @@ Home: https://github.com/gorhill/uBlock */ -/* globals WebAssembly */ - -'use strict'; - -/******************************************************************************/ - -import { createRequire } from 'module'; - -import { readFileSync } from 'fs'; -import { dirname, resolve } from 'path'; -import { domainToASCII, fileURLToPath } from 'url'; - -const __dirname = dirname(fileURLToPath(import.meta.url)); - -import publicSuffixList from './lib/publicsuffixlist/publicsuffixlist.js'; - -import snfe from './js/static-net-filtering.js'; -import { FilteringContext } from './js/filtering-context.js'; -import { LineIterator } from './js/text-utils.js'; +import * as s14e from './js/s14e-serializer.js'; import * as sfp from './js/static-filtering-parser.js'; import { CompiledListReader, CompiledListWriter, } from './js/static-filtering-io.js'; +import { + TextDecoder, + TextEncoder, +} from 'util'; +import { + dirname, + resolve +} from 'path'; +import { + domainToASCII, + fileURLToPath +} from 'url'; + +import { FilteringContext } from './js/filtering-context.js'; +import { LineIterator } from './js/text-utils.js'; +import { createRequire } from 'module'; +import publicSuffixList from './lib/publicsuffixlist/publicsuffixlist.js'; +import { readFileSync } from 'fs'; +import snfe from './js/static-net-filtering.js'; + +/******************************************************************************/ + +const __dirname = dirname(fileURLToPath(import.meta.url)); + +// https://stackoverflow.com/questions/69187442/const-utf8encoder-new-textencoder-in-node-js +globalThis.TextDecoder = TextDecoder; +globalThis.TextEncoder = TextEncoder; /******************************************************************************/ @@ -241,11 +250,13 @@ class StaticNetFilteringEngine { } serialize() { - return snfe.serialize(); + const data = snfe.serialize(); + return s14e.serialize(data, { compress: true }); } deserialize(serialized) { - return snfe.unserialize(serialized); + const data = s14e.deserialize(serialized); + return snfe.unserialize(data); } static async create({ noPSL = false } = {}) { diff --git a/platform/npm/package.json b/platform/npm/package.json index a505449e6..a65faffd3 100644 --- a/platform/npm/package.json +++ b/platform/npm/package.json @@ -31,7 +31,7 @@ }, "homepage": "https://github.com/gorhill/uBlock#readme", "engines": { - "node": ">=14.0.0", + "node": ">=18.0.0", "npm": ">=6.14.4" }, "devDependencies": { diff --git a/src/js/s14e-serializer.js b/src/js/s14e-serializer.js index 8ef715a23..98f0d9cc2 100644 --- a/src/js/s14e-serializer.js +++ b/src/js/s14e-serializer.js @@ -249,8 +249,29 @@ const toArrayBufferViewConstructor = { /******************************************************************************/ -const textDecoder = new TextDecoder(); -const textEncoder = new TextEncoder(); +const textCodec = { + decoder: null, + encoder: null, + decode(...args) { + if ( this.decoder === null ) { + this.decoder = new globalThis.TextDecoder(); + } + return this.decoder.decode(...args); + }, + encode(...args) { + if ( this.encoder === null ) { + this.encoder = new globalThis.TextEncoder(); + } + return this.encoder.encode(...args); + }, + encodeInto(...args) { + if ( this.encoder === null ) { + this.encoder = new globalThis.TextEncoder(); + } + return this.encoder.encodeInto(...args); + }, +}; + const isInteger = Number.isInteger; const writeRefs = new Map(); @@ -269,7 +290,7 @@ const uint8InputFromAsciiStr = s => { if ( uint8Input === null || uint8Input.length < s.length ) { uint8Input = new Uint8Array(s.length + 0x03FF & ~0x03FF); } - textEncoder.encodeInto(s, uint8Input); + textCodec.encodeInto(s, uint8Input); return uint8Input; }; @@ -407,7 +428,7 @@ const denseArrayBufferToStr = (arrbuf, details) => { } } } - return textDecoder.decode(output); + return textCodec.decode(output); }; const BASE88_POW1 = NUMSAFECHARS; @@ -489,7 +510,7 @@ const sparseArrayBufferToStr = (arrbuf, details) => { uint8out[j++] = SEPARATORCHARCODE; } } - return textDecoder.decode(uint8out); + return textCodec.decode(uint8out); }; const sparseArrayBufferFromStr = (sparseStr, arrbuf) => { @@ -1060,7 +1081,7 @@ export const serialize = (data, options = {}) => { writeBuffer.length = 0; if ( shouldCompress(s, options) === false ) { return s; } const lz4Util = new LZ4BlockJS(); - const uint8ArrayBefore = textEncoder.encode(s); + const uint8ArrayBefore = textCodec.encode(s); const uint8ArrayAfter = lz4Util.encode(uint8ArrayBefore, 0); const lz4 = { size: uint8ArrayBefore.length, @@ -1087,7 +1108,7 @@ export const deserialize = s => { readStr = ''; const lz4Util = new LZ4BlockJS(); const uint8ArrayAfter = lz4Util.decode(lz4.data, 0, lz4.size); - s = textDecoder.decode(new Uint8Array(uint8ArrayAfter)); + s = textCodec.decode(new Uint8Array(uint8ArrayAfter)); } if ( s.startsWith(MAGICPREFIX) === false ) { return; } refCounter = 1; diff --git a/tools/make-nodejs.sh b/tools/make-nodejs.sh index 1e38ba143..b17a0a469 100755 --- a/tools/make-nodejs.sh +++ b/tools/make-nodejs.sh @@ -14,6 +14,7 @@ cp src/js/filtering-context.js $DES/js cp src/js/hnswitches.js $DES/js cp src/js/hntrie.js $DES/js cp src/js/redirect-resources.js $DES/js +cp src/js/s14e-serializer.js $DES/js cp src/js/static-dnr-filtering.js $DES/js cp src/js/static-filtering-parser.js $DES/js cp src/js/static-net-filtering.js $DES/js