Convert publicsuffixlist.js into an ES module (#3846)

This commit is contained in:
Manish Jethani 2021-08-23 19:12:27 +05:30 committed by GitHub
parent ba83c21354
commit 9761b02c79
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 30 additions and 55 deletions

View file

@ -23,10 +23,9 @@
/******************************************************************************/ /******************************************************************************/
import './lib/publicsuffixlist/publicsuffixlist.js'; import publicSuffixList from './lib/publicsuffixlist/publicsuffixlist.js';
import punycode from './lib/punycode.js'; import punycode from './lib/punycode.js';
import globals from './js/globals.js';
import staticNetFilteringEngine from './js/static-net-filtering.js'; import staticNetFilteringEngine from './js/static-net-filtering.js';
import { FilteringContext } from './js/filtering-context.js'; import { FilteringContext } from './js/filtering-context.js';
import { LineIterator } from './js/text-utils.js'; import { LineIterator } from './js/text-utils.js';
@ -83,7 +82,7 @@ function applyList(name, raw) {
function enableWASM(path) { function enableWASM(path) {
return Promise.all([ return Promise.all([
globals.publicSuffixList.enableWASM(`${path}/lib/publicsuffixlist`), publicSuffixList.enableWASM(`${path}/lib/publicsuffixlist`),
staticNetFilteringEngine.enableWASM(`${path}/js`), staticNetFilteringEngine.enableWASM(`${path}/js`),
]); ]);
} }
@ -93,7 +92,7 @@ function pslInit(raw) {
console.info('Unable to populate public suffix list'); console.info('Unable to populate public suffix list');
return; return;
} }
globals.publicSuffixList.parse(raw, punycode.toASCII); publicSuffixList.parse(raw, punycode.toASCII);
console.info('Public suffix list populated'); console.info('Public suffix list populated');
} }

View file

@ -34,9 +34,8 @@ import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url)); const __dirname = dirname(fileURLToPath(import.meta.url));
import punycode from './lib/punycode.js'; import punycode from './lib/punycode.js';
import './lib/publicsuffixlist/publicsuffixlist.js'; import publicSuffixList from './lib/publicsuffixlist/publicsuffixlist.js';
import globals from './js/globals.js';
import snfe from './js/static-net-filtering.js'; import snfe from './js/static-net-filtering.js';
import { FilteringContext } from './js/filtering-context.js'; import { FilteringContext } from './js/filtering-context.js';
import { LineIterator } from './js/text-utils.js'; import { LineIterator } from './js/text-utils.js';
@ -63,7 +62,7 @@ async function enableWASM() {
}; };
try { try {
const results = await Promise.all([ const results = await Promise.all([
globals.publicSuffixList.enableWASM(wasmModuleFetcher, './lib/publicsuffixlist/wasm/'), publicSuffixList.enableWASM(wasmModuleFetcher, './lib/publicsuffixlist/wasm/'),
snfe.enableWASM(wasmModuleFetcher, './js/wasm/'), snfe.enableWASM(wasmModuleFetcher, './js/wasm/'),
]); ]);
return results.every(a => a === true); return results.every(a => a === true);
@ -77,8 +76,8 @@ async function enableWASM() {
function pslInit(raw) { function pslInit(raw) {
if ( typeof raw === 'string' && raw.trim() !== '' ) { if ( typeof raw === 'string' && raw.trim() !== '' ) {
globals.publicSuffixList.parse(raw, punycode.toASCII); publicSuffixList.parse(raw, punycode.toASCII);
return globals.publicSuffixList; return publicSuffixList;
} }
// Use serialized version if available // Use serialized version if available
@ -93,8 +92,8 @@ function pslInit(raw) {
} }
} }
if ( serialized !== null ) { if ( serialized !== null ) {
globals.publicSuffixList.fromSelfie(serialized); publicSuffixList.fromSelfie(serialized);
return globals.publicSuffixList; return publicSuffixList;
} }
raw = readFileSync( raw = readFileSync(
@ -105,8 +104,8 @@ function pslInit(raw) {
console.error('Unable to populate public suffix list'); console.error('Unable to populate public suffix list');
return; return;
} }
globals.publicSuffixList.parse(raw, punycode.toASCII); publicSuffixList.parse(raw, punycode.toASCII);
return globals.publicSuffixList; return publicSuffixList;
} }
/******************************************************************************/ /******************************************************************************/

View file

@ -24,7 +24,7 @@
/******************************************************************************/ /******************************************************************************/
describe('Leaks', () => { describe('Leaks', () => {
it('should not leak', async () => { it('should not leak global variables', async () => {
await import('../index.js'); await import('../index.js');
}); });
}); });

View file

@ -25,17 +25,14 @@
/******************************************************************************/ /******************************************************************************/
import '../lib/publicsuffixlist/publicsuffixlist.js'; import publicSuffixList from '../lib/publicsuffixlist/publicsuffixlist.js';
import globals from './globals.js';
import { hostnameFromURI } from './uri-utils.js'; import { hostnameFromURI } from './uri-utils.js';
import './codemirror/ubo-dynamic-filtering.js'; import './codemirror/ubo-dynamic-filtering.js';
/******************************************************************************/ /******************************************************************************/
const publicSuffixList = globals.publicSuffixList;
const hostnameToDomainMap = new Map(); const hostnameToDomainMap = new Map();
const mergeView = new CodeMirror.MergeView( const mergeView = new CodeMirror.MergeView(
@ -625,11 +622,11 @@ const editSaveHandler = function() {
/******************************************************************************/ /******************************************************************************/
globals.cloud.onPush = function() { self.cloud.onPush = function() {
return thePanes.orig.original.join('\n'); return thePanes.orig.original.join('\n');
}; };
globals.cloud.onPull = function(data, append) { self.cloud.onPull = function(data, append) {
if ( typeof data !== 'string' ) { return; } if ( typeof data !== 'string' ) { return; }
applyDiff( applyDiff(
false, false,
@ -640,7 +637,7 @@ globals.cloud.onPull = function(data, append) {
/******************************************************************************/ /******************************************************************************/
globals.hasUnsavedData = function() { self.hasUnsavedData = function() {
return mergeView.editor().isClean(cleanEditToken) === false; return mergeView.editor().isClean(cleanEditToken) === false;
}; };

View file

@ -23,12 +23,11 @@
/******************************************************************************/ /******************************************************************************/
import '../lib/publicsuffixlist/publicsuffixlist.js'; import publicSuffixList from '../lib/publicsuffixlist/publicsuffixlist.js';
import punycode from '../lib/punycode.js'; import punycode from '../lib/punycode.js';
import cacheStorage from './cachestorage.js'; import cacheStorage from './cachestorage.js';
import cosmeticFilteringEngine from './cosmetic-filtering.js'; import cosmeticFilteringEngine from './cosmetic-filtering.js';
import globals from './globals.js';
import logger from './logger.js'; import logger from './logger.js';
import lz4Codec from './lz4.js'; import lz4Codec from './lz4.js';
import io from './assets.js'; import io from './assets.js';
@ -1115,7 +1114,7 @@ const getRules = function() {
sessionSwitches.toArray(), sessionSwitches.toArray(),
sessionURLFiltering.toArray() sessionURLFiltering.toArray()
), ),
pslSelfie: globals.publicSuffixList.toSelfie(), pslSelfie: publicSuffixList.toSelfie(),
}; };
}; };

View file

@ -19,15 +19,16 @@
Home: https://github.com/gorhill/uBlock Home: https://github.com/gorhill/uBlock
*/ */
/* globals WebAssembly */
'use strict'; 'use strict';
/******************************************************************************/ /******************************************************************************/
import '../lib/publicsuffixlist/publicsuffixlist.js'; import publicSuffixList from '../lib/publicsuffixlist/publicsuffixlist.js';
import punycode from '../lib/punycode.js'; import punycode from '../lib/punycode.js';
import cosmeticFilteringEngine from './cosmetic-filtering.js'; import cosmeticFilteringEngine from './cosmetic-filtering.js';
import globals from './globals.js';
import io from './assets.js'; import io from './assets.js';
import logger from './logger.js'; import logger from './logger.js';
import lz4Codec from './lz4.js'; import lz4Codec from './lz4.js';
@ -1203,7 +1204,7 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
/******************************************************************************/ /******************************************************************************/
µb.loadPublicSuffixList = async function() { µb.loadPublicSuffixList = async function() {
const psl = globals.publicSuffixList; const psl = publicSuffixList;
// WASM is nice but not critical // WASM is nice but not critical
if ( vAPI.canWASM && this.hiddenSettings.disableWebAssembly !== true ) { if ( vAPI.canWASM && this.hiddenSettings.disableWebAssembly !== true ) {
@ -1211,7 +1212,7 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
return fetch( `${path}.wasm`, { return fetch( `${path}.wasm`, {
mode: 'same-origin' mode: 'same-origin'
}).then( }).then(
globals.WebAssembly.compileStreaming WebAssembly.compileStreaming
).catch(reason => { ).catch(reason => {
ubolog(reason); ubolog(reason);
}); });
@ -1243,7 +1244,7 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
}; };
µb.compilePublicSuffixList = function(content) { µb.compilePublicSuffixList = function(content) {
const psl = globals.publicSuffixList; const psl = publicSuffixList;
psl.parse(content, punycode.toASCII); psl.parse(content, punycode.toASCII);
io.put(`compiled/${this.pslAssetKey}`, psl.toSelfie(sparseBase64)); io.put(`compiled/${this.pslAssetKey}`, psl.toSelfie(sparseBase64));
}; };

View file

@ -23,18 +23,14 @@
/******************************************************************************/ /******************************************************************************/
import '../lib/publicsuffixlist/publicsuffixlist.js'; import publicSuffixList from '../lib/publicsuffixlist/publicsuffixlist.js';
import punycode from '../lib/punycode.js'; import punycode from '../lib/punycode.js';
import globals from './globals.js';
/******************************************************************************/ /******************************************************************************/
// Originally: // Originally:
// https://github.com/gorhill/uBlock/blob/8b5733a58d3acf9fb62815e14699c986bd1c2fdc/src/js/uritools.js // https://github.com/gorhill/uBlock/blob/8b5733a58d3acf9fb62815e14699c986bd1c2fdc/src/js/uritools.js
const psl = globals.publicSuffixList;
const reCommonHostnameFromURL = const reCommonHostnameFromURL =
/^https?:\/\/([0-9a-z_][0-9a-z._-]*[0-9a-z])\//; /^https?:\/\/([0-9a-z_][0-9a-z._-]*[0-9a-z])\//;
const reAuthorityFromURI = const reAuthorityFromURI =
@ -65,7 +61,7 @@ const reHostnameVeryCoarse = /[g-z_\-]/;
function domainFromHostname(hostname) { function domainFromHostname(hostname) {
return reIPAddressNaive.test(hostname) return reIPAddressNaive.test(hostname)
? hostname ? hostname
: psl.getDomain(hostname); : publicSuffixList.getDomain(hostname);
} }
function domainFromURI(uri) { function domainFromURI(uri) {

View file

@ -43,7 +43,7 @@
/******************************************************************************/ /******************************************************************************/
(function(context) { export default (function() {
// >>>>>>>> start of anonymous namespace // >>>>>>>> start of anonymous namespace
/******************************************************************************* /*******************************************************************************
@ -605,32 +605,16 @@ const disableWASM = function() {
/******************************************************************************/ /******************************************************************************/
context.publicSuffixList = { return ({
version: '2.0', version: '2.0',
parse, parse,
getDomain, getDomain,
getPublicSuffix, getPublicSuffix,
toSelfie, fromSelfie, toSelfie, fromSelfie,
disableWASM, enableWASM, disableWASM, enableWASM,
}; });
if ( typeof module !== 'undefined' ) {
module.exports = context.publicSuffixList;
} else if ( typeof exports !== 'undefined' ) {
exports = context.publicSuffixList;
}
/******************************************************************************/ /******************************************************************************/
// <<<<<<<< end of anonymous namespace // <<<<<<<< end of anonymous namespace
})( })();
(root => {
if ( root !== undefined ) { return root; }
// jshint ignore:start
if ( typeof self !== 'undefined' ) { return self; }
if ( typeof window !== 'undefined' ) { return window; }
if ( typeof global !== 'undefined' ) { return global; }
// jshint ignore:end
throw new Error('unable to locate global object');
})(this)
);