@@ -27,28 +27,25 @@ export class StreamPump {
27
27
new Stream . Readable ( ) . readableHighWaterMark ;
28
28
this . accumalatedSize = 0 ;
29
29
this . stream = stream ;
30
- this . enqueue = this . enqueue . bind ( this ) ;
31
- this . error = this . error . bind ( this ) ;
32
- this . close = this . close . bind ( this ) ;
33
30
}
34
31
35
- size ( chunk : Uint8Array ) {
36
- return chunk ? .byteLength || 0 ;
32
+ size ( chunk : Uint8Array ) : number {
33
+ return chunk . byteLength || 0 ;
37
34
}
38
35
39
- start ( controller : ReadableStreamController < Uint8Array > ) {
36
+ start ( controller : ReadableStreamController < Uint8Array > ) : void {
40
37
this . controller = controller ;
41
38
this . stream . on ( "data" , this . enqueue ) ;
42
39
this . stream . once ( "error" , this . error ) ;
43
40
this . stream . once ( "end" , this . close ) ;
44
41
this . stream . once ( "close" , this . close ) ;
45
42
}
46
43
47
- pull ( ) {
44
+ pull ( ) : void {
48
45
this . resume ( ) ;
49
46
}
50
47
51
- cancel ( reason ?: Error ) {
48
+ cancel ( reason ?: Error ) : void {
52
49
if ( this . stream . destroy ) {
53
50
this . stream . destroy ( reason ) ;
54
51
}
@@ -59,17 +56,17 @@ export class StreamPump {
59
56
this . stream . off ( "close" , this . close ) ;
60
57
}
61
58
62
- enqueue ( chunk : Uint8Array | string ) {
59
+ enqueue = ( chunk : Uint8Array | string ) : void => {
63
60
if ( this . controller ) {
64
61
try {
65
- let bytes = chunk instanceof Uint8Array ? chunk : Buffer . from ( chunk ) ;
62
+ const bytes = chunk instanceof Uint8Array ? chunk : Buffer . from ( chunk ) ;
66
63
67
- let available = ( this . controller . desiredSize || 0 ) - bytes . byteLength ;
64
+ const available = ( this . controller . desiredSize || 0 ) - bytes . byteLength ;
68
65
this . controller . enqueue ( bytes ) ;
69
66
if ( available <= 0 ) {
70
67
this . pause ( ) ;
71
68
}
72
- } catch ( error : any ) {
69
+ } catch ( error ) {
73
70
this . controller . error (
74
71
new Error (
75
72
"Could not create Buffer, chunk must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object"
@@ -78,60 +75,62 @@ export class StreamPump {
78
75
this . cancel ( ) ;
79
76
}
80
77
}
81
- }
78
+ } ;
82
79
83
- pause ( ) {
80
+ pause ( ) : void {
84
81
if ( this . stream . pause ) {
85
82
this . stream . pause ( ) ;
86
83
}
87
84
}
88
85
89
- resume ( ) {
86
+ resume ( ) : void {
90
87
if ( this . stream . readable && this . stream . resume ) {
91
88
this . stream . resume ( ) ;
92
89
}
93
90
}
94
91
95
- close ( ) {
92
+ close = ( ) : void => {
96
93
if ( this . controller ) {
97
94
this . controller . close ( ) ;
98
95
delete this . controller ;
99
96
}
100
- }
97
+ } ;
101
98
102
- error ( error : Error ) {
99
+ error = ( error : Error ) : void => {
103
100
if ( this . controller ) {
104
101
this . controller . error ( error ) ;
105
102
delete this . controller ;
106
103
}
107
- }
104
+ } ;
108
105
}
109
106
110
- export const createReadableStreamFromReadable = (
107
+ export function createReadableStreamFromReadable (
111
108
source : Readable & { readableHighWaterMark ?: number }
112
- ) = > {
113
- let pump = new StreamPump ( source ) ;
114
- let stream = new ReadableStream ( pump , pump ) ;
109
+ ) : ReadableStream < Uint8Array > {
110
+ const pump = new StreamPump ( source ) ;
111
+ const stream = new ReadableStream ( pump , pump ) ;
115
112
return stream ;
116
- } ;
113
+ }
117
114
118
- export async function writeReadableStreamToWritable (
119
- stream : ReadableStream ,
115
+ export async function writeReadableStreamToWritable < R = any > (
116
+ stream : ReadableStream < R > ,
120
117
writable : Writable
121
- ) {
122
- let reader = stream . getReader ( ) ;
123
- let flushable = writable as { flush ?: Function } ;
118
+ ) : Promise < void > {
119
+ const reader = stream . getReader ( ) ;
120
+ const flushable = writable as { flush ?: ( ) => void } ;
124
121
125
122
try {
123
+ // eslint-disable-next-line no-constant-condition, @typescript-eslint/no-unnecessary-condition -- this is expected to be exhaustive
126
124
while ( true ) {
127
- let { done, value } = await reader . read ( ) ;
125
+ const { done, value } = await reader . read ( ) ;
128
126
129
127
if ( done ) {
130
128
writable . end ( ) ;
131
129
break ;
132
130
}
133
131
134
132
writable . write ( value ) ;
133
+
135
134
if ( typeof flushable . flush === "function" ) {
136
135
flushable . flush ( ) ;
137
136
}
0 commit comments