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.
- package/LICENSE +21 -0
- package/README.md +469 -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,106 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export const EXPORT_FORMAT = 'inlay-export';
|
|
3
|
+
export const EXPORT_FORMAT_VERSION = 1;
|
|
4
|
+
/** Exports made before shipped content had editions say `initial` for what is now `release`. */
|
|
5
|
+
const source = z.preprocess((v) => (v === 'initial' ? 'release' : v), z.enum(['admin', 'mcp', 'restore', 'import', 'release', 'system']));
|
|
6
|
+
const nullableString = z.string().nullable();
|
|
7
|
+
const nullableInt = z.number().int().nullable();
|
|
8
|
+
export const manifestSchema = z.object({
|
|
9
|
+
format: z.literal(EXPORT_FORMAT),
|
|
10
|
+
formatVersion: z.number().int().min(1),
|
|
11
|
+
inlayVersion: z.string(),
|
|
12
|
+
exportedAt: z.string(),
|
|
13
|
+
locales: z.array(z.string()),
|
|
14
|
+
defaultLocale: z.string(),
|
|
15
|
+
types: z.array(z.object({
|
|
16
|
+
name: z.string(),
|
|
17
|
+
kind: z.enum(['singleton', 'collection', 'global']),
|
|
18
|
+
label: z.string(),
|
|
19
|
+
template: z.string(),
|
|
20
|
+
schema: z.record(z.string(), z.unknown()),
|
|
21
|
+
})),
|
|
22
|
+
counts: z.object({
|
|
23
|
+
documents: z.number().int(),
|
|
24
|
+
revisions: z.number().int(),
|
|
25
|
+
publishEvents: z.number().int(),
|
|
26
|
+
media: z.number().int(),
|
|
27
|
+
redirects: z.number().int(),
|
|
28
|
+
}),
|
|
29
|
+
siteLock: z
|
|
30
|
+
.object({ enabled: z.boolean(), passwordHash: z.string().regex(/^[^:\s]+:[^:\s]+$/).nullable() })
|
|
31
|
+
.refine((lock) => !lock.enabled || lock.passwordHash !== null)
|
|
32
|
+
.optional(),
|
|
33
|
+
});
|
|
34
|
+
const documentRecord = z.object({
|
|
35
|
+
record: z.literal('document'),
|
|
36
|
+
id: z.string(),
|
|
37
|
+
kind: z.enum(['singleton', 'entry', 'global']),
|
|
38
|
+
type: z.string(),
|
|
39
|
+
groupId: z.string(),
|
|
40
|
+
locale: z.string(),
|
|
41
|
+
headRevision: nullableInt,
|
|
42
|
+
publishedRevision: nullableInt,
|
|
43
|
+
deletedAt: nullableString,
|
|
44
|
+
createdAt: z.string(),
|
|
45
|
+
key: nullableString.default(null),
|
|
46
|
+
edition: nullableInt.default(null),
|
|
47
|
+
});
|
|
48
|
+
const revisionRecord = z.object({
|
|
49
|
+
record: z.literal('revision'),
|
|
50
|
+
id: z.number().int(),
|
|
51
|
+
documentId: z.string(),
|
|
52
|
+
data: z.custom((v) => typeof v === 'object' && v !== null && !Array.isArray(v)),
|
|
53
|
+
authorId: nullableString,
|
|
54
|
+
authorName: z.string(),
|
|
55
|
+
source,
|
|
56
|
+
message: nullableString,
|
|
57
|
+
createdAt: z.string(),
|
|
58
|
+
releaseId: nullableString.default(null),
|
|
59
|
+
});
|
|
60
|
+
const publishEventRecord = z.object({
|
|
61
|
+
record: z.literal('publishEvent'),
|
|
62
|
+
id: z.number().int(),
|
|
63
|
+
at: z.string(),
|
|
64
|
+
userId: nullableString,
|
|
65
|
+
userName: z.string(),
|
|
66
|
+
source,
|
|
67
|
+
message: nullableString,
|
|
68
|
+
items: z.array(z.object({ documentId: z.string(), revisionId: nullableInt, action: z.enum(['publish', 'unpublish']) })),
|
|
69
|
+
});
|
|
70
|
+
export const contentRecordSchema = z.discriminatedUnion('record', [documentRecord, revisionRecord, publishEventRecord]);
|
|
71
|
+
export const redirectsSchema = z.array(z.object({
|
|
72
|
+
id: z.number().int(),
|
|
73
|
+
from: z.string(),
|
|
74
|
+
to: z.union([z.object({ path: z.string() }), z.object({ group: z.string() })]),
|
|
75
|
+
status: z.union([z.literal(301), z.literal(302)]),
|
|
76
|
+
source: z.enum(['manual', 'auto', 'import']),
|
|
77
|
+
note: nullableString,
|
|
78
|
+
createdById: nullableString,
|
|
79
|
+
createdBy: z.string(),
|
|
80
|
+
createdAt: z.string(),
|
|
81
|
+
}));
|
|
82
|
+
export const mediaIndexSchema = z.object({
|
|
83
|
+
files: z.array(z.object({
|
|
84
|
+
id: z.string(),
|
|
85
|
+
filename: z.string(),
|
|
86
|
+
mime: z.string(),
|
|
87
|
+
size: z.number().int(),
|
|
88
|
+
width: nullableInt,
|
|
89
|
+
height: nullableInt,
|
|
90
|
+
defaultAlt: nullableString,
|
|
91
|
+
createdById: nullableString,
|
|
92
|
+
createdBy: z.string(),
|
|
93
|
+
createdAt: z.string(),
|
|
94
|
+
})),
|
|
95
|
+
deletions: z.array(z.object({
|
|
96
|
+
mediaId: z.string(),
|
|
97
|
+
filename: z.string(),
|
|
98
|
+
deletedAt: z.string(),
|
|
99
|
+
deletedById: nullableString,
|
|
100
|
+
deletedBy: z.string(),
|
|
101
|
+
})),
|
|
102
|
+
});
|
|
103
|
+
/** `inlay-export-<stamp>.tar.gz`, with the same stamp as backup file names. */
|
|
104
|
+
export function exportFileName(at) {
|
|
105
|
+
return `inlay-export-${at.toISOString().replace(/[:.]/g, '-')}.tar.gz`;
|
|
106
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { Kysely } from 'kysely';
|
|
2
|
+
import { type BackupInfo } from '../backup/backup.js';
|
|
3
|
+
import type { Database } from '../db/schema.js';
|
|
4
|
+
import type { MediaStore } from '../media/store.js';
|
|
5
|
+
import type { SiteDefinition } from '../registry/define.js';
|
|
6
|
+
import { type SiteLockRow } from '../site-lock/site-lock.js';
|
|
7
|
+
import type { Ctx } from '../types.js';
|
|
8
|
+
import { type ExportBundle } from './read.js';
|
|
9
|
+
import { type ImportReport } from './report.js';
|
|
10
|
+
import { type ReplaceCounts } from './replace.js';
|
|
11
|
+
import { type Exclusions } from './stage.js';
|
|
12
|
+
/**
|
|
13
|
+
* Removes the files in `ids` that no live `media` row uses, checking each row under the file's lock.
|
|
14
|
+
* Returns the ids it removed. A failure on one file is ignored.
|
|
15
|
+
*/
|
|
16
|
+
export declare function removeUnusedMedia(db: Kysely<Database>, store: MediaStore, ids: readonly string[]): Promise<string[]>;
|
|
17
|
+
/** A dry run's report, and the damaged media files an import leaves out. */
|
|
18
|
+
export interface ImportPlan {
|
|
19
|
+
report: ImportReport;
|
|
20
|
+
exclude: Exclusions;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Stages `bundle` against `site`, leaving out damaged media files, and reports
|
|
24
|
+
* per type and locale what restoring it would do. Types and locales the site
|
|
25
|
+
* doesn't have are kept, hidden. Writes only to a scratch database in `bundle.dir`.
|
|
26
|
+
*/
|
|
27
|
+
export declare function planImport(site: SiteDefinition, bundle: ExportBundle, now?: () => Date, current?: SiteLockRow): Promise<ImportPlan>;
|
|
28
|
+
/** Reads the export at `archive` and reports what restoring it would do. Changes nothing. */
|
|
29
|
+
export declare function dryRunImport(site: SiteDefinition, archive: string, now?: () => Date, current?: SiteLockRow): Promise<ImportReport>;
|
|
30
|
+
export interface ImportResult {
|
|
31
|
+
report: ImportReport;
|
|
32
|
+
backup: BackupInfo;
|
|
33
|
+
imported: ReplaceCounts;
|
|
34
|
+
/** Ids of the damaged media files left out. */
|
|
35
|
+
skipped: {
|
|
36
|
+
media: string[];
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
export interface ImportOptions {
|
|
40
|
+
site: SiteDefinition;
|
|
41
|
+
db: Kysely<Database>;
|
|
42
|
+
backups: string;
|
|
43
|
+
/** The live media store, or null when media isn't set up. */
|
|
44
|
+
media: MediaStore | null;
|
|
45
|
+
archive: string;
|
|
46
|
+
ctx: Ctx;
|
|
47
|
+
now?: () => Date;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Replaces the live content with the export at `archive`, leaving out damaged
|
|
51
|
+
* media files. Dry-runs it first and refuses when the site can't be brought up
|
|
52
|
+
* to date on top of it. Takes a `pre-import` backup before changing anything.
|
|
53
|
+
*/
|
|
54
|
+
export declare function importContent(opts: ImportOptions): Promise<ImportResult>;
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { createBackup } from '../backup/backup.js';
|
|
4
|
+
import { InlayError } from '../errors.js';
|
|
5
|
+
import { withMediaLock } from '../media/library.js';
|
|
6
|
+
import { formatReport } from '../report.js';
|
|
7
|
+
import { readSiteLock } from '../site-lock/site-lock.js';
|
|
8
|
+
import { readExport } from './read.js';
|
|
9
|
+
import { siteLockEffects } from './report.js';
|
|
10
|
+
import { replaceContent } from './replace.js';
|
|
11
|
+
import { stageBundle } from './stage.js';
|
|
12
|
+
/**
|
|
13
|
+
* Removes the files in `ids` that no live `media` row uses, checking each row under the file's lock.
|
|
14
|
+
* Returns the ids it removed. A failure on one file is ignored.
|
|
15
|
+
*/
|
|
16
|
+
export async function removeUnusedMedia(db, store, ids) {
|
|
17
|
+
const removed = [];
|
|
18
|
+
for (const id of ids) {
|
|
19
|
+
await withMediaLock(store, id, async () => {
|
|
20
|
+
if (await db.selectFrom('media').select('id').where('id', '=', id).executeTakeFirst())
|
|
21
|
+
return;
|
|
22
|
+
await store.remove(id);
|
|
23
|
+
removed.push(id);
|
|
24
|
+
}).catch(() => { });
|
|
25
|
+
}
|
|
26
|
+
return removed;
|
|
27
|
+
}
|
|
28
|
+
/** Ordered names with no repeats. */
|
|
29
|
+
const unique = (...lists) => [...new Set(lists.flat())];
|
|
30
|
+
/** Titles of the shipped documents the import would create or replace. */
|
|
31
|
+
async function shippedTitles(stage) {
|
|
32
|
+
const titles = [];
|
|
33
|
+
for (const item of [...stage.report.shipped.created, ...stage.report.shipped.replaced]) {
|
|
34
|
+
const doc = item.documentId === null ? null : await stage.core.getDocument(item.documentId);
|
|
35
|
+
if (doc)
|
|
36
|
+
titles.push(doc.title);
|
|
37
|
+
}
|
|
38
|
+
return titles;
|
|
39
|
+
}
|
|
40
|
+
/** How many documents of each type would have a gap once imported. */
|
|
41
|
+
async function gapCounts(stage) {
|
|
42
|
+
const counts = new Map();
|
|
43
|
+
for (const gap of await stage.core.listGaps())
|
|
44
|
+
counts.set(gap.document.type, (counts.get(gap.document.type) ?? 0) + 1);
|
|
45
|
+
return counts;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Stages `bundle` against `site`, leaving out damaged media files, and reports
|
|
49
|
+
* per type and locale what restoring it would do. Types and locales the site
|
|
50
|
+
* doesn't have are kept, hidden. Writes only to a scratch database in `bundle.dir`.
|
|
51
|
+
*/
|
|
52
|
+
export async function planImport(site, bundle, now, current) {
|
|
53
|
+
const { manifest } = bundle;
|
|
54
|
+
const exclude = { media: new Set(bundle.damaged.map((m) => m.id)) };
|
|
55
|
+
const stage = await stageBundle(site, bundle, exclude, now);
|
|
56
|
+
try {
|
|
57
|
+
const { registry } = stage.core;
|
|
58
|
+
const gaps = await gapCounts(stage);
|
|
59
|
+
const typeNames = unique(manifest.types.map((t) => t.name), bundle.documents.map((d) => d.type));
|
|
60
|
+
const localeNames = unique(manifest.locales, bundle.documents.map((d) => d.locale));
|
|
61
|
+
const types = typeNames.map((name) => {
|
|
62
|
+
const known = registry.types.get(name);
|
|
63
|
+
const label = known?.label ?? manifest.types.find((t) => t.name === name)?.label ?? name;
|
|
64
|
+
const documents = bundle.documents.filter((d) => d.type === name).length;
|
|
65
|
+
return {
|
|
66
|
+
type: name,
|
|
67
|
+
current: known ? name : null,
|
|
68
|
+
label,
|
|
69
|
+
documents,
|
|
70
|
+
status: known ? 'compatible' : 'hidden',
|
|
71
|
+
gaps: gaps.get(name) ?? 0,
|
|
72
|
+
};
|
|
73
|
+
});
|
|
74
|
+
const locales = localeNames.map((locale) => {
|
|
75
|
+
const documents = bundle.documents.filter((d) => d.locale === locale).length;
|
|
76
|
+
return { locale, documents, status: registry.locales.includes(locale) ? 'compatible' : 'hidden' };
|
|
77
|
+
});
|
|
78
|
+
const report = {
|
|
79
|
+
ok: stage.report.ok,
|
|
80
|
+
exportedAt: manifest.exportedAt,
|
|
81
|
+
inlayVersion: manifest.inlayVersion,
|
|
82
|
+
types,
|
|
83
|
+
locales,
|
|
84
|
+
general: stage.report.problems,
|
|
85
|
+
media: { files: bundle.media.length, damaged: bundle.damaged.map((m) => m.filename) },
|
|
86
|
+
redirects: { count: bundle.redirects.length },
|
|
87
|
+
shipped: await shippedTitles(stage),
|
|
88
|
+
siteLock: manifest.siteLock
|
|
89
|
+
? {
|
|
90
|
+
enabled: manifest.siteLock.enabled,
|
|
91
|
+
hasPassword: manifest.siteLock.passwordHash !== null,
|
|
92
|
+
effects: current ? siteLockEffects(manifest.siteLock, current) : null,
|
|
93
|
+
}
|
|
94
|
+
: null,
|
|
95
|
+
};
|
|
96
|
+
return { report, exclude };
|
|
97
|
+
}
|
|
98
|
+
finally {
|
|
99
|
+
await stage.close();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/** Reads the export at `archive` and reports what restoring it would do. Changes nothing. */
|
|
103
|
+
export async function dryRunImport(site, archive, now, current) {
|
|
104
|
+
const bundle = await readExport(archive);
|
|
105
|
+
try {
|
|
106
|
+
return (await planImport(site, bundle, now, current)).report;
|
|
107
|
+
}
|
|
108
|
+
finally {
|
|
109
|
+
await bundle.close();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/** The error for an import the site can't be brought up to date on top of, as boot gives it. */
|
|
113
|
+
function refused(report) {
|
|
114
|
+
const problems = report.general;
|
|
115
|
+
const list = formatReport({ ok: false, systemMigrations: [], shipped: { created: [], replaced: [], skipped: [], deletedRefs: [], warnings: [] }, problems, warnings: [] }).join('\n');
|
|
116
|
+
return new InlayError('check_failed', 'error.checkFailed', { count: problems.length, list }, { problems, importReport: report });
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Replaces the live content with the export at `archive`, leaving out damaged
|
|
120
|
+
* media files. Dry-runs it first and refuses when the site can't be brought up
|
|
121
|
+
* to date on top of it. Takes a `pre-import` backup before changing anything.
|
|
122
|
+
*/
|
|
123
|
+
export async function importContent(opts) {
|
|
124
|
+
const bundle = await readExport(opts.archive);
|
|
125
|
+
let stage = null;
|
|
126
|
+
try {
|
|
127
|
+
const { report, exclude } = await planImport(opts.site, bundle, opts.now, await readSiteLock(opts.db));
|
|
128
|
+
if (!report.ok)
|
|
129
|
+
throw refused(report);
|
|
130
|
+
stage = await stageBundle(opts.site, bundle, exclude, opts.now);
|
|
131
|
+
if (!stage.report.ok)
|
|
132
|
+
throw refused({ ...report, ok: false, general: stage.report.problems });
|
|
133
|
+
const staged = (await stage.db.selectFrom('media').select('id').execute()).map((m) => m.id);
|
|
134
|
+
if (staged.length > 0 && !opts.media)
|
|
135
|
+
throw new InlayError('invalid_operation', 'error.mediaNotConfigured');
|
|
136
|
+
const backup = await createBackup({
|
|
137
|
+
db: opts.db,
|
|
138
|
+
backups: opts.backups,
|
|
139
|
+
...(opts.media && { media: opts.media.dir }),
|
|
140
|
+
reason: 'pre-import',
|
|
141
|
+
ctx: opts.ctx,
|
|
142
|
+
...(opts.now && { now: opts.now }),
|
|
143
|
+
});
|
|
144
|
+
const store = opts.media;
|
|
145
|
+
const added = [];
|
|
146
|
+
let replaced;
|
|
147
|
+
try {
|
|
148
|
+
if (store) {
|
|
149
|
+
for (const id of staged) {
|
|
150
|
+
await withMediaLock(store, id, async () => {
|
|
151
|
+
if (await store.has(id))
|
|
152
|
+
return;
|
|
153
|
+
await store.put(id, await readFile(join(bundle.dir, 'media', id)));
|
|
154
|
+
added.push(id);
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
replaced = await replaceContent(opts.db, stage.db, bundle.manifest.siteLock);
|
|
159
|
+
}
|
|
160
|
+
catch (e) {
|
|
161
|
+
if (store)
|
|
162
|
+
await removeUnusedMedia(opts.db, store, added);
|
|
163
|
+
throw e;
|
|
164
|
+
}
|
|
165
|
+
// The content is replaced: from here a failure only leaves a stray file, so the import still succeeds.
|
|
166
|
+
const { removedMedia, ...imported } = replaced;
|
|
167
|
+
if (store)
|
|
168
|
+
await removeUnusedMedia(opts.db, store, removedMedia);
|
|
169
|
+
return {
|
|
170
|
+
report,
|
|
171
|
+
backup,
|
|
172
|
+
imported,
|
|
173
|
+
skipped: { media: [...exclude.media] },
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
finally {
|
|
177
|
+
await stage?.close().catch(() => { });
|
|
178
|
+
await bundle.close().catch(() => { });
|
|
179
|
+
}
|
|
180
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type DocumentRecord, type Manifest, type MediaDeletionRecord, type MediaRecord, type PublishEventRecord, type RedirectRecord, type RevisionRecord } from './format.js';
|
|
2
|
+
export interface ExportBundle {
|
|
3
|
+
dir: string;
|
|
4
|
+
manifest: Manifest;
|
|
5
|
+
documents: DocumentRecord[];
|
|
6
|
+
revisions: RevisionRecord[];
|
|
7
|
+
publishEvents: PublishEventRecord[];
|
|
8
|
+
redirects: RedirectRecord[];
|
|
9
|
+
media: MediaRecord[];
|
|
10
|
+
deletions: MediaDeletionRecord[];
|
|
11
|
+
/** A `files` entry whose file is missing or whose sha256 isn't its id; not in `media`. */
|
|
12
|
+
damaged: MediaRecord[];
|
|
13
|
+
/** Removes `dir`. */
|
|
14
|
+
close(): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
export declare function readExport(archive: string): Promise<ExportBundle>;
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { createReadStream } from 'node:fs';
|
|
3
|
+
import { mkdtemp, readFile, rm } from 'node:fs/promises';
|
|
4
|
+
import { tmpdir } from 'node:os';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
import { extractArchive } from '../archive.js';
|
|
7
|
+
import { InlayError } from '../errors.js';
|
|
8
|
+
import { t } from '../messages/index.js';
|
|
9
|
+
import { paramNames } from '../redirects/collapse.js';
|
|
10
|
+
import { cleanPath, redirectFrom } from '../redirects/redirects.js';
|
|
11
|
+
import { EXPORT_FORMAT, EXPORT_FORMAT_VERSION, contentRecordSchema, manifestSchema, mediaIndexSchema, redirectsSchema, } from './format.js';
|
|
12
|
+
const FIXED = ['manifest.json', 'content.ndjson', 'redirects.json', 'media.json'];
|
|
13
|
+
const MEDIA_ID = /^[0-9a-f]{64}$/;
|
|
14
|
+
const allowed = (path) => FIXED.includes(path) || /^media\/[0-9a-f]{64}$/.test(path);
|
|
15
|
+
const invalid = (detail) => new InlayError('validation', 'error.exportInvalid', { detail });
|
|
16
|
+
const damaged = (detail, lines) => new InlayError('validation', 'error.exportDamaged', { detail }, lines === undefined ? {} : { lines });
|
|
17
|
+
export async function readExport(archive) {
|
|
18
|
+
const dir = await mkdtemp(join(tmpdir(), 'inlay-import-'));
|
|
19
|
+
try {
|
|
20
|
+
const got = new Set(await extractArchive(archive, dir, allowed));
|
|
21
|
+
const manifest = await readManifest(dir, got);
|
|
22
|
+
const { documents, revisions, publishEvents } = await readContent(dir, got, manifest);
|
|
23
|
+
const redirects = await readJson(dir, got, 'redirects.json', redirectsSchema);
|
|
24
|
+
const index = await readJson(dir, got, 'media.json', mediaIndexSchema);
|
|
25
|
+
checkRedirects(redirects, documents, manifest);
|
|
26
|
+
checkMediaIndex(index.files);
|
|
27
|
+
const media = [];
|
|
28
|
+
const bad = [];
|
|
29
|
+
for (const row of index.files) {
|
|
30
|
+
const ok = MEDIA_ID.test(row.id) && got.has(`media/${row.id}`) && (await hashFile(join(dir, 'media', row.id))) === row.id;
|
|
31
|
+
if (ok)
|
|
32
|
+
media.push(row);
|
|
33
|
+
else
|
|
34
|
+
bad.push(row);
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
dir,
|
|
38
|
+
manifest,
|
|
39
|
+
documents,
|
|
40
|
+
revisions,
|
|
41
|
+
publishEvents,
|
|
42
|
+
redirects,
|
|
43
|
+
media,
|
|
44
|
+
deletions: index.deletions,
|
|
45
|
+
damaged: bad,
|
|
46
|
+
close: () => rm(dir, { recursive: true, force: true }),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
catch (e) {
|
|
50
|
+
await rm(dir, { recursive: true, force: true });
|
|
51
|
+
throw e;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
async function readText(dir, got, name) {
|
|
55
|
+
if (!got.has(name))
|
|
56
|
+
throw invalid(name);
|
|
57
|
+
return readFile(join(dir, name), 'utf8');
|
|
58
|
+
}
|
|
59
|
+
async function readJson(dir, got, name, schema) {
|
|
60
|
+
let raw;
|
|
61
|
+
try {
|
|
62
|
+
raw = JSON.parse(await readText(dir, got, name));
|
|
63
|
+
}
|
|
64
|
+
catch (e) {
|
|
65
|
+
if (e instanceof InlayError)
|
|
66
|
+
throw e;
|
|
67
|
+
throw invalid(name);
|
|
68
|
+
}
|
|
69
|
+
const parsed = schema.safeParse(raw);
|
|
70
|
+
if (!parsed.success)
|
|
71
|
+
throw invalid(name);
|
|
72
|
+
return parsed.data;
|
|
73
|
+
}
|
|
74
|
+
async function readManifest(dir, got) {
|
|
75
|
+
let raw;
|
|
76
|
+
try {
|
|
77
|
+
raw = JSON.parse(await readText(dir, got, 'manifest.json'));
|
|
78
|
+
}
|
|
79
|
+
catch (e) {
|
|
80
|
+
if (e instanceof InlayError)
|
|
81
|
+
throw e;
|
|
82
|
+
throw invalid('manifest.json');
|
|
83
|
+
}
|
|
84
|
+
if (typeof raw !== 'object' || raw === null || raw.format !== EXPORT_FORMAT) {
|
|
85
|
+
throw invalid('manifest.json');
|
|
86
|
+
}
|
|
87
|
+
const version = raw.formatVersion;
|
|
88
|
+
if (typeof version === 'number' && Number.isInteger(version) && version > EXPORT_FORMAT_VERSION) {
|
|
89
|
+
throw new InlayError('validation', 'error.exportNewer', { version });
|
|
90
|
+
}
|
|
91
|
+
const parsed = manifestSchema.safeParse(raw);
|
|
92
|
+
if (!parsed.success)
|
|
93
|
+
throw invalid('manifest.json');
|
|
94
|
+
return parsed.data;
|
|
95
|
+
}
|
|
96
|
+
/** The document kind each type kind holds. */
|
|
97
|
+
const KIND_OF_TYPE = { singleton: 'singleton', collection: 'entry', global: 'global' };
|
|
98
|
+
async function readContent(dir, got, manifest) {
|
|
99
|
+
const text = await readText(dir, got, 'content.ndjson');
|
|
100
|
+
const documents = [];
|
|
101
|
+
const revisions = [];
|
|
102
|
+
const events = [];
|
|
103
|
+
const fail = (line, message) => {
|
|
104
|
+
throw damaged(t('export.line', { line, message }), [{ line, message }]);
|
|
105
|
+
};
|
|
106
|
+
text.split('\n').forEach((raw, i) => {
|
|
107
|
+
const line = i + 1;
|
|
108
|
+
if (raw.trim() === '')
|
|
109
|
+
return;
|
|
110
|
+
let json;
|
|
111
|
+
try {
|
|
112
|
+
json = JSON.parse(raw);
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
fail(line, t('export.badJson'));
|
|
116
|
+
}
|
|
117
|
+
const parsed = contentRecordSchema.safeParse(json);
|
|
118
|
+
if (!parsed.success)
|
|
119
|
+
fail(line, t('export.badRecord'));
|
|
120
|
+
else if (parsed.data.record === 'document')
|
|
121
|
+
documents.push({ line, rec: parsed.data });
|
|
122
|
+
else if (parsed.data.record === 'revision')
|
|
123
|
+
revisions.push({ line, rec: parsed.data });
|
|
124
|
+
else
|
|
125
|
+
events.push({ line, rec: parsed.data });
|
|
126
|
+
});
|
|
127
|
+
const typeKind = new Map(manifest.types.map((type) => [type.name, KIND_OF_TYPE[type.kind]]));
|
|
128
|
+
const docById = new Map();
|
|
129
|
+
const translations = new Set();
|
|
130
|
+
for (const { line, rec } of documents) {
|
|
131
|
+
if (docById.has(rec.id))
|
|
132
|
+
fail(line, t('export.duplicate', { id: rec.id }));
|
|
133
|
+
docById.set(rec.id, rec);
|
|
134
|
+
const translation = JSON.stringify([rec.groupId, rec.locale]);
|
|
135
|
+
if (translations.has(translation))
|
|
136
|
+
fail(line, t('export.duplicateTranslation', { id: rec.groupId, locale: rec.locale }));
|
|
137
|
+
translations.add(translation);
|
|
138
|
+
const kind = typeKind.get(rec.type);
|
|
139
|
+
if (kind !== undefined && kind !== rec.kind)
|
|
140
|
+
fail(line, t('export.wrongKind', { type: rec.type }));
|
|
141
|
+
}
|
|
142
|
+
const revById = new Map();
|
|
143
|
+
for (const { line, rec } of revisions) {
|
|
144
|
+
if (revById.has(rec.id))
|
|
145
|
+
fail(line, t('export.duplicate', { id: rec.id }));
|
|
146
|
+
if (!docById.has(rec.documentId))
|
|
147
|
+
fail(line, t('export.unknownDocument', { id: rec.documentId }));
|
|
148
|
+
revById.set(rec.id, rec);
|
|
149
|
+
}
|
|
150
|
+
for (const { line, rec } of documents) {
|
|
151
|
+
for (const id of [rec.headRevision, rec.publishedRevision]) {
|
|
152
|
+
if (id !== null && revById.get(id)?.documentId !== rec.id)
|
|
153
|
+
fail(line, t('export.foreignRevision', { id }));
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
const eventIds = new Set();
|
|
157
|
+
for (const { line, rec } of events) {
|
|
158
|
+
if (eventIds.has(rec.id))
|
|
159
|
+
fail(line, t('export.duplicate', { id: rec.id }));
|
|
160
|
+
eventIds.add(rec.id);
|
|
161
|
+
for (const item of rec.items) {
|
|
162
|
+
if (!docById.has(item.documentId))
|
|
163
|
+
fail(line, t('export.unknownDocument', { id: item.documentId }));
|
|
164
|
+
if (item.revisionId !== null && revById.get(item.revisionId)?.documentId !== item.documentId) {
|
|
165
|
+
fail(line, t('export.foreignRevision', { id: item.revisionId }));
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return {
|
|
170
|
+
documents: documents.map((d) => d.rec),
|
|
171
|
+
revisions: revisions.map((r) => r.rec),
|
|
172
|
+
publishEvents: events.map((e) => e.rec),
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
const PARAM_SEGMENT = /^:[A-Za-z_][A-Za-z0-9_]*$/;
|
|
176
|
+
const ENCODED_SEPARATOR = /%(2f|5c)/i;
|
|
177
|
+
/**
|
|
178
|
+
* The stored (decoded) form of a path, checked the way creating it would. Each segment is encoded first, since a
|
|
179
|
+
* stored `/100%-off` is not itself a valid URL, then the result must come back unchanged. Params stay as they are.
|
|
180
|
+
* A literal `%2F` or `%5C` is refused outright: it is only ever a disguised separator.
|
|
181
|
+
*/
|
|
182
|
+
function storedPath(path, check) {
|
|
183
|
+
if (ENCODED_SEPARATOR.test(path))
|
|
184
|
+
return null;
|
|
185
|
+
const encoded = path.split('/').map((s) => (PARAM_SEGMENT.test(s) ? s : encodeURIComponent(s))).join('/');
|
|
186
|
+
return check(encoded);
|
|
187
|
+
}
|
|
188
|
+
/** Checks each redirect the way creating it would, against the exporting site's default language. */
|
|
189
|
+
function checkRedirects(redirects, documents, manifest) {
|
|
190
|
+
const groups = new Set(documents.map((d) => d.groupId));
|
|
191
|
+
// redirectFrom only needs the default language, which the manifest has.
|
|
192
|
+
const site = { defaultLocale: manifest.defaultLocale };
|
|
193
|
+
const ids = new Set();
|
|
194
|
+
const froms = new Set();
|
|
195
|
+
redirects.forEach((r, i) => {
|
|
196
|
+
const fail = (message) => {
|
|
197
|
+
throw damaged(t('export.entry', { file: 'redirects.json', entry: i + 1, message }));
|
|
198
|
+
};
|
|
199
|
+
if (ids.has(r.id))
|
|
200
|
+
fail(t('export.duplicate', { id: r.id }));
|
|
201
|
+
ids.add(r.id);
|
|
202
|
+
if (storedPath(r.from, (p) => redirectFrom(site, p)) !== r.from)
|
|
203
|
+
fail(t('export.redirectFrom', { path: r.from }));
|
|
204
|
+
if (froms.has(r.from))
|
|
205
|
+
fail(t('export.duplicateFrom', { path: r.from }));
|
|
206
|
+
froms.add(r.from);
|
|
207
|
+
if ('group' in r.to) {
|
|
208
|
+
if (!groups.has(r.to.group))
|
|
209
|
+
fail(t('export.unknownGroup', { id: r.to.group }));
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
const to = r.to.path;
|
|
213
|
+
const params = paramNames(r.from);
|
|
214
|
+
if (storedPath(to, cleanPath) !== to || to.split('/')[1] === manifest.defaultLocale || paramNames(to).some((n) => !params.includes(n))) {
|
|
215
|
+
fail(t('export.redirectTo', { path: to }));
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
function checkMediaIndex(files) {
|
|
220
|
+
const ids = new Set();
|
|
221
|
+
files.forEach((m, i) => {
|
|
222
|
+
if (ids.has(m.id)) {
|
|
223
|
+
const message = t('export.duplicate', { id: m.id });
|
|
224
|
+
throw damaged(t('export.entry', { file: 'media.json', entry: i + 1, message }));
|
|
225
|
+
}
|
|
226
|
+
ids.add(m.id);
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
async function hashFile(file) {
|
|
230
|
+
const hash = createHash('sha256');
|
|
231
|
+
for await (const chunk of createReadStream(file))
|
|
232
|
+
hash.update(chunk);
|
|
233
|
+
return hash.digest('hex');
|
|
234
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Kysely } from 'kysely';
|
|
2
|
+
import type { Database } from '../db/schema.js';
|
|
3
|
+
import { type SiteLockRow } from '../site-lock/site-lock.js';
|
|
4
|
+
export interface ReplaceCounts {
|
|
5
|
+
documents: number;
|
|
6
|
+
revisions: number;
|
|
7
|
+
media: number;
|
|
8
|
+
redirects: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Replaces every content table of `live` with the rows of `from`, ids and all,
|
|
12
|
+
* in one transaction. `removedMedia` lists the media ids `live` had that
|
|
13
|
+
* `from` doesn't, so the caller can remove their files once this commits.
|
|
14
|
+
*/
|
|
15
|
+
export declare function replaceContent(live: Kysely<Database>, from: Kysely<Database>, siteLock?: SiteLockRow): Promise<ReplaceCounts & {
|
|
16
|
+
removedMedia: string[];
|
|
17
|
+
}>;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { writeSiteLock } from '../site-lock/site-lock.js';
|
|
2
|
+
const CHUNK = 200;
|
|
3
|
+
/** Deleted children first, so no foreign key is left pointing at a removed row. */
|
|
4
|
+
const DELETE_ORDER = ['refs', 'publish_events', 'revisions', 'documents', 'media_deletions', 'media', 'redirects'];
|
|
5
|
+
/** Copied parents first, for the same reason. */
|
|
6
|
+
const COPY_ORDER = ['documents', 'revisions', 'publish_events', 'refs', 'media', 'media_deletions', 'redirects'];
|
|
7
|
+
/**
|
|
8
|
+
* Replaces every content table of `live` with the rows of `from`, ids and all,
|
|
9
|
+
* in one transaction. `removedMedia` lists the media ids `live` had that
|
|
10
|
+
* `from` doesn't, so the caller can remove their files once this commits.
|
|
11
|
+
*/
|
|
12
|
+
export async function replaceContent(live, from, siteLock) {
|
|
13
|
+
return live.transaction().execute(async (trx) => {
|
|
14
|
+
const oldMedia = (await trx.selectFrom('media').select('id').execute()).map((m) => m.id);
|
|
15
|
+
for (const table of DELETE_ORDER)
|
|
16
|
+
await trx.deleteFrom(table).execute();
|
|
17
|
+
const copied = new Map();
|
|
18
|
+
for (const table of COPY_ORDER) {
|
|
19
|
+
const rows = await from.selectFrom(table).selectAll().execute();
|
|
20
|
+
for (let i = 0; i < rows.length; i += CHUNK) {
|
|
21
|
+
await trx.insertInto(table).values(rows.slice(i, i + CHUNK)).execute();
|
|
22
|
+
}
|
|
23
|
+
copied.set(table, rows.length);
|
|
24
|
+
}
|
|
25
|
+
if (siteLock)
|
|
26
|
+
await writeSiteLock(trx, siteLock);
|
|
27
|
+
const newMedia = new Set((await trx.selectFrom('media').select('id').execute()).map((m) => m.id));
|
|
28
|
+
return {
|
|
29
|
+
documents: copied.get('documents') ?? 0,
|
|
30
|
+
revisions: copied.get('revisions') ?? 0,
|
|
31
|
+
media: copied.get('media') ?? 0,
|
|
32
|
+
redirects: copied.get('redirects') ?? 0,
|
|
33
|
+
removedMedia: oldMedia.filter((id) => !newMedia.has(id)),
|
|
34
|
+
};
|
|
35
|
+
});
|
|
36
|
+
}
|