alchemy 0.51.2 → 0.52.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.
Files changed (103) hide show
  1. package/lib/apply.js +1 -1
  2. package/lib/apply.js.map +1 -1
  3. package/lib/cloudflare/assets.d.ts +1 -1
  4. package/lib/cloudflare/assets.d.ts.map +1 -1
  5. package/lib/cloudflare/bindings.d.ts +12 -11
  6. package/lib/cloudflare/bindings.d.ts.map +1 -1
  7. package/lib/cloudflare/bindings.js.map +1 -1
  8. package/lib/cloudflare/bound.d.ts +11 -18
  9. package/lib/cloudflare/bound.d.ts.map +1 -1
  10. package/lib/cloudflare/bucket.d.ts +4 -5
  11. package/lib/cloudflare/bucket.d.ts.map +1 -1
  12. package/lib/cloudflare/bucket.js +1 -16
  13. package/lib/cloudflare/bucket.js.map +1 -1
  14. package/lib/cloudflare/compatibility-date.gen.d.ts +1 -1
  15. package/lib/cloudflare/compatibility-date.gen.js +1 -1
  16. package/lib/cloudflare/d1-database.d.ts +85 -26
  17. package/lib/cloudflare/d1-database.d.ts.map +1 -1
  18. package/lib/cloudflare/d1-database.js +66 -47
  19. package/lib/cloudflare/d1-database.js.map +1 -1
  20. package/lib/cloudflare/d1-local-migrations.d.ts +10 -0
  21. package/lib/cloudflare/d1-local-migrations.d.ts.map +1 -0
  22. package/lib/cloudflare/d1-local-migrations.js +39 -0
  23. package/lib/cloudflare/d1-local-migrations.js.map +1 -0
  24. package/lib/cloudflare/dispatch-namespace.d.ts +4 -6
  25. package/lib/cloudflare/dispatch-namespace.d.ts.map +1 -1
  26. package/lib/cloudflare/dispatch-namespace.js +1 -11
  27. package/lib/cloudflare/dispatch-namespace.js.map +1 -1
  28. package/lib/cloudflare/event-source.d.ts +3 -3
  29. package/lib/cloudflare/event-source.d.ts.map +1 -1
  30. package/lib/cloudflare/hyperdrive.d.ts +3 -4
  31. package/lib/cloudflare/hyperdrive.d.ts.map +1 -1
  32. package/lib/cloudflare/hyperdrive.js +1 -9
  33. package/lib/cloudflare/hyperdrive.js.map +1 -1
  34. package/lib/cloudflare/index.d.ts +1 -0
  35. package/lib/cloudflare/index.d.ts.map +1 -1
  36. package/lib/cloudflare/index.js +1 -0
  37. package/lib/cloudflare/index.js.map +1 -1
  38. package/lib/cloudflare/kv-namespace.d.ts +4 -5
  39. package/lib/cloudflare/kv-namespace.d.ts.map +1 -1
  40. package/lib/cloudflare/kv-namespace.js +1 -14
  41. package/lib/cloudflare/kv-namespace.js.map +1 -1
  42. package/lib/cloudflare/pipeline.d.ts +4 -5
  43. package/lib/cloudflare/pipeline.d.ts.map +1 -1
  44. package/lib/cloudflare/pipeline.js +1 -12
  45. package/lib/cloudflare/pipeline.js.map +1 -1
  46. package/lib/cloudflare/queue-consumer.d.ts +3 -3
  47. package/lib/cloudflare/queue-consumer.d.ts.map +1 -1
  48. package/lib/cloudflare/queue.d.ts +3 -4
  49. package/lib/cloudflare/queue.d.ts.map +1 -1
  50. package/lib/cloudflare/queue.js +1 -11
  51. package/lib/cloudflare/queue.js.map +1 -1
  52. package/lib/cloudflare/vectorize-index.d.ts +4 -5
  53. package/lib/cloudflare/vectorize-index.d.ts.map +1 -1
  54. package/lib/cloudflare/vectorize-index.js +1 -15
  55. package/lib/cloudflare/vectorize-index.js.map +1 -1
  56. package/lib/cloudflare/vectorize-metadata-index.d.ts +2 -2
  57. package/lib/cloudflare/vectorize-metadata-index.d.ts.map +1 -1
  58. package/lib/cloudflare/worker-ref.d.ts +41 -0
  59. package/lib/cloudflare/worker-ref.d.ts.map +1 -0
  60. package/lib/cloudflare/worker-ref.js +27 -0
  61. package/lib/cloudflare/worker-ref.js.map +1 -0
  62. package/lib/cloudflare/worker.d.ts +6 -68
  63. package/lib/cloudflare/worker.d.ts.map +1 -1
  64. package/lib/cloudflare/worker.js +5 -198
  65. package/lib/cloudflare/worker.js.map +1 -1
  66. package/lib/cloudflare/wrangler.json.js +1 -0
  67. package/lib/cloudflare/wrangler.json.js.map +1 -1
  68. package/lib/cloudflare/zone.d.ts.map +1 -1
  69. package/lib/cloudflare/zone.js +2 -2
  70. package/lib/cloudflare/zone.js.map +1 -1
  71. package/lib/esbuild/bundle.d.ts +2 -2
  72. package/lib/state/cloudflare-state-store.d.ts +3 -1
  73. package/lib/state/cloudflare-state-store.d.ts.map +1 -1
  74. package/lib/state/cloudflare-state-store.js +11 -2
  75. package/lib/state/cloudflare-state-store.js.map +1 -1
  76. package/lib/state/instrumented-state-store.d.ts +2 -0
  77. package/lib/state/instrumented-state-store.d.ts.map +1 -1
  78. package/lib/state/instrumented-state-store.js +2 -0
  79. package/lib/state/instrumented-state-store.js.map +1 -1
  80. package/package.json +1 -1
  81. package/src/apply.ts +1 -1
  82. package/src/cloudflare/bindings.ts +18 -17
  83. package/src/cloudflare/bound.ts +35 -18
  84. package/src/cloudflare/bucket.ts +5 -27
  85. package/src/cloudflare/compatibility-date.gen.ts +1 -1
  86. package/src/cloudflare/d1-database.ts +107 -100
  87. package/src/cloudflare/d1-local-migrations.ts +52 -0
  88. package/src/cloudflare/dispatch-namespace.ts +6 -25
  89. package/src/cloudflare/event-source.ts +3 -3
  90. package/src/cloudflare/hyperdrive.ts +4 -19
  91. package/src/cloudflare/index.ts +1 -0
  92. package/src/cloudflare/kv-namespace.ts +5 -28
  93. package/src/cloudflare/pipeline.ts +8 -27
  94. package/src/cloudflare/queue-consumer.ts +3 -3
  95. package/src/cloudflare/queue.ts +4 -22
  96. package/src/cloudflare/vectorize-index.ts +5 -27
  97. package/src/cloudflare/vectorize-metadata-index.ts +2 -2
  98. package/src/cloudflare/worker-ref.ts +51 -0
  99. package/src/cloudflare/worker.ts +16 -332
  100. package/src/cloudflare/wrangler.json.ts +1 -0
  101. package/src/cloudflare/zone.ts +9 -8
  102. package/src/state/cloudflare-state-store.ts +73 -54
  103. package/src/state/instrumented-state-store.ts +2 -0
@@ -1,13 +1,11 @@
1
1
  import type { Context } from "../context.ts";
2
2
  import { Resource, ResourceKind } from "../resource.ts";
3
- import { bind } from "../runtime/bind.ts";
4
3
  import { CloudflareApiError, handleApiError } from "./api-error.ts";
5
4
  import {
6
5
  createCloudflareApi,
7
6
  type CloudflareApi,
8
7
  type CloudflareApiOptions,
9
8
  } from "./api.ts";
10
- import type { Bound } from "./bound.ts";
11
9
 
12
10
  /**
13
11
  * Settings for a Cloudflare Queue
@@ -95,7 +93,7 @@ export function isQueue(eventSource: any): eventSource is Queue {
95
93
  /**
96
94
  * Output returned after Cloudflare Queue creation/update
97
95
  */
98
- export interface QueueResource<Body = unknown>
96
+ export interface Queue<Body = unknown>
99
97
  extends Resource<"cloudflare::Queue">,
100
98
  QueueProps {
101
99
  /**
@@ -131,9 +129,6 @@ export interface QueueResource<Body = unknown>
131
129
  Batch: MessageBatch<Body>;
132
130
  }
133
131
 
134
- export type Queue<Body = unknown> = QueueResource<Body> &
135
- Bound<QueueResource<Body>>;
136
-
137
132
  /**
138
133
  * Creates and manages Cloudflare Queues.
139
134
  *
@@ -213,23 +208,10 @@ export type Queue<Body = unknown> = QueueResource<Body> &
213
208
  *
214
209
  * @see https://developers.cloudflare.com/queues/
215
210
  */
216
- export async function Queue<Body = unknown>(
217
- name: string,
218
- props: QueueProps = {},
219
- ): Promise<Queue<Body>> {
220
- const queue = await QueueResource<Body>(name, props);
221
- const binding = await bind(queue);
222
- return {
223
- ...queue,
224
- send: binding.send,
225
- sendBatch: binding.sendBatch,
226
- } as Queue<Body>;
227
- }
228
-
229
- const QueueResource = Resource("cloudflare::Queue", async function <
211
+ export const Queue = Resource("cloudflare::Queue", async function <
230
212
  T = unknown,
231
- >(this: Context<QueueResource<T>>, id: string, props: QueueProps = {}): Promise<
232
- QueueResource<T>
213
+ >(this: Context<Queue<T>>, id: string, props: QueueProps = {}): Promise<
214
+ Queue<T>
233
215
  > {
234
216
  const api = await createCloudflareApi(props);
235
217
  const queueName = props.name ?? id;
@@ -1,6 +1,5 @@
1
1
  import type { Context } from "../context.ts";
2
2
  import { Resource, ResourceKind } from "../resource.ts";
3
- import { bind } from "../runtime/bind.ts";
4
3
  import { logger } from "../util/logger.ts";
5
4
  import { CloudflareApiError, handleApiError } from "./api-error.ts";
6
5
  import {
@@ -8,7 +7,6 @@ import {
8
7
  type CloudflareApi,
9
8
  type CloudflareApiOptions,
10
9
  } from "./api.ts";
11
- import type { Bound } from "./bound.ts";
12
10
 
13
11
  /**
14
12
  * Properties for creating or updating a Vectorize Index
@@ -53,14 +51,14 @@ export interface VectorizeIndexProps extends CloudflareApiOptions {
53
51
 
54
52
  export function isVectorizeIndex(
55
53
  resource: Resource,
56
- ): resource is VectorizeIndexResource {
54
+ ): resource is VectorizeIndex {
57
55
  return resource[ResourceKind] === "cloudflare::VectorizeIndex";
58
56
  }
59
57
 
60
58
  /**
61
59
  * Output returned after Vectorize Index creation/update
62
60
  */
63
- export interface VectorizeIndexResource
61
+ export interface VectorizeIndex
64
62
  extends Resource<"cloudflare::VectorizeIndex">,
65
63
  VectorizeIndexProps {
66
64
  type: "vectorize";
@@ -76,9 +74,6 @@ export interface VectorizeIndexResource
76
74
  createdAt?: number;
77
75
  }
78
76
 
79
- export type VectorizeIndex = VectorizeIndexResource &
80
- Bound<VectorizeIndexResource>;
81
-
82
77
  /**
83
78
  * Creates and manages Cloudflare Vectorize Indexes.
84
79
  *
@@ -118,30 +113,13 @@ export type VectorizeIndex = VectorizeIndexResource &
118
113
  *
119
114
  * @see https://developers.cloudflare.com/vectorize/
120
115
  */
121
- export async function VectorizeIndex(
122
- name: string,
123
- props: VectorizeIndexProps,
124
- ): Promise<VectorizeIndex> {
125
- const index = await _VectorizeIndex(name, props);
126
- const binding = await bind(index);
127
- return {
128
- ...index,
129
- describe: binding.describe,
130
- query: binding.query,
131
- insert: binding.insert,
132
- upsert: binding.upsert,
133
- deleteByIds: binding.deleteByIds,
134
- getByIds: binding.getByIds,
135
- };
136
- }
137
-
138
- const _VectorizeIndex = Resource(
116
+ export const VectorizeIndex = Resource(
139
117
  "cloudflare::VectorizeIndex",
140
118
  async function (
141
- this: Context<VectorizeIndexResource>,
119
+ this: Context<VectorizeIndex>,
142
120
  id: string,
143
121
  props: VectorizeIndexProps,
144
- ): Promise<VectorizeIndexResource> {
122
+ ): Promise<VectorizeIndex> {
145
123
  const api = await createCloudflareApi(props);
146
124
  const indexName = props.name || id;
147
125
 
@@ -7,7 +7,7 @@ import {
7
7
  type CloudflareApi,
8
8
  type CloudflareApiOptions,
9
9
  } from "./api.ts";
10
- import type { VectorizeIndexResource } from "./vectorize-index.ts";
10
+ import type { VectorizeIndex } from "./vectorize-index.ts";
11
11
 
12
12
  /**
13
13
  * Properties for creating or deleting a Vectorize Metadata Index
@@ -16,7 +16,7 @@ export interface VectorizeMetadataIndexProps extends CloudflareApiOptions {
16
16
  /**
17
17
  * Parent Vectorize Index
18
18
  */
19
- index: VectorizeIndexResource;
19
+ index: VectorizeIndex;
20
20
 
21
21
  /**
22
22
  * Name of the property in the metadata to create an index for
@@ -0,0 +1,51 @@
1
+ import type { WorkerBindingService } from "./bindings.ts";
2
+
3
+ /**
4
+ * Represents a reference to a Cloudflare Worker service.
5
+ *
6
+ * @template RPC - The type of the worker's RPC entrypoint, defaults to Rpc.WorkerEntrypointBranded.
7
+ *
8
+ * This interface extends all properties of WorkerBindingService except for "name".
9
+ * It also includes an optional __rpc__ property for type branding.
10
+ */
11
+ export type WorkerRef<
12
+ RPC extends Rpc.WorkerEntrypointBranded = Rpc.WorkerEntrypointBranded,
13
+ > = Omit<WorkerBindingService, "name"> & {
14
+ /**
15
+ * Optional type branding for the worker's RPC entrypoint.
16
+ */
17
+ __rpc__?: RPC;
18
+ };
19
+
20
+ /**
21
+ * Creates a reference to a Cloudflare Worker service.
22
+ *
23
+ * @example
24
+ * // Create a reference to a Cloudflare Worker service:
25
+ * const ref = WorkerRef({
26
+ * service: "my-worker",
27
+ * environment: "production",
28
+ * namespace: "main"
29
+ * });
30
+ *
31
+ * // Optionally, you can specify only the service:
32
+ * const ref2 = WorkerRef({ service: "my-worker" });
33
+ *
34
+ * // You can also specify the RPC type for stronger typing:
35
+ * interface MyWorkerRPC extends Rpc.WorkerEntrypointBranded {
36
+ * myMethod(arg: string): Promise<number>;
37
+ * }
38
+ * const typedRef = WorkerRef<MyWorkerRPC>({ service: "my-worker" });
39
+ */
40
+ export function WorkerRef<
41
+ RPC extends Rpc.WorkerEntrypointBranded = Rpc.WorkerEntrypointBranded,
42
+ >(options?: {
43
+ service: string;
44
+ environment?: string;
45
+ namespace?: string;
46
+ }): WorkerRef<RPC> {
47
+ return {
48
+ ...options,
49
+ type: "service",
50
+ } as WorkerRef<RPC>;
51
+ }
@@ -6,13 +6,8 @@ import { isDeepStrictEqual } from "node:util";
6
6
  import path from "pathe";
7
7
  import type { Context } from "../context.ts";
8
8
  import type { BundleProps } from "../esbuild/bundle.ts";
9
- import { InnerResourceScope, Resource, ResourceKind } from "../resource.ts";
10
- import { getBindKey, tryGetBinding } from "../runtime/bind.ts";
11
- import { isRuntime } from "../runtime/global.ts";
12
- import { bootstrapPlugin } from "../runtime/plugin.ts";
13
- import { Scope } from "../scope.ts";
14
- import { Secret, isSecret, secret } from "../secret.ts";
15
- import { serializeScope } from "../serde.ts";
9
+ import { Resource, ResourceKind } from "../resource.ts";
10
+ import { isSecret } from "../secret.ts";
16
11
  import type { type } from "../type.ts";
17
12
  import { DeferredPromise } from "../util/deferred-promise.ts";
18
13
  import { logger } from "../util/logger.ts";
@@ -25,16 +20,12 @@ import {
25
20
  createCloudflareApi,
26
21
  } from "./api.ts";
27
22
  import type { Assets } from "./assets.ts";
28
- import {
29
- type Binding,
30
- type Bindings,
31
- Json,
32
- type WorkerBindingDurableObjectNamespace,
33
- type WorkerBindingService,
34
- type WorkerBindingSpec,
23
+ import type {
24
+ Bindings,
25
+ WorkerBindingDurableObjectNamespace,
26
+ WorkerBindingSpec,
35
27
  } from "./bindings.ts";
36
28
  import type { Bound } from "./bound.ts";
37
- import { isBucket } from "./bucket.ts";
38
29
  import {
39
30
  type WorkerBundle,
40
31
  type WorkerBundleChunk,
@@ -49,36 +40,27 @@ import {
49
40
  } from "./compatibility-presets.ts";
50
41
  import { type Container, ContainerApplication } from "./container.ts";
51
42
  import { CustomDomain } from "./custom-domain.ts";
52
- import { isD1Database } from "./d1-database.ts";
53
- import type { DispatchNamespaceResource } from "./dispatch-namespace.ts";
43
+ import type { DispatchNamespace } from "./dispatch-namespace.ts";
54
44
  import {
55
45
  DurableObjectNamespace,
56
46
  isDurableObjectNamespace,
57
47
  } from "./durable-object-namespace.ts";
58
- import {
59
- type EventSource,
60
- type QueueEventSource,
61
- isQueueEventSource,
62
- } from "./event-source.ts";
63
- import { isKVNamespace } from "./kv-namespace.ts";
48
+ import { type EventSource, isQueueEventSource } from "./event-source.ts";
64
49
  import type { MiniflareWorkerOptions } from "./miniflare/miniflare-worker-options.ts";
65
50
  import { miniflareServer } from "./miniflare/miniflare.ts";
66
- import { isPipeline } from "./pipeline.ts";
67
51
  import {
68
52
  QueueConsumer,
69
53
  deleteQueueConsumer,
70
54
  listQueueConsumersForWorker,
71
55
  } from "./queue-consumer.ts";
72
- import { type QueueResource, isQueue } from "./queue.ts";
56
+ import { isQueue } from "./queue.ts";
73
57
  import { Route } from "./route.ts";
74
- import { isVectorizeIndex } from "./vectorize-index.ts";
75
58
  import { uploadAssets } from "./worker-assets.ts";
76
59
  import {
77
60
  type WorkerScriptMetadata,
78
61
  bumpMigrationTagVersion,
79
62
  prepareWorkerMetadata,
80
63
  } from "./worker-metadata.ts";
81
- import { WorkerStub, isWorkerStub } from "./worker-stub.ts";
82
64
  import { WorkerSubdomain, disableWorkerSubdomain } from "./worker-subdomain.ts";
83
65
  import { createTail } from "./worker-tail.ts";
84
66
  import { Workflow, isWorkflow, upsertWorkflow } from "./workflow.ts";
@@ -330,7 +312,7 @@ export interface BaseWorkerProps<
330
312
  *
331
313
  * This allows workers to be routed to via dispatch namespace routing rules
332
314
  */
333
- namespace?: string | DispatchNamespaceResource;
315
+ namespace?: string | DispatchNamespace;
334
316
 
335
317
  /**
336
318
  * Version label for this worker deployment
@@ -455,45 +437,6 @@ export type WorkerProps<
455
437
  RPC extends Rpc.WorkerEntrypointBranded = Rpc.WorkerEntrypointBranded,
456
438
  > = InlineWorkerProps<B, RPC> | EntrypointWorkerProps<B, RPC>;
457
439
 
458
- export type FetchWorkerProps<
459
- B extends Bindings = Bindings,
460
- E extends EventSource[] = EventSource[],
461
- > = Omit<WorkerProps<B>, "entrypoint" | "eventSources"> & {
462
- /**
463
- * A function that will be used to fetch the worker script.
464
- *
465
- * This is only used when using the fetch property.
466
- */
467
- fetch?(
468
- request: Request,
469
- env: Bindings.Runtime<B>,
470
- ctx: ExecutionContext,
471
- ): Promise<Response>;
472
- /**
473
- * Event sources that this worker will consume.
474
- *
475
- * Can include queues, streams, or other event sources.
476
- */
477
- eventSources?: E;
478
-
479
- /**
480
- * A function that will be used to queue the worker script.
481
- *
482
- * This is only used when using the queue property.
483
- */
484
- queue?(
485
- batch: QueueBatch<E>,
486
- env: Bindings.Runtime<B>,
487
- ctx: ExecutionContext,
488
- ): Promise<void>;
489
- };
490
-
491
- type QueueBatch<E extends EventSource[]> = E[number] extends QueueEventSource
492
- ? any
493
- : E[number] extends QueueResource<infer Body>
494
- ? MessageBatch<Body>
495
- : MessageBatch<unknown>;
496
-
497
440
  export function isWorker(resource: Resource): resource is Worker<any> {
498
441
  return resource[ResourceKind] === "cloudflare::Worker";
499
442
  }
@@ -505,8 +448,7 @@ export type Worker<
505
448
  B extends Bindings | undefined = Bindings | undefined,
506
449
  RPC extends Rpc.WorkerEntrypointBranded = Rpc.WorkerEntrypointBranded,
507
450
  > = Resource<"cloudflare::Worker"> &
508
- Omit<WorkerProps<B>, "url" | "script" | "routes" | "domains"> &
509
- globalThis.Service & {
451
+ Omit<WorkerProps<B>, "url" | "script" | "routes" | "domains"> & {
510
452
  /** @internal phantom property */
511
453
  __rpc__?: RPC;
512
454
 
@@ -586,7 +528,7 @@ export type Worker<
586
528
  /**
587
529
  * The dispatch namespace this worker is deployed to
588
530
  */
589
- namespace?: string | DispatchNamespaceResource;
531
+ namespace?: string | DispatchNamespace;
590
532
 
591
533
  /**
592
534
  * Version label for this worker deployment
@@ -601,56 +543,6 @@ export type Worker<
601
543
  };
602
544
  };
603
545
 
604
- /**
605
- * Represents a reference to a Cloudflare Worker service.
606
- *
607
- * @template RPC - The type of the worker's RPC entrypoint, defaults to Rpc.WorkerEntrypointBranded.
608
- *
609
- * This interface extends all properties of WorkerBindingService except for "name".
610
- * It also includes an optional __rpc__ property for type branding.
611
- */
612
- export type WorkerRef<
613
- RPC extends Rpc.WorkerEntrypointBranded = Rpc.WorkerEntrypointBranded,
614
- > = Omit<WorkerBindingService, "name"> & {
615
- /**
616
- * Optional type branding for the worker's RPC entrypoint.
617
- */
618
- __rpc__?: RPC;
619
- };
620
-
621
- /**
622
- * Creates a reference to a Cloudflare Worker service.
623
- *
624
- * @example
625
- * // Create a reference to a Cloudflare Worker service:
626
- * const ref = WorkerRef({
627
- * service: "my-worker",
628
- * environment: "production",
629
- * namespace: "main"
630
- * });
631
- *
632
- * // Optionally, you can specify only the service:
633
- * const ref2 = WorkerRef({ service: "my-worker" });
634
- *
635
- * // You can also specify the RPC type for stronger typing:
636
- * interface MyWorkerRPC extends Rpc.WorkerEntrypointBranded {
637
- * myMethod(arg: string): Promise<number>;
638
- * }
639
- * const typedRef = WorkerRef<MyWorkerRPC>({ service: "my-worker" });
640
- */
641
- export function WorkerRef<
642
- RPC extends Rpc.WorkerEntrypointBranded = Rpc.WorkerEntrypointBranded,
643
- >(options?: {
644
- service: string;
645
- environment?: string;
646
- namespace?: string;
647
- }): WorkerRef<RPC> {
648
- return {
649
- ...options,
650
- type: "service",
651
- } as WorkerRef<RPC>;
652
- }
653
-
654
546
  /**
655
547
  * A Cloudflare Worker is a serverless function that can be deployed to the Cloudflare network.
656
548
  *
@@ -807,220 +699,12 @@ export function Worker<
807
699
  const B extends Bindings,
808
700
  RPC extends Rpc.WorkerEntrypointBranded,
809
701
  >(id: string, props: WorkerProps<B, RPC>): Promise<Worker<B, RPC>>;
810
- export function Worker<const B extends Bindings, const E extends EventSource[]>(
811
- id: string,
812
- meta: ImportMeta,
813
- props: FetchWorkerProps<B, E>,
814
- ): Promise<Worker<B>> & globalThis.Service;
702
+
815
703
  export function Worker<const B extends Bindings>(
816
- ...args:
817
- | [id: string, props: WorkerProps<B>]
818
- | [id: string, meta: ImportMeta, props: FetchWorkerProps<B>]
704
+ id: string,
705
+ props: WorkerProps<B>,
819
706
  ): Promise<Worker<B>> {
820
- const [id, meta, props] =
821
- args.length === 2 ? [args[0], undefined, args[1]] : args;
822
-
823
- if (("fetch" in props && props.fetch) || ("queue" in props && props.queue)) {
824
- const scope = Scope.current;
825
-
826
- const workerName = props.name ?? id;
827
-
828
- // we need to make sure the worker exists
829
- const stub = WorkerStub(`${id}/stub`, {
830
- name: workerName,
831
- accountId: props.accountId,
832
- apiKey: props.apiKey,
833
- apiToken: props.apiToken,
834
- baseUrl: props.baseUrl,
835
- email: props.email,
836
- });
837
-
838
- async function collectResources(
839
- scope: Scope | undefined,
840
- ): Promise<Resource[]> {
841
- if (!scope) {
842
- return [];
843
- }
844
- return (
845
- await Promise.all(
846
- Array.from(scope.resources.values()).map(async (resource) => [
847
- (await resource) as Resource,
848
- ...(await collectResources(await resource[InnerResourceScope])),
849
- ]),
850
- )
851
- ).flat();
852
- }
853
-
854
- const deferred = scope.defer(async () => {
855
- const autoBindings: Record<string, Binding> = {};
856
- for (const resource of await collectResources(Scope.root)) {
857
- if (
858
- isQueue(resource) ||
859
- isWorkerStub(resource) ||
860
- isWorker(resource) ||
861
- isD1Database(resource) ||
862
- isBucket(resource) ||
863
- isPipeline(resource) ||
864
- isVectorizeIndex(resource) ||
865
- isKVNamespace(resource)
866
- ) {
867
- // TODO(sam): make this generic/pluggable
868
- autoBindings[getBindKey(resource)] = resource;
869
- }
870
- }
871
-
872
- // TODO(sam): prune to only needed secrets
873
- for (const secret of Secret.all()) {
874
- autoBindings[secret.name] = secret;
875
- }
876
-
877
- const bindings = {
878
- ...props.bindings,
879
- __ALCHEMY_WORKER_NAME__: workerName,
880
- __ALCHEMY_SERIALIZED_SCOPE__: Json(await serializeScope(scope)),
881
- ALCHEMY_STAGE: scope.stage,
882
- ALCHEMY_PASSWORD: secret(scope.password),
883
- // TODO(sam): prune un-needed bindings
884
- ...autoBindings,
885
- };
886
-
887
- return _Worker(id, {
888
- ...(props as any),
889
- url: true,
890
- compatibilityFlags: Array.from(
891
- new Set([
892
- "nodejs_compat",
893
- ...unionCompatibilityFlags(
894
- props.compatibility,
895
- props.compatibilityFlags,
896
- ),
897
- ]),
898
- ),
899
- entrypoint: meta!.filename,
900
- name: workerName,
901
- // adopt because the stub guarnatees that the worker exists
902
- adopt: true,
903
- bindings,
904
- bundle: {
905
- ...props.bundle,
906
- plugins: [bootstrapPlugin],
907
- external: [
908
- // for alchemy
909
- "libsodium*",
910
- "@swc/*",
911
- "esbuild",
912
- // TODO(sam): this is for fetch, why is it a package?
913
- "undici",
914
- "miniflare",
915
- "@iarna/toml",
916
- "cli-*",
917
- "react*",
918
- ],
919
- banner: {
920
- js: "var __ALCHEMY_RUNTIME__ = true;",
921
- },
922
- inject: [
923
- ...(props.bundle?.inject ?? []),
924
- path.resolve(import.meta.dirname, "..", "runtime", "shims.js"),
925
- ],
926
- },
927
- });
928
- }) as Promise<Worker<B>>;
929
-
930
- // defer construction of this worker until the app is about to finaloze
931
- // this ensures that the Worker's dependencies are instantiated before we bundle
932
- // it is then safe to bundle and import the Worker to detect which resources need to be auto-bound
933
- const promise = Promise.all([deferred, stub]).then(
934
- ([worker]) => worker,
935
- ) as Promise<Worker<B>> & {
936
- fetch?: (...args: any[]) => Promise<Response>;
937
- queue?: (
938
- batch: QueueBatch<any>,
939
- env: Bindings.Runtime<B>,
940
- ctx: ExecutionContext,
941
- ) => Promise<void>;
942
- };
943
-
944
- if (isRuntime) {
945
- if (props.fetch) {
946
- promise.fetch = async (
947
- request: Request,
948
- env: Bindings.Runtime<B>,
949
- ctx: ExecutionContext,
950
- ) => {
951
- try {
952
- return await props.fetch!(request, env as any, ctx);
953
- } catch (err: any) {
954
- return new Response(err.message + err.stack, {
955
- status: 500,
956
- });
957
- }
958
- };
959
- }
960
- if (props.queue) {
961
- promise.queue = async (
962
- batch: QueueBatch<any>,
963
- env: Bindings.Runtime<B>,
964
- ctx: ExecutionContext,
965
- ) => {
966
- return await props.queue!(batch, env, ctx);
967
- };
968
- }
969
- } else {
970
- promise.fetch = async (
971
- request: string | URL | Request,
972
- init?: RequestInit,
973
- ) => {
974
- const worker = await promise;
975
- if (!worker.url) {
976
- throw new Error("Worker URL is not available in runtime");
977
- }
978
- if (request instanceof Request) {
979
- const origin = new URL(worker.url);
980
- const incoming = request.url.startsWith("/")
981
- ? new URL(`${worker.url}${request.url}`)
982
- : new URL(request.url);
983
- logger.log(incoming);
984
- const proxyURL = new URL(
985
- `${incoming.pathname}${incoming.search}${incoming.hash}`,
986
- origin,
987
- );
988
-
989
- const headers = new Headers(request.headers);
990
- headers.set("host", origin.host);
991
-
992
- try {
993
- return await fetch(
994
- new Request(proxyURL, {
995
- method: request.method,
996
- body: request.body,
997
- headers,
998
- // TODO(sam): do we need this? GPT added it ...
999
- // redirect: "manual",
1000
- }),
1001
- );
1002
- } catch (err: any) {
1003
- return new Response(err.message ?? "proxy error", { status: 500 });
1004
- }
1005
- } else {
1006
- if (typeof request === "string" && request.startsWith("/")) {
1007
- request = new URL(`${worker.url}${request}`);
1008
- }
1009
- return await fetch(request, init);
1010
- }
1011
- };
1012
- }
1013
- return promise;
1014
- }
1015
- return _Worker(id, props as WorkerProps<B>).then(async (worker) => {
1016
- const binding = await tryGetBinding(worker);
1017
- if (binding) {
1018
- worker.fetch = (request: Request) => binding.fetch(request);
1019
- worker.connect = (address: SocketAddress, options: SocketOptions) =>
1020
- binding.connect(address, options);
1021
- }
1022
- return worker;
1023
- });
707
+ return _Worker(id, props as WorkerProps<B>);
1024
708
  }
1025
709
 
1026
710
  export const _Worker = Resource(
@@ -645,6 +645,7 @@ function processBindings(
645
645
  script_name: binding.scriptName,
646
646
  });
647
647
  } else if (binding.type === "d1") {
648
+ console.log("binding.dev", binding.dev);
648
649
  d1Databases.push({
649
650
  binding: bindingName,
650
651
  database_id: binding.id,
@@ -651,14 +651,15 @@ export async function findZoneForHostname(
651
651
  const totalPages = firstPageData.result_info?.total_pages ?? 1;
652
652
 
653
653
  // Fetch remaining pages concurrently if needed
654
- const allZones = totalPages > 1
655
- ? await Promise.all([
656
- Promise.resolve(firstPageData.result),
657
- ...Array.from({ length: totalPages - 1 }, (_, i) =>
658
- fetchZonePage(i + 2).then(data => data.result)
659
- ),
660
- ]).then(results => results.flat())
661
- : firstPageData.result;
654
+ const allZones =
655
+ totalPages > 1
656
+ ? await Promise.all([
657
+ Promise.resolve(firstPageData.result),
658
+ ...Array.from({ length: totalPages - 1 }, (_, i) =>
659
+ fetchZonePage(i + 2).then((data) => data.result),
660
+ ),
661
+ ]).then((results) => results.flat())
662
+ : firstPageData.result;
662
663
 
663
664
  // Find the zone that best matches the hostname
664
665
  // We look for the longest matching zone name (most specific)