Skip to content

Commit acab80f

Browse files
committed
Updating coincident to its latest
1 parent 036b48c commit acab80f

22 files changed

+235
-261
lines changed

cjs/package.json

-1
This file was deleted.

docs/index.js

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/zip-CGWtiqjJ.js

-2
This file was deleted.

docs/zip-gl8b5xR3.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/zip-CGWtiqjJ.js.map renamed to docs/zip-gl8b5xR3.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

esm/worker/_template.js

+7-31
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// bigger than it used to be before any changes is applied to this file.
66

77
import * as JSON from '@ungap/structured-clone/json';
8-
import coincident from 'coincident/window';
8+
import coincident from 'coincident/window/worker';
99

1010
import { assign, create, createFunction, createOverload, createResolved, dispatch, registerJSModules } from '../utils.js';
1111
import createJSModules from './js_modules.js';
@@ -32,19 +32,23 @@ const add = (type, fn) => {
3232

3333
const { parse, stringify } = JSON;
3434

35-
const { proxy: sync, window, isWindowProxy } = coincident(self, {
35+
const { proxy: sync, sync: syncMainAndWorker, polyfill, window, isWindowProxy } = await coincident({
3636
parse,
3737
stringify,
3838
transform: value => transform ? transform(value) : value
3939
});
4040

4141
const xworker = {
42+
// propagate the fact SharedArrayBuffer is polyfilled
43+
polyfill,
4244
// allows synchronous utilities between this worker and the main thread
4345
sync,
4446
// allow access to the main thread world
4547
window,
4648
// allow introspection for foreign (main thread) refrences
4749
isWindowProxy,
50+
// propagate the fact `window` can be used
51+
hasWindow: syncMainAndWorker,
4852
// standard worker related events / features
4953
onmessage: console.info,
5054
onerror: console.error,
@@ -61,38 +65,10 @@ add('message', ({ data: { options, config: baseURL, configURL, code, hooks } })
6165

6266
const interpreter = await getRuntime(runtimeID, baseURL, configURL, config);
6367

64-
const { js_modules, sync_main_only } = configs.get(runtimeID);
68+
const { js_modules } = configs.get(runtimeID);
6569

6670
const mainModules = js_modules?.main;
6771

68-
// this flag allows interacting with the xworker.sync exposed
69-
// *only in the worker* and eventually invoked *only from main*.
70-
// If that flag is `false` or not present, then SharedArrayBuffer
71-
// must be available or not much can work in here.
72-
let syncMainAndWorker = !sync_main_only;
73-
74-
// bails out out of the box with a native/meaningful error
75-
// in case the SharedArrayBuffer is not available
76-
try {
77-
new SharedArrayBuffer(4);
78-
// if this does not throw there's no reason to
79-
// branch out of all the features ... but ...
80-
syncMainAndWorker = true;
81-
}
82-
// eslint-disable-next-line no-unused-vars
83-
catch (_) {
84-
// if it does throw and `sync_main_only` was not `true`
85-
// then there's no way to go further
86-
if (syncMainAndWorker) {
87-
throw new Error(
88-
[
89-
'Unable to use SharedArrayBuffer due insecure environment.',
90-
'Please read requirements in MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements',
91-
].join('\n'),
92-
);
93-
}
94-
}
95-
9672
const details = create(registry.get(type));
9773

9874
const resolved = createResolved(

esm/worker/class.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import * as JSON from '@ungap/structured-clone/json';
21
import fetch from '@webreflection/fetch';
3-
import coincident from 'coincident/window';
42
import xworker from './xworker.js';
53
import { getConfigURLAndType } from '../loader.js';
64
import { assign, create, defineProperties, importCSS, importJS } from '../utils.js';
@@ -45,7 +43,7 @@ export default (...args) =>
4543
});
4644

4745
const sync = assign(
48-
coincident(worker, JSON).proxy,
46+
worker.proxy,
4947
{ importJS, importCSS },
5048
);
5149

package-lock.json

+165-170
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+10-15
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
"name": "polyscript",
33
"version": "0.13.10",
44
"description": "PyScript single core to rule them all",
5-
"main": "./cjs/index.js",
5+
"main": "./esm/index.js",
66
"types": "./types/polyscript/esm/index.d.ts",
77
"scripts": {
88
"server": "npx static-handler --coi .",
9-
"build": "export ESLINT_USE_FLAT_CONFIG=false; npm run build:3rd-party && rm -rf dist && mkdir -p esm/python && node rollup/build_python.cjs && npm run rollup:xworker && npm run rollup:core && eslint esm/ && npm run ts && npm run cjs && npm run rollup:integrations && npm run build:docs",
9+
"build": "export ESLINT_USE_FLAT_CONFIG=false; npm run build:3rd-party && rm -rf dist && mkdir -p esm/python && node rollup/build_python.cjs && npm run rollup:xworker && npm run rollup:core && eslint esm/ && npm run ts && npm run rollup:integrations && npm run build:docs",
1010
"build:3rd-party": "node rollup/3rd-party.cjs",
1111
"build:docs": "mv docs/coi.js ./; rm docs/*.{js,map}; cp dist/* docs/; rm -f docs/_template.js; mv coi.js docs/",
12-
"cjs": "ascjs --no-default esm cjs",
1312
"dev": "node dev.cjs",
1413
"rollup:core": "rollup --config rollup/core.config.js",
1514
"rollup:integrations": "node rollup/build_integrations.cjs",
@@ -40,18 +39,17 @@
4039
"license": "APACHE-2.0",
4140
"devDependencies": {
4241
"@node-loader/import-maps": "^1.1.0",
43-
"@playwright/test": "^1.45.2",
42+
"@playwright/test": "^1.45.3",
4443
"@rollup/plugin-node-resolve": "^15.2.3",
4544
"@rollup/plugin-terser": "^0.4.4",
4645
"@zip.js/zip.js": "^2.7.47",
47-
"ascjs": "^6.0.3",
4846
"c8": "^10.1.2",
4947
"chokidar": "^3.6.0",
5048
"eslint": "^9.7.0",
5149
"linkedom": "^0.18.4",
52-
"rollup": "^4.18.1",
50+
"rollup": "^4.19.0",
5351
"static-handler": "^0.4.3",
54-
"typescript": "^5.5.3"
52+
"typescript": "^5.5.4"
5553
},
5654
"type": "module",
5755
"module": "./dist/index.js",
@@ -61,18 +59,15 @@
6159
"exports": {
6260
".": {
6361
"types": "./types/index.d.ts",
64-
"import": "./esm/index.js",
65-
"default": "./cjs/index.js"
62+
"import": "./esm/index.js"
6663
},
6764
"./exports": {
6865
"types": "./types/exports.d.ts",
69-
"import": "./esm/exports.js",
70-
"default": "./cjs/exports.js"
66+
"import": "./esm/exports.js"
7167
},
7268
"./xworker": {
7369
"types": "./types/xworker.d.ts",
74-
"import": "./esm/xworker.js",
75-
"default": "./cjs/xworker.js"
70+
"import": "./esm/xworker.js"
7671
},
7772
"./package.json": "./package.json"
7873
},
@@ -83,14 +78,14 @@
8378
"@webreflection/idb-map": "^0.3.1",
8479
"basic-devtools": "^0.1.6",
8580
"codedent": "^0.1.2",
86-
"coincident": "^1.2.3",
81+
"coincident": "^2.0.0-rc3",
8782
"gc-hook": "^0.3.1",
8883
"html-escaper": "^3.0.3",
8984
"proxy-target": "^3.0.2",
9085
"sticky-module": "^0.1.1",
9186
"to-json-callback": "^0.1.1"
9287
},
9388
"worker": {
94-
"blob": "sha256-1GVYWvaeoNZQWMkVaV243TH9CjdQ95JPuNGGAj3OrDI="
89+
"blob": "sha256-/7mU0W9+r+gE4w81wkQG4lOLZZuastawu03Ln1AMXK4="
9590
}
9691
}

rollup/build_xworker.cjs

+15-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,24 @@ const DIST_DIR = resolve(join(__dirname, "..", "dist"));
1212
const WORKERS_DIR = resolve(join(__dirname, "..", "esm", "worker"));
1313
const PACKAGE_JSON = resolve(join(__dirname, "..", "package.json"));
1414

15+
const coincident = [
16+
"import * as JSON from '@ungap/structured-clone/json';",
17+
"import coincident from 'coincident/window/main';",
18+
'const { Worker } = coincident(JSON);',
19+
];
20+
1521
for (const file of readdirSync(DIST_DIR)) {
1622
if (file.startsWith("_")) {
1723
if (process.env.NO_MIN) {
1824
writeFileSync(
1925
join(WORKERS_DIR, "xworker.js"),
20-
`/* c8 ignore next */\nexport default () => new Worker('/dist/_template.js',{type:'module'});`,
26+
[
27+
'/* c8 ignore start */',
28+
...coincident,
29+
`export default () => new Worker('/dist/_template.js');`,
30+
'/* c8 ignore stop */',
31+
''
32+
].join("\n")
2133
);
2234
} else {
2335
const js = JSON.stringify(
@@ -39,7 +51,8 @@ for (const file of readdirSync(DIST_DIR)) {
3951
'const {url} = import.meta;',
4052
`const re = ${/import\((['"])([^)]+?\.js)\1\)/}g;`,
4153
`const place = ${(_,q,f) => `import(${q}${new URL(f,url).href}${q})`};`,
42-
`export default () => new Worker(URL.createObjectURL(new Blob([${js}.replace(re,place)],{type:'application/javascript'})),{type:'module'})`,
54+
...coincident,
55+
`export default () => new Worker(URL.createObjectURL(new Blob([${js}.replace(re,place)],{type:'application/javascript'})))`,
4356
'/* c8 ignore stop */',
4457
''
4558
].join("\n")

test/_utils.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
const { writeFileShim } = require("../cjs/interpreter/_utils.js");
2-
3-
const assert = require("./assert.js");
1+
import { writeFileShim } from "../esm/interpreter/_utils.js";
2+
import { dirname } from "node:path";
3+
import { fileURLToPath } from "node:url";
44

55
const FS = {
66
mkdir(...args) {
77
this.mkdir_args = args;
88
},
9-
cwd: () => __dirname,
9+
cwd: () => dirname(fileURLToPath(import.meta.url)),
1010
writeFile(...args) {
1111
this.writeFile_args = args;
1212
},

test/assert.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
const assert = (current, expected, message = "Unexpected Error") => {
1+
export default (current, expected, message = "Unexpected Error") => {
22
if (!Object.is(current, expected)) {
33
console.error(`\x1b[1m${message}\x1b[0m`);
44
console.error(" expected", expected);
55
console.error(" got", current);
66
process.exit(1);
77
}
88
};
9-
10-
module.exports = assert;

test/index.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const dedent = require("codedent");
2-
const assert = require("./assert.js");
3-
require("./_utils.js");
1+
import dedent from "codedent";
2+
import assert from "./assert.js";
3+
import "./_utils.js";
44

55
const { fetch } = globalThis;
66

@@ -26,14 +26,15 @@ const patchFetch = (callback) => {
2626
});
2727
};
2828

29-
const { parseHTML } = require("linkedom");
29+
import { parseHTML } from "linkedom";
3030
const { document, window, CustomEvent } = parseHTML("...");
3131

3232
globalThis.indexedDB = { open: () => ({}) };
3333
globalThis.document = document;
3434
globalThis.Element = window.Element;
3535
globalThis.CustomEvent = CustomEvent;
3636
globalThis.MutationObserver = window.MutationObserver;
37+
globalThis.Worker = class {};
3738
globalThis.XPathResult = {};
3839
globalThis.XPathEvaluator =
3940
window.XPathEvaluator ||
@@ -43,9 +44,8 @@ globalThis.XPathEvaluator =
4344
}
4445
};
4546

46-
const polyscript = require("../cjs");
4747

48-
(async () => {
48+
import("../esm/index.js").then(async polyscript => {
4949
// shared 3rd party mocks
5050
const {
5151
python: pyodide,
@@ -212,4 +212,4 @@ const polyscript = require("../cjs");
212212
}
213213

214214
globalThis.URL = URL;
215-
})();
215+
});

test/integration.spec.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
const { existsSync, readdirSync } = require('node:fs');
2-
const { join } = require('node:path');
3-
const playwright = require('@playwright/test');
1+
import { existsSync, readdirSync } from 'node:fs';
2+
import { join } from 'node:path';
3+
import playwright from '@playwright/test';
4+
import { dirname } from "node:path";
5+
import { fileURLToPath } from "node:url";
6+
const __dirname = dirname(fileURLToPath(import.meta.url));
47

58
// integration tests for interpreters
69
const TEST_INTERPRETER = join(__dirname, 'integration');
@@ -11,7 +14,8 @@ const CORE_INTERPRETER = join(__dirname, '..', 'esm', 'interpreter');
1114
for (const file of readdirSync(TEST_INTERPRETER)) {
1215
// filter only JS files that match their counter-part interpreter
1316
if (/\.js$/.test(file) && existsSync(join(CORE_INTERPRETER, file))) {
14-
require(join(TEST_INTERPRETER, file))(
17+
const { default: test } = await import(join(TEST_INTERPRETER, file));
18+
test(
1519
playwright,
1620
`http://localhost:8080/test/integration/interpreter/${file.slice(0, -3)}`
1721
);

test/integration/_shared.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
exports.shared = {
3+
export const shared = {
44
bootstrap: ({ expect }, baseURL) => async ({ page }) => {
55
await page.goto(`${baseURL}/bootstrap.html`);
66
await page.waitForSelector('html.ready');
@@ -37,7 +37,7 @@ exports.shared = {
3737
},
3838
};
3939

40-
exports.python = {
40+
export const python = {
4141
bootstrap: ({ expect }, baseURL) => async ({ page }) => {
4242
await page.goto(`${baseURL}/bootstrap.html`);
4343
await page.waitForSelector('html.ready');

test/integration/micropython.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

3-
const { shared, python } = require('./_shared.js');
3+
import { shared, python } from './_shared.js';
44

5-
module.exports = (playwright, baseURL) => {
5+
export default (playwright, baseURL) => {
66
const { expect, test } = playwright;
77

88
test('MicroPython bootstrap', python.bootstrap(playwright, baseURL));

test/integration/pyodide.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

3-
const { shared, python } = require('./_shared.js');
3+
import { shared, python } from './_shared.js';
44

5-
module.exports = (playwright, baseURL) => {
5+
export default (playwright, baseURL) => {
66
const { expect, test } = playwright;
77

88
test('Pyodide bootstrap', python.bootstrap(playwright, baseURL));

test/integration/ruby-wasm-wasi.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

3-
const { shared } = require('./_shared.js');
3+
import { shared } from './_shared.js';
44

5-
module.exports = (playwright, baseURL) => {
5+
export default (playwright, baseURL) => {
66
const { test } = playwright;
77

88
test('Ruby WASM WASI bootstrap', shared.bootstrap(playwright, baseURL));

test/integration/wasmoon.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

3-
const { shared } = require('./_shared.js');
3+
import { shared } from './_shared.js';
44

5-
module.exports = (playwright, baseURL) => {
5+
export default (playwright, baseURL) => {
66
const { test } = playwright;
77

88
test('Wasmoon bootstrap', shared.bootstrap(playwright, baseURL));

test/integration/webr.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

3-
const { shared } = require('./_shared.js');
3+
import { shared } from './_shared.js';
44

5-
module.exports = (playwright, baseURL) => {
5+
export default (playwright, baseURL) => {
66
const { test } = playwright;
77

88
test('WebR just click', shared.justClick(playwright, baseURL));

test/package.json

-3
This file was deleted.

0 commit comments

Comments
 (0)