1- import { ReadStream , createReadStream } from "fs" ;
1+ import { createReadStream } from "fs" ;
2+ import { Readable } from "stream" ;
23import { once } from "events" ;
34import type { ElementType , ReadStreamOptions } from "../index.types" ;
45import { CHARACTER , ERRORS } from "../constants" ;
56
67class JsonArrayStreamer < T > {
7- private readStream : ReadStream | null ;
8+ private readStream : Readable | null ;
89 private rootDetected : boolean ;
910 private elementDetected : boolean ;
1011 private elementType : ElementType ;
@@ -17,12 +18,9 @@ class JsonArrayStreamer<T> {
1718 private chunkBuffer : string ;
1819 private resultBuffer : T [ ] ;
1920
20- private constructor ( readStream : ReadStream ) ;
21+ private constructor ( readStream : Readable ) ;
2122 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 ) {
2624 this . readStream = JsonArrayStreamer . getReadStreamWithEncoding (
2725 source ,
2826 options
@@ -201,7 +199,7 @@ class JsonArrayStreamer<T> {
201199 }
202200 }
203201
204- this . readStream ?. close ( ) ;
202+ this . readStream ?. destroy ( ) ;
205203 this . readStream = null ;
206204
207205 if ( this . chunkBuffer . length ) {
@@ -215,7 +213,7 @@ class JsonArrayStreamer<T> {
215213
216214 return this . resultBuffer ;
217215 } catch ( error ) {
218- this . readStream ?. close ( ) ;
216+ this . readStream ?. destroy ( ) ;
219217 this . resetParser ( ) ;
220218 this . resultBuffer = [ ] ;
221219 this . readStream = null ;
@@ -237,11 +235,11 @@ class JsonArrayStreamer<T> {
237235 } ;
238236
239237 private static getReadStreamWithEncoding = (
240- source : ReadStream | string ,
238+ source : Readable | string ,
241239 options ?: ReadStreamOptions
242240 ) => {
243241 const readStream =
244- source instanceof ReadStream
242+ source instanceof Readable
245243 ? source
246244 : createReadStream (
247245 source ,
@@ -256,18 +254,16 @@ class JsonArrayStreamer<T> {
256254 return readStream ;
257255 } ;
258256
259- public static create < T > (
260- readStream : ReadStream
261- ) : Promise < JsonArrayStreamer < T > > ;
257+ public static create < T > ( readStream : Readable ) : Promise < JsonArrayStreamer < T > > ;
262258 public static create < T > (
263259 filePath : string ,
264260 options ?: ReadStreamOptions
265261 ) : Promise < JsonArrayStreamer < T > > ;
266262 public static async create < T > (
267- source : ReadStream | string ,
263+ source : Readable | string ,
268264 options ?: ReadStreamOptions
269265 ) : Promise < JsonArrayStreamer < T > > {
270- const sourceIsReadableStream = source instanceof ReadStream ;
266+ const sourceIsReadableStream = source instanceof Readable ;
271267 const instance = sourceIsReadableStream
272268 ? new JsonArrayStreamer < T > ( source )
273269 : new JsonArrayStreamer < T > ( source , options ) ;
0 commit comments