Skip to content

Commit b0f7c4c

Browse files
tniessentargos
authored andcommittedApr 28, 2022
lib,src: implement WebAssembly Web API
Refs: #41749 Fixes: #21130 PR-URL: #42701 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent c6c1dc5 commit b0f7c4c

File tree

161 files changed

+8056
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

161 files changed

+8056
-2
lines changed
 

‎doc/api/errors.md

+11
Original file line numberDiff line numberDiff line change
@@ -2890,6 +2890,17 @@ The WASI instance has already started.
28902890

28912891
The WASI instance has not been started.
28922892

2893+
<a id="ERR_WEBASSEMBLY_RESPONSE"></a>
2894+
2895+
### `ERR_WEBASSEMBLY_RESPONSE`
2896+
2897+
<!-- YAML
2898+
added: REPLACEME
2899+
-->
2900+
2901+
The `Response` that has been passed to `WebAssembly.compileStreaming` or to
2902+
`WebAssembly.instantiateStreaming` is not a valid WebAssembly response.
2903+
28932904
<a id="ERR_WORKER_INIT_FAILED"></a>
28942905

28952906
### `ERR_WORKER_INIT_FAILED`

‎lib/internal/bootstrap/pre_execution.js

+44-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const {
55
ObjectDefineProperties,
66
ObjectDefineProperty,
77
ObjectGetOwnPropertyDescriptor,
8+
PromiseResolve,
89
SafeMap,
910
SafeWeakMap,
1011
StringPrototypeStartsWith,
@@ -24,7 +25,11 @@ const {
2425
} = require('internal/util');
2526

2627
const { Buffer } = require('buffer');
27-
const { ERR_MANIFEST_ASSERT_INTEGRITY } = require('internal/errors').codes;
28+
const {
29+
ERR_INVALID_ARG_TYPE,
30+
ERR_MANIFEST_ASSERT_INTEGRITY,
31+
ERR_WEBASSEMBLY_RESPONSE,
32+
} = require('internal/errors').codes;
2833
const assert = require('internal/assert');
2934

3035
function prepareMainThreadExecution(expandArgv1 = false,
@@ -215,6 +220,44 @@ function setupFetch() {
215220
Request: lazyInterface('Request'),
216221
Response: lazyInterface('Response'),
217222
});
223+
224+
// The WebAssembly Web API: https://webassembly.github.io/spec/web-api
225+
internalBinding('wasm_web_api').setImplementation((streamState, source) => {
226+
(async () => {
227+
const response = await PromiseResolve(source);
228+
if (!(response instanceof lazyUndici().Response)) {
229+
throw new ERR_INVALID_ARG_TYPE(
230+
'source', ['Response', 'Promise resolving to Response'], response);
231+
}
232+
233+
const contentType = response.headers.get('Content-Type');
234+
if (contentType !== 'application/wasm') {
235+
throw new ERR_WEBASSEMBLY_RESPONSE(
236+
`has unsupported MIME type '${contentType}'`);
237+
}
238+
239+
if (!response.ok) {
240+
throw new ERR_WEBASSEMBLY_RESPONSE(
241+
`has status code ${response.status}`);
242+
}
243+
244+
if (response.bodyUsed !== false) {
245+
throw new ERR_WEBASSEMBLY_RESPONSE('body has already been used');
246+
}
247+
248+
// Pass all data from the response body to the WebAssembly compiler.
249+
for await (const chunk of response.body) {
250+
streamState.push(chunk);
251+
}
252+
})().then(() => {
253+
// No error occurred. Tell the implementation that the stream has ended.
254+
streamState.finish();
255+
}, (err) => {
256+
// An error occurred, either because the given object was not a valid
257+
// and usable Response or because a network error occurred.
258+
streamState.abort(err);
259+
});
260+
});
218261
}
219262

220263
// TODO(aduh95): move this to internal/bootstrap/browser when the CLI flag is

0 commit comments

Comments
 (0)