Skip to content

Commit

Permalink
fix(cli): improve JSON stream performance (#4663)
Browse files Browse the repository at this point in the history
  • Loading branch information
mstade authored Sep 17, 2024
1 parent fa97dc2 commit 3908f83
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ async function writeIrToFile({
tmpdir: workspaceTempDir.path
});
const absolutePathToIr = AbsoluteFilePath.of(irFile.path);
await streamObjectToFile(absolutePathToIr, ir, { pretty: true });
await streamObjectToFile(absolutePathToIr, ir, { pretty: false });
context.logger.debug(`Wrote IR to ${absolutePathToIr}`);
return absolutePathToIr;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/commons/fs-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@fern-api/core-utils": "workspace:*",
"glob": "^8.0.3",
"glob-promise": "^5.0.0",
"json-stream-stringify": "^3.0.1",
"json-stream-stringify": "^3.1.4",
"stream-json": "^1.8.0",
"tmp-promise": "^3.0.3"
},
Expand Down
22 changes: 9 additions & 13 deletions packages/commons/fs-utils/src/streamObjectFromFile.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import { createReadStream } from "fs";
import { AbsoluteFilePath } from "./AbsoluteFilePath";
import StreamObject from "stream-json/streamers/StreamObject.js";
import StreamJSON from "stream-json";
import Assembler from "stream-json/Assembler.js";

export async function streamObjectFromFile(filePath: AbsoluteFilePath): Promise<unknown> {
const jsonStream = StreamObject.withParser();
const accumulator = {};
return new Promise((resolve, reject) => {
const jsonStream = StreamJSON.parser({
streamValues: false
});

jsonStream.on("data", ({ key, value }: { key: string; value: unknown }) =>
Object.assign(accumulator, { [key]: value })
);
const asm = Assembler.connectTo(jsonStream);
asm.on("done", ({ current }) => resolve(current));

return new Promise((resolve, reject) => {
createReadStream(filePath)
.pipe(jsonStream)
.on("error", reject)
.on("finish", () => {
resolve(accumulator);
});
createReadStream(filePath).pipe(jsonStream).on("error", reject);
});
}
13 changes: 8 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3908f83

Please sign in to comment.