@cloudflare/deploy-helpers 0.11.2 → 0.12.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/dist/{chunk-3G6JZQDH.mjs → chunk-QBZBEHZB.mjs} +0 -7
- package/dist/create-worker-upload-form.mjs +1 -1
- package/dist/index.d.mts +39 -8
- package/dist/index.mjs +309 -146
- package/dist/metafile-esm.json +1 -1
- package/dist/startup-profile.d.mts +45 -0
- package/dist/startup-profile.mjs +646 -0
- package/package.json +12 -6
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Protocol } from 'devtools-protocol';
|
|
2
|
+
import { FormData } from 'undici';
|
|
3
|
+
|
|
4
|
+
interface StartupProfileSummary {
|
|
5
|
+
profileWindow: number;
|
|
6
|
+
sampledTime: number;
|
|
7
|
+
activeTime: number;
|
|
8
|
+
garbageCollectionTime: number;
|
|
9
|
+
idleTime: number;
|
|
10
|
+
sampleCount: number;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Parses a multipart Worker upload from disk, or returns an existing upload.
|
|
14
|
+
*
|
|
15
|
+
* @param workerBundle - A serialized upload path or an already parsed upload.
|
|
16
|
+
* @returns The parsed multipart Worker upload.
|
|
17
|
+
*/
|
|
18
|
+
declare function parseWorkerBundle(workerBundle: string | FormData): Promise<FormData>;
|
|
19
|
+
/**
|
|
20
|
+
* Measures the uncompressed and gzip-compressed size of a Worker upload.
|
|
21
|
+
* Source maps and metadata are excluded from both measurements.
|
|
22
|
+
*
|
|
23
|
+
* @param workerBundle - A parsed multipart Worker upload.
|
|
24
|
+
* @returns Raw and compressed byte counts.
|
|
25
|
+
*/
|
|
26
|
+
declare function getBundleSize(workerBundle: FormData): Promise<{
|
|
27
|
+
size: number;
|
|
28
|
+
gzipSize: number;
|
|
29
|
+
}>;
|
|
30
|
+
/**
|
|
31
|
+
* Summarizes the timing samples in a Worker startup CPU profile.
|
|
32
|
+
*
|
|
33
|
+
* @param profile - A Chrome DevTools Protocol CPU profile.
|
|
34
|
+
* @returns Aggregate startup, active, idle, and garbage-collection timings.
|
|
35
|
+
*/
|
|
36
|
+
declare function summarizeStartupProfile(profile: Protocol.Profiler.Profile): StartupProfileSummary;
|
|
37
|
+
/**
|
|
38
|
+
* Runs a Worker upload locally and records its module-evaluation CPU profile.
|
|
39
|
+
*
|
|
40
|
+
* @param workerBundle - A serialized upload path or a parsed multipart upload.
|
|
41
|
+
* @returns A Chrome DevTools Protocol CPU profile.
|
|
42
|
+
*/
|
|
43
|
+
declare function analyseBundle(workerBundle: string | FormData): Promise<Protocol.Profiler.Profile>;
|
|
44
|
+
|
|
45
|
+
export { type StartupProfileSummary, analyseBundle, getBundleSize, parseWorkerBundle, summarizeStartupProfile };
|
|
@@ -0,0 +1,646 @@
|
|
|
1
|
+
import { fromMimeType } from './chunk-QBZBEHZB.mjs';
|
|
2
|
+
import { __name } from './chunk-Q72B4Q5Z.mjs';
|
|
3
|
+
import { randomUUID } from 'node:crypto';
|
|
4
|
+
import { readFile } from 'node:fs/promises';
|
|
5
|
+
import { setTimeout, clearTimeout } from 'node:timers';
|
|
6
|
+
import { gzipSync } from 'node:zlib';
|
|
7
|
+
import { UserError } from '@cloudflare/workers-utils';
|
|
8
|
+
import { Miniflare, convertV4MiniflareOptions } from 'miniflare';
|
|
9
|
+
import { Response } from 'undici';
|
|
10
|
+
import { WebSocket } from 'ws';
|
|
11
|
+
|
|
12
|
+
var STARTUP_PROFILE_TIMEOUT_MS = 1e4;
|
|
13
|
+
var MODULE_TYPE_TO_MINIFLARE_TYPE = {
|
|
14
|
+
esm: "ESModule",
|
|
15
|
+
commonjs: "CommonJS",
|
|
16
|
+
"compiled-wasm": "CompiledWasm",
|
|
17
|
+
buffer: "Data",
|
|
18
|
+
text: "Text",
|
|
19
|
+
python: "PythonModule",
|
|
20
|
+
"python-requirement": "PythonRequirement"
|
|
21
|
+
};
|
|
22
|
+
async function parseWorkerBundle(workerBundle) {
|
|
23
|
+
if (typeof workerBundle !== "string") {
|
|
24
|
+
return workerBundle;
|
|
25
|
+
}
|
|
26
|
+
const bundle = await readFile(workerBundle);
|
|
27
|
+
const firstLine = bundle.findIndex((value) => value === 10);
|
|
28
|
+
const boundary = Uint8Array.prototype.slice.call(bundle, 2, firstLine).toString();
|
|
29
|
+
const response = new Response(bundle, {
|
|
30
|
+
headers: {
|
|
31
|
+
"Content-Type": `multipart/form-data; boundary=${boundary}`
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
return await response.formData();
|
|
35
|
+
}
|
|
36
|
+
__name(parseWorkerBundle, "parseWorkerBundle");
|
|
37
|
+
async function getBundleSize(workerBundle) {
|
|
38
|
+
const modules = [];
|
|
39
|
+
for (const entry of workerBundle.values()) {
|
|
40
|
+
if (entry instanceof Blob && entry.type !== "application/source-map") {
|
|
41
|
+
modules.push(entry);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const bundle = new Blob(modules);
|
|
45
|
+
return {
|
|
46
|
+
size: bundle.size,
|
|
47
|
+
gzipSize: gzipSync(await bundle.arrayBuffer()).byteLength
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
__name(getBundleSize, "getBundleSize");
|
|
51
|
+
function summarizeStartupProfile(profile) {
|
|
52
|
+
const nodes = new Map(profile.nodes.map((node) => [node.id, node]));
|
|
53
|
+
const samples = profile.samples ?? [];
|
|
54
|
+
const timeDeltas = profile.timeDeltas ?? [];
|
|
55
|
+
let sampledTime = 0;
|
|
56
|
+
let idleTime = 0;
|
|
57
|
+
let garbageCollectionTime = 0;
|
|
58
|
+
for (const [index, timeDelta] of timeDeltas.entries()) {
|
|
59
|
+
sampledTime += timeDelta;
|
|
60
|
+
const functionName = nodes.get(samples[index] ?? 0)?.callFrame.functionName;
|
|
61
|
+
if (functionName === "(idle)") {
|
|
62
|
+
idleTime += timeDelta;
|
|
63
|
+
} else if (functionName === "(garbage collector)") {
|
|
64
|
+
garbageCollectionTime += timeDelta;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
profileWindow: profile.endTime - profile.startTime,
|
|
69
|
+
sampledTime,
|
|
70
|
+
activeTime: sampledTime - idleTime,
|
|
71
|
+
garbageCollectionTime,
|
|
72
|
+
idleTime,
|
|
73
|
+
sampleCount: samples.length
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
__name(summarizeStartupProfile, "summarizeStartupProfile");
|
|
77
|
+
async function analyseBundle(workerBundle) {
|
|
78
|
+
const parsedWorkerBundle = await parseWorkerBundle(workerBundle);
|
|
79
|
+
const metadata = JSON.parse(
|
|
80
|
+
parsedWorkerBundle.get("metadata")
|
|
81
|
+
);
|
|
82
|
+
if (typeof metadata.main_module !== "string") {
|
|
83
|
+
throw new UserError(
|
|
84
|
+
"Startup profiling does not support service-worker format Workers. Refer to https://developers.cloudflare.com/workers/reference/migrate-to-module-workers/ for migration guidance.",
|
|
85
|
+
{
|
|
86
|
+
telemetryMessage: "startup profiling service worker format unsupported"
|
|
87
|
+
}
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
const convertedBindings = await convertWorkerBundleBindings(
|
|
91
|
+
parsedWorkerBundle,
|
|
92
|
+
metadata.bindings
|
|
93
|
+
);
|
|
94
|
+
const mf = new Miniflare(
|
|
95
|
+
convertV4MiniflareOptions({
|
|
96
|
+
name: "profiler",
|
|
97
|
+
compatibilityDate: typeof metadata.compatibility_date === "string" ? metadata.compatibility_date : void 0,
|
|
98
|
+
compatibilityFlags: Array.isArray(metadata.compatibility_flags) ? metadata.compatibility_flags : void 0,
|
|
99
|
+
...convertedBindings.options,
|
|
100
|
+
modulesRoot: "/",
|
|
101
|
+
modules: [
|
|
102
|
+
{
|
|
103
|
+
type: "ESModule",
|
|
104
|
+
// Keep the profiler entrypoint separate from all user module names.
|
|
105
|
+
path: randomUUID(),
|
|
106
|
+
contents: (
|
|
107
|
+
/* javascript */
|
|
108
|
+
`
|
|
109
|
+
async function startup() {
|
|
110
|
+
await import(${JSON.stringify(metadata.main_module)});
|
|
111
|
+
}
|
|
112
|
+
export default {
|
|
113
|
+
async fetch() {
|
|
114
|
+
await startup();
|
|
115
|
+
return new Response("ok");
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
`
|
|
119
|
+
)
|
|
120
|
+
},
|
|
121
|
+
...await convertWorkerBundleToModules(parsedWorkerBundle)
|
|
122
|
+
],
|
|
123
|
+
inspectorPort: 0,
|
|
124
|
+
handleStructuredLogs: /* @__PURE__ */ __name(() => void 0, "handleStructuredLogs")
|
|
125
|
+
})
|
|
126
|
+
);
|
|
127
|
+
const abortController = new AbortController();
|
|
128
|
+
const timeout = setTimeout(() => {
|
|
129
|
+
abortController.abort(
|
|
130
|
+
new UserError(
|
|
131
|
+
`Worker startup profiling timed out after ${STARTUP_PROFILE_TIMEOUT_MS / 1e3} seconds.`,
|
|
132
|
+
{ telemetryMessage: "startup profiling timed out" }
|
|
133
|
+
)
|
|
134
|
+
);
|
|
135
|
+
}, STARTUP_PROFILE_TIMEOUT_MS);
|
|
136
|
+
let ws;
|
|
137
|
+
const onInspectorError = /* @__PURE__ */ __name((error) => abortController.abort(
|
|
138
|
+
error instanceof Error ? error : new Error(`Worker startup profiler inspector error: ${String(error)}`)
|
|
139
|
+
), "onInspectorError");
|
|
140
|
+
const onInspectorClose = /* @__PURE__ */ __name(() => abortController.abort(
|
|
141
|
+
new Error("The Worker startup profiler inspector connection closed.")
|
|
142
|
+
), "onInspectorClose");
|
|
143
|
+
try {
|
|
144
|
+
await waitForPromise(mf.ready, abortController.signal);
|
|
145
|
+
const inspectorUrl = await waitForPromise(
|
|
146
|
+
mf.getInspectorURL(),
|
|
147
|
+
abortController.signal
|
|
148
|
+
);
|
|
149
|
+
ws = new WebSocket(new URL("/core:user:profiler", inspectorUrl.href));
|
|
150
|
+
ws.on("error", onInspectorError);
|
|
151
|
+
ws.on("close", onInspectorClose);
|
|
152
|
+
await waitForInspectorOpen(ws, abortController.signal);
|
|
153
|
+
await sendInspectorCommand(
|
|
154
|
+
ws,
|
|
155
|
+
1,
|
|
156
|
+
"Profiler.enable",
|
|
157
|
+
abortController.signal
|
|
158
|
+
);
|
|
159
|
+
await sendInspectorCommand(ws, 2, "Profiler.start", abortController.signal);
|
|
160
|
+
const response = await waitForPromise(
|
|
161
|
+
mf.dispatchFetch("https://example.com", {
|
|
162
|
+
signal: abortController.signal
|
|
163
|
+
}),
|
|
164
|
+
abortController.signal
|
|
165
|
+
);
|
|
166
|
+
await waitForPromise(response.text(), abortController.signal);
|
|
167
|
+
if (!response.ok && convertedBindings.unsupportedBindings.length > 0) {
|
|
168
|
+
const unsupportedBindings = ` The upload contains bindings that cannot be reproduced locally during startup profiling: ${convertedBindings.unsupportedBindings.map(
|
|
169
|
+
(binding) => `${JSON.stringify(binding.name)} (${JSON.stringify(binding.type)})`
|
|
170
|
+
).join(", ")}.`;
|
|
171
|
+
throw new UserError(
|
|
172
|
+
`Worker startup profiling failed during module evaluation (status ${response.status}).${unsupportedBindings}`,
|
|
173
|
+
{
|
|
174
|
+
telemetryMessage: "startup profiling module evaluation failed"
|
|
175
|
+
}
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
const stopResult = await sendInspectorCommand(ws, 3, "Profiler.stop", abortController.signal);
|
|
179
|
+
if (stopResult?.profile === void 0) {
|
|
180
|
+
throw new Error("Inspector command Profiler.stop returned no profile.");
|
|
181
|
+
}
|
|
182
|
+
return stopResult.profile;
|
|
183
|
+
} finally {
|
|
184
|
+
try {
|
|
185
|
+
await mf.dispose();
|
|
186
|
+
} finally {
|
|
187
|
+
try {
|
|
188
|
+
if (ws !== void 0) {
|
|
189
|
+
ws.off("error", onInspectorError);
|
|
190
|
+
ws.off("close", onInspectorClose);
|
|
191
|
+
if (ws.readyState !== WebSocket.CLOSED) {
|
|
192
|
+
ws.once("error", () => void 0);
|
|
193
|
+
ws.terminate();
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
} finally {
|
|
197
|
+
clearTimeout(timeout);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
__name(analyseBundle, "analyseBundle");
|
|
203
|
+
async function convertWorkerBundleBindings(workerBundle, bindings) {
|
|
204
|
+
const agentMemory = {};
|
|
205
|
+
let ai;
|
|
206
|
+
const aiSearchInstances = {};
|
|
207
|
+
const aiSearchNamespaces = {};
|
|
208
|
+
const analyticsEngineDatasets = {};
|
|
209
|
+
const artifacts = {};
|
|
210
|
+
const miniflareBindings = {};
|
|
211
|
+
let browserRendering;
|
|
212
|
+
const dataBlobBindings = {};
|
|
213
|
+
const d1Databases = {};
|
|
214
|
+
const dispatchNamespaces = {};
|
|
215
|
+
const sendEmailBindings = [];
|
|
216
|
+
const flagship = {};
|
|
217
|
+
const helloWorld = {};
|
|
218
|
+
let images;
|
|
219
|
+
const kvNamespaces = {};
|
|
220
|
+
let media;
|
|
221
|
+
const mtlsCertificates = {};
|
|
222
|
+
const pipelines = {};
|
|
223
|
+
const queueProducers = {};
|
|
224
|
+
const r2Buckets = {};
|
|
225
|
+
const ratelimits = {};
|
|
226
|
+
const secretsStoreSecrets = {};
|
|
227
|
+
const serviceBindings = {};
|
|
228
|
+
let stream;
|
|
229
|
+
const vectorize = {};
|
|
230
|
+
let versionMetadata;
|
|
231
|
+
const vpcNetworks = {};
|
|
232
|
+
const vpcServices = {};
|
|
233
|
+
const workerLoaders = {};
|
|
234
|
+
const unsupportedBindings = [];
|
|
235
|
+
for (const value of Array.isArray(bindings) ? bindings : []) {
|
|
236
|
+
if (!isNamedWorkerBinding(value)) {
|
|
237
|
+
unsupportedBindings.push({ name: "<unknown>", type: "unknown" });
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
const binding = value;
|
|
241
|
+
if (isRawWorkerBinding(binding)) {
|
|
242
|
+
serviceBindings[binding.name] = createOfflineFetcherBinding();
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
switch (binding.type) {
|
|
246
|
+
case "plain_text":
|
|
247
|
+
case "secret_text":
|
|
248
|
+
miniflareBindings[binding.name] = binding.text;
|
|
249
|
+
break;
|
|
250
|
+
case "json":
|
|
251
|
+
miniflareBindings[binding.name] = binding.json;
|
|
252
|
+
break;
|
|
253
|
+
case "text_blob":
|
|
254
|
+
miniflareBindings[binding.name] = await getTextBindingPart(
|
|
255
|
+
workerBundle,
|
|
256
|
+
binding
|
|
257
|
+
);
|
|
258
|
+
break;
|
|
259
|
+
case "data_blob":
|
|
260
|
+
dataBlobBindings[binding.name] = await getBinaryBindingPart(
|
|
261
|
+
workerBundle,
|
|
262
|
+
binding
|
|
263
|
+
);
|
|
264
|
+
break;
|
|
265
|
+
case "kv_namespace":
|
|
266
|
+
kvNamespaces[binding.name] = { id: binding.namespace_id };
|
|
267
|
+
break;
|
|
268
|
+
case "d1":
|
|
269
|
+
d1Databases[binding.name] = { id: binding.id };
|
|
270
|
+
break;
|
|
271
|
+
case "r2_bucket":
|
|
272
|
+
r2Buckets[binding.name] = { id: binding.bucket_name };
|
|
273
|
+
break;
|
|
274
|
+
case "queue":
|
|
275
|
+
queueProducers[binding.name] = {
|
|
276
|
+
queueName: binding.queue_name,
|
|
277
|
+
deliveryDelay: binding.delivery_delay
|
|
278
|
+
};
|
|
279
|
+
break;
|
|
280
|
+
case "service":
|
|
281
|
+
serviceBindings[binding.name] = createOfflineFetcherBinding();
|
|
282
|
+
unsupportedBindings.push(binding);
|
|
283
|
+
break;
|
|
284
|
+
case "assets":
|
|
285
|
+
serviceBindings[binding.name] = createOfflineFetcherBinding();
|
|
286
|
+
break;
|
|
287
|
+
case "browser":
|
|
288
|
+
browserRendering = { binding: binding.name };
|
|
289
|
+
break;
|
|
290
|
+
case "ai":
|
|
291
|
+
ai = { binding: binding.name };
|
|
292
|
+
break;
|
|
293
|
+
case "images":
|
|
294
|
+
images = { binding: binding.name };
|
|
295
|
+
break;
|
|
296
|
+
case "stream":
|
|
297
|
+
stream = { binding: binding.name };
|
|
298
|
+
break;
|
|
299
|
+
case "version_metadata":
|
|
300
|
+
versionMetadata = binding.name;
|
|
301
|
+
break;
|
|
302
|
+
case "ai_search_namespace":
|
|
303
|
+
aiSearchNamespaces[binding.name] = {
|
|
304
|
+
namespace: binding.namespace
|
|
305
|
+
};
|
|
306
|
+
break;
|
|
307
|
+
case "ai_search":
|
|
308
|
+
aiSearchInstances[binding.name] = {
|
|
309
|
+
instance_name: binding.instance_name
|
|
310
|
+
};
|
|
311
|
+
break;
|
|
312
|
+
case "agent_memory":
|
|
313
|
+
agentMemory[binding.name] = { namespace: binding.namespace };
|
|
314
|
+
break;
|
|
315
|
+
case "media":
|
|
316
|
+
media = { binding: binding.name };
|
|
317
|
+
break;
|
|
318
|
+
case "send_email": {
|
|
319
|
+
const shared = {
|
|
320
|
+
name: binding.name,
|
|
321
|
+
allowed_sender_addresses: binding.allowed_sender_addresses
|
|
322
|
+
};
|
|
323
|
+
if (binding.destination_address !== void 0) {
|
|
324
|
+
sendEmailBindings.push({
|
|
325
|
+
...shared,
|
|
326
|
+
destination_address: binding.destination_address
|
|
327
|
+
});
|
|
328
|
+
} else if (binding.allowed_destination_addresses !== void 0) {
|
|
329
|
+
sendEmailBindings.push({
|
|
330
|
+
...shared,
|
|
331
|
+
allowed_destination_addresses: binding.allowed_destination_addresses
|
|
332
|
+
});
|
|
333
|
+
} else {
|
|
334
|
+
sendEmailBindings.push(shared);
|
|
335
|
+
}
|
|
336
|
+
break;
|
|
337
|
+
}
|
|
338
|
+
case "durable_object_namespace":
|
|
339
|
+
case "workflow":
|
|
340
|
+
unsupportedBindings.push(binding);
|
|
341
|
+
break;
|
|
342
|
+
case "vectorize":
|
|
343
|
+
vectorize[binding.name] = { index_name: binding.index_name };
|
|
344
|
+
break;
|
|
345
|
+
case "analytics_engine":
|
|
346
|
+
analyticsEngineDatasets[binding.name] = {
|
|
347
|
+
dataset: binding.dataset ?? "dataset"
|
|
348
|
+
};
|
|
349
|
+
break;
|
|
350
|
+
case "dispatch_namespace":
|
|
351
|
+
dispatchNamespaces[binding.name] = {
|
|
352
|
+
namespace: binding.namespace
|
|
353
|
+
};
|
|
354
|
+
break;
|
|
355
|
+
case "mtls_certificate":
|
|
356
|
+
mtlsCertificates[binding.name] = {
|
|
357
|
+
certificate_id: binding.certificate_id
|
|
358
|
+
};
|
|
359
|
+
break;
|
|
360
|
+
case "pipelines":
|
|
361
|
+
if (binding.stream !== void 0) {
|
|
362
|
+
pipelines[binding.name] = { stream: binding.stream };
|
|
363
|
+
} else if (binding.pipeline !== void 0) {
|
|
364
|
+
pipelines[binding.name] = { pipeline: binding.pipeline };
|
|
365
|
+
} else {
|
|
366
|
+
unsupportedBindings.push(binding);
|
|
367
|
+
}
|
|
368
|
+
break;
|
|
369
|
+
case "secrets_store_secret":
|
|
370
|
+
secretsStoreSecrets[binding.name] = {
|
|
371
|
+
store_id: binding.store_id,
|
|
372
|
+
secret_name: binding.secret_name
|
|
373
|
+
};
|
|
374
|
+
break;
|
|
375
|
+
case "artifacts":
|
|
376
|
+
artifacts[binding.name] = { namespace: binding.namespace };
|
|
377
|
+
break;
|
|
378
|
+
case "unsafe_hello_world":
|
|
379
|
+
helloWorld[binding.name] = {
|
|
380
|
+
enable_timer: binding.enable_timer
|
|
381
|
+
};
|
|
382
|
+
break;
|
|
383
|
+
case "flagship":
|
|
384
|
+
flagship[binding.name] = { app_id: binding.app_id };
|
|
385
|
+
break;
|
|
386
|
+
case "ratelimit":
|
|
387
|
+
ratelimits[binding.name] = {
|
|
388
|
+
namespace_id: binding.namespace_id,
|
|
389
|
+
simple: binding.simple
|
|
390
|
+
};
|
|
391
|
+
break;
|
|
392
|
+
case "vpc_service":
|
|
393
|
+
vpcServices[binding.name] = { service_id: binding.service_id };
|
|
394
|
+
break;
|
|
395
|
+
case "vpc_network":
|
|
396
|
+
if (binding.tunnel_id !== void 0) {
|
|
397
|
+
vpcNetworks[binding.name] = { tunnel_id: binding.tunnel_id };
|
|
398
|
+
} else if (binding.network_id !== void 0) {
|
|
399
|
+
vpcNetworks[binding.name] = { network_id: binding.network_id };
|
|
400
|
+
} else {
|
|
401
|
+
unsupportedBindings.push(binding);
|
|
402
|
+
}
|
|
403
|
+
break;
|
|
404
|
+
case "worker_loader":
|
|
405
|
+
workerLoaders[binding.name] = {};
|
|
406
|
+
break;
|
|
407
|
+
case "inherit":
|
|
408
|
+
unsupportedBindings.push(binding);
|
|
409
|
+
break;
|
|
410
|
+
case "wasm_module":
|
|
411
|
+
unsupportedBindings.push(binding);
|
|
412
|
+
break;
|
|
413
|
+
case "hyperdrive":
|
|
414
|
+
unsupportedBindings.push(binding);
|
|
415
|
+
break;
|
|
416
|
+
case "logfwdr":
|
|
417
|
+
unsupportedBindings.push(binding);
|
|
418
|
+
break;
|
|
419
|
+
default: {
|
|
420
|
+
const unknownBinding = binding;
|
|
421
|
+
unsupportedBindings.push(unknownBinding);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
return {
|
|
426
|
+
options: {
|
|
427
|
+
agentMemory,
|
|
428
|
+
ai,
|
|
429
|
+
aiSearchInstances,
|
|
430
|
+
aiSearchNamespaces,
|
|
431
|
+
analyticsEngineDatasets,
|
|
432
|
+
artifacts,
|
|
433
|
+
bindings: miniflareBindings,
|
|
434
|
+
browserRendering,
|
|
435
|
+
dataBlobBindings,
|
|
436
|
+
d1Databases,
|
|
437
|
+
dispatchNamespaces,
|
|
438
|
+
email: { send_email: sendEmailBindings },
|
|
439
|
+
flagship,
|
|
440
|
+
helloWorld,
|
|
441
|
+
images,
|
|
442
|
+
kvNamespaces,
|
|
443
|
+
media,
|
|
444
|
+
mtlsCertificates,
|
|
445
|
+
pipelines,
|
|
446
|
+
queueProducers,
|
|
447
|
+
r2Buckets,
|
|
448
|
+
ratelimits,
|
|
449
|
+
secretsStoreSecrets,
|
|
450
|
+
serviceBindings,
|
|
451
|
+
stream,
|
|
452
|
+
vectorize,
|
|
453
|
+
versionMetadata,
|
|
454
|
+
vpcNetworks,
|
|
455
|
+
vpcServices,
|
|
456
|
+
workerLoaders
|
|
457
|
+
},
|
|
458
|
+
unsupportedBindings
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
__name(convertWorkerBundleBindings, "convertWorkerBundleBindings");
|
|
462
|
+
function isRawWorkerBinding(binding) {
|
|
463
|
+
switch (binding.type) {
|
|
464
|
+
case "browser":
|
|
465
|
+
case "ai":
|
|
466
|
+
case "images":
|
|
467
|
+
case "kv_namespace":
|
|
468
|
+
case "workflow":
|
|
469
|
+
case "queue":
|
|
470
|
+
case "r2_bucket":
|
|
471
|
+
case "d1":
|
|
472
|
+
case "vectorize":
|
|
473
|
+
return binding.raw === true;
|
|
474
|
+
default:
|
|
475
|
+
return false;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
__name(isRawWorkerBinding, "isRawWorkerBinding");
|
|
479
|
+
function isNamedWorkerBinding(value) {
|
|
480
|
+
return typeof value === "object" && value !== null && "name" in value && typeof value.name === "string" && "type" in value && typeof value.type === "string";
|
|
481
|
+
}
|
|
482
|
+
__name(isNamedWorkerBinding, "isNamedWorkerBinding");
|
|
483
|
+
function createOfflineFetcherBinding() {
|
|
484
|
+
return () => new Response("Binding calls are unavailable during startup profiling.", {
|
|
485
|
+
status: 503
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
__name(createOfflineFetcherBinding, "createOfflineFetcherBinding");
|
|
489
|
+
async function getBinaryBindingPart(workerBundle, binding) {
|
|
490
|
+
const part = getBindingPart(workerBundle, binding);
|
|
491
|
+
return part instanceof Blob ? new Uint8Array(await part.arrayBuffer()) : new TextEncoder().encode(part);
|
|
492
|
+
}
|
|
493
|
+
__name(getBinaryBindingPart, "getBinaryBindingPart");
|
|
494
|
+
async function getTextBindingPart(workerBundle, binding) {
|
|
495
|
+
const part = getBindingPart(workerBundle, binding);
|
|
496
|
+
return part instanceof Blob ? await part.text() : part;
|
|
497
|
+
}
|
|
498
|
+
__name(getTextBindingPart, "getTextBindingPart");
|
|
499
|
+
function getBindingPart(workerBundle, binding) {
|
|
500
|
+
const part = workerBundle.get(binding.part);
|
|
501
|
+
if (part === null) {
|
|
502
|
+
throw new UserError(
|
|
503
|
+
`Startup profiling could not find multipart part ${JSON.stringify(binding.part)} for binding ${JSON.stringify(binding.name)}.`,
|
|
504
|
+
{ telemetryMessage: "startup profiling binding part missing" }
|
|
505
|
+
);
|
|
506
|
+
}
|
|
507
|
+
return part;
|
|
508
|
+
}
|
|
509
|
+
__name(getBindingPart, "getBindingPart");
|
|
510
|
+
async function waitForPromise(promise, signal) {
|
|
511
|
+
if (signal.aborted) {
|
|
512
|
+
throw signal.reason;
|
|
513
|
+
}
|
|
514
|
+
return await new Promise((resolve, reject) => {
|
|
515
|
+
const onAbort = /* @__PURE__ */ __name(() => reject(signal.reason), "onAbort");
|
|
516
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
517
|
+
void promise.then(resolve, reject).finally(() => {
|
|
518
|
+
signal.removeEventListener("abort", onAbort);
|
|
519
|
+
});
|
|
520
|
+
});
|
|
521
|
+
}
|
|
522
|
+
__name(waitForPromise, "waitForPromise");
|
|
523
|
+
async function waitForInspectorOpen(ws, signal) {
|
|
524
|
+
if (signal.aborted) {
|
|
525
|
+
throw signal.reason;
|
|
526
|
+
}
|
|
527
|
+
return await new Promise((resolve, reject) => {
|
|
528
|
+
const cleanup = /* @__PURE__ */ __name(() => {
|
|
529
|
+
ws.off("open", onOpen);
|
|
530
|
+
ws.off("error", onError);
|
|
531
|
+
ws.off("close", onClose);
|
|
532
|
+
signal.removeEventListener("abort", onAbort);
|
|
533
|
+
}, "cleanup");
|
|
534
|
+
const onOpen = /* @__PURE__ */ __name(() => {
|
|
535
|
+
cleanup();
|
|
536
|
+
resolve();
|
|
537
|
+
}, "onOpen");
|
|
538
|
+
const onError = /* @__PURE__ */ __name((error) => {
|
|
539
|
+
cleanup();
|
|
540
|
+
reject(error);
|
|
541
|
+
}, "onError");
|
|
542
|
+
const onClose = /* @__PURE__ */ __name(() => {
|
|
543
|
+
cleanup();
|
|
544
|
+
reject(
|
|
545
|
+
new Error("The Worker startup profiler inspector connection closed.")
|
|
546
|
+
);
|
|
547
|
+
}, "onClose");
|
|
548
|
+
const onAbort = /* @__PURE__ */ __name(() => {
|
|
549
|
+
cleanup();
|
|
550
|
+
reject(signal.reason);
|
|
551
|
+
}, "onAbort");
|
|
552
|
+
ws.once("open", onOpen);
|
|
553
|
+
ws.once("error", onError);
|
|
554
|
+
ws.once("close", onClose);
|
|
555
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
__name(waitForInspectorOpen, "waitForInspectorOpen");
|
|
559
|
+
async function sendInspectorCommand(ws, id, method, signal) {
|
|
560
|
+
if (signal.aborted) {
|
|
561
|
+
throw signal.reason;
|
|
562
|
+
}
|
|
563
|
+
return await new Promise((resolve, reject) => {
|
|
564
|
+
const cleanup = /* @__PURE__ */ __name(() => {
|
|
565
|
+
ws.off("message", onMessage);
|
|
566
|
+
signal.removeEventListener("abort", onAbort);
|
|
567
|
+
}, "cleanup");
|
|
568
|
+
const onMessage = /* @__PURE__ */ __name((rawData) => {
|
|
569
|
+
let message;
|
|
570
|
+
try {
|
|
571
|
+
message = JSON.parse(rawData.toString());
|
|
572
|
+
} catch (error) {
|
|
573
|
+
cleanup();
|
|
574
|
+
reject(error);
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
577
|
+
if (message.id !== id) {
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
cleanup();
|
|
581
|
+
if (message.error != null) {
|
|
582
|
+
reject(
|
|
583
|
+
new Error(
|
|
584
|
+
`Inspector command ${method} failed: ${message.error.message ?? `error ${message.error.code ?? "unknown"}`}`
|
|
585
|
+
)
|
|
586
|
+
);
|
|
587
|
+
return;
|
|
588
|
+
}
|
|
589
|
+
resolve(message.result);
|
|
590
|
+
}, "onMessage");
|
|
591
|
+
const onAbort = /* @__PURE__ */ __name(() => {
|
|
592
|
+
cleanup();
|
|
593
|
+
reject(signal.reason);
|
|
594
|
+
}, "onAbort");
|
|
595
|
+
ws.on("message", onMessage);
|
|
596
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
597
|
+
try {
|
|
598
|
+
ws.send(JSON.stringify({ id, method, params: {} }), (error) => {
|
|
599
|
+
if (error) {
|
|
600
|
+
cleanup();
|
|
601
|
+
reject(error);
|
|
602
|
+
}
|
|
603
|
+
});
|
|
604
|
+
} catch (error) {
|
|
605
|
+
cleanup();
|
|
606
|
+
reject(error);
|
|
607
|
+
}
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
__name(sendInspectorCommand, "sendInspectorCommand");
|
|
611
|
+
async function getEntryValue(entry) {
|
|
612
|
+
if (entry instanceof Blob) {
|
|
613
|
+
return new Uint8Array(await entry.arrayBuffer());
|
|
614
|
+
}
|
|
615
|
+
return entry;
|
|
616
|
+
}
|
|
617
|
+
__name(getEntryValue, "getEntryValue");
|
|
618
|
+
function getModuleType(entry) {
|
|
619
|
+
if (entry instanceof Blob) {
|
|
620
|
+
try {
|
|
621
|
+
return MODULE_TYPE_TO_MINIFLARE_TYPE[fromMimeType(entry.type)];
|
|
622
|
+
} catch {
|
|
623
|
+
throw new Error(
|
|
624
|
+
`Unable to determine module type for ${entry.type} mime type`
|
|
625
|
+
);
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
return "Text";
|
|
629
|
+
}
|
|
630
|
+
__name(getModuleType, "getModuleType");
|
|
631
|
+
async function convertWorkerBundleToModules(workerBundle) {
|
|
632
|
+
return await Promise.all(
|
|
633
|
+
[...workerBundle.entries()].filter(
|
|
634
|
+
(module) => module[1] instanceof Blob && module[1].type !== "application/source-map"
|
|
635
|
+
).map(
|
|
636
|
+
async ([modulePath, entry]) => ({
|
|
637
|
+
type: getModuleType(entry),
|
|
638
|
+
path: modulePath,
|
|
639
|
+
contents: await getEntryValue(entry)
|
|
640
|
+
})
|
|
641
|
+
)
|
|
642
|
+
);
|
|
643
|
+
}
|
|
644
|
+
__name(convertWorkerBundleToModules, "convertWorkerBundleToModules");
|
|
645
|
+
|
|
646
|
+
export { analyseBundle, getBundleSize, parseWorkerBundle, summarizeStartupProfile };
|