node.exe imports crypt32, dbghelp, iphlpapi, ole32, shell32, userenv
and winmm, so every process loads them, and the DLLs they depend on,
at start-up. They are only needed by less common code paths such as
the system certificate store, network interface queries and
diagnostic reports.
Delay-load them so they are loaded on first use. On Windows 11 this
takes `node -e 0` from 35 to 26 loaded images and saves about 2.5 ms
per process.
Signed-off-by: Alexandre Zollinger Chohfi <alzollin@microsoft.com>
PR-URL: https://github.com/nodejs/node/pull/67098
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Stefan Stojanovic <stefan.stojanovic@janeasystems.com>
StreamBase::WriteBuffer() and WriteString() duplicated the code that
creates a request object when needed and attaches the handle being sent
over an IPC pipe. Move it into a shared PrepareSendHandle() helper.
Assisted-by: Claude
Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67496
Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
All three callers of report::WriteNodeReport() computed the
excludeNetwork and excludeEnv options the same way, falling back to the
process-wide defaults when there is no Environment. Do that once inside
WriteNodeReport() instead.
Assisted-by: Claude
Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67496
Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
ImportMetaResolveLazyGetter() and PathHelpersLazyGetter() duplicated
the code that retrieves the creation context from the holder object.
Move it into a shared GetHolderCreationContext() helper.
Assisted-by: Claude
Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67496
Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
When the creation context of the import.meta object has no associated
Realm, the getter threw ERR_INVALID_INVOCATION but then fell through
and dereferenced the null Realm. Return after throwing, as the other
error paths in the getter already do.
Assisted-by: Claude
Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67496
Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
The SetMethod(), SetFastMethod(), SetMethodNoSideEffect(),
SetProtoMethod(), SetInstanceMethod(), and SetProto*Dispose() variants
each repeated the same function template construction, internalized
name string creation, and installation code, differing only in side
effect type, receiver signature, and target. Factor that into a small
set of internal helpers.
Assisted-by: Claude
Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67496
Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
The network and DOM storage agents repeated the same pattern dozens of
times: read a property with an ObjectGet*() helper and throw a
"Missing <name> in event" TypeError if it was absent. Add
ObjectGetRequired() overloads that combine the two, move the duplicated
ThrowEventError() into inspector_object_utils, and use them throughout
both agents. Error messages are unchanged.
Assisted-by: Claude
Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67496
Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
DynamicLibrary::UnregisterCallback(), RefCallback(), and UnrefCallback()
each duplicated the closed-library check, pointer argument validation,
and callback map lookup. Move them into a shared FindCallback() helper.
Assisted-by: Claude
Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67496
Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
napi_create_error(), napi_create_type_error(), napi_create_range_error()
and node_api_create_syntax_error() differed only in the v8::Exception
factory used, as did the corresponding napi_throw_*() functions. Move
the bodies into internal CreateError() and ThrowError() helpers that
take the factory. The exported functions and their signatures are
unchanged, so there is no ABI impact.
Assisted-by: Claude
Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67496
Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
The System V and Win64 halves of the x64 fast trampoline emitter each
defined identical copies of the register constants, the byte/word
emitters, the REX/ModRM, mov, call, jmp, stack adjustment, and ret
encoders, and the narrow return normalization helpers. Define them
once ahead of the ABI-specific code.
Assisted-by: Claude
Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67496
Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
The loong64, ppc64, riscv64, and s390x fast trampoline emitters each
duplicated the signature validation, executable memory allocation,
cache flush and mprotect sequence, trampoline release, and (for the
fixed-width 32-bit ISAs) the instruction word emitters. Move them into
a shared scalar_trampoline.h header so each platform file only
contains its architecture-specific instruction encoding.
Assisted-by: Claude
Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67496
Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
When a server responds before it has read the whole request body, the
client stops forwarding the socket's 'drain' to the request as soon as
the response is complete. A writer that honors backpressure, such as a
stream piped into the request, then never receives 'drain', never calls
end(), and the socket keeps the process alive.
Keep the 'drain' listener until the request has ended and remove it in
responseKeepAlive() when the socket is released back to the pool.
Fixes: https://github.com/nodejs/node/issues/67763
Signed-off-by: Matteo Collina <matteo.collina@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67780
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
CallIntoModule() reset last_exception only after handle_exception()
returned. When handle_exception() reports the exception through
'uncaughtException', the listener runs while the exception is still
pending, so calling back into the module from it saw the same
exception again: an addon function failed with napi_pending_exception
and rethrew it, and a finalizer run by gc() in an experimental module
reported it a second time, which aborted the process with
"Re-entrant uncaught exception detected".
Take the exception out of last_exception before handling it.
Fixes: https://github.com/nodejs/node/issues/67421
Assisted-by: a coding agent
Signed-off-by: Manjeet Singh <024manjeetsingh@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67448
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
node::MakeCallback() looks up the Environment from the creation context
of the callback on every call. An addon that calls into the same
Environment many times, a server calling one handler per request for
example, can now take it once with GetCurrentEnvironment() and pass it.
The callback must belong to that Environment.
Add a benchmark, napi/make_callback_env, comparing the two overloads.
Refs: https://github.com/nodejs/performance/issues/24
Signed-off-by: Nigro Simone <nigro.simone@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/66510
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
processChunkSync() and processSyncWithDefaults() collected output
chunks with buffers.push(), which looks up Array.prototype.push at call
time. Replacing that method from user code changed how synchronous zlib
calls such as gunzipSync() collected their output. Use indexed
assignments instead.
Signed-off-by: Roberto Bianchi <roberto.bianchi@spendesk.com>
PR-URL: https://github.com/nodejs/node/pull/67748
Reviewed-By: René <contact.9a5d6388@renegade334.me.uk>
Reviewed-By: Jordan Harband <ljharb@gmail.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: James M Snell <jasnell@gmail.com>
parseFileMode() accepted any 32-bit unsigned integer, but the native
bindings read the mode as a signed 32-bit integer and CHECK that it is
one. A mode between 2**31 and 2**32 - 1 therefore passed validation and
then aborted the process from fs.open(), fs.chmod(), fs.mkdir() and
their sync and promise variants. Validate the mode as a non-negative
Int32 instead so that the caller gets a RangeError.
ReadStream and WriteStream now validate options.mode in the constructor
too, so an invalid mode throws where the stream is created instead of
failing inside the open callback.
Fixes: https://github.com/nodejs/node/issues/62516
Refs: https://github.com/nodejs/node/pull/62538
Co-authored-by: wei_ds <wei_ds@126.com>
Signed-off-by: Yohannes Desta <yohannesdestagebru10@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67498
Reviewed-By: James M Snell <jasnell@gmail.com>
This fixes the fix from the PR #67446.
The previous PR did not allow already submitted
data, such as a Webtransport close capsule to be
transmitted to ngtcp2.
This PR instead uses the nullptr reader,
to detect a closed stream and signals eof.
This is of course only an intermediate solution,
instead quic RESET_AT semantics are a far better
solution.
Signed-off-by: Marten Richter <marten.richter@freenet.de>
PR-URL: https://github.com/nodejs/node/pull/67706
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>