string_decoder: support typed array or data view
Refs: https://github.com/nodejs/node/issues/1826 PR-URL: https://github.com/nodejs/node/pull/22562 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
parent
1b92214d09
commit
e2325bcc04
@ -59,7 +59,8 @@ Creates a new `StringDecoder` instance.
|
|||||||
added: v0.9.3
|
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: {string}
|
||||||
|
|
||||||
Returns any remaining input stored in the internal buffer as a string. Bytes
|
Returns any remaining input stored in the internal buffer as a string. Bytes
|
||||||
@ -79,10 +80,11 @@ changes:
|
|||||||
character instead of one for each individual byte.
|
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: {string}
|
||||||
|
|
||||||
Returns a decoded string, ensuring that any incomplete multibyte characters at
|
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
|
the end of the `Buffer`, or `TypedArray`, or `DataView` are omitted from the
|
||||||
internal buffer for the next call to `stringDecoder.write()` or
|
returned string and stored in an internal buffer for the next call to
|
||||||
`stringDecoder.end()`.
|
`stringDecoder.write()` or `stringDecoder.end()`.
|
||||||
|
@ -73,7 +73,7 @@ StringDecoder.prototype.write = function write(buf) {
|
|||||||
return buf;
|
return buf;
|
||||||
if (!ArrayBuffer.isView(buf))
|
if (!ArrayBuffer.isView(buf))
|
||||||
throw new ERR_INVALID_ARG_TYPE('buf',
|
throw new ERR_INVALID_ARG_TYPE('buf',
|
||||||
['Buffer', 'Uint8Array', 'ArrayBufferView'],
|
['Buffer', 'TypedArray', 'DataView'],
|
||||||
buf);
|
buf);
|
||||||
return decode(this[kNativeDecoder], buf);
|
return decode(this[kNativeDecoder], buf);
|
||||||
};
|
};
|
||||||
|
@ -97,6 +97,17 @@ assert.strictEqual(decoder.lastTotal, 3);
|
|||||||
|
|
||||||
assert.strictEqual(decoder.end(), '\ufffd');
|
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');
|
decoder = new StringDecoder('utf8');
|
||||||
assert.strictEqual(decoder.write(Buffer.from('E18B', 'hex')), '');
|
assert.strictEqual(decoder.write(Buffer.from('E18B', 'hex')), '');
|
||||||
assert.strictEqual(decoder.end(), '\ufffd');
|
assert.strictEqual(decoder.end(), '\ufffd');
|
||||||
@ -174,8 +185,8 @@ common.expectsError(
|
|||||||
{
|
{
|
||||||
code: 'ERR_INVALID_ARG_TYPE',
|
code: 'ERR_INVALID_ARG_TYPE',
|
||||||
type: TypeError,
|
type: TypeError,
|
||||||
message: 'The "buf" argument must be one of type Buffer, Uint8Array, or' +
|
message: 'The "buf" argument must be one of type Buffer, TypedArray,' +
|
||||||
' ArrayBufferView. Received type object'
|
' or DataView. Received type object'
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user