@nomicfoundation/hardhat-utils 4.4.0 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/dist/src/bytes.d.ts +5 -4
- package/dist/src/bytes.d.ts.map +1 -1
- package/dist/src/bytes.js +22 -13
- package/dist/src/bytes.js.map +1 -1
- package/dist/src/fs.d.ts +25 -29
- package/dist/src/fs.d.ts.map +1 -1
- package/dist/src/fs.js +84 -82
- package/dist/src/fs.js.map +1 -1
- package/dist/src/internal/fs.d.ts +6 -0
- package/dist/src/internal/fs.d.ts.map +1 -1
- package/dist/src/internal/fs.js +51 -3
- package/dist/src/internal/fs.js.map +1 -1
- package/dist/src/internal/json.d.ts +48 -0
- package/dist/src/internal/json.d.ts.map +1 -0
- package/dist/src/internal/json.js +54 -0
- package/dist/src/internal/json.js.map +1 -0
- package/dist/src/internal/request.d.ts.map +1 -1
- package/dist/src/internal/request.js +3 -2
- package/dist/src/internal/request.js.map +1 -1
- package/package.json +2 -2
- package/src/bytes.ts +31 -14
- package/src/fs.ts +96 -87
- package/src/internal/fs.ts +66 -3
- package/src/internal/json.ts +59 -0
- package/src/internal/request.ts +3 -2
package/dist/src/internal/fs.js
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import fsPromises from "node:fs/promises";
|
|
2
2
|
import path from "node:path";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
3
|
+
import { pipeline } from "node:stream/promises";
|
|
4
|
+
import { ensureError, ensureNodeErrnoExceptionError } from "../error.js";
|
|
5
|
+
import { FileSystemAccessError, JsonSerializationError, NotADirectoryError, } from "../errors/fs.js";
|
|
6
|
+
import { exists, isDirectory, mkdir, remove } from "../fs.js";
|
|
7
|
+
// We don't load json-stream-stringify on startup because it's only used by
|
|
8
|
+
// writeLargeJsonFile when it can't use JSON.stringify.
|
|
9
|
+
let jsonStreamStringify;
|
|
6
10
|
/**
|
|
7
11
|
* Like `readdirOrEmpty`, but returns `Dirent` entries to know if an entry is a
|
|
8
12
|
* directory or not without an extra `lstat` syscall.
|
|
@@ -83,4 +87,48 @@ export async function collectAllDirectoriesMatching(dirFrom, results, matches) {
|
|
|
83
87
|
await collectAllDirectoriesMatching(absolutePathToFile, results, matches);
|
|
84
88
|
}));
|
|
85
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* Writes an object to a JSON file as a stream, which never holds the whole
|
|
92
|
+
* JSON as a single string. It's used by `writeLargeJsonFile` when it can't use
|
|
93
|
+
* `JSON.stringify`.
|
|
94
|
+
*/
|
|
95
|
+
export async function streamJsonToFile(absolutePathToFile, object) {
|
|
96
|
+
const dirPath = path.dirname(absolutePathToFile);
|
|
97
|
+
const dirExists = await exists(dirPath);
|
|
98
|
+
if (!dirExists) {
|
|
99
|
+
await mkdir(dirPath);
|
|
100
|
+
}
|
|
101
|
+
let fileHandle;
|
|
102
|
+
try {
|
|
103
|
+
fileHandle = await fsPromises.open(absolutePathToFile, "w");
|
|
104
|
+
if (jsonStreamStringify === undefined) {
|
|
105
|
+
jsonStreamStringify = await import("json-stream-stringify");
|
|
106
|
+
}
|
|
107
|
+
const jsonStream = new jsonStreamStringify.JsonStreamStringify(object);
|
|
108
|
+
const fileWriteStream = fileHandle.createWriteStream();
|
|
109
|
+
await pipeline(jsonStream, fileWriteStream);
|
|
110
|
+
}
|
|
111
|
+
catch (e) {
|
|
112
|
+
ensureError(e);
|
|
113
|
+
// if the directory was created, we should remove it
|
|
114
|
+
if (dirExists === false) {
|
|
115
|
+
try {
|
|
116
|
+
await remove(dirPath);
|
|
117
|
+
// we don't want to override the original error
|
|
118
|
+
}
|
|
119
|
+
catch (_error) { }
|
|
120
|
+
}
|
|
121
|
+
// If the code is defined, we assume the error to be related to the file system
|
|
122
|
+
if ("code" in e && e.code !== undefined) {
|
|
123
|
+
throw new FileSystemAccessError(e.message, e);
|
|
124
|
+
}
|
|
125
|
+
// Otherwise, we assume the error to be related to the file formatting
|
|
126
|
+
throw new JsonSerializationError(absolutePathToFile, e);
|
|
127
|
+
}
|
|
128
|
+
finally {
|
|
129
|
+
// NOTE: Historically, not closing the file handle caused issues on Windows,
|
|
130
|
+
// for example, when trying to move the file previously written to by this function
|
|
131
|
+
await fileHandle?.close();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
86
134
|
//# sourceMappingURL=fs.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fs.js","sourceRoot":"","sources":["../../../src/internal/fs.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"fs.js","sourceRoot":"","sources":["../../../src/internal/fs.ts"],"names":[],"mappings":"AAIA,OAAO,UAAU,MAAM,kBAAkB,CAAC;AAC1C,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAEhD,OAAO,EAAE,WAAW,EAAE,6BAA6B,EAAE,MAAM,aAAa,CAAC;AACzE,OAAO,EACL,qBAAqB,EACrB,sBAAsB,EACtB,kBAAkB,GACnB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAE9D,2EAA2E;AAC3E,uDAAuD;AACvD,IAAI,mBAA2D,CAAC;AAEhE;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,2BAA2B,CAC/C,OAAe;IAEf,IAAI,CAAC;QACH,OAAO,MAAM,UAAU,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,aAAa,EAAE,IAAI,EAAE,CAAC,CAAC;IACpE,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,6BAA6B,CAAC,CAAC,CAAC,CAAC;QAEjC,IAAI,CAAC,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YACxB,OAAO,EAAE,CAAC;QACZ,CAAC;QAED,IAAI,CAAC,CAAC,IAAI,KAAK,SA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* At its peak, `JSON.parse` holds both the whole text, as a single string, and
|
|
5
|
+
* the objects it builds:
|
|
6
|
+
* - V8 stores a string with 1 byte per character only if every character is
|
|
7
|
+
* in Latin-1, and otherwise with 2. Real-world JSON rarely qualifies: solc's
|
|
8
|
+
* output, for example, usually includes NatSpec comments, which can use
|
|
9
|
+
* characters like `’`, `→` or `—`. Each UTF-8 byte decodes to at most one
|
|
10
|
+
* character, so the string takes at most 2 bytes per byte of JSON.
|
|
11
|
+
* - The objects add about 1.6 bytes per byte of JSON.
|
|
12
|
+
*
|
|
13
|
+
* Measured on real solc output (19 and 95 MB) as the smallest
|
|
14
|
+
* `--max-old-space-size` the parse succeeds with, the peak was about 3.6 bytes
|
|
15
|
+
* per byte, the worst of Node 22, 24 and 26. Rounded up.
|
|
16
|
+
*/
|
|
17
|
+
export declare const READ_HEAP_BYTES_PER_JSON_BYTE = 4;
|
|
18
|
+
/**
|
|
19
|
+
* The heap needed per byte of JSON to serialize it with `JSON.stringify`.
|
|
20
|
+
*
|
|
21
|
+
* The objects already exist, so it's just the output string, at 2 bytes per
|
|
22
|
+
* character (see above). Measured the same way on Node 22, 24 and 26, a
|
|
23
|
+
* ~400M-character output needed exactly the string's size, with no extra
|
|
24
|
+
* peak.
|
|
25
|
+
*/
|
|
26
|
+
export declare const WRITE_HEAP_BYTES_PER_JSON_BYTE = 2;
|
|
27
|
+
/**
|
|
28
|
+
* Extra heap we require on top of our own allocation, for everything else that
|
|
29
|
+
* needs it while that allocation is alive, like garbage collection and the
|
|
30
|
+
* rest of the program, and to absorb small errors in the estimates above.
|
|
31
|
+
*
|
|
32
|
+
* It isn't measured: it's a cautious round number, small next to Node's
|
|
33
|
+
* default heap limits (about 2 to 4 GiB, depending on the machine's memory).
|
|
34
|
+
*/
|
|
35
|
+
export declare const HEAP_MARGIN_BYTES: number;
|
|
36
|
+
/**
|
|
37
|
+
* Checks whether the heap can grow by `bytes`, plus a safety margin, before
|
|
38
|
+
* reaching its limit.
|
|
39
|
+
*
|
|
40
|
+
* Running out of heap crashes the process, and can't be caught, so this must
|
|
41
|
+
* be called right before the allocation. At each `await` in between, other
|
|
42
|
+
* code can run and use memory.
|
|
43
|
+
*
|
|
44
|
+
* @param bytes The number of bytes about to be allocated in the heap.
|
|
45
|
+
* @returns `true` if there's enough room for them.
|
|
46
|
+
*/
|
|
47
|
+
export declare function hasHeapHeadroomFor(bytes: number): boolean;
|
|
48
|
+
//# sourceMappingURL=json.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../../src/internal/json.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,6BAA6B,IAAI,CAAC;AAE/C;;;;;;;GAOG;AACH,eAAO,MAAM,8BAA8B,IAAI,CAAC;AAEhD;;;;;;;GAOG;AACH,eAAO,MAAM,iBAAiB,EAAE,MAA0B,CAAC;AAE3D;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAMzD"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// NOTE: We use the default import so that tests can mock getHeapStatistics.
|
|
2
|
+
import v8 from "node:v8";
|
|
3
|
+
/**
|
|
4
|
+
* The heap needed per byte of JSON to parse it with `JSON.parse`.
|
|
5
|
+
*
|
|
6
|
+
* At its peak, `JSON.parse` holds both the whole text, as a single string, and
|
|
7
|
+
* the objects it builds:
|
|
8
|
+
* - V8 stores a string with 1 byte per character only if every character is
|
|
9
|
+
* in Latin-1, and otherwise with 2. Real-world JSON rarely qualifies: solc's
|
|
10
|
+
* output, for example, usually includes NatSpec comments, which can use
|
|
11
|
+
* characters like `’`, `→` or `—`. Each UTF-8 byte decodes to at most one
|
|
12
|
+
* character, so the string takes at most 2 bytes per byte of JSON.
|
|
13
|
+
* - The objects add about 1.6 bytes per byte of JSON.
|
|
14
|
+
*
|
|
15
|
+
* Measured on real solc output (19 and 95 MB) as the smallest
|
|
16
|
+
* `--max-old-space-size` the parse succeeds with, the peak was about 3.6 bytes
|
|
17
|
+
* per byte, the worst of Node 22, 24 and 26. Rounded up.
|
|
18
|
+
*/
|
|
19
|
+
export const READ_HEAP_BYTES_PER_JSON_BYTE = 4;
|
|
20
|
+
/**
|
|
21
|
+
* The heap needed per byte of JSON to serialize it with `JSON.stringify`.
|
|
22
|
+
*
|
|
23
|
+
* The objects already exist, so it's just the output string, at 2 bytes per
|
|
24
|
+
* character (see above). Measured the same way on Node 22, 24 and 26, a
|
|
25
|
+
* ~400M-character output needed exactly the string's size, with no extra
|
|
26
|
+
* peak.
|
|
27
|
+
*/
|
|
28
|
+
export const WRITE_HEAP_BYTES_PER_JSON_BYTE = 2;
|
|
29
|
+
/**
|
|
30
|
+
* Extra heap we require on top of our own allocation, for everything else that
|
|
31
|
+
* needs it while that allocation is alive, like garbage collection and the
|
|
32
|
+
* rest of the program, and to absorb small errors in the estimates above.
|
|
33
|
+
*
|
|
34
|
+
* It isn't measured: it's a cautious round number, small next to Node's
|
|
35
|
+
* default heap limits (about 2 to 4 GiB, depending on the machine's memory).
|
|
36
|
+
*/
|
|
37
|
+
export const HEAP_MARGIN_BYTES = 256 * 1024 * 1024;
|
|
38
|
+
/**
|
|
39
|
+
* Checks whether the heap can grow by `bytes`, plus a safety margin, before
|
|
40
|
+
* reaching its limit.
|
|
41
|
+
*
|
|
42
|
+
* Running out of heap crashes the process, and can't be caught, so this must
|
|
43
|
+
* be called right before the allocation. At each `await` in between, other
|
|
44
|
+
* code can run and use memory.
|
|
45
|
+
*
|
|
46
|
+
* @param bytes The number of bytes about to be allocated in the heap.
|
|
47
|
+
* @returns `true` if there's enough room for them.
|
|
48
|
+
*/
|
|
49
|
+
export function hasHeapHeadroomFor(bytes) {
|
|
50
|
+
// `total_available_size` counts garbage that hasn't been collected yet as
|
|
51
|
+
// used, so this errs toward `false`, which is the safe direction.
|
|
52
|
+
return (bytes + HEAP_MARGIN_BYTES <= v8.getHeapStatistics().total_available_size);
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=json.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json.js","sourceRoot":"","sources":["../../../src/internal/json.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,OAAO,EAAE,MAAM,SAAS,CAAC;AAEzB;;;;;;;;;;;;;;;GAeG;AACH,MAAM,CAAC,MAAM,6BAA6B,GAAG,CAAC,CAAC;AAE/C;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,8BAA8B,GAAG,CAAC,CAAC;AAEhD;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAW,GAAG,GAAG,IAAI,GAAG,IAAI,CAAC;AAE3D;;;;;;;;;;GAUG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAa;IAC9C,0EAA0E;IAC1E,kEAAkE;IAClE,OAAO,CACL,KAAK,GAAG,iBAAiB,IAAI,EAAE,CAAC,iBAAiB,EAAE,CAAC,oBAAoB,CACzE,CAAC;AACJ,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../../src/internal/request.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AACvE,OAAO,KAAK,YAAY,MAAM,aAAa,CAAC;AAC5C,OAAO,KAAK,KAAK,OAAO,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../../src/internal/request.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AACvE,OAAO,KAAK,YAAY,MAAM,aAAa,CAAC;AAC5C,OAAO,KAAK,KAAK,OAAO,MAAM,QAAQ,CAAC;AAoBvC,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,KAAK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O,EAAE,KAAK,CAAC,GAC/C,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC,CAS7B;AAED,wBAAsB,iBAAiB,CACrC,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,OAAO,CAAC,IAAI,CAAC,OAAO,GAC5B,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAOvB;AAED,wBAAsB,kBAAkB,CACtC,OAAO,EAAE,OAAO,CAAC,KAAK,CAAC,OAAO,GAC7B,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAMxB;AAED,wBAAgB,wBAAwB,CACtC,OAAO,GAAE,MAAwC,EACjD,gBAAgB,GAAE,OAAe,GAChC,OAAO,CAAC,MAAM,CAAC,OAAO,CAaxB;AAED,wBAAgB,WAAW,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAItD;AAED,wBAAgB,WAAW,CAAC,CAAC,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,GAAG,IAAI,CAuB9D"}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import crypto from "node:crypto";
|
|
2
|
-
import { STATUS_CODES } from "node:http";
|
|
3
2
|
import path from "node:path";
|
|
4
3
|
import { mkdir } from "../fs.js";
|
|
5
4
|
import { isObject } from "../lang.js";
|
|
@@ -263,7 +262,9 @@ function describeResponseError(e) {
|
|
|
263
262
|
if (typeof statusCode !== "number") {
|
|
264
263
|
return;
|
|
265
264
|
}
|
|
266
|
-
|
|
265
|
+
// Importing `node:http` loads Node's whole HTTP stack, so we defer
|
|
266
|
+
// using `process.getBuiltinModule` as a dynamic load.
|
|
267
|
+
const reason = process.getBuiltinModule("node:http").STATUS_CODES[statusCode];
|
|
267
268
|
e.message = `Response status code ${statusCode}${reason !== undefined ? `: ${reason}` : ""}`;
|
|
268
269
|
}
|
|
269
270
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.js","sourceRoot":"","sources":["../../../src/internal/request.ts"],"names":[],"mappings":"AAIA,OAAO,MAA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,OAAO,CAAC;AACjB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nomicfoundation/hardhat-utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"description": "Utilities for Hardhat and its plugins",
|
|
5
5
|
"homepage": "https://github.com/NomicFoundation/hardhat/tree/main/packages/hardhat-utils",
|
|
6
6
|
"repository": {
|
|
@@ -63,7 +63,7 @@
|
|
|
63
63
|
"c8": "^12.0.0",
|
|
64
64
|
"eslint": "9.39.5",
|
|
65
65
|
"expect-type": "^1.4.0",
|
|
66
|
-
"prettier": "3.9.
|
|
66
|
+
"prettier": "3.9.9",
|
|
67
67
|
"rimraf": "^6.1.3",
|
|
68
68
|
"tsx": "^4.23.1",
|
|
69
69
|
"typescript": "~6.0.3"
|
package/src/bytes.ts
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
|
+
import { constants as bufferConstants } from "node:buffer";
|
|
1
2
|
import { Readable } from "node:stream";
|
|
2
3
|
|
|
4
|
+
import { InvalidParameterError } from "./common-errors.js";
|
|
5
|
+
import { ensureError } from "./error.js";
|
|
3
6
|
import { buildLpsTable, parseJsonStream } from "./internal/bytes.js";
|
|
7
|
+
import {
|
|
8
|
+
hasHeapHeadroomFor,
|
|
9
|
+
READ_HEAP_BYTES_PER_JSON_BYTE,
|
|
10
|
+
} from "./internal/json.js";
|
|
4
11
|
|
|
5
12
|
/**
|
|
6
13
|
* Checks if a value is an instance of Uint8Array.
|
|
@@ -110,27 +117,37 @@ export function bytesIncludesUtf8String(
|
|
|
110
117
|
/**
|
|
111
118
|
* Parses UTF-8 encoded JSON bytes into a value.
|
|
112
119
|
*
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
120
|
+
* The bytes are decoded and parsed with `JSON.parse`, which is much faster,
|
|
121
|
+
* when the decoded text fits in a single string and the heap has room for it.
|
|
122
|
+
* Otherwise, they are parsed as a stream, which never holds the whole text as
|
|
123
|
+
* one string. Both give the same result.
|
|
117
124
|
*
|
|
118
125
|
* @param bytes The UTF-8 encoded JSON bytes.
|
|
119
126
|
* @returns The parsed JSON object.
|
|
127
|
+
* @throws InvalidParameterError if the bytes aren't valid UTF-8 encoded JSON.
|
|
120
128
|
*/
|
|
121
129
|
export async function parseJsonBytes<T>(bytes: Uint8Array): Promise<T> {
|
|
122
|
-
let json: string;
|
|
123
130
|
try {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
//
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
131
|
+
// Each UTF-8 byte decodes to at most one character, so this also
|
|
132
|
+
// guarantees that the text fits in a string. Everything from the heap
|
|
133
|
+
// check on is synchronous, so no other code can use memory in between.
|
|
134
|
+
if (
|
|
135
|
+
bytes.length > bufferConstants.MAX_STRING_LENGTH ||
|
|
136
|
+
!hasHeapHeadroomFor(bytes.length * READ_HEAP_BYTES_PER_JSON_BYTE)
|
|
137
|
+
) {
|
|
138
|
+
return await parseJsonStream<T>(Readable.from([bytes]));
|
|
139
|
+
}
|
|
132
140
|
|
|
133
|
-
|
|
141
|
+
// Like the stream parser, this skips a leading BOM and rejects invalid
|
|
142
|
+
// UTF-8, so both paths behave the same.
|
|
143
|
+
return JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
|
|
144
|
+
} catch (e) {
|
|
145
|
+
ensureError(e);
|
|
146
|
+
throw new InvalidParameterError(
|
|
147
|
+
"The bytes are not valid UTF-8 encoded JSON",
|
|
148
|
+
e,
|
|
149
|
+
);
|
|
150
|
+
}
|
|
134
151
|
}
|
|
135
152
|
|
|
136
153
|
export { bytesToBigInt, bytesToNumber, numberToBytes } from "./number.js";
|
package/src/fs.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type * as JsonStreamStringify from "json-stream-stringify";
|
|
2
1
|
import type { FileHandle } from "node:fs/promises";
|
|
3
2
|
|
|
3
|
+
import { constants as bufferConstants } from "node:buffer";
|
|
4
4
|
import fsPromises from "node:fs/promises";
|
|
5
5
|
import { tmpdir } from "node:os";
|
|
6
6
|
import path from "node:path";
|
|
7
|
-
import { pipeline } from "node:stream/promises";
|
|
8
7
|
|
|
8
|
+
import { parseJsonBytes } from "./bytes.js";
|
|
9
9
|
import { ensureError, ensureNodeErrnoExceptionError } from "./error.js";
|
|
10
10
|
import {
|
|
11
11
|
FileNotFoundError,
|
|
@@ -21,11 +21,20 @@ import { parseJsonStream } from "./internal/bytes.js";
|
|
|
21
21
|
import {
|
|
22
22
|
collectAllDirectoriesMatching,
|
|
23
23
|
collectAllFilesMatching,
|
|
24
|
+
streamJsonToFile,
|
|
24
25
|
} from "./internal/fs.js";
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
import {
|
|
27
|
+
hasHeapHeadroomFor,
|
|
28
|
+
READ_HEAP_BYTES_PER_JSON_BYTE,
|
|
29
|
+
WRITE_HEAP_BYTES_PER_JSON_BYTE,
|
|
30
|
+
} from "./internal/json.js";
|
|
31
|
+
import { AsyncMutex } from "./synchronization.js";
|
|
32
|
+
|
|
33
|
+
// Lets only one call of writeLargeJsonFile use JSON.stringify at a time, so
|
|
34
|
+
// that their strings don't use up each other's heap headroom.
|
|
35
|
+
// NOTE: Created on first use: synchronization.ts imports this file, so
|
|
36
|
+
// AsyncMutex may not be initialized yet while this file is first loading.
|
|
37
|
+
let bufferedWriteMutex: AsyncMutex | undefined;
|
|
29
38
|
|
|
30
39
|
const AMBIGUOUS_CASING_DIR_ENTRY = Symbol("ambiguous");
|
|
31
40
|
|
|
@@ -343,25 +352,6 @@ export async function getAllDirectoriesMatching(
|
|
|
343
352
|
return results;
|
|
344
353
|
}
|
|
345
354
|
|
|
346
|
-
/**
|
|
347
|
-
* Determines the true case path of a given relative path from a specified
|
|
348
|
-
* directory, without resolving symbolic links, and returns it.
|
|
349
|
-
*
|
|
350
|
-
* @param from The absolute path of the directory to start the search from.
|
|
351
|
-
* @param relativePath The relative path to get the true case of.
|
|
352
|
-
* @returns The true case of the relative path.
|
|
353
|
-
* @throws FileNotFoundError if the starting directory or the relative path doesn't exist.
|
|
354
|
-
* @throws NotADirectoryError if the starting directory is not a directory.
|
|
355
|
-
* @throws FileSystemAccessError for any other error.
|
|
356
|
-
* @deprecated Use {@link TrueCasePathResolver} instead.
|
|
357
|
-
*/
|
|
358
|
-
export async function getFileTrueCase(
|
|
359
|
-
from: string,
|
|
360
|
-
relativePath: string,
|
|
361
|
-
): Promise<string> {
|
|
362
|
-
return await new TrueCasePathResolver().getFileTrueCase(from, relativePath);
|
|
363
|
-
}
|
|
364
|
-
|
|
365
355
|
/**
|
|
366
356
|
* Checks if a given path is a directory.
|
|
367
357
|
*
|
|
@@ -386,26 +376,10 @@ export async function isDirectory(absolutePath: string): Promise<boolean> {
|
|
|
386
376
|
/**
|
|
387
377
|
* Reads a JSON file and parses it. The encoding used is "utf8".
|
|
388
378
|
*
|
|
389
|
-
*
|
|
390
|
-
*
|
|
391
|
-
*
|
|
392
|
-
*
|
|
393
|
-
* @throws IsDirectoryError if the path is a directory instead of a file.
|
|
394
|
-
* @throws FileSystemAccessError for any other error.
|
|
395
|
-
*/
|
|
396
|
-
export async function readJsonFile<T>(absolutePathToFile: string): Promise<T> {
|
|
397
|
-
const content = await readUtf8File(absolutePathToFile);
|
|
398
|
-
try {
|
|
399
|
-
return JSON.parse(content.toString());
|
|
400
|
-
} catch (e) {
|
|
401
|
-
ensureError(e);
|
|
402
|
-
throw new InvalidFileFormatError(absolutePathToFile, e);
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
/**
|
|
407
|
-
* Reads a JSON file as a stream and parses it. The encoding used is "utf8".
|
|
408
|
-
* This function should be used when parsing very large JSON files.
|
|
379
|
+
* Files of any size are supported. The file is parsed with `JSON.parse`,
|
|
380
|
+
* which is much faster, when its text fits in a single string and the heap has
|
|
381
|
+
* room for it. Otherwise, it's parsed as a stream, which never holds the whole
|
|
382
|
+
* text as one string. Both give the same result.
|
|
409
383
|
*
|
|
410
384
|
* @param absolutePathToFile The path to the file.
|
|
411
385
|
* @returns The parsed JSON object.
|
|
@@ -414,20 +388,37 @@ export async function readJsonFile<T>(absolutePathToFile: string): Promise<T> {
|
|
|
414
388
|
* @throws IsDirectoryError if the path is a directory instead of a file.
|
|
415
389
|
* @throws FileSystemAccessError for any other error.
|
|
416
390
|
*/
|
|
417
|
-
export async function
|
|
418
|
-
absolutePathToFile: string,
|
|
419
|
-
): Promise<T> {
|
|
391
|
+
export async function readJsonFile<T>(absolutePathToFile: string): Promise<T> {
|
|
420
392
|
let fileHandle: FileHandle | undefined;
|
|
421
393
|
|
|
422
394
|
try {
|
|
423
395
|
fileHandle = await fsPromises.open(absolutePathToFile, "r");
|
|
424
396
|
|
|
425
|
-
|
|
397
|
+
// Each UTF-8 byte decodes to at most one character, so the file's size
|
|
398
|
+
// tells us whether its text could fit in a string. If it can't, or the
|
|
399
|
+
// heap has no room for it right now, we stream it from disk instead of
|
|
400
|
+
// reading it all into memory.
|
|
401
|
+
const { size } = await fileHandle.stat();
|
|
402
|
+
if (
|
|
403
|
+
size > bufferConstants.MAX_STRING_LENGTH ||
|
|
404
|
+
!hasHeapHeadroomFor(size * READ_HEAP_BYTES_PER_JSON_BYTE)
|
|
405
|
+
) {
|
|
406
|
+
return await parseJsonStream<T>(fileHandle.createReadStream());
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// The bytes are stored outside of the heap. parseJsonBytes checks the heap
|
|
410
|
+
// again, as other code may have used it while we were reading.
|
|
411
|
+
return await parseJsonBytes<T>(await fileHandle.readFile());
|
|
426
412
|
} catch (e) {
|
|
427
413
|
ensureError(e);
|
|
428
414
|
|
|
429
415
|
// If the code is defined, we assume the error to be related to the file system
|
|
430
416
|
if ("code" in e) {
|
|
417
|
+
// Except for invalid UTF-8, which is a problem with the file's contents
|
|
418
|
+
if (e.code === "ERR_ENCODING_INVALID_ENCODED_DATA") {
|
|
419
|
+
throw new InvalidFileFormatError(absolutePathToFile, e);
|
|
420
|
+
}
|
|
421
|
+
|
|
431
422
|
if (e.code === "ENOENT") {
|
|
432
423
|
throw new FileNotFoundError(absolutePathToFile, e);
|
|
433
424
|
}
|
|
@@ -438,7 +429,7 @@ export async function readJsonFileAsStream<T>(
|
|
|
438
429
|
|
|
439
430
|
// If the code is defined, we assume the error to be related to the file system
|
|
440
431
|
if (e.code !== undefined) {
|
|
441
|
-
throw new FileSystemAccessError(
|
|
432
|
+
throw new FileSystemAccessError(e.message, e);
|
|
442
433
|
}
|
|
443
434
|
}
|
|
444
435
|
|
|
@@ -475,59 +466,77 @@ export async function writeJsonFile<T>(
|
|
|
475
466
|
}
|
|
476
467
|
|
|
477
468
|
/**
|
|
478
|
-
* Writes an object to a JSON file
|
|
479
|
-
*
|
|
480
|
-
*
|
|
469
|
+
* Writes an object to a JSON file, supporting objects whose JSON is too large
|
|
470
|
+
* to be held in a single string. The JSON is compact (not indented), the
|
|
471
|
+
* encoding used is "utf8", and the file is overwritten. If part of the path
|
|
472
|
+
* doesn't exist, it will be created.
|
|
473
|
+
*
|
|
474
|
+
* Use this function for objects whose JSON can be very large, like solc's
|
|
475
|
+
* output, and {@link writeJsonFile} for everything else.
|
|
476
|
+
*
|
|
477
|
+
* The object is serialized with `JSON.stringify`, which is much faster, when
|
|
478
|
+
* the heap has room for the longest string it could build. Otherwise, or if
|
|
479
|
+
* its JSON doesn't fit in a single string, it's serialized as a stream, which
|
|
480
|
+
* never holds the whole JSON as one string. Only one call uses
|
|
481
|
+
* `JSON.stringify` at a time: concurrent calls take turns.
|
|
482
|
+
*
|
|
483
|
+
* Both write the same JSON, except for objects that contain a BigInt, a
|
|
484
|
+
* Promise, or a `toJSON` method that throws. `JSON.stringify` throws a
|
|
485
|
+
* `JsonSerializationError` for a BigInt or a throwing `toJSON`, and writes a
|
|
486
|
+
* Promise as `{}`. The stream writes a BigInt as a plain number, writes a
|
|
487
|
+
* Promise's resolved value, and crashes the process if a `toJSON` throws.
|
|
481
488
|
*
|
|
482
489
|
* @param absolutePathToFile The path to the file. If the file exists, it will be overwritten.
|
|
483
490
|
* @param object The object to write.
|
|
484
491
|
* @throws JsonSerializationError if the object can't be serialized to JSON.
|
|
485
492
|
* @throws FileSystemAccessError for any other error.
|
|
486
493
|
*/
|
|
487
|
-
export async function
|
|
494
|
+
export async function writeLargeJsonFile<T>(
|
|
488
495
|
absolutePathToFile: string,
|
|
489
496
|
object: T,
|
|
490
497
|
): Promise<void> {
|
|
491
|
-
|
|
492
|
-
const dirExists = await exists(dirPath);
|
|
493
|
-
if (!dirExists) {
|
|
494
|
-
await mkdir(dirPath);
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
let fileHandle: FileHandle | undefined;
|
|
498
|
-
|
|
499
|
-
try {
|
|
500
|
-
fileHandle = await fsPromises.open(absolutePathToFile, "w");
|
|
498
|
+
bufferedWriteMutex ??= new AsyncMutex();
|
|
501
499
|
|
|
502
|
-
|
|
503
|
-
|
|
500
|
+
const written = await bufferedWriteMutex.exclusiveRun(async () => {
|
|
501
|
+
// We can't know the JSON's length before building it, so we require room
|
|
502
|
+
// for the longest string V8 can build. JSON.stringify throws a RangeError
|
|
503
|
+
// instead of going past it, so with this much heap free, it can't run out.
|
|
504
|
+
if (
|
|
505
|
+
!hasHeapHeadroomFor(
|
|
506
|
+
bufferConstants.MAX_STRING_LENGTH * WRITE_HEAP_BYTES_PER_JSON_BYTE,
|
|
507
|
+
)
|
|
508
|
+
) {
|
|
509
|
+
return false;
|
|
504
510
|
}
|
|
505
511
|
|
|
506
|
-
|
|
507
|
-
|
|
512
|
+
let json: string;
|
|
513
|
+
try {
|
|
514
|
+
json = JSON.stringify(object);
|
|
515
|
+
} catch (e) {
|
|
516
|
+
ensureError(e);
|
|
508
517
|
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
try {
|
|
515
|
-
await remove(dirPath);
|
|
516
|
-
// we don't want to override the original error
|
|
517
|
-
} catch (_error) {}
|
|
518
|
-
}
|
|
518
|
+
// The JSON is too long for a string, or the object is nested too deeply
|
|
519
|
+
// for JSON.stringify. Streaming handles both.
|
|
520
|
+
if (e instanceof RangeError) {
|
|
521
|
+
return false;
|
|
522
|
+
}
|
|
519
523
|
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
524
|
+
// Anything else isn't about size, so streaming wouldn't help:
|
|
525
|
+
// - A circular object (TypeError): streaming detects it too.
|
|
526
|
+
// - A BigInt (TypeError): streaming writes it as a plain number, which
|
|
527
|
+
// is read back as a rounded number if it's too large. Failing is safer.
|
|
528
|
+
// - An error thrown by a toJSON method: streaming can't report it, and
|
|
529
|
+
// crashes the process instead.
|
|
530
|
+
throw new JsonSerializationError(absolutePathToFile, e);
|
|
523
531
|
}
|
|
524
532
|
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
533
|
+
await writeUtf8File(absolutePathToFile, json);
|
|
534
|
+
return true;
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
if (!written) {
|
|
538
|
+
// Streaming uses little memory, so it doesn't need to take turns.
|
|
539
|
+
await streamJsonToFile(absolutePathToFile, object);
|
|
531
540
|
}
|
|
532
541
|
}
|
|
533
542
|
|