@saulx/utils
Advanced tools
Comparing version
@@ -5,3 +5,3 @@ /// <reference types="node" resolution-mode="require"/> | ||
throttle?: number; | ||
maxCunkSize?: number; | ||
maxChunkSize?: number; | ||
}) => Promise<Buffer>; |
import { Writable } from 'stream'; | ||
export const readStream = (stream, opts) => new Promise((resolve, reject) => { | ||
const maxCunkSize = opts?.maxCunkSize ?? 0; | ||
const maxChunkSize = opts?.maxChunkSize ?? 0; | ||
const throttle = opts?.throttle ?? 0; | ||
const buffers = []; | ||
const processChunk = (c, next) => { | ||
buffers.push(c.slice(0, maxCunkSize)); | ||
const chunkP = c.slice(maxCunkSize, c.byteLength); | ||
if (chunkP.byteLength > maxCunkSize) { | ||
buffers.push(c.slice(0, maxChunkSize)); | ||
const chunkP = c.slice(maxChunkSize, c.byteLength); | ||
if (chunkP.byteLength > maxChunkSize) { | ||
if (throttle) { | ||
@@ -33,3 +33,3 @@ setTimeout(() => { | ||
write: (c, _encoding, next) => { | ||
if (maxCunkSize && c.byteLength > maxCunkSize) { | ||
if (maxChunkSize && c.byteLength > maxChunkSize) { | ||
processChunk(c, next); | ||
@@ -36,0 +36,0 @@ } |
{ | ||
"name": "@saulx/utils", | ||
"version": "4.3.1", | ||
"version": "4.3.2", | ||
"repository": "https://github.com/atelier-saulx/utils", | ||
@@ -5,0 +5,0 @@ "files": [ |
Sorry, the diff of this file is not supported yet
88252
0.01%