Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions doc/api/buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -1019,9 +1019,13 @@ the byte length as reported by `.byteLength` is returned.

<!-- YAML
added: v26.11.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/REPLACEME
description: DataView is now supported.
-->

* `input` {Buffer | ArrayBuffer | TypedArray} The bytes that would be decoded.
* `input` {Buffer | ArrayBuffer | TypedArray | DataView} The bytes that would be decoded.
* `encoding` {string} The character encoding `input` would be decoded with.
**Default:** `'utf8'`.
* Returns: {integer}
Expand Down Expand Up @@ -5398,6 +5402,9 @@ added:
- v19.6.0
- v18.15.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/REPLACEME
description: DataView is now supported.
- version:
- v26.8.0
- v24.21.0
Expand All @@ -5406,7 +5413,7 @@ changes:
as empty.
-->

* `input` {Buffer | ArrayBuffer | TypedArray} The input to validate.
* `input` {Buffer | ArrayBuffer | TypedArray | DataView} The input to validate.
* Returns: {boolean}

This function returns `true` if `input` contains only valid ASCII-encoded data,
Expand Down Expand Up @@ -5468,6 +5475,9 @@ added:
- v19.4.0
- v18.14.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/REPLACEME
description: DataView is now supported.
- version:
- v26.8.0
- v24.21.0
Expand All @@ -5476,7 +5486,7 @@ changes:
as empty.
-->

* `input` {Buffer | ArrayBuffer | TypedArray} The input to validate.
* `input` {Buffer | ArrayBuffer | TypedArray | DataView} The input to validate.
* Returns: {boolean}

This function returns `true` if `input` contains only valid UTF-8-encoded data,
Expand Down
12 changes: 6 additions & 6 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1484,19 +1484,19 @@ function atob(input) {
}

function isUtf8(input) {
if (isTypedArray(input) || isAnyArrayBuffer(input)) {
if (isArrayBufferView(input) || isAnyArrayBuffer(input)) {
return bindingIsUtf8(input);
}

throw new ERR_INVALID_ARG_TYPE('input', ['ArrayBuffer', 'Buffer', 'TypedArray'], input);
throw new ERR_INVALID_ARG_TYPE('input', ['ArrayBuffer', 'Buffer', 'TypedArray', 'DataView'], input);
}

function isAscii(input) {
if (isTypedArray(input) || isAnyArrayBuffer(input)) {
if (isArrayBufferView(input) || isAnyArrayBuffer(input)) {
return bindingIsAscii(input);
}

throw new ERR_INVALID_ARG_TYPE('input', ['ArrayBuffer', 'Buffer', 'TypedArray'], input);
throw new ERR_INVALID_ARG_TYPE('input', ['ArrayBuffer', 'Buffer', 'TypedArray', 'DataView'], input);
}

/**
Expand All @@ -1511,8 +1511,8 @@ function isLatin1(input) {
}

function stringLength(input, encoding = 'utf8') {
if (!isTypedArray(input) && !isAnyArrayBuffer(input)) {
throw new ERR_INVALID_ARG_TYPE('input', ['ArrayBuffer', 'Buffer', 'TypedArray'], input);
if (!isArrayBufferView(input) && !isAnyArrayBuffer(input)) {
throw new ERR_INVALID_ARG_TYPE('input', ['ArrayBuffer', 'Buffer', 'TypedArray', 'DataView'], input);
}
validateString(encoding, 'encoding');
const ops = getEncodingOps(encoding);
Expand Down
6 changes: 3 additions & 3 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1372,7 +1372,7 @@ static bool ValidateUtf8(Local<Value> value) {

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.

args[0]->IsSharedArrayBuffer());

args.GetReturnValue().Set(ValidateUtf8(args[0]));
Expand All @@ -1397,7 +1397,7 @@ static bool ValidateAscii(Local<Value> value) {

static void IsAscii(const FunctionCallbackInfo<Value>& args) {
CHECK_EQ(args.Length(), 1);
CHECK(args[0]->IsTypedArray() || args[0]->IsArrayBuffer() ||
CHECK(args[0]->IsArrayBufferView() || args[0]->IsArrayBuffer() ||
args[0]->IsSharedArrayBuffer());

args.GetReturnValue().Set(ValidateAscii(args[0]));
Expand Down Expand Up @@ -1627,7 +1627,7 @@ static double StringLengthUtf8Impl(Local<Value> value) {

static void StringLengthUtf8(const FunctionCallbackInfo<Value>& args) {
CHECK_EQ(args.Length(), 1);
CHECK(args[0]->IsTypedArray() || args[0]->IsArrayBuffer() ||
CHECK(args[0]->IsArrayBufferView() || args[0]->IsArrayBuffer() ||
args[0]->IsSharedArrayBuffer());

args.GetReturnValue().Set(StringLengthUtf8Impl(args[0]));
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-buffer-isascii.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const encoder = new TextEncoder();
assert.strictEqual(isAscii(encoder.encode('hello')), true);
assert.strictEqual(isAscii(encoder.encode('ğ')), false);
assert.strictEqual(isAscii(Buffer.from([])), true);
assert.strictEqual(isAscii(new DataView(new ArrayBuffer(0))), true);
assert.strictEqual(isAscii(new DataView(encoder.encode('hello').buffer)), true);
assert.strictEqual(isAscii(new DataView(encoder.encode('ğ').buffer)), false);

[
undefined,
Expand All @@ -34,10 +37,12 @@ assert.strictEqual(isAscii(Buffer.from([])), true);
const arrayBuffer = new ArrayBuffer(1);
const typedArray = new Uint8Array(arrayBuffer);
typedArray[0] = 0xff;
const dataView = new DataView(arrayBuffer);
const inputs = [
arrayBuffer,
typedArray,
Buffer.from(arrayBuffer),
dataView,
];
for (const input of inputs) {
assert.strictEqual(isAscii(input), false);
Expand All @@ -47,3 +52,9 @@ assert.strictEqual(isAscii(Buffer.from([])), true);
assert.strictEqual(isAscii(input), true);
}
}

{
const buffer = Buffer.from('hello world');
const view = new DataView(buffer.buffer, buffer.byteOffset, 5);
assert.strictEqual(isAscii(view), true);
}
10 changes: 10 additions & 0 deletions test/parallel/test-buffer-isutf8.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const encoder = new TextEncoder();
assert.strictEqual(isUtf8(encoder.encode('hello')), true);
assert.strictEqual(isUtf8(encoder.encode('ğ')), true);
assert.strictEqual(isUtf8(Buffer.from([])), true);
assert.strictEqual(isUtf8(new DataView(new ArrayBuffer(0))), true);
assert.strictEqual(isUtf8(new DataView(encoder.encode('hello').buffer)), true);

// Taken from test/fixtures/wpt/encoding/textdecoder-fatal.any.js
[
Expand Down Expand Up @@ -78,10 +80,12 @@ assert.strictEqual(isUtf8(Buffer.from([])), true);
const arrayBuffer = new ArrayBuffer(1);
const typedArray = new Uint8Array(arrayBuffer);
typedArray[0] = 0xff;
const dataView = new DataView(arrayBuffer);
const inputs = [
arrayBuffer,
typedArray,
Buffer.from(arrayBuffer),
dataView,
];
for (const input of inputs) {
assert.strictEqual(isUtf8(input), false);
Expand All @@ -91,3 +95,9 @@ assert.strictEqual(isUtf8(Buffer.from([])), true);
assert.strictEqual(isUtf8(input), true);
}
}

{
const buffer = Buffer.from('hello world');
const view = new DataView(buffer.buffer, buffer.byteOffset, 5);
assert.strictEqual(isUtf8(view), true);
}
6 changes: 4 additions & 2 deletions test/parallel/test-buffer-stringlength.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ assert.strictEqual(Buffer.stringLength(Buffer.alloc(0)), 0);
assert.strictEqual(Buffer.stringLength(ab), expected);
assert.strictEqual(Buffer.stringLength(sab), expected);
assert.strictEqual(Buffer.stringLength(new Uint8Array(ab)), expected);
assert.strictEqual(Buffer.stringLength(new DataView(ab)), expected);
assert.strictEqual(Buffer.stringLength(new Uint16Array(ab.slice(0, 16))),
Buffer.from(ab.slice(0, 16)).toString().length);
}
Expand All @@ -43,9 +44,11 @@ assert.strictEqual(Buffer.stringLength(Buffer.alloc(0)), 0);
{
const ab = new ArrayBuffer(8);
const view = new Uint8Array(ab);
const dataView = new DataView(ab);
structuredClone(ab, { transfer: [ab] });
assert.strictEqual(Buffer.stringLength(ab), 0);
assert.strictEqual(Buffer.stringLength(view), 0);
assert.strictEqual(Buffer.stringLength(dataView), 0);
}

// Invalid UTF-8 is counted the way toString() decodes it (one U+FFFD per
Expand Down Expand Up @@ -119,8 +122,7 @@ assert.strictEqual(Buffer.stringLength(Buffer.alloc(0)), 0);
}

// Argument validation.
for (const input of [undefined, null, 'abc', 42, {}, [], new Blob([]),
new DataView(new ArrayBuffer(4))]) {
for (const input of [undefined, null, 'abc', 42, {}, [], new Blob([])]) {
assert.throws(() => Buffer.stringLength(input),
{ code: 'ERR_INVALID_ARG_TYPE' });
}
Expand Down
1 change: 1 addition & 0 deletions typings/internalBinding/buffer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface BufferBinding {
isUtf8(input: ArrayBufferView | ArrayBuffer | SharedArrayBuffer): boolean;
isAscii(input: ArrayBufferView | ArrayBuffer | SharedArrayBuffer): boolean;
isLatin1(input: string): boolean;
stringLengthUtf8(input: ArrayBufferView | ArrayBuffer | SharedArrayBuffer): number;

mask(source: ArrayBufferView, destination: ArrayBufferView, offset: number, length: number, mask: number): boolean;

Expand Down