Skip to content

Commit 9846a4d

Browse files
committed
fix(createReadStream): Add support to stream from any Readable
1 parent cf8c74e commit 9846a4d

File tree

4 files changed

+30
-22
lines changed

4 files changed

+30
-22
lines changed

README.md

+15-3
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,25 @@ npm install jsonarrayfs
3131

3232
```ts
3333
import { createReadStream } from "jsonarrayfs";
34-
// import fs from "fs";
3534

3635
// Option 1: Create a streamer to read JSON array elements from a file
3736
const streamer = await createReadStream("./data.json", { encoding: "utf-8" });
3837

39-
// Option 2: Pass an existing readStream
40-
// const streamer = await createReadStream(fs.createReadStream("./data.json", { encoding: "utf-8" }));
38+
// Option 2: Create a streamer to read JSON array elements from an existing Readable:
39+
40+
// From a file
41+
import fs from "fs";
42+
const readStream = fs.createReadStream("./data.json", { encoding: "utf-8" });
43+
const streamer = await createReadStream(readStream);
44+
45+
// From an API response
46+
import { ReadableStream } from "stream/web";
47+
import { Readable } from "stream";
48+
const response = await fetch("https://www.example.com/json-data");
49+
const readableStream = Readable.fromWeb(
50+
response.body as unknown as ReadableStream
51+
);
52+
const streamer = await createReadStream(readableStream);
4153

4254
// Stream JSON array elements in batches of 100
4355
for await (const chunk of streamer.stream(100)) {

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsonarrayfs",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "Efficiently handle JSON array files in Node.js with minimal memory usage. Perfect for processing large data volumes without worrying about memory limitations.",
55
"main": "./dist/index.js",
66
"module": "./dist/index.mjs",

src/modules/JsonArrayStreamer.ts

+12-16
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { ReadStream, createReadStream } from "fs";
1+
import { createReadStream } from "fs";
2+
import { Readable } from "stream";
23
import { once } from "events";
34
import type { ElementType, ReadStreamOptions } from "../index.types";
45
import { CHARACTER, ERRORS } from "../constants";
56

67
class 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

Comments
 (0)