@pipeline-builder/api-core 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 (184) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +98 -0
  3. package/lib/constants/ai-providers.d.ts +52 -0
  4. package/lib/constants/ai-providers.js +129 -0
  5. package/lib/constants/http-status.d.ts +20 -0
  6. package/lib/constants/http-status.js +26 -0
  7. package/lib/constants/index.d.ts +3 -0
  8. package/lib/constants/index.js +6 -0
  9. package/lib/constants/time.d.ts +10 -0
  10. package/lib/constants/time.js +13 -0
  11. package/lib/errors/app-errors.d.ts +30 -0
  12. package/lib/errors/app-errors.js +53 -0
  13. package/lib/errors/index.d.ts +1 -0
  14. package/lib/errors/index.js +4 -0
  15. package/lib/helpers/access-helpers.d.ts +63 -0
  16. package/lib/helpers/access-helpers.js +82 -0
  17. package/lib/helpers/crud-helpers.d.ts +9 -0
  18. package/lib/helpers/crud-helpers.js +23 -0
  19. package/lib/helpers/index.d.ts +6 -0
  20. package/lib/helpers/index.js +9 -0
  21. package/lib/helpers/org-hierarchy-http.d.ts +74 -0
  22. package/lib/helpers/org-hierarchy-http.js +144 -0
  23. package/lib/helpers/org-hierarchy.d.ts +57 -0
  24. package/lib/helpers/org-hierarchy.js +110 -0
  25. package/lib/helpers/restore-helpers.d.ts +49 -0
  26. package/lib/helpers/restore-helpers.js +78 -0
  27. package/lib/helpers/sse-helpers.d.ts +20 -0
  28. package/lib/helpers/sse-helpers.js +45 -0
  29. package/lib/index.d.ts +58 -0
  30. package/lib/index.js +71 -0
  31. package/lib/middleware/auth.d.ts +208 -0
  32. package/lib/middleware/auth.js +588 -0
  33. package/lib/middleware/index.d.ts +3 -0
  34. package/lib/middleware/index.js +6 -0
  35. package/lib/middleware/mongo-sanitize.d.ts +32 -0
  36. package/lib/middleware/mongo-sanitize.js +100 -0
  37. package/lib/middleware/step-up.d.ts +45 -0
  38. package/lib/middleware/step-up.js +138 -0
  39. package/lib/openapi/extend-zod.d.ts +1 -0
  40. package/lib/openapi/extend-zod.js +6 -0
  41. package/lib/openapi/index.d.ts +2 -0
  42. package/lib/openapi/index.js +5 -0
  43. package/lib/openapi/registry.d.ts +17 -0
  44. package/lib/openapi/registry.js +38 -0
  45. package/lib/openapi/routes/ask-routes.d.ts +7 -0
  46. package/lib/openapi/routes/ask-routes.js +62 -0
  47. package/lib/openapi/routes/billing-routes.d.ts +1 -0
  48. package/lib/openapi/routes/billing-routes.js +66 -0
  49. package/lib/openapi/routes/image-registry-routes.d.ts +7 -0
  50. package/lib/openapi/routes/image-registry-routes.js +98 -0
  51. package/lib/openapi/routes/index.d.ts +5 -0
  52. package/lib/openapi/routes/index.js +28 -0
  53. package/lib/openapi/routes/message-routes.d.ts +1 -0
  54. package/lib/openapi/routes/message-routes.js +105 -0
  55. package/lib/openapi/routes/pipeline-routes.d.ts +1 -0
  56. package/lib/openapi/routes/pipeline-routes.js +105 -0
  57. package/lib/openapi/routes/pipeline-template-routes.d.ts +1 -0
  58. package/lib/openapi/routes/pipeline-template-routes.js +61 -0
  59. package/lib/openapi/routes/plugin-routes.d.ts +1 -0
  60. package/lib/openapi/routes/plugin-routes.js +96 -0
  61. package/lib/openapi/routes/quota-routes.d.ts +1 -0
  62. package/lib/openapi/routes/quota-routes.js +62 -0
  63. package/lib/openapi/schema-registry.d.ts +25 -0
  64. package/lib/openapi/schema-registry.js +91 -0
  65. package/lib/routes/health.d.ts +41 -0
  66. package/lib/routes/health.js +82 -0
  67. package/lib/routes/index.d.ts +1 -0
  68. package/lib/routes/index.js +4 -0
  69. package/lib/services/audit-spool.d.ts +80 -0
  70. package/lib/services/audit-spool.js +172 -0
  71. package/lib/services/cache-service.d.ts +124 -0
  72. package/lib/services/cache-service.js +276 -0
  73. package/lib/services/circuit-breaker.d.ts +45 -0
  74. package/lib/services/circuit-breaker.js +138 -0
  75. package/lib/services/compliance-client.d.ts +46 -0
  76. package/lib/services/compliance-client.js +128 -0
  77. package/lib/services/compliance-event-subscriber.d.ts +16 -0
  78. package/lib/services/compliance-event-subscriber.js +72 -0
  79. package/lib/services/durable-event-bus.d.ts +96 -0
  80. package/lib/services/durable-event-bus.js +204 -0
  81. package/lib/services/entity-events.d.ts +48 -0
  82. package/lib/services/entity-events.js +79 -0
  83. package/lib/services/env-redis.d.ts +44 -0
  84. package/lib/services/env-redis.js +94 -0
  85. package/lib/services/http-client.d.ts +143 -0
  86. package/lib/services/http-client.js +335 -0
  87. package/lib/services/index.d.ts +18 -0
  88. package/lib/services/index.js +20 -0
  89. package/lib/services/leader-lock.d.ts +45 -0
  90. package/lib/services/leader-lock.js +85 -0
  91. package/lib/services/quota.d.ts +130 -0
  92. package/lib/services/quota.js +264 -0
  93. package/lib/services/remote-audit-client.d.ts +160 -0
  94. package/lib/services/remote-audit-client.js +390 -0
  95. package/lib/services/retry-strategy.d.ts +74 -0
  96. package/lib/services/retry-strategy.js +118 -0
  97. package/lib/services/scheduler.d.ts +41 -0
  98. package/lib/services/scheduler.js +86 -0
  99. package/lib/services/service-boot.d.ts +20 -0
  100. package/lib/services/service-boot.js +28 -0
  101. package/lib/services/sse-ticket-store.d.ts +51 -0
  102. package/lib/services/sse-ticket-store.js +130 -0
  103. package/lib/services/token-revocation.d.ts +52 -0
  104. package/lib/services/token-revocation.js +120 -0
  105. package/lib/testing/tier-mock.d.ts +44 -0
  106. package/lib/testing/tier-mock.js +65 -0
  107. package/lib/types/audit-events.d.ts +69 -0
  108. package/lib/types/audit-events.js +4 -0
  109. package/lib/types/catalog-metadata.d.ts +36 -0
  110. package/lib/types/catalog-metadata.js +21 -0
  111. package/lib/types/common.d.ts +255 -0
  112. package/lib/types/common.js +41 -0
  113. package/lib/types/error-codes.d.ts +54 -0
  114. package/lib/types/error-codes.js +107 -0
  115. package/lib/types/feature-flags.d.ts +36 -0
  116. package/lib/types/feature-flags.js +128 -0
  117. package/lib/types/http.d.ts +27 -0
  118. package/lib/types/http.js +4 -0
  119. package/lib/types/index.d.ts +11 -0
  120. package/lib/types/index.js +14 -0
  121. package/lib/types/permissions.d.ts +65 -0
  122. package/lib/types/permissions.js +131 -0
  123. package/lib/types/pipeline-template.d.ts +30 -0
  124. package/lib/types/pipeline-template.js +13 -0
  125. package/lib/types/pipeline.d.ts +59 -0
  126. package/lib/types/pipeline.js +31 -0
  127. package/lib/types/quota-tiers.d.ts +79 -0
  128. package/lib/types/quota-tiers.js +221 -0
  129. package/lib/types/visibility.d.ts +20 -0
  130. package/lib/types/visibility.js +22 -0
  131. package/lib/utils/alias-resolver.d.ts +37 -0
  132. package/lib/utils/alias-resolver.js +68 -0
  133. package/lib/utils/audit.d.ts +15 -0
  134. package/lib/utils/audit.js +39 -0
  135. package/lib/utils/aws-scrub.d.ts +11 -0
  136. package/lib/utils/aws-scrub.js +67 -0
  137. package/lib/utils/compliance-attributes.d.ts +6 -0
  138. package/lib/utils/compliance-attributes.js +69 -0
  139. package/lib/utils/concurrency.d.ts +9 -0
  140. package/lib/utils/concurrency.js +29 -0
  141. package/lib/utils/crash-handlers.d.ts +18 -0
  142. package/lib/utils/crash-handlers.js +36 -0
  143. package/lib/utils/env.d.ts +28 -0
  144. package/lib/utils/env.js +54 -0
  145. package/lib/utils/headers.d.ts +18 -0
  146. package/lib/utils/headers.js +21 -0
  147. package/lib/utils/identity.d.ts +40 -0
  148. package/lib/utils/identity.js +51 -0
  149. package/lib/utils/index.d.ts +17 -0
  150. package/lib/utils/index.js +20 -0
  151. package/lib/utils/logger.d.ts +38 -0
  152. package/lib/utils/logger.js +174 -0
  153. package/lib/utils/metric-emitter.d.ts +36 -0
  154. package/lib/utils/metric-emitter.js +32 -0
  155. package/lib/utils/object.d.ts +13 -0
  156. package/lib/utils/object.js +18 -0
  157. package/lib/utils/params.d.ts +150 -0
  158. package/lib/utils/params.js +206 -0
  159. package/lib/utils/response.d.ts +117 -0
  160. package/lib/utils/response.js +223 -0
  161. package/lib/utils/safe-require.d.ts +13 -0
  162. package/lib/utils/safe-require.js +19 -0
  163. package/lib/utils/secret-encryption.d.ts +176 -0
  164. package/lib/utils/secret-encryption.js +374 -0
  165. package/lib/utils/ssrf.d.ts +30 -0
  166. package/lib/utils/ssrf.js +106 -0
  167. package/lib/validation/ai-schemas.d.ts +56 -0
  168. package/lib/validation/ai-schemas.js +78 -0
  169. package/lib/validation/common-schemas.d.ts +133 -0
  170. package/lib/validation/common-schemas.js +94 -0
  171. package/lib/validation/index.d.ts +7 -0
  172. package/lib/validation/index.js +10 -0
  173. package/lib/validation/message-schemas.d.ts +160 -0
  174. package/lib/validation/message-schemas.js +156 -0
  175. package/lib/validation/middleware.d.ts +72 -0
  176. package/lib/validation/middleware.js +65 -0
  177. package/lib/validation/pipeline-schemas.d.ts +181 -0
  178. package/lib/validation/pipeline-schemas.js +101 -0
  179. package/lib/validation/pipeline-template-schemas.d.ts +155 -0
  180. package/lib/validation/pipeline-template-schemas.js +66 -0
  181. package/lib/validation/plugin-schemas.d.ts +171 -0
  182. package/lib/validation/plugin-schemas.js +81 -0
  183. package/openapi.yaml +292 -0
  184. package/package.json +167 -0
@@ -0,0 +1,208 @@
1
+ import type { Request, Response, NextFunction } from 'express';
2
+ import { type Permission } from '../types/permissions.js';
3
+ export interface RequireAuthOptions {
4
+ /**
5
+ * Allow x-org-id/x-org-name headers to override the JWT's organization fields.
6
+ *
7
+ * When enabled, the `x-org-id`/`x-org-name` headers override the caller's org
8
+ * — but ONLY for a verified SYS-ADMIN (`isSuperAdmin` claim). For any ordinary
9
+ * authenticated user the headers are ignored, so enabling this can never let a
10
+ * normal user impersonate another tenant's org. Use for cross-org admin tooling
11
+ * (e.g. a sysadmin managing a given org's billing). If unsure, leave it disabled.
12
+ */
13
+ allowOrgHeaderOverride?: boolean;
14
+ }
15
+ /** JWT auth middleware. Use directly or call with options. */
16
+ export declare function requireAuth(req: Request, res: Response, next: NextFunction): void;
17
+ export declare function requireAuth(options?: RequireAuthOptions): (req: Request, res: Response, next: NextFunction) => void;
18
+ /**
19
+ * A source of the CURRENT `tokenVersion` for a user, backed by a store the
20
+ * stateless services can read (in practice a Redis key the platform publishes
21
+ * to on every privilege change). Registered via {@link setTokenRevocationStore}.
22
+ *
23
+ * `getCurrentVersion` returns the user's current version, or `null` when the
24
+ * store has no entry — a miss is treated as "no known revocation" (fail-open):
25
+ * revocation entries are published with a TTL matching the access-token
26
+ * lifetime, so a missing entry means any token old enough to predate it has
27
+ * already expired on its own.
28
+ */
29
+ export interface TokenRevocationStore {
30
+ getCurrentVersion(userId: string): Promise<number | null>;
31
+ }
32
+ /**
33
+ * Register (or clear, with `undefined`) the process-wide token-revocation store.
34
+ * When unset (the default), `requireAuth` performs NO revocation check (it relies
35
+ * on the short access-token TTL); services opt in at boot by wiring their Redis
36
+ * client. Platform keeps its own Mongo-backed check and need not register one.
37
+ */
38
+ export declare function setTokenRevocationStore(store: TokenRevocationStore | undefined): void;
39
+ /**
40
+ * Public revocation check for routes that verify a platform JWT OUTSIDE
41
+ * `requireAuth` (e.g. the image-registry `/token` mint path, which resolves
42
+ * identity itself). Returns true when the token's `tokenVersion` is strictly
43
+ * behind the store's current version. Fail-open (never throws) — a store outage
44
+ * or a token/store without a usable version yields false (allow), matching
45
+ * `requireAuth`. Pass the verified JWT claims (`sub`, `tokenVersion`).
46
+ */
47
+ export declare function isAccessTokenRevoked(claims: {
48
+ sub?: string;
49
+ tokenVersion?: number;
50
+ }): Promise<boolean>;
51
+ /**
52
+ * Whether the request's user holds `permission`. Superadmins implicitly hold
53
+ * every permission. Reads the resolved `permissions` claim (set at token issue,
54
+ * or re-derived per request by the platform).
55
+ */
56
+ export declare function userHasPermission(req: Request, permission: Permission): boolean;
57
+ /**
58
+ * Context passed to a registered authorization-denial auditor when a
59
+ * state-changing request is rejected by `requirePermission` /
60
+ * `requireSystemAdmin`.
61
+ */
62
+ export interface AuthzDenialInfo {
63
+ /** The denied user's id (`sub`), if authenticated. */
64
+ actorId?: string;
65
+ actorEmail?: string;
66
+ /** The user's active org at denial time. */
67
+ orgId?: string;
68
+ /** HTTP method + path of the rejected request. */
69
+ method: string;
70
+ path: string;
71
+ /** What was required: the missing permission(s), or 'system-admin'. */
72
+ required: string;
73
+ }
74
+ /**
75
+ * Register (or clear, with `undefined`) the process-wide authorization-denial
76
+ * auditor. Idempotent; the last registration wins.
77
+ */
78
+ export declare function setAuthzDenialAuditor(fn: ((info: AuthzDenialInfo) => void) | undefined): void;
79
+ /**
80
+ * Requires that the user hold AT LEAST ONE of the given permissions (or be a
81
+ * superadmin). Use after requireAuth. Mirrors `requireRole`'s any-of semantics;
82
+ * pass a single permission for a specific action, or several when any one of
83
+ * them should grant access.
84
+ */
85
+ export declare function requirePermission(...permissions: Permission[]): (req: Request, res: Response, next: NextFunction) => void;
86
+ /**
87
+ * Requires that the user hold EVERY one of the given permissions (or be a
88
+ * superadmin) — the AND counterpart to `requirePermission`'s any-of. Use for a
89
+ * sensitive action that legitimately demands two distinct capabilities at once
90
+ * (e.g. a cross-surface operation), so the guarantee is explicit rather than
91
+ * approximated by chaining two `requirePermission` gates. Superadmins bypass
92
+ * (they implicitly hold every permission).
93
+ */
94
+ export declare function requireAllPermissions(...permissions: Permission[]): (req: Request, res: Response, next: NextFunction) => void;
95
+ /**
96
+ * Like {@link requirePermission} (any-of) but ALSO admits an internal service
97
+ * principal (a `service:*` machine token). For READ endpoints that BOTH
98
+ * interactive users — who must hold one of the `:read` capabilities — AND
99
+ * service-to-service callers legitimately hit; service tokens carry no
100
+ * permission claims (they're least-privilege `role:member`), so a plain
101
+ * `requirePermission` would wrongly 403 them. Superadmins pass via
102
+ * `userHasPermission`'s implicit-all. Use only where a service caller is a known,
103
+ * intended consumer of the route (verify the callers); prefer plain
104
+ * `requirePermission` for purely user-facing reads.
105
+ */
106
+ export declare function requirePermissionOrService(...permissions: Permission[]): (req: Request, res: Response, next: NextFunction) => void;
107
+ /**
108
+ * Gate an endpoint to internal service-to-service callers only (a `service:*`
109
+ * machine principal minted via {@link signServiceToken} / `getServiceAuthHeader`).
110
+ * Rejects any user token. For NON-user-facing endpoints — entity-event ingest,
111
+ * auto-subscribe, and similar peer-service hooks — that a browser must never
112
+ * reach. Compose after `requireAuth` so `req.user` is populated.
113
+ */
114
+ export declare function requireServicePrincipal(req: Request, res: Response, next: NextFunction): void;
115
+ /**
116
+ * The system tenant's canonical org **id** — a fixed, well-known ObjectId (NOT
117
+ * the string 'system'). This is the single knob every service resolves the
118
+ * system tenant through; override via the `SYSTEM_ORG_ID` env for alternate
119
+ * installs. The system org's human identifier stays the slug/name 'system'
120
+ * (see {@link SYSTEM_ORG_SLUG}); only its `_id` is this ObjectId.
121
+ */
122
+ export declare const SYSTEM_ORG_ID: string;
123
+ /** The system org's well-known slug/name (the human identifier; its `_id` is {@link SYSTEM_ORG_ID}). */
124
+ export declare const SYSTEM_ORG_SLUG = "system";
125
+ /**
126
+ * Check if an orgId or orgName/slug matches the system org. Use this instead of
127
+ * comparing directly: the id is now an ObjectId ({@link SYSTEM_ORG_ID}) while the
128
+ * name/slug is 'system' ({@link SYSTEM_ORG_SLUG}), so the two are compared against
129
+ * their respective canonical values.
130
+ */
131
+ export declare function isSystemOrgId(orgId?: string, orgName?: string): boolean;
132
+ /**
133
+ * Check if the request is from a system admin.
134
+ *
135
+ * Authority is granted solely by the user-level `isSuperAdmin` flag carried
136
+ * in the JWT. The "membership in the well-known 'system' org with role
137
+ * admin/owner" path was removed — it conflated a Pipeline Builder operator
138
+ * with a real customer tenant in the data model, and meant any unintended
139
+ * write that created a 'system'-named org could quietly grant cross-org
140
+ * authority. The `system` org still exists as a *content holder* for shared
141
+ * sample data; it just no longer confers privilege.
142
+ */
143
+ export declare function isSystemAdmin(req: Request): boolean;
144
+ /**
145
+ * Whether the request's token carries a specific capability `scope` (e.g.
146
+ * `'reporting:ingest'`). Used to gate endpoints that accept a narrow machine
147
+ * identity — a normal interactive user token has no `scope` and returns false.
148
+ */
149
+ export declare function hasScope(req: Request, scope: string): boolean;
150
+ /** Requires a system admin — granted solely by the `isSuperAdmin` token claim
151
+ * (the org-membership path was removed). Use after requireAuth. */
152
+ export declare function requireSystemAdmin(req: Request, res: Response, next: NextFunction): void;
153
+ /**
154
+ * Require a specific feature flag. Use after requireAuth.
155
+ * Checks the `features` array in the JWT payload (set at token issuance).
156
+ * Sysadmins (isSuperAdmin) bypass — they always have every feature.
157
+ */
158
+ export declare function requireFeature(feature: string): (req: Request, res: Response, next: NextFunction) => void;
159
+ /** Replace the service-token denylist (service names). Empty ⇒ kill-switch off. */
160
+ export declare function setServiceTokenDenylist(names: Iterable<string>): void;
161
+ /** True when `sub` is `service:<name>` and `<name>` is on the denylist. */
162
+ export declare function isServiceTokenDenied(sub: string): boolean;
163
+ export interface ServiceTokenOptions {
164
+ /** Calling service identifier (e.g. 'billing', 'platform'). Embedded as `sub: service:<name>`. */
165
+ serviceName: string;
166
+ /** Active org context for the call. Use the target tenant's org id, or the
167
+ * well-known `SYSTEM_ORG_ID` (an ObjectId) for system-wide ops — NOT the string
168
+ * 'system', which id-based system-org checks won't recognize. */
169
+ orgId?: string;
170
+ /** Active org name. Defaults to orgId. */
171
+ orgName?: string;
172
+ /** TTL in seconds (default 300). */
173
+ ttlSeconds?: number;
174
+ /**
175
+ * Role the token carries (required — no implicit default). **Pass the LOWEST
176
+ * role the call actually needs** (`'member'` for read / data-plane calls) so a
177
+ * leaked service token can't perform admin actions in the target org.
178
+ * `isAdmin` is derived from this (admin|owner → true).
179
+ */
180
+ role: 'owner' | 'admin' | 'member';
181
+ /**
182
+ * Optional fine-grained permission claim. Prefer this + `role: 'member'` over a
183
+ * high role when a call needs ONE specific capability (e.g. `['plugins:write']`
184
+ * to push a plugin image) — the token then satisfies that permission gate
185
+ * WITHOUT carrying `isAdmin`, so a leaked token can't perform admin actions.
186
+ */
187
+ permissions?: Permission[];
188
+ }
189
+ /**
190
+ * Mint a JWT identifying the calling service. Used for inter-service HTTP calls.
191
+ * The token satisfies `requireAuth` and (when orgId is present) `requireOrganization`.
192
+ * Scope it with `opts.role` — least privilege keeps a leaked token low-value.
193
+ */
194
+ export declare function signServiceToken(opts: ServiceTokenOptions): string;
195
+ /** Convenience: returns a `Bearer <token>` header value for fetch/axios calls. */
196
+ export declare function getServiceAuthHeader(opts: ServiceTokenOptions): string;
197
+ /** True when `req.user.sub` was issued by `signServiceToken` (i.e. starts with `service:`). */
198
+ export declare function isServicePrincipal(req: Request): boolean;
199
+ /**
200
+ * PRE-auth check: cryptographically verify the request carries a valid, signed
201
+ * SERVICE token (`sub` starts with `service:`). Unlike {@link isServicePrincipal}
202
+ * (which reads the already-populated `req.user`), this verifies the bearer token
203
+ * itself, so it is safe to call BEFORE `requireAuth` runs — e.g. the global rate
204
+ * limiter's `skip`, which must not trust the spoofable `x-internal-service`
205
+ * header. Mirrors `requireAuth`'s verification (algorithm pinning + optional
206
+ * issuer/audience) and returns `false` on any missing/invalid/non-service token.
207
+ */
208
+ export declare function verifyServicePrincipal(req: Request): boolean;