inlaycms 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 (364) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +469 -2
  3. package/assets/admin/admin.css +2 -0
  4. package/assets/admin/admin.js +1 -0
  5. package/assets/admin/editor.js +125 -0
  6. package/dist/adapters/admin/activity.d.ts +17 -0
  7. package/dist/adapters/admin/activity.js +59 -0
  8. package/dist/adapters/admin/assets.d.ts +14 -0
  9. package/dist/adapters/admin/assets.js +64 -0
  10. package/dist/adapters/admin/attention.d.ts +15 -0
  11. package/dist/adapters/admin/attention.js +34 -0
  12. package/dist/adapters/admin/context.d.ts +40 -0
  13. package/dist/adapters/admin/context.js +142 -0
  14. package/dist/adapters/admin/form/walk.d.ts +61 -0
  15. package/dist/adapters/admin/form/walk.js +565 -0
  16. package/dist/adapters/admin/pages.d.ts +38 -0
  17. package/dist/adapters/admin/pages.js +66 -0
  18. package/dist/adapters/admin/publish.d.ts +12 -0
  19. package/dist/adapters/admin/publish.js +195 -0
  20. package/dist/adapters/admin/routes.d.ts +47 -0
  21. package/dist/adapters/admin/routes.js +255 -0
  22. package/dist/adapters/admin/screens/backups.d.ts +11 -0
  23. package/dist/adapters/admin/screens/backups.js +223 -0
  24. package/dist/adapters/admin/screens/dashboard.d.ts +4 -0
  25. package/dist/adapters/admin/screens/dashboard.js +160 -0
  26. package/dist/adapters/admin/screens/editor.d.ts +67 -0
  27. package/dist/adapters/admin/screens/editor.js +416 -0
  28. package/dist/adapters/admin/screens/history.d.ts +4 -0
  29. package/dist/adapters/admin/screens/history.js +105 -0
  30. package/dist/adapters/admin/screens/lists.d.ts +4 -0
  31. package/dist/adapters/admin/screens/lists.js +297 -0
  32. package/dist/adapters/admin/screens/media.d.ts +20 -0
  33. package/dist/adapters/admin/screens/media.js +186 -0
  34. package/dist/adapters/admin/screens/pickers.d.ts +29 -0
  35. package/dist/adapters/admin/screens/pickers.js +134 -0
  36. package/dist/adapters/admin/screens/preview-pane.d.ts +10 -0
  37. package/dist/adapters/admin/screens/preview-pane.js +74 -0
  38. package/dist/adapters/admin/screens/redirects.d.ts +4 -0
  39. package/dist/adapters/admin/screens/redirects.js +137 -0
  40. package/dist/adapters/admin/screens/releases.d.ts +7 -0
  41. package/dist/adapters/admin/screens/releases.js +287 -0
  42. package/dist/adapters/admin/screens/search.d.ts +22 -0
  43. package/dist/adapters/admin/screens/search.js +113 -0
  44. package/dist/adapters/admin/screens/security.d.ts +4 -0
  45. package/dist/adapters/admin/screens/security.js +85 -0
  46. package/dist/adapters/admin/screens/users.d.ts +4 -0
  47. package/dist/adapters/admin/screens/users.js +330 -0
  48. package/dist/adapters/admin/theme.d.ts +1 -0
  49. package/dist/adapters/admin/theme.js +1 -0
  50. package/dist/adapters/admin/translations.d.ts +16 -0
  51. package/dist/adapters/admin/translations.js +23 -0
  52. package/dist/adapters/admin/upload.d.ts +8 -0
  53. package/dist/adapters/admin/upload.js +116 -0
  54. package/dist/adapters/admin/views/data-table.d.ts +82 -0
  55. package/dist/adapters/admin/views/data-table.js +117 -0
  56. package/dist/adapters/admin/views/icons.d.ts +48 -0
  57. package/dist/adapters/admin/views/icons.js +47 -0
  58. package/dist/adapters/admin/views/layout.d.ts +51 -0
  59. package/dist/adapters/admin/views/layout.js +145 -0
  60. package/dist/adapters/admin/views/sheet.d.ts +15 -0
  61. package/dist/adapters/admin/views/sheet.js +6 -0
  62. package/dist/adapters/admin/views/status.d.ts +32 -0
  63. package/dist/adapters/admin/views/status.js +74 -0
  64. package/dist/adapters/auth/handler.d.ts +10 -0
  65. package/dist/adapters/auth/handler.js +63 -0
  66. package/dist/adapters/auth/request.d.ts +8 -0
  67. package/dist/adapters/auth/request.js +14 -0
  68. package/dist/adapters/cli/bin.d.ts +2 -0
  69. package/dist/adapters/cli/bin.js +42 -0
  70. package/dist/adapters/cli/main.d.ts +7 -0
  71. package/dist/adapters/cli/main.js +982 -0
  72. package/dist/adapters/cli/remote.d.ts +52 -0
  73. package/dist/adapters/cli/remote.js +161 -0
  74. package/dist/adapters/mcp/agent-refs.d.ts +9 -0
  75. package/dist/adapters/mcp/agent-refs.js +25 -0
  76. package/dist/adapters/mcp/documents.d.ts +15 -0
  77. package/dist/adapters/mcp/documents.js +26 -0
  78. package/dist/adapters/mcp/fetch-url.d.ts +19 -0
  79. package/dist/adapters/mcp/fetch-url.js +244 -0
  80. package/dist/adapters/mcp/request-state.d.ts +15 -0
  81. package/dist/adapters/mcp/request-state.js +38 -0
  82. package/dist/adapters/mcp/results.d.ts +20 -0
  83. package/dist/adapters/mcp/results.js +61 -0
  84. package/dist/adapters/mcp/route.d.ts +34 -0
  85. package/dist/adapters/mcp/route.js +52 -0
  86. package/dist/adapters/mcp/server.d.ts +44 -0
  87. package/dist/adapters/mcp/server.js +49 -0
  88. package/dist/adapters/mcp/tools/history.d.ts +5 -0
  89. package/dist/adapters/mcp/tools/history.js +84 -0
  90. package/dist/adapters/mcp/tools/media.d.ts +5 -0
  91. package/dist/adapters/mcp/tools/media.js +102 -0
  92. package/dist/adapters/mcp/tools/publish.d.ts +5 -0
  93. package/dist/adapters/mcp/tools/publish.js +157 -0
  94. package/dist/adapters/mcp/tools/read.d.ts +5 -0
  95. package/dist/adapters/mcp/tools/read.js +279 -0
  96. package/dist/adapters/mcp/tools/redirects.d.ts +5 -0
  97. package/dist/adapters/mcp/tools/redirects.js +74 -0
  98. package/dist/adapters/mcp/tools/write.d.ts +5 -0
  99. package/dist/adapters/mcp/tools/write.js +304 -0
  100. package/dist/adapters/site/backup-schedule.d.ts +8 -0
  101. package/dist/adapters/site/backup-schedule.js +31 -0
  102. package/dist/adapters/site/cache.d.ts +29 -0
  103. package/dist/adapters/site/cache.js +63 -0
  104. package/dist/adapters/site/context.d.ts +29 -0
  105. package/dist/adapters/site/context.js +278 -0
  106. package/dist/adapters/site/head.d.ts +23 -0
  107. package/dist/adapters/site/head.js +44 -0
  108. package/dist/adapters/site/limiter.d.ts +17 -0
  109. package/dist/adapters/site/limiter.js +39 -0
  110. package/dist/adapters/site/lock.d.ts +30 -0
  111. package/dist/adapters/site/lock.js +143 -0
  112. package/dist/adapters/site/media-routes.d.ts +4 -0
  113. package/dist/adapters/site/media-routes.js +55 -0
  114. package/dist/adapters/site/plugin.d.ts +122 -0
  115. package/dist/adapters/site/plugin.js +436 -0
  116. package/dist/adapters/site/preview.d.ts +36 -0
  117. package/dist/adapters/site/preview.js +82 -0
  118. package/dist/adapters/site/public.d.ts +6 -0
  119. package/dist/adapters/site/public.js +87 -0
  120. package/dist/adapters/site/render.d.ts +45 -0
  121. package/dist/adapters/site/render.js +191 -0
  122. package/dist/adapters/site/routes.d.ts +17 -0
  123. package/dist/adapters/site/routes.js +125 -0
  124. package/dist/adapters/site/sitemap.d.ts +10 -0
  125. package/dist/adapters/site/sitemap.js +10 -0
  126. package/dist/adapters/web.d.ts +22 -0
  127. package/dist/adapters/web.js +75 -0
  128. package/dist/config.d.ts +21 -0
  129. package/dist/config.js +58 -0
  130. package/dist/core/agent/format.d.ts +62 -0
  131. package/dist/core/agent/format.js +211 -0
  132. package/dist/core/agent/schema.d.ts +6 -0
  133. package/dist/core/agent/schema.js +86 -0
  134. package/dist/core/archive.d.ts +12 -0
  135. package/dist/core/archive.js +72 -0
  136. package/dist/core/auth/auth.d.ts +4911 -0
  137. package/dist/core/auth/auth.js +61 -0
  138. package/dist/core/auth/roles.d.ts +10 -0
  139. package/dist/core/auth/roles.js +38 -0
  140. package/dist/core/auth/service.d.ts +118 -0
  141. package/dist/core/auth/service.js +424 -0
  142. package/dist/core/auth/upload-token.d.ts +8 -0
  143. package/dist/core/auth/upload-token.js +27 -0
  144. package/dist/core/backup/backup.d.ts +28 -0
  145. package/dist/core/backup/backup.js +126 -0
  146. package/dist/core/backup/restore.d.ts +41 -0
  147. package/dist/core/backup/restore.js +264 -0
  148. package/dist/core/backup/schedule.d.ts +12 -0
  149. package/dist/core/backup/schedule.js +29 -0
  150. package/dist/core/boot.d.ts +63 -0
  151. package/dist/core/boot.js +141 -0
  152. package/dist/core/check.d.ts +32 -0
  153. package/dist/core/check.js +111 -0
  154. package/dist/core/content/activity.d.ts +8 -0
  155. package/dist/core/content/activity.js +101 -0
  156. package/dist/core/content/delete.d.ts +5 -0
  157. package/dist/core/content/delete.js +100 -0
  158. package/dist/core/content/diff.d.ts +6 -0
  159. package/dist/core/content/diff.js +129 -0
  160. package/dist/core/content/dry-run.d.ts +24 -0
  161. package/dist/core/content/dry-run.js +69 -0
  162. package/dist/core/content/gaps.d.ts +25 -0
  163. package/dist/core/content/gaps.js +50 -0
  164. package/dist/core/content/hidden.d.ts +33 -0
  165. package/dist/core/content/hidden.js +103 -0
  166. package/dist/core/content/history.d.ts +15 -0
  167. package/dist/core/content/history.js +62 -0
  168. package/dist/core/content/patch.d.ts +40 -0
  169. package/dist/core/content/patch.js +287 -0
  170. package/dist/core/content/path.d.ts +6 -0
  171. package/dist/core/content/path.js +38 -0
  172. package/dist/core/content/publish.d.ts +26 -0
  173. package/dist/core/content/publish.js +211 -0
  174. package/dist/core/content/read.d.ts +99 -0
  175. package/dist/core/content/read.js +141 -0
  176. package/dist/core/content/reader.d.ts +37 -0
  177. package/dist/core/content/reader.js +195 -0
  178. package/dist/core/content/refs.d.ts +44 -0
  179. package/dist/core/content/refs.js +161 -0
  180. package/dist/core/content/release-migrations.d.ts +12 -0
  181. package/dist/core/content/release-migrations.js +154 -0
  182. package/dist/core/content/save.d.ts +25 -0
  183. package/dist/core/content/save.js +87 -0
  184. package/dist/core/content/schema.d.ts +22 -0
  185. package/dist/core/content/schema.js +127 -0
  186. package/dist/core/content/search.d.ts +8 -0
  187. package/dist/core/content/search.js +89 -0
  188. package/dist/core/content/shipped-input.d.ts +39 -0
  189. package/dist/core/content/shipped-input.js +221 -0
  190. package/dist/core/content/shipped.d.ts +112 -0
  191. package/dist/core/content/shipped.js +437 -0
  192. package/dist/core/content/slug.d.ts +23 -0
  193. package/dist/core/content/slug.js +182 -0
  194. package/dist/core/content/stats.d.ts +11 -0
  195. package/dist/core/content/stats.js +30 -0
  196. package/dist/core/content/translate.d.ts +3 -0
  197. package/dist/core/content/translate.js +68 -0
  198. package/dist/core/content/usages.d.ts +13 -0
  199. package/dist/core/content/usages.js +70 -0
  200. package/dist/core/content/validate.d.ts +21 -0
  201. package/dist/core/content/validate.js +124 -0
  202. package/dist/core/content/write.d.ts +10 -0
  203. package/dist/core/content/write.js +93 -0
  204. package/dist/core/core.d.ts +227 -0
  205. package/dist/core/core.js +251 -0
  206. package/dist/core/db/connection.d.ts +6 -0
  207. package/dist/core/db/connection.js +12 -0
  208. package/dist/core/db/migrate.d.ts +9 -0
  209. package/dist/core/db/migrate.js +42 -0
  210. package/dist/core/db/migrations/0001_content.d.ts +2 -0
  211. package/dist/core/db/migrations/0001_content.js +69 -0
  212. package/dist/core/db/migrations/0002_media.d.ts +2 -0
  213. package/dist/core/db/migrations/0002_media.js +26 -0
  214. package/dist/core/db/migrations/0003_redirects.d.ts +2 -0
  215. package/dist/core/db/migrations/0003_redirects.js +19 -0
  216. package/dist/core/db/migrations/0004_token_cutoffs.d.ts +3 -0
  217. package/dist/core/db/migrations/0004_token_cutoffs.js +8 -0
  218. package/dist/core/db/migrations/0005_site_lock.d.ts +3 -0
  219. package/dist/core/db/migrations/0005_site_lock.js +10 -0
  220. package/dist/core/db/migrations/0006_releases.d.ts +3 -0
  221. package/dist/core/db/migrations/0006_releases.js +34 -0
  222. package/dist/core/db/schema.d.ts +155 -0
  223. package/dist/core/db/schema.js +1 -0
  224. package/dist/core/errors.d.ts +27 -0
  225. package/dist/core/errors.js +15 -0
  226. package/dist/core/export/export.d.ts +17 -0
  227. package/dist/core/export/export.js +185 -0
  228. package/dist/core/export/format.d.ts +241 -0
  229. package/dist/core/export/format.js +106 -0
  230. package/dist/core/export/import.d.ts +54 -0
  231. package/dist/core/export/import.js +180 -0
  232. package/dist/core/export/read.d.ts +16 -0
  233. package/dist/core/export/read.js +234 -0
  234. package/dist/core/export/replace.d.ts +17 -0
  235. package/dist/core/export/replace.js +36 -0
  236. package/dist/core/export/report.d.ts +55 -0
  237. package/dist/core/export/report.js +50 -0
  238. package/dist/core/export/schema.d.ts +8 -0
  239. package/dist/core/export/schema.js +47 -0
  240. package/dist/core/export/stage.d.ts +25 -0
  241. package/dist/core/export/stage.js +192 -0
  242. package/dist/core/fields/builtins.d.ts +70 -0
  243. package/dist/core/fields/builtins.js +335 -0
  244. package/dist/core/fields/define.d.ts +86 -0
  245. package/dist/core/fields/define.js +34 -0
  246. package/dist/core/fields/walk.d.ts +49 -0
  247. package/dist/core/fields/walk.js +270 -0
  248. package/dist/core/fields/widgets.d.ts +27 -0
  249. package/dist/core/fields/widgets.js +330 -0
  250. package/dist/core/fields/zod-internals.d.ts +29 -0
  251. package/dist/core/fields/zod-internals.js +28 -0
  252. package/dist/core/media/detect.d.ts +5 -0
  253. package/dist/core/media/detect.js +66 -0
  254. package/dist/core/media/labels.d.ts +6 -0
  255. package/dist/core/media/labels.js +17 -0
  256. package/dist/core/media/library.d.ts +45 -0
  257. package/dist/core/media/library.js +239 -0
  258. package/dist/core/media/store.d.ts +11 -0
  259. package/dist/core/media/store.js +49 -0
  260. package/dist/core/media/variants.d.ts +10 -0
  261. package/dist/core/media/variants.js +59 -0
  262. package/dist/core/messages/en.d.ts +1278 -0
  263. package/dist/core/messages/en.js +1287 -0
  264. package/dist/core/messages/index.d.ts +7 -0
  265. package/dist/core/messages/index.js +14 -0
  266. package/dist/core/preview.d.ts +16 -0
  267. package/dist/core/preview.js +42 -0
  268. package/dist/core/redirects/collapse.d.ts +68 -0
  269. package/dist/core/redirects/collapse.js +153 -0
  270. package/dist/core/redirects/csv.d.ts +15 -0
  271. package/dist/core/redirects/csv.js +68 -0
  272. package/dist/core/redirects/redirects.d.ts +58 -0
  273. package/dist/core/redirects/redirects.js +545 -0
  274. package/dist/core/registry/content.d.ts +26 -0
  275. package/dist/core/registry/content.js +9 -0
  276. package/dist/core/registry/define.d.ts +145 -0
  277. package/dist/core/registry/define.js +41 -0
  278. package/dist/core/registry/migration.d.ts +44 -0
  279. package/dist/core/registry/migration.js +4 -0
  280. package/dist/core/registry/registry.d.ts +18 -0
  281. package/dist/core/registry/registry.js +255 -0
  282. package/dist/core/registry/render.d.ts +98 -0
  283. package/dist/core/registry/render.js +1 -0
  284. package/dist/core/registry/route.d.ts +42 -0
  285. package/dist/core/registry/route.js +3 -0
  286. package/dist/core/release/bundle.d.ts +21 -0
  287. package/dist/core/release/bundle.js +252 -0
  288. package/dist/core/release/keys.d.ts +11 -0
  289. package/dist/core/release/keys.js +60 -0
  290. package/dist/core/release/manifest.d.ts +103 -0
  291. package/dist/core/release/manifest.js +138 -0
  292. package/dist/core/release/pack.d.ts +20 -0
  293. package/dist/core/release/pack.js +97 -0
  294. package/dist/core/release/pointer.d.ts +19 -0
  295. package/dist/core/release/pointer.js +53 -0
  296. package/dist/core/release/read.d.ts +59 -0
  297. package/dist/core/release/read.js +256 -0
  298. package/dist/core/report.d.ts +31 -0
  299. package/dist/core/report.js +37 -0
  300. package/dist/core/richtext/html.d.ts +15 -0
  301. package/dist/core/richtext/html.js +81 -0
  302. package/dist/core/richtext/markdown.d.ts +19 -0
  303. package/dist/core/richtext/markdown.js +951 -0
  304. package/dist/core/richtext/schema.d.ts +98 -0
  305. package/dist/core/richtext/schema.js +275 -0
  306. package/dist/core/site/live.d.ts +56 -0
  307. package/dist/core/site/live.js +145 -0
  308. package/dist/core/site/paths.d.ts +15 -0
  309. package/dist/core/site/paths.js +64 -0
  310. package/dist/core/site/resolve.d.ts +9 -0
  311. package/dist/core/site/resolve.js +202 -0
  312. package/dist/core/site/sitemap.d.ts +11 -0
  313. package/dist/core/site/sitemap.js +24 -0
  314. package/dist/core/site-lock/site-lock.d.ts +21 -0
  315. package/dist/core/site-lock/site-lock.js +65 -0
  316. package/dist/core/site-lock/token.d.ts +7 -0
  317. package/dist/core/site-lock/token.js +24 -0
  318. package/dist/core/theme.d.ts +22 -0
  319. package/dist/core/theme.js +133 -0
  320. package/dist/core/types.d.ts +253 -0
  321. package/dist/core/types.js +1 -0
  322. package/dist/core/version.d.ts +1 -0
  323. package/dist/core/version.js +5 -0
  324. package/dist/host/activate.d.ts +53 -0
  325. package/dist/host/activate.js +162 -0
  326. package/dist/host/app.d.ts +46 -0
  327. package/dist/host/app.js +129 -0
  328. package/dist/host/bin.d.ts +5 -0
  329. package/dist/host/bin.js +8 -0
  330. package/dist/host/config.d.ts +36 -0
  331. package/dist/host/config.js +152 -0
  332. package/dist/host/dev.d.ts +10 -0
  333. package/dist/host/dev.js +293 -0
  334. package/dist/host/diff.d.ts +78 -0
  335. package/dist/host/diff.js +346 -0
  336. package/dist/host/hooks.d.ts +41 -0
  337. package/dist/host/hooks.js +49 -0
  338. package/dist/host/ipc.d.ts +123 -0
  339. package/dist/host/ipc.js +61 -0
  340. package/dist/host/preview-proxy.d.ts +24 -0
  341. package/dist/host/preview-proxy.js +154 -0
  342. package/dist/host/preview-worker.d.ts +42 -0
  343. package/dist/host/preview-worker.js +242 -0
  344. package/dist/host/primary.d.ts +38 -0
  345. package/dist/host/primary.js +357 -0
  346. package/dist/host/push.d.ts +125 -0
  347. package/dist/host/push.js +420 -0
  348. package/dist/host/serve.d.ts +16 -0
  349. package/dist/host/serve.js +150 -0
  350. package/dist/host/store.d.ts +48 -0
  351. package/dist/host/store.js +109 -0
  352. package/dist/host/worker.d.ts +67 -0
  353. package/dist/host/worker.js +310 -0
  354. package/dist/index.d.ts +47 -0
  355. package/dist/index.js +32 -0
  356. package/dist/jsx/html.d.ts +16 -0
  357. package/dist/jsx/html.js +65 -0
  358. package/dist/jsx/jsx-dev-runtime.d.ts +4 -0
  359. package/dist/jsx/jsx-dev-runtime.js +5 -0
  360. package/dist/jsx/jsx-runtime.d.ts +17 -0
  361. package/dist/jsx/jsx-runtime.js +41 -0
  362. package/dist/site.d.ts +17 -0
  363. package/dist/site.js +14 -0
  364. package/package.json +111 -3
@@ -0,0 +1,545 @@
1
+ import { assertCan } from '../auth/roles.js';
2
+ import { whereVisible } from '../content/read.js';
3
+ import { contentIssue } from '../content/validate.js';
4
+ import { InlayError } from '../errors.js';
5
+ import { t } from '../messages/index.js';
6
+ import { resolveWithoutRules } from '../site/resolve.js';
7
+ import { encodePath, isReserved, normalizePath, splitLocale, withLocale } from '../site/paths.js';
8
+ import { sitePath } from '../site/live.js';
9
+ import { collapseTargets, fillParams, hasLocalePrefix, isPattern, localize, paramNames, pathTarget, patternRegExp, ruleCandidates, ruleIndex, } from './collapse.js';
10
+ import { parseCsv } from './csv.js';
11
+ import { withTransaction } from '../content/dry-run.js';
12
+ const LABELS = {
13
+ from: 'label.redirect.from',
14
+ to: 'label.redirect.to',
15
+ status: 'label.redirect.status',
16
+ };
17
+ export function rowTarget(row) {
18
+ return row.to_group_id !== null ? { document: row.to_group_id } : { path: row.to_path ?? '/' };
19
+ }
20
+ function toRedirect(row) {
21
+ return {
22
+ id: row.id,
23
+ from: row.from_path,
24
+ to: rowTarget(row),
25
+ status: row.status,
26
+ source: row.source,
27
+ note: row.note,
28
+ createdBy: row.created_by_name,
29
+ createdAt: row.created_at,
30
+ };
31
+ }
32
+ export async function listRedirects(deps) {
33
+ const rows = await deps.db.selectFrom('redirects').selectAll().orderBy('from_path').orderBy('id').execute();
34
+ return rows.map(toRedirect);
35
+ }
36
+ const ORIGIN = /^https?:\/\/[^/?#]*/i;
37
+ /** A clean decoded site path without a query or fragment, or `null`. */
38
+ export function cleanPath(raw) {
39
+ const path = normalizePath(raw);
40
+ return path === null || path.includes('?') || path.includes('#') ? null : path;
41
+ }
42
+ /**
43
+ * The stored form of a redirect's `from` (a full http(s) URL loses its origin), or `null` if it can't be
44
+ * used. A default-locale prefix can't be used: such a request is sent to the unprefixed path first.
45
+ */
46
+ export function redirectFrom(registry, raw) {
47
+ const trimmed = raw.trim();
48
+ const stripped = trimmed.replace(ORIGIN, '');
49
+ const path = cleanPath(stripped === '' && stripped !== trimmed ? '/' : stripped);
50
+ if (path === null || isReserved(path) || path.split('/')[1] === registry.defaultLocale)
51
+ return null;
52
+ const names = paramNames(path);
53
+ return new Set(names).size === names.length ? path : null;
54
+ }
55
+ function isRoutable(deps, target) {
56
+ const def = deps.registry.types.get(target.type);
57
+ if (!def)
58
+ return false;
59
+ if (target.kind === 'singleton')
60
+ return def.kind === 'singleton' && def.path !== null;
61
+ if (target.kind === 'entry')
62
+ return def.kind === 'collection' && def.route !== null;
63
+ return false;
64
+ }
65
+ function lookup(deps) {
66
+ const memo = new Map();
67
+ return async (path, state = 'published') => {
68
+ const key = `${state} ${path}`;
69
+ let kind = memo.get(key);
70
+ if (kind === undefined) {
71
+ kind = (await resolveWithoutRules(deps, encodePath(path), { state })).kind;
72
+ memo.set(key, kind);
73
+ }
74
+ return kind;
75
+ };
76
+ }
77
+ /** Whether an exact `from` is the path of a published page in any locale it matches in. */
78
+ async function isLivePage(registry, look, from) {
79
+ const paths = splitLocale(registry, from).locale === registry.defaultLocale
80
+ ? registry.locales.map((l) => withLocale(registry, l, from))
81
+ : [from];
82
+ for (const p of paths) {
83
+ if ((await look(p)) === 'page')
84
+ return true;
85
+ }
86
+ return false;
87
+ }
88
+ /**
89
+ * Checks a rule on its own (not against other rules): the shape of `from`
90
+ * and `to` (neither may start with the default locale, which a request is
91
+ * sent away from first), the status, and that an exact `from` isn't a
92
+ * published page.
93
+ */
94
+ export async function checkRedirect(deps, input, look = lookup(deps)) {
95
+ const issues = [];
96
+ const issue = (at, code, messageId, params = {}) => issues.push(contentIssue([at], [t(LABELS[at])], code, messageId, params));
97
+ const from = redirectFrom(deps.registry, input.from);
98
+ if (from === null)
99
+ issue('from', 'invalid', 'issue.redirectFrom', { path: input.from });
100
+ else if (!isPattern(from) && (await isLivePage(deps.registry, look, from))) {
101
+ issue('from', 'path_taken', 'issue.redirectLivePath', { path: from });
102
+ }
103
+ let to = null;
104
+ if (typeof input.to === 'string') {
105
+ const path = cleanPath(input.to.trim());
106
+ const locale = deps.registry.defaultLocale;
107
+ if (path === null)
108
+ issue('to', 'invalid', 'issue.redirectTo', { path: input.to });
109
+ else if (path.split('/')[1] === locale)
110
+ issue('to', 'invalid', 'issue.redirectToDefaultLocale', { path, locale });
111
+ else {
112
+ const allowed = from === null ? [] : paramNames(from);
113
+ const missing = paramNames(path).find((n) => !allowed.includes(n));
114
+ if (missing !== undefined)
115
+ issue('to', 'invalid', 'issue.redirectParam', { name: missing });
116
+ else
117
+ to = { path };
118
+ }
119
+ }
120
+ else {
121
+ const group = input.to.document;
122
+ const target = await deps.db
123
+ .selectFrom('documents')
124
+ .select(['kind', 'type'])
125
+ .where('group_id', '=', group)
126
+ .where('deleted_at', 'is', null)
127
+ .$call(whereVisible(deps.registry))
128
+ .executeTakeFirst();
129
+ if (!target)
130
+ issue('to', 'not_found', 'issue.notFound');
131
+ else if (!isRoutable(deps, target))
132
+ issue('to', 'not_routable', 'issue.notRoutable');
133
+ else
134
+ to = { document: group };
135
+ }
136
+ const status = input.status ?? 301;
137
+ if (status !== 301 && status !== 302)
138
+ issue('status', 'invalid', 'issue.redirectStatus');
139
+ const note = input.note?.trim() || null;
140
+ const rule = from !== null && to !== null && issues.length === 0 ? { from, to, status, note } : null;
141
+ return { rule, issues };
142
+ }
143
+ function columns(to) {
144
+ return 'path' in to ? { to_path: to.path, to_group_id: null } : { to_path: null, to_group_id: to.document };
145
+ }
146
+ function invalid(issues) {
147
+ return new InlayError('validation', 'error.redirectInvalid', {}, { issues });
148
+ }
149
+ const MAX_HOPS = 32;
150
+ /** Starts each sample param value when walking a pattern: it matches any param but no exact rule or page. */
151
+ const SAMPLE = '\u0000';
152
+ function ruleOf(row) {
153
+ return { id: row.id, from: row.from_path, to: rowTarget(row) };
154
+ }
155
+ /**
156
+ * Settles a write of `incoming` rules over `rules` (every other rule, in id order, with any incoming
157
+ * rule that updates one in its place and new ones last, as they would be numbered). Each incoming
158
+ * exact path target is collapsed through exact rules, and existing rules whose target is an incoming
159
+ * exact `from` are re-pointed to where that rule goes. Then every changed rule is walked as requests
160
+ * would follow it. Rules are changed in place; `repointed` lists the existing ones that changed and
161
+ * `loops` the `blame` of every incoming rule whose write would make a loop.
162
+ */
163
+ async function settle(deps, look, rules, incoming) {
164
+ const { registry } = deps;
165
+ const others = registry.locales.filter((l) => l !== registry.defaultLocale);
166
+ const before = ruleIndex(rules);
167
+ const isStop = async (path, state) => (await look(path, state)) !== 'not_found';
168
+ /** Whether a request for `path` reaches its exact rule in every locale and state the rule fires in. */
169
+ const reaches = async (path) => {
170
+ if ((await isStop(path, 'published')) || (await isStop(path, 'preview')))
171
+ return false;
172
+ if (splitLocale(registry, path).locale !== registry.defaultLocale)
173
+ return true;
174
+ for (const l of others) {
175
+ const p = withLocale(registry, l, path);
176
+ if (before.exact.has(p) || (await isStop(p, 'published')) || (await isStop(p, 'preview')))
177
+ return false;
178
+ }
179
+ return true;
180
+ };
181
+ /** Whether a rule from `from` to `path` may be stored as going straight on to `next` instead. */
182
+ const canFollow = async (path, next, from) => sameHop(registry, from, path, next) && (await reaches(path));
183
+ const settled = await collapseTargets(before.exact, incoming, canFollow, (from) => hopScope(registry, from));
184
+ incoming.forEach((r, i) => (r.to = settled[i]));
185
+ const changed = new Set(incoming);
186
+ const blame = new Map(incoming.map((r) => [r, r.blame]));
187
+ const byTarget = new Map();
188
+ for (const r of rules) {
189
+ if (!('path' in r.to) || changed.has(r))
190
+ continue;
191
+ const pointing = byTarget.get(r.to.path) ?? [];
192
+ pointing.push(r);
193
+ byTarget.set(r.to.path, pointing);
194
+ }
195
+ const repointed = [];
196
+ for (const r of incoming) {
197
+ const pointing = byTarget.get(r.from);
198
+ if (!pointing || isPattern(r.from) || !(await reaches(r.from)))
199
+ continue;
200
+ for (const p of pointing) {
201
+ if (('path' in r.to && r.to.path === p.from) || !sameHop(registry, p.from, r.from, r.to))
202
+ continue;
203
+ p.to = r.to;
204
+ repointed.push(p);
205
+ changed.add(p);
206
+ blame.set(p, r.blame);
207
+ }
208
+ }
209
+ const index = ruleIndex(rules);
210
+ const loops = new Set();
211
+ let starts;
212
+ const startsAt = () => (starts ??= startsOf(registry, rules));
213
+ for (const rule of changed) {
214
+ for (const start of walkStarts(registry, rule, startsAt)) {
215
+ const trail = walk(registry, index, rule, start);
216
+ if (trail && !(await someAsync(trail, (p) => isStop(p, 'published')))) {
217
+ loops.add(blame.get(rule));
218
+ break;
219
+ }
220
+ }
221
+ }
222
+ return { repointed, loops };
223
+ }
224
+ /** What `sameHop` depends on in a rule's `from`: its locale, and whether its first segment is a param. */
225
+ function hopScope(registry, from) {
226
+ return `${splitLocale(registry, from).locale}${isPattern(`/${from.split('/')[1] ?? ''}`) ? ':' : ''}`;
227
+ }
228
+ /**
229
+ * Whether a rule from `from` to the exact path `path` sends every request it answers to the same
230
+ * place when it goes straight to `next`, the target of the rule at `path`. A rule matched through
231
+ * the unprefixed rest of a request gives an unprefixed target the request's locale, and a document
232
+ * target resolves in the locale of the request it answers, so the hop through `path` may change that
233
+ * locale. A target that names a locale lands the same either way.
234
+ */
235
+ function sameHop(registry, from, path, next) {
236
+ if ('path' in next && hasLocalePrefix(registry, next.path))
237
+ return true;
238
+ const fromLocale = splitLocale(registry, from).locale;
239
+ const pathLocale = splitLocale(registry, path).locale;
240
+ const fromPrefixed = fromLocale !== registry.defaultLocale;
241
+ const pathPrefixed = pathLocale !== registry.defaultLocale;
242
+ if ('path' in next)
243
+ return fromPrefixed || !pathPrefixed;
244
+ if (hopScope(registry, from).endsWith(':'))
245
+ return false;
246
+ return fromPrefixed ? fromLocale === pathLocale : !pathPrefixed;
247
+ }
248
+ async function someAsync(items, test) {
249
+ for (const item of items)
250
+ if (await test(item))
251
+ return true;
252
+ return false;
253
+ }
254
+ /** The keys `startsOf` files a path under: its segment count, and that with its first segment. */
255
+ function startKeys(path) {
256
+ const parts = path.split('/');
257
+ return [`${parts.length}`, `${parts.length} ${parts[1] ?? ''}`];
258
+ }
259
+ /**
260
+ * The concrete paths a rule's path target can send a request to, in every locale the rule fires in
261
+ * when its `from` is unprefixed, keyed by segment count and by segment count and first segment (of
262
+ * the path, and of its unprefixed rest). A pattern rule can be reached at these as well as at samples.
263
+ */
264
+ function startsOf(registry, rules) {
265
+ const out = new Map();
266
+ const add = (p, key) => {
267
+ for (const k of startKeys(key)) {
268
+ const list = out.get(k) ?? [];
269
+ list.push(p);
270
+ out.set(k, list);
271
+ }
272
+ };
273
+ for (const r of rules) {
274
+ if (!('path' in r.to) || isPattern(r.to.path))
275
+ continue;
276
+ const to = r.to.path;
277
+ const locales = splitLocale(registry, r.from).locale === registry.defaultLocale ? registry.locales : [null];
278
+ for (const l of locales) {
279
+ const p = l === null ? to : localize(registry, l, to);
280
+ add(p, p);
281
+ const { rest } = splitLocale(registry, p);
282
+ if (rest !== p)
283
+ add(p, rest);
284
+ }
285
+ }
286
+ return out;
287
+ }
288
+ /**
289
+ * Where to start walking a rule: its `from` in every locale it fires in (params set to samples, a
290
+ * distinct one per name). A pattern with params in its target also starts at every concrete target
291
+ * it matches, since an exact rule may send such a request on differently from a sample.
292
+ */
293
+ function* walkStarts(registry, rule, starts) {
294
+ const names = paramNames(rule.from);
295
+ const from = fillParams(rule.from, Object.fromEntries(names.map((n) => [n, `${SAMPLE}${n}`])));
296
+ const unprefixed = splitLocale(registry, rule.from).locale === registry.defaultLocale;
297
+ for (const l of unprefixed ? registry.locales : [null])
298
+ yield l === null ? from : withLocale(registry, l, from);
299
+ if (names.length === 0 || !('path' in rule.to) || !isPattern(rule.to.path))
300
+ return;
301
+ const re = patternRegExp(rule.from);
302
+ const [any, literal] = startKeys(rule.from);
303
+ const seen = new Set();
304
+ const firstIsParam = isPattern(`/${rule.from.split('/')[1] ?? ''}`);
305
+ for (const p of starts().get(firstIsParam ? any : literal) ?? []) {
306
+ if (seen.has(p))
307
+ continue;
308
+ seen.add(p);
309
+ const { locale, rest } = splitLocale(registry, p);
310
+ if (re.test(p) || (unprefixed && locale !== registry.defaultLocale && re.test(rest)))
311
+ yield p;
312
+ }
313
+ }
314
+ /**
315
+ * Follows a request for `start` that `rule` answers, matching rules exactly as `resolvePath` does,
316
+ * and gives the paths it passes when it goes round in a loop (or on for more than `MAX_HOPS`), or
317
+ * `null`. It is a loop only if none of those paths is a stop, where a page or a path redirect answers
318
+ * before any rule. A rule that would send `start` to itself is a loop. `null` when `rule` doesn't
319
+ * answer `start`. A document target ends the walk.
320
+ */
321
+ function walk(registry, index, rule, start) {
322
+ const trail = [start];
323
+ const seen = new Set(trail);
324
+ let path = start;
325
+ for (let hops = 0;; hops++) {
326
+ const { locale } = splitLocale(registry, path);
327
+ let to = null;
328
+ for (const candidate of ruleCandidates(registry, index, path)) {
329
+ const target = candidate.rule.to;
330
+ to = 'document' in target ? target : { path: pathTarget(registry, locale, target.path, candidate) };
331
+ if ('path' in to && to.path === path) {
332
+ if (hops === 0 && candidate.rule === rule)
333
+ return trail;
334
+ to = null;
335
+ continue;
336
+ }
337
+ if (hops === 0 && candidate.rule !== rule)
338
+ return null;
339
+ break;
340
+ }
341
+ if (to === null || 'document' in to)
342
+ return null;
343
+ if (seen.has(to.path) || hops >= MAX_HOPS)
344
+ return trail;
345
+ trail.push(to.path);
346
+ seen.add(to.path);
347
+ path = to.path;
348
+ }
349
+ }
350
+ export async function createRedirect(deps, input, ctx) {
351
+ assertCan(ctx, 'redirects.write');
352
+ return withTransaction(deps, async (tdeps) => {
353
+ const trx = tdeps.db;
354
+ const look = lookup(tdeps);
355
+ const { rule, issues } = await checkRedirect(tdeps, input, look);
356
+ if (!rule)
357
+ throw invalid(issues);
358
+ const existing = await trx.selectFrom('redirects').selectAll().orderBy('id').execute();
359
+ if (existing.some((r) => r.from_path === rule.from)) {
360
+ const at = ['from'];
361
+ throw invalid([
362
+ contentIssue(at, [t(LABELS.from)], 'path_taken', 'issue.redirectExists', { path: rule.from }),
363
+ ]);
364
+ }
365
+ const added = { id: null, from: rule.from, to: rule.to, blame: 0 };
366
+ const { repointed, loops } = await settle(tdeps, look, [...existing.map(ruleOf), added], [added]);
367
+ if (loops.size > 0) {
368
+ throw invalid([contentIssue(['to'], [t(LABELS.to)], 'redirect_loop', 'issue.redirectLoop', { path: rule.from })]);
369
+ }
370
+ for (const r of repointed) {
371
+ await trx.updateTable('redirects').set(columns(r.to)).where('id', '=', r.id).execute();
372
+ }
373
+ const created = await trx
374
+ .insertInto('redirects')
375
+ .values({
376
+ from_path: added.from,
377
+ ...columns(added.to),
378
+ status: rule.status,
379
+ source: 'manual',
380
+ note: rule.note,
381
+ created_by_id: ctx.actor.id,
382
+ created_by_name: ctx.actor.name,
383
+ created_at: deps.now().toISOString(),
384
+ })
385
+ .returningAll()
386
+ .executeTakeFirstOrThrow();
387
+ return toRedirect(created);
388
+ });
389
+ }
390
+ export async function deleteRedirect(deps, id, ctx) {
391
+ assertCan(ctx, 'redirects.write');
392
+ const res = await deps.db.deleteFrom('redirects').where('id', '=', id).executeTakeFirst();
393
+ if (Number(res.numDeletedRows) === 0)
394
+ throw new InlayError('not_found', 'error.redirectNotFound');
395
+ }
396
+ /**
397
+ * Called by `publish` inside its transaction, before a document's published
398
+ * slug and path move to its head. When its live URL changes, the old URL
399
+ * gets an automatic 301 to the document, rules that pointed at the old URL
400
+ * follow it to the new one, and an automatic rule from the new URL is dropped.
401
+ * A rule that fires in the document's locale follows the document itself;
402
+ * one that fires elsewhere keeps a path target, so its visitors stay on the
403
+ * same translation rather than switching to the request's locale.
404
+ */
405
+ export async function autoRedirect(deps, ctx, doc) {
406
+ const def = deps.registry.types.get(doc.type);
407
+ if (!def || def.kind !== 'collection' || (def.route === null && !def.customPaths))
408
+ return;
409
+ const newPath = sitePath(deps.registry, def, doc.locale, doc.head_slug, doc.head_path);
410
+ if (newPath !== null) {
411
+ await deps.db.deleteFrom('redirects').where('from_path', '=', newPath).where('source', '=', 'auto').execute();
412
+ }
413
+ if (doc.published_rev === null)
414
+ return;
415
+ const oldPath = sitePath(deps.registry, def, doc.locale, doc.published_slug, doc.published_path);
416
+ if (oldPath === null || oldPath === newPath)
417
+ return;
418
+ const target = columns({ document: doc.group_id });
419
+ await deps.db
420
+ .insertInto('redirects')
421
+ .values({
422
+ from_path: oldPath,
423
+ ...target,
424
+ status: 301,
425
+ source: 'auto',
426
+ note: null,
427
+ created_by_id: ctx.actor.id,
428
+ created_by_name: ctx.actor.name,
429
+ created_at: deps.now().toISOString(),
430
+ })
431
+ .onConflict((oc) => oc.column('from_path').doUpdateSet({ ...target, status: 301, source: 'auto' }))
432
+ .execute();
433
+ const pointing = await deps.db.selectFrom('redirects').select(['id', 'from_path']).where('to_path', '=', oldPath).execute();
434
+ for (const r of pointing) {
435
+ const inLocale = splitLocale(deps.registry, r.from_path).locale === doc.locale;
436
+ const to = newPath === null || inLocale ? target : columns({ path: newPath });
437
+ await deps.db.updateTable('redirects').set(to).where('id', '=', r.id).execute();
438
+ }
439
+ }
440
+ /**
441
+ * Imports `from,to[,status][,note]` rows (with an optional header row). All or
442
+ * nothing: any problem throws `error.redirectImportFailed` with every problem
443
+ * by line. A `from` that already has a rule updates that rule.
444
+ */
445
+ export async function importRedirects(deps, csv, ctx) {
446
+ assertCan(ctx, 'redirects.write');
447
+ const parsed = parseCsv(csv);
448
+ const first = parsed.rows[0];
449
+ const rows = first && first.cells[0]?.trim().toLowerCase() === 'from' ? parsed.rows.slice(1) : parsed.rows;
450
+ return withTransaction(deps, async (tdeps) => {
451
+ const trx = tdeps.db;
452
+ const look = lookup(tdeps);
453
+ const problems = [...parsed.problems];
454
+ const at = (line, message) => problems.push({ line, message: t('import.line', { line, message }) });
455
+ const existing = await trx.selectFrom('redirects').selectAll().orderBy('id').execute();
456
+ const seen = new Map();
457
+ const file = [];
458
+ for (const { line, cells } of rows) {
459
+ if (cells.length < 2 || cells.length > 4) {
460
+ problems.push({ line, message: t('import.columns', { line }) });
461
+ continue;
462
+ }
463
+ const [from = '', to = '', status = '', note = ''] = cells;
464
+ const key = redirectFrom(deps.registry, from);
465
+ const firstLine = key === null ? undefined : seen.get(key);
466
+ if (key !== null && firstLine !== undefined) {
467
+ problems.push({ line, message: t('import.repeated', { line, path: key, first: firstLine }) });
468
+ continue;
469
+ }
470
+ if (key !== null)
471
+ seen.set(key, line);
472
+ const input = {
473
+ from,
474
+ to,
475
+ note,
476
+ ...(status.trim() !== '' && { status: Number(status.trim()) }),
477
+ };
478
+ const { rule, issues } = await checkRedirect(tdeps, input, look);
479
+ for (const issue of issues)
480
+ at(line, issue.message);
481
+ if (rule)
482
+ file.push({ ...rule, line });
483
+ }
484
+ const imported = new Map(file.map((r) => [r.from, r]));
485
+ const incoming = new Map();
486
+ const rules = existing.map((row) => {
487
+ const r = imported.get(row.from_path);
488
+ if (!r)
489
+ return ruleOf(row);
490
+ const rule = { id: row.id, from: r.from, to: r.to, blame: r.line };
491
+ incoming.set(r.from, rule);
492
+ return rule;
493
+ });
494
+ for (const r of file) {
495
+ if (incoming.has(r.from))
496
+ continue;
497
+ const rule = { id: null, from: r.from, to: r.to, blame: r.line };
498
+ incoming.set(r.from, rule);
499
+ rules.push(rule);
500
+ }
501
+ const { repointed, loops } = await settle(tdeps, look, rules, [...incoming.values()]);
502
+ for (const line of loops) {
503
+ const rule = file.find((r) => r.line === line);
504
+ at(line, contentIssue(['to'], [t(LABELS.to)], 'redirect_loop', 'issue.redirectLoop', { path: rule.from }).message);
505
+ }
506
+ if (problems.length > 0) {
507
+ problems.sort((a, b) => a.line - b.line);
508
+ throw new InlayError('validation', 'error.redirectImportFailed', { count: problems.length }, { lines: problems });
509
+ }
510
+ for (const r of repointed) {
511
+ await trx.updateTable('redirects').set(columns(r.to)).where('id', '=', r.id).execute();
512
+ }
513
+ let created = 0;
514
+ let updated = 0;
515
+ for (const r of file) {
516
+ const rule = incoming.get(r.from);
517
+ const target = columns(rule.to);
518
+ if (rule.id === null) {
519
+ await trx
520
+ .insertInto('redirects')
521
+ .values({
522
+ from_path: r.from,
523
+ ...target,
524
+ status: r.status,
525
+ source: 'import',
526
+ note: r.note,
527
+ created_by_id: ctx.actor.id,
528
+ created_by_name: ctx.actor.name,
529
+ created_at: deps.now().toISOString(),
530
+ })
531
+ .execute();
532
+ created++;
533
+ }
534
+ else {
535
+ await trx
536
+ .updateTable('redirects')
537
+ .set({ ...target, status: r.status, note: r.note, source: 'import' })
538
+ .where('id', '=', rule.id)
539
+ .execute();
540
+ updated++;
541
+ }
542
+ }
543
+ return { created, updated };
544
+ });
545
+ }
@@ -0,0 +1,26 @@
1
+ import type { ContentInput } from '../types.js';
2
+ export interface ShippedDocument {
3
+ key: string;
4
+ edition: number;
5
+ /** Keyed by locale. */
6
+ content: Record<string, ContentInput>;
7
+ }
8
+ export interface ShippedContent {
9
+ type: string;
10
+ /** Which constructor made it; `buildRegistry` checks it against the type's kind. */
11
+ via: 'content' | 'entries';
12
+ documents: ShippedDocument[];
13
+ }
14
+ /** How shipped content refers to another shipped document; `key` defaults to `type` for singletons and globals. */
15
+ export type ShippedDocumentRef = {
16
+ type: string;
17
+ key?: string;
18
+ };
19
+ export declare const KEY_PATTERN: RegExp;
20
+ /** Ships a singleton or global. Its key is the type name. */
21
+ export declare function defineContent(type: string, def: {
22
+ edition: number;
23
+ content: Record<string, ContentInput>;
24
+ }): ShippedContent;
25
+ /** Ships entries of a collection, each with a stable key. */
26
+ export declare function defineEntries(type: string, entries: ShippedDocument[]): ShippedContent;
@@ -0,0 +1,9 @@
1
+ export const KEY_PATTERN = /^[a-z0-9]+(-[a-z0-9]+)*$/;
2
+ /** Ships a singleton or global. Its key is the type name. */
3
+ export function defineContent(type, def) {
4
+ return { type, via: 'content', documents: [{ key: type, edition: def.edition, content: def.content }] };
5
+ }
6
+ /** Ships entries of a collection, each with a stable key. */
7
+ export function defineEntries(type, entries) {
8
+ return { type, via: 'entries', documents: entries };
9
+ }