From a8cd2770033ed50056f37b935a7d3d5412c5056f Mon Sep 17 00:00:00 2001 From: Lucas Garron Date: Fri, 8 Sep 2023 08:43:39 -0700 Subject: [PATCH] Initial version. As suggest at: https://github.com/whatwg/html/issues/6911#issuecomment-1711316779 --- Makefile | 2 +- README.md | 129 ++++++++++++++++++ package-lock.json | 2 +- src/classic-worker.js | 5 - src/module-worker.js | 5 - .../WorkerExecutionOriginPolyfill.d.ts | 2 +- .../WorkerExecutionOriginPolyfill.js | 2 +- {src => test}/assert.js | 0 test/classic-worker.js | 5 + {src => test}/index.css | 0 {src => test}/index.html | 0 {src => test}/index.js | 8 +- test/module-worker.js | 5 + 13 files changed, 147 insertions(+), 18 deletions(-) create mode 100644 README.md delete mode 100644 src/classic-worker.js delete mode 100644 src/module-worker.js rename {src => test}/WorkerExecutionOriginPolyfill.d.ts (78%) rename {src => test}/WorkerExecutionOriginPolyfill.js (93%) rename {src => test}/assert.js (100%) create mode 100644 test/classic-worker.js rename {src => test}/index.css (100%) rename {src => test}/index.html (100%) rename {src => test}/index.js (89%) create mode 100644 test/module-worker.js diff --git a/Makefile b/Makefile index 7c74aab..5db143d 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ .PHONY: serve serve: node_modules - npx http-server src --cors --port 8080 + npx http-server test --cors --port 8080 .PHONY: node_modules node_modules: diff --git a/README.md b/README.md new file mode 100644 index 0000000..216224d --- /dev/null +++ b/README.md @@ -0,0 +1,129 @@ +# Worker `executionOrigin` Proposal + +This proposal aims to make it easy for JavaScript libraries to instantiate web workers when hosted on a CDN (or any origin that does not match) by introducing a `executionOrigin` option to the `Worker` constructor: + +```js +// https://web-app.example.com +import { calculation } from "https://cdn.example.com/lib/index.js"; +console.log(await calculation()); + +// https://cdn.example.com/lib/index.js (served using CORS) +export async function calculation() { + const worker = new Worker(import.meta.resolve("./worker.js"), { + "type": "module", + "executionOrigin": "from-calling-script" + }) + … + return …; +} + +// https://cdn.example.com/lib/worker.js (served using CORS) +self.addEventListener("message", function (event) { + // heavy work can go here without freezing the main thread +}); +``` + +## Motivation + +[Web workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers) are invaluable for implementing computationally intensive operations on the web, [without blocking the main thread](https://web.dev/off-main-thread/). + +Unfortunately, writing portable web worker code [has always been +difficult](https://github.com/whatwg/html/issues/6911). This has gotten easier over time, due to new features like +[`import.meta.resolve(…)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import.meta/resolve). However, it remains difficult to instantiate a web worker using a URL that does +not share its origin with the calling script — the "CDN problem". This is because the default +execution origin of the a worker comes from its URL rather than the script that +is instantiating it, which blocks the worker code due the [same-origin +policy](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy).[^1] + +[^1]:Note that this is in contrast with the `