express-file-cluster 0.3.15 → 0.5.0
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 +71 -33
- package/dist/auth/index.cjs +12 -2
- package/dist/auth/index.cjs.map +1 -1
- package/dist/auth/index.d.cts +13 -2
- package/dist/auth/index.d.ts +13 -2
- package/dist/auth/index.js +12 -2
- package/dist/chunk-3VWD2ZED.js +60 -0
- package/dist/chunk-3VWD2ZED.js.map +1 -0
- package/dist/chunk-47K2532L.cjs +196 -0
- package/dist/chunk-47K2532L.cjs.map +1 -0
- package/dist/chunk-5PDAWMGF.js +438 -0
- package/dist/chunk-5PDAWMGF.js.map +1 -0
- package/dist/chunk-7YEX4MQV.cjs +38 -0
- package/dist/chunk-7YEX4MQV.cjs.map +1 -0
- package/dist/chunk-ALBDM5R3.cjs +60 -0
- package/dist/chunk-ALBDM5R3.cjs.map +1 -0
- package/dist/chunk-JJJJTACK.js +196 -0
- package/dist/chunk-JJJJTACK.js.map +1 -0
- package/dist/chunk-LJNSJFOL.js +38 -0
- package/dist/chunk-LJNSJFOL.js.map +1 -0
- package/dist/chunk-M7MUPWCE.js +116 -0
- package/dist/chunk-M7MUPWCE.js.map +1 -0
- package/dist/chunk-PMF4GMHD.js +154 -0
- package/dist/chunk-PMF4GMHD.js.map +1 -0
- package/dist/chunk-QTRGJ2EN.cjs +2635 -0
- package/dist/chunk-QTRGJ2EN.cjs.map +1 -0
- package/dist/chunk-RSBW2B3D.cjs +126 -0
- package/dist/chunk-RSBW2B3D.cjs.map +1 -0
- package/dist/chunk-RTFJ32T5.js +2635 -0
- package/dist/chunk-RTFJ32T5.js.map +1 -0
- package/dist/chunk-SFVEQ2T5.cjs +438 -0
- package/dist/chunk-SFVEQ2T5.cjs.map +1 -0
- package/dist/chunk-SZT36SGB.cjs +154 -0
- package/dist/chunk-SZT36SGB.cjs.map +1 -0
- package/dist/chunk-TI24L4XE.cjs.map +1 -1
- package/dist/chunk-ULQXD4YZ.cjs +116 -0
- package/dist/chunk-ULQXD4YZ.cjs.map +1 -0
- package/dist/chunk-VYHJX54R.js +126 -0
- package/dist/chunk-VYHJX54R.js.map +1 -0
- package/dist/cli/db-runner.cjs +497 -0
- package/dist/cli/db-runner.cjs.map +1 -0
- package/dist/cli/db-runner.d.cts +2 -0
- package/dist/cli/db-runner.d.ts +2 -0
- package/dist/cli/db-runner.js +497 -0
- package/dist/cli/db-runner.js.map +1 -0
- package/dist/cli/index.cjs +447 -69
- package/dist/cli/index.cjs.map +1 -1
- package/dist/cli/index.js +473 -95
- package/dist/cli/index.js.map +1 -1
- package/dist/docs/api-reference/auth.md +232 -0
- package/dist/docs/api-reference/compose.md +78 -0
- package/dist/docs/api-reference/db.md +351 -0
- package/dist/docs/api-reference/http-error.md +109 -0
- package/dist/docs/api-reference/ignite.md +323 -0
- package/dist/docs/api-reference/table.md +250 -0
- package/dist/docs/api-reference/tasks.md +140 -0
- package/dist/docs/cli/db.md +38 -0
- package/dist/docs/cli/diagnostics.md +145 -0
- package/dist/docs/cli/generate.md +120 -0
- package/dist/docs/cli/index.md +48 -0
- package/dist/docs/cli/start.md +112 -0
- package/dist/docs/contributing/index.md +139 -0
- package/dist/docs/core-concepts/background-tasks.md +228 -0
- package/dist/docs/core-concepts/clustering.md +114 -0
- package/dist/docs/core-concepts/file-based-routing.md +235 -0
- package/dist/docs/core-concepts/index.md +79 -0
- package/dist/docs/core-concepts/middleware.md +144 -0
- package/dist/docs/getting-started/index.md +91 -0
- package/dist/docs/getting-started/project-structure.md +154 -0
- package/dist/docs/guides/ai-agents.md +48 -0
- package/dist/docs/guides/authentication.md +196 -0
- package/dist/docs/guides/caching.md +124 -0
- package/dist/docs/guides/database.md +217 -0
- package/dist/docs/guides/deployment.md +171 -0
- package/dist/docs/guides/environment-variables.md +106 -0
- package/dist/docs/guides/error-handling.md +172 -0
- package/dist/docs/guides/generated-portals.md +115 -0
- package/dist/docs/guides/logging.md +70 -0
- package/dist/docs/guides/mailer.md +166 -0
- package/dist/docs/guides/migrations.md +106 -0
- package/dist/docs/guides/rbac.md +89 -0
- package/dist/docs/guides/validation.md +95 -0
- package/dist/docs/index.md +93 -0
- package/dist/docs/skills/SKILL.md +44 -0
- package/dist/docs/skills/api-surface.md +308 -0
- package/dist/docs/skills/auth-internals.md +141 -0
- package/dist/docs/skills/pre-flight.md +142 -0
- package/dist/docs/skills/routing-rules.md +114 -0
- package/dist/docs/skills/tasks-internals.md +172 -0
- package/dist/docs/skills/what-not-to-invent.md +251 -0
- package/dist/index.cjs +289 -391
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +472 -7
- package/dist/index.d.ts +472 -7
- package/dist/index.js +283 -385
- package/dist/index.js.map +1 -1
- package/dist/pgboss-backend-3E6ATY42.cjs +14 -0
- package/dist/pgboss-backend-3E6ATY42.cjs.map +1 -0
- package/dist/pgboss-backend-UUJOVEYK.js +14 -0
- package/dist/pgboss-backend-UUJOVEYK.js.map +1 -0
- package/dist/tasks/index.cjs.map +1 -1
- package/dist/tasks/index.d.cts +1 -1
- package/dist/tasks/index.d.ts +1 -1
- package/dist/types-DJCPAsM4.d.cts +541 -0
- package/dist/types-DJCPAsM4.d.ts +541 -0
- package/package.json +28 -5
- package/dist/chunk-6522HS2J.cjs +0 -99
- package/dist/chunk-6522HS2J.cjs.map +0 -1
- package/dist/chunk-DGVOJNR6.cjs +0 -55
- package/dist/chunk-DGVOJNR6.cjs.map +0 -1
- package/dist/chunk-KOHLLUN7.js +0 -55
- package/dist/chunk-KOHLLUN7.js.map +0 -1
- package/dist/chunk-VIP3PFWZ.js +0 -99
- package/dist/chunk-VIP3PFWZ.js.map +0 -1
- package/dist/types-BusbSenn.d.cts +0 -162
- package/dist/types-BusbSenn.d.ts +0 -162
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
EFC is an opinionated backend framework built on Express. Drop files in `src/api/` and they become routes. Every CPU core serves traffic automatically. Heavy work goes to a queue-backed task subsystem so requests stay fast.
|
|
6
6
|
|
|
7
|
-
> **Status: v0.
|
|
7
|
+
> **Status: v0.5.0 (Beta).** Router (incl. catch-all/optional segments and request validation), clustering, auth, structured logging, `defineModel` on MongoDB and PostgreSQL (types inferred from the schema), PostgreSQL-native `defineTable` with Prisma-style queries, BullMQ and pg-boss tasks with cron, and migrations are all implemented.
|
|
8
8
|
|
|
9
9
|
---
|
|
10
10
|
|
|
@@ -142,22 +142,18 @@ Declare a typed model with a schema. EFC compiles it to a Mongoose model and wra
|
|
|
142
142
|
|
|
143
143
|
```ts
|
|
144
144
|
// src/model/User.ts
|
|
145
|
-
import { defineModel } from 'express-file-cluster';
|
|
145
|
+
import { defineModel, type InferRecord } from 'express-file-cluster';
|
|
146
146
|
|
|
147
|
-
|
|
148
|
-
name: string;
|
|
149
|
-
email: string;
|
|
150
|
-
role: 'admin' | 'user';
|
|
151
|
-
verifyToken: string;
|
|
152
|
-
createdAt?: Date;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
export const User = defineModel<UserDocument>('User', {
|
|
147
|
+
export const User = defineModel('User', {
|
|
156
148
|
name: { type: 'string', required: true },
|
|
157
149
|
email: { type: 'string', required: true, unique: true },
|
|
158
150
|
role: { type: 'string', enum: ['admin', 'user'], default: 'user' },
|
|
159
151
|
verifyToken: { type: 'string', default: '$uuid' }, // fresh UUID per document
|
|
160
152
|
});
|
|
153
|
+
|
|
154
|
+
// Types come from the schema — no interface to write:
|
|
155
|
+
export type User = InferRecord<typeof User>;
|
|
156
|
+
// { id: string; name: string; email: string; role: 'admin' | 'user'; verifyToken: string; createdAt: Date; ... }
|
|
161
157
|
```
|
|
162
158
|
|
|
163
159
|
### Schema default operator codes
|
|
@@ -179,7 +175,7 @@ Any other value is used as-is — only these exact strings are special-cased.
|
|
|
179
175
|
### Auto-increment (`sequence`)
|
|
180
176
|
|
|
181
177
|
```ts
|
|
182
|
-
export const Order = defineModel
|
|
178
|
+
export const Order = defineModel('Order', {
|
|
183
179
|
orderNumber: { type: 'number', sequence: true, required: true },
|
|
184
180
|
// sequence: 'global.orders' — explicit key to share a counter across models
|
|
185
181
|
});
|
|
@@ -191,34 +187,39 @@ export const Order = defineModel<OrderDocument>('Order', {
|
|
|
191
187
|
|
|
192
188
|
```ts
|
|
193
189
|
// Disable Mongoose's automatic createdAt/updatedAt (default: on)
|
|
194
|
-
export const Role = defineModel
|
|
190
|
+
export const Role = defineModel('Role', schema, {
|
|
195
191
|
timestamps: false,
|
|
196
192
|
});
|
|
197
193
|
|
|
198
194
|
// Or rename them
|
|
199
|
-
export const Audit = defineModel
|
|
195
|
+
export const Audit = defineModel('Audit', schema, {
|
|
200
196
|
timestamps: { createdAt: 'created_at', updatedAt: false },
|
|
201
197
|
});
|
|
202
198
|
```
|
|
203
199
|
|
|
204
|
-
###
|
|
200
|
+
### Model API — identical on MongoDB and PostgreSQL
|
|
205
201
|
|
|
206
202
|
```ts
|
|
207
|
-
await User.find({ role: 'admin' }
|
|
208
|
-
|
|
209
|
-
await User.
|
|
210
|
-
await User.
|
|
211
|
-
await User.
|
|
212
|
-
await User.
|
|
213
|
-
await User.
|
|
203
|
+
await User.find({ role: { $in: ['admin', 'editor'] }, 'address.city': 'Oslo' },
|
|
204
|
+
{ sort: { createdAt: -1 }, limit: 20, skip: 20, select: ['name'], populate: 'team' });
|
|
205
|
+
await User.findById(id); // malformed id → null
|
|
206
|
+
await User.findOne({ email: 'a@b.com' });
|
|
207
|
+
await User.create({ name: 'Alice' }); // validated (400 ModelValidationError / 409 duplicate)
|
|
208
|
+
await User.createMany([...]);
|
|
209
|
+
await User.update(id, { $inc: { logins: 1 } }); // validated too
|
|
210
|
+
await User.updateMany(filter, update); // → count
|
|
211
|
+
await User.delete(id);
|
|
212
|
+
await User.deleteMany(filter); // → count
|
|
213
|
+
await User.count(filter);
|
|
214
|
+
await User.exists(filter); // → boolean
|
|
215
|
+
await User.distinct('role', filter); // → sorted distinct values
|
|
216
|
+
await User.updateOne(filter, update); // first match, one statement → record | null
|
|
217
|
+
await User.deleteOne(filter); // → deleted record | null
|
|
218
|
+
await User.upsert(filter, create, update); // update the match, or create
|
|
219
|
+
await transaction(async () => { /* model calls here share one transaction */ });
|
|
214
220
|
```
|
|
215
221
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
```ts
|
|
219
|
-
await Post.find({}, { populate: 'author' });
|
|
220
|
-
await Post.findById(id, { populate: ['author', 'comments.user'] });
|
|
221
|
-
```
|
|
222
|
+
The engine follows `DATABASE_URL` (`mongodb://` → mongoose, `postgres://` → Drizzle; install `mongoose` or `pg drizzle-orm`). Tables and indexes are created and kept in step with the models automatically on first use (`autoSync`); `efc db sync --dry-run` shows pending changes. MongoDB-only extras: `aggregate()`, TTL indexes. `Model.raw()` returns the native mongoose model / Drizzle table.
|
|
222
223
|
|
|
223
224
|
---
|
|
224
225
|
|
|
@@ -266,12 +267,34 @@ Task options:
|
|
|
266
267
|
| Option | Default | Description |
|
|
267
268
|
|---|---|---|
|
|
268
269
|
| `thread` | `false` | Run in a `worker_threads` thread (CPU-bound work) |
|
|
269
|
-
| `retries` | `3` |
|
|
270
|
+
| `retries` | `3` | Re-runs after a failure (up to `retries + 1` attempts) before dead-lettering |
|
|
270
271
|
| `backoff` | `'exponential'` | Delay strategy between retries |
|
|
271
272
|
| `concurrency` | `tasks.concurrency` | Parallel jobs for this task |
|
|
272
|
-
| `schedule` | — | Cron
|
|
273
|
+
| `schedule` | — | Cron pattern for recurring runs (payload `{}`) |
|
|
274
|
+
| `timezone` | server time | IANA timezone for `schedule` |
|
|
275
|
+
|
|
276
|
+
Backends: `tasks: { backend: 'bullmq', redisUrl }` or `tasks: { backend: 'pg-boss' }` (jobs in your PostgreSQL). `enqueue()` rejects after `tasks.enqueueTimeout` (default 5 s) instead of hanging when the backend is down. Dead letters: `efc tasks failed | retry | purge`.
|
|
277
|
+
|
|
278
|
+
### PostgreSQL-native: `defineTable()`
|
|
273
279
|
|
|
274
|
-
|
|
280
|
+
On PostgreSQL, `defineTable` gives you Prisma's query API and SQL types on the same foundation:
|
|
281
|
+
|
|
282
|
+
```ts
|
|
283
|
+
import { defineTable } from 'express-file-cluster';
|
|
284
|
+
|
|
285
|
+
export const Order = defineTable('Order', {
|
|
286
|
+
number: { type: 'string', required: true, unique: true, maxLength: 20 }, // varchar(20)
|
|
287
|
+
total: { type: 'decimal', precision: 10, scale: 2, required: true }, // '19.99', exact
|
|
288
|
+
qty: { type: 'integer', required: true },
|
|
289
|
+
tags: { type: 'array', of: 'string', default: [] }, // text[]
|
|
290
|
+
}, { id: 'serial' });
|
|
291
|
+
|
|
292
|
+
await Order.findMany({ where: { total: { gte: '100' }, tags: { has: 'vip' } }, orderBy: { createdAt: 'desc' }, take: 20 });
|
|
293
|
+
await Order.upsert({ where: { number }, create: { number, total: '0', qty: 1 }, update: { qty: { increment: 1 } } });
|
|
294
|
+
await Order.groupBy({ by: ['qty'], _sum: { total: true } });
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
See [defineTable](https://github.com/siggold/express-file-cluster/blob/main/docs/api-reference/table.md).
|
|
275
298
|
|
|
276
299
|
---
|
|
277
300
|
|
|
@@ -314,6 +337,11 @@ export const POST = async (req, res) => {
|
|
|
314
337
|
// Role-gated route
|
|
315
338
|
export const middlewares = [requireAuth('admin')];
|
|
316
339
|
|
|
340
|
+
// Real logout (needs `cache` in ignite()): the token is rejected from now on.
|
|
341
|
+
import { revokeSession, revokeAllSessions } from 'express-file-cluster/auth';
|
|
342
|
+
await revokeSession(req, res); // this device
|
|
343
|
+
await revokeAllSessions(userId); // every device
|
|
344
|
+
|
|
317
345
|
// getCurrentUser() reads the same verified JWT payload from anywhere in the
|
|
318
346
|
// async call chain — not just where `req` is in scope. This is also what
|
|
319
347
|
// powers the '$currentUser' / '$currentUser.<key>' defineModel default codes.
|
|
@@ -380,6 +408,8 @@ ignite({
|
|
|
380
408
|
| `tasks` | `TaskConfig \| false` | `false` | Background task runtime |
|
|
381
409
|
| `cors` | `boolean \| CorsConfig` | `true` | CORS — pass `origin` explicitly, not auto-read from env |
|
|
382
410
|
| `requestTimeout` | `number` | — | Request timeout in ms (408 on exceed) |
|
|
411
|
+
| `logging` | `LoggingConfig \| false` | pretty in dev TTY, JSON otherwise | pino logging; every request gets an `X-Request-Id` |
|
|
412
|
+
| `cache` | `CacheConfig \| false` | — | `{ backend: 'redis', url }` or `{ backend: 'memory' }` — enables `cache`, `cacheResponse()` and session revocation |
|
|
383
413
|
| `globalMiddlewares` | `RequestHandler[]` | `[]` | Applied to every route |
|
|
384
414
|
| `dashboard` | `boolean` | `true` in dev | Dev route dashboard at `/` |
|
|
385
415
|
| `onWorkerReady` | `(id) => void` | — | Called when a worker boots |
|
|
@@ -395,8 +425,8 @@ ignite({
|
|
|
395
425
|
efc start dev # Hot-reload single process (tsx --watch), .env re-read on every restart
|
|
396
426
|
|
|
397
427
|
# Production
|
|
398
|
-
efc build prod # Type-check +
|
|
399
|
-
efc start prod # Run dist/ with clustering enabled
|
|
428
|
+
efc build prod # Type-check + transpile src/ → dist/src/ (tsup, unbundled)
|
|
429
|
+
efc start prod # Run dist/src/index.js with clustering enabled
|
|
400
430
|
|
|
401
431
|
# Tests
|
|
402
432
|
efc run tests # Vitest (--watch, --coverage passthrough)
|
|
@@ -405,10 +435,18 @@ efc run tests # Vitest (--watch, --coverage passthrough)
|
|
|
405
435
|
efc generate route users/[id] # → src/api/users/[id].ts
|
|
406
436
|
efc generate task ProcessPayment # → src/tasks/ProcessPayment.ts
|
|
407
437
|
efc generate middleware authorize # → src/middlewares/authorize.ts
|
|
438
|
+
efc generate migration add_orders # → src/migrations/<timestamp>_add_orders.ts
|
|
439
|
+
efc generate seed demo_users # → src/seeds/demo_users.ts
|
|
440
|
+
|
|
441
|
+
# Database
|
|
442
|
+
efc db migrate | rollback | status # Migrations
|
|
443
|
+
efc db seed [name] # Seeds
|
|
444
|
+
efc db studio # Read-only browser on 127.0.0.1
|
|
408
445
|
|
|
409
446
|
# Diagnostics
|
|
410
447
|
efc routes # Print resolved route table (path → file → methods)
|
|
411
448
|
efc tasks # List registered background tasks
|
|
449
|
+
efc tasks failed | retry | purge # Dead-letter queue
|
|
412
450
|
efc doctor # Validate project setup, package.json/tsconfig, .env vars
|
|
413
451
|
```
|
|
414
452
|
|
package/dist/auth/index.cjs
CHANGED
|
@@ -5,13 +5,23 @@
|
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
var _chunk6522HS2Jcjs = require('../chunk-6522HS2J.cjs');
|
|
9
8
|
|
|
9
|
+
var _chunkSFVEQ2T5cjs = require('../chunk-SFVEQ2T5.cjs');
|
|
10
|
+
require('../chunk-SZT36SGB.cjs');
|
|
11
|
+
require('../chunk-ALBDM5R3.cjs');
|
|
12
|
+
require('../chunk-7YEX4MQV.cjs');
|
|
13
|
+
require('../chunk-TI24L4XE.cjs');
|
|
10
14
|
|
|
11
15
|
|
|
16
|
+
var _chunkRSBW2B3Dcjs = require('../chunk-RSBW2B3D.cjs');
|
|
12
17
|
|
|
13
18
|
|
|
14
19
|
|
|
15
20
|
|
|
16
|
-
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
exports.configureAuth = _chunkSFVEQ2T5cjs.configureAuth; exports.getCurrentUser = _chunkRSBW2B3Dcjs.getCurrentUser; exports.issueToken = _chunkSFVEQ2T5cjs.issueToken; exports.requireAuth = _chunkSFVEQ2T5cjs.requireAuth; exports.revokeAllSessions = _chunkSFVEQ2T5cjs.revokeAllSessions; exports.revokeSession = _chunkSFVEQ2T5cjs.revokeSession; exports.revokeToken = _chunkSFVEQ2T5cjs.revokeToken; exports.signToken = _chunkSFVEQ2T5cjs.signToken;
|
|
17
27
|
//# sourceMappingURL=index.cjs.map
|
package/dist/auth/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/home/runner/work/
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/express-file-cluster/express-file-cluster/packages/core/dist/auth/index.cjs"],"names":[],"mappings":"AAAA;AACE;AACA;AACA;AACA;AACA;AACA;AACA;AACF,yDAA8B;AAC9B,iCAA8B;AAC9B,iCAA8B;AAC9B,iCAA8B;AAC9B,iCAA8B;AAC9B;AACE;AACF,yDAA8B;AAC9B;AACE;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACF,2bAAC","file":"/home/runner/work/express-file-cluster/express-file-cluster/packages/core/dist/auth/index.cjs"}
|
package/dist/auth/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Request, Response, NextFunction, RequestHandler } from 'express';
|
|
2
|
-
import { A as AuthStrategy } from '../types-
|
|
2
|
+
import { A as AuthStrategy } from '../types-DJCPAsM4.cjs';
|
|
3
3
|
|
|
4
4
|
declare function getCurrentUser(): Record<string, unknown> | undefined;
|
|
5
5
|
|
|
@@ -13,6 +13,17 @@ declare function configureAuth(config: AuthConfig): void;
|
|
|
13
13
|
declare function issueToken(res: Response, payload: Record<string, unknown>): Promise<void>;
|
|
14
14
|
declare function revokeToken(res: Response): void;
|
|
15
15
|
declare function signToken(payload: Record<string, unknown>): Promise<string>;
|
|
16
|
+
/**
|
|
17
|
+
* Revokes the token the current request was authenticated with: `requireAuth` rejects
|
|
18
|
+
* it from now until it would have expired anyway. Pass `res` to also clear the cookie
|
|
19
|
+
* (http-only strategy). Throws if the cache is unreachable, so a failed logout is visible.
|
|
20
|
+
*/
|
|
21
|
+
declare function revokeSession(req: Request, res?: Response): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* Revokes every token issued to `userId` (the JWT's `sub` or `id` claim) before now —
|
|
24
|
+
* "log out of all devices", or after a password change. Tokens issued afterwards work.
|
|
25
|
+
*/
|
|
26
|
+
declare function revokeAllSessions(userId: string | number): Promise<void>;
|
|
16
27
|
/**
|
|
17
28
|
* Used bare (`middlewares = [requireAuth]`) it just verifies the JWT.
|
|
18
29
|
* Called with role names (`middlewares = [requireAuth('admin')]`) it
|
|
@@ -24,4 +35,4 @@ interface RequireAuth {
|
|
|
24
35
|
}
|
|
25
36
|
declare const requireAuth: RequireAuth;
|
|
26
37
|
|
|
27
|
-
export { type RequireAuth, configureAuth, getCurrentUser, issueToken, requireAuth, revokeToken, signToken };
|
|
38
|
+
export { type RequireAuth, configureAuth, getCurrentUser, issueToken, requireAuth, revokeAllSessions, revokeSession, revokeToken, signToken };
|
package/dist/auth/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Request, Response, NextFunction, RequestHandler } from 'express';
|
|
2
|
-
import { A as AuthStrategy } from '../types-
|
|
2
|
+
import { A as AuthStrategy } from '../types-DJCPAsM4.js';
|
|
3
3
|
|
|
4
4
|
declare function getCurrentUser(): Record<string, unknown> | undefined;
|
|
5
5
|
|
|
@@ -13,6 +13,17 @@ declare function configureAuth(config: AuthConfig): void;
|
|
|
13
13
|
declare function issueToken(res: Response, payload: Record<string, unknown>): Promise<void>;
|
|
14
14
|
declare function revokeToken(res: Response): void;
|
|
15
15
|
declare function signToken(payload: Record<string, unknown>): Promise<string>;
|
|
16
|
+
/**
|
|
17
|
+
* Revokes the token the current request was authenticated with: `requireAuth` rejects
|
|
18
|
+
* it from now until it would have expired anyway. Pass `res` to also clear the cookie
|
|
19
|
+
* (http-only strategy). Throws if the cache is unreachable, so a failed logout is visible.
|
|
20
|
+
*/
|
|
21
|
+
declare function revokeSession(req: Request, res?: Response): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* Revokes every token issued to `userId` (the JWT's `sub` or `id` claim) before now —
|
|
24
|
+
* "log out of all devices", or after a password change. Tokens issued afterwards work.
|
|
25
|
+
*/
|
|
26
|
+
declare function revokeAllSessions(userId: string | number): Promise<void>;
|
|
16
27
|
/**
|
|
17
28
|
* Used bare (`middlewares = [requireAuth]`) it just verifies the JWT.
|
|
18
29
|
* Called with role names (`middlewares = [requireAuth('admin')]`) it
|
|
@@ -24,4 +35,4 @@ interface RequireAuth {
|
|
|
24
35
|
}
|
|
25
36
|
declare const requireAuth: RequireAuth;
|
|
26
37
|
|
|
27
|
-
export { type RequireAuth, configureAuth, getCurrentUser, issueToken, requireAuth, revokeToken, signToken };
|
|
38
|
+
export { type RequireAuth, configureAuth, getCurrentUser, issueToken, requireAuth, revokeAllSessions, revokeSession, revokeToken, signToken };
|
package/dist/auth/index.js
CHANGED
|
@@ -1,16 +1,26 @@
|
|
|
1
1
|
import {
|
|
2
2
|
configureAuth,
|
|
3
|
-
getCurrentUser,
|
|
4
3
|
issueToken,
|
|
5
4
|
requireAuth,
|
|
5
|
+
revokeAllSessions,
|
|
6
|
+
revokeSession,
|
|
6
7
|
revokeToken,
|
|
7
8
|
signToken
|
|
8
|
-
} from "../chunk-
|
|
9
|
+
} from "../chunk-5PDAWMGF.js";
|
|
10
|
+
import "../chunk-PMF4GMHD.js";
|
|
11
|
+
import "../chunk-3VWD2ZED.js";
|
|
12
|
+
import "../chunk-LJNSJFOL.js";
|
|
13
|
+
import "../chunk-HBYSKULC.js";
|
|
14
|
+
import {
|
|
15
|
+
getCurrentUser
|
|
16
|
+
} from "../chunk-VYHJX54R.js";
|
|
9
17
|
export {
|
|
10
18
|
configureAuth,
|
|
11
19
|
getCurrentUser,
|
|
12
20
|
issueToken,
|
|
13
21
|
requireAuth,
|
|
22
|
+
revokeAllSessions,
|
|
23
|
+
revokeSession,
|
|
14
24
|
revokeToken,
|
|
15
25
|
signToken
|
|
16
26
|
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getLogger
|
|
3
|
+
} from "./chunk-VYHJX54R.js";
|
|
4
|
+
|
|
5
|
+
// src/db/postgres.ts
|
|
6
|
+
var pool;
|
|
7
|
+
function notInstalled(err, pkg, install) {
|
|
8
|
+
const code = err.code;
|
|
9
|
+
return code === "ERR_MODULE_NOT_FOUND" || code === "MODULE_NOT_FOUND" ? new Error(`[EFC] ${pkg} is not installed. Run: ${install}`) : err;
|
|
10
|
+
}
|
|
11
|
+
var orm;
|
|
12
|
+
async function connectPostgres(url) {
|
|
13
|
+
let PoolCtor;
|
|
14
|
+
try {
|
|
15
|
+
const pg = await import("pg");
|
|
16
|
+
PoolCtor = pg.default?.Pool ?? pg.Pool;
|
|
17
|
+
} catch (err) {
|
|
18
|
+
throw notInstalled(err, "pg", "npm install pg drizzle-orm");
|
|
19
|
+
}
|
|
20
|
+
let drizzle;
|
|
21
|
+
try {
|
|
22
|
+
({ drizzle } = await import("drizzle-orm/node-postgres"));
|
|
23
|
+
} catch (err) {
|
|
24
|
+
throw notInstalled(err, "drizzle-orm", "npm install drizzle-orm");
|
|
25
|
+
}
|
|
26
|
+
const next = new PoolCtor({ connectionString: url });
|
|
27
|
+
next.on("error", (err) => getLogger().error({ err }, "PostgreSQL pool error"));
|
|
28
|
+
await next.query("select 1");
|
|
29
|
+
pool = next;
|
|
30
|
+
orm = drizzle(next);
|
|
31
|
+
getLogger().info("PostgreSQL connected");
|
|
32
|
+
return next;
|
|
33
|
+
}
|
|
34
|
+
function getOrm() {
|
|
35
|
+
if (!orm) {
|
|
36
|
+
throw new Error(
|
|
37
|
+
"[EFC] PostgreSQL not connected \u2014 set database/databaseUrl in ignite() (or call connectPostgres)"
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
return orm;
|
|
41
|
+
}
|
|
42
|
+
function getPool() {
|
|
43
|
+
if (!pool) throw new Error("[EFC] PostgreSQL not connected");
|
|
44
|
+
return pool;
|
|
45
|
+
}
|
|
46
|
+
async function closePostgres() {
|
|
47
|
+
const p = pool;
|
|
48
|
+
pool = void 0;
|
|
49
|
+
orm = void 0;
|
|
50
|
+
await p?.end();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export {
|
|
54
|
+
notInstalled,
|
|
55
|
+
connectPostgres,
|
|
56
|
+
getOrm,
|
|
57
|
+
getPool,
|
|
58
|
+
closePostgres
|
|
59
|
+
};
|
|
60
|
+
//# sourceMappingURL=chunk-3VWD2ZED.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/db/postgres.ts"],"sourcesContent":["import type { Pool } from 'pg';\nimport { getLogger } from '../logger.js';\n\n// Pre-Flight's PostgreSQL connection: a node-postgres Pool (exposed as `db`) plus a\n// Drizzle instance over it that the model adapter queries through.\n\n/* eslint-disable @typescript-eslint/no-explicit-any -- drizzle is an optional peer, loaded dynamically */\nlet pool: Pool | undefined;\n\n/** Only a genuinely missing package becomes \"not installed\"; other load errors surface as-is. */\nexport function notInstalled(err: unknown, pkg: string, install: string): Error {\n const code = (err as { code?: string }).code;\n return code === 'ERR_MODULE_NOT_FOUND' || code === 'MODULE_NOT_FOUND'\n ? new Error(`[EFC] ${pkg} is not installed. Run: ${install}`)\n : (err as Error);\n}\nlet orm: any;\n\nexport async function connectPostgres(url: string): Promise<Pool> {\n let PoolCtor: typeof Pool;\n try {\n const pg = (await import('pg')) as unknown as {\n default?: { Pool: typeof Pool };\n Pool?: typeof Pool;\n };\n PoolCtor = (pg.default?.Pool ?? pg.Pool)!;\n } catch (err) {\n throw notInstalled(err, 'pg', 'npm install pg drizzle-orm');\n }\n let drizzle: (client: Pool) => unknown;\n try {\n ({ drizzle } = (await import('drizzle-orm/node-postgres')) as unknown as {\n drizzle: (client: Pool) => unknown;\n });\n } catch (err) {\n throw notInstalled(err, 'drizzle-orm', 'npm install drizzle-orm');\n }\n\n const next = new PoolCtor({ connectionString: url });\n // Idle-client errors (e.g. the server restarting) are emitted on the pool; unhandled\n // they would crash the process.\n next.on('error', (err) => getLogger().error({ err }, 'PostgreSQL pool error'));\n await next.query('select 1');\n pool = next;\n orm = drizzle(next);\n getLogger().info('PostgreSQL connected');\n return next;\n}\n\nexport function getOrm(): any {\n if (!orm) {\n throw new Error(\n '[EFC] PostgreSQL not connected — set database/databaseUrl in ignite() (or call connectPostgres)',\n );\n }\n return orm;\n}\n\nexport function getPool(): Pool {\n if (!pool) throw new Error('[EFC] PostgreSQL not connected');\n return pool;\n}\n\nexport async function closePostgres(): Promise<void> {\n const p = pool;\n pool = undefined;\n orm = undefined;\n await p?.end();\n}\n"],"mappings":";;;;;AAOA,IAAI;AAGG,SAAS,aAAa,KAAc,KAAa,SAAwB;AAC9E,QAAM,OAAQ,IAA0B;AACxC,SAAO,SAAS,0BAA0B,SAAS,qBAC/C,IAAI,MAAM,SAAS,GAAG,2BAA2B,OAAO,EAAE,IACzD;AACP;AACA,IAAI;AAEJ,eAAsB,gBAAgB,KAA4B;AAChE,MAAI;AACJ,MAAI;AACF,UAAM,KAAM,MAAM,OAAO,IAAI;AAI7B,eAAY,GAAG,SAAS,QAAQ,GAAG;AAAA,EACrC,SAAS,KAAK;AACZ,UAAM,aAAa,KAAK,MAAM,4BAA4B;AAAA,EAC5D;AACA,MAAI;AACJ,MAAI;AACF,KAAC,EAAE,QAAQ,IAAK,MAAM,OAAO,2BAA2B;AAAA,EAG1D,SAAS,KAAK;AACZ,UAAM,aAAa,KAAK,eAAe,yBAAyB;AAAA,EAClE;AAEA,QAAM,OAAO,IAAI,SAAS,EAAE,kBAAkB,IAAI,CAAC;AAGnD,OAAK,GAAG,SAAS,CAAC,QAAQ,UAAU,EAAE,MAAM,EAAE,IAAI,GAAG,uBAAuB,CAAC;AAC7E,QAAM,KAAK,MAAM,UAAU;AAC3B,SAAO;AACP,QAAM,QAAQ,IAAI;AAClB,YAAU,EAAE,KAAK,sBAAsB;AACvC,SAAO;AACT;AAEO,SAAS,SAAc;AAC5B,MAAI,CAAC,KAAK;AACR,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACA,SAAO;AACT;AAEO,SAAS,UAAgB;AAC9B,MAAI,CAAC,KAAM,OAAM,IAAI,MAAM,gCAAgC;AAC3D,SAAO;AACT;AAEA,eAAsB,gBAA+B;AACnD,QAAM,IAAI;AACV,SAAO;AACP,QAAM;AACN,QAAM,GAAG,IAAI;AACf;","names":[]}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
|
+
|
|
3
|
+
var _chunk7YEX4MQVcjs = require('./chunk-7YEX4MQV.cjs');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
var _chunkTI24L4XEcjs = require('./chunk-TI24L4XE.cjs');
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
var _chunkRSBW2B3Dcjs = require('./chunk-RSBW2B3D.cjs');
|
|
11
|
+
|
|
12
|
+
// src/tasks/pgboss-backend.ts
|
|
13
|
+
var BOX = "__efcValue";
|
|
14
|
+
var box = (payload) => payload !== null && typeof payload === "object" && !Array.isArray(payload) ? payload : { [BOX]: payload };
|
|
15
|
+
var unbox = (data) => data !== null && typeof data === "object" && BOX in data ? data[BOX] : data;
|
|
16
|
+
var retryOptions = (def) => ({
|
|
17
|
+
// pg-boss's retryLimit counts re-runs, exactly like EFC's `retries`.
|
|
18
|
+
retryLimit: _nullishCoalesce(def.options.retries, () => ( 3)),
|
|
19
|
+
retryDelay: 1,
|
|
20
|
+
retryBackoff: (_nullishCoalesce(def.options.backoff, () => ( "exponential"))) === "exponential"
|
|
21
|
+
});
|
|
22
|
+
async function loadPgBoss() {
|
|
23
|
+
try {
|
|
24
|
+
const mod = await Promise.resolve().then(() => _interopRequireWildcard(require("pg-boss")));
|
|
25
|
+
const ctor = _nullishCoalesce(mod.PgBoss, () => ( (typeof mod.default === "function" ? mod.default : mod.default.PgBoss)));
|
|
26
|
+
return ctor;
|
|
27
|
+
} catch (e) {
|
|
28
|
+
throw new Error("[EFC] pg-boss is not installed. Run: npm install pg-boss");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
var closeImpl = null;
|
|
32
|
+
async function closePgBoss() {
|
|
33
|
+
const close = closeImpl;
|
|
34
|
+
closeImpl = null;
|
|
35
|
+
await _optionalChain([close, 'optionalCall', _2 => _2()]);
|
|
36
|
+
}
|
|
37
|
+
async function initPgBoss(opts) {
|
|
38
|
+
const PgBoss = await loadPgBoss();
|
|
39
|
+
const boss = new PgBoss({
|
|
40
|
+
connectionString: opts.connectionString,
|
|
41
|
+
...opts.schema !== void 0 && { schema: opts.schema }
|
|
42
|
+
});
|
|
43
|
+
boss.on("error", (err) => _chunkRSBW2B3Dcjs.getLogger.call(void 0, ).error({ err }, "pg-boss error"));
|
|
44
|
+
await boss.start();
|
|
45
|
+
const queueFor = (name) => `${opts.queue}.${name}`;
|
|
46
|
+
const wantedSchedules = /* @__PURE__ */ new Set();
|
|
47
|
+
for (const def of _chunkTI24L4XEcjs.taskRegistry.values()) {
|
|
48
|
+
const queue = queueFor(def.name);
|
|
49
|
+
await boss.createQueue(queue, retryOptions(def));
|
|
50
|
+
await boss.work(
|
|
51
|
+
queue,
|
|
52
|
+
{ localConcurrency: _nullishCoalesce(def.options.concurrency, () => ( opts.concurrency)) },
|
|
53
|
+
async (jobs) => {
|
|
54
|
+
for (const job of jobs) {
|
|
55
|
+
const payload = unbox(job.data);
|
|
56
|
+
try {
|
|
57
|
+
if (def.options.thread && def.filePath) await _chunk7YEX4MQVcjs.runInThread.call(void 0, def.filePath, payload);
|
|
58
|
+
else await def.handler(payload);
|
|
59
|
+
_chunkRSBW2B3Dcjs.getLogger.call(void 0, ).info({ task: def.name, jobId: job.id }, `Task ${def.name} completed`);
|
|
60
|
+
} catch (err) {
|
|
61
|
+
_chunkRSBW2B3Dcjs.getLogger.call(void 0, ).error(
|
|
62
|
+
{ task: def.name, jobId: job.id, attempt: job.retryCount + 1, err },
|
|
63
|
+
`Task ${def.name} failed`
|
|
64
|
+
);
|
|
65
|
+
throw err;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
);
|
|
70
|
+
if (def.options.schedule) {
|
|
71
|
+
wantedSchedules.add(queue);
|
|
72
|
+
await boss.schedule(
|
|
73
|
+
queue,
|
|
74
|
+
def.options.schedule,
|
|
75
|
+
{},
|
|
76
|
+
{
|
|
77
|
+
...retryOptions(def),
|
|
78
|
+
...def.options.timezone !== void 0 && { tz: def.options.timezone }
|
|
79
|
+
}
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
for (const s of await boss.getSchedules()) {
|
|
84
|
+
if (s.name.startsWith(`${opts.queue}.`) && !wantedSchedules.has(s.name)) {
|
|
85
|
+
await boss.unschedule(s.name);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (wantedSchedules.size > 0) _chunkRSBW2B3Dcjs.getLogger.call(void 0, ).info(`Scheduled ${wantedSchedules.size} cron task(s)`);
|
|
89
|
+
_chunkTI24L4XEcjs.setEnqueueImpl.call(void 0, async (name, payload) => {
|
|
90
|
+
const def = _chunkTI24L4XEcjs.taskRegistry.get(name);
|
|
91
|
+
if (!def) {
|
|
92
|
+
throw new Error(
|
|
93
|
+
`[EFC] Cannot enqueue unknown task: "${name}". Is the task file in tasksDir?`
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
let timer;
|
|
97
|
+
const timeout = new Promise((_, reject) => {
|
|
98
|
+
timer = setTimeout(
|
|
99
|
+
() => reject(
|
|
100
|
+
new Error(
|
|
101
|
+
`[EFC] Cannot enqueue task "${name}": PostgreSQL did not respond within ${opts.enqueueTimeout}ms`
|
|
102
|
+
)
|
|
103
|
+
),
|
|
104
|
+
opts.enqueueTimeout
|
|
105
|
+
);
|
|
106
|
+
});
|
|
107
|
+
try {
|
|
108
|
+
await Promise.race([boss.send(queueFor(name), box(payload), retryOptions(def)), timeout]);
|
|
109
|
+
} catch (err) {
|
|
110
|
+
if (err instanceof Error && err.message.startsWith("[EFC]")) throw err;
|
|
111
|
+
throw new Error(
|
|
112
|
+
`[EFC] Cannot enqueue task "${name}": ${err instanceof Error ? err.message : String(err)}`
|
|
113
|
+
);
|
|
114
|
+
} finally {
|
|
115
|
+
clearTimeout(timer);
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
closeImpl = async () => {
|
|
119
|
+
await boss.stop({ graceful: true, timeout: 1e4 });
|
|
120
|
+
};
|
|
121
|
+
_chunkRSBW2B3Dcjs.getLogger.call(void 0, ).info("pg-boss backend initialised");
|
|
122
|
+
}
|
|
123
|
+
async function pgBossAdmin(connectionString, queue = "efc", schema = "pgboss") {
|
|
124
|
+
const PgBoss = await loadPgBoss();
|
|
125
|
+
const boss = new PgBoss({
|
|
126
|
+
connectionString,
|
|
127
|
+
schema,
|
|
128
|
+
supervise: false,
|
|
129
|
+
schedule: false,
|
|
130
|
+
migrate: false
|
|
131
|
+
});
|
|
132
|
+
boss.on("error", () => {
|
|
133
|
+
});
|
|
134
|
+
await boss.start();
|
|
135
|
+
const db = boss.getDb();
|
|
136
|
+
const prefix = `${queue}.`;
|
|
137
|
+
const taskOf = (name) => name.slice(prefix.length);
|
|
138
|
+
const table = `"${schema.replace(/"/g, '""')}".job`;
|
|
139
|
+
const failedRows = async (ids, limit = 1e4) => (await db.executeSql(
|
|
140
|
+
`SELECT id, name, data, output, retry_count, completed_on FROM ${table}
|
|
141
|
+
WHERE state = 'failed' AND starts_with(name, $1) ${ids.length ? "AND id = ANY($3::uuid[])" : ""}
|
|
142
|
+
ORDER BY completed_on DESC NULLS LAST LIMIT $2`,
|
|
143
|
+
ids.length ? [prefix, limit, ids] : [prefix, limit]
|
|
144
|
+
)).rows;
|
|
145
|
+
const byQueue = (rows) => {
|
|
146
|
+
const groups = /* @__PURE__ */ new Map();
|
|
147
|
+
for (const r of rows) groups.set(r.name, [..._nullishCoalesce(groups.get(r.name), () => ( [])), r.id]);
|
|
148
|
+
return groups;
|
|
149
|
+
};
|
|
150
|
+
return {
|
|
151
|
+
async counts() {
|
|
152
|
+
const { rows } = await db.executeSql(
|
|
153
|
+
`SELECT state, count(*)::int AS n FROM ${table} WHERE starts_with(name, $1) GROUP BY state`,
|
|
154
|
+
[prefix]
|
|
155
|
+
);
|
|
156
|
+
return Object.fromEntries(
|
|
157
|
+
rows.map((r) => [r.state, r.n])
|
|
158
|
+
);
|
|
159
|
+
},
|
|
160
|
+
async listFailed({ limit, name }) {
|
|
161
|
+
const rows = (await failedRows([], name ? 1e4 : limit)).filter(
|
|
162
|
+
(r) => !name || taskOf(r.name) === name
|
|
163
|
+
);
|
|
164
|
+
return rows.slice(0, limit).map(
|
|
165
|
+
(r) => ({
|
|
166
|
+
id: r.id,
|
|
167
|
+
name: taskOf(r.name),
|
|
168
|
+
data: unbox(r.data),
|
|
169
|
+
error: String(_nullishCoalesce(_nullishCoalesce(_optionalChain([r, 'access', _3 => _3.output, 'optionalAccess', _4 => _4.message]), () => ( r.output)), () => ( ""))),
|
|
170
|
+
attempts: r.retry_count + 1,
|
|
171
|
+
failedAt: _nullishCoalesce(r.completed_on, () => ( void 0))
|
|
172
|
+
})
|
|
173
|
+
);
|
|
174
|
+
},
|
|
175
|
+
async retry(ids) {
|
|
176
|
+
const rows = await failedRows(ids);
|
|
177
|
+
for (const [q, list] of byQueue(rows)) await boss.retry(q, list);
|
|
178
|
+
return rows.length;
|
|
179
|
+
},
|
|
180
|
+
async purge(ids) {
|
|
181
|
+
const rows = await failedRows(ids);
|
|
182
|
+
for (const [q, list] of byQueue(rows)) await boss.deleteJob(q, list);
|
|
183
|
+
return rows.length;
|
|
184
|
+
},
|
|
185
|
+
async close() {
|
|
186
|
+
await boss.stop({ graceful: false });
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
exports.closePgBoss = closePgBoss; exports.initPgBoss = initPgBoss; exports.pgBossAdmin = pgBossAdmin;
|
|
196
|
+
//# sourceMappingURL=chunk-47K2532L.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/express-file-cluster/express-file-cluster/packages/core/dist/chunk-47K2532L.cjs","../src/tasks/pgboss-backend.ts"],"names":[],"mappings":"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","file":"/home/runner/work/express-file-cluster/express-file-cluster/packages/core/dist/chunk-47K2532L.cjs","sourcesContent":[null,"import type { PgBoss as PgBossType } from 'pg-boss';\nimport type { TaskDefinition } from '../types.js';\nimport { taskRegistry, setEnqueueImpl } from './index.js';\nimport { runInThread } from './thread-runner.js';\nimport { getLogger } from '../logger.js';\nimport type { FailedJob, TaskAdmin } from './admin.js';\n\n// pg-boss keeps one queue per job name, so each task gets its own pg-boss queue\n// named `<tasks.queue>.<TaskName>` — the prefix keeps apps sharing a database apart.\n\ninterface PgBossOpts {\n connectionString: string;\n concurrency: number;\n enqueueTimeout: number;\n queue: string;\n schema?: string;\n}\n\n// pg-boss payloads must be objects; other values are boxed so any enqueue() payload works.\nconst BOX = '__efcValue';\nconst box = (payload: unknown): object =>\n payload !== null && typeof payload === 'object' && !Array.isArray(payload)\n ? (payload as object)\n : { [BOX]: payload };\nconst unbox = (data: unknown): unknown =>\n data !== null && typeof data === 'object' && BOX in data\n ? (data as Record<string, unknown>)[BOX]\n : data;\n\nconst retryOptions = (def: TaskDefinition) => ({\n // pg-boss's retryLimit counts re-runs, exactly like EFC's `retries`.\n retryLimit: def.options.retries ?? 3,\n retryDelay: 1,\n retryBackoff: (def.options.backoff ?? 'exponential') === 'exponential',\n});\n\nasync function loadPgBoss(): Promise<typeof PgBossType> {\n try {\n const mod = (await import('pg-boss')) as unknown as {\n PgBoss?: typeof PgBossType;\n default?: typeof PgBossType | { PgBoss: typeof PgBossType };\n };\n const ctor =\n mod.PgBoss ??\n (typeof mod.default === 'function'\n ? mod.default\n : (mod.default as { PgBoss: typeof PgBossType }).PgBoss);\n return ctor;\n } catch {\n throw new Error('[EFC] pg-boss is not installed. Run: npm install pg-boss');\n }\n}\n\nlet closeImpl: (() => Promise<void>) | null = null;\n\nexport async function closePgBoss(): Promise<void> {\n const close = closeImpl;\n closeImpl = null;\n await close?.();\n}\n\nexport async function initPgBoss(opts: PgBossOpts): Promise<void> {\n const PgBoss = await loadPgBoss();\n const boss = new PgBoss({\n connectionString: opts.connectionString,\n ...(opts.schema !== undefined && { schema: opts.schema }),\n });\n boss.on('error', (err: Error) => getLogger().error({ err }, 'pg-boss error'));\n await boss.start();\n\n const queueFor = (name: string) => `${opts.queue}.${name}`;\n const wantedSchedules = new Set<string>();\n\n for (const def of taskRegistry.values()) {\n const queue = queueFor(def.name);\n await boss.createQueue(queue, retryOptions(def));\n await boss.work(\n queue,\n { localConcurrency: def.options.concurrency ?? opts.concurrency },\n async (jobs: { id: string; data: unknown; retryCount: number }[]) => {\n for (const job of jobs) {\n const payload = unbox(job.data);\n try {\n if (def.options.thread && def.filePath) await runInThread(def.filePath, payload);\n else await def.handler(payload);\n getLogger().info({ task: def.name, jobId: job.id }, `Task ${def.name} completed`);\n } catch (err) {\n getLogger().error(\n { task: def.name, jobId: job.id, attempt: job.retryCount + 1, err },\n `Task ${def.name} failed`,\n );\n throw err;\n }\n }\n },\n );\n if (def.options.schedule) {\n wantedSchedules.add(queue);\n await boss.schedule(\n queue,\n def.options.schedule,\n {},\n {\n ...retryOptions(def),\n ...(def.options.timezone !== undefined && { tz: def.options.timezone }),\n },\n );\n }\n }\n\n // Drop schedules for tasks that lost their `schedule` or no longer exist.\n for (const s of await boss.getSchedules()) {\n if (s.name.startsWith(`${opts.queue}.`) && !wantedSchedules.has(s.name)) {\n await boss.unschedule(s.name);\n }\n }\n if (wantedSchedules.size > 0) getLogger().info(`Scheduled ${wantedSchedules.size} cron task(s)`);\n\n setEnqueueImpl(async (name, payload) => {\n const def = taskRegistry.get(name);\n if (!def) {\n throw new Error(\n `[EFC] Cannot enqueue unknown task: \"${name}\". Is the task file in tasksDir?`,\n );\n }\n let timer: NodeJS.Timeout | undefined;\n const timeout = new Promise<never>((_, reject) => {\n timer = setTimeout(\n () =>\n reject(\n new Error(\n `[EFC] Cannot enqueue task \"${name}\": PostgreSQL did not respond within ${opts.enqueueTimeout}ms`,\n ),\n ),\n opts.enqueueTimeout,\n );\n });\n try {\n await Promise.race([boss.send(queueFor(name), box(payload), retryOptions(def)), timeout]);\n } catch (err) {\n if (err instanceof Error && err.message.startsWith('[EFC]')) throw err;\n throw new Error(\n `[EFC] Cannot enqueue task \"${name}\": ${err instanceof Error ? err.message : String(err)}`,\n );\n } finally {\n clearTimeout(timer);\n }\n });\n\n closeImpl = async () => {\n // graceful: lets in-flight jobs finish before the connection pool closes.\n await boss.stop({ graceful: true, timeout: 10_000 });\n };\n\n getLogger().info('pg-boss backend initialised');\n}\n\n/** Dead-letter inspection for `efc tasks failed | retry | purge` on pg-boss. */\nexport async function pgBossAdmin(\n connectionString: string,\n queue = 'efc',\n schema = 'pgboss',\n): Promise<TaskAdmin> {\n const PgBoss = await loadPgBoss();\n // No supervision or cron here: this instance only reads and settles jobs.\n const boss = new PgBoss({\n connectionString,\n schema,\n supervise: false,\n schedule: false,\n migrate: false,\n });\n boss.on('error', () => {});\n await boss.start();\n const db = boss.getDb();\n const prefix = `${queue}.`;\n const taskOf = (name: string) => name.slice(prefix.length);\n const table = `\"${schema.replace(/\"/g, '\"\"')}\".job`;\n\n const failedRows = async (ids: string[], limit = 10_000) =>\n (\n await db.executeSql(\n `SELECT id, name, data, output, retry_count, completed_on FROM ${table}\n WHERE state = 'failed' AND starts_with(name, $1) ${ids.length ? 'AND id = ANY($3::uuid[])' : ''}\n ORDER BY completed_on DESC NULLS LAST LIMIT $2`,\n ids.length ? [prefix, limit, ids] : [prefix, limit],\n )\n ).rows as {\n id: string;\n name: string;\n data: unknown;\n output: unknown;\n retry_count: number;\n completed_on: Date | null;\n }[];\n\n const byQueue = (rows: { id: string; name: string }[]) => {\n const groups = new Map<string, string[]>();\n for (const r of rows) groups.set(r.name, [...(groups.get(r.name) ?? []), r.id]);\n return groups;\n };\n\n return {\n async counts() {\n const { rows } = await db.executeSql(\n `SELECT state, count(*)::int AS n FROM ${table} WHERE starts_with(name, $1) GROUP BY state`,\n [prefix],\n );\n return Object.fromEntries(\n (rows as { state: string; n: number }[]).map((r) => [r.state, r.n]),\n );\n },\n async listFailed({ limit, name }) {\n const rows = (await failedRows([], name ? 10_000 : limit)).filter(\n (r) => !name || taskOf(r.name) === name,\n );\n return rows.slice(0, limit).map(\n (r): FailedJob => ({\n id: r.id,\n name: taskOf(r.name),\n data: unbox(r.data),\n error: String((r.output as { message?: string } | null)?.message ?? r.output ?? ''),\n attempts: r.retry_count + 1,\n failedAt: r.completed_on ?? undefined,\n }),\n );\n },\n async retry(ids) {\n const rows = await failedRows(ids);\n for (const [q, list] of byQueue(rows)) await boss.retry(q, list);\n return rows.length;\n },\n async purge(ids) {\n const rows = await failedRows(ids);\n for (const [q, list] of byQueue(rows)) await boss.deleteJob(q, list);\n return rows.length;\n },\n async close() {\n await boss.stop({ graceful: false });\n },\n };\n}\n"]}
|