goatly 0.1.0 → 0.1.2
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 +874 -1
- package/bin/goatly.js +99 -0
- package/dist/cli/index.js +234 -0
- package/dist/compat/gatsby-node-entry.js +16 -0
- package/dist/compat/gatsby-package.json +6 -0
- package/dist/compat/md5-file.js +47 -0
- package/dist/compat-apis.json +72 -0
- package/dist/compat-apis.json.js +62 -0
- package/dist/graphql.js +3 -0
- package/dist/node/actions/index.js +619 -0
- package/dist/node/actions/job-store.js +180 -0
- package/dist/node/actions/jobs.js +174 -0
- package/dist/node/api-runner.js +258 -0
- package/dist/node/bootstrap.js +569 -0
- package/dist/node/build/assets.js +110 -0
- package/dist/node/build/incremental.js +208 -0
- package/dist/node/build/index.js +763 -0
- package/dist/node/build/memory-governor.js +450 -0
- package/dist/node/build/output-dirs.js +70 -0
- package/dist/node/build/page-data.js +40 -0
- package/dist/node/build/page-files.js +186 -0
- package/dist/node/build/partitioned-state.js +258 -0
- package/dist/node/build/query-worker.js +626 -0
- package/dist/node/build/query-workers.js +863 -0
- package/dist/node/build/render-thread.js +103 -0
- package/dist/node/build/render-worker.js +162 -0
- package/dist/node/build/state.js +24 -0
- package/dist/node/build/timing-breakdown.js +216 -0
- package/dist/node/build/work-clock.js +75 -0
- package/dist/node/build/worker-pool.js +242 -0
- package/dist/node/cache-layout.js +99 -0
- package/dist/node/cache.js +1430 -0
- package/dist/node/compat/chokidar-shim.js +148 -0
- package/dist/node/compat/core-utils-lmdb.js +145 -0
- package/dist/node/compat/create-node-id-shim.js +1 -0
- package/dist/node/compat/module-hooks.js +245 -0
- package/dist/node/compat/redux-shim.js +1 -0
- package/dist/node/config/index.js +162 -0
- package/dist/node/config/not-found-pages.js +95 -0
- package/dist/node/datastore/codec.js +144 -0
- package/dist/node/datastore/index.js +127 -0
- package/dist/node/datastore/logfile/cache.js +89 -0
- package/dist/node/datastore/logfile/index.js +1329 -0
- package/dist/node/datastore/logfile/record.js +71 -0
- package/dist/node/datastore/types.js +1 -0
- package/dist/node/develop/express-compat.js +207 -0
- package/dist/node/develop/graphiql.js +22 -0
- package/dist/node/develop/hydration-report.js +41 -0
- package/dist/node/develop/index.js +618 -0
- package/dist/node/develop/local-url.js +10 -0
- package/dist/node/develop/ssr-runner.js +66 -0
- package/dist/node/gql.js +5 -0
- package/dist/node/index.js +15 -0
- package/dist/node/info.js +270 -0
- package/dist/node/internal-plugins/data-bridge.js +196 -0
- package/dist/node/internal-plugins/page-creator.js +318 -0
- package/dist/node/loader/import-module.js +160 -0
- package/dist/node/node-helpers.js +66 -0
- package/dist/node/not-found-pages.js +220 -0
- package/dist/node/plugins/api-list.js +12 -0
- package/dist/node/plugins/load-plugins.js +427 -0
- package/dist/node/plugins/resolve-plugin.js +79 -0
- package/dist/node/plugins/static-exports.js +64 -0
- package/dist/node/query/compile.js +326 -0
- package/dist/node/query/dependencies.js +45 -0
- package/dist/node/query/execute.js +417 -0
- package/dist/node/query/extract.js +372 -0
- package/dist/node/query/gql-tada-introspection.js +124 -0
- package/dist/node/query/graphql-env.js +231 -0
- package/dist/node/query/graphql-runner.js +79 -0
- package/dist/node/redux/actions.js +1 -0
- package/dist/node/redux/index.js +133 -0
- package/dist/node/redux/node-changes.js +128 -0
- package/dist/node/redux/reducers.js +193 -0
- package/dist/node/redux/state.js +71 -0
- package/dist/node/renderer.js +124 -0
- package/dist/node/reporter-transports.js +259 -0
- package/dist/node/reporter.js +497 -0
- package/dist/node/schema/date.js +539 -0
- package/dist/node/schema/extensions.js +315 -0
- package/dist/node/schema/index.js +542 -0
- package/dist/node/schema/infer.js +388 -0
- package/dist/node/schema/inline-objects.js +128 -0
- package/dist/node/schema/naming.js +20 -0
- package/dist/node/schema/node-model.js +207 -0
- package/dist/node/schema/query-engine.js +1808 -0
- package/dist/node/schema/resolver-kinds.js +7 -0
- package/dist/node/schema/resolvers.js +273 -0
- package/dist/node/schema/type-builders.js +45 -0
- package/dist/node/schema/types.js +241 -0
- package/dist/node/serve.js +90 -0
- package/dist/node/site-page-nodes.js +277 -0
- package/dist/node/types.js +1 -0
- package/dist/node/utils/content-digest.js +107 -0
- package/dist/node/utils/cpus.js +104 -0
- package/dist/node/utils/create-node-id.js +51 -0
- package/dist/node/utils/env.js +25 -0
- package/dist/node/utils/fs-retry.js +74 -0
- package/dist/node/utils/memory-signals.js +441 -0
- package/dist/node/utils/memory.js +657 -0
- package/dist/node/utils/murmurhash.js +32 -0
- package/dist/node/utils/parser.js +33 -0
- package/dist/node/utils/partition-hash.js +19 -0
- package/dist/node/utils/paths.js +122 -0
- package/dist/node/utils/resolver.js +48 -0
- package/dist/node/utils/runtime.js +5 -0
- package/dist/node/utils/serializable.js +41 -0
- package/dist/node/utils/strings.js +41 -0
- package/dist/node/vite/config.js +289 -0
- package/dist/node/vite/paths.js +12 -0
- package/dist/node/vite/plugin-deps.js +232 -0
- package/dist/node/vite/plugin.js +283 -0
- package/dist/node/vite/react-compiler.js +362 -0
- package/dist/node/vite/source-files.js +83 -0
- package/dist/node/vite/static-image.js +136 -0
- package/dist/node/vite/styled-components.js +73 -0
- package/dist/node/vite/transforms.js +330 -0
- package/dist/node/wasm-data/host.js +406 -0
- package/dist/node/wasm-data/lower.js +406 -0
- package/dist/node/wasm-data/runner.js +79 -0
- package/dist/node/wasm-data/worker.js +95 -0
- package/dist/reporter.js +5 -0
- package/dist/runtime/api-runner.js +81 -0
- package/dist/runtime/app.js +256 -0
- package/dist/runtime/client-entry.js +4 -0
- package/dist/runtime/client.js +383 -0
- package/dist/runtime/default-html.js +31 -0
- package/dist/runtime/head-markup.js +100 -0
- package/dist/runtime/head-sync.js +101 -0
- package/dist/runtime/head-tags.js +45 -0
- package/dist/runtime/hydration-report.js +111 -0
- package/dist/runtime/index.js +12 -0
- package/dist/runtime/link.js +293 -0
- package/dist/runtime/loader.js +449 -0
- package/dist/runtime/not-found.js +61 -0
- package/dist/runtime/page-renderer.js +35 -0
- package/dist/runtime/preamble.js +47 -0
- package/dist/runtime/react-compat.js +14 -0
- package/dist/runtime/renderer-shared.js +10 -0
- package/dist/runtime/renderers/react-client.js +14 -0
- package/dist/runtime/renderers/react-server.js +99 -0
- package/dist/runtime/renderers/react.js +17 -0
- package/dist/runtime/renderers/types.js +1 -0
- package/dist/runtime/root-error-handlers.js +29 -0
- package/dist/runtime/router/history.js +132 -0
- package/dist/runtime/router/index.js +240 -0
- package/dist/runtime/router/utils.js +118 -0
- package/dist/runtime/script.js +110 -0
- package/dist/runtime/scroll.js +40 -0
- package/dist/runtime/slice.js +11 -0
- package/dist/runtime/ssr.js +240 -0
- package/dist/runtime/static-query.js +47 -0
- package/dist/utils.js +4 -0
- package/package.json +105 -3
- package/types/graphql-tada.d.ts +223 -0
- package/types/graphql.d.ts +2 -0
- package/types/index.d.ts +2287 -0
- package/types/node.d.ts +389 -0
- package/types/reporter.d.ts +187 -0
- package/types/router.d.ts +102 -0
- package/types/vendor/gql-tada.d.ts +2529 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { performance } from "node:perf_hooks";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
// src/node/build/page-files.ts
|
|
5
|
+
/**
|
|
6
|
+
* Writes an output file of a page (its HTML or page-data.json) synchronously, in the thread that
|
|
7
|
+
* rendered the page. A build writes two files per page, 200,000 or more for a large site, and the
|
|
8
|
+
* cost per file adds up:
|
|
9
|
+
*
|
|
10
|
+
* - Synchronous calls, not `fs.promises`: `writeFile` takes three trips through libuv's thread
|
|
11
|
+
* pool (open, write, close) and `mkdir` one more, and all worker threads of the process share that
|
|
12
|
+
* pool (4 threads by default). Writing 100,000 pages from 3 threads took 22–32 s instead of
|
|
13
|
+
* 30–39 s, with less kernel time.
|
|
14
|
+
* - `exists` (an earlier build wrote the page): the file is overwritten in place – written from the
|
|
15
|
+
* start and truncated only when the new content is shorter – instead of being truncated to zero
|
|
16
|
+
* first. Truncating frees the file's blocks and the write allocates new ones, and on ext4
|
|
17
|
+
* (`auto_da_alloc`) closing a file that was truncated to zero starts its writeback at once.
|
|
18
|
+
* Rewriting the files of 100,000 pages took 18 s instead of 36 s. A file that is missing after
|
|
19
|
+
* all is created.
|
|
20
|
+
* - New pages: written first – the build creates the directories of new pages before rendering
|
|
21
|
+
* them (output-dirs.ts) – and their directory is created when the write finds none.
|
|
22
|
+
*
|
|
23
|
+
* Neither way is atomic: a build that stops while writing leaves the files it was writing, as
|
|
24
|
+
* before. Its HTML state is still marked pending then, so the next build writes them again.
|
|
25
|
+
*/
|
|
26
|
+
function writePageFile(file, data, exists) {
|
|
27
|
+
if (exists && overwrite(file, data)) return;
|
|
28
|
+
try {
|
|
29
|
+
fs.writeFileSync(file, data);
|
|
30
|
+
} catch (e) {
|
|
31
|
+
if (e.code !== "ENOENT") throw e;
|
|
32
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
33
|
+
fs.writeFileSync(file, data);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/** overwrites `file` in place; false when it doesn't exist */
|
|
37
|
+
function overwrite(file, data) {
|
|
38
|
+
let fd;
|
|
39
|
+
try {
|
|
40
|
+
fd = fs.openSync(file, fs.constants.O_WRONLY);
|
|
41
|
+
} catch (e) {
|
|
42
|
+
if (e.code === "ENOENT") return false;
|
|
43
|
+
throw e;
|
|
44
|
+
}
|
|
45
|
+
try {
|
|
46
|
+
const bytes = Buffer.from(data);
|
|
47
|
+
let written = 0;
|
|
48
|
+
while (written < bytes.length) written += fs.writeSync(fd, bytes, written, bytes.length - written, written);
|
|
49
|
+
if (fs.fstatSync(fd).size > bytes.length) fs.ftruncateSync(fd, bytes.length);
|
|
50
|
+
} finally {
|
|
51
|
+
fs.closeSync(fd);
|
|
52
|
+
}
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
/** like writePageFile, through libuv's thread pool: other work goes on while the file is written */
|
|
56
|
+
async function writePageFileAsync(file, data, exists) {
|
|
57
|
+
if (exists && await overwriteAsync(file, data)) return;
|
|
58
|
+
try {
|
|
59
|
+
await fs.promises.writeFile(file, data);
|
|
60
|
+
} catch (e) {
|
|
61
|
+
if (e.code !== "ENOENT") throw e;
|
|
62
|
+
await fs.promises.mkdir(path.dirname(file), { recursive: true });
|
|
63
|
+
await fs.promises.writeFile(file, data);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
async function overwriteAsync(file, data) {
|
|
67
|
+
let handle;
|
|
68
|
+
try {
|
|
69
|
+
handle = await fs.promises.open(file, fs.constants.O_WRONLY);
|
|
70
|
+
} catch (e) {
|
|
71
|
+
if (e.code === "ENOENT") return false;
|
|
72
|
+
throw e;
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
const bytes = Buffer.from(data);
|
|
76
|
+
let written = 0;
|
|
77
|
+
while (written < bytes.length) written += (await handle.write(bytes, written, bytes.length - written, written)).bytesWritten;
|
|
78
|
+
if ((await handle.stat()).size > bytes.length) await handle.truncate(bytes.length);
|
|
79
|
+
} finally {
|
|
80
|
+
await handle.close();
|
|
81
|
+
}
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* A filesystem is slow to write a page's file to when a synchronous write takes this long (ms) on
|
|
86
|
+
* average – it takes 0.02–0.1 ms into the page cache of a local disk. What takes milliseconds is
|
|
87
|
+
* creating a file on a bind mount of Docker Desktop, on a network drive, or under an antivirus
|
|
88
|
+
* scanner, and a thread that waits for it does nothing else: 5 workers that write two files per
|
|
89
|
+
* page at 27 ms each build 90 pages a second, however fast they render.
|
|
90
|
+
*/
|
|
91
|
+
const SLOW_WRITE_MS = 1;
|
|
92
|
+
/** …measured over this many writes (fewer when the first ones already took that long in sum) */
|
|
93
|
+
const WRITES_MEASURED = 8;
|
|
94
|
+
/**
|
|
95
|
+
* While writes overlap, the disk is tried again after this many files, with synchronous writes:
|
|
96
|
+
* when 8 of them are fast (a quarter of what counts as slow, on average), the writes are
|
|
97
|
+
* synchronous again. A try ends with the first write that is too slow for that, and the next one
|
|
98
|
+
* comes after twice as many files (at most PROBE_EVERY_MAX): on a disk that stays slow, trying
|
|
99
|
+
* costs a write now and then.
|
|
100
|
+
*/
|
|
101
|
+
const PROBE_EVERY = 512;
|
|
102
|
+
const PROBE_EVERY_MAX = 65536;
|
|
103
|
+
/**
|
|
104
|
+
* The threads of libuv's pool (UV_THREADPOOL_SIZE: the CLI sets it, see ioThreads): how many file
|
|
105
|
+
* operations of the whole process run at a time
|
|
106
|
+
*/
|
|
107
|
+
function ioPoolSize(env = process.env) {
|
|
108
|
+
const size = Number(env.UV_THREADPOOL_SIZE);
|
|
109
|
+
return size >= 1 ? Math.floor(size) : 4;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* The size of libuv's thread pool for a build (UV_THREADPOOL_SIZE, read when the pool is first
|
|
113
|
+
* used: set it before): 16 threads per CPU, 32 to 128. They are what lets file writes overlap on a
|
|
114
|
+
* slow filesystem (see createPageWriter) – a thread that waits for the disk needs no CPU – and idle
|
|
115
|
+
* threads cost nothing. undefined: it is set already.
|
|
116
|
+
*/
|
|
117
|
+
function ioThreads(cpus, env = process.env) {
|
|
118
|
+
if (env.UV_THREADPOOL_SIZE) return void 0;
|
|
119
|
+
return Math.min(128, Math.max(32, 16 * cpus));
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Writes the output files of the pages a thread renders, the way that is fastest on the disk it
|
|
123
|
+
* finds:
|
|
124
|
+
*
|
|
125
|
+
* - synchronously (writePageFile) while that is fast – on a local disk a write is a copy into the
|
|
126
|
+
* page cache, and a trip through the thread pool per open, write and close costs more than it
|
|
127
|
+
* saves (see docs/decisions-writing-pages.md);
|
|
128
|
+
* - several at a time (writePageFileAsync) once the writes are slow: `write` then returns a
|
|
129
|
+
* promise, and the thread goes on rendering while the files are written. Whoever calls it limits
|
|
130
|
+
* the writes in flight (`window`: pages, by the size of the thread pool) and waits for them
|
|
131
|
+
* before it reports its pages as written.
|
|
132
|
+
*
|
|
133
|
+
* It measures the synchronous writes, and tries them again now and then while writes overlap.
|
|
134
|
+
*/
|
|
135
|
+
function createPageWriter(options = {}) {
|
|
136
|
+
const mode = options.mode ?? process.env.GOATLY_PAGE_WRITES;
|
|
137
|
+
const now = options.now ?? (() => performance.now());
|
|
138
|
+
const writeSync = options.writeSync ?? writePageFile;
|
|
139
|
+
const writeAsync = options.writeAsync ?? writePageFileAsync;
|
|
140
|
+
let overlapping = mode === "overlap";
|
|
141
|
+
/** the synchronous writes being measured: how many, and what they took (ms) */
|
|
142
|
+
let count = 0;
|
|
143
|
+
let sum = 0;
|
|
144
|
+
/** while writes overlap: files written since the disk was last tried, and when it is tried again */
|
|
145
|
+
let sinceProbe = 0;
|
|
146
|
+
let probeEvery = PROBE_EVERY;
|
|
147
|
+
let probing = false;
|
|
148
|
+
const measure = (ms) => {
|
|
149
|
+
count++;
|
|
150
|
+
sum += ms;
|
|
151
|
+
if (mode) return;
|
|
152
|
+
if (sum >= (overlapping ? SLOW_WRITE_MS / 4 : SLOW_WRITE_MS) * WRITES_MEASURED) {
|
|
153
|
+
if (!overlapping) {
|
|
154
|
+
overlapping = true;
|
|
155
|
+
options.onSlow?.(sum / count);
|
|
156
|
+
probeEvery = PROBE_EVERY;
|
|
157
|
+
} else probeEvery = Math.min(PROBE_EVERY_MAX, probeEvery * 2);
|
|
158
|
+
} else if (count < WRITES_MEASURED) return;
|
|
159
|
+
else if (overlapping) {
|
|
160
|
+
overlapping = false;
|
|
161
|
+
probeEvery = PROBE_EVERY;
|
|
162
|
+
}
|
|
163
|
+
count = sum = sinceProbe = 0;
|
|
164
|
+
probing = false;
|
|
165
|
+
};
|
|
166
|
+
return {
|
|
167
|
+
/** writes overlap (the disk is slow) */
|
|
168
|
+
get overlapping() {
|
|
169
|
+
return overlapping;
|
|
170
|
+
},
|
|
171
|
+
/** pages whose files may be in flight at a time: half the thread pool's size, 2 to 32 */
|
|
172
|
+
window: Math.min(32, Math.max(2, Math.floor((options.poolSize ?? ioPoolSize()) / 2))),
|
|
173
|
+
/** undefined: the file is written; else a promise that settles when it is (or failed) */
|
|
174
|
+
write(file, data, exists) {
|
|
175
|
+
if (overlapping && !probing) {
|
|
176
|
+
if (mode || ++sinceProbe < probeEvery) return writeAsync(file, data, exists);
|
|
177
|
+
probing = true;
|
|
178
|
+
count = sum = 0;
|
|
179
|
+
}
|
|
180
|
+
const start = now();
|
|
181
|
+
writeSync(file, data, exists);
|
|
182
|
+
measure(now() - start);
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
export { createPageWriter, ioPoolSize, ioThreads, writePageFile, writePageFileAsync };
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { writeFileIfChanged } from "../cache-layout.js";
|
|
2
|
+
import { partitionHash } from "../utils/partition-hash.js";
|
|
3
|
+
import { hashString } from "./incremental.js";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import fs from "node:fs";
|
|
6
|
+
// src/node/build/partitioned-state.ts
|
|
7
|
+
/**
|
|
8
|
+
* The build state (see `cache-layout.ts`): a directory with a
|
|
9
|
+
* small head file and the large maps split by a hash of their keys into partition files. A build
|
|
10
|
+
* that changes a few entries writes a few partitions, not the whole map: when `.cache` travels as
|
|
11
|
+
* image layers, overlayfs copies every modified file into the new layer as a whole.
|
|
12
|
+
*
|
|
13
|
+
* - Partition files are immutable and content-addressed (`nodes-07-<hash>.json`): new content goes
|
|
14
|
+
* to a new file, an unchanged partition is not touched.
|
|
15
|
+
* - A map has a power of two of partitions (at least 64), about 256 KB each: the count follows the
|
|
16
|
+
* map's size (see `targetBytes`), a change of it writes every partition again under a new head.
|
|
17
|
+
* - The head lists the partition files and is written last (tmp + rename): until then the previous
|
|
18
|
+
* head and the files it lists are intact, so a crash leaves the previous state.
|
|
19
|
+
* - Files the head does not list are deleted after it is written (by the next completed write when
|
|
20
|
+
* a write leaves them, see `keepUnlisted`).
|
|
21
|
+
* - Reading verifies every listed file against the hash in its name. Any problem: no state.
|
|
22
|
+
*/
|
|
23
|
+
/** the fewest partitions per map; their count is a power of two (a changed entry rewrites one) */
|
|
24
|
+
const MIN_PARTITION_COUNT = 64;
|
|
25
|
+
/** (indexes in file names have at most 4 hex digits) */
|
|
26
|
+
const MAX_PARTITION_COUNT = 65536;
|
|
27
|
+
const HEAD_FILE = "head.json";
|
|
28
|
+
const PARTITION_FILE = /^([a-z]+)-([0-9a-f]{2,4})-([0-9a-f]{16})\.json$/;
|
|
29
|
+
/**
|
|
30
|
+
* The partition size a map aims at, so a changed entry rewrites about this much however large the
|
|
31
|
+
* map grows. The count changes (and every partition is written again, once) only when the average
|
|
32
|
+
* leaves [target / 2, target * 2], to the count that puts it within a factor of √2 of the target
|
|
33
|
+
* (halfway to either bound): a map that stays near a bound does not switch back and forth.
|
|
34
|
+
*/
|
|
35
|
+
let targetBytes = 262144;
|
|
36
|
+
/** @internal (exported for tests) */
|
|
37
|
+
function setPartitionTargetBytes(bytes) {
|
|
38
|
+
targetBytes = bytes;
|
|
39
|
+
}
|
|
40
|
+
function isPartitionCount(n) {
|
|
41
|
+
return n >= 64 && n <= MAX_PARTITION_COUNT && (n & n - 1) === 0;
|
|
42
|
+
}
|
|
43
|
+
/** @internal (exported for tests) the partition count for `bytes` of a map written with `count` */
|
|
44
|
+
function partitionCountFor(bytes, count) {
|
|
45
|
+
const average = bytes / count;
|
|
46
|
+
if (average >= targetBytes / 2 && average <= targetBytes * 2) return count;
|
|
47
|
+
let next = 64;
|
|
48
|
+
while (next < MAX_PARTITION_COUNT && bytes / next > targetBytes * Math.SQRT2) next *= 2;
|
|
49
|
+
return next;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The partitions each map (by directory) was last written with. A partition with the same keys and
|
|
53
|
+
* values (by identity: entries are replaced, never changed in place) is not serialized again: a
|
|
54
|
+
* build writes the large node map two or three times.
|
|
55
|
+
*/
|
|
56
|
+
const lastWritten = /* @__PURE__ */ new Map();
|
|
57
|
+
function sameItems(a, b) {
|
|
58
|
+
if (a.length !== b.length) return false;
|
|
59
|
+
for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
/** `entries` split into `count` partitions; `previous` are reused, new ones are not written yet */
|
|
63
|
+
function split(dir, map, entries, count, previous) {
|
|
64
|
+
const buckets = Array.from({ length: count }, () => []);
|
|
65
|
+
for (const key in entries) buckets[partitionHash(key) % count].push(key);
|
|
66
|
+
if (previous?.length !== count) previous = void 0;
|
|
67
|
+
const digits = Math.max(2, (count - 1).toString(16).length);
|
|
68
|
+
return buckets.map((keys, i) => {
|
|
69
|
+
if (!keys.length) return null;
|
|
70
|
+
const values = keys.map((key) => entries[key]);
|
|
71
|
+
const before = previous?.[i];
|
|
72
|
+
if (before && sameItems(before.keys, keys) && sameItems(before.values, values) && fs.existsSync(path.join(dir, before.name))) return before;
|
|
73
|
+
const partition = Object.create(null);
|
|
74
|
+
for (const key of [...keys].sort()) partition[key] = entries[key];
|
|
75
|
+
const data = JSON.stringify(partition);
|
|
76
|
+
return {
|
|
77
|
+
keys,
|
|
78
|
+
values,
|
|
79
|
+
name: `${map}-${i.toString(16).padStart(digits, "0")}-${hashString(data)}.json`,
|
|
80
|
+
bytes: Buffer.byteLength(data),
|
|
81
|
+
data
|
|
82
|
+
};
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Writes the partitions of `entries` that do not exist yet; returns their names by partition (null:
|
|
87
|
+
* no entries). Keys are sorted within a partition: the same entries give the same files whatever
|
|
88
|
+
* order the map was built in. `previous`: the names the map was last written with, whose count is
|
|
89
|
+
* kept while the map's size stays in its range (see `targetBytes`).
|
|
90
|
+
*/
|
|
91
|
+
function writePartitions(dir, map, entries, previous) {
|
|
92
|
+
const last = lastWritten.get(path.join(dir, map));
|
|
93
|
+
const count = Array.isArray(previous) && isPartitionCount(previous.length) ? previous.length : 64;
|
|
94
|
+
let partitions = split(dir, map, entries, count, last);
|
|
95
|
+
const next = partitionCountFor(partitions.reduce((sum, p) => sum + (p?.bytes ?? 0), 0), count);
|
|
96
|
+
if (next !== count) partitions = split(dir, map, entries, next, last);
|
|
97
|
+
for (const p of partitions) if (p?.data !== void 0) {
|
|
98
|
+
writeFileIfChanged(path.join(dir, p.name), p.data, {
|
|
99
|
+
atomic: true,
|
|
100
|
+
contentAddressed: true
|
|
101
|
+
});
|
|
102
|
+
delete p.data;
|
|
103
|
+
}
|
|
104
|
+
lastWritten.set(path.join(dir, map), partitions);
|
|
105
|
+
return partitions.map((p) => p?.name ?? null);
|
|
106
|
+
}
|
|
107
|
+
/** the entries of the listed partitions (without prototype), or null when one is missing or broken */
|
|
108
|
+
function readPartitions(dir, map, names) {
|
|
109
|
+
if (!Array.isArray(names) || !isPartitionCount(names.length)) return null;
|
|
110
|
+
const bodies = [];
|
|
111
|
+
for (let i = 0; i < names.length; i++) {
|
|
112
|
+
const name = names[i];
|
|
113
|
+
if (name === null) continue;
|
|
114
|
+
const match = typeof name === "string" ? PARTITION_FILE.exec(name) : null;
|
|
115
|
+
if (!match || match[1] !== map || parseInt(match[2], 16) !== i) return null;
|
|
116
|
+
const file = path.join(dir, name);
|
|
117
|
+
let data;
|
|
118
|
+
try {
|
|
119
|
+
data = fs.readFileSync(file, "utf8");
|
|
120
|
+
} catch {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
if (hashString(data) !== match[3] || data[0] !== "{" || data[data.length - 1] !== "}") {
|
|
124
|
+
fs.rmSync(file, { force: true });
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
if (data.length > 2) bodies.push(data.slice(1, -1));
|
|
128
|
+
}
|
|
129
|
+
let entries;
|
|
130
|
+
try {
|
|
131
|
+
entries = JSON.parse(`{${bodies.join(",")}}`);
|
|
132
|
+
} catch {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
Object.setPrototypeOf(entries, null);
|
|
136
|
+
return entries;
|
|
137
|
+
}
|
|
138
|
+
function readHead(dir) {
|
|
139
|
+
try {
|
|
140
|
+
const head = JSON.parse(fs.readFileSync(path.join(dir, HEAD_FILE), "utf8"));
|
|
141
|
+
return head && typeof head === "object" ? head : null;
|
|
142
|
+
} catch {
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/** writes the head (when it changed) and deletes the files it does not list */
|
|
147
|
+
function commitHead(dir, head, keepUnlisted = false) {
|
|
148
|
+
writeFileIfChanged(path.join(dir, HEAD_FILE), JSON.stringify(head), { atomic: true });
|
|
149
|
+
if (keepUnlisted) return;
|
|
150
|
+
const listed = /* @__PURE__ */ new Set();
|
|
151
|
+
for (const value of Object.values(head)) if (Array.isArray(value)) {
|
|
152
|
+
for (const name of value) if (typeof name === "string") listed.add(name);
|
|
153
|
+
}
|
|
154
|
+
for (const entry of fs.readdirSync(dir)) if (!listed.has(entry) && (PARTITION_FILE.test(entry) || entry.startsWith(".tmp-"))) fs.rmSync(path.join(dir, entry), { force: true });
|
|
155
|
+
}
|
|
156
|
+
/** (2: partitions by `partitionHash`) */
|
|
157
|
+
const QUERY_HEAD_VERSION = 2;
|
|
158
|
+
function readPartitionedQueryState(dir) {
|
|
159
|
+
const head = readHead(dir);
|
|
160
|
+
if (head?.version !== QUERY_HEAD_VERSION || head.stateVersion !== 1) return null;
|
|
161
|
+
if (typeof head.schemaHash !== "string") return null;
|
|
162
|
+
const nodes = readPartitions(dir, "nodes", head.nodes);
|
|
163
|
+
const pages = nodes && readPartitions(dir, "pages", head.pages);
|
|
164
|
+
if (!nodes || !pages) return null;
|
|
165
|
+
return {
|
|
166
|
+
version: 1,
|
|
167
|
+
generation: typeof head.generation === "string" ? head.generation : void 0,
|
|
168
|
+
schemaHash: head.schemaHash,
|
|
169
|
+
nodes,
|
|
170
|
+
nodeCount: typeof head.nodeCount === "number" ? head.nodeCount : void 0,
|
|
171
|
+
pages
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* `keepUnlisted`: the files of the previous head stay (the pending state written before rendering:
|
|
176
|
+
* the final state usually lists most of them again, and a file deleted and written again within a
|
|
177
|
+
* build is in its layer diff).
|
|
178
|
+
*/
|
|
179
|
+
function writePartitionedQueryState(dir, state, options = {}) {
|
|
180
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
181
|
+
const previous = readHead(dir);
|
|
182
|
+
commitHead(dir, {
|
|
183
|
+
version: QUERY_HEAD_VERSION,
|
|
184
|
+
stateVersion: state.version,
|
|
185
|
+
generation: state.generation,
|
|
186
|
+
schemaHash: state.schemaHash,
|
|
187
|
+
nodeCount: state.nodeCount,
|
|
188
|
+
nodes: writePartitions(dir, "nodes", state.nodes, previous?.nodes),
|
|
189
|
+
pages: writePartitions(dir, "pages", state.pages, previous?.pages)
|
|
190
|
+
}, options.keepUnlisted);
|
|
191
|
+
}
|
|
192
|
+
/** (2: partitions by `partitionHash`) */
|
|
193
|
+
const HTML_HEAD_VERSION = 2;
|
|
194
|
+
/**
|
|
195
|
+
* Marks the state as in progress: a build writing pages (see `markHtmlStatePending`). The head is
|
|
196
|
+
* left as it is, so an unchanged build ends with the files it started with.
|
|
197
|
+
*/
|
|
198
|
+
const PENDING_FILE = "pending.json";
|
|
199
|
+
function readPending(dir) {
|
|
200
|
+
let data;
|
|
201
|
+
try {
|
|
202
|
+
data = fs.readFileSync(path.join(dir, PENDING_FILE), "utf8");
|
|
203
|
+
} catch {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
try {
|
|
207
|
+
const pending = JSON.parse(data);
|
|
208
|
+
return Array.isArray(pending?.paths) ? pending.paths : null;
|
|
209
|
+
} catch {
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
function readCommittedHtmlState(dir) {
|
|
214
|
+
const head = readHead(dir);
|
|
215
|
+
if (head?.version !== HTML_HEAD_VERSION || typeof head.ssrHash !== "string") return null;
|
|
216
|
+
const pages = readPartitions(dir, "pages", head.pages);
|
|
217
|
+
return pages && {
|
|
218
|
+
ssrHash: head.ssrHash,
|
|
219
|
+
pages
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* The HTML state; while a build is in progress (or failed) it has no ssrHash and also lists the
|
|
224
|
+
* paths that build may have written, like the single layout's mid-build state.
|
|
225
|
+
*/
|
|
226
|
+
function readPartitionedHtmlState(dir) {
|
|
227
|
+
const committed = readCommittedHtmlState(dir);
|
|
228
|
+
const pending = readPending(dir);
|
|
229
|
+
if (pending === void 0) return committed;
|
|
230
|
+
const pages = Object.create(null);
|
|
231
|
+
for (const p of pending ?? []) if (typeof p === "string") pages[p] = "";
|
|
232
|
+
if (committed) Object.assign(pages, committed.pages);
|
|
233
|
+
return {
|
|
234
|
+
ssrHash: "",
|
|
235
|
+
pages
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Marks the state as in progress before pages are written, with the paths the build may write that
|
|
240
|
+
* the state does not list (`added`; kept with those of an earlier unfinished build).
|
|
241
|
+
*/
|
|
242
|
+
function markHtmlStatePending(dir, added) {
|
|
243
|
+
const paths = new Set(readPending(dir) ?? []);
|
|
244
|
+
for (const p of added) paths.add(p);
|
|
245
|
+
const data = JSON.stringify({ paths: [...paths].sort() });
|
|
246
|
+
writeFileIfChanged(path.join(dir, PENDING_FILE), data, { atomic: true });
|
|
247
|
+
}
|
|
248
|
+
/** the state of a completed build: written, then the in-progress marker removed */
|
|
249
|
+
function writePartitionedHtmlState(dir, state) {
|
|
250
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
251
|
+
commitHead(dir, {
|
|
252
|
+
version: HTML_HEAD_VERSION,
|
|
253
|
+
ssrHash: state.ssrHash,
|
|
254
|
+
pages: writePartitions(dir, "pages", state.pages, readHead(dir)?.pages)
|
|
255
|
+
});
|
|
256
|
+
fs.rmSync(path.join(dir, PENDING_FILE), { force: true });
|
|
257
|
+
}
|
|
258
|
+
export { MIN_PARTITION_COUNT, markHtmlStatePending, partitionCountFor, readPartitionedHtmlState, readPartitionedQueryState, readPartitions, setPartitionTargetBytes, writePartitionedHtmlState, writePartitionedQueryState, writePartitions };
|