src: allow embedders to disable esm loader · nodejs/node@5e28660 · GitHub
Skip to content

Commit 5e28660

Browse files
codebytereaddaleax
authored andcommitted
src: allow embedders to disable esm loader
PR-URL: #34060 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: David Carlier <devnexen@gmail.com> Reviewed-By: Gus Caplan <me@gus.host>
1 parent 4cd7f5f commit 5e28660

7 files changed

Lines changed: 76 additions & 4 deletions

File tree

lib/internal/bootstrap/pre_execution.js

Lines changed: 8 additions & 2 deletions

lib/internal/options.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const { getOptions } = internalBinding('options');
3+
const { getOptions, shouldNotRegisterESMLoader } = internalBinding('options');
44
const { options, aliases } = getOptions();
55

66
let warnOnAllowUnauthorized = true;
@@ -32,4 +32,5 @@ module.exports = {
3232
aliases,
3333
getOptionValue,
3434
getAllowUnauthorized,
35+
shouldNotRegisterESMLoader
3536
};

src/env-inl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,10 @@ inline bool Environment::is_main_thread() const {
859859
return worker_context() == nullptr;
860860
}
861861

862+
inline bool Environment::should_not_register_esm_loader() const {
863+
return flags_ & EnvironmentFlags::kNoRegisterESMLoader;
864+
}
865+
862866
inline bool Environment::owns_process_state() const {
863867
return flags_ & EnvironmentFlags::kOwnsProcessState;
864868
}

src/env.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,7 @@ class Environment : public MemoryRetainer {
10631063
inline void set_has_serialized_options(bool has_serialized_options);
10641064

10651065
inline bool is_main_thread() const;
1066+
inline bool should_not_register_esm_loader() const;
10661067
inline bool owns_process_state() const;
10671068
inline bool owns_inspector() const;
10681069
inline uint64_t thread_id() const;

src/node.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,11 @@ enum Flags : uint64_t {
396396
// Set if this Environment instance is associated with the global inspector
397397
// handling code (i.e. listening on SIGUSR1).
398398
// This is set when using kDefaultFlags.
399-
kOwnsInspector = 1 << 2
399+
kOwnsInspector = 1 << 2,
400+
// Set if Node.js should not run its own esm loader. This is needed by some
401+
// embedders, because it's possible for the Node.js esm loader to conflict
402+
// with another one in an embedder environment, e.g. Blink's in Chromium.
403+
kNoRegisterESMLoader = 1 << 3
400404
};
401405
} // namespace EnvironmentFlags
402406

src/node_options.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,12 @@ void Initialize(Local<Object> target,
994994
context, FIXED_ONE_BYTE_STRING(isolate, "envSettings"), env_settings)
995995
.Check();
996996

997+
target
998+
->Set(context,
999+
FIXED_ONE_BYTE_STRING(env->isolate(), "shouldNotRegisterESMLoader"),
1000+
Boolean::New(isolate, env->should_not_register_esm_loader()))
1001+
.Check();
1002+
9971003
Local<Object> types = Object::New(isolate);
9981004
NODE_DEFINE_CONSTANT(types, kNoOp);
9991005
NODE_DEFINE_CONSTANT(types, kV8Option);

test/cctest/test_environment.cc

Lines changed: 50 additions & 0 deletions

0 commit comments

Comments
 (0)