1
- import { ReadStream , createReadStream } from "fs" ;
1
+ import { createReadStream } from "fs" ;
2
+ import { Readable } from "stream" ;
2
3
import { once } from "events" ;
3
4
import type { ElementType , ReadStreamOptions } from "../index.types" ;
4
5
import { CHARACTER , ERRORS } from "../constants" ;
5
6
6
7
class JsonArrayStreamer < T > {
7
- private readStream : ReadStream | null ;
8
+ private readStream : Readable | null ;
8
9
private rootDetected : boolean ;
9
10
private elementDetected : boolean ;
10
11
private elementType : ElementType ;
@@ -17,12 +18,9 @@ class JsonArrayStreamer<T> {
17
18
private chunkBuffer : string ;
18
19
private resultBuffer : T [ ] ;
19
20
20
- private constructor ( readStream : ReadStream ) ;
21
+ private constructor ( readStream : Readable ) ;
21
22
private constructor ( filePath : string , options ?: ReadStreamOptions ) ;
22
- private constructor (
23
- source : ReadStream | string ,
24
- options ?: ReadStreamOptions
25
- ) {
23
+ private constructor ( source : Readable | string , options ?: ReadStreamOptions ) {
26
24
this . readStream = JsonArrayStreamer . getReadStreamWithEncoding (
27
25
source ,
28
26
options
@@ -201,7 +199,7 @@ class JsonArrayStreamer<T> {
201
199
}
202
200
}
203
201
204
- this . readStream ?. close ( ) ;
202
+ this . readStream ?. destroy ( ) ;
205
203
this . readStream = null ;
206
204
207
205
if ( this . chunkBuffer . length ) {
@@ -215,7 +213,7 @@ class JsonArrayStreamer<T> {
215
213
216
214
return this . resultBuffer ;
217
215
} catch ( error ) {
218
- this . readStream ?. close ( ) ;
216
+ this . readStream ?. destroy ( ) ;
219
217
this . resetParser ( ) ;
220
218
this . resultBuffer = [ ] ;
221
219
this . readStream = null ;
@@ -237,11 +235,11 @@ class JsonArrayStreamer<T> {
237
235
} ;
238
236
239
237
private static getReadStreamWithEncoding = (
240
- source : ReadStream | string ,
238
+ source : Readable | string ,
241
239
options ?: ReadStreamOptions
242
240
) => {
243
241
const readStream =
244
- source instanceof ReadStream
242
+ source instanceof Readable
245
243
? source
246
244
: createReadStream (
247
245
source ,
@@ -256,18 +254,16 @@ class JsonArrayStreamer<T> {
256
254
return readStream ;
257
255
} ;
258
256
259
- public static create < T > (
260
- readStream : ReadStream
261
- ) : Promise < JsonArrayStreamer < T > > ;
257
+ public static create < T > ( readStream : Readable ) : Promise < JsonArrayStreamer < T > > ;
262
258
public static create < T > (
263
259
filePath : string ,
264
260
options ?: ReadStreamOptions
265
261
) : Promise < JsonArrayStreamer < T > > ;
266
262
public static async create < T > (
267
- source : ReadStream | string ,
263
+ source : Readable | string ,
268
264
options ?: ReadStreamOptions
269
265
) : Promise < JsonArrayStreamer < T > > {
270
- const sourceIsReadableStream = source instanceof ReadStream ;
266
+ const sourceIsReadableStream = source instanceof Readable ;
271
267
const instance = sourceIsReadableStream
272
268
? new JsonArrayStreamer < T > ( source )
273
269
: new JsonArrayStreamer < T > ( source , options ) ;
0 commit comments