From 9ddbb293c02364a7c85b2ac9ad47244f420be96b Mon Sep 17 00:00:00 2001 From: Manish Jethani Date: Sun, 22 Aug 2021 21:33:59 +0530 Subject: [PATCH] Convert punycode.js into an ES module (#3845) --- platform/browser/main.js | 4 +-- platform/nodejs/index.js | 6 ++--- src/js/dynamic-net-filtering.js | 5 ++-- src/js/hnswitches.js | 4 +-- src/js/messaging.js | 3 +-- src/js/popup-fenix.js | 4 ++- src/js/popup.js | 4 ++- src/js/storage.js | 4 +-- src/js/uri-utils.js | 3 +-- src/lib/punycode.js | 43 +++------------------------------ src/popup-fenix.html | 3 +-- src/popup.html | 3 +-- src/whitelist.html | 1 - 13 files changed, 24 insertions(+), 63 deletions(-) diff --git a/platform/browser/main.js b/platform/browser/main.js index 082fd11a4..2937a100d 100644 --- a/platform/browser/main.js +++ b/platform/browser/main.js @@ -24,7 +24,7 @@ /******************************************************************************/ import './lib/publicsuffixlist/publicsuffixlist.js'; -import './lib/punycode.js'; +import punycode from './lib/punycode.js'; import globals from './js/globals.js'; import staticNetFilteringEngine from './js/static-net-filtering.js'; @@ -93,7 +93,7 @@ function pslInit(raw) { console.info('Unable to populate public suffix list'); return; } - globals.publicSuffixList.parse(raw, globals.punycode.toASCII); + globals.publicSuffixList.parse(raw, punycode.toASCII); console.info('Public suffix list populated'); } diff --git a/platform/nodejs/index.js b/platform/nodejs/index.js index 69292817d..59bf36450 100644 --- a/platform/nodejs/index.js +++ b/platform/nodejs/index.js @@ -33,7 +33,7 @@ import { fileURLToPath } from 'url'; const __dirname = dirname(fileURLToPath(import.meta.url)); -import './lib/punycode.js'; +import punycode from './lib/punycode.js'; import './lib/publicsuffixlist/publicsuffixlist.js'; import globals from './js/globals.js'; @@ -77,7 +77,7 @@ async function enableWASM() { function pslInit(raw) { if ( typeof raw === 'string' && raw.trim() !== '' ) { - globals.publicSuffixList.parse(raw, globals.punycode.toASCII); + globals.publicSuffixList.parse(raw, punycode.toASCII); return globals.publicSuffixList; } @@ -105,7 +105,7 @@ function pslInit(raw) { console.error('Unable to populate public suffix list'); return; } - globals.publicSuffixList.parse(raw, globals.punycode.toASCII); + globals.publicSuffixList.parse(raw, punycode.toASCII); return globals.publicSuffixList; } diff --git a/src/js/dynamic-net-filtering.js b/src/js/dynamic-net-filtering.js index 9c950a0e8..28d8b5145 100644 --- a/src/js/dynamic-net-filtering.js +++ b/src/js/dynamic-net-filtering.js @@ -23,7 +23,8 @@ /******************************************************************************/ -import globals from './globals.js'; +import punycode from '../lib/punycode.js'; + import { LineIterator } from './text-utils.js'; import { @@ -33,8 +34,6 @@ import { /******************************************************************************/ -const punycode = globals.punycode || undefined; - // Object.create(null) is used below to eliminate worries about unexpected // property names in prototype chain -- and this way we don't have to use // hasOwnProperty() to avoid this. diff --git a/src/js/hnswitches.js b/src/js/hnswitches.js index 5ff8dd61b..afc0e4fd7 100644 --- a/src/js/hnswitches.js +++ b/src/js/hnswitches.js @@ -25,13 +25,13 @@ /******************************************************************************/ -import globals from './globals.js'; +import punycode from '../lib/punycode.js'; + import { decomposeHostname } from './uri-utils.js'; import { LineIterator } from './text-utils.js'; /******************************************************************************/ -const punycode = globals.punycode || undefined; const decomposedSource = []; // Object.create(null) is used below to eliminate worries about unexpected diff --git a/src/js/messaging.js b/src/js/messaging.js index de7435ef3..dc669b8f4 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -24,7 +24,7 @@ /******************************************************************************/ import '../lib/publicsuffixlist/publicsuffixlist.js'; -import '../lib/punycode.js'; +import punycode from '../lib/punycode.js'; import cacheStorage from './cachestorage.js'; import cosmeticFilteringEngine from './cosmetic-filtering.js'; @@ -1087,7 +1087,6 @@ const getLists = async function(callback) { // TODO: also return origin of embedded frames? const getOriginHints = function() { - const punycode = globals.punycode; const out = new Set(); for ( const tabId of µb.pageStores.keys() ) { if ( tabId === -1 ) { continue; } diff --git a/src/js/popup-fenix.js b/src/js/popup-fenix.js index d0cf5c926..ecbd0b433 100644 --- a/src/js/popup-fenix.js +++ b/src/js/popup-fenix.js @@ -19,10 +19,12 @@ Home: https://github.com/gorhill/uBlock */ -/* global punycode, uDom */ +/* global uDom */ 'use strict'; +import punycode from '../lib/punycode.js'; + /******************************************************************************/ { diff --git a/src/js/popup.js b/src/js/popup.js index 205c4e527..48d570fbe 100644 --- a/src/js/popup.js +++ b/src/js/popup.js @@ -19,10 +19,12 @@ Home: https://github.com/gorhill/uBlock */ -/* global punycode, uDom */ +/* global uDom */ 'use strict'; +import punycode from '../lib/punycode.js'; + /******************************************************************************/ (( ) => { diff --git a/src/js/storage.js b/src/js/storage.js index f2269bf74..2beda9259 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -24,7 +24,7 @@ /******************************************************************************/ import '../lib/publicsuffixlist/publicsuffixlist.js'; -import '../lib/punycode.js'; +import punycode from '../lib/punycode.js'; import cosmeticFilteringEngine from './cosmetic-filtering.js'; import globals from './globals.js'; @@ -1244,7 +1244,7 @@ self.addEventListener('hiddenSettingsChanged', ( ) => { µb.compilePublicSuffixList = function(content) { const psl = globals.publicSuffixList; - psl.parse(content, globals.punycode.toASCII); + psl.parse(content, punycode.toASCII); io.put(`compiled/${this.pslAssetKey}`, psl.toSelfie(sparseBase64)); }; diff --git a/src/js/uri-utils.js b/src/js/uri-utils.js index f371af588..d7fb013ec 100644 --- a/src/js/uri-utils.js +++ b/src/js/uri-utils.js @@ -24,7 +24,7 @@ /******************************************************************************/ import '../lib/publicsuffixlist/publicsuffixlist.js'; -import '../lib/punycode.js'; +import punycode from '../lib/punycode.js'; import globals from './globals.js'; @@ -34,7 +34,6 @@ import globals from './globals.js'; // https://github.com/gorhill/uBlock/blob/8b5733a58d3acf9fb62815e14699c986bd1c2fdc/src/js/uritools.js const psl = globals.publicSuffixList; -const punycode = globals.punycode; const reCommonHostnameFromURL = /^https?:\/\/([0-9a-z_][0-9a-z._-]*[0-9a-z])\//; diff --git a/src/lib/punycode.js b/src/lib/punycode.js index 18a94eac6..a7f990d63 100644 --- a/src/lib/punycode.js +++ b/src/lib/punycode.js @@ -1,20 +1,5 @@ /*! https://mths.be/punycode v1.3.2 by @mathias */ -;(function(root) { - - /** Detect free variables */ - var freeExports = typeof exports == 'object' && exports && - !exports.nodeType && exports; - var freeModule = typeof module == 'object' && module && - !module.nodeType && module; - var freeGlobal = typeof global == 'object' && global || self; - if ( - freeGlobal.global === freeGlobal || - freeGlobal.window === freeGlobal || - freeGlobal.self === freeGlobal - ) { - root = freeGlobal; - } - +export default (function() { /** * The `punycode` object. * @name punycode @@ -504,27 +489,5 @@ 'toUnicode': toUnicode }; - /** Expose `punycode` */ - // Some AMD build optimizers, like r.js, check for specific condition patterns - // like the following: - if ( - typeof define == 'function' && - typeof define.amd == 'object' && - define.amd - ) { - define('punycode', function() { - return punycode; - }); - } else if (freeExports && freeModule) { - if (module.exports == freeExports) { // in Node.js or RingoJS v0.8.0+ - freeModule.exports = punycode; - } else { // in Narwhal or RingoJS v0.7.0- - for (key in punycode) { - punycode.hasOwnProperty(key) && (freeExports[key] = punycode[key]); - } - } - } else { // in Rhino or a web browser - root.punycode = punycode; - } - -}(this)); + return punycode; +})(); diff --git a/src/popup-fenix.html b/src/popup-fenix.html index 09d49d639..ab8401592 100644 --- a/src/popup-fenix.html +++ b/src/popup-fenix.html @@ -96,13 +96,12 @@ - - + diff --git a/src/popup.html b/src/popup.html index 7df02d25a..049866d63 100644 --- a/src/popup.html +++ b/src/popup.html @@ -62,13 +62,12 @@ - - + diff --git a/src/whitelist.html b/src/whitelist.html index b5a63a186..53167ced7 100644 --- a/src/whitelist.html +++ b/src/whitelist.html @@ -43,7 +43,6 @@ -