@weft-framework/server 0.0.0-stage → 0.0.3
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/LICENSE +21 -0
- package/README.md +4 -2
- package/dist/account.d.ts +124 -0
- package/dist/account.js +344 -0
- package/dist/action.d.ts +43 -0
- package/dist/action.js +135 -0
- package/dist/admin.d.ts +99 -0
- package/dist/admin.js +431 -0
- package/dist/api.d.ts +21 -0
- package/dist/api.js +72 -0
- package/dist/app.d.ts +202 -0
- package/dist/app.js +3692 -0
- package/dist/assets.d.ts +4 -0
- package/dist/assets.js +91 -0
- package/dist/auth.d.ts +150 -0
- package/dist/auth.js +215 -0
- package/dist/backup.d.ts +46 -0
- package/dist/backup.js +345 -0
- package/dist/build.d.ts +33 -0
- package/dist/build.js +274 -0
- package/dist/bun.d.ts +30 -0
- package/dist/bun.js +53 -0
- package/dist/calendar.d.ts +161 -0
- package/dist/calendar.js +690 -0
- package/dist/compiler.d.ts +13 -0
- package/dist/compiler.js +58 -0
- package/dist/compress.d.ts +6 -0
- package/dist/compress.js +78 -0
- package/dist/config.d.ts +26 -0
- package/dist/config.js +317 -0
- package/dist/context.d.ts +9 -0
- package/dist/context.js +1 -0
- package/dist/contract.d.ts +18 -0
- package/dist/contract.js +171 -0
- package/dist/datamap.d.ts +24 -0
- package/dist/datamap.js +365 -0
- package/dist/db-backup.d.ts +27 -0
- package/dist/db-backup.js +115 -0
- package/dist/dev-events.d.ts +14 -0
- package/dist/dev-events.js +135 -0
- package/dist/dev.d.ts +1 -0
- package/dist/dev.js +240 -0
- package/dist/devtools-client.d.ts +1 -0
- package/dist/devtools-client.js +362 -0
- package/dist/devtools.d.ts +87 -0
- package/dist/devtools.js +68 -0
- package/dist/edge.d.ts +3 -0
- package/dist/edge.js +7 -0
- package/dist/email-signin.d.ts +92 -0
- package/dist/email-signin.js +259 -0
- package/dist/env.d.ts +1 -0
- package/dist/env.js +15 -0
- package/dist/explain.d.ts +12 -0
- package/dist/explain.js +217 -0
- package/dist/files.d.ts +6 -0
- package/dist/files.js +170 -0
- package/dist/grants.d.ts +69 -0
- package/dist/grants.js +206 -0
- package/dist/image/bitmap.d.ts +11 -0
- package/dist/image/bitmap.js +172 -0
- package/dist/image/jpeg.d.ts +10 -0
- package/dist/image/jpeg.js +697 -0
- package/dist/image/png.d.ts +8 -0
- package/dist/image/png.js +282 -0
- package/dist/image/transform.d.ts +20 -0
- package/dist/image/transform.js +52 -0
- package/dist/image/worker.d.ts +1 -0
- package/dist/image/worker.js +11 -0
- package/dist/images.d.ts +39 -0
- package/dist/images.js +283 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.js +31 -0
- package/dist/inspect-hub.d.ts +42 -0
- package/dist/inspect-hub.js +14 -0
- package/dist/inspector.d.ts +51 -0
- package/dist/inspector.js +193 -0
- package/dist/job-admin.d.ts +55 -0
- package/dist/job-admin.js +89 -0
- package/dist/jobs.d.ts +222 -0
- package/dist/jobs.js +789 -0
- package/dist/live.d.ts +72 -0
- package/dist/live.js +481 -0
- package/dist/load-config.d.ts +2 -0
- package/dist/load-config.js +41 -0
- package/dist/log.d.ts +20 -0
- package/dist/log.js +143 -0
- package/dist/mail.d.ts +189 -0
- package/dist/mail.js +999 -0
- package/dist/media.d.ts +129 -0
- package/dist/media.js +470 -0
- package/dist/meta.d.ts +54 -0
- package/dist/meta.js +146 -0
- package/dist/middleware.d.ts +20 -0
- package/dist/middleware.js +101 -0
- package/dist/migrate.d.ts +12 -0
- package/dist/migrate.js +88 -0
- package/dist/node.d.ts +26 -0
- package/dist/node.js +96 -0
- package/dist/notice.d.ts +21 -0
- package/dist/notice.js +48 -0
- package/dist/npm.d.ts +16 -0
- package/dist/npm.js +347 -0
- package/dist/oauth.d.ts +76 -0
- package/dist/oauth.js +408 -0
- package/dist/otel.d.ts +57 -0
- package/dist/otel.js +293 -0
- package/dist/passkey.d.ts +99 -0
- package/dist/passkey.js +572 -0
- package/dist/password.d.ts +8 -0
- package/dist/password.js +65 -0
- package/dist/pdf.d.ts +46 -0
- package/dist/pdf.js +332 -0
- package/dist/personal-data.d.ts +22 -0
- package/dist/personal-data.js +148 -0
- package/dist/pointers.d.ts +9 -0
- package/dist/pointers.js +148 -0
- package/dist/postcss.d.ts +3 -0
- package/dist/postcss.js +89 -0
- package/dist/presence.d.ts +27 -0
- package/dist/presence.js +161 -0
- package/dist/preview.d.ts +25 -0
- package/dist/preview.js +353 -0
- package/dist/production.d.ts +16 -0
- package/dist/production.js +119 -0
- package/dist/project.d.ts +23 -0
- package/dist/project.js +536 -0
- package/dist/regions.d.ts +47 -0
- package/dist/regions.js +511 -0
- package/dist/replay.d.ts +44 -0
- package/dist/replay.js +259 -0
- package/dist/router.d.ts +29 -0
- package/dist/router.js +146 -0
- package/dist/rules-diff.d.ts +18 -0
- package/dist/rules-diff.js +165 -0
- package/dist/rules-test.d.ts +17 -0
- package/dist/rules-test.js +108 -0
- package/dist/saml.d.ts +160 -0
- package/dist/saml.js +959 -0
- package/dist/seal.d.ts +35 -0
- package/dist/seal.js +107 -0
- package/dist/secret.d.ts +14 -0
- package/dist/secret.js +110 -0
- package/dist/security.d.ts +53 -0
- package/dist/security.js +250 -0
- package/dist/seed.d.ts +16 -0
- package/dist/seed.js +94 -0
- package/dist/seo.d.ts +42 -0
- package/dist/seo.js +175 -0
- package/dist/settings.d.ts +79 -0
- package/dist/settings.js +212 -0
- package/dist/shared.d.ts +15 -0
- package/dist/shared.js +283 -0
- package/dist/shell.d.ts +12 -0
- package/dist/shell.js +118 -0
- package/dist/storage.d.ts +13 -0
- package/dist/storage.js +55 -0
- package/dist/support.d.ts +83 -0
- package/dist/support.js +236 -0
- package/dist/sync.d.ts +35 -0
- package/dist/sync.js +167 -0
- package/dist/tailwind.d.ts +14 -0
- package/dist/tailwind.js +117 -0
- package/dist/two-factor.d.ts +103 -0
- package/dist/two-factor.js +435 -0
- package/dist/uploads.d.ts +185 -0
- package/dist/uploads.js +991 -0
- package/dist/webhooks.d.ts +169 -0
- package/dist/webhooks.js +758 -0
- package/dist/webpush.d.ts +97 -0
- package/dist/webpush.js +372 -0
- package/dist/wizard.d.ts +141 -0
- package/dist/wizard.js +410 -0
- package/dist/workflows.d.ts +134 -0
- package/dist/workflows.js +511 -0
- package/package.json +186 -4
|
@@ -0,0 +1,511 @@
|
|
|
1
|
+
import { decodeJson } from "./action.js";
|
|
2
|
+
import { authState } from "./auth.js";
|
|
3
|
+
import { defineJob, JobError } from "./jobs.js";
|
|
4
|
+
const FINISHED = ["done", "failed", "cancelled"];
|
|
5
|
+
export class MemoryWorkflowStore {
|
|
6
|
+
#runs = new Map();
|
|
7
|
+
#signals = [];
|
|
8
|
+
create(run) {
|
|
9
|
+
if (run.key !== null && [...this.#runs.values()].some((r) => r.key === run.key && r.status === "running"))
|
|
10
|
+
return false;
|
|
11
|
+
this.#runs.set(run.id, structuredClone(run));
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
get(id) {
|
|
15
|
+
const r = this.#runs.get(id);
|
|
16
|
+
return r ? structuredClone(r) : null;
|
|
17
|
+
}
|
|
18
|
+
acquire(id, holder, until, now) {
|
|
19
|
+
const r = this.#runs.get(id);
|
|
20
|
+
if (!r || r.status !== "running" || (r.holder !== null && r.holder !== holder && r.holdUntil >= now))
|
|
21
|
+
return null;
|
|
22
|
+
r.holder = holder;
|
|
23
|
+
r.holdUntil = until;
|
|
24
|
+
return structuredClone(r);
|
|
25
|
+
}
|
|
26
|
+
save(id, holder, patch, now) {
|
|
27
|
+
const r = this.#runs.get(id);
|
|
28
|
+
if (!r || r.holder !== holder || r.status !== "running")
|
|
29
|
+
return false;
|
|
30
|
+
Object.assign(r, structuredClone(patch), { updated: now });
|
|
31
|
+
if (patch.status && FINISHED.includes(patch.status)) {
|
|
32
|
+
r.key = null;
|
|
33
|
+
r.holder = null;
|
|
34
|
+
r.holdUntil = 0;
|
|
35
|
+
r.waiting = null;
|
|
36
|
+
r.wakeAt = null;
|
|
37
|
+
}
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
release(id, holder) {
|
|
41
|
+
const r = this.#runs.get(id);
|
|
42
|
+
if (r && r.holder === holder) {
|
|
43
|
+
r.holder = null;
|
|
44
|
+
r.holdUntil = 0;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
waitingFor(event, key) {
|
|
48
|
+
return [...this.#runs.values()].filter((r) => r.status === "running" && r.waiting?.event === event && r.waiting.key === key).map((r) => r.id);
|
|
49
|
+
}
|
|
50
|
+
addSignal(s) {
|
|
51
|
+
this.#signals.push(structuredClone(s));
|
|
52
|
+
}
|
|
53
|
+
signals(event, key, since, limit) {
|
|
54
|
+
return this.#signals.filter((s) => s.event === event && s.key === key && s.at >= since).slice(0, limit).map((s) => structuredClone(s));
|
|
55
|
+
}
|
|
56
|
+
cancel(id, now) {
|
|
57
|
+
const r = this.#runs.get(id);
|
|
58
|
+
if (!r || r.status !== "running")
|
|
59
|
+
return false;
|
|
60
|
+
Object.assign(r, { status: "cancelled", key: null, holder: null, holdUntil: 0, waiting: null, wakeAt: null, updated: now });
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
list(f) {
|
|
64
|
+
return [...this.#runs.values()]
|
|
65
|
+
.filter((r) => (!f.status || r.status === f.status) && (!f.name || r.name === f.name))
|
|
66
|
+
.sort((a, b) => b.created - a.created || (a.id < b.id ? 1 : -1))
|
|
67
|
+
.slice(0, Math.min(Math.max(1, f.limit ?? 100), 1000))
|
|
68
|
+
.map((r) => structuredClone(r));
|
|
69
|
+
}
|
|
70
|
+
sweep(before) {
|
|
71
|
+
for (const [id, r] of this.#runs)
|
|
72
|
+
if (FINISHED.includes(r.status) && r.updated < before)
|
|
73
|
+
this.#runs.delete(id);
|
|
74
|
+
for (let i = this.#signals.length - 1; i >= 0; i--)
|
|
75
|
+
if (this.#signals[i].at < before)
|
|
76
|
+
this.#signals.splice(i, 1);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
export class SqlWorkflowStore {
|
|
80
|
+
#db;
|
|
81
|
+
#t;
|
|
82
|
+
#s;
|
|
83
|
+
constructor(db, table = "weft_workflows") {
|
|
84
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(table))
|
|
85
|
+
throw new Error(`@weft-framework/server: not a valid table name: ${table}`);
|
|
86
|
+
this.#db = db;
|
|
87
|
+
this.#t = `"${table}"`;
|
|
88
|
+
this.#s = `"${table}_signals"`;
|
|
89
|
+
db.exec(`CREATE TABLE IF NOT EXISTS ${this.#t} (id TEXT PRIMARY KEY NOT NULL, name TEXT NOT NULL, args TEXT NOT NULL, actor TEXT, ` +
|
|
90
|
+
`status TEXT NOT NULL, history TEXT NOT NULL, wait_event TEXT, wait_key TEXT, wake_at INTEGER, result TEXT, error TEXT, ` +
|
|
91
|
+
`run_key TEXT UNIQUE, created INTEGER NOT NULL, updated INTEGER NOT NULL, holder TEXT, hold_until INTEGER NOT NULL)`);
|
|
92
|
+
db.exec(`CREATE INDEX IF NOT EXISTS "${table}_waiting" ON ${this.#t} (wait_event, wait_key)`);
|
|
93
|
+
db.exec(`CREATE TABLE IF NOT EXISTS ${this.#s} (id TEXT PRIMARY KEY NOT NULL, event TEXT NOT NULL, sig_key TEXT NOT NULL, payload TEXT NOT NULL, at INTEGER NOT NULL)`);
|
|
94
|
+
db.exec(`CREATE INDEX IF NOT EXISTS "${table}_signals_by" ON ${this.#s} (event, sig_key, at)`);
|
|
95
|
+
}
|
|
96
|
+
static #run(r) {
|
|
97
|
+
if (!r)
|
|
98
|
+
return null;
|
|
99
|
+
const s = (v) => (v === null || v === undefined ? null : String(v));
|
|
100
|
+
return {
|
|
101
|
+
id: String(r.id),
|
|
102
|
+
name: String(r.name),
|
|
103
|
+
args: JSON.parse(String(r.args)),
|
|
104
|
+
actor: r.actor === null || r.actor === undefined ? null : JSON.parse(String(r.actor)),
|
|
105
|
+
status: String(r.status),
|
|
106
|
+
history: JSON.parse(String(r.history)),
|
|
107
|
+
waiting: r.wait_event === null || r.wait_event === undefined ? null : { event: String(r.wait_event), key: String(r.wait_key) },
|
|
108
|
+
wakeAt: r.wake_at === null || r.wake_at === undefined ? null : Number(r.wake_at),
|
|
109
|
+
result: r.result === null || r.result === undefined ? null : JSON.parse(String(r.result)),
|
|
110
|
+
error: s(r.error),
|
|
111
|
+
key: s(r.run_key),
|
|
112
|
+
created: Number(r.created),
|
|
113
|
+
updated: Number(r.updated),
|
|
114
|
+
holder: s(r.holder),
|
|
115
|
+
holdUntil: Number(r.hold_until),
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
create(run) {
|
|
119
|
+
return (this.#db
|
|
120
|
+
.prepare(`INSERT INTO ${this.#t} (id, name, args, actor, status, history, wait_event, wait_key, wake_at, result, error, run_key, created, updated, holder, hold_until) ` +
|
|
121
|
+
`VALUES (?, ?, ?, ?, ?, ?, NULL, NULL, NULL, NULL, NULL, ?, ?, ?, NULL, 0) ON CONFLICT DO NOTHING RETURNING id`)
|
|
122
|
+
.all(run.id, run.name, JSON.stringify(run.args), run.actor ? JSON.stringify(run.actor) : null, run.status, JSON.stringify(run.history), run.key, run.created, run.updated)
|
|
123
|
+
.length === 1);
|
|
124
|
+
}
|
|
125
|
+
get(id) {
|
|
126
|
+
return SqlWorkflowStore.#run(this.#db.prepare(`SELECT * FROM ${this.#t} WHERE id = ?`).all(id)[0]);
|
|
127
|
+
}
|
|
128
|
+
acquire(id, holder, until, now) {
|
|
129
|
+
return SqlWorkflowStore.#run(this.#db
|
|
130
|
+
.prepare(`UPDATE ${this.#t} SET holder = ?, hold_until = ? WHERE id = ? AND status = 'running' ` +
|
|
131
|
+
`AND (holder IS NULL OR holder = ? OR hold_until < ?) RETURNING *`)
|
|
132
|
+
.all(holder, until, id, holder, now)[0]);
|
|
133
|
+
}
|
|
134
|
+
save(id, holder, patch, now) {
|
|
135
|
+
const sets = ["updated = ?"];
|
|
136
|
+
const args = [now];
|
|
137
|
+
if (patch.history !== undefined)
|
|
138
|
+
(sets.push("history = ?"), args.push(JSON.stringify(patch.history)));
|
|
139
|
+
if (patch.waiting !== undefined)
|
|
140
|
+
(sets.push("wait_event = ?", "wait_key = ?"), args.push(patch.waiting?.event ?? null, patch.waiting?.key ?? null));
|
|
141
|
+
if (patch.wakeAt !== undefined)
|
|
142
|
+
(sets.push("wake_at = ?"), args.push(patch.wakeAt));
|
|
143
|
+
if (patch.result !== undefined)
|
|
144
|
+
(sets.push("result = ?"), args.push(JSON.stringify(patch.result)));
|
|
145
|
+
if (patch.error !== undefined)
|
|
146
|
+
(sets.push("error = ?"), args.push(patch.error));
|
|
147
|
+
if (patch.status !== undefined) {
|
|
148
|
+
sets.push("status = ?");
|
|
149
|
+
args.push(patch.status);
|
|
150
|
+
if (FINISHED.includes(patch.status))
|
|
151
|
+
sets.push("run_key = NULL", "holder = NULL", "hold_until = 0", "wait_event = NULL", "wait_key = NULL", "wake_at = NULL");
|
|
152
|
+
}
|
|
153
|
+
return (this.#db.prepare(`UPDATE ${this.#t} SET ${sets.join(", ")} WHERE id = ? AND holder = ? AND status = 'running' RETURNING id`).all(...args, id, holder)
|
|
154
|
+
.length === 1);
|
|
155
|
+
}
|
|
156
|
+
release(id, holder) {
|
|
157
|
+
this.#db.prepare(`UPDATE ${this.#t} SET holder = NULL, hold_until = 0 WHERE id = ? AND holder = ?`).run(id, holder);
|
|
158
|
+
}
|
|
159
|
+
waitingFor(event, key) {
|
|
160
|
+
return this.#db.prepare(`SELECT id FROM ${this.#t} WHERE wait_event = ? AND wait_key = ? AND status = 'running'`).all(event, key).map((r) => String(r.id));
|
|
161
|
+
}
|
|
162
|
+
addSignal(s) {
|
|
163
|
+
this.#db.prepare(`INSERT INTO ${this.#s} (id, event, sig_key, payload, at) VALUES (?, ?, ?, ?, ?)`).run(s.id, s.event, s.key, JSON.stringify(s.payload), s.at);
|
|
164
|
+
}
|
|
165
|
+
signals(event, key, since, limit) {
|
|
166
|
+
const rows = this.#db
|
|
167
|
+
.prepare(`SELECT * FROM ${this.#s} WHERE event = ? AND sig_key = ? AND at >= ? ORDER BY at, id LIMIT ?`)
|
|
168
|
+
.all(event, key, since, limit);
|
|
169
|
+
return rows.map((r) => ({ id: String(r.id), event: String(r.event), key: String(r.sig_key), payload: JSON.parse(String(r.payload)), at: Number(r.at) }));
|
|
170
|
+
}
|
|
171
|
+
cancel(id, now) {
|
|
172
|
+
return (this.#db
|
|
173
|
+
.prepare(`UPDATE ${this.#t} SET status = 'cancelled', run_key = NULL, holder = NULL, hold_until = 0, wait_event = NULL, wait_key = NULL, ` +
|
|
174
|
+
`wake_at = NULL, updated = ? WHERE id = ? AND status = 'running' RETURNING id`)
|
|
175
|
+
.all(now, id).length === 1);
|
|
176
|
+
}
|
|
177
|
+
list(f) {
|
|
178
|
+
const where = [];
|
|
179
|
+
const args = [];
|
|
180
|
+
if (f.status)
|
|
181
|
+
(where.push("status = ?"), args.push(f.status));
|
|
182
|
+
if (f.name)
|
|
183
|
+
(where.push("name = ?"), args.push(f.name));
|
|
184
|
+
const rows = this.#db
|
|
185
|
+
.prepare(`SELECT * FROM ${this.#t}${where.length ? ` WHERE ${where.join(" AND ")}` : ""} ORDER BY created DESC, id DESC LIMIT ?`)
|
|
186
|
+
.all(...args, Math.min(Math.max(1, f.limit ?? 100), 1000));
|
|
187
|
+
return rows.map((r) => SqlWorkflowStore.#run(r));
|
|
188
|
+
}
|
|
189
|
+
sweep(before) {
|
|
190
|
+
this.#db.prepare(`DELETE FROM ${this.#t} WHERE status IN ('done', 'failed', 'cancelled') AND updated < ?`).run(before);
|
|
191
|
+
this.#db.prepare(`DELETE FROM ${this.#s} WHERE at < ?`).run(before);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
const WORKFLOW = Symbol.for("weft.workflow");
|
|
195
|
+
const NAME_RE = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
196
|
+
const STEP_RE = /^[\x21-\x7e][\x20-\x7e]{0,199}$/;
|
|
197
|
+
const EVENT_RE = /^[A-Za-z][A-Za-z0-9_.:-]{0,99}$/;
|
|
198
|
+
const MAX_PAYLOAD = 64 * 1024;
|
|
199
|
+
const MAX_RESULT = 256 * 1024;
|
|
200
|
+
export function defineWorkflow(name, params, run) {
|
|
201
|
+
if (!NAME_RE.test(name))
|
|
202
|
+
throw new Error(`defineWorkflow: not a valid name: ${name}`);
|
|
203
|
+
if (typeof run !== "function")
|
|
204
|
+
throw new Error(`defineWorkflow ${name}: the body is a function`);
|
|
205
|
+
return Object.freeze({ [WORKFLOW]: true, name, params: Object.freeze([...params]), run });
|
|
206
|
+
}
|
|
207
|
+
export function isWorkflow(v) {
|
|
208
|
+
return typeof v === "object" && v !== null && v[WORKFLOW] === true;
|
|
209
|
+
}
|
|
210
|
+
class Suspend {
|
|
211
|
+
suspend = true;
|
|
212
|
+
}
|
|
213
|
+
function randomId() {
|
|
214
|
+
const b = crypto.getRandomValues(new Uint8Array(16));
|
|
215
|
+
return [...b].map((x) => x.toString(16).padStart(2, "0")).join("");
|
|
216
|
+
}
|
|
217
|
+
function asJson(v, max, what) {
|
|
218
|
+
const text = JSON.stringify(v === undefined ? null : v);
|
|
219
|
+
if (text === undefined)
|
|
220
|
+
throw new JobError(`${what} is not JSON`, { retry: false });
|
|
221
|
+
if (text.length > max)
|
|
222
|
+
throw new JobError(`${what} is larger than ${max} bytes`, { retry: false });
|
|
223
|
+
return JSON.parse(text);
|
|
224
|
+
}
|
|
225
|
+
export class Workflows {
|
|
226
|
+
job;
|
|
227
|
+
#defs = new Map();
|
|
228
|
+
#store;
|
|
229
|
+
#attempts;
|
|
230
|
+
#timeout;
|
|
231
|
+
#keep;
|
|
232
|
+
#onError;
|
|
233
|
+
#now;
|
|
234
|
+
#jobs = null;
|
|
235
|
+
#swept = 0;
|
|
236
|
+
constructor(o) {
|
|
237
|
+
for (const w of o.workflows ?? []) {
|
|
238
|
+
if (!isWorkflow(w))
|
|
239
|
+
throw new Error("createWorkflows: `workflows` holds defineWorkflow(...) results");
|
|
240
|
+
if (this.#defs.has(w.name))
|
|
241
|
+
throw new Error(`createWorkflows: two workflows are called ${w.name}`);
|
|
242
|
+
this.#defs.set(w.name, w);
|
|
243
|
+
}
|
|
244
|
+
if (this.#defs.size === 0)
|
|
245
|
+
throw new Error("createWorkflows: at least one workflow");
|
|
246
|
+
this.#store = o.store ?? new MemoryWorkflowStore();
|
|
247
|
+
const range = (what, v, lo, hi) => {
|
|
248
|
+
if (!Number.isInteger(v) || v < lo || v > hi)
|
|
249
|
+
throw new Error(`createWorkflows: ${what} is ${lo} to ${hi}`);
|
|
250
|
+
return v;
|
|
251
|
+
};
|
|
252
|
+
this.#attempts = range("attempts", o.attempts ?? 5, 1, 100);
|
|
253
|
+
this.#timeout = range("timeoutMs", o.timeoutMs ?? 10 * 60_000, 1000, 24 * 3600_000);
|
|
254
|
+
this.#keep = range("keepMs", o.keepMs ?? 30 * 86_400_000, 60_000, 365 * 86_400_000);
|
|
255
|
+
this.#onError = o.onError ?? ((err, run) => console.error(`[weft] workflow ${run?.name ?? ""}:`, err));
|
|
256
|
+
this.#now = o.now ?? Date.now;
|
|
257
|
+
this.job = defineJob(o.jobName ?? "weftWorkflow", [{ name: "run", type: "string" }], (ctx, id) => this.#execute(ctx, id), {
|
|
258
|
+
attempts: this.#attempts,
|
|
259
|
+
timeoutMs: this.#timeout,
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
use(jobs) {
|
|
263
|
+
this.#jobs = jobs;
|
|
264
|
+
return this;
|
|
265
|
+
}
|
|
266
|
+
#queue() {
|
|
267
|
+
if (!this.#jobs)
|
|
268
|
+
throw new Error("workflows: call use(jobs) first");
|
|
269
|
+
return this.#jobs;
|
|
270
|
+
}
|
|
271
|
+
async start(ctx, workflow, args = {}, o = {}) {
|
|
272
|
+
const def = this.#defs.get(workflow?.name);
|
|
273
|
+
if (!def || def !== workflow)
|
|
274
|
+
throw new Error(`workflows: ${workflow?.name ?? "that"} is not one of these workflows`);
|
|
275
|
+
const d = decodeJson(def.params, args);
|
|
276
|
+
if (!d.ok)
|
|
277
|
+
throw new TypeError(`workflow ${def.name}: ${d.error}`);
|
|
278
|
+
if (o.key !== undefined && (typeof o.key !== "string" || o.key === "" || o.key.length > 256))
|
|
279
|
+
throw new TypeError("workflows: `key` is 1-256 characters");
|
|
280
|
+
let actor = null;
|
|
281
|
+
if (o.as !== undefined) {
|
|
282
|
+
if (o.as !== null && (typeof o.as.user !== "string" || o.as.user === ""))
|
|
283
|
+
throw new TypeError("workflows: `as` needs a user id");
|
|
284
|
+
actor = o.as === null ? null : { user: o.as.user, ...(o.as.tenant !== undefined ? { tenant: o.as.tenant } : {}) };
|
|
285
|
+
}
|
|
286
|
+
else if (ctx) {
|
|
287
|
+
const s = await authState(ctx);
|
|
288
|
+
if (s && !s.pending)
|
|
289
|
+
actor = { user: s.user, ...(s.tenant !== undefined ? { tenant: s.tenant } : {}) };
|
|
290
|
+
}
|
|
291
|
+
const now = this.#now();
|
|
292
|
+
const run = {
|
|
293
|
+
id: randomId(),
|
|
294
|
+
name: def.name,
|
|
295
|
+
args: JSON.parse(JSON.stringify(d.args.map((a) => (a === undefined ? null : a)))),
|
|
296
|
+
actor,
|
|
297
|
+
status: "running",
|
|
298
|
+
history: {},
|
|
299
|
+
waiting: null,
|
|
300
|
+
wakeAt: null,
|
|
301
|
+
result: null,
|
|
302
|
+
error: null,
|
|
303
|
+
key: o.key ?? null,
|
|
304
|
+
created: now,
|
|
305
|
+
updated: now,
|
|
306
|
+
holder: null,
|
|
307
|
+
holdUntil: 0,
|
|
308
|
+
};
|
|
309
|
+
this.#queue();
|
|
310
|
+
if (!(await this.#store.create(run)))
|
|
311
|
+
return false;
|
|
312
|
+
await this.#wake(run, now);
|
|
313
|
+
return run.id;
|
|
314
|
+
}
|
|
315
|
+
async signal(event, key, payload = null) {
|
|
316
|
+
if (typeof event !== "string" || !EVENT_RE.test(event))
|
|
317
|
+
throw new TypeError("workflows: not a valid event name");
|
|
318
|
+
if (typeof key !== "string" || key === "" || key.length > 256)
|
|
319
|
+
throw new TypeError("workflows: `key` is 1-256 characters");
|
|
320
|
+
const text = JSON.stringify(payload ?? null);
|
|
321
|
+
if (text === undefined || text.length > MAX_PAYLOAD)
|
|
322
|
+
throw new TypeError(`workflows: the payload is JSON of at most ${MAX_PAYLOAD} bytes`);
|
|
323
|
+
const now = this.#now();
|
|
324
|
+
await this.#store.addSignal({ id: `${now.toString(36).padStart(10, "0")}-${randomId()}`, event, key, payload: JSON.parse(text), at: now });
|
|
325
|
+
const ids = await this.#store.waitingFor(event, key);
|
|
326
|
+
for (const id of ids) {
|
|
327
|
+
const run = await this.#store.get(id);
|
|
328
|
+
if (run)
|
|
329
|
+
await this.#wake(run, now);
|
|
330
|
+
}
|
|
331
|
+
return ids.length;
|
|
332
|
+
}
|
|
333
|
+
get(id) {
|
|
334
|
+
return Promise.resolve(this.#store.get(id)).then((r) => r ?? null);
|
|
335
|
+
}
|
|
336
|
+
list(f = {}) {
|
|
337
|
+
return Promise.resolve(this.#store.list(f));
|
|
338
|
+
}
|
|
339
|
+
cancel(id) {
|
|
340
|
+
return Promise.resolve(this.#store.cancel(id, this.#now()));
|
|
341
|
+
}
|
|
342
|
+
async #wake(run, at, timed = false) {
|
|
343
|
+
const key = timed ? { key: `weft-wf:${run.id}:${at}` } : {};
|
|
344
|
+
await this.#queue().enqueue(null, this.job, { run: run.id }, { runAt: at, as: run.actor, ...key });
|
|
345
|
+
}
|
|
346
|
+
async #execute(ctx, id) {
|
|
347
|
+
const now = this.#now();
|
|
348
|
+
if (now - this.#swept > 3600_000) {
|
|
349
|
+
this.#swept = now;
|
|
350
|
+
await this.#store.sweep(now - this.#keep);
|
|
351
|
+
}
|
|
352
|
+
const holder = ctx.job.id;
|
|
353
|
+
const run = await this.#store.acquire(id, holder, now + this.#timeout + 60_000, now);
|
|
354
|
+
if (!run) {
|
|
355
|
+
const other = await this.#store.get(id);
|
|
356
|
+
if (other && other.status === "running" && other.holder !== null)
|
|
357
|
+
await this.#wake(other, other.holdUntil + 1, true);
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
const def = this.#defs.get(run.name);
|
|
361
|
+
if (!def) {
|
|
362
|
+
await this.#store.release(id, holder);
|
|
363
|
+
throw new JobError(`no workflow ${run.name} in this version of the app`);
|
|
364
|
+
}
|
|
365
|
+
const history = { ...run.history };
|
|
366
|
+
const used = new Set();
|
|
367
|
+
let suspended = null;
|
|
368
|
+
const SUSPEND = new Suspend();
|
|
369
|
+
const LOST = new Suspend();
|
|
370
|
+
const save = async (patch) => {
|
|
371
|
+
if (!(await this.#store.save(id, holder, patch, this.#now())))
|
|
372
|
+
throw LOST;
|
|
373
|
+
};
|
|
374
|
+
const claim = (name) => {
|
|
375
|
+
if (typeof name !== "string" || !STEP_RE.test(name))
|
|
376
|
+
throw new JobError(`not a valid step name: ${String(name).slice(0, 50)}`, { retry: false });
|
|
377
|
+
if (used.has(name))
|
|
378
|
+
throw new JobError(`step ${name} is used twice in one run`, { retry: false });
|
|
379
|
+
used.add(name);
|
|
380
|
+
if (suspended)
|
|
381
|
+
throw SUSPEND;
|
|
382
|
+
};
|
|
383
|
+
const taken = () => new Set(Object.values(history)
|
|
384
|
+
.map((h) => h.got?.id)
|
|
385
|
+
.filter((x) => typeof x === "string"));
|
|
386
|
+
const suspend = (wakeAt, waiting) => {
|
|
387
|
+
suspended = { wakeAt, waiting };
|
|
388
|
+
throw SUSPEND;
|
|
389
|
+
};
|
|
390
|
+
const sleepUntil = async (name, at) => {
|
|
391
|
+
claim(name);
|
|
392
|
+
if (typeof at !== "number" || !Number.isFinite(at))
|
|
393
|
+
throw new JobError(`sleep ${name}: not a time`, { retry: false });
|
|
394
|
+
let h = history[name];
|
|
395
|
+
if (!h) {
|
|
396
|
+
h = { wake: at };
|
|
397
|
+
history[name] = h;
|
|
398
|
+
await save({ history });
|
|
399
|
+
}
|
|
400
|
+
if (this.#now() >= h.wake)
|
|
401
|
+
return;
|
|
402
|
+
suspend(h.wake, null);
|
|
403
|
+
};
|
|
404
|
+
const wf = {
|
|
405
|
+
user: ctx.user,
|
|
406
|
+
run: { id, name: run.name },
|
|
407
|
+
step: async (name, fn) => {
|
|
408
|
+
claim(name);
|
|
409
|
+
const h = history[name];
|
|
410
|
+
if (h && "v" in h)
|
|
411
|
+
return h.v;
|
|
412
|
+
const v = asJson(await fn(), MAX_RESULT, `step ${name}'s result`);
|
|
413
|
+
history[name] = { v };
|
|
414
|
+
await save({ history });
|
|
415
|
+
return v;
|
|
416
|
+
},
|
|
417
|
+
sleep: (name, ms) => {
|
|
418
|
+
if (typeof ms !== "number" || !Number.isFinite(ms) || ms < 0)
|
|
419
|
+
throw new JobError(`sleep ${name}: ms is a positive number`, { retry: false });
|
|
420
|
+
return sleepUntil(name, this.#now() + ms);
|
|
421
|
+
},
|
|
422
|
+
sleepUntil,
|
|
423
|
+
waitFor: async (name, event, o) => {
|
|
424
|
+
claim(name);
|
|
425
|
+
if (!EVENT_RE.test(event) || typeof o?.key !== "string" || o.key === "" || o.key.length > 256) {
|
|
426
|
+
throw new JobError(`waitFor ${name}: an event name and a key`, { retry: false });
|
|
427
|
+
}
|
|
428
|
+
let h = history[name];
|
|
429
|
+
if (h && h.got !== undefined)
|
|
430
|
+
return h.got === null ? null : { payload: h.got.payload, at: h.got.at };
|
|
431
|
+
if (!h) {
|
|
432
|
+
const t = o.timeoutMs;
|
|
433
|
+
if (t !== undefined && (typeof t !== "number" || !Number.isFinite(t) || t < 0))
|
|
434
|
+
throw new JobError(`waitFor ${name}: timeoutMs`, { retry: false });
|
|
435
|
+
h = { deadline: t === undefined ? null : this.#now() + t };
|
|
436
|
+
history[name] = h;
|
|
437
|
+
}
|
|
438
|
+
const seen = taken();
|
|
439
|
+
const sig = (await this.#store.signals(event, o.key, run.created, 1000)).find((s) => !seen.has(s.id));
|
|
440
|
+
if (sig) {
|
|
441
|
+
h.got = { id: sig.id, payload: sig.payload, at: sig.at };
|
|
442
|
+
await save({ history, waiting: null });
|
|
443
|
+
return { payload: sig.payload, at: sig.at };
|
|
444
|
+
}
|
|
445
|
+
if (h.deadline !== null && this.#now() >= h.deadline) {
|
|
446
|
+
h.got = null;
|
|
447
|
+
await save({ history, waiting: null });
|
|
448
|
+
return null;
|
|
449
|
+
}
|
|
450
|
+
return suspend(h.deadline, { event, key: o.key });
|
|
451
|
+
},
|
|
452
|
+
};
|
|
453
|
+
const args = def.params.map((p, i) => (run.args[i] === null && p.optional ? undefined : run.args[i]));
|
|
454
|
+
let result;
|
|
455
|
+
try {
|
|
456
|
+
result = await def.run(wf, ...args);
|
|
457
|
+
}
|
|
458
|
+
catch (err) {
|
|
459
|
+
if (err === LOST)
|
|
460
|
+
return;
|
|
461
|
+
if (err === SUSPEND || suspended) {
|
|
462
|
+
await this.#pause(run, holder, history, suspended);
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
const final = ctx.job.attempt >= ctx.job.maxAttempts || (err instanceof JobError && !err.retry);
|
|
466
|
+
const message = (err instanceof Error ? err.message : String(err)).slice(0, 1000) || "failed";
|
|
467
|
+
try {
|
|
468
|
+
if (final)
|
|
469
|
+
await this.#store.save(id, holder, { history, status: "failed", error: message }, this.#now());
|
|
470
|
+
else
|
|
471
|
+
await this.#store.release(id, holder);
|
|
472
|
+
}
|
|
473
|
+
catch (e) {
|
|
474
|
+
this.#onError(e, run);
|
|
475
|
+
}
|
|
476
|
+
this.#onError(err, run);
|
|
477
|
+
throw err;
|
|
478
|
+
}
|
|
479
|
+
if (suspended) {
|
|
480
|
+
await this.#pause(run, holder, history, suspended);
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
try {
|
|
484
|
+
await save({ history, status: "done", result: asJson(result, MAX_RESULT, "the result") });
|
|
485
|
+
}
|
|
486
|
+
catch (err) {
|
|
487
|
+
if (err === LOST)
|
|
488
|
+
return;
|
|
489
|
+
await this.#store.save(id, holder, { history, status: "failed", error: err instanceof Error ? err.message : String(err) }, this.#now());
|
|
490
|
+
throw err;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
async #pause(run, holder, history, s) {
|
|
494
|
+
if (!(await this.#store.save(run.id, holder, { history, waiting: s.waiting, wakeAt: s.wakeAt }, this.#now())))
|
|
495
|
+
return;
|
|
496
|
+
await this.#store.release(run.id, holder);
|
|
497
|
+
if (s.wakeAt !== null)
|
|
498
|
+
await this.#wake(run, s.wakeAt, true);
|
|
499
|
+
if (s.waiting) {
|
|
500
|
+
const seen = new Set(Object.values(history)
|
|
501
|
+
.map((h) => h.got?.id)
|
|
502
|
+
.filter((x) => typeof x === "string"));
|
|
503
|
+
const late = (await this.#store.signals(s.waiting.event, s.waiting.key, run.created, 1000)).some((x) => !seen.has(x.id));
|
|
504
|
+
if (late)
|
|
505
|
+
await this.#wake(run, this.#now());
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
export function createWorkflows(o) {
|
|
510
|
+
return new Workflows(o);
|
|
511
|
+
}
|