@xtandard/permissions 0.0.0-stage → 0.2.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.
Files changed (375) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +364 -2
  3. package/bin/xtandard-permissions.mjs +3 -0
  4. package/dist/better-auth-BEmX8qyz.mjs +94 -0
  5. package/dist/better-auth-BEmX8qyz.mjs.map +1 -0
  6. package/dist/better-auth-DQET1sXa.d.mts +75 -0
  7. package/dist/better-auth-MR7dBtpo.cjs +111 -0
  8. package/dist/better-auth-MR7dBtpo.cjs.map +1 -0
  9. package/dist/better-auth-vgRYgjYK.d.cts +75 -0
  10. package/dist/catalog-DA_d1SdC.d.cts +153 -0
  11. package/dist/catalog-DJcJ3ycq.d.mts +153 -0
  12. package/dist/catalog.cjs +198 -0
  13. package/dist/catalog.cjs.map +1 -0
  14. package/dist/catalog.d.cts +3 -0
  15. package/dist/catalog.d.mts +3 -0
  16. package/dist/catalog.mjs +141 -0
  17. package/dist/catalog.mjs.map +1 -0
  18. package/dist/chunk-DakpK96I.cjs +43 -0
  19. package/dist/cli.cjs +554 -0
  20. package/dist/cli.cjs.map +1 -0
  21. package/dist/cli.d.cts +28 -0
  22. package/dist/cli.d.mts +28 -0
  23. package/dist/cli.mjs +550 -0
  24. package/dist/cli.mjs.map +1 -0
  25. package/dist/contract-BAisPHBw.d.cts +78 -0
  26. package/dist/contract-BganM0fx.mjs +79 -0
  27. package/dist/contract-BganM0fx.mjs.map +1 -0
  28. package/dist/contract-Bpy8yPrS.cjs +70 -0
  29. package/dist/contract-Bpy8yPrS.cjs.map +1 -0
  30. package/dist/contract-Brnu9XXo.cjs +114 -0
  31. package/dist/contract-Brnu9XXo.cjs.map +1 -0
  32. package/dist/contract-CNgMXr2u.d.cts +46 -0
  33. package/dist/contract-CNgMXr2u.d.mts +46 -0
  34. package/dist/contract-CXPMGuHk.d.cts +391 -0
  35. package/dist/contract-D6j-yrBL.d.mts +78 -0
  36. package/dist/contract-DTdTXZtJ.mjs +47 -0
  37. package/dist/contract-DTdTXZtJ.mjs.map +1 -0
  38. package/dist/contract-DhS23Pcp.d.mts +391 -0
  39. package/dist/contract-RMkmERhT.d.cts +2 -0
  40. package/dist/contract-RMkmERhT.d.mts +2 -0
  41. package/dist/create-fetch-handler-B0twltQL.mjs +673 -0
  42. package/dist/create-fetch-handler-B0twltQL.mjs.map +1 -0
  43. package/dist/create-fetch-handler-B711wb64.d.mts +77 -0
  44. package/dist/create-fetch-handler-BHT4vSZs.cjs +690 -0
  45. package/dist/create-fetch-handler-BHT4vSZs.cjs.map +1 -0
  46. package/dist/create-fetch-handler-DxhvXuBA.d.cts +77 -0
  47. package/dist/elysia-shared-BFcWzfc9.cjs +101 -0
  48. package/dist/elysia-shared-BFcWzfc9.cjs.map +1 -0
  49. package/dist/elysia-shared-BwUYg0Ln.mjs +78 -0
  50. package/dist/elysia-shared-BwUYg0Ln.mjs.map +1 -0
  51. package/dist/elysia-shared-C2LnDOj3.d.cts +58 -0
  52. package/dist/elysia-shared-WuDr0jco.d.mts +58 -0
  53. package/dist/engine--C7zo-4M.cjs +667 -0
  54. package/dist/engine--C7zo-4M.cjs.map +1 -0
  55. package/dist/engine-BTgwbHeO.d.cts +336 -0
  56. package/dist/engine-C3xHfi3q.d.mts +336 -0
  57. package/dist/engine-CR75K253.mjs +654 -0
  58. package/dist/engine-CR75K253.mjs.map +1 -0
  59. package/dist/entry-auth-basic.cjs +40 -0
  60. package/dist/entry-auth-basic.cjs.map +1 -0
  61. package/dist/entry-auth-basic.d.cts +24 -0
  62. package/dist/entry-auth-basic.d.mts +24 -0
  63. package/dist/entry-auth-basic.mjs +26 -0
  64. package/dist/entry-auth-basic.mjs.map +1 -0
  65. package/dist/entry-auth-better-auth.cjs +15 -0
  66. package/dist/entry-auth-better-auth.d.cts +5 -0
  67. package/dist/entry-auth-better-auth.d.mts +5 -0
  68. package/dist/entry-auth-better-auth.mjs +4 -0
  69. package/dist/entry-auth-delegated.cjs +8 -0
  70. package/dist/entry-auth-delegated.d.cts +2 -0
  71. package/dist/entry-auth-delegated.d.mts +2 -0
  72. package/dist/entry-auth-delegated.mjs +2 -0
  73. package/dist/entry-auth-jwt.cjs +120 -0
  74. package/dist/entry-auth-jwt.cjs.map +1 -0
  75. package/dist/entry-auth-jwt.d.cts +71 -0
  76. package/dist/entry-auth-jwt.d.mts +71 -0
  77. package/dist/entry-auth-jwt.mjs +109 -0
  78. package/dist/entry-auth-jwt.mjs.map +1 -0
  79. package/dist/entry-auth-none.cjs +14 -0
  80. package/dist/entry-auth-none.d.cts +2 -0
  81. package/dist/entry-auth-none.d.mts +2 -0
  82. package/dist/entry-auth-none.mjs +2 -0
  83. package/dist/entry-auth-session.cjs +86 -0
  84. package/dist/entry-auth-session.cjs.map +1 -0
  85. package/dist/entry-auth-session.d.cts +25 -0
  86. package/dist/entry-auth-session.d.mts +25 -0
  87. package/dist/entry-auth-session.mjs +76 -0
  88. package/dist/entry-auth-session.mjs.map +1 -0
  89. package/dist/entry-authorization-delegated.cjs +31 -0
  90. package/dist/entry-authorization-delegated.cjs.map +1 -0
  91. package/dist/entry-authorization-delegated.d.cts +27 -0
  92. package/dist/entry-authorization-delegated.d.mts +27 -0
  93. package/dist/entry-authorization-delegated.mjs +30 -0
  94. package/dist/entry-authorization-delegated.mjs.map +1 -0
  95. package/dist/entry-authorization-none.cjs +29 -0
  96. package/dist/entry-authorization-none.cjs.map +1 -0
  97. package/dist/entry-authorization-none.d.cts +17 -0
  98. package/dist/entry-authorization-none.d.mts +17 -0
  99. package/dist/entry-authorization-none.mjs +28 -0
  100. package/dist/entry-authorization-none.mjs.map +1 -0
  101. package/dist/entry-authorization-permissions.cjs +4 -0
  102. package/dist/entry-authorization-permissions.d.cts +49 -0
  103. package/dist/entry-authorization-permissions.d.mts +49 -0
  104. package/dist/entry-authorization-permissions.mjs +2 -0
  105. package/dist/entry-authorization-roles.cjs +9 -0
  106. package/dist/entry-authorization-roles.d.cts +37 -0
  107. package/dist/entry-authorization-roles.d.mts +37 -0
  108. package/dist/entry-authorization-roles.mjs +3 -0
  109. package/dist/entry-better-auth-client.cjs +31 -0
  110. package/dist/entry-better-auth-client.cjs.map +1 -0
  111. package/dist/entry-better-auth-client.d.cts +63 -0
  112. package/dist/entry-better-auth-client.d.mts +63 -0
  113. package/dist/entry-better-auth-client.mjs +29 -0
  114. package/dist/entry-better-auth-client.mjs.map +1 -0
  115. package/dist/entry-better-auth.cjs +289 -0
  116. package/dist/entry-better-auth.cjs.map +1 -0
  117. package/dist/entry-better-auth.d.cts +3 -0
  118. package/dist/entry-better-auth.d.mts +3 -0
  119. package/dist/entry-better-auth.mjs +284 -0
  120. package/dist/entry-better-auth.mjs.map +1 -0
  121. package/dist/entry-bridges-audit.cjs +106 -0
  122. package/dist/entry-bridges-audit.cjs.map +1 -0
  123. package/dist/entry-bridges-audit.d.cts +58 -0
  124. package/dist/entry-bridges-audit.d.mts +58 -0
  125. package/dist/entry-bridges-audit.mjs +105 -0
  126. package/dist/entry-bridges-audit.mjs.map +1 -0
  127. package/dist/entry-bridges-webhooks.cjs +90 -0
  128. package/dist/entry-bridges-webhooks.cjs.map +1 -0
  129. package/dist/entry-bridges-webhooks.d.cts +46 -0
  130. package/dist/entry-bridges-webhooks.d.mts +46 -0
  131. package/dist/entry-bridges-webhooks.mjs +88 -0
  132. package/dist/entry-bridges-webhooks.mjs.map +1 -0
  133. package/dist/entry-bun.cjs +33 -0
  134. package/dist/entry-bun.cjs.map +1 -0
  135. package/dist/entry-bun.d.cts +31 -0
  136. package/dist/entry-bun.d.mts +31 -0
  137. package/dist/entry-bun.mjs +30 -0
  138. package/dist/entry-bun.mjs.map +1 -0
  139. package/dist/entry-drizzle-mysql.cjs +34 -0
  140. package/dist/entry-drizzle-mysql.cjs.map +1 -0
  141. package/dist/entry-drizzle-mysql.d.cts +20 -0
  142. package/dist/entry-drizzle-mysql.d.mts +20 -0
  143. package/dist/entry-drizzle-mysql.mjs +33 -0
  144. package/dist/entry-drizzle-mysql.mjs.map +1 -0
  145. package/dist/entry-drizzle-pg.cjs +44 -0
  146. package/dist/entry-drizzle-pg.cjs.map +1 -0
  147. package/dist/entry-drizzle-pg.d.cts +20 -0
  148. package/dist/entry-drizzle-pg.d.mts +20 -0
  149. package/dist/entry-drizzle-pg.mjs +43 -0
  150. package/dist/entry-drizzle-pg.mjs.map +1 -0
  151. package/dist/entry-drizzle-sqlite.cjs +34 -0
  152. package/dist/entry-drizzle-sqlite.cjs.map +1 -0
  153. package/dist/entry-drizzle-sqlite.d.cts +15 -0
  154. package/dist/entry-drizzle-sqlite.d.mts +15 -0
  155. package/dist/entry-drizzle-sqlite.mjs +33 -0
  156. package/dist/entry-drizzle-sqlite.mjs.map +1 -0
  157. package/dist/entry-elysia.cjs +117 -0
  158. package/dist/entry-elysia.cjs.map +1 -0
  159. package/dist/entry-elysia.d.cts +446 -0
  160. package/dist/entry-elysia.d.mts +446 -0
  161. package/dist/entry-elysia.mjs +113 -0
  162. package/dist/entry-elysia.mjs.map +1 -0
  163. package/dist/entry-elysia2.cjs +117 -0
  164. package/dist/entry-elysia2.cjs.map +1 -0
  165. package/dist/entry-elysia2.d.cts +571 -0
  166. package/dist/entry-elysia2.d.mts +571 -0
  167. package/dist/entry-elysia2.mjs +113 -0
  168. package/dist/entry-elysia2.mjs.map +1 -0
  169. package/dist/entry-express.cjs +101 -0
  170. package/dist/entry-express.cjs.map +1 -0
  171. package/dist/entry-express.d.cts +36 -0
  172. package/dist/entry-express.d.mts +36 -0
  173. package/dist/entry-express.mjs +97 -0
  174. package/dist/entry-express.mjs.map +1 -0
  175. package/dist/entry-hono.cjs +63 -0
  176. package/dist/entry-hono.cjs.map +1 -0
  177. package/dist/entry-hono.d.cts +40 -0
  178. package/dist/entry-hono.d.mts +40 -0
  179. package/dist/entry-hono.mjs +60 -0
  180. package/dist/entry-hono.mjs.map +1 -0
  181. package/dist/entry-hooks-log.cjs +39 -0
  182. package/dist/entry-hooks-log.cjs.map +1 -0
  183. package/dist/entry-hooks-log.d.cts +19 -0
  184. package/dist/entry-hooks-log.d.mts +19 -0
  185. package/dist/entry-hooks-log.mjs +36 -0
  186. package/dist/entry-hooks-log.mjs.map +1 -0
  187. package/dist/entry-next.cjs +67 -0
  188. package/dist/entry-next.cjs.map +1 -0
  189. package/dist/entry-next.d.cts +60 -0
  190. package/dist/entry-next.d.mts +60 -0
  191. package/dist/entry-next.mjs +57 -0
  192. package/dist/entry-next.mjs.map +1 -0
  193. package/dist/entry-react.cjs +401 -0
  194. package/dist/entry-react.cjs.map +1 -0
  195. package/dist/entry-react.d.cts +206 -0
  196. package/dist/entry-react.d.mts +206 -0
  197. package/dist/entry-react.mjs +388 -0
  198. package/dist/entry-react.mjs.map +1 -0
  199. package/dist/entry-storage-cloudflare-kv.cjs +8 -0
  200. package/dist/entry-storage-cloudflare-kv.d.cts +2 -0
  201. package/dist/entry-storage-cloudflare-kv.d.mts +2 -0
  202. package/dist/entry-storage-cloudflare-kv.mjs +2 -0
  203. package/dist/entry-storage-drizzle.cjs +71 -0
  204. package/dist/entry-storage-drizzle.cjs.map +1 -0
  205. package/dist/entry-storage-drizzle.d.cts +27 -0
  206. package/dist/entry-storage-drizzle.d.mts +27 -0
  207. package/dist/entry-storage-drizzle.mjs +68 -0
  208. package/dist/entry-storage-drizzle.mjs.map +1 -0
  209. package/dist/entry-storage-file.cjs +54 -0
  210. package/dist/entry-storage-file.cjs.map +1 -0
  211. package/dist/entry-storage-file.d.cts +24 -0
  212. package/dist/entry-storage-file.d.mts +24 -0
  213. package/dist/entry-storage-file.mjs +46 -0
  214. package/dist/entry-storage-file.mjs.map +1 -0
  215. package/dist/entry-storage-libsql.cjs +64 -0
  216. package/dist/entry-storage-libsql.cjs.map +1 -0
  217. package/dist/entry-storage-libsql.d.cts +28 -0
  218. package/dist/entry-storage-libsql.d.mts +28 -0
  219. package/dist/entry-storage-libsql.mjs +61 -0
  220. package/dist/entry-storage-libsql.mjs.map +1 -0
  221. package/dist/entry-storage-memory.cjs +3 -0
  222. package/dist/entry-storage-memory.d.cts +25 -0
  223. package/dist/entry-storage-memory.d.mts +25 -0
  224. package/dist/entry-storage-memory.mjs +2 -0
  225. package/dist/entry-storage-mongodb.cjs +61 -0
  226. package/dist/entry-storage-mongodb.cjs.map +1 -0
  227. package/dist/entry-storage-mongodb.d.cts +25 -0
  228. package/dist/entry-storage-mongodb.d.mts +25 -0
  229. package/dist/entry-storage-mongodb.mjs +58 -0
  230. package/dist/entry-storage-mongodb.mjs.map +1 -0
  231. package/dist/entry-storage-postgres.cjs +62 -0
  232. package/dist/entry-storage-postgres.cjs.map +1 -0
  233. package/dist/entry-storage-postgres.d.cts +29 -0
  234. package/dist/entry-storage-postgres.d.mts +29 -0
  235. package/dist/entry-storage-postgres.mjs +58 -0
  236. package/dist/entry-storage-postgres.mjs.map +1 -0
  237. package/dist/entry-storage-redis.cjs +76 -0
  238. package/dist/entry-storage-redis.cjs.map +1 -0
  239. package/dist/entry-storage-redis.d.cts +29 -0
  240. package/dist/entry-storage-redis.d.mts +29 -0
  241. package/dist/entry-storage-redis.mjs +72 -0
  242. package/dist/entry-storage-redis.mjs.map +1 -0
  243. package/dist/entry-storage-sqlite.cjs +46 -0
  244. package/dist/entry-storage-sqlite.cjs.map +1 -0
  245. package/dist/entry-storage-sqlite.d.cts +22 -0
  246. package/dist/entry-storage-sqlite.d.mts +22 -0
  247. package/dist/entry-storage-sqlite.mjs +43 -0
  248. package/dist/entry-storage-sqlite.mjs.map +1 -0
  249. package/dist/entry-storage-unstorage.cjs +8 -0
  250. package/dist/entry-storage-unstorage.d.cts +2 -0
  251. package/dist/entry-storage-unstorage.d.mts +2 -0
  252. package/dist/entry-storage-unstorage.mjs +2 -0
  253. package/dist/error-codes-9MRMSSMQ.cjs +49 -0
  254. package/dist/error-codes-9MRMSSMQ.cjs.map +1 -0
  255. package/dist/error-codes-CCKuDDu7.mjs +44 -0
  256. package/dist/error-codes-CCKuDDu7.mjs.map +1 -0
  257. package/dist/errors-CG_OLEEv.mjs +64 -0
  258. package/dist/errors-CG_OLEEv.mjs.map +1 -0
  259. package/dist/errors-CjpymzFj.cjs +105 -0
  260. package/dist/errors-CjpymzFj.cjs.map +1 -0
  261. package/dist/events-AE4Ytqmz.d.cts +6 -0
  262. package/dist/events-AE4Ytqmz.d.mts +6 -0
  263. package/dist/events-DYZShrYB.mjs +15 -0
  264. package/dist/events-DYZShrYB.mjs.map +1 -0
  265. package/dist/events-FJ2K0wly.cjs +26 -0
  266. package/dist/events-FJ2K0wly.cjs.map +1 -0
  267. package/dist/guard-Bj8zk23F.mjs +141 -0
  268. package/dist/guard-Bj8zk23F.mjs.map +1 -0
  269. package/dist/guard-DvaoUgjk.cjs +188 -0
  270. package/dist/guard-DvaoUgjk.cjs.map +1 -0
  271. package/dist/guard-VEK0W8ug.d.cts +62 -0
  272. package/dist/guard-k7A71SWC.d.mts +62 -0
  273. package/dist/index-BVVgDSdq.d.cts +1 -0
  274. package/dist/index-BVVgDSdq.d.mts +1 -0
  275. package/dist/index.cjs +243 -0
  276. package/dist/index.cjs.map +1 -0
  277. package/dist/index.d.cts +213 -0
  278. package/dist/index.d.mts +213 -0
  279. package/dist/index.mjs +60 -0
  280. package/dist/index.mjs.map +1 -0
  281. package/dist/kit-51JtMxsn.d.mts +62 -0
  282. package/dist/kit-BM3uosfV.mjs +80 -0
  283. package/dist/kit-BM3uosfV.mjs.map +1 -0
  284. package/dist/kit-BrqnCcDw.cjs +97 -0
  285. package/dist/kit-BrqnCcDw.cjs.map +1 -0
  286. package/dist/kit-DLF7DZrp.d.cts +62 -0
  287. package/dist/memory-BbBbbY8-.mjs +32 -0
  288. package/dist/memory-BbBbbY8-.mjs.map +1 -0
  289. package/dist/memory-knijqKw_.cjs +39 -0
  290. package/dist/memory-knijqKw_.cjs.map +1 -0
  291. package/dist/permissions-CeV6M3v7.mjs +62 -0
  292. package/dist/permissions-CeV6M3v7.mjs.map +1 -0
  293. package/dist/permissions-DDEkHNvN.cjs +73 -0
  294. package/dist/permissions-DDEkHNvN.cjs.map +1 -0
  295. package/dist/policy-BQmlo2lu.mjs +3 -0
  296. package/dist/policy-CGA6QD0a.d.mts +4 -0
  297. package/dist/policy-DFq_SYR3.d.cts +4 -0
  298. package/dist/react-embed.js +18735 -0
  299. package/dist/react.css +1 -0
  300. package/dist/relationships-BLYEJ9tc.cjs +907 -0
  301. package/dist/relationships-BLYEJ9tc.cjs.map +1 -0
  302. package/dist/relationships-BfLonEMI.mjs +756 -0
  303. package/dist/relationships-BfLonEMI.mjs.map +1 -0
  304. package/dist/roles-DBk4i8b5.mjs +69 -0
  305. package/dist/roles-DBk4i8b5.mjs.map +1 -0
  306. package/dist/roles-a8B5uZ35.cjs +104 -0
  307. package/dist/roles-a8B5uZ35.cjs.map +1 -0
  308. package/dist/rolldown-runtime-D7D4PA-g.mjs +13 -0
  309. package/dist/routes-B2LrYpT0.cjs +1055 -0
  310. package/dist/routes-B2LrYpT0.cjs.map +1 -0
  311. package/dist/routes-Cx-kl4PY.mjs +1007 -0
  312. package/dist/routes-Cx-kl4PY.mjs.map +1 -0
  313. package/dist/server-Bn643UOy.d.mts +886 -0
  314. package/dist/server-YdH25OEA.d.cts +886 -0
  315. package/dist/table-DA98mVVT.d.cts +2 -0
  316. package/dist/table-DA98mVVT.d.mts +2 -0
  317. package/dist/testing.cjs +59 -0
  318. package/dist/testing.cjs.map +1 -0
  319. package/dist/testing.d.cts +37 -0
  320. package/dist/testing.d.mts +37 -0
  321. package/dist/testing.mjs +57 -0
  322. package/dist/testing.mjs.map +1 -0
  323. package/dist/types-elysia2/adapters/elysia-shared.d.ts +71 -0
  324. package/dist/types-elysia2/adapters/elysia2.d.ts +432 -0
  325. package/dist/types-elysia2/adapters/guard.d.ts +140 -0
  326. package/dist/types-elysia2/auth/contract.d.ts +9 -0
  327. package/dist/types-elysia2/auth-provider/contract.d.ts +85 -0
  328. package/dist/types-elysia2/auth-provider/kit.d.ts +62 -0
  329. package/dist/types-elysia2/authorization/contract.d.ts +53 -0
  330. package/dist/types-elysia2/authorization/permissions.d.ts +54 -0
  331. package/dist/types-elysia2/catalog.d.ts +197 -0
  332. package/dist/types-elysia2/engine.d.ts +233 -0
  333. package/dist/types-elysia2/entry-elysia2.d.ts +9 -0
  334. package/dist/types-elysia2/errors.d.ts +55 -0
  335. package/dist/types-elysia2/hooks/contract.d.ts +100 -0
  336. package/dist/types-elysia2/keys.d.ts +78 -0
  337. package/dist/types-elysia2/policy.d.ts +13 -0
  338. package/dist/types-elysia2/relationships.d.ts +162 -0
  339. package/dist/types-elysia2/schema.d.ts +315 -0
  340. package/dist/types-elysia2/server/cors.d.ts +8 -0
  341. package/dist/types-elysia2/server/create-fetch-handler.d.ts +87 -0
  342. package/dist/types-elysia2/server/openapi.d.ts +29 -0
  343. package/dist/types-elysia2/server/routes.d.ts +143 -0
  344. package/dist/types-elysia2/server/validation.d.ts +205 -0
  345. package/dist/types-elysia2/storage/contract.d.ts +18 -0
  346. package/dist/types-react/catalog.d.ts +197 -0
  347. package/dist/types-react/engine.d.ts +233 -0
  348. package/dist/types-react/entry-react-embed.d.ts +20 -0
  349. package/dist/types-react/errors.d.ts +55 -0
  350. package/dist/types-react/hooks/contract.d.ts +100 -0
  351. package/dist/types-react/keys.d.ts +78 -0
  352. package/dist/types-react/policy.d.ts +13 -0
  353. package/dist/types-react/react-embed.d.ts +37 -0
  354. package/dist/types-react/relationships.d.ts +162 -0
  355. package/dist/types-react/schema.d.ts +315 -0
  356. package/dist/types-react/storage/contract.d.ts +18 -0
  357. package/dist/types-react/ui/App.d.ts +28 -0
  358. package/dist/types-react/ui/api.d.ts +73 -0
  359. package/dist/types-react/ui/components/perm-bits.d.ts +68 -0
  360. package/dist/types-react/ui/lib/queries.d.ts +46 -0
  361. package/dist/types-react/ui/theme.d.ts +10 -0
  362. package/dist/types-react/ui/types.d.ts +118 -0
  363. package/dist/types-react/ui/views/CatalogView.d.ts +3 -0
  364. package/dist/types-react/ui/views/HistoryView.d.ts +6 -0
  365. package/dist/types-react/ui/views/PlaygroundView.d.ts +14 -0
  366. package/dist/types-react/ui/views/RelationshipsView.d.ts +6 -0
  367. package/dist/types-react/ui/views/SubjectDetailView.d.ts +7 -0
  368. package/dist/types-react/ui/views/SubjectsView.d.ts +6 -0
  369. package/dist/ui/apple-touch-icon.png +0 -0
  370. package/dist/ui/assets/index-DCPZRo-Z.css +1 -0
  371. package/dist/ui/assets/index-DdvxpphI.js +104 -0
  372. package/dist/ui/favicon.ico +0 -0
  373. package/dist/ui/favicon.svg +15 -0
  374. package/dist/ui/index.html +20 -0
  375. package/package.json +587 -4
@@ -0,0 +1,756 @@
1
+ import { _ as isAllowedTuple, h as hasRelation, o as createMemoryTuples, r as RelationSchemaError, s as createRelationChecker, v as relationEdges } from "./policy-BQmlo2lu.mjs";
2
+ import * as v from "valibot";
3
+ import { parseInput } from "@xtandard/kit/util";
4
+ import { HookDeniedError, createHookErrorReporter, normalizeHooks, runAfter, runBefore } from "@xtandard/kit/hooks";
5
+ import { isCompareAndSwap, isTransactional, isWatchable } from "@xtandard/kit/storage";
6
+ //#region src/schema.ts
7
+ const nonEmpty = v.pipe(v.string(), v.minLength(1, "must not be empty"));
8
+ /** Who performs a write: an actor id, an {@link Actor}, or `null` (system). */
9
+ const actorSchema = v.nullable(v.union([nonEmpty, v.object({
10
+ id: nonEmpty,
11
+ type: v.optional(v.string()),
12
+ name: v.optional(v.string()),
13
+ email: v.optional(v.string())
14
+ })]));
15
+ const reasonSchema = v.pipe(v.string(), v.maxLength(1e3, "must be at most 1000 characters"));
16
+ const instantSchema = v.union([
17
+ v.pipe(v.string(), v.check((s) => !Number.isNaN(Date.parse(s)), "must be an ISO-8601 date")),
18
+ v.pipe(v.date(), v.check((date) => !Number.isNaN(date.getTime()), "must be a valid date")),
19
+ v.pipe(v.number(), v.finite())
20
+ ]);
21
+ /** Input of `grant()`. */
22
+ const grantInputSchema = v.object({
23
+ subject: nonEmpty,
24
+ permission: nonEmpty,
25
+ actor: v.optional(actorSchema),
26
+ reason: v.optional(reasonSchema),
27
+ /** ISO string, `Date` or unix ms; `null` = never. */
28
+ expiresAt: v.optional(v.nullable(instantSchema))
29
+ });
30
+ /** Input of `revoke()`. */
31
+ const revokeInputSchema = v.object({
32
+ subject: nonEmpty,
33
+ permission: nonEmpty,
34
+ actor: v.optional(actorSchema),
35
+ reason: v.optional(reasonSchema)
36
+ });
37
+ /** Input of `setRole()`; `role: null` clears the stored assignment. */
38
+ const setRoleInputSchema = v.object({
39
+ subject: nonEmpty,
40
+ role: v.nullable(nonEmpty),
41
+ actor: v.optional(actorSchema),
42
+ reason: v.optional(reasonSchema)
43
+ });
44
+ /** Input of `upsertSubject()`. `null` clears a field. */
45
+ const upsertSubjectInputSchema = v.object({
46
+ id: nonEmpty,
47
+ name: v.optional(v.nullable(v.pipe(v.string(), v.maxLength(500)))),
48
+ email: v.optional(v.nullable(v.pipe(v.string(), v.maxLength(500))))
49
+ });
50
+ /** Query of `list()`. */
51
+ const listQuerySchema = v.object({
52
+ subject: v.optional(nonEmpty),
53
+ permission: v.optional(nonEmpty),
54
+ includeExpired: v.optional(v.boolean())
55
+ });
56
+ const refPart = "[^:#]+";
57
+ /** `type:id` (the id may hold `:`, `/`, `%`, non-ASCII, but not `#`, which starts a userset). */
58
+ const objectRefSchema = v.pipe(v.string(), v.maxLength(512, "must be at most 512 characters"), v.regex(new RegExp(`^${refPart}:[^#]+$`), "must be \"type:id\" (no \"#\" in the id)"));
59
+ /** `type:id`, `type:id#relation` or `type:*`. */
60
+ const subjectRefSchema = v.pipe(v.string(), v.maxLength(512, "must be at most 512 characters"), v.regex(new RegExp(`^${refPart}:[^#]+(#${refPart})?$`), "must be \"type:id\", \"type:id#relation\" or \"type:*\""));
61
+ /** Input of `writeRelationship()` / `deleteRelationship()`. */
62
+ const relationshipInputSchema = v.object({
63
+ object: objectRefSchema,
64
+ relation: nonEmpty,
65
+ subject: subjectRefSchema,
66
+ actor: v.optional(actorSchema),
67
+ reason: v.optional(reasonSchema)
68
+ });
69
+ /** Query of `readRelationships()`. */
70
+ const relationshipQuerySchema = v.object({
71
+ /** `type:id`, or just `type` for every object of a type. */
72
+ object: v.optional(v.pipe(v.string(), v.minLength(1, "must not be empty"))),
73
+ relation: v.optional(nonEmpty),
74
+ subject: v.optional(subjectRefSchema),
75
+ limit: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(1e3))),
76
+ cursor: v.optional(v.string())
77
+ });
78
+ /** Input of `checkRelation()`. */
79
+ const relationCheckSchema = v.object({
80
+ subject: subjectRefSchema,
81
+ relation: nonEmpty,
82
+ object: objectRefSchema
83
+ });
84
+ /** Query of `listObjects()`. */
85
+ const listObjectsQuerySchema = v.object({
86
+ subject: subjectRefSchema,
87
+ relation: nonEmpty,
88
+ type: nonEmpty
89
+ });
90
+ /** Query of `listRelationSubjects()`. */
91
+ const listRelationSubjectsQuerySchema = v.object({
92
+ object: objectRefSchema,
93
+ relation: nonEmpty,
94
+ /** Only subjects of this type. */
95
+ type: v.optional(nonEmpty)
96
+ });
97
+ /** Query of `history()`. */
98
+ const historyQuerySchema = v.object({
99
+ subject: v.optional(nonEmpty),
100
+ limit: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(1e3))),
101
+ cursor: v.optional(v.string())
102
+ });
103
+ //#endregion
104
+ //#region src/hooks/contract.ts
105
+ /**
106
+ * Control-plane hooks fired around grant, revoke, role and relationship writes (mirrors the
107
+ * family's hooks contract, `@xtandard/kit/hooks`).
108
+ *
109
+ * - `before` runs sequentially before the write commits. **Throw to deny**
110
+ * (e.g. {@link HookDeniedError}); the error propagates to the caller.
111
+ * - `after` runs once committed, for side effects (audit, webhooks, cache
112
+ * busting). Errors are isolated and reported via `onHookError`.
113
+ *
114
+ * A re-grant of an existing grant, a revoke of nothing, a role set to the
115
+ * role already stored, a re-write of a stored relationship and a delete of a
116
+ * missing one are no-ops: they fire neither `before` nor `after`.
117
+ * Checks never go through hooks.
118
+ *
119
+ * @module
120
+ */
121
+ /** Default `after`-hook error reporter: warn with the `[@xtandard/permissions]` prefix, never throw. */
122
+ const defaultHookErrorReporter = createHookErrorReporter("@xtandard/permissions");
123
+ //#endregion
124
+ //#region src/keys.ts
125
+ /**
126
+ * Key layout (ADR 0003). Pure string helpers; every id segment is
127
+ * `encodeURIComponent`-ed, so any subject id or permission name is safe in a
128
+ * key and can't contain the `/` separator.
129
+ *
130
+ * ```txt
131
+ * perm/grants/{subject}/{permission} -> Grant one key per pair: unique on every driver
132
+ * perm/subjects/{subject} -> SubjectRecord optional role assignment + display fields
133
+ * perm/history/{subject}/{at}-{id} -> HistoryEntry append-only; sortable by the last segment
134
+ *
135
+ * perm/rel/{objectType}/{objectId}/{relation}/{subject} -> Relationship
136
+ * perm/relrev/{subjectType}/{subjectId}/{subjectRel}/{objectType}/{objectId}/{relation}
137
+ * -> RelationshipRef
138
+ * ```
139
+ *
140
+ * Relationships (ADR 0013): one forward key per tuple (unique on every driver, so writes are
141
+ * idempotent), read by `(object, relation)` with one prefix scan; the reverse key serves
142
+ * `listObjects` (every tuple naming a subject, plain or as a userset). `{subject}` is the whole
143
+ * subject reference (`user:ana`, `group:eng#member`, `user:*`) as one segment; `{subjectRel}` is
144
+ * `_` for a plain subject or a wildcard and `#{relation}` (encoded `%23…`) for a userset, so it
145
+ * can't collide with a relation named `_`.
146
+ *
147
+ * @module
148
+ */
149
+ /** Root namespace of every key. */
150
+ const ROOT = "perm";
151
+ const e = encodeURIComponent;
152
+ const d = decodeURIComponent;
153
+ /** Prefix of every grant. */
154
+ const grantsPrefix = () => `${ROOT}/grants/`;
155
+ /** Prefix of one subject's grants. */
156
+ const subjectGrantsPrefix = (subject) => `${grantsPrefix()}${e(subject)}/`;
157
+ /** One grant. */
158
+ const grantKey = (subject, permission) => `${subjectGrantsPrefix(subject)}${e(permission)}`;
159
+ /** Prefix of every subject record. */
160
+ const subjectsPrefix = () => `${ROOT}/subjects/`;
161
+ /** One subject record. */
162
+ const subjectKey = (subject) => `${subjectsPrefix()}${e(subject)}`;
163
+ /** Prefix of the whole history log. */
164
+ const historyPrefix = () => `${ROOT}/history/`;
165
+ /** Prefix of one subject's history. */
166
+ const subjectHistoryPrefix = (subject) => `${historyPrefix()}${e(subject)}/`;
167
+ /** One history entry: the last segment (`{at}-{id}`) sorts by time. */
168
+ const historyKey = (subject, at, id) => `${subjectHistoryPrefix(subject)}${e(`${at}-${id}`)}`;
169
+ /** Split `type:id` (first colon). */
170
+ function splitRef(ref) {
171
+ const colon = ref.indexOf(":");
172
+ return [ref.slice(0, colon), ref.slice(colon + 1)];
173
+ }
174
+ /** Split a subject `type:id` / `type:id#relation` into object ref and relation (first `#`). */
175
+ function splitSubject(subject) {
176
+ const hash = subject.indexOf("#");
177
+ return hash === -1 ? [subject, void 0] : [subject.slice(0, hash), subject.slice(hash + 1)];
178
+ }
179
+ /** The `{subjectRel}` segment of a reverse key. */
180
+ const subjectRelSegment = (relation) => relation === void 0 ? "_" : e(`#${relation}`);
181
+ /** Prefix of every relationship (forward keys). */
182
+ const relationshipsPrefix = () => `${ROOT}/rel/`;
183
+ /** Prefix of one object type's relationships. */
184
+ const relationshipTypePrefix = (type) => `${relationshipsPrefix()}${e(type)}/`;
185
+ /** Prefix of one object's relationships (`doc:1`). */
186
+ const objectRelationshipsPrefix = (object) => {
187
+ const [type, id] = splitRef(object);
188
+ return `${relationshipTypePrefix(type)}${e(id)}/`;
189
+ };
190
+ /** Prefix of the subjects stored on `(object, relation)`: what a tuple reader scans. */
191
+ const relationSubjectsPrefix = (object, relation) => `${objectRelationshipsPrefix(object)}${e(relation)}/`;
192
+ /** One relationship's forward key. */
193
+ const relationshipKey = (object, relation, subject) => `${relationSubjectsPrefix(object, relation)}${e(subject)}`;
194
+ /** Prefix of every reverse key. */
195
+ const reverseRelationshipsPrefix = () => `${ROOT}/relrev/`;
196
+ /** Prefix of the reverse keys of one subject object (`user:ana`), plain and as any userset. */
197
+ const subjectObjectReversePrefix = (object) => {
198
+ const [type, id] = splitRef(object);
199
+ return `${reverseRelationshipsPrefix()}${e(type)}/${e(id)}/`;
200
+ };
201
+ /** Prefix of the reverse keys of exactly `subject` (`user:ana`, `user:*`, `group:eng#member`). */
202
+ const subjectReversePrefix = (subject) => {
203
+ const [object, relation] = splitSubject(subject);
204
+ return `${subjectObjectReversePrefix(object)}${subjectRelSegment(relation)}/`;
205
+ };
206
+ /** One relationship's reverse key. */
207
+ const reverseRelationshipKey = (object, relation, subject) => {
208
+ const [type, id] = splitRef(object);
209
+ return `${subjectReversePrefix(subject)}${e(type)}/${e(id)}/${e(relation)}`;
210
+ };
211
+ /** A forward key back to its tuple; `null` when it is not one. */
212
+ function parseRelationshipKey(key) {
213
+ const parts = key.split("/");
214
+ if (parts.length !== 6 || parts[0] !== "perm" || parts[1] !== "rel") return null;
215
+ try {
216
+ const [type, id, relation, subject] = parts.slice(2).map(d);
217
+ return {
218
+ object: `${type}:${id}`,
219
+ relation,
220
+ subject
221
+ };
222
+ } catch {
223
+ return null;
224
+ }
225
+ }
226
+ /** A reverse key back to its tuple; `null` when it is not one. */
227
+ function parseReverseRelationshipKey(key) {
228
+ const parts = key.split("/");
229
+ if (parts.length !== 8 || parts[0] !== "perm" || parts[1] !== "relrev") return null;
230
+ try {
231
+ const [subjectType, subjectId, subjectRel, type, id, relation] = parts.slice(2).map(d);
232
+ if (subjectRel !== "_" && !subjectRel.startsWith("#")) return null;
233
+ const subject = `${subjectType}:${subjectId}${subjectRel === "_" ? "" : subjectRel}`;
234
+ return {
235
+ object: `${type}:${id}`,
236
+ relation,
237
+ subject
238
+ };
239
+ } catch {
240
+ return null;
241
+ }
242
+ }
243
+ /** The decoded segments after the root's area (`grants`, `subjects`, `history`). */
244
+ function parseKey(key) {
245
+ const parts = key.split("/");
246
+ if (parts[0] !== "perm" || parts.length < 3) return null;
247
+ const area = parts[1];
248
+ if (area !== "grants" && area !== "subjects" && area !== "history") return null;
249
+ try {
250
+ const subject = d(parts[2]);
251
+ const rest = parts.length > 3 ? d(parts.slice(3).join("/")) : void 0;
252
+ return rest === void 0 ? {
253
+ area,
254
+ subject
255
+ } : {
256
+ area,
257
+ subject,
258
+ rest
259
+ };
260
+ } catch {
261
+ return null;
262
+ }
263
+ }
264
+ //#endregion
265
+ //#region src/relationships.ts
266
+ /**
267
+ * Relationships (ReBAC, ADR 0013): the tuple store, the tuple reader the lib's checker plugs into,
268
+ * `checkRelation`, `listObjects`, `listRelationSubjects` and the synchronous preload for rules.
269
+ * **The lib evaluates** (`@xtandard/lib/policy/rebac`: schema, `check`, `explain`, reverse edges);
270
+ * this module stores, indexes and lists. The engine (`createPermissions`) mixes these methods in.
271
+ *
272
+ * Storage: one forward key per tuple, `perm/rel/{objectType}/{objectId}/{relation}/{subject}`
273
+ * (unique on every driver, so writes are idempotent), plus a reverse key per tuple under
274
+ * `perm/relrev/{subjectType}/{subjectId}/{_ | #relation}/…` for listing. A write sets the forward
275
+ * key, then the reverse key; a delete removes the reverse key, then the forward key. So a crash
276
+ * between the two leaves a forward key without its reverse (listing misses the tuple; checks still
277
+ * see it), never the other way round; `repairRelationships()` restores the invariant.
278
+ *
279
+ * @module
280
+ */
281
+ const DEFAULT_PAGE = 100;
282
+ const DEFAULT_MAX_OBJECTS = 1e3;
283
+ const parseRef = (ref) => {
284
+ const hash = ref.indexOf("#");
285
+ const object = hash === -1 ? ref : ref.slice(0, hash);
286
+ const colon = object.indexOf(":");
287
+ return {
288
+ type: object.slice(0, colon),
289
+ id: object.slice(colon + 1),
290
+ object,
291
+ ...hash === -1 ? {} : { relation: ref.slice(hash + 1) }
292
+ };
293
+ };
294
+ const byString = (a, b) => a < b ? -1 : a > b ? 1 : 0;
295
+ /** Run `fn` over `items` with at most `limit` in flight. */
296
+ async function mapLimit(items, limit, fn) {
297
+ const out = Array.from({ length: items.length });
298
+ let next = 0;
299
+ const worker = async () => {
300
+ while (next < items.length) {
301
+ const index = next++;
302
+ out[index] = await fn(items[index]);
303
+ }
304
+ };
305
+ await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker));
306
+ return out;
307
+ }
308
+ /** Build the relationship methods over the engine's plumbing. */
309
+ function createRelationships(deps) {
310
+ const { storage, hooks, onHookError } = deps;
311
+ const maxDepth = deps.options?.maxDepth ?? 25;
312
+ const requireSchema = () => {
313
+ if (!deps.schema) throw new RelationSchemaError("This catalog declares no relations: pass `relations` to definePermissions to use ReBAC.");
314
+ return deps.schema;
315
+ };
316
+ /**
317
+ * Coalesces identical reads that are in flight at the same time (concurrent checks of the same
318
+ * object share one `getKeys`); within one check the lib already reads each pair once.
319
+ */
320
+ const inflight = /* @__PURE__ */ new Map();
321
+ const scan = (prefix, parse) => {
322
+ let pending = inflight.get(prefix);
323
+ if (!pending) {
324
+ pending = storage.getKeys(prefix).then((keys) => keys.map(parse).filter((value) => value !== null)).finally(() => inflight.delete(prefix));
325
+ inflight.set(prefix, pending);
326
+ }
327
+ return pending;
328
+ };
329
+ /** The tuple reader the lib's checker plugs into: subjects stored on `(object, relation)`. */
330
+ const read = (object, relation, context = {}) => {
331
+ context.signal?.throwIfAborted();
332
+ return scan(relationSubjectsPrefix(object, relation), (key) => {
333
+ const parsed = parseRelationshipKey(key);
334
+ return parsed && parsed.object === object && parsed.relation === relation ? parsed.subject : null;
335
+ });
336
+ };
337
+ let checker = null;
338
+ const getChecker = () => {
339
+ if (!checker) checker = createRelationChecker(requireSchema(), {
340
+ read,
341
+ maxDepth
342
+ });
343
+ return checker;
344
+ };
345
+ let edgesBySource = null;
346
+ const sourceKey = (type, relation) => `${type}#${relation ?? ""}`;
347
+ const edgesFrom = (type, relation) => {
348
+ if (!edgesBySource) {
349
+ edgesBySource = /* @__PURE__ */ new Map();
350
+ for (const edge of relationEdges(requireSchema())) {
351
+ const key = sourceKey(edge.source.type, edge.source.relation);
352
+ const list = edgesBySource.get(key);
353
+ if (list) list.push(edge);
354
+ else edgesBySource.set(key, [edge]);
355
+ }
356
+ }
357
+ return edgesBySource.get(sourceKey(type, relation)) ?? [];
358
+ };
359
+ const validTuple = (input) => {
360
+ const schema = requireSchema();
361
+ const parsed = parseInput(relationshipInputSchema, input);
362
+ const tuple = {
363
+ object: parsed.object,
364
+ relation: parsed.relation,
365
+ subject: parsed.subject
366
+ };
367
+ if (!isAllowedTuple(schema, tuple)) {
368
+ const { type } = parseRef(tuple.object);
369
+ throw new RelationSchemaError(hasRelation(schema, type, tuple.relation) ? `The schema doesn't allow "${tuple.subject}" on ${type}#${tuple.relation}.` : `Unknown relation "${type}#${tuple.relation}".`);
370
+ }
371
+ return {
372
+ parsed,
373
+ tuple
374
+ };
375
+ };
376
+ const validQuery = (relation, object) => {
377
+ const schema = requireSchema();
378
+ const { type } = parseRef(object);
379
+ if (!hasRelation(schema, type, relation)) throw new RelationSchemaError(`Unknown relation "${type}#${relation}".`);
380
+ };
381
+ async function writeRelationship(input) {
382
+ deps.assertWritable("write relationship");
383
+ const { parsed, tuple } = validTuple(input);
384
+ const actor = deps.toActor(parsed.actor);
385
+ const key = relationshipKey(tuple.object, tuple.relation, tuple.subject);
386
+ const reverse = reverseRelationshipKey(tuple.object, tuple.relation, tuple.subject);
387
+ return deps.withLock(key, async () => {
388
+ const existing = await storage.getItem(key);
389
+ if (existing) {
390
+ if (await storage.getItem(reverse) === null) await storage.setItem(reverse, tuple);
391
+ return {
392
+ relationship: existing,
393
+ created: false
394
+ };
395
+ }
396
+ const at = deps.nowIso();
397
+ const relationship = {
398
+ object: tuple.object,
399
+ relation: tuple.relation,
400
+ subject: tuple.subject,
401
+ createdBy: actor?.id ?? null,
402
+ createdAt: at,
403
+ ...parsed.reason !== void 0 ? { reason: parsed.reason } : {}
404
+ };
405
+ await runBefore(hooks, {
406
+ type: "relationship.write",
407
+ subject: tuple.subject,
408
+ relationship,
409
+ actor
410
+ });
411
+ if (isCompareAndSwap(storage)) {
412
+ if (!await storage.compareAndSwap({
413
+ key,
414
+ expected: null,
415
+ next: relationship
416
+ })) {
417
+ const winner = await storage.getItem(key);
418
+ if (winner) return {
419
+ relationship: winner,
420
+ created: false
421
+ };
422
+ throw new Error(`writeRelationship: storage kept changing "${key}"; giving up`);
423
+ }
424
+ } else await storage.setItem(key, relationship);
425
+ await storage.setItem(reverse, tuple);
426
+ await deps.appendHistory({
427
+ type: "relate",
428
+ subject: tuple.subject,
429
+ object: tuple.object,
430
+ relation: tuple.relation,
431
+ actor: actor?.id ?? null,
432
+ ...parsed.reason !== void 0 ? { reason: parsed.reason } : {}
433
+ }, at);
434
+ await runAfter(hooks, {
435
+ type: "relationship.written",
436
+ subject: tuple.subject,
437
+ relationship,
438
+ actor,
439
+ at
440
+ }, onHookError);
441
+ return {
442
+ relationship,
443
+ created: true
444
+ };
445
+ });
446
+ }
447
+ async function deleteRelationship(input) {
448
+ deps.assertWritable("delete relationship");
449
+ requireSchema();
450
+ const parsed = parseInput(relationshipInputSchema, input);
451
+ const actor = deps.toActor(parsed.actor);
452
+ const key = relationshipKey(parsed.object, parsed.relation, parsed.subject);
453
+ const reverse = reverseRelationshipKey(parsed.object, parsed.relation, parsed.subject);
454
+ return deps.withLock(key, async () => {
455
+ const existing = await storage.getItem(key);
456
+ if (!existing) {
457
+ if (await storage.getItem(reverse) !== null) await storage.removeItem(reverse);
458
+ return {
459
+ removed: false,
460
+ relationship: null
461
+ };
462
+ }
463
+ const reason = parsed.reason !== void 0 ? { reason: parsed.reason } : {};
464
+ await runBefore(hooks, {
465
+ type: "relationship.delete",
466
+ subject: existing.subject,
467
+ relationship: existing,
468
+ actor,
469
+ ...reason
470
+ });
471
+ await storage.removeItem(reverse);
472
+ await storage.removeItem(key);
473
+ const at = deps.nowIso();
474
+ await deps.appendHistory({
475
+ type: "unrelate",
476
+ subject: existing.subject,
477
+ object: existing.object,
478
+ relation: existing.relation,
479
+ actor: actor?.id ?? null,
480
+ ...reason
481
+ }, at);
482
+ await runAfter(hooks, {
483
+ type: "relationship.deleted",
484
+ subject: existing.subject,
485
+ relationship: existing,
486
+ actor,
487
+ ...reason,
488
+ at
489
+ }, onHookError);
490
+ return {
491
+ removed: true,
492
+ relationship: existing
493
+ };
494
+ });
495
+ }
496
+ async function readRelationships(query = {}) {
497
+ const parsed = parseInput(relationshipQuerySchema, query);
498
+ const limit = parsed.limit ?? DEFAULT_PAGE;
499
+ const { object, relation, subject } = parsed;
500
+ const isRef = object !== void 0 && object.includes(":");
501
+ let keys;
502
+ if (isRef && relation !== void 0 && subject !== void 0) keys = [relationshipKey(object, relation, subject)];
503
+ else if (subject !== void 0 && !isRef) keys = (await storage.getKeys(subjectReversePrefix(subject))).map(parseReverseRelationshipKey).filter((t) => t !== null && t.subject === subject).filter((t) => object === void 0 || parseRef(t.object).type === object).map((t) => relationshipKey(t.object, t.relation, t.subject));
504
+ else if (isRef && relation !== void 0) keys = await storage.getKeys(relationSubjectsPrefix(object, relation));
505
+ else if (isRef) keys = await storage.getKeys(objectRelationshipsPrefix(object));
506
+ else if (object !== void 0) keys = await storage.getKeys(relationshipTypePrefix(object));
507
+ else keys = await storage.getKeys(relationshipsPrefix());
508
+ let tuples = keys.map((key) => ({
509
+ key,
510
+ tuple: parseRelationshipKey(key)
511
+ })).filter((entry) => entry.tuple !== null && (relation === void 0 || entry.tuple.relation === relation) && (subject === void 0 || entry.tuple.subject === subject) && (!isRef || entry.tuple.object === object));
512
+ tuples.sort((a, b) => byString(a.key, b.key));
513
+ if (parsed.cursor !== void 0) {
514
+ const after = parsed.cursor;
515
+ tuples = tuples.filter((entry) => entry.key > after);
516
+ }
517
+ const page = tuples.slice(0, limit);
518
+ const relationships = (await Promise.all(page.map((entry) => storage.getItem(entry.key)))).filter((r) => r !== null && typeof r === "object");
519
+ const last = page[page.length - 1];
520
+ return {
521
+ relationships,
522
+ nextCursor: tuples.length > limit && last ? last.key : null
523
+ };
524
+ }
525
+ async function checkRelation(query, context = {}) {
526
+ const parsed = parseInput(relationCheckSchema, query);
527
+ validQuery(parsed.relation, parsed.object);
528
+ return getChecker().explain(parsed, context);
529
+ }
530
+ /** `checkRelation(...).allowed`, `false` when the check throws (depth, stale schema). */
531
+ const confirm = async (subject, relation, object) => {
532
+ try {
533
+ return (await getChecker().explain({
534
+ subject,
535
+ relation,
536
+ object
537
+ })).allowed;
538
+ } catch {
539
+ return false;
540
+ }
541
+ };
542
+ async function listObjects(query) {
543
+ const parsed = parseInput(listObjectsQuerySchema, query);
544
+ if (!hasRelation(requireSchema(), parsed.type, parsed.relation)) throw new RelationSchemaError(`Unknown relation "${parsed.type}#${parsed.relation}".`);
545
+ const subject = parseRef(parsed.subject);
546
+ /** Every tuple naming exactly `ref`, memoized for this walk. */
547
+ const reverseMemo = /* @__PURE__ */ new Map();
548
+ const tuplesNaming = (ref) => {
549
+ let pending = reverseMemo.get(ref);
550
+ if (!pending) {
551
+ pending = storage.getKeys(subjectReversePrefix(ref)).then((keys) => keys.map(parseReverseRelationshipKey).filter((t) => t !== null && t.subject === ref));
552
+ reverseMemo.set(ref, pending);
553
+ }
554
+ return pending;
555
+ };
556
+ const reached = /* @__PURE__ */ new Map();
557
+ const found = /* @__PURE__ */ new Map();
558
+ let frontier = [];
559
+ const reach = (object, relation, exact) => {
560
+ const key = `${object}#${relation}`;
561
+ const seen = reached.get(key);
562
+ if (seen === true || seen === false && !exact) return;
563
+ reached.set(key, exact);
564
+ if (parseRef(object).type === parsed.type && relation === parsed.relation) found.set(object, (found.get(object) ?? false) || exact);
565
+ frontier.push({
566
+ object,
567
+ relation,
568
+ exact
569
+ });
570
+ };
571
+ /** Direct tuples on `(X, q)` naming `ref`, whose type `(sourceType, sourceRelation)` is allowed. */
572
+ const viaDirect = async (ref, sourceType, sourceRelation, wildcard, exact) => {
573
+ const edges = edgesFrom(sourceType, sourceRelation).filter((edge) => edge.kind === "direct" && (edge.source.wildcard ?? false) === wildcard);
574
+ if (edges.length === 0) return;
575
+ for (const tuple of await tuplesNaming(ref)) {
576
+ const type = parseRef(tuple.object).type;
577
+ for (const edge of edges) if (edge.target.type === type && edge.target.relation === tuple.relation) reach(tuple.object, tuple.relation, exact && edge.exact);
578
+ }
579
+ };
580
+ if (subject.relation !== void 0) reach(subject.object, subject.relation, true);
581
+ else {
582
+ await viaDirect(parsed.subject, subject.type, void 0, false, true);
583
+ if (subject.id !== "*") await viaDirect(`${subject.type}:*`, subject.type, void 0, true, true);
584
+ }
585
+ for (let depth = 0; frontier.length > 0 && depth < maxDepth; depth++) {
586
+ const current = frontier;
587
+ frontier = [];
588
+ await mapLimit(current, 16, async (node) => {
589
+ const { type } = parseRef(node.object);
590
+ await viaDirect(`${node.object}#${node.relation}`, type, node.relation, false, node.exact);
591
+ for (const edge of edgesFrom(type, node.relation)) if (edge.kind === "computed") reach(node.object, edge.target.relation, node.exact && edge.exact);
592
+ else if (edge.kind === "from") {
593
+ for (const tuple of await tuplesNaming(node.object)) if (tuple.relation === edge.tupleset && parseRef(tuple.object).type === edge.target.type) reach(tuple.object, edge.target.relation, node.exact && edge.exact);
594
+ }
595
+ });
596
+ }
597
+ const objects = [...found.keys()];
598
+ const allowed = await mapLimit(objects, 16, async (object) => found.get(object) ? true : confirm(parsed.subject, parsed.relation, object));
599
+ return objects.filter((_, i) => allowed[i]).sort(byString);
600
+ }
601
+ async function listRelationSubjects(query) {
602
+ const parsed = parseInput(listRelationSubjectsQuerySchema, query);
603
+ const schema = requireSchema();
604
+ validQuery(parsed.relation, parsed.object);
605
+ const visited = /* @__PURE__ */ new Map();
606
+ const candidates = /* @__PURE__ */ new Map();
607
+ const add = (subject, exact) => candidates.set(subject, (candidates.get(subject) ?? false) || exact);
608
+ const expand = async (object, relation, depth, exact) => {
609
+ if (depth > maxDepth) return;
610
+ const key = `${object}#${relation}`;
611
+ const seen = visited.get(key);
612
+ if (seen === true || seen === false && !exact) return;
613
+ visited.set(key, exact);
614
+ const { type } = parseRef(object);
615
+ if (!hasRelation(schema, type, relation)) return;
616
+ await walk(schema[type][relation], object, relation, depth, exact);
617
+ };
618
+ const walk = async (expr, object, relation, depth, exact) => {
619
+ switch (expr.kind) {
620
+ case "direct":
621
+ for (const stored of await read(object, relation)) {
622
+ const ref = parseRef(stored);
623
+ if (ref.relation !== void 0) await expand(ref.object, ref.relation, depth + 1, exact);
624
+ else add(stored, exact);
625
+ }
626
+ return;
627
+ case "computed": return expand(object, expr.relation, depth + 1, exact);
628
+ case "from":
629
+ for (const related of await read(object, expr.tupleset)) if (!related.includes("#")) await expand(related, expr.relation, depth + 1, exact);
630
+ return;
631
+ case "union":
632
+ for (const child of expr.children) await walk(child, object, relation, depth, exact);
633
+ return;
634
+ case "intersection":
635
+ for (const child of expr.children) await walk(child, object, relation, depth, false);
636
+ return;
637
+ case "exclusion": return walk(expr.base, object, relation, depth, false);
638
+ }
639
+ };
640
+ await expand(parsed.object, parsed.relation, 0, true);
641
+ const subjects = [...candidates.keys()].filter((subject) => parsed.type === void 0 || parseRef(subject).type === parsed.type);
642
+ const allowed = await mapLimit(subjects, 16, async (subject) => candidates.get(subject) ? true : confirm(subject, parsed.relation, parsed.object));
643
+ return subjects.filter((_, i) => allowed[i]).sort(byString);
644
+ }
645
+ async function loadRelations(objects, options = {}) {
646
+ const schema = requireSchema();
647
+ const maxObjects = options.maxObjects ?? DEFAULT_MAX_OBJECTS;
648
+ const memory = createMemoryTuples();
649
+ const loaded = /* @__PURE__ */ new Set();
650
+ let frontier = [...new Set(objects)];
651
+ for (let depth = 0; frontier.length > 0 && depth <= maxDepth; depth++) {
652
+ const batch = frontier.filter((object) => !loaded.has(object)).slice(0, maxObjects - loaded.size);
653
+ frontier = [];
654
+ for (const object of batch) loaded.add(object);
655
+ const keys = await mapLimit(batch, 16, (object) => storage.getKeys(objectRelationshipsPrefix(object)));
656
+ for (const tuple of keys.flat().map(parseRelationshipKey)) {
657
+ if (!tuple) continue;
658
+ memory.add(tuple);
659
+ const ref = parseRef(tuple.subject);
660
+ if (ref.id !== "*" && Object.keys(schema[ref.type] ?? {}).length > 0 && !loaded.has(ref.object)) frontier.push(ref.object);
661
+ }
662
+ if (loaded.size >= maxObjects) break;
663
+ }
664
+ const sync = createRelationChecker(schema, {
665
+ read: memory.read,
666
+ maxDepth
667
+ });
668
+ return {
669
+ check: (query) => sync.check(query),
670
+ explain: (query) => sync.explain(query),
671
+ relation: (subject, relation, object) => sync.check({
672
+ subject,
673
+ relation,
674
+ object
675
+ }),
676
+ tuples: memory.tuples
677
+ };
678
+ }
679
+ async function repairRelationships() {
680
+ deps.assertWritable("repair relationships");
681
+ const [forward, reverse] = await Promise.all([storage.getKeys(relationshipsPrefix()), storage.getKeys(reverseRelationshipsPrefix())]);
682
+ const reverseSet = new Set(reverse);
683
+ const expected = /* @__PURE__ */ new Set();
684
+ let restored = 0;
685
+ let removed = 0;
686
+ for (const key of forward) {
687
+ const tuple = parseRelationshipKey(key);
688
+ if (!tuple) continue;
689
+ const rev = reverseRelationshipKey(tuple.object, tuple.relation, tuple.subject);
690
+ expected.add(rev);
691
+ if (!reverseSet.has(rev)) {
692
+ await storage.setItem(rev, tuple);
693
+ restored++;
694
+ }
695
+ }
696
+ for (const key of reverse) {
697
+ if (expected.has(key)) continue;
698
+ const tuple = parseReverseRelationshipKey(key);
699
+ if (tuple && await storage.getItem(relationshipKey(tuple.object, tuple.relation, tuple.subject))) continue;
700
+ await storage.removeItem(key);
701
+ removed++;
702
+ }
703
+ return {
704
+ scanned: forward.length,
705
+ restored,
706
+ removed
707
+ };
708
+ }
709
+ return {
710
+ writeRelationship,
711
+ deleteRelationship,
712
+ readRelationships,
713
+ checkRelation,
714
+ listObjects,
715
+ listRelationSubjects,
716
+ loadRelations,
717
+ repairRelationships
718
+ };
719
+ }
720
+ /** Render a relation expression on one line. */
721
+ function describeRelation(expr) {
722
+ const wrap = (child) => child.kind === "union" || child.kind === "intersection" || child.kind === "exclusion" ? `(${describeRelation(child)})` : describeRelation(child);
723
+ switch (expr.kind) {
724
+ case "direct": return `[${expr.types.join(", ")}]`;
725
+ case "computed": return expr.relation;
726
+ case "from": return `${expr.relation} from ${expr.tupleset}`;
727
+ case "union": return expr.children.map(wrap).join(" or ");
728
+ case "intersection": return expr.children.map(wrap).join(" and ");
729
+ case "exclusion": return `${wrap(expr.base)} but not ${wrap(expr.subtract)}`;
730
+ }
731
+ }
732
+ const directSubjects = (expr, out = []) => {
733
+ if (expr.kind === "direct") {
734
+ for (const type of expr.types) if (!out.includes(type)) out.push(type);
735
+ } else if (expr.kind === "union" || expr.kind === "intersection") for (const child of expr.children) directSubjects(child, out);
736
+ else if (expr.kind === "exclusion") {
737
+ directSubjects(expr.base, out);
738
+ directSubjects(expr.subtract, out);
739
+ }
740
+ return out;
741
+ };
742
+ /** The relation schema as plain data (types, relations, writable subject types, rules). */
743
+ function relationSchemaView(schema) {
744
+ return Object.entries(schema).map(([type, relations]) => ({
745
+ type,
746
+ relations: Object.entries(relations).map(([name, expr]) => ({
747
+ name,
748
+ subjects: directSubjects(expr),
749
+ rule: describeRelation(expr)
750
+ }))
751
+ }));
752
+ }
753
+ //#endregion
754
+ export { revokeInputSchema as A, historyQuerySchema as C, relationCheckSchema as D, listRelationSubjectsQuerySchema as E, upsertSubjectInputSchema as M, relationshipInputSchema as O, grantInputSchema as S, listQuerySchema as T, defaultHookErrorReporter as _, isWatchable as a, runBefore as b, grantsPrefix as c, parseKey as d, subjectGrantsPrefix as f, HookDeniedError as g, subjectsPrefix as h, isTransactional as i, setRoleInputSchema as j, relationshipQuerySchema as k, historyKey as l, subjectKey as m, relationSchemaView as n, ROOT as o, subjectHistoryPrefix as p, isCompareAndSwap as r, grantKey as s, createRelationships as t, historyPrefix as u, normalizeHooks as v, listObjectsQuerySchema as w, actorSchema as x, runAfter as y };
755
+
756
+ //# sourceMappingURL=relationships-BfLonEMI.mjs.map