effect-cf 0.23.1 → 0.25.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,184 @@
1
+ import { d as WorkerConfig, f as WorkerEnvironment, r as fromDurableObjectState, t as DurableObjectState } from "./DurableObjectState-B5RCFnRy.mjs";
2
+ import { ConfigProvider, Effect, Exit, Layer } from "effect";
3
+ import { env } from "cloudflare:workers";
4
+ import { abortAllDurableObjects as abortAllDurableObjects$1, adminSecretsStore as adminSecretsStore$1, applyD1Migrations as applyD1Migrations$1, createExecutionContext, createMessageBatch, createPagesEventContext, createScheduledController, evictAllDurableObjects as evictAllDurableObjects$1, evictDurableObject as evictDurableObject$1, getQueueResult, introspectWorkflow as introspectWorkflow$1, introspectWorkflowInstance as introspectWorkflowInstance$1, listDurableObjectIds as listDurableObjectIds$1, reset as reset$1, runDurableObjectAlarm as runDurableObjectAlarm$1, runInDurableObject as runInDurableObject$1, waitOnExecutionContext } from "cloudflare:test";
5
+ //#region src/Vitest.ts
6
+ /**
7
+ * Effect-native helpers for tests running in Cloudflare's Vitest Workers pool.
8
+ *
9
+ * Import this module through the `effect-cf/vitest` subpath. It is intended to
10
+ * run inside the Workers test runtime, where `cloudflare:test` is available.
11
+ */
12
+ const currentEnv = env;
13
+ /**
14
+ * Provides the Workers pool environment as both {@link WorkerEnvironment} and
15
+ * the active Effect `ConfigProvider`.
16
+ */
17
+ const layer = Layer.mergeAll(Layer.succeed(WorkerEnvironment, currentEnv), ConfigProvider.layer(WorkerConfig.providerFromEnv(currentEnv)));
18
+ /**
19
+ * Creates an `ExecutionContext` and drains all `waitUntil` work before the
20
+ * returned effect completes, including when `use` fails or is interrupted.
21
+ */
22
+ const withExecutionContext = Effect.fn("effect-cf/vitest/withExecutionContext")(function* (use) {
23
+ return yield* Effect.acquireUseRelease(Effect.sync(createExecutionContext), use, (context) => Effect.promise(() => waitOnExecutionContext(context)));
24
+ });
25
+ /**
26
+ * Invokes an Effect-backed Worker fetch handler and drains its `waitUntil`
27
+ * work before returning the response.
28
+ */
29
+ const fetch = Effect.fn("effect-cf/vitest/fetch")(function* (Worker, request, workerEnv = currentEnv) {
30
+ return yield* withExecutionContext(Effect.fnUntraced(function* (context) {
31
+ const worker = new Worker(context, workerEnv);
32
+ return yield* Effect.promise(() => worker.fetch(request));
33
+ }));
34
+ });
35
+ /**
36
+ * Invokes a modules-format scheduled handler and drains its `waitUntil` work.
37
+ */
38
+ const scheduled = Effect.fn("effect-cf/vitest/scheduled")(function* (handler, options, workerEnv = currentEnv) {
39
+ const controller = createScheduledController(options);
40
+ yield* withExecutionContext((context) => Effect.promise(async () => handler(controller, workerEnv, context)));
41
+ return controller;
42
+ });
43
+ /**
44
+ * Invokes a Pages Function with a typed event context and drains its
45
+ * `waitUntil` work, including when the function fails.
46
+ */
47
+ const pages = Effect.fn("effect-cf/vitest/pages")(function* (handler, init) {
48
+ return yield* Effect.acquireUseRelease(Effect.sync(() => createPagesEventContext(init)), (context) => Effect.promise(async () => handler(context)), (context) => Effect.promise(() => waitOnExecutionContext(context)));
49
+ });
50
+ /**
51
+ * Invokes an Effect-backed queue consumer, drains its `waitUntil` work, and
52
+ * returns Pool Workers' acknowledgement and retry result.
53
+ */
54
+ const queue = Effect.fn("effect-cf/vitest/queue")(function* (Worker, queueName, messages, workerEnv = currentEnv) {
55
+ return yield* withExecutionContext(Effect.fnUntraced(function* (context) {
56
+ const batch = createMessageBatch(queueName, [...messages]);
57
+ const worker = new Worker(context, workerEnv);
58
+ yield* Effect.promise(() => worker.queue(batch));
59
+ return {
60
+ batch,
61
+ result: yield* Effect.promise(() => getQueueResult(batch, context))
62
+ };
63
+ }));
64
+ });
65
+ /**
66
+ * Runs an Effect inside the I/O context of a Durable Object instance.
67
+ *
68
+ * The caller's Effect context is preserved, and the instance's native state is
69
+ * provided as {@link DurableObjectState}. A wrapped state service is also
70
+ * passed to `use` for direct access.
71
+ */
72
+ const runInDurableObject = Effect.fn("effect-cf/vitest/runInDurableObject")(function* (stub, use) {
73
+ const context = yield* Effect.context();
74
+ return yield* yield* Effect.promise(() => runInDurableObject$1(stub, (instance, nativeState) => {
75
+ const state = fromDurableObjectState(nativeState);
76
+ const effect = Effect.provideService(use(instance, state), DurableObjectState, state);
77
+ return Effect.runPromiseExitWith(context)(effect);
78
+ }));
79
+ });
80
+ /** Immediately runs and removes a scheduled Durable Object alarm. */
81
+ const runDurableObjectAlarm = Effect.fn("effect-cf/vitest/runDurableObjectAlarm")(function* (stub) {
82
+ return yield* Effect.promise(() => runDurableObjectAlarm$1(stub));
83
+ });
84
+ /** Gracefully evicts one running Durable Object while preserving durable storage. */
85
+ const evictDurableObject = Effect.fn("effect-cf/vitest/evictDurableObject")(function* (stub, options) {
86
+ yield* Effect.promise(() => evictDurableObject$1(stub, options));
87
+ });
88
+ /** Lists all IDs created in a Durable Object namespace. */
89
+ const listDurableObjectIds = Effect.fn("effect-cf/vitest/listDurableObjectIds")(function* (namespace) {
90
+ return yield* Effect.promise(() => listDurableObjectIds$1(namespace));
91
+ });
92
+ /** Deletes data from all bindings attached to the current test Worker. */
93
+ const reset = Effect.fn("effect-cf/vitest/reset")(function* () {
94
+ yield* Effect.promise(reset$1);
95
+ });
96
+ /** Aborts all Durable Object instances without deleting their persisted data. */
97
+ const abortAllDurableObjects = Effect.fn("effect-cf/vitest/abortAllDurableObjects")(function* () {
98
+ yield* Effect.promise(abortAllDurableObjects$1);
99
+ });
100
+ /** Gracefully evicts all running Durable Objects while preserving durable storage. */
101
+ const evictAllDurableObjects = Effect.fn("effect-cf/vitest/evictAllDurableObjects")(function* (options) {
102
+ yield* Effect.promise(() => evictAllDurableObjects$1(options));
103
+ });
104
+ /** Applies all D1 migrations that have not already been recorded. */
105
+ const applyD1Migrations = Effect.fn("effect-cf/vitest/applyD1Migrations")(function* (database, migrations, migrationsTableName) {
106
+ yield* Effect.promise(() => applyD1Migrations$1(database, migrations.map(({ name, queries }) => ({
107
+ name,
108
+ queries: [...queries]
109
+ })), migrationsTableName));
110
+ });
111
+ /** Acquires an Effect-native admin client for a Secrets Store binding. */
112
+ const adminSecretsStore = Effect.fn("effect-cf/vitest/adminSecretsStore")(function* (binding) {
113
+ const admin = yield* Effect.sync(() => adminSecretsStore$1(binding));
114
+ return {
115
+ raw: admin,
116
+ create: (value) => Effect.promise(() => admin.create(value)),
117
+ update: (value, id) => Effect.promise(() => admin.update(value, id)),
118
+ duplicate: (id, newName) => Effect.promise(() => admin.duplicate(id, newName)),
119
+ delete: (id) => Effect.promise(() => admin.delete(id)),
120
+ list: Effect.promise(() => admin.list()),
121
+ get: (id) => Effect.promise(() => admin.get(id))
122
+ };
123
+ });
124
+ const wrapWorkflowModifier = (modifier) => ({
125
+ disableSleeps: (steps) => Effect.promise(() => modifier.disableSleeps(steps === void 0 ? void 0 : [...steps])),
126
+ disableRetryDelays: (steps) => Effect.promise(() => modifier.disableRetryDelays(steps === void 0 ? void 0 : [...steps])),
127
+ mockStepResult: (step, result) => Effect.promise(() => modifier.mockStepResult(step, result)),
128
+ mockStepError: (step, error, times) => Effect.promise(() => modifier.mockStepError(step, error, times)),
129
+ forceStepTimeout: (step, times) => Effect.promise(async () => {
130
+ await modifier.forceStepTimeout(step, times);
131
+ }),
132
+ mockEvent: (event) => Effect.promise(() => modifier.mockEvent(event)),
133
+ forceEventTimeout: (step) => Effect.promise(() => modifier.forceEventTimeout(step))
134
+ });
135
+ const runWorkflowModifier = Effect.fnUntraced(function* (register, use) {
136
+ const context = yield* Effect.context();
137
+ return yield* yield* Effect.promise(async () => {
138
+ let callbackExit;
139
+ try {
140
+ await register(async (modifier) => {
141
+ callbackExit = await Effect.runPromiseExitWith(context)(use(wrapWorkflowModifier(modifier)));
142
+ if (Exit.isFailure(callbackExit)) throw callbackExit;
143
+ });
144
+ } catch (cause) {
145
+ if (callbackExit !== void 0 && Exit.isFailure(callbackExit)) return callbackExit;
146
+ throw cause;
147
+ }
148
+ if (callbackExit === void 0) throw new Error("Pool Workers did not invoke the Workflow modifier callback");
149
+ return callbackExit;
150
+ });
151
+ });
152
+ const wrapWorkflowInstanceIntrospector = (introspector) => ({
153
+ raw: introspector,
154
+ modify: (use) => runWorkflowModifier((callback) => introspector.modify(callback), use),
155
+ waitForStepResult: (step) => Effect.promise(() => introspector.waitForStepResult(step)),
156
+ waitForStatus: (status) => Effect.promise(() => introspector.waitForStatus(status)),
157
+ getOutput: Effect.promise(() => introspector.getOutput()),
158
+ getError: Effect.promise(() => introspector.getError())
159
+ });
160
+ const wrapWorkflowIntrospector = (introspector) => ({
161
+ raw: introspector,
162
+ modifyAll: (use) => runWorkflowModifier((callback) => introspector.modifyAll(callback), use),
163
+ get: Effect.promise(async () => {
164
+ return (await introspector.get()).map(wrapWorkflowInstanceIntrospector);
165
+ })
166
+ });
167
+ /**
168
+ * Acquires an Effect-native Workflow introspector that is disposed when the
169
+ * surrounding Effect scope closes.
170
+ */
171
+ const introspectWorkflow = Effect.fn("effect-cf/vitest/introspectWorkflow")(function* (workflow) {
172
+ const introspector = yield* Effect.acquireDisposable(Effect.promise(() => introspectWorkflow$1(workflow)));
173
+ return wrapWorkflowIntrospector(introspector);
174
+ });
175
+ /**
176
+ * Acquires an Effect-native Workflow instance introspector that is disposed
177
+ * when the surrounding Effect scope closes.
178
+ */
179
+ const introspectWorkflowInstance = Effect.fn("effect-cf/vitest/introspectWorkflowInstance")(function* (workflow, instanceId) {
180
+ const introspector = yield* Effect.acquireDisposable(Effect.promise(() => introspectWorkflowInstance$1(workflow, instanceId)));
181
+ return wrapWorkflowInstanceIntrospector(introspector);
182
+ });
183
+ //#endregion
184
+ export { abortAllDurableObjects, adminSecretsStore, applyD1Migrations, evictAllDurableObjects, evictDurableObject, fetch, introspectWorkflow, introspectWorkflowInstance, layer, listDurableObjectIds, pages, queue, reset, runDurableObjectAlarm, runInDurableObject, scheduled, withExecutionContext };