Repository navigation
feat(ext/node): add a byteLength/length parameter to Buffer.indexOf/lastIndexOf/includes - #35872
Conversation
af1c6d2 to
e99bf1f
Compare
|
Hi! When you have some time, could you please review this PR? Notes:
// buffer.ts - line 50
for (let i = start; i < lastStart; i += step) {to : // buffer.ts - line 50
for (let i = start; i <= lastStart; i += step) {After those changes, the checks passed. |
|
Nice work, this tracks Node's Two small divergences from Node's C++ ( end = end < targetBufferLength ? end : targetBufferLength;
Both are cheap to close if we want byte-exact parity: end = end < targetBufferLength ? end : targetBufferLength;
if (end < 0) end = 0;
if (isUcs2) end &= ~1;Neither blocks the PR since the test passes as written; up to you whether to fold them in now or leave a follow-up. |
e99bf1f to
ae43740
Compare
|
Thanks! I'll add unit tests to cover these additional cases that were missing from the upstream
NotesI'll also update the TypeScript declarations for While investigating the type errors, I also found that One question: for the new overloads, should the signature be byteOffset: number | undefined,
end: number,or should both parameters remain optional? byteOffset?: number,
end?: number,I believe the first option is more appropriate because |
d5b58e3 to
6d17e27
Compare
…/lastIndexOf/includes
6d17e27 to
2ae9bdf
Compare
Reimplement indexOfBuffer/indexOfNumber to mirror node/src/node_buffer.cc (IndexOfOffset + search_end clamp + guard sequence + SearchString) instead of the ad-hoc end clamping. Fixes three divergences from Node: - indexOf(val, 0, NaN) searched the whole buffer instead of returning -1 (Node reads end as an int64, so NaN floors to 0 via max(end, 0)). - Empty needle with a negative byteOffset returned the raw negative offset instead of the normalized position. - UCS2 empty needle returned -1 instead of the clamped offset. Verified against all 718 assertions of Node's test/parallel/test-buffer-indexof.js.
The two runtime/ sites gated malloc_trim on target_os = linux only, but it is a glibc extension absent on musl. Match the existing gnu-gated pattern in cli/lib/worker.rs.
…ffset in Buffer search Mirror three details of Node's node_buffer.cc that the byte-space port missed: - UCS2 forward search now aligns an odd byteOffset down to the code-unit grid (Node's offset / 2); previously it scanned odd byte positions. - A UCS2 needle with a trailing odd byte is truncated to whole code units (Node's needle_length / 2). - A fractional byteOffset is truncated toward zero, matching Node reading it as an int64 at the binding boundary. Add unit_node tests for each case (none are covered by the re-enabled upstream test-buffer-indexof.js, whose ucs2 cases use even offsets).
This reverts commit 1066ca0.
Move the end/encoding argument normalization into bidirectionalIndexOf instead of repeating it in indexOf/lastIndexOf/includes, and factor the byteOffset/end int64 coercion into a toInt64 helper.
Fixes part of #35293 (
Buffer#indexOf/lastIndexOf/includes).This PR updates the Node
Buffersearch implementation to support the newendparameter introduced in newer Node.js versions.The changes:
endargument through the internalBuffersearch APIs.indexOfBuffer,indexOfNumber,indexOfNeedle, andfindLastIndexto accept and forward the new parameter.Testing
./x test-compat parallel/test-buffer-indexof.jsAI Disclosure
I used Cloude to help understand the Node.js implementation, discuss the design, and review the proposed changes + Write this description for me. The implementation, testing, and final code changes were completed and verified by me.