-
Notifications
You must be signed in to change notification settings - Fork 3.2k
[import-bytes] add import bytes #11657
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
base: main
Are you sure you want to change the base?
Changes from 10 commits
f8b1b09
183a2ce
b894372
9647411
e5b9757
9828655
9896faa
2c529f9
72f4bc0
e780b8b
8cf14bf
ef8b0c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -64211,6 +64211,21 @@ o............A....e | |||||
| response has a MIME type that is not a <span>JavaScript MIME type</span>.</p> | ||||||
| </div> | ||||||
|
|
||||||
| <div class="example" id="bytes-module-script-example"> | ||||||
| <p>The following sample shows how a <span>bytes module script</span> can be imported from inside | ||||||
| a <span>JavaScript module script</span>:</p> | ||||||
|
|
||||||
| <pre><code class="html" data-x=""><script type="module"> | ||||||
| import logoBytes from "https://resources.whatwg.org/logo.png" with { type: "bytes" }; | ||||||
|
|
||||||
| console.log("Binary data length:", logoBytes.byteLength); | ||||||
| </script></code></pre> | ||||||
|
|
||||||
| <p>The <span>bytes module script</span> will return a <code>Uint8Array</code> object containing | ||||||
| the raw bytes of the fetched resource. Unlike <span data-x="JSON module script">JSON module scripts</span> | ||||||
| which require a specific MIME type, bytes module scripts can be used to import binary data from any resource.</p> | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 9896faa (for some reason, github wouldn't accept the suggestion) |
||||||
| </div> | ||||||
|
|
||||||
| <div w-nodev> | ||||||
|
|
||||||
| <h5 id="script-processing-model">Processing model</h5> | ||||||
|
|
@@ -112104,7 +112119,7 @@ document.querySelector("button").addEventListener("click", bound); | |||||
| script">JavaScript module scripts</span>;</p></li> | ||||||
|
|
||||||
| <li><p>a <span>Synthetic Module Record</span>, for <span data-x="CSS module script">CSS module | ||||||
| scripts</span> and <span data-x="JSON module script">JSON module scripts</span>;</p></li> | ||||||
| scripts</span>, <span data-x="JSON module script">JSON module scripts</span>, and <span data-x="bytes module script">bytes module scripts</span>;</p></li> | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs wrapping at 100 columns as per guidelines.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 8cf14bf |
||||||
|
|
||||||
| <li><p>a <span>WebAssembly Module Record</span>, for <span data-x="WebAssembly module | ||||||
| script">WebAssembly module scripts</span>; or</p></li> | ||||||
|
|
@@ -112206,18 +112221,26 @@ document.querySelector("button").addEventListener("click", bound); | |||||
| --> | ||||||
| </li> | ||||||
|
|
||||||
| <li> | ||||||
| <p>A <span>module script</span> is a <dfn export>bytes module script</dfn> if its <span | ||||||
| data-x="concept-script-record">record</span> is a <span>Synthetic Module Record</span>, and it | ||||||
| was created via the <span data-x="creating a bytes module script">create a bytes module | ||||||
| script</span> algorithm. Bytes module scripts represent binary data as <code>Uint8Array</code> | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 9896faa (for some reason, github wouldn't accept the suggestion) |
||||||
| backed by an immutable <code data-x="idl-ArrayBuffer">ArrayBuffer</code>.</p> | ||||||
| </li> | ||||||
|
|
||||||
| <li> | ||||||
| <p>A <span>module script</span> is a <dfn export>WebAssembly module script</dfn> if | ||||||
| its <span data-x="concept-script-record">record</span> is a <span>WebAssembly Module | ||||||
|
annevk marked this conversation as resolved.
Outdated
|
||||||
| Record</span>.</p> | ||||||
| </li> | ||||||
| </ul> | ||||||
|
|
||||||
| <p class="note">As CSS style sheets and JSON documents do not import dependent modules, and do not | ||||||
| <p class="note">As CSS style sheets, JSON documents, and binary data do not import dependent modules, and do not | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe "As CSS style sheets and plain data ..." (which then encompasses both and also text in the future).
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 8cf14bf |
||||||
| throw exceptions on evaluation, the <span data-x="concept-script-script-fetch-options">fetch | ||||||
| options</span> and <span data-x="concept-script-base-url">base URL</span> of <span data-x="CSS | ||||||
| module script">CSS module scripts</span> and <span data-x="JSON module script">JSON module | ||||||
| scripts</span> and are always null.</p> | ||||||
| module script">CSS module scripts</span>, <span data-x="JSON module script">JSON module | ||||||
| scripts</span>, and <span data-x="bytes module script">bytes module scripts</span> are always null.</p> | ||||||
|
|
||||||
| <p>The <dfn>active script</dfn> is determined by the following algorithm:</p> | ||||||
|
|
||||||
|
|
@@ -113097,6 +113120,10 @@ document.querySelector("button").addEventListener("click", bound); | |||||
|
|
||||||
| <li><p>Let <var>moduleScript</var> be null.</p></li> | ||||||
|
|
||||||
| <li><p>If <var>moduleType</var> is "<code data-x="">bytes</code>", then set <var>moduleScript</var> to | ||||||
| the result of <span>creating a bytes module script</span> given <var>bodyBytes</var> and | ||||||
| <var>settingsObject</var>.</p></li> | ||||||
|
|
||||||
| <li><p>Let <var>referrerPolicy</var> be the result of <span | ||||||
| data-x="parse-referrer-policy-header">parsing the `<code>Referrer-Policy</code>` header</span> | ||||||
| given <var>response</var>. <ref>REFERRERPOLICY</ref></p></li> | ||||||
|
|
@@ -113432,6 +113459,35 @@ document.querySelector("button").addEventListener("click", bound); | |||||
| <li><p>Return <var>script</var>.</p></li> | ||||||
| </ol> | ||||||
|
|
||||||
| <p>To <dfn data-x="creating a bytes module script">create a bytes module script</dfn>, given a | ||||||
| byte sequence <var>bytes</var> and an <span>environment settings object</span> <var>settings</var>:</p> | ||||||
|
|
||||||
| <ol> | ||||||
| <li><p>Let <var>script</var> be a new <span>module script</span> that this algorithm will | ||||||
| subsequently initialize.</p></li> | ||||||
|
|
||||||
| <li><p>Set <var>script</var>'s <span data-x="concept-script-settings-object">settings | ||||||
| object</span> to <var>settings</var>.</p></li> | ||||||
|
|
||||||
| <li><p>Set <var>script</var>'s <span data-x="concept-script-base-url">base URL</span> and | ||||||
| <span data-x="concept-script-script-fetch-options">fetch options</span> to null.</p></li> | ||||||
|
|
||||||
| <li><p>Set <var>script</var>'s <span data-x="concept-script-parse-error">parse error</span> and | ||||||
| <span data-x="concept-script-error-to-rethrow">error to rethrow</span> to null.</p></li> | ||||||
|
|
||||||
| <li><p>Let <var>result</var> be a new <code>Uint8Array</code> object, in <var>settings</var>'s | ||||||
| <span data-x="environment settings object's realm">realm</span>, whose backing bytes are <var>bytes</var>.</p></li> | ||||||
|
|
||||||
| <li><p>Let <var>record</var> be <span>CreateBytesModule</span>(<var>immutableBytes</var>). <ref>JSIMPORTBYTES</ref></p></li> | ||||||
|
|
||||||
| <li><p>Set <var>script</var>'s <span data-x="concept-script-record">record</span> to <var>record</var>.</p></li> | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This appears in the wrong place. It should usually appear just before a
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 72f4bc0 |
||||||
|
|
||||||
| <li><p>Return <var>script</var>.</p></li> | ||||||
| </ol> | ||||||
|
|
||||||
| <p>User agents that support JavaScript must also implement the <cite>Import Bytes</cite> proposal. | ||||||
| The following terms are defined there, and used in this specification: <ref>JSIMPORTBYTES</ref></p> | ||||||
|
annevk marked this conversation as resolved.
|
||||||
|
|
||||||
| <p>The <dfn>module type from module request</dfn> steps, given a <span>ModuleRequest | ||||||
| Record</span> <var>moduleRequest</var>, are as follows:</p> | ||||||
|
|
||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You may want to use the creating an ArrayBufferView operation here (see e.g. the I don't think "backing bytes" is really a concept which exists anywhere.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, you might have to add a new argument for that or a separate operation.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I fully understand. Mind leaving a code suggestion?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the algorithm where creation of Uint8Array&friends is defined: https://github.com/whatwg/webidl/blob/a79e69ef7398ece546e526a1eebec977363c7304/index.bs#L9277-L9291 It needs to take a new parameter (I guess either a boolean or an enum like preserveResizability of https://tc39.es/proposal-immutable-arraybuffer/#sec-arraybuffercopyanddetach), propagate it to https://github.com/whatwg/webidl/blob/a79e69ef7398ece546e526a1eebec977363c7304/index.bs#L9252 and then call AllocateImmutableArrayBuffer (or ArrayBufferCopyAndDetach, depending on what is simpler) appropriately.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More precisely, as mentioned below, HTML should only be creating the ArrayBuffer, and then calling CreateBytesModule from the proposal. So it's only the create an ArrayBuffer algorithm which needs updated with the new parameter, not the algorithm for creating new views like Uint8Array.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So it sounds like I need to create a PR to the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah that seems right. |
||||||
|
|
@@ -113467,7 +113523,7 @@ document.querySelector("button").addEventListener("click", bound); | |||||
|
|
||||||
| <ol> | ||||||
| <li><p>If <var>moduleType</var> is not "<code data-x="">javascript-or-wasm</code>", "<code | ||||||
| data-x="">css</code>", or "<code data-x="">json</code>", then return false.</p></li> | ||||||
| data-x="">css</code>", "<code data-x="">json</code>", or "<code data-x="">bytes</code>", then return false.</p></li> | ||||||
|
|
||||||
| <li><p>If <var>moduleType</var> is "<code data-x="">css</code>" and the | ||||||
| <code>CSSStyleSheet</code> interface is not <span data-x="idl-exposed">exposed</span> in | ||||||
|
|
@@ -113488,6 +113544,9 @@ document.querySelector("button").addEventListener("click", bound); | |||||
| <li>If <var>moduleType</var> is "<code data-x="">css</code>", then return "<code | ||||||
| data-x="">style</code>".</li> | ||||||
|
|
||||||
| <li>If <var>moduleType</var> is "<code data-x="">bytes</code>", then return "<code | ||||||
| data-x="">empty</code>".</li> | ||||||
|
|
||||||
| <li>Return <var>defaultDestination</var>.</li> | ||||||
| </ol> | ||||||
|
|
||||||
|
|
@@ -150175,6 +150234,9 @@ INSERT INTERFACES HERE | |||||
| <dt id="refsJSINTL">[JSINTL]</dt> | ||||||
| <dd><cite><a href="https://tc39.es/ecma402/">ECMAScript Internationalization API Specification</a></cite>. Ecma International.</dd> | ||||||
|
|
||||||
| <dt id="refsJSIMPORTBYTES">[JSIMPORTBYTES]</dt> | ||||||
| <dd><cite><a href="https://tc39.es/proposal-import-bytes/">Import Bytes</a></cite>. Ecma International.</dd> | ||||||
|
|
||||||
| <dt id="refsJSON">[JSON]</dt> | ||||||
| <dd><cite><a href="https://www.rfc-editor.org/rfc/rfc8259">The JavaScript Object Notation (JSON) Data Interchange Format</a></cite>, T. Bray. IETF.</dd> | ||||||
|
|
||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Not a big fan of "binary data".)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in ef8b0c0