node/lib/internal/wasm_web_api.js
Joyee Cheung 06f5d45647
bootstrap: support more builtins in the embedded code cache
This patch:

- Make NativeModuleLoader::LookupAndCompile() detect parameters based
  on module IDs. This allows us to compile more builtins when
  generating the embedded bootstrap, including
  - internal/per_context/*
  - internal/bootstrap/*
  - internal/main/*
- Move pre_execution.js to lib/internal/process as it needs to be
  compiled as a regular built-in module, unlike other scripts
  in lib/internal/bootstrap
- Move markBootstrapComplete() to the performance binding instead of
  making it a function-wrapper-based global to reduce number of
  special cases.

PR-URL: https://github.com/nodejs/node/pull/44018
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2022-08-05 01:11:20 +08:00

67 lines
1.9 KiB
JavaScript

'use strict';
const {
PromiseResolve,
} = primordials;
const {
ERR_INVALID_ARG_TYPE,
ERR_WEBASSEMBLY_RESPONSE,
} = require('internal/errors').codes;
let undici;
function lazyUndici() {
return undici ??= require('internal/deps/undici/undici');
}
// This is essentially an implementation of a v8::WasmStreamingCallback, except
// that it is implemented in JavaScript because the fetch() implementation is
// difficult to use from C++. See lib/internal/process/pre_execution.js and
// src/node_wasm_web_api.cc that interact with this function.
function wasmStreamingCallback(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');
}
if (response.url) {
streamState.setURL(response.url);
}
// Pass all data from the response body to the WebAssembly compiler.
const { body } = response;
if (body != null) {
for await (const chunk of 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);
});
}
module.exports = {
wasmStreamingCallback
};