@akanjs/cli 3.2.0-rc.17 → 3.2.0-rc.19
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/.build-stamp +2 -2
- package/{CodeAgent-18qg03re.js → CodeAgent-q0xkt4at.js} +2 -2
- package/{CodeTui-zkm51zf3.js → CodeTui-n56kpaca.js} +819 -663
- package/TunnelShare-844s9te4.js +18 -0
- package/{agent.command-5k2zegmj.js → agent.command-k07raqft.js} +20 -20
- package/akanCode.js +8 -8
- package/{appExecutor-1bgs9a69.js → appExecutor-xav1zemr.js} +3 -3
- package/{application.command-ge27047n.js → application.command-dv8y8r12.js} +52 -25
- package/{applicationBuildRunner-7skfg8zn.js → applicationBuildRunner-vhx7zq97.js} +9 -10
- package/buildBatch.proc.js +20 -11
- package/{cloud.command-d18qj1v2.js → cloud.command-0vjy3x96.js} +22 -22
- package/cloud.script-jp48r0kw.js +35 -0
- package/{code.command-tz4x5zm6.js → code.command-xc5exq4b.js} +10 -10
- package/{context.command-x1smtm5q.js → context.command-etsat8yk.js} +22 -22
- package/context.runner-e52ak0cx.js +34 -0
- package/{guideline.command-rdcwxf8g.js → guideline.command-zqkntk7b.js} +6 -6
- package/guidelines/agentRule/agentRule.instruction.md +5 -4
- package/guidelines/conventions/core.md +18 -0
- package/guidelines/errorCodes/errorCodes.instruction.md +6 -0
- package/guidelines/mcpRule/mcpRule.instruction.md +4 -2
- package/guidelines/transportRule/transportRule.instruction.md +3 -1
- package/incrementalBuilder.proc.js +559 -409
- package/{index-gh4brxzn.js → index-12js5pj4.js} +1 -1
- package/{index-jfkh776h.js → index-12vv4g80.js} +1 -1
- package/index-4zqebbqb.js +80 -0
- package/{index-er2ja34m.js → index-6bryy19d.js} +500 -422
- package/{index-3641r8q0.js → index-6pzncry1.js} +4 -4
- package/{index-fcrr79gh.js → index-6rrh0ffr.js} +44 -32
- package/{index-nd39rssn.js → index-7jr9mfp2.js} +286 -144
- package/{index-sp331bww.js → index-81qt1jyj.js} +1 -1
- package/{index-2mrn1mrg.js → index-9f3reh2y.js} +1 -1
- package/{index-e82xwj3x.js → index-a05ewnnp.js} +1 -1
- package/{index-bjnebzzc.js → index-bxevbjw7.js} +8 -8
- package/{index-vm6pr11y.js → index-c1vym3gx.js} +2 -2
- package/{index-t8tysbxv.js → index-c9hd79py.js} +4 -4
- package/{index-j2eqe3jk.js → index-c9mnfy9r.js} +38 -23
- package/{index-dtpeyts6.js → index-cc9pevta.js} +1 -1
- package/{index-trp7jdr1.js → index-cet2s7xk.js} +7 -6
- package/{index-yr5jv8yd.js → index-dasa0kbj.js} +3 -3
- package/{index-bxqbt5p3.js → index-ddc2zjj5.js} +1 -1
- package/{index-g7e58wz2.js → index-dz7z9n9t.js} +25 -25
- package/{index-z7t6neq1.js → index-e4vna9dn.js} +4 -4
- package/{index-669sg3yp.js → index-ean2tff1.js} +723 -691
- package/{index-ttfz2sc7.js → index-fgp04fh7.js} +6 -6
- package/{index-bzdq8wfw.js → index-g541mhzw.js} +3 -3
- package/{index-t329y8am.js → index-gh741qft.js} +8 -8
- package/{index-d2r762fk.js → index-k51718xz.js} +2136 -2175
- package/{index-39byv35b.js → index-m6r3pvf5.js} +2 -2
- package/{index-emf3wzvh.js → index-my17308p.js} +7 -7
- package/{index-xywq95wk.js → index-nd8bq17n.js} +6 -6
- package/{index-q4e18ne4.js → index-nxcmgqk3.js} +3 -3
- package/{index-b2sptt87.js → index-pkgha3rt.js} +5 -5
- package/{index-s75h0455.js → index-q2t2w996.js} +1 -1
- package/{index-6p02be7n.js → index-qtqhsb33.js} +204 -160
- package/{index-h7yd92c8.js → index-rj2n5kfk.js} +67 -67
- package/{index-1ts8vfsk.js → index-s5gvwvkb.js} +103 -138
- package/{index-neassptx.js → index-sh3yzse2.js} +8 -7
- package/{index-f482h2hj.js → index-syn4vm3y.js} +1 -1
- package/{index-n8mxyy3f.js → index-t00xrkey.js} +1 -1
- package/{index-g56xae1n.js → index-v72mh89f.js} +7 -8
- package/{index-q57rsax4.js → index-wq8pnev4.js} +3 -3
- package/{index-dp7sht3v.js → index-wxr3941g.js} +8 -8
- package/{index-0c8zj1da.js → index-y8mqkhcf.js} +1 -1
- package/{index-9mx3b3zh.js → index-zk1gjk46.js} +4 -4
- package/index.js +27 -27
- package/{libExecutor-hxe5eqfh.js → libExecutor-mr3vxtnr.js} +2 -2
- package/{library.command-83shs8fw.js → library.command-ps9q5t37.js} +7 -7
- package/{localRegistry.command-sz713jg3.js → localRegistry.command-rszxmvd0.js} +21 -21
- package/{module.command-jhhm0nmw.js → module.command-fcybnn1a.js} +10 -10
- package/{package.command-42syy96v.js → package.command-zen3rhye.js} +10 -10
- package/package.json +2 -2
- package/{page.command-jkv29zzs.js → page.command-gwrxymms.js} +7 -7
- package/{pipeline.command-6sg9g4hq.js → pipeline.command-m1brkqqw.js} +6 -6
- package/{pkgExecutor-6p82w5y1.js → pkgExecutor-x51agavp.js} +2 -2
- package/{primitive.command-tt6aq7m9.js → primitive.command-5vkxvvmk.js} +11 -11
- package/{projectTypecheck-wfrz0cxr.js → projectTypecheck-9dzfah37.js} +1 -1
- package/{quality.command-tqar7ss5.js → quality.command-gqry7eas.js} +9 -9
- package/{repair.command-qkcn329t.js → repair.command-qs4ejcrg.js} +10 -10
- package/{routeSourceValidator-8aph1dsr.js → routeSourceValidator-8n6j1vn7.js} +3 -1
- package/{scalar.command-6nvq11ag.js → scalar.command-ny0vchqq.js} +9 -9
- package/skills/akan-scaffold/SKILL.md +1 -1
- package/{subspace.command-dvwds4cv.js → subspace.command-5pxqjgmw.js} +12 -12
- package/templates/biomeForm.ts +1 -1
- package/templates/env/_env.server.type.ts +1 -1
- package/templates/facetIndex/index.ts +2 -37
- package/templates/workspaceRoot/AGENTS.md.template +2 -1
- package/{tunnel.command-h8r69a7v.js → tunnel.command-ndajt0xk.js} +11 -11
- package/{verify.command-ynec8kt2.js → verify.command-m59ashc7.js} +17 -17
- package/{workflow.command-f70fx73a.js → workflow.command-jchqey88.js} +15 -15
- package/{workspace.command-vagjy1q9.js → workspace.command-g0gzxkpd.js} +36 -36
- package/TunnelShare-6t4rwyc0.js +0 -19
- package/cloud.script-0hbhm3mw.js +0 -35
- package/context.runner-vb7jc0k8.js +0 -34
- package/index-dqsc1b5t.js +0 -18
|
@@ -3,18 +3,21 @@ import {
|
|
|
3
3
|
HmrWatcher,
|
|
4
4
|
WatchRootResolver,
|
|
5
5
|
ApplicationBuildReporter
|
|
6
|
-
} from "./index-
|
|
6
|
+
} from "./index-s5gvwvkb.js";
|
|
7
7
|
import {
|
|
8
8
|
CodegenLock,
|
|
9
9
|
WorkspaceExecutor
|
|
10
|
-
} from "./index-
|
|
10
|
+
} from "./index-v72mh89f.js";
|
|
11
11
|
import"./index-by2tvsq6.js";
|
|
12
12
|
import"./index-j4yvgfhr.js";
|
|
13
13
|
import {
|
|
14
14
|
AppExecutor2
|
|
15
|
-
} from "./index-
|
|
16
|
-
import"./index-
|
|
17
|
-
import"./index-
|
|
15
|
+
} from "./index-cet2s7xk.js";
|
|
16
|
+
import"./index-t00xrkey.js";
|
|
17
|
+
import"./index-cc9pevta.js";
|
|
18
|
+
import {
|
|
19
|
+
ServerGraphFile
|
|
20
|
+
} from "./index-rj2n5kfk.js";
|
|
18
21
|
import {
|
|
19
22
|
GraphClientEntryDiscovery,
|
|
20
23
|
RouteClientBuilder,
|
|
@@ -24,30 +27,25 @@ import {
|
|
|
24
27
|
DevChangePlanner,
|
|
25
28
|
DevGeneratedIndexSync,
|
|
26
29
|
SsrDevBundler
|
|
27
|
-
} from "./index-
|
|
28
|
-
import
|
|
29
|
-
ServerGraphFile
|
|
30
|
-
} from "./index-h7yd92c8.js";
|
|
31
|
-
import"./index-nd39rssn.js";
|
|
30
|
+
} from "./index-k51718xz.js";
|
|
31
|
+
import"./index-7jr9mfp2.js";
|
|
32
32
|
import {
|
|
33
33
|
FrameworkGraph
|
|
34
|
-
} from "./index-
|
|
34
|
+
} from "./index-q2t2w996.js";
|
|
35
35
|
import {
|
|
36
36
|
GitRevision
|
|
37
37
|
} from "./index-zyb0ebm4.js";
|
|
38
38
|
import {
|
|
39
39
|
WorkspaceGraph
|
|
40
|
-
} from "./index-
|
|
40
|
+
} from "./index-81qt1jyj.js";
|
|
41
41
|
import {
|
|
42
42
|
prepareDevWatchBatch
|
|
43
43
|
} from "./index-a5vmm6x4.js";
|
|
44
|
-
import"./index-
|
|
44
|
+
import"./index-4zqebbqb.js";
|
|
45
45
|
|
|
46
46
|
// pkgs/@akanjs/devkit/incrementalBuilder/incrementalBuilder.proc.ts
|
|
47
|
-
import
|
|
48
|
-
import path2 from "path";
|
|
47
|
+
import path6 from "path";
|
|
49
48
|
import { Logger as Logger2 } from "akanjs/common";
|
|
50
|
-
import { CSR_DEV_PATCHING_MARKER } from "akanjs/server/hmr/csrDevManifest";
|
|
51
49
|
|
|
52
50
|
// pkgs/@akanjs/devkit/incrementalBuilder/buildBatchRunner.ts
|
|
53
51
|
import path from "path";
|
|
@@ -92,12 +90,13 @@ class BuildBatchRunner {
|
|
|
92
90
|
env: process.env,
|
|
93
91
|
stdio: ["ignore", "inherit", "inherit"],
|
|
94
92
|
serialization: "advanced",
|
|
95
|
-
ipc: (message) => {
|
|
93
|
+
ipc: (message, worker) => {
|
|
96
94
|
if (!message || typeof message !== "object")
|
|
97
95
|
return;
|
|
98
|
-
if (message.type === "build-batch-result")
|
|
96
|
+
if (message.type === "build-batch-result") {
|
|
99
97
|
result = message.data;
|
|
100
|
-
|
|
98
|
+
worker.disconnect();
|
|
99
|
+
} else {
|
|
101
100
|
if (message.type === "build-status")
|
|
102
101
|
reported.set(message.data.phase, message.data.message);
|
|
103
102
|
onMessage(message);
|
|
@@ -122,6 +121,9 @@ class BuildBatchRunner {
|
|
|
122
121
|
}
|
|
123
122
|
}
|
|
124
123
|
|
|
124
|
+
// pkgs/@akanjs/devkit/incrementalBuilder/builderBoot.ts
|
|
125
|
+
import path2 from "path";
|
|
126
|
+
|
|
125
127
|
// pkgs/@akanjs/devkit/incrementalBuilder/builderChannel.ts
|
|
126
128
|
class BuilderChannel {
|
|
127
129
|
static #flushTimeoutMs = 5000;
|
|
@@ -144,6 +146,15 @@ class BuilderChannel {
|
|
|
144
146
|
static emit(message) {
|
|
145
147
|
BuilderChannel.send(message);
|
|
146
148
|
}
|
|
149
|
+
static async announceReady(logger) {
|
|
150
|
+
const gate = process.env.AKAN_DEV_BUILDER_READY_GATE;
|
|
151
|
+
if (gate && !await Bun.file(gate).exists()) {
|
|
152
|
+
logger.warn(`AKAN_DEV_BUILDER_READY_GATE is set: holding builder-ready until ${gate} exists`);
|
|
153
|
+
while (!await Bun.file(gate).exists())
|
|
154
|
+
await Bun.sleep(100);
|
|
155
|
+
}
|
|
156
|
+
BuilderChannel.emit({ type: "builder-ready" });
|
|
157
|
+
}
|
|
147
158
|
static async drain() {
|
|
148
159
|
let flushed = 0;
|
|
149
160
|
while (BuilderChannel.#flushing.size) {
|
|
@@ -155,6 +166,93 @@ class BuilderChannel {
|
|
|
155
166
|
}
|
|
156
167
|
}
|
|
157
168
|
|
|
169
|
+
// pkgs/@akanjs/devkit/incrementalBuilder/builderBoot.ts
|
|
170
|
+
class BuilderBoot {
|
|
171
|
+
static async buildBootDeps(app, runner) {
|
|
172
|
+
const result = await runner.run({
|
|
173
|
+
appName: app.name,
|
|
174
|
+
workspaceRoot: app.workspace.workspaceRoot,
|
|
175
|
+
repoName: app.workspace.repoName,
|
|
176
|
+
generation: 0,
|
|
177
|
+
needs: ["base"],
|
|
178
|
+
changedFiles: [],
|
|
179
|
+
pageKeys: null,
|
|
180
|
+
optimizedFonts: null,
|
|
181
|
+
cssAssets: null,
|
|
182
|
+
artifactDir: path2.resolve(`${app.cwdPath}/.akan/artifact`)
|
|
183
|
+
});
|
|
184
|
+
if (result.errors.base)
|
|
185
|
+
throw new Error(result.errors.base);
|
|
186
|
+
if (!result.artifact || !result.optimizedFonts)
|
|
187
|
+
throw new Error("boot build reported success without an artifact; the build worker likely died");
|
|
188
|
+
const discovery = await GraphClientEntryDiscovery.create(app);
|
|
189
|
+
return { artifact: result.artifact, optimizedFonts: result.optimizedFonts, discovery };
|
|
190
|
+
}
|
|
191
|
+
static recoverBoot(app, bootError, logger, runner, initialGeneration, create) {
|
|
192
|
+
const firstMessage = bootError instanceof Error ? bootError.message : String(bootError);
|
|
193
|
+
logger.error(`boot build failed; entering degraded watch mode until the error is fixed: ${firstMessage}`);
|
|
194
|
+
let generation = initialGeneration;
|
|
195
|
+
const sendFailure = (files, message) => {
|
|
196
|
+
BuilderChannel.emit({
|
|
197
|
+
type: "build-status",
|
|
198
|
+
data: { generation, phase: "pages", ok: false, files, message: `Boot build failed: ${message}` }
|
|
199
|
+
});
|
|
200
|
+
};
|
|
201
|
+
sendFailure([], firstMessage);
|
|
202
|
+
BuilderChannel.emit({ type: "builder-ready" });
|
|
203
|
+
BuilderChannel.emit({ type: "boot-armed" });
|
|
204
|
+
return new Promise((resolve, reject) => {
|
|
205
|
+
(async () => {
|
|
206
|
+
const roots = await new WatchRootResolver(app).resolve();
|
|
207
|
+
const watcher = new HmrWatcher({
|
|
208
|
+
roots,
|
|
209
|
+
logger,
|
|
210
|
+
onBatch: async (batch) => {
|
|
211
|
+
generation += 1;
|
|
212
|
+
const files = [...batch.files].sort();
|
|
213
|
+
try {
|
|
214
|
+
if (new Set(batch.kinds).has("config"))
|
|
215
|
+
await app.getConfig({ refresh: true });
|
|
216
|
+
const deps = await BuilderBoot.buildBootDeps(app, runner);
|
|
217
|
+
const builder = create({ app, watch: true, initialGeneration: generation, ...deps });
|
|
218
|
+
watcher.stop();
|
|
219
|
+
logger.info(`boot build recovered generation=${generation}`);
|
|
220
|
+
resolve({ builder, changedFiles: files });
|
|
221
|
+
} catch (err) {
|
|
222
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
223
|
+
logger.error(`boot build retry failed: ${message}`);
|
|
224
|
+
sendFailure(files, message);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
await watcher.start();
|
|
229
|
+
logger.warn(`[degraded] watching ${roots.length} roots for a fix`);
|
|
230
|
+
})().catch(reject);
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
static initialGeneration() {
|
|
234
|
+
const generation = Number(process.env.AKAN_BUILDER_INITIAL_GENERATION);
|
|
235
|
+
return Number.isInteger(generation) && generation > 0 ? generation + 1 : 0;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// pkgs/@akanjs/devkit/incrementalBuilder/builderBuildStatus.ts
|
|
240
|
+
var sendBuildStatus = (phase, {
|
|
241
|
+
generation,
|
|
242
|
+
ok,
|
|
243
|
+
files,
|
|
244
|
+
message,
|
|
245
|
+
scope
|
|
246
|
+
}) => {
|
|
247
|
+
if (typeof generation !== "number")
|
|
248
|
+
return;
|
|
249
|
+
BuilderChannel.emit({
|
|
250
|
+
type: "build-status",
|
|
251
|
+
data: { generation, phase, ok, files: files ?? [], message, ...scope !== undefined ? { scope } : {} }
|
|
252
|
+
});
|
|
253
|
+
};
|
|
254
|
+
var isCompileError = (error) => error instanceof AggregateError;
|
|
255
|
+
|
|
158
256
|
// pkgs/@akanjs/devkit/incrementalBuilder/builderGraphPayloads.ts
|
|
159
257
|
class BuilderGraphPayloads {
|
|
160
258
|
#app;
|
|
@@ -292,43 +390,12 @@ class BuilderWorkQueue {
|
|
|
292
390
|
}
|
|
293
391
|
}
|
|
294
392
|
|
|
295
|
-
// pkgs/@akanjs/devkit/incrementalBuilder/
|
|
296
|
-
class
|
|
297
|
-
#logger = new Logger2("IncrementalBuilder");
|
|
393
|
+
// pkgs/@akanjs/devkit/incrementalBuilder/builderLanes.ts
|
|
394
|
+
class BuilderLanes {
|
|
298
395
|
#app;
|
|
299
|
-
#
|
|
300
|
-
#
|
|
301
|
-
#optimizedFonts;
|
|
302
|
-
#discovery;
|
|
303
|
-
#batchRunner;
|
|
304
|
-
#changePlanner;
|
|
305
|
-
#generatedIndexSync;
|
|
306
|
-
#autoImportSync;
|
|
307
|
-
#watcher = null;
|
|
308
|
-
#generation = 0;
|
|
309
|
-
#shellKey = null;
|
|
310
|
-
#graphPayloads;
|
|
311
|
-
#discoveredThrough = 0;
|
|
312
|
-
#csrActive = IncrementalBuilder.#csrArmedByEnv();
|
|
313
|
-
#csrBundler;
|
|
314
|
-
#patcher;
|
|
315
|
-
#csrGate = Promise.resolve();
|
|
316
|
-
#csrArming = false;
|
|
317
|
-
#csrArms = 0;
|
|
318
|
-
#crashRetryTimer = null;
|
|
319
|
-
#crashRetryEntries = new Set;
|
|
320
|
-
#csrWasActive = false;
|
|
321
|
-
#csrArmSucceeded = false;
|
|
322
|
-
#ssrBundler;
|
|
323
|
-
#ssrPatcher;
|
|
324
|
-
#ssrLock = Promise.resolve();
|
|
325
|
-
#delegations = new Map;
|
|
326
|
-
#ssrArming = null;
|
|
327
|
-
#ssrBroken = false;
|
|
328
|
-
#ssrCrashedAt = null;
|
|
329
|
-
static #crashRetryMs = 1e4;
|
|
396
|
+
#logger;
|
|
397
|
+
#links;
|
|
330
398
|
#beside = new Set;
|
|
331
|
-
#barrelFailed = false;
|
|
332
399
|
#fastQueue = Promise.resolve();
|
|
333
400
|
#workQueue;
|
|
334
401
|
#inFlight = 0;
|
|
@@ -337,28 +404,12 @@ class IncrementalBuilder {
|
|
|
337
404
|
#cssRebuildQueue = Promise.resolve();
|
|
338
405
|
#cssRebuildTimer = null;
|
|
339
406
|
#pendingCssRebuild = null;
|
|
340
|
-
constructor(
|
|
341
|
-
this.#app =
|
|
342
|
-
this.#
|
|
343
|
-
this.#
|
|
344
|
-
this.#optimizedFonts = options.optimizedFonts;
|
|
345
|
-
this.#discovery = options.discovery;
|
|
346
|
-
this.#generation = options.initialGeneration ?? 0;
|
|
347
|
-
this.#discoveredThrough = this.#generation;
|
|
348
|
-
this.#graphPayloads = new BuilderGraphPayloads(options.app, () => this.#generation);
|
|
349
|
-
this.#batchRunner = new BuildBatchRunner({
|
|
350
|
-
workspaceRoot: options.app.workspace.workspaceRoot,
|
|
351
|
-
cwd: options.app.cwdPath
|
|
352
|
-
});
|
|
353
|
-
this.#changePlanner = new DevChangePlanner({ workspaceRoot: options.app.workspace.workspaceRoot });
|
|
354
|
-
this.#generatedIndexSync = new DevGeneratedIndexSync({ workspaceRoot: options.app.workspace.workspaceRoot });
|
|
355
|
-
this.#autoImportSync = new AutoImportSync({ workspaceRoot: options.app.workspace.workspaceRoot });
|
|
356
|
-
this.#csrBundler = new CsrDevBundler(options.app);
|
|
357
|
-
this.#patcher = process.env.AKAN_DEV_CSR_PATCHER === "off" ? null : new CsrDevPatcher(this.#csrBundler, { resident: true });
|
|
358
|
-
this.#ssrBundler = new SsrDevBundler(options.app, { discovery: () => this.#discovery });
|
|
359
|
-
this.#ssrPatcher = process.env.AKAN_DEV_CSR_PATCHER === "off" ? null : new CsrDevPatcher(this.#ssrBundler, { resident: true });
|
|
407
|
+
constructor(app, { logger }, links) {
|
|
408
|
+
this.#app = app;
|
|
409
|
+
this.#logger = logger;
|
|
410
|
+
this.#links = links;
|
|
360
411
|
this.#workQueue = new BuilderWorkQueue({
|
|
361
|
-
runBatch: async (batch) => await this.#runQueuedBatch(batch),
|
|
412
|
+
runBatch: async (batch) => await this.#links.runQueuedBatch(batch),
|
|
362
413
|
onSettled: (label, ms) => {
|
|
363
414
|
this.#workCount += 1;
|
|
364
415
|
this.#logger.verbose(`[work-queue] ${label} finished in ${ms}ms`);
|
|
@@ -366,72 +417,19 @@ class IncrementalBuilder {
|
|
|
366
417
|
}
|
|
367
418
|
});
|
|
368
419
|
}
|
|
369
|
-
get
|
|
370
|
-
return
|
|
420
|
+
get workQueue() {
|
|
421
|
+
return this.#workQueue;
|
|
371
422
|
}
|
|
372
|
-
|
|
373
|
-
|
|
423
|
+
get inFlight() {
|
|
424
|
+
return this.#inFlight;
|
|
374
425
|
}
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
try {
|
|
378
|
-
const delta = await new RouteClientBuilder({
|
|
379
|
-
app: this.#app,
|
|
380
|
-
routeId: msg.routeId,
|
|
381
|
-
seeds: msg.seeds,
|
|
382
|
-
graphSeeds: msg.graphSeeds,
|
|
383
|
-
artifact: this.#artifact,
|
|
384
|
-
knownEntries: new Set(msg.knownEntries),
|
|
385
|
-
discovery: this.#discovery,
|
|
386
|
-
browser: "registry"
|
|
387
|
-
}).build();
|
|
388
|
-
await this.#ensureSsrEntries(delta.registryEntries ?? []);
|
|
389
|
-
this.#logger.verbose(`build-route ok routeId=${msg.routeId} newEntries=${delta.newEntries.length}`);
|
|
390
|
-
this.#sendBuildStatus("route", { generation: this.#generation, ok: true, files: msg.seeds, scope: msg.routeId });
|
|
391
|
-
return {
|
|
392
|
-
type: "build-route-res",
|
|
393
|
-
id: msg.id,
|
|
394
|
-
ok: true,
|
|
395
|
-
data: {
|
|
396
|
-
manifestDelta: delta.manifestDelta,
|
|
397
|
-
ssrManifestDelta: delta.ssrManifestDelta.moduleMap,
|
|
398
|
-
newEntries: delta.newEntries,
|
|
399
|
-
discoveredEntries: delta.discoveredEntries,
|
|
400
|
-
clientDeps: delta.clientDeps,
|
|
401
|
-
clientDepsByEntry: delta.clientDepsByEntry,
|
|
402
|
-
routeId: msg.routeId,
|
|
403
|
-
generation: msg.generation,
|
|
404
|
-
seenGeneration
|
|
405
|
-
}
|
|
406
|
-
};
|
|
407
|
-
} catch (err) {
|
|
408
|
-
const errMsg = ApplicationBuildReporter.formatError(err, this.#app.workspace.workspaceRoot);
|
|
409
|
-
this.#logger.error(`build-route failed routeId=${msg.routeId}: ${errMsg}`);
|
|
410
|
-
this.#sendBuildStatus("route", {
|
|
411
|
-
generation: this.#generation,
|
|
412
|
-
ok: false,
|
|
413
|
-
files: msg.seeds,
|
|
414
|
-
message: errMsg,
|
|
415
|
-
scope: msg.routeId
|
|
416
|
-
});
|
|
417
|
-
return { type: "build-route-res", id: msg.id, ok: false, error: errMsg };
|
|
418
|
-
}
|
|
426
|
+
get shuttingDown() {
|
|
427
|
+
return this.#shuttingDown;
|
|
419
428
|
}
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
ok,
|
|
423
|
-
files,
|
|
424
|
-
message,
|
|
425
|
-
scope
|
|
426
|
-
}) {
|
|
427
|
-
if (typeof generation !== "number")
|
|
428
|
-
return;
|
|
429
|
-
BuilderChannel.emit({
|
|
430
|
-
type: "build-status",
|
|
431
|
-
data: { generation, phase, ok, files: files ?? [], message, ...scope !== undefined ? { scope } : {} }
|
|
432
|
-
});
|
|
429
|
+
markShuttingDown() {
|
|
430
|
+
this.#shuttingDown = true;
|
|
433
431
|
}
|
|
434
|
-
async
|
|
432
|
+
async enqueueFast(label, fn) {
|
|
435
433
|
const started = Date.now();
|
|
436
434
|
this.#inFlight += 1;
|
|
437
435
|
const run = this.#fastQueue.then(fn, fn);
|
|
@@ -452,7 +450,7 @@ class IncrementalBuilder {
|
|
|
452
450
|
get #idle() {
|
|
453
451
|
return this.#inFlight === 0 && this.#workQueue.size === 0 && this.#cssRebuildTimer === null;
|
|
454
452
|
}
|
|
455
|
-
|
|
453
|
+
runBeside(work) {
|
|
456
454
|
this.#inFlight += 1;
|
|
457
455
|
const tracked = work.finally(() => {
|
|
458
456
|
this.#inFlight -= 1;
|
|
@@ -467,16 +465,10 @@ class IncrementalBuilder {
|
|
|
467
465
|
return;
|
|
468
466
|
BuilderChannel.emit({
|
|
469
467
|
type: "builder-metrics",
|
|
470
|
-
data: { rssBytes: process.memoryUsage.rss(), generation: this.#generation, workCount: this.#workCount }
|
|
468
|
+
data: { rssBytes: process.memoryUsage.rss(), generation: this.#links.generation(), workCount: this.#workCount }
|
|
471
469
|
});
|
|
472
470
|
}
|
|
473
|
-
async
|
|
474
|
-
if (this.#shuttingDown)
|
|
475
|
-
return;
|
|
476
|
-
this.#shuttingDown = true;
|
|
477
|
-
const started = Date.now();
|
|
478
|
-
this.#logger.debug(`shutdown requested (${reason}); draining ${this.#inFlight} work item(s)`);
|
|
479
|
-
this.#watcher?.stop();
|
|
471
|
+
async drain() {
|
|
480
472
|
if (this.#cssRebuildTimer) {
|
|
481
473
|
clearTimeout(this.#cssRebuildTimer);
|
|
482
474
|
this.#cssRebuildTimer = null;
|
|
@@ -491,47 +483,9 @@ class IncrementalBuilder {
|
|
|
491
483
|
});
|
|
492
484
|
while (this.#beside.size > 0)
|
|
493
485
|
await Promise.all([...this.#beside]);
|
|
494
|
-
if (this.#delegations.size > 0)
|
|
495
|
-
this.#logger.verbose(`registry saves handed to build workers this session: ${[...this.#delegations].map(([reason, count]) => `${reason}=${count}`).join(", ")}`);
|
|
496
|
-
const flushed = await BuilderChannel.drain();
|
|
497
|
-
this.#logger.debug(`drained in ${Date.now() - started}ms${flushed ? ` after flushing ${flushed} ipc write(s)` : ""}; exiting for recycle`);
|
|
498
|
-
process.exit(0);
|
|
499
|
-
}
|
|
500
|
-
get shuttingDown() {
|
|
501
|
-
return this.#shuttingDown;
|
|
502
486
|
}
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
const absRoot = path2.resolve(root.realDir);
|
|
506
|
-
if (abs === absRoot || abs.startsWith(`${absRoot}${path2.sep}`))
|
|
507
|
-
return root;
|
|
508
|
-
}
|
|
509
|
-
return null;
|
|
510
|
-
}
|
|
511
|
-
batchTouchesPagesTree(roots, batch) {
|
|
512
|
-
for (const f of batch.files) {
|
|
513
|
-
const abs = path2.resolve(f);
|
|
514
|
-
if (!IncrementalBuilder.#matchPageRoot(roots, abs))
|
|
515
|
-
continue;
|
|
516
|
-
if (/\.(tsx|ts|jsx|js)$/.test(abs))
|
|
517
|
-
return true;
|
|
518
|
-
}
|
|
519
|
-
return false;
|
|
520
|
-
}
|
|
521
|
-
async batchMayChangePageKeys(roots, batch) {
|
|
522
|
-
const pageKeys = new Set((await this.#app.getPageKeys()).map((key) => path2.normalize(key)));
|
|
523
|
-
for (const f of batch.files) {
|
|
524
|
-
const abs = path2.resolve(f);
|
|
525
|
-
const root = IncrementalBuilder.#matchPageRoot(roots, abs);
|
|
526
|
-
if (!root)
|
|
527
|
-
continue;
|
|
528
|
-
if (!/\.(tsx|ts|jsx|js)$/.test(abs))
|
|
529
|
-
continue;
|
|
530
|
-
const rel = path2.normalize(`${root.keyPrefix}${path2.relative(path2.resolve(root.realDir), abs)}`);
|
|
531
|
-
if (!await Bun.file(abs).exists() || !pageKeys.has(rel))
|
|
532
|
-
return true;
|
|
533
|
-
}
|
|
534
|
-
return false;
|
|
487
|
+
slowLaneFailed(label) {
|
|
488
|
+
return (error) => this.#logger.error(`${label} failed in the slow lane: ${ApplicationBuildReporter.formatError(error, this.#app.workspace.workspaceRoot)}`);
|
|
535
489
|
}
|
|
536
490
|
scheduleCssRebuild({ generation, changedFiles }) {
|
|
537
491
|
this.#pendingCssRebuild = { generation, changedFiles };
|
|
@@ -545,15 +499,15 @@ class IncrementalBuilder {
|
|
|
545
499
|
return;
|
|
546
500
|
this.#inFlight += 1;
|
|
547
501
|
this.#cssRebuildQueue = this.#cssRebuildQueue.then(async () => {
|
|
548
|
-
await this.#runBatch({
|
|
549
|
-
generation: next.generation ?? this.#generation,
|
|
502
|
+
await this.#links.runBatch({
|
|
503
|
+
generation: next.generation ?? this.#links.generation(),
|
|
550
504
|
needs: ["css"],
|
|
551
505
|
changedFiles: next.changedFiles ?? []
|
|
552
506
|
});
|
|
553
507
|
}).catch((err) => {
|
|
554
508
|
const message = err instanceof Error ? err.message : String(err);
|
|
555
509
|
this.#logger.error(`css-rebuild failed: ${message}`);
|
|
556
|
-
|
|
510
|
+
sendBuildStatus("css", {
|
|
557
511
|
generation: next.generation,
|
|
558
512
|
ok: false,
|
|
559
513
|
files: next.changedFiles,
|
|
@@ -565,13 +519,78 @@ class IncrementalBuilder {
|
|
|
565
519
|
});
|
|
566
520
|
}, 150);
|
|
567
521
|
}
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
// pkgs/@akanjs/devkit/incrementalBuilder/builderWatch.ts
|
|
525
|
+
import path3 from "path";
|
|
526
|
+
class BuilderWatch {
|
|
527
|
+
#app;
|
|
528
|
+
#logger;
|
|
529
|
+
#lanes;
|
|
530
|
+
#csr;
|
|
531
|
+
#ssr;
|
|
532
|
+
#graphPayloads;
|
|
533
|
+
#links;
|
|
534
|
+
#changePlanner;
|
|
535
|
+
#generatedIndexSync;
|
|
536
|
+
#autoImportSync;
|
|
537
|
+
#watcher = null;
|
|
538
|
+
#barrelFailed = false;
|
|
539
|
+
constructor(app, { logger, lanes, csr, ssr, graphPayloads }, links) {
|
|
540
|
+
this.#app = app;
|
|
541
|
+
this.#logger = logger;
|
|
542
|
+
this.#lanes = lanes;
|
|
543
|
+
this.#csr = csr;
|
|
544
|
+
this.#ssr = ssr;
|
|
545
|
+
this.#graphPayloads = graphPayloads;
|
|
546
|
+
this.#links = links;
|
|
547
|
+
this.#changePlanner = new DevChangePlanner({ workspaceRoot: app.workspace.workspaceRoot });
|
|
548
|
+
this.#generatedIndexSync = new DevGeneratedIndexSync({ workspaceRoot: app.workspace.workspaceRoot });
|
|
549
|
+
this.#autoImportSync = new AutoImportSync({ workspaceRoot: app.workspace.workspaceRoot });
|
|
550
|
+
}
|
|
551
|
+
stop() {
|
|
552
|
+
this.#watcher?.stop();
|
|
553
|
+
}
|
|
554
|
+
static #matchPageRoot(roots, abs) {
|
|
555
|
+
for (const root of roots) {
|
|
556
|
+
const absRoot = path3.resolve(root.realDir);
|
|
557
|
+
if (abs === absRoot || abs.startsWith(`${absRoot}${path3.sep}`))
|
|
558
|
+
return root;
|
|
559
|
+
}
|
|
560
|
+
return null;
|
|
561
|
+
}
|
|
562
|
+
batchTouchesPagesTree(roots, batch) {
|
|
563
|
+
for (const f of batch.files) {
|
|
564
|
+
const abs = path3.resolve(f);
|
|
565
|
+
if (!BuilderWatch.#matchPageRoot(roots, abs))
|
|
566
|
+
continue;
|
|
567
|
+
if (/\.(tsx|ts|jsx|js)$/.test(abs))
|
|
568
|
+
return true;
|
|
569
|
+
}
|
|
570
|
+
return false;
|
|
571
|
+
}
|
|
572
|
+
async batchMayChangePageKeys(roots, batch) {
|
|
573
|
+
const pageKeys = new Set((await this.#app.getPageKeys()).map((key) => path3.normalize(key)));
|
|
574
|
+
for (const f of batch.files) {
|
|
575
|
+
const abs = path3.resolve(f);
|
|
576
|
+
const root = BuilderWatch.#matchPageRoot(roots, abs);
|
|
577
|
+
if (!root)
|
|
578
|
+
continue;
|
|
579
|
+
if (!/\.(tsx|ts|jsx|js)$/.test(abs))
|
|
580
|
+
continue;
|
|
581
|
+
const rel = path3.normalize(`${root.keyPrefix}${path3.relative(path3.resolve(root.realDir), abs)}`);
|
|
582
|
+
if (!await Bun.file(abs).exists() || !pageKeys.has(rel))
|
|
583
|
+
return true;
|
|
584
|
+
}
|
|
585
|
+
return false;
|
|
586
|
+
}
|
|
568
587
|
async installWatcher() {
|
|
569
588
|
const roots = await new WatchRootResolver(this.#app).resolve();
|
|
570
589
|
const watcher = new HmrWatcher({
|
|
571
590
|
roots,
|
|
572
591
|
logger: this.#logger,
|
|
573
592
|
onBatch: async (batch) => {
|
|
574
|
-
await this.#enqueueFast("hmr-batch", async () => this.#handleWatchBatch(batch));
|
|
593
|
+
await this.#lanes.enqueueFast("hmr-batch", async () => this.#handleWatchBatch(batch));
|
|
575
594
|
}
|
|
576
595
|
});
|
|
577
596
|
await watcher.start();
|
|
@@ -583,7 +602,7 @@ class IncrementalBuilder {
|
|
|
583
602
|
const rawKinds = new Set(batch.kinds);
|
|
584
603
|
if (rawKinds.size === 0)
|
|
585
604
|
return;
|
|
586
|
-
const generation =
|
|
605
|
+
const generation = this.#links.nextGeneration();
|
|
587
606
|
const trace = { ...batch.trace, batchAt: Date.now() };
|
|
588
607
|
const [autoImport, indexSync] = await CodegenLock.run(this.#app.workspace.workspaceRoot, `hmr-batch:${this.#app.name}`, async () => {
|
|
589
608
|
const auto = await this.#autoImportSync.syncForBatch(batch.files);
|
|
@@ -606,15 +625,15 @@ class IncrementalBuilder {
|
|
|
606
625
|
const discovery = kinds.includes("code") ? { files, refresh: kinds.includes("config"), generation } : undefined;
|
|
607
626
|
if (hasSyncErrors) {
|
|
608
627
|
this.#barrelFailed = true;
|
|
609
|
-
|
|
628
|
+
sendBuildStatus("barrel", { generation, ok: false, files, message: indexSync.errors.join(`
|
|
610
629
|
`) });
|
|
611
630
|
BuilderChannel.emit(event);
|
|
612
631
|
if (discovery)
|
|
613
|
-
this.#workQueue.enqueue("discovery", async () => await this.#refreshDiscovery(discovery)).catch(this.#slowLaneFailed("discovery"));
|
|
632
|
+
this.#lanes.workQueue.enqueue("discovery", async () => await this.#links.refreshDiscovery(discovery)).catch(this.#lanes.slowLaneFailed("discovery"));
|
|
614
633
|
return;
|
|
615
634
|
}
|
|
616
635
|
if (indexSync.changedFiles.length > 0 || this.#barrelFailed)
|
|
617
|
-
|
|
636
|
+
sendBuildStatus("barrel", { generation, ok: true, files });
|
|
618
637
|
this.#barrelFailed = false;
|
|
619
638
|
const rebuildClient = devPlan.actions.includes("rebuild-client");
|
|
620
639
|
if (kinds.includes("code") && !rebuildClient) {
|
|
@@ -631,13 +650,13 @@ class IncrementalBuilder {
|
|
|
631
650
|
BuilderChannel.emit(event);
|
|
632
651
|
if (kinds.includes("code") && rebuildClient) {
|
|
633
652
|
const needs = ["pages", "css"];
|
|
634
|
-
if (!this.#csrActive)
|
|
653
|
+
if (!this.#csr.csrActive)
|
|
635
654
|
this.#logger.verbose(`csr-rebundle skipped; request /__csr or ?csr=true (or set AKAN_DEV_CSR_REBUILD=1) to enable per-save CSR rebuilds`);
|
|
636
|
-
const patchCsr = async () => this.#csrActive ? await this.#patchCsr(generation, files, trace) : false;
|
|
637
|
-
const patchSsr = async () => await this.#patchSsr(generation, files, trace);
|
|
655
|
+
const patchCsr = async () => this.#csr.csrActive ? await this.#csr.patchCsr(generation, files, trace) : false;
|
|
656
|
+
const patchSsr = async () => await this.#ssr.patchSsr(generation, files, trace);
|
|
638
657
|
let csrToWorker;
|
|
639
658
|
let ssrToWorker;
|
|
640
|
-
if (this.#csrArming) {
|
|
659
|
+
if (this.#csr.csrArming) {
|
|
641
660
|
ssrToWorker = await patchSsr();
|
|
642
661
|
csrToWorker = await patchCsr();
|
|
643
662
|
} else {
|
|
@@ -650,27 +669,66 @@ class IncrementalBuilder {
|
|
|
650
669
|
needs.unshift("ssr");
|
|
651
670
|
const batch = { generation, needs, changedFiles: files, trace, ...discovery ? { discovery } : {} };
|
|
652
671
|
if (needs.includes("csr") || needs.includes("ssr"))
|
|
653
|
-
await this.#workQueue.enqueueBatch(batch);
|
|
672
|
+
await this.#lanes.workQueue.enqueueBatch(batch);
|
|
654
673
|
else
|
|
655
|
-
this.#workQueue.enqueueBatch(batch).catch(this.#slowLaneFailed("batch"));
|
|
674
|
+
this.#lanes.workQueue.enqueueBatch(batch).catch(this.#lanes.slowLaneFailed("batch"));
|
|
656
675
|
return;
|
|
657
676
|
}
|
|
658
677
|
if (discovery)
|
|
659
|
-
this.#workQueue.enqueue("discovery", async () => await this.#refreshDiscovery(discovery)).catch(this.#slowLaneFailed("discovery"));
|
|
678
|
+
this.#lanes.workQueue.enqueue("discovery", async () => await this.#links.refreshDiscovery(discovery)).catch(this.#lanes.slowLaneFailed("discovery"));
|
|
660
679
|
if (kinds.includes("css")) {
|
|
661
|
-
this.scheduleCssRebuild({ generation, changedFiles: files });
|
|
680
|
+
this.#lanes.scheduleCssRebuild({ generation, changedFiles: files });
|
|
662
681
|
this.#logger.verbose(`css-rebuild scheduled generation=${generation}`);
|
|
663
682
|
}
|
|
664
683
|
}
|
|
665
|
-
|
|
666
|
-
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
// pkgs/@akanjs/devkit/incrementalBuilder/csrRegistry.ts
|
|
687
|
+
import { rm } from "fs/promises";
|
|
688
|
+
import path4 from "path";
|
|
689
|
+
import { CSR_DEV_PATCHING_MARKER } from "akanjs/server/hmr/csrDevManifest";
|
|
690
|
+
class CsrRegistry {
|
|
691
|
+
#app;
|
|
692
|
+
#logger;
|
|
693
|
+
#lanes;
|
|
694
|
+
#batchRunner;
|
|
695
|
+
#links;
|
|
696
|
+
#shellKey = null;
|
|
697
|
+
#csrActive = CsrRegistry.#csrArmedByEnv();
|
|
698
|
+
#csrBundler;
|
|
699
|
+
#patcher;
|
|
700
|
+
#csrGate = Promise.resolve();
|
|
701
|
+
#csrArming = false;
|
|
702
|
+
#csrArms = 0;
|
|
703
|
+
#csrWasActive = false;
|
|
704
|
+
#csrArmSucceeded = false;
|
|
705
|
+
constructor(app, { logger, lanes, batchRunner }, links) {
|
|
706
|
+
this.#app = app;
|
|
707
|
+
this.#logger = logger;
|
|
708
|
+
this.#lanes = lanes;
|
|
709
|
+
this.#batchRunner = batchRunner;
|
|
710
|
+
this.#links = links;
|
|
711
|
+
this.#csrBundler = new CsrDevBundler(app);
|
|
712
|
+
this.#patcher = process.env.AKAN_DEV_CSR_PATCHER === "off" ? null : new CsrDevPatcher(this.#csrBundler, { resident: true });
|
|
713
|
+
}
|
|
714
|
+
get csrActive() {
|
|
715
|
+
return this.#csrActive;
|
|
716
|
+
}
|
|
717
|
+
get csrArming() {
|
|
718
|
+
return this.#csrArming;
|
|
719
|
+
}
|
|
720
|
+
get patcher() {
|
|
721
|
+
return this.#patcher;
|
|
722
|
+
}
|
|
723
|
+
async patchCsr(generation, files, trace) {
|
|
724
|
+
const patcher = this.#patcher;
|
|
667
725
|
if (!patcher)
|
|
668
726
|
return true;
|
|
669
727
|
await this.#csrGate;
|
|
670
728
|
if (!this.#csrActive)
|
|
671
729
|
return false;
|
|
672
730
|
const started = Date.now();
|
|
673
|
-
const marker =
|
|
731
|
+
const marker = path4.join(this.#csrBundler.outDir, CSR_DEV_PATCHING_MARKER);
|
|
674
732
|
await Bun.write(marker, String(process.pid));
|
|
675
733
|
try {
|
|
676
734
|
const result = await patcher.update(files, {
|
|
@@ -691,16 +749,16 @@ class IncrementalBuilder {
|
|
|
691
749
|
}
|
|
692
750
|
});
|
|
693
751
|
if (result.kind === "delegate") {
|
|
694
|
-
this.#logger.verbose(`csr-patch handed to a build worker (${this.#countDelegation("csr", result.reason)}): ${result.reason}`);
|
|
752
|
+
this.#logger.verbose(`csr-patch handed to a build worker (${this.#links.countDelegation("csr", result.reason)}): ${result.reason}`);
|
|
695
753
|
return true;
|
|
696
754
|
}
|
|
697
|
-
|
|
755
|
+
sendBuildStatus("csr", { generation, ok: true, files });
|
|
698
756
|
this.#logger.verbose(result.kind === "unchanged" ? `csr-patch unchanged (${Date.now() - started}ms)` : `csr-patch generation=${result.update.generation} ${result.update.reload ? `reload (${result.update.reason})` : `patch modules=${result.update.changedIds.length}`} (${Date.now() - started}ms)`);
|
|
699
757
|
return false;
|
|
700
758
|
} catch (err) {
|
|
701
759
|
const message = ApplicationBuildReporter.formatError(err, this.#app.workspace.workspaceRoot);
|
|
702
|
-
if (
|
|
703
|
-
|
|
760
|
+
if (isCompileError(err)) {
|
|
761
|
+
sendBuildStatus("csr", { generation, ok: false, files, message });
|
|
704
762
|
return false;
|
|
705
763
|
}
|
|
706
764
|
this.#logger.verbose(`csr-patch threw; a build worker takes this save: ${message}`);
|
|
@@ -710,32 +768,149 @@ class IncrementalBuilder {
|
|
|
710
768
|
await rm(marker, { force: true });
|
|
711
769
|
}
|
|
712
770
|
}
|
|
713
|
-
async
|
|
771
|
+
async handleBuildCsr(msg) {
|
|
772
|
+
if (msg.shell) {
|
|
773
|
+
await this.#buildShell(msg);
|
|
774
|
+
return;
|
|
775
|
+
}
|
|
776
|
+
this.#beginCsrArm();
|
|
777
|
+
this.#csrActive = true;
|
|
778
|
+
const armed = this.#lanes.workQueue.enqueue("build-csr", async () => {
|
|
779
|
+
const started = Date.now();
|
|
780
|
+
let error;
|
|
781
|
+
try {
|
|
782
|
+
const result = await this.#batchRunner.run(await this.#links.batchRequest({ generation: this.#links.generation(), needs: ["csr"], changedFiles: [] }));
|
|
783
|
+
error = result.errors.csr;
|
|
784
|
+
} catch (err) {
|
|
785
|
+
error = ApplicationBuildReporter.formatError(err, this.#app.workspace.workspaceRoot);
|
|
786
|
+
}
|
|
787
|
+
if (error) {
|
|
788
|
+
this.#logger.error(`csr-build failed: ${error}`);
|
|
789
|
+
await BuilderChannel.send({ type: "build-csr-res", id: msg.id, ok: false, error });
|
|
790
|
+
return;
|
|
791
|
+
}
|
|
792
|
+
this.#csrArmSucceeded = true;
|
|
793
|
+
this.#patcher?.forget();
|
|
794
|
+
this.#logger.info(`csr-build ok on demand (${Date.now() - started}ms); rebuilding CSR on every save now`);
|
|
795
|
+
await BuilderChannel.send({ type: "build-csr-res", id: msg.id, ok: true });
|
|
796
|
+
});
|
|
797
|
+
this.#csrGate = this.#armingCsr(armed);
|
|
798
|
+
await armed;
|
|
799
|
+
}
|
|
800
|
+
async#buildShell(msg) {
|
|
801
|
+
await this.#lanes.workQueue.enqueue("build-shell", async () => {
|
|
802
|
+
const generation = this.#links.generation();
|
|
803
|
+
const shellKey = `${generation}:${JSON.stringify(this.#links.artifact().cssAssets)}`;
|
|
804
|
+
let error;
|
|
805
|
+
if (this.#shellKey !== shellKey)
|
|
806
|
+
try {
|
|
807
|
+
const result = await this.#batchRunner.run(await this.#links.batchRequest({ generation, needs: ["shell"], changedFiles: [] }));
|
|
808
|
+
error = result.errors.shell;
|
|
809
|
+
if (!error)
|
|
810
|
+
this.#shellKey = shellKey;
|
|
811
|
+
} catch (err) {
|
|
812
|
+
error = ApplicationBuildReporter.formatError(err, this.#app.workspace.workspaceRoot);
|
|
813
|
+
}
|
|
814
|
+
await BuilderChannel.send(error ? { type: "build-csr-res", id: msg.id, ok: false, error } : { type: "build-csr-res", id: msg.id, ok: true });
|
|
815
|
+
});
|
|
816
|
+
}
|
|
817
|
+
#beginCsrArm() {
|
|
818
|
+
if (this.#csrArms === 0) {
|
|
819
|
+
this.#csrWasActive = this.#csrActive;
|
|
820
|
+
this.#csrArmSucceeded = false;
|
|
821
|
+
}
|
|
822
|
+
this.#csrArms += 1;
|
|
823
|
+
this.#csrArming = true;
|
|
824
|
+
}
|
|
825
|
+
#armingCsr(armed) {
|
|
826
|
+
const settle = () => {
|
|
827
|
+
this.#csrArms -= 1;
|
|
828
|
+
if (this.#csrArms > 0)
|
|
829
|
+
return;
|
|
830
|
+
this.#csrArming = false;
|
|
831
|
+
this.#csrActive = this.#csrWasActive || this.#csrArmSucceeded;
|
|
832
|
+
};
|
|
833
|
+
return armed.then(settle, settle);
|
|
834
|
+
}
|
|
835
|
+
static #csrArmedByEnv() {
|
|
836
|
+
return process.env.AKAN_DEV_CSR_REBUILD === "1";
|
|
837
|
+
}
|
|
838
|
+
async rearmCsrFromEnv() {
|
|
839
|
+
if (!CsrRegistry.#csrArmedByEnv())
|
|
840
|
+
return;
|
|
841
|
+
this.#beginCsrArm();
|
|
842
|
+
this.#csrActive = true;
|
|
843
|
+
this.#csrArmSucceeded = true;
|
|
844
|
+
const rearmed = this.#lanes.workQueue.enqueue("build-csr-rearm", async () => {
|
|
845
|
+
const result = await this.#batchRunner.run(await this.#links.batchRequest({ generation: this.#links.generation(), needs: ["csr"], changedFiles: [] }), (msg) => BuilderChannel.emit(msg));
|
|
846
|
+
this.#patcher?.forget();
|
|
847
|
+
if (result.errors.csr)
|
|
848
|
+
this.#logger.error(`csr-rearm failed: ${result.errors.csr}`);
|
|
849
|
+
else
|
|
850
|
+
this.#logger.verbose("csr-rearm ok; this session had CSR armed before the builder restarted");
|
|
851
|
+
});
|
|
852
|
+
this.#csrGate = this.#armingCsr(rearmed);
|
|
853
|
+
await rearmed.catch((err) => this.#logger.error(`csr-rearm failed: ${ApplicationBuildReporter.formatError(err, this.#app.workspace.workspaceRoot)}`));
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
// pkgs/@akanjs/devkit/incrementalBuilder/ssrRegistry.ts
|
|
858
|
+
import { rm as rm2 } from "fs/promises";
|
|
859
|
+
import path5 from "path";
|
|
860
|
+
import { CSR_DEV_PATCHING_MARKER as CSR_DEV_PATCHING_MARKER2 } from "akanjs/server/hmr/csrDevManifest";
|
|
861
|
+
class SsrRegistry {
|
|
862
|
+
#app;
|
|
863
|
+
#logger;
|
|
864
|
+
#lanes;
|
|
865
|
+
#artifactDir;
|
|
866
|
+
#links;
|
|
867
|
+
#crashRetryTimer = null;
|
|
868
|
+
#crashRetryEntries = new Set;
|
|
869
|
+
#ssrBundler;
|
|
870
|
+
#ssrPatcher;
|
|
871
|
+
#ssrLock = Promise.resolve();
|
|
872
|
+
#ssrArming = null;
|
|
873
|
+
#ssrBroken = false;
|
|
874
|
+
#ssrCrashedAt = null;
|
|
875
|
+
static #crashRetryMs = 1e4;
|
|
876
|
+
constructor(app, { logger, lanes, artifactDir }, links) {
|
|
877
|
+
this.#app = app;
|
|
878
|
+
this.#logger = logger;
|
|
879
|
+
this.#lanes = lanes;
|
|
880
|
+
this.#artifactDir = artifactDir;
|
|
881
|
+
this.#links = links;
|
|
882
|
+
this.#ssrBundler = new SsrDevBundler(app, { discovery: links.discovery });
|
|
883
|
+
this.#ssrPatcher = process.env.AKAN_DEV_CSR_PATCHER === "off" ? null : new CsrDevPatcher(this.#ssrBundler, { resident: true });
|
|
884
|
+
}
|
|
885
|
+
get ssrPatcher() {
|
|
886
|
+
return this.#ssrPatcher;
|
|
887
|
+
}
|
|
888
|
+
async patchSsr(generation, files, trace) {
|
|
714
889
|
const patcher = this.#ssrPatcher;
|
|
715
890
|
if (!patcher)
|
|
716
891
|
return true;
|
|
717
892
|
const hold = await ServerGraphFile.touches(await ServerGraphFile.read(this.#artifactDir), files, (file) => ServerGraphFile.clientExportsOf(file));
|
|
718
|
-
this.#discovery.invalidate?.(files);
|
|
719
|
-
return await this
|
|
893
|
+
this.#links.discovery().invalidate?.(files);
|
|
894
|
+
return await this.withSsrLock(async () => await this.#runSsrPatcher(patcher, files, { trace, hold, batchGeneration: generation }));
|
|
720
895
|
}
|
|
721
|
-
async
|
|
896
|
+
async ensureSsrEntries(entries, { crashRetry = false } = {}) {
|
|
722
897
|
if (entries.length === 0)
|
|
723
898
|
return;
|
|
724
899
|
const arming = this.#ssrArming;
|
|
725
900
|
if (arming) {
|
|
726
|
-
this.#runBeside(arming.then(async () => await this
|
|
901
|
+
this.#lanes.runBeside(arming.then(async () => await this.ensureSsrEntries(entries, { crashRetry })).catch(this.#lanes.slowLaneFailed("ssr-ensure")));
|
|
727
902
|
return;
|
|
728
903
|
}
|
|
729
|
-
await this
|
|
904
|
+
await this.withSsrLock(async () => {
|
|
730
905
|
const patcher = this.#ssrPatcher;
|
|
731
|
-
const generation = this.#generation;
|
|
906
|
+
const generation = this.#links.generation();
|
|
732
907
|
if (patcher && !await this.#runSsrPatcher(patcher, [], { roots: entries, onlyRoots: true, batchGeneration: generation }))
|
|
733
908
|
return;
|
|
734
909
|
if (!patcher && await this.#ssrBundler.holds(entries))
|
|
735
910
|
return;
|
|
736
911
|
if (this.#ssrBroken)
|
|
737
912
|
return;
|
|
738
|
-
if (this.#ssrCrashedAt !== null && Date.now() - this.#ssrCrashedAt <
|
|
913
|
+
if (this.#ssrCrashedAt !== null && Date.now() - this.#ssrCrashedAt < SsrRegistry.#crashRetryMs) {
|
|
739
914
|
patcher?.releaseHandedOver();
|
|
740
915
|
this.#retryAfterCrash(this.#ssrCrashedAt, entries);
|
|
741
916
|
return;
|
|
@@ -756,10 +931,10 @@ class IncrementalBuilder {
|
|
|
756
931
|
this.#crashRetryTimer = null;
|
|
757
932
|
const retried = [...this.#crashRetryEntries];
|
|
758
933
|
this.#crashRetryEntries.clear();
|
|
759
|
-
if (this.shuttingDown || retried.length === 0)
|
|
934
|
+
if (this.#lanes.shuttingDown || retried.length === 0)
|
|
760
935
|
return;
|
|
761
|
-
this.#runBeside(this
|
|
762
|
-
}, Math.max(0, crashedAt +
|
|
936
|
+
this.#lanes.runBeside(this.ensureSsrEntries(retried, { crashRetry: true })).catch(this.#lanes.slowLaneFailed("ssr-crash-retry"));
|
|
937
|
+
}, Math.max(0, crashedAt + SsrRegistry.#crashRetryMs - Date.now()));
|
|
763
938
|
timer.unref?.();
|
|
764
939
|
this.#crashRetryTimer = timer;
|
|
765
940
|
}
|
|
@@ -767,41 +942,29 @@ class IncrementalBuilder {
|
|
|
767
942
|
const delayMs = Number(process.env.AKAN_DEV_SSR_ARM_DELAY_MS);
|
|
768
943
|
if (Number.isInteger(delayMs) && delayMs > 0)
|
|
769
944
|
await Bun.sleep(delayMs);
|
|
770
|
-
const arm = async () => await this
|
|
771
|
-
const generation = this.#generation;
|
|
945
|
+
const arm = async () => await this.withSsrLock(async () => {
|
|
946
|
+
const generation = this.#links.generation();
|
|
772
947
|
if (this.#ssrPatcher && !await this.#runSsrPatcher(this.#ssrPatcher, [], { batchGeneration: generation }))
|
|
773
948
|
return;
|
|
774
949
|
await this.#runSsrWorker(generation);
|
|
775
950
|
}).catch((err) => {
|
|
776
951
|
this.#logger.error(`ssr-registry boot build failed; the next save retries it: ${ApplicationBuildReporter.formatError(err, this.#app.workspace.workspaceRoot)}`);
|
|
777
952
|
});
|
|
778
|
-
const arming = this.#runBeside((async () => {
|
|
953
|
+
const arming = this.#lanes.runBeside((async () => {
|
|
779
954
|
await arm();
|
|
780
955
|
const crashedAt = this.#ssrCrashedAt;
|
|
781
956
|
if (crashedAt === null)
|
|
782
957
|
return;
|
|
783
|
-
while (!this.shuttingDown && Date.now() < crashedAt +
|
|
958
|
+
while (!this.#lanes.shuttingDown && Date.now() < crashedAt + SsrRegistry.#crashRetryMs)
|
|
784
959
|
await Bun.sleep(250);
|
|
785
|
-
if (!this.shuttingDown)
|
|
960
|
+
if (!this.#lanes.shuttingDown)
|
|
786
961
|
await arm();
|
|
787
962
|
})());
|
|
788
963
|
this.#ssrArming = arming;
|
|
789
964
|
await arming;
|
|
790
965
|
this.#ssrArming = null;
|
|
791
966
|
}
|
|
792
|
-
|
|
793
|
-
return error instanceof AggregateError;
|
|
794
|
-
}
|
|
795
|
-
#slowLaneFailed(label) {
|
|
796
|
-
return (error) => this.#logger.error(`${label} failed in the slow lane: ${ApplicationBuildReporter.formatError(error, this.#app.workspace.workspaceRoot)}`);
|
|
797
|
-
}
|
|
798
|
-
#countDelegation(registry, reason) {
|
|
799
|
-
const key = `${registry}:${reason.split(":")[0]}`;
|
|
800
|
-
const count = (this.#delegations.get(key) ?? 0) + 1;
|
|
801
|
-
this.#delegations.set(key, count);
|
|
802
|
-
return `#${count} for this reason`;
|
|
803
|
-
}
|
|
804
|
-
async#withSsrLock(fn) {
|
|
967
|
+
async withSsrLock(fn) {
|
|
805
968
|
const run = this.#ssrLock.then(fn, fn);
|
|
806
969
|
this.#ssrLock = run.then(() => {
|
|
807
970
|
return;
|
|
@@ -813,15 +976,15 @@ class IncrementalBuilder {
|
|
|
813
976
|
async#runSsrWorker(generation) {
|
|
814
977
|
let result;
|
|
815
978
|
try {
|
|
816
|
-
result = await this.#runBatch({ generation, needs: ["ssr"], changedFiles: [] });
|
|
979
|
+
result = await this.#links.runBatch({ generation, needs: ["ssr"], changedFiles: [] });
|
|
817
980
|
} finally {
|
|
818
981
|
this.#ssrPatcher?.forget();
|
|
819
982
|
}
|
|
820
|
-
this
|
|
983
|
+
this.noteSsrWorker(result);
|
|
821
984
|
if (result.errors.ssr)
|
|
822
985
|
throw new Error(result.errors.ssr);
|
|
823
986
|
}
|
|
824
|
-
|
|
987
|
+
noteSsrWorker(result) {
|
|
825
988
|
const crashed = !!result.crashed && (result.crashedNeeds ?? ["ssr"]).includes("ssr");
|
|
826
989
|
this.#ssrBroken = !!result.errors.ssr && !crashed;
|
|
827
990
|
this.#ssrCrashedAt = crashed ? Date.now() : null;
|
|
@@ -834,7 +997,7 @@ class IncrementalBuilder {
|
|
|
834
997
|
batchGeneration
|
|
835
998
|
} = {}) {
|
|
836
999
|
const started = Date.now();
|
|
837
|
-
const marker =
|
|
1000
|
+
const marker = path5.join(this.#ssrBundler.outDir, CSR_DEV_PATCHING_MARKER2);
|
|
838
1001
|
await Bun.write(marker, String(process.pid));
|
|
839
1002
|
try {
|
|
840
1003
|
const result = await patcher.update(files, {
|
|
@@ -860,47 +1023,178 @@ class IncrementalBuilder {
|
|
|
860
1023
|
}
|
|
861
1024
|
});
|
|
862
1025
|
if (result.kind === "delegate") {
|
|
863
|
-
this.#logger.verbose(`ssr-patch handed to a build worker (${this.#countDelegation("ssr", result.reason)}): ${result.reason}`);
|
|
1026
|
+
this.#logger.verbose(`ssr-patch handed to a build worker (${this.#links.countDelegation("ssr", result.reason)}): ${result.reason}`);
|
|
864
1027
|
return true;
|
|
865
1028
|
}
|
|
866
1029
|
if (!onlyRoots) {
|
|
867
1030
|
this.#ssrBroken = false;
|
|
868
1031
|
this.#ssrCrashedAt = null;
|
|
869
|
-
|
|
1032
|
+
sendBuildStatus("ssr", { generation: batchGeneration, ok: true, files });
|
|
870
1033
|
}
|
|
871
1034
|
this.#logger.verbose(result.kind === "unchanged" ? `ssr-patch unchanged (${Date.now() - started}ms)` : `ssr-patch generation=${result.update.generation} ${result.update.reload ? `reload (${result.update.reason})` : `patch modules=${result.update.changedIds.length}`} (${Date.now() - started}ms)`);
|
|
872
1035
|
return false;
|
|
873
1036
|
} catch (err) {
|
|
874
1037
|
const message = ApplicationBuildReporter.formatError(err, this.#app.workspace.workspaceRoot);
|
|
875
|
-
if (
|
|
876
|
-
|
|
1038
|
+
if (isCompileError(err)) {
|
|
1039
|
+
sendBuildStatus("ssr", { generation: batchGeneration, ok: false, files, message });
|
|
877
1040
|
return false;
|
|
878
1041
|
}
|
|
879
1042
|
this.#logger.verbose(`ssr-patch threw; a build worker takes it: ${message}`);
|
|
880
1043
|
patcher.forget();
|
|
881
1044
|
return true;
|
|
882
1045
|
} finally {
|
|
883
|
-
await
|
|
1046
|
+
await rm2(marker, { force: true });
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
// pkgs/@akanjs/devkit/incrementalBuilder/incrementalBuilder.proc.ts
|
|
1052
|
+
class IncrementalBuilder {
|
|
1053
|
+
#logger = new Logger2("IncrementalBuilder");
|
|
1054
|
+
#app;
|
|
1055
|
+
#artifact;
|
|
1056
|
+
#watch;
|
|
1057
|
+
#optimizedFonts;
|
|
1058
|
+
#discovery;
|
|
1059
|
+
#batchRunner;
|
|
1060
|
+
#generation = 0;
|
|
1061
|
+
#graphPayloads;
|
|
1062
|
+
#discoveredThrough = 0;
|
|
1063
|
+
#delegations = new Map;
|
|
1064
|
+
#lanes;
|
|
1065
|
+
#csr;
|
|
1066
|
+
#ssr;
|
|
1067
|
+
#watcher;
|
|
1068
|
+
constructor(options) {
|
|
1069
|
+
this.#app = options.app;
|
|
1070
|
+
this.#artifact = options.artifact;
|
|
1071
|
+
this.#watch = options.watch;
|
|
1072
|
+
this.#optimizedFonts = options.optimizedFonts;
|
|
1073
|
+
this.#discovery = options.discovery;
|
|
1074
|
+
this.#generation = options.initialGeneration ?? 0;
|
|
1075
|
+
this.#discoveredThrough = this.#generation;
|
|
1076
|
+
this.#graphPayloads = new BuilderGraphPayloads(options.app, () => this.#generation);
|
|
1077
|
+
this.#batchRunner = new BuildBatchRunner({
|
|
1078
|
+
workspaceRoot: options.app.workspace.workspaceRoot,
|
|
1079
|
+
cwd: options.app.cwdPath
|
|
1080
|
+
});
|
|
1081
|
+
const logger = this.#logger;
|
|
1082
|
+
const generation = () => this.#generation;
|
|
1083
|
+
const countDelegation = (registry, reason) => this.#countDelegation(registry, reason);
|
|
1084
|
+
const runBatch = (work) => this.#runBatch(work);
|
|
1085
|
+
this.#lanes = new BuilderLanes(options.app, { logger }, { generation, runQueuedBatch: (batch) => this.#runQueuedBatch(batch), runBatch });
|
|
1086
|
+
const lanes = this.#lanes;
|
|
1087
|
+
this.#csr = new CsrRegistry(options.app, { logger, lanes, batchRunner: this.#batchRunner }, { generation, artifact: () => this.#artifact, batchRequest: (work) => this.#batchRequest(work), countDelegation });
|
|
1088
|
+
this.#ssr = new SsrRegistry(options.app, { logger, lanes, artifactDir: this.#artifactDir }, { generation, discovery: () => this.#discovery, runBatch, countDelegation });
|
|
1089
|
+
this.#watcher = new BuilderWatch(options.app, { logger, lanes, csr: this.#csr, ssr: this.#ssr, graphPayloads: this.#graphPayloads }, { nextGeneration: () => ++this.#generation, refreshDiscovery: (job) => this.#refreshDiscovery(job) });
|
|
1090
|
+
}
|
|
1091
|
+
get #artifactDir() {
|
|
1092
|
+
return `${this.#app.cwdPath}/.akan/artifact`;
|
|
1093
|
+
}
|
|
1094
|
+
async handleBuildRoute(msg) {
|
|
1095
|
+
await this.#lanes.workQueue.enqueue(`build-route:${msg.routeId}`, async () => BuilderChannel.send(await this.#handleBuildRoute(msg)));
|
|
1096
|
+
}
|
|
1097
|
+
async#handleBuildRoute(msg) {
|
|
1098
|
+
const seenGeneration = this.#discoveredThrough;
|
|
1099
|
+
try {
|
|
1100
|
+
const delta = await new RouteClientBuilder({
|
|
1101
|
+
app: this.#app,
|
|
1102
|
+
routeId: msg.routeId,
|
|
1103
|
+
seeds: msg.seeds,
|
|
1104
|
+
graphSeeds: msg.graphSeeds,
|
|
1105
|
+
artifact: this.#artifact,
|
|
1106
|
+
knownEntries: new Set(msg.knownEntries),
|
|
1107
|
+
discovery: this.#discovery,
|
|
1108
|
+
browser: "registry"
|
|
1109
|
+
}).build();
|
|
1110
|
+
await this.#ssr.ensureSsrEntries(delta.registryEntries ?? []);
|
|
1111
|
+
this.#logger.verbose(`build-route ok routeId=${msg.routeId} newEntries=${delta.newEntries.length}`);
|
|
1112
|
+
sendBuildStatus("route", { generation: this.#generation, ok: true, files: msg.seeds, scope: msg.routeId });
|
|
1113
|
+
return {
|
|
1114
|
+
type: "build-route-res",
|
|
1115
|
+
id: msg.id,
|
|
1116
|
+
ok: true,
|
|
1117
|
+
data: {
|
|
1118
|
+
manifestDelta: delta.manifestDelta,
|
|
1119
|
+
ssrManifestDelta: delta.ssrManifestDelta.moduleMap,
|
|
1120
|
+
newEntries: delta.newEntries,
|
|
1121
|
+
discoveredEntries: delta.discoveredEntries,
|
|
1122
|
+
clientDeps: delta.clientDeps,
|
|
1123
|
+
clientDepsByEntry: delta.clientDepsByEntry,
|
|
1124
|
+
routeId: msg.routeId,
|
|
1125
|
+
generation: msg.generation,
|
|
1126
|
+
seenGeneration
|
|
1127
|
+
}
|
|
1128
|
+
};
|
|
1129
|
+
} catch (err) {
|
|
1130
|
+
const errMsg = ApplicationBuildReporter.formatError(err, this.#app.workspace.workspaceRoot);
|
|
1131
|
+
this.#logger.error(`build-route failed routeId=${msg.routeId}: ${errMsg}`);
|
|
1132
|
+
sendBuildStatus("route", {
|
|
1133
|
+
generation: this.#generation,
|
|
1134
|
+
ok: false,
|
|
1135
|
+
files: msg.seeds,
|
|
1136
|
+
message: errMsg,
|
|
1137
|
+
scope: msg.routeId
|
|
1138
|
+
});
|
|
1139
|
+
return { type: "build-route-res", id: msg.id, ok: false, error: errMsg };
|
|
884
1140
|
}
|
|
885
1141
|
}
|
|
1142
|
+
async shutdown(reason) {
|
|
1143
|
+
if (this.#lanes.shuttingDown)
|
|
1144
|
+
return;
|
|
1145
|
+
this.#lanes.markShuttingDown();
|
|
1146
|
+
const started = Date.now();
|
|
1147
|
+
this.#logger.debug(`shutdown requested (${reason}); draining ${this.#lanes.inFlight} work item(s)`);
|
|
1148
|
+
this.#watcher.stop();
|
|
1149
|
+
await this.#lanes.drain();
|
|
1150
|
+
if (this.#delegations.size > 0)
|
|
1151
|
+
this.#logger.verbose(`registry saves handed to build workers this session: ${[...this.#delegations].map(([reason, count]) => `${reason}=${count}`).join(", ")}`);
|
|
1152
|
+
const flushed = await BuilderChannel.drain();
|
|
1153
|
+
this.#logger.debug(`drained in ${Date.now() - started}ms${flushed ? ` after flushing ${flushed} ipc write(s)` : ""}; exiting for recycle`);
|
|
1154
|
+
process.exit(0);
|
|
1155
|
+
}
|
|
1156
|
+
get shuttingDown() {
|
|
1157
|
+
return this.#lanes.shuttingDown;
|
|
1158
|
+
}
|
|
1159
|
+
batchTouchesPagesTree(roots, batch) {
|
|
1160
|
+
return this.#watcher.batchTouchesPagesTree(roots, batch);
|
|
1161
|
+
}
|
|
1162
|
+
batchMayChangePageKeys(roots, batch) {
|
|
1163
|
+
return this.#watcher.batchMayChangePageKeys(roots, batch);
|
|
1164
|
+
}
|
|
1165
|
+
scheduleCssRebuild(rebuild) {
|
|
1166
|
+
this.#lanes.scheduleCssRebuild(rebuild);
|
|
1167
|
+
}
|
|
1168
|
+
installWatcher() {
|
|
1169
|
+
return this.#watcher.installWatcher();
|
|
1170
|
+
}
|
|
1171
|
+
armSsrRegistry() {
|
|
1172
|
+
return this.#ssr.armSsrRegistry();
|
|
1173
|
+
}
|
|
1174
|
+
#countDelegation(registry, reason) {
|
|
1175
|
+
const key = `${registry}:${reason.split(":")[0]}`;
|
|
1176
|
+
const count = (this.#delegations.get(key) ?? 0) + 1;
|
|
1177
|
+
this.#delegations.set(key, count);
|
|
1178
|
+
return `#${count} for this reason`;
|
|
1179
|
+
}
|
|
886
1180
|
async#runQueuedBatch({ discovery, ...work }) {
|
|
887
1181
|
if (discovery)
|
|
888
1182
|
await this.#refreshDiscovery(discovery);
|
|
889
1183
|
this.#discoveredThrough = Math.max(this.#discoveredThrough, work.generation);
|
|
890
1184
|
try {
|
|
891
1185
|
if (work.needs.includes("ssr"))
|
|
892
|
-
await this.#withSsrLock(async () => {
|
|
1186
|
+
await this.#ssr.withSsrLock(async () => {
|
|
893
1187
|
try {
|
|
894
|
-
this.#noteSsrWorker(await this.#runBatch(work));
|
|
1188
|
+
this.#ssr.noteSsrWorker(await this.#runBatch(work));
|
|
895
1189
|
} finally {
|
|
896
|
-
this.#ssrPatcher?.forget();
|
|
1190
|
+
this.#ssr.ssrPatcher?.forget();
|
|
897
1191
|
}
|
|
898
1192
|
});
|
|
899
1193
|
else
|
|
900
1194
|
await this.#runBatch(work);
|
|
901
1195
|
} finally {
|
|
902
1196
|
if (work.needs.includes("csr"))
|
|
903
|
-
this.#patcher?.forget();
|
|
1197
|
+
this.#csr.patcher?.forget();
|
|
904
1198
|
}
|
|
905
1199
|
}
|
|
906
1200
|
async#refreshDiscovery({ files, refresh, generation }) {
|
|
@@ -923,12 +1217,12 @@ class IncrementalBuilder {
|
|
|
923
1217
|
if (result.crashed) {
|
|
924
1218
|
const crashedNeeds = result.crashedNeeds ?? needs;
|
|
925
1219
|
if (crashedNeeds.includes("csr"))
|
|
926
|
-
this.#patcher?.releaseHandedOver();
|
|
1220
|
+
this.#csr.patcher?.releaseHandedOver();
|
|
927
1221
|
if (crashedNeeds.includes("ssr"))
|
|
928
|
-
this.#ssrPatcher?.releaseHandedOver();
|
|
1222
|
+
this.#ssr.ssrPatcher?.releaseHandedOver();
|
|
929
1223
|
for (const need of crashedNeeds)
|
|
930
1224
|
if (need !== "base" && need !== "shell")
|
|
931
|
-
|
|
1225
|
+
sendBuildStatus(need, {
|
|
932
1226
|
generation,
|
|
933
1227
|
ok: false,
|
|
934
1228
|
files: changedFiles,
|
|
@@ -950,19 +1244,19 @@ class IncrementalBuilder {
|
|
|
950
1244
|
pageKeys: await this.#app.getPageKeys(),
|
|
951
1245
|
optimizedFonts: this.#optimizedFonts,
|
|
952
1246
|
cssAssets: this.#artifact.cssAssets ?? null,
|
|
953
|
-
artifactDir:
|
|
1247
|
+
artifactDir: path6.resolve(this.#artifactDir),
|
|
954
1248
|
...trace ? { trace } : {}
|
|
955
1249
|
};
|
|
956
1250
|
}
|
|
957
1251
|
async boot() {
|
|
958
1252
|
if (this.#watch)
|
|
959
1253
|
await this.installWatcher();
|
|
960
|
-
BuilderChannel.
|
|
1254
|
+
await BuilderChannel.announceReady(this.#logger);
|
|
961
1255
|
this.#logger.verbose(`ready (watch=${this.#watch})`);
|
|
962
1256
|
}
|
|
963
1257
|
async announceRecoveredState(changedFiles) {
|
|
964
1258
|
const generation = ++this.#generation;
|
|
965
|
-
await this.#workQueue.enqueue("boot-recovered", async () => {
|
|
1259
|
+
await this.#lanes.workQueue.enqueue("boot-recovered", async () => {
|
|
966
1260
|
await this.#runBatch({ generation, needs: ["pages", "css"], changedFiles });
|
|
967
1261
|
});
|
|
968
1262
|
}
|
|
@@ -982,7 +1276,7 @@ class IncrementalBuilder {
|
|
|
982
1276
|
const cssAssets = this.#artifact.cssAssets ?? {};
|
|
983
1277
|
const cssBase64ByUrl = Object.fromEntries(await Promise.all(Object.values(cssAssets).map(async ({ cssUrl, cssRelPath }) => [
|
|
984
1278
|
cssUrl,
|
|
985
|
-
Buffer.from(await Bun.file(
|
|
1279
|
+
Buffer.from(await Bun.file(path6.join(this.#artifactDir, cssRelPath)).arrayBuffer()).toString("base64")
|
|
986
1280
|
])));
|
|
987
1281
|
await BuilderChannel.send({
|
|
988
1282
|
type: "css-updated",
|
|
@@ -1000,151 +1294,11 @@ class IncrementalBuilder {
|
|
|
1000
1294
|
await BuilderChannel.send({ type: "build-graph-res", id: msg.id, ok: false, error });
|
|
1001
1295
|
}
|
|
1002
1296
|
}
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
await this.#buildShell(msg);
|
|
1006
|
-
return;
|
|
1007
|
-
}
|
|
1008
|
-
this.#beginCsrArm();
|
|
1009
|
-
this.#csrActive = true;
|
|
1010
|
-
const armed = this.#workQueue.enqueue("build-csr", async () => {
|
|
1011
|
-
const started = Date.now();
|
|
1012
|
-
let error;
|
|
1013
|
-
try {
|
|
1014
|
-
const result = await this.#batchRunner.run(await this.#batchRequest({ generation: this.#generation, needs: ["csr"], changedFiles: [] }));
|
|
1015
|
-
error = result.errors.csr;
|
|
1016
|
-
} catch (err) {
|
|
1017
|
-
error = ApplicationBuildReporter.formatError(err, this.#app.workspace.workspaceRoot);
|
|
1018
|
-
}
|
|
1019
|
-
if (error) {
|
|
1020
|
-
this.#logger.error(`csr-build failed: ${error}`);
|
|
1021
|
-
await BuilderChannel.send({ type: "build-csr-res", id: msg.id, ok: false, error });
|
|
1022
|
-
return;
|
|
1023
|
-
}
|
|
1024
|
-
this.#csrArmSucceeded = true;
|
|
1025
|
-
this.#patcher?.forget();
|
|
1026
|
-
this.#logger.info(`csr-build ok on demand (${Date.now() - started}ms); rebuilding CSR on every save now`);
|
|
1027
|
-
await BuilderChannel.send({ type: "build-csr-res", id: msg.id, ok: true });
|
|
1028
|
-
});
|
|
1029
|
-
this.#csrGate = this.#armingCsr(armed);
|
|
1030
|
-
await armed;
|
|
1031
|
-
}
|
|
1032
|
-
async#buildShell(msg) {
|
|
1033
|
-
await this.#workQueue.enqueue("build-shell", async () => {
|
|
1034
|
-
const generation = this.#generation;
|
|
1035
|
-
const shellKey = `${generation}:${JSON.stringify(this.#artifact.cssAssets)}`;
|
|
1036
|
-
let error;
|
|
1037
|
-
if (this.#shellKey !== shellKey)
|
|
1038
|
-
try {
|
|
1039
|
-
const result = await this.#batchRunner.run(await this.#batchRequest({ generation, needs: ["shell"], changedFiles: [] }));
|
|
1040
|
-
error = result.errors.shell;
|
|
1041
|
-
if (!error)
|
|
1042
|
-
this.#shellKey = shellKey;
|
|
1043
|
-
} catch (err) {
|
|
1044
|
-
error = ApplicationBuildReporter.formatError(err, this.#app.workspace.workspaceRoot);
|
|
1045
|
-
}
|
|
1046
|
-
await BuilderChannel.send(error ? { type: "build-csr-res", id: msg.id, ok: false, error } : { type: "build-csr-res", id: msg.id, ok: true });
|
|
1047
|
-
});
|
|
1048
|
-
}
|
|
1049
|
-
#beginCsrArm() {
|
|
1050
|
-
if (this.#csrArms === 0) {
|
|
1051
|
-
this.#csrWasActive = this.#csrActive;
|
|
1052
|
-
this.#csrArmSucceeded = false;
|
|
1053
|
-
}
|
|
1054
|
-
this.#csrArms += 1;
|
|
1055
|
-
this.#csrArming = true;
|
|
1056
|
-
}
|
|
1057
|
-
#armingCsr(armed) {
|
|
1058
|
-
const settle = () => {
|
|
1059
|
-
this.#csrArms -= 1;
|
|
1060
|
-
if (this.#csrArms > 0)
|
|
1061
|
-
return;
|
|
1062
|
-
this.#csrArming = false;
|
|
1063
|
-
this.#csrActive = this.#csrWasActive || this.#csrArmSucceeded;
|
|
1064
|
-
};
|
|
1065
|
-
return armed.then(settle, settle);
|
|
1297
|
+
handleBuildCsr(msg) {
|
|
1298
|
+
return this.#csr.handleBuildCsr(msg);
|
|
1066
1299
|
}
|
|
1067
|
-
|
|
1068
|
-
return
|
|
1069
|
-
}
|
|
1070
|
-
static async#buildBootDeps(app, runner) {
|
|
1071
|
-
const result = await runner.run({
|
|
1072
|
-
appName: app.name,
|
|
1073
|
-
workspaceRoot: app.workspace.workspaceRoot,
|
|
1074
|
-
repoName: app.workspace.repoName,
|
|
1075
|
-
generation: 0,
|
|
1076
|
-
needs: ["base"],
|
|
1077
|
-
changedFiles: [],
|
|
1078
|
-
pageKeys: null,
|
|
1079
|
-
optimizedFonts: null,
|
|
1080
|
-
cssAssets: null,
|
|
1081
|
-
artifactDir: path2.resolve(`${app.cwdPath}/.akan/artifact`)
|
|
1082
|
-
});
|
|
1083
|
-
if (result.errors.base)
|
|
1084
|
-
throw new Error(result.errors.base);
|
|
1085
|
-
if (!result.artifact || !result.optimizedFonts)
|
|
1086
|
-
throw new Error("boot build reported success without an artifact; the build worker likely died");
|
|
1087
|
-
const discovery = await GraphClientEntryDiscovery.create(app);
|
|
1088
|
-
return { artifact: result.artifact, optimizedFonts: result.optimizedFonts, discovery };
|
|
1089
|
-
}
|
|
1090
|
-
async rearmCsrFromEnv() {
|
|
1091
|
-
if (!IncrementalBuilder.#csrArmedByEnv())
|
|
1092
|
-
return;
|
|
1093
|
-
this.#beginCsrArm();
|
|
1094
|
-
this.#csrActive = true;
|
|
1095
|
-
this.#csrArmSucceeded = true;
|
|
1096
|
-
const rearmed = this.#workQueue.enqueue("build-csr-rearm", async () => {
|
|
1097
|
-
const result = await this.#batchRunner.run(await this.#batchRequest({ generation: this.#generation, needs: ["csr"], changedFiles: [] }), (msg) => BuilderChannel.emit(msg));
|
|
1098
|
-
this.#patcher?.forget();
|
|
1099
|
-
if (result.errors.csr)
|
|
1100
|
-
this.#logger.error(`csr-rearm failed: ${result.errors.csr}`);
|
|
1101
|
-
else
|
|
1102
|
-
this.#logger.verbose("csr-rearm ok; this session had CSR armed before the builder restarted");
|
|
1103
|
-
});
|
|
1104
|
-
this.#csrGate = this.#armingCsr(rearmed);
|
|
1105
|
-
await rearmed.catch((err) => this.#logger.error(`csr-rearm failed: ${ApplicationBuildReporter.formatError(err, this.#app.workspace.workspaceRoot)}`));
|
|
1106
|
-
}
|
|
1107
|
-
static #recoverBoot(app, bootError, logger, runner, initialGeneration) {
|
|
1108
|
-
const firstMessage = bootError instanceof Error ? bootError.message : String(bootError);
|
|
1109
|
-
logger.error(`boot build failed; entering degraded watch mode until the error is fixed: ${firstMessage}`);
|
|
1110
|
-
let generation = initialGeneration;
|
|
1111
|
-
const sendFailure = (files, message) => {
|
|
1112
|
-
BuilderChannel.emit({
|
|
1113
|
-
type: "build-status",
|
|
1114
|
-
data: { generation, phase: "pages", ok: false, files, message: `Boot build failed: ${message}` }
|
|
1115
|
-
});
|
|
1116
|
-
};
|
|
1117
|
-
sendFailure([], firstMessage);
|
|
1118
|
-
BuilderChannel.emit({ type: "builder-ready" });
|
|
1119
|
-
BuilderChannel.emit({ type: "boot-armed" });
|
|
1120
|
-
return new Promise((resolve, reject) => {
|
|
1121
|
-
(async () => {
|
|
1122
|
-
const roots = await new WatchRootResolver(app).resolve();
|
|
1123
|
-
const watcher = new HmrWatcher({
|
|
1124
|
-
roots,
|
|
1125
|
-
logger,
|
|
1126
|
-
onBatch: async (batch) => {
|
|
1127
|
-
generation += 1;
|
|
1128
|
-
const files = [...batch.files].sort();
|
|
1129
|
-
try {
|
|
1130
|
-
if (new Set(batch.kinds).has("config"))
|
|
1131
|
-
await app.getConfig({ refresh: true });
|
|
1132
|
-
const deps = await IncrementalBuilder.#buildBootDeps(app, runner);
|
|
1133
|
-
const builder = new IncrementalBuilder({ app, watch: true, initialGeneration: generation, ...deps });
|
|
1134
|
-
watcher.stop();
|
|
1135
|
-
logger.info(`boot build recovered generation=${generation}`);
|
|
1136
|
-
resolve({ builder, changedFiles: files });
|
|
1137
|
-
} catch (err) {
|
|
1138
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
1139
|
-
logger.error(`boot build retry failed: ${message}`);
|
|
1140
|
-
sendFailure(files, message);
|
|
1141
|
-
}
|
|
1142
|
-
}
|
|
1143
|
-
});
|
|
1144
|
-
await watcher.start();
|
|
1145
|
-
logger.warn(`[degraded] watching ${roots.length} roots for a fix`);
|
|
1146
|
-
})().catch(reject);
|
|
1147
|
-
});
|
|
1300
|
+
rearmCsrFromEnv() {
|
|
1301
|
+
return this.#csr.rearmCsrFromEnv();
|
|
1148
1302
|
}
|
|
1149
1303
|
static async main() {
|
|
1150
1304
|
const logger = new Logger2("IncrementalBuilder");
|
|
@@ -1184,14 +1338,14 @@ class IncrementalBuilder {
|
|
|
1184
1338
|
});
|
|
1185
1339
|
let recoveredFiles = null;
|
|
1186
1340
|
const bootRunner = new BuildBatchRunner({ workspaceRoot, cwd: app.cwdPath });
|
|
1187
|
-
const initialGeneration =
|
|
1341
|
+
const initialGeneration = BuilderBoot.initialGeneration();
|
|
1188
1342
|
try {
|
|
1189
|
-
const deps = await
|
|
1343
|
+
const deps = await BuilderBoot.buildBootDeps(app, bootRunner);
|
|
1190
1344
|
builder = new IncrementalBuilder({ app, watch, initialGeneration, ...deps });
|
|
1191
1345
|
} catch (err) {
|
|
1192
1346
|
if (!watch)
|
|
1193
1347
|
throw err;
|
|
1194
|
-
const recovered = await
|
|
1348
|
+
const recovered = await BuilderBoot.recoverBoot(app, err, logger, bootRunner, initialGeneration, (options) => new IncrementalBuilder(options));
|
|
1195
1349
|
builder = recovered.builder;
|
|
1196
1350
|
recoveredFiles = recovered.changedFiles;
|
|
1197
1351
|
}
|
|
@@ -1203,10 +1357,6 @@ class IncrementalBuilder {
|
|
|
1203
1357
|
await Promise.all([builder.rearmCsrFromEnv(), builder.armSsrRegistry()]);
|
|
1204
1358
|
BuilderChannel.emit({ type: "boot-armed" });
|
|
1205
1359
|
}
|
|
1206
|
-
static #initialGeneration() {
|
|
1207
|
-
const generation = Number(process.env.AKAN_BUILDER_INITIAL_GENERATION);
|
|
1208
|
-
return Number.isInteger(generation) && generation > 0 ? generation + 1 : 0;
|
|
1209
|
-
}
|
|
1210
1360
|
}
|
|
1211
1361
|
await IncrementalBuilder.main().catch((err) => {
|
|
1212
1362
|
console.error(err);
|