Skip to content

Commit a3308ac

Browse files
committed
Fix #43 - Break src + worker urls
1 parent 1da978d commit a3308ac

File tree

17 files changed

+71
-76
lines changed

17 files changed

+71
-76
lines changed

core.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ The list of options' fields is described as such and all of these are *optional*
444444
| :------------------------ | :-------------------------------------------- | :--------|
445445
| version | `{verstion: '0.23.2'}` | Allow the usage of a specific version of an interpreter, same way `version` attribute works with `<script>` elements. |
446446
| config | `{config: 'type.toml'}` `{config: {}}` | Ensure such config is already parsed and available, if not already passed as object, for every custom `type` that execute code. |
447+
| onerror | `(error, element) => { throw error; } | Allows custom types to intercept early errors possibly happened while bootstrapping elements. |
447448
| env | `{env: 'my-project'}` | Guarantee same environment for every custom `type`, avoiding conflicts with any other possible default or custom environment. |
448449
| onInterpreterReady | `{onInterpreterReady(wrap, element) {}}` | This is the main entry point to define anything extra to the context of the always same interpreter. This callback is *awaited* and executed, after the desired *interpreter* is fully available and bootstrapped *once* though other optional fields, per each element that matches the defined `type`. The `wrap` reference contains many fields and utilities helpful to run most common operations, and it is passed along most other options too, when defined. |
449450
| onBeforeRun | `{onBeforeRun(wrap, element) {}}` | This is a **hook** into the logic that runs right before any *interpreter* `run(...)` is performed. It receives the same `wrap` already sent when *onInterpreterReady* executes, and it passes along the current `element` that is going to execute such code. |

docs/core.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/core.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

esm/custom.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,29 @@ export const handleCustomType = (node) => {
4545
version,
4646
env,
4747
onInterpreterReady,
48+
onerror,
4849
} = options;
4950

50-
const worker = workerURL(node);
51-
if (worker) {
52-
const xworker = XWorker.call(new Hook(null, options), worker, {
53-
...nodeInfo(node, type),
54-
version,
55-
type: runtime,
56-
custom: type,
57-
config: node.getAttribute('config') || config || {},
58-
async: node.hasAttribute('async')
59-
});
60-
defineProperty(node, 'xworker', { value: xworker });
61-
resolve({ type, xworker });
62-
return;
51+
let error;
52+
try {
53+
const worker = workerURL(node);
54+
if (worker) {
55+
const xworker = XWorker.call(new Hook(null, options), worker, {
56+
...nodeInfo(node, type),
57+
version,
58+
type: runtime,
59+
custom: type,
60+
config: node.getAttribute('config') || config || {},
61+
async: node.hasAttribute('async')
62+
});
63+
defineProperty(node, 'xworker', { value: xworker });
64+
resolve({ type, xworker });
65+
return;
66+
}
67+
}
68+
// let the custom type handle errors via its `io`
69+
catch (workerError) {
70+
error = workerError;
6371
}
6472

6573
const name = getRuntimeID(runtime, version);
@@ -138,6 +146,8 @@ export const handleCustomType = (node) => {
138146

139147
resolve(resolved);
140148

149+
if (error) onerror?.(error, node);
150+
141151
onInterpreterReady?.(resolved, node);
142152
});
143153
}

esm/worker/url.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@ import { dedent } from '../utils.js';
22

33
/* c8 ignore start */ // tested via integration
44
export default element => {
5-
const { worker } = element.attributes;
5+
const { src, worker } = element.attributes;
66
if (worker) {
77
let { value } = worker;
8+
// throw on worker values as ambiguous
9+
// @see https://github.com/pyscript/polyscript/issues/43
10+
if (value) throw new SyntaxError(`Invalid worker attribute: ${value}`);
11+
value = src?.value;
812
if (!value) {
13+
// throw on empty src attributes
14+
if (src) throw new SyntaxError('Invalid src attribute');
915
if (!element.childElementCount)
1016
value = element.textContent;
1117
else {

test/integration.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<title>polyscript integration tests</title>
77
</head>
8-
<body><ul><li><strong>micropython</strong><ul><li><a href="/test/integration/interpreter/micropython/bootstrap.html">bootstrap</a></li><li><a href="/test/integration/interpreter/micropython/config-object.html">config-object</a></li><li><a href="/test/integration/interpreter/micropython/fetch.html">fetch</a></li><li><a href="/test/integration/interpreter/micropython/no-type.html">no-type</a></li><li><a href="/test/integration/interpreter/micropython/worker-attribute.html">worker-attribute</a></li><li><a href="/test/integration/interpreter/micropython/worker-empty-attribute.html">worker-empty-attribute</a></li><li><a href="/test/integration/interpreter/micropython/worker-error.html">worker-error</a></li><li><a href="/test/integration/interpreter/micropython/worker-lua.html">worker-lua</a></li><li><a href="/test/integration/interpreter/micropython/worker-tag.html">worker-tag</a></li><li><a href="/test/integration/interpreter/micropython/worker-window.html">worker-window</a></li><li><a href="/test/integration/interpreter/micropython/worker.html">worker</a></li></ul><li><strong>pyodide</strong><ul><li><a href="/test/integration/interpreter/pyodide/bootstrap.html">bootstrap</a></li><li><a href="/test/integration/interpreter/pyodide/fetch.html">fetch</a></li><li><a href="/test/integration/interpreter/pyodide/sync.html">sync</a></li><li><a href="/test/integration/interpreter/pyodide/worker-error.html">worker-error</a></li><li><a href="/test/integration/interpreter/pyodide/worker-transform.html">worker-transform</a></li><li><a href="/test/integration/interpreter/pyodide/worker.html">worker</a></li></ul><li><strong>ruby-wasm-wasi</strong><ul><li><a href="/test/integration/interpreter/ruby-wasm-wasi/bootstrap.html">bootstrap</a></li></ul><li><strong>wasmoon</strong><ul><li><a href="/test/integration/interpreter/wasmoon/bootstrap.html">bootstrap</a></li><li><a href="/test/integration/interpreter/wasmoon/worker.html">worker</a></li></ul></ul></body>
8+
<body><ul><li><strong>micropython</strong><ul><li><a href="/test/integration/interpreter/micropython/bootstrap.html">bootstrap</a></li><li><a href="/test/integration/interpreter/micropython/config-object.html">config-object</a></li><li><a href="/test/integration/interpreter/micropython/fetch.html">fetch</a></li><li><a href="/test/integration/interpreter/micropython/no-type.html">no-type</a></li><li><a href="/test/integration/interpreter/micropython/worker-attribute.html">worker-attribute</a></li><li><a href="/test/integration/interpreter/micropython/worker-bad.html">worker-bad</a></li><li><a href="/test/integration/interpreter/micropython/worker-empty-attribute.html">worker-empty-attribute</a></li><li><a href="/test/integration/interpreter/micropython/worker-error.html">worker-error</a></li><li><a href="/test/integration/interpreter/micropython/worker-lua.html">worker-lua</a></li><li><a href="/test/integration/interpreter/micropython/worker-tag.html">worker-tag</a></li><li><a href="/test/integration/interpreter/micropython/worker-window.html">worker-window</a></li><li><a href="/test/integration/interpreter/micropython/worker.html">worker</a></li></ul><li><strong>pyodide</strong><ul><li><a href="/test/integration/interpreter/pyodide/bootstrap.html">bootstrap</a></li><li><a href="/test/integration/interpreter/pyodide/fetch.html">fetch</a></li><li><a href="/test/integration/interpreter/pyodide/sync.html">sync</a></li><li><a href="/test/integration/interpreter/pyodide/worker-error.html">worker-error</a></li><li><a href="/test/integration/interpreter/pyodide/worker-transform.html">worker-transform</a></li><li><a href="/test/integration/interpreter/pyodide/worker.html">worker</a></li></ul><li><strong>ruby-wasm-wasi</strong><ul><li><a href="/test/integration/interpreter/ruby-wasm-wasi/bootstrap.html">bootstrap</a></li></ul><li><strong>wasmoon</strong><ul><li><a href="/test/integration/interpreter/wasmoon/bootstrap.html">bootstrap</a></li><li><a href="/test/integration/interpreter/wasmoon/worker.html">worker</a></li></ul></ul></body>
99
</html>

test/integration/_shared.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,12 @@ exports.python = {
108108
await expect(logs.pop()).toBe('worker attribute');
109109
await expect(logs.pop().slice(0, 11)).toBe('Deprecated:');
110110
},
111+
112+
workerTagBadAttribute: ({ expect }, url) => async ({ page }) => {
113+
const logs = [];
114+
page.on('console', msg => logs.push(msg.text()));
115+
await page.goto(url);
116+
await page.waitForSelector('html.error');
117+
await expect(logs.pop()).toBe('Invalid worker attribute: nope.py');
118+
},
111119
};

test/integration/interpreter/micropython/worker-attribute.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
<script type="module" src="/core.js"></script>
77
</head>
88
<body>
9-
<script type="micropython" worker="../worker_attribute.py"></script>
9+
<script type="micropython" src="../worker_attribute.py" worker></script>
1010
</body>
1111
</html>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<script type="module">
7+
import { define } from '/core.js';
8+
define('mpy', {
9+
interpreter: 'micropython',
10+
onerror({ message }) {
11+
console.error(message);
12+
document.documentElement.classList.add('error');
13+
}
14+
});
15+
</script>
16+
</head>
17+
<body>
18+
<mpy-script worker="nope.py"></mpy-script>
19+
</body>
20+
</html>

0 commit comments

Comments
 (0)