@spfn/core 0.3.0-beta.1 → 0.3.0-beta.10

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 (68) hide show
  1. package/README.md +301 -26
  2. package/dist/app-config/index.d.ts +96 -0
  3. package/dist/app-config/index.js +58 -0
  4. package/dist/app-config/index.js.map +1 -0
  5. package/dist/authz/index.js +1 -381
  6. package/dist/authz/index.js.map +1 -1
  7. package/dist/{boss-D16fO2oG.d.ts → boss-BiDCbbAQ.d.ts} +60 -7
  8. package/dist/config/index.d.ts +0 -63
  9. package/dist/config/index.js +12 -12
  10. package/dist/config/index.js.map +1 -1
  11. package/dist/contract/index.d.ts +5 -4
  12. package/dist/db/index.d.ts +201 -34
  13. package/dist/db/index.js +294 -158
  14. package/dist/db/index.js.map +1 -1
  15. package/dist/{define-middleware-DfDP39Nq.d.ts → define-middleware-CVKgqo8S.d.ts} +1 -1
  16. package/dist/env/loader.js +24 -1
  17. package/dist/env/loader.js.map +1 -1
  18. package/dist/errors/index.js +1 -381
  19. package/dist/errors/index.js.map +1 -1
  20. package/dist/event/sse/client.d.ts +15 -2
  21. package/dist/event/sse/client.js +38 -2
  22. package/dist/event/sse/client.js.map +1 -1
  23. package/dist/job/index.d.ts +48 -3
  24. package/dist/job/index.js +118 -40
  25. package/dist/job/index.js.map +1 -1
  26. package/dist/logger/index.js +0 -12
  27. package/dist/logger/index.js.map +1 -1
  28. package/dist/middleware/index.d.ts +6 -2
  29. package/dist/middleware/index.js +7 -388
  30. package/dist/middleware/index.js.map +1 -1
  31. package/dist/nextjs/index.d.ts +37 -1
  32. package/dist/nextjs/index.js +40 -1
  33. package/dist/nextjs/index.js.map +1 -1
  34. package/dist/nextjs/server.d.ts +34 -1
  35. package/dist/nextjs/server.js +14 -0
  36. package/dist/nextjs/server.js.map +1 -1
  37. package/dist/ops/index.d.ts +115 -14
  38. package/dist/ops/index.js +339 -15
  39. package/dist/ops/index.js.map +1 -1
  40. package/dist/route/index.d.ts +5 -3
  41. package/dist/route/index.js +1 -1
  42. package/dist/route/index.js.map +1 -1
  43. package/dist/{router-ukNdAZcN.d.ts → route-builder-2ani2jEI.d.ts} +3 -126
  44. package/dist/router-DJdpwuB6.d.ts +127 -0
  45. package/dist/server/index.d.ts +130 -11
  46. package/dist/server/index.js +714 -70
  47. package/dist/server/index.js.map +1 -1
  48. package/dist/{types-Bvvig_tT.d.ts → types-ClQVomgV.d.ts} +1 -1
  49. package/docs/file-upload.md +195 -333
  50. package/package.json +14 -8
  51. package/src/cache/README.md +330 -0
  52. package/src/codegen/README.md +516 -0
  53. package/src/config/README.md +326 -0
  54. package/src/contract/README.md +326 -0
  55. package/src/db/README.md +589 -0
  56. package/src/db/manager/README.md +500 -0
  57. package/src/db/schema/README.md +354 -0
  58. package/src/db/transaction/README.md +822 -0
  59. package/src/env/README.md +651 -0
  60. package/src/errors/README.md +429 -0
  61. package/src/event/README.md +741 -0
  62. package/src/job/README.md +613 -0
  63. package/src/logger/README.md +321 -0
  64. package/src/middleware/README.md +634 -0
  65. package/src/nextjs/README.md +608 -0
  66. package/src/route/README.md +738 -0
  67. package/src/security/README.md +100 -0
  68. package/src/server/README.md +704 -0
@@ -0,0 +1,127 @@
1
+ import { a as NamedMiddleware } from './define-middleware-CVKgqo8S.js';
2
+ import { R as RouteDef } from './route-builder-2ani2jEI.js';
3
+
4
+ /**
5
+ * Router Definition
6
+ *
7
+ * Provides router composition and middleware management
8
+ */
9
+
10
+ /**
11
+ * Router definition - holds all routes
12
+ */
13
+ interface Router<TRoutes extends Record<string, RouteDef<any, any, any> | Router<any>>> {
14
+ routes: TRoutes;
15
+ _routes: TRoutes;
16
+ _packageRouters: Router<any>[];
17
+ _globalMiddlewares: NamedMiddleware<string>[];
18
+ /** The contract version these routes publish, or null when uncontracted. */
19
+ _contractVersion: string | null;
20
+ /**
21
+ * Register package routers (type-hidden)
22
+ *
23
+ * Package routes are:
24
+ * - Recognized by RPC proxy and backend
25
+ * - NOT exposed in client types (use package's own API like authApi, cmsApi)
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * import { authRouter } from '@spfn/auth/server';
30
+ * import { cmsAppRouter } from '@spfn/cms/server';
31
+ *
32
+ * export const appRouter = defineRouter({
33
+ * getRoot,
34
+ * getStatus,
35
+ * })
36
+ * .packages([authRouter, cmsAppRouter]);
37
+ *
38
+ * // Client usage:
39
+ * // api.getRoot.call({}) - app routes
40
+ * // authApi.login.call({}) - package API
41
+ * ```
42
+ */
43
+ packages(routers: Router<any>[]): Router<TRoutes>;
44
+ /**
45
+ * Register global middlewares
46
+ *
47
+ * Applied to all routes unless explicitly skipped via .skip()
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * import { authMiddleware, loggingMiddleware } from './middlewares';
52
+ *
53
+ * export const appRouter = defineRouter({
54
+ * getRoot,
55
+ * getStatus,
56
+ * })
57
+ * .packages([authRouter])
58
+ * .use([authMiddleware, loggingMiddleware]);
59
+ * ```
60
+ */
61
+ use(middlewares: NamedMiddleware<string>[]): Router<TRoutes>;
62
+ /**
63
+ * Declare the contract version these routes publish.
64
+ *
65
+ * A client compiled against this server — a mobile app in a store — is
66
+ * generated from one version of the contract and cannot be updated when the
67
+ * server changes. The server announces this version on every response so
68
+ * that client can tell whether the two ends still agree.
69
+ *
70
+ * This is the version's source. A released snapshot is written to
71
+ * `contracts/released/<version>.json` from what is declared here, so the
72
+ * filename follows the code rather than the code having to be told what the
73
+ * filename said.
74
+ *
75
+ * Only a server with contracted routes needs it. Without it the contract
76
+ * generator still writes `current.json` and still runs the compatibility
77
+ * gate; what it cannot do is cut a release or announce a version.
78
+ *
79
+ * @example
80
+ * ```ts
81
+ * export const appRouter = defineRouter({ getRoot, listItems })
82
+ * .contractVersion('1.2.0')
83
+ * .packages([authRouter]);
84
+ * ```
85
+ */
86
+ contractVersion(version: string): Router<TRoutes>;
87
+ }
88
+ /**
89
+ * Define a router with multiple routes (tRPC-style)
90
+ *
91
+ * Supports chainable API for packages and middlewares:
92
+ *
93
+ * @example
94
+ * ```ts
95
+ * // Basic usage
96
+ * export const appRouter = defineRouter({
97
+ * getRoot,
98
+ * getStatus,
99
+ * listExamples,
100
+ * });
101
+ *
102
+ * // With package routers (type-hidden)
103
+ * export const appRouter = defineRouter({
104
+ * getRoot,
105
+ * getStatus,
106
+ * })
107
+ * .packages([authRouter, cmsAppRouter]);
108
+ *
109
+ * // With global middlewares
110
+ * export const appRouter = defineRouter({
111
+ * getRoot,
112
+ * getStatus,
113
+ * })
114
+ * .packages([authRouter])
115
+ * .use([authMiddleware, loggingMiddleware]);
116
+ *
117
+ * export type AppRouter = typeof appRouter;
118
+ * ```
119
+ *
120
+ * Package routes:
121
+ * - Recognized by RPC proxy and backend for routing
122
+ * - NOT included in AppRouter type (use authApi, cmsApi instead)
123
+ * - Prevents confusion between app API and package APIs
124
+ */
125
+ declare function defineRouter<TRoutes extends Record<string, RouteDef<any, any, any> | Router<any>>>(routes: TRoutes): Router<TRoutes>;
126
+
127
+ export { type Router as R, defineRouter as d };
@@ -5,13 +5,18 @@ import { serve } from '@hono/node-server';
5
5
  import { NamedMiddleware, Router } from '@spfn/core/route';
6
6
  import { OnErrorContext, ProxyGuardConfig, RateLimitOptions } from '@spfn/core/middleware';
7
7
  import { SafeFetchPolicy } from '@spfn/core/security';
8
- import { c as JobRouter, e as BossOptions } from '../boss-D16fO2oG.js';
8
+ import { c as JobRouter, f as BossOptions } from '../boss-BiDCbbAQ.js';
9
9
  import { b as EventRouterDef, E as EventDef } from '../token-manager-BT5EnUAR.js';
10
10
  import { d as SSEHandlerConfig, e as SSEAuthConfig } from '../types-ZQODsBft.js';
11
11
  import { W as WSRouterDef, f as WSHandlerConfig, e as WSMessageHandlers, g as WSAuthConfig } from '../types-2AbaW4Ie.js';
12
12
  import { DatabaseProvider, MigrationStatus, MigrationStatusDb } from '@spfn/core/db';
13
- import '@sinclair/typebox';
13
+ import * as _sinclair_typebox from '@sinclair/typebox';
14
+ import { Static } from '@sinclair/typebox';
15
+ import { R as RouteDef } from '../route-builder-2ani2jEI.js';
14
16
  import 'pg-boss';
17
+ import '../define-middleware-CVKgqo8S.js';
18
+ import 'hono/utils/http-status';
19
+ import '../route/types.js';
15
20
 
16
21
  /**
17
22
  * @deprecated Use `loadEnv` from '@spfn/core/env/loader' instead.
@@ -22,6 +27,33 @@ import 'pg-boss';
22
27
  */
23
28
  declare function loadEnvFiles(): void;
24
29
 
30
+ /** Stable operation identity used by separately deployed clients. */
31
+ declare const CORE_TIME_OPERATION_ID = "core.time";
32
+ /**
33
+ * Closed response shape for the server-time wire capability.
34
+ *
35
+ * Millisecond timestamps use integers throughout SPFN's external contracts.
36
+ */
37
+ declare const ServerTimeResponseSchema: _sinclair_typebox.TObject<{
38
+ serverTimeMillis: _sinclair_typebox.TInteger;
39
+ }>;
40
+ type ServerTimeResponse = Static<typeof ServerTimeResponseSchema>;
41
+ /** Injectable source of Unix epoch milliseconds. */
42
+ interface ServerClock {
43
+ now(): number;
44
+ }
45
+ /**
46
+ * Build the route from a clock so tests and alternate runtimes can supply the
47
+ * epoch source without replacing global time.
48
+ */
49
+ declare function createCoreTimeRoute(clock?: ServerClock): RouteDef<{}, {}, {
50
+ serverTimeMillis: number;
51
+ }>;
52
+ /** The production route and the public wire-contract source of truth. */
53
+ declare const CORE_TIME_ROUTE: RouteDef<{}, {}, {
54
+ serverTimeMillis: number;
55
+ }>;
56
+
25
57
  /**
26
58
  * Workflow router interface for @spfn/core integration
27
59
  *
@@ -202,6 +234,8 @@ interface ServerConfig {
202
234
  * Jobs defined with job()...handler() style
203
235
  * Uses pg-boss for PostgreSQL-based job queue
204
236
  *
237
+ * Merged across every `.jobs()` call, so this holds all of them.
238
+ *
205
239
  * @example
206
240
  * ```typescript
207
241
  * import { job, defineJobRouter } from '@spfn/core/job';
@@ -454,7 +488,9 @@ interface ServerConfig {
454
488
  };
455
489
  /**
456
490
  * Health check endpoint configuration
457
- * Provides monitoring endpoints for Kubernetes probes and load balancers
491
+ *
492
+ * The endpoint answers at `/_core/health`, which no app route can take from
493
+ * it. Point Kubernetes probes and load balancers there.
458
494
  */
459
495
  healthCheck?: {
460
496
  /**
@@ -464,9 +500,19 @@ interface ServerConfig {
464
500
  */
465
501
  enabled?: boolean;
466
502
  /**
467
- * Health check endpoint path
468
- * @default '/health'
469
- * @env HEALTH_CHECK_PATH
503
+ * An additional path to answer on, for a deployment whose probe path is
504
+ * fixed somewhere you cannot change it.
505
+ *
506
+ * BREAKING (@spfn/core 0.4): unset, the endpoint answers at
507
+ * `/_core/health` and nowhere else. It used to default to `/health` and
508
+ * swallow any app route declared there. `/health` now belongs to your
509
+ * app; set this to `'/health'` to have the built-in answer there again.
510
+ *
511
+ * The path is registered before app routes, so an app route on the same
512
+ * path will not run and the server says so at boot.
513
+ *
514
+ * @default undefined — `/_core/health` only
515
+ * @example '/health'
470
516
  */
471
517
  path?: string;
472
518
  /**
@@ -477,6 +523,15 @@ interface ServerConfig {
477
523
  */
478
524
  detailed?: boolean;
479
525
  };
526
+ /**
527
+ * Server-time capability dependencies.
528
+ *
529
+ * The endpoint is always enabled at `GET /_core/time`. Supplying a clock is
530
+ * primarily a deterministic test seam; production defaults to `Date.now()`.
531
+ */
532
+ serverTime?: {
533
+ clock?: ServerClock;
534
+ };
480
535
  /**
481
536
  * Migration boot gate
482
537
  *
@@ -501,17 +556,23 @@ interface ServerConfig {
501
556
  /**
502
557
  * Infrastructure initialization control
503
558
  * Controls automatic initialization of database and Redis
504
- * @default Both enabled if credentials exist
559
+ *
560
+ * Both are initialized unless set to `false` — the credentials are not
561
+ * sniffed. A server that needs no database must say so, otherwise boot
562
+ * fails with `No database configuration found`. A component set to `false`
563
+ * is reported as `disabled` by the health endpoint and never degrades it.
564
+ *
565
+ * @default Both enabled
505
566
  */
506
567
  infrastructure?: {
507
568
  /**
508
569
  * Enable/disable automatic database initialization
509
- * @default true if DATABASE_URL exists
570
+ * @default true
510
571
  */
511
572
  database?: boolean;
512
573
  /**
513
574
  * Enable/disable automatic Redis initialization
514
- * @default true if REDIS_URL exists
575
+ * @default true
515
576
  */
516
577
  redis?: boolean;
517
578
  };
@@ -710,6 +771,43 @@ declare module 'hono' {
710
771
  */
711
772
  declare function createServer(config?: ServerConfig): Promise<Hono>;
712
773
 
774
+ /**
775
+ * The paths `@spfn/core` registers for itself.
776
+ *
777
+ * Its own module because both `create-server.ts` (which registers them) and
778
+ * `helpers.ts` (which reports them in the startup banner) need them, and
779
+ * `create-server.ts` already imports `helpers.ts` — putting them in either file
780
+ * would close a cycle, which `pnpm check:circular` fails the build on.
781
+ *
782
+ * @module server/namespace
783
+ */
784
+ /**
785
+ * The path prefix `@spfn/core` registers its own endpoints under.
786
+ *
787
+ * An app declaring a route in here is declaring something it does not own, and
788
+ * the route will not run — core's endpoints are registered before app routes.
789
+ * `@spfn/auth` (`/_auth/`) and ops routes (`/_ops/`) follow the same convention,
790
+ * and neither has ever had a shadowing defect.
791
+ */
792
+ declare const CORE_NAMESPACE = "/_core";
793
+ /**
794
+ * Where the built-in health endpoint always answers.
795
+ *
796
+ * Point a readiness probe, a Dockerfile `HEALTHCHECK` or an uptime monitor here
797
+ * rather than at `/health`: no app route can claim this path, so the answer does
798
+ * not depend on what the app happens to declare. A probe's path is fixed in
799
+ * places this repository cannot change — a GitOps manifest, a Dockerfile, a load
800
+ * balancer console — and a version bump migrates none of them.
801
+ */
802
+ declare const CORE_HEALTH_PATH = "/_core/health";
803
+ /**
804
+ * Where clients obtain the server's current Unix epoch in milliseconds.
805
+ *
806
+ * This endpoint is registered before application routes and application auth
807
+ * middleware so a client can call it before it has a proof or session.
808
+ */
809
+ declare const CORE_TIME_PATH = "/_core/time";
810
+
713
811
  /**
714
812
  * Start SPFN Server
715
813
  *
@@ -1049,6 +1147,20 @@ declare class ServerConfigBuilder {
1049
1147
  /**
1050
1148
  * Register background jobs router
1051
1149
  *
1150
+ * May be called once per domain router: repeated calls **merge** into one
1151
+ * router, the way `.routes()` composes. Calling it several times used to
1152
+ * keep only the last router and register nothing else, without warning.
1153
+ *
1154
+ * The pg-boss `config` is per server, so pass it on one call only.
1155
+ *
1156
+ * Two names can collide, and both fail loudly:
1157
+ * - a router **key** present in two calls throws here, at config time;
1158
+ * - two jobs sharing a **`job.name`** (the pg-boss queue name) throw in
1159
+ * `registerJobs()` at boot, before any queue or worker is created.
1160
+ *
1161
+ * @throws if a router key was already registered by an earlier call
1162
+ * @throws if `config` is given when an earlier call already gave one
1163
+ *
1052
1164
  * @example
1053
1165
  * ```typescript
1054
1166
  * import { job, defineJobRouter } from '@spfn/core/job';
@@ -1061,7 +1173,8 @@ declare class ServerConfigBuilder {
1061
1173
  *
1062
1174
  * export default defineServerConfig()
1063
1175
  * .routes(appRouter)
1064
- * .jobs(jobRouter)
1176
+ * .jobs(jobRouter, { clearOnStart: isDev }) // config on one call only
1177
+ * .jobs(billingJobRouter) // merged, not replaced
1065
1178
  * .build();
1066
1179
  * ```
1067
1180
  */
@@ -1149,6 +1262,12 @@ declare class ServerConfigBuilder {
1149
1262
  * Configure health check endpoint
1150
1263
  */
1151
1264
  healthCheck(healthCheck: ServerConfig['healthCheck']): this;
1265
+ /**
1266
+ * Supply the clock used by the built-in `GET /_core/time` capability.
1267
+ * Production servers normally keep the default `Date.now()` clock; this is
1268
+ * exposed so tests can assert an exact wire value without replacing globals.
1269
+ */
1270
+ serverTime(serverTime: ServerConfig['serverTime']): this;
1152
1271
  /**
1153
1272
  * Configure infrastructure initialization
1154
1273
  */
@@ -1225,4 +1344,4 @@ declare class ServerConfigBuilder {
1225
1344
  */
1226
1345
  declare function defineServerConfig(): ServerConfigBuilder;
1227
1346
 
1228
- export { type AppFactory, type MigrationSnapshot, PendingMigrationsError, type ServerConfig, type ServerInstance, type ShutdownHookOptions, createServer, createServerlessApp, defineServerConfig, getMigrationSnapshot, getShutdownManager, loadEnvFiles, provisionInfrastructure, resetMigrationSnapshot, resetServerlessApp, startServer };
1347
+ export { type AppFactory, CORE_HEALTH_PATH, CORE_NAMESPACE, CORE_TIME_OPERATION_ID, CORE_TIME_PATH, CORE_TIME_ROUTE, type MigrationSnapshot, PendingMigrationsError, type ServerClock, type ServerConfig, type ServerInstance, type ServerTimeResponse, ServerTimeResponseSchema, type ShutdownHookOptions, createCoreTimeRoute, createServer, createServerlessApp, defineServerConfig, getMigrationSnapshot, getShutdownManager, loadEnvFiles, provisionInfrastructure, resetMigrationSnapshot, resetServerlessApp, startServer };