@@ -6,6 +6,7 @@ import type * as misc from './types/misc';
66import { ENCODING_UTF8 , TEncodingExtended } from '../encoding' ;
77import { bufferFrom } from '../internal/buffer' ;
88import queueMicrotask from '../queueMicrotask' ;
9+ import { Readable } from 'stream' ;
910
1011export const isWin = process . platform === 'win32' ;
1112
@@ -178,6 +179,15 @@ export function validateFd(fd) {
178179 if ( ! isFd ( fd ) ) throw TypeError ( ERRSTR . FD ) ;
179180}
180181
182+ export function streamToBuffer ( stream : Readable ) {
183+ const chunks : any [ ] = [ ] ;
184+ return new Promise < Buffer > ( ( resolve , reject ) => {
185+ stream . on ( 'data' , chunk => chunks . push ( chunk ) ) ;
186+ stream . on ( 'end' , ( ) => resolve ( Buffer . concat ( chunks ) ) ) ;
187+ stream . on ( 'error' , reject ) ;
188+ } ) ;
189+ }
190+
181191export function dataToBuffer ( data : misc . TData , encoding : string = ENCODING_UTF8 ) : Buffer {
182192 if ( Buffer . isBuffer ( data ) ) return data ;
183193 else if ( data instanceof Uint8Array ) return bufferFrom ( data ) ;
@@ -289,6 +299,16 @@ export function bufferToEncoding(buffer: Buffer, encoding?: TEncodingExtended):
289299 else return buffer . toString ( encoding ) ;
290300}
291301
302+ export function isReadableStream ( stream ) : stream is Readable {
303+ return (
304+ stream !== null &&
305+ typeof stream === 'object' &&
306+ typeof stream . pipe === 'function' &&
307+ typeof stream . on === 'function' &&
308+ stream . readable === true
309+ ) ;
310+ }
311+
292312const isSeparator = ( str , i ) => {
293313 let char = str [ i ] ;
294314 return i > 0 && ( char === '/' || ( isWin && char === '\\' ) ) ;
0 commit comments