Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle large JSON payloads #2512

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/hardhat-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
"glob": "^7.1.3",
"immutable": "^4.0.0-rc.12",
"io-ts": "1.10.4",
"json-stream-stringify": "^2.0.4",
"lodash": "^4.17.11",
"merkle-patricia-tree": "^4.2.2",
"mnemonist": "^0.38.0",
Expand Down
8 changes: 5 additions & 3 deletions packages/hardhat-core/src/internal/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { HardhatError } from "./core/errors";
import { ERRORS } from "./core/errors-list";
import { glob, globSync } from "./util/glob";
import { createNonCryptographicHashBasedIdentifier } from "./util/hash";
import { writeJson } from "./util/write-json";

const log = debug("hardhat:core:artifacts");

Expand Down Expand Up @@ -130,7 +131,7 @@ export class Artifacts implements IArtifacts {
await fsExtra.ensureDir(path.dirname(artifactPath));

// write artifact
await fsExtra.writeJSON(artifactPath, artifact, {
await writeJson(artifactPath, artifact, {
spaces: 2,
});

Expand All @@ -142,7 +143,7 @@ export class Artifacts implements IArtifacts {
const debugFilePath = this._getDebugFilePath(artifactPath);
const debugFile = this._createDebugFile(artifactPath, pathToBuildInfo);

await fsExtra.writeJSON(debugFilePath, debugFile, {
await writeJson(debugFilePath, debugFile, {
spaces: 2,
});
}
Expand Down Expand Up @@ -171,7 +172,8 @@ export class Artifacts implements IArtifacts {
);

const buildInfoPath = path.join(buildInfoDir, `${buildInfoName}.json`);
await fsExtra.writeJson(buildInfoPath, buildInfo, { spaces: 2 });

await writeJson(buildInfoPath, buildInfo, { spaces: 2 });

return buildInfoPath;
}
Expand Down
36 changes: 36 additions & 0 deletions packages/hardhat-core/src/internal/util/write-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { createWriteStream } from 'fs';
import JsonStreamStringify from 'json-stream-stringify';

export interface Options {
encoding?: string;
flag?: string;
mode?: number;
replacer?: any;
spaces?: number | string;
}

export async function writeJson(file: string, object: object, options: Options) {
const { replacer, spaces, encoding, flag, mode } = options;
const jsonStream = new JsonStreamStringify(object, replacer, spaces);

const fsOptions = {
flags: flag,
encoding, mode
};
const f = createWriteStream(file, fsOptions);

jsonStream.once('error', () => console.log('Error in json-string-stream'));
jsonStream.pipe(f);

return new Promise((resolve, reject) => {
f.on('finish', () => {
f.close(err => {
if (err) {
reject(err);
} else {
resolve(file);
}
});
});
});
}
Loading