Skip to content

Commit a10d03d

Browse files
BeniChenitargos
authored andcommitted
string_decoder: support typed array or data view
Refs: #1826 PR-URL: #22562 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent 84d498c commit a10d03d

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

doc/api/string_decoder.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ Creates a new `StringDecoder` instance.
5959
added: v0.9.3
6060
-->
6161

62-
* `buffer` {Buffer} A `Buffer` containing the bytes to decode.
62+
* `buffer` {Buffer|TypedArray|DataView} A `Buffer`, or `TypedArray`, or
63+
`DataView` containing the bytes to decode.
6364
* Returns: {string}
6465

6566
Returns any remaining input stored in the internal buffer as a string. Bytes
@@ -79,10 +80,11 @@ changes:
7980
character instead of one for each individual byte.
8081
-->
8182

82-
* `buffer` {Buffer} A `Buffer` containing the bytes to decode.
83+
* `buffer` {Buffer|TypedArray|DataView} A `Buffer`, or `TypedArray`, or
84+
`DataView` containing the bytes to decode.
8385
* Returns: {string}
8486

8587
Returns a decoded string, ensuring that any incomplete multibyte characters at
86-
the end of the `Buffer` are omitted from the returned string and stored in an
87-
internal buffer for the next call to `stringDecoder.write()` or
88-
`stringDecoder.end()`.
88+
the end of the `Buffer`, or `TypedArray`, or `DataView` are omitted from the
89+
returned string and stored in an internal buffer for the next call to
90+
`stringDecoder.write()` or `stringDecoder.end()`.

lib/string_decoder.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ StringDecoder.prototype.write = function write(buf) {
7373
return buf;
7474
if (!ArrayBuffer.isView(buf))
7575
throw new ERR_INVALID_ARG_TYPE('buf',
76-
['Buffer', 'Uint8Array', 'ArrayBufferView'],
76+
['Buffer', 'TypedArray', 'DataView'],
7777
buf);
7878
return decode(this[kNativeDecoder], buf);
7979
};

test/parallel/test-string-decoder.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ assert.strictEqual(decoder.lastTotal, 3);
9797

9898
assert.strictEqual(decoder.end(), '\ufffd');
9999

100+
// ArrayBufferView tests
101+
const arrayBufferViewStr = 'String for ArrayBufferView tests\n';
102+
const inputBuffer = Buffer.from(arrayBufferViewStr.repeat(8), 'utf8');
103+
for (const expectView of common.getArrayBufferViews(inputBuffer)) {
104+
assert.strictEqual(
105+
decoder.write(expectView),
106+
inputBuffer.toString('utf8')
107+
);
108+
assert.strictEqual(decoder.end(), '');
109+
}
110+
100111
decoder = new StringDecoder('utf8');
101112
assert.strictEqual(decoder.write(Buffer.from('E18B', 'hex')), '');
102113
assert.strictEqual(decoder.end(), '\ufffd');
@@ -174,8 +185,8 @@ common.expectsError(
174185
{
175186
code: 'ERR_INVALID_ARG_TYPE',
176187
type: TypeError,
177-
message: 'The "buf" argument must be one of type Buffer, Uint8Array, or' +
178-
' ArrayBufferView. Received type object'
188+
message: 'The "buf" argument must be one of type Buffer, TypedArray,' +
189+
' or DataView. Received type object'
179190
}
180191
);
181192

0 commit comments

Comments
 (0)