Skip to content

Commit

Permalink
string_decoder: support typed array or data view
Browse files Browse the repository at this point in the history
Refs: #1826
PR-URL: #22562
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
  • Loading branch information
BeniCheni authored and targos committed Sep 20, 2018
1 parent 84d498c commit a10d03d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
12 changes: 7 additions & 5 deletions doc/api/string_decoder.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ Creates a new `StringDecoder` instance.
added: v0.9.3
-->

* `buffer` {Buffer} A `Buffer` containing the bytes to decode.
* `buffer` {Buffer|TypedArray|DataView} A `Buffer`, or `TypedArray`, or
`DataView` containing the bytes to decode.
* Returns: {string}

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

* `buffer` {Buffer} A `Buffer` containing the bytes to decode.
* `buffer` {Buffer|TypedArray|DataView} A `Buffer`, or `TypedArray`, or
`DataView` containing the bytes to decode.
* Returns: {string}

Returns a decoded string, ensuring that any incomplete multibyte characters at
the end of the `Buffer` are omitted from the returned string and stored in an
internal buffer for the next call to `stringDecoder.write()` or
`stringDecoder.end()`.
the end of the `Buffer`, or `TypedArray`, or `DataView` are omitted from the
returned string and stored in an internal buffer for the next call to
`stringDecoder.write()` or `stringDecoder.end()`.
2 changes: 1 addition & 1 deletion lib/string_decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ StringDecoder.prototype.write = function write(buf) {
return buf;
if (!ArrayBuffer.isView(buf))
throw new ERR_INVALID_ARG_TYPE('buf',
['Buffer', 'Uint8Array', 'ArrayBufferView'],
['Buffer', 'TypedArray', 'DataView'],
buf);
return decode(this[kNativeDecoder], buf);
};
Expand Down
15 changes: 13 additions & 2 deletions test/parallel/test-string-decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ assert.strictEqual(decoder.lastTotal, 3);

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

// ArrayBufferView tests
const arrayBufferViewStr = 'String for ArrayBufferView tests\n';
const inputBuffer = Buffer.from(arrayBufferViewStr.repeat(8), 'utf8');
for (const expectView of common.getArrayBufferViews(inputBuffer)) {
assert.strictEqual(
decoder.write(expectView),
inputBuffer.toString('utf8')
);
assert.strictEqual(decoder.end(), '');
}

decoder = new StringDecoder('utf8');
assert.strictEqual(decoder.write(Buffer.from('E18B', 'hex')), '');
assert.strictEqual(decoder.end(), '\ufffd');
Expand Down Expand Up @@ -174,8 +185,8 @@ common.expectsError(
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "buf" argument must be one of type Buffer, Uint8Array, or' +
' ArrayBufferView. Received type object'
message: 'The "buf" argument must be one of type Buffer, TypedArray,' +
' or DataView. Received type object'
}
);

Expand Down

0 comments on commit a10d03d

Please sign in to comment.