logic-sdk 0.7.0 → 0.7.2
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/README.md +9 -9
- package/dist/adapters/cloudflare-queue.d.ts +14 -0
- package/dist/adapters/cloudflare-queue.js +48 -0
- package/dist/adapters/drizzle-pg.d.ts +250 -0
- package/dist/adapters/drizzle-pg.js +69 -0
- package/dist/adapters/drizzle-sqlite.d.ts +257 -0
- package/dist/adapters/drizzle-sqlite.js +196 -0
- package/dist/adapters/pg-boss.d.ts +45 -0
- package/dist/adapters/pg-boss.js +180 -0
- package/dist/adapters/postgres.d.ts +32 -0
- package/dist/adapters/postgres.js +105 -0
- package/dist/adapters/sqlite.d.ts +13 -0
- package/dist/adapters/sqlite.js +88 -0
- package/dist/{stored-jobs.d.ts → adapters/stored-jobs.d.ts} +27 -10
- package/dist/{stored-jobs.js → adapters/stored-jobs.js} +106 -20
- package/dist/ai.d.ts +1 -32
- package/dist/ai.js +1 -40
- package/dist/cloudflare.d.ts +2 -55
- package/dist/cloudflare.js +2 -85
- package/dist/core/in-process.d.ts +9 -0
- package/dist/core/in-process.js +17 -0
- package/dist/core/jobs.d.ts +57 -0
- package/dist/{jobs.js → core/jobs.js} +161 -42
- package/dist/core/registry.d.ts +120 -0
- package/dist/core/registry.js +503 -0
- package/dist/core/testing.d.ts +75 -0
- package/dist/core/testing.js +230 -0
- package/dist/{types.d.ts → core/types.d.ts} +224 -49
- package/dist/drizzle/pg.d.ts +3 -236
- package/dist/drizzle/pg.js +2 -67
- package/dist/drizzle/sqlite.d.ts +3 -242
- package/dist/drizzle/sqlite.js +2 -194
- package/dist/fetch.d.ts +1 -19
- package/dist/fetch.js +1 -30
- package/dist/hosts/cloudflare.d.ts +42 -0
- package/dist/hosts/cloudflare.js +39 -0
- package/dist/hosts/node.d.ts +26 -0
- package/dist/hosts/node.js +76 -0
- package/dist/hosts/pg-boss.d.ts +22 -0
- package/dist/hosts/pg-boss.js +55 -0
- package/dist/index.d.ts +5 -124
- package/dist/index.js +4 -495
- package/dist/integrations/ai.d.ts +32 -0
- package/dist/integrations/ai.js +40 -0
- package/dist/integrations/fetch.d.ts +19 -0
- package/dist/integrations/fetch.js +30 -0
- package/dist/integrations/orpc.d.ts +22 -0
- package/dist/integrations/orpc.js +37 -0
- package/dist/node.d.ts +1 -26
- package/dist/node.js +1 -76
- package/dist/orpc.d.ts +1 -21
- package/dist/orpc.js +1 -37
- package/dist/pg-boss.d.ts +2 -55
- package/dist/pg-boss.js +2 -211
- package/dist/postgres.d.ts +2 -28
- package/dist/postgres.js +1 -82
- package/dist/sqlite.d.ts +2 -9
- package/dist/sqlite.js +1 -65
- package/dist/testing.d.ts +1 -67
- package/dist/testing.js +1 -214
- package/package.json +2 -2
- package/skills/logic-sdk/SKILL.md +7 -6
- package/src/adapters/cloudflare-queue.ts +56 -0
- package/src/adapters/drizzle-pg.ts +108 -0
- package/src/adapters/drizzle-sqlite.ts +234 -0
- package/src/adapters/pg-boss.ts +195 -0
- package/src/adapters/postgres.ts +112 -0
- package/src/adapters/sqlite.ts +96 -0
- package/src/{stored-jobs.ts → adapters/stored-jobs.ts} +133 -30
- package/src/ai.ts +1 -62
- package/src/cloudflare.ts +2 -107
- package/src/core/in-process.ts +18 -0
- package/src/{jobs.ts → core/jobs.ts} +166 -43
- package/src/core/registry.ts +671 -0
- package/src/core/testing.ts +307 -0
- package/src/{types.ts → core/types.ts} +219 -46
- package/src/drizzle/pg.ts +3 -110
- package/src/drizzle/sqlite.ts +3 -236
- package/src/fetch.ts +1 -31
- package/src/hosts/cloudflare.ts +54 -0
- package/src/hosts/node.ts +99 -0
- package/src/hosts/pg-boss.ts +71 -0
- package/src/index.ts +17 -655
- package/src/integrations/ai.ts +63 -0
- package/src/integrations/fetch.ts +31 -0
- package/src/integrations/orpc.ts +47 -0
- package/src/node.ts +1 -99
- package/src/orpc.ts +1 -46
- package/src/pg-boss.ts +2 -242
- package/src/postgres.ts +2 -90
- package/src/sqlite.ts +2 -74
- package/src/testing.ts +1 -288
- package/dist/jobs.d.ts +0 -44
- /package/dist/{errors.d.ts → core/errors.d.ts} +0 -0
- /package/dist/{errors.js → core/errors.js} +0 -0
- /package/dist/{schedule.d.ts → core/schedule.d.ts} +0 -0
- /package/dist/{schedule.js → core/schedule.js} +0 -0
- /package/dist/{types.js → core/types.js} +0 -0
- /package/src/{errors.ts → core/errors.ts} +0 -0
- /package/src/{schedule.ts → core/schedule.ts} +0 -0
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import {} from "./types.js";
|
|
2
|
+
import { deliveryOf, replaceKeyOf } from "./jobs.js";
|
|
3
|
+
import { INTERNAL } from "./types.js";
|
|
4
|
+
class Rollback extends Error {
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* The test's jobs adapter. What waits (a delayed listener, `ctx.schedule`) is held for `drain()`, and in `"manual"` so
|
|
8
|
+
* is everything else; the rest runs now. It holds and replaces what your app's adapter does, so a test fails where
|
|
9
|
+
* production would.
|
|
10
|
+
*/
|
|
11
|
+
const testJobs = (mode, app, queued, scheduled) => ({
|
|
12
|
+
name: app?.name,
|
|
13
|
+
holds: app?.holds,
|
|
14
|
+
replaces: app?.replaces,
|
|
15
|
+
retries: app?.retries,
|
|
16
|
+
async deliver(jobs, runner) {
|
|
17
|
+
for (const job of jobs) {
|
|
18
|
+
const key = replaceKeyOf(job);
|
|
19
|
+
if (key) {
|
|
20
|
+
const waiting = scheduled.findIndex((c) => replaceKeyOf(c) === key);
|
|
21
|
+
if (waiting >= 0)
|
|
22
|
+
scheduled.splice(waiting, 1);
|
|
23
|
+
}
|
|
24
|
+
const waits = job.runAt !== undefined || (mode === "manual" && job.kind !== "cron");
|
|
25
|
+
if (!waits)
|
|
26
|
+
await runner.run({ ...job, attempts: 1 }); // one try, like inProcess(): reported, so the test sees it
|
|
27
|
+
else if (job.kind === "call")
|
|
28
|
+
scheduled.push(job);
|
|
29
|
+
else if (job.kind === "delivery")
|
|
30
|
+
queued.push(job);
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
/** Removes the jobs that are due from `list` and returns them. */
|
|
35
|
+
function takeDue(list, cutoff) {
|
|
36
|
+
const due = (job) => !job.runAt || Date.parse(job.runAt) <= cutoff;
|
|
37
|
+
const ready = list.filter(due);
|
|
38
|
+
list.splice(0, list.length, ...list.filter((job) => !due(job)));
|
|
39
|
+
return ready;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Run each test inside a transaction that's rolled back, set up once with hooks:
|
|
43
|
+
*
|
|
44
|
+
* ```ts
|
|
45
|
+
* const t = createTestRegistry(registry);
|
|
46
|
+
* beforeEach(() => t.begin());
|
|
47
|
+
* afterEach(() => t.rollback());
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
50
|
+
* Every top-level mutation inside becomes a savepoint, so a failing mutation rolls back just its own writes, like it
|
|
51
|
+
* would in production. Tests that share `t` run one at a time (one connection, nested savepoints).
|
|
52
|
+
*/
|
|
53
|
+
export function createTestRegistry(registry, options = {}) {
|
|
54
|
+
const { options: registryOptions } = registry[INTERNAL];
|
|
55
|
+
const fixed = options.now;
|
|
56
|
+
const now = typeof fixed === "function" ? fixed : fixed === undefined ? undefined : () => new Date(fixed);
|
|
57
|
+
/** While `drain()` runs a job that waited, the time it was due: what `ctx.now()` reads then. */
|
|
58
|
+
let travelled;
|
|
59
|
+
const clock = () => {
|
|
60
|
+
const base = (now ?? registryOptions.now ?? (() => new Date()))();
|
|
61
|
+
return travelled !== undefined && travelled > base.getTime() ? new Date(travelled) : base;
|
|
62
|
+
};
|
|
63
|
+
let open;
|
|
64
|
+
const current = () => {
|
|
65
|
+
if (!open)
|
|
66
|
+
throw new Error("[logic-sdk] No test transaction. Add beforeEach(() => t.begin()) and afterEach(() => t.rollback()).");
|
|
67
|
+
return open;
|
|
68
|
+
};
|
|
69
|
+
/** `db` is the test's transaction, and `database` the adapter nested in it (none without a database). */
|
|
70
|
+
function start(db, database, close) {
|
|
71
|
+
const events = [];
|
|
72
|
+
const stubs = new Map();
|
|
73
|
+
const queued = [];
|
|
74
|
+
const scheduled = [];
|
|
75
|
+
const failures = [];
|
|
76
|
+
const derived = registry[INTERNAL].derive({
|
|
77
|
+
database,
|
|
78
|
+
services: options.services,
|
|
79
|
+
config: options.config,
|
|
80
|
+
now: clock,
|
|
81
|
+
stubs,
|
|
82
|
+
record: (e) => events.push(e),
|
|
83
|
+
adapter: testJobs(options.jobs, registryOptions.jobs?.adapter, queued, scheduled),
|
|
84
|
+
failed: null, // failures throw in the test instead
|
|
85
|
+
waitUntil: undefined, // listeners finish before a call returns, inside the test
|
|
86
|
+
onError(error, info) {
|
|
87
|
+
if (info.outcome !== "discarded")
|
|
88
|
+
failures.push(error);
|
|
89
|
+
registryOptions.onError?.(error, info);
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
return { registry: derived, db, events, stubs, queued, scheduled, failures, close };
|
|
93
|
+
}
|
|
94
|
+
/** Run `work`, then throw the first listener failure since the last one thrown (calls through `t.registry` included). */
|
|
95
|
+
async function settle(state, work) {
|
|
96
|
+
let out;
|
|
97
|
+
try {
|
|
98
|
+
out = await work();
|
|
99
|
+
}
|
|
100
|
+
catch (e) {
|
|
101
|
+
state.failures.length = 0; // what it threw is the failure: don't report it again on the next call
|
|
102
|
+
throw e;
|
|
103
|
+
}
|
|
104
|
+
throwFailures(state);
|
|
105
|
+
return out;
|
|
106
|
+
}
|
|
107
|
+
function throwFailures(state) {
|
|
108
|
+
if (!state.failures.length)
|
|
109
|
+
return;
|
|
110
|
+
const [first] = state.failures.splice(0);
|
|
111
|
+
throw first;
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
async begin() {
|
|
115
|
+
if (open)
|
|
116
|
+
throw new Error("[logic-sdk] t.begin() ran again before t.rollback(). Roll back in afterEach, and don't run tests that share `t` at the same time (test.concurrent).");
|
|
117
|
+
const database = registryOptions.database?.adapter;
|
|
118
|
+
if (!database) {
|
|
119
|
+
open = start(undefined, undefined, async () => { });
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (!database.nest)
|
|
123
|
+
throw new Error("[logic-sdk] This database adapter can't nest transactions, so tests can't roll back.");
|
|
124
|
+
// Drizzle transactions live inside a callback: park it there until rollback.
|
|
125
|
+
const opened = Promise.withResolvers();
|
|
126
|
+
const released = Promise.withResolvers();
|
|
127
|
+
const done = database
|
|
128
|
+
.transaction(async (tx) => {
|
|
129
|
+
opened.resolve(tx);
|
|
130
|
+
await released.promise;
|
|
131
|
+
throw new Rollback();
|
|
132
|
+
})
|
|
133
|
+
.catch((e) => {
|
|
134
|
+
if (!(e instanceof Rollback))
|
|
135
|
+
throw e;
|
|
136
|
+
});
|
|
137
|
+
const tx = await Promise.race([opened.promise, done]); // `done` settles first only if the transaction failed to open
|
|
138
|
+
open = start(tx, database.nest(tx), async () => {
|
|
139
|
+
released.resolve();
|
|
140
|
+
await done;
|
|
141
|
+
});
|
|
142
|
+
},
|
|
143
|
+
/** Also throws a listener failure nothing surfaced yet (a call through `t.registry`), so the test fails. */
|
|
144
|
+
async rollback() {
|
|
145
|
+
const state = open;
|
|
146
|
+
open = undefined;
|
|
147
|
+
await state?.close();
|
|
148
|
+
if (state)
|
|
149
|
+
throwFailures(state);
|
|
150
|
+
},
|
|
151
|
+
as: (scope) => ({
|
|
152
|
+
scope,
|
|
153
|
+
run: (fn, ...args) => {
|
|
154
|
+
const state = current();
|
|
155
|
+
return settle(state, () => state.registry.as(scope).run(fn, ...args));
|
|
156
|
+
},
|
|
157
|
+
}),
|
|
158
|
+
stub: (fn, impl) => void current().stubs.set(fn, impl),
|
|
159
|
+
get events() {
|
|
160
|
+
return current().events;
|
|
161
|
+
},
|
|
162
|
+
get db() {
|
|
163
|
+
return current().db;
|
|
164
|
+
},
|
|
165
|
+
get registry() {
|
|
166
|
+
return current().registry;
|
|
167
|
+
},
|
|
168
|
+
jobs: {
|
|
169
|
+
get queued() {
|
|
170
|
+
return current().queued;
|
|
171
|
+
},
|
|
172
|
+
get scheduled() {
|
|
173
|
+
return current().scheduled;
|
|
174
|
+
},
|
|
175
|
+
async drain({ until } = {}) {
|
|
176
|
+
const state = current();
|
|
177
|
+
const end = until === undefined ? clock() : new Date(until);
|
|
178
|
+
if (Number.isNaN(end.getTime()))
|
|
179
|
+
throw new Error(`[logic-sdk] drain({ until }) needs a valid date (got ${String(until)}).`);
|
|
180
|
+
try {
|
|
181
|
+
for (let round = 1;; round++) {
|
|
182
|
+
const ready = [...takeDue(state.queued, end.getTime()), ...takeDue(state.scheduled, end.getTime())];
|
|
183
|
+
if (!ready.length)
|
|
184
|
+
return;
|
|
185
|
+
if (round > 100)
|
|
186
|
+
throw new Error("[logic-sdk] drain() ran 100 rounds and jobs are still due: does one reschedule itself, inside `until`, more often than you meant?");
|
|
187
|
+
ready.sort((a, b) => (a.runAt ?? "").localeCompare(b.runAt ?? ""));
|
|
188
|
+
for (const job of ready) {
|
|
189
|
+
// the job runs at the time it was due, so a self-rescheduling one moves forward and its checks see that time
|
|
190
|
+
if (job.runAt)
|
|
191
|
+
travelled = Math.max(travelled ?? 0, Date.parse(job.runAt));
|
|
192
|
+
await settle(state, () => state.registry.jobs.run(job));
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
finally {
|
|
197
|
+
travelled = undefined;
|
|
198
|
+
}
|
|
199
|
+
},
|
|
200
|
+
perform(target, ...rest) {
|
|
201
|
+
const state = current();
|
|
202
|
+
if (target.kind === "cron") {
|
|
203
|
+
const { at, attempt } = (rest[0] ?? {});
|
|
204
|
+
const cron = state.registry.definitions.crons.find((c) => c.cron === target);
|
|
205
|
+
if (!cron)
|
|
206
|
+
throw new Error("[logic-sdk] t.jobs.perform got a cron that isn't registered: export it from its module.");
|
|
207
|
+
const when = at === undefined ? clock() : new Date(at);
|
|
208
|
+
if (Number.isNaN(when.getTime()))
|
|
209
|
+
throw new Error(`[logic-sdk] t.jobs.perform({ at }) needs a valid date (got ${String(at)}).`);
|
|
210
|
+
const [job] = state.registry[INTERNAL].due(when, cron.id);
|
|
211
|
+
return settle(state, () => state.registry.jobs.run(job, { attempt }));
|
|
212
|
+
}
|
|
213
|
+
const listener = target;
|
|
214
|
+
const [payload, { scope, attempt }] = rest;
|
|
215
|
+
const registered = state.registry.definitions.listeners.find((l) => l.listener === listener);
|
|
216
|
+
if (!registered)
|
|
217
|
+
throw new Error("[logic-sdk] t.jobs.perform got a listener that isn't registered: export it from its module.");
|
|
218
|
+
const envelope = state.registry[INTERNAL].envelope(registered.event, payload, scope);
|
|
219
|
+
return settle(state, () => state.registry.jobs.run(deliveryOf(registered, envelope), { attempt }));
|
|
220
|
+
},
|
|
221
|
+
async tick(at) {
|
|
222
|
+
const state = current();
|
|
223
|
+
const jobs = state.registry[INTERNAL].due(at === undefined ? clock() : new Date(at));
|
|
224
|
+
for (const job of jobs)
|
|
225
|
+
await settle(state, () => state.registry.jobs.run(job));
|
|
226
|
+
return jobs.map((job) => job.cron);
|
|
227
|
+
},
|
|
228
|
+
},
|
|
229
|
+
};
|
|
230
|
+
}
|