From 9a20976a588a96cbf598cbd4408a5ed0f3a9536f Mon Sep 17 00:00:00 2001 From: RedYetiDev <38299977+RedYetiDev@users.noreply.github.com> Date: Thu, 29 Aug 2024 17:30:49 -0400 Subject: [PATCH 1/3] module: support `import()`-ing `blob:` urls --- doc/api/esm.md | 34 +++++++++++-- lib/internal/modules/esm/get_format.js | 10 +++- lib/internal/modules/esm/load.js | 9 +++- test/es-module/test-esm-blob-urls.js | 68 ++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 6 deletions(-) create mode 100644 test/es-module/test-esm-blob-urls.js diff --git a/doc/api/esm.md b/doc/api/esm.md index d1215b3caf5..09179951abe 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -189,7 +189,7 @@ typically configured server. ES modules are resolved and cached as URLs. This means that special characters must be [percent-encoded][], such as `#` with `%23` and `?` with `%3F`. -`file:`, `node:`, and `data:` URL schemes are supported. A specifier like +`file:`, `node:`, `data:`, and `blob:` URL schemes are supported. A specifier like `'https://example.com/app.js'` is not supported natively in Node.js unless using a [custom HTTPS loader][]. @@ -231,6 +231,34 @@ and [absolute specifiers][Terminology]. Resolving from `data:text/javascript,import "./foo";` fails to resolve because there is no concept of relative resolution for `data:` URLs. +#### `blob:` imports + + + +`blob:` URLs are supported for importing with the following MIME types: + +* `text/javascript` for ES modules +* `application/json` for JSON +* `application/wasm` for Wasm + +Blob URLs are registered within the current thread. Loaders will not be +able to handle resolve Blob's from the main thread. + +```mjs +import { Blob } from 'node:buffer'; + +const mjs = new Blob(['export "Hello, World!"'], { type: 'text/javascript' }); +await import(URL.createObjectURL(mjs)); + +const json = new Blob(['{}'], { type: 'application/json' }); +await import(URL.createObjectURL(json), { with: { type: 'json' } }); + +const wasm = new Blob(['WASM Data'], { type: 'application/wasm' }); +await import(URL.createObjectURL(wasm)); +``` + #### `node:` imports +> Stability: 1 - Experimental + `blob:` URLs are supported for importing with the following MIME types: * `text/javascript` for ES modules From b57674ee8f69cc2a54df0a8795ecd53263ace60e Mon Sep 17 00:00:00 2001 From: Aviv Keller Date: Sun, 1 Sep 2024 14:14:39 -0400 Subject: [PATCH 3/3] Update esm.md --- doc/api/esm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/esm.md b/doc/api/esm.md index 08d135bdc6a..98e7b501415 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -246,7 +246,7 @@ added: REPLACEME * `application/wasm` for Wasm Blob URLs are registered within the current thread. Loaders will not be -able to handle resolve Blob's from the main thread. +able to resolve Blob URLs created from the main thread. ```mjs import { Blob } from 'node:buffer';