Skip to content

Commit

Permalink
bootstrap: hide experimental web globals with flag kNoBrowserGlobals
Browse files Browse the repository at this point in the history
Do not install experimental web globals when the environment is
initialized with embedder flag
`node::EnvironmentFlags::kNoBrowserGlobals`.

PR-URL: nodejs#48545
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
legendecas authored and Ceres6 committed Aug 14, 2023
1 parent bf3688e commit ad8bae0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/internal/process/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const {
const {
getOptionValue,
refreshOptions,
getEmbedderOptions,
} = require('internal/options');
const { reconnectZeroFillToggle } = require('internal/buffer');
const {
Expand Down Expand Up @@ -241,7 +242,7 @@ function setupWarningHandler() {

// https://fetch.spec.whatwg.org/
function setupFetch() {
if (process.config.variables.node_no_browser_globals ||
if (getEmbedderOptions().noBrowserGlobals ||
getOptionValue('--no-experimental-fetch')) {
return;
}
Expand Down Expand Up @@ -291,7 +292,7 @@ function setupFetch() {
// TODO(aduh95): move this to internal/bootstrap/web/* when the CLI flag is
// removed.
function setupWebCrypto() {
if (process.config.variables.node_no_browser_globals ||
if (getEmbedderOptions().noBrowserGlobals ||
getOptionValue('--no-experimental-global-webcrypto')) {
return;
}
Expand Down Expand Up @@ -339,7 +340,7 @@ function setupCodeCoverage() {
// TODO(daeyeon): move this to internal/bootstrap/web/* when the CLI flag is
// removed.
function setupCustomEvent() {
if (process.config.variables.node_no_browser_globals ||
if (getEmbedderOptions().noBrowserGlobals ||
getOptionValue('--no-experimental-global-customevent')) {
return;
}
Expand Down
6 changes: 6 additions & 0 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,12 @@ void GetEmbedderOptions(const FunctionCallbackInfo<Value>& args) {
Boolean::New(isolate, env->no_global_search_paths()))
.IsNothing()) return;

if (ret->Set(context,
FIXED_ONE_BYTE_STRING(env->isolate(), "noBrowserGlobals"),
Boolean::New(isolate, env->no_browser_globals()))
.IsNothing())
return;

args.GetReturnValue().Set(ret);
}

Expand Down
33 changes: 33 additions & 0 deletions test/cctest/test_environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,39 @@ class EnvironmentTest : public EnvironmentTestFixture {
}
};

TEST_F(EnvironmentTest, EnvironmentWithoutBrowserGlobals) {
const v8::HandleScope handle_scope(isolate_);
Argv argv;
Env env{handle_scope, argv, node::EnvironmentFlags::kNoBrowserGlobals};

SetProcessExitHandler(*env, [&](node::Environment* env_, int exit_code) {
EXPECT_EQ(*env, env_);
EXPECT_EQ(exit_code, 0);
node::Stop(*env);
});

node::LoadEnvironment(
*env,
"const assert = require('assert');"
"const path = require('path');"
"const relativeRequire = "
" require('module').createRequire(path.join(process.cwd(), 'stub.js'));"
"const { intrinsics, nodeGlobals } = "
" relativeRequire('./test/common/globals');"
"const items = Object.getOwnPropertyNames(globalThis);"
"const leaks = [];"
"for (const item of items) {"
" if (intrinsics.has(item)) {"
" continue;"
" }"
" if (nodeGlobals.has(item)) {"
" continue;"
" }"
" leaks.push(item);"
"}"
"assert.deepStrictEqual(leaks, []);");
}

TEST_F(EnvironmentTest, EnvironmentWithESMLoader) {
const v8::HandleScope handle_scope(isolate_);
Argv argv;
Expand Down

0 comments on commit ad8bae0

Please sign in to comment.