Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTobias Nießen <tniessen@tnie.de>2022-04-23 06:09:15 +0300
committerGitHub <noreply@github.com>2022-04-23 06:09:15 +0300
commitc4781ea69ce164b1163a10f50d818be3d0a6bca4 (patch)
tree69743d339d2473f2978d75d76716cbbd06b8e409 /lib
parenteeb27c2e0aecbf418635a8c4b7c4529385798c63 (diff)
lib,src: implement WebAssembly Web API
Refs: https://github.com/nodejs/node/pull/41749 Fixes: https://github.com/nodejs/node/issues/21130 PR-URL: https://github.com/nodejs/node/pull/42701 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/internal/bootstrap/pre_execution.js45
-rw-r--r--lib/internal/errors.js1
2 files changed, 45 insertions, 1 deletions
diff --git a/lib/internal/bootstrap/pre_execution.js b/lib/internal/bootstrap/pre_execution.js
index b64dfaf980f..e1b882b6dbe 100644
--- a/lib/internal/bootstrap/pre_execution.js
+++ b/lib/internal/bootstrap/pre_execution.js
@@ -5,6 +5,7 @@ const {
ObjectDefineProperties,
ObjectDefineProperty,
ObjectGetOwnPropertyDescriptor,
+ PromiseResolve,
SafeMap,
SafeWeakMap,
StringPrototypeStartsWith,
@@ -24,7 +25,11 @@ const {
} = require('internal/util');
const { Buffer } = require('buffer');
-const { ERR_MANIFEST_ASSERT_INTEGRITY } = require('internal/errors').codes;
+const {
+ ERR_INVALID_ARG_TYPE,
+ ERR_MANIFEST_ASSERT_INTEGRITY,
+ ERR_WEBASSEMBLY_RESPONSE,
+} = require('internal/errors').codes;
const assert = require('internal/assert');
function prepareMainThreadExecution(expandArgv1 = false,
@@ -215,6 +220,44 @@ function setupFetch() {
Request: lazyInterface('Request'),
Response: lazyInterface('Response'),
});
+
+ // The WebAssembly Web API: https://webassembly.github.io/spec/web-api
+ internalBinding('wasm_web_api').setImplementation((streamState, source) => {
+ (async () => {
+ const response = await PromiseResolve(source);
+ if (!(response instanceof lazyUndici().Response)) {
+ throw new ERR_INVALID_ARG_TYPE(
+ 'source', ['Response', 'Promise resolving to Response'], response);
+ }
+
+ const contentType = response.headers.get('Content-Type');
+ if (contentType !== 'application/wasm') {
+ throw new ERR_WEBASSEMBLY_RESPONSE(
+ `has unsupported MIME type '${contentType}'`);
+ }
+
+ if (!response.ok) {
+ throw new ERR_WEBASSEMBLY_RESPONSE(
+ `has status code ${response.status}`);
+ }
+
+ if (response.bodyUsed !== false) {
+ throw new ERR_WEBASSEMBLY_RESPONSE('body has already been used');
+ }
+
+ // Pass all data from the response body to the WebAssembly compiler.
+ for await (const chunk of response.body) {
+ streamState.push(chunk);
+ }
+ })().then(() => {
+ // No error occurred. Tell the implementation that the stream has ended.
+ streamState.finish();
+ }, (err) => {
+ // An error occurred, either because the given object was not a valid
+ // and usable Response or because a network error occurred.
+ streamState.abort(err);
+ });
+ });
}
// TODO(aduh95): move this to internal/bootstrap/browser when the CLI flag is
diff --git a/lib/internal/errors.js b/lib/internal/errors.js
index 1dfa36e5f36..d5bb5023a79 100644
--- a/lib/internal/errors.js
+++ b/lib/internal/errors.js
@@ -1659,6 +1659,7 @@ E('ERR_VM_MODULE_NOT_MODULE',
'Provided module is not an instance of Module', Error);
E('ERR_VM_MODULE_STATUS', 'Module status %s', Error);
E('ERR_WASI_ALREADY_STARTED', 'WASI instance has already started', Error);
+E('ERR_WEBASSEMBLY_RESPONSE', 'WebAssembly response %s', TypeError);
E('ERR_WORKER_INIT_FAILED', 'Worker initialization failure: %s', Error);
E('ERR_WORKER_INVALID_EXEC_ARGV', (errors, msg = 'invalid execArgv flags') =>
`Initiated Worker with ${msg}: ${ArrayPrototypeJoin(errors, ', ')}`,