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 `