@@ -141,18 +141,13 @@ export interface VSBufferReadable {
141141 read ( ) : VSBuffer | null ;
142142}
143143
144- /**
145- * A buffer readable stream emits data to listeners. The stream
146- * will only start emitting when the first data listener has
147- * been added or the resume() method has been called.
148- */
149- export interface VSBufferReadableStream {
144+ export interface ReadableStream < T > {
150145
151146 /**
152147 * The 'data' event is emitted whenever the stream is
153148 * relinquishing ownership of a chunk of data to a consumer.
154149 */
155- on ( event : 'data' , callback : ( chunk : VSBuffer ) => void ) : void ;
150+ on ( event : 'data' , callback : ( chunk : T ) => void ) : void ;
156151
157152 /**
158153 * Emitted when any error occurs.
@@ -188,6 +183,17 @@ export function isVSBufferReadableStream(obj: any): obj is VSBufferReadableStrea
188183 return candidate && [ candidate . on , candidate . pause , candidate . resume , candidate . destroy ] . every ( fn => typeof fn === 'function' ) ;
189184}
190185
186+ /**
187+ * A readable stream that sends data via VSBuffer.
188+ */
189+ export interface VSBufferReadableStream extends ReadableStream < VSBuffer > { }
190+
191+ export function isVSBufferReadableStream ( obj : any ) : obj is VSBufferReadableStream {
192+ const candidate : VSBufferReadableStream = obj ;
193+
194+ return candidate && [ candidate . on , candidate . pause , candidate . resume , candidate . destroy ] . every ( fn => typeof fn === 'function' ) ;
195+ }
196+
191197/**
192198 * Helper to fully read a VSBuffer readable into a single buffer.
193199 */
@@ -245,6 +251,19 @@ export function bufferToStream(buffer: VSBuffer): VSBufferReadableStream {
245251 return stream ;
246252}
247253
254+ /**
255+ * Helper to create a VSBufferStream from a Uint8Array stream.
256+ */
257+ export function toVSBufferReadableStream ( stream : ReadableStream < Uint8Array > ) : VSBufferReadableStream {
258+ const vsbufferStream = writeableBufferStream ( ) ;
259+
260+ stream . on ( 'data' , data => vsbufferStream . write ( VSBuffer . wrap ( data ) ) ) ;
261+ stream . on ( 'end' , ( ) => vsbufferStream . end ( ) ) ;
262+ stream . on ( 'error' , error => vsbufferStream . error ( error ) ) ;
263+
264+ return vsbufferStream ;
265+ }
266+
248267/**
249268 * Helper to create a VSBufferStream that can be pushed
250269 * buffers to. Will only start to emit data when a listener
0 commit comments