Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Switch from commonjs to ESM #255

Merged
merged 65 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
f7acaee
add type = module to package.json
cowlicks Aug 14, 2020
f9772cb
bump mocha version to get ESM support
cowlicks Aug 14, 2020
98cd45a
Switch to ESM
cowlicks Aug 14, 2020
bae0a61
Switch to ESM
cowlicks Aug 14, 2020
95ad820
constants.js to ESM
cowlicks Aug 14, 2020
e6415d6
Add constants module test
cowlicks Aug 14, 2020
473956e
ESM shims, fakes, and utils. utils_test works
cowlicks Aug 14, 2020
a4cdd04
ESM fakes tests
cowlicks Aug 14, 2020
ba53576
add constants tests
cowlicks Aug 14, 2020
5795329
Split browser stuff out of utlis to rm shim dep
cowlicks Aug 14, 2020
174c4c4
ESM shim.js and shim_test.js
cowlicks Aug 14, 2020
724fecc
update psl data (make psl)
cowlicks Aug 14, 2020
b15f263
make getpsl.py generate a ESM module
cowlicks Aug 14, 2020
538a81e
Run `make psl` again to generate ESM psl.js
cowlicks Aug 14, 2020
734fcba
ESM basedomain and tests
cowlicks Aug 14, 2020
e2a440b
ESM mdfp and tests
cowlicks Aug 14, 2020
a08832b
initFunc docs
cowlicks Aug 14, 2020
be132ac
ESM modules for third party stuff and tests
cowlicks Aug 14, 2020
05261e1
ESM to get schemes_test.js working
cowlicks Aug 15, 2020
f2edc06
ESM to get messages_test.js working
cowlicks Aug 15, 2020
ff07fa6
disk_map_test.js ESM
cowlicks Aug 15, 2020
ee7452f
ESM fingerconting_test.js
cowlicks Aug 15, 2020
3fe94ab
rename constants testing
cowlicks Aug 16, 2020
e05aa14
ESM popup_server.js
cowlicks Aug 17, 2020
ec2242d
whitespace
cowlicks Aug 17, 2020
39e29b7
add useJSODM function
cowlicks Aug 17, 2020
13444ab
add wrapObject, rm React shims
cowlicks Aug 17, 2020
cd7da0a
ESM helpers.js
cowlicks Aug 17, 2020
cd96953
ESM popup_test.js. it works now
cowlicks Aug 17, 2020
ffe2a66
fix naming bug from shim change
cowlicks Aug 17, 2020
12256d3
Always load helper.js for tests
cowlicks Aug 17, 2020
918b243
fix shim naming bug in tabs.js
cowlicks Aug 17, 2020
a2b1c29
Add useJSDOM to global befor func
cowlicks Aug 17, 2020
beb0f59
ESM possum.js and tests
cowlicks Aug 17, 2020
303eaf1
fix shim naming bug
cowlicks Aug 17, 2020
55d2d74
ESM etag_test.js
cowlicks Aug 17, 2020
bdd212b
ESM referer_test.js
cowlicks Aug 17, 2020
39b4f62
ESM reasons_test.js
cowlicks Aug 17, 2020
a77ca63
ESM store_test.js
cowlicks Aug 17, 2020
bb45d13
ESM tabs_test.js
cowlicks Aug 17, 2020
fd34d26
ESM webrequest_test.js
cowlicks Aug 17, 2020
bde12b8
fix npm test
cowlicks Aug 17, 2020
acfa28f
ESM intialize.js
cowlicks Aug 20, 2020
8b5e26e
Refactor popup_components to load React async
cowlicks Aug 20, 2020
c730470
Refactor popup to load react async
cowlicks Aug 20, 2020
7087c17
ESM initialize_popup.js
cowlicks Aug 20, 2020
9ea38ad
Add back React/ReactDOM to shims
cowlicks Aug 20, 2020
615b9dc
Wait for connect with await
cowlicks Aug 20, 2020
95a3293
Let errorOccurred take a callback.
cowlicks Aug 20, 2020
9d16498
rm console.log from debugging
cowlicks Aug 21, 2020
24cc59a
Load stuff as ESM in popup.html
cowlicks Aug 21, 2020
c1adfe9
Add background.html and load stuff as ESM
cowlicks Aug 21, 2020
909e79d
s/shims/shim/g
cowlicks Aug 21, 2020
12e1143
Fix shim rename
cowlicks Aug 21, 2020
6f7fdc1
rm "use strict" from ESM
cowlicks Aug 21, 2020
55833dd
Delete blank lines at top of files
cowlicks Aug 21, 2020
adc53ec
rm inserting 'use strict' in getpsl.py
cowlicks Aug 21, 2020
88cbbf1
Remove unused require system. RIP
cowlicks Aug 21, 2020
8debb90
ignore package-lock.json
cowlicks Aug 27, 2020
0d31012
Revert "Remove unused require system. RIP"
cowlicks Aug 27, 2020
3cb8286
Revert "ESM fingerconting_test.js"
cowlicks Aug 27, 2020
4d03037
rename fingercounting.cjs to mark as cjs module
cowlicks Aug 27, 2020
d1df7e7
more renames to cjs
cowlicks Aug 27, 2020
3b4cbeb
No need for these script tags
cowlicks Aug 27, 2020
ed2df54
better error messages
cowlicks Aug 27, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
};
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