effect-cf 0.39.1 → 0.40.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.
- package/CHANGELOG.md +18 -0
- package/README.md +9 -1
- package/dist/AiGateway.d.mts +86 -0
- package/dist/AiGateway.mjs +178 -0
- package/dist/AnalyticsEngine.d.mts +170 -0
- package/dist/AnalyticsEngine.mjs +275 -0
- package/dist/{Artifacts-B3r3RuTB.d.mts → Artifacts.d.mts} +8 -5
- package/dist/{Artifacts-CBxWGeZB.mjs → Artifacts.mjs} +9 -5
- package/dist/Binding.d.mts +45 -0
- package/dist/{Binding-C82jRhdr.mjs → Binding.mjs} +8 -4
- package/dist/BrowserRendering.d.mts +85 -0
- package/dist/BrowserRendering.mjs +81 -0
- package/dist/Cache.d.mts +40 -0
- package/dist/Cache.mjs +51 -0
- package/dist/CloudflareOtlp.d.mts +95 -0
- package/dist/CloudflareOtlp.mjs +125 -0
- package/dist/CloudflareTracer.d.mts +20 -0
- package/dist/CloudflareTracer.mjs +69 -0
- package/dist/ComputerArtifacts.d.mts +2 -2
- package/dist/ComputerArtifacts.mjs +3 -2
- package/dist/ComputerWorkspace.d.mts +36 -31
- package/dist/ComputerWorkspace.mjs +8 -3
- package/dist/ComputerWorkspaceHost.d.mts +1 -1
- package/dist/ComputerWorkspaceHost.mjs +1 -1
- package/dist/{ContainerNamespace-DbLaeFhN.d.mts → ContainerNamespace.d.mts} +16 -12
- package/dist/ContainerNamespace.mjs +165 -0
- package/dist/D1.d.mts +33 -0
- package/dist/D1.mjs +32 -0
- package/dist/DurableObject.d.mts +103 -0
- package/dist/DurableObject.mjs +77 -0
- package/dist/DurableObjectAlarm.d.mts +187 -0
- package/dist/DurableObjectAlarm.mjs +344 -0
- package/dist/DurableObjectDefinition.d.mts +87 -0
- package/dist/DurableObjectDefinition.mjs +125 -0
- package/dist/DurableObjectNamespace.d.mts +154 -0
- package/dist/DurableObjectNamespace.mjs +144 -0
- package/dist/DurableObjectRpcWebSocket.d.mts +112 -0
- package/dist/DurableObjectRpcWebSocket.mjs +547 -0
- package/dist/DurableObjectSqlite.d.mts +11 -0
- package/dist/DurableObjectSqlite.mjs +16 -0
- package/dist/DurableObjectState.d.mts +65 -0
- package/dist/DurableObjectState.mjs +47 -0
- package/dist/DurableObjectStorage.d.mts +112 -0
- package/dist/{DurableObjectState-DLL0YgBd.mjs → DurableObjectStorage.mjs} +19 -157
- package/dist/DurableObjectWebSocket.d.mts +83 -0
- package/dist/DurableObjectWebSocket.mjs +120 -0
- package/dist/Email.d.mts +106 -0
- package/dist/Email.mjs +247 -0
- package/dist/Environment.d.mts +59 -0
- package/dist/{ErrorMessage-Byx2OgNd.mjs → Environment.mjs} +7 -18
- package/dist/{Hyperdrive-HrwFlZ6x.d.mts → Hyperdrive.d.mts} +8 -5
- package/dist/Hyperdrive.mjs +32 -0
- package/dist/HyperdrivePg.d.mts +4 -3
- package/dist/HyperdrivePg.mjs +3 -1
- package/dist/Images.d.mts +191 -0
- package/dist/Images.mjs +112 -0
- package/dist/Kv.d.mts +84 -0
- package/dist/Kv.mjs +132 -0
- package/dist/Mcp.d.mts +5 -3
- package/dist/Mcp.mjs +13 -4
- package/dist/Queue.d.mts +68 -0
- package/dist/Queue.mjs +54 -0
- package/dist/QueueBinding.d.mts +42 -0
- package/dist/QueueBinding.mjs +69 -0
- package/dist/QueueDefinition.d.mts +48 -0
- package/dist/QueueDefinition.mjs +63 -0
- package/dist/R2.d.mts +89 -0
- package/dist/R2.mjs +117 -0
- package/dist/Rpc.d.mts +29 -0
- package/dist/Rpc.mjs +28 -0
- package/dist/RpcDefinition.d.mts +116 -0
- package/dist/RpcDefinition.mjs +184 -0
- package/dist/RpcTracing.d.mts +52 -0
- package/dist/RpcTracing.mjs +77 -0
- package/dist/Sandbox.d.mts +10 -5
- package/dist/Sandbox.mjs +9 -3
- package/dist/ServiceBinding.d.mts +112 -0
- package/dist/ServiceBinding.mjs +145 -0
- package/dist/Vectorize.d.mts +68 -0
- package/dist/Vectorize.mjs +76 -0
- package/dist/Vitest.d.mts +6 -4
- package/dist/Vitest.mjs +8 -4
- package/dist/WebTransport.d.mts +66 -0
- package/dist/WebTransport.mjs +63 -0
- package/dist/Worker.d.mts +139 -0
- package/dist/Worker.mjs +141 -0
- package/dist/WorkerDefinition.d.mts +100 -0
- package/dist/WorkerDefinition.mjs +103 -0
- package/dist/WorkersAi.d.mts +84 -0
- package/dist/WorkersAi.mjs +84 -0
- package/dist/Workflow.d.mts +91 -0
- package/dist/Workflow.mjs +139 -0
- package/dist/{WorkflowBinding-DtVykUCz.d.mts → WorkflowBinding.d.mts} +14 -10
- package/dist/WorkflowBinding.mjs +100 -0
- package/dist/WorkflowDefinition.d.mts +51 -0
- package/dist/WorkflowDefinition.mjs +69 -0
- package/dist/{rolldown-runtime-D7D4PA-g.mjs → _virtual/_rolldown/runtime.mjs} +1 -1
- package/dist/index.d.mts +40 -1800
- package/dist/index.mjs +40 -3123
- package/dist/{ComputerWorkspaceHostRegistry-CY5Fv4Ng.mjs → internal/ComputerWorkspaceHostRegistry.mjs} +1 -1
- package/dist/internal/Entrypoint.mjs +37 -0
- package/dist/internal/ErrorMessage.mjs +17 -0
- package/dist/internal/NativeCallback.mjs +33 -0
- package/dist/internal/RpcInvocation.d.mts +13 -0
- package/dist/internal/RpcInvocation.mjs +34 -0
- package/dist/internal/Runtime.mjs +31 -0
- package/dist/internal/Telemetry.mjs +20 -0
- package/dist/{WorkerContext-Yobnfw-K.mjs → internal/WorkerContext.mjs} +4 -31
- package/package.json +1 -1
- package/src/AiGateway.ts +5 -1
- package/src/AnalyticsEngine.ts +14 -19
- package/src/Artifacts.ts +6 -1
- package/src/Binding.ts +5 -1
- package/src/BrowserRendering.ts +5 -1
- package/src/Cache.ts +5 -1
- package/src/CloudflareOtlp.ts +12 -10
- package/src/CloudflareTracer.ts +7 -1
- package/src/ComputerArtifacts.ts +2 -1
- package/src/ComputerWorkspace.ts +7 -1
- package/src/ContainerNamespace.ts +6 -1
- package/src/D1.ts +4 -1
- package/src/DurableObject.ts +122 -29
- package/src/DurableObjectAlarm.ts +185 -52
- package/src/DurableObjectDefinition.ts +22 -22
- package/src/DurableObjectNamespace.ts +4 -1
- package/src/DurableObjectRpcWebSocket.ts +7 -1
- package/src/DurableObjectSqlite.ts +2 -1
- package/src/DurableObjectState.ts +4 -1
- package/src/DurableObjectStorage.ts +11 -13
- package/src/DurableObjectWebSocket.ts +5 -1
- package/src/Email.ts +5 -1
- package/src/Environment.ts +6 -1
- package/src/Hyperdrive.ts +4 -1
- package/src/HyperdrivePg.ts +3 -1
- package/src/Images.ts +7 -1
- package/src/Kv.ts +7 -1
- package/src/Mcp.ts +13 -13
- package/src/Queue.ts +3 -1
- package/src/QueueBinding.ts +5 -1
- package/src/QueueDefinition.ts +4 -1
- package/src/R2.ts +7 -1
- package/src/Rpc.ts +4 -1
- package/src/RpcDefinition.ts +4 -1
- package/src/RpcTracing.ts +9 -1
- package/src/Sandbox.ts +7 -1
- package/src/ServiceBinding.ts +6 -1
- package/src/Vectorize.ts +7 -1
- package/src/Vitest.ts +5 -1
- package/src/WebTransport.ts +6 -1
- package/src/Worker.ts +10 -16
- package/src/WorkerDefinition.ts +3 -1
- package/src/WorkersAi.ts +7 -1
- package/src/Workflow.ts +8 -11
- package/src/WorkflowBinding.ts +6 -1
- package/src/WorkflowDefinition.ts +4 -1
- package/src/internal/Entrypoint.ts +3 -1
- package/src/internal/ErrorMessage.ts +1 -1
- package/src/internal/NativeCallback.ts +3 -1
- package/src/internal/RpcInvocation.ts +5 -1
- package/src/internal/Runtime.ts +5 -1
- package/src/internal/Telemetry.ts +4 -3
- package/src/internal/WorkerContext.ts +3 -1
- package/dist/Binding-DKcLrQCM.d.mts +0 -94
- package/dist/DurableObjectState-DJ0K-zyd.d.mts +0 -244
- package/dist/Worker-XsGJsCJX.mjs +0 -885
- package/dist/Worker-gV4mDqIs.d.mts +0 -641
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { __exportAll } from "./_virtual/_rolldown/runtime.mjs";
|
|
2
|
+
import { runNativeCallback } from "./internal/NativeCallback.mjs";
|
|
3
|
+
import { fromDurableObjectStorage } from "./DurableObjectStorage.mjs";
|
|
4
|
+
import { fromWebSocket } from "./DurableObjectWebSocket.mjs";
|
|
5
|
+
import { makeWaitUntilScheduler } from "./internal/WorkerContext.mjs";
|
|
6
|
+
import * as Context from "effect/Context";
|
|
7
|
+
import * as Effect from "effect/Effect";
|
|
8
|
+
import * as Cause from "effect/Cause";
|
|
9
|
+
import * as Exit from "effect/Exit";
|
|
10
|
+
//#region src/DurableObjectState.ts
|
|
11
|
+
var DurableObjectState_exports = /* @__PURE__ */ __exportAll({
|
|
12
|
+
DurableObjectState: () => DurableObjectState,
|
|
13
|
+
fromDurableObjectState: () => fromDurableObjectState
|
|
14
|
+
});
|
|
15
|
+
var DurableObjectState = class extends Context.Service()("effect-cf/DurableObjectState") {};
|
|
16
|
+
const fromDurableObjectState = (state) => {
|
|
17
|
+
const scheduleWaitUntil = makeWaitUntilScheduler("DurableObjectState.waitUntil", (promise) => state.waitUntil(promise));
|
|
18
|
+
const waitUntil = ((input, options) => Effect.isEffect(input) ? scheduleWaitUntil(input, options, options?.mode ?? "observe") : Effect.sync(() => state.waitUntil(input)));
|
|
19
|
+
return {
|
|
20
|
+
raw: state,
|
|
21
|
+
id: state.id,
|
|
22
|
+
storage: fromDurableObjectStorage(state.storage),
|
|
23
|
+
waitUntil,
|
|
24
|
+
blockConcurrencyWhile: (effect) => runNativeCallback((run) => state.blockConcurrencyWhile(() => runExitPreservingTypedFailures(run, effect))).pipe(Effect.orDie, Effect.flatten),
|
|
25
|
+
blockConcurrencyWhileOrReset: (effect) => runNativeCallback((run) => state.blockConcurrencyWhile(async () => {
|
|
26
|
+
const exit = await run(effect);
|
|
27
|
+
if (Exit.isFailure(exit)) throw Cause.squash(exit.cause);
|
|
28
|
+
return exit.value;
|
|
29
|
+
})).pipe(Effect.orDie),
|
|
30
|
+
acceptWebSocket: (ws, tags) => Effect.sync(() => state.acceptWebSocket(ws.raw, tags)),
|
|
31
|
+
getWebSockets: (tag) => Effect.sync(() => state.getWebSockets(tag).map((socket) => fromWebSocket(socket))),
|
|
32
|
+
setWebSocketAutoResponse: (pair) => Effect.sync(() => state.setWebSocketAutoResponse(pair)),
|
|
33
|
+
getWebSocketAutoResponse: Effect.sync(() => state.getWebSocketAutoResponse()),
|
|
34
|
+
getWebSocketAutoResponseTimestamp: (ws) => Effect.sync(() => state.getWebSocketAutoResponseTimestamp(ws.raw)),
|
|
35
|
+
setHibernatableWebSocketEventTimeout: (timeoutMs) => Effect.sync(() => state.setHibernatableWebSocketEventTimeout(timeoutMs)),
|
|
36
|
+
getHibernatableWebSocketEventTimeout: Effect.sync(() => state.getHibernatableWebSocketEventTimeout()),
|
|
37
|
+
getTags: (ws) => Effect.sync(() => state.getTags(ws.raw)),
|
|
38
|
+
abort: (reason, options) => Effect.sync(() => state.abort(reason, options))
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
const runExitPreservingTypedFailures = async (run, effect) => {
|
|
42
|
+
const exit = await run(effect);
|
|
43
|
+
if (Exit.isFailure(exit) && (Cause.hasDies(exit.cause) || Cause.hasInterrupts(exit.cause))) throw Cause.squash(exit.cause);
|
|
44
|
+
return exit;
|
|
45
|
+
};
|
|
46
|
+
//#endregion
|
|
47
|
+
export { DurableObjectState, DurableObjectState_exports, fromDurableObjectState };
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import * as Effect from "effect/Effect";
|
|
2
|
+
import * as S from "effect/Schema";
|
|
3
|
+
import * as Option from "effect/Option";
|
|
4
|
+
import * as Cause from "effect/Cause";
|
|
5
|
+
import * as Duration from "effect/Duration";
|
|
6
|
+
declare namespace DurableObjectStorage_d_exports {
|
|
7
|
+
export { DurableObjectStorage, DurableObjectTransaction, SchemaBackedSyncKvStorage, SqlCursor, SqlStorage, SqlStorageValue, StorageOperationError, SyncKvDefinition, SyncKvStorage, fromDurableObjectStorage };
|
|
8
|
+
}
|
|
9
|
+
type SqlStorageValue = globalThis.SqlStorageValue;
|
|
10
|
+
declare const StorageOperationError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => Cause.YieldableError & {
|
|
11
|
+
readonly _tag: "StorageOperationError";
|
|
12
|
+
} & Readonly<A>;
|
|
13
|
+
declare class StorageOperationError extends StorageOperationError_base<{
|
|
14
|
+
readonly operation: string;
|
|
15
|
+
readonly cause: unknown;
|
|
16
|
+
}> {
|
|
17
|
+
get message(): string;
|
|
18
|
+
}
|
|
19
|
+
type StorageEffect<A> = Effect.Effect<A, StorageOperationError>;
|
|
20
|
+
interface SqlCursor<T extends Record<string, SqlStorageValue>> {
|
|
21
|
+
next(): StorageEffect<{
|
|
22
|
+
done?: false;
|
|
23
|
+
value: T;
|
|
24
|
+
} | {
|
|
25
|
+
done: true;
|
|
26
|
+
value?: never;
|
|
27
|
+
}>;
|
|
28
|
+
toArray(): StorageEffect<Array<T>>;
|
|
29
|
+
one(): StorageEffect<T>;
|
|
30
|
+
raw<U extends Array<SqlStorageValue>>(): StorageEffect<IterableIterator<U>>;
|
|
31
|
+
readonly columnNames: Array<string>;
|
|
32
|
+
readonly rowsRead: StorageEffect<number>;
|
|
33
|
+
readonly rowsWritten: StorageEffect<number>;
|
|
34
|
+
}
|
|
35
|
+
interface SqlStorage {
|
|
36
|
+
exec<T extends Record<string, SqlStorageValue>>(query: string, ...bindings: Array<SqlStorageValue>): StorageEffect<SqlCursor<T>>;
|
|
37
|
+
readonly databaseSize: number;
|
|
38
|
+
}
|
|
39
|
+
interface SyncKvDefinition<Key, Value, EncodedValue> {
|
|
40
|
+
readonly key: S.Codec<Key, string>;
|
|
41
|
+
readonly value: S.Codec<Value, EncodedValue>;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Sync KV wrapper that transparently encodes keys/values through Effect Schema.
|
|
45
|
+
*/
|
|
46
|
+
interface SchemaBackedSyncKvStorage<Key, Value> {
|
|
47
|
+
get(key: Key): Effect.Effect<Option.Option<Value>, unknown>;
|
|
48
|
+
put(key: Key, value: Value): Effect.Effect<void, unknown>;
|
|
49
|
+
delete(key: Key): Effect.Effect<boolean, unknown>;
|
|
50
|
+
list(options?: globalThis.SyncKvListOptions): Effect.Effect<Array<[Key, Value]>, unknown>;
|
|
51
|
+
}
|
|
52
|
+
interface SyncKvStorage {
|
|
53
|
+
get<T = unknown>(key: string): StorageEffect<T | undefined>;
|
|
54
|
+
put<T>(key: string, value: T): StorageEffect<void>;
|
|
55
|
+
delete(key: string): StorageEffect<boolean>;
|
|
56
|
+
list<T = unknown>(options?: globalThis.SyncKvListOptions): StorageEffect<Array<[string, T]>>;
|
|
57
|
+
schema<Key, Value, EncodedValue>(definition: SyncKvDefinition<Key, Value, EncodedValue>): SchemaBackedSyncKvStorage<Key, Value>;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Effect wrapper around Cloudflare transaction callbacks.
|
|
61
|
+
*/
|
|
62
|
+
interface DurableObjectTransaction {
|
|
63
|
+
get<T = unknown>(key: string, options?: globalThis.DurableObjectGetOptions): StorageEffect<T | undefined>;
|
|
64
|
+
get<T = unknown>(keys: Array<string>, options?: globalThis.DurableObjectGetOptions): StorageEffect<Map<string, T>>;
|
|
65
|
+
list<T = unknown>(options?: globalThis.DurableObjectListOptions): StorageEffect<Map<string, T>>;
|
|
66
|
+
put<T>(key: string, value: T, options?: globalThis.DurableObjectPutOptions): StorageEffect<void>;
|
|
67
|
+
put<T>(entries: Record<string, T>, options?: globalThis.DurableObjectPutOptions): StorageEffect<void>;
|
|
68
|
+
delete(key: string, options?: globalThis.DurableObjectPutOptions): StorageEffect<boolean>;
|
|
69
|
+
delete(keys: Array<string>, options?: globalThis.DurableObjectPutOptions): StorageEffect<number>;
|
|
70
|
+
rollback(): StorageEffect<void>;
|
|
71
|
+
getAlarm(options?: globalThis.DurableObjectGetAlarmOptions): StorageEffect<number | null>;
|
|
72
|
+
setAlarm(scheduledTime: number | Date, options?: globalThis.DurableObjectSetAlarmOptions): StorageEffect<void>;
|
|
73
|
+
setAlarmAfter(delay: Duration.Input, options?: globalThis.DurableObjectSetAlarmOptions): StorageEffect<void>;
|
|
74
|
+
deleteAlarm(options?: globalThis.DurableObjectSetAlarmOptions): StorageEffect<void>;
|
|
75
|
+
}
|
|
76
|
+
interface DurableObjectStorage {
|
|
77
|
+
get<T = unknown>(key: string, options?: globalThis.DurableObjectGetOptions): StorageEffect<T | undefined>;
|
|
78
|
+
put<T>(key: string, value: T, options?: globalThis.DurableObjectPutOptions): StorageEffect<void>;
|
|
79
|
+
delete(key: string, options?: globalThis.DurableObjectPutOptions): StorageEffect<boolean>;
|
|
80
|
+
/**
|
|
81
|
+
* Deletes all stored data. On compatibility dates before 2026-02-24, Cloudflare
|
|
82
|
+
* documents that active alarms must be deleted separately with `deleteAlarm()`.
|
|
83
|
+
*/
|
|
84
|
+
deleteAll(options?: globalThis.DurableObjectPutOptions): StorageEffect<void>;
|
|
85
|
+
getAlarm(options?: globalThis.DurableObjectGetAlarmOptions): StorageEffect<number | null>;
|
|
86
|
+
setAlarm(scheduledTime: number | Date, options?: globalThis.DurableObjectSetAlarmOptions): StorageEffect<void>;
|
|
87
|
+
setAlarmAfter(delay: Duration.Input, options?: globalThis.DurableObjectSetAlarmOptions): StorageEffect<void>;
|
|
88
|
+
deleteAlarm(options?: globalThis.DurableObjectSetAlarmOptions): StorageEffect<void>;
|
|
89
|
+
/**
|
|
90
|
+
* SQLite-backed Durable Object storage only.
|
|
91
|
+
*
|
|
92
|
+
* The callback Effect must complete synchronously. If it requires asynchronous
|
|
93
|
+
* work, use `transaction` instead so Cloudflare can run the async transaction
|
|
94
|
+
* callback under the platform's transaction contract.
|
|
95
|
+
*/
|
|
96
|
+
transactionSync<A, E, R>(closure: () => Effect.Effect<A, E, R>): Effect.Effect<A, E | StorageOperationError, R>;
|
|
97
|
+
/**
|
|
98
|
+
* Runs an async transaction and exposes a typed transaction wrapper.
|
|
99
|
+
*/
|
|
100
|
+
transaction<A, E, R>(closure: (txn: DurableObjectTransaction) => Effect.Effect<A, E, R>): Effect.Effect<A, E | StorageOperationError, R>;
|
|
101
|
+
/** Flushes pending writes to disk. */
|
|
102
|
+
sync(): StorageEffect<void>;
|
|
103
|
+
/** Requires SQLite-backed Durable Object storage. */
|
|
104
|
+
getCurrentBookmark(): StorageEffect<string>;
|
|
105
|
+
/** Requires SQLite-backed Durable Object storage. */
|
|
106
|
+
onNextSessionRestoreBookmark(bookmark: string): StorageEffect<string>;
|
|
107
|
+
readonly sql: SqlStorage;
|
|
108
|
+
readonly kv: SyncKvStorage;
|
|
109
|
+
}
|
|
110
|
+
declare const fromDurableObjectStorage: (storage: globalThis.DurableObjectStorage) => DurableObjectStorage;
|
|
111
|
+
//#endregion
|
|
112
|
+
export { DurableObjectStorage, DurableObjectStorage_d_exports, DurableObjectTransaction, SchemaBackedSyncKvStorage, SqlCursor, SqlStorage, SqlStorageValue, StorageOperationError, SyncKvDefinition, SyncKvStorage, fromDurableObjectStorage };
|
|
@@ -1,7 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
1
|
+
import { __exportAll } from "./_virtual/_rolldown/runtime.mjs";
|
|
2
|
+
import { causeMessage } from "./internal/ErrorMessage.mjs";
|
|
3
|
+
import { runNativeCallback } from "./internal/NativeCallback.mjs";
|
|
4
|
+
import * as Data from "effect/Data";
|
|
5
|
+
import * as Effect from "effect/Effect";
|
|
6
|
+
import * as S from "effect/Schema";
|
|
7
|
+
import * as Predicate from "effect/Predicate";
|
|
8
|
+
import * as Option from "effect/Option";
|
|
9
|
+
import * as Result from "effect/Result";
|
|
10
|
+
import * as Cause from "effect/Cause";
|
|
11
|
+
import * as Exit from "effect/Exit";
|
|
12
|
+
import * as Clock from "effect/Clock";
|
|
13
|
+
import * as Duration from "effect/Duration";
|
|
14
|
+
import * as Fiber from "effect/Fiber";
|
|
5
15
|
//#region src/DurableObjectStorage.ts
|
|
6
16
|
var DurableObjectStorage_exports = /* @__PURE__ */ __exportAll({
|
|
7
17
|
StorageOperationError: () => StorageOperationError,
|
|
@@ -47,10 +57,10 @@ const fromSqlStorage = (sql) => ({
|
|
|
47
57
|
}
|
|
48
58
|
});
|
|
49
59
|
const schemaBackedSyncKvStorage = (kv, definition) => {
|
|
50
|
-
const encodeKey =
|
|
51
|
-
const decodeKey =
|
|
52
|
-
const encodeValue =
|
|
53
|
-
const decodeValue =
|
|
60
|
+
const encodeKey = S.encodeEffect(definition.key);
|
|
61
|
+
const decodeKey = S.decodeUnknownEffect(definition.key);
|
|
62
|
+
const encodeValue = S.encodeEffect(definition.value);
|
|
63
|
+
const decodeValue = S.decodeUnknownEffect(definition.value);
|
|
54
64
|
return {
|
|
55
65
|
get: (key) => Effect.gen(function* () {
|
|
56
66
|
const encodedKey = yield* encodeKey(key);
|
|
@@ -140,152 +150,4 @@ const fromDurableObjectStorage = (storage) => ({
|
|
|
140
150
|
kv: fromSyncKvStorage(storage.kv)
|
|
141
151
|
});
|
|
142
152
|
//#endregion
|
|
143
|
-
|
|
144
|
-
var DurableObjectWebSocket_exports = /* @__PURE__ */ __exportAll({
|
|
145
|
-
DurableWebSocketAttachmentError: () => DurableWebSocketAttachmentError,
|
|
146
|
-
DurableWebSocketCloseError: () => DurableWebSocketCloseError,
|
|
147
|
-
DurableWebSocketSendError: () => DurableWebSocketSendError,
|
|
148
|
-
acceptUpgrade: () => acceptUpgrade,
|
|
149
|
-
attachment: () => attachment,
|
|
150
|
-
fromWebSocket: () => fromWebSocket,
|
|
151
|
-
handlers: () => handlers
|
|
152
|
-
});
|
|
153
|
-
var DurableWebSocketSendError = class extends Data.TaggedError("DurableWebSocketSendError") {
|
|
154
|
-
get message() {
|
|
155
|
-
return `Durable Object websocket send failed: ${causeMessage(this.cause)}`;
|
|
156
|
-
}
|
|
157
|
-
};
|
|
158
|
-
var DurableWebSocketCloseError = class extends Data.TaggedError("DurableWebSocketCloseError") {
|
|
159
|
-
get message() {
|
|
160
|
-
return `Durable Object websocket close failed: ${causeMessage(this.cause)}`;
|
|
161
|
-
}
|
|
162
|
-
};
|
|
163
|
-
/** Error raised when serializing or deserializing a websocket attachment fails. */
|
|
164
|
-
var DurableWebSocketAttachmentError = class extends Data.TaggedError("DurableWebSocketAttachmentError") {
|
|
165
|
-
get message() {
|
|
166
|
-
return `Durable Object websocket attachment ${this.operation} failed: ${causeMessage(this.cause)}`;
|
|
167
|
-
}
|
|
168
|
-
};
|
|
169
|
-
const fromWebSocket = (raw) => ({
|
|
170
|
-
raw,
|
|
171
|
-
send: (data) => Effect.try({
|
|
172
|
-
try: () => raw.send(data),
|
|
173
|
-
catch: (cause) => new DurableWebSocketSendError({ cause })
|
|
174
|
-
}),
|
|
175
|
-
close: (code, reason) => Effect.try({
|
|
176
|
-
try: () => raw.close(code, reason),
|
|
177
|
-
catch: (cause) => new DurableWebSocketCloseError({ cause })
|
|
178
|
-
}),
|
|
179
|
-
serializeAttachment: (value) => Effect.try({
|
|
180
|
-
try: () => raw.serializeAttachment(value),
|
|
181
|
-
catch: (cause) => new DurableWebSocketAttachmentError({
|
|
182
|
-
operation: "serialize",
|
|
183
|
-
cause
|
|
184
|
-
})
|
|
185
|
-
}),
|
|
186
|
-
deserializeAttachment: Effect.try({
|
|
187
|
-
try: () => raw.deserializeAttachment(),
|
|
188
|
-
catch: (cause) => new DurableWebSocketAttachmentError({
|
|
189
|
-
operation: "deserialize",
|
|
190
|
-
cause
|
|
191
|
-
})
|
|
192
|
-
})
|
|
193
|
-
});
|
|
194
|
-
const acceptUpgrade = Effect.fn("DurableObjectWebSocket.acceptUpgrade")(function* (options = {}) {
|
|
195
|
-
const state = yield* DurableObjectState;
|
|
196
|
-
const pair = new WebSocketPair();
|
|
197
|
-
const client = pair[0];
|
|
198
|
-
const server = fromWebSocket(pair[1]);
|
|
199
|
-
if (options.attachment !== void 0) yield* server.serializeAttachment(options.attachment);
|
|
200
|
-
yield* state.acceptWebSocket(server, options.tags === void 0 ? void 0 : [...options.tags]);
|
|
201
|
-
return {
|
|
202
|
-
client,
|
|
203
|
-
server,
|
|
204
|
-
response: new Response(null, {
|
|
205
|
-
status: 101,
|
|
206
|
-
webSocket: client
|
|
207
|
-
})
|
|
208
|
-
};
|
|
209
|
-
});
|
|
210
|
-
const attachment = (schema) => {
|
|
211
|
-
const serialize = (socket, value) => Schema.encodeEffect(schema)(value).pipe(Effect.mapError((cause) => new DurableWebSocketAttachmentError({
|
|
212
|
-
operation: "serialize",
|
|
213
|
-
cause
|
|
214
|
-
})), Effect.flatMap((encoded) => socket.serializeAttachment(encoded)));
|
|
215
|
-
const deserialize = (socket) => Effect.gen(function* () {
|
|
216
|
-
const value = yield* socket.deserializeAttachment;
|
|
217
|
-
if (value === null || value === void 0) return Option.none();
|
|
218
|
-
return Option.some(yield* Schema.decodeUnknownEffect(schema)(value).pipe(Effect.mapError((cause) => new DurableWebSocketAttachmentError({
|
|
219
|
-
operation: "deserialize",
|
|
220
|
-
cause
|
|
221
|
-
}))));
|
|
222
|
-
});
|
|
223
|
-
const rehydrate = (options = {}) => Effect.gen(function* () {
|
|
224
|
-
const sockets = yield* (yield* DurableObjectState).getWebSockets(options.tag);
|
|
225
|
-
const restored = [];
|
|
226
|
-
const onInvalid = options.onInvalid ?? "ignore";
|
|
227
|
-
for (const socket of sockets) {
|
|
228
|
-
const decoded = yield* Effect.result(deserialize(socket));
|
|
229
|
-
if (Result.isSuccess(decoded) && Option.isSome(decoded.success)) {
|
|
230
|
-
restored.push({
|
|
231
|
-
socket,
|
|
232
|
-
attachment: decoded.success.value
|
|
233
|
-
});
|
|
234
|
-
continue;
|
|
235
|
-
}
|
|
236
|
-
if (Result.isFailure(decoded) && onInvalid === "fail") return yield* Effect.fail(decoded.failure);
|
|
237
|
-
if (Result.isFailure(decoded) && onInvalid === "ignore-and-close") yield* socket.close(1008, "invalid websocket attachment").pipe(Effect.ignore);
|
|
238
|
-
}
|
|
239
|
-
return restored;
|
|
240
|
-
});
|
|
241
|
-
return {
|
|
242
|
-
serialize,
|
|
243
|
-
deserialize,
|
|
244
|
-
rehydrate,
|
|
245
|
-
schema
|
|
246
|
-
};
|
|
247
|
-
};
|
|
248
|
-
const handlers = (options) => ({
|
|
249
|
-
webSocketMessage: options.message,
|
|
250
|
-
webSocketClose: options.close,
|
|
251
|
-
webSocketError: options.error
|
|
252
|
-
});
|
|
253
|
-
//#endregion
|
|
254
|
-
//#region src/DurableObjectState.ts
|
|
255
|
-
var DurableObjectState_exports = /* @__PURE__ */ __exportAll({
|
|
256
|
-
DurableObjectState: () => DurableObjectState,
|
|
257
|
-
fromDurableObjectState: () => fromDurableObjectState
|
|
258
|
-
});
|
|
259
|
-
var DurableObjectState = class extends Context.Service()("effect-cf/DurableObjectState") {};
|
|
260
|
-
const fromDurableObjectState = (state) => {
|
|
261
|
-
const scheduleWaitUntil = makeWaitUntilScheduler("DurableObjectState.waitUntil", (promise) => state.waitUntil(promise));
|
|
262
|
-
const waitUntil = ((input, options) => Effect.isEffect(input) ? scheduleWaitUntil(input, options, options?.mode ?? "observe") : Effect.sync(() => state.waitUntil(input)));
|
|
263
|
-
return {
|
|
264
|
-
raw: state,
|
|
265
|
-
id: state.id,
|
|
266
|
-
storage: fromDurableObjectStorage(state.storage),
|
|
267
|
-
waitUntil,
|
|
268
|
-
blockConcurrencyWhile: (effect) => runNativeCallback((run) => state.blockConcurrencyWhile(() => runExitPreservingTypedFailures(run, effect))).pipe(Effect.orDie, Effect.flatten),
|
|
269
|
-
blockConcurrencyWhileOrReset: (effect) => runNativeCallback((run) => state.blockConcurrencyWhile(async () => {
|
|
270
|
-
const exit = await run(effect);
|
|
271
|
-
if (Exit.isFailure(exit)) throw Cause.squash(exit.cause);
|
|
272
|
-
return exit.value;
|
|
273
|
-
})).pipe(Effect.orDie),
|
|
274
|
-
acceptWebSocket: (ws, tags) => Effect.sync(() => state.acceptWebSocket(ws.raw, tags)),
|
|
275
|
-
getWebSockets: (tag) => Effect.sync(() => state.getWebSockets(tag).map((socket) => fromWebSocket(socket))),
|
|
276
|
-
setWebSocketAutoResponse: (pair) => Effect.sync(() => state.setWebSocketAutoResponse(pair)),
|
|
277
|
-
getWebSocketAutoResponse: Effect.sync(() => state.getWebSocketAutoResponse()),
|
|
278
|
-
getWebSocketAutoResponseTimestamp: (ws) => Effect.sync(() => state.getWebSocketAutoResponseTimestamp(ws.raw)),
|
|
279
|
-
setHibernatableWebSocketEventTimeout: (timeoutMs) => Effect.sync(() => state.setHibernatableWebSocketEventTimeout(timeoutMs)),
|
|
280
|
-
getHibernatableWebSocketEventTimeout: Effect.sync(() => state.getHibernatableWebSocketEventTimeout()),
|
|
281
|
-
getTags: (ws) => Effect.sync(() => state.getTags(ws.raw)),
|
|
282
|
-
abort: (reason, options) => Effect.sync(() => state.abort(reason, options))
|
|
283
|
-
};
|
|
284
|
-
};
|
|
285
|
-
const runExitPreservingTypedFailures = async (run, effect) => {
|
|
286
|
-
const exit = await run(effect);
|
|
287
|
-
if (Exit.isFailure(exit) && (Cause.hasDies(exit.cause) || Cause.hasInterrupts(exit.cause))) throw Cause.squash(exit.cause);
|
|
288
|
-
return exit;
|
|
289
|
-
};
|
|
290
|
-
//#endregion
|
|
291
|
-
export { DurableWebSocketAttachmentError as a, StorageOperationError as c, DurableObjectWebSocket_exports as i, DurableObjectState_exports as n, fromWebSocket as o, fromDurableObjectState as r, DurableObjectStorage_exports as s, DurableObjectState as t };
|
|
153
|
+
export { DurableObjectStorage_exports, StorageOperationError, fromDurableObjectStorage };
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { DurableObjectState } from "./DurableObjectState.mjs";
|
|
2
|
+
import * as Effect from "effect/Effect";
|
|
3
|
+
import * as S from "effect/Schema";
|
|
4
|
+
import * as Option from "effect/Option";
|
|
5
|
+
declare namespace DurableObjectWebSocket_d_exports {
|
|
6
|
+
export { AcceptUpgradeOptions, AcceptedUpgrade, AttachmentInvalidPolicy, AttachmentRehydrateOptions, DurableWebSocket, DurableWebSocketAttachment, DurableWebSocketAttachmentError, DurableWebSocketCloseError, DurableWebSocketHandlers, DurableWebSocketSendData, DurableWebSocketSendError, RehydratedDurableWebSocket, acceptUpgrade, attachment, fromWebSocket, handlers };
|
|
7
|
+
}
|
|
8
|
+
type DurableWebSocketSendData = string | ArrayBuffer | ArrayBufferView;
|
|
9
|
+
declare const DurableWebSocketSendError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
|
|
10
|
+
readonly _tag: "DurableWebSocketSendError";
|
|
11
|
+
} & Readonly<A>;
|
|
12
|
+
declare class DurableWebSocketSendError extends DurableWebSocketSendError_base<{
|
|
13
|
+
readonly cause: unknown;
|
|
14
|
+
}> {
|
|
15
|
+
get message(): string;
|
|
16
|
+
}
|
|
17
|
+
declare const DurableWebSocketCloseError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
|
|
18
|
+
readonly _tag: "DurableWebSocketCloseError";
|
|
19
|
+
} & Readonly<A>;
|
|
20
|
+
declare class DurableWebSocketCloseError extends DurableWebSocketCloseError_base<{
|
|
21
|
+
readonly cause: unknown;
|
|
22
|
+
}> {
|
|
23
|
+
get message(): string;
|
|
24
|
+
}
|
|
25
|
+
declare const DurableWebSocketAttachmentError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
|
|
26
|
+
readonly _tag: "DurableWebSocketAttachmentError";
|
|
27
|
+
} & Readonly<A>;
|
|
28
|
+
/** Error raised when serializing or deserializing a websocket attachment fails. */
|
|
29
|
+
declare class DurableWebSocketAttachmentError extends DurableWebSocketAttachmentError_base<{
|
|
30
|
+
readonly operation: "serialize" | "deserialize";
|
|
31
|
+
readonly cause: unknown;
|
|
32
|
+
}> {
|
|
33
|
+
get message(): string;
|
|
34
|
+
}
|
|
35
|
+
interface DurableWebSocket<Attachment = unknown> {
|
|
36
|
+
readonly raw: WebSocket;
|
|
37
|
+
send(data: DurableWebSocketSendData): Effect.Effect<void, DurableWebSocketSendError>;
|
|
38
|
+
close(code?: number, reason?: string): Effect.Effect<void, DurableWebSocketCloseError>;
|
|
39
|
+
/** Serializes hibernation attachment metadata onto the socket. */
|
|
40
|
+
serializeAttachment<A extends Attachment = Attachment>(value: A): Effect.Effect<void, DurableWebSocketAttachmentError>;
|
|
41
|
+
/** Deserializes hibernation attachment metadata from the socket. */
|
|
42
|
+
readonly deserializeAttachment: Effect.Effect<unknown, DurableWebSocketAttachmentError>;
|
|
43
|
+
}
|
|
44
|
+
declare const fromWebSocket: <Attachment = unknown>(raw: WebSocket) => DurableWebSocket<Attachment>;
|
|
45
|
+
interface AcceptUpgradeOptions<Attachment = unknown> {
|
|
46
|
+
readonly tags?: ReadonlyArray<string> | undefined;
|
|
47
|
+
/** Optional attachment serialized onto the server socket. */
|
|
48
|
+
readonly attachment?: Attachment | undefined;
|
|
49
|
+
}
|
|
50
|
+
interface AcceptedUpgrade<Attachment = unknown> {
|
|
51
|
+
readonly client: WebSocket;
|
|
52
|
+
readonly server: DurableWebSocket<Attachment>;
|
|
53
|
+
readonly response: Response;
|
|
54
|
+
}
|
|
55
|
+
declare const acceptUpgrade: <Attachment = unknown>(options?: AcceptUpgradeOptions<Attachment> | undefined) => Effect.Effect<AcceptedUpgrade<Attachment>, DurableWebSocketAttachmentError, DurableObjectState>;
|
|
56
|
+
type AttachmentInvalidPolicy = "ignore" | "ignore-and-close" | "fail";
|
|
57
|
+
interface AttachmentRehydrateOptions {
|
|
58
|
+
readonly tag?: string | undefined;
|
|
59
|
+
readonly onInvalid?: AttachmentInvalidPolicy | undefined;
|
|
60
|
+
}
|
|
61
|
+
interface RehydratedDurableWebSocket<Attachment> {
|
|
62
|
+
readonly socket: DurableWebSocket<Attachment>;
|
|
63
|
+
readonly attachment: Attachment;
|
|
64
|
+
}
|
|
65
|
+
interface DurableWebSocketAttachment<Attachment, Encoded> {
|
|
66
|
+
serialize(socket: DurableWebSocket<unknown>, value: Attachment): Effect.Effect<void, DurableWebSocketAttachmentError>;
|
|
67
|
+
deserialize(socket: DurableWebSocket<unknown>): Effect.Effect<Option.Option<Attachment>, DurableWebSocketAttachmentError>;
|
|
68
|
+
rehydrate(options?: AttachmentRehydrateOptions): Effect.Effect<Array<RehydratedDurableWebSocket<Attachment>>, DurableWebSocketAttachmentError, DurableObjectState>;
|
|
69
|
+
readonly schema: S.Codec<Attachment, Encoded, never, never>;
|
|
70
|
+
}
|
|
71
|
+
declare const attachment: <const AttachmentSchema extends S.Codec<any, any, never, never>>(schema: AttachmentSchema) => DurableWebSocketAttachment<S.Schema.Type<AttachmentSchema>, S.Codec.Encoded<AttachmentSchema>>;
|
|
72
|
+
interface DurableWebSocketHandlers<R = never, E = unknown> {
|
|
73
|
+
readonly message?: (socket: DurableWebSocket, message: string | ArrayBuffer) => Effect.Effect<void, E, R>;
|
|
74
|
+
readonly close?: (socket: DurableWebSocket, code: number, reason: string, wasClean: boolean) => Effect.Effect<void, E, R>;
|
|
75
|
+
readonly error?: (socket: DurableWebSocket, cause: unknown) => Effect.Effect<void, E, R>;
|
|
76
|
+
}
|
|
77
|
+
declare const handlers: <R = never, E = unknown>(options: DurableWebSocketHandlers<R, E>) => {
|
|
78
|
+
webSocketMessage: ((socket: DurableWebSocket, message: string | ArrayBuffer) => Effect.Effect<void, E, R>) | undefined;
|
|
79
|
+
webSocketClose: ((socket: DurableWebSocket, code: number, reason: string, wasClean: boolean) => Effect.Effect<void, E, R>) | undefined;
|
|
80
|
+
webSocketError: ((socket: DurableWebSocket, cause: unknown) => Effect.Effect<void, E, R>) | undefined;
|
|
81
|
+
};
|
|
82
|
+
//#endregion
|
|
83
|
+
export { AcceptUpgradeOptions, AcceptedUpgrade, AttachmentInvalidPolicy, AttachmentRehydrateOptions, DurableObjectWebSocket_d_exports, DurableWebSocket, DurableWebSocketAttachment, DurableWebSocketAttachmentError, DurableWebSocketCloseError, DurableWebSocketHandlers, DurableWebSocketSendData, DurableWebSocketSendError, RehydratedDurableWebSocket, acceptUpgrade, attachment, fromWebSocket, handlers };
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { __exportAll } from "./_virtual/_rolldown/runtime.mjs";
|
|
2
|
+
import { causeMessage } from "./internal/ErrorMessage.mjs";
|
|
3
|
+
import { DurableObjectState } from "./DurableObjectState.mjs";
|
|
4
|
+
import * as Data from "effect/Data";
|
|
5
|
+
import * as Effect from "effect/Effect";
|
|
6
|
+
import * as S from "effect/Schema";
|
|
7
|
+
import * as Option from "effect/Option";
|
|
8
|
+
import * as Result from "effect/Result";
|
|
9
|
+
//#region src/DurableObjectWebSocket.ts
|
|
10
|
+
var DurableObjectWebSocket_exports = /* @__PURE__ */ __exportAll({
|
|
11
|
+
DurableWebSocketAttachmentError: () => DurableWebSocketAttachmentError,
|
|
12
|
+
DurableWebSocketCloseError: () => DurableWebSocketCloseError,
|
|
13
|
+
DurableWebSocketSendError: () => DurableWebSocketSendError,
|
|
14
|
+
acceptUpgrade: () => acceptUpgrade,
|
|
15
|
+
attachment: () => attachment,
|
|
16
|
+
fromWebSocket: () => fromWebSocket,
|
|
17
|
+
handlers: () => handlers
|
|
18
|
+
});
|
|
19
|
+
var DurableWebSocketSendError = class extends Data.TaggedError("DurableWebSocketSendError") {
|
|
20
|
+
get message() {
|
|
21
|
+
return `Durable Object websocket send failed: ${causeMessage(this.cause)}`;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
var DurableWebSocketCloseError = class extends Data.TaggedError("DurableWebSocketCloseError") {
|
|
25
|
+
get message() {
|
|
26
|
+
return `Durable Object websocket close failed: ${causeMessage(this.cause)}`;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
/** Error raised when serializing or deserializing a websocket attachment fails. */
|
|
30
|
+
var DurableWebSocketAttachmentError = class extends Data.TaggedError("DurableWebSocketAttachmentError") {
|
|
31
|
+
get message() {
|
|
32
|
+
return `Durable Object websocket attachment ${this.operation} failed: ${causeMessage(this.cause)}`;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const fromWebSocket = (raw) => ({
|
|
36
|
+
raw,
|
|
37
|
+
send: (data) => Effect.try({
|
|
38
|
+
try: () => raw.send(data),
|
|
39
|
+
catch: (cause) => new DurableWebSocketSendError({ cause })
|
|
40
|
+
}),
|
|
41
|
+
close: (code, reason) => Effect.try({
|
|
42
|
+
try: () => raw.close(code, reason),
|
|
43
|
+
catch: (cause) => new DurableWebSocketCloseError({ cause })
|
|
44
|
+
}),
|
|
45
|
+
serializeAttachment: (value) => Effect.try({
|
|
46
|
+
try: () => raw.serializeAttachment(value),
|
|
47
|
+
catch: (cause) => new DurableWebSocketAttachmentError({
|
|
48
|
+
operation: "serialize",
|
|
49
|
+
cause
|
|
50
|
+
})
|
|
51
|
+
}),
|
|
52
|
+
deserializeAttachment: Effect.try({
|
|
53
|
+
try: () => raw.deserializeAttachment(),
|
|
54
|
+
catch: (cause) => new DurableWebSocketAttachmentError({
|
|
55
|
+
operation: "deserialize",
|
|
56
|
+
cause
|
|
57
|
+
})
|
|
58
|
+
})
|
|
59
|
+
});
|
|
60
|
+
const acceptUpgrade = Effect.fn("DurableObjectWebSocket.acceptUpgrade")(function* (options = {}) {
|
|
61
|
+
const state = yield* DurableObjectState;
|
|
62
|
+
const pair = new WebSocketPair();
|
|
63
|
+
const client = pair[0];
|
|
64
|
+
const server = fromWebSocket(pair[1]);
|
|
65
|
+
if (options.attachment !== void 0) yield* server.serializeAttachment(options.attachment);
|
|
66
|
+
yield* state.acceptWebSocket(server, options.tags === void 0 ? void 0 : [...options.tags]);
|
|
67
|
+
return {
|
|
68
|
+
client,
|
|
69
|
+
server,
|
|
70
|
+
response: new Response(null, {
|
|
71
|
+
status: 101,
|
|
72
|
+
webSocket: client
|
|
73
|
+
})
|
|
74
|
+
};
|
|
75
|
+
});
|
|
76
|
+
const attachment = (schema) => {
|
|
77
|
+
const serialize = (socket, value) => S.encodeEffect(schema)(value).pipe(Effect.mapError((cause) => new DurableWebSocketAttachmentError({
|
|
78
|
+
operation: "serialize",
|
|
79
|
+
cause
|
|
80
|
+
})), Effect.flatMap((encoded) => socket.serializeAttachment(encoded)));
|
|
81
|
+
const deserialize = (socket) => Effect.gen(function* () {
|
|
82
|
+
const value = yield* socket.deserializeAttachment;
|
|
83
|
+
if (value === null || value === void 0) return Option.none();
|
|
84
|
+
return Option.some(yield* S.decodeUnknownEffect(schema)(value).pipe(Effect.mapError((cause) => new DurableWebSocketAttachmentError({
|
|
85
|
+
operation: "deserialize",
|
|
86
|
+
cause
|
|
87
|
+
}))));
|
|
88
|
+
});
|
|
89
|
+
const rehydrate = (options = {}) => Effect.gen(function* () {
|
|
90
|
+
const sockets = yield* (yield* DurableObjectState).getWebSockets(options.tag);
|
|
91
|
+
const restored = [];
|
|
92
|
+
const onInvalid = options.onInvalid ?? "ignore";
|
|
93
|
+
for (const socket of sockets) {
|
|
94
|
+
const decoded = yield* Effect.result(deserialize(socket));
|
|
95
|
+
if (Result.isSuccess(decoded) && Option.isSome(decoded.success)) {
|
|
96
|
+
restored.push({
|
|
97
|
+
socket,
|
|
98
|
+
attachment: decoded.success.value
|
|
99
|
+
});
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (Result.isFailure(decoded) && onInvalid === "fail") return yield* Effect.fail(decoded.failure);
|
|
103
|
+
if (Result.isFailure(decoded) && onInvalid === "ignore-and-close") yield* socket.close(1008, "invalid websocket attachment").pipe(Effect.ignore);
|
|
104
|
+
}
|
|
105
|
+
return restored;
|
|
106
|
+
});
|
|
107
|
+
return {
|
|
108
|
+
serialize,
|
|
109
|
+
deserialize,
|
|
110
|
+
rehydrate,
|
|
111
|
+
schema
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
const handlers = (options) => ({
|
|
115
|
+
webSocketMessage: options.message,
|
|
116
|
+
webSocketClose: options.close,
|
|
117
|
+
webSocketError: options.error
|
|
118
|
+
});
|
|
119
|
+
//#endregion
|
|
120
|
+
export { DurableObjectWebSocket_exports, DurableWebSocketAttachmentError, DurableWebSocketCloseError, DurableWebSocketSendError, acceptUpgrade, attachment, fromWebSocket, handlers };
|
package/dist/Email.d.mts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { WorkerEnvironment } from "./Environment.mjs";
|
|
2
|
+
import { BindingNotFoundError, BindingValidationError } from "./Binding.mjs";
|
|
3
|
+
import * as Context$1 from "effect/Context";
|
|
4
|
+
import * as Effect from "effect/Effect";
|
|
5
|
+
import { Layer } from "effect";
|
|
6
|
+
declare namespace Email_d_exports {
|
|
7
|
+
export { EmailAddress, EmailAttachment, EmailBinding, EmailClient, EmailDefinition, EmailErrorCode, EmailMessageBuilder, EmailOperationError, EmailRecipients, EmailSendError, EmailSendOptions, EmailSendResult, EmailService, EmailValidationError, EmailViolation, LayerOptions, Tag, TagClass, emailErrorCodes, isEmailBinding, layer, make, makeClient, sendLimits, validateMessage };
|
|
8
|
+
}
|
|
9
|
+
declare const sendLimits: {
|
|
10
|
+
readonly maxRecipients: 50;
|
|
11
|
+
readonly maxAttachments: 32;
|
|
12
|
+
readonly maxMessageBytes: number;
|
|
13
|
+
readonly maxHeaderNameBytes: 100;
|
|
14
|
+
readonly maxHeaderValueBytes: 2048;
|
|
15
|
+
readonly maxHeadersBytes: number;
|
|
16
|
+
readonly maxAllowlistHeaders: 20;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Error codes reported by Cloudflare Email Sending.
|
|
20
|
+
*
|
|
21
|
+
* Surfaced on {@link EmailOperationError} `code` on a best-effort basis: the
|
|
22
|
+
* runtime attaches them as a non-standard `code` property on thrown errors,
|
|
23
|
+
* which may be missing depending on the runtime or dev-time proxying
|
|
24
|
+
* (miniflare remote bindings serialize errors and can strip it).
|
|
25
|
+
*/
|
|
26
|
+
declare const emailErrorCodes: readonly ["E_VALIDATION_ERROR", "E_FIELD_MISSING", "E_TOO_MANY_RECIPIENTS", "E_TOO_MANY_ATTACHMENTS", "E_SENDER_NOT_VERIFIED", "E_RECIPIENT_NOT_ALLOWED", "E_RECIPIENT_SUPPRESSED", "E_SENDER_DOMAIN_NOT_AVAILABLE", "E_CONTENT_TOO_LARGE", "E_DELIVERY_FAILED", "E_RATE_LIMIT_EXCEEDED", "E_DAILY_LIMIT_EXCEEDED", "E_INTERNAL_SERVER_ERROR", "E_HEADER_NOT_ALLOWED", "E_HEADER_USE_API_FIELD", "E_HEADER_VALUE_INVALID", "E_HEADER_VALUE_TOO_LONG", "E_HEADER_NAME_INVALID", "E_HEADERS_TOO_LARGE", "E_HEADERS_TOO_MANY"];
|
|
27
|
+
type EmailErrorCode = (typeof emailErrorCodes)[number];
|
|
28
|
+
declare const EmailOperationError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
|
|
29
|
+
readonly _tag: "EmailOperationError";
|
|
30
|
+
} & Readonly<A>;
|
|
31
|
+
declare class EmailOperationError extends EmailOperationError_base<{
|
|
32
|
+
readonly binding: string;
|
|
33
|
+
readonly operation: string;
|
|
34
|
+
readonly cause: unknown;
|
|
35
|
+
/**
|
|
36
|
+
* Cloudflare error code taken from the thrown error, when present.
|
|
37
|
+
*
|
|
38
|
+
* Best-effort: may be `undefined` depending on the runtime or dev-time
|
|
39
|
+
* proxying — miniflare remote bindings (`"remote": true`) serialize thrown
|
|
40
|
+
* errors and can strip the non-standard `code` property.
|
|
41
|
+
*/
|
|
42
|
+
readonly code?: EmailErrorCode | (string & {});
|
|
43
|
+
}> {
|
|
44
|
+
get message(): string;
|
|
45
|
+
}
|
|
46
|
+
interface EmailViolation {
|
|
47
|
+
readonly path: string;
|
|
48
|
+
readonly message: string;
|
|
49
|
+
readonly limit?: number;
|
|
50
|
+
readonly actual?: number;
|
|
51
|
+
}
|
|
52
|
+
declare const EmailValidationError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
|
|
53
|
+
readonly _tag: "EmailValidationError";
|
|
54
|
+
} & Readonly<A>;
|
|
55
|
+
declare class EmailValidationError extends EmailValidationError_base<{
|
|
56
|
+
readonly binding: string;
|
|
57
|
+
readonly operation: string;
|
|
58
|
+
readonly violations: ReadonlyArray<EmailViolation>;
|
|
59
|
+
}> {
|
|
60
|
+
get message(): string;
|
|
61
|
+
}
|
|
62
|
+
interface EmailDefinition {
|
|
63
|
+
readonly binding: string;
|
|
64
|
+
}
|
|
65
|
+
type EmailAddress = CloudflareEmailAddress;
|
|
66
|
+
type EmailAttachment = CloudflareEmailAttachment;
|
|
67
|
+
type EmailMessageBuilder = Parameters<CloudflareSendEmail["send"]>[0];
|
|
68
|
+
type EmailSendResult = CloudflareEmailSendResult;
|
|
69
|
+
type EmailBinding = CloudflareSendEmail;
|
|
70
|
+
type EmailRecipients = string | EmailAddress | ReadonlyArray<string | EmailAddress>;
|
|
71
|
+
type EmailSendError = EmailOperationError | EmailValidationError;
|
|
72
|
+
interface EmailSendOptions {
|
|
73
|
+
readonly validate?: boolean;
|
|
74
|
+
}
|
|
75
|
+
interface EmailClient {
|
|
76
|
+
readonly send: (message: EmailMessageBuilder) => Effect.Effect<EmailSendResult, EmailSendError>;
|
|
77
|
+
readonly rawUnsafe: Effect.Effect<EmailBinding>;
|
|
78
|
+
readonly definition: EmailDefinition;
|
|
79
|
+
}
|
|
80
|
+
declare const EmailServiceTypeId: unique symbol;
|
|
81
|
+
interface EmailService<Id extends string> {
|
|
82
|
+
readonly [EmailServiceTypeId]: {
|
|
83
|
+
readonly id: Id;
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
type LayerOptions = {
|
|
87
|
+
readonly binding: string;
|
|
88
|
+
readonly send?: EmailSendOptions;
|
|
89
|
+
};
|
|
90
|
+
interface TagClass<Self, Id extends string> extends Context$1.ServiceClass<Self, Id, EmailClient> {
|
|
91
|
+
readonly id: Id;
|
|
92
|
+
readonly layer: (options: LayerOptions) => Layer.Layer<Self, BindingNotFoundError | BindingValidationError, WorkerEnvironment>;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Validates a structured message against documented Cloudflare Email Sending
|
|
96
|
+
* limits and returns every violation found. Total message size is not checked
|
|
97
|
+
* because the encoded MIME size is only known once Cloudflare composes it.
|
|
98
|
+
*/
|
|
99
|
+
declare const validateMessage: (message: EmailMessageBuilder) => ReadonlyArray<EmailViolation>;
|
|
100
|
+
declare const isEmailBinding: <Candidate>(value: Candidate) => value is Candidate & EmailBinding;
|
|
101
|
+
declare const makeClient: (definition: EmailDefinition, options?: EmailSendOptions) => (email: EmailBinding) => EmailClient;
|
|
102
|
+
declare const layer: <Self>(tag: Context$1.Service<Self, EmailClient>, definition: LayerOptions) => Layer.Layer<Self, BindingNotFoundError | BindingValidationError, WorkerEnvironment>;
|
|
103
|
+
declare const make: <Id extends string>(id: Id) => TagClass<EmailService<Id>, Id>;
|
|
104
|
+
declare const Tag: <Self>() => <Id extends string>(id: Id) => TagClass<Self, Id>;
|
|
105
|
+
//#endregion
|
|
106
|
+
export { EmailAddress, EmailAttachment, EmailBinding, EmailClient, EmailDefinition, EmailErrorCode, EmailMessageBuilder, EmailOperationError, EmailRecipients, EmailSendError, EmailSendOptions, EmailSendResult, EmailService, EmailValidationError, EmailViolation, Email_d_exports, LayerOptions, Tag, TagClass, emailErrorCodes, isEmailBinding, layer, make, makeClient, sendLimits, validateMessage };
|