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,626 @@
|
|
|
1
|
+
import { WorkerControl } from "./memory-governor.js";
|
|
2
|
+
import { performance } from "node:perf_hooks";
|
|
3
|
+
import { isMainThread, parentPort, resourceLimits, workerData } from "node:worker_threads";
|
|
4
|
+
// src/node/build/query-worker.ts
|
|
5
|
+
/**
|
|
6
|
+
* Query worker (parallel query running): loads the datastore (e.g. the logfile store's index), runs
|
|
7
|
+
* page queries (recording their data dependencies) and – as soon as the main thread sent the
|
|
8
|
+
* renderer config – renders each batch right after querying it. Until then results are buffered,
|
|
9
|
+
* as many as the main thread allows (it stops sending queries when the buffer reaches its share of
|
|
10
|
+
* the heap), and rendered in chunks the main thread asks for. Pages with a still valid query result
|
|
11
|
+
* (incremental builds) are rendered from their existing page-data.json when their HTML is outdated
|
|
12
|
+
* (the main thread sends them once the bundles exist). Pages are rendered in chunks of at most
|
|
13
|
+
* RENDER_CHUNK pages or RENDER_CHUNK_CHARS characters of page data, and the worker drops its caches
|
|
14
|
+
* when its heap fills up (createHeapGuard). A worker whose heap holds node data renders in a thread
|
|
15
|
+
* of its own (RENDER_THREAD_HEAP_MB).
|
|
16
|
+
*/
|
|
17
|
+
/** pages and characters of page data rendered at a time (cached results are read per chunk, too) */
|
|
18
|
+
const RENDER_CHUNK = 64;
|
|
19
|
+
const RENDER_CHUNK_CHARS = 8388608;
|
|
20
|
+
/** page queries of a batch running at a time (Gatsby: 4) */
|
|
21
|
+
const QUERY_CONCURRENCY = 8;
|
|
22
|
+
/**
|
|
23
|
+
* A query worker with this much heap in use (MB) renders its pages in a thread of its own
|
|
24
|
+
* (render-thread.ts), which holds the SSR bundle and one batch of pages. Rendering is mostly
|
|
25
|
+
* garbage – parsed results, elements, HTML – and what collecting it costs depends on the heap it
|
|
26
|
+
* is collected in: next to the node data of page queries that ran in JS (slim nodes, views,
|
|
27
|
+
* indexes), in a heap of 750 MB, rendering took 2.5 ms a page instead of 2.0 ms in a thread of its
|
|
28
|
+
* own. That thread costs about 55 MB, and handing it the results a little time: with a heap of
|
|
29
|
+
* 220 MB it was the slower way (2.4 instead of 2.1 ms a page). A worker whose queries run in WASM
|
|
30
|
+
* keeps a small heap (35–140 MB).
|
|
31
|
+
* (GOATLY_RENDER_THREAD=on|off|later: internal, for tests – "later": after the first batch)
|
|
32
|
+
*/
|
|
33
|
+
const RENDER_THREAD_HEAP_MB = 512;
|
|
34
|
+
/** the least heap limit (MB) of that thread, when the worker's heap leaves less of its own limit */
|
|
35
|
+
const RENDER_THREAD_MIN_MB = 256;
|
|
36
|
+
async function main(data) {
|
|
37
|
+
const port = parentPort;
|
|
38
|
+
try {
|
|
39
|
+
const { bootstrapWorker } = await import("../bootstrap.js");
|
|
40
|
+
const { runGraphQL } = await import("../query/graphql-runner.js");
|
|
41
|
+
const { generateHtmlPath, generatePageDataPath } = await import("../utils/paths.js");
|
|
42
|
+
const { AssetResolver } = await import("./assets.js");
|
|
43
|
+
const { createRenderer } = await import("./render-worker.js");
|
|
44
|
+
const { createWorkClock } = await import("./work-clock.js");
|
|
45
|
+
const { startRenderThread } = await import("./render-thread.js");
|
|
46
|
+
const { createDirectoryMaker, outputDirs } = await import("./output-dirs.js");
|
|
47
|
+
const { finishCaches, persistCaches, releaseCaches } = await import("../cache.js");
|
|
48
|
+
const { getDataStore, nodeTypeOf } = await import("../datastore/index.js");
|
|
49
|
+
const { forwardJobs } = await import("../actions/jobs.js");
|
|
50
|
+
const jobs = /* @__PURE__ */ new Map();
|
|
51
|
+
let jobIds = 0;
|
|
52
|
+
forwardJobs((job, plugin) => {
|
|
53
|
+
const jobId = jobIds++;
|
|
54
|
+
let resolve, reject;
|
|
55
|
+
const promise = new Promise((res, rej) => (resolve = res, reject = rej));
|
|
56
|
+
jobs.set(jobId, {
|
|
57
|
+
promise,
|
|
58
|
+
resolve,
|
|
59
|
+
reject
|
|
60
|
+
});
|
|
61
|
+
try {
|
|
62
|
+
port.postMessage({
|
|
63
|
+
type: "job",
|
|
64
|
+
jobId,
|
|
65
|
+
job,
|
|
66
|
+
plugin
|
|
67
|
+
});
|
|
68
|
+
} catch (e) {
|
|
69
|
+
jobs.delete(jobId);
|
|
70
|
+
return Promise.reject(e);
|
|
71
|
+
}
|
|
72
|
+
return promise;
|
|
73
|
+
});
|
|
74
|
+
port.on("message", (msg) => {
|
|
75
|
+
if (msg.type !== "job-done" && msg.type !== "job-failed") return;
|
|
76
|
+
const waiting = jobs.get(msg.jobId);
|
|
77
|
+
if (!waiting) return;
|
|
78
|
+
jobs.delete(msg.jobId);
|
|
79
|
+
if (msg.type === "job-done") return waiting.resolve(msg.result);
|
|
80
|
+
if (msg.reported) waiting.promise.catch(() => {});
|
|
81
|
+
waiting.reject(Object.assign(new Error(msg.message), { stack: msg.stack }));
|
|
82
|
+
});
|
|
83
|
+
const { clearQueryEngineCaches, setSlimNodesHeap } = await import("../schema/query-engine.js");
|
|
84
|
+
const { createHeapGuard, heapRatio, heapUsedMb, RUNTIME, threadCpuMs, watchGarbageCollections } = await import("../utils/memory.js");
|
|
85
|
+
const { reporter } = await import("../reporter.js");
|
|
86
|
+
const { QueryDependencies } = await import("../query/dependencies.js");
|
|
87
|
+
const { hashString, htmlDigest, htmlRemovedEachBuild, readCachedResult, serializeDependencies } = await import("./incremental.js");
|
|
88
|
+
const { createSitePageDeferrals, sitePageNodesWereRead } = await import("../site-page-nodes.js");
|
|
89
|
+
const wasmModule = data.wasmData ? await import("../wasm-data/worker.js") : void 0;
|
|
90
|
+
const wasm = wasmModule && data.wasmData ? wasmModule.createWasmRunner(data.wasmData) : void 0;
|
|
91
|
+
await bootstrapWorker({
|
|
92
|
+
directory: data.directory,
|
|
93
|
+
prefixPaths: data.prefixPaths,
|
|
94
|
+
inferenceFile: data.inferenceFile,
|
|
95
|
+
renderer: data.renderer,
|
|
96
|
+
sitePageNodes: data.sitePageNodes,
|
|
97
|
+
cacheRun: data.cacheRun
|
|
98
|
+
});
|
|
99
|
+
const control = data.control ? new Int32Array(data.control) : void 0;
|
|
100
|
+
/** the heap (MB) this worker measures itself against: the budget it was given, if any */
|
|
101
|
+
const budget = () => control && Atomics.load(control, WorkerControl.BUDGET) || data.heapBudgetMb;
|
|
102
|
+
const v8 = await import("node:v8");
|
|
103
|
+
let limitMb;
|
|
104
|
+
const heapLimitMb = () => limitMb ??= v8.getHeapStatistics().heap_size_limit / 1048576;
|
|
105
|
+
let slimFor = budget() ?? heapLimitMb();
|
|
106
|
+
setSlimNodesHeap(slimFor);
|
|
107
|
+
/** publishes the heap in use (the adaptive pool sizes budgets and picks workers by it) */
|
|
108
|
+
const publish = () => {
|
|
109
|
+
if (!control) return;
|
|
110
|
+
Atomics.store(control, WorkerControl.HEAP, Math.round(heapUsedMb()));
|
|
111
|
+
if (wasm) Atomics.store(control, WorkerControl.WASM, Math.round(wasm.threadBytes() / 1048576));
|
|
112
|
+
Atomics.add(control, WorkerControl.BEAT, 1);
|
|
113
|
+
};
|
|
114
|
+
const deferrals = createSitePageDeferrals();
|
|
115
|
+
let job;
|
|
116
|
+
let renderer;
|
|
117
|
+
/** the thread this worker renders in when its heap is large (see RENDER_THREAD_HEAP_MB) */
|
|
118
|
+
let renderThread;
|
|
119
|
+
/** the thread could not load the SSR bundle this worker loaded: not tried again */
|
|
120
|
+
let renderThreadFailed = false;
|
|
121
|
+
const renderThreadMode = process.env.GOATLY_RENDER_THREAD;
|
|
122
|
+
/**
|
|
123
|
+
* Whether to render in a thread of its own: by the heap that stays – without the results the
|
|
124
|
+
* worker holds for rendering (`setup`: when the render job arrives; "later": only afterwards).
|
|
125
|
+
* (Bun: not tried there – its worker threads have no heap limits of their own)
|
|
126
|
+
*/
|
|
127
|
+
const wantsRenderThread = (setup) => RUNTIME !== "bun" && !renderThreadFailed && renderThreadMode !== "off" && (renderThreadMode === "on" || renderThreadMode === "later" && !setup || renderThreadMode !== "later" && heapUsedMb() - bufferedBytes / 1048576 >= RENDER_THREAD_HEAP_MB);
|
|
128
|
+
/** undefined: the thread could not be started – this worker renders, as without one */
|
|
129
|
+
const startRenderThreadFor = async (options) => {
|
|
130
|
+
const heapMb = Math.round(heapUsedMb());
|
|
131
|
+
const limitMb = resourceLimits?.maxOldGenerationSizeMb;
|
|
132
|
+
try {
|
|
133
|
+
renderThread = await startRenderThread(options, {
|
|
134
|
+
limitMb: limitMb ? Math.max(RENDER_THREAD_MIN_MB, Math.round(limitMb - heapMb)) : void 0,
|
|
135
|
+
onDeath: (error, oom) => port.postMessage({
|
|
136
|
+
type: "failed",
|
|
137
|
+
error: oom ? "its render thread ran out of memory" : error.stack ?? error.message,
|
|
138
|
+
oom
|
|
139
|
+
})
|
|
140
|
+
});
|
|
141
|
+
} catch (e) {
|
|
142
|
+
renderThreadFailed = true;
|
|
143
|
+
reporter.verbose(`query worker ${data.workerId} renders in its own thread: a render thread could not be started (${e?.message ?? e})`);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
reporter.verbose(`query worker ${data.workerId} renders in a thread of its own (its heap holds ${heapMb} MB)`);
|
|
147
|
+
return renderThread;
|
|
148
|
+
};
|
|
149
|
+
/** creates the output directories of the pages this worker renders */
|
|
150
|
+
let dirs;
|
|
151
|
+
let assets;
|
|
152
|
+
/** the renderer could not be set up: every page fails with this error */
|
|
153
|
+
let setupError;
|
|
154
|
+
/** results waiting for the renderer bundle, by page path (`resultJson` unset: cached result) */
|
|
155
|
+
const buffered = /* @__PURE__ */ new Map();
|
|
156
|
+
let bufferedBytes = 0;
|
|
157
|
+
/** estimated heap bytes of a page's results: its Page clone and `chars` of JSON (1–2 bytes each) */
|
|
158
|
+
const resultBytes = (chars) => 1024 + 2 * chars;
|
|
159
|
+
const itemBytes = (item) => resultBytes(item.resultJson?.length ?? 0);
|
|
160
|
+
/** characters of cached results render() read (they count towards a batch's bytes) */
|
|
161
|
+
let readChars = 0;
|
|
162
|
+
const ratio = () => heapRatio(budget());
|
|
163
|
+
const clock = () => performance.timeOrigin + performance.now();
|
|
164
|
+
/** where this worker's time goes (see WorkerTimings) */
|
|
165
|
+
const work = createWorkClock({
|
|
166
|
+
epoch: clock,
|
|
167
|
+
now: () => performance.now(),
|
|
168
|
+
cpu: threadCpuMs
|
|
169
|
+
});
|
|
170
|
+
let collectedMb;
|
|
171
|
+
const gcWatch = watchGarbageCollections(({ start, ms, major }) => {
|
|
172
|
+
work.collected(start, ms);
|
|
173
|
+
if (major) collectedMb = heapUsedMb();
|
|
174
|
+
});
|
|
175
|
+
/**
|
|
176
|
+
* the heap (MB) the guard measures against (see heapRatio): the budget, at most the heap limit
|
|
177
|
+
* (Bun, which has no limit: the budget)
|
|
178
|
+
*/
|
|
179
|
+
const measuredAgainst = () => {
|
|
180
|
+
const mb = budget();
|
|
181
|
+
return RUNTIME === "bun" ? mb : Math.min(mb ?? 1e400, heapLimitMb());
|
|
182
|
+
};
|
|
183
|
+
const guard = createHeapGuard({
|
|
184
|
+
ratio,
|
|
185
|
+
collected() {
|
|
186
|
+
const seen = collectedMb;
|
|
187
|
+
collectedMb = void 0;
|
|
188
|
+
const against = measuredAgainst();
|
|
189
|
+
return seen === void 0 || !against ? void 0 : seen / against;
|
|
190
|
+
},
|
|
191
|
+
ceiling: () => RUNTIME === "bun" ? 1 : heapLimitMb() / (measuredAgainst() ?? 1),
|
|
192
|
+
hold: control ? void 0 : () => 1.25,
|
|
193
|
+
release() {
|
|
194
|
+
clearQueryEngineCaches();
|
|
195
|
+
releaseCaches();
|
|
196
|
+
getDataStore().trimCache?.();
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
/** what the guard measured against at its last check */
|
|
200
|
+
let guardBudget = measuredAgainst();
|
|
201
|
+
/**
|
|
202
|
+
* Checks the heap after a batch (see createHeapGuard) against the current budget: what the
|
|
203
|
+
* guard knows about the heap follows a budget the adaptive pool changed (a lower one applies
|
|
204
|
+
* with this check); the slim nodes follow it, too
|
|
205
|
+
*/
|
|
206
|
+
const checkHeap = () => {
|
|
207
|
+
const now = measuredAgainst();
|
|
208
|
+
if (now && guardBudget && now !== guardBudget) guard.rescale(guardBudget / now);
|
|
209
|
+
guardBudget = now;
|
|
210
|
+
const forSlim = budget() ?? heapLimitMb();
|
|
211
|
+
if (Math.abs(forSlim - slimFor) >= .25 * slimFor) setSlimNodesHeap(slimFor = forSlim);
|
|
212
|
+
const checks = guard.checks;
|
|
213
|
+
guard.afterBatch();
|
|
214
|
+
timings.guard = {
|
|
215
|
+
ms: guard.forcedMs,
|
|
216
|
+
releases: guard.releases
|
|
217
|
+
};
|
|
218
|
+
if (control && guard.checks !== checks) Atomics.store(control, WorkerControl.LIVE, Math.round(heapUsedMb()));
|
|
219
|
+
publish();
|
|
220
|
+
};
|
|
221
|
+
const emptySpan = () => ({
|
|
222
|
+
ms: 0,
|
|
223
|
+
start: 1e400,
|
|
224
|
+
end: -1e400
|
|
225
|
+
});
|
|
226
|
+
const timings = {
|
|
227
|
+
query: emptySpan(),
|
|
228
|
+
render: emptySpan()
|
|
229
|
+
};
|
|
230
|
+
const emptyResult = () => ({
|
|
231
|
+
rendered: 0,
|
|
232
|
+
skipped: 0,
|
|
233
|
+
digests: {},
|
|
234
|
+
errors: []
|
|
235
|
+
});
|
|
236
|
+
const queryOutput = () => ({
|
|
237
|
+
queries: [],
|
|
238
|
+
errors: [],
|
|
239
|
+
deferred: []
|
|
240
|
+
});
|
|
241
|
+
/** runs a page query (undefined: the page is left to the main thread, see `deferrals`) */
|
|
242
|
+
async function runQuery(page, out) {
|
|
243
|
+
const index = data.componentQueries[page.componentPath];
|
|
244
|
+
const query = index === void 0 ? void 0 : data.queryTexts[index];
|
|
245
|
+
let resultData;
|
|
246
|
+
let error;
|
|
247
|
+
const deps = new QueryDependencies();
|
|
248
|
+
if (query) {
|
|
249
|
+
if (deferrals.skip(index)) {
|
|
250
|
+
out.deferred.push(page.path);
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
const start = deferrals.start();
|
|
254
|
+
const res = await runGraphQL(query, {
|
|
255
|
+
...page,
|
|
256
|
+
...page.context
|
|
257
|
+
}, page.path, deps, { plainObjects: true });
|
|
258
|
+
if (deferrals.missed(start, index)) {
|
|
259
|
+
out.deferred.push(page.path);
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
if (res.errors?.length) error = {
|
|
263
|
+
pagePath: page.path,
|
|
264
|
+
message: res.errors.map((e) => e.message).join("\n")
|
|
265
|
+
};
|
|
266
|
+
resultData = res.data;
|
|
267
|
+
}
|
|
268
|
+
if (error) out.errors.push(error);
|
|
269
|
+
const resultJson = JSON.stringify({
|
|
270
|
+
data: resultData,
|
|
271
|
+
pageContext: page.context
|
|
272
|
+
});
|
|
273
|
+
const resultHash = hashString(resultJson);
|
|
274
|
+
const { n, c } = serializeDependencies(deps, nodeTypeOf);
|
|
275
|
+
out.queries.push([
|
|
276
|
+
page.path,
|
|
277
|
+
resultHash,
|
|
278
|
+
n,
|
|
279
|
+
c
|
|
280
|
+
]);
|
|
281
|
+
return {
|
|
282
|
+
page,
|
|
283
|
+
resultHash,
|
|
284
|
+
resultJson
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Runs the pages of `items` whose queries the WASM data side supports, WASM_BATCH per call,
|
|
289
|
+
* into `ran` (pages it can't answer stay unset: they run in JS). Verify mode: their queries
|
|
290
|
+
* run in JS, too, the JS result is used and a different WASM result is reported.
|
|
291
|
+
*/
|
|
292
|
+
async function runWasm(runner, { WASM_BATCH, resultJsonOf, sameResult }, items, ran, out) {
|
|
293
|
+
/** the items handled here */
|
|
294
|
+
const done = /* @__PURE__ */ new Set();
|
|
295
|
+
const pending = [];
|
|
296
|
+
for (let i = 0; i < items.length; i++) {
|
|
297
|
+
const item = items[i];
|
|
298
|
+
if (item.resultHash !== void 0) continue;
|
|
299
|
+
const index = data.componentQueries[item.page.componentPath];
|
|
300
|
+
if (index === void 0 || deferrals.skip(index)) continue;
|
|
301
|
+
const page = runner.page(item.page, index);
|
|
302
|
+
if (page) pending.push({
|
|
303
|
+
i,
|
|
304
|
+
page
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
for (let from = 0; from < pending.length; from += WASM_BATCH) {
|
|
308
|
+
const chunk = pending.slice(from, from + WASM_BATCH);
|
|
309
|
+
const results = runner.run(chunk.map((p) => p.page));
|
|
310
|
+
if (!results) break;
|
|
311
|
+
for (let k = 0; k < chunk.length; k++) {
|
|
312
|
+
const r = results[k];
|
|
313
|
+
if (r.status !== 0) continue;
|
|
314
|
+
const { i } = chunk[k];
|
|
315
|
+
done.add(i);
|
|
316
|
+
const item = items[i];
|
|
317
|
+
const { page } = item;
|
|
318
|
+
const resultJson = resultJsonOf(page, r.data);
|
|
319
|
+
if (runner.verify) {
|
|
320
|
+
const js = queryOutput();
|
|
321
|
+
const result = await runQuery(page, js);
|
|
322
|
+
out.queries.push(...js.queries);
|
|
323
|
+
out.errors.push(...js.errors);
|
|
324
|
+
out.deferred.push(...js.deferred);
|
|
325
|
+
ran[i] = result && {
|
|
326
|
+
...result,
|
|
327
|
+
previous: item.previous
|
|
328
|
+
};
|
|
329
|
+
if (!result) continue;
|
|
330
|
+
const record = js.queries[0];
|
|
331
|
+
const jsResult = {
|
|
332
|
+
resultJson: result.resultJson,
|
|
333
|
+
n: record?.[2],
|
|
334
|
+
c: record?.[3],
|
|
335
|
+
errors: js.errors.length
|
|
336
|
+
};
|
|
337
|
+
if (!sameResult({
|
|
338
|
+
resultJson,
|
|
339
|
+
n: r.n,
|
|
340
|
+
c: r.c
|
|
341
|
+
}, jsResult)) runner.mismatch({
|
|
342
|
+
path: page.path,
|
|
343
|
+
wasm: {
|
|
344
|
+
resultJson,
|
|
345
|
+
n: r.n,
|
|
346
|
+
c: r.c
|
|
347
|
+
},
|
|
348
|
+
js: jsResult
|
|
349
|
+
});
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
const resultHash = hashString(resultJson);
|
|
353
|
+
out.queries.push([
|
|
354
|
+
page.path,
|
|
355
|
+
resultHash,
|
|
356
|
+
r.n,
|
|
357
|
+
r.c
|
|
358
|
+
]);
|
|
359
|
+
ran[i] = {
|
|
360
|
+
page,
|
|
361
|
+
resultHash,
|
|
362
|
+
resultJson,
|
|
363
|
+
previous: item.previous
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
publish();
|
|
367
|
+
}
|
|
368
|
+
return done;
|
|
369
|
+
}
|
|
370
|
+
async function render(items, queryOut) {
|
|
371
|
+
const out = emptyResult();
|
|
372
|
+
if (!job || !items.length) return out;
|
|
373
|
+
if (!renderer || !assets) {
|
|
374
|
+
for (const { page } of items) {
|
|
375
|
+
const message = setupError?.message ?? "the renderer is not set up";
|
|
376
|
+
out.errors.push({
|
|
377
|
+
pagePath: page.path,
|
|
378
|
+
message,
|
|
379
|
+
stack: setupError?.stack
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
return out;
|
|
383
|
+
}
|
|
384
|
+
const rendering = work.begin(timings.render);
|
|
385
|
+
if (!renderThread && wantsRenderThread(false)) renderer = await startRenderThreadFor(job.renderer) ?? renderer;
|
|
386
|
+
let tasks = [];
|
|
387
|
+
let taskChars = 0;
|
|
388
|
+
const renderTasks = async () => {
|
|
389
|
+
if (!tasks.length) return;
|
|
390
|
+
const chunk = tasks;
|
|
391
|
+
tasks = [];
|
|
392
|
+
taskChars = 0;
|
|
393
|
+
await dirs?.ensure(outputDirs(chunk));
|
|
394
|
+
const res = await renderer.renderBatch(chunk);
|
|
395
|
+
if (renderThread) {
|
|
396
|
+
const { gcMs, cpuMs } = renderThread.takeStats();
|
|
397
|
+
timings.render.gcMs = (timings.render.gcMs ?? 0) + gcMs;
|
|
398
|
+
if (cpuMs !== void 0) timings.render.cpuMs = (timings.render.cpuMs ?? 0) + cpuMs;
|
|
399
|
+
}
|
|
400
|
+
publish();
|
|
401
|
+
out.rendered += res.rendered;
|
|
402
|
+
out.errors.push(...res.errors);
|
|
403
|
+
};
|
|
404
|
+
try {
|
|
405
|
+
for (let item of items) {
|
|
406
|
+
const { page } = item;
|
|
407
|
+
let digest = htmlDigest(page, item.resultHash);
|
|
408
|
+
if (job.previousSsrHash === job.ssrHash && item.previous === digest && !htmlRemovedEachBuild(page.path)) {
|
|
409
|
+
out.digests[page.path] = digest;
|
|
410
|
+
out.skipped++;
|
|
411
|
+
continue;
|
|
412
|
+
}
|
|
413
|
+
const htmlFile = generateHtmlPath(job.publicDir, page.path);
|
|
414
|
+
const pageDataFile = generatePageDataPath(job.publicDir, page.path);
|
|
415
|
+
const cached = item.resultJson === void 0 ? readCachedResult(job.publicDir, page) : void 0;
|
|
416
|
+
let resultJson = item.resultJson ?? cached?.resultJson;
|
|
417
|
+
if (cached) readChars += cached.resultJson.length;
|
|
418
|
+
if (resultJson === void 0) {
|
|
419
|
+
const querying = work.begin(timings.query);
|
|
420
|
+
let ran;
|
|
421
|
+
try {
|
|
422
|
+
ran = await runQuery(page, queryOut);
|
|
423
|
+
} finally {
|
|
424
|
+
work.end(querying);
|
|
425
|
+
}
|
|
426
|
+
if (!ran) continue;
|
|
427
|
+
item = {
|
|
428
|
+
...ran,
|
|
429
|
+
previous: item.previous
|
|
430
|
+
};
|
|
431
|
+
resultJson = item.resultJson;
|
|
432
|
+
digest = htmlDigest(page, item.resultHash);
|
|
433
|
+
}
|
|
434
|
+
out.digests[page.path] = digest;
|
|
435
|
+
const fixed = page.path === "/" ? "index" : page.path.replace(/^\/|\/$/g, "");
|
|
436
|
+
tasks.push({
|
|
437
|
+
pagePath: page.path,
|
|
438
|
+
componentChunkName: page.componentChunkName,
|
|
439
|
+
matchPath: page.matchPath,
|
|
440
|
+
resultJson,
|
|
441
|
+
assets: assets.forComponents([page.componentPath]),
|
|
442
|
+
pageDataUrl: `${job.pagePrefix}/page-data/${fixed}/page-data.json`,
|
|
443
|
+
htmlFile,
|
|
444
|
+
pageDataFile,
|
|
445
|
+
pageDataStaticQueryHashes: cached?.staticQueryHashes,
|
|
446
|
+
filesExist: job.outputFiles === "any" || job.outputFiles === "listed" && item.previous !== void 0
|
|
447
|
+
});
|
|
448
|
+
taskChars += resultJson.length;
|
|
449
|
+
if (tasks.length >= RENDER_CHUNK || taskChars >= RENDER_CHUNK_CHARS) await renderTasks();
|
|
450
|
+
}
|
|
451
|
+
await renderTasks();
|
|
452
|
+
} finally {
|
|
453
|
+
work.end(rendering);
|
|
454
|
+
}
|
|
455
|
+
return out;
|
|
456
|
+
}
|
|
457
|
+
const reply = (id, result = {}) => port.postMessage({
|
|
458
|
+
type: "batch",
|
|
459
|
+
id,
|
|
460
|
+
buffered: false,
|
|
461
|
+
bytes: 0,
|
|
462
|
+
bufferedBytes,
|
|
463
|
+
heapRatio: ratio(),
|
|
464
|
+
timings,
|
|
465
|
+
queries: [],
|
|
466
|
+
queryErrors: [],
|
|
467
|
+
render: emptyResult(),
|
|
468
|
+
deferred: [],
|
|
469
|
+
sitePagesRead: sitePageNodesWereRead(),
|
|
470
|
+
...result
|
|
471
|
+
});
|
|
472
|
+
const outputOf = (out) => ({
|
|
473
|
+
queries: out.queries,
|
|
474
|
+
queryErrors: out.errors,
|
|
475
|
+
deferred: out.deferred
|
|
476
|
+
});
|
|
477
|
+
async function handle(msg) {
|
|
478
|
+
if (msg.type === "query") {
|
|
479
|
+
const out = queryOutput();
|
|
480
|
+
const querying = work.begin(timings.query);
|
|
481
|
+
const ran = Array.from({ length: msg.items.length });
|
|
482
|
+
try {
|
|
483
|
+
const done = wasm && wasmModule ? await runWasm(wasm, wasmModule, msg.items, ran, out) : void 0;
|
|
484
|
+
let next = 0;
|
|
485
|
+
const runNext = async () => {
|
|
486
|
+
while (next < msg.items.length) {
|
|
487
|
+
const i = next++;
|
|
488
|
+
if (done?.has(i)) continue;
|
|
489
|
+
const item = msg.items[i];
|
|
490
|
+
if (item.resultHash !== void 0) {
|
|
491
|
+
ran[i] = {
|
|
492
|
+
page: item.page,
|
|
493
|
+
resultHash: item.resultHash,
|
|
494
|
+
previous: item.previous
|
|
495
|
+
};
|
|
496
|
+
continue;
|
|
497
|
+
}
|
|
498
|
+
const result = await runQuery(item.page, out);
|
|
499
|
+
publish();
|
|
500
|
+
ran[i] = result && {
|
|
501
|
+
...result,
|
|
502
|
+
previous: item.previous
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
await Promise.all(Array.from({ length: Math.min(QUERY_CONCURRENCY, msg.items.length) }, runNext));
|
|
507
|
+
} finally {
|
|
508
|
+
work.end(querying, out.queries.length > 0 || out.deferred.length > 0);
|
|
509
|
+
}
|
|
510
|
+
const items = ran.filter((item) => item !== void 0);
|
|
511
|
+
let bytes = 0;
|
|
512
|
+
for (const item of items) bytes += itemBytes(item);
|
|
513
|
+
if (job) {
|
|
514
|
+
readChars = 0;
|
|
515
|
+
const result = await render(items, out);
|
|
516
|
+
bytes += 2 * readChars;
|
|
517
|
+
checkHeap();
|
|
518
|
+
reply(msg.id, {
|
|
519
|
+
bytes,
|
|
520
|
+
...outputOf(out),
|
|
521
|
+
render: result
|
|
522
|
+
});
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
for (const item of items) {
|
|
526
|
+
buffered.set(item.page.path, item.resultJson === void 0 ? item : {
|
|
527
|
+
...item,
|
|
528
|
+
page: {
|
|
529
|
+
...item.page,
|
|
530
|
+
context: {}
|
|
531
|
+
}
|
|
532
|
+
});
|
|
533
|
+
bufferedBytes += itemBytes(item);
|
|
534
|
+
}
|
|
535
|
+
checkHeap();
|
|
536
|
+
reply(msg.id, {
|
|
537
|
+
buffered: true,
|
|
538
|
+
bytes,
|
|
539
|
+
...outputOf(out)
|
|
540
|
+
});
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
if (msg.type === "render") {
|
|
544
|
+
const setup = work.begin(timings.render);
|
|
545
|
+
job = msg.job;
|
|
546
|
+
dirs = void 0;
|
|
547
|
+
await renderThread?.close();
|
|
548
|
+
renderThread = void 0;
|
|
549
|
+
try {
|
|
550
|
+
assets = new AssetResolver(job.manifest, job.root, job.base);
|
|
551
|
+
renderer = (wantsRenderThread(true) ? await startRenderThreadFor(job.renderer) : void 0) ?? await createRenderer(job.renderer);
|
|
552
|
+
dirs = createDirectoryMaker(job.publicDir);
|
|
553
|
+
} catch (e) {
|
|
554
|
+
setupError = e instanceof Error ? e : new Error(String(e));
|
|
555
|
+
}
|
|
556
|
+
work.end(setup);
|
|
557
|
+
reply(msg.id);
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
if (msg.type === "flush") {
|
|
561
|
+
const items = [];
|
|
562
|
+
for (const pagePath of msg.paths) {
|
|
563
|
+
const item = buffered.get(pagePath);
|
|
564
|
+
if (!item) continue;
|
|
565
|
+
buffered.delete(pagePath);
|
|
566
|
+
bufferedBytes -= itemBytes(item);
|
|
567
|
+
items.push(item);
|
|
568
|
+
}
|
|
569
|
+
const out = queryOutput();
|
|
570
|
+
const result = await render(items, out);
|
|
571
|
+
checkHeap();
|
|
572
|
+
reply(msg.id, {
|
|
573
|
+
...outputOf(out),
|
|
574
|
+
render: result
|
|
575
|
+
});
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
if (msg.type === "finish") {
|
|
579
|
+
wasm?.detach();
|
|
580
|
+
await renderThread?.close();
|
|
581
|
+
persistCaches();
|
|
582
|
+
finishCaches();
|
|
583
|
+
gcWatch.close();
|
|
584
|
+
if (guard.checks) {
|
|
585
|
+
const budgetText = budget() ? ` (budget ${data.heapBudgetMb && data.heapBudgetMb !== budget() ? `${data.heapBudgetMb} → ` : ""}${budget()} MB)` : "";
|
|
586
|
+
const skipped = guard.skipped ? `, ${guard.skipped} left out (they had taken their share of its time)` : "";
|
|
587
|
+
reporter.verbose(`query worker ${data.workerId} ${guard.releases ? `dropped its caches ${guard.releases} time(s): its heap was full` : "checked its heap"}${budgetText} – ${guard.checks} forced collection(s), ${(guard.forcedMs / 1e3).toFixed(3)}s${skipped}`);
|
|
588
|
+
}
|
|
589
|
+
port.postMessage({
|
|
590
|
+
type: "finished",
|
|
591
|
+
id: msg.id,
|
|
592
|
+
timings
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
let queue = Promise.resolve();
|
|
597
|
+
port.on("message", (msg) => {
|
|
598
|
+
if (msg.type === "job-done" || msg.type === "job-failed") return;
|
|
599
|
+
queue = queue.then(() => handle(msg).catch((e) => {
|
|
600
|
+
const error = {
|
|
601
|
+
pagePath: "(worker)",
|
|
602
|
+
message: e?.message ?? String(e),
|
|
603
|
+
stack: e?.stack
|
|
604
|
+
};
|
|
605
|
+
reply(msg.id, { render: {
|
|
606
|
+
...emptyResult(),
|
|
607
|
+
errors: [error]
|
|
608
|
+
} });
|
|
609
|
+
if (msg.type === "finish") port.postMessage({
|
|
610
|
+
type: "finished",
|
|
611
|
+
id: msg.id,
|
|
612
|
+
timings
|
|
613
|
+
});
|
|
614
|
+
}));
|
|
615
|
+
});
|
|
616
|
+
publish();
|
|
617
|
+
port.postMessage({ type: "ready" });
|
|
618
|
+
} catch (e) {
|
|
619
|
+
port.postMessage({
|
|
620
|
+
type: "error",
|
|
621
|
+
error: e?.stack ?? String(e)
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
if (!isMainThread && parentPort && workerData?.__goatlyQueryWorker) main(workerData);
|
|
626
|
+
export {};
|