Skip to content

Commit cf8c74e

Browse files
committed
feat(createReadStream): Support for passing readStream
1 parent d87e7d8 commit cf8c74e

File tree

4 files changed

+57
-17
lines changed

4 files changed

+57
-17
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ npm install jsonarrayfs
3131

3232
```ts
3333
import { createReadStream } from "jsonarrayfs";
34+
// import fs from "fs";
3435

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

39+
// Option 2: Pass an existing readStream
40+
// const streamer = await createReadStream(fs.createReadStream("./data.json", { encoding: "utf-8" }));
41+
3842
// Stream JSON array elements in batches of 100
3943
for await (const chunk of streamer.stream(100)) {
4044
// Your processing logic here

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsonarrayfs",
3-
"version": "1.1.3",
3+
"version": "1.2.0",
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

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@ class JsonArrayStreamer<T> {
1717
private chunkBuffer: string;
1818
private resultBuffer: T[];
1919

20-
private constructor(path: string, options?: ReadStreamOptions) {
21-
this.readStream = createReadStream(
22-
path,
23-
JsonArrayStreamer.sanitizeReadStreamOptions(options)
20+
private constructor(readStream: ReadStream);
21+
private constructor(filePath: string, options?: ReadStreamOptions);
22+
private constructor(
23+
source: ReadStream | string,
24+
options?: ReadStreamOptions
25+
) {
26+
this.readStream = JsonArrayStreamer.getReadStreamWithEncoding(
27+
source,
28+
options
2429
);
2530
this.rootDetected = false;
2631
this.elementDetected = false;
@@ -222,9 +227,6 @@ class JsonArrayStreamer<T> {
222227
options?: Record<string, any>
223228
) => {
224229
const sanitizedOptions = { ...(options || {}) };
225-
if (!sanitizedOptions.encoding) {
226-
sanitizedOptions.encoding = "utf-8";
227-
}
228230
Object.keys(sanitizedOptions).forEach((key) => {
229231
if (!["signal", "encoding", "highWatermark"].includes(key)) {
230232
delete sanitizedOptions[key];
@@ -234,14 +236,48 @@ class JsonArrayStreamer<T> {
234236
return sanitizedOptions as ReadStreamOptions;
235237
};
236238

237-
public static create = async <T>(
238-
path: string,
239+
private static getReadStreamWithEncoding = (
240+
source: ReadStream | string,
239241
options?: ReadStreamOptions
240242
) => {
241-
const instance = new JsonArrayStreamer<T>(path, options);
242-
await once(instance.readStream!, "readable");
243-
return instance;
243+
const readStream =
244+
source instanceof ReadStream
245+
? source
246+
: createReadStream(
247+
source,
248+
JsonArrayStreamer.sanitizeReadStreamOptions(options)
249+
);
250+
if (!readStream.readableEncoding) {
251+
console.warn(
252+
"Warning: Encoding not specified. Defaulting to UTF-8 to prevent issues."
253+
);
254+
readStream.setEncoding("utf-8");
255+
}
256+
return readStream;
244257
};
258+
259+
public static create<T>(
260+
readStream: ReadStream
261+
): Promise<JsonArrayStreamer<T>>;
262+
public static create<T>(
263+
filePath: string,
264+
options?: ReadStreamOptions
265+
): Promise<JsonArrayStreamer<T>>;
266+
public static async create<T>(
267+
source: ReadStream | string,
268+
options?: ReadStreamOptions
269+
): Promise<JsonArrayStreamer<T>> {
270+
const sourceIsReadableStream = source instanceof ReadStream;
271+
const instance = sourceIsReadableStream
272+
? new JsonArrayStreamer<T>(source)
273+
: new JsonArrayStreamer<T>(source, options);
274+
275+
if (sourceIsReadableStream) {
276+
await once(instance.readStream!, "readable");
277+
}
278+
279+
return instance;
280+
}
245281
}
246282

247283
export default JsonArrayStreamer;

0 commit comments

Comments
 (0)