foldkit-plus 0.17.0 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +241 -28
- package/dist/cli.mjs +10930 -35
- package/dist/cli.mjs.map +1 -1
- package/dist/docs.d.mts +89 -0
- package/dist/docs.d.mts.map +1 -0
- package/dist/docs.mjs +290 -0
- package/dist/docs.mjs.map +1 -0
- package/dist/project.d.mts +45 -0
- package/dist/project.d.mts.map +1 -0
- package/dist/project.mjs +221 -0
- package/dist/project.mjs.map +1 -0
- package/dist/remote/filter.d.mts +1 -0
- package/dist/remote/filter.mjs +2 -0
- package/dist/remote/live.d.mts +1 -0
- package/dist/remote/live.mjs +2 -0
- package/dist/remote/outbox.d.mts +1 -0
- package/dist/remote/outbox.mjs +2 -0
- package/dist/remote/overlay.d.mts +1 -0
- package/dist/remote/overlay.mjs +2 -0
- package/dist/remote/persistence.d.mts +1 -0
- package/dist/remote/persistence.mjs +2 -0
- package/dist/remote/testing.d.mts +1 -0
- package/dist/remote/testing.mjs +2 -0
- package/dist/remote/why.d.mts +1 -0
- package/dist/remote/why.mjs +2 -0
- package/dist/setup.d.mts +45 -7
- package/dist/setup.d.mts.map +1 -1
- package/dist/setup.mjs +168 -25
- package/dist/setup.mjs.map +1 -1
- package/dist/testing/peer.d.mts +8 -0
- package/dist/testing/peer.d.mts.map +1 -0
- package/dist/testing/peer.mjs +19 -0
- package/dist/testing/peer.mjs.map +1 -0
- package/dist/testing/playwright.d.mts +84 -0
- package/dist/testing/playwright.d.mts.map +1 -0
- package/dist/testing/playwright.mjs +327 -0
- package/dist/testing/playwright.mjs.map +1 -0
- package/dist/testing/workers.d.mts +177 -0
- package/dist/testing/workers.d.mts.map +1 -0
- package/dist/testing/workers.mjs +593 -0
- package/dist/testing/workers.mjs.map +1 -0
- package/package.json +126 -59
|
@@ -0,0 +1,593 @@
|
|
|
1
|
+
import { peer } from "./peer.mjs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { Option, Schema } from "effect";
|
|
4
|
+
import { readFileSync, watch } from "node:fs";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import { basename, dirname, extname, join, relative, resolve } from "node:path";
|
|
7
|
+
import { ChildProcess, spawn } from "node:child_process";
|
|
8
|
+
import { subscribe, unsubscribe } from "node:diagnostics_channel";
|
|
9
|
+
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
10
|
+
import { createInterface } from "node:readline";
|
|
11
|
+
// src/testing/workers.ts
|
|
12
|
+
/**
|
|
13
|
+
* `foldkit-plus/testing/workers`: a Worker on a local workerd, configured by
|
|
14
|
+
* its own Wrangler configuration.
|
|
15
|
+
*
|
|
16
|
+
* Wrangler's test harness reads the configuration, so bindings, the
|
|
17
|
+
* compatibility date and flags, `.dev.vars` and D1 migrations mean here what
|
|
18
|
+
* they mean to `wrangler deploy`. This module owns only what Wrangler cannot
|
|
19
|
+
* know: the bundle. esbuild builds the entry with the workspace's source
|
|
20
|
+
* condition into a directory of the stack's own, and the harness runs the
|
|
21
|
+
* configuration with `main` pointed there.
|
|
22
|
+
*
|
|
23
|
+
* `wrangler`, `esbuild` and `@foldkit/vite-plugin` are optional peers of
|
|
24
|
+
* `foldkit-plus`, loaded on the first call; Miniflare is read from Wrangler's
|
|
25
|
+
* own copy, so the date check asks the runtime Wrangler starts.
|
|
26
|
+
*/
|
|
27
|
+
const SUBPATH = "testing/workers";
|
|
28
|
+
const wrangler = () => peer(SUBPATH, "wrangler", () => import("wrangler"));
|
|
29
|
+
const esbuild = () => peer(SUBPATH, "esbuild", () => import("esbuild"));
|
|
30
|
+
const foldkitVite = () => peer(SUBPATH, "@foldkit/vite-plugin", () => import("@foldkit/vite-plugin"));
|
|
31
|
+
const collecting = /* @__PURE__ */ new Set();
|
|
32
|
+
const isWorkerd = (child) => typeof child.spawnfile === "string" && /^workerd(\.exe)?$/.test(basename(child.spawnfile));
|
|
33
|
+
const onSpawn = (message) => {
|
|
34
|
+
if (typeof message !== "object" || message === null || !("process" in message)) return;
|
|
35
|
+
const child = message.process;
|
|
36
|
+
if (!(child instanceof ChildProcess)) return;
|
|
37
|
+
for (const spawned of collecting) spawned.processes.push(child);
|
|
38
|
+
};
|
|
39
|
+
/** Runs `step`, adding the processes it spawns to `into`. */
|
|
40
|
+
const collect = async (into, step) => {
|
|
41
|
+
if (collecting.size === 0) subscribe("child_process", onSpawn);
|
|
42
|
+
for (const other of collecting) other.shared = into.shared = true;
|
|
43
|
+
collecting.add(into);
|
|
44
|
+
try {
|
|
45
|
+
return await step();
|
|
46
|
+
} finally {
|
|
47
|
+
collecting.delete(into);
|
|
48
|
+
if (collecting.size === 0) unsubscribe("child_process", onSpawn);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
const isRunning = (child) => child.exitCode === null && child.signalCode === null;
|
|
52
|
+
/** Milliseconds a Linux process's main thread has run, by the scheduler's nanoseconds. */
|
|
53
|
+
const linuxTime = async (pid) => Number((await readFile(`/proc/${pid}/task/${pid}/schedstat`, "utf8")).split(" ")[0]) / 1e6;
|
|
54
|
+
const linuxReader = () => ({
|
|
55
|
+
read: async (pids) => (await Promise.all(pids.map(linuxTime))).reduce((a, b) => a + b, 0),
|
|
56
|
+
close: () => {}
|
|
57
|
+
});
|
|
58
|
+
/**
|
|
59
|
+
* Windows has no `/proc`, and Node no call for another process's times, so
|
|
60
|
+
* one PowerShell kept open answers each read: started per read, it takes
|
|
61
|
+
* longer than the requests it measures. The main thread is the one that
|
|
62
|
+
* started first. Windows adds to a thread's time a scheduler tick (15.6 ms)
|
|
63
|
+
* at a time.
|
|
64
|
+
*/
|
|
65
|
+
const windowsReader = () => {
|
|
66
|
+
const shell = spawn("powershell.exe", [
|
|
67
|
+
"-NoProfile",
|
|
68
|
+
"-NonInteractive",
|
|
69
|
+
"-Command",
|
|
70
|
+
"-"
|
|
71
|
+
], {
|
|
72
|
+
stdio: [
|
|
73
|
+
"pipe",
|
|
74
|
+
"pipe",
|
|
75
|
+
"ignore"
|
|
76
|
+
],
|
|
77
|
+
windowsHide: true
|
|
78
|
+
});
|
|
79
|
+
const waiting = [];
|
|
80
|
+
createInterface({ input: shell.stdout }).on("line", (line) => waiting.shift()?.(Option.some(line)));
|
|
81
|
+
const ended = () => waiting.splice(0).forEach((answer) => answer(Option.none()));
|
|
82
|
+
shell.on("error", ended).on("exit", ended);
|
|
83
|
+
return {
|
|
84
|
+
read: (pids) => new Promise((resolve, reject) => {
|
|
85
|
+
waiting.push((line) => {
|
|
86
|
+
const ticks = Number(Option.getOrElse(line, () => ""));
|
|
87
|
+
if (Option.isSome(line) && Number.isFinite(ticks)) resolve(ticks / 1e4);
|
|
88
|
+
else reject(/* @__PURE__ */ new Error(`workerd's CPU time could not be read: ${Option.getOrElse(line, () => "PowerShell ended")}`));
|
|
89
|
+
});
|
|
90
|
+
shell.stdin.write(`try { (@(${pids.join(",")}) | % { ([Diagnostics.Process]::GetProcessById($_).Threads | Sort-Object StartTime | Select-Object -First 1).TotalProcessorTime.Ticks } | Measure-Object -Sum).Sum } catch { "$_" }\n`);
|
|
91
|
+
}),
|
|
92
|
+
close: () => shell.kill()
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
const readerFor = () => {
|
|
96
|
+
if (process.platform === "linux") return linuxReader();
|
|
97
|
+
if (process.platform === "win32") return windowsReader();
|
|
98
|
+
throw new Error(`The stack reads workerd's CPU time on Linux and Windows, not ${process.platform}.`);
|
|
99
|
+
};
|
|
100
|
+
/** The CPU meter over a stack's workerd processes, its reader made on the first read. */
|
|
101
|
+
const workerdMeter = (origin, dispatch, spawned) => {
|
|
102
|
+
let reader = Option.none();
|
|
103
|
+
return {
|
|
104
|
+
meter: {
|
|
105
|
+
origin,
|
|
106
|
+
fetch: async (request) => dispatch(request.url, {
|
|
107
|
+
method: request.method,
|
|
108
|
+
headers: [...request.headers],
|
|
109
|
+
...request.body === null ? {} : { body: await request.arrayBuffer() }
|
|
110
|
+
}),
|
|
111
|
+
spent: async () => {
|
|
112
|
+
if (spawned.shared) throw new Error("Another stack started in this process while this one did, so whose workerd is whose is unknown: start stacks one at a time to read a stack's CPU.");
|
|
113
|
+
const current = Option.getOrElse(reader, readerFor);
|
|
114
|
+
reader = Option.some(current);
|
|
115
|
+
return current.read(spawned.processes.filter((child) => isWorkerd(child) && isRunning(child)).flatMap((child) => child.pid === void 0 ? [] : [child.pid]));
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
close: () => Option.map(reader, (open) => open.close())
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* Asks each route once, then `runs` times more, one request at a time, each
|
|
123
|
+
* response read in full, and reads what each cost. Throws listing every
|
|
124
|
+
* reading when a route spends more than the budget, or answers a 5xx, whose
|
|
125
|
+
* cost says nothing of the route's.
|
|
126
|
+
*/
|
|
127
|
+
const budget = async (meter, { routes, warm, cold, runs = 5 }) => {
|
|
128
|
+
if (!Number.isInteger(runs) || runs < 1) throw new Error(`Cpu.budget: runs must be a whole number from 1, not ${runs}.`);
|
|
129
|
+
const requestOf = (route) => typeof route === "string" ? new Request(new URL(route, meter.origin)) : route();
|
|
130
|
+
const readings = [];
|
|
131
|
+
for (const [name, route] of Object.entries(routes)) {
|
|
132
|
+
let status = 0;
|
|
133
|
+
const ask = async () => {
|
|
134
|
+
const response = await meter.fetch(requestOf(route));
|
|
135
|
+
await response.arrayBuffer();
|
|
136
|
+
status = response.status;
|
|
137
|
+
if (status >= 500) throw new Error(`Cpu.budget: ${name} answered ${status}.`);
|
|
138
|
+
};
|
|
139
|
+
const start = await meter.spent();
|
|
140
|
+
await ask();
|
|
141
|
+
const first = await meter.spent();
|
|
142
|
+
for (let run = 0; run < runs; run++) await ask();
|
|
143
|
+
const end = await meter.spent();
|
|
144
|
+
readings.push({
|
|
145
|
+
route: name,
|
|
146
|
+
status,
|
|
147
|
+
cold: first - start,
|
|
148
|
+
warm: (end - first) / runs
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
const over = readings.filter((reading) => reading.warm > warm || cold !== void 0 && reading.cold > cold);
|
|
152
|
+
if (over.length > 0) throw new Error(`Over the CPU budget (warm ${warm} ms${cold === void 0 ? "" : `, cold ${cold} ms`}): ${over.map((reading) => reading.route).join(", ")}\n` + readings.map((reading) => ` ${reading.route}: cold ${reading.cold.toFixed(2)} ms, warm ${reading.warm.toFixed(2)} ms (status ${reading.status})`).join("\n"));
|
|
153
|
+
return readings;
|
|
154
|
+
};
|
|
155
|
+
const pause = new Int32Array(new SharedArrayBuffer(4));
|
|
156
|
+
/**
|
|
157
|
+
* Milliseconds this thread has run. Linux books a running thread's time at
|
|
158
|
+
* each scheduler tick (4 ms at 250 Hz), and `threadCpuUsage` reads that
|
|
159
|
+
* book; a millisecond's sleep has the scheduler book the rest, and its own
|
|
160
|
+
* count is in nanoseconds. Read synchronously, so on this thread rather
|
|
161
|
+
* than the pool's.
|
|
162
|
+
*/
|
|
163
|
+
const threadTime = () => {
|
|
164
|
+
if (process.platform === "linux") {
|
|
165
|
+
Atomics.wait(pause, 0, 0, 1);
|
|
166
|
+
return Number(readFileSync("/proc/thread-self/schedstat", "utf8").split(" ")[0]) / 1e6;
|
|
167
|
+
}
|
|
168
|
+
const { user, system } = process.threadCpuUsage();
|
|
169
|
+
return (user + system) / 1e3;
|
|
170
|
+
};
|
|
171
|
+
/**
|
|
172
|
+
* A meter over `answer` run in this process, by this thread's CPU time: for
|
|
173
|
+
* a Worker's handler Node can run (no D1 or other binding of workerd's),
|
|
174
|
+
* whose work happens on the thread that awaits it.
|
|
175
|
+
*/
|
|
176
|
+
const inThread = (answer, origin = "http://localhost") => ({
|
|
177
|
+
origin,
|
|
178
|
+
fetch: answer,
|
|
179
|
+
spent: async () => threadTime()
|
|
180
|
+
});
|
|
181
|
+
/**
|
|
182
|
+
* What a request costs in CPU time, which Miniflare neither limits nor
|
|
183
|
+
* reports, while a deployed Worker fails past its plan's limit (error 1102).
|
|
184
|
+
* `Cpu.budget` reads routes through a meter: a stack's `cpu`, or `Cpu.inThread`.
|
|
185
|
+
*/
|
|
186
|
+
const Cpu = {
|
|
187
|
+
budget,
|
|
188
|
+
inThread
|
|
189
|
+
};
|
|
190
|
+
/**
|
|
191
|
+
* The part of the configuration the stack reads itself. Wrangler validates
|
|
192
|
+
* the whole of it when the harness loads it.
|
|
193
|
+
*/
|
|
194
|
+
const Config = Schema.Struct({
|
|
195
|
+
main: Schema.String,
|
|
196
|
+
compatibility_date: Schema.String,
|
|
197
|
+
no_bundle: Schema.Literal(true),
|
|
198
|
+
d1_databases: Schema.optional(Schema.Array(Schema.Struct({ binding: Schema.String })))
|
|
199
|
+
});
|
|
200
|
+
/**
|
|
201
|
+
* The newest date the workerd Wrangler starts supports, from Wrangler's own
|
|
202
|
+
* Miniflare: an application's own `miniflare` may be another release. Call it
|
|
203
|
+
* after `wrangler()`, which says so when Wrangler is missing.
|
|
204
|
+
*/
|
|
205
|
+
const supportedDate = () => {
|
|
206
|
+
const fromWrangler = createRequire(createRequire(import.meta.url).resolve("wrangler"));
|
|
207
|
+
return Schema.decodeUnknownSync(Schema.Struct({ supportedCompatibilityDate: Schema.String }))(fromWrangler("miniflare")).supportedCompatibilityDate;
|
|
208
|
+
};
|
|
209
|
+
const readConfig = async (path) => {
|
|
210
|
+
const { experimental_readRawConfig } = await wrangler();
|
|
211
|
+
const { rawConfig: raw } = experimental_readRawConfig({ config: path });
|
|
212
|
+
if ("env" in raw) throw new Error(`${path}: the local stack runs the top-level Worker; [env.*] is not read.`);
|
|
213
|
+
const config = Schema.decodeUnknownSync(Config)(raw);
|
|
214
|
+
const supported = supportedDate();
|
|
215
|
+
if (config.compatibility_date > supported) throw new Error(`${path} asks for compatibility_date ${config.compatibility_date}, but the installed workerd supports up to ${supported}. Move wrangler to a release that knows it.`);
|
|
216
|
+
return {
|
|
217
|
+
raw,
|
|
218
|
+
config
|
|
219
|
+
};
|
|
220
|
+
};
|
|
221
|
+
const scriptLoaders = {
|
|
222
|
+
".ts": "ts",
|
|
223
|
+
".mts": "ts",
|
|
224
|
+
".tsx": "tsx",
|
|
225
|
+
".js": "js",
|
|
226
|
+
".mjs": "js",
|
|
227
|
+
".jsx": "jsx"
|
|
228
|
+
};
|
|
229
|
+
const SCRIPT = /\.(?:ts|tsx|js|jsx|mts|mjs)$/;
|
|
230
|
+
const scripts = new Set(Object.values(scriptLoaders));
|
|
231
|
+
const isPackaged = (path) => /[\\/]node_modules[\\/]/.test(path);
|
|
232
|
+
/**
|
|
233
|
+
* The plugins, with the Worker's views branded as `@foldkit/vite-plugin`
|
|
234
|
+
* brands the page's build: each function's result named by its module's path
|
|
235
|
+
* from `root`. Foldkit adopts a served element only where that name is the
|
|
236
|
+
* one the browser's view gives it; unbranded, the Worker's page was rebuilt on
|
|
237
|
+
* every hydration, silently in production (#412).
|
|
238
|
+
*
|
|
239
|
+
* Vite brands what any load returns, but esbuild takes the first `onLoad`
|
|
240
|
+
* that answers, so each plugin's is wrapped and a module it loads (with
|
|
241
|
+
* `pnpm mutate`'s edits, say) is branded too. `foldkit/brand` resolves from
|
|
242
|
+
* `root`, as the page's does; a root that resolves none has no Foldkit page
|
|
243
|
+
* to match, and the Worker is bundled as written.
|
|
244
|
+
*/
|
|
245
|
+
const viewIdentity = (root, plugins) => {
|
|
246
|
+
let brand = Option.none();
|
|
247
|
+
const resolving = Symbol("foldkit/brand");
|
|
248
|
+
const brands = ({ path, namespace }) => Option.isSome(brand) && namespace === "file" && SCRIPT.test(path) && !isPackaged(path);
|
|
249
|
+
/** `contents` branded, or none where nothing in them returns. */
|
|
250
|
+
const branded = async (contents, loader, path) => {
|
|
251
|
+
const { transform } = await esbuild();
|
|
252
|
+
const { transformViewIdentity } = await foldkitVite();
|
|
253
|
+
const { code } = await transform(contents, {
|
|
254
|
+
loader,
|
|
255
|
+
sourcefile: path
|
|
256
|
+
});
|
|
257
|
+
return Option.fromNullOr(transformViewIdentity(code, path, root)).pipe(Option.map((result) => result.code));
|
|
258
|
+
};
|
|
259
|
+
const brandedResult = async (result, args) => {
|
|
260
|
+
const loader = result.loader ?? scriptLoaders[extname(args.path)];
|
|
261
|
+
const { contents } = result;
|
|
262
|
+
if (!brands(args) || contents === void 0 || loader === void 0 || !scripts.has(loader)) return result;
|
|
263
|
+
return Option.match(await branded(contents, loader, args.path), {
|
|
264
|
+
onNone: () => result,
|
|
265
|
+
onSome: (contents) => ({
|
|
266
|
+
...result,
|
|
267
|
+
contents,
|
|
268
|
+
loader: "js"
|
|
269
|
+
})
|
|
270
|
+
});
|
|
271
|
+
};
|
|
272
|
+
const wrapped = (plugin) => ({
|
|
273
|
+
...plugin,
|
|
274
|
+
setup: (build) => plugin.setup({
|
|
275
|
+
...build,
|
|
276
|
+
onLoad: (options, load) => build.onLoad(options, async (args) => {
|
|
277
|
+
const result = await load(args);
|
|
278
|
+
return result === null || result === void 0 ? result : brandedResult(result, args);
|
|
279
|
+
})
|
|
280
|
+
})
|
|
281
|
+
});
|
|
282
|
+
return [
|
|
283
|
+
{
|
|
284
|
+
name: "foldkit-view-identity",
|
|
285
|
+
setup: (build) => {
|
|
286
|
+
build.onStart(async () => {
|
|
287
|
+
const found = await build.resolve("foldkit/brand", {
|
|
288
|
+
kind: "import-statement",
|
|
289
|
+
resolveDir: root,
|
|
290
|
+
pluginData: resolving
|
|
291
|
+
});
|
|
292
|
+
brand = found.errors.length === 0 ? Option.some(found.path) : Option.none();
|
|
293
|
+
});
|
|
294
|
+
build.onResolve({ filter: /^foldkit\/brand$/ }, (args) => args.pluginData === resolving ? void 0 : Option.match(brand, {
|
|
295
|
+
onNone: () => void 0,
|
|
296
|
+
onSome: (path) => ({ path })
|
|
297
|
+
}));
|
|
298
|
+
}
|
|
299
|
+
},
|
|
300
|
+
...plugins.map(wrapped),
|
|
301
|
+
{
|
|
302
|
+
name: "foldkit-view-identity-load",
|
|
303
|
+
setup: (build) => build.onLoad({ filter: SCRIPT }, async (args) => {
|
|
304
|
+
const loader = scriptLoaders[extname(args.path)];
|
|
305
|
+
if (!brands(args) || loader === void 0) return void 0;
|
|
306
|
+
const code = await branded(await readFile(args.path), loader, args.path);
|
|
307
|
+
return Option.match(code, {
|
|
308
|
+
onNone: () => void 0,
|
|
309
|
+
onSome: (contents) => ({
|
|
310
|
+
contents,
|
|
311
|
+
loader: "js"
|
|
312
|
+
})
|
|
313
|
+
});
|
|
314
|
+
})
|
|
315
|
+
}
|
|
316
|
+
];
|
|
317
|
+
};
|
|
318
|
+
/**
|
|
319
|
+
* How the entry is bundled, for the stack and for `wrangler deploy`: the
|
|
320
|
+
* script at `outfile`, and beside it any `.wasm` it imports, which `no_bundle`
|
|
321
|
+
* Wrangler loads as a compiled module. `node:*` stays external for
|
|
322
|
+
* `nodejs_compat`.
|
|
323
|
+
*/
|
|
324
|
+
const bundleOptions = (options, outfile) => ({
|
|
325
|
+
entryPoints: [options.entry],
|
|
326
|
+
bundle: true,
|
|
327
|
+
format: "esm",
|
|
328
|
+
platform: "browser",
|
|
329
|
+
conditions: ["foldkit-plus:source"],
|
|
330
|
+
external: ["cloudflare:workers", "node:*"],
|
|
331
|
+
loader: { ".wasm": "copy" },
|
|
332
|
+
plugins: viewIdentity(resolve(options.pageRoot ?? dirname(options.wrangler)), options.plugins ?? []),
|
|
333
|
+
outdir: dirname(outfile),
|
|
334
|
+
entryNames: basename(outfile, ".js"),
|
|
335
|
+
write: false,
|
|
336
|
+
logLevel: "silent"
|
|
337
|
+
});
|
|
338
|
+
const writeOutputs = async (files) => {
|
|
339
|
+
for (const file of files) {
|
|
340
|
+
await mkdir(dirname(file.path), { recursive: true });
|
|
341
|
+
await writeFile(file.path, file.contents);
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
const identityOf = (files) => files.map((file) => `${file.path}:${file.hash}`).join();
|
|
345
|
+
/** Node's console method for each level workerd writes; any other prints as a log. */
|
|
346
|
+
const printers = /* @__PURE__ */ new Map([
|
|
347
|
+
["debug", (line) => console.debug(line)],
|
|
348
|
+
["info", (line) => console.info(line)],
|
|
349
|
+
["warn", (line) => console.warn(line)],
|
|
350
|
+
["error", (line) => console.error(line)]
|
|
351
|
+
]);
|
|
352
|
+
const print = (log) => (printers.get(log.level) ?? ((line) => console.log(line)))(`[worker] ${log.message}`);
|
|
353
|
+
const errorText = (error) => error instanceof Error ? error.message : String(error);
|
|
354
|
+
/** Writes the bundle where `main` says, for `wrangler deploy`. */
|
|
355
|
+
const writeBundle = async (options) => {
|
|
356
|
+
const { config } = await readConfig(options.wrangler);
|
|
357
|
+
const outfile = resolve(dirname(options.wrangler), config.main);
|
|
358
|
+
const { build } = await esbuild();
|
|
359
|
+
await writeOutputs((await build(bundleOptions(options, outfile))).outputFiles);
|
|
360
|
+
};
|
|
361
|
+
const migrate = async (worker, config) => {
|
|
362
|
+
for (const database of config.d1_databases ?? []) await worker.applyD1Migrations(database.binding);
|
|
363
|
+
};
|
|
364
|
+
/**
|
|
365
|
+
* Starts the Worker with a fresh, migrated D1 for each `[[d1_databases]]`,
|
|
366
|
+
* and empty R2 and Durable Object storage. Each call is a stack of its own,
|
|
367
|
+
* so test files running side by side share nothing.
|
|
368
|
+
*/
|
|
369
|
+
const start = async (options) => {
|
|
370
|
+
const { raw, config } = await readConfig(options.wrangler);
|
|
371
|
+
const { build, context } = await esbuild();
|
|
372
|
+
const { createTestHarness } = await wrangler();
|
|
373
|
+
const directory = join(tmpdir(), `foldkit-workers-${crypto.randomUUID()}`);
|
|
374
|
+
const outfile = join(directory, basename(config.main));
|
|
375
|
+
const trialDirectory = join(directory, "trial");
|
|
376
|
+
const bundle = bundleOptions(options, outfile);
|
|
377
|
+
const first = (await build(bundle)).outputFiles;
|
|
378
|
+
const optionsFor = (raw, main, vars) => ({
|
|
379
|
+
root: dirname(options.wrangler),
|
|
380
|
+
workers: [{
|
|
381
|
+
config: {
|
|
382
|
+
...raw,
|
|
383
|
+
main
|
|
384
|
+
},
|
|
385
|
+
vars,
|
|
386
|
+
secrets: options.secrets
|
|
387
|
+
}]
|
|
388
|
+
});
|
|
389
|
+
const given = options.vars;
|
|
390
|
+
let vars = typeof given === "function" ? void 0 : given;
|
|
391
|
+
const harness = createTestHarness(optionsFor(raw, outfile, vars));
|
|
392
|
+
const spawned = {
|
|
393
|
+
processes: [],
|
|
394
|
+
shared: false
|
|
395
|
+
};
|
|
396
|
+
const trials = {
|
|
397
|
+
processes: [],
|
|
398
|
+
shared: false
|
|
399
|
+
};
|
|
400
|
+
let closeMeter = () => {};
|
|
401
|
+
const kept = [];
|
|
402
|
+
const drain = () => {
|
|
403
|
+
const fresh = harness.getLogs();
|
|
404
|
+
harness.clearLogs();
|
|
405
|
+
if (options.console !== "quiet") fresh.forEach(print);
|
|
406
|
+
kept.push(...fresh);
|
|
407
|
+
};
|
|
408
|
+
const draining = setInterval(drain, 100);
|
|
409
|
+
draining.unref();
|
|
410
|
+
let watching = Option.none();
|
|
411
|
+
let configWatcher = Option.none();
|
|
412
|
+
let rereading = Option.none();
|
|
413
|
+
let swaps = Promise.resolve();
|
|
414
|
+
let stopping = Option.none();
|
|
415
|
+
const dispose = async (watcher) => {
|
|
416
|
+
const opened = await watcher.then(Option.some, () => Option.none());
|
|
417
|
+
if (Option.isSome(opened)) await opened.value.dispose();
|
|
418
|
+
};
|
|
419
|
+
const stopOnce = async () => {
|
|
420
|
+
clearInterval(draining);
|
|
421
|
+
closeMeter();
|
|
422
|
+
Option.map(rereading, clearTimeout);
|
|
423
|
+
Option.map(configWatcher, (watcher) => watcher.close());
|
|
424
|
+
try {
|
|
425
|
+
const watcher = watching;
|
|
426
|
+
watching = Option.none();
|
|
427
|
+
if (Option.isSome(watcher)) await dispose(watcher.value);
|
|
428
|
+
await swaps;
|
|
429
|
+
} finally {
|
|
430
|
+
try {
|
|
431
|
+
await harness.close();
|
|
432
|
+
} finally {
|
|
433
|
+
drain();
|
|
434
|
+
await rm(directory, {
|
|
435
|
+
recursive: true,
|
|
436
|
+
force: true
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
};
|
|
441
|
+
const stop = () => Option.getOrElse(stopping, () => {
|
|
442
|
+
const once = stopOnce();
|
|
443
|
+
stopping = Option.some(once);
|
|
444
|
+
return once;
|
|
445
|
+
});
|
|
446
|
+
try {
|
|
447
|
+
await writeOutputs(first);
|
|
448
|
+
const { url } = await collect(spawned, () => harness.listen());
|
|
449
|
+
if (typeof given === "function") {
|
|
450
|
+
vars = given(url.origin);
|
|
451
|
+
await collect(spawned, () => harness.update(optionsFor(raw, outfile, vars)));
|
|
452
|
+
}
|
|
453
|
+
const worker = harness.getWorker();
|
|
454
|
+
const metered = workerdMeter(url.origin, worker.fetch, spawned);
|
|
455
|
+
closeMeter = metered.close;
|
|
456
|
+
await migrate(worker, config);
|
|
457
|
+
const watch$1 = async (listener) => {
|
|
458
|
+
if (Option.isSome(stopping)) throw new Error("This stack has stopped.");
|
|
459
|
+
if (Option.isSome(watching)) throw new Error("This stack is already watching.");
|
|
460
|
+
let running = Option.some({
|
|
461
|
+
raw,
|
|
462
|
+
config,
|
|
463
|
+
files: first
|
|
464
|
+
});
|
|
465
|
+
/**
|
|
466
|
+
* Starts `next` on a workerd of its own, then swaps it in. A Worker
|
|
467
|
+
* that does not start is refused there, and the stack's harness, which
|
|
468
|
+
* a failed update tears down with its storage, never sees it.
|
|
469
|
+
*/
|
|
470
|
+
const swap = async (next) => {
|
|
471
|
+
try {
|
|
472
|
+
await writeOutputs(next.files.map((file) => ({
|
|
473
|
+
path: join(trialDirectory, relative(directory, file.path)),
|
|
474
|
+
contents: file.contents
|
|
475
|
+
})));
|
|
476
|
+
const trial = createTestHarness(optionsFor(next.raw, join(trialDirectory, basename(outfile)), vars));
|
|
477
|
+
try {
|
|
478
|
+
await collect(trials, () => trial.listen());
|
|
479
|
+
await migrate(trial.getWorker(), next.config);
|
|
480
|
+
} finally {
|
|
481
|
+
await trial.close();
|
|
482
|
+
}
|
|
483
|
+
} catch (error) {
|
|
484
|
+
listener.onRefused(errorText(error));
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
try {
|
|
488
|
+
await writeOutputs(next.files);
|
|
489
|
+
await collect(spawned, () => harness.update(optionsFor(next.raw, outfile, vars)));
|
|
490
|
+
} catch (error) {
|
|
491
|
+
running = Option.none();
|
|
492
|
+
listener.onStopped(errorText(error));
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
running = Option.some(next);
|
|
496
|
+
try {
|
|
497
|
+
await migrate(harness.getWorker(), next.config);
|
|
498
|
+
} catch (error) {
|
|
499
|
+
listener.onRefused(`The Worker was swapped in, but a migration failed: ${errorText(error)}`);
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
listener.onSwap();
|
|
503
|
+
};
|
|
504
|
+
/** Queues a swap of what `change` makes of the running stack, which may be none. */
|
|
505
|
+
const enqueue = (change) => {
|
|
506
|
+
const step = swaps.then(async () => {
|
|
507
|
+
if (Option.isNone(running) || Option.isSome(stopping)) return;
|
|
508
|
+
const next = await change(running.value);
|
|
509
|
+
if (Option.isSome(next)) await swap(next.value);
|
|
510
|
+
});
|
|
511
|
+
swaps = step.catch(() => void 0);
|
|
512
|
+
return step;
|
|
513
|
+
};
|
|
514
|
+
const watcher = context({
|
|
515
|
+
...bundle,
|
|
516
|
+
plugins: [...bundle.plugins, {
|
|
517
|
+
name: "foldkit-swap",
|
|
518
|
+
setup: (build) => build.onEnd((result) => enqueue(async (now) => {
|
|
519
|
+
if (result.errors.length > 0) {
|
|
520
|
+
listener.onRefused(result.errors.map((error) => error.text).join("\n"));
|
|
521
|
+
return Option.none();
|
|
522
|
+
}
|
|
523
|
+
const files = result.outputFiles ?? [];
|
|
524
|
+
if (files.some((file) => file.path === outfile && file.contents.length === 0)) {
|
|
525
|
+
listener.onRefused(`${options.entry} bundles to no code, so no Worker could start.`);
|
|
526
|
+
return Option.none();
|
|
527
|
+
}
|
|
528
|
+
return identityOf(files) === identityOf(now.files) ? Option.none() : Option.some({
|
|
529
|
+
...now,
|
|
530
|
+
files
|
|
531
|
+
});
|
|
532
|
+
}))
|
|
533
|
+
}]
|
|
534
|
+
});
|
|
535
|
+
watching = Option.some(watcher);
|
|
536
|
+
try {
|
|
537
|
+
await (await watcher).watch();
|
|
538
|
+
} catch (error) {
|
|
539
|
+
if (Option.contains(watching, watcher)) {
|
|
540
|
+
watching = Option.none();
|
|
541
|
+
await dispose(watcher);
|
|
542
|
+
}
|
|
543
|
+
throw error;
|
|
544
|
+
}
|
|
545
|
+
if (Option.isSome(stopping)) return;
|
|
546
|
+
const reread = () => enqueue(async (now) => {
|
|
547
|
+
return (await readConfig(options.wrangler).then(Option.some, (error) => {
|
|
548
|
+
listener.onRefused(errorText(error));
|
|
549
|
+
return Option.none();
|
|
550
|
+
})).pipe(Option.filter(({ raw }) => JSON.stringify(raw) !== JSON.stringify(now.raw)), Option.map(({ raw, config }) => ({
|
|
551
|
+
...now,
|
|
552
|
+
raw,
|
|
553
|
+
config
|
|
554
|
+
})));
|
|
555
|
+
});
|
|
556
|
+
const name = basename(options.wrangler);
|
|
557
|
+
configWatcher = Option.some(watch(dirname(options.wrangler), (_, changed) => {
|
|
558
|
+
if (changed !== name) return;
|
|
559
|
+
Option.map(rereading, clearTimeout);
|
|
560
|
+
rereading = Option.some(setTimeout(() => void reread(), 50));
|
|
561
|
+
}).on("error", (error) => listener.onRefused(`The configuration is no longer watched: ${errorText(error)}`)));
|
|
562
|
+
};
|
|
563
|
+
const env = () => harness.getWorker().getEnv();
|
|
564
|
+
const logs = () => {
|
|
565
|
+
drain();
|
|
566
|
+
return kept.splice(0);
|
|
567
|
+
};
|
|
568
|
+
return {
|
|
569
|
+
origin: url.origin,
|
|
570
|
+
env,
|
|
571
|
+
dispatch: worker.fetch,
|
|
572
|
+
logs,
|
|
573
|
+
cpu: metered.meter,
|
|
574
|
+
watch: watch$1,
|
|
575
|
+
stop
|
|
576
|
+
};
|
|
577
|
+
} catch (error) {
|
|
578
|
+
await stop().catch(() => void 0);
|
|
579
|
+
throw error;
|
|
580
|
+
}
|
|
581
|
+
};
|
|
582
|
+
/**
|
|
583
|
+
* The local Workers stack. `Stack.start` runs the Worker for tests and
|
|
584
|
+
* development; `Stack.writeBundle` writes what `wrangler deploy` uploads,
|
|
585
|
+
* bundled the same way.
|
|
586
|
+
*/
|
|
587
|
+
const Stack = {
|
|
588
|
+
start,
|
|
589
|
+
writeBundle
|
|
590
|
+
};
|
|
591
|
+
export { Cpu, Stack };
|
|
592
|
+
|
|
593
|
+
//# sourceMappingURL=workers.mjs.map
|