Use `require` instead of `fs`

Related feedback:
- https://github.com/uBlockOrigin/uBlock-issues/issues/1664#issuecomment-888332409
This commit is contained in:
Raymond Hill 2021-07-28 10:38:24 -04:00
parent 22022f636f
commit e1222d1643
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
3 changed files with 25 additions and 21 deletions

View File

@ -23,6 +23,8 @@
/******************************************************************************/ /******************************************************************************/
import { createRequire } from 'module';
import './lib/punycode.js'; import './lib/punycode.js';
import './lib/publicsuffixlist/publicsuffixlist.js'; import './lib/publicsuffixlist/publicsuffixlist.js';
@ -89,8 +91,12 @@ function enableWASM(path) {
function pslInit(raw) { function pslInit(raw) {
if ( typeof raw !== 'string' || raw.trim() === '' ) { if ( typeof raw !== 'string' || raw.trim() === '' ) {
console.info('Unable to populate public suffix list'); const require = createRequire(import.meta.url); // jshint ignore:line
return; raw = require('./data/effective_tld_names.json');
if ( typeof raw !== 'string' || raw.trim() === '' ) {
console.info('Unable to populate public suffix list');
return;
}
} }
globals.publicSuffixList.parse(raw, globals.punycode.toASCII); globals.publicSuffixList.parse(raw, globals.punycode.toASCII);
console.info('Public suffix list populated'); console.info('Public suffix list populated');

View File

@ -25,7 +25,7 @@
/******************************************************************************/ /******************************************************************************/
import { readFile } from 'fs'; import { createRequire } from 'module';
import { import {
FilteringContext, FilteringContext,
@ -35,15 +35,10 @@ import {
/******************************************************************************/ /******************************************************************************/
function fetch(path) { function fetch(listName) {
return new Promise((resolve, reject) => { return new Promise(resolve => {
readFile(path, 'utf8', (err, data) => { const require = createRequire(import.meta.url); // jshint ignore:line
if ( err ) { resolve(require(`./data/${listName}.json`));
reject(err);
} else {
resolve(data);
}
});
}); });
} }
@ -56,13 +51,11 @@ function fetch(path) {
} }
*/ */
await fetch('./data/effective_tld_names.dat').then(pslRaw => { pslInit();
pslInit(pslRaw);
});
const snfe = await Promise.all([ const snfe = await Promise.all([
fetch('./data/easylist.txt'), fetch('easylist'),
fetch('./data/easyprivacy.txt'), fetch('easyprivacy'),
]).then(rawLists => { ]).then(rawLists => {
return restart([ return restart([
{ name: 'easylist', raw: rawLists[0] }, { name: 'easylist', raw: rawLists[0] },

View File

@ -21,11 +21,16 @@ cp -R src/lib/punycode.js $DES/lib/
cp -R src/lib/publicsuffixlist $DES/lib/ cp -R src/lib/publicsuffixlist $DES/lib/
cp -R src/lib/regexanalyzer $DES/lib/ cp -R src/lib/regexanalyzer $DES/lib/
# https://github.com/uBlockOrigin/uBlock-issues/issues/1664#issuecomment-888332409
THIRDPARTY=../uAssets/thirdparties/publicsuffix.org
mkdir -p $DES/data mkdir -p $DES/data
cp -R ../uAssets/thirdparties/publicsuffix.org/list/* \ node -pe "JSON.stringify(fs.readFileSync('$THIRDPARTY/list/effective_tld_names.dat', 'utf8'))" \
$DES/data > $DES/data/effective_tld_names.json
cp -R ../uAssets/thirdparties/easylist-downloads.adblockplus.org/* \ THIRDPARTY=../uAssets/thirdparties/easylist-downloads.adblockplus.org
$DES/data node -pe "JSON.stringify(fs.readFileSync('$THIRDPARTY/easylist.txt', 'utf8'))" \
> $DES/data/easylist.json
node -pe "JSON.stringify(fs.readFileSync('$THIRDPARTY/easyprivacy.txt', 'utf8'))" \
> $DES/data/easyprivacy.json
cp platform/nodejs/*.js $DES/ cp platform/nodejs/*.js $DES/
cp platform/nodejs/*.json $DES/ cp platform/nodejs/*.json $DES/