effect-cf 0.1.0 → 0.3.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/CHANGELOG.md +27 -0
- package/README.md +139 -1
- package/dist/index.d.mts +904 -311
- package/dist/index.mjs +616 -127
- package/package.json +3 -2
- package/src/DurableObject.ts +9 -6
- package/src/DurableObjectRpcWebSocket.ts +21 -17
- package/src/DurableObjectState.ts +12 -10
- package/src/DurableObjectWebSocket.ts +203 -6
- package/src/Queue.ts +116 -0
- package/src/QueueBinding.ts +131 -0
- package/src/QueueDefinition.ts +129 -0
- package/src/Worker.ts +57 -68
- package/src/WorkerDefinition.ts +6 -0
- package/src/Workflow.ts +279 -0
- package/src/WorkflowBinding.ts +254 -0
- package/src/WorkflowDefinition.ts +167 -0
- package/src/index.ts +6 -0
- package/src/internal/WorkerContext.ts +83 -0
package/src/index.ts
CHANGED
|
@@ -8,9 +8,15 @@ export * as DurableObjectState from "./DurableObjectState";
|
|
|
8
8
|
export * as DurableObjectWebSocket from "./DurableObjectWebSocket";
|
|
9
9
|
export * as DurableObjectStorage from "./DurableObjectStorage";
|
|
10
10
|
export * as Kv from "./Kv";
|
|
11
|
+
export * as Queue from "./Queue";
|
|
12
|
+
export * as QueueBinding from "./QueueBinding";
|
|
13
|
+
export * as QueueDefinition from "./QueueDefinition";
|
|
11
14
|
export * as Rpc from "./Rpc";
|
|
12
15
|
export * as RpcDefinition from "./RpcDefinition";
|
|
13
16
|
export * as ServiceBinding from "./ServiceBinding";
|
|
14
17
|
export * as Worker from "./Worker";
|
|
15
18
|
export * as WorkerDefinition from "./WorkerDefinition";
|
|
19
|
+
export * as Workflow from "./Workflow";
|
|
20
|
+
export * as WorkflowBinding from "./WorkflowBinding";
|
|
21
|
+
export * as WorkflowDefinition from "./WorkflowDefinition";
|
|
16
22
|
export { WorkerConfig, WorkerEnvironment, type WorkerEnv } from "./Environment";
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { Cause, Effect, Exit } from "effect";
|
|
2
|
+
|
|
3
|
+
import type { WorkerContextService, WorkerContextWaitUntilOptions } from "../Worker";
|
|
4
|
+
|
|
5
|
+
export type RunWaitUntilEffect = <A, E>(
|
|
6
|
+
effect: Effect.Effect<A, E, never>,
|
|
7
|
+
) => Promise<Exit.Exit<A, unknown>>;
|
|
8
|
+
|
|
9
|
+
const causeError = <E>(cause: Cause.Cause<E>) => new Error(Cause.pretty(cause));
|
|
10
|
+
|
|
11
|
+
const failureHandler = <E, R>(
|
|
12
|
+
cause: Cause.Cause<E>,
|
|
13
|
+
options: WorkerContextWaitUntilOptions<E, R> | undefined,
|
|
14
|
+
) =>
|
|
15
|
+
(
|
|
16
|
+
options?.onFailure?.(cause) ??
|
|
17
|
+
Effect.logError("WorkerContext.waitUntil failed", Cause.pretty(cause))
|
|
18
|
+
).pipe(
|
|
19
|
+
Effect.catchCause((handlerCause) =>
|
|
20
|
+
Effect.logError(
|
|
21
|
+
"WorkerContext.waitUntil failure handler failed",
|
|
22
|
+
Cause.pretty(cause),
|
|
23
|
+
Cause.pretty(handlerCause),
|
|
24
|
+
),
|
|
25
|
+
),
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
export const fromExecutionContext = (
|
|
29
|
+
ctx: globalThis.ExecutionContext,
|
|
30
|
+
runPromiseExit: RunWaitUntilEffect,
|
|
31
|
+
): WorkerContextService => {
|
|
32
|
+
const schedule = <A, E, R, R2 = never>(
|
|
33
|
+
effect: Effect.Effect<A, E, R>,
|
|
34
|
+
options: WorkerContextWaitUntilOptions<E, R2> | undefined,
|
|
35
|
+
mode: "observe" | "propagate",
|
|
36
|
+
) =>
|
|
37
|
+
Effect.context<R | R2>().pipe(
|
|
38
|
+
Effect.flatMap((context) =>
|
|
39
|
+
Effect.sync(() => {
|
|
40
|
+
const runHandler = (cause: Cause.Cause<E>) =>
|
|
41
|
+
runPromiseExit(
|
|
42
|
+
Effect.scoped(Effect.provideContext(failureHandler(cause, options), context)),
|
|
43
|
+
).then((exit) => {
|
|
44
|
+
if (Exit.isFailure(exit)) {
|
|
45
|
+
console.error(
|
|
46
|
+
"WorkerContext.waitUntil failure handler failed",
|
|
47
|
+
Cause.pretty(exit.cause),
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
ctx.waitUntil(
|
|
53
|
+
runPromiseExit(Effect.scoped(Effect.provideContext(effect, context))).then(
|
|
54
|
+
async (exit) => {
|
|
55
|
+
if (Exit.isSuccess(exit)) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
await runHandler(exit.cause as Cause.Cause<E>);
|
|
60
|
+
|
|
61
|
+
if (mode === "propagate") {
|
|
62
|
+
throw causeError(exit.cause as Cause.Cause<E>);
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
),
|
|
66
|
+
);
|
|
67
|
+
}),
|
|
68
|
+
),
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
raw: ctx,
|
|
73
|
+
waitUntil: <A, E, R, R2 = never>(
|
|
74
|
+
effect: Effect.Effect<A, E, R>,
|
|
75
|
+
options?: WorkerContextWaitUntilOptions<E, R2>,
|
|
76
|
+
) => schedule(effect, options, options?.mode ?? "observe"),
|
|
77
|
+
waitUntilPropagating: <A, E, R, R2 = never>(
|
|
78
|
+
effect: Effect.Effect<A, E, R>,
|
|
79
|
+
options?: Omit<WorkerContextWaitUntilOptions<E, R2>, "mode">,
|
|
80
|
+
) => schedule(effect, options, "propagate"),
|
|
81
|
+
passThroughOnException: Effect.sync(() => ctx.passThroughOnException()),
|
|
82
|
+
};
|
|
83
|
+
};
|