@weft-framework/server 0.0.0-stage → 0.0.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.
Files changed (175) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +4 -2
  3. package/dist/account.d.ts +124 -0
  4. package/dist/account.js +344 -0
  5. package/dist/action.d.ts +43 -0
  6. package/dist/action.js +135 -0
  7. package/dist/admin.d.ts +99 -0
  8. package/dist/admin.js +431 -0
  9. package/dist/api.d.ts +21 -0
  10. package/dist/api.js +72 -0
  11. package/dist/app.d.ts +202 -0
  12. package/dist/app.js +3692 -0
  13. package/dist/assets.d.ts +4 -0
  14. package/dist/assets.js +91 -0
  15. package/dist/auth.d.ts +150 -0
  16. package/dist/auth.js +215 -0
  17. package/dist/backup.d.ts +46 -0
  18. package/dist/backup.js +345 -0
  19. package/dist/build.d.ts +33 -0
  20. package/dist/build.js +274 -0
  21. package/dist/bun.d.ts +30 -0
  22. package/dist/bun.js +53 -0
  23. package/dist/calendar.d.ts +161 -0
  24. package/dist/calendar.js +690 -0
  25. package/dist/compiler.d.ts +13 -0
  26. package/dist/compiler.js +58 -0
  27. package/dist/compress.d.ts +6 -0
  28. package/dist/compress.js +78 -0
  29. package/dist/config.d.ts +26 -0
  30. package/dist/config.js +317 -0
  31. package/dist/context.d.ts +9 -0
  32. package/dist/context.js +1 -0
  33. package/dist/contract.d.ts +18 -0
  34. package/dist/contract.js +171 -0
  35. package/dist/datamap.d.ts +24 -0
  36. package/dist/datamap.js +365 -0
  37. package/dist/db-backup.d.ts +27 -0
  38. package/dist/db-backup.js +115 -0
  39. package/dist/dev-events.d.ts +14 -0
  40. package/dist/dev-events.js +135 -0
  41. package/dist/dev.d.ts +1 -0
  42. package/dist/dev.js +240 -0
  43. package/dist/devtools-client.d.ts +1 -0
  44. package/dist/devtools-client.js +362 -0
  45. package/dist/devtools.d.ts +87 -0
  46. package/dist/devtools.js +68 -0
  47. package/dist/edge.d.ts +3 -0
  48. package/dist/edge.js +7 -0
  49. package/dist/email-signin.d.ts +92 -0
  50. package/dist/email-signin.js +259 -0
  51. package/dist/env.d.ts +1 -0
  52. package/dist/env.js +15 -0
  53. package/dist/explain.d.ts +12 -0
  54. package/dist/explain.js +217 -0
  55. package/dist/files.d.ts +6 -0
  56. package/dist/files.js +170 -0
  57. package/dist/grants.d.ts +69 -0
  58. package/dist/grants.js +206 -0
  59. package/dist/image/bitmap.d.ts +11 -0
  60. package/dist/image/bitmap.js +172 -0
  61. package/dist/image/jpeg.d.ts +10 -0
  62. package/dist/image/jpeg.js +697 -0
  63. package/dist/image/png.d.ts +8 -0
  64. package/dist/image/png.js +282 -0
  65. package/dist/image/transform.d.ts +20 -0
  66. package/dist/image/transform.js +52 -0
  67. package/dist/image/worker.d.ts +1 -0
  68. package/dist/image/worker.js +11 -0
  69. package/dist/images.d.ts +39 -0
  70. package/dist/images.js +283 -0
  71. package/dist/index.d.ts +64 -0
  72. package/dist/index.js +31 -0
  73. package/dist/inspect-hub.d.ts +42 -0
  74. package/dist/inspect-hub.js +14 -0
  75. package/dist/inspector.d.ts +51 -0
  76. package/dist/inspector.js +193 -0
  77. package/dist/job-admin.d.ts +55 -0
  78. package/dist/job-admin.js +89 -0
  79. package/dist/jobs.d.ts +222 -0
  80. package/dist/jobs.js +789 -0
  81. package/dist/live.d.ts +72 -0
  82. package/dist/live.js +481 -0
  83. package/dist/load-config.d.ts +2 -0
  84. package/dist/load-config.js +41 -0
  85. package/dist/log.d.ts +20 -0
  86. package/dist/log.js +143 -0
  87. package/dist/mail.d.ts +189 -0
  88. package/dist/mail.js +999 -0
  89. package/dist/media.d.ts +129 -0
  90. package/dist/media.js +470 -0
  91. package/dist/meta.d.ts +54 -0
  92. package/dist/meta.js +146 -0
  93. package/dist/middleware.d.ts +20 -0
  94. package/dist/middleware.js +101 -0
  95. package/dist/migrate.d.ts +12 -0
  96. package/dist/migrate.js +88 -0
  97. package/dist/node.d.ts +26 -0
  98. package/dist/node.js +96 -0
  99. package/dist/notice.d.ts +21 -0
  100. package/dist/notice.js +48 -0
  101. package/dist/npm.d.ts +16 -0
  102. package/dist/npm.js +347 -0
  103. package/dist/oauth.d.ts +76 -0
  104. package/dist/oauth.js +408 -0
  105. package/dist/otel.d.ts +57 -0
  106. package/dist/otel.js +293 -0
  107. package/dist/passkey.d.ts +99 -0
  108. package/dist/passkey.js +572 -0
  109. package/dist/password.d.ts +8 -0
  110. package/dist/password.js +65 -0
  111. package/dist/pdf.d.ts +46 -0
  112. package/dist/pdf.js +332 -0
  113. package/dist/personal-data.d.ts +22 -0
  114. package/dist/personal-data.js +148 -0
  115. package/dist/pointers.d.ts +9 -0
  116. package/dist/pointers.js +148 -0
  117. package/dist/postcss.d.ts +3 -0
  118. package/dist/postcss.js +89 -0
  119. package/dist/presence.d.ts +27 -0
  120. package/dist/presence.js +161 -0
  121. package/dist/preview.d.ts +25 -0
  122. package/dist/preview.js +353 -0
  123. package/dist/production.d.ts +16 -0
  124. package/dist/production.js +119 -0
  125. package/dist/project.d.ts +23 -0
  126. package/dist/project.js +536 -0
  127. package/dist/regions.d.ts +47 -0
  128. package/dist/regions.js +511 -0
  129. package/dist/replay.d.ts +44 -0
  130. package/dist/replay.js +259 -0
  131. package/dist/router.d.ts +29 -0
  132. package/dist/router.js +146 -0
  133. package/dist/rules-diff.d.ts +18 -0
  134. package/dist/rules-diff.js +165 -0
  135. package/dist/rules-test.d.ts +17 -0
  136. package/dist/rules-test.js +108 -0
  137. package/dist/saml.d.ts +160 -0
  138. package/dist/saml.js +959 -0
  139. package/dist/seal.d.ts +35 -0
  140. package/dist/seal.js +107 -0
  141. package/dist/secret.d.ts +14 -0
  142. package/dist/secret.js +110 -0
  143. package/dist/security.d.ts +53 -0
  144. package/dist/security.js +250 -0
  145. package/dist/seed.d.ts +16 -0
  146. package/dist/seed.js +94 -0
  147. package/dist/seo.d.ts +42 -0
  148. package/dist/seo.js +175 -0
  149. package/dist/settings.d.ts +79 -0
  150. package/dist/settings.js +212 -0
  151. package/dist/shared.d.ts +15 -0
  152. package/dist/shared.js +283 -0
  153. package/dist/shell.d.ts +12 -0
  154. package/dist/shell.js +118 -0
  155. package/dist/storage.d.ts +13 -0
  156. package/dist/storage.js +55 -0
  157. package/dist/support.d.ts +83 -0
  158. package/dist/support.js +236 -0
  159. package/dist/sync.d.ts +35 -0
  160. package/dist/sync.js +167 -0
  161. package/dist/tailwind.d.ts +14 -0
  162. package/dist/tailwind.js +117 -0
  163. package/dist/two-factor.d.ts +103 -0
  164. package/dist/two-factor.js +435 -0
  165. package/dist/uploads.d.ts +185 -0
  166. package/dist/uploads.js +991 -0
  167. package/dist/webhooks.d.ts +169 -0
  168. package/dist/webhooks.js +758 -0
  169. package/dist/webpush.d.ts +97 -0
  170. package/dist/webpush.js +372 -0
  171. package/dist/wizard.d.ts +141 -0
  172. package/dist/wizard.js +410 -0
  173. package/dist/workflows.d.ts +134 -0
  174. package/dist/workflows.js +511 -0
  175. package/package.json +186 -4
package/dist/jobs.js ADDED
@@ -0,0 +1,789 @@
1
+ import { defineModel } from "@weft-framework/data";
2
+ import { decodeJson } from "./action.js";
3
+ import { authState } from "./auth.js";
4
+ const ENDED = new Set(["done", "dead", "cancelled"]);
5
+ export class MemoryJobStore {
6
+ #jobs = new Map();
7
+ #keys = new Map();
8
+ add(r) {
9
+ if (r.key !== null && this.#keys.has(r.key))
10
+ return false;
11
+ this.#jobs.set(r.id, structuredClone(r));
12
+ if (r.key !== null)
13
+ this.#keys.set(r.key, r.id);
14
+ return true;
15
+ }
16
+ get(id) {
17
+ const r = this.#jobs.get(id);
18
+ return r ? structuredClone(r) : null;
19
+ }
20
+ claim(now, worker, leaseUntil) {
21
+ let best = null;
22
+ for (const r of this.#jobs.values()) {
23
+ const due = (r.status === "queued" && r.runAt <= now) || (r.status === "running" && r.leaseUntil < now);
24
+ if (due && (!best || r.runAt < best.runAt))
25
+ best = r;
26
+ }
27
+ if (!best)
28
+ return null;
29
+ Object.assign(best, { status: "running", attempts: best.attempts + 1, worker, leaseUntil, updated: now });
30
+ return structuredClone(best);
31
+ }
32
+ extend(id, worker, leaseUntil, progress) {
33
+ const r = this.#jobs.get(id);
34
+ if (!r || r.status !== "running" || r.worker !== worker)
35
+ return false;
36
+ r.leaseUntil = leaseUntil;
37
+ if (progress !== null)
38
+ r.progress = progress;
39
+ return true;
40
+ }
41
+ settle(id, worker, to, now) {
42
+ const r = this.#jobs.get(id);
43
+ if (!r || r.status !== "running" || r.worker !== worker)
44
+ return false;
45
+ r.status = to.status;
46
+ r.worker = null;
47
+ r.leaseUntil = 0;
48
+ r.updated = now;
49
+ if (to.runAt !== undefined)
50
+ r.runAt = to.runAt;
51
+ if (to.error !== undefined)
52
+ r.error = to.error;
53
+ if (ENDED.has(r.status))
54
+ this.#release(r);
55
+ return true;
56
+ }
57
+ move(id, from, to, now) {
58
+ const r = this.#jobs.get(id);
59
+ if (!r || !from.includes(r.status))
60
+ return false;
61
+ if (to.status === "queued" && r.key !== null && this.#keys.get(r.key) !== r.id) {
62
+ if (this.#keys.has(r.key))
63
+ return false;
64
+ this.#keys.set(r.key, r.id);
65
+ }
66
+ r.status = to.status;
67
+ r.updated = now;
68
+ if (to.runAt !== undefined)
69
+ r.runAt = to.runAt;
70
+ if (to.attempts !== undefined)
71
+ r.attempts = to.attempts;
72
+ if (ENDED.has(r.status))
73
+ this.#release(r);
74
+ return true;
75
+ }
76
+ #release(r) {
77
+ if (r.key !== null && !r.keepKey && this.#keys.get(r.key) === r.id)
78
+ this.#keys.delete(r.key);
79
+ }
80
+ list(f) {
81
+ const out = [...this.#jobs.values()].filter((r) => (!f.status || r.status === f.status) && (!f.name || r.name === f.name));
82
+ out.sort((a, b) => b.created - a.created || (a.id < b.id ? 1 : -1));
83
+ return out.slice(0, f.limit ?? 100).map((r) => structuredClone(r));
84
+ }
85
+ counts() {
86
+ const c = { queued: 0, running: 0, done: 0, dead: 0, cancelled: 0 };
87
+ for (const r of this.#jobs.values())
88
+ c[r.status]++;
89
+ return c;
90
+ }
91
+ sweep(before) {
92
+ for (const [id, r] of this.#jobs) {
93
+ if ((r.status === "done" || r.status === "cancelled") && r.updated < before) {
94
+ this.#jobs.delete(id);
95
+ if (r.key !== null && this.#keys.get(r.key) === id)
96
+ this.#keys.delete(r.key);
97
+ }
98
+ }
99
+ }
100
+ }
101
+ function jobFromRow(r) {
102
+ if (!r)
103
+ return null;
104
+ return {
105
+ id: String(r.id),
106
+ name: String(r.name),
107
+ args: JSON.parse(String(r.args)),
108
+ actor: r.actor === null || r.actor === undefined ? null : JSON.parse(String(r.actor)),
109
+ status: String(r.status),
110
+ attempts: Number(r.attempts),
111
+ maxAttempts: Number(r.max_attempts),
112
+ runAt: Number(r.run_at),
113
+ created: Number(r.created),
114
+ updated: Number(r.updated),
115
+ worker: r.worker === null || r.worker === undefined ? null : String(r.worker),
116
+ leaseUntil: Number(r.lease_until),
117
+ error: r.error === null || r.error === undefined ? null : String(r.error),
118
+ key: r.job_key === null || r.job_key === undefined ? null : String(r.job_key),
119
+ keepKey: r.keep_key === true || Number(r.keep_key) === 1,
120
+ progress: r.progress === null || r.progress === undefined ? null : Number(r.progress),
121
+ };
122
+ }
123
+ export class SqlJobStore {
124
+ #db;
125
+ #t;
126
+ constructor(db, table = "weft_jobs") {
127
+ if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(table))
128
+ throw new Error(`@weft-framework/server: not a valid table name: ${table}`);
129
+ this.#db = db;
130
+ this.#t = `"${table}"`;
131
+ db.exec(`CREATE TABLE IF NOT EXISTS ${this.#t} (id TEXT PRIMARY KEY NOT NULL, name TEXT NOT NULL, args TEXT NOT NULL, ` +
132
+ `actor TEXT, status TEXT NOT NULL, attempts INTEGER NOT NULL, max_attempts INTEGER NOT NULL, run_at INTEGER NOT NULL, ` +
133
+ `created INTEGER NOT NULL, updated INTEGER NOT NULL, worker TEXT, lease_until INTEGER NOT NULL, error TEXT, ` +
134
+ `job_key TEXT UNIQUE, keep_key INTEGER NOT NULL, progress REAL)`);
135
+ db.exec(`CREATE INDEX IF NOT EXISTS "${table}_due" ON ${this.#t} (status, run_at)`);
136
+ }
137
+ add(r) {
138
+ return (this.#db
139
+ .prepare(`INSERT INTO ${this.#t} (id, name, args, actor, status, attempts, max_attempts, run_at, created, updated, worker, ` +
140
+ `lease_until, error, job_key, keep_key, progress) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ` +
141
+ `ON CONFLICT DO NOTHING RETURNING id`)
142
+ .all(r.id, r.name, JSON.stringify(r.args), r.actor ? JSON.stringify(r.actor) : null, r.status, r.attempts, r.maxAttempts, r.runAt, r.created, r.updated, r.worker, r.leaseUntil, r.error, r.key, r.keepKey ? 1 : 0, r.progress).length === 1);
143
+ }
144
+ get(id) {
145
+ return jobFromRow(this.#db.prepare(`SELECT * FROM ${this.#t} WHERE id = ?`).all(id)[0]);
146
+ }
147
+ claim(now, worker, leaseUntil) {
148
+ const due = `((status = 'queued' AND run_at <= ?) OR (status = 'running' AND lease_until < ?))`;
149
+ const row = this.#db
150
+ .prepare(`UPDATE ${this.#t} SET status = 'running', attempts = attempts + 1, worker = ?, lease_until = ?, updated = ? ` +
151
+ `WHERE id = (SELECT id FROM ${this.#t} WHERE ${due} ORDER BY run_at, id LIMIT 1) AND ${due} RETURNING *`)
152
+ .all(worker, leaseUntil, now, now, now, now, now)[0];
153
+ return jobFromRow(row);
154
+ }
155
+ extend(id, worker, leaseUntil, progress) {
156
+ return (this.#db
157
+ .prepare(`UPDATE ${this.#t} SET lease_until = ?, progress = COALESCE(?, progress) WHERE id = ? AND status = 'running' AND worker = ? RETURNING id`)
158
+ .all(leaseUntil, progress, id, worker).length === 1);
159
+ }
160
+ settle(id, worker, to, now) {
161
+ const ended = to.status !== "queued";
162
+ return (this.#db
163
+ .prepare(`UPDATE ${this.#t} SET status = ?, worker = NULL, lease_until = 0, updated = ?, run_at = COALESCE(?, run_at), ` +
164
+ `error = CASE WHEN ? THEN ? ELSE error END, ` +
165
+ `job_key = CASE WHEN ? AND keep_key = 0 THEN NULL ELSE job_key END ` +
166
+ `WHERE id = ? AND status = 'running' AND worker = ? RETURNING id`)
167
+ .all(to.status, now, to.runAt ?? null, to.error !== undefined ? 1 : 0, to.error ?? null, ended ? 1 : 0, id, worker).length === 1);
168
+ }
169
+ move(id, from, to, now) {
170
+ const marks = from.map(() => "?").join(", ");
171
+ return (this.#db
172
+ .prepare(`UPDATE ${this.#t} SET status = ?, updated = ?, run_at = COALESCE(?, run_at), attempts = COALESCE(?, attempts), ` +
173
+ `job_key = CASE WHEN ? AND keep_key = 0 THEN NULL ELSE job_key END ` +
174
+ `WHERE id = ? AND status IN (${marks}) RETURNING id`)
175
+ .all(to.status, now, to.runAt ?? null, to.attempts ?? null, to.status === "cancelled" ? 1 : 0, id, ...from).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) => jobFromRow(r));
188
+ }
189
+ counts() {
190
+ const c = { queued: 0, running: 0, done: 0, dead: 0, cancelled: 0 };
191
+ for (const r of this.#db.prepare(`SELECT status, count(*) AS n FROM ${this.#t} GROUP BY status`).all()) {
192
+ if (r.status in c)
193
+ c[r.status] = Number(r.n);
194
+ }
195
+ return c;
196
+ }
197
+ sweep(before) {
198
+ this.#db.prepare(`DELETE FROM ${this.#t} WHERE status IN ('done', 'cancelled') AND updated < ?`).run(before);
199
+ }
200
+ }
201
+ export class PgJobStore {
202
+ #db;
203
+ #t;
204
+ #table;
205
+ #ready = null;
206
+ constructor(db, table = "weft_jobs") {
207
+ if (!db || typeof db.query !== "function")
208
+ throw new TypeError("PgJobStore: needs a Postgres pool (`query(text, values)`)");
209
+ if (!/^[A-Za-z_][A-Za-z0-9_]{0,50}$/.test(table))
210
+ throw new Error(`@weft-framework/server: not a valid table name: ${table}`);
211
+ this.#db = db;
212
+ this.#table = table;
213
+ this.#t = `"${table}"`;
214
+ }
215
+ ready() {
216
+ this.#ready ??= (async () => {
217
+ await this.#db.query(`CREATE TABLE IF NOT EXISTS ${this.#t} (id TEXT PRIMARY KEY NOT NULL, name TEXT NOT NULL, args TEXT NOT NULL, ` +
218
+ `actor TEXT, status TEXT NOT NULL, attempts INTEGER NOT NULL, max_attempts INTEGER NOT NULL, run_at BIGINT NOT NULL, ` +
219
+ `created BIGINT NOT NULL, updated BIGINT NOT NULL, worker TEXT, lease_until BIGINT NOT NULL, error TEXT, ` +
220
+ `job_key TEXT UNIQUE, keep_key BOOLEAN NOT NULL, progress DOUBLE PRECISION)`);
221
+ await this.#db.query(`CREATE INDEX IF NOT EXISTS "${this.#table}_due" ON ${this.#t} (status, run_at)`);
222
+ })().catch((err) => {
223
+ this.#ready = null;
224
+ throw err;
225
+ });
226
+ return this.#ready;
227
+ }
228
+ async #q(text, values = []) {
229
+ await this.ready();
230
+ return (await this.#db.query(text, values)).rows;
231
+ }
232
+ async add(r) {
233
+ const rows = await this.#q(`INSERT INTO ${this.#t} (id, name, args, actor, status, attempts, max_attempts, run_at, created, updated, worker, ` +
234
+ `lease_until, error, job_key, keep_key, progress) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16) ` +
235
+ `ON CONFLICT DO NOTHING RETURNING id`, [
236
+ r.id, r.name, JSON.stringify(r.args), r.actor ? JSON.stringify(r.actor) : null, r.status, r.attempts, r.maxAttempts,
237
+ r.runAt, r.created, r.updated, r.worker, r.leaseUntil, r.error, r.key, r.keepKey, r.progress,
238
+ ]);
239
+ return rows.length === 1;
240
+ }
241
+ async get(id) {
242
+ return jobFromRow((await this.#q(`SELECT * FROM ${this.#t} WHERE id = $1`, [id]))[0]);
243
+ }
244
+ async claim(now, worker, leaseUntil) {
245
+ const rows = await this.#q(`UPDATE ${this.#t} SET status = 'running', attempts = attempts + 1, worker = $1, lease_until = $2, updated = $3 ` +
246
+ `WHERE id = (SELECT id FROM ${this.#t} WHERE (status = 'queued' AND run_at <= $3) OR (status = 'running' AND lease_until < $3) ` +
247
+ `ORDER BY run_at, id LIMIT 1 FOR UPDATE SKIP LOCKED) RETURNING *`, [worker, leaseUntil, now]);
248
+ return jobFromRow(rows[0]);
249
+ }
250
+ async extend(id, worker, leaseUntil, progress) {
251
+ const rows = await this.#q(`UPDATE ${this.#t} SET lease_until = $1, progress = COALESCE($2::double precision, progress) ` +
252
+ `WHERE id = $3 AND status = 'running' AND worker = $4 RETURNING id`, [leaseUntil, progress, id, worker]);
253
+ return rows.length === 1;
254
+ }
255
+ async settle(id, worker, to, now) {
256
+ const rows = await this.#q(`UPDATE ${this.#t} SET status = $1, worker = NULL, lease_until = 0, updated = $2, run_at = COALESCE($3::bigint, run_at), ` +
257
+ `error = CASE WHEN $4::boolean THEN $5::text ELSE error END, ` +
258
+ `job_key = CASE WHEN $6::boolean AND NOT keep_key THEN NULL ELSE job_key END ` +
259
+ `WHERE id = $7 AND status = 'running' AND worker = $8 RETURNING id`, [to.status, now, to.runAt ?? null, to.error !== undefined, to.error ?? null, to.status !== "queued", id, worker]);
260
+ return rows.length === 1;
261
+ }
262
+ async move(id, from, to, now) {
263
+ const rows = await this.#q(`UPDATE ${this.#t} SET status = $1, updated = $2, run_at = COALESCE($3::bigint, run_at), attempts = COALESCE($4::integer, attempts), ` +
264
+ `job_key = CASE WHEN $5::boolean AND NOT keep_key THEN NULL ELSE job_key END ` +
265
+ `WHERE id = $6 AND status = ANY($7::text[]) RETURNING id`, [to.status, now, to.runAt ?? null, to.attempts ?? null, to.status === "cancelled", id, [...from]]);
266
+ return rows.length === 1;
267
+ }
268
+ async list(f) {
269
+ const where = [];
270
+ const args = [];
271
+ if (f.status)
272
+ (args.push(f.status), where.push(`status = $${args.length}`));
273
+ if (f.name)
274
+ (args.push(f.name), where.push(`name = $${args.length}`));
275
+ args.push(Math.min(Math.max(1, f.limit ?? 100), 1000));
276
+ const rows = await this.#q(`SELECT * FROM ${this.#t}${where.length ? ` WHERE ${where.join(" AND ")}` : ""} ORDER BY created DESC, id DESC LIMIT $${args.length}`, args);
277
+ return rows.map((r) => jobFromRow(r));
278
+ }
279
+ async counts() {
280
+ const c = { queued: 0, running: 0, done: 0, dead: 0, cancelled: 0 };
281
+ for (const r of await this.#q(`SELECT status, count(*) AS n FROM ${this.#t} GROUP BY status`)) {
282
+ const s = String(r.status);
283
+ if (s in c)
284
+ c[s] = Number(r.n);
285
+ }
286
+ return c;
287
+ }
288
+ async sweep(before) {
289
+ await this.#q(`DELETE FROM ${this.#t} WHERE status IN ('done', 'cancelled') AND updated < $1`, [before]);
290
+ }
291
+ }
292
+ const JOB = Symbol.for("weft.job");
293
+ export class JobError extends Error {
294
+ retry;
295
+ constructor(message, opts = {}) {
296
+ super(message, opts.cause !== undefined ? { cause: opts.cause } : undefined);
297
+ this.name = "JobError";
298
+ this.retry = opts.retry !== false;
299
+ }
300
+ }
301
+ function range(what, v, lo, hi) {
302
+ if (!Number.isInteger(v) || v < lo || v > hi)
303
+ throw new Error(`${what} is ${lo} to ${hi}`);
304
+ return v;
305
+ }
306
+ export function defineJob(name, params, run, opts = {}) {
307
+ if (!/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name))
308
+ throw new Error(`defineJob: not a valid job name: ${name}`);
309
+ if (typeof run !== "function")
310
+ throw new Error(`defineJob ${name}: the body is a function`);
311
+ if (opts.backoff !== undefined && typeof opts.backoff !== "function")
312
+ throw new Error(`defineJob ${name}: backoff is a function`);
313
+ return Object.freeze({
314
+ [JOB]: true,
315
+ name,
316
+ params: Object.freeze([...params]),
317
+ options: Object.freeze({
318
+ attempts: range(`defineJob ${name}: attempts`, opts.attempts ?? 5, 1, 100),
319
+ timeoutMs: range(`defineJob ${name}: timeoutMs`, opts.timeoutMs ?? 10 * 60_000, 1000, 24 * 3600_000),
320
+ ...(opts.backoff ? { backoff: opts.backoff } : {}),
321
+ }),
322
+ run,
323
+ });
324
+ }
325
+ export function isJob(v) {
326
+ return typeof v === "object" && v !== null && v[JOB] === true;
327
+ }
328
+ const MACROS = {
329
+ "@yearly": "0 0 1 1 *",
330
+ "@annually": "0 0 1 1 *",
331
+ "@monthly": "0 0 1 * *",
332
+ "@weekly": "0 0 * * 0",
333
+ "@daily": "0 0 * * *",
334
+ "@midnight": "0 0 * * *",
335
+ "@hourly": "0 * * * *",
336
+ };
337
+ const MONTHS = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];
338
+ const DAYS = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"];
339
+ export function parseCron(expression) {
340
+ if (typeof expression !== "string")
341
+ throw new Error("cron: an expression is a string");
342
+ const src = MACROS[expression.trim().toLowerCase()] ?? expression.trim();
343
+ const parts = src.split(/\s+/);
344
+ if (parts.length !== 5)
345
+ throw new Error(`cron: "${expression}" needs five fields (minute hour day month weekday)`);
346
+ const field = (text, lo, hi, names, offset = 0) => {
347
+ const out = new Set();
348
+ for (const item of text.toLowerCase().split(",")) {
349
+ const m = /^(\*|[a-z0-9]+(?:-[a-z0-9]+)?)(?:\/(\d+))?$/.exec(item);
350
+ if (!m)
351
+ throw new Error(`cron: bad field "${text}" in "${expression}"`);
352
+ const value = (s) => {
353
+ const i = names ? names.indexOf(s) : -1;
354
+ const n = i >= 0 ? i + offset : /^\d+$/.test(s) ? Number(s) : NaN;
355
+ if (!Number.isInteger(n) || n < lo || n > hi)
356
+ throw new Error(`cron: ${s} is out of range in "${expression}"`);
357
+ return n;
358
+ };
359
+ let a = lo;
360
+ let b = hi;
361
+ if (m[1] !== "*") {
362
+ const [x, y] = m[1].split("-");
363
+ a = value(x);
364
+ b = y !== undefined ? value(y) : m[2] !== undefined ? hi : a;
365
+ if (b < a)
366
+ throw new Error(`cron: range ${m[1]} runs backwards in "${expression}"`);
367
+ }
368
+ const step = m[2] !== undefined ? Number(m[2]) : 1;
369
+ if (step < 1 || step > hi - lo + 1)
370
+ throw new Error(`cron: bad step in "${expression}"`);
371
+ for (let v = a; v <= b; v += step)
372
+ out.add(v);
373
+ }
374
+ return out;
375
+ };
376
+ const minutes = field(parts[0], 0, 59);
377
+ const hours = field(parts[1], 0, 23);
378
+ const doms = field(parts[2], 1, 31);
379
+ const months = field(parts[3], 1, 12, MONTHS, 1);
380
+ const dows = field(parts[4], 0, 7, DAYS);
381
+ if (dows.has(7))
382
+ dows.add(0);
383
+ const domAny = parts[2] === "*" || parts[2].startsWith("*/") && doms.size === 31;
384
+ const dowAny = parts[4] === "*" || parts[4].startsWith("*/") && dows.size >= 7;
385
+ const dayMatches = (d) => {
386
+ const dom = doms.has(d.getUTCDate());
387
+ const dow = dows.has(d.getUTCDay());
388
+ if (domAny && dowAny)
389
+ return true;
390
+ if (domAny)
391
+ return dow;
392
+ if (dowAny)
393
+ return dom;
394
+ return dom || dow;
395
+ };
396
+ return Object.freeze({
397
+ expression,
398
+ next(after) {
399
+ let t = Math.floor(after / 60_000) * 60_000 + 60_000;
400
+ for (let i = 0; i < 200_000; i++) {
401
+ const d = new Date(t);
402
+ if (!months.has(d.getUTCMonth() + 1)) {
403
+ t = Date.UTC(d.getUTCFullYear(), d.getUTCMonth() + 1, 1);
404
+ continue;
405
+ }
406
+ if (!dayMatches(d)) {
407
+ t = Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate() + 1);
408
+ continue;
409
+ }
410
+ if (!hours.has(d.getUTCHours())) {
411
+ t = Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), d.getUTCHours() + 1);
412
+ continue;
413
+ }
414
+ if (!minutes.has(d.getUTCMinutes())) {
415
+ t += 60_000;
416
+ continue;
417
+ }
418
+ return t;
419
+ }
420
+ throw new Error(`cron: "${expression}" never fires`);
421
+ },
422
+ });
423
+ }
424
+ class Tracker {
425
+ tracker = true;
426
+ }
427
+ const TRACKER = new Tracker();
428
+ const PROGRESS_STATUS = { queued: "queued", running: "running", done: "done", dead: "failed", cancelled: "cancelled" };
429
+ function randomId() {
430
+ const b = crypto.getRandomValues(new Uint8Array(16));
431
+ return [...b].map((x) => x.toString(16).padStart(2, "0")).join("");
432
+ }
433
+ export class Jobs {
434
+ #jobs = new Map();
435
+ #store;
436
+ #user;
437
+ #concurrency;
438
+ #pollMs;
439
+ #leaseMs;
440
+ #keepDone;
441
+ #onError;
442
+ #telemetry;
443
+ #now;
444
+ #random;
445
+ #worker = randomId();
446
+ #schedules = [];
447
+ #running = new Set();
448
+ #timer = null;
449
+ #started = false;
450
+ #swept = 0;
451
+ models = {};
452
+ #progress = null;
453
+ constructor(o) {
454
+ const list = Array.isArray(o.jobs) ? o.jobs : Object.values(o.jobs ?? {}).filter(isJob);
455
+ for (const j of list) {
456
+ if (!isJob(j))
457
+ throw new Error("createJobs: `jobs` holds jobs made with defineJob");
458
+ if (this.#jobs.has(j.name))
459
+ throw new Error(`createJobs: two jobs are called ${j.name}`);
460
+ this.#jobs.set(j.name, j);
461
+ }
462
+ this.#store = o.store ?? new MemoryJobStore();
463
+ this.#user = o.user;
464
+ this.#concurrency = range("createJobs: concurrency", o.concurrency ?? 4, 1, 64);
465
+ this.#pollMs = range("createJobs: pollMs", o.pollMs ?? 1000, 10, 60_000);
466
+ this.#leaseMs = range("createJobs: leaseMs", o.leaseMs ?? 60_000, 5000, 3600_000);
467
+ this.#keepDone = range("createJobs: keepDoneMs", o.keepDoneMs ?? 24 * 3600_000, 60_000, 90 * 24 * 3600_000);
468
+ this.#onError = o.onError ?? ((err, job) => console.error(`[weft] job ${job?.name ?? "queue"}:`, err));
469
+ this.#telemetry = o.telemetry ?? null;
470
+ this.#now = o.now ?? Date.now;
471
+ this.#random = o.random ?? Math.random;
472
+ if (o.track !== undefined) {
473
+ const prefix = o.track?.prefix ?? "Weft";
474
+ if (!/^[A-Z][A-Za-z0-9]{0,31}$/.test(prefix))
475
+ throw new Error("createJobs: track.prefix is a model name");
476
+ const mine = (a, r) => a instanceof Tracker || (typeof a === "object" && a !== null && typeof a.id === "string" && a.id === r.user);
477
+ const where = { or: [{ cmp: "==", l: { field: ["user"] }, r: { actor: ["id"] } }, { truthy: { actor: ["tracker"] } }] };
478
+ this.#progress = defineModel(`${prefix}JobProgress`, {
479
+ fields: { id: "string", user: "string", name: "string", status: "string", progress: "number | null", updated: "number" },
480
+ read: mine,
481
+ write: (a) => a instanceof Tracker,
482
+ readWhere: where,
483
+ writeWhere: { truthy: { actor: ["tracker"] } },
484
+ });
485
+ this.models.JobProgress = this.#progress;
486
+ }
487
+ const now = this.#now();
488
+ const names = new Set();
489
+ for (const s of o.schedule ?? []) {
490
+ if (!isJob(s.job) || this.#jobs.get(s.job.name) !== s.job)
491
+ throw new Error("createJobs: a schedule's job must be one of `jobs`");
492
+ const name = s.name ?? s.job.name;
493
+ if (names.has(name))
494
+ throw new Error(`createJobs: two schedules are called ${name} (give one a name)`);
495
+ names.add(name);
496
+ const cron = parseCron(s.cron);
497
+ this.#schedules.push({ name, cron, job: s.job, args: this.#args(s.job, s.args ?? {}), as: s.as ?? null, next: cron.next(now) });
498
+ }
499
+ }
500
+ #args(job, args) {
501
+ const d = decodeJson(job.params, args);
502
+ if (!d.ok)
503
+ throw new TypeError(`job ${job.name}: ${d.error}`);
504
+ return JSON.parse(JSON.stringify(d.args.map((a) => (a === undefined ? null : a))));
505
+ }
506
+ async enqueue(ctx, job, args = {}, o = {}) {
507
+ const j = this.#jobs.get(job?.name);
508
+ if (!j || j !== job)
509
+ throw new Error(`jobs: ${job?.name ?? "that"} is not one of this queue's jobs`);
510
+ const stored = this.#args(j, args);
511
+ let actor = null;
512
+ if (o.as !== undefined) {
513
+ if (o.as !== null && (typeof o.as.user !== "string" || o.as.user === ""))
514
+ throw new TypeError("jobs: `as` needs a user id");
515
+ actor = o.as === null ? null : { user: o.as.user, ...(o.as.tenant !== undefined ? { tenant: o.as.tenant } : {}) };
516
+ }
517
+ else if (ctx) {
518
+ const s = await authState(ctx);
519
+ if (s && !s.pending)
520
+ actor = { user: s.user, ...(s.tenant !== undefined ? { tenant: s.tenant } : {}) };
521
+ }
522
+ if (o.key !== undefined && (typeof o.key !== "string" || o.key === "" || o.key.length > 256 || o.key.startsWith("cron:"))) {
523
+ throw new TypeError("jobs: `key` is 1-256 characters, not starting cron:");
524
+ }
525
+ if (o.track !== undefined && typeof o.track !== "boolean")
526
+ throw new TypeError("jobs: `track` is true or false");
527
+ if (o.track && !this.#progress)
528
+ throw new TypeError("jobs: `track` needs createJobs({ track: {} })");
529
+ if (o.track && !actor)
530
+ throw new TypeError("jobs: a tracked job needs the user who follows it (a signed-in ctx, or `as`)");
531
+ const now = this.#now();
532
+ const runAt = o.runAt ?? now + Math.max(0, o.delayMs ?? 0);
533
+ if (!Number.isFinite(runAt))
534
+ throw new TypeError("jobs: bad runAt");
535
+ const id = await this.#add(j, stored, actor, runAt, o.key ?? null, false);
536
+ if (id && o.track && actor) {
537
+ this.#progress.as(TRACKER).create({ id, user: actor.user, name: j.name, status: "queued", progress: null, updated: now });
538
+ }
539
+ return id;
540
+ }
541
+ progress(actor, id) {
542
+ if (!this.#progress || typeof id !== "string")
543
+ return null;
544
+ const r = this.#progress.as(actor).get(id);
545
+ return r ? { name: r.name, status: r.status, progress: r.progress } : null;
546
+ }
547
+ names() {
548
+ return [...this.#jobs.keys()];
549
+ }
550
+ #track(id, patch) {
551
+ if (!this.#progress)
552
+ return;
553
+ try {
554
+ this.#progress.as(TRACKER).update((r) => r.id === id, { ...patch, updated: this.#now() });
555
+ }
556
+ catch (err) {
557
+ this.#onError(err, null);
558
+ }
559
+ }
560
+ async #add(j, args, actor, runAt, key, keepKey) {
561
+ const now = this.#now();
562
+ const id = randomId();
563
+ const added = await this.#store.add({
564
+ id, name: j.name, args, actor, status: "queued", attempts: 0, maxAttempts: j.options.attempts, runAt,
565
+ created: now, updated: now, worker: null, leaseUntil: 0, error: null, key, keepKey, progress: null,
566
+ });
567
+ if (added)
568
+ this.#wake();
569
+ return added ? id : false;
570
+ }
571
+ get(id) {
572
+ return Promise.resolve(this.#store.get(id)).then((r) => r ?? null);
573
+ }
574
+ list(f = {}) {
575
+ return Promise.resolve(this.#store.list(f));
576
+ }
577
+ counts() {
578
+ return Promise.resolve(this.#store.counts());
579
+ }
580
+ async retry(id) {
581
+ const ok = await this.#store.move(id, ["dead", "cancelled"], { status: "queued", runAt: this.#now(), attempts: 0 }, this.#now());
582
+ if (ok) {
583
+ this.#track(id, { status: "queued", progress: null });
584
+ this.#wake();
585
+ }
586
+ return ok;
587
+ }
588
+ async cancel(id) {
589
+ const ok = await this.#store.move(id, ["queued", "dead"], { status: "cancelled" }, this.#now());
590
+ if (ok)
591
+ this.#track(id, { status: "cancelled" });
592
+ return ok;
593
+ }
594
+ start() {
595
+ if (this.#started)
596
+ return;
597
+ this.#started = true;
598
+ this.#wake();
599
+ }
600
+ async stop() {
601
+ this.#started = false;
602
+ if (this.#timer)
603
+ clearTimeout(this.#timer);
604
+ this.#timer = null;
605
+ await Promise.allSettled([...this.#running]);
606
+ }
607
+ #wake() {
608
+ if (!this.#started)
609
+ return;
610
+ if (this.#timer)
611
+ clearTimeout(this.#timer);
612
+ this.#timer = setTimeout(() => {
613
+ this.#timer = null;
614
+ this.tick()
615
+ .catch((err) => this.#onError(err, null))
616
+ .finally(() => {
617
+ if (this.#started && !this.#timer) {
618
+ this.#timer = setTimeout(() => this.#wake(), this.#pollMs);
619
+ this.#timer.unref?.();
620
+ }
621
+ });
622
+ }, 0);
623
+ this.#timer.unref?.();
624
+ }
625
+ async tick() {
626
+ const now = this.#now();
627
+ for (const s of this.#schedules) {
628
+ if (s.next > now)
629
+ continue;
630
+ let slot = s.next;
631
+ for (let n = s.cron.next(slot); n <= now; n = s.cron.next(slot))
632
+ slot = n;
633
+ s.next = s.cron.next(slot);
634
+ await this.#add(s.job, s.args, s.as, slot, `cron:${s.name}:${slot}`, true);
635
+ }
636
+ if (now - this.#swept > 60_000) {
637
+ this.#swept = now;
638
+ await this.#store.sweep(now - this.#keepDone);
639
+ if (this.#progress) {
640
+ const before = now - this.#keepDone;
641
+ this.#progress.as(TRACKER).delete((r) => r.status !== "queued" && r.status !== "running" && r.updated < before);
642
+ }
643
+ }
644
+ const runs = [];
645
+ while (this.#running.size < this.#concurrency) {
646
+ const rec = await this.#store.claim(this.#now(), this.#worker, this.#now() + this.#leaseMs);
647
+ if (!rec)
648
+ break;
649
+ const p = this.#run(rec).finally(() => this.#running.delete(p));
650
+ this.#running.add(p);
651
+ runs.push(p);
652
+ }
653
+ await Promise.allSettled(runs);
654
+ }
655
+ async drain(max = 1000) {
656
+ for (let i = 0; i < max; i++) {
657
+ await this.tick();
658
+ if (this.#running.size > 0)
659
+ continue;
660
+ const due = (await this.list({ status: "queued", limit: 1000 })).some((r) => r.runAt <= this.#now());
661
+ if (!due)
662
+ return;
663
+ }
664
+ }
665
+ #backoff(job, attempt) {
666
+ if (job.options.backoff) {
667
+ const ms = job.options.backoff(attempt);
668
+ return Number.isFinite(ms) && ms >= 0 ? Math.min(ms, 7 * 24 * 3600_000) : 1000;
669
+ }
670
+ const base = Math.min(3600_000, 1000 * 2 ** Math.min(attempt - 1, 22));
671
+ return Math.round(base * (0.8 + 0.4 * this.#random()));
672
+ }
673
+ async #run(rec) {
674
+ const job = this.#jobs.get(rec.name);
675
+ if (!job) {
676
+ await this.#store.settle(rec.id, this.#worker, { status: "queued", runAt: this.#now() + this.#leaseMs }, this.#now());
677
+ return;
678
+ }
679
+ let span = null;
680
+ try {
681
+ span = this.#telemetry?.span?.(`job ${rec.name}`, {
682
+ kind: "consumer",
683
+ attributes: { "weft.job.name": rec.name, "weft.job.id": rec.id, "weft.job.attempt": rec.attempts },
684
+ metric: { name: "weft.job.duration", attributes: { "weft.job.name": rec.name } },
685
+ }) ?? null;
686
+ }
687
+ catch (err) {
688
+ this.#onError(err, rec);
689
+ }
690
+ const outcome = { value: "done", error: undefined };
691
+ try {
692
+ await this.#attempt(rec, job, outcome);
693
+ }
694
+ finally {
695
+ try {
696
+ span?.end({
697
+ ...(outcome.value !== "done" ? { error: outcome.error ?? new Error("failed") } : {}),
698
+ attributes: { "weft.job.outcome": outcome.value },
699
+ });
700
+ }
701
+ catch (err) {
702
+ this.#onError(err, rec);
703
+ }
704
+ }
705
+ }
706
+ async #attempt(rec, job, outcome) {
707
+ const fail = async (err, retry) => {
708
+ const message = (err instanceof Error ? err.message : String(err)).slice(0, 1000) || "failed";
709
+ const again = retry && rec.attempts < rec.maxAttempts;
710
+ outcome.value = again ? "retry" : "dead";
711
+ outcome.error = err;
712
+ const held = await this.#store.settle(rec.id, this.#worker, again ? { status: "queued", runAt: this.#now() + this.#backoff(job, rec.attempts), error: message } : { status: "dead", error: message }, this.#now());
713
+ if (held)
714
+ this.#track(rec.id, { status: PROGRESS_STATUS[again ? "queued" : "dead"] });
715
+ };
716
+ let actor = null;
717
+ try {
718
+ if (rec.actor) {
719
+ actor = this.#user ? await this.#user(rec.actor.user, rec.actor.tenant) : { id: rec.actor.user };
720
+ if (actor === null || actor === undefined) {
721
+ await fail(new Error(`the user who queued it (${rec.actor.user}) no longer exists`), false);
722
+ return;
723
+ }
724
+ }
725
+ }
726
+ catch (err) {
727
+ this.#onError(err, rec);
728
+ await fail(err, true);
729
+ return;
730
+ }
731
+ const controller = new AbortController();
732
+ let progress = null;
733
+ let shown = -1;
734
+ let shownAt = 0;
735
+ this.#track(rec.id, { status: "running" });
736
+ const renew = setInterval(() => {
737
+ Promise.resolve(this.#store.extend(rec.id, this.#worker, this.#now() + this.#leaseMs, progress))
738
+ .then((held) => {
739
+ if (!held)
740
+ controller.abort(new Error("lease lost"));
741
+ })
742
+ .catch((err) => this.#onError(err, rec));
743
+ }, Math.max(1000, Math.floor(this.#leaseMs / 3)));
744
+ renew.unref?.();
745
+ let timer;
746
+ const timeout = new Promise((_, reject) => {
747
+ timer = setTimeout(() => {
748
+ const err = new Error(`timed out after ${job.options.timeoutMs} ms`);
749
+ controller.abort(err);
750
+ reject(err);
751
+ }, job.options.timeoutMs);
752
+ timer.unref?.();
753
+ });
754
+ const ctx = {
755
+ user: actor,
756
+ job: { id: rec.id, name: rec.name, attempt: rec.attempts, maxAttempts: rec.maxAttempts },
757
+ signal: controller.signal,
758
+ progress: (f) => {
759
+ if (typeof f !== "number" || !Number.isFinite(f))
760
+ return;
761
+ progress = Math.min(1, Math.max(0, f));
762
+ const t = this.#now();
763
+ if (progress !== shown && (t - shownAt >= 500 || progress === 1)) {
764
+ shown = progress;
765
+ shownAt = t;
766
+ this.#track(rec.id, { progress });
767
+ }
768
+ },
769
+ };
770
+ const args = job.params.map((p, i) => (rec.args[i] === null && p.optional ? undefined : rec.args[i]));
771
+ try {
772
+ await Promise.race([Promise.resolve().then(() => job.run(ctx, ...args)), timeout]);
773
+ if (await this.#store.settle(rec.id, this.#worker, { status: "done", error: null }, this.#now())) {
774
+ this.#track(rec.id, { status: "done", ...(progress !== null ? { progress: 1 } : {}) });
775
+ }
776
+ }
777
+ catch (err) {
778
+ this.#onError(err, rec);
779
+ await fail(err, !(err instanceof JobError) || err.retry);
780
+ }
781
+ finally {
782
+ clearInterval(renew);
783
+ clearTimeout(timer);
784
+ }
785
+ }
786
+ }
787
+ export function createJobs(o) {
788
+ return new Jobs(o);
789
+ }