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>
Add tools/sbom/generate_sbom.py, a stdlib-only Python 3 script that
writes a CycloneDX 1.7 SBOM describing the bundled dependencies of the
Node.js binary.
The script reads config.gypi to determine which dependencies are
active in the build, then extracts each one's version from whichever
file records it: C headers, CMakeLists.txt, package.json, Cargo.toml
and others. A version that cannot be read is reported as "unknown"
with a warning rather than failing the run. Dependencies satisfied by
a system library or disabled via a configure flag are omitted, so the
document lists only what ships in the build.
The root component records a pkg:generic/nodejs/node purl, the git
commit SHA as a hash on a VCS external reference, V8's embedder string
and the build machine's hostname. Its version, and the file name,
match process.version, including -pre, -alpha.* and `--tag=` suffixes.
A formulation section records the build environment: the C/C++
compiler, rustc, cargo and nasm versions that ./configure recorded in
config.gypi, their distro package versions on Linux, the host's glibc
version, and the ./configure command from config.status. Each probe is
independent, and anything undetermined is left out rather than failing
generation. The C and C++ compilers are named by product - gcc, clang
or apple-clang - since the version alone is ambiguous: Apple clang
17.0.0 is not upstream clang 17. configure.py now records those
products and versions, plus rustc and cargo, in config.gypi.
Output defaults to out/Release/node-v{version}-sbom.json, overridable
with SBOM_OUTPUT.
New Makefile targets:
make sbom - generate the SBOM (requires ./configure first)
make test-sbom - validate the generated SBOM
doc/contributing/maintaining/maintaining-dependencies.md now asks for a
DEP_CATALOG entry when adding a dependency, so new deps do not silently
go missing from the SBOM. tools/sbom/README.md covers the output and
how to generate it.
Unit tests in test/tools/test_generate_sbom.py cover catalog structural
correctness, the version extractors, scope resolution and deps/
coverage. They run under the existing `make tooltest` target.
Refs: https://github.com/nodejs/security-wg/issues/1115
Signed-off-by: Beth Griggs <bethanyngriggs@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/66761
Reviewed-By: Richard Lau <richard.lau@ibm.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Stewart X Addison <sxa@redhat.com>
Without an async context, napi_make_callback went through the public
node::MakeCallback(), which looks up the Environment from the creation
context of the callback on every call. The napi_env already knows it,
so call into it directly, as the path with an async context does.
node_napi_env__::node_env() also looked it up from the context on every
call: find it once, a napi_env lives as long as its Environment.
Add a benchmark, napi/make_callback_napi.
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/66509
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
stringPercentDecode() UTF-8 encodes its input into a fresh Uint8Array
and then percent-decodes it into a second array of the same size. After
URL parsing, a data: URL body contains '%' only when it has non-ASCII or
control characters or explicit escapes, so plain-text and base64 bodies
were copied byte-for-byte for nothing.
Return the encoded bytes directly when the input contains no '%'. UTF-8
never emits 0x25 inside a multi-byte sequence, so the result is
identical, and each call still returns its own buffer.
When there is a '%', pass its index to percentDecode(), which copies a
long unescaped prefix in bulk and decodes the rest through subarray
views, so the loop still starts at 0 and keeps its bounds checks
eliminated. The string index is used only when every code point encoded
to a single byte, so it is also the byte index.
Signed-off-by: Yaroslav Chuhai <yaroslavchuhai@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67684
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
This is not impactful in any way - it's a fixed leak of the openssl
record compression descriptors, about 24 bytes leaked total, once
per process. Not meaningful.
It is annoying if you want to run with ASAN & leak detection though,
since it's an unambiguous leak we trigger every time crypto is used.
Signed-off-by: Tim Perry <pimterry@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/66600
Reviewed-By: Filip Skokan <panva.ip@gmail.com>