Skip to content

Commit

Permalink
Fix npm test suite
Browse files Browse the repository at this point in the history
Related commits:
- 02cba63331
- 41693407b2
  • Loading branch information
gorhill committed Oct 6, 2024
1 parent 160d7f3 commit 818cb2d
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.bak
*.pem
__pycache__/
node_modules/
/dist/build/
/tmp/
38 changes: 38 additions & 0 deletions platform/nodejs/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -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" ]
}
}
51 changes: 31 additions & 20 deletions platform/nodejs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,40 @@
Home: https://github.com/gorhill/uBlock
*/

/* globals WebAssembly */

'use strict';
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 { dirname, resolve } from 'path';
import { domainToASCII, fileURLToPath } from 'url';

const __dirname = dirname(fileURLToPath(import.meta.url));
import snfe from './js/static-net-filtering.js';

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 sfp from './js/static-filtering-parser.js';
const __dirname = dirname(fileURLToPath(import.meta.url));

import {
CompiledListReader,
CompiledListWriter,
} from './js/static-filtering-io.js';
// https://stackoverflow.com/questions/69187442/const-utf8encoder-new-textencoder-in-node-js
globalThis.TextDecoder = TextDecoder;
globalThis.TextEncoder = TextEncoder;

/******************************************************************************/

Expand Down Expand Up @@ -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 } = {}) {
Expand Down
2 changes: 1 addition & 1 deletion platform/npm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
35 changes: 28 additions & 7 deletions src/js/s14e-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
};

Expand Down Expand Up @@ -407,7 +428,7 @@ const denseArrayBufferToStr = (arrbuf, details) => {
}
}
}
return textDecoder.decode(output);
return textCodec.decode(output);
};

const BASE88_POW1 = NUMSAFECHARS;
Expand Down Expand Up @@ -489,7 +510,7 @@ const sparseArrayBufferToStr = (arrbuf, details) => {
uint8out[j++] = SEPARATORCHARCODE;
}
}
return textDecoder.decode(uint8out);
return textCodec.decode(uint8out);
};

const sparseArrayBufferFromStr = (sparseStr, arrbuf) => {
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions tools/make-nodejs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 818cb2d

Please sign in to comment.