1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-12 00:54:02 -05:00

feat(etc/fetch): Support WebAssembly.instantiateStreaming for file fetches (#12901)

Fetching of local files, added in #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.
This commit is contained in:
Andreu Botella 2021-11-26 09:52:41 +01:00 committed by GitHub
parent 6a780543a4
commit d763633781
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 6 deletions

View file

@ -76,6 +76,20 @@ Deno.test(async function wasmInstantiateStreaming() {
assertEquals(add(1, 3), 4); 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( Deno.test(
{ permissions: { net: true } }, { permissions: { net: true } },
async function wasmStreamingNonTrivial() { async function wasmStreamingNonTrivial() {

View file

@ -37,6 +37,7 @@
PromisePrototypeThen, PromisePrototypeThen,
PromisePrototypeCatch, PromisePrototypeCatch,
String, String,
StringPrototypeStartsWith,
StringPrototypeToLowerCase, StringPrototypeToLowerCase,
TypedArrayPrototypeSubarray, TypedArrayPrototypeSubarray,
TypeError, TypeError,
@ -498,6 +499,9 @@
// The spec is ambiguous here, see // The spec is ambiguous here, see
// https://github.com/WebAssembly/spec/issues/1138. The WPT tests // https://github.com/WebAssembly/spec/issues/1138. The WPT tests
// expect the raw value of the Content-Type attribute lowercased. // expect the raw value of the Content-Type attribute lowercased.
// 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"); const contentType = res.headers.get("Content-Type");
if ( if (
typeof contentType !== "string" || typeof contentType !== "string" ||
@ -505,6 +509,7 @@
) { ) {
throw new TypeError("Invalid WebAssembly content type."); throw new TypeError("Invalid WebAssembly content type.");
} }
}
// 2.5. // 2.5.
if (!res.ok) { if (!res.ok) {