Skip to content

Commit

Permalink
feat(wasm): Support WebAssembly.instantiateStreaming for file fetches
Browse files Browse the repository at this point in the history
Fetching of local files, added in denoland#12545, returns a response with no
headers, including the `Content-Type` header. This currently makes it
not work with the WebAssembly streaming APIs, which require the response
to have a content type of `application/wasm`.

Since the only way to obtain a `Response` object with a non-empty `url`
field is via `fetch()`, this change changes the content type requirement
to only apply to responses whose url has the `file:` scheme.
  • Loading branch information
Andreu Botella committed Nov 26, 2021
1 parent 6a78054 commit 83f6d15
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
14 changes: 14 additions & 0 deletions cli/tests/unit/wasm_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ Deno.test(async function wasmInstantiateStreaming() {
assertEquals(add(1, 3), 4);
});

Deno.test(
{ permissions: { read: true } },
async function wasmFileStreaming() {
const url = new URL("../testdata/unreachable.wasm", import.meta.url);
assert(url.href.startsWith("file://"));

const { module } = await WebAssembly.instantiateStreaming(fetch(url));
assertEquals(WebAssembly.Module.exports(module), [{
name: "unreachable",
kind: "function",
}]);
},
);

Deno.test(
{ permissions: { net: true } },
async function wasmStreamingNonTrivial() {
Expand Down
17 changes: 11 additions & 6 deletions ext/fetch/26_fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
PromisePrototypeThen,
PromisePrototypeCatch,
String,
StringPrototypeStartsWith,
StringPrototypeToLowerCase,
TypedArrayPrototypeSubarray,
TypeError,
Expand Down Expand Up @@ -498,12 +499,16 @@
// The spec is ambiguous here, see
// https://github.com/WebAssembly/spec/issues/1138. The WPT tests
// expect the raw value of the Content-Type attribute lowercased.
const contentType = res.headers.get("Content-Type");
if (
typeof contentType !== "string" ||
StringPrototypeToLowerCase(contentType) !== "application/wasm"
) {
throw new TypeError("Invalid WebAssembly content type.");
// We ignore this for file:// because file fetches don't have a
// Content-Type.
if (!StringPrototypeStartsWith(res.url, "file://")) {
const contentType = res.headers.get("Content-Type");
if (
typeof contentType !== "string" ||
StringPrototypeToLowerCase(contentType) !== "application/wasm"
) {
throw new TypeError("Invalid WebAssembly content type.");
}
}

// 2.5.
Expand Down

0 comments on commit 83f6d15

Please sign in to comment.