@midsummer-ai/sdk 0.1.0-staging.3428 → 0.1.0-staging.3434
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/auth-B7rCtXFQ.d.mts +36 -0
- package/{build-DQo-JrlN.d.mts → build-BhmOZmGQ.d.mts} +2 -27
- package/client.mjs +1 -1
- package/contract.mjs +2 -2
- package/database/build.d.mts +1 -1
- package/database/build.mjs +35 -2
- package/database/server.mjs +1 -1
- package/database/stand-in.d.mts +2 -1
- package/database/stand-in.mjs +2 -2
- package/email/client.d.mts +5 -4
- package/email/client.mjs +1 -1
- package/email/contract.d.mts +3 -1
- package/email/contract.mjs +111 -4
- package/email/index.d.mts +85 -16
- package/email/index.mjs +2 -2
- package/email/smtp.mjs +24 -8
- package/email/stand-in.d.mts +3 -0
- package/email/stand-in.mjs +17 -7
- package/email-IH5OllU2.mjs +337 -0
- package/errors-B1tO_wdm.mjs +28 -0
- package/flows/contract.d.mts +13 -0
- package/flows/contract.mjs +483 -0
- package/flows/index.d.mts +148 -0
- package/flows/index.mjs +347 -0
- package/index.mjs +2 -2
- package/{names-DfCBKupJ.mjs → names-Do0ot5Ha.mjs} +2 -28
- package/package.json +29 -1
- package/queues/contract.mjs +144 -1
- package/queues/index.d.mts +149 -5
- package/queues/index.mjs +56 -12
- package/queues/stand-in.mjs +333 -31
- package/queues/wire.mjs +302 -16
- package/s3.mjs +3 -2
- package/{server-EDRbF2u-.mjs → server-Drjni3-F.mjs} +4 -2
- package/socket.mjs +1 -1
- package/{src-BKrQAqJN.mjs → src-CkP59_Qu.mjs} +2 -1
- package/stand-in.mjs +2 -1
- package/storage/contract.d.mts +25 -0
- package/storage/contract.mjs +153 -0
- package/storage/files.d.mts +144 -0
- package/storage/files.mjs +513 -0
- package/wire.mjs +2 -1
- package/email-DXtpr7pT.mjs +0 -214
package/flows/index.mjs
ADDED
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
import { QueueError, bodyText, checkEvent, checkQueueName, checkStepName, checkToken, hereEngine, invalid, queues } from "../queues/index.mjs";
|
|
2
|
+
//#region src/flows/index.ts
|
|
3
|
+
/**
|
|
4
|
+
* Flows: work in steps that survives waits of minutes or months, retries each step on its own, and
|
|
5
|
+
* waits for a signal from outside, such as a person's approval. A Flow runs on the queue its
|
|
6
|
+
* environment binds under the Flow's name; each run is one message of that queue, kept with its steps.
|
|
7
|
+
*
|
|
8
|
+
* import { flow } from "@midsummer-ai/sdk/flows"
|
|
9
|
+
*
|
|
10
|
+
* export const onboarding = flow("onboarding", async (input: { userId: string }, step) => {
|
|
11
|
+
* const user = await step.run("load the user", () => users.get(input.userId))
|
|
12
|
+
* await step.run("send the welcome email", () => mail.welcome(user), { retries: 3 })
|
|
13
|
+
* await step.sleep("wait a day", 86_400)
|
|
14
|
+
* const answer = await step.waitFor<{ approved: boolean }>("approval", { timeoutSeconds: 3 * 86_400 })
|
|
15
|
+
* return answer === undefined ? { expired: true } : { approved: answer.approved }
|
|
16
|
+
* })
|
|
17
|
+
*
|
|
18
|
+
* const { id } = await onboarding.start({ userId }, { id: `onboarding-${userId}` })
|
|
19
|
+
* await onboarding.signal(id, "approval", { approved: true })
|
|
20
|
+
*
|
|
21
|
+
* Its runs are pushed to a path of your server: name the path in `midsummer.json`'s `consumers` for
|
|
22
|
+
* the Flow's queue, and serve it with `serveFlows`. Or pull them where you choose with `work`: a test,
|
|
23
|
+
* a scheduled path, or `midsummer dev`.
|
|
24
|
+
*
|
|
25
|
+
* app.post("/api/flows", (c) => serveFlows([onboarding])(c.req.raw))
|
|
26
|
+
*
|
|
27
|
+
* How a run goes. Each delivery calls the Flow's function from the top. A step already recorded
|
|
28
|
+
* returns what it returned without running again; the first step not recorded runs, and its result is
|
|
29
|
+
* recorded before the code goes on. So keep the function deterministic outside its steps: the same
|
|
30
|
+
* steps, in the same order, with the same names. A step's name used again in one run is a step of its
|
|
31
|
+
* own (`send`, then `send#2`).
|
|
32
|
+
*
|
|
33
|
+
* - `step.run` throws: the step is tried again after a backoff (10 seconds, doubling, at most 10
|
|
34
|
+
* minutes), `retries` times (4 unless the Flow or the step says), then it fails for good and throws
|
|
35
|
+
* `StepFailed` into the function, which may catch it and go on, such as to undo earlier steps. Not
|
|
36
|
+
* caught, the run fails.
|
|
37
|
+
* - `step.sleep` and `step.waitFor` end the delivery; the run's message is next delivered when it wakes,
|
|
38
|
+
* so a run that waits costs nothing until then. A signal sent before the run waits for it is kept
|
|
39
|
+
* for that wait. A wait that times out returns undefined.
|
|
40
|
+
* - Anything else the function throws fails the delivery, which is tried again with the backoff; after
|
|
41
|
+
* the queue's `maxAttempts` deliveries in a row without progress, the run fails.
|
|
42
|
+
* - A run cancelled, or past its `timeoutSeconds`, records nothing more: a delivery under way stops at
|
|
43
|
+
* its next step. A failed or cancelled run can be retried from its last recorded step
|
|
44
|
+
* (`midsummer queue run retry`).
|
|
45
|
+
*
|
|
46
|
+
* At least once: a step interrupted before its result is recorded runs again, so make the outside
|
|
47
|
+
* writes a step makes idempotent, with a key that stays the same across attempts, such as the run's id
|
|
48
|
+
* and the step's name. Don't catch `FlowPause` (what a sleep, a wait or a retry throws to end the
|
|
49
|
+
* delivery): rethrow anything you didn't expect. A run records at most 1,000 steps, 100 signals and
|
|
50
|
+
* 4 MiB of input, values and signals; each value is at most 128 KiB as JSON.
|
|
51
|
+
*/
|
|
52
|
+
/** What a step that failed for good throws into its Flow's function: `step` is its name, `message` the last error. */
|
|
53
|
+
var StepFailed = class extends Error {
|
|
54
|
+
step;
|
|
55
|
+
constructor(step, why) {
|
|
56
|
+
super(why);
|
|
57
|
+
this.step = step;
|
|
58
|
+
this.name = "StepFailed";
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
/** What a sleep, a wait or a step's retry throws to end a delivery. Never catch it: the run goes on at its next delivery. */
|
|
62
|
+
var FlowPause = class extends Error {
|
|
63
|
+
constructor() {
|
|
64
|
+
super("The run pauses here and goes on at its next delivery.");
|
|
65
|
+
this.name = "FlowPause";
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
const ended = (status) => status === "completed" || status === "failed" || status === "cancelled";
|
|
69
|
+
const parsed = (text) => text === void 0 ? void 0 : JSON.parse(text);
|
|
70
|
+
const whole = (n, low, high) => typeof n === "number" && Number.isSafeInteger(n) && n >= low && n <= high;
|
|
71
|
+
const messageOf = (error) => (error instanceof Error ? error.message || error.name : String(error)).slice(0, 2e3);
|
|
72
|
+
/** A run as code reads it: JSON parsed. */
|
|
73
|
+
function runOf(run) {
|
|
74
|
+
return {
|
|
75
|
+
id: run.id,
|
|
76
|
+
status: run.status,
|
|
77
|
+
input: JSON.parse(run.input),
|
|
78
|
+
startedAt: run.startedAt,
|
|
79
|
+
...run.output !== void 0 ? { output: JSON.parse(run.output) } : {},
|
|
80
|
+
...run.error !== void 0 ? { error: run.error } : {},
|
|
81
|
+
...run.endedAt !== void 0 ? { endedAt: run.endedAt } : {},
|
|
82
|
+
...run.nextAt !== void 0 ? { nextAt: run.nextAt } : {},
|
|
83
|
+
...run.deadline !== void 0 ? { deadline: run.deadline } : {},
|
|
84
|
+
...run.group !== void 0 ? { group: run.group } : {},
|
|
85
|
+
steps: run.steps.map(({ value, ...step }) => ({
|
|
86
|
+
...step,
|
|
87
|
+
...value !== void 0 ? { value: JSON.parse(value) } : {}
|
|
88
|
+
}))
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/** Ends a delivery because the run, or the delivery's receipt, has ended. */
|
|
92
|
+
var RunOver = class extends Error {};
|
|
93
|
+
/** Fails the run for good: something no retry can help, such as a value too large to keep. */
|
|
94
|
+
var RunFails = class extends Error {};
|
|
95
|
+
/**
|
|
96
|
+
* Runs one delivery of run `runId` of the Flow on queue `name`: loads what the run recorded, calls
|
|
97
|
+
* `fn` from the top with steps that return what was recorded, and records what it does next.
|
|
98
|
+
*/
|
|
99
|
+
async function execute(engine, name, fn, options, delivery) {
|
|
100
|
+
const { run: runId, receipt } = delivery;
|
|
101
|
+
const loaded = await engine.load(name, runId, receipt);
|
|
102
|
+
if (!loaded) return { kind: "stale" };
|
|
103
|
+
if (ended(loaded.run.status)) return { kind: "ended" };
|
|
104
|
+
const clock = options.clock ?? Date.now;
|
|
105
|
+
const begun = clock();
|
|
106
|
+
const budget = options.budgetMs ?? 2e4;
|
|
107
|
+
const steps = new Map(loaded.run.steps.map((step) => [step.id, step]));
|
|
108
|
+
const uses = /* @__PURE__ */ new Map();
|
|
109
|
+
let stopped;
|
|
110
|
+
const stop = (why) => {
|
|
111
|
+
throw stopped = why;
|
|
112
|
+
};
|
|
113
|
+
const idOf = (stepName) => {
|
|
114
|
+
if (stopped) throw stopped;
|
|
115
|
+
checkStepName(stepName);
|
|
116
|
+
const n = (uses.get(stepName) ?? 0) + 1;
|
|
117
|
+
uses.set(stepName, n);
|
|
118
|
+
return n === 1 ? stepName : `${stepName}#${n}`;
|
|
119
|
+
};
|
|
120
|
+
const record = async (write) => {
|
|
121
|
+
const kept = await engine.record(name, runId, receipt, write);
|
|
122
|
+
if (!kept) return stop(new RunOver());
|
|
123
|
+
steps.set(kept.id, kept);
|
|
124
|
+
return kept;
|
|
125
|
+
};
|
|
126
|
+
/** Before a step's work runs: a delivery past its budget hands the run to its next delivery, at once. */
|
|
127
|
+
const paced = async () => {
|
|
128
|
+
if (clock() - begun < budget) return;
|
|
129
|
+
await engine.retry(name, receipt, 0);
|
|
130
|
+
stop(new FlowPause());
|
|
131
|
+
};
|
|
132
|
+
const retriesOf = (asked) => {
|
|
133
|
+
const retries = asked ?? options.retries ?? 4;
|
|
134
|
+
if (!whole(retries, 0, 100)) invalid("a step's retries are 0 to 100");
|
|
135
|
+
return retries;
|
|
136
|
+
};
|
|
137
|
+
const tools = {
|
|
138
|
+
async run(stepName, work, stepOptions) {
|
|
139
|
+
const id = idOf(stepName);
|
|
140
|
+
const retries = retriesOf(stepOptions?.retries);
|
|
141
|
+
const held = steps.get(id);
|
|
142
|
+
if (held?.state === "done") return parsed(held.value);
|
|
143
|
+
if (held?.state === "failed") throw new StepFailed(stepName, held.error ?? "It failed.");
|
|
144
|
+
await paced();
|
|
145
|
+
let value;
|
|
146
|
+
try {
|
|
147
|
+
value = await work();
|
|
148
|
+
} catch (error) {
|
|
149
|
+
if (error instanceof FlowPause || error instanceof RunOver) throw error;
|
|
150
|
+
const kept = await record({
|
|
151
|
+
kind: "run",
|
|
152
|
+
id,
|
|
153
|
+
error: messageOf(error),
|
|
154
|
+
retries
|
|
155
|
+
});
|
|
156
|
+
if (kept.state === "done") return parsed(kept.value);
|
|
157
|
+
if (kept.state === "failed") throw new StepFailed(stepName, kept.error ?? messageOf(error));
|
|
158
|
+
return stop(new FlowPause());
|
|
159
|
+
}
|
|
160
|
+
let text;
|
|
161
|
+
try {
|
|
162
|
+
text = bodyText(value === void 0 ? null : value);
|
|
163
|
+
} catch (error) {
|
|
164
|
+
throw new RunFails(`Step "${stepName}" returned ${error instanceof Error ? error.message : "something that is not JSON"}.`);
|
|
165
|
+
}
|
|
166
|
+
const kept = await record({
|
|
167
|
+
kind: "run",
|
|
168
|
+
id,
|
|
169
|
+
value: text
|
|
170
|
+
});
|
|
171
|
+
if (kept.state === "failed") throw new StepFailed(stepName, kept.error ?? "It failed.");
|
|
172
|
+
return parsed(kept.value);
|
|
173
|
+
},
|
|
174
|
+
async sleep(stepName, seconds) {
|
|
175
|
+
const id = idOf(stepName);
|
|
176
|
+
if (!whole(seconds, 0, 31536e3)) invalid("a sleep is a whole number of seconds, 0 to 31,536,000 (365 days)");
|
|
177
|
+
if (steps.get(id)?.state === "done") return;
|
|
178
|
+
if ((await record({
|
|
179
|
+
kind: "sleep",
|
|
180
|
+
id,
|
|
181
|
+
ms: seconds * 1e3
|
|
182
|
+
})).state !== "done") stop(new FlowPause());
|
|
183
|
+
},
|
|
184
|
+
async sleepUntil(stepName, at) {
|
|
185
|
+
const id = idOf(stepName);
|
|
186
|
+
const when = typeof at === "number" ? at : at.getTime();
|
|
187
|
+
if (!Number.isFinite(when)) invalid("a sleep's time is a date");
|
|
188
|
+
if (steps.get(id)?.state === "done") return;
|
|
189
|
+
const ms = Math.max(0, Math.ceil(when - loaded.now));
|
|
190
|
+
if (ms > 31536e6) invalid("a sleep is at most 365 days");
|
|
191
|
+
if ((await record({
|
|
192
|
+
kind: "sleep",
|
|
193
|
+
id,
|
|
194
|
+
ms
|
|
195
|
+
})).state !== "done") stop(new FlowPause());
|
|
196
|
+
},
|
|
197
|
+
async waitFor(stepName, waitOptions) {
|
|
198
|
+
const id = idOf(stepName);
|
|
199
|
+
const event = waitOptions.event ?? stepName;
|
|
200
|
+
checkEvent(event);
|
|
201
|
+
if (!whole(waitOptions.timeoutSeconds, 1, 31536e3)) invalid("a wait's timeout is 1 to 31,536,000 seconds (365 days)");
|
|
202
|
+
const held = steps.get(id);
|
|
203
|
+
if (held?.state === "done") return parsed(held.value);
|
|
204
|
+
const kept = await record({
|
|
205
|
+
kind: "wait",
|
|
206
|
+
id,
|
|
207
|
+
event,
|
|
208
|
+
timeoutMs: waitOptions.timeoutSeconds * 1e3
|
|
209
|
+
});
|
|
210
|
+
if (kept.state !== "done") return stop(new FlowPause());
|
|
211
|
+
return parsed(kept.value);
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
let output;
|
|
215
|
+
try {
|
|
216
|
+
output = await fn(JSON.parse(loaded.run.input), tools);
|
|
217
|
+
} catch (error) {
|
|
218
|
+
if (stopped instanceof RunOver || error instanceof RunOver) return { kind: "ended" };
|
|
219
|
+
if (stopped instanceof FlowPause || error instanceof FlowPause) return { kind: "paused" };
|
|
220
|
+
if (error instanceof StepFailed || error instanceof RunFails) {
|
|
221
|
+
await engine.finish(name, runId, receipt, { error: error instanceof StepFailed ? `Step "${error.step}" failed: ${error.message}` : error.message });
|
|
222
|
+
return { kind: "ended" };
|
|
223
|
+
}
|
|
224
|
+
return {
|
|
225
|
+
kind: "retry",
|
|
226
|
+
why: messageOf(error)
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
if (stopped instanceof FlowPause) return { kind: "paused" };
|
|
230
|
+
if (stopped instanceof RunOver) return { kind: "ended" };
|
|
231
|
+
let text;
|
|
232
|
+
try {
|
|
233
|
+
text = bodyText(output === void 0 ? null : output);
|
|
234
|
+
} catch (error) {
|
|
235
|
+
await engine.finish(name, runId, receipt, { error: `It returned ${error instanceof Error ? error.message : "something that is not JSON"}.` });
|
|
236
|
+
return { kind: "ended" };
|
|
237
|
+
}
|
|
238
|
+
await engine.finish(name, runId, receipt, { output: text });
|
|
239
|
+
return { kind: "ended" };
|
|
240
|
+
}
|
|
241
|
+
/** A fresh run id: random, so two starts without an id make two runs. */
|
|
242
|
+
const freshId = () => `run_${crypto.randomUUID().replaceAll("-", "")}`;
|
|
243
|
+
/** Flows over `engine`: the Stand-in's in tests (`flows(standIn({ clock }))`), or `hereEngine()`. */
|
|
244
|
+
function flows(engine, runner = {}) {
|
|
245
|
+
return { flow(name, fn, options = {}) {
|
|
246
|
+
checkQueueName(name);
|
|
247
|
+
if (options.retries !== void 0 && !whole(options.retries, 0, 100)) invalid("a Flow's retries are 0 to 100");
|
|
248
|
+
const handle = (delivery) => execute(engine, name, fn, {
|
|
249
|
+
...options,
|
|
250
|
+
...runner
|
|
251
|
+
}, delivery);
|
|
252
|
+
const missing = (id) => {
|
|
253
|
+
throw new QueueError("not-found", `${name} has no run ${id}`);
|
|
254
|
+
};
|
|
255
|
+
return {
|
|
256
|
+
name,
|
|
257
|
+
async start(input, startOptions = {}) {
|
|
258
|
+
const id = startOptions.id ?? freshId();
|
|
259
|
+
checkToken(id, "a run's id");
|
|
260
|
+
if (startOptions.group !== void 0) checkToken(startOptions.group, "a group");
|
|
261
|
+
if (startOptions.timeoutSeconds !== void 0 && !whole(startOptions.timeoutSeconds, 1, 31536e3)) invalid("a run's timeout is 1 to 31,536,000 seconds (365 days)");
|
|
262
|
+
return engine.start(name, {
|
|
263
|
+
id,
|
|
264
|
+
input: bodyText(input),
|
|
265
|
+
...startOptions.timeoutSeconds !== void 0 ? { timeoutMs: startOptions.timeoutSeconds * 1e3 } : {},
|
|
266
|
+
...startOptions.group !== void 0 ? { group: startOptions.group } : {}
|
|
267
|
+
});
|
|
268
|
+
},
|
|
269
|
+
async signal(id, event, value = null, signalOptions = {}) {
|
|
270
|
+
checkToken(id, "a run's id");
|
|
271
|
+
checkEvent(event);
|
|
272
|
+
if (signalOptions.key !== void 0) checkToken(signalOptions.key, "a signal's key");
|
|
273
|
+
const state = await engine.signal(name, id, {
|
|
274
|
+
event,
|
|
275
|
+
value: bodyText(value),
|
|
276
|
+
...signalOptions.key !== void 0 ? { key: signalOptions.key } : {}
|
|
277
|
+
});
|
|
278
|
+
return state === "missing" ? missing(id) : state;
|
|
279
|
+
},
|
|
280
|
+
async cancel(id) {
|
|
281
|
+
checkToken(id, "a run's id");
|
|
282
|
+
const run = await engine.cancel(name, id);
|
|
283
|
+
return run ? runOf(run) : missing(id);
|
|
284
|
+
},
|
|
285
|
+
async show(id) {
|
|
286
|
+
checkToken(id, "a run's id");
|
|
287
|
+
const run = await engine.run(name, id);
|
|
288
|
+
return run && runOf(run);
|
|
289
|
+
},
|
|
290
|
+
async work(workOptions = {}) {
|
|
291
|
+
const deliveries = await queues(engine).queue(name).receive({
|
|
292
|
+
limit: workOptions.limit ?? 10,
|
|
293
|
+
visibilitySeconds: workOptions.visibilitySeconds ?? 30,
|
|
294
|
+
maxAttempts: workOptions.maxAttempts ?? 5
|
|
295
|
+
});
|
|
296
|
+
await Promise.all(deliveries.map(async (delivery) => {
|
|
297
|
+
if (delivery.run === void 0) return engine.retry(name, delivery.receipt);
|
|
298
|
+
if ((await handle({
|
|
299
|
+
run: delivery.run,
|
|
300
|
+
receipt: delivery.receipt
|
|
301
|
+
})).kind === "retry") await engine.retry(name, delivery.receipt);
|
|
302
|
+
}));
|
|
303
|
+
return deliveries.length;
|
|
304
|
+
},
|
|
305
|
+
handle
|
|
306
|
+
};
|
|
307
|
+
} };
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* The Flow `name`, on the queue the environment binds under that name, from code in a deployment in
|
|
311
|
+
* any framework, or `midsummer dev`'s queue of that name.
|
|
312
|
+
*/
|
|
313
|
+
function flow(name, fn, options) {
|
|
314
|
+
return flows(hereEngine()).flow(name, fn, options);
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* A request handler for the path a Flow's queue pushes its runs to (`midsummer.json`'s `consumers`):
|
|
318
|
+
* it takes only deliveries Midsummer checked (their `Midsummer-Queue` header is the queue's
|
|
319
|
+
* binding name), runs the run's Flow, and returns 204 once the delivery is done or paused, or 500 to have it
|
|
320
|
+
* tried again. One path can serve several Flows.
|
|
321
|
+
*/
|
|
322
|
+
function serveFlows(list) {
|
|
323
|
+
const text = (body, status) => new Response(body, {
|
|
324
|
+
status,
|
|
325
|
+
headers: { "content-type": "text/plain; charset=utf-8" }
|
|
326
|
+
});
|
|
327
|
+
return async (request) => {
|
|
328
|
+
const binding = request.headers.get("midsummer-queue");
|
|
329
|
+
if (!binding || request.method !== "POST") return text("Not a delivery.", 403);
|
|
330
|
+
const named = list.find((one) => one.name === binding);
|
|
331
|
+
if (!named) return text(`No flow named ${binding} is served here.`, 404);
|
|
332
|
+
let delivery;
|
|
333
|
+
try {
|
|
334
|
+
delivery = await request.json();
|
|
335
|
+
} catch {
|
|
336
|
+
return text("The delivery is not JSON.", 400);
|
|
337
|
+
}
|
|
338
|
+
if (typeof delivery.run !== "string" || typeof delivery.receipt !== "string") return text("This message is no run: start runs with the Flow's start.", 422);
|
|
339
|
+
const outcome = await named.handle({
|
|
340
|
+
run: delivery.run,
|
|
341
|
+
receipt: delivery.receipt
|
|
342
|
+
});
|
|
343
|
+
return outcome.kind === "retry" ? text(outcome.why, 500) : new Response(null, { status: 204 });
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
//#endregion
|
|
347
|
+
export { FlowPause, StepFailed, flow, flows, serveFlows };
|
package/index.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { n as storage, r as streamsOf, t as bucket } from "./src-
|
|
1
|
+
import { t as StorageError } from "./errors-B1tO_wdm.mjs";
|
|
2
|
+
import { n as storage, r as streamsOf, t as bucket } from "./src-CkP59_Qu.mjs";
|
|
3
3
|
export { StorageError, bucket, storage, streamsOf };
|
|
@@ -1,30 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
const storageErrorCodes = [
|
|
3
|
-
"invalid",
|
|
4
|
-
"not_found",
|
|
5
|
-
"conflict",
|
|
6
|
-
"forbidden",
|
|
7
|
-
"quota",
|
|
8
|
-
"unavailable"
|
|
9
|
-
];
|
|
10
|
-
const storageErrorReasons = [
|
|
11
|
-
"range",
|
|
12
|
-
"upload",
|
|
13
|
-
"part",
|
|
14
|
-
"part-order",
|
|
15
|
-
"part-too-small"
|
|
16
|
-
];
|
|
17
|
-
var StorageError = class extends Error {
|
|
18
|
-
code;
|
|
19
|
-
reason;
|
|
20
|
-
constructor(code, why, reason) {
|
|
21
|
-
super(why);
|
|
22
|
-
this.code = code;
|
|
23
|
-
this.reason = reason;
|
|
24
|
-
this.name = "StorageError";
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
//#endregion
|
|
1
|
+
import { t as StorageError } from "./errors-B1tO_wdm.mjs";
|
|
28
2
|
//#region src/names.ts
|
|
29
3
|
function invalid(why) {
|
|
30
4
|
throw new StorageError("invalid", why);
|
|
@@ -112,4 +86,4 @@ function sameSizes(sizes, named) {
|
|
|
112
86
|
for (let i = 1; i < sizes.length; i += 1) if (i < sizes.length - 1 ? sizes[i] !== sizes[0] : sizes[i] > sizes[0]) throw new StorageError("invalid", `part ${named[i].partNumber} is ${sizes[i]} bytes: every part but the last must be ${sizes[0]} bytes, as the first is, and the last no larger`, "part");
|
|
113
87
|
}
|
|
114
88
|
//#endregion
|
|
115
|
-
export {
|
|
89
|
+
export { checkMetadata as a, checkRange as c, invalid as d, resolveRange as f, checkListLimit as i, checkUploadId as l, sha256 as m, checkBucketName as n, checkPartNumber as o, sameSizes as p, checkKey as r, checkParts as s, MIN_PART as t, compareKeys as u };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midsummer-ai/sdk",
|
|
3
|
-
"version": "0.1.0-staging.
|
|
3
|
+
"version": "0.1.0-staging.3434",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Midsummer's handles for an app's code: its Database, Storage, Queues, Vector DB, AI, Email and Agents, with stand-ins for tests.",
|
|
@@ -107,6 +107,14 @@
|
|
|
107
107
|
"types": "./queues/wire.d.mts",
|
|
108
108
|
"import": "./queues/wire.mjs"
|
|
109
109
|
},
|
|
110
|
+
"./flows": {
|
|
111
|
+
"types": "./flows/index.d.mts",
|
|
112
|
+
"import": "./flows/index.mjs"
|
|
113
|
+
},
|
|
114
|
+
"./flows/contract": {
|
|
115
|
+
"types": "./flows/contract.d.mts",
|
|
116
|
+
"import": "./flows/contract.mjs"
|
|
117
|
+
},
|
|
110
118
|
"./email": {
|
|
111
119
|
"types": "./email/index.d.mts",
|
|
112
120
|
"import": "./email/index.mjs"
|
|
@@ -147,6 +155,14 @@
|
|
|
147
155
|
"types": "./vector/wire.d.mts",
|
|
148
156
|
"import": "./vector/wire.mjs"
|
|
149
157
|
},
|
|
158
|
+
"./files": {
|
|
159
|
+
"types": "./storage/files.d.mts",
|
|
160
|
+
"import": "./storage/files.mjs"
|
|
161
|
+
},
|
|
162
|
+
"./files/contract": {
|
|
163
|
+
"types": "./storage/contract.d.mts",
|
|
164
|
+
"import": "./storage/contract.mjs"
|
|
165
|
+
},
|
|
150
166
|
"./flags": {
|
|
151
167
|
"types": "./flags.d.mts",
|
|
152
168
|
"import": "./flags.mjs"
|
|
@@ -228,6 +244,12 @@
|
|
|
228
244
|
"queues/wire": [
|
|
229
245
|
"./queues/wire.d.mts"
|
|
230
246
|
],
|
|
247
|
+
"flows": [
|
|
248
|
+
"./flows/index.d.mts"
|
|
249
|
+
],
|
|
250
|
+
"flows/contract": [
|
|
251
|
+
"./flows/contract.d.mts"
|
|
252
|
+
],
|
|
231
253
|
"email": [
|
|
232
254
|
"./email/index.d.mts"
|
|
233
255
|
],
|
|
@@ -258,6 +280,12 @@
|
|
|
258
280
|
"vector/wire": [
|
|
259
281
|
"./vector/wire.d.mts"
|
|
260
282
|
],
|
|
283
|
+
"files": [
|
|
284
|
+
"./storage/files.d.mts"
|
|
285
|
+
],
|
|
286
|
+
"files/contract": [
|
|
287
|
+
"./storage/contract.d.mts"
|
|
288
|
+
],
|
|
261
289
|
"flags": [
|
|
262
290
|
"./flags.d.mts"
|
|
263
291
|
],
|
package/queues/contract.mjs
CHANGED
|
@@ -230,6 +230,146 @@ const queuesContract = [
|
|
|
230
230
|
equal((await queues.queue("a").receive()).map((d) => d.body), ["for a"], "deliveries from a");
|
|
231
231
|
}
|
|
232
232
|
},
|
|
233
|
+
{
|
|
234
|
+
name: "a Message delayed for days is received only once the delay is over",
|
|
235
|
+
async run({ queues, advance }) {
|
|
236
|
+
const q = queues.queue("renewals");
|
|
237
|
+
await q.send("renew", { delaySeconds: 2592e3 });
|
|
238
|
+
advance(2592e3 * SECOND - SECOND);
|
|
239
|
+
equal((await q.receive()).length, 0, "deliveries a second before 30 days");
|
|
240
|
+
advance(SECOND);
|
|
241
|
+
equal((await q.receive()).map((d) => d.body), ["renew"], "deliveries at 30 days");
|
|
242
|
+
}
|
|
243
|
+
},
|
|
244
|
+
{
|
|
245
|
+
name: "Messages of one group are delivered one at a time in the order sent, while other groups go on",
|
|
246
|
+
async run({ queues }) {
|
|
247
|
+
const q = queues.queue("ledger");
|
|
248
|
+
await q.sendBatch([
|
|
249
|
+
{
|
|
250
|
+
body: "a1",
|
|
251
|
+
group: "a"
|
|
252
|
+
},
|
|
253
|
+
{
|
|
254
|
+
body: "a2",
|
|
255
|
+
group: "a"
|
|
256
|
+
},
|
|
257
|
+
{
|
|
258
|
+
body: "b1",
|
|
259
|
+
group: "b"
|
|
260
|
+
},
|
|
261
|
+
{ body: "free" },
|
|
262
|
+
{
|
|
263
|
+
body: "a3",
|
|
264
|
+
group: "a"
|
|
265
|
+
}
|
|
266
|
+
]);
|
|
267
|
+
const first = await q.receive({ limit: 10 });
|
|
268
|
+
equal(first.map((d) => d.body), [
|
|
269
|
+
"a1",
|
|
270
|
+
"b1",
|
|
271
|
+
"free"
|
|
272
|
+
], "the first receive: the head of each group, and the Message with none");
|
|
273
|
+
equal(await q.stats(), {
|
|
274
|
+
depth: 5,
|
|
275
|
+
ready: 0,
|
|
276
|
+
inFlight: 3,
|
|
277
|
+
delayed: 2,
|
|
278
|
+
oldestSentAt: Date.UTC(2026, 9, 27, 12),
|
|
279
|
+
dead: 0
|
|
280
|
+
}, "the stats: the group's later ones wait");
|
|
281
|
+
await q.ack(first[0].receipt);
|
|
282
|
+
equal((await q.receive({ limit: 10 })).map((d) => d.body), ["a2"], "after a1 is acked, a2 and nothing else");
|
|
283
|
+
}
|
|
284
|
+
},
|
|
285
|
+
{
|
|
286
|
+
name: "a retried Message keeps its group's place: the next is not delivered before it, whatever its backoff",
|
|
287
|
+
async run({ queues, advance }) {
|
|
288
|
+
const q = queues.queue("ledger");
|
|
289
|
+
await q.sendBatch([{
|
|
290
|
+
body: 1,
|
|
291
|
+
group: "acct-7"
|
|
292
|
+
}, {
|
|
293
|
+
body: 2,
|
|
294
|
+
group: "acct-7"
|
|
295
|
+
}]);
|
|
296
|
+
await q.retry((await q.receive())[0].receipt);
|
|
297
|
+
equal((await q.receive()).length, 0, "deliveries while the first waits for its retry");
|
|
298
|
+
advance(10 * SECOND);
|
|
299
|
+
const [again] = await q.receive();
|
|
300
|
+
equal([again?.body, again?.attempt], [1, 2], "the first, delivered again");
|
|
301
|
+
await q.ack(again.receipt);
|
|
302
|
+
equal((await q.receive()).map((d) => d.body), [2], "then the second");
|
|
303
|
+
}
|
|
304
|
+
},
|
|
305
|
+
{
|
|
306
|
+
name: "a grouped Message whose window ends unacked is delivered again before the group's next",
|
|
307
|
+
async run({ queues, advance }) {
|
|
308
|
+
const q = queues.queue("ledger");
|
|
309
|
+
await q.sendBatch([{
|
|
310
|
+
body: "first",
|
|
311
|
+
group: "g"
|
|
312
|
+
}, {
|
|
313
|
+
body: "second",
|
|
314
|
+
group: "g"
|
|
315
|
+
}]);
|
|
316
|
+
await q.receive({ visibilitySeconds: 5 });
|
|
317
|
+
advance(5 * SECOND);
|
|
318
|
+
equal((await q.receive()).map((d) => [d.body, d.attempt]), [["first", 2]], "the first again, not the second");
|
|
319
|
+
}
|
|
320
|
+
},
|
|
321
|
+
{
|
|
322
|
+
name: "a grouped Message moved to the dead-letter Queue lets its group's next one go",
|
|
323
|
+
async run({ queues }) {
|
|
324
|
+
const q = queues.queue("ledger");
|
|
325
|
+
await q.sendBatch([{
|
|
326
|
+
body: "poison",
|
|
327
|
+
group: "g"
|
|
328
|
+
}, {
|
|
329
|
+
body: "next",
|
|
330
|
+
group: "g"
|
|
331
|
+
}]);
|
|
332
|
+
await q.retry((await q.receive({ maxAttempts: 1 }))[0].receipt);
|
|
333
|
+
equal((await q.receive()).map((d) => d.body), ["next"], "the group's next one");
|
|
334
|
+
equal((await q.deadLetter.receive()).map((d) => d.body), ["poison"], "the dead-letter Queue");
|
|
335
|
+
}
|
|
336
|
+
},
|
|
337
|
+
{
|
|
338
|
+
name: "a duplicate keyed send to a group takes no place in it",
|
|
339
|
+
async run({ queues }) {
|
|
340
|
+
const q = queues.queue("ledger");
|
|
341
|
+
const one = await q.send("charge", {
|
|
342
|
+
key: "charge-1",
|
|
343
|
+
group: "g"
|
|
344
|
+
});
|
|
345
|
+
const again = await q.send("charge", {
|
|
346
|
+
key: "charge-1",
|
|
347
|
+
group: "g"
|
|
348
|
+
});
|
|
349
|
+
await q.send("refund", { group: "g" });
|
|
350
|
+
equal(again.id, one.id, "the second send's id");
|
|
351
|
+
const [first] = await q.receive();
|
|
352
|
+
equal(first?.body, "charge", "the first delivery");
|
|
353
|
+
await q.ack(first.receipt);
|
|
354
|
+
equal((await q.receive()).map((d) => d.body), ["refund"], "the next delivery");
|
|
355
|
+
equal((await q.stats()).depth, 1, "the depth: one charge, one refund, the charge acked");
|
|
356
|
+
}
|
|
357
|
+
},
|
|
358
|
+
{
|
|
359
|
+
name: "extending a delivery keeps it hidden past its first window, and an ended receipt extends nothing",
|
|
360
|
+
async run({ queues, advance }) {
|
|
361
|
+
const q = queues.queue("reports");
|
|
362
|
+
await q.send("big");
|
|
363
|
+
const [delivery] = await q.receive({ visibilitySeconds: 30 });
|
|
364
|
+
advance(20 * SECOND);
|
|
365
|
+
equal(await q.extend(delivery.receipt, { visibilitySeconds: 60 }), Date.UTC(2026, 9, 27, 12) + 80 * SECOND, "the window's new end");
|
|
366
|
+
advance(40 * SECOND);
|
|
367
|
+
equal((await q.receive()).length, 0, "deliveries 60 seconds in, inside the extended window");
|
|
368
|
+
advance(20 * SECOND);
|
|
369
|
+
equal(await q.extend(delivery.receipt), void 0, "extending once the window has ended");
|
|
370
|
+
equal((await q.receive()).map((d) => d.attempt), [2], "the delivery after it");
|
|
371
|
+
}
|
|
372
|
+
},
|
|
233
373
|
{
|
|
234
374
|
name: "a bad name, body, delay, limit or receipt is invalid",
|
|
235
375
|
async run({ queues }) {
|
|
@@ -238,7 +378,10 @@ const queuesContract = [
|
|
|
238
378
|
const q = queues.queue("ok");
|
|
239
379
|
await rejects(q.send(void 0), "an undefined body");
|
|
240
380
|
await rejects(q.send("x".repeat(131071)), "a body over 128 KiB as JSON");
|
|
241
|
-
await rejects(q.send("x", { delaySeconds:
|
|
381
|
+
await rejects(q.send("x", { delaySeconds: 31536001 }), "a delay over 365 days");
|
|
382
|
+
await rejects(q.send("x", { group: "has space" }), "a group with a space");
|
|
383
|
+
await rejects(q.extend("not a receipt"), "extending a receipt with spaces");
|
|
384
|
+
await rejects(q.extend("a.b", { visibilitySeconds: 43201 }), "extending past 12 hours");
|
|
242
385
|
await rejects(q.send("x", { key: "" }), "an empty key");
|
|
243
386
|
await rejects(q.receive({ limit: 101 }), "a limit over 100");
|
|
244
387
|
await rejects(q.receive({ visibilitySeconds: 0 }), "a visibility of 0");
|