inlaycms 0.0.0-stage → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +462 -2
- package/assets/admin/admin.css +2 -0
- package/assets/admin/admin.js +1 -0
- package/assets/admin/editor.js +125 -0
- package/dist/adapters/admin/activity.d.ts +17 -0
- package/dist/adapters/admin/activity.js +59 -0
- package/dist/adapters/admin/assets.d.ts +14 -0
- package/dist/adapters/admin/assets.js +64 -0
- package/dist/adapters/admin/attention.d.ts +15 -0
- package/dist/adapters/admin/attention.js +34 -0
- package/dist/adapters/admin/context.d.ts +40 -0
- package/dist/adapters/admin/context.js +142 -0
- package/dist/adapters/admin/form/walk.d.ts +61 -0
- package/dist/adapters/admin/form/walk.js +565 -0
- package/dist/adapters/admin/pages.d.ts +38 -0
- package/dist/adapters/admin/pages.js +66 -0
- package/dist/adapters/admin/publish.d.ts +12 -0
- package/dist/adapters/admin/publish.js +195 -0
- package/dist/adapters/admin/routes.d.ts +47 -0
- package/dist/adapters/admin/routes.js +255 -0
- package/dist/adapters/admin/screens/backups.d.ts +11 -0
- package/dist/adapters/admin/screens/backups.js +223 -0
- package/dist/adapters/admin/screens/dashboard.d.ts +4 -0
- package/dist/adapters/admin/screens/dashboard.js +160 -0
- package/dist/adapters/admin/screens/editor.d.ts +67 -0
- package/dist/adapters/admin/screens/editor.js +416 -0
- package/dist/adapters/admin/screens/history.d.ts +4 -0
- package/dist/adapters/admin/screens/history.js +105 -0
- package/dist/adapters/admin/screens/lists.d.ts +4 -0
- package/dist/adapters/admin/screens/lists.js +297 -0
- package/dist/adapters/admin/screens/media.d.ts +20 -0
- package/dist/adapters/admin/screens/media.js +186 -0
- package/dist/adapters/admin/screens/pickers.d.ts +29 -0
- package/dist/adapters/admin/screens/pickers.js +134 -0
- package/dist/adapters/admin/screens/preview-pane.d.ts +10 -0
- package/dist/adapters/admin/screens/preview-pane.js +74 -0
- package/dist/adapters/admin/screens/redirects.d.ts +4 -0
- package/dist/adapters/admin/screens/redirects.js +137 -0
- package/dist/adapters/admin/screens/releases.d.ts +7 -0
- package/dist/adapters/admin/screens/releases.js +287 -0
- package/dist/adapters/admin/screens/search.d.ts +22 -0
- package/dist/adapters/admin/screens/search.js +113 -0
- package/dist/adapters/admin/screens/security.d.ts +4 -0
- package/dist/adapters/admin/screens/security.js +85 -0
- package/dist/adapters/admin/screens/users.d.ts +4 -0
- package/dist/adapters/admin/screens/users.js +330 -0
- package/dist/adapters/admin/theme.d.ts +1 -0
- package/dist/adapters/admin/theme.js +1 -0
- package/dist/adapters/admin/translations.d.ts +16 -0
- package/dist/adapters/admin/translations.js +23 -0
- package/dist/adapters/admin/upload.d.ts +8 -0
- package/dist/adapters/admin/upload.js +116 -0
- package/dist/adapters/admin/views/data-table.d.ts +82 -0
- package/dist/adapters/admin/views/data-table.js +117 -0
- package/dist/adapters/admin/views/icons.d.ts +48 -0
- package/dist/adapters/admin/views/icons.js +47 -0
- package/dist/adapters/admin/views/layout.d.ts +51 -0
- package/dist/adapters/admin/views/layout.js +145 -0
- package/dist/adapters/admin/views/sheet.d.ts +15 -0
- package/dist/adapters/admin/views/sheet.js +6 -0
- package/dist/adapters/admin/views/status.d.ts +32 -0
- package/dist/adapters/admin/views/status.js +74 -0
- package/dist/adapters/auth/handler.d.ts +10 -0
- package/dist/adapters/auth/handler.js +63 -0
- package/dist/adapters/auth/request.d.ts +8 -0
- package/dist/adapters/auth/request.js +14 -0
- package/dist/adapters/cli/bin.d.ts +2 -0
- package/dist/adapters/cli/bin.js +42 -0
- package/dist/adapters/cli/main.d.ts +7 -0
- package/dist/adapters/cli/main.js +982 -0
- package/dist/adapters/cli/remote.d.ts +52 -0
- package/dist/adapters/cli/remote.js +161 -0
- package/dist/adapters/mcp/agent-refs.d.ts +9 -0
- package/dist/adapters/mcp/agent-refs.js +25 -0
- package/dist/adapters/mcp/documents.d.ts +15 -0
- package/dist/adapters/mcp/documents.js +26 -0
- package/dist/adapters/mcp/fetch-url.d.ts +19 -0
- package/dist/adapters/mcp/fetch-url.js +244 -0
- package/dist/adapters/mcp/request-state.d.ts +15 -0
- package/dist/adapters/mcp/request-state.js +38 -0
- package/dist/adapters/mcp/results.d.ts +20 -0
- package/dist/adapters/mcp/results.js +61 -0
- package/dist/adapters/mcp/route.d.ts +34 -0
- package/dist/adapters/mcp/route.js +52 -0
- package/dist/adapters/mcp/server.d.ts +44 -0
- package/dist/adapters/mcp/server.js +49 -0
- package/dist/adapters/mcp/tools/history.d.ts +5 -0
- package/dist/adapters/mcp/tools/history.js +84 -0
- package/dist/adapters/mcp/tools/media.d.ts +5 -0
- package/dist/adapters/mcp/tools/media.js +102 -0
- package/dist/adapters/mcp/tools/publish.d.ts +5 -0
- package/dist/adapters/mcp/tools/publish.js +157 -0
- package/dist/adapters/mcp/tools/read.d.ts +5 -0
- package/dist/adapters/mcp/tools/read.js +279 -0
- package/dist/adapters/mcp/tools/redirects.d.ts +5 -0
- package/dist/adapters/mcp/tools/redirects.js +74 -0
- package/dist/adapters/mcp/tools/write.d.ts +5 -0
- package/dist/adapters/mcp/tools/write.js +304 -0
- package/dist/adapters/site/backup-schedule.d.ts +8 -0
- package/dist/adapters/site/backup-schedule.js +31 -0
- package/dist/adapters/site/cache.d.ts +29 -0
- package/dist/adapters/site/cache.js +63 -0
- package/dist/adapters/site/context.d.ts +29 -0
- package/dist/adapters/site/context.js +278 -0
- package/dist/adapters/site/head.d.ts +23 -0
- package/dist/adapters/site/head.js +44 -0
- package/dist/adapters/site/limiter.d.ts +17 -0
- package/dist/adapters/site/limiter.js +39 -0
- package/dist/adapters/site/lock.d.ts +30 -0
- package/dist/adapters/site/lock.js +143 -0
- package/dist/adapters/site/media-routes.d.ts +4 -0
- package/dist/adapters/site/media-routes.js +55 -0
- package/dist/adapters/site/plugin.d.ts +122 -0
- package/dist/adapters/site/plugin.js +436 -0
- package/dist/adapters/site/preview.d.ts +36 -0
- package/dist/adapters/site/preview.js +82 -0
- package/dist/adapters/site/public.d.ts +6 -0
- package/dist/adapters/site/public.js +87 -0
- package/dist/adapters/site/render.d.ts +45 -0
- package/dist/adapters/site/render.js +191 -0
- package/dist/adapters/site/routes.d.ts +17 -0
- package/dist/adapters/site/routes.js +125 -0
- package/dist/adapters/site/sitemap.d.ts +10 -0
- package/dist/adapters/site/sitemap.js +10 -0
- package/dist/adapters/web.d.ts +22 -0
- package/dist/adapters/web.js +75 -0
- package/dist/config.d.ts +21 -0
- package/dist/config.js +58 -0
- package/dist/core/agent/format.d.ts +62 -0
- package/dist/core/agent/format.js +211 -0
- package/dist/core/agent/schema.d.ts +6 -0
- package/dist/core/agent/schema.js +86 -0
- package/dist/core/archive.d.ts +12 -0
- package/dist/core/archive.js +72 -0
- package/dist/core/auth/auth.d.ts +4911 -0
- package/dist/core/auth/auth.js +61 -0
- package/dist/core/auth/roles.d.ts +10 -0
- package/dist/core/auth/roles.js +38 -0
- package/dist/core/auth/service.d.ts +118 -0
- package/dist/core/auth/service.js +424 -0
- package/dist/core/auth/upload-token.d.ts +8 -0
- package/dist/core/auth/upload-token.js +27 -0
- package/dist/core/backup/backup.d.ts +28 -0
- package/dist/core/backup/backup.js +126 -0
- package/dist/core/backup/restore.d.ts +41 -0
- package/dist/core/backup/restore.js +264 -0
- package/dist/core/backup/schedule.d.ts +12 -0
- package/dist/core/backup/schedule.js +29 -0
- package/dist/core/boot.d.ts +63 -0
- package/dist/core/boot.js +141 -0
- package/dist/core/check.d.ts +32 -0
- package/dist/core/check.js +111 -0
- package/dist/core/content/activity.d.ts +8 -0
- package/dist/core/content/activity.js +101 -0
- package/dist/core/content/delete.d.ts +5 -0
- package/dist/core/content/delete.js +100 -0
- package/dist/core/content/diff.d.ts +6 -0
- package/dist/core/content/diff.js +129 -0
- package/dist/core/content/dry-run.d.ts +24 -0
- package/dist/core/content/dry-run.js +69 -0
- package/dist/core/content/gaps.d.ts +25 -0
- package/dist/core/content/gaps.js +50 -0
- package/dist/core/content/hidden.d.ts +33 -0
- package/dist/core/content/hidden.js +103 -0
- package/dist/core/content/history.d.ts +15 -0
- package/dist/core/content/history.js +62 -0
- package/dist/core/content/patch.d.ts +40 -0
- package/dist/core/content/patch.js +287 -0
- package/dist/core/content/path.d.ts +6 -0
- package/dist/core/content/path.js +38 -0
- package/dist/core/content/publish.d.ts +26 -0
- package/dist/core/content/publish.js +211 -0
- package/dist/core/content/read.d.ts +99 -0
- package/dist/core/content/read.js +141 -0
- package/dist/core/content/reader.d.ts +37 -0
- package/dist/core/content/reader.js +195 -0
- package/dist/core/content/refs.d.ts +44 -0
- package/dist/core/content/refs.js +161 -0
- package/dist/core/content/release-migrations.d.ts +12 -0
- package/dist/core/content/release-migrations.js +154 -0
- package/dist/core/content/save.d.ts +25 -0
- package/dist/core/content/save.js +87 -0
- package/dist/core/content/schema.d.ts +22 -0
- package/dist/core/content/schema.js +127 -0
- package/dist/core/content/search.d.ts +8 -0
- package/dist/core/content/search.js +89 -0
- package/dist/core/content/shipped-input.d.ts +39 -0
- package/dist/core/content/shipped-input.js +221 -0
- package/dist/core/content/shipped.d.ts +112 -0
- package/dist/core/content/shipped.js +437 -0
- package/dist/core/content/slug.d.ts +23 -0
- package/dist/core/content/slug.js +182 -0
- package/dist/core/content/stats.d.ts +11 -0
- package/dist/core/content/stats.js +30 -0
- package/dist/core/content/translate.d.ts +3 -0
- package/dist/core/content/translate.js +68 -0
- package/dist/core/content/usages.d.ts +13 -0
- package/dist/core/content/usages.js +70 -0
- package/dist/core/content/validate.d.ts +21 -0
- package/dist/core/content/validate.js +124 -0
- package/dist/core/content/write.d.ts +10 -0
- package/dist/core/content/write.js +93 -0
- package/dist/core/core.d.ts +227 -0
- package/dist/core/core.js +251 -0
- package/dist/core/db/connection.d.ts +6 -0
- package/dist/core/db/connection.js +12 -0
- package/dist/core/db/migrate.d.ts +9 -0
- package/dist/core/db/migrate.js +42 -0
- package/dist/core/db/migrations/0001_content.d.ts +2 -0
- package/dist/core/db/migrations/0001_content.js +69 -0
- package/dist/core/db/migrations/0002_media.d.ts +2 -0
- package/dist/core/db/migrations/0002_media.js +26 -0
- package/dist/core/db/migrations/0003_redirects.d.ts +2 -0
- package/dist/core/db/migrations/0003_redirects.js +19 -0
- package/dist/core/db/migrations/0004_token_cutoffs.d.ts +3 -0
- package/dist/core/db/migrations/0004_token_cutoffs.js +8 -0
- package/dist/core/db/migrations/0005_site_lock.d.ts +3 -0
- package/dist/core/db/migrations/0005_site_lock.js +10 -0
- package/dist/core/db/migrations/0006_releases.d.ts +3 -0
- package/dist/core/db/migrations/0006_releases.js +34 -0
- package/dist/core/db/schema.d.ts +155 -0
- package/dist/core/db/schema.js +1 -0
- package/dist/core/errors.d.ts +27 -0
- package/dist/core/errors.js +15 -0
- package/dist/core/export/export.d.ts +17 -0
- package/dist/core/export/export.js +185 -0
- package/dist/core/export/format.d.ts +241 -0
- package/dist/core/export/format.js +106 -0
- package/dist/core/export/import.d.ts +54 -0
- package/dist/core/export/import.js +180 -0
- package/dist/core/export/read.d.ts +16 -0
- package/dist/core/export/read.js +234 -0
- package/dist/core/export/replace.d.ts +17 -0
- package/dist/core/export/replace.js +36 -0
- package/dist/core/export/report.d.ts +55 -0
- package/dist/core/export/report.js +50 -0
- package/dist/core/export/schema.d.ts +8 -0
- package/dist/core/export/schema.js +47 -0
- package/dist/core/export/stage.d.ts +25 -0
- package/dist/core/export/stage.js +192 -0
- package/dist/core/fields/builtins.d.ts +70 -0
- package/dist/core/fields/builtins.js +335 -0
- package/dist/core/fields/define.d.ts +86 -0
- package/dist/core/fields/define.js +34 -0
- package/dist/core/fields/walk.d.ts +49 -0
- package/dist/core/fields/walk.js +270 -0
- package/dist/core/fields/widgets.d.ts +27 -0
- package/dist/core/fields/widgets.js +330 -0
- package/dist/core/fields/zod-internals.d.ts +29 -0
- package/dist/core/fields/zod-internals.js +28 -0
- package/dist/core/media/detect.d.ts +5 -0
- package/dist/core/media/detect.js +66 -0
- package/dist/core/media/labels.d.ts +6 -0
- package/dist/core/media/labels.js +17 -0
- package/dist/core/media/library.d.ts +45 -0
- package/dist/core/media/library.js +239 -0
- package/dist/core/media/store.d.ts +11 -0
- package/dist/core/media/store.js +49 -0
- package/dist/core/media/variants.d.ts +10 -0
- package/dist/core/media/variants.js +59 -0
- package/dist/core/messages/en.d.ts +1278 -0
- package/dist/core/messages/en.js +1287 -0
- package/dist/core/messages/index.d.ts +7 -0
- package/dist/core/messages/index.js +14 -0
- package/dist/core/preview.d.ts +16 -0
- package/dist/core/preview.js +42 -0
- package/dist/core/redirects/collapse.d.ts +68 -0
- package/dist/core/redirects/collapse.js +153 -0
- package/dist/core/redirects/csv.d.ts +15 -0
- package/dist/core/redirects/csv.js +68 -0
- package/dist/core/redirects/redirects.d.ts +58 -0
- package/dist/core/redirects/redirects.js +545 -0
- package/dist/core/registry/content.d.ts +26 -0
- package/dist/core/registry/content.js +9 -0
- package/dist/core/registry/define.d.ts +145 -0
- package/dist/core/registry/define.js +41 -0
- package/dist/core/registry/migration.d.ts +44 -0
- package/dist/core/registry/migration.js +4 -0
- package/dist/core/registry/registry.d.ts +18 -0
- package/dist/core/registry/registry.js +255 -0
- package/dist/core/registry/render.d.ts +98 -0
- package/dist/core/registry/render.js +1 -0
- package/dist/core/registry/route.d.ts +42 -0
- package/dist/core/registry/route.js +3 -0
- package/dist/core/release/bundle.d.ts +21 -0
- package/dist/core/release/bundle.js +252 -0
- package/dist/core/release/keys.d.ts +11 -0
- package/dist/core/release/keys.js +60 -0
- package/dist/core/release/manifest.d.ts +103 -0
- package/dist/core/release/manifest.js +138 -0
- package/dist/core/release/pack.d.ts +20 -0
- package/dist/core/release/pack.js +97 -0
- package/dist/core/release/pointer.d.ts +19 -0
- package/dist/core/release/pointer.js +53 -0
- package/dist/core/release/read.d.ts +59 -0
- package/dist/core/release/read.js +256 -0
- package/dist/core/report.d.ts +31 -0
- package/dist/core/report.js +37 -0
- package/dist/core/richtext/html.d.ts +15 -0
- package/dist/core/richtext/html.js +81 -0
- package/dist/core/richtext/markdown.d.ts +19 -0
- package/dist/core/richtext/markdown.js +951 -0
- package/dist/core/richtext/schema.d.ts +98 -0
- package/dist/core/richtext/schema.js +275 -0
- package/dist/core/site/live.d.ts +56 -0
- package/dist/core/site/live.js +145 -0
- package/dist/core/site/paths.d.ts +15 -0
- package/dist/core/site/paths.js +64 -0
- package/dist/core/site/resolve.d.ts +9 -0
- package/dist/core/site/resolve.js +202 -0
- package/dist/core/site/sitemap.d.ts +11 -0
- package/dist/core/site/sitemap.js +24 -0
- package/dist/core/site-lock/site-lock.d.ts +21 -0
- package/dist/core/site-lock/site-lock.js +65 -0
- package/dist/core/site-lock/token.d.ts +7 -0
- package/dist/core/site-lock/token.js +24 -0
- package/dist/core/theme.d.ts +22 -0
- package/dist/core/theme.js +133 -0
- package/dist/core/types.d.ts +253 -0
- package/dist/core/types.js +1 -0
- package/dist/core/version.d.ts +1 -0
- package/dist/core/version.js +5 -0
- package/dist/host/activate.d.ts +53 -0
- package/dist/host/activate.js +162 -0
- package/dist/host/app.d.ts +46 -0
- package/dist/host/app.js +129 -0
- package/dist/host/bin.d.ts +5 -0
- package/dist/host/bin.js +8 -0
- package/dist/host/config.d.ts +36 -0
- package/dist/host/config.js +152 -0
- package/dist/host/dev.d.ts +10 -0
- package/dist/host/dev.js +293 -0
- package/dist/host/diff.d.ts +78 -0
- package/dist/host/diff.js +346 -0
- package/dist/host/hooks.d.ts +41 -0
- package/dist/host/hooks.js +49 -0
- package/dist/host/ipc.d.ts +123 -0
- package/dist/host/ipc.js +61 -0
- package/dist/host/preview-proxy.d.ts +24 -0
- package/dist/host/preview-proxy.js +154 -0
- package/dist/host/preview-worker.d.ts +42 -0
- package/dist/host/preview-worker.js +242 -0
- package/dist/host/primary.d.ts +38 -0
- package/dist/host/primary.js +357 -0
- package/dist/host/push.d.ts +125 -0
- package/dist/host/push.js +420 -0
- package/dist/host/serve.d.ts +16 -0
- package/dist/host/serve.js +150 -0
- package/dist/host/store.d.ts +48 -0
- package/dist/host/store.js +109 -0
- package/dist/host/worker.d.ts +67 -0
- package/dist/host/worker.js +310 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.js +32 -0
- package/dist/jsx/html.d.ts +16 -0
- package/dist/jsx/html.js +65 -0
- package/dist/jsx/jsx-dev-runtime.d.ts +4 -0
- package/dist/jsx/jsx-dev-runtime.js +5 -0
- package/dist/jsx/jsx-runtime.d.ts +17 -0
- package/dist/jsx/jsx-runtime.js +41 -0
- package/dist/site.d.ts +17 -0
- package/dist/site.js +14 -0
- package/package.json +111 -3
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { readdir, stat } from 'node:fs/promises';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { systemCtx } from './auth/roles.js';
|
|
4
|
+
import { createAuthService } from './auth/service.js';
|
|
5
|
+
import { createBackup } from './backup/backup.js';
|
|
6
|
+
import { createCore } from './core.js';
|
|
7
|
+
import { openDatabase } from './db/connection.js';
|
|
8
|
+
import { migrateSystem, pendingSystemMigrations } from './db/migrate.js';
|
|
9
|
+
import { InlayError } from './errors.js';
|
|
10
|
+
import { t } from './messages/index.js';
|
|
11
|
+
import { formatReport } from './report.js';
|
|
12
|
+
/**
|
|
13
|
+
* Spec §5 steps 3–4 on an open database: system migrations, better-auth's
|
|
14
|
+
* migrations when `auth` is given, ref repair, then shipped content
|
|
15
|
+
* (`validateAll` checks shipped documents already applied too; `content:
|
|
16
|
+
* 'none'` skips shipped content). Stored content is never rewritten: it's
|
|
17
|
+
* read against the current schema. Problems go into the report; nothing here
|
|
18
|
+
* throws for them.
|
|
19
|
+
*/
|
|
20
|
+
export async function upgradeDatabase(db, core, systemMigrations, auth, opts = {}) {
|
|
21
|
+
await migrateSystem(db);
|
|
22
|
+
if (auth)
|
|
23
|
+
await auth.migrate();
|
|
24
|
+
const report = {
|
|
25
|
+
ok: true,
|
|
26
|
+
systemMigrations,
|
|
27
|
+
shipped: { created: [], replaced: [], skipped: [], deletedRefs: [], warnings: [] },
|
|
28
|
+
problems: [],
|
|
29
|
+
warnings: [],
|
|
30
|
+
};
|
|
31
|
+
await core.repairRefs();
|
|
32
|
+
if (opts.content !== 'none') {
|
|
33
|
+
try {
|
|
34
|
+
report.shipped = await core.applyShippedContent(opts.validateAll !== undefined ? { validateAll: opts.validateAll } : {});
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
if (!(e instanceof InlayError) || e.code !== 'shipped_content')
|
|
38
|
+
throw e;
|
|
39
|
+
report.ok = false;
|
|
40
|
+
report.problems.push({ code: 'shipped_content', message: e.message, ...(e.details.issues && { issues: e.details.issues }) });
|
|
41
|
+
return report;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
for (const { document: d, slug, path } of await core.shadowedEntries()) {
|
|
45
|
+
report.warnings.push({
|
|
46
|
+
code: 'slug_shadowed',
|
|
47
|
+
message: t('report.slugShadowed', { title: d.title, locale: d.locale, slug, path }),
|
|
48
|
+
document: { id: d.id, type: d.type, locale: d.locale, title: d.title },
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
return report;
|
|
52
|
+
}
|
|
53
|
+
/** True for a database that has never been set up: no content tables yet. */
|
|
54
|
+
export async function isFresh(db) {
|
|
55
|
+
const tables = await db.introspection.getTables();
|
|
56
|
+
return !tables.some((table) => table.name === 'documents');
|
|
57
|
+
}
|
|
58
|
+
async function mtime(path) {
|
|
59
|
+
try {
|
|
60
|
+
return (await stat(path)).mtimeMs;
|
|
61
|
+
}
|
|
62
|
+
catch (e) {
|
|
63
|
+
if (e.code === 'ENOENT')
|
|
64
|
+
return null;
|
|
65
|
+
throw e;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/** When the database file or its -wal last changed. Must run before the database is opened, since opening creates a fresh -wal. */
|
|
69
|
+
async function lastChanged(db) {
|
|
70
|
+
const times = [await mtime(db), await mtime(`${db}-wal`)].filter((t) => t !== null);
|
|
71
|
+
return times.length > 0 ? Math.max(...times) : null;
|
|
72
|
+
}
|
|
73
|
+
/** When the newest pre-migration backup was written, or null if there is none. */
|
|
74
|
+
async function lastPreMigrationBackup(backups) {
|
|
75
|
+
let names;
|
|
76
|
+
try {
|
|
77
|
+
names = await readdir(backups);
|
|
78
|
+
}
|
|
79
|
+
catch (e) {
|
|
80
|
+
if (e.code === 'ENOENT')
|
|
81
|
+
return null;
|
|
82
|
+
throw e;
|
|
83
|
+
}
|
|
84
|
+
let newest = null;
|
|
85
|
+
for (const name of names.filter((n) => n.endsWith('-pre-migration.tar.gz'))) {
|
|
86
|
+
const time = await mtime(join(backups, name));
|
|
87
|
+
if (time !== null && (newest === null || time > newest))
|
|
88
|
+
newest = time;
|
|
89
|
+
}
|
|
90
|
+
return newest;
|
|
91
|
+
}
|
|
92
|
+
/** True if a pre-migration backup was written after the database last changed. */
|
|
93
|
+
async function backedUpSince(backups, changed) {
|
|
94
|
+
if (changed === null)
|
|
95
|
+
return false;
|
|
96
|
+
const backup = await lastPreMigrationBackup(backups);
|
|
97
|
+
return backup !== null && backup > changed;
|
|
98
|
+
}
|
|
99
|
+
export async function bootInlay(opts) {
|
|
100
|
+
const changed = await lastChanged(opts.db);
|
|
101
|
+
const db = openDatabase(opts.db);
|
|
102
|
+
try {
|
|
103
|
+
const core = createCore({
|
|
104
|
+
site: opts.site,
|
|
105
|
+
db,
|
|
106
|
+
...(opts.media !== undefined && { media: opts.media }),
|
|
107
|
+
backups: opts.backups,
|
|
108
|
+
...(opts.uploads && { uploads: opts.uploads }),
|
|
109
|
+
...(opts.now && { now: opts.now }),
|
|
110
|
+
...(opts.shippedFiles && { shippedFiles: opts.shippedFiles }),
|
|
111
|
+
});
|
|
112
|
+
const auth = opts.auth ? createAuthService({ db, ...opts.auth }) : null;
|
|
113
|
+
const systemMigrations = await pendingSystemMigrations(db);
|
|
114
|
+
let backup = null;
|
|
115
|
+
// A boot that keeps failing would otherwise add a full backup on every retry.
|
|
116
|
+
if (!(await isFresh(db)) &&
|
|
117
|
+
(systemMigrations.length > 0 || (await auth?.migrationsPending())) &&
|
|
118
|
+
!(await backedUpSince(opts.backups, changed))) {
|
|
119
|
+
backup = await createBackup({
|
|
120
|
+
db,
|
|
121
|
+
backups: opts.backups,
|
|
122
|
+
...(opts.media !== undefined && { media: opts.media }),
|
|
123
|
+
reason: 'pre-migration',
|
|
124
|
+
ctx: systemCtx('system'),
|
|
125
|
+
...(opts.now && { now: opts.now }),
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
const report = await upgradeDatabase(db, core, systemMigrations, auth ?? undefined, {
|
|
129
|
+
...(opts.content && { content: opts.content }),
|
|
130
|
+
});
|
|
131
|
+
if (report.problems.length > 0) {
|
|
132
|
+
const list = formatReport(report).join('\n');
|
|
133
|
+
throw new InlayError('check_failed', 'error.checkFailed', { count: report.problems.length, list }, { problems: report.problems });
|
|
134
|
+
}
|
|
135
|
+
return { core, db, report, backup, auth };
|
|
136
|
+
}
|
|
137
|
+
catch (e) {
|
|
138
|
+
await db.destroy();
|
|
139
|
+
throw e;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { SiteDefinition } from './registry/define.js';
|
|
2
|
+
import type { CheckProblem, CheckReport } from './report.js';
|
|
3
|
+
/**
|
|
4
|
+
* Runs the boot pipeline (system migrations, shipped content) on a throwaway
|
|
5
|
+
* snapshot of the database at `opts.db`, and reports what would happen.
|
|
6
|
+
* Every shipped document is checked, including those at editions already
|
|
7
|
+
* applied, which boot leaves alone; a shipped slug or path that is already taken is a problem here, though
|
|
8
|
+
* activation resolves it with a warning. Shipped-content files are imported into a throwaway media
|
|
9
|
+
* directory. Never writes to the given file. Returns `ok: false` with
|
|
10
|
+
* the problems instead of throwing.
|
|
11
|
+
*/
|
|
12
|
+
export declare function checkDatabase(opts: {
|
|
13
|
+
site: SiteDefinition;
|
|
14
|
+
db: string;
|
|
15
|
+
now?: () => Date;
|
|
16
|
+
}): Promise<CheckReport>;
|
|
17
|
+
/** The report of a check that looked at no database. */
|
|
18
|
+
export declare function emptyCheckReport(): CheckReport;
|
|
19
|
+
/**
|
|
20
|
+
* Checks `site` against a fresh, empty database, so its shipped content is
|
|
21
|
+
* validated when there is no database to check. Reports only problems and
|
|
22
|
+
* warnings: what a fresh database would get isn't news.
|
|
23
|
+
*/
|
|
24
|
+
export declare function checkWithoutDatabase(opts: {
|
|
25
|
+
site: SiteDefinition;
|
|
26
|
+
now?: () => Date;
|
|
27
|
+
}): Promise<CheckReport>;
|
|
28
|
+
/**
|
|
29
|
+
* Bundles the site at `configFile` the way `inlaycms pack` does, into a throwaway file, and reports an import a
|
|
30
|
+
* release can't hold, or code it refuses, as a problem. Other bundling failures throw.
|
|
31
|
+
*/
|
|
32
|
+
export declare function checkBundle(configFile: string): Promise<CheckProblem[]>;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { copyFile, mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { snapshotDatabase } from './backup/backup.js';
|
|
6
|
+
import { upgradeDatabase } from './boot.js';
|
|
7
|
+
import { createCore } from './core.js';
|
|
8
|
+
import { openDatabase } from './db/connection.js';
|
|
9
|
+
import { pendingSystemMigrations } from './db/migrate.js';
|
|
10
|
+
import { InlayError } from './errors.js';
|
|
11
|
+
import { bundleSite } from './release/bundle.js';
|
|
12
|
+
/**
|
|
13
|
+
* Runs the boot pipeline (system migrations, shipped content) on a throwaway
|
|
14
|
+
* snapshot of the database at `opts.db`, and reports what would happen.
|
|
15
|
+
* Every shipped document is checked, including those at editions already
|
|
16
|
+
* applied, which boot leaves alone; a shipped slug or path that is already taken is a problem here, though
|
|
17
|
+
* activation resolves it with a warning. Shipped-content files are imported into a throwaway media
|
|
18
|
+
* directory. Never writes to the given file. Returns `ok: false` with
|
|
19
|
+
* the problems instead of throwing.
|
|
20
|
+
*/
|
|
21
|
+
export async function checkDatabase(opts) {
|
|
22
|
+
if (!existsSync(opts.db))
|
|
23
|
+
throw new InlayError('not_found', 'error.noDatabase', { path: opts.db });
|
|
24
|
+
const dir = await mkdtemp(join(tmpdir(), 'inlay-check-'));
|
|
25
|
+
try {
|
|
26
|
+
const snapshot = join(dir, 'inlay.db');
|
|
27
|
+
if (existsSync(`${opts.db}-wal`)) {
|
|
28
|
+
const source = openDatabase(opts.db, { readonly: true });
|
|
29
|
+
try {
|
|
30
|
+
await snapshotDatabase(source, snapshot);
|
|
31
|
+
}
|
|
32
|
+
finally {
|
|
33
|
+
await source.destroy();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
// With no -wal file the main file holds every committed page, so a byte
|
|
38
|
+
// copy is the snapshot. Opening it with SQLite, even readonly, would
|
|
39
|
+
// create -wal/-shm beside it (and fail in a read-only directory); the
|
|
40
|
+
// bundled SQLite is built without URI support, so `?immutable=1` isn't
|
|
41
|
+
// available.
|
|
42
|
+
await copyFile(opts.db, snapshot);
|
|
43
|
+
}
|
|
44
|
+
const db = openDatabase(snapshot);
|
|
45
|
+
try {
|
|
46
|
+
const core = createCore({ site: opts.site, db, media: join(dir, 'media'), ...(opts.now && { now: opts.now }) });
|
|
47
|
+
const report = await upgradeDatabase(db, core, await pendingSystemMigrations(db), undefined, { validateAll: true });
|
|
48
|
+
// Activation resolves a slug or path that is taken and goes ahead; a check asks for it to be fixed.
|
|
49
|
+
const warnings = report.shipped.warnings ?? [];
|
|
50
|
+
if (warnings.length === 0)
|
|
51
|
+
return report;
|
|
52
|
+
for (const w of warnings)
|
|
53
|
+
report.problems.push({ code: 'shipped_content', message: w.message });
|
|
54
|
+
return { ...report, ok: false, shipped: { ...report.shipped, warnings: [] } };
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
await db.destroy();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
finally {
|
|
61
|
+
await rm(dir, { recursive: true, force: true });
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/** The report of a check that looked at no database. */
|
|
65
|
+
export function emptyCheckReport() {
|
|
66
|
+
return {
|
|
67
|
+
ok: true,
|
|
68
|
+
systemMigrations: [],
|
|
69
|
+
shipped: { created: [], replaced: [], skipped: [], deletedRefs: [], warnings: [] },
|
|
70
|
+
problems: [],
|
|
71
|
+
warnings: [],
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Checks `site` against a fresh, empty database, so its shipped content is
|
|
76
|
+
* validated when there is no database to check. Reports only problems and
|
|
77
|
+
* warnings: what a fresh database would get isn't news.
|
|
78
|
+
*/
|
|
79
|
+
export async function checkWithoutDatabase(opts) {
|
|
80
|
+
const dir = await mkdtemp(join(tmpdir(), 'inlay-check-empty-'));
|
|
81
|
+
try {
|
|
82
|
+
const db = join(dir, 'empty.db');
|
|
83
|
+
await writeFile(db, '');
|
|
84
|
+
const report = await checkDatabase({ ...opts, db });
|
|
85
|
+
return { ...emptyCheckReport(), ok: report.ok, problems: report.problems, warnings: report.warnings };
|
|
86
|
+
}
|
|
87
|
+
finally {
|
|
88
|
+
await rm(dir, { recursive: true, force: true });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Bundles the site at `configFile` the way `inlaycms pack` does, into a throwaway file, and reports an import a
|
|
93
|
+
* release can't hold, or code it refuses, as a problem. Other bundling failures throw.
|
|
94
|
+
*/
|
|
95
|
+
export async function checkBundle(configFile) {
|
|
96
|
+
const dir = await mkdtemp(join(tmpdir(), 'inlay-check-bundle-'));
|
|
97
|
+
try {
|
|
98
|
+
await bundleSite(configFile, join(dir, 'site.js'));
|
|
99
|
+
return [];
|
|
100
|
+
}
|
|
101
|
+
catch (e) {
|
|
102
|
+
if (e instanceof InlayError && e.messageId === 'error.packImport')
|
|
103
|
+
return [{ code: 'release_import', message: e.message }];
|
|
104
|
+
if (e instanceof InlayError && e.messageId === 'error.packForbidden')
|
|
105
|
+
return [{ code: 'release_forbidden', message: e.message }];
|
|
106
|
+
throw e;
|
|
107
|
+
}
|
|
108
|
+
finally {
|
|
109
|
+
await rm(dir, { recursive: true, force: true });
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Activity, CoreDeps } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* What happened lately, newest first: edits (a run of edits to one document by one author through one channel counts
|
|
4
|
+
* as one item) and publish events, for documents that are visible and not deleted. System writes are left out.
|
|
5
|
+
*/
|
|
6
|
+
export declare function recentActivity(deps: CoreDeps, opts?: {
|
|
7
|
+
limit?: number;
|
|
8
|
+
}): Promise<Activity[]>;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { summaryQuery, toSummary, whereVisible } from './read.js';
|
|
2
|
+
const DEFAULT_LIMIT = 20;
|
|
3
|
+
const MAX_LIMIT = 100;
|
|
4
|
+
const CHUNK = 400;
|
|
5
|
+
const HIDDEN_SOURCES = ['system'];
|
|
6
|
+
/** The visible, not deleted documents among `ids`, by id. */
|
|
7
|
+
async function summariesOf(deps, ids) {
|
|
8
|
+
const found = new Map();
|
|
9
|
+
for (let i = 0; i < ids.length; i += CHUNK) {
|
|
10
|
+
const rows = await summaryQuery(deps)
|
|
11
|
+
.where('documents.id', 'in', ids.slice(i, i + CHUNK))
|
|
12
|
+
.where('documents.deleted_at', 'is', null)
|
|
13
|
+
.execute();
|
|
14
|
+
for (const row of rows)
|
|
15
|
+
found.set(row.id, toSummary(deps, row));
|
|
16
|
+
}
|
|
17
|
+
return found;
|
|
18
|
+
}
|
|
19
|
+
function publishedItems(json) {
|
|
20
|
+
let parsed;
|
|
21
|
+
try {
|
|
22
|
+
parsed = JSON.parse(json);
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
27
|
+
if (!Array.isArray(parsed))
|
|
28
|
+
return [];
|
|
29
|
+
const items = [];
|
|
30
|
+
for (const item of parsed) {
|
|
31
|
+
if (typeof item !== 'object' || item === null)
|
|
32
|
+
continue;
|
|
33
|
+
const { documentId, action } = item;
|
|
34
|
+
if (typeof documentId === 'string' && (action === 'publish' || action === 'unpublish'))
|
|
35
|
+
items.push({ documentId, action });
|
|
36
|
+
}
|
|
37
|
+
return items;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* What happened lately, newest first: edits (a run of edits to one document by one author through one channel counts
|
|
41
|
+
* as one item) and publish events, for documents that are visible and not deleted. System writes are left out.
|
|
42
|
+
*/
|
|
43
|
+
export async function recentActivity(deps, opts = {}) {
|
|
44
|
+
const limit = Math.min(MAX_LIMIT, Math.max(1, Math.trunc(opts.limit ?? DEFAULT_LIMIT)));
|
|
45
|
+
const window = limit * 10;
|
|
46
|
+
const revisions = await deps.db
|
|
47
|
+
.selectFrom('revisions')
|
|
48
|
+
.innerJoin('documents', 'documents.id', 'revisions.document_id')
|
|
49
|
+
.select(['revisions.id as id', 'revisions.document_id as document_id', 'revisions.author_name as author_name', 'revisions.source as source', 'revisions.created_at as created_at'])
|
|
50
|
+
.where('documents.deleted_at', 'is', null)
|
|
51
|
+
.where('revisions.source', 'not in', HIDDEN_SOURCES)
|
|
52
|
+
.$call(whereVisible(deps.registry))
|
|
53
|
+
.orderBy('revisions.id', 'desc')
|
|
54
|
+
.limit(window)
|
|
55
|
+
.execute();
|
|
56
|
+
const events = await deps.db.selectFrom('publish_events').selectAll().orderBy('id', 'desc').limit(window).execute();
|
|
57
|
+
const eventItems = events.map((event) => publishedItems(event.items));
|
|
58
|
+
const ids = new Set([...revisions.map((r) => r.document_id), ...eventItems.flatMap((items) => items.map((i) => i.documentId))]);
|
|
59
|
+
const documents = await summariesOf(deps, [...ids]);
|
|
60
|
+
const activity = [];
|
|
61
|
+
const open = new Map();
|
|
62
|
+
for (const rev of revisions) {
|
|
63
|
+
const document = documents.get(rev.document_id);
|
|
64
|
+
if (!document)
|
|
65
|
+
continue;
|
|
66
|
+
const source = rev.source;
|
|
67
|
+
const run = open.get(rev.document_id);
|
|
68
|
+
if (run && run.authorName === rev.author_name && run.source === source) {
|
|
69
|
+
run.count += 1;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
const item = {
|
|
73
|
+
kind: 'edit',
|
|
74
|
+
at: rev.created_at,
|
|
75
|
+
authorName: rev.author_name,
|
|
76
|
+
source,
|
|
77
|
+
document,
|
|
78
|
+
revisionId: rev.id,
|
|
79
|
+
count: 1,
|
|
80
|
+
};
|
|
81
|
+
open.set(rev.document_id, item);
|
|
82
|
+
activity.push(item);
|
|
83
|
+
}
|
|
84
|
+
events.forEach((event, index) => {
|
|
85
|
+
for (const action of ['publish', 'unpublish']) {
|
|
86
|
+
const seen = new Set();
|
|
87
|
+
const listed = [];
|
|
88
|
+
for (const item of eventItems[index] ?? []) {
|
|
89
|
+
const document = documents.get(item.documentId);
|
|
90
|
+
if (item.action !== action || !document || seen.has(item.documentId))
|
|
91
|
+
continue;
|
|
92
|
+
seen.add(item.documentId);
|
|
93
|
+
listed.push(document);
|
|
94
|
+
}
|
|
95
|
+
if (listed.length > 0) {
|
|
96
|
+
activity.push({ kind: action, at: event.at, authorName: event.user_name, source: event.source, documents: listed });
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
return activity.sort((a, b) => (a.at < b.at ? 1 : a.at > b.at ? -1 : 0)).slice(0, limit);
|
|
101
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { CoreDeps, Ctx, DocumentView } from '../types.js';
|
|
2
|
+
/** Deletes every locale of an entry, unless something else still uses it. */
|
|
3
|
+
export declare function deleteEntry(deps: CoreDeps, groupId: string, ctx: Ctx): Promise<void>;
|
|
4
|
+
/** Brings a deleted entry back as unpublished drafts; a slug taken meanwhile gets the next free one. */
|
|
5
|
+
export declare function restoreEntry(deps: CoreDeps, groupId: string, ctx: Ctx): Promise<DocumentView[]>;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { assertCan } from '../auth/roles.js';
|
|
2
|
+
import { InlayError } from '../errors.js';
|
|
3
|
+
import { t } from '../messages/index.js';
|
|
4
|
+
import { getDocument, headQuery, parseData, summaryQuery, toSummary } from './read.js';
|
|
5
|
+
import { recordEvent } from './publish.js';
|
|
6
|
+
import { carryUnknown } from './reader.js';
|
|
7
|
+
import { syncRefs } from './refs.js';
|
|
8
|
+
import { uniqueSlug } from './slug.js';
|
|
9
|
+
import { findUsages } from './usages.js';
|
|
10
|
+
import { insertRevision } from './write.js';
|
|
11
|
+
import { withTransaction } from './dry-run.js';
|
|
12
|
+
async function groupRows(deps, groupId) {
|
|
13
|
+
const rows = await summaryQuery(deps).where('documents.group_id', '=', groupId).execute();
|
|
14
|
+
if (rows.length === 0)
|
|
15
|
+
throw new InlayError('not_found', 'error.notFound', { name: groupId });
|
|
16
|
+
if (rows.some((r) => r.kind !== 'entry')) {
|
|
17
|
+
throw new InlayError('invalid_operation', 'error.deleteNotEntry', { name: toSummary(deps, rows[0]).title });
|
|
18
|
+
}
|
|
19
|
+
return rows;
|
|
20
|
+
}
|
|
21
|
+
/** Deletes every locale of an entry, unless something else still uses it. */
|
|
22
|
+
export async function deleteEntry(deps, groupId, ctx) {
|
|
23
|
+
assertCan(ctx, 'entry.delete');
|
|
24
|
+
await withTransaction(deps, async (tx) => {
|
|
25
|
+
const trx = tx.db;
|
|
26
|
+
const rows = await groupRows(tx, groupId);
|
|
27
|
+
const live = rows.filter((r) => r.deleted_at === null);
|
|
28
|
+
if (live.length === 0)
|
|
29
|
+
return;
|
|
30
|
+
const main = live.find((r) => r.locale === deps.registry.defaultLocale) ?? live[0];
|
|
31
|
+
const usages = await findUsages(tx, { kind: 'document', id: groupId });
|
|
32
|
+
if (usages.length > 0) {
|
|
33
|
+
throw new InlayError('in_use', 'error.entryInUse', { title: toSummary(tx, main).title, count: usages.length }, { usages });
|
|
34
|
+
}
|
|
35
|
+
// Documents in hidden locales go too, so they can't outlive their entry.
|
|
36
|
+
const targets = await trx
|
|
37
|
+
.selectFrom('documents')
|
|
38
|
+
.select(['id', 'published_rev'])
|
|
39
|
+
.where('group_id', '=', groupId)
|
|
40
|
+
.where('deleted_at', 'is', null)
|
|
41
|
+
.execute();
|
|
42
|
+
const items = targets
|
|
43
|
+
.filter((r) => r.published_rev !== null)
|
|
44
|
+
.map((r) => ({ documentId: r.id, revisionId: null, action: 'unpublish' }));
|
|
45
|
+
await trx
|
|
46
|
+
.updateTable('documents')
|
|
47
|
+
.set({
|
|
48
|
+
deleted_at: deps.now().toISOString(),
|
|
49
|
+
published_rev: null,
|
|
50
|
+
published_slug: null,
|
|
51
|
+
published_path: null,
|
|
52
|
+
})
|
|
53
|
+
.where('id', 'in', targets.map((r) => r.id))
|
|
54
|
+
.execute();
|
|
55
|
+
for (const r of targets)
|
|
56
|
+
await syncRefs(tx, r.id);
|
|
57
|
+
await recordEvent(tx, ctx, null, items);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
/** Brings a deleted entry back as unpublished drafts; a slug taken meanwhile gets the next free one. */
|
|
61
|
+
export async function restoreEntry(deps, groupId, ctx) {
|
|
62
|
+
assertCan(ctx, 'entry.restore');
|
|
63
|
+
const ids = await withTransaction(deps, async (tx) => {
|
|
64
|
+
const trx = tx.db;
|
|
65
|
+
const rows = await groupRows(tx, groupId);
|
|
66
|
+
// Documents in hidden locales come back too, as deleting took them.
|
|
67
|
+
const all = await headQuery(tx).where('documents.group_id', '=', groupId).execute();
|
|
68
|
+
const deleted = all.filter((r) => r.deleted_at !== null);
|
|
69
|
+
if (deleted.length > 0) {
|
|
70
|
+
await trx
|
|
71
|
+
.updateTable('documents')
|
|
72
|
+
.set({ deleted_at: null })
|
|
73
|
+
.where('id', 'in', deleted.map((r) => r.id))
|
|
74
|
+
.execute();
|
|
75
|
+
}
|
|
76
|
+
for (const row of deleted) {
|
|
77
|
+
// Hidden-locale documents keep their stored data untouched: nobody can take their slug meanwhile.
|
|
78
|
+
if (row.head_slug !== null && deps.registry.locales.includes(row.locale)) {
|
|
79
|
+
const slug = await uniqueSlug(tx, row.type, row.locale, row.head_slug, row.id);
|
|
80
|
+
if (slug !== row.head_slug) {
|
|
81
|
+
const head = await trx
|
|
82
|
+
.selectFrom('revisions')
|
|
83
|
+
.select('data')
|
|
84
|
+
.where('id', '=', row.head_rev)
|
|
85
|
+
.executeTakeFirstOrThrow();
|
|
86
|
+
// Carry from the raw head so a value that no longer fits the schema survives the new revision.
|
|
87
|
+
const read = parseData(tx, row.type, head.data);
|
|
88
|
+
const data = carryUnknown(deps.registry, row.type, JSON.parse(head.data), read, { ...read, slug });
|
|
89
|
+
const at = deps.now().toISOString();
|
|
90
|
+
const revId = await insertRevision(tx, row.id, data, { ...ctx, source: 'restore' }, t('message.restoreSlug', { slug }), at);
|
|
91
|
+
await trx.updateTable('documents').set({ head_rev: revId, head_slug: slug }).where('id', '=', row.id).execute();
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
await syncRefs(tx, row.id);
|
|
95
|
+
}
|
|
96
|
+
return rows.map((r) => r.id);
|
|
97
|
+
});
|
|
98
|
+
const views = await Promise.all(ids.map((id) => getDocument(deps, id)));
|
|
99
|
+
return views.filter((v) => v !== null);
|
|
100
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Registry } from '../registry/registry.js';
|
|
2
|
+
import type { DiffEntry, DocumentData } from '../types.js';
|
|
3
|
+
/** Leaf-level changes from `before` (or empty) to `after`, in document schema order. */
|
|
4
|
+
export declare function diffData(registry: Registry, type: string, before: DocumentData | null, after: DocumentData): DiffEntry[];
|
|
5
|
+
/** One readable line per entry. */
|
|
6
|
+
export declare function formatDiff(entries: DiffEntry[]): string[];
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
2
|
+
import { fieldMeta } from '../fields/define.js';
|
|
3
|
+
import { defOf, isRecord, unwrap } from '../fields/zod-internals.js';
|
|
4
|
+
import { t } from '../messages/index.js';
|
|
5
|
+
import { richTextIsBlank, richTextText } from '../richtext/schema.js';
|
|
6
|
+
import { docLabels, documentSchema } from './schema.js';
|
|
7
|
+
const MAX_LENGTH = 80;
|
|
8
|
+
/** The object or array schema under any wrappers, if there is one. */
|
|
9
|
+
function structure(s) {
|
|
10
|
+
if (!s)
|
|
11
|
+
return undefined;
|
|
12
|
+
const def = defOf(unwrap(s));
|
|
13
|
+
return def.type === 'object' || def.type === 'array' ? def : undefined;
|
|
14
|
+
}
|
|
15
|
+
function isLeafSchema(s) {
|
|
16
|
+
if (!s)
|
|
17
|
+
return false;
|
|
18
|
+
const kind = fieldMeta(s)?.kind;
|
|
19
|
+
return kind !== undefined && kind !== 'group' && kind !== 'list';
|
|
20
|
+
}
|
|
21
|
+
/** Fallback labels for a path that isn't in the schema: the keys without the structural ones. */
|
|
22
|
+
const rawKeys = (path) => path.filter((s, i) => !(i === 0 && (s === 'fields' || s === 'slots')) && !(i === 2 && s === 'data' && path[0] === 'slots')).map(String);
|
|
23
|
+
/** Leaf-level changes from `before` (or empty) to `after`, in document schema order. */
|
|
24
|
+
export function diffData(registry, type, before, after) {
|
|
25
|
+
const { template } = registry.getType(type);
|
|
26
|
+
const schema = documentSchema(registry, type, 'draft');
|
|
27
|
+
const normalize = (data) => {
|
|
28
|
+
const parsed = schema.safeParse(data);
|
|
29
|
+
return parsed.success ? parsed.data : data;
|
|
30
|
+
};
|
|
31
|
+
before = before && normalize(before);
|
|
32
|
+
after = normalize(after);
|
|
33
|
+
const out = [];
|
|
34
|
+
const push = (path, change, b, a) => {
|
|
35
|
+
const entry = { path, labels: docLabels(registry, type, path) ?? rawKeys(path), change };
|
|
36
|
+
if (change !== 'added')
|
|
37
|
+
entry.before = b;
|
|
38
|
+
if (change !== 'removed')
|
|
39
|
+
entry.after = a;
|
|
40
|
+
out.push(entry);
|
|
41
|
+
};
|
|
42
|
+
const walk = (schema, path, b, a) => {
|
|
43
|
+
if (isDeepStrictEqual(b, a))
|
|
44
|
+
return;
|
|
45
|
+
if (b === undefined || b === null) {
|
|
46
|
+
if (a !== undefined && a !== null)
|
|
47
|
+
push(path, 'added', undefined, a);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (a === undefined || a === null)
|
|
51
|
+
return push(path, 'removed', b);
|
|
52
|
+
if (!isLeafSchema(schema)) {
|
|
53
|
+
const struct = structure(schema);
|
|
54
|
+
if (isRecord(b) && isRecord(a) && (struct === undefined || struct.type === 'object')) {
|
|
55
|
+
const shape = struct?.shape ?? {};
|
|
56
|
+
const keys = [...new Set([...Object.keys(shape), ...Object.keys(b), ...Object.keys(a)])];
|
|
57
|
+
for (const key of keys)
|
|
58
|
+
walk(shape[key], [...path, key], b[key], a[key]);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (Array.isArray(b) && Array.isArray(a) && (struct === undefined || struct.type === 'array')) {
|
|
62
|
+
for (let i = 0; i < Math.max(b.length, a.length); i++) {
|
|
63
|
+
if (i >= b.length)
|
|
64
|
+
push([...path, i], 'added', undefined, a[i]);
|
|
65
|
+
else if (i >= a.length)
|
|
66
|
+
push([...path, i], 'removed', b[i]);
|
|
67
|
+
else
|
|
68
|
+
walk(struct?.element, [...path, i], b[i], a[i]);
|
|
69
|
+
}
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
push(path, 'changed', b, a);
|
|
74
|
+
};
|
|
75
|
+
walk(undefined, ['slug'], before?.slug, after.slug);
|
|
76
|
+
walk(undefined, ['path'], before?.path, after.path);
|
|
77
|
+
walk(template.fields, ['fields'], before?.fields ?? {}, after.fields);
|
|
78
|
+
for (const [key, slot] of Object.entries(template.slots)) {
|
|
79
|
+
const b = before?.slots[key];
|
|
80
|
+
const a = after.slots[key];
|
|
81
|
+
if (!a)
|
|
82
|
+
continue;
|
|
83
|
+
if ((b?.enabled ?? false) !== a.enabled) {
|
|
84
|
+
push(['slots', key, 'enabled'], 'changed', b?.enabled ?? false, a.enabled);
|
|
85
|
+
}
|
|
86
|
+
walk(slot.block.schema, ['slots', key, 'data'], b?.data ?? {}, a.data);
|
|
87
|
+
}
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
90
|
+
function cut(s) {
|
|
91
|
+
return s.length > MAX_LENGTH ? `${s.slice(0, MAX_LENGTH - 1)}…` : s;
|
|
92
|
+
}
|
|
93
|
+
function short(value) {
|
|
94
|
+
if (typeof value === 'string')
|
|
95
|
+
return cut(value);
|
|
96
|
+
if (typeof value === 'boolean')
|
|
97
|
+
return t(value ? 'diff.yes' : 'diff.no');
|
|
98
|
+
if (value === undefined || value === null)
|
|
99
|
+
return t('diff.empty');
|
|
100
|
+
if (Array.isArray(value))
|
|
101
|
+
return t('diff.items', { n: value.length });
|
|
102
|
+
if (isRecord(value) && value.type === 'doc') {
|
|
103
|
+
const doc = value;
|
|
104
|
+
return richTextIsBlank(doc) ? t('diff.empty') : cut(richTextText(doc).replace(/\s+/g, ' ').trim());
|
|
105
|
+
}
|
|
106
|
+
if (isRecord(value)) {
|
|
107
|
+
if (typeof value.text === 'string' && value.text !== '')
|
|
108
|
+
return cut(value.text);
|
|
109
|
+
if (typeof value.url === 'string')
|
|
110
|
+
return cut(value.url);
|
|
111
|
+
if (typeof value.document === 'string')
|
|
112
|
+
return cut(value.document);
|
|
113
|
+
if (typeof value.media === 'string')
|
|
114
|
+
return t('diff.media', { id: value.media.slice(0, 8) });
|
|
115
|
+
return cut(JSON.stringify(value));
|
|
116
|
+
}
|
|
117
|
+
return cut(String(value));
|
|
118
|
+
}
|
|
119
|
+
/** One readable line per entry. */
|
|
120
|
+
export function formatDiff(entries) {
|
|
121
|
+
return entries.map((e) => {
|
|
122
|
+
const labels = e.labels.join(' → ');
|
|
123
|
+
if (e.change === 'changed')
|
|
124
|
+
return t('diff.changed', { labels, before: short(e.before), after: short(e.after) });
|
|
125
|
+
if (e.change === 'added')
|
|
126
|
+
return t('diff.added', { labels, value: short(e.after) });
|
|
127
|
+
return t('diff.removed', { labels, value: short(e.before) });
|
|
128
|
+
});
|
|
129
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { CoreDeps } from '../types.js';
|
|
2
|
+
/** Thrown inside a dry run's transaction to roll it back. */
|
|
3
|
+
export declare class DryRun extends Error {
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Runs `fn` in a transaction. When `deps.db` is already a transaction, `fn`
|
|
7
|
+
* runs in a savepoint of it: a throw rolls back only what `fn` wrote, and
|
|
8
|
+
* the rest commits or rolls back with the outer transaction. With `write`, a
|
|
9
|
+
* new transaction takes the write lock before `fn` reads anything, so a commit
|
|
10
|
+
* from another connection can't make its first write fail with
|
|
11
|
+
* SQLITE_BUSY_SNAPSHOT; it waits for that connection instead. `write` inside
|
|
12
|
+
* a transaction throws `error.transactionWrite`: what the outer one read can't
|
|
13
|
+
* be made safe from there.
|
|
14
|
+
*/
|
|
15
|
+
export declare function withTransaction<T>(deps: CoreDeps, fn: (tx: CoreDeps) => Promise<T>, opts?: {
|
|
16
|
+
write?: true;
|
|
17
|
+
}): Promise<T>;
|
|
18
|
+
/**
|
|
19
|
+
* Runs `fn` in a transaction (a savepoint inside one). With `dryRun`, `fn`
|
|
20
|
+
* runs in full, then the transaction is rolled back and `fn`'s result returned.
|
|
21
|
+
*/
|
|
22
|
+
export declare function runTransaction<T>(deps: CoreDeps, fn: (tx: CoreDeps) => Promise<T>, opts: {
|
|
23
|
+
dryRun?: boolean;
|
|
24
|
+
}): Promise<T>;
|