wrangler 2.0.28 → 2.0.29

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.
@@ -0,0 +1,412 @@
1
+ import { fork } from "node:child_process";
2
+ import { realpathSync } from "node:fs";
3
+ import { writeFile } from "node:fs/promises";
4
+ import * as path from "node:path";
5
+ import onExit from "signal-exit";
6
+ import tmp from "tmp-promise";
7
+ import { bundleWorker } from "../bundle";
8
+ import { registerWorker } from "../dev-registry";
9
+ import { runCustomBuild } from "../entry";
10
+ import { logger } from "../logger";
11
+ import { waitForPortToBeAvailable } from "../proxy";
12
+ import {
13
+ setupBindings,
14
+ setupMiniflareOptions,
15
+ setupNodeOptions,
16
+ } from "./local";
17
+ import { validateDevProps } from "./validate-dev-props";
18
+
19
+ import type { Config } from "../config";
20
+ import type { Entry } from "../entry";
21
+ import type { DevProps, DirectorySyncResult } from "./dev";
22
+ import type { LocalProps } from "./local";
23
+ import type { EsbuildBundle } from "./use-esbuild";
24
+
25
+ import type { ChildProcess } from "node:child_process";
26
+
27
+ export async function startDevServer(
28
+ props: DevProps & {
29
+ local: boolean;
30
+ }
31
+ ) {
32
+ try {
33
+ validateDevProps(props);
34
+
35
+ if (props.build.command) {
36
+ const relativeFile =
37
+ path.relative(props.entry.directory, props.entry.file) || ".";
38
+ await runCustomBuild(props.entry.file, relativeFile, props.build).catch(
39
+ (err) => {
40
+ logger.error("Custom build failed:", err);
41
+ }
42
+ );
43
+ }
44
+
45
+ //implement a react-free version of useTmpDir
46
+ const directory = setupTempDir();
47
+ if (!directory) {
48
+ throw new Error("Failed to create temporary directory.");
49
+ }
50
+
51
+ //implement a react-free version of useEsbuild
52
+ const bundle = await runEsbuild({
53
+ entry: props.entry,
54
+ destination: directory.name,
55
+ jsxFactory: props.jsxFactory,
56
+ rules: props.rules,
57
+ jsxFragment: props.jsxFragment,
58
+ serveAssetsFromWorker: Boolean(
59
+ props.assetPaths && !props.isWorkersSite && props.local
60
+ ),
61
+ tsconfig: props.tsconfig,
62
+ minify: props.minify,
63
+ nodeCompat: props.nodeCompat,
64
+ define: props.define,
65
+ noBundle: props.noBundle,
66
+ assets: props.assetsConfig,
67
+ services: props.bindings.services,
68
+ });
69
+
70
+ //run local now
71
+ const { stop, inspectorUrl } = await startLocalServer({
72
+ name: props.name,
73
+ bundle: bundle,
74
+ format: props.entry.format,
75
+ compatibilityDate: props.compatibilityDate,
76
+ compatibilityFlags: props.compatibilityFlags,
77
+ bindings: props.bindings,
78
+ assetPaths: props.assetPaths,
79
+ port: props.port,
80
+ ip: props.ip,
81
+ rules: props.rules,
82
+ inspectorPort: props.inspectorPort,
83
+ enableLocalPersistence: props.enableLocalPersistence,
84
+ liveReload: props.liveReload,
85
+ crons: props.crons,
86
+ localProtocol: props.localProtocol,
87
+ localUpstream: props.localUpstream,
88
+ logLevel: props.logLevel,
89
+ logPrefix: props.logPrefix,
90
+ inspect: props.inspect,
91
+ onReady: props.onReady,
92
+ enablePagesAssetsServiceBinding: props.enablePagesAssetsServiceBinding,
93
+ usageModel: undefined,
94
+ workerDefinitions: undefined,
95
+ });
96
+
97
+ return {
98
+ stop: async () => {
99
+ stop();
100
+ },
101
+ inspectorUrl,
102
+ };
103
+ } catch (err) {
104
+ logger.error(err);
105
+ }
106
+ }
107
+
108
+ function setupTempDir(): DirectorySyncResult | undefined {
109
+ let dir: DirectorySyncResult | undefined;
110
+ try {
111
+ dir = tmp.dirSync({ unsafeCleanup: true });
112
+
113
+ return dir;
114
+ } catch (err) {
115
+ logger.error("Failed to create temporary directory to store built files.");
116
+ }
117
+ }
118
+
119
+ async function runEsbuild({
120
+ entry,
121
+ destination,
122
+ jsxFactory,
123
+ jsxFragment,
124
+ rules,
125
+ assets,
126
+ serveAssetsFromWorker,
127
+ tsconfig,
128
+ minify,
129
+ nodeCompat,
130
+ define,
131
+ noBundle,
132
+ }: {
133
+ entry: Entry;
134
+ destination: string | undefined;
135
+ jsxFactory: string | undefined;
136
+ jsxFragment: string | undefined;
137
+ rules: Config["rules"];
138
+ assets: Config["assets"];
139
+ define: Config["define"];
140
+ services: Config["services"];
141
+ serveAssetsFromWorker: boolean;
142
+ tsconfig: string | undefined;
143
+ minify: boolean | undefined;
144
+ nodeCompat: boolean | undefined;
145
+ noBundle: boolean;
146
+ }): Promise<EsbuildBundle | undefined> {
147
+ if (!destination) return;
148
+
149
+ const {
150
+ resolvedEntryPointPath,
151
+ bundleType,
152
+ modules,
153
+ sourceMapPath,
154
+ }: Awaited<ReturnType<typeof bundleWorker>> = noBundle
155
+ ? {
156
+ modules: [],
157
+ resolvedEntryPointPath: entry.file,
158
+ bundleType: entry.format === "modules" ? "esm" : "commonjs",
159
+ stop: undefined,
160
+ sourceMapPath: undefined,
161
+ }
162
+ : await bundleWorker(entry, destination, {
163
+ serveAssetsFromWorker,
164
+ jsxFactory,
165
+ jsxFragment,
166
+ rules,
167
+ tsconfig,
168
+ minify,
169
+ nodeCompat,
170
+ define,
171
+ checkFetch: true,
172
+ assets: assets && {
173
+ ...assets,
174
+ // disable the cache in dev
175
+ bypassCache: true,
176
+ },
177
+ services: undefined,
178
+ workerDefinitions: undefined,
179
+ firstPartyWorkerDevFacade: undefined,
180
+ });
181
+
182
+ return {
183
+ id: 0,
184
+ entry,
185
+ path: resolvedEntryPointPath,
186
+ type: bundleType,
187
+ modules,
188
+ sourceMapPath,
189
+ };
190
+ }
191
+
192
+ export async function startLocalServer({
193
+ name: workerName,
194
+ bundle,
195
+ format,
196
+ compatibilityDate,
197
+ compatibilityFlags,
198
+ usageModel,
199
+ bindings,
200
+ workerDefinitions,
201
+ assetPaths,
202
+ port,
203
+ inspectorPort,
204
+ rules,
205
+ enableLocalPersistence,
206
+ liveReload,
207
+ ip,
208
+ crons,
209
+ localProtocol,
210
+ localUpstream,
211
+ inspect,
212
+ onReady,
213
+ logLevel,
214
+ logPrefix,
215
+ enablePagesAssetsServiceBinding,
216
+ }: LocalProps) {
217
+ let local: ChildProcess | undefined;
218
+ let removeSignalExitListener: (() => void) | undefined;
219
+ let inspectorUrl: string | undefined;
220
+ const setInspectorUrl = (url: string) => {
221
+ inspectorUrl = url;
222
+ };
223
+
224
+ // if we're using local persistence for data, we should use the cwd
225
+ // as an explicit path, or else it'll use the temp dir
226
+ // which disappears when dev ends
227
+ const localPersistencePath = enableLocalPersistence
228
+ ? // Maybe we could make the path configurable as well?
229
+ path.join(process.cwd(), "wrangler-local-state")
230
+ : // We otherwise choose null, but choose true later
231
+ // so that it's persisted in the temp dir across a dev session
232
+ // even when we change source and reload
233
+ null;
234
+
235
+ const abortController = new AbortController();
236
+ async function startLocalWorker() {
237
+ if (!bundle || !format) return;
238
+
239
+ // port for the worker
240
+ await waitForPortToBeAvailable(port, {
241
+ retryPeriod: 200,
242
+ timeout: 2000,
243
+ abortSignal: abortController.signal,
244
+ });
245
+
246
+ if (bindings.services && bindings.services.length > 0) {
247
+ throw new Error(
248
+ "⎔ Service bindings are not yet supported in local mode."
249
+ );
250
+ }
251
+
252
+ // In local mode, we want to copy all referenced modules into
253
+ // the output bundle directory before starting up
254
+ for (const module of bundle.modules) {
255
+ await writeFile(
256
+ path.join(path.dirname(bundle.path), module.name),
257
+ module.content
258
+ );
259
+ }
260
+
261
+ const scriptPath = realpathSync(bundle.path);
262
+
263
+ const upstream =
264
+ typeof localUpstream === "string"
265
+ ? `${localProtocol}://${localUpstream}`
266
+ : undefined;
267
+
268
+ const {
269
+ externalDurableObjects,
270
+ internalDurableObjects,
271
+ wasmBindings,
272
+ textBlobBindings,
273
+ dataBlobBindings,
274
+ } = setupBindings({
275
+ wasm_modules: bindings.wasm_modules,
276
+ text_blobs: bindings.text_blobs,
277
+ data_blobs: bindings.data_blobs,
278
+ durable_objects: bindings.durable_objects,
279
+ format,
280
+ bundle,
281
+ });
282
+
283
+ const { forkOptions, miniflareCLIPath } = setupMiniflareOptions({
284
+ workerName,
285
+ port,
286
+ scriptPath,
287
+ localProtocol,
288
+ ip,
289
+ format,
290
+ rules,
291
+ compatibilityDate,
292
+ compatibilityFlags,
293
+ usageModel,
294
+ kv_namespaces: bindings?.kv_namespaces,
295
+ r2_buckets: bindings?.r2_buckets,
296
+ internalDurableObjects,
297
+ externalDurableObjects,
298
+ localPersistencePath,
299
+ liveReload,
300
+ assetPaths,
301
+ vars: bindings?.vars,
302
+ wasmBindings,
303
+ textBlobBindings,
304
+ dataBlobBindings,
305
+ crons,
306
+ upstream,
307
+ logLevel,
308
+ logPrefix,
309
+ workerDefinitions,
310
+ enablePagesAssetsServiceBinding,
311
+ });
312
+
313
+ const nodeOptions = setupNodeOptions({ inspect, ip, inspectorPort });
314
+ logger.log("⎔ Starting a local server...");
315
+
316
+ const child = (local = fork(miniflareCLIPath, forkOptions, {
317
+ cwd: path.dirname(scriptPath),
318
+ execArgv: nodeOptions,
319
+ stdio: "pipe",
320
+ }));
321
+
322
+ child.on("message", async (messageString) => {
323
+ const message = JSON.parse(messageString as string);
324
+ if (message.ready) {
325
+ // Let's register our presence in the dev registry
326
+ if (workerName) {
327
+ await registerWorker(workerName, {
328
+ protocol: localProtocol,
329
+ mode: "local",
330
+ port,
331
+ host: ip,
332
+ durableObjects: internalDurableObjects.map((binding) => ({
333
+ name: binding.name,
334
+ className: binding.class_name,
335
+ })),
336
+ ...(message.durableObjectsPort
337
+ ? {
338
+ durableObjectsHost: ip,
339
+ durableObjectsPort: message.durableObjectsPort,
340
+ }
341
+ : {}),
342
+ });
343
+ }
344
+ onReady?.();
345
+ }
346
+ });
347
+
348
+ child.on("close", (code) => {
349
+ if (code) {
350
+ logger.log(`Miniflare process exited with code ${code}`);
351
+ }
352
+ });
353
+
354
+ child.stdout?.on("data", (data: Buffer) => {
355
+ process.stdout.write(data);
356
+ });
357
+
358
+ // parse the node inspector url (which may be received in chunks) from stderr
359
+ let stderrData = "";
360
+ let inspectorUrlFound = false;
361
+ child.stderr?.on("data", (data: Buffer) => {
362
+ if (!inspectorUrlFound) {
363
+ stderrData += data.toString();
364
+ const matches =
365
+ /Debugger listening on (ws:\/\/127\.0\.0\.1:\d+\/[A-Za-z0-9-]+)[\r|\n]/.exec(
366
+ stderrData
367
+ );
368
+ if (matches) {
369
+ inspectorUrlFound = true;
370
+ setInspectorUrl(matches[1]);
371
+ }
372
+ }
373
+
374
+ process.stderr.write(data);
375
+ });
376
+
377
+ child.on("exit", (code) => {
378
+ if (code) {
379
+ logger.error(`Miniflare process exited with code ${code}`);
380
+ }
381
+ });
382
+
383
+ child.on("error", (error: Error) => {
384
+ logger.error(`Miniflare process failed to spawn`);
385
+ logger.error(error);
386
+ });
387
+
388
+ removeSignalExitListener = onExit((_code, _signal) => {
389
+ logger.log("⎔ Shutting down local server.");
390
+ child.kill();
391
+ local = undefined;
392
+ });
393
+ }
394
+
395
+ startLocalWorker().catch((err) => {
396
+ logger.error("local worker:", err);
397
+ });
398
+
399
+ return {
400
+ inspectorUrl,
401
+ stop: () => {
402
+ abortController.abort();
403
+ if (local) {
404
+ logger.log("⎔ Shutting down local server.");
405
+ local.kill();
406
+ local = undefined;
407
+ removeSignalExitListener && removeSignalExitListener();
408
+ removeSignalExitListener = undefined;
409
+ }
410
+ },
411
+ };
412
+ }