Skip to content

Commit

Permalink
Convert publicsuffixlist.js into an ES module (gorhill#3846)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjethani authored Aug 23, 2021
1 parent ba83c21 commit 9761b02
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 55 deletions.
7 changes: 3 additions & 4 deletions platform/browser/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@

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

import './lib/publicsuffixlist/publicsuffixlist.js';
import publicSuffixList from './lib/publicsuffixlist/publicsuffixlist.js';
import punycode from './lib/punycode.js';

import globals from './js/globals.js';
import staticNetFilteringEngine from './js/static-net-filtering.js';
import { FilteringContext } from './js/filtering-context.js';
import { LineIterator } from './js/text-utils.js';
Expand Down Expand Up @@ -83,7 +82,7 @@ function applyList(name, raw) {

function enableWASM(path) {
return Promise.all([
globals.publicSuffixList.enableWASM(`${path}/lib/publicsuffixlist`),
publicSuffixList.enableWASM(`${path}/lib/publicsuffixlist`),
staticNetFilteringEngine.enableWASM(`${path}/js`),
]);
}
Expand All @@ -93,7 +92,7 @@ function pslInit(raw) {
console.info('Unable to populate public suffix list');
return;
}
globals.publicSuffixList.parse(raw, punycode.toASCII);
publicSuffixList.parse(raw, punycode.toASCII);
console.info('Public suffix list populated');
}

Expand Down
17 changes: 8 additions & 9 deletions platform/nodejs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));

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 { FilteringContext } from './js/filtering-context.js';
import { LineIterator } from './js/text-utils.js';
Expand All @@ -63,7 +62,7 @@ async function enableWASM() {
};
try {
const results = await Promise.all([
globals.publicSuffixList.enableWASM(wasmModuleFetcher, './lib/publicsuffixlist/wasm/'),
publicSuffixList.enableWASM(wasmModuleFetcher, './lib/publicsuffixlist/wasm/'),
snfe.enableWASM(wasmModuleFetcher, './js/wasm/'),
]);
return results.every(a => a === true);
Expand All @@ -77,8 +76,8 @@ async function enableWASM() {

function pslInit(raw) {
if ( typeof raw === 'string' && raw.trim() !== '' ) {
globals.publicSuffixList.parse(raw, punycode.toASCII);
return globals.publicSuffixList;
publicSuffixList.parse(raw, punycode.toASCII);
return publicSuffixList;
}

// Use serialized version if available
Expand All @@ -93,8 +92,8 @@ function pslInit(raw) {
}
}
if ( serialized !== null ) {
globals.publicSuffixList.fromSelfie(serialized);
return globals.publicSuffixList;
publicSuffixList.fromSelfie(serialized);
return publicSuffixList;
}

raw = readFileSync(
Expand All @@ -105,8 +104,8 @@ function pslInit(raw) {
console.error('Unable to populate public suffix list');
return;
}
globals.publicSuffixList.parse(raw, punycode.toASCII);
return globals.publicSuffixList;
publicSuffixList.parse(raw, punycode.toASCII);
return publicSuffixList;
}

/******************************************************************************/
Expand Down
2 changes: 1 addition & 1 deletion platform/npm/tests/leaks.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/******************************************************************************/

describe('Leaks', () => {
it('should not leak', async () => {
it('should not leak global variables', async () => {
await import('../index.js');
});
});
11 changes: 4 additions & 7 deletions src/js/dyna-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 './codemirror/ubo-dynamic-filtering.js';

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

const publicSuffixList = globals.publicSuffixList;

const hostnameToDomainMap = new Map();

const mergeView = new CodeMirror.MergeView(
Expand Down Expand Up @@ -625,11 +622,11 @@ const editSaveHandler = function() {

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

globals.cloud.onPush = function() {
self.cloud.onPush = function() {
return thePanes.orig.original.join('\n');
};

globals.cloud.onPull = function(data, append) {
self.cloud.onPull = function(data, append) {
if ( typeof data !== 'string' ) { return; }
applyDiff(
false,
Expand All @@ -640,7 +637,7 @@ globals.cloud.onPull = function(data, append) {

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

globals.hasUnsavedData = function() {
self.hasUnsavedData = function() {
return mergeView.editor().isClean(cleanEditToken) === false;
};

Expand Down
5 changes: 2 additions & 3 deletions src/js/messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@

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

import '../lib/publicsuffixlist/publicsuffixlist.js';
import publicSuffixList from '../lib/publicsuffixlist/publicsuffixlist.js';
import punycode from '../lib/punycode.js';

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

Expand Down
11 changes: 6 additions & 5 deletions src/js/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
Home: https://github.com/gorhill/uBlock
*/

/* globals WebAssembly */

'use strict';

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

import '../lib/publicsuffixlist/publicsuffixlist.js';
import publicSuffixList from '../lib/publicsuffixlist/publicsuffixlist.js';
import punycode from '../lib/punycode.js';

import cosmeticFilteringEngine from './cosmetic-filtering.js';
import globals from './globals.js';
import io from './assets.js';
import logger from './logger.js';
import lz4Codec from './lz4.js';
Expand Down Expand Up @@ -1203,15 +1204,15 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
/******************************************************************************/

µb.loadPublicSuffixList = async function() {
const psl = globals.publicSuffixList;
const psl = publicSuffixList;

// WASM is nice but not critical
if ( vAPI.canWASM && this.hiddenSettings.disableWebAssembly !== true ) {
const wasmModuleFetcher = function(path) {
return fetch( `${path}.wasm`, {
mode: 'same-origin'
}).then(
globals.WebAssembly.compileStreaming
WebAssembly.compileStreaming
).catch(reason => {
ubolog(reason);
});
Expand Down Expand Up @@ -1243,7 +1244,7 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
};

µb.compilePublicSuffixList = function(content) {
const psl = globals.publicSuffixList;
const psl = publicSuffixList;
psl.parse(content, punycode.toASCII);
io.put(`compiled/${this.pslAssetKey}`, psl.toSelfie(sparseBase64));
};
Expand Down
8 changes: 2 additions & 6 deletions src/js/uri-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,14 @@

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

import '../lib/publicsuffixlist/publicsuffixlist.js';
import publicSuffixList from '../lib/publicsuffixlist/publicsuffixlist.js';
import punycode from '../lib/punycode.js';

import globals from './globals.js';

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

// Originally:
// https://github.com/gorhill/uBlock/blob/8b5733a58d3acf9fb62815e14699c986bd1c2fdc/src/js/uritools.js

const psl = globals.publicSuffixList;

const reCommonHostnameFromURL =
/^https?:\/\/([0-9a-z_][0-9a-z._-]*[0-9a-z])\//;
const reAuthorityFromURI =
Expand Down Expand Up @@ -65,7 +61,7 @@ const reHostnameVeryCoarse = /[g-z_\-]/;
function domainFromHostname(hostname) {
return reIPAddressNaive.test(hostname)
? hostname
: psl.getDomain(hostname);
: publicSuffixList.getDomain(hostname);
}

function domainFromURI(uri) {
Expand Down
24 changes: 4 additions & 20 deletions src/lib/publicsuffixlist/publicsuffixlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

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

(function(context) {
export default (function() {
// >>>>>>>> start of anonymous namespace

/*******************************************************************************
Expand Down Expand Up @@ -605,32 +605,16 @@ const disableWASM = function() {

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

context.publicSuffixList = {
return ({
version: '2.0',
parse,
getDomain,
getPublicSuffix,
toSelfie, fromSelfie,
disableWASM, enableWASM,
};

if ( typeof module !== 'undefined' ) {
module.exports = context.publicSuffixList;
} else if ( typeof exports !== 'undefined' ) {
exports = context.publicSuffixList;
}
});

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

// <<<<<<<< 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)
);
})();

0 comments on commit 9761b02

Please sign in to comment.