Files
electron/patches/node/fix_expose_readfilesync_override_for_modules.patch
electron-roller[bot] 49437d48a2 chore: bump node to v24.13.1 (41-x-y) (#49743)
* chore: bump node in DEPS to v24.13.1

* chore: fixup patches

refs:
* https://github.com/nodejs/node/pull/60425
* https://github.com/nodejs/node/pull/61270
* https://github.com/nodejs/node/pull/61044

* fix: generate_config_gypi needs to generate valid JSON

https://github.com/nodejs/node/pull/60794

* doc: align Buffer.concat documentation with behavior

https://github.com/nodejs/node/pull/60405

* src: fix off-thread cert loading in bundled cert mode

https://github.com/nodejs/node/pull/60764

* chore: fixup patch indices

* build: fix extraneous includes

---------

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
2026-02-13 13:37:27 +01:00

143 lines
5.7 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Fedor Indutny <indutny@signal.org>
Date: Mon, 31 Mar 2025 11:21:29 -0700
Subject: fix: expose ReadFileSync override for modules
To avoid copying out `package.json` files out of the ASAR file we need
an API override to replace the native `ReadFileSync` in the `modules`
binding.
diff --git a/src/env_properties.h b/src/env_properties.h
index bb874fec74c73b5de0ef6d1e2a872ebceefb11ce..6fafe2a8029c535fa98276e2d73f04ee603a7805 100644
--- a/src/env_properties.h
+++ b/src/env_properties.h
@@ -491,6 +491,7 @@
V(maybe_cache_generated_source_map, v8::Function) \
V(messaging_deserialize_create_object, v8::Function) \
V(message_port, v8::Object) \
+ V(modules_read_file_sync, v8::Function) \
V(builtin_module_require, v8::Function) \
V(performance_entry_callback, v8::Function) \
V(prepare_stack_trace_callback, v8::Function) \
diff --git a/src/node_modules.cc b/src/node_modules.cc
index 95a7b8c32693c32ca9f57e01aff9ef06b84f78b2..cecdda74847801fd5821bc0afdf0dfc9f131c44a 100644
--- a/src/node_modules.cc
+++ b/src/node_modules.cc
@@ -23,12 +23,14 @@ namespace modules {
using v8::Array;
using v8::Context;
using v8::External;
+using v8::Function;
using v8::FunctionCallbackInfo;
using v8::HandleScope;
using v8::Integer;
using v8::Isolate;
using v8::Local;
using v8::LocalVector;
+using v8::MaybeLocal;
using v8::Name;
using v8::NewStringType;
using v8::Null;
@@ -94,6 +96,7 @@ Local<Array> BindingData::PackageConfig::Serialize(Realm* realm) const {
const BindingData::PackageConfig* BindingData::GetPackageJSON(
Realm* realm, std::string_view path, ErrorContext* error_context) {
+ auto isolate = realm->isolate();
auto binding_data = realm->GetBindingData<BindingData>();
auto cache_entry = binding_data->package_configs_.find(path.data());
@@ -111,8 +114,48 @@ const BindingData::PackageConfig* BindingData::GetPackageJSON(
PackageConfig package_config{};
package_config.file_path = path;
+
+ Local<Function> modules_read_file_sync = realm->modules_read_file_sync();
+
+ int read_err = -1;
// No need to exclude BOM since simdjson will skip it.
- if (ReadFileSync(&package_config.raw_json, path.data()) < 0) {
+ if (modules_read_file_sync.IsEmpty()) {
+ read_err = ReadFileSync(&package_config.raw_json, path.data());
+ } else if (realm->env()->can_call_into_js()) {
+ v8::TryCatch try_catch(isolate);
+ Local<Value> args[] = {
+ v8::String::NewFromUtf8(isolate, path.data()).ToLocalChecked(),
+ };
+ MaybeLocal<Value> maybe_result = modules_read_file_sync->Call(
+ realm->context(),
+ Undefined(isolate),
+ arraysize(args),
+ args);
+
+ if (maybe_result.IsEmpty()) {
+ CHECK(try_catch.HasCaught());
+ }
+
+ if (try_catch.HasCaught()) {
+ if (!try_catch.HasTerminated())
+ try_catch.ReThrow();
+ return nullptr;
+ }
+
+ Local<Value> result = maybe_result.ToLocalChecked();
+ if (result->IsUndefined()) {
+ // Fallback
+ read_err = ReadFileSync(&package_config.raw_json, path.data());
+ } else if (result->IsFalse()) {
+ // Not found
+ read_err = -1;
+ } else {
+ BufferValue data(isolate, result);
+ package_config.raw_json = data.ToString();
+ read_err = 0;
+ }
+ }
+ if (read_err < 0) {
// Add `nullopt` to the package config cache so that we don't
// need to open and attempt to read this path again
binding_data->package_configs_.insert({std::string(path), std::nullopt});
@@ -254,6 +297,12 @@ const BindingData::PackageConfig* BindingData::GetPackageJSON(
return &*cached.first->second;
}
+void BindingData::OverrideReadFileSync(const FunctionCallbackInfo<Value>& args) {
+ Realm* realm = Realm::GetCurrent(args);
+ CHECK(args[0]->IsFunction());
+ realm->set_modules_read_file_sync(args[0].As<Function>());
+}
+
void BindingData::ReadPackageJSON(const FunctionCallbackInfo<Value>& args) {
CHECK_GE(args.Length(), 1); // path, [is_esm, base, specifier]
CHECK(args[0]->IsString()); // path
@@ -672,6 +721,8 @@ void SaveCompileCacheEntry(const FunctionCallbackInfo<Value>& args) {
void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
Local<ObjectTemplate> target) {
Isolate* isolate = isolate_data->isolate();
+ SetMethod(isolate, target, "overrideReadFileSync", OverrideReadFileSync);
+
SetMethod(isolate, target, "readPackageJSON", ReadPackageJSON);
SetMethod(isolate,
target,
@@ -735,6 +786,8 @@ void BindingData::CreatePerContextProperties(Local<Object> target,
void BindingData::RegisterExternalReferences(
ExternalReferenceRegistry* registry) {
+ registry->Register(OverrideReadFileSync);
+
registry->Register(ReadPackageJSON);
registry->Register(GetNearestParentPackageJSONType);
registry->Register(GetNearestParentPackageJSON);
diff --git a/src/node_modules.h b/src/node_modules.h
index d610306a3a3111327f111def823b07c3da04192f..5bb7245816822282977e62ce3b0e6c0b9c6ac32b 100644
--- a/src/node_modules.h
+++ b/src/node_modules.h
@@ -54,6 +54,8 @@ class BindingData : public SnapshotableObject {
SET_SELF_SIZE(BindingData)
SET_MEMORY_INFO_NAME(BindingData)
+ static void OverrideReadFileSync(
+ const v8::FunctionCallbackInfo<v8::Value>& args);
static void ReadPackageJSON(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetNearestParentPackageJSON(
const v8::FunctionCallbackInfo<v8::Value>& args);