Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a10d03d

Browse filesBrowse files
BeniChenitargos
authored andcommitted
string_decoder: support typed array or data view
Refs: #1826 PR-URL: #22562 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 84d498c commit a10d03d
Copy full SHA for a10d03d

File tree

Expand file treeCollapse file tree

3 files changed

+21
-8
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+21
-8
lines changed
Open diff view settings
Collapse file

‎doc/api/string_decoder.md‎

Copy file name to clipboardExpand all lines: doc/api/string_decoder.md
+7-5Lines changed: 7 additions & 5 deletions
  • Display the source diff
  • Display the rich diff
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()`.
Collapse file

‎lib/string_decoder.js‎

Copy file name to clipboardExpand all lines: lib/string_decoder.js
+1-1Lines changed: 1 addition & 1 deletion
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
};
Collapse file

‎test/parallel/test-string-decoder.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-string-decoder.js
+13-2Lines changed: 13 additions & 2 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.