Skip to content

Commit

Permalink
Support ArrayBuffer stream chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Aug 9, 2023
1 parent cd51cf7 commit 5b17cf0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ const getChunkType = chunk => {

const prototypeName = objectToString.call(chunk);

if (prototypeName === '[object ArrayBuffer]') {
return 'arrayBuffer';
}

if (
prototypeName !== '[object DataView]'
&& Number.isInteger(chunk.byteLength)
Expand Down Expand Up @@ -120,6 +124,7 @@ const chunkTypes = {
convertChunk: {
string: useBufferFrom,
buffer: identity,
arrayBuffer: useBufferFrom,
typedArray: useBufferFromWithOffset,
others: throwObjectStream,
},
Expand All @@ -129,6 +134,7 @@ const chunkTypes = {
convertChunk: {
string: identity,
buffer: useTextDecoder,
arrayBuffer: useTextDecoder,
typedArray: useTextDecoder,
others: throwObjectStream,
},
Expand Down
3 changes: 3 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const getStreamToString = async (t, inputStream) => {

test('get stream from string to string', getStreamToString, fixtureString);
test('get stream from buffer to string', getStreamToString, fixtureBuffer);
test('get stream from arrayBuffer to string', getStreamToString, fixtureArrayBuffer);
test('get stream from typedArray to string', getStreamToString, fixtureTypedArray);
test('get stream from typedArray with offset to string', getStreamToString, fixtureTypedArrayWithOffset);
test('get stream from uint16Array to string', getStreamToString, fixtureUint16Array);
Expand All @@ -59,6 +60,7 @@ const getStreamToBuffer = async (t, inputStream) => {

test('get stream from string to buffer', getStreamToBuffer, fixtureString);
test('get stream from buffer to buffer', getStreamToBuffer, fixtureBuffer);
test('get stream from arrayBuffer to buffer', getStreamToBuffer, fixtureArrayBuffer);
test('get stream from typedArray to buffer', getStreamToBuffer, fixtureTypedArray);
test('get stream from typedArray with offset to buffer', getStreamToBuffer, fixtureTypedArrayWithOffset);
test('get stream from uint16Array to buffer', getStreamToBuffer, fixtureUint16Array);
Expand Down Expand Up @@ -117,6 +119,7 @@ const checkMaxBuffer = async (t, setupFunction, longValue, shortValue) => {

test('maxBuffer throws when size is exceeded with a string', checkMaxBuffer, setup, longString, fixtureString);
test('maxBuffer throws when size is exceeded with a buffer', checkMaxBuffer, setupBuffer, longBuffer, fixtureBuffer);
test('maxBuffer throws when size is exceeded with an arrayBuffer', checkMaxBuffer, setupBuffer, longArrayBuffer, fixtureArrayBuffer);
test('maxBuffer throws when size is exceeded with a typedArray', checkMaxBuffer, setupBuffer, longTypedArray, fixtureTypedArray);
test('maxBuffer throws when size is exceeded with an uint16Array', checkMaxBuffer, setupBuffer, longUint16Array, fixtureUint16Array);

Expand Down

0 comments on commit 5b17cf0

Please sign in to comment.