@esposter/shared-node 4.4.0 → 4.6.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/README.md +17 -10
- package/dist/bench.d.ts +3 -0
- package/dist/bench.js +9 -0
- package/dist/cache.d.ts +4 -0
- package/dist/cache.js +35 -0
- package/dist/child-process.d.ts +7 -0
- package/dist/child-process.js +16 -0
- package/dist/execSync-DCcbOCo8.js +15 -0
- package/dist/reporter.d.ts +4 -0
- package/dist/reporter.js +127 -0
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
[![NPM downloads][badge-npm-downloads]][url-npm]
|
|
6
6
|
[![NPM Unpacked Size (with version)][badge-npm-unpacked-size]][url-npm]
|
|
7
7
|
|
|
8
|
-
Node-only
|
|
8
|
+
Provides the Node-only tools that Esposter packages share: a machine-wide download cache, hidden-window child processes and benchmark reporting.
|
|
9
9
|
|
|
10
10
|
Design notes live in [the specs](https://github.com/Esposter/Esposter/tree/main/openspec/specs).
|
|
11
11
|
|
|
@@ -19,27 +19,34 @@ Design notes live in [the specs](https://github.com/Esposter/Esposter/tree/main/
|
|
|
19
19
|
|
|
20
20
|
## <a name="getting-started">🚀 Getting Started</a>
|
|
21
21
|
|
|
22
|
+
Import this package only in a server or build environment, never in browser code.
|
|
23
|
+
|
|
24
|
+
1. Install the package.
|
|
25
|
+
|
|
22
26
|
```bash
|
|
23
27
|
pnpm i @esposter/shared-node
|
|
24
28
|
```
|
|
25
29
|
|
|
30
|
+
Check: `package.json` lists `@esposter/shared-node` in its dependencies.
|
|
31
|
+
|
|
32
|
+
2. Run this code.
|
|
33
|
+
|
|
26
34
|
```ts
|
|
27
35
|
import { spawnSync } from "@esposter/shared-node/child-process";
|
|
28
36
|
|
|
29
37
|
spawnSync("pnpm", ["install"], { stdio: "inherit" });
|
|
30
38
|
```
|
|
31
39
|
|
|
32
|
-
- Server/build environment only; never import in browser code.
|
|
33
|
-
|
|
34
40
|
## <a name="key-exports">🧩 Key Exports</a>
|
|
35
41
|
|
|
36
|
-
| Export | Description
|
|
37
|
-
| ------------------------------------------------------------------------------- |
|
|
38
|
-
| `BENCHMARK_RUN_OPTIONS` (`/bench`) | Run options every `bench.compare` passes
|
|
39
|
-
| `BenchmarkMarkdownReporter` (default export, `/reporter`) | Vitest reporter
|
|
40
|
-
| `spawn`, `spawnSync`, `execFile`, `execFileSync`, `execSync` (`/child-process`) | Node's own child-process functions
|
|
41
|
-
| `execFileAsync` (`/child-process`) | The promisified `execFile`, with the same window flag
|
|
42
|
-
| `CACHE_ROOT`, `readContentAddressedObject` (`/cache`) | The download cache folder every checkout shares
|
|
42
|
+
| Export | Description |
|
|
43
|
+
| ------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
44
|
+
| `BENCHMARK_RUN_OPTIONS` (`/bench`) | Run options that every `bench.compare` call passes. They fix the iteration count. |
|
|
45
|
+
| `BenchmarkMarkdownReporter` (default export, `/reporter`) | Vitest reporter. Writes `Foo.bench.md` and `Foo.bench.json` beside `Foo.bench.ts`. |
|
|
46
|
+
| `spawn`, `spawnSync`, `execFile`, `execFileSync`, `execSync` (`/child-process`) | Node's own child-process functions. Each forces `windowsHide: true`, so a console child never opens a window of its own. |
|
|
47
|
+
| `execFileAsync` (`/child-process`) | The promisified `execFile`, with the same window flag. Resolves `{ stdout, stderr }`. |
|
|
48
|
+
| `CACHE_ROOT`, `readContentAddressedObject` (`/cache`) | The download cache folder that every checkout shares. A read-through cache for objects named by the hex SHA-256 of their bytes. |
|
|
49
|
+
| `writePartialThenMove` (`/cache`) | Writes a file whole. A partial file is written beside the path and renamed onto it once written, and it is removed when either step fails. |
|
|
43
50
|
|
|
44
51
|
## <a name="license">⚖️ License</a>
|
|
45
52
|
|
package/dist/bench.d.ts
ADDED
package/dist/bench.js
ADDED
package/dist/cache.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare const CACHE_ROOT: string;
|
|
2
|
+
export declare const CONTENT_CACHE_DIRECTORY: string;
|
|
3
|
+
export declare const readContentAddressedObject: (hash: string, download: () => Promise<Uint8Array>, directory?: string) => Promise<Buffer>;
|
|
4
|
+
export declare const writePartialThenMove: (path: string, write: (partialPath: string) => Promise<void>) => Promise<void>;
|
package/dist/cache.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { InvalidOperationError, Operation, SITE_NAME, getResultAsync, noop } from "@esposter/shared";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { existsSync, rmSync } from "node:fs";
|
|
5
|
+
import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
|
|
6
|
+
import { createHash } from "node:crypto";
|
|
7
|
+
// src/services/cache/constants.ts
|
|
8
|
+
const CACHE_ROOT = join(homedir(), SITE_NAME, "cache");
|
|
9
|
+
const CONTENT_CACHE_DIRECTORY = join(CACHE_ROOT, "content");
|
|
10
|
+
// src/services/cache/writePartialThenMove.ts
|
|
11
|
+
const writePartialThenMove = async (path, write) => {
|
|
12
|
+
await mkdir(dirname(path), { recursive: true });
|
|
13
|
+
const partialPath = `${path}.${crypto.randomUUID()}.part`;
|
|
14
|
+
(await getResultAsync(async () => {
|
|
15
|
+
await write(partialPath);
|
|
16
|
+
await rename(partialPath, path);
|
|
17
|
+
})).match(noop, (error) => {
|
|
18
|
+
rmSync(partialPath, { force: true });
|
|
19
|
+
throw error;
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
// src/services/cache/readContentAddressedObject.ts
|
|
23
|
+
const SHA_256_HEX_REGEX = /^[0-9a-f]{64}$/u;
|
|
24
|
+
const readContentAddressedObject = async (hash, download, directory = CONTENT_CACHE_DIRECTORY) => {
|
|
25
|
+
if (!SHA_256_HEX_REGEX.test(hash)) throw new InvalidOperationError(Operation.Read, hash, "is not a SHA-256 hex digest");
|
|
26
|
+
const path = join(directory, hash);
|
|
27
|
+
if (existsSync(path)) return readFile(path);
|
|
28
|
+
const bytes = Buffer.from(await download());
|
|
29
|
+
if (createHash("sha256").update(bytes).digest("hex") !== hash) throw new InvalidOperationError(Operation.Read, hash, "does not hash to its name");
|
|
30
|
+
await getResultAsync(() => writePartialThenMove(path, (partialPath) => writeFile(partialPath, bytes))).match(noop, (error) => {
|
|
31
|
+
if (!existsSync(path)) throw error;
|
|
32
|
+
});
|
|
33
|
+
return bytes;
|
|
34
|
+
};
|
|
35
|
+
export { CACHE_ROOT, CONTENT_CACHE_DIRECTORY, readContentAddressedObject, writePartialThenMove };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { execFile as execFile$1, execFileSync as execFileSync$1, execSync as execSync$1, spawn as spawn$1, spawnSync as spawnSync$1 } from "node:child_process";
|
|
2
|
+
export declare const execFile: typeof execFile$1;
|
|
3
|
+
export declare const execFileAsync: typeof execFile$1.__promisify__;
|
|
4
|
+
export declare const execFileSync: typeof execFileSync$1;
|
|
5
|
+
export declare const execSync: typeof execSync$1;
|
|
6
|
+
export declare const spawn: typeof spawn$1;
|
|
7
|
+
export declare const spawnSync: typeof spawnSync$1;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { n as withHiddenWindow, t as execSync } from "./execSync-DCcbOCo8.js";
|
|
2
|
+
import { execFile as execFile$1, execFileSync as execFileSync$1, spawn as spawn$1, spawnSync as spawnSync$1 } from "node:child_process";
|
|
3
|
+
import { promisify } from "node:util";
|
|
4
|
+
// src/services/childProcess/execFile.ts
|
|
5
|
+
const execFile = ((file, ...parameters) => execFile$1(file, ...withHiddenWindow(parameters)));
|
|
6
|
+
Object.defineProperty(execFile, promisify.custom, { value: (file, ...parameters) => {
|
|
7
|
+
return promisify(execFile$1)(file, ...withHiddenWindow(parameters));
|
|
8
|
+
} });
|
|
9
|
+
const execFileAsync = promisify(execFile);
|
|
10
|
+
// src/services/childProcess/execFileSync.ts
|
|
11
|
+
const execFileSync = ((file, ...parameters) => execFileSync$1(file, ...withHiddenWindow(parameters)));
|
|
12
|
+
// src/services/childProcess/spawn.ts
|
|
13
|
+
const spawn = ((file, ...parameters) => spawn$1(file, ...withHiddenWindow(parameters)));
|
|
14
|
+
// src/services/childProcess/spawnSync.ts
|
|
15
|
+
const spawnSync = ((file, ...parameters) => spawnSync$1(file, ...withHiddenWindow(parameters)));
|
|
16
|
+
export { execFile, execFileAsync, execFileSync, execSync, spawn, spawnSync };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { execSync } from "node:child_process";
|
|
2
|
+
// src/services/childProcess/withHiddenWindow.ts
|
|
3
|
+
const withHiddenWindow = (parameters) => {
|
|
4
|
+
const optionsIndex = Array.isArray(parameters[0]) || parameters[0] === void 0 && parameters.length > 1 ? 1 : 0;
|
|
5
|
+
const options = parameters[optionsIndex];
|
|
6
|
+
if (typeof options === "object" && options !== null) return parameters.toSpliced(optionsIndex, 1, {
|
|
7
|
+
...options,
|
|
8
|
+
windowsHide: true
|
|
9
|
+
});
|
|
10
|
+
const isPlaceholder = options === void 0 && optionsIndex < parameters.length;
|
|
11
|
+
return parameters.toSpliced(optionsIndex, Number(isPlaceholder), { windowsHide: true });
|
|
12
|
+
};
|
|
13
|
+
// src/services/childProcess/execSync.ts
|
|
14
|
+
const execSync$1 = ((command, ...parameters) => execSync(command, ...withHiddenWindow(parameters)));
|
|
15
|
+
export { withHiddenWindow as n, execSync$1 as t };
|
package/dist/reporter.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { t as execSync } from "./execSync-DCcbOCo8.js";
|
|
2
|
+
import "./bench.js";
|
|
3
|
+
import { GIBIBYTE, InvalidOperationError, Operation, getResult, getResultAsync, noop, normalizeString, takeOne } from "@esposter/shared";
|
|
4
|
+
import { arch, cpus, platform, release, totalmem } from "node:os";
|
|
5
|
+
import { writeFile } from "node:fs/promises";
|
|
6
|
+
// src/services/readBenchmarkEnvironment.ts
|
|
7
|
+
const readCommit = () => getResult(() => execSync("git rev-parse --short HEAD", {
|
|
8
|
+
encoding: "utf8",
|
|
9
|
+
stdio: "pipe"
|
|
10
|
+
})).match((stdout) => normalizeString(stdout), () => "unknown");
|
|
11
|
+
const readBenchmarkEnvironment = () => {
|
|
12
|
+
const cpu = cpus();
|
|
13
|
+
return [
|
|
14
|
+
`- Commit: ${readCommit()}`,
|
|
15
|
+
`- Node: ${process.version}`,
|
|
16
|
+
`- OS: ${platform()} ${release()} (${arch()})`,
|
|
17
|
+
`- CPU: ${normalizeString(cpu[0]?.model) || "unknown"} × ${cpu.length}`,
|
|
18
|
+
`- RAM: ${(totalmem() / GIBIBYTE).toFixed(1)} GiB`
|
|
19
|
+
].join("\n");
|
|
20
|
+
};
|
|
21
|
+
// src/services/buildBenchmarkFileReport.ts
|
|
22
|
+
const getGroupName = (testCase, benchmarkName, benchmarkCount) => benchmarkCount > 1 ? `${testCase.fullName} > ${benchmarkName}` : testCase.fullName;
|
|
23
|
+
const buildBenchmarkFileReport = (filepath, testCases) => {
|
|
24
|
+
const groups = [];
|
|
25
|
+
for (const testCase of testCases) {
|
|
26
|
+
const testCaseBenchmarks = testCase.benchmarks();
|
|
27
|
+
for (const { name, tasks } of testCaseBenchmarks) {
|
|
28
|
+
const benchmarks = [];
|
|
29
|
+
for (const task of tasks.toSorted((firstTask, secondTask) => firstTask.rank - secondTask.rank)) {
|
|
30
|
+
if (!Number.isFinite(task.latency.mean)) throw new InvalidOperationError(Operation.Read, testCase.fullName, `benchmark "${task.name}" produced no samples — it likely threw on every iteration`);
|
|
31
|
+
benchmarks.push({
|
|
32
|
+
mean: task.latency.mean,
|
|
33
|
+
name: task.name,
|
|
34
|
+
p99: task.latency.p99,
|
|
35
|
+
rme: task.latency.rme,
|
|
36
|
+
sampleCount: task.latency.samplesCount
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
if (benchmarks.length > 0) groups.push({
|
|
40
|
+
benchmarks,
|
|
41
|
+
fullName: getGroupName(testCase, name, testCaseBenchmarks.length)
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return { files: [{
|
|
46
|
+
filepath,
|
|
47
|
+
groups
|
|
48
|
+
}] };
|
|
49
|
+
};
|
|
50
|
+
// src/services/formatBenchmarkMarkdown.ts
|
|
51
|
+
const getBaseline = (benchmarks) => benchmarks.find(({ name }) => name === "native") ?? benchmarks[0];
|
|
52
|
+
const formatRelative = (baselineMean, mean) => {
|
|
53
|
+
const ratio = baselineMean / mean;
|
|
54
|
+
return `${ratio >= 1 ? ratio.toFixed(2) : Number(ratio.toPrecision(2))}×`;
|
|
55
|
+
};
|
|
56
|
+
const HEADER = [
|
|
57
|
+
"task",
|
|
58
|
+
"vs base",
|
|
59
|
+
"mean (ms)",
|
|
60
|
+
"±rme",
|
|
61
|
+
"p99 (ms)",
|
|
62
|
+
"samples"
|
|
63
|
+
];
|
|
64
|
+
const formatTable = (rows) => {
|
|
65
|
+
const widths = rows.reduce((accumulator, row) => accumulator.map((width, index) => Math.max(width, takeOne(row, index).length)), HEADER.map((header) => header.length));
|
|
66
|
+
const formatRow = (cells) => `| ${cells.map((cell, index) => cell.padEnd(takeOne(widths, index))).join(" | ")} |`;
|
|
67
|
+
return [
|
|
68
|
+
formatRow(HEADER),
|
|
69
|
+
`| ${widths.map((width) => "-".repeat(width)).join(" | ")} |`,
|
|
70
|
+
...rows.map((row) => formatRow(row))
|
|
71
|
+
];
|
|
72
|
+
};
|
|
73
|
+
const formatBenchmarkMarkdown = (report, environment) => {
|
|
74
|
+
return [
|
|
75
|
+
"# Benchmark results",
|
|
76
|
+
"",
|
|
77
|
+
"Auto-generated by the BenchmarkMarkdownReporter on `pnpm bench`, colocated beside the bench file (a",
|
|
78
|
+
"`.bench.json` sits next to it). Diff over time to catch regressions. Tasks are listed fastest first.",
|
|
79
|
+
"`vs base` is a throughput multiplier against each group's baseline (the `native` task, else the",
|
|
80
|
+
"fastest): `1.00×` is the baseline, higher is faster, lower is slower — a fast task drifting back",
|
|
81
|
+
"toward `1.00×` is a regression. `±rme` is the relative margin of error.",
|
|
82
|
+
"Numbers are machine- and commit-dependent (see Environment); only compare runs from the same host.",
|
|
83
|
+
"",
|
|
84
|
+
"## Environment",
|
|
85
|
+
"",
|
|
86
|
+
environment,
|
|
87
|
+
"",
|
|
88
|
+
...report.files.flatMap((file) => file.groups.map((group) => {
|
|
89
|
+
const baselineMean = getBaseline(group.benchmarks)?.mean ?? NaN;
|
|
90
|
+
const rows = group.benchmarks.map(({ mean, name, p99, rme, sampleCount }) => [
|
|
91
|
+
name.replaceAll("|", String.raw`\|`),
|
|
92
|
+
formatRelative(baselineMean, mean),
|
|
93
|
+
mean.toFixed(4),
|
|
94
|
+
`±${rme.toFixed(2)}%`,
|
|
95
|
+
p99.toFixed(4),
|
|
96
|
+
`${sampleCount}`
|
|
97
|
+
]);
|
|
98
|
+
return [
|
|
99
|
+
`## ${group.fullName}`,
|
|
100
|
+
"",
|
|
101
|
+
...formatTable(rows),
|
|
102
|
+
""
|
|
103
|
+
].join("\n");
|
|
104
|
+
}))
|
|
105
|
+
].join("\n");
|
|
106
|
+
};
|
|
107
|
+
// src/services/writeBenchmarkReport.ts
|
|
108
|
+
const TS_EXTENSION_REGEX = /\.ts$/u;
|
|
109
|
+
const writeFileOrThrow = (path, contents) => getResultAsync(() => writeFile(path, contents)).match(noop, (error) => {
|
|
110
|
+
throw new InvalidOperationError(Operation.Create, path, error.message);
|
|
111
|
+
});
|
|
112
|
+
const writeBenchmarkReport = async (moduleId, relativeModuleId, testCases, environment) => {
|
|
113
|
+
const report = buildBenchmarkFileReport(relativeModuleId, testCases);
|
|
114
|
+
const [benchmarkFile] = report.files;
|
|
115
|
+
if (benchmarkFile === void 0 || benchmarkFile.groups.length === 0) return;
|
|
116
|
+
const basePath = moduleId.replace(TS_EXTENSION_REGEX, "");
|
|
117
|
+
const reportPath = basePath.endsWith(".platform.bench") ? `${basePath}.${process.platform}` : basePath;
|
|
118
|
+
await Promise.all([writeFileOrThrow(`${reportPath}.json`, `${JSON.stringify(report, null, 2)}\n`), writeFileOrThrow(`${reportPath}.md`, formatBenchmarkMarkdown(report, environment))]);
|
|
119
|
+
};
|
|
120
|
+
// src/services/BenchmarkMarkdownReporter.ts
|
|
121
|
+
var BenchmarkMarkdownReporter = class {
|
|
122
|
+
async onTestRunEnd(testModules) {
|
|
123
|
+
const environment = readBenchmarkEnvironment();
|
|
124
|
+
await Promise.all(testModules.map((testModule) => writeBenchmarkReport(testModule.moduleId, testModule.relativeModuleId, testModule.children.allTests(), environment)));
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
export { BenchmarkMarkdownReporter as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@esposter/shared-node",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.6.0",
|
|
4
4
|
"description": "Node-only shared tooling: the machine-wide download cache, hidden-window child processes, and benchmark reporting reused across packages.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"benchmark",
|
|
@@ -50,20 +50,20 @@
|
|
|
50
50
|
"typecheck": "tsrs --fast"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@esposter/shared": "4.
|
|
53
|
+
"@esposter/shared": "4.6.0"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
|
-
"@esposter/configuration": "4.
|
|
56
|
+
"@esposter/configuration": "4.6.0",
|
|
57
57
|
"@types/node": "^26.6.6",
|
|
58
|
-
"tsdown": "0.23.
|
|
58
|
+
"tsdown": "^0.23.7"
|
|
59
59
|
},
|
|
60
60
|
"peerDependencies": {
|
|
61
|
-
"vitest": "^5.0
|
|
61
|
+
"vitest": "^5.1.0"
|
|
62
62
|
},
|
|
63
63
|
"peerDependenciesMeta": {
|
|
64
64
|
"vitest": {
|
|
65
65
|
"optional": true
|
|
66
66
|
}
|
|
67
67
|
},
|
|
68
|
-
"gitHead": "
|
|
68
|
+
"gitHead": "0ceea3ffbe12ca69413ea5db2d3cfad197a63cba"
|
|
69
69
|
}
|