Some checks failed
CI/CD Pipeline / unit-tests (push) Failing after 1m16s
CI/CD Pipeline / integration-tests (push) Failing after 2m32s
CI/CD Pipeline / lint (push) Successful in 5m22s
CI/CD Pipeline / e2e-tests (push) Has been skipped
CI/CD Pipeline / build (push) Has been skipped
37 lines
1.1 KiB
JavaScript
37 lines
1.1 KiB
JavaScript
import {getStreamContents} from './contents.js';
|
|
import {identity, getContentsProp, throwObjectStream, getLengthProp} from './utils.js';
|
|
|
|
export async function getStreamAsString(stream, options) {
|
|
return getStreamContents(stream, stringMethods, options);
|
|
}
|
|
|
|
const initString = () => ({contents: '', textDecoder: new TextDecoder()});
|
|
|
|
const useTextDecoder = (chunk, {textDecoder}) => textDecoder.decode(chunk, {stream: true});
|
|
|
|
const addStringChunk = (convertedChunk, {contents}) => contents + convertedChunk;
|
|
|
|
const truncateStringChunk = (convertedChunk, chunkSize) => convertedChunk.slice(0, chunkSize);
|
|
|
|
const getFinalStringChunk = ({textDecoder}) => {
|
|
const finalChunk = textDecoder.decode();
|
|
return finalChunk === '' ? undefined : finalChunk;
|
|
};
|
|
|
|
const stringMethods = {
|
|
init: initString,
|
|
convertChunk: {
|
|
string: identity,
|
|
buffer: useTextDecoder,
|
|
arrayBuffer: useTextDecoder,
|
|
dataView: useTextDecoder,
|
|
typedArray: useTextDecoder,
|
|
others: throwObjectStream,
|
|
},
|
|
getSize: getLengthProp,
|
|
truncateChunk: truncateStringChunk,
|
|
addChunk: addStringChunk,
|
|
getFinalChunk: getFinalStringChunk,
|
|
finalize: getContentsProp,
|
|
};
|