Repository navigation
buffer: add DataView support to isUtf8, isAscii, and stringLength - #67179
ShakilUrRehman21 wants to merge 4 commits into
Conversation
ECMAScript defines both TypedArray and DataView as subclasses of ArrayBufferView. While methods like `Buffer.byteLength()` accept any ArrayBufferView (including DataView), `buffer.isUtf8()`, `buffer.isAscii()`, and `Buffer.stringLength()` restricted their argument checks to `isTypedArray()` and `IsTypedArray()`, rejecting DataView with an ERR_INVALID_ARG_TYPE TypeError. The underlying C++ helper `ArrayBufferViewContents<T>` already natively supports any `v8::ArrayBufferView`. This commit updates: - `lib/buffer.js` to validate with `isArrayBufferView()` instead of `isTypedArray()`, and include 'DataView' in the ERR_INVALID_ARG_TYPE expected type list. - `src/node_buffer.cc` to check `IsArrayBufferView()` instead of `IsTypedArray()`. - `doc/api/buffer.md` to document DataView support. - Tests in `test-buffer-isutf8.js`, `test-buffer-isascii.js`, and `test-buffer-stringlength.js` to cover DataView instances. Signed-off-by: shakilurrehman21 <rehmanshakil21@gmail.com>
|
Welcome to Node.js, and thank you for your first contribution! Before review, please take a moment to read:
Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal. |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
| static void IsUtf8(const FunctionCallbackInfo<Value>& args) { | ||
| CHECK_EQ(args.Length(), 1); | ||
| CHECK(args[0]->IsTypedArray() || args[0]->IsArrayBuffer() || | ||
| CHECK(args[0]->IsArrayBufferView() || args[0]->IsArrayBuffer() || |
There was a problem hiding this comment.
these changes require updates to typings folder as well...
There was a problem hiding this comment.
Updated typings/internalBinding/buffer.d.ts to include stringLengthUtf8. Thanks for pointing that out!
Keeping this PR in draft for now per first-time contributor guidelines until my first PR (#67178) lands.
Refs: nodejs#67179 Signed-off-by: shakilurrehman21 <rehmanshakil21@gmail.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #67179 +/- ##
==========================================
- Coverage 90.68% 90.67% -0.02%
==========================================
Files 797 800 +3
Lines 278790 280894 +2104
Branches 53617 54280 +663
==========================================
+ Hits 252834 254689 +1855
- Misses 16303 16306 +3
- Partials 9653 9899 +246
🚀 New features to boost your workflow:
|
Description
ECMAScript defines both
TypedArrayandDataViewas subclasses ofArrayBufferView(ArrayBuffer.isView(x)returnstruefor both).While other buffer methods like
Buffer.byteLength()accept anyArrayBufferView(includingDataView),buffer.isUtf8(),buffer.isAscii(), andBuffer.stringLength()restricted their argument validation toisTypedArray()andIsTypedArray(). Passing aDataViewthrewTypeError [ERR_INVALID_ARG_TYPE].The underlying C++ helper
ArrayBufferViewContents<T>insrc/util-inl.halready natively supportsv8::ArrayBufferView(includingDataView).This PR:
lib/buffer.jsto validate withisArrayBufferView()instead ofisTypedArray(), and includes'DataView'in theERR_INVALID_ARG_TYPEexpected types.src/node_buffer.ccto checkIsArrayBufferView()instead ofIsTypedArray().doc/api/buffer.mdto documentDataViewsupport.DataViewintest-buffer-isutf8.js,test-buffer-isascii.js, andtest-buffer-stringlength.js.Refs: #64503
Refs: #64504
Refs: #66064
Checklist
testpasses