effect-cf 0.34.0 → 0.36.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.
@@ -0,0 +1,60 @@
1
+ import { Cause, Effect, Exit, FiberSet } from "effect";
2
+ //#region src/internal/NativeCallback.ts
3
+ /**
4
+ * Runs an Effect callback owned by a native Promise operation.
5
+ *
6
+ * The callback keeps the caller's context, while a private scope owns its
7
+ * fiber. Closing that scope interrupts and joins the callback before awaiting
8
+ * the native operation's settlement.
9
+ */
10
+ const runNativeCallback = Effect.fnUntraced(function* (operation) {
11
+ const callerContext = yield* Effect.context();
12
+ return yield* Effect.scoped(Effect.gen(function* () {
13
+ let nativePromise;
14
+ yield* Effect.addFinalizer(() => {
15
+ const promise = nativePromise;
16
+ return promise === void 0 ? Effect.void : Effect.promise(() => promise.then(() => void 0, () => void 0));
17
+ });
18
+ const runPromise = yield* FiberSet.makeRuntimePromise();
19
+ return yield* Effect.callback((resume) => {
20
+ try {
21
+ nativePromise = operation((effect) => runPromise(Effect.provideContext(Effect.exit(effect), callerContext)));
22
+ } catch (cause) {
23
+ resume(Effect.fail(cause));
24
+ return;
25
+ }
26
+ nativePromise.then((value) => resume(Effect.succeed(value)), (cause) => resume(Effect.fail(cause)));
27
+ });
28
+ }));
29
+ });
30
+ //#endregion
31
+ //#region src/internal/WorkerContext.ts
32
+ const causeError = (cause) => {
33
+ const squashed = Cause.squash(cause);
34
+ return squashed instanceof Error ? squashed : new Error(Cause.pretty(cause), { cause: squashed });
35
+ };
36
+ const failureHandler = (label, cause, options) => Effect.suspend(() => options?.onFailure?.(cause) ?? Effect.logError(`${label} failed`, Cause.pretty(cause))).pipe(Effect.catchCause((handlerCause) => Effect.logError(`${label} failure handler failed`, Cause.pretty(cause), Cause.pretty(handlerCause))));
37
+ const makeWaitUntilScheduler = (label, register, runPromiseExit = (effect) => Effect.runPromiseExit(effect)) => {
38
+ return (effect, options, mode) => Effect.context().pipe(Effect.flatMap((context) => Effect.sync(() => {
39
+ const runHandler = (cause) => runPromiseExit(Effect.provideContext(Effect.scoped(failureHandler(label, cause, options)), context)).then((exit) => {
40
+ if (Exit.isFailure(exit)) console.error(`${label} failure handler failed`, Cause.pretty(exit.cause));
41
+ });
42
+ register(runPromiseExit(Effect.provideContext(Effect.scoped(effect), context)).then(async (exit) => {
43
+ if (Exit.isSuccess(exit)) return;
44
+ await runHandler(exit.cause);
45
+ if (mode === "propagate") throw causeError(exit.cause);
46
+ }));
47
+ })));
48
+ };
49
+ const fromExecutionContext = (ctx, runPromiseExit = (effect) => Effect.runPromiseExit(effect)) => {
50
+ const schedule = makeWaitUntilScheduler("WorkerContext.waitUntil", (promise) => ctx.waitUntil(promise), runPromiseExit);
51
+ return {
52
+ raw: ctx,
53
+ waitUntil: (effect, options) => schedule(effect, options, options?.mode ?? "observe"),
54
+ waitUntilPropagating: (effect, options) => schedule(effect, options, "propagate"),
55
+ passThroughOnException: Effect.sync(() => ctx.passThroughOnException()),
56
+ abort: (reason) => Effect.sync(() => ctx.abort(reason))
57
+ };
58
+ };
59
+ //#endregion
60
+ export { makeWaitUntilScheduler as n, runNativeCallback as r, fromExecutionContext as t };
@@ -1,5 +1,5 @@
1
1
  import { c as WorkerEnvironment, r as BindingValidationError, t as BindingNotFoundError } from "./Binding-DKcLrQCM.mjs";
2
- import { w as ServiceFreeSchema } from "./DurableObjectState-CrEunfQ-.mjs";
2
+ import { S as ServiceFreeSchema } from "./Worker-Dl6_Pijl.mjs";
3
3
  import { Context, Effect, Option, Schema } from "effect";
4
4
  declare namespace WorkflowBinding_d_exports {
5
5
  export { WorkflowBindingClient, WorkflowBindingDefinition, WorkflowInstance, WorkflowInstanceCreateBatchOptions, WorkflowInstanceCreateOptions, WorkflowInstanceEvent, WorkflowInstanceLocationHint, WorkflowInstanceRestartOptions, WorkflowInstanceStatus, WorkflowInstanceStatusName, WorkflowInstanceTerminateOptions, WorkflowOperationError, WorkflowResultDecodeError, isWorkflow, layer, makeClient };
package/dist/index.d.mts CHANGED
@@ -1,9 +1,10 @@
1
1
  import { a as Environment_d_exports, c as WorkerEnvironment, i as Binding_d_exports, n as BindingService, o as WorkerConfig, r as BindingValidationError, s as WorkerEnv, t as BindingNotFoundError } from "./Binding-DKcLrQCM.mjs";
2
2
  import { l as Artifacts_d_exports } from "./Artifacts-B3r3RuTB.mjs";
3
- import { A as DurableWebSocket, C as RpcDefinition_d_exports, D as AcceptUpgradeOptions, E as Rpc_d_exports, M as DurableObjectStorage_d_exports, N as StorageOperationError, O as AcceptedUpgrade, S as ReservedMethodName$2, T as Provider, _ as Queue_d_exports, a as NativeRequest, b as Definition$4, c as WorkerDefinition_d_exports, d as AsyncMethodCloudflareReturn, f as AsyncMethodKey, g as RpcTracing_d_exports, h as RpcInvocationInfo, i as ExecutionContext, j as DurableWebSocketAttachmentError, k as DurableObjectWebSocket_d_exports, l as ServiceBinding_d_exports, m as ReceiverOptions, o as WorkerContext, p as AsyncMethodSuccess, r as DurableObjectState_d_exports, s as Worker_d_exports, t as DurableObjectState, u as AsyncMethodArgs, v as QueueDefinition_d_exports, w as ServiceFreeSchema$1, x as Method$2, y as QueueBinding_d_exports } from "./DurableObjectState-CrEunfQ-.mjs";
3
+ import { a as AcceptedUpgrade, c as DurableWebSocketAttachmentError, i as AcceptUpgradeOptions, l as DurableObjectStorage_d_exports, o as DurableObjectWebSocket_d_exports, r as DurableObjectState_d_exports, s as DurableWebSocket, t as DurableObjectState, u as StorageOperationError } from "./DurableObjectState-CpgAmFqo.mjs";
4
+ import { C as Provider, S as ServiceFreeSchema$1, _ as QueueBinding_d_exports, a as Worker_d_exports, b as ReservedMethodName$2, c as AsyncMethodArgs, d as AsyncMethodSuccess, f as ReceiverOptions, g as QueueDefinition_d_exports, h as Queue_d_exports, l as AsyncMethodCloudflareReturn, m as RpcTracing_d_exports, n as NativeRequest, o as WorkerDefinition_d_exports, p as RpcInvocationInfo, r as WorkerContext, s as ServiceBinding_d_exports, t as ExecutionContext, u as AsyncMethodKey, v as Definition$4, w as Rpc_d_exports, x as RpcDefinition_d_exports, y as Method$2 } from "./Worker-Dl6_Pijl.mjs";
4
5
  import { t as Hyperdrive_d_exports } from "./Hyperdrive-HrwFlZ6x.mjs";
5
6
  import { t as ContainerNamespace_d_exports } from "./ContainerNamespace-DbLaeFhN.mjs";
6
- import { a as WorkflowInstanceCreateOptions, i as WorkflowInstanceCreateBatchOptions, n as WorkflowBinding_d_exports, r as WorkflowInstance, s as WorkflowOperationError, t as WorkflowBindingClient } from "./WorkflowBinding-BumVQTuW.mjs";
7
+ import { a as WorkflowInstanceCreateOptions, i as WorkflowInstanceCreateBatchOptions, n as WorkflowBinding_d_exports, r as WorkflowInstance, s as WorkflowOperationError, t as WorkflowBindingClient } from "./WorkflowBinding-CvJT9hB5.mjs";
7
8
  import { Cause, Config, Context, Data, DateTime, Duration, Effect, Layer, Option, Predicate, Redacted, Schema, Scope } from "effect";
8
9
  import { Headers as Headers$1, HttpClient, HttpClientResponse } from "effect/unstable/http";
9
10
  import { OtlpExporter, OtlpResource } from "effect/unstable/observability";
@@ -513,6 +514,8 @@ declare const layerProtobuf: (options?: Omit<LayerOptions$9, "serialization">) =
513
514
  *
514
515
  * Provide this layer at runtime scope for long-lived metrics aggregation, or at
515
516
  * handler scope when traces/logs should flush as the Cloudflare event finishes.
517
+ * A handler-scoped exporter can delay scope closure while its own finalizer
518
+ * flushes; the two-second background flush limit does not bound layer shutdown.
516
519
  */
517
520
  declare const layerWorker: (options?: WorkerLayerOptions) => Layer.Layer<OtlpExporter.Flusher>;
518
521
  /**
@@ -660,7 +663,7 @@ type DurableObjectNamespaceEffectClient<Api extends object, Definition extends D
660
663
  * Use this for RPC methods that return Cloudflare RPC resources or other
661
664
  * disposable objects whose lifetime should be tied to an Effect scope.
662
665
  */
663
- readonly scopedCall: <Method extends StubMethodKey<Api>>(stub: DurableObjectStubClient<Api>, method: Method, ...args: StubMethodArgs<Api, Method>) => Effect.Effect<Awaited<StubMethodSuccess<Api, Method>>, unknown, Scope.Scope>;
666
+ readonly scopedCall: <Method extends StubMethodKey<Api>>(stub: DurableObjectStubClient<Api>, method: Method, ...args: StubMethodArgs<Api, Method>) => Effect.Effect<Awaited<StubMethodSuccess<Api, Method>>, DurableObjectRpcError, Scope.Scope>;
664
667
  /**
665
668
  * Exposes the underlying native Durable Object namespace binding.
666
669
  *
@@ -679,7 +682,7 @@ type DurableObjectNamespaceStaticClient<R, Api extends object, Definition extend
679
682
  readonly fetch: (stub: DurableObjectStubClient<Api>, input: RequestInfo | URL, init?: RequestInit) => Effect.Effect<globalThis.Response, DurableObjectFetchError, R>;
680
683
  readonly rpc: <Method extends StubMethodKey<Api>>(stub: DurableObjectStubClient<Api>, method: Method, ...args: StubMethodArgs<Api, Method>) => Effect.Effect<StubMethodCloudflareReturn<Api, Method>, DurableObjectRpcError, R>;
681
684
  readonly call: <Method extends StubMethodKey<Api>>(stub: DurableObjectStubClient<Api>, method: Method, ...args: StubMethodArgs<Api, Method>) => Effect.Effect<StubMethodSuccess<Api, Method>, DurableObjectRpcError, R>;
682
- readonly scopedCall: <Method extends StubMethodKey<Api>>(stub: DurableObjectStubClient<Api>, method: Method, ...args: StubMethodArgs<Api, Method>) => Effect.Effect<Awaited<StubMethodSuccess<Api, Method>>, unknown, Scope.Scope | R>;
685
+ readonly scopedCall: <Method extends StubMethodKey<Api>>(stub: DurableObjectStubClient<Api>, method: Method, ...args: StubMethodArgs<Api, Method>) => Effect.Effect<Awaited<StubMethodSuccess<Api, Method>>, DurableObjectRpcError, Scope.Scope | R>;
683
686
  readonly rawUnsafe: () => Effect.Effect<DurableObjectNamespaceClient<Api>, never, R>;
684
687
  };
685
688
  declare const isDurableObjectNamespaceClient: <Api extends object>(value: NamespaceCandidate) => value is DurableObjectNamespaceClient<Api>;
@@ -842,6 +845,12 @@ interface DurableObjectOptions<RRuntime, REvent = never, EventLayerError = never
842
845
  type DurableObjectClass<Rpc extends DurableObjectRpc<ROut>, ROut> = new (state: globalThis.DurableObjectState, env: WorkerEnv) => DurableObject<WorkerEnv> & DurableObjectRpcApi<Rpc, ROut> & {
843
846
  [RunSymbol]<A, E>(effect: Effect.Effect<A, E, HandlerContext<ROut>>, options?: RunOptions): Promise<A>;
844
847
  };
848
+ /**
849
+ * Builds a Durable Object whose base `layer` lives for the in-memory instance.
850
+ * Cloudflare does not expose an eviction or shutdown hook, so finalizers in
851
+ * that layer are not guaranteed to run. Put resources that require timely
852
+ * release in `eventLayer`, whose scope closes after each handled event.
853
+ */
845
854
  declare const make$6: <ROut, LayerError, REvent = never, EventLayerError = never, const Rpc extends DurableObjectRpc<ROut | REvent> = Record<never, never>>(layer: Layer.Layer<ROut, LayerError, DurableObjectState | WorkerEnvironment>, options?: DurableObjectOptions<ROut, REvent, EventLayerError, Rpc>) => DurableObjectClass<Rpc, ROut | REvent>;
846
855
  declare namespace DurableObjectAlarm_d_exports {
847
856
  export { AlarmDefinitionConfig, AlarmDefinitionEntry, AlarmDefinitionPayload, AlarmDefinitions, AlarmFailurePolicy, AlarmPayload, AlarmPayloadSchema, AlarmRef, AlarmRetryPolicy, AlarmScheduler, DefinedAlarmEvent, DefinedAlarmHandlers, DurableObjectAlarm, DurableObjectAlarmError, DurableObjectAlarmEvent, InvalidAlarmPayloadError, InvalidAlarmRefError, InvalidProcessDueAlarmsOptionsError, InvalidRepeatEveryError, ProcessDueAlarmsFailure, ProcessDueAlarmsFailureAction, ProcessDueAlarmsHandler, ProcessDueAlarmsMode, ProcessDueAlarmsOptions, ProcessDueAlarmsResult, ScheduleAlarmInput, StoredAlarmDecodeError, define, processDue };