From 46c6ff8708419614201d3e477b20f74c2c275afd Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sun, 1 Aug 2021 12:58:21 -0400 Subject: [PATCH] Make the nodejs package load a serialized version of the PSL Related discussion: - https://github.com/cliqz-oss/adblocker/pull/2091#issuecomment-890545926 --- platform/nodejs/main.js | 15 ++++----------- tools/make-nodejs.sh | 4 ++-- tools/make-psl.json.js | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 13 deletions(-) create mode 100755 tools/make-psl.json.js diff --git a/platform/nodejs/main.js b/platform/nodejs/main.js index 171ec8636..8c9bbe998 100644 --- a/platform/nodejs/main.js +++ b/platform/nodejs/main.js @@ -25,7 +25,6 @@ import { createRequire } from 'module'; -import './lib/punycode.js'; import './lib/publicsuffixlist/publicsuffixlist.js'; import globals from './js/globals.js'; @@ -98,16 +97,10 @@ async function enableWASM() { return false; } -function pslInit(raw) { - if ( typeof raw !== 'string' || raw.trim() === '' ) { - const require = createRequire(import.meta.url); // jshint ignore:line - raw = require('./data/effective_tld_names.json'); - if ( typeof raw !== 'string' || raw.trim() === '' ) { - console.error('Unable to populate public suffix list'); - return; - } - } - globals.publicSuffixList.parse(raw, globals.punycode.toASCII); +function pslInit() { + const require = createRequire(import.meta.url); // jshint ignore:line + const json = require('./data/publicsuffixlist.json'); + globals.publicSuffixList.fromSelfie(json); return globals.publicSuffixList; } diff --git a/tools/make-nodejs.sh b/tools/make-nodejs.sh index 9404d2c2d..6d5bfff2f 100755 --- a/tools/make-nodejs.sh +++ b/tools/make-nodejs.sh @@ -39,13 +39,13 @@ UASSETS=submodules/uAssets # https://github.com/uBlockOrigin/uBlock-issues/issues/1664#issuecomment-888332409 THIRDPARTY=$UASSETS/thirdparties/publicsuffix.org mkdir -p $DES/data -node -pe "JSON.stringify(fs.readFileSync('$THIRDPARTY/list/effective_tld_names.dat', 'utf8'))" \ - > $DES/data/effective_tld_names.json THIRDPARTY=$UASSETS/thirdparties/easylist-downloads.adblockplus.org 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 +# https://github.com/cliqz-oss/adblocker/pull/2091#issuecomment-890545926 +node ./tools/make-psl.json.js cp platform/nodejs/*.js $DES/ cp platform/nodejs/*.json $DES/ diff --git a/tools/make-psl.json.js b/tools/make-psl.json.js new file mode 100755 index 000000000..0228492bc --- /dev/null +++ b/tools/make-psl.json.js @@ -0,0 +1,40 @@ +/******************************************************************************* + + uBlock Origin - a browser extension to block requests. + Copyright (C) 2014-present Raymond Hill + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see {http://www.gnu.org/licenses/}. + + Home: https://github.com/gorhill/uBlock +*/ + +/* globals process, require */ + +'use strict'; + +const fs = require('fs'); +const raw = fs.readFileSync('./submodules/uAssets/thirdparties/publicsuffix.org/list/effective_tld_names.dat', 'utf8'); +const punycode = require('../src/lib/punycode.js'); +const psl = require('../src/lib/publicsuffixlist/publicsuffixlist.js'); + +if ( typeof raw !== 'string' || raw.trim() === '' ) { + return process.exit('Unable to populate public suffix list'); +} + +psl.parse(raw, punycode.toASCII); + +fs.writeFileSync( + './dist/build/uBlock0.nodejs/data/publicsuffixlist.json', + JSON.stringify(psl.toSelfie()) +);