Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: add fetch to bootstrap/browser.js #41958

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/internal/bootstrap/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ const {

const {
defineOperation,
emitExperimentalWarning,
exposeInterface,
lazyDOMExceptionClass,
} = require('internal/util');

const { getOptionValue } = require('internal/options');

const config = internalBinding('config');

// Override global console from the one provided by the VM
Expand Down Expand Up @@ -71,6 +75,17 @@ defineOperation(globalThis, 'setTimeout', timers.setTimeout);
defineReplacableAttribute(globalThis, 'performance',
require('perf_hooks').performance);

// https://fetch.spec.whatwg.org/
if (getOptionValue('--experimental-fetch')) {
emitExperimentalWarning('Fetch');

const undici = require('internal/deps/undici/undici');
defineOperation(globalThis, 'fetch', undici.fetch);
exposeInterface(globalThis, 'Headers', undici.Headers);
exposeInterface(globalThis, 'Request', undici.Request);
exposeInterface(globalThis, 'Response', undici.Response);
}

function createGlobalConsole(consoleFromVM) {
const consoleFromNode =
require('internal/console/global');
Expand Down
22 changes: 0 additions & 22 deletions lib/internal/bootstrap/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ const {
getEmbedderOptions,
} = require('internal/options');
const { reconnectZeroFillToggle } = require('internal/buffer');
const {
defineOperation,
emitExperimentalWarning,
exposeInterface,
} = require('internal/util');

const { Buffer } = require('buffer');
const { ERR_MANIFEST_ASSERT_INTEGRITY } = require('internal/errors').codes;
Expand All @@ -35,7 +30,6 @@ function prepareMainThreadExecution(expandArgv1 = false) {
setupPerfHooks();
setupInspectorHooks();
setupWarningHandler();
setupFetch();

// Resolve the coverage directory to an absolute path, and
// overwrite process.env so that the original path gets passed
Expand Down Expand Up @@ -147,21 +141,6 @@ function setupWarningHandler() {
}
}

// https://fetch.spec.whatwg.org/
function setupFetch() {
if (!getOptionValue('--experimental-fetch')) {
return;
}

emitExperimentalWarning('Fetch');

const undici = require('internal/deps/undici/undici');
defineOperation(globalThis, 'fetch', undici.fetch);
exposeInterface(globalThis, 'Headers', undici.Headers);
exposeInterface(globalThis, 'Request', undici.Request);
exposeInterface(globalThis, 'Response', undici.Response);
}

// Setup User-facing NODE_V8_COVERAGE environment variable that writes
// ScriptCoverage to a specified file.
function setupCoverageHooks(dir) {
Expand Down Expand Up @@ -502,7 +481,6 @@ module.exports = {
patchProcessObject,
setupCoverageHooks,
setupWarningHandler,
setupFetch,
setupDebugEnv,
setupPerfHooks,
prepareMainThreadExecution,
Expand Down
2 changes: 0 additions & 2 deletions lib/internal/main/worker_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const {
setupCoverageHooks,
setupInspectorHooks,
setupWarningHandler,
setupFetch,
setupDebugEnv,
setupPerfHooks,
initializeDeprecations,
Expand Down Expand Up @@ -68,7 +67,6 @@ setupInspectorHooks();
setupDebugEnv();

setupWarningHandler();
setupFetch();
initializeSourceMapsHandlers();

// Since worker threads cannot switch cwd, we do not need to
Expand Down
1 change: 1 addition & 0 deletions src/node_external_reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class ExternalReferenceRegistry {
V(messaging) \
V(native_module) \
V(os) \
V(options) \
V(performance) \
V(process_methods) \
V(process_object) \
Expand Down
7 changes: 7 additions & 0 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,11 @@ void Initialize(Local<Object> target,
.Check();
}

void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(GetCLIOptions);
registry->Register(GetEmbedderOptions);
}

} // namespace options_parser

void HandleEnvOptions(std::shared_ptr<EnvironmentOptions> env_options) {
Expand Down Expand Up @@ -1194,3 +1199,5 @@ std::vector<std::string> ParseNodeOptionsEnvVar(
} // namespace node

NODE_MODULE_CONTEXT_AWARE_INTERNAL(options, node::options_parser::Initialize)
NODE_MODULE_EXTERNAL_REFERENCE(options,
node::options_parser::RegisterExternalReferences)