get-stream
Advanced tools
Comparing version 8.0.0 to 8.0.1
{ | ||
"name": "get-stream", | ||
"version": "8.0.0", | ||
"version": "8.0.1", | ||
"description": "Get a stream as a string, Buffer, ArrayBuffer or array", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
import {getStreamContents} from './contents.js'; | ||
import {throwObjectStream, getLengthProp} from './utils.js'; | ||
import {noop, throwObjectStream, getLengthProp} from './utils.js'; | ||
@@ -8,3 +8,3 @@ export async function getStreamAsArrayBuffer(stream, options) { | ||
const initArrayBuffer = () => new Uint8Array(0); | ||
const initArrayBuffer = () => ({contents: new ArrayBuffer(0)}); | ||
@@ -18,6 +18,8 @@ const useTextEncoder = chunk => textEncoder.encode(chunk); | ||
const truncateArrayBufferChunk = (convertedChunk, chunkSize) => convertedChunk.slice(0, chunkSize); | ||
// `contents` is an increasingly growing `Uint8Array`. | ||
const addArrayBufferChunk = (convertedChunk, contents, length, previousLength) => { | ||
const addArrayBufferChunk = (convertedChunk, {contents, length: previousLength}, length) => { | ||
const newContents = hasArrayBufferResize() ? resizeArrayBuffer(contents, length) : resizeArrayBufferSlow(contents, length); | ||
newContents.set(convertedChunk, previousLength); | ||
new Uint8Array(newContents).set(convertedChunk, previousLength); | ||
return newContents; | ||
@@ -30,9 +32,9 @@ }; | ||
const resizeArrayBufferSlow = (contents, length) => { | ||
if (length <= contents.length) { | ||
if (length <= contents.byteLength) { | ||
return contents; | ||
} | ||
const newContents = new Uint8Array(getNewContentsLength(length)); | ||
newContents.set(contents, 0); | ||
return newContents; | ||
const arrayBuffer = new ArrayBuffer(getNewContentsLength(length)); | ||
new Uint8Array(arrayBuffer).set(new Uint8Array(contents), 0); | ||
return arrayBuffer; | ||
}; | ||
@@ -45,11 +47,10 @@ | ||
const resizeArrayBuffer = (contents, length) => { | ||
if (length <= contents.buffer.maxByteLength) { | ||
contents.buffer.resize(length); | ||
return new Uint8Array(contents.buffer, 0, length); | ||
if (length <= contents.maxByteLength) { | ||
contents.resize(length); | ||
return contents; | ||
} | ||
const arrayBuffer = new ArrayBuffer(length, {maxByteLength: getNewContentsLength(length)}); | ||
const newContents = new Uint8Array(arrayBuffer); | ||
newContents.set(contents, 0); | ||
return newContents; | ||
new Uint8Array(arrayBuffer).set(new Uint8Array(contents), 0); | ||
return arrayBuffer; | ||
}; | ||
@@ -62,3 +63,3 @@ | ||
const finalizeArrayBuffer = ({buffer}, length) => hasArrayBufferResize() ? buffer : buffer.slice(0, length); | ||
const finalizeArrayBuffer = ({contents, length}) => hasArrayBufferResize() ? contents : contents.slice(0, length); | ||
@@ -84,4 +85,6 @@ // `ArrayBuffer.slice()` is slow. When `ArrayBuffer.resize()` is available | ||
getSize: getLengthProp, | ||
truncateChunk: truncateArrayBufferChunk, | ||
addChunk: addArrayBufferChunk, | ||
getFinalChunk: noop, | ||
finalize: finalizeArrayBuffer, | ||
}; |
import {getStreamContents} from './contents.js'; | ||
import {identity} from './utils.js'; | ||
import {identity, noop, getContentsProp} from './utils.js'; | ||
@@ -8,7 +8,7 @@ export async function getStreamAsArray(stream, options) { | ||
const initArray = () => ([]); | ||
const initArray = () => ({contents: []}); | ||
const increment = () => 1; | ||
const addArrayChunk = (convertedChunk, contents) => { | ||
const addArrayChunk = (convertedChunk, {contents}) => { | ||
contents.push(convertedChunk); | ||
@@ -29,4 +29,6 @@ return contents; | ||
getSize: increment, | ||
truncateChunk: noop, | ||
addChunk: addArrayChunk, | ||
finalize: identity, | ||
getFinalChunk: noop, | ||
finalize: getContentsProp, | ||
}; |
@@ -1,2 +0,2 @@ | ||
export const getStreamContents = async (stream, {init, convertChunk, getSize, addChunk, finalize}, {maxBuffer = Number.POSITIVE_INFINITY} = {}) => { | ||
export const getStreamContents = async (stream, {init, convertChunk, getSize, truncateChunk, addChunk, getFinalChunk, finalize}, {maxBuffer = Number.POSITIVE_INFINITY} = {}) => { | ||
if (!isAsyncIterable(stream)) { | ||
@@ -6,5 +6,4 @@ throw new Error('The first argument must be a Readable, a ReadableStream, or an async iterable.'); | ||
let length = 0; | ||
let contents = init(); | ||
const textDecoder = new TextDecoder(); | ||
const state = init(); | ||
state.length = 0; | ||
@@ -14,17 +13,10 @@ try { | ||
const chunkType = getChunkType(chunk); | ||
const convertedChunk = convertChunk[chunkType](chunk, textDecoder); | ||
const chunkSize = getSize(convertedChunk); | ||
if (length + chunkSize > maxBuffer) { | ||
throw new MaxBufferError(); | ||
} | ||
const newLength = length + chunkSize; | ||
contents = addChunk(convertedChunk, contents, newLength, length); | ||
length = newLength; | ||
const convertedChunk = convertChunk[chunkType](chunk, state); | ||
appendChunk({convertedChunk, state, getSize, truncateChunk, addChunk, maxBuffer}); | ||
} | ||
return finalize(contents, length, textDecoder); | ||
appendFinalChunk({state, convertChunk, getSize, truncateChunk, addChunk, getFinalChunk, maxBuffer}); | ||
return finalize(state); | ||
} catch (error) { | ||
error.bufferedData = finalize(contents, length, textDecoder); | ||
error.bufferedData = finalize(state); | ||
throw error; | ||
@@ -34,2 +26,32 @@ } | ||
const appendFinalChunk = ({state, getSize, truncateChunk, addChunk, getFinalChunk, maxBuffer}) => { | ||
const convertedChunk = getFinalChunk(state); | ||
if (convertedChunk !== undefined) { | ||
appendChunk({convertedChunk, state, getSize, truncateChunk, addChunk, maxBuffer}); | ||
} | ||
}; | ||
const appendChunk = ({convertedChunk, state, getSize, truncateChunk, addChunk, maxBuffer}) => { | ||
const chunkSize = getSize(convertedChunk); | ||
const newLength = state.length + chunkSize; | ||
if (newLength <= maxBuffer) { | ||
addNewChunk(convertedChunk, state, addChunk, newLength); | ||
return; | ||
} | ||
const truncatedChunk = truncateChunk(convertedChunk, maxBuffer - state.length); | ||
if (truncatedChunk !== undefined) { | ||
addNewChunk(truncatedChunk, state, addChunk, maxBuffer); | ||
} | ||
throw new MaxBufferError(); | ||
}; | ||
const addNewChunk = (convertedChunk, state, addChunk, newLength) => { | ||
state.contents = addChunk(convertedChunk, state, newLength); | ||
state.length = newLength; | ||
}; | ||
const isAsyncIterable = stream => typeof stream === 'object' && stream !== null && typeof stream[Symbol.asyncIterator] === 'function'; | ||
@@ -36,0 +58,0 @@ |
import {getStreamContents} from './contents.js'; | ||
import {identity, throwObjectStream, getLengthProp} from './utils.js'; | ||
import {identity, getContentsProp, throwObjectStream, getLengthProp} from './utils.js'; | ||
@@ -8,10 +8,15 @@ export async function getStreamAsString(stream, options) { | ||
const initString = () => ''; | ||
const initString = () => ({contents: '', textDecoder: new TextDecoder()}); | ||
const useTextDecoder = (chunk, textDecoder) => textDecoder.decode(chunk, {stream: true}); | ||
const useTextDecoder = (chunk, {textDecoder}) => textDecoder.decode(chunk, {stream: true}); | ||
const addStringChunk = (convertedChunk, contents) => contents + convertedChunk; | ||
const addStringChunk = (convertedChunk, {contents}) => contents + convertedChunk; | ||
const finalizeString = (contents, length, textDecoder) => `${contents}${textDecoder.decode()}`; | ||
const truncateStringChunk = (convertedChunk, chunkSize) => convertedChunk.slice(0, chunkSize); | ||
const getFinalStringChunk = ({textDecoder}) => { | ||
const finalChunk = textDecoder.decode(); | ||
return finalChunk === '' ? undefined : finalChunk; | ||
}; | ||
const stringMethods = { | ||
@@ -28,4 +33,6 @@ init: initString, | ||
getSize: getLengthProp, | ||
truncateChunk: truncateStringChunk, | ||
addChunk: addStringChunk, | ||
finalize: finalizeString, | ||
getFinalChunk: getFinalStringChunk, | ||
finalize: getContentsProp, | ||
}; |
export const identity = value => value; | ||
export const noop = () => undefined; | ||
export const getContentsProp = ({contents}) => contents; | ||
export const throwObjectStream = chunk => { | ||
@@ -4,0 +8,0 @@ throw new Error(`Streams in object mode are not supported: ${String(chunk)}`); |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
25151
324