Skip to content

Commit

Permalink
Merge pull request #255 from cowlicks/esm-modules
Browse files Browse the repository at this point in the history
WIP: Switch from commonjs to ESM
  • Loading branch information
cowlicks authored Aug 27, 2020
2 parents f8bd0d6 + ed2df54 commit 1e8cf12
Show file tree
Hide file tree
Showing 62 changed files with 840 additions and 687 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,5 @@ typings/
# dotenv environment variables file
.env

# we only use package-lock.json in dev, so we ignore it
package-lock.json
8 changes: 1 addition & 7 deletions scripts/getpsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,11 @@
psl_url = 'https://publicsuffix.org/list/public_suffix_list.dat'

file_text = '''/* eslint-disable */
"use strict";
[(function(exports) {
const publicSuffixes = new Map(
%s
);
Object.assign(exports, {publicSuffixes});
})].map(func => typeof exports == 'undefined' ? define('/domains/psl', func) : func(exports));'''
export {publicSuffixes};'''


def get_psl_text():
Expand Down
2 changes: 0 additions & 2 deletions src/js/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
"use strict";

/**
* Simple commonjs-like module system that is compatible with node.
*
Expand Down
10 changes: 2 additions & 8 deletions src/js/browser_compat.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
"use strict";

[(function(exports) {

const shim = require('./shim');
import {shim} from './shim.js';

/*
* Firefox & Chrome now take different values in opt_extraInfoSpec
Expand All @@ -19,6 +15,4 @@ function getOnHeadersReceivedOptions({OnHeadersReceivedOptions} = shim) {
return ["BLOCKING", "RESPONSE_HEADERS", "RESPONSEHEADERS", "EXTRA_HEADERS"].map(x => OnHeadersReceivedOptions[x]).filter(x => x);
}

Object.assign(exports, {getOnBeforeRequestOptions, getOnBeforeSendHeadersOptions, getOnHeadersReceivedOptions});

})].map(func => typeof exports == 'undefined' ? define('/browser_compat', func) : func(exports));
export {getOnBeforeRequestOptions, getOnBeforeSendHeadersOptions, getOnHeadersReceivedOptions};
67 changes: 67 additions & 0 deletions src/js/browser_utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {activeIcons, inactiveIcons} from './constants.js';

import {shim} from './shim.js';
const {tabsQuery, tabsGet, setIcon, setBadgeText} = shim;


async function currentTab() {
const active = true, lastFocusedWindow = true;
return new Promise(resolve => {
tabsQuery({active, lastFocusedWindow}, tabsFirstTry => {
if (tabsFirstTry.length > 0) {
resolve(tabsFirstTry[0]);
} else { // tab not focused
tabsQuery({active}, tabsSecondTry => {
resolve(tabsSecondTry[0]);
});
}
});
});
}

function errorOccurred(cb = ()=>{}) {
if (typeof chrome !== 'undefined' && chrome.runtime.lastError) {
cb(chrome.runtime.lastError);
return true;
} else {
return false;
}
}

async function tabExists(tabId) {
if (tabId >= 0) {
return await new Promise(resolve => {
tabsGet(tabId, () => {
if (!errorOccurred()) {
resolve(true);
} else {
resolve(false);
}
});
});
} else {
return true;
}
}

// todo after setIcon return's a promise, make this return a promise
async function setTabIconActive(tabId, active) {
if (await tabExists(tabId)) {
let icons = active ? activeIcons : inactiveIcons;
setIcon({tabId: tabId, path: icons});
}
}

async function safeSetBadgeText(tabId, text) {
if (await tabExists(tabId)) {
setBadgeText({text, tabId});
}
}

export {
currentTab,
errorOccurred,
tabExists,
setTabIconActive,
safeSetBadgeText,
}
12 changes: 3 additions & 9 deletions src/js/constants.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
"use strict";

[(function(exports) {

// disk name
const DISK_NAME = 'p055um';

Expand Down Expand Up @@ -57,7 +53,7 @@ const FINGERPRINTING = 'fingerprinting',

const CONTENTSCRIPTS = new Set([
'/js/bootstrap.js',
'/js/contentscripts/fingercounting.js',
'/js/contentscripts/fingercounting.cjs',
'/js/initialize_contentscripts.js',
]);

Expand All @@ -78,7 +74,7 @@ const REMOVE_ACTION = 'remove_action';

const GET_DEBUG_LOG = 'get_debug_log';

Object.assign(exports, {
export {
DISK_NAME,
responses,
NO_ACTION,
Expand All @@ -102,6 +98,4 @@ Object.assign(exports, {
POPUP,
REMOVE_ACTION,
GET_DEBUG_LOG,
});

})].map(func => typeof exports == 'undefined' ? define('/constants', func) : func(exports));
};
File renamed without changes.
2 changes: 0 additions & 2 deletions src/js/contentscripts/twitter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
"use strict";

let query_param,
tcos_with_destination,
fixes = {};
Expand Down
6 changes: 1 addition & 5 deletions src/js/disk_map.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
"use strict";
/**
* DiskMap wraps an asynchronous get/set api, and prefixes its keys with a
* string, effectively giving each instance of DiskMap a namespace. This allows
Expand All @@ -11,7 +10,6 @@
* resource intensive. So instead we do this.
*/

[(function(exports) {

class DiskMap {
constructor(name, disk) {
Expand Down Expand Up @@ -89,6 +87,4 @@ class DiskMap {
}
}

Object.assign(exports, {DiskMap});

})].map(func => typeof exports == 'undefined' ? define('/disk_map', func) : func(exports));
export {DiskMap};
11 changes: 3 additions & 8 deletions src/js/domains/basedomain.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
* Available under MIT license <http://mths.be/mit>
*/

"use strict";

[(function(exports) {

const {publicSuffixes} = require('./psl'),
{memoize} = require('../utils');
import {publicSuffixes} from './psl.js';
import {memoize} from '../utils.js';

const re_ipv4 = /[0-9]$/;

Expand Down Expand Up @@ -70,6 +67,4 @@ function getBaseDomain(/**String*/ hostname) {
}
getBaseDomain = memoize(getBaseDomain, (x) => x, 1000);

Object.assign(exports, {getBaseDomain});

})].map(func => typeof exports == 'undefined' ? define('/domains/basedomain', func) : func(exports));
export {getBaseDomain};
25 changes: 15 additions & 10 deletions src/js/domains/mdfp.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
"use strict";

[(function(exports) {

const {memoize, lazyDef} = require('../utils');
import {memoize} from '../utils.js';


let multiDomainFirstPartiesArray = [
Expand Down Expand Up @@ -321,11 +317,20 @@ class MultiDomainFirstParties {
}
}

lazyDef(exports, 'isMdfp', () => {
/*
* Function wrapper that lets us call initFunc to initialize the function.
* the initFunc should return the function
*/
function withInit(initFunc) {
let func = null
return function() {
return (func ? func : func = initFunc()).apply(null, arguments);
}
}

const isMdfp = withInit(() => {
let mdfp = new MultiDomainFirstParties();
return {isMdfp: mdfp.isMdfp.bind(mdfp)};
return mdfp.isMdfp.bind(mdfp);
});

Object.assign(exports, {MultiDomainFirstParties});

})].map(func => typeof exports == 'undefined' ? define('/domains/mdfp', func) : func(exports));
export {MultiDomainFirstParties, isMdfp};
14 changes: 4 additions & 10 deletions src/js/domains/parties.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
"use strict";

[(function(exports) {

const {memoize} = require('../utils'),
{getBaseDomain} = require('./basedomain'),
{isMdfp} = require('./mdfp');
import {memoize} from '../utils.js';
import {getBaseDomain} from './basedomain.js';
import {isMdfp} from './mdfp.js';

function isThirdParty(d1, d2) {
let b1 = getBaseDomain(d1),
Expand All @@ -17,6 +13,4 @@ function isThirdParty(d1, d2) {
}
isThirdParty = memoize(isThirdParty, ([a, b]) => a + ' ' + b, 1000);

Object.assign(exports, {isThirdParty});

})].map(func => typeof exports == 'undefined' ? define('/domains/parties', func) : func(exports));
export {isThirdParty};
Loading

0 comments on commit 1e8cf12

Please sign in to comment.