Skip to content

Commit

Permalink
Support passing an object as the import map to Worker
Browse files Browse the repository at this point in the history
This commit allows passing a JavaScript object to `Worker`'s `importMap`
option, which is validated and used as the worker's import map.

The existing validation of the JSON import map has been delegated to
WebIDL, so that it can be re-used for this option's value.
  • Loading branch information
nicolo-ribaudo committed Nov 23, 2024
1 parent cec5fcc commit 6533032
Showing 1 changed file with 51 additions and 43 deletions.
94 changes: 51 additions & 43 deletions source
Original file line number Diff line number Diff line change
Expand Up @@ -2318,6 +2318,7 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute
<li>The <dfn data-x-href="https://infra.spec.whatwg.org/#forgiving-base64-encode">forgiving-base64 encode</dfn> and
<dfn data-x-href="https://infra.spec.whatwg.org/#forgiving-base64-decode">forgiving-base64 decode</dfn> algorithms</li>
<li><dfn data-x-href="https://infra.spec.whatwg.org/#the-exclusive-range">exclusive range</dfn></li>
<li><dfn data-x-href="https://infra.spec.whatwg.org/#parse-a-json-string-to-a-javascript-value">parse a JSON string to a JavaScript value</dfn></li>
<li><dfn data-x-href="https://infra.spec.whatwg.org/#parse-a-json-string-to-an-infra-value">parse a JSON string to an Infra value</dfn></li>
<li><dfn data-x-href="https://infra.spec.whatwg.org/#html-namespace">HTML namespace</dfn></li>
<li><dfn data-x-href="https://infra.spec.whatwg.org/#mathml-namespace">MathML namespace</dfn></li>
Expand Down Expand Up @@ -2878,6 +2879,7 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute
<li><dfn data-x="LegacyUnenumerableNamedProperties" data-x-href="https://webidl.spec.whatwg.org/#LegacyUnenumerableNamedProperties"><code>[LegacyUnenumerableNamedProperties]</code></dfn></li>
<li><dfn data-x="LegacyUnforgeable" data-x-href="https://webidl.spec.whatwg.org/#LegacyUnforgeable"><code>[LegacyUnforgeable]</code></dfn></li>
<li><dfn data-x-href="https://webidl.spec.whatwg.org/#dfn-set-entries">set entries</dfn></li>
<li><dfn data-x-href="https://webidl.spec.whatwg.org/#dfn-convert-ecmascript-to-idl-value">convert to an IDL value</dfn></li>
</ul>

<p><cite>Web IDL</cite> also defines the following types that are used in Web IDL fragments in
Expand Down Expand Up @@ -109747,27 +109749,51 @@ dictionary <dfn dictionary>PromiseRejectionEventInit</dfn> : <span>EventInit</sp
</ol>
<hr>

<pre><code class="idl">dictionary <dfn dictionary>ImportMap</dfn> {
record&lt;DOMString, any> imports;
record&lt;DOMString, record&lt;DOMString, any>> scopes;
record&lt;DOMString, any> integrity;
};</code></pre>

<p>To <dfn>parse an import map string</dfn>, given a <span>string</span> <var>input</var> and a
<span>URL</span> <var>baseURL</var>:</p>

<ol>
<li><p>Let <var>parsed</var> be the result of <span data-x="parse a JSON string to an Infra
value">parsing a JSON string to an Infra value</span> given <var>input</var>.</p></li>
<li><p>Let <var>jsVal</var> be the result of <span data-x="parse a JSON string to a JavaScript
value">parsing a JSON string to a JavaScript value</span> given <var>input</var>.</p></li>

<li><p>If <var>jsVal</var> is null or undefined, throw a <code>TypeError</code> indicating that
the top-level value needs to be a JSON object.</p></li>

<li><p>Let <var>parsed</var> be <var>jsVal</var> <span data-x="convert to an IDL
value">converted</span> to an <code>ImportMap</code>.</p>

<li><p>Let <var>importMap</var> be the result of <span data-x="create an import map">creating an
import map</span> given <var>parsed</var> and <var>baseURL</var>.</p></li>

<li><p>If <var>parsed</var> is not an <span>ordered map</span>, then throw a
<code>TypeError</code> indicating that the top-level value needs to be a JSON object.</p></li>
<li>
<p>If <var>jsVal</var>.[[OwnPropertyKeys]]() <span data-x="list contains">contains</span> any
items besides "<code data-x="">imports</code>", "<code data-x="">scopes</code>", or "<code
data-x="">integrity</code>", then the user agent should <span>report a warning to the
console</span> indicating that an invalid top-level key was present in the import map.</p>

<p class="note">This can help detect typos. It is not an error, because that would prevent any
future extensions from being added backward-compatibly.</p>
</li>

<li><p>Return <var>importMap</var>.</p></li>
</ol>

<p>To <dfn>create an import map</dfn>, given an <code>ImportMap</code> <var>parsed</var> and a
<span>URL</span> <var>baseURL</var>:</p>

<ol>
<li><p>Let <var>sortedAndNormalizedImports</var> be an empty <span>ordered map</span>.</p></li>

<li>
<p>If <var>parsed</var>["<code data-x="">imports</code>"] <span data-x="map
exists">exists</span>, then:</p>
<p>If <var>parsed</var>["<code data-x="">imports</code>"] is not undefined, then:</p>

<ol>
<li><p>If <var>parsed</var>["<code data-x="">imports</code>"] is not an <span>ordered
map</span>, then throw a <code>TypeError</code> indicating that the value for the "<code
data-x="">imports</code>" top-level key needs to be a JSON object.</p></li>

<li><p>Set <var>sortedAndNormalizedImports</var> to the result of <span>sorting and
normalizing a module specifier map</span> given <var>parsed</var>["<code
data-x="">imports</code>"] and <var>baseURL</var>.</p></li>
Expand All @@ -109777,14 +109803,9 @@ dictionary <dfn dictionary>PromiseRejectionEventInit</dfn> : <span>EventInit</sp
<li><p>Let <var>sortedAndNormalizedScopes</var> be an empty <span>ordered map</span>.</p></li>

<li>
<p>If <var>parsed</var>["<code data-x="">scopes</code>"] <span data-x="map
exists">exists</span>, then:</p>
<p>If <var>parsed</var>["<code data-x="">scopes</code>"] is not undefined, then:</p>

<ol>
<li><p>If <var>parsed</var>["<code data-x="">scopes</code>"] is not an <span>ordered
map</span>, then throw a <code>TypeError</code> indicating that the value for the "<code
data-x="">scopes</code>" top-level key needs to be a JSON object.</p></li>

<li><p>Set <var>sortedAndNormalizedScopes</var> to the result of <span>sorting and normalizing
scopes</span> given <var>parsed</var>["<code data-x="">scopes</code>"] and
<var>baseURL</var>.</p></li>
Expand All @@ -109794,31 +109815,15 @@ dictionary <dfn dictionary>PromiseRejectionEventInit</dfn> : <span>EventInit</sp
<li><p>Let <var>normalizedIntegrity</var> be an empty <span>ordered map</span>.</p></li>

<li>
<p>If <var>parsed</var>["<code data-x="">integrity</code>"] <span data-x="map
exists">exists</span>, then:</p>
<p>If <var>parsed</var>["<code data-x="">integrity</code>"] is not undefined, then:</p>

<ol>
<li><p>If <var>parsed</var>["<code data-x="">integrity</code>"] is not an <span>ordered
map</span>, then throw a <code>TypeError</code> indicating that the value for the "<code
data-x="">integrity</code>" top-level key needs to be a JSON object.</p></li>

<li><p>Set <var>normalizedIntegrity</var> to the result of <span>normalizing a module
integrity map</span> given <var>parsed</var>["<code data-x="">integrity</code>"] and
<var>baseURL</var>.</p></li>
</ol>
</li>

<li>
<p>If <var>parsed</var>'s <span data-x="map key">keys</span> <span data-x="list
contains">contains</span> any items besides "<code data-x="">imports</code>", "<code
data-x="">scopes</code>", or "<code data-x="">integrity</code>", then the user agent should
<span>report a warning to the console</span> indicating that an invalid top-level key was
present in the import map.</p>

<p class="note">This can help detect typos. It is not an error, because that would prevent any
future extensions from being added backward-compatibly.</p>
</li>

<li><p>Return an <span>import map</span> whose <span
data-x="concept-import-map-imports">imports</span> are <var>sortedAndNormalizedImports</var>,
whose <span data-x="concept-import-map-scopes">scopes</span> are
Expand Down Expand Up @@ -110197,8 +110202,8 @@ dictionary <dfn dictionary>PromiseRejectionEventInit</dfn> : <span>EventInit</sp
</div>

<p>To <dfn data-x="sorting and normalizing a module specifier map">sort and normalize a module
specifier map</dfn>, given an <span>ordered map</span> <var>originalMap</var> and a
<span>URL</span> <var>baseURL</var>:</p>
specifier map</dfn>, given a <code data-x="">record&lt;DOMString, any></code>
<var>originalMap</var> and a <span>URL</span> <var>baseURL</var>:</p>

<ol>
<li><p>Let <var>normalized</var> be an empty <span>ordered map</span>.</p></li>
Expand Down Expand Up @@ -110269,8 +110274,9 @@ dictionary <dfn dictionary>PromiseRejectionEventInit</dfn> : <span>EventInit</sp
<var>b</var>'s <span data-x="map key">key</span>.</p></li>
</ol>

<p>To <dfn data-x="sorting and normalizing scopes">sort and normalize scopes</dfn>, given an
<span>ordered map</span> <var>originalMap</var> and a <span>URL</span> <var>baseURL</var>:</p>
<p>To <dfn data-x="sorting and normalizing scopes">sort and normalize scopes</dfn>, given a <code
data-x="">record&lt;DOMString, record&lt;DOMString, any>></code> <var>originalMap</var> and a
<span>URL</span> <var>baseURL</var>:</p>

<ol>
<li><p>Let <var>normalized</var> be an empty <span>ordered map</span>.</p></li>
Expand All @@ -110280,10 +110286,6 @@ dictionary <dfn dictionary>PromiseRejectionEventInit</dfn> : <span>EventInit</sp
<var>potentialSpecifierMap</var> of <var>originalMap</var>:</p>

<ol>
<li><p>If <var>potentialSpecifierMap</var> is not an <span>ordered map</span>, then throw a
<code>TypeError</code> indicating that the value of the scope with prefix
<var>scopePrefix</var> needs to be a JSON object.</p></li>

<li><p>Let <var>scopePrefixURL</var> be the result of <span data-x="URL parser">URL
parsing</span> <var>scopePrefix</var> with <var>baseURL</var>.</p></li>

Expand Down Expand Up @@ -110319,7 +110321,7 @@ dictionary <dfn dictionary>PromiseRejectionEventInit</dfn> : <span>EventInit</sp
during <span data-x="resolve a module specifier">module specifier resolution</span>.</p>

<p>To <dfn data-x="normalizing a module integrity map">normalize a module integrity map</dfn>,
given an <span>ordered map</span> <var>originalMap</var>:</p>
given a <code data-x="">record&lt;DOMString, any></code> <var>originalMap</var>:</p>

<ol>
<li><p>Let <var>normalized</var> be an empty <span>ordered map</span>.</p></li>
Expand Down Expand Up @@ -121021,7 +121023,7 @@ dictionary <dfn dictionary>WorkerOptions</dfn> {
};

dictionary <dfn dictionary>DedicatedWorkerOptions</dfn> : <span>WorkerOptions</span> {
<span>WorkerImportMapInheritance</span> importMap = "none";
(<span>WorkerImportMapInheritance</span> or <span>ImportMap</span>) importMap = "none";
};

enum <dfn enum>WorkerType</dfn> { "classic", "module" };
Expand Down Expand Up @@ -121136,6 +121138,12 @@ enum <dfn enum>WorkerImportMapInheritance</dfn> { "none", "clone" };
updates will not be reflected in the cloned import map.</p>
</li>

<li><p>Else, if <var>options</var>'s <code data-x="">importMap</code> member is an
<code>ImportMap</code>, set <var>importMap</var> to the result of <span data-x="create an import
map">creating an import map</span> given <var>options</var>'s <code data-x="">importMap</code>
member and <span class="XXX">TODO: which base URL? The worker's, or the
creator's?</span>.</p></li>

<li><p>Let <var>worker</var> be a new <code>Worker</code> object.</p></li>

<li><p>Let <var>outside port</var> be a <span>new</span> <code>MessagePort</code> in <var>outside
Expand Down

0 comments on commit 6533032

Please sign in to comment.