Skip to content

buffer: add DataView support to isUtf8, isAscii, and stringLength - #67179

Open
ShakilUrRehman21 wants to merge 4 commits into
nodejs:mainfrom
ShakilUrRehman21:buffer-support-dataview
Open

ShakilUrRehman21 wants to merge 4 commits into
nodejs:mainfrom
ShakilUrRehman21:buffer-support-dataview

Conversation

@ShakilUrRehman21

Copy link
Copy Markdown

Description

ECMAScript defines both TypedArray and DataView as subclasses of ArrayBufferView (ArrayBuffer.isView(x) returns true for both).

While other buffer methods like Buffer.byteLength() accept any ArrayBufferView (including DataView), buffer.isUtf8(), buffer.isAscii(), and Buffer.stringLength() restricted their argument validation to isTypedArray() and IsTypedArray(). Passing a DataView threw TypeError [ERR_INVALID_ARG_TYPE].

The underlying C++ helper ArrayBufferViewContents<T> in src/util-inl.h already natively supports v8::ArrayBufferView (including DataView).

This PR:

  1. Updates lib/buffer.js to validate with isArrayBufferView() instead of isTypedArray(), and includes 'DataView' in the ERR_INVALID_ARG_TYPE expected types.
  2. Updates src/node_buffer.cc to check IsArrayBufferView() instead of IsTypedArray().
  3. Updates doc/api/buffer.md to document DataView support.
  4. Adds test cases covering DataView in test-buffer-isutf8.js, test-buffer-isascii.js, and test-buffer-stringlength.js.

Refs: #64503
Refs: #64504
Refs: #66064

Checklist

  • test passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

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>
@nodejs-github-bot nodejs-github-bot added buffer Issues and PRs related to the buffer subsystem. c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Nov 1, 2026
@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

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.

@MikeMcC399

This comment was marked as outdated.

@ShakilUrRehman21
ShakilUrRehman21 marked this pull request as draft November 1, 2026 11:39
@ShakilUrRehman21

This comment was marked as outdated.

Comment thread src/node_buffer.cc
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() ||

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these changes require updates to typings folder as well...

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anonrig I've moved my other PR (#67178) into draft to prioritize this one instead. Typings are updated, all 29 CI checks are passing, and this is ready for review whenever you have time!

Refs: nodejs#67179
Signed-off-by: shakilurrehman21 <rehmanshakil21@gmail.com>
@ShakilUrRehman21
ShakilUrRehman21 marked this pull request as ready for review November 17, 2026 09:52
@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.33333% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 90.67%. Comparing base (e97c75e) to head (4fec62e).
⚠️ Report is 317 commits behind head on main.

Files with missing lines Patch % Lines
src/node_buffer.cc 66.66% 1 Missing ⚠️
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     
Files with missing lines Coverage Δ
lib/buffer.js 99.74% <100.00%> (+<0.01%) ⬆️
src/node_buffer.cc 74.86% <66.66%> (+3.19%) ⬆️

... and 200 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jasnell jasnell added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 18, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 18, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@MikeMcC399 MikeMcC399 added the resume-ci Add this label to resume the latest eligible Jenkins CI run on a PR with an approving review. label Nov 20, 2026
@github-actions github-actions Bot removed the resume-ci Add this label to resume the latest eligible Jenkins CI run on a PR with an approving review. label Nov 20, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

buffer Issues and PRs related to the buffer subsystem. c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants