@opensaas/stack-core 0.44.0-alpha.6 → 0.44.0-alpha.7

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 (352) hide show
  1. package/CHANGELOG.md +655 -0
  2. package/dist/.build-hash +1 -1
  3. package/dist/access/actor.d.ts +56 -0
  4. package/dist/access/actor.d.ts.map +1 -0
  5. package/dist/access/actor.js +66 -0
  6. package/dist/access/actor.js.map +1 -0
  7. package/dist/access/decide-advertisement.d.ts +55 -0
  8. package/dist/access/decide-advertisement.d.ts.map +1 -0
  9. package/dist/access/decide-advertisement.js +125 -0
  10. package/dist/access/decide-advertisement.js.map +1 -0
  11. package/dist/access/declared-dependencies.d.ts +4 -89
  12. package/dist/access/declared-dependencies.d.ts.map +1 -1
  13. package/dist/access/declared-dependencies.js +14 -84
  14. package/dist/access/declared-dependencies.js.map +1 -1
  15. package/dist/access/depth-limits.d.ts +2 -0
  16. package/dist/access/depth-limits.d.ts.map +1 -1
  17. package/dist/access/depth-limits.js +2 -0
  18. package/dist/access/depth-limits.js.map +1 -1
  19. package/dist/access/engine.d.ts +1 -24
  20. package/dist/access/engine.d.ts.map +1 -1
  21. package/dist/access/engine.js +0 -35
  22. package/dist/access/engine.js.map +1 -1
  23. package/dist/access/field-access.d.ts +21 -19
  24. package/dist/access/field-access.d.ts.map +1 -1
  25. package/dist/access/field-access.js +100 -122
  26. package/dist/access/field-access.js.map +1 -1
  27. package/dist/access/index.d.ts +3 -6
  28. package/dist/access/index.d.ts.map +1 -1
  29. package/dist/access/index.js +3 -6
  30. package/dist/access/index.js.map +1 -1
  31. package/dist/access/rule-item.d.ts +7 -7
  32. package/dist/access/rule-item.d.ts.map +1 -1
  33. package/dist/access/rule-item.js +38 -38
  34. package/dist/access/rule-item.js.map +1 -1
  35. package/dist/access/session-vocabulary.d.ts +124 -0
  36. package/dist/access/session-vocabulary.d.ts.map +1 -0
  37. package/dist/access/session-vocabulary.js +366 -0
  38. package/dist/access/session-vocabulary.js.map +1 -0
  39. package/dist/access/transaction-registry.d.ts +6 -0
  40. package/dist/access/transaction-registry.d.ts.map +1 -1
  41. package/dist/access/transaction-registry.js +19 -2
  42. package/dist/access/transaction-registry.js.map +1 -1
  43. package/dist/access/types.d.ts +55 -6
  44. package/dist/access/types.d.ts.map +1 -1
  45. package/dist/access/vocabulary-filter-specs.d.ts +17 -0
  46. package/dist/access/vocabulary-filter-specs.d.ts.map +1 -0
  47. package/dist/access/vocabulary-filter-specs.js +27 -0
  48. package/dist/access/vocabulary-filter-specs.js.map +1 -0
  49. package/dist/client-safe.d.ts +8 -0
  50. package/dist/client-safe.d.ts.map +1 -0
  51. package/dist/client-safe.js +8 -0
  52. package/dist/client-safe.js.map +1 -0
  53. package/dist/config/field-kind.d.ts +7 -21
  54. package/dist/config/field-kind.d.ts.map +1 -1
  55. package/dist/config/field-kind.js +8 -5
  56. package/dist/config/field-kind.js.map +1 -1
  57. package/dist/config/index.d.ts +1 -1
  58. package/dist/config/index.d.ts.map +1 -1
  59. package/dist/config/index.js +3 -2
  60. package/dist/config/index.js.map +1 -1
  61. package/dist/config/mcp-default-tools.d.ts +3 -0
  62. package/dist/config/mcp-default-tools.d.ts.map +1 -0
  63. package/dist/config/mcp-default-tools.js +14 -0
  64. package/dist/config/mcp-default-tools.js.map +1 -0
  65. package/dist/config/mcp-tool-names.d.ts +2 -0
  66. package/dist/config/mcp-tool-names.d.ts.map +1 -1
  67. package/dist/config/mcp-tool-names.js +17 -5
  68. package/dist/config/mcp-tool-names.js.map +1 -1
  69. package/dist/config/nav-count.d.ts +8 -16
  70. package/dist/config/nav-count.d.ts.map +1 -1
  71. package/dist/config/nav-count.js +20 -32
  72. package/dist/config/nav-count.js.map +1 -1
  73. package/dist/config/plugin-engine.d.ts.map +1 -1
  74. package/dist/config/plugin-engine.js +6 -0
  75. package/dist/config/plugin-engine.js.map +1 -1
  76. package/dist/config/types.d.ts +141 -28
  77. package/dist/config/types.d.ts.map +1 -1
  78. package/dist/context/engine-context.d.ts +2 -0
  79. package/dist/context/engine-context.d.ts.map +1 -1
  80. package/dist/context/engine-context.js +8 -0
  81. package/dist/context/engine-context.js.map +1 -1
  82. package/dist/context/hook-pipeline.d.ts +2 -1
  83. package/dist/context/hook-pipeline.d.ts.map +1 -1
  84. package/dist/context/hook-pipeline.js +4 -2
  85. package/dist/context/hook-pipeline.js.map +1 -1
  86. package/dist/context/index.d.ts +5 -1
  87. package/dist/context/index.d.ts.map +1 -1
  88. package/dist/context/index.js +156 -84
  89. package/dist/context/index.js.map +1 -1
  90. package/dist/context/junction.d.ts +28 -21
  91. package/dist/context/junction.d.ts.map +1 -1
  92. package/dist/context/junction.js +74 -69
  93. package/dist/context/junction.js.map +1 -1
  94. package/dist/context/plugin-field-write.d.ts.map +1 -1
  95. package/dist/context/plugin-field-write.js +19 -25
  96. package/dist/context/plugin-field-write.js.map +1 -1
  97. package/dist/context/process-lifetime.d.ts +8 -0
  98. package/dist/context/process-lifetime.d.ts.map +1 -0
  99. package/dist/context/process-lifetime.js +14 -0
  100. package/dist/context/process-lifetime.js.map +1 -0
  101. package/dist/context/relationship-input.d.ts +2 -15
  102. package/dist/context/relationship-input.d.ts.map +1 -1
  103. package/dist/context/relationship-input.js +44 -85
  104. package/dist/context/relationship-input.js.map +1 -1
  105. package/dist/context/transaction-boundary.d.ts +3 -2
  106. package/dist/context/transaction-boundary.d.ts.map +1 -1
  107. package/dist/context/transaction-boundary.js +17 -2
  108. package/dist/context/transaction-boundary.js.map +1 -1
  109. package/dist/context/write-pipeline.d.ts +5 -19
  110. package/dist/context/write-pipeline.d.ts.map +1 -1
  111. package/dist/context/write-pipeline.js +40 -98
  112. package/dist/context/write-pipeline.js.map +1 -1
  113. package/dist/context/write-target.d.ts +52 -0
  114. package/dist/context/write-target.d.ts.map +1 -0
  115. package/dist/context/write-target.js +118 -0
  116. package/dist/context/write-target.js.map +1 -0
  117. package/dist/contract/dependencies.d.ts +24 -10
  118. package/dist/contract/dependencies.d.ts.map +1 -1
  119. package/dist/contract/dependencies.js +120 -80
  120. package/dist/contract/dependencies.js.map +1 -1
  121. package/dist/contract/derive.d.ts +6 -11
  122. package/dist/contract/derive.d.ts.map +1 -1
  123. package/dist/contract/derive.js +256 -211
  124. package/dist/contract/derive.js.map +1 -1
  125. package/dist/contract/id-boundary.js +1 -1
  126. package/dist/contract/id-boundary.js.map +1 -1
  127. package/dist/contract/strategy.d.ts +13 -0
  128. package/dist/contract/strategy.d.ts.map +1 -0
  129. package/dist/contract/strategy.js +38 -0
  130. package/dist/contract/strategy.js.map +1 -0
  131. package/dist/db/client-alias.d.ts +12 -0
  132. package/dist/db/client-alias.d.ts.map +1 -0
  133. package/dist/db/client-alias.js +8 -0
  134. package/dist/db/client-alias.js.map +1 -0
  135. package/dist/extend.d.ts +1 -1
  136. package/dist/extend.d.ts.map +1 -1
  137. package/dist/extend.js.map +1 -1
  138. package/dist/fields/index.d.ts +0 -3
  139. package/dist/fields/index.d.ts.map +1 -1
  140. package/dist/fields/index.js +25 -10
  141. package/dist/fields/index.js.map +1 -1
  142. package/dist/filter/collect.d.ts +28 -39
  143. package/dist/filter/collect.d.ts.map +1 -1
  144. package/dist/filter/collect.js +46 -68
  145. package/dist/filter/collect.js.map +1 -1
  146. package/dist/filter/index.d.ts +1 -1
  147. package/dist/filter/index.d.ts.map +1 -1
  148. package/dist/filter/index.js +1 -1
  149. package/dist/filter/index.js.map +1 -1
  150. package/dist/filter/map.d.ts +4 -0
  151. package/dist/filter/map.d.ts.map +1 -1
  152. package/dist/filter/map.js +36 -3
  153. package/dist/filter/map.js.map +1 -1
  154. package/dist/hooks/index.d.ts +4 -1
  155. package/dist/hooks/index.d.ts.map +1 -1
  156. package/dist/hooks/index.js +34 -5
  157. package/dist/hooks/index.js.map +1 -1
  158. package/dist/index.d.ts +8 -7
  159. package/dist/index.d.ts.map +1 -1
  160. package/dist/index.js +5 -5
  161. package/dist/index.js.map +1 -1
  162. package/dist/internal.d.ts +5 -2
  163. package/dist/internal.d.ts.map +1 -1
  164. package/dist/internal.js +4 -5
  165. package/dist/internal.js.map +1 -1
  166. package/dist/mcp/advertise.d.ts +5 -13
  167. package/dist/mcp/advertise.d.ts.map +1 -1
  168. package/dist/mcp/advertise.js +7 -22
  169. package/dist/mcp/advertise.js.map +1 -1
  170. package/dist/mcp/arguments.d.ts.map +1 -1
  171. package/dist/mcp/arguments.js +8 -11
  172. package/dist/mcp/arguments.js.map +1 -1
  173. package/dist/mcp/field-schema.d.ts +4 -15
  174. package/dist/mcp/field-schema.d.ts.map +1 -1
  175. package/dist/mcp/field-schema.js +41 -71
  176. package/dist/mcp/field-schema.js.map +1 -1
  177. package/dist/mcp/handler.d.ts +111 -18
  178. package/dist/mcp/handler.d.ts.map +1 -1
  179. package/dist/mcp/handler.js +168 -73
  180. package/dist/mcp/handler.js.map +1 -1
  181. package/dist/mcp/index.d.ts +2 -1
  182. package/dist/mcp/index.d.ts.map +1 -1
  183. package/dist/mcp/index.js +1 -1
  184. package/dist/mcp/index.js.map +1 -1
  185. package/dist/mcp/procedure-tools.d.ts +63 -0
  186. package/dist/mcp/procedure-tools.d.ts.map +1 -0
  187. package/dist/mcp/procedure-tools.js +139 -0
  188. package/dist/mcp/procedure-tools.js.map +1 -0
  189. package/dist/mcp/projection.d.ts.map +1 -1
  190. package/dist/mcp/projection.js +28 -36
  191. package/dist/mcp/projection.js.map +1 -1
  192. package/dist/mcp/reachability.d.ts +5 -1
  193. package/dist/mcp/reachability.d.ts.map +1 -1
  194. package/dist/mcp/reachability.js +24 -5
  195. package/dist/mcp/reachability.js.map +1 -1
  196. package/dist/mcp/relation-member.d.ts +15 -0
  197. package/dist/mcp/relation-member.d.ts.map +1 -0
  198. package/dist/mcp/relation-member.js +50 -0
  199. package/dist/mcp/relation-member.js.map +1 -0
  200. package/dist/mcp/types.d.ts +12 -0
  201. package/dist/mcp/types.d.ts.map +1 -1
  202. package/dist/mcp/where-id-boundary.d.ts.map +1 -1
  203. package/dist/mcp/where-id-boundary.js +12 -24
  204. package/dist/mcp/where-id-boundary.js.map +1 -1
  205. package/dist/mcp/where-reachability.d.ts +2 -0
  206. package/dist/mcp/where-reachability.d.ts.map +1 -1
  207. package/dist/mcp/where-reachability.js +39 -23
  208. package/dist/mcp/where-reachability.js.map +1 -1
  209. package/dist/members/claims.d.ts +71 -0
  210. package/dist/members/claims.d.ts.map +1 -0
  211. package/dist/members/claims.js +199 -0
  212. package/dist/members/claims.js.map +1 -0
  213. package/dist/members/list-members.d.ts +87 -0
  214. package/dist/members/list-members.d.ts.map +1 -0
  215. package/dist/members/list-members.js +250 -0
  216. package/dist/members/list-members.js.map +1 -0
  217. package/dist/procedures/client.d.ts +163 -0
  218. package/dist/procedures/client.d.ts.map +1 -0
  219. package/dist/procedures/client.js +152 -0
  220. package/dist/procedures/client.js.map +1 -0
  221. package/dist/procedures/codes.d.ts +23 -0
  222. package/dist/procedures/codes.d.ts.map +1 -0
  223. package/dist/procedures/codes.js +46 -0
  224. package/dist/procedures/codes.js.map +1 -0
  225. package/dist/procedures/context-face.d.ts +4 -0
  226. package/dist/procedures/context-face.d.ts.map +1 -0
  227. package/dist/procedures/context-face.js +31 -0
  228. package/dist/procedures/context-face.js.map +1 -0
  229. package/dist/procedures/failures.d.ts +31 -0
  230. package/dist/procedures/failures.d.ts.map +1 -0
  231. package/dist/procedures/failures.js +51 -0
  232. package/dist/procedures/failures.js.map +1 -0
  233. package/dist/procedures/http.d.ts +114 -0
  234. package/dist/procedures/http.d.ts.map +1 -0
  235. package/dist/procedures/http.js +224 -0
  236. package/dist/procedures/http.js.map +1 -0
  237. package/dist/procedures/index.d.ts +7 -0
  238. package/dist/procedures/index.d.ts.map +1 -0
  239. package/dist/procedures/index.js +11 -0
  240. package/dist/procedures/index.js.map +1 -0
  241. package/dist/procedures/procedure.d.ts +204 -0
  242. package/dist/procedures/procedure.d.ts.map +1 -0
  243. package/dist/procedures/procedure.js +123 -0
  244. package/dist/procedures/procedure.js.map +1 -0
  245. package/dist/procedures/result.d.ts +42 -0
  246. package/dist/procedures/result.d.ts.map +1 -0
  247. package/dist/procedures/result.js +51 -0
  248. package/dist/procedures/result.js.map +1 -0
  249. package/dist/procedures/run.d.ts +36 -0
  250. package/dist/procedures/run.d.ts.map +1 -0
  251. package/dist/procedures/run.js +76 -0
  252. package/dist/procedures/run.js.map +1 -0
  253. package/dist/procedures/server.d.ts +131 -0
  254. package/dist/procedures/server.d.ts.map +1 -0
  255. package/dist/procedures/server.js +115 -0
  256. package/dist/procedures/server.js.map +1 -0
  257. package/dist/procedures/validate.d.ts +60 -0
  258. package/dist/procedures/validate.d.ts.map +1 -0
  259. package/dist/procedures/validate.js +254 -0
  260. package/dist/procedures/validate.js.map +1 -0
  261. package/dist/procedures/web-session.d.ts +12 -0
  262. package/dist/procedures/web-session.d.ts.map +1 -0
  263. package/dist/procedures/web-session.js +35 -0
  264. package/dist/procedures/web-session.js.map +1 -0
  265. package/dist/query/relationship-options.d.ts +2 -0
  266. package/dist/query/relationship-options.d.ts.map +1 -1
  267. package/dist/query/relationship-options.js +3 -2
  268. package/dist/query/relationship-options.js.map +1 -1
  269. package/dist/secured/binding.d.ts +84 -0
  270. package/dist/secured/binding.d.ts.map +1 -0
  271. package/dist/secured/binding.js +52 -0
  272. package/dist/secured/binding.js.map +1 -0
  273. package/dist/secured/declared-branch.d.ts +11 -0
  274. package/dist/secured/declared-branch.d.ts.map +1 -0
  275. package/dist/secured/declared-branch.js +17 -0
  276. package/dist/secured/declared-branch.js.map +1 -0
  277. package/dist/secured/field-visibility.d.ts +56 -0
  278. package/dist/secured/field-visibility.d.ts.map +1 -0
  279. package/dist/secured/field-visibility.js +624 -0
  280. package/dist/secured/field-visibility.js.map +1 -0
  281. package/dist/secured/include.d.ts +68 -41
  282. package/dist/secured/include.d.ts.map +1 -1
  283. package/dist/secured/include.js +217 -115
  284. package/dist/secured/include.js.map +1 -1
  285. package/dist/secured/lower.d.ts.map +1 -1
  286. package/dist/secured/lower.js +1 -3
  287. package/dist/secured/lower.js.map +1 -1
  288. package/dist/secured/read.d.ts +11 -108
  289. package/dist/secured/read.d.ts.map +1 -1
  290. package/dist/secured/read.js +46 -633
  291. package/dist/secured/read.js.map +1 -1
  292. package/dist/secured/select.d.ts +0 -24
  293. package/dist/secured/select.d.ts.map +1 -1
  294. package/dist/secured/select.js +60 -153
  295. package/dist/secured/select.js.map +1 -1
  296. package/dist/secured/vocabulary.d.ts +13 -1
  297. package/dist/secured/vocabulary.d.ts.map +1 -1
  298. package/dist/secured/vocabulary.js +173 -83
  299. package/dist/secured/vocabulary.js.map +1 -1
  300. package/dist/testing/untyped-rule.d.ts +8 -0
  301. package/dist/testing/untyped-rule.d.ts.map +1 -0
  302. package/dist/testing/untyped-rule.js +4 -0
  303. package/dist/testing/untyped-rule.js.map +1 -0
  304. package/dist/types/index.d.ts +1 -1
  305. package/dist/types/index.d.ts.map +1 -1
  306. package/dist/types/rows.d.ts +18 -1
  307. package/dist/types/rows.d.ts.map +1 -1
  308. package/dist/validation/config-refusal.d.ts +3 -1
  309. package/dist/validation/config-refusal.d.ts.map +1 -1
  310. package/dist/validation/database-config.d.ts +1 -0
  311. package/dist/validation/database-config.d.ts.map +1 -1
  312. package/dist/validation/database-config.js +1 -1
  313. package/dist/validation/database-config.js.map +1 -1
  314. package/dist/validation/default-value.d.ts.map +1 -1
  315. package/dist/validation/default-value.js +3 -8
  316. package/dist/validation/default-value.js.map +1 -1
  317. package/dist/validation/extension-packs.d.ts.map +1 -1
  318. package/dist/validation/extension-packs.js +1 -0
  319. package/dist/validation/extension-packs.js.map +1 -1
  320. package/dist/validation/field-config.d.ts +3 -2
  321. package/dist/validation/field-config.d.ts.map +1 -1
  322. package/dist/validation/field-config.js +3 -1
  323. package/dist/validation/field-config.js.map +1 -1
  324. package/dist/validation/field-names.d.ts +2 -1
  325. package/dist/validation/field-names.d.ts.map +1 -1
  326. package/dist/validation/field-names.js +76 -63
  327. package/dist/validation/field-names.js.map +1 -1
  328. package/dist/validation/identifier-names.d.ts +1 -0
  329. package/dist/validation/identifier-names.d.ts.map +1 -1
  330. package/dist/validation/identifier-names.js +17 -9
  331. package/dist/validation/identifier-names.js.map +1 -1
  332. package/dist/validation/needs-closure.d.ts +30 -14
  333. package/dist/validation/needs-closure.d.ts.map +1 -1
  334. package/dist/validation/needs-closure.js +184 -31
  335. package/dist/validation/needs-closure.js.map +1 -1
  336. package/dist/validation/schema.d.ts +1 -0
  337. package/dist/validation/schema.d.ts.map +1 -1
  338. package/dist/validation/schema.js +4 -2
  339. package/dist/validation/schema.js.map +1 -1
  340. package/package.json +20 -4
  341. package/dist/access/field-visibility.d.ts +0 -11
  342. package/dist/access/field-visibility.d.ts.map +0 -1
  343. package/dist/access/field-visibility.js +0 -366
  344. package/dist/access/field-visibility.js.map +0 -1
  345. package/dist/access/query-validation.d.ts +0 -30
  346. package/dist/access/query-validation.d.ts.map +0 -1
  347. package/dist/access/query-validation.js +0 -49
  348. package/dist/access/query-validation.js.map +0 -1
  349. package/dist/access/system-fields.d.ts +0 -2
  350. package/dist/access/system-fields.d.ts.map +0 -1
  351. package/dist/access/system-fields.js +0 -7
  352. package/dist/access/system-fields.js.map +0 -1
@@ -0,0 +1,204 @@
1
+ import type * as z from 'zod';
2
+ import { type ProcedureFail } from './failures.js';
3
+ import { type ProcedureResult, type ReservedFailureCode } from './result.js';
4
+ /** A surface a procedure can be reached through. */
5
+ export type ProcedureSurface = 'web' | 'mcp';
6
+ /** The surfaces a procedure is exposed on. Never empty. */
7
+ export type ProcedureExposure = readonly [ProcedureSurface, ...ProcedureSurface[]];
8
+ /**
9
+ * The members of a transaction's context a procedure may reach. The generated
10
+ * `Context`'s transaction context satisfies it.
11
+ */
12
+ export interface ProcedureHostTransactionContext {
13
+ db: object;
14
+ session: object | null;
15
+ storage: object;
16
+ plugins: object;
17
+ advisoryLock: (key: string) => Promise<void>;
18
+ transaction: <T>(fn: () => Promise<T>) => Promise<T>;
19
+ }
20
+ /**
21
+ * The members of an application context a procedure may reach. The generated
22
+ * `Context` satisfies it; a procedure is handed only these members, narrowed
23
+ * at runtime.
24
+ *
25
+ * @typeParam Tx - the context `transaction` hands its callback.
26
+ */
27
+ export interface ProcedureHostContext<Tx extends ProcedureHostTransactionContext = ProcedureHostTransactionContext> {
28
+ db: object;
29
+ session: object | null;
30
+ storage: object;
31
+ plugins: object;
32
+ transaction: <T>(fn: (txContext: Tx) => Promise<T>) => Promise<T>;
33
+ }
34
+ /** The context an application context's `transaction` hands its callback. */
35
+ export type TransactionContextOf<C> = C extends {
36
+ transaction: (fn: (txContext: infer Tx extends ProcedureHostTransactionContext) => Promise<unknown>) => unknown;
37
+ } ? Tx : never;
38
+ /**
39
+ * What a callback given to a procedure's `context.transaction` receives: the
40
+ * transaction-bound `db`, whose reads can take a row lock with `.forUpdate()`,
41
+ * the session, storage, plugin services and `advisoryLock`. It is narrowed at
42
+ * runtime exactly as {@link ProcedureContext} is. `transaction` on it joins
43
+ * the open transaction and hands its callback this same context.
44
+ *
45
+ * @typeParam Tx - the application's own transaction context, usually
46
+ * `TransactionContextOf<Context>`.
47
+ */
48
+ export interface ProcedureTransactionContext<Tx extends ProcedureHostTransactionContext> {
49
+ readonly db: Tx['db'];
50
+ readonly session: Tx['session'];
51
+ readonly storage: Tx['storage'];
52
+ readonly plugins: Tx['plugins'];
53
+ /**
54
+ * Take PostgreSQL's transaction-scoped advisory lock on `key`, waiting until
55
+ * it is free. Released when the transaction ends.
56
+ */
57
+ readonly advisoryLock: (key: string) => Promise<void>;
58
+ readonly transaction: <T>(fn: (txContext: ProcedureTransactionContext<Tx>) => Promise<T>) => Promise<T>;
59
+ }
60
+ /**
61
+ * What `access` and `run` receive as `context`: the secured `db`, the session,
62
+ * storage, plugin services and `transaction`. `sudo()`, `unsafe`,
63
+ * `withSession()`, `serverAction` and the engine's own face are absent, at
64
+ * runtime as well as in the type.
65
+ */
66
+ export interface ProcedureContext<C extends ProcedureHostContext<Tx>, Tx extends ProcedureHostTransactionContext = TransactionContextOf<C>> {
67
+ readonly db: C['db'];
68
+ readonly session: C['session'];
69
+ readonly storage: C['storage'];
70
+ readonly plugins: C['plugins'];
71
+ /**
72
+ * Run `fn` inside one transaction; a throw anywhere in it, a declared
73
+ * failure included, rolls every write in it back, and a declared failure
74
+ * `fn` returns is treated as thrown. `run` opens no
75
+ * transaction of its own, so work that must not hold row locks (an SMS, a
76
+ * model call) sits outside this callback. The transaction runs at Read
77
+ * Committed and takes no options; a concurrency-sensitive rule takes a row
78
+ * lock inside it (ADR-0047).
79
+ */
80
+ readonly transaction: <T>(fn: (txContext: ProcedureTransactionContext<Tx>) => Promise<T>) => Promise<T>;
81
+ }
82
+ /** The argument a procedure's `access` rule receives. It never sees the input. */
83
+ export interface ProcedureAccessArgs<C extends ProcedureHostContext<Tx>, Tx extends ProcedureHostTransactionContext = TransactionContextOf<C>> {
84
+ session: C['session'];
85
+ context: ProcedureContext<C, Tx>;
86
+ }
87
+ /** The argument a procedure's `run` receives. */
88
+ export interface ProcedureRunArgs<C extends ProcedureHostContext<Tx>, TInput, TCode extends string = never, Tx extends ProcedureHostTransactionContext = TransactionContextOf<C>> {
89
+ input: TInput;
90
+ context: ProcedureContext<C, Tx>;
91
+ /**
92
+ * Constructs one of the procedure's declared failures. **Throw** what it
93
+ * returns: `throw fail('SLOT_FULL')`. One returned from `run` or from a
94
+ * `context.transaction` callback is treated as thrown. A code the procedure
95
+ * does not declare is a compile error.
96
+ */
97
+ fail: ProcedureFail<TCode>;
98
+ }
99
+ /**
100
+ * The failures a procedure declares: each code and the message a caller
101
+ * receives with it. A code is upper-case letters, digits and `_`, starting
102
+ * with a letter, and is never one of the reserved codes.
103
+ */
104
+ export type ProcedureFailures<TCode extends string> = Readonly<Record<TCode, string>> & Readonly<Partial<Record<ReservedFailureCode, never>>>;
105
+ /**
106
+ * A procedure as written: every member but `failures` is required.
107
+ *
108
+ * @typeParam C - the application's own context, usually the generated `Context`.
109
+ * @typeParam TIn - the Zod schema the input is parsed with.
110
+ * @typeParam TOut - the Zod schema the output is parsed with. A default
111
+ * `z.object` removes keys it does not declare before the result leaves the
112
+ * host, and a strict one refuses them. The shapes Zod provides that do
113
+ * neither (`z.looseObject`, `.catchall()`, `z.unknown`, `z.json()`,
114
+ * `z.looseRecord`, a `z.record` of `any`/`unknown`) are refused when a host
115
+ * is created. A hand-written schema that accepts as much is not recognised.
116
+ * @typeParam TCode - the failure codes the procedure declares.
117
+ */
118
+ export interface ProcedureDefinition<C extends ProcedureHostContext<Tx>, TIn extends z.ZodType, TOut extends z.ZodType, TCode extends string = never, Tx extends ProcedureHostTransactionContext = TransactionContextOf<C>> {
119
+ /** What the procedure does, for the people and agents that call it. */
120
+ description: string;
121
+ input: TIn;
122
+ output: TOut;
123
+ /**
124
+ * The named failures `run` may throw, as code and message. A caller
125
+ * receives one as `{ success: false, error: { code, message } }`, which the
126
+ * HTTP host answers with status 200. Declaring a reserved code is refused
127
+ * when the procedure is defined.
128
+ */
129
+ failures?: ProcedureFailures<TCode>;
130
+ /**
131
+ * Whether this session may call the procedure. Only `true` admits; a
132
+ * procedure open to anonymous callers says so by returning `true` for a
133
+ * `null` session.
134
+ */
135
+ access: (args: ProcedureAccessArgs<C, Tx>) => boolean | Promise<boolean>;
136
+ /** The surfaces that expose the procedure. A surface not named answers as an unknown name. */
137
+ expose: ProcedureExposure;
138
+ /**
139
+ * The OAuth scopes an MCP token must carry for the procedure to be listed
140
+ * or called as a tool. Omitted, the config's `mcp.scopes.write` is
141
+ * required, as for a custom tool that names none; `[]` requires none. Only
142
+ * the MCP host reads it: the HTTP host authenticates by cookie, which
143
+ * carries no scopes.
144
+ */
145
+ scopes?: readonly string[];
146
+ /**
147
+ * The action itself. It runs in no transaction unless it opens one with
148
+ * `context.transaction`. The value it returns is parsed by `output` after
149
+ * any such transaction has committed, so output that fails its schema is
150
+ * `INTERNAL` and undoes nothing.
151
+ */
152
+ run: (args: ProcedureRunArgs<C, z.output<TIn>, TCode, Tx>) => Promise<z.input<TOut>> | z.input<TOut>;
153
+ }
154
+ /**
155
+ * A defined procedure, as the builder returns it and a registry holds it.
156
+ * `access` and `run` take the application's own context and narrow it
157
+ * themselves before the definition's functions see it.
158
+ */
159
+ export interface Procedure<C extends ProcedureHostContext, TIn extends z.ZodType = z.ZodType, TOut extends z.ZodType = z.ZodType, TCode extends string = string> {
160
+ readonly description: string;
161
+ readonly input: TIn;
162
+ readonly output: TOut;
163
+ readonly expose: ProcedureExposure;
164
+ /** The OAuth scopes an MCP token must carry, when the procedure names them. */
165
+ readonly scopes?: readonly string[];
166
+ /** The declared failures, as code and message. */
167
+ readonly failures: Readonly<Partial<Record<TCode, string>>>;
168
+ access(args: {
169
+ context: C;
170
+ }): boolean | Promise<boolean>;
171
+ run(args: {
172
+ input: z.output<TIn>;
173
+ context: C;
174
+ }): Promise<z.input<TOut>> | z.input<TOut>;
175
+ }
176
+ /**
177
+ * An application's procedures, keyed by name. The registry is the
178
+ * application's own module, given to each host at runtime, and is never part
179
+ * of the config, so generation does not load it.
180
+ */
181
+ export type ProcedureRegistry<C extends ProcedureHostContext> = Readonly<Record<string, Procedure<C>>>;
182
+ /**
183
+ * The result a caller of `TProcedure` receives: its parsed output, or a
184
+ * failure whose code is a reserved code or one the procedure declares.
185
+ */
186
+ export type ProcedureResultOf<TProcedure> = TProcedure extends Procedure<infer _C, infer _TIn, infer TOut, infer TCode> ? ProcedureResult<z.output<TOut>, TCode> : never;
187
+ /** The builder the generated bundle binds to the application's own context. */
188
+ export type ProcedureBuilder<C extends ProcedureHostContext<Tx>, Tx extends ProcedureHostTransactionContext = TransactionContextOf<C>> = <TIn extends z.ZodType, TOut extends z.ZodType, TCode extends string = never>(definition: ProcedureDefinition<C, TIn, TOut, TCode, Tx>) => Procedure<C, TIn, TOut, TCode>;
189
+ /** Thrown when a procedure is defined without one of its required members. */
190
+ export declare class ProcedureDefinitionError extends Error {
191
+ constructor(message: string);
192
+ }
193
+ /** Whether `value` is a procedure the builder defined. */
194
+ export declare function isProcedure<C extends ProcedureHostContext>(value: unknown): value is Procedure<C>;
195
+ /**
196
+ * The procedure builder, bound to an application's context type. The
197
+ * generated bundle calls this once in `.opensaas/procedures.ts`; an
198
+ * application imports `procedure` from there rather than calling it itself.
199
+ *
200
+ * @throws {ProcedureDefinitionError} for a definition missing a required
201
+ * member, or declaring a malformed or reserved failure code.
202
+ */
203
+ export declare function createProcedureBuilder<C extends ProcedureHostContext<Tx>, Tx extends ProcedureHostTransactionContext = TransactionContextOf<C>>(): ProcedureBuilder<C, Tx>;
204
+ //# sourceMappingURL=procedure.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"procedure.d.ts","sourceRoot":"","sources":["../../src/procedures/procedure.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,CAAC,MAAM,KAAK,CAAA;AAI7B,OAAO,EAAY,KAAK,aAAa,EAAE,MAAM,eAAe,CAAA;AAC5D,OAAO,EAAyB,KAAK,eAAe,EAAE,KAAK,mBAAmB,EAAE,MAAM,aAAa,CAAA;AAEnG,oDAAoD;AACpD,MAAM,MAAM,gBAAgB,GAAG,KAAK,GAAG,KAAK,CAAA;AAE5C,2DAA2D;AAC3D,MAAM,MAAM,iBAAiB,GAAG,SAAS,CAAC,gBAAgB,EAAE,GAAG,gBAAgB,EAAE,CAAC,CAAA;AAElF;;;GAGG;AACH,MAAM,WAAW,+BAA+B;IAC9C,EAAE,EAAE,MAAM,CAAA;IACV,OAAO,EAAE,MAAM,GAAG,IAAI,CAAA;IACtB,OAAO,EAAE,MAAM,CAAA;IACf,OAAO,EAAE,MAAM,CAAA;IACf,YAAY,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IAC5C,WAAW,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAA;CACrD;AAED;;;;;;GAMG;AACH,MAAM,WAAW,oBAAoB,CACnC,EAAE,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AAS,oBAAoB,CAAC,EAAE,CAAC,EAClC,EAAE,SAAS,+BAA+B,GAAG,oBAAoB,CAAC,CAAC,CAAC,IAClE,CAAC,GAAG,SAAS,CAAC,CAAC,OAAO,EAAE,IAAI,SAAS,CAAC,CAAC,OAAO,EAAE,KAAK,SAAS,MAAM,GAAG,KAAK,EAC9E,UAAU,EAAE,mBAAmB,CAAC,CAAC,EAAE,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,KACrD,SAAS,CAAC,CAAC,EAAE,GAAG,EAAE,IAAI,EAAE,KAAK,CAAC,CAAA;AAEnC,8EAA8E;AAC9E,qBAAa,wBAAyB,SAAQ,KAAK;IACjD,YAAY,OAAO,EAAE,MAAM,EAG1B;CACF;AAYD,0DAA0D;AAC1D,wBAAgB,WAAW,CAAC,CAAC,SAAS,oBAAoB,EAAE,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,SAAS,CAAC,CAAC,CAAC,CAEjG;AAyGD;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CACpC,CAAC,SAAS,oBAAoB,CAAC,EAAE,CAAC,EAClC,EAAE,SAAS,+BAA+B,GAAG,oBAAoB,CAAC,CAAC,CAAC,KACjE,gBAAgB,CAAC,CAAC,EAAE,EAAE,CAAC,CAyB3B"}
@@ -0,0 +1,123 @@
1
+ import { processGlobal } from '../lib/process-global.js';
2
+ import { DECLARED_FAILURE_CODE_PATTERN } from './codes.js';
3
+ import { procedureContextOf } from './context-face.js';
4
+ import { failWith } from './failures.js';
5
+ import { isReservedFailureCode } from './result.js';
6
+ /** Thrown when a procedure is defined without one of its required members. */
7
+ export class ProcedureDefinitionError extends Error {
8
+ constructor(message) {
9
+ super(message);
10
+ this.name = 'ProcedureDefinitionError';
11
+ }
12
+ }
13
+ const SURFACES = new Set(['web', 'mcp']);
14
+ function isWeakSet(value) {
15
+ return value instanceof WeakSet;
16
+ }
17
+ function definedProcedures() {
18
+ return processGlobal('procedures', isWeakSet, () => new WeakSet());
19
+ }
20
+ /** Whether `value` is a procedure the builder defined. */
21
+ export function isProcedure(value) {
22
+ return typeof value === 'object' && value !== null && definedProcedures().has(value);
23
+ }
24
+ function isZodSchema(value) {
25
+ return (typeof value === 'object' &&
26
+ value !== null &&
27
+ 'safeParse' in value &&
28
+ typeof value.safeParse === 'function');
29
+ }
30
+ function assertExposure(expose) {
31
+ if (!Array.isArray(expose) || expose.length === 0) {
32
+ throw new ProcedureDefinitionError("A procedure's `expose` must name at least one surface: 'web', 'mcp', or both.");
33
+ }
34
+ const seen = new Set();
35
+ for (const surface of expose) {
36
+ if (typeof surface !== 'string' || !SURFACES.has(surface) || seen.has(surface)) {
37
+ throw new ProcedureDefinitionError(`A procedure's \`expose\` names each of 'web' and 'mcp' at most once; got ${JSON.stringify(expose)}.`);
38
+ }
39
+ seen.add(surface);
40
+ }
41
+ }
42
+ function assertFailures(description, failures) {
43
+ if (failures === undefined)
44
+ return;
45
+ if (typeof failures !== 'object' || failures === null || Array.isArray(failures)) {
46
+ throw new ProcedureDefinitionError("A procedure's `failures` is an object of failure code to message.");
47
+ }
48
+ for (const [code, message] of Object.entries(failures)) {
49
+ if (!DECLARED_FAILURE_CODE_PATTERN.test(code)) {
50
+ throw new ProcedureDefinitionError(`Failure code ${JSON.stringify(code)} must start with an upper-case letter and contain only upper-case letters, digits and "_" (at most 64 characters).`);
51
+ }
52
+ if (isReservedFailureCode(code)) {
53
+ throw new ProcedureDefinitionError(`Procedure ${JSON.stringify(description)} declares the failure code "${code}", which the stack reserves for its own failures. Declare a code of the procedure's own instead.`);
54
+ }
55
+ if (typeof message !== 'string' || message.trim() === '') {
56
+ throw new ProcedureDefinitionError(`Failure code "${code}" needs a non-empty message.`);
57
+ }
58
+ }
59
+ }
60
+ function assertScopes(scopes) {
61
+ if (scopes === undefined)
62
+ return;
63
+ if (!Array.isArray(scopes) ||
64
+ !scopes.every((scope) => typeof scope === 'string' && scope !== '' && !/\s/.test(scope))) {
65
+ throw new ProcedureDefinitionError("A procedure's `scopes` is an array of OAuth scope names, each a non-empty string without whitespace.");
66
+ }
67
+ }
68
+ function declaredFailures(failures) {
69
+ const none = {};
70
+ return Object.freeze({ ...(failures ?? none) });
71
+ }
72
+ function assertDefinition(definition) {
73
+ if (typeof definition.description !== 'string' || definition.description.trim() === '') {
74
+ throw new ProcedureDefinitionError('A procedure needs a non-empty `description`.');
75
+ }
76
+ if (!isZodSchema(definition.input)) {
77
+ throw new ProcedureDefinitionError('A procedure needs an `input` Zod schema.');
78
+ }
79
+ if (!isZodSchema(definition.output)) {
80
+ throw new ProcedureDefinitionError('A procedure needs an `output` Zod schema.');
81
+ }
82
+ assertFailures(definition.description, definition.failures);
83
+ if (typeof definition.access !== 'function') {
84
+ throw new ProcedureDefinitionError('A procedure needs an `access` rule. One open to anonymous callers returns `true` for a `null` session.');
85
+ }
86
+ if (typeof definition.run !== 'function') {
87
+ throw new ProcedureDefinitionError('A procedure needs a `run` function.');
88
+ }
89
+ assertExposure(definition.expose);
90
+ assertScopes(definition.scopes);
91
+ }
92
+ /**
93
+ * The procedure builder, bound to an application's context type. The
94
+ * generated bundle calls this once in `.opensaas/procedures.ts`; an
95
+ * application imports `procedure` from there rather than calling it itself.
96
+ *
97
+ * @throws {ProcedureDefinitionError} for a definition missing a required
98
+ * member, or declaring a malformed or reserved failure code.
99
+ */
100
+ export function createProcedureBuilder() {
101
+ return (definition) => {
102
+ assertDefinition(definition);
103
+ const { access, run } = definition;
104
+ const failures = declaredFailures(definition.failures);
105
+ const fail = failWith(failures);
106
+ const defined = Object.freeze({
107
+ description: definition.description,
108
+ input: definition.input,
109
+ output: definition.output,
110
+ expose: Object.freeze([...definition.expose]),
111
+ ...(definition.scopes === undefined ? {} : { scopes: Object.freeze([...definition.scopes]) }),
112
+ failures,
113
+ access: (args) => {
114
+ const context = procedureContextOf(args.context);
115
+ return access({ session: context.session, context });
116
+ },
117
+ run: (args) => run({ input: args.input, context: procedureContextOf(args.context), fail }),
118
+ });
119
+ definedProcedures().add(defined);
120
+ return defined;
121
+ };
122
+ }
123
+ //# sourceMappingURL=procedure.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"procedure.js","sourceRoot":"","sources":["../../src/procedures/procedure.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAA;AACxD,OAAO,EAAE,6BAA6B,EAAE,MAAM,YAAY,CAAA;AAC1D,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AACtD,OAAO,EAAE,QAAQ,EAAsB,MAAM,eAAe,CAAA;AAC5D,OAAO,EAAE,qBAAqB,EAAkD,MAAM,aAAa,CAAA;AAkPnG,8EAA8E;AAC9E,MAAM,OAAO,wBAAyB,SAAQ,KAAK;IACjD,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,IAAI,GAAG,0BAA0B,CAAA;IACxC,CAAC;CACF;AAED,MAAM,QAAQ,GAAwB,IAAI,GAAG,CAAmB,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAA;AAE/E,SAAS,SAAS,CAAC,KAAc;IAC/B,OAAO,KAAK,YAAY,OAAO,CAAA;AACjC,CAAC;AAED,SAAS,iBAAiB;IACxB,OAAO,aAAa,CAAC,YAAY,EAAE,SAAS,EAAE,GAAG,EAAE,CAAC,IAAI,OAAO,EAAU,CAAC,CAAA;AAC5E,CAAC;AAED,0DAA0D;AAC1D,MAAM,UAAU,WAAW,CAAiC,KAAc;IACxE,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,iBAAiB,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;AACtF,CAAC;AAED,SAAS,WAAW,CAAC,KAAc;IACjC,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,WAAW,IAAI,KAAK;QACpB,OAAO,KAAK,CAAC,SAAS,KAAK,UAAU,CACtC,CAAA;AACH,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AAC,MAAM,CAAC,CAAC,EAAE,CAAC;YAC7F,QAAQ;YACR,MAAM,EAAE,CAAC,IAAoB,EAAE,EAAE;gBAC/B,MAAM,OAAO,GAAG,kBAAkB,CAAQ,IAAI,CAAC,OAAO,CAAC,CAAA;gBACvD,OAAO,MAAM,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,OAAO,EAAE,CAAC,CAAA;YACtD,CAAC;YACD,GAAG,EAAE,CAAC,IAA0C,EAAE,EAAE,CAClD,GAAG,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,OAAO,EAAE,kBAAkB,CAAQ,IAAI,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,CAAC;SACrF,CAAC,CAAA;QACF,iBAAiB,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA;QAChC,OAAO,OAAO,CAAA;IAChB,CAAC,CAAA;AACH,CAAC"}
@@ -0,0 +1,42 @@
1
+ import type { ReservedFailureCode } from './codes.js';
2
+ export { FAILURE_STATUS, isReservedFailureCode, RESERVED_FAILURE_CODES, type ReservedFailureCode, } from './codes.js';
3
+ /**
4
+ * A failed call. `code` is a reserved code or one the procedure declares.
5
+ * `fieldErrors` is present when the failure names fields: on `INVALID_INPUT`
6
+ * and `VALIDATION`, and on a `CONFLICT` raised by a unique constraint the
7
+ * generator emitted, where it names the fields that collided. A `CONFLICT`'s
8
+ * keys are the list's field names, which need not be keys of the procedure's
9
+ * input.
10
+ *
11
+ * `RETRY` says the transaction that lost a race was rolled back. It does not
12
+ * say that work outside the transaction was undone, and nothing retries the
13
+ * call automatically: the caller decides whether running it again is safe.
14
+ *
15
+ * @typeParam TCode - the failure codes the procedure declares.
16
+ */
17
+ export interface ProcedureFailure<TCode extends string = never> {
18
+ code: ReservedFailureCode | TCode;
19
+ message: string;
20
+ fieldErrors?: Record<string, string>;
21
+ }
22
+ /** The result of calling a procedure, on every surface. */
23
+ export type ProcedureResult<TData, TCode extends string = never> = {
24
+ success: true;
25
+ data: TData;
26
+ } | {
27
+ success: false;
28
+ error: ProcedureFailure<TCode>;
29
+ };
30
+ export declare function failure(code: ReservedFailureCode, detail?: {
31
+ message?: string;
32
+ fieldErrors?: Record<string, string>;
33
+ }): {
34
+ success: false;
35
+ error: ProcedureFailure;
36
+ };
37
+ /** The failure a thrown error becomes, with `report` told of what an operator should see. See ADR-0086. */
38
+ export declare function failureFromError(error: unknown, report: (error: unknown) => void): {
39
+ success: false;
40
+ error: ProcedureFailure;
41
+ };
42
+ //# sourceMappingURL=result.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result.d.ts","sourceRoot":"","sources":["../../src/procedures/result.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAErD,OAAO,EACL,cAAc,EACd,qBAAqB,EACrB,sBAAsB,EACtB,KAAK,mBAAmB,GACzB,MAAM,YAAY,CAAA;AAEnB;;;;;;;;;;;;;GAaG;AACH,MAAM,WAAW,gBAAgB,CAAC,KAAK,SAAS,MAAM,GAAG,KAAK;IAC5D,IAAI,EAAE,mBAAmB,GAAG,KAAK,CAAA;IACjC,OAAO,EAAE,MAAM,CAAA;IACf,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CACrC;AAED,2DAA2D;AAC3D,MAAM,MAAM,eAAe,CAAC,KAAK,EAAE,KAAK,SAAS,MAAM,GAAG,KAAK,IAC7D;IAAE,OAAO,EAAE,IAAI,CAAC;IAAC,IAAI,EAAE,KAAK,CAAA;CAAE,GAAG;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,gBAAgB,CAAC,KAAK,CAAC,CAAA;CAAE,CAAA;AAYrF,wBAAgB,OAAO,CACrB,IAAI,EAAE,mBAAmB,EACzB,MAAM,GAAE;IAAE,OAAO,CAAC,EAAE,MAAM,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAAO,GACtE;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,gBAAgB,CAAA;CAAE,CAM7C;AAYD,2GAA2G;AAC3G,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,OAAO,EACd,MAAM,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,GAC/B;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,gBAAgB,CAAA;CAAE,CAgB7C"}
@@ -0,0 +1,51 @@
1
+ import { AccessScopeDepthExceededError, ResolveOutputCycleError } from '../access/errors.js';
2
+ import { MalformedRelationInputError, NestedRelationInputError, NonOwningRelationInputError, } from '../context/relationship-input.js';
3
+ import { ValidationError } from '../hooks/index.js';
4
+ import { isClientSafeError } from '../lib/client-safe-error.js';
5
+ import { DatabaseError, isSerializationFailure, isUniqueConstraintViolation, } from '../lib/database-errors.js';
6
+ export { FAILURE_STATUS, isReservedFailureCode, RESERVED_FAILURE_CODES, } from './codes.js';
7
+ const MESSAGES = {
8
+ UNAUTHENTICATED: 'Authentication required',
9
+ DENIED: 'Access denied',
10
+ INVALID_INPUT: 'Invalid input',
11
+ VALIDATION: 'Validation failed',
12
+ RETRY: 'The operation conflicted with a concurrent change and was rolled back',
13
+ CONFLICT: 'The operation conflicted with existing data',
14
+ INTERNAL: 'Internal error',
15
+ };
16
+ export function failure(code, detail = {}) {
17
+ const error = { code, message: detail.message ?? MESSAGES[code] };
18
+ if (detail.fieldErrors !== undefined && Object.keys(detail.fieldErrors).length > 0) {
19
+ error.fieldErrors = detail.fieldErrors;
20
+ }
21
+ return { success: false, error };
22
+ }
23
+ function isEngineRefusal(error) {
24
+ return (error instanceof NonOwningRelationInputError ||
25
+ error instanceof MalformedRelationInputError ||
26
+ error instanceof NestedRelationInputError ||
27
+ error instanceof AccessScopeDepthExceededError ||
28
+ error instanceof ResolveOutputCycleError);
29
+ }
30
+ /** The failure a thrown error becomes, with `report` told of what an operator should see. See ADR-0086. */
31
+ export function failureFromError(error, report) {
32
+ if (error instanceof ValidationError) {
33
+ return failure('VALIDATION', { message: error.message, fieldErrors: error.fieldErrors });
34
+ }
35
+ if (error instanceof DatabaseError) {
36
+ if (isSerializationFailure(error))
37
+ return failure('RETRY');
38
+ const { message, fieldErrors } = error;
39
+ if (!isUniqueConstraintViolation(error))
40
+ report(error);
41
+ return failure('CONFLICT', { message, fieldErrors });
42
+ }
43
+ if (isClientSafeError(error)) {
44
+ if (isEngineRefusal(error))
45
+ report(error);
46
+ return failure('INVALID_INPUT', { message: error.message });
47
+ }
48
+ report(error);
49
+ return failure('INTERNAL');
50
+ }
51
+ //# sourceMappingURL=result.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result.js","sourceRoot":"","sources":["../../src/procedures/result.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,6BAA6B,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAA;AAC5F,OAAO,EACL,2BAA2B,EAC3B,wBAAwB,EACxB,2BAA2B,GAC5B,MAAM,kCAAkC,CAAA;AACzC,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAA;AAC/D,OAAO,EACL,aAAa,EACb,sBAAsB,EACtB,2BAA2B,GAC5B,MAAM,2BAA2B,CAAA;AAGlC,OAAO,EACL,cAAc,EACd,qBAAqB,EACrB,sBAAsB,GAEvB,MAAM,YAAY,CAAA;AA0BnB,MAAM,QAAQ,GAAkD;IAC9D,eAAe,EAAE,yBAAyB;IAC1C,MAAM,EAAE,eAAe;IACvB,aAAa,EAAE,eAAe;IAC9B,UAAU,EAAE,mBAAmB;IAC/B,KAAK,EAAE,uEAAuE;IAC9E,QAAQ,EAAE,6CAA6C;IACvD,QAAQ,EAAE,gBAAgB;CAC3B,CAAA;AAED,MAAM,UAAU,OAAO,CACrB,IAAyB,EACzB,MAAM,GAA+D,EAAE;IAEvE,MAAM,KAAK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,eAAe,CAAC,KAAK,CAAC;YAAE,MAAM,CAAC,KAAK,CAAC,CAAA;QACzC,OAAO,OAAO,CAAC,eAAe,EAAE,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC,CAAA;IAC7D,CAAC;IACD,MAAM,CAAC,KAAK,CAAC,CAAA;IACb,OAAO,OAAO,CAAC,UAAU,CAAC,CAAA;AAC5B,CAAC"}
@@ -0,0 +1,36 @@
1
+ import type { Procedure, ProcedureHostContext } from './procedure.js';
2
+ import { type ProcedureResult } from './result.js';
3
+ /** The caller's raw input, or `ok: false` when it could not be decoded. */
4
+ export type RawInput = {
5
+ ok: true;
6
+ value: unknown;
7
+ } | {
8
+ ok: false;
9
+ };
10
+ /**
11
+ * Reads the caller's raw input. Called only once access has admitted the
12
+ * call, so a host that reads lazily consumes nothing for a refused one. A
13
+ * rejection is reported as `INTERNAL`.
14
+ */
15
+ export type InputReader = () => RawInput | Promise<RawInput>;
16
+ /** Everything one call needs once a host has resolved the name and the identity. */
17
+ export interface ProcedureCall<C extends ProcedureHostContext> {
18
+ name: string;
19
+ procedure: Procedure<C>;
20
+ /** The context the call runs in. Its `session` is the caller's identity. */
21
+ context: () => Promise<C>;
22
+ readInput: InputReader;
23
+ onUnexpectedError: UnexpectedErrorReporter;
24
+ /**
25
+ * Handed every error the pipeline catches before it becomes a failure. A
26
+ * host that lets an error propagate, as the in-process caller does with a
27
+ * framework's control flow, rethrows it from here.
28
+ */
29
+ propagate?: (error: unknown) => void;
30
+ }
31
+ export type UnexpectedErrorReporter = (error: unknown, procedureName: string) => void;
32
+ /** A reporter that cannot throw or leave a rejection unhandled: a failing one falls back to `console.error`. */
33
+ export declare function guardReporter(report: UnexpectedErrorReporter): UnexpectedErrorReporter;
34
+ /** The pipeline every host shares. See ADR-0086. */
35
+ export declare function runProcedure<C extends ProcedureHostContext>(call: ProcedureCall<C>): Promise<ProcedureResult<unknown, string>>;
36
+ //# sourceMappingURL=run.d.ts.map
@@ -0,0 +1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Failure } from './failures.js';
2
+ import { failure, failureFromError } from './result.js';
3
+ /** A reporter that cannot throw or leave a rejection unhandled: a failing one falls back to `console.error`. */
4
+ export function guardReporter(report) {
5
+ return (error, procedureName) => {
6
+ const fallback = (reporterError) => {
7
+ console.error(`onUnexpectedError threw while reporting procedure "${procedureName}":`, reporterError, error);
8
+ };
9
+ try {
10
+ const returned = report(error, procedureName);
11
+ if (isThenable(returned))
12
+ void Promise.resolve(returned).catch(fallback);
13
+ }
14
+ catch (reporterError) {
15
+ fallback(reporterError);
16
+ }
17
+ };
18
+ }
19
+ function isThenable(value) {
20
+ return (typeof value === 'object' &&
21
+ value !== null &&
22
+ 'then' in value &&
23
+ typeof value.then === 'function');
24
+ }
25
+ function fieldErrorsOf(error) {
26
+ const fieldErrors = new Map();
27
+ for (const issue of error.issues) {
28
+ const key = issue.path.map(String).join('.') || '(root)';
29
+ if (!fieldErrors.has(key))
30
+ fieldErrors.set(key, issue.message);
31
+ }
32
+ return Object.fromEntries(fieldErrors);
33
+ }
34
+ /** The pipeline every host shares. See ADR-0086. */
35
+ export async function runProcedure(call) {
36
+ const { name, procedure } = call;
37
+ const internal = (error) => call.onUnexpectedError(error, name);
38
+ try {
39
+ const context = await call.context();
40
+ const admitted = await procedure.access({ context });
41
+ if (admitted !== true) {
42
+ return failure(context.session === null ? 'UNAUTHENTICATED' : 'DENIED');
43
+ }
44
+ const raw = await call.readInput();
45
+ if (!raw.ok)
46
+ return failure('INVALID_INPUT');
47
+ const input = await procedure.input.safeParseAsync(raw.value);
48
+ if (!input.success) {
49
+ return failure('INVALID_INPUT', { fieldErrors: fieldErrorsOf(input.error) });
50
+ }
51
+ let returned;
52
+ try {
53
+ returned = await procedure.run({ input: input.data, context });
54
+ throwReturnedFailure(returned);
55
+ }
56
+ catch (error) {
57
+ const declared = declaredFailureOf(error, procedure.failures);
58
+ if (declared !== undefined)
59
+ return { success: false, error: declared };
60
+ throw error;
61
+ }
62
+ const output = await procedure.output.safeParseAsync(returned);
63
+ if (!output.success) {
64
+ internal(new Error(`Procedure "${name}" returned output that does not match its output schema`, {
65
+ cause: output.error,
66
+ }));
67
+ return failure('INTERNAL');
68
+ }
69
+ return { success: true, data: output.data };
70
+ }
71
+ catch (error) {
72
+ call.propagate?.(error);
73
+ return failureFromError(error, internal);
74
+ }
75
+ }
76
+ //# sourceMappingURL=run.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"run.js","sourceRoot":"","sources":["../../src/procedures/run.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAA;AAEvE,OAAO,EAAE,OAAO,EAAE,gBAAgB,EAAwB,MAAM,aAAa,CAAA;AA8B7E,gHAAgH;AAChH,MAAM,UAAU,aAAa,CAAC,MAA+B;IAC3D,OAAO,CAAC,KAAK,EAAE,aAAa,EAAE,EAAE;QAC9B,MAAM,QAAQ,GAAG,CAAC,aAAsB,EAAQ,EAAE;YAChD,OAAO,CAAC,KAAK,CACX,sDAAsD,aAAa,IAAI,EACvE,aAAa,EACb,KAAK,CACN,CAAA;QACH,CAAC,CAAA;QACD,IAAI,CAAC;YACH,MAAM,QAAQ,GAAY,MAAM,CAAC,KAAK,EAAE,aAAa,CAAC,CAAA;YACtD,IAAI,UAAU,CAAC,QAAQ,CAAC;gBAAE,KAAK,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAA;QAC1E,CAAC;QAAC,OAAO,aAAa,EAAE,CAAC;YACvB,QAAQ,CAAC,aAAa,CAAC,CAAA;QACzB,CAAC;IACH,CAAC,CAAA;AACH,CAAC;AAED,SAAS,UAAU,CAAC,KAAc;IAChC,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,MAAM,IAAI,KAAK;QACf,OAAO,KAAK,CAAC,IAAI,KAAK,UAAU,CACjC,CAAA;AACH,CAAC;AAED,SAAS,aAAa,CAAC,KA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