effect-cf 0.41.1 → 0.42.1

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 (62) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/README.md +28 -0
  3. package/dist/AiGateway.d.mts +2 -2
  4. package/dist/AnalyticsEngine.d.mts +2 -2
  5. package/dist/Artifacts.d.mts +2 -2
  6. package/dist/BrowserRendering.d.mts +2 -2
  7. package/dist/ContainerNamespace.d.mts +2 -2
  8. package/dist/D1.d.mts +9 -12
  9. package/dist/DurableObject.d.mts +9 -5
  10. package/dist/DurableObject.mjs +9 -4
  11. package/dist/DurableObjectDefinition.d.mts +15 -12
  12. package/dist/DurableObjectDefinition.mjs +5 -3
  13. package/dist/DurableObjectNamespace.d.mts +10 -8
  14. package/dist/DurableObjectSqlite.mjs +1 -1
  15. package/dist/Email.d.mts +2 -2
  16. package/dist/Hyperdrive.d.mts +2 -2
  17. package/dist/Images.d.mts +2 -2
  18. package/dist/Kv.d.mts +2 -2
  19. package/dist/Queue.d.mts +5 -4
  20. package/dist/QueueDefinition.d.mts +4 -4
  21. package/dist/R2.d.mts +2 -2
  22. package/dist/RpcDefinition.d.mts +26 -12
  23. package/dist/RpcDefinition.mjs +22 -5
  24. package/dist/Sandbox.d.mts +1 -1
  25. package/dist/ServiceBinding.d.mts +17 -18
  26. package/dist/Vectorize.d.mts +2 -2
  27. package/dist/Worker.d.mts +5 -3
  28. package/dist/Worker.mjs +4 -3
  29. package/dist/WorkerDefinition.d.mts +15 -11
  30. package/dist/WorkerDefinition.mjs +5 -3
  31. package/dist/WorkersAi.d.mts +2 -2
  32. package/dist/Workflow.d.mts +5 -4
  33. package/dist/WorkflowDefinition.d.mts +4 -4
  34. package/dist/index.d.mts +2 -2
  35. package/dist/internal/NativeCallback.mjs +5 -2
  36. package/package.json +1 -1
  37. package/src/AiGateway.ts +2 -0
  38. package/src/AnalyticsEngine.ts +2 -0
  39. package/src/Artifacts.ts +2 -0
  40. package/src/BrowserRendering.ts +2 -0
  41. package/src/ContainerNamespace.ts +2 -0
  42. package/src/D1.ts +16 -8
  43. package/src/DurableObject.ts +30 -4
  44. package/src/DurableObjectDefinition.ts +33 -17
  45. package/src/DurableObjectNamespace.ts +19 -35
  46. package/src/Email.ts +2 -0
  47. package/src/Hyperdrive.ts +2 -0
  48. package/src/Images.ts +2 -0
  49. package/src/Kv.ts +2 -0
  50. package/src/Queue.ts +4 -7
  51. package/src/QueueDefinition.ts +7 -4
  52. package/src/R2.ts +2 -0
  53. package/src/RpcDefinition.ts +64 -25
  54. package/src/Sandbox.ts +2 -0
  55. package/src/ServiceBinding.ts +39 -34
  56. package/src/Vectorize.ts +2 -0
  57. package/src/Worker.ts +12 -1
  58. package/src/WorkerDefinition.ts +28 -17
  59. package/src/WorkersAi.ts +2 -0
  60. package/src/Workflow.ts +9 -8
  61. package/src/WorkflowDefinition.ts +12 -4
  62. package/src/internal/NativeCallback.ts +13 -2
@@ -13,6 +13,8 @@ import * as ErrorMessage from "./internal/ErrorMessage";
13
13
  import * as RpcInvocation from "./internal/RpcInvocation";
14
14
  import * as RpcTracing from "./RpcTracing";
15
15
 
16
+ export type { BindingNotFoundError, BindingValidationError } from "./Binding";
17
+
16
18
  const TypeId = "effect-cf/ServiceBinding" as const;
17
19
  const expectedServiceBinding = "Worker service binding with fetch()";
18
20
 
@@ -91,11 +93,25 @@ type ServiceMethodCloudflareReturn<
91
93
  Method extends keyof Api,
92
94
  > = RpcInvocation.AsyncMethodCloudflareReturn<Api, Method>;
93
95
 
94
- type ServiceCall<R, Api> = <Method extends ServiceMethodKey<Api>>(
96
+ export type ServiceCall<R, Api> = <Method extends ServiceMethodKey<Api>>(
95
97
  method: Method,
96
98
  ...args: ServiceMethodArgs<Api, Method>
97
99
  ) => Effect.Effect<ServiceMethodSuccess<Api, Method>, ServiceBindingRpcError, R>;
98
100
 
101
+ export type ServiceRpc<R, Api> = <Method extends ServiceMethodKey<Api>>(
102
+ method: Method,
103
+ ...args: ServiceMethodArgs<Api, Method>
104
+ ) => Effect.Effect<ServiceMethodCloudflareReturn<Api, Method>, ServiceBindingRpcError, R>;
105
+
106
+ export type ServiceScopedCall<R, Api> = <Method extends ServiceMethodKey<Api>>(
107
+ method: Method,
108
+ ...args: ServiceMethodArgs<Api, Method>
109
+ ) => Effect.Effect<
110
+ Awaited<ServiceMethodSuccess<Api, Method>>,
111
+ ServiceBindingRpcError,
112
+ Scope.Scope | R
113
+ >;
114
+
99
115
  type DefinitionDirectMethods<R, Definition extends WorkerDefinition.Definition.Any> = {
100
116
  readonly [Method in RpcDefinition.Definition.MethodNames<Definition>]: (
101
117
  ...args: RpcDefinition.Method.Args<Definition["methods"][Method]>
@@ -127,20 +143,14 @@ export type ServiceBindingEffectClient<
127
143
  *
128
144
  * Most application code should use {@link call} instead.
129
145
  */
130
- readonly rpc: <Method extends ServiceMethodKey<Api>>(
131
- method: Method,
132
- ...args: ServiceMethodArgs<Api, Method>
133
- ) => Effect.Effect<ServiceMethodCloudflareReturn<Api, Method>, ServiceBindingRpcError>;
146
+ readonly rpc: ServiceRpc<never, Api>;
134
147
  /**
135
148
  * Invokes a Worker RPC method, resolves Cloudflare's RPC result, and decodes
136
149
  * the success value when the binding was created from a definition.
137
150
  *
138
151
  * This is the normal choice when application code wants the final typed value.
139
152
  */
140
- readonly call: <Method extends ServiceMethodKey<Api>>(
141
- method: Method,
142
- ...args: ServiceMethodArgs<Api, Method>
143
- ) => Effect.Effect<ServiceMethodSuccess<Api, Method>, ServiceBindingRpcError>;
153
+ readonly call: ServiceCall<never, Api>;
144
154
  /**
145
155
  * Invokes a Worker RPC method in the current `Scope`, resolves Cloudflare's RPC
146
156
  * result, decodes definition-backed success values, and disposes the resolved
@@ -149,14 +159,7 @@ export type ServiceBindingEffectClient<
149
159
  * Use this for RPC methods that return Cloudflare RPC resources or other
150
160
  * disposable objects whose lifetime should be tied to an Effect scope.
151
161
  */
152
- readonly scopedCall: <Method extends ServiceMethodKey<Api>>(
153
- method: Method,
154
- ...args: ServiceMethodArgs<Api, Method>
155
- ) => Effect.Effect<
156
- Awaited<ServiceMethodSuccess<Api, Method>>,
157
- ServiceBindingRpcError,
158
- Scope.Scope
159
- >;
162
+ readonly scopedCall: ServiceScopedCall<never, Api>;
160
163
  };
161
164
 
162
165
  export type ServiceBindingStaticClient<
@@ -168,24 +171,26 @@ export type ServiceBindingStaticClient<
168
171
  input: RequestInfo | URL,
169
172
  init?: RequestInit,
170
173
  ) => Effect.Effect<globalThis.Response, ServiceBindingFetchError, R>;
171
- readonly rpc: <Method extends ServiceMethodKey<Api>>(
172
- method: Method,
173
- ...args: ServiceMethodArgs<Api, Method>
174
- ) => Effect.Effect<ServiceMethodCloudflareReturn<Api, Method>, ServiceBindingRpcError, R>;
175
- readonly call: <Method extends ServiceMethodKey<Api>>(
176
- method: Method,
177
- ...args: ServiceMethodArgs<Api, Method>
178
- ) => Effect.Effect<ServiceMethodSuccess<Api, Method>, ServiceBindingRpcError, R>;
179
- readonly scopedCall: <Method extends ServiceMethodKey<Api>>(
180
- method: Method,
181
- ...args: ServiceMethodArgs<Api, Method>
182
- ) => Effect.Effect<
183
- Awaited<ServiceMethodSuccess<Api, Method>>,
184
- ServiceBindingRpcError,
185
- Scope.Scope | R
186
- >;
174
+ readonly rpc: ServiceRpc<R, Api>;
175
+ readonly call: ServiceCall<R, Api>;
176
+ readonly scopedCall: ServiceScopedCall<R, Api>;
187
177
  };
188
178
 
179
+ export type ServiceClass<
180
+ Self,
181
+ Id extends string,
182
+ Api extends object = never,
183
+ Definition extends WorkerDefinition.Definition.Any | undefined = undefined,
184
+ > = Binding.BindingService<
185
+ Self,
186
+ Id,
187
+ ServiceBindingEffectClient<ApiOrDefinition<Api, Definition>, Definition>
188
+ > &
189
+ ServiceBindingStaticClient<Self, ApiOrDefinition<Api, Definition>, Definition> & {
190
+ readonly [TypeId]: typeof TypeId;
191
+ readonly definition: ServiceBindingDefinition<Definition>;
192
+ };
193
+
189
194
  type ServiceBindingValue = Schema.Schema.Type<typeof Schema.Unknown>;
190
195
 
191
196
  const ServiceBindingSchema = Schema.declare(
@@ -384,7 +389,7 @@ export const Service =
384
389
  >(
385
390
  id: Id,
386
391
  definition: ServiceBindingDefinition<Definition>,
387
- ) => {
392
+ ): ServiceClass<Self, Id, Api, Definition> => {
388
393
  type ServiceApi = ApiOrDefinition<Api, Definition>;
389
394
 
390
395
  const tag = Binding.Service<Self>()(
package/src/Vectorize.ts CHANGED
@@ -25,6 +25,8 @@ import * as Binding from "./Binding";
25
25
  import type { WorkerEnvironment } from "./Environment";
26
26
  import * as ErrorMessage from "./internal/ErrorMessage";
27
27
 
28
+ export type { BindingNotFoundError, BindingValidationError } from "./Binding";
29
+
28
30
  const expectedVectorizeBinding =
29
31
  "Vectorize index binding with describe(), query(), insert(), upsert(), deleteByIds(), and getByIds()";
30
32
 
package/src/Worker.ts CHANGED
@@ -20,6 +20,15 @@ import * as Runtime from "./internal/Runtime";
20
20
  import * as Telemetry from "./internal/Telemetry";
21
21
  import { fromExecutionContext } from "./internal/WorkerContext";
22
22
 
23
+ export type { BindingNotFoundError, BindingValidationError } from "./Binding";
24
+ export type {
25
+ ServiceBindingFetchError,
26
+ ServiceBindingRpcError,
27
+ ServiceCall,
28
+ ServiceRpc,
29
+ ServiceScopedCall,
30
+ } from "./ServiceBinding";
31
+
23
32
  export class ExecutionContext extends Context.Service<
24
33
  ExecutionContext,
25
34
  globalThis.ExecutionContext
@@ -579,14 +588,16 @@ export type {
579
588
  LayerOptions,
580
589
  Method,
581
590
  Methods,
591
+ NativeSchema,
582
592
  NoReservedMethods,
583
593
  Options,
594
+ RpcSchema,
584
595
  ServerApi,
585
596
  ServiceFreeSchema,
586
597
  TagClass,
587
598
  } from "./WorkerDefinition";
588
599
 
589
- export { implement, method, Tag } from "./WorkerDefinition";
600
+ export { implement, method, native, Tag } from "./WorkerDefinition";
590
601
 
591
602
  /** @deprecated Use {@link WorkerRpcContract}. */
592
603
  export { type WorkerRpcContract as "WorkerRpcShape" };
@@ -11,6 +11,15 @@ import * as RpcDefinition from "./RpcDefinition";
11
11
  import { recordDecodedArgs } from "./internal/RpcInvocation";
12
12
  import * as ServiceBinding from "./ServiceBinding";
13
13
 
14
+ export type { BindingNotFoundError, BindingValidationError } from "./Binding";
15
+ export type {
16
+ ServiceBindingFetchError,
17
+ ServiceBindingRpcError,
18
+ ServiceCall,
19
+ ServiceRpc,
20
+ ServiceScopedCall,
21
+ } from "./ServiceBinding";
22
+
14
23
  /**
15
24
  * The client tuple types are re-established by the final `TagClass` cast, so
16
25
  * these internal invocations erase the binding client's generic argument
@@ -19,38 +28,41 @@ import * as ServiceBinding from "./ServiceBinding";
19
28
  type UnsafeInvoke<E> = (...args: ReadonlyArray<unknown>) => Effect.Effect<unknown, E>;
20
29
 
21
30
  export type ServiceFreeSchema = S.Codec<any, any, never, never>;
31
+ export type NativeSchema<Schema extends ServiceFreeSchema = ServiceFreeSchema> =
32
+ RpcDefinition.NativeSchema<Schema>;
33
+ export type RpcSchema = RpcDefinition.RpcSchema;
22
34
 
23
35
  export interface Method<
24
- Args extends ReadonlyArray<ServiceFreeSchema> = ReadonlyArray<ServiceFreeSchema>,
25
- Success extends ServiceFreeSchema = ServiceFreeSchema,
36
+ Args extends ReadonlyArray<RpcSchema> = ReadonlyArray<RpcSchema>,
37
+ Success extends RpcSchema = RpcSchema,
26
38
  > {
27
39
  readonly args: Args;
28
40
  readonly success: Success;
29
41
  }
30
42
 
31
43
  export namespace Method {
32
- export type Any = Method<ReadonlyArray<ServiceFreeSchema>, ServiceFreeSchema>;
44
+ export type Any = Method<ReadonlyArray<RpcSchema>, RpcSchema>;
33
45
 
34
- type ArgsFromSchemas<Args extends ReadonlyArray<ServiceFreeSchema>> = Args extends readonly []
46
+ type ArgsFromSchemas<Args extends ReadonlyArray<RpcSchema>> = Args extends readonly []
35
47
  ? []
36
48
  : Args extends readonly [
37
- infer Head extends ServiceFreeSchema,
38
- ...infer Tail extends ReadonlyArray<ServiceFreeSchema>,
49
+ infer Head extends RpcSchema,
50
+ ...infer Tail extends ReadonlyArray<RpcSchema>,
39
51
  ]
40
- ? [S.Schema.Type<Head>, ...ArgsFromSchemas<Tail>]
41
- : Array<S.Schema.Type<Args[number]>>;
52
+ ? [RpcDefinition.SchemaType<Head>, ...ArgsFromSchemas<Tail>]
53
+ : Array<RpcDefinition.SchemaType<Args[number]>>;
42
54
 
43
- type EncodedArgsFromSchemas<Args extends ReadonlyArray<ServiceFreeSchema>> = {
44
- [Index in keyof Args]: S.Json;
55
+ type EncodedArgsFromSchemas<Args extends ReadonlyArray<RpcSchema>> = {
56
+ [Index in keyof Args]: RpcDefinition.WireEncoded<Args[Index]>;
45
57
  };
46
58
 
47
59
  export type Args<Self extends Any> = ArgsFromSchemas<Self["args"]>;
48
60
 
49
61
  export type EncodedArgs<Self extends Any> = EncodedArgsFromSchemas<Self["args"]>;
50
62
 
51
- export type Success<Self extends Any> = S.Schema.Type<Self["success"]>;
63
+ export type Success<Self extends Any> = RpcDefinition.SchemaType<Self["success"]>;
52
64
 
53
- export type EncodedSuccess<_Self extends Any> = S.Json;
65
+ export type EncodedSuccess<Self extends Any> = RpcDefinition.WireEncoded<Self["success"]>;
54
66
  }
55
67
 
56
68
  export type Methods = Record<string, Method.Any>;
@@ -227,18 +239,17 @@ const assumeTagClass = <Self, Id extends string, MethodDefinitions extends Metho
227
239
  * Defines a single RPC method schema in a worker definition.
228
240
  */
229
241
  export const method: {
230
- <Success extends ServiceFreeSchema>(definition: {
242
+ <Success extends RpcSchema>(definition: {
231
243
  readonly success: Success;
232
244
  }): Method<readonly [], Success>;
233
- <
234
- const Args extends ReadonlyArray<ServiceFreeSchema>,
235
- Success extends ServiceFreeSchema,
236
- >(definition: {
245
+ <const Args extends ReadonlyArray<RpcSchema>, Success extends RpcSchema>(definition: {
237
246
  readonly args: Args;
238
247
  readonly success: Success;
239
248
  }): Method<Args, Success>;
240
249
  } = RpcDefinition.method;
241
250
 
251
+ export const native = RpcDefinition.native;
252
+
242
253
  const makeDefinition = <Id extends string, const MethodDefinitions extends Methods>(
243
254
  id: Id,
244
255
  methods: MethodDefinitions & NoReservedMethods<MethodDefinitions>,
package/src/WorkersAi.ts CHANGED
@@ -22,6 +22,8 @@ import * as Binding from "./Binding";
22
22
  import type { WorkerEnvironment } from "./Environment";
23
23
  import * as ErrorMessage from "./internal/ErrorMessage";
24
24
 
25
+ export type { BindingNotFoundError, BindingValidationError } from "./Binding";
26
+
25
27
  const expectedWorkersAiBinding = "Workers AI binding with run(), gateway(), and models()";
26
28
 
27
29
  export class WorkersAiOperationError extends Data.TaggedError("WorkersAiOperationError")<{
package/src/Workflow.ts CHANGED
@@ -27,6 +27,14 @@ import * as ErrorMessage from "./internal/ErrorMessage";
27
27
  import * as Runtime from "./internal/Runtime";
28
28
  import { fromExecutionContext } from "./internal/WorkerContext";
29
29
 
30
+ export type { BindingNotFoundError, BindingValidationError } from "./Binding";
31
+ export type {
32
+ WorkflowInstance,
33
+ WorkflowInstanceCreateOptions,
34
+ WorkflowInstanceCreateBatchOptions,
35
+ WorkflowOperationError,
36
+ } from "./WorkflowBinding";
37
+
30
38
  export interface WorkflowEventService<Payload = unknown> {
31
39
  readonly raw: CloudflareWorkflowEvent<unknown>;
32
40
  readonly payload: Payload;
@@ -316,14 +324,7 @@ export namespace Definition {
316
324
 
317
325
  export type LayerOptions = WorkflowDefinition.LayerOptions;
318
326
 
319
- export type TagClass<
320
- Self,
321
- Id extends string,
322
- Payload extends WorkflowDefinition.Definition.Any["payload"],
323
- Result extends WorkflowDefinition.Definition.Any["result"],
324
- > = WorkflowDefinition.TagClass<Self, Id, Payload, Result>;
325
-
326
- export { implement, Tag } from "./WorkflowDefinition";
327
+ export { implement, Tag, type TagClass } from "./WorkflowDefinition";
327
328
 
328
329
  export type Handler<
329
330
  ROut,
@@ -10,6 +10,14 @@ import type * as RpcDefinition from "./RpcDefinition";
10
10
  import * as WorkflowBinding from "./WorkflowBinding";
11
11
  import * as WorkflowEntrypoint from "./Workflow";
12
12
 
13
+ export type { BindingNotFoundError, BindingValidationError } from "./Binding";
14
+ export type {
15
+ WorkflowInstance,
16
+ WorkflowInstanceCreateOptions,
17
+ WorkflowInstanceCreateBatchOptions,
18
+ WorkflowOperationError,
19
+ } from "./WorkflowBinding";
20
+
13
21
  export interface Definition<
14
22
  Id extends string = string,
15
23
  Payload extends RpcDefinition.ServiceFreeSchema = RpcDefinition.ServiceFreeSchema,
@@ -44,16 +52,16 @@ export type LayerOptions = {
44
52
  readonly binding: string;
45
53
  };
46
54
 
47
- export interface TagClass<
55
+ export type TagClass<
48
56
  Self,
49
57
  Id extends string,
50
58
  Payload extends RpcDefinition.ServiceFreeSchema,
51
59
  Result extends RpcDefinition.ServiceFreeSchema,
52
- > extends Context.ServiceClass<
60
+ > = Context.ServiceClass<
53
61
  Self,
54
62
  `effect-cf/Workflow/${Id}`,
55
63
  WorkflowBinding.WorkflowBindingClient<Payload, Result>
56
- > {
64
+ > & {
57
65
  readonly id: Id;
58
66
  readonly payload: Payload;
59
67
  readonly result: Result;
@@ -94,7 +102,7 @@ export interface TagClass<
94
102
  Self
95
103
  >;
96
104
  readonly rawUnsafe: Effect.Effect<globalThis.Workflow<S.Codec.Encoded<Payload>>, never, Self>;
97
- }
105
+ };
98
106
 
99
107
  const makeDefinition = <
100
108
  Id extends string,
@@ -1,9 +1,13 @@
1
+ import * as Context from "effect/Context";
1
2
  import * as Effect from "effect/Effect";
2
3
  import * as Exit from "effect/Exit";
3
4
  import * as FiberSet from "effect/FiberSet";
5
+ import * as Scheduler from "effect/Scheduler";
4
6
 
5
7
  type RunCallback<A, E, R> = (effect: Effect.Effect<A, E, R>) => Promise<Exit.Exit<A, E>>;
6
8
 
9
+ const callbackScheduler = new Scheduler.MixedScheduler("sync");
10
+
7
11
  /**
8
12
  * Runs an Effect callback owned by a native Promise operation.
9
13
  *
@@ -14,7 +18,14 @@ type RunCallback<A, E, R> = (effect: Effect.Effect<A, E, R>) => Promise<Exit.Exi
14
18
  export const runNativeCallback = Effect.fnUntraced(function* <A, E, R, B>(
15
19
  operation: (run: RunCallback<A, E, R>) => Promise<B>,
16
20
  ): Effect.fn.Return<B, unknown, R> {
17
- const callerContext = yield* Effect.context<R>();
21
+ // An older timer in the blocked parent input gate prevents later timers in
22
+ // this owner from running. Yield through microtasks while the native gate is
23
+ // held, including when the caller explicitly provided its own scheduler.
24
+ const callbackContext = Context.add(
25
+ yield* Effect.context<R>(),
26
+ Scheduler.Scheduler,
27
+ callbackScheduler,
28
+ );
18
29
 
19
30
  return yield* Effect.scoped(
20
31
  Effect.gen(function* () {
@@ -39,7 +50,7 @@ export const runNativeCallback = Effect.fnUntraced(function* <A, E, R, B>(
39
50
  return yield* Effect.callback<B, unknown>((resume) => {
40
51
  try {
41
52
  nativePromise = operation((effect) =>
42
- runPromise(Effect.provideContext(Effect.exit(effect), callerContext)),
53
+ runPromise(Effect.provideContext(Effect.exit(effect), callbackContext)),
43
54
  );
44
55
  } catch (cause) {
45
56
  resume(Effect.fail(cause));