@saulx/utils
Advanced tools
Comparing version 4.2.1 to 4.3.0
/// <reference types="node" resolution-mode="require"/> | ||
import { Stream } from 'stream'; | ||
export declare const readStream: (stream: Stream) => Promise<Buffer>; | ||
export declare const readStream: (stream: Stream, opts?: { | ||
throttle?: number; | ||
maxCunkSize?: number; | ||
}) => Promise<Buffer>; |
@@ -1,22 +0,61 @@ | ||
import { PassThrough } from 'stream'; | ||
export const readStream = (stream) => new Promise((resolve, reject) => { | ||
const s = new PassThrough(); | ||
s.on('error', (err) => { | ||
reject(err); | ||
}); | ||
let buffers = []; | ||
s.on('data', (s) => { | ||
if (typeof s === 'string') { | ||
buffers.push(Buffer.from(s)); | ||
import { Writable } from 'stream'; | ||
export const readStream = (stream, opts) => new Promise((resolve, reject) => { | ||
const maxCunkSize = opts?.maxCunkSize ?? 0; | ||
const throttle = opts?.throttle ?? 0; | ||
const buffers = []; | ||
const processChunk = (c, next) => { | ||
buffers.push(c.slice(0, maxCunkSize)); | ||
const chunkP = c.slice(maxCunkSize); | ||
if (chunkP.byteLength > maxCunkSize) { | ||
if (throttle) { | ||
setTimeout(() => { | ||
processChunk(chunkP, next); | ||
}, throttle); | ||
} | ||
else { | ||
processChunk(chunkP, next); | ||
} | ||
} | ||
else { | ||
buffers.push(s); | ||
if (throttle) { | ||
setTimeout(() => { | ||
next(); | ||
}, throttle); | ||
} | ||
else { | ||
next(); | ||
} | ||
} | ||
}; | ||
const s = new Writable({ | ||
write: (c, _encoding, next) => { | ||
if (maxCunkSize && c.byteLength > maxCunkSize) { | ||
processChunk(c, next); | ||
} | ||
else { | ||
if (typeof c === 'string') { | ||
buffers.push(Buffer.from(c)); | ||
} | ||
else { | ||
buffers.push(c); | ||
} | ||
if (throttle) { | ||
setTimeout(() => { | ||
next(); | ||
}, throttle); | ||
} | ||
else { | ||
next(); | ||
} | ||
} | ||
}, | ||
}); | ||
s.on('end', () => { | ||
s.on('error', (err) => { | ||
reject(err); | ||
}); | ||
s.on('finish', () => { | ||
resolve(Buffer.concat(buffers)); | ||
}); | ||
// @ts-ignore | ||
stream.pipe(s); | ||
}); | ||
//# sourceMappingURL=readStream.js.map |
{ | ||
"name": "@saulx/utils", | ||
"version": "4.2.1", | ||
"version": "4.3.0", | ||
"repository": "https://github.com/atelier-saulx/utils", | ||
@@ -5,0 +5,0 @@ "files": [ |
Sorry, the diff of this file is not supported yet
88135
1284