Skip to content

Commit f8b1b09

Browse files
committed
add import bytes
1 parent 553d412 commit f8b1b09

File tree

1 file changed

+66
-5
lines changed

1 file changed

+66
-5
lines changed

source

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64211,6 +64211,21 @@ o............A....e
6421164211
response has a MIME type that is not a <span>JavaScript MIME type</span>.</p>
6421264212
</div>
6421364213

64214+
<div class="example" id="bytes-module-script-example">
64215+
<p>The following sample shows how a <span>bytes module script</span> can be imported from inside
64216+
a <span>JavaScript module script</span>:</p>
64217+
64218+
<pre><code class="html" data-x="">&lt;script type="module">
64219+
import logoBytes from "https://resources.whatwg.org/logo.png" with { type: "bytes" };
64220+
64221+
console.log("Binary data length:", logoBytes.byteLength);
64222+
&lt;/script></code></pre>
64223+
64224+
<p>The <span>Bytes module script</span> will return a <code>Uint8Array</code> object containing
64225+
the raw bytes of the fetched resource. Unlike <span data-x="JSON module script">JSON module scripts</span>
64226+
which require a specific MIME type, bytes modules can be used to import binary data from any resource.</p>
64227+
</div>
64228+
6421464229
<div w-nodev>
6421564230

6421664231
<h5 id="script-processing-model">Processing model</h5>
@@ -112104,7 +112119,7 @@ document.querySelector("button").addEventListener("click", bound);
112104112119
script">JavaScript module scripts</span>;</p></li>
112105112120

112106112121
<li><p>a <span>Synthetic Module Record</span>, for <span data-x="CSS module script">CSS module
112107-
scripts</span> and <span data-x="JSON module script">JSON module scripts</span>;</p></li>
112122+
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>
112108112123

112109112124
<li><p>a <span>WebAssembly Module Record</span>, for <span data-x="WebAssembly module
112110112125
script">WebAssembly module scripts</span>; or</p></li>
@@ -112206,18 +112221,32 @@ document.querySelector("button").addEventListener("click", bound);
112206112221
-->
112207112222
</li>
112208112223

112224+
<li>
112225+
<p>A <span>module script</span> is a <dfn export>Bytes module script</dfn> if its <span
112226+
data-x="concept-script-record">record</span> is a <span>Synthetic Module Record</span>, and it
112227+
was created via the <span data-x="creating a Bytes module script">create a Bytes module
112228+
script</span> algorithm. Bytes module scripts represent binary data as Uint8Array backed by
112229+
an immutable ArrayBuffer.</p>
112230+
112231+
<!--
112232+
This definition is not super-rigorous, but it doesn't need to be for now. If we ever start
112233+
testing if something is a Bytes module script in algorithms, instead of just referring to the
112234+
concept, then we should consider adding a type item to the module script struct.
112235+
-->
112236+
</li>
112237+
112209112238
<li>
112210112239
<p>A <span>module script</span> is a <dfn export>WebAssembly module script</dfn> if
112211112240
its <span data-x="concept-script-record">record</span> is a <span>WebAssembly Module
112212112241
Record</span>.</p>
112213112242
</li>
112214112243
</ul>
112215112244

112216-
<p class="note">As CSS style sheets and JSON documents do not import dependent modules, and do not
112245+
<p class="note">As CSS stylesheets, JSON documents, and binary data do not import dependent modules, and do not
112217112246
throw exceptions on evaluation, the <span data-x="concept-script-script-fetch-options">fetch
112218112247
options</span> and <span data-x="concept-script-base-url">base URL</span> of <span data-x="CSS
112219-
module script">CSS module scripts</span> and <span data-x="JSON module script">JSON module
112220-
scripts</span> and are always null.</p>
112248+
module script">CSS module scripts</span>, <span data-x="JSON module script">JSON module
112249+
scripts</span>, and <span data-x="Bytes module script">Bytes module scripts</span> are always null.</p>
112221112250

112222112251
<p>The <dfn>active script</dfn> is determined by the following algorithm:</p>
112223112252

@@ -113112,6 +113141,10 @@ document.querySelector("button").addEventListener("click", bound);
113112113141
<var>settingsObject</var>, <var>response</var>'s <span
113113113142
data-x="concept-response-url">URL</span>, and <var>options</var>.</p></li>
113114113143

113144+
<li><p>If <var>moduleType</var> is "<code data-x="">bytes</code>", then set <var>moduleScript</var> to
113145+
the result of <span>creating a Bytes module script</span> given <var>bodyBytes</var> and
113146+
<var>settingsObject</var>.</p></li>
113147+
113115113148
<li>
113116113149
<p>Otherwise:</p>
113117113150
<ol>
@@ -113432,6 +113465,31 @@ document.querySelector("button").addEventListener("click", bound);
113432113465
<li><p>Return <var>script</var>.</p></li>
113433113466
</ol>
113434113467

113468+
<p>To <dfn data-x="creating a Bytes module script">create a Bytes module script</dfn>, given a
113469+
byte sequence <var>bytes</var> and an <span>environment settings object</span> <var>settings</var>:</p>
113470+
113471+
<ol>
113472+
<li><p>Let <var>script</var> be a new <span>module script</span> that this algorithm will
113473+
subsequently initialize.</p></li>
113474+
113475+
<li><p>Set <var>script</var>'s <span data-x="concept-script-settings-object">settings
113476+
object</span> to <var>settings</var>.</p></li>
113477+
113478+
<li><p>Set <var>script</var>'s <span data-x="concept-script-base-url">base URL</span> and
113479+
<span data-x="concept-script-script-fetch-options">fetch options</span> to null.</p></li>
113480+
113481+
<li><p>Set <var>script</var>'s <span data-x="concept-script-parse-error">parse error</span> and
113482+
<span data-x="concept-script-error-to-rethrow">error to rethrow</span> to null.</p></li>
113483+
113484+
<li><p>Let <var>result</var> be a new <code>Uint8Array</code> object, in <var>settings</var>'s
113485+
<span data-x="environment settings object's realm">realm</span>, whose backing bytes are <var>bytes</var>.</p></li>
113486+
113487+
<li><p>Set <var>script</var>'s <span data-x="concept-script-record">record</span> to the result
113488+
of <span>CreateDefaultExportSyntheticModule</span>(<var>result</var>).</p></li>
113489+
113490+
<li><p>Return <var>script</var>.</p></li>
113491+
</ol>
113492+
113435113493
<p>The <dfn>module type from module request</dfn> steps, given a <span>ModuleRequest
113436113494
Record</span> <var>moduleRequest</var>, are as follows:</p>
113437113495

@@ -113467,7 +113525,7 @@ document.querySelector("button").addEventListener("click", bound);
113467113525

113468113526
<ol>
113469113527
<li><p>If <var>moduleType</var> is not "<code data-x="">javascript-or-wasm</code>", "<code
113470-
data-x="">css</code>", or "<code data-x="">json</code>", then return false.</p></li>
113528+
data-x="">css</code>", "<code data-x="">json</code>", or "<code data-x="">bytes</code>", then return false.</p></li>
113471113529

113472113530
<li><p>If <var>moduleType</var> is "<code data-x="">css</code>" and the
113473113531
<code>CSSStyleSheet</code> interface is not <span data-x="idl-exposed">exposed</span> in
@@ -113488,6 +113546,9 @@ document.querySelector("button").addEventListener("click", bound);
113488113546
<li>If <var>moduleType</var> is "<code data-x="">css</code>", then return "<code
113489113547
data-x="">style</code>".</li>
113490113548

113549+
<li>If <var>moduleType</var> is "<code data-x="">bytes</code>", then return "<code
113550+
data-x="">empty</code>".</li>
113551+
113491113552
<li>Return <var>defaultDestination</var>.</li>
113492113553
</ol>
113493113554

0 commit comments

Comments
 (0)