Skip to content

Commit 818cb2d

Browse files
committed
Fix npm test suite
Related commits: - 02cba63331 - 41693407b2
1 parent 160d7f3 commit 818cb2d

File tree

6 files changed

+100
-28
lines changed

6 files changed

+100
-28
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
*.bak
22
*.pem
33
__pycache__/
4+
node_modules/
45
/dist/build/
56
/tmp/

platform/nodejs/.eslintrc.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"root": true,
3+
"env": {
4+
"es2021": true,
5+
"node": true
6+
},
7+
"extends": "eslint:recommended",
8+
"parserOptions": {
9+
"ecmaVersion": 12,
10+
"sourceType": "module"
11+
},
12+
"rules": {
13+
"eqeqeq": [ "warn", "always" ],
14+
"indent": [
15+
"warn",
16+
4,
17+
{
18+
"ArrayExpression": "first",
19+
"CallExpression": { "arguments": "first" },
20+
"MemberExpression": "off",
21+
"ObjectExpression": "off",
22+
"ignoreComments": true,
23+
"ignoredNodes": [
24+
"AssignmentExpression:has(Literal)"
25+
]
26+
}
27+
],
28+
"getter-return": "off",
29+
"no-control-regex": "off",
30+
"no-empty": [ "error", { "allowEmptyCatch": true } ],
31+
"no-promise-executor-return": [ "error" ],
32+
"no-template-curly-in-string": [ "error" ],
33+
"no-unreachable-loop": [ "error" ],
34+
"no-useless-backreference": [ "error" ],
35+
"no-useless-escape": "off",
36+
"require-atomic-updates": [ "warn" ]
37+
}
38+
}

platform/nodejs/index.js

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,40 @@
1919
Home: https://github.com/gorhill/uBlock
2020
*/
2121

22-
/* globals WebAssembly */
23-
24-
'use strict';
22+
import * as s14e from './js/s14e-serializer.js';
23+
import * as sfp from './js/static-filtering-parser.js';
2524

26-
/******************************************************************************/
25+
import {
26+
CompiledListReader,
27+
CompiledListWriter,
28+
} from './js/static-filtering-io.js';
29+
import {
30+
TextDecoder,
31+
TextEncoder,
32+
} from 'util';
33+
import {
34+
dirname,
35+
resolve
36+
} from 'path';
37+
import {
38+
domainToASCII,
39+
fileURLToPath
40+
} from 'url';
2741

42+
import { FilteringContext } from './js/filtering-context.js';
43+
import { LineIterator } from './js/text-utils.js';
2844
import { createRequire } from 'module';
29-
45+
import publicSuffixList from './lib/publicsuffixlist/publicsuffixlist.js';
3046
import { readFileSync } from 'fs';
31-
import { dirname, resolve } from 'path';
32-
import { domainToASCII, fileURLToPath } from 'url';
33-
34-
const __dirname = dirname(fileURLToPath(import.meta.url));
47+
import snfe from './js/static-net-filtering.js';
3548

36-
import publicSuffixList from './lib/publicsuffixlist/publicsuffixlist.js';
49+
/******************************************************************************/
3750

38-
import snfe from './js/static-net-filtering.js';
39-
import { FilteringContext } from './js/filtering-context.js';
40-
import { LineIterator } from './js/text-utils.js';
41-
import * as sfp from './js/static-filtering-parser.js';
51+
const __dirname = dirname(fileURLToPath(import.meta.url));
4252

43-
import {
44-
CompiledListReader,
45-
CompiledListWriter,
46-
} from './js/static-filtering-io.js';
53+
// https://stackoverflow.com/questions/69187442/const-utf8encoder-new-textencoder-in-node-js
54+
globalThis.TextDecoder = TextDecoder;
55+
globalThis.TextEncoder = TextEncoder;
4756

4857
/******************************************************************************/
4958

@@ -241,11 +250,13 @@ class StaticNetFilteringEngine {
241250
}
242251

243252
serialize() {
244-
return snfe.serialize();
253+
const data = snfe.serialize();
254+
return s14e.serialize(data, { compress: true });
245255
}
246256

247257
deserialize(serialized) {
248-
return snfe.unserialize(serialized);
258+
const data = s14e.deserialize(serialized);
259+
return snfe.unserialize(data);
249260
}
250261

251262
static async create({ noPSL = false } = {}) {

platform/npm/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
},
3232
"homepage": "https://github.com/gorhill/uBlock#readme",
3333
"engines": {
34-
"node": ">=14.0.0",
34+
"node": ">=18.0.0",
3535
"npm": ">=6.14.4"
3636
},
3737
"devDependencies": {

src/js/s14e-serializer.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,29 @@ const toArrayBufferViewConstructor = {
249249

250250
/******************************************************************************/
251251

252-
const textDecoder = new TextDecoder();
253-
const textEncoder = new TextEncoder();
252+
const textCodec = {
253+
decoder: null,
254+
encoder: null,
255+
decode(...args) {
256+
if ( this.decoder === null ) {
257+
this.decoder = new globalThis.TextDecoder();
258+
}
259+
return this.decoder.decode(...args);
260+
},
261+
encode(...args) {
262+
if ( this.encoder === null ) {
263+
this.encoder = new globalThis.TextEncoder();
264+
}
265+
return this.encoder.encode(...args);
266+
},
267+
encodeInto(...args) {
268+
if ( this.encoder === null ) {
269+
this.encoder = new globalThis.TextEncoder();
270+
}
271+
return this.encoder.encodeInto(...args);
272+
},
273+
};
274+
254275
const isInteger = Number.isInteger;
255276

256277
const writeRefs = new Map();
@@ -269,7 +290,7 @@ const uint8InputFromAsciiStr = s => {
269290
if ( uint8Input === null || uint8Input.length < s.length ) {
270291
uint8Input = new Uint8Array(s.length + 0x03FF & ~0x03FF);
271292
}
272-
textEncoder.encodeInto(s, uint8Input);
293+
textCodec.encodeInto(s, uint8Input);
273294
return uint8Input;
274295
};
275296

@@ -407,7 +428,7 @@ const denseArrayBufferToStr = (arrbuf, details) => {
407428
}
408429
}
409430
}
410-
return textDecoder.decode(output);
431+
return textCodec.decode(output);
411432
};
412433

413434
const BASE88_POW1 = NUMSAFECHARS;
@@ -489,7 +510,7 @@ const sparseArrayBufferToStr = (arrbuf, details) => {
489510
uint8out[j++] = SEPARATORCHARCODE;
490511
}
491512
}
492-
return textDecoder.decode(uint8out);
513+
return textCodec.decode(uint8out);
493514
};
494515

495516
const sparseArrayBufferFromStr = (sparseStr, arrbuf) => {
@@ -1060,7 +1081,7 @@ export const serialize = (data, options = {}) => {
10601081
writeBuffer.length = 0;
10611082
if ( shouldCompress(s, options) === false ) { return s; }
10621083
const lz4Util = new LZ4BlockJS();
1063-
const uint8ArrayBefore = textEncoder.encode(s);
1084+
const uint8ArrayBefore = textCodec.encode(s);
10641085
const uint8ArrayAfter = lz4Util.encode(uint8ArrayBefore, 0);
10651086
const lz4 = {
10661087
size: uint8ArrayBefore.length,
@@ -1087,7 +1108,7 @@ export const deserialize = s => {
10871108
readStr = '';
10881109
const lz4Util = new LZ4BlockJS();
10891110
const uint8ArrayAfter = lz4Util.decode(lz4.data, 0, lz4.size);
1090-
s = textDecoder.decode(new Uint8Array(uint8ArrayAfter));
1111+
s = textCodec.decode(new Uint8Array(uint8ArrayAfter));
10911112
}
10921113
if ( s.startsWith(MAGICPREFIX) === false ) { return; }
10931114
refCounter = 1;

tools/make-nodejs.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ cp src/js/filtering-context.js $DES/js
1414
cp src/js/hnswitches.js $DES/js
1515
cp src/js/hntrie.js $DES/js
1616
cp src/js/redirect-resources.js $DES/js
17+
cp src/js/s14e-serializer.js $DES/js
1718
cp src/js/static-dnr-filtering.js $DES/js
1819
cp src/js/static-filtering-parser.js $DES/js
1920
cp src/js/static-net-filtering.js $DES/js

0 commit comments

Comments
 (0)