feat: add typed batch matching entry points - #7
Merged
Conversation
Add testManySlice(), testManyString(), and testManyBuffer(), plus their asynchronous counterparts, to RE2 and RE2Set. Batch inputs now carry a native input kind so each entry point validates and reads one input shape: Buffers directly, slice objects as clamped byte ranges of a view, and strings as their UTF-8 bytes. Native batch reading is unified behind BatchInputs, which either borrows the supplied views or owns a copied batch. String bytes only exist in the engine's representation, so they are always copied and `unsafe` has no effect on the string variants. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The mutation fixture is the only test that distinguishes a real borrow from a silent copy, so extend it to the new entry points: unsafe slice and Buffer batches observe mutation, their safe counterparts do not, and unsafe string batches stay unaffected because their bytes are copied. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Adds typed, input-specific batch matching entry points to the Node.js RE2 wrapper and native addon, enabling separate batch APIs for strings, Buffers, and byte-range “slice” objects (sync + async) while keeping the existing binary-view batch behavior intact.
Changes:
- Added
testMany{String,Buffer,Slice}andtestMany{String,Buffer,Slice}Asyncon bothRE2andRE2Set, plus exportedSlicetype. - Updated native batch plumbing to accept an explicit “input kind” and to parse/validate each input shape natively (including slice clamping semantics and string UTF-8 encoding).
- Expanded JS/TS and native-boundary tests to cover the new entry points, kind validation, and async snapshot/unsafe semantics.
Reviewed changes
Copilot reviewed 17 out of 17 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| type-tests/index.ts | Adds TS type coverage for the six new methods on RE2/RE2Set and negative type cases. |
| test/native-boundary.test.js | Verifies native “kind” parameter validation and tarball manifest expectations. |
| test.js | Adds extensive runtime tests for string/Buffer/slice batch variants (sync + async) on both classes. |
| src/re2.ts | Exposes the new typed batch entry points on RE2 and passes explicit input kind to native calls. |
| src/re2-set.ts | Exposes the new typed batch entry points on RE2Set and passes explicit input kind to native calls. |
| src/index.ts | Exports the new Slice type from the public entry point. |
| src/binding.ts | Broadens binding typings to accept multiple batch input shapes and an optional kind. |
| src/binary.ts | Introduces Slice, input-kind constants, and new per-kind input normalizers. |
| README.md | Documents the new batch entry points and slice/string semantics. |
| native/regex-binding.cc | Extends regex batch bindings to accept/validate InputKind and unified batch input handling. |
| native/set-binding.cc | Extends set batch bindings similarly to regex batch bindings. |
| native/napi-utils.h | Replaces legacy text-batch getters with InputKind + GetBatchInputs declarations. |
| native/napi-utils.cc | Implements InputKind validation and unified batch input extraction for view/buffer/string/slice. |
| native/batch-inputs.h | Adds shared InputKind enum and BatchInputs container used across native batch paths. |
| native/async-batch.h | Refactors async batch representation to derive from BatchInputs and carry borrowed references. |
| native/async-batch.cc | Updates async batch preparation to use GetBatchInputs and kind-aware snapshot/borrow behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Name the methods the Buffer/TypedArray/DataView constraint applies to and point at the string and slice entry points, and state that the borrowing and snapshot rules extend to the slice and Buffer variants. Also spell out why the string byte guard rejects on equality. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds the requested overloads to both
RE2andRE2Set, sync and async:Sliceis a new exported type:{ buffer: BinaryView, byteOffset?: number, byteLength?: number }, with the byte-range clamping oftest()—byteOffsetdefaults to 0,byteLengthto the bytes remaining after the truncated offset.Native
Each batch call now passes an input kind, so one entry point reads one input shape:
testMany*) — Buffer, TypedArray, or DataView, unchanged.napi_is_bufferplusnapi_get_buffer_info, no view fallbacks.buffer,byteOffset, andbyteLengthare read once, natively, then clamped liketest(). Because the properties are validated natively, invalid slice contents reject the Promise of the async variants instead of throwing.Buffer.from()allocation. Unpaired surrogates encode as U+FFFD, matchingBuffer.from(input).GetTexts()andGetTextBatch()are replaced by oneGetBatchInputs()over a newBatchInputs(borrowed views or an owned copied batch), whichAsyncBatchnow derives from instead of duplicating the storage choice. String bytes cannot be borrowed, so they are always copied andunsafehas no effect on the string variants. Omitting the kind keeps the binary-view behaviour of earlier releases, so direct native callers are unaffected.Tests
test.js: string batches (multi-byte UTF-8, embedded NUL, unpaired surrogate, empty), Buffer batches (subarrays, empty,SharedArrayBuffer-backed, rejection of other views), slice batches (defaults, negative/fractional/NaN/Infinity/oversized ranges, agreement withtest()on integral ranges, single property read), safe-async snapshotting for all three kinds, plus batch-size and error paths. Mirrored forRE2Set.test/native-boundary.test.js: input-kind validation, the omitted-kind default, and the new header in the npm tarball manifest.type-tests/index.ts: typed usage of all six methods on both classes and@ts-expect-errorcases for mismatched inputs.npm testpasses (62 passing, 3 platform skips). Coverage oflib/re2.jsandlib/re2-set.jsis 100% line/branch/function;lib/binary.jsis 98.4%, with the only uncovered lines being the pre-existingToo many patternsguard.🤖 Generated with Claude Code