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,344 @@
|
|
|
1
|
+
import { __exportAll } from "./_virtual/_rolldown/runtime.mjs";
|
|
2
|
+
import { causeMessage } from "./internal/ErrorMessage.mjs";
|
|
3
|
+
import { StorageOperationError } from "./DurableObjectStorage.mjs";
|
|
4
|
+
import { DurableObjectState } from "./DurableObjectState.mjs";
|
|
5
|
+
import * as Context from "effect/Context";
|
|
6
|
+
import * as Data from "effect/Data";
|
|
7
|
+
import * as Effect from "effect/Effect";
|
|
8
|
+
import * as S from "effect/Schema";
|
|
9
|
+
import * as Layer from "effect/Layer";
|
|
10
|
+
import * as Predicate from "effect/Predicate";
|
|
11
|
+
import * as Exit from "effect/Exit";
|
|
12
|
+
import * as Clock from "effect/Clock";
|
|
13
|
+
import * as Duration from "effect/Duration";
|
|
14
|
+
import * as DateTime from "effect/DateTime";
|
|
15
|
+
//#region src/DurableObjectAlarm.ts
|
|
16
|
+
var DurableObjectAlarm_exports = /* @__PURE__ */ __exportAll({
|
|
17
|
+
DurableObjectAlarm: () => DurableObjectAlarm,
|
|
18
|
+
DurableObjectAlarmEvent: () => DurableObjectAlarmEvent,
|
|
19
|
+
InvalidAlarmPayloadError: () => InvalidAlarmPayloadError,
|
|
20
|
+
InvalidAlarmRefError: () => InvalidAlarmRefError,
|
|
21
|
+
InvalidProcessDueAlarmsOptionsError: () => InvalidProcessDueAlarmsOptionsError,
|
|
22
|
+
InvalidRepeatEveryError: () => InvalidRepeatEveryError,
|
|
23
|
+
StoredAlarmDecodeError: () => StoredAlarmDecodeError,
|
|
24
|
+
Tag: () => Tag,
|
|
25
|
+
define: () => define,
|
|
26
|
+
processDue: () => processDue
|
|
27
|
+
});
|
|
28
|
+
const INIT_TABLE_SQL = `
|
|
29
|
+
CREATE TABLE IF NOT EXISTS effect_cf_scheduled_alarms (
|
|
30
|
+
storage_id TEXT PRIMARY KEY,
|
|
31
|
+
alarm_id TEXT NOT NULL,
|
|
32
|
+
tag TEXT NOT NULL,
|
|
33
|
+
run_at INTEGER NOT NULL,
|
|
34
|
+
repeat_every_ms INTEGER,
|
|
35
|
+
payload TEXT NOT NULL
|
|
36
|
+
);
|
|
37
|
+
CREATE INDEX IF NOT EXISTS idx_effect_cf_scheduled_alarms_run_at_storage_id
|
|
38
|
+
ON effect_cf_scheduled_alarms (run_at, storage_id);
|
|
39
|
+
`;
|
|
40
|
+
const DEFAULT_PROCESS_DUE_ALARMS_LIMIT = 100;
|
|
41
|
+
const DEFAULT_PROCESS_DUE_ALARMS_FAILURE_RESCHEDULE_AFTER = "30 seconds";
|
|
42
|
+
const getScheduledEventId = (input) => `effect-cf-alarm:${encodeURIComponent(input.tag)}:${encodeURIComponent(input.id)}`;
|
|
43
|
+
var InvalidAlarmRefError = class extends Data.TaggedError("InvalidAlarmRefError") {
|
|
44
|
+
get message() {
|
|
45
|
+
return `Invalid Durable Object alarm ref: ${causeMessage(this.cause)}`;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
var InvalidAlarmPayloadError = class extends Data.TaggedError("InvalidAlarmPayloadError") {
|
|
49
|
+
get message() {
|
|
50
|
+
return `Invalid Durable Object alarm payload: ${causeMessage(this.cause)}`;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
var InvalidRepeatEveryError = class extends Data.TaggedError("InvalidRepeatEveryError") {
|
|
54
|
+
get message() {
|
|
55
|
+
return `Invalid Durable Object alarm repeatEvery: ${causeMessage(this.cause)}`;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
var InvalidProcessDueAlarmsOptionsError = class extends Data.TaggedError("InvalidProcessDueAlarmsOptionsError") {
|
|
59
|
+
get message() {
|
|
60
|
+
return `Invalid processDueAlarms options: ${causeMessage(this.cause)}`;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
var StoredAlarmDecodeError = class extends Data.TaggedError("StoredAlarmDecodeError") {
|
|
64
|
+
get message() {
|
|
65
|
+
return `Failed to decode stored alarm "${this.storageId}": ${causeMessage(this.cause)}`;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
const DurableObjectAlarmEvent = S.TaggedUnion({ AlarmDue: {
|
|
69
|
+
id: S.NonEmptyString,
|
|
70
|
+
payload: S.Json,
|
|
71
|
+
/** Persisted due time, which can differ from the invocation time during retries. */
|
|
72
|
+
scheduledAt: S.DateTimeUtc,
|
|
73
|
+
tag: S.NonEmptyString
|
|
74
|
+
} });
|
|
75
|
+
const AlarmRefSchema = S.Struct({
|
|
76
|
+
id: S.NonEmptyString,
|
|
77
|
+
tag: S.NonEmptyString
|
|
78
|
+
});
|
|
79
|
+
const decodeAlarmRef = (input) => S.decodeUnknownEffect(AlarmRefSchema)(input).pipe(Effect.mapError((cause) => new InvalidAlarmRefError({ cause })));
|
|
80
|
+
const StoredPayloadString = S.fromJsonString(S.Json);
|
|
81
|
+
const decodeStoredPayload = (row) => S.decodeUnknownEffect(StoredPayloadString)(row.payload).pipe(Effect.mapError((cause) => new StoredAlarmDecodeError({
|
|
82
|
+
cause,
|
|
83
|
+
storageId: row.storage_id
|
|
84
|
+
})));
|
|
85
|
+
const encodeStoredPayload = (payload) => S.encodeEffect(StoredPayloadString)(payload).pipe(Effect.mapError((cause) => new InvalidAlarmPayloadError({ cause })));
|
|
86
|
+
const ensureTable = (state) => state.storage.sql.exec(INIT_TABLE_SQL).pipe(Effect.asVoid);
|
|
87
|
+
const toRepeatEveryMillis = (input) => {
|
|
88
|
+
if (input === void 0) return Effect.succeed(null);
|
|
89
|
+
return Effect.try({
|
|
90
|
+
try: () => {
|
|
91
|
+
const millis = Duration.toMillis(input);
|
|
92
|
+
if (!Number.isFinite(millis) || millis <= 0) throw new Error("Alarm repeatEvery must be a positive finite duration");
|
|
93
|
+
return Math.ceil(millis);
|
|
94
|
+
},
|
|
95
|
+
catch: (cause) => new InvalidRepeatEveryError({ cause })
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
const toAlarmDue = (row) => Effect.gen(function* () {
|
|
99
|
+
if (!Number.isFinite(row.run_at)) return yield* Effect.fail(new StoredAlarmDecodeError({
|
|
100
|
+
cause: /* @__PURE__ */ new Error("Stored alarm run_at must be a finite number"),
|
|
101
|
+
storageId: row.storage_id
|
|
102
|
+
}));
|
|
103
|
+
return DurableObjectAlarmEvent.make({
|
|
104
|
+
_tag: "AlarmDue",
|
|
105
|
+
id: row.alarm_id,
|
|
106
|
+
payload: yield* decodeStoredPayload(row),
|
|
107
|
+
scheduledAt: DateTime.makeUnsafe(row.run_at),
|
|
108
|
+
tag: row.tag
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
const getProcessLimit = (options) => {
|
|
112
|
+
const limit = options?.limit ?? DEFAULT_PROCESS_DUE_ALARMS_LIMIT;
|
|
113
|
+
if (!Number.isSafeInteger(limit) || limit <= 0) return Effect.fail(new InvalidProcessDueAlarmsOptionsError({ cause: /* @__PURE__ */ new Error("processDueAlarms limit must be a positive safe integer") }));
|
|
114
|
+
return Effect.succeed(limit);
|
|
115
|
+
};
|
|
116
|
+
const toFailureRescheduleMillis = (input) => Effect.try({
|
|
117
|
+
try: () => {
|
|
118
|
+
const millis = Duration.toMillis(input);
|
|
119
|
+
if (!Number.isFinite(millis) || millis <= 0) throw new Error("Alarm failure rescheduleAfter must be a positive finite duration");
|
|
120
|
+
return Math.ceil(millis);
|
|
121
|
+
},
|
|
122
|
+
catch: (cause) => new InvalidProcessDueAlarmsOptionsError({ cause })
|
|
123
|
+
});
|
|
124
|
+
const getFailureRetryDelay = (options) => toFailureRescheduleMillis(options?.retryFailedAfter ?? DEFAULT_PROCESS_DUE_ALARMS_FAILURE_RESCHEDULE_AFTER);
|
|
125
|
+
const getFailureActionMode = (action) => Predicate.isString(action) ? action : action.mode;
|
|
126
|
+
const getFailureActionRetryDelay = (action) => Predicate.isString(action) || action.mode !== "retry" ? void 0 : action.retryFailedAfter;
|
|
127
|
+
const processDue = (handle, options = {}) => Effect.gen(function* () {
|
|
128
|
+
return yield* (yield* DurableObjectAlarm).processDueAlarms(handle, options);
|
|
129
|
+
});
|
|
130
|
+
const TypedAlarmSchedulerTypeId = Symbol.for("effect-cf/DurableObjectAlarm/TypedScheduler");
|
|
131
|
+
const isAlarmDefinitionConfig = (definition) => Predicate.isObject(definition) && "payload" in definition;
|
|
132
|
+
const getAlarmDefinitionSchema = (definition) => isAlarmDefinitionConfig(definition) ? definition.payload : definition;
|
|
133
|
+
const getAlarmDefinitionFailureAction = (definition) => {
|
|
134
|
+
if (definition === void 0 || !isAlarmDefinitionConfig(definition)) return;
|
|
135
|
+
if (definition.failure === void 0) return;
|
|
136
|
+
return definition.failure === "retry" ? {
|
|
137
|
+
mode: "retry",
|
|
138
|
+
retryFailedAfter: definition.retry?.initialDelay
|
|
139
|
+
} : { mode: definition.failure };
|
|
140
|
+
};
|
|
141
|
+
const makeDefinition = (definitions) => {
|
|
142
|
+
const definitionFor = (tag) => Object.hasOwn(definitions, tag) ? definitions[tag] : void 0;
|
|
143
|
+
const bind = (mutations) => ({
|
|
144
|
+
scheduleAlarm: Effect.fn("DefinedAlarms.scheduleAlarm")(function* (input) {
|
|
145
|
+
const definition = definitionFor(input.tag);
|
|
146
|
+
if (definition === void 0) return yield* Effect.fail(new InvalidAlarmRefError({ cause: /* @__PURE__ */ new Error(`Unknown alarm tag "${input.tag}"`) }));
|
|
147
|
+
const payload = yield* S.encodeEffect(getAlarmDefinitionSchema(definition))(input.payload).pipe(Effect.flatMap(S.decodeUnknownEffect(S.Json)), Effect.mapError((cause) => new InvalidAlarmPayloadError({ cause })));
|
|
148
|
+
yield* mutations.scheduleAlarm({
|
|
149
|
+
...input,
|
|
150
|
+
payload
|
|
151
|
+
});
|
|
152
|
+
}),
|
|
153
|
+
cancelAlarm: Effect.fn("DefinedAlarms.cancelAlarm")(function* (input) {
|
|
154
|
+
if (definitionFor(input.tag) === void 0) return yield* Effect.fail(new InvalidAlarmRefError({ cause: /* @__PURE__ */ new Error(`Unknown alarm tag "${input.tag}"`) }));
|
|
155
|
+
yield* mutations.cancelAlarm(input);
|
|
156
|
+
})
|
|
157
|
+
});
|
|
158
|
+
return {
|
|
159
|
+
make: (alarms) => ({
|
|
160
|
+
[TypedAlarmSchedulerTypeId]: TypedAlarmSchedulerTypeId,
|
|
161
|
+
...bind(alarms),
|
|
162
|
+
transaction: (closure) => alarms.transaction((tx) => closure(bind(tx)))
|
|
163
|
+
}),
|
|
164
|
+
handlers: (handlers, options) => processDue((event) => Effect.gen(function* () {
|
|
165
|
+
const definition = definitionFor(event.tag);
|
|
166
|
+
if (definition === void 0) return yield* Effect.fail(new StoredAlarmDecodeError({
|
|
167
|
+
cause: /* @__PURE__ */ new Error(`Unknown alarm tag "${event.tag}"`),
|
|
168
|
+
storageId: getScheduledEventId(event)
|
|
169
|
+
}));
|
|
170
|
+
const schema = getAlarmDefinitionSchema(definition);
|
|
171
|
+
const payload = yield* S.decodeUnknownEffect(schema)(event.payload).pipe(Effect.mapError((cause) => new StoredAlarmDecodeError({
|
|
172
|
+
cause,
|
|
173
|
+
storageId: getScheduledEventId(event)
|
|
174
|
+
})));
|
|
175
|
+
const handler = handlers[event.tag];
|
|
176
|
+
yield* handler({
|
|
177
|
+
...event,
|
|
178
|
+
payload
|
|
179
|
+
});
|
|
180
|
+
}), {
|
|
181
|
+
...options,
|
|
182
|
+
onFailure: (failure) => Effect.gen(function* () {
|
|
183
|
+
const action = getAlarmDefinitionFailureAction(definitionFor(failure.tag));
|
|
184
|
+
const optionAction = options?.onFailure === void 0 ? void 0 : yield* options.onFailure(failure);
|
|
185
|
+
return action ?? optionAction;
|
|
186
|
+
})
|
|
187
|
+
})
|
|
188
|
+
};
|
|
189
|
+
};
|
|
190
|
+
/** Handler-only definitions for applications using the raw scheduler. Prefer Tag for typed scheduling. */
|
|
191
|
+
const define = (definitions) => ({ handlers: makeDefinition(definitions).handlers });
|
|
192
|
+
/** The typed scheduler becomes available when its handlers are registered on a Durable Object. */
|
|
193
|
+
const Tag = () => (id, definitions) => {
|
|
194
|
+
const tag = Context.Service()(id);
|
|
195
|
+
const definition = makeDefinition(definitions);
|
|
196
|
+
return Object.assign(tag, { handlers: (handlers, options) => ({
|
|
197
|
+
layer: Layer.effect(tag, Effect.map(DurableObjectAlarm, definition.make)),
|
|
198
|
+
run: definition.handlers(handlers, options)
|
|
199
|
+
}) });
|
|
200
|
+
};
|
|
201
|
+
var DurableObjectAlarm = class DurableObjectAlarm extends Context.Service()("effect-cf/DurableObjectAlarm") {
|
|
202
|
+
static layer = Layer.effect(DurableObjectAlarm, Effect.gen(function* () {
|
|
203
|
+
const state = yield* DurableObjectState;
|
|
204
|
+
const reconcileAlarm = Effect.fn("DurableObjectAlarm.reconcileAlarm")(function* () {
|
|
205
|
+
const next = (yield* (yield* state.storage.sql.exec(`SELECT run_at FROM effect_cf_scheduled_alarms ORDER BY run_at ASC, storage_id ASC LIMIT 1`)).toArray())[0];
|
|
206
|
+
if (next === void 0) {
|
|
207
|
+
yield* state.storage.deleteAlarm();
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
yield* state.storage.setAlarm(next.run_at);
|
|
211
|
+
});
|
|
212
|
+
const rescheduleFailedAlarm = Effect.fn("DurableObjectAlarm.rescheduleFailedAlarm")(function* (row, retryDelayMillis) {
|
|
213
|
+
const retryAt = (yield* Clock.currentTimeMillis) + retryDelayMillis;
|
|
214
|
+
if (row.repeat_every_ms === null) {
|
|
215
|
+
yield* (yield* state.storage.sql.exec(`UPDATE effect_cf_scheduled_alarms
|
|
216
|
+
SET run_at = ?
|
|
217
|
+
WHERE storage_id = ?
|
|
218
|
+
AND run_at = ?
|
|
219
|
+
AND repeat_every_ms IS NULL
|
|
220
|
+
AND payload = ?`, retryAt, row.storage_id, row.run_at, row.payload)).rowsWritten;
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
yield* (yield* state.storage.sql.exec(`UPDATE effect_cf_scheduled_alarms
|
|
224
|
+
SET run_at = ?
|
|
225
|
+
WHERE storage_id = ?
|
|
226
|
+
AND run_at = ?
|
|
227
|
+
AND repeat_every_ms = ?
|
|
228
|
+
AND payload = ?`, retryAt, row.storage_id, row.run_at, row.repeat_every_ms, row.payload)).rowsWritten;
|
|
229
|
+
});
|
|
230
|
+
const acknowledgeAlarm = Effect.fn("DurableObjectAlarm.acknowledgeAlarm")(function* (row) {
|
|
231
|
+
if (row.repeat_every_ms === null) {
|
|
232
|
+
yield* (yield* state.storage.sql.exec(`DELETE FROM effect_cf_scheduled_alarms
|
|
233
|
+
WHERE storage_id = ?
|
|
234
|
+
AND run_at = ?
|
|
235
|
+
AND repeat_every_ms IS NULL
|
|
236
|
+
AND payload = ?`, row.storage_id, row.run_at, row.payload)).rowsWritten;
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
const acknowledgedAt = yield* Clock.currentTimeMillis;
|
|
240
|
+
yield* (yield* state.storage.sql.exec(`UPDATE effect_cf_scheduled_alarms
|
|
241
|
+
SET run_at = ?
|
|
242
|
+
WHERE storage_id = ?
|
|
243
|
+
AND run_at = ?
|
|
244
|
+
AND repeat_every_ms = ?
|
|
245
|
+
AND payload = ?`, acknowledgedAt + row.repeat_every_ms, row.storage_id, row.run_at, row.repeat_every_ms, row.payload)).rowsWritten;
|
|
246
|
+
});
|
|
247
|
+
const cancelAlarm = Effect.fn("DurableObjectAlarm.cancelAlarm")(function* (input) {
|
|
248
|
+
const ref = yield* decodeAlarmRef(input);
|
|
249
|
+
yield* state.storage.sql.exec(`DELETE FROM effect_cf_scheduled_alarms WHERE storage_id = ?`, getScheduledEventId(ref));
|
|
250
|
+
});
|
|
251
|
+
const scheduleAlarm = Effect.fn("DurableObjectAlarm.scheduleAlarm")(function* (input) {
|
|
252
|
+
const ref = yield* decodeAlarmRef(input);
|
|
253
|
+
const repeatEveryMillis = yield* toRepeatEveryMillis(input.repeatEvery);
|
|
254
|
+
const payload = yield* encodeStoredPayload(input.payload);
|
|
255
|
+
const runAt = DateTime.toEpochMillis(input.runAt);
|
|
256
|
+
yield* state.storage.sql.exec(`INSERT OR REPLACE INTO effect_cf_scheduled_alarms
|
|
257
|
+
(storage_id, alarm_id, tag, run_at, repeat_every_ms, payload)
|
|
258
|
+
VALUES (?, ?, ?, ?, ?, ?)`, getScheduledEventId(ref), ref.id, ref.tag, runAt, repeatEveryMillis, payload);
|
|
259
|
+
});
|
|
260
|
+
const transaction = (closure) => state.storage.transaction(() => Effect.withFiber((owner) => {
|
|
261
|
+
let active = true;
|
|
262
|
+
const requireActive = (effect) => Effect.withFiber((fiber) => active && fiber === owner ? effect : Effect.fail(new StorageOperationError({
|
|
263
|
+
operation: "alarm.transaction",
|
|
264
|
+
cause: /* @__PURE__ */ new Error("Alarm mutations require their active transaction callback")
|
|
265
|
+
})));
|
|
266
|
+
return Effect.gen(function* () {
|
|
267
|
+
yield* ensureTable(state);
|
|
268
|
+
const result = yield* Effect.suspend(() => closure({
|
|
269
|
+
cancelAlarm: (input) => requireActive(cancelAlarm(input)),
|
|
270
|
+
scheduleAlarm: (input) => requireActive(scheduleAlarm(input))
|
|
271
|
+
})).pipe(Effect.ensuring(Effect.sync(() => {
|
|
272
|
+
active = false;
|
|
273
|
+
})));
|
|
274
|
+
yield* reconcileAlarm();
|
|
275
|
+
return result;
|
|
276
|
+
});
|
|
277
|
+
}));
|
|
278
|
+
const processDueAlarms = Effect.fn("DurableObjectAlarm.processDueAlarms")(function* (handle, options) {
|
|
279
|
+
yield* ensureTable(state);
|
|
280
|
+
const mode = options?.mode ?? "isolated";
|
|
281
|
+
const limit = yield* getProcessLimit(options);
|
|
282
|
+
const now = yield* Clock.currentTimeMillis;
|
|
283
|
+
const dueRows = yield* (yield* state.storage.sql.exec(`SELECT storage_id, alarm_id, tag, run_at, repeat_every_ms, payload
|
|
284
|
+
FROM effect_cf_scheduled_alarms
|
|
285
|
+
WHERE run_at <= ?
|
|
286
|
+
ORDER BY run_at ASC, storage_id ASC
|
|
287
|
+
LIMIT ?`, now, limit)).toArray();
|
|
288
|
+
const handled = [];
|
|
289
|
+
const failed = [];
|
|
290
|
+
const handleFailure = function* (row, event, cause) {
|
|
291
|
+
const failure = {
|
|
292
|
+
cause,
|
|
293
|
+
event,
|
|
294
|
+
id: row.alarm_id,
|
|
295
|
+
storageId: row.storage_id,
|
|
296
|
+
tag: row.tag
|
|
297
|
+
};
|
|
298
|
+
failed.push(failure);
|
|
299
|
+
const failureAction = options?.onFailure === void 0 ? void 0 : yield* options.onFailure(failure);
|
|
300
|
+
const actionMode = failureAction === void 0 ? mode : getFailureActionMode(failureAction);
|
|
301
|
+
if (actionMode === "retry" || actionMode === "isolated") {
|
|
302
|
+
const actionRetryDelay = failureAction === void 0 ? void 0 : getFailureActionRetryDelay(failureAction);
|
|
303
|
+
const retryDelay = actionRetryDelay === void 0 ? yield* getFailureRetryDelay(options) : yield* toFailureRescheduleMillis(actionRetryDelay);
|
|
304
|
+
yield* rescheduleFailedAlarm(row, retryDelay);
|
|
305
|
+
return "continue";
|
|
306
|
+
}
|
|
307
|
+
if (actionMode === "skip-and-advance-repeat") {
|
|
308
|
+
yield* acknowledgeAlarm(row);
|
|
309
|
+
return "continue";
|
|
310
|
+
}
|
|
311
|
+
yield* reconcileAlarm();
|
|
312
|
+
return "stop";
|
|
313
|
+
};
|
|
314
|
+
for (const row of dueRows) {
|
|
315
|
+
const eventExit = yield* Effect.exit(toAlarmDue(row));
|
|
316
|
+
if (Exit.isFailure(eventExit)) {
|
|
317
|
+
if ((yield* handleFailure(row, void 0, eventExit.cause)) === "stop") return yield* Effect.failCause(eventExit.cause);
|
|
318
|
+
continue;
|
|
319
|
+
}
|
|
320
|
+
const event = eventExit.value;
|
|
321
|
+
const handleExit = yield* Effect.exit(handle(event));
|
|
322
|
+
if (Exit.isFailure(handleExit)) {
|
|
323
|
+
if ((yield* handleFailure(row, event, handleExit.cause)) === "stop") return yield* Effect.failCause(handleExit.cause);
|
|
324
|
+
continue;
|
|
325
|
+
}
|
|
326
|
+
yield* acknowledgeAlarm(row);
|
|
327
|
+
handled.push(event);
|
|
328
|
+
}
|
|
329
|
+
yield* reconcileAlarm();
|
|
330
|
+
return {
|
|
331
|
+
failed,
|
|
332
|
+
handled
|
|
333
|
+
};
|
|
334
|
+
});
|
|
335
|
+
return DurableObjectAlarm.of({
|
|
336
|
+
cancelAlarm: (input) => transaction((alarms) => alarms.cancelAlarm(input)),
|
|
337
|
+
processDueAlarms,
|
|
338
|
+
scheduleAlarm: (input) => transaction((alarms) => alarms.scheduleAlarm(input)),
|
|
339
|
+
transaction
|
|
340
|
+
});
|
|
341
|
+
}));
|
|
342
|
+
};
|
|
343
|
+
//#endregion
|
|
344
|
+
export { DurableObjectAlarm, DurableObjectAlarmEvent, DurableObjectAlarm_exports, InvalidAlarmPayloadError, InvalidAlarmRefError, InvalidProcessDueAlarmsOptionsError, InvalidRepeatEveryError, StoredAlarmDecodeError, Tag, define, processDue };
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { WorkerEnvironment } from "./Environment.mjs";
|
|
2
|
+
import { BindingNotFoundError, BindingValidationError } from "./Binding.mjs";
|
|
3
|
+
import { Provider } from "./Rpc.mjs";
|
|
4
|
+
import { ReservedMethodName as ReservedMethodName$1 } from "./RpcDefinition.mjs";
|
|
5
|
+
import { DurableObjectNamespaceEffectClient, DurableObjectNamespaceStaticClient } from "./DurableObjectNamespace.mjs";
|
|
6
|
+
import { DurableObjectClass, DurableObjectHandler, DurableObjectOptions, RuntimeContext } from "./DurableObject.mjs";
|
|
7
|
+
import * as Context$1 from "effect/Context";
|
|
8
|
+
import { Layer, Schema } from "effect";
|
|
9
|
+
declare namespace DurableObjectDefinition_d_exports {
|
|
10
|
+
export { Api, Definition, DurableObject, HandlerEffect, Handlers, LayerOptions, Method, Methods, NoReservedMethods, Options, ReservedMethodName, ServerApi, ServiceFreeSchema, Tag, TagClass, implement, make, method };
|
|
11
|
+
}
|
|
12
|
+
type ServiceFreeSchema = Schema.Codec<any, any, never, never>;
|
|
13
|
+
interface Method<Args extends ReadonlyArray<ServiceFreeSchema> = ReadonlyArray<ServiceFreeSchema>, Success extends ServiceFreeSchema = ServiceFreeSchema> {
|
|
14
|
+
readonly args: Args;
|
|
15
|
+
readonly success: Success;
|
|
16
|
+
}
|
|
17
|
+
declare namespace Method {
|
|
18
|
+
export type Any = Method<ReadonlyArray<ServiceFreeSchema>, ServiceFreeSchema>;
|
|
19
|
+
type ArgsFromSchemas<Args extends ReadonlyArray<ServiceFreeSchema>> = Args extends readonly [] ? [] : Args extends readonly [infer Head extends ServiceFreeSchema, ...infer Tail extends ReadonlyArray<ServiceFreeSchema>] ? [Schema.Schema.Type<Head>, ...ArgsFromSchemas<Tail>] : Array<Schema.Schema.Type<Args[number]>>;
|
|
20
|
+
type EncodedArgsFromSchemas<Args extends ReadonlyArray<ServiceFreeSchema>> = { [Index in keyof Args]: Schema.Json; };
|
|
21
|
+
export type Args<Self extends Any> = ArgsFromSchemas<Self["args"]>;
|
|
22
|
+
export type EncodedArgs<Self extends Any> = EncodedArgsFromSchemas<Self["args"]>;
|
|
23
|
+
export type Success<Self extends Any> = Schema.Schema.Type<Self["success"]>;
|
|
24
|
+
export type EncodedSuccess<_Self extends Any> = Schema.Json;
|
|
25
|
+
export {};
|
|
26
|
+
}
|
|
27
|
+
type Methods = Record<string, Method.Any>;
|
|
28
|
+
/**
|
|
29
|
+
* RPC contract for a Durable Object class.
|
|
30
|
+
*/
|
|
31
|
+
interface Definition<Id extends string = string, MethodDefinitions extends Methods = Methods> {
|
|
32
|
+
readonly id: Id;
|
|
33
|
+
readonly methods: MethodDefinitions;
|
|
34
|
+
}
|
|
35
|
+
declare namespace Definition {
|
|
36
|
+
type Any = Definition<string, Methods>;
|
|
37
|
+
}
|
|
38
|
+
type ReservedMethodName = ReservedMethodName$1 | "fetch" | "alarm" | "webSocketMessage" | "webSocketClose" | "webSocketError";
|
|
39
|
+
type NoReservedMethods<MethodDefinitions extends Methods> = Extract<keyof MethodDefinitions, ReservedMethodName> extends never ? MethodDefinitions : never;
|
|
40
|
+
/**
|
|
41
|
+
* Promise-based client API derived from a Durable Object definition.
|
|
42
|
+
*/
|
|
43
|
+
type ServerApi<Self extends Definition.Any> = { readonly [Key in keyof Self["methods"]]: (...args: Method.Args<Self["methods"][Key]>) => Promise<Method.Success<Self["methods"][Key]>>; };
|
|
44
|
+
type Api<Self extends Definition.Any> = Provider<ServerApi<Self>, ReservedMethodName>;
|
|
45
|
+
/**
|
|
46
|
+
* Effect handlers for each RPC method in a Durable Object definition.
|
|
47
|
+
*/
|
|
48
|
+
type Handlers<ROut, Self extends Definition.Any> = { readonly [Key in keyof Self["methods"]]: (...args: Method.Args<Self["methods"][Key]>) => DurableObjectHandler<ROut, Method.Success<Self["methods"][Key]>>; };
|
|
49
|
+
/**
|
|
50
|
+
* Durable Object constructor options for a specific RPC definition.
|
|
51
|
+
*/
|
|
52
|
+
type Options<ROut, Self extends Definition.Any, REvent = never, EventLayerError = never, RAlarm = never> = DurableObjectOptions<ROut, REvent, EventLayerError, Handlers<NoInfer<ROut> | REvent | NoInfer<RAlarm>, Self>, RAlarm> & {
|
|
53
|
+
readonly rpc: Handlers<NoInfer<ROut> | REvent | NoInfer<RAlarm>, Self>;
|
|
54
|
+
};
|
|
55
|
+
type LayerOptions = {
|
|
56
|
+
readonly binding: string;
|
|
57
|
+
readonly rpcTracing?: boolean;
|
|
58
|
+
};
|
|
59
|
+
type TagClass<Self, Id extends string, MethodDefinitions extends Methods> = Context$1.ServiceClass<Self, `effect-cf/DurableObject/${Id}`, DurableObjectNamespaceEffectClient<Api<Definition<Id, MethodDefinitions>>, Definition<Id, MethodDefinitions>>> & DurableObjectNamespaceStaticClient<Self, Api<Definition<Id, MethodDefinitions>>, Definition<Id, MethodDefinitions>> & {
|
|
60
|
+
readonly id: Id;
|
|
61
|
+
readonly methods: MethodDefinitions;
|
|
62
|
+
readonly make: <ROut, LayerError, REvent = never, EventLayerError = never, RAlarm = never>(layer: Layer.Layer<ROut, LayerError, RuntimeContext<NoInfer<RAlarm>>>, options: Options<ROut, Definition<Id, MethodDefinitions>, REvent, EventLayerError, RAlarm>) => DurableObjectClass<Handlers<ROut | REvent | RAlarm, Definition<Id, MethodDefinitions>>, ROut | REvent | RAlarm>;
|
|
63
|
+
readonly layer: (options: LayerOptions) => Layer.Layer<Self, BindingNotFoundError | BindingValidationError, WorkerEnvironment>;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Defines a single RPC method schema in a Durable Object definition.
|
|
67
|
+
*/
|
|
68
|
+
declare function method<Success extends ServiceFreeSchema>(definition: {
|
|
69
|
+
readonly success: Success;
|
|
70
|
+
}): Method<readonly [], Success>;
|
|
71
|
+
declare function method<const Args extends ReadonlyArray<ServiceFreeSchema>, Success extends ServiceFreeSchema>(definition: {
|
|
72
|
+
readonly args: Args;
|
|
73
|
+
readonly success: Success;
|
|
74
|
+
}): Method<Args, Success>;
|
|
75
|
+
declare const make: <Id extends string, const MethodDefinitions extends Methods>(id: Id, methods: MethodDefinitions & NoReservedMethods<MethodDefinitions>) => TagClass<Definition<Id, MethodDefinitions>, Id, MethodDefinitions>;
|
|
76
|
+
declare const Tag: <Self>() => <Id extends string, const MethodDefinitions extends Methods>(id: Id, methods: MethodDefinitions & NoReservedMethods<MethodDefinitions>) => TagClass<Self, Id, MethodDefinitions>;
|
|
77
|
+
declare const DurableObject: typeof Tag;
|
|
78
|
+
/**
|
|
79
|
+
* Helper for implementing handlers with the exact method shape of a definition.
|
|
80
|
+
*/
|
|
81
|
+
declare const implement: <ROut, const Self extends Definition.Any>(_definition: Self, handlers: Handlers<ROut, Self>) => Handlers<ROut, Self>;
|
|
82
|
+
/**
|
|
83
|
+
* Convenience alias for a single Durable Object RPC handler Effect.
|
|
84
|
+
*/
|
|
85
|
+
type HandlerEffect<ROut, Self extends Definition.Any, Key extends keyof Self["methods"]> = DurableObjectHandler<ROut, Method.Success<Self["methods"][Key]>>;
|
|
86
|
+
//#endregion
|
|
87
|
+
export { Api, Definition, DurableObject, DurableObjectDefinition_d_exports, HandlerEffect, Handlers, LayerOptions, Method, Methods, NoReservedMethods, Options, ReservedMethodName, ServerApi, ServiceFreeSchema, Tag, TagClass, implement, make, method };
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { __exportAll } from "./_virtual/_rolldown/runtime.mjs";
|
|
2
|
+
import { assertNoReservedMethods, decodeArgs, encodeSuccess, make as make$1, method as method$1 } from "./RpcDefinition.mjs";
|
|
3
|
+
import { recordDecodedArgs } from "./internal/RpcInvocation.mjs";
|
|
4
|
+
import { layer, makeDirectMethods } from "./DurableObjectNamespace.mjs";
|
|
5
|
+
import { make as make$2 } from "./DurableObject.mjs";
|
|
6
|
+
import * as Context from "effect/Context";
|
|
7
|
+
import * as Effect from "effect/Effect";
|
|
8
|
+
//#region src/DurableObjectDefinition.ts
|
|
9
|
+
var DurableObjectDefinition_exports = /* @__PURE__ */ __exportAll({
|
|
10
|
+
DurableObject: () => DurableObject,
|
|
11
|
+
Tag: () => Tag,
|
|
12
|
+
implement: () => implement,
|
|
13
|
+
make: () => make,
|
|
14
|
+
method: () => method
|
|
15
|
+
});
|
|
16
|
+
const reservedMethodNames = /* @__PURE__ */ new Set([
|
|
17
|
+
"constructor",
|
|
18
|
+
"dup",
|
|
19
|
+
"fetch",
|
|
20
|
+
"alarm",
|
|
21
|
+
"webSocketMessage",
|
|
22
|
+
"webSocketClose",
|
|
23
|
+
"webSocketError"
|
|
24
|
+
]);
|
|
25
|
+
function method(definition) {
|
|
26
|
+
return method$1(definition);
|
|
27
|
+
}
|
|
28
|
+
const makeDefinition = (id, methods) => {
|
|
29
|
+
assertNoReservedMethods("Durable Object", methods, reservedMethodNames);
|
|
30
|
+
const definition = make$1(id, methods);
|
|
31
|
+
return Object.assign(definition, { make: (layer, options) => make$2(layer, {
|
|
32
|
+
...options,
|
|
33
|
+
rpc: wrapHandlers(definition, options.rpc)
|
|
34
|
+
}) });
|
|
35
|
+
};
|
|
36
|
+
const make = (id, methods) => Tag()(id, methods);
|
|
37
|
+
const Tag = () => (id, methods) => {
|
|
38
|
+
const definition = makeDefinition(id, methods);
|
|
39
|
+
const serviceKey = `effect-cf/DurableObject/${id}`;
|
|
40
|
+
const tag = Context.Service()(serviceKey);
|
|
41
|
+
const bindingDefinition = (binding) => ({
|
|
42
|
+
...binding,
|
|
43
|
+
definition
|
|
44
|
+
});
|
|
45
|
+
const layer$1 = (binding) => layer(tag, bindingDefinition(binding));
|
|
46
|
+
const newUniqueId = Effect.fn("DurableObject.newUniqueId")(function* (options) {
|
|
47
|
+
return yield* (yield* tag).newUniqueId(options);
|
|
48
|
+
});
|
|
49
|
+
const idFromName = Effect.fn("DurableObject.idFromName")(function* (name) {
|
|
50
|
+
return yield* (yield* tag).idFromName(name);
|
|
51
|
+
});
|
|
52
|
+
const idFromString = Effect.fn("DurableObject.idFromString")(function* (value) {
|
|
53
|
+
return yield* (yield* tag).idFromString(value);
|
|
54
|
+
});
|
|
55
|
+
const get = Effect.fn("DurableObject.get")(function* (objectId, options) {
|
|
56
|
+
return yield* (yield* tag).get(objectId, options);
|
|
57
|
+
});
|
|
58
|
+
const getByName = Effect.fn("DurableObject.getByName")(function* (name, options) {
|
|
59
|
+
return yield* (yield* tag).getByName(name, options);
|
|
60
|
+
});
|
|
61
|
+
const jurisdiction = Effect.fn("DurableObject.jurisdiction")(function* (value) {
|
|
62
|
+
return yield* (yield* tag).jurisdiction(value);
|
|
63
|
+
});
|
|
64
|
+
const fetch = Effect.fn("DurableObject.fetch")(function* (stub, input, init) {
|
|
65
|
+
return yield* (yield* tag).fetch(stub, input, init);
|
|
66
|
+
});
|
|
67
|
+
const rpc = Effect.fnUntraced(function* (stub, methodName, ...args) {
|
|
68
|
+
const invokeRpc = (yield* tag).rpc;
|
|
69
|
+
return yield* invokeRpc(stub, methodName, ...args);
|
|
70
|
+
});
|
|
71
|
+
const call = Effect.fnUntraced(function* (stub, methodName, ...args) {
|
|
72
|
+
const invokeCall = (yield* tag).call;
|
|
73
|
+
return yield* invokeCall(stub, methodName, ...args);
|
|
74
|
+
});
|
|
75
|
+
const scopedCall = Effect.fnUntraced(function* (stub, methodName, ...args) {
|
|
76
|
+
const invokeScopedCall = (yield* tag).scopedCall;
|
|
77
|
+
return yield* invokeScopedCall(stub, methodName, ...args);
|
|
78
|
+
});
|
|
79
|
+
const rawUnsafe = Effect.fnUntraced(function* () {
|
|
80
|
+
return yield* (yield* tag).rawUnsafe;
|
|
81
|
+
});
|
|
82
|
+
const directMethods = makeDirectMethods(definition, {
|
|
83
|
+
call,
|
|
84
|
+
fetch,
|
|
85
|
+
get,
|
|
86
|
+
getByName
|
|
87
|
+
});
|
|
88
|
+
return Object.assign(tag, directMethods, {
|
|
89
|
+
id: definition.id,
|
|
90
|
+
methods: definition.methods,
|
|
91
|
+
make: definition.make,
|
|
92
|
+
layer: layer$1,
|
|
93
|
+
newUniqueId,
|
|
94
|
+
idFromName,
|
|
95
|
+
idFromString,
|
|
96
|
+
get,
|
|
97
|
+
getByName,
|
|
98
|
+
jurisdiction,
|
|
99
|
+
fetch,
|
|
100
|
+
rpc,
|
|
101
|
+
call,
|
|
102
|
+
scopedCall,
|
|
103
|
+
rawUnsafe
|
|
104
|
+
});
|
|
105
|
+
};
|
|
106
|
+
const DurableObject = Tag;
|
|
107
|
+
const wrapHandlers = (definition, handlers) => {
|
|
108
|
+
const wrapped = Object.create(Object.prototype);
|
|
109
|
+
for (const key of Object.keys(definition.methods)) {
|
|
110
|
+
const handler = handlers[key];
|
|
111
|
+
wrapped[key] = (...args) => Effect.gen(function* () {
|
|
112
|
+
const decodedArgs = yield* decodeArgs(definition, key, args);
|
|
113
|
+
yield* recordDecodedArgs(decodedArgs);
|
|
114
|
+
const value = yield* handler(...decodedArgs);
|
|
115
|
+
return yield* encodeSuccess(definition, key, value);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
return wrapped;
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* Helper for implementing handlers with the exact method shape of a definition.
|
|
122
|
+
*/
|
|
123
|
+
const implement = (_definition, handlers) => handlers;
|
|
124
|
+
//#endregion
|
|
125
|
+
export { DurableObject, DurableObjectDefinition_exports, Tag, implement, make, method };
|