-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathworker.ts
More file actions
502 lines (442 loc) · 9.49 KB
/
worker.ts
File metadata and controls
502 lines (442 loc) · 9.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
import type { CacheOptions, Observability, Route } from "./config/environment";
import type { INHERIT_SYMBOL } from "./constants";
import type { Json, WorkerMetadata } from "./types";
import type { AssetConfig, RouterConfig } from "@cloudflare/workers-shared";
/**
* The type of Worker
*/
export type CfScriptFormat = "modules" | "service-worker";
/**
* A module type.
*/
export type CfModuleType =
| "esm"
| "commonjs"
| "compiled-wasm"
| "text"
| "buffer"
| "python"
| "python-requirement";
/**
* An imported module.
*/
export interface CfModule {
/**
* The module name.
*
* @example
* './src/index.js'
*/
name: string;
/**
* The absolute path of the module on disk, or `undefined` if this is a
* virtual module. Used as the source URL for this module, so source maps are
* correctly resolved.
*
* @example
* '/path/to/src/index.js'
*/
filePath: string | undefined;
/**
* The module content, usually JavaScript or WASM code.
*
* @example
* export default {
* async fetch(request) {
* return new Response('Ok')
* }
* }
*/
content: string | Buffer<ArrayBuffer>;
/**
* An optional sourcemap for this module if it's of a ESM or CJS type, this will only be present
* if we're deploying with sourcemaps enabled. Since we copy extra modules that aren't bundled
* we need to also copy the relevant sourcemaps into the final out directory.
*/
sourceMap?: CfWorkerSourceMap;
/**
* The module type.
*
* If absent, will default to the main module's type.
*/
type?: CfModuleType;
}
/**
* A map of variable names to values.
*/
export interface CfVars {
[key: string]: string | Json;
}
/**
* A KV namespace.
*/
export interface CfKvNamespace {
binding: string;
id?: string | typeof INHERIT_SYMBOL;
remote?: boolean;
raw?: boolean;
}
/**
* A binding to send email.
*/
export type CfSendEmailBindings = {
name: string;
remote?: boolean;
} & (
| { destination_address?: string }
| { allowed_destination_addresses?: string[] }
| { allowed_sender_addresses?: string[] }
);
/**
* A binding to a wasm module (in service-worker format)
*/
export interface CfWasmModuleBindings {
[key: string]: string | Uint8Array<ArrayBuffer>;
}
/**
* A binding to a text blob (in service-worker format)
*/
export interface CfTextBlobBindings {
[key: string]: string;
}
/**
* A binding to a browser
*/
export interface CfBrowserBinding {
binding: string;
raw?: boolean;
remote?: boolean;
}
/**
* A binding to the AI project
*/
export interface CfAIBinding {
binding: string;
staging?: boolean;
remote?: boolean;
raw?: boolean;
}
/**
* A binding to Cloudflare Images
*/
export interface CfImagesBinding {
binding: string;
raw?: boolean;
remote?: boolean;
}
/**
* A binding to Cloudflare Media Transformations
*/
export interface CfMediaBinding {
binding: string;
remote?: boolean;
}
/**
* A binding to Cloudflare Stream
*/
export interface CfStreamBinding {
binding: string;
remote?: boolean;
}
/**
* A binding to the Worker Version's metadata
*/
export interface CfVersionMetadataBinding {
binding: string;
}
/**
* A binding to a data blob (in service-worker format)
*/
export interface CfDataBlobBindings {
[key: string]: string | Uint8Array<ArrayBuffer>;
}
/**
* A Durable Object.
*/
export interface CfDurableObject {
name: string;
class_name: string;
script_name?: string;
environment?: string;
}
export interface CfWorkflow {
name: string;
class_name: string;
binding: string;
script_name?: string;
remote?: boolean;
raw?: boolean;
limits?: {
steps?: number;
};
}
export interface CfQueue {
binding: string;
queue_name: string;
delivery_delay?: number;
remote?: boolean;
raw?: boolean;
}
export interface CfR2Bucket {
binding: string;
bucket_name?: string | typeof INHERIT_SYMBOL;
jurisdiction?: string;
remote?: boolean;
raw?: boolean;
}
// TODO: figure out if this is duplicated in packages/wrangler/src/config/environment.ts
export interface CfD1Database {
binding: string;
database_id?: string | typeof INHERIT_SYMBOL;
database_name?: string;
preview_database_id?: string;
database_internal_env?: string;
migrations_table?: string;
migrations_dir?: string;
remote?: boolean;
raw?: boolean;
}
export interface CfVectorize {
binding: string;
index_name: string;
raw?: boolean;
remote?: boolean;
}
export interface CfAISearchNamespace {
binding: string;
namespace: string | typeof INHERIT_SYMBOL;
remote?: boolean;
}
export interface CfAISearch {
binding: string;
instance_name: string;
remote?: boolean;
}
export interface CfSecretsStoreSecrets {
binding: string;
store_id: string;
secret_name: string;
}
export interface CfHelloWorld {
binding: string;
enable_timer?: boolean;
}
export interface CfFlagship {
binding: string;
app_id: string;
}
export interface CfWorkerLoader {
binding: string;
}
export interface CfRateLimit {
name: string;
namespace_id: string;
simple: {
limit: number;
period: 10 | 60;
};
}
export interface CfHyperdrive {
binding: string;
id: string;
localConnectionString?: string;
}
export interface CfService {
binding: string;
service: string;
environment?: string;
entrypoint?: string;
props?: Record<string, unknown>;
remote?: boolean;
cross_account_grant?: string;
}
export interface CfVpcService {
binding: string;
service_id: string;
remote?: boolean;
}
export interface CfVpcNetwork {
binding: string;
tunnel_id?: string;
network_id?: string;
remote?: boolean;
}
export interface CfAnalyticsEngineDataset {
binding: string;
dataset?: string;
}
export interface CfDispatchNamespace {
binding: string;
namespace: string;
outbound?: {
service: string;
environment?: string;
parameters?: string[];
};
remote?: boolean;
}
export interface CfMTlsCertificate {
binding: string;
certificate_id: string;
remote?: boolean;
}
export interface CfLogfwdr {
bindings: CfLogfwdrBinding[];
}
export interface CfLogfwdrBinding {
name: string;
destination: string;
}
export interface CfAssetsBinding {
binding: string;
}
export interface CfPipeline {
binding: string;
pipeline: string;
remote?: boolean;
}
export interface CfUnsafeBinding {
name: string;
type: string;
dev?: {
plugin: {
/**
* Package is the bare specifier of the package that exposes plugins to integrate into Miniflare via a named `plugins` export.
* @example "@cloudflare/my-external-miniflare-plugin"
*/
package: string;
/**
* Plugin is the name of the plugin exposed by the package.
* @example "my-unsafe-plugin"
*/
name: string;
};
/**
* dev-only options to pass to the plugin.
*/
options?: Record<string, unknown>;
};
}
type CfUnsafeMetadata = Record<string, unknown>;
export type CfCapnp =
| {
base_path?: never;
source_schemas?: never;
compiled_schema: string;
}
| {
base_path: string;
source_schemas: string[];
compiled_schema?: never;
};
export interface CfUnsafe {
bindings: CfUnsafeBinding[] | undefined;
metadata: CfUnsafeMetadata | undefined;
capnp: CfCapnp | undefined;
}
export interface CfDurableObjectMigrations {
old_tag?: string;
new_tag: string;
steps: {
new_classes?: string[];
new_sqlite_classes?: string[];
renamed_classes?: {
from: string;
to: string;
}[];
deleted_classes?: string[];
}[];
}
export type CfPlacement =
| { mode: "smart"; hint?: string }
| { mode?: "targeted"; region: string }
| { mode?: "targeted"; host: string }
| { mode?: "targeted"; hostname: string };
export interface CfTailConsumer {
service: string;
environment?: string;
}
export interface CfUserLimits {
cpu_ms?: number;
subrequests?: number;
}
/**
* Options for creating a `CfWorker`.
*/
export interface CfWorkerInit {
/**
* The name of the worker.
*/
name: string | undefined;
/**
* The entrypoint module.
*/
main: CfModule;
/**
* The list of additional modules.
*/
modules: CfModule[] | undefined;
/**
* The list of source maps to include on upload.
*/
sourceMaps: CfWorkerSourceMap[] | undefined;
containers: { class_name: string }[] | undefined;
migrations: CfDurableObjectMigrations | undefined;
compatibility_date: string | undefined;
compatibility_flags: string[] | undefined;
keepVars: boolean | undefined;
keepSecrets: boolean | undefined;
keepBindings?: WorkerMetadata["keep_bindings"];
logpush: boolean | undefined;
placement: CfPlacement | undefined;
tail_consumers: CfTailConsumer[] | undefined;
streaming_tail_consumers?: CfTailConsumer[] | undefined;
limits: CfUserLimits | undefined;
annotations?: Record<string, string | undefined>;
keep_assets?: boolean | undefined;
assets:
| {
jwt: string;
routerConfig: RouterConfig;
assetConfig: AssetConfig;
run_worker_first?: string[] | boolean;
_redirects?: string;
_headers?: string;
}
| undefined;
observability: Observability | undefined;
cache: CacheOptions | undefined;
}
export interface CfWorkerContext {
env: string | undefined;
useServiceEnvironments: boolean | undefined;
zone: string | undefined;
host: string | undefined;
routes: Route[] | undefined;
sendMetrics: boolean | undefined;
}
export interface CfWorkerSourceMap {
/**
* The name of the source map.
*
* @example
* 'out.js.map'
*/
name: string;
/**
* The content of the source map, which is a JSON object described by the v3
* spec.
*
* @example
* {
* "version" : 3,
* "file": "out.js",
* "sourceRoot": "",
* "sources": ["foo.js", "bar.js"],
* "sourcesContent": [null, null],
* "names": ["src", "maps", "are", "fun"],
* "mappings": "A,AAAB;;ABCDE;"
* }
*/
content: string | Buffer;
}