Skip to content

Relax SharedArrayBuffer requirement for named workers #107

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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: 1 addition & 1 deletion docs/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/index.js.map

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions esm/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,19 @@ export const handleCustomType = async (node) => {
try {
const worker = workerURL(node);
if (worker) {
const workerName = node.getAttribute('name');
const xworker = XW.call(new Hook(null, hooks), worker, {
...nodeInfo(node, type),
version,
configURL,
type: runtime,
custom: type,
config: node.getAttribute('config') || config || {},
async: node.hasAttribute('async')
async: node.hasAttribute('async'),
named: !!workerName,
});
defineProperty(node, 'xworker', { value: xworker });
resolve({ type, xworker });
const workerName = node.getAttribute('name');
if (workerName) workers[workerName].resolve(xworker.ready);
return;
}
Expand Down
3 changes: 2 additions & 1 deletion esm/script-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,18 @@ export const handle = async (script) => {
/* c8 ignore start */
const url = workerURL(script);
if (url) {
const workerName = wn?.value;
const XWorker = $xworker(type, versionValue);
const xworker = new XWorker(url, {
...nodeInfo(script, type),
async: !!isAsync,
config: configValue,
named: !!workerName,
});
handled.set(
defineProperty(script, 'xworker', { value: xworker }),
{ xworker },
);
const workerName = wn?.value;
if (workerName) workers[workerName].resolve(xworker.ready);
return;
}
Expand Down
23 changes: 15 additions & 8 deletions esm/worker/_template.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { configs, registry } from '../interpreters.js';
import { getRuntime, getRuntimeID } from '../loader.js';
import { patch, polluteJS, js as jsHooks, code as codeHooks } from '../hooks.js';

const SAB = 'SharedArrayBuffer';

let interpreter, runEvent, transform;
const add = (type, fn) => {
addEventListener(
Expand Down Expand Up @@ -43,7 +45,7 @@ const xworker = {
sync,
// allow access to the main thread world
window,
// allow introspection for foreign (main thread) refrences
// allow introspection for foreign (main thread) references
isWindowProxy,
// standard worker related events / features
onmessage: console.info,
Expand All @@ -55,7 +57,7 @@ const xworker = {
add('message', ({ data: { options, config: baseURL, configURL, code, hooks } }) => {
interpreter = (async () => {
try {
const { id, tag, type, custom, version, config, async: isAsync } = options;
const { id, tag, type, custom, version, config, named, async: isAsync } = options;

const runtimeID = getRuntimeID(type, version);

Expand Down Expand Up @@ -84,13 +86,18 @@ add('message', ({ data: { options, config: baseURL, configURL, code, hooks } })
// if it does throw and `sync_main_only` was not `true`
// then there's no way to go further
if (syncMainAndWorker) {
throw new Error(
[
'Unable to use SharedArrayBuffer due insecure environment.',
'Please read requirements in MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements',
].join('\n'),
);
const prefix = `Unable to use ${SAB}`;
if (!named || sync_main_only === false) {
throw new Error(
[
`${prefix} due insecure environment.`,
`Please read requirements in MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/${SAB}#security_requirements`,
].join('\n'),
);
}
console.warn(`${prefix}. Fallback to async only interaction.`);
}
syncMainAndWorker = false;
}

const details = create(registry.get(type));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,6 @@
"to-json-callback": "^0.1.1"
},
"worker": {
"blob": "sha256-zrvckOW/DJa6Ds33kWWyNZtvV7aajCLCG+5LnBkpLiA="
"blob": "sha256-FrixSFfNmrY7qKa+FUYOv6+j5y6mlvzlirb+qQII9SY="
}
}
29 changes: 29 additions & 0 deletions test/worker/element.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<title>named workers</title>
<script type="module" src="/dist/index.js"></script>
</head>
<body>
<script type="micropython" async>
from polyscript import workers

await (await workers["mpy"]).greetings()
await (await workers["py"]).greetings()
</script>
<script type="micropython" worker name="mpy">
def greetings():
print("micropython")

__export__ = ['greetings']
</script>
<script type="pyodide" config='{"sync_main_only":true}' worker name="py">
def greetings():
print("pyodide")

__export__ = ['greetings']
</script>
</body>
</html>