@kidecms/core 0.1.10 → 0.13.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/admin/components/AssetsGrid.tsx +366 -173
- package/admin/components/BlockEditor.tsx +260 -679
- package/admin/components/CheckboxField.tsx +2 -2
- package/admin/components/CollaborationReviewBar.tsx +384 -0
- package/admin/components/ColorField.tsx +87 -0
- package/admin/components/CommandPalette.tsx +169 -0
- package/admin/components/ContentEditor.tsx +687 -0
- package/admin/components/DocumentActions.tsx +4 -2
- package/admin/components/DocumentsDataTable.tsx +91 -10
- package/admin/components/FieldControl.astro +151 -26
- package/admin/components/FocalPointSelector.tsx +27 -1
- package/admin/components/InternalLinkPicker.tsx +4 -3
- package/admin/components/LinkField.tsx +126 -0
- package/admin/components/RelationField.tsx +105 -26
- package/admin/components/RepeaterInput.tsx +46 -0
- package/admin/components/RichTextEditor.tsx +44 -9
- package/admin/components/SharedSectionBlockField.tsx +93 -0
- package/admin/components/SidebarUserMenu.tsx +1 -1
- package/admin/components/Toast.astro +1 -1
- package/admin/components/TreeItemsEditor.tsx +20 -19
- package/admin/components/TreeSelect.tsx +2 -2
- package/admin/components/YoutubeField.tsx +48 -0
- package/admin/components/block-fields.tsx +777 -0
- package/admin/components/content-block-spec.ts +114 -0
- package/admin/components/slash-command.tsx +242 -0
- package/admin/components/tree-utils.ts +14 -2
- package/admin/components/ui/badge.tsx +2 -3
- package/admin/components/ui/button.tsx +5 -4
- package/admin/components/ui/checkbox.tsx +1 -1
- package/admin/components/ui/input.tsx +1 -1
- package/admin/components/ui/select.tsx +1 -1
- package/admin/components/ui/table.tsx +1 -1
- package/admin/components/ui/textarea.tsx +1 -1
- package/admin/layouts/AdminLayout.astro +323 -123
- package/admin/lib/access.ts +54 -0
- package/admin/lib/collaboration.ts +178 -0
- package/admin/lib/edit-data.ts +448 -0
- package/admin/views/AccessDeniedView.astro +8 -0
- package/admin/views/EditView.astro +706 -0
- package/admin/views/ListView.astro +202 -0
- package/admin/views/RecentView.astro +76 -0
- package/admin/views/SinglesView.astro +65 -0
- package/admin/views/edit/EditScripts.astro +145 -0
- package/admin/views/edit/ReverseReferencesList.astro +68 -0
- package/core/admin.ts +193 -0
- package/core/ai.ts +87 -0
- package/core/api.ts +1392 -0
- package/core/assets.ts +336 -0
- package/core/audit.ts +84 -0
- package/core/auth-config.ts +44 -0
- package/core/auth.ts +303 -0
- package/core/collaboration.ts +276 -0
- package/core/content.ts +27 -0
- package/core/create-admin.ts +28 -0
- package/core/define.ts +625 -0
- package/core/field-model.ts +172 -0
- package/core/generator.ts +519 -0
- package/core/http.ts +42 -0
- package/core/image.ts +219 -0
- package/core/index.ts +211 -0
- package/core/locks.ts +52 -0
- package/core/log.ts +37 -0
- package/core/migrate.ts +278 -0
- package/core/rate-limit.ts +110 -0
- package/core/request-scope.ts +33 -0
- package/core/richtext.ts +1 -0
- package/core/runtime.ts +63 -0
- package/core/schema.ts +32 -0
- package/core/search.ts +489 -0
- package/core/seed.ts +101 -0
- package/core/shared-sections.ts +96 -0
- package/core/tasks.ts +176 -0
- package/core/values.ts +433 -0
- package/core/webhooks.ts +85 -0
- package/core/youtube.ts +39 -0
- package/internals/cli.mjs +34 -0
- package/internals/context.ts +67 -0
- package/internals/create-admin.ts +40 -0
- package/internals/describe.ts +25 -0
- package/internals/eject.ts +144 -0
- package/internals/generator.ts +11 -0
- package/internals/integration.ts +481 -0
- package/internals/mcp.ts +397 -0
- package/internals/project.ts +26 -0
- package/internals/push.ts +160 -0
- package/internals/reindex.ts +11 -0
- package/internals/restore.ts +257 -0
- package/internals/seed.data.ts +7 -0
- package/internals/seed.ts +11 -0
- package/internals/upgrade.ts +776 -0
- package/{virtual.d.ts → internals/virtual.d.ts} +39 -5
- package/middleware/auth.ts +130 -17
- package/middleware/push.ts +121 -0
- package/package.json +55 -67
- package/platform/cloudflare/cf-env.ts +60 -0
- package/platform/cloudflare/database.ts +28 -0
- package/platform/cloudflare/globals.d.ts +5 -0
- package/platform/cloudflare/storage.ts +33 -0
- package/platform/cloudflare/uploads-route.ts +37 -0
- package/platform/node/database.ts +39 -0
- package/platform/node/storage.ts +52 -0
- package/routes/admin/[...path].astro +71 -0
- package/routes/{pages/admin → admin}/assets/[id].astro +25 -19
- package/routes/admin/assets/index.astro +90 -0
- package/routes/admin/forgot-password.astro +60 -0
- package/routes/{pages/admin → admin}/invite.astro +7 -6
- package/routes/admin/login.astro +103 -0
- package/routes/admin/reset-password.astro +73 -0
- package/routes/{pages/admin → admin}/setup.astro +32 -8
- package/routes/api/{cms/[collection] → [collection]}/[...path].ts +66 -14
- package/routes/api/admin/search.ts +89 -0
- package/routes/api/{cms/assets → assets}/[id].ts +24 -14
- package/routes/api/{cms/assets → assets}/index.ts +4 -2
- package/routes/api/assets/upload.ts +147 -0
- package/routes/api/auth/forgot-password.ts +77 -0
- package/routes/api/{cms/auth → auth}/invite.ts +41 -9
- package/routes/api/auth/login.ts +167 -0
- package/routes/api/{cms/auth → auth}/logout.ts +11 -2
- package/routes/api/auth/reset-password.ts +85 -0
- package/routes/api/auth/setup.ts +110 -0
- package/routes/api/auth/sso/[provider]/start.ts +34 -0
- package/routes/api/collaboration/index.ts +106 -0
- package/routes/api/cron/_authorize.ts +25 -0
- package/routes/api/{cms/cron → cron}/publish.ts +3 -9
- package/routes/api/cron/tasks.ts +33 -0
- package/routes/api/forms/submit/[slug].ts +143 -0
- package/routes/api/img/[...path].ts +65 -0
- package/routes/api/{cms/preview → preview}/render.ts +8 -3
- package/routes/api/{cms/references → references}/[collection]/[id].ts +31 -1
- package/routes/api/webhooks/[provider].ts +54 -0
- package/README.md +0 -28
- package/dist/admin.d.ts +0 -33
- package/dist/admin.js +0 -92
- package/dist/ai.d.ts +0 -15
- package/dist/ai.js +0 -70
- package/dist/api.d.ts +0 -14
- package/dist/api.js +0 -833
- package/dist/assets.d.ts +0 -51
- package/dist/assets.js +0 -163
- package/dist/auth.d.ts +0 -30
- package/dist/auth.js +0 -132
- package/dist/content.d.ts +0 -3
- package/dist/content.js +0 -29
- package/dist/create-admin.d.ts +0 -5
- package/dist/create-admin.js +0 -23
- package/dist/define.d.ts +0 -254
- package/dist/define.js +0 -36
- package/dist/generator.d.ts +0 -9
- package/dist/generator.js +0 -370
- package/dist/image.d.ts +0 -9
- package/dist/image.js +0 -69
- package/dist/index.d.ts +0 -28
- package/dist/index.js +0 -16
- package/dist/integration.d.ts +0 -14
- package/dist/integration.js +0 -327
- package/dist/locks.d.ts +0 -7
- package/dist/locks.js +0 -37
- package/dist/richtext.d.ts +0 -1
- package/dist/richtext.js +0 -1
- package/dist/runtime.d.ts +0 -24
- package/dist/runtime.js +0 -26
- package/dist/schema.d.ts +0 -17
- package/dist/schema.js +0 -13
- package/dist/seed.d.ts +0 -2
- package/dist/seed.js +0 -84
- package/dist/values.d.ts +0 -8
- package/dist/values.js +0 -102
- package/dist/webhooks.d.ts +0 -2
- package/dist/webhooks.js +0 -73
- package/routes/api/cms/assets/upload.ts +0 -112
- package/routes/api/cms/auth/login.ts +0 -124
- package/routes/api/cms/auth/setup.ts +0 -77
- package/routes/api/cms/img/[...path].ts +0 -24
- package/routes/pages/admin/[...path].astro +0 -1107
- package/routes/pages/admin/assets/index.astro +0 -63
- package/routes/pages/admin/login.astro +0 -57
- /package/routes/api/{cms/ai → ai}/alt-text.ts +0 -0
- /package/routes/api/{cms/ai → ai}/seo.ts +0 -0
- /package/routes/api/{cms/ai → ai}/translate.ts +0 -0
- /package/routes/api/{cms/assets → assets}/folders.ts +0 -0
- /package/routes/api/{cms/locks → locks}/[...path].ts +0 -0
package/core/assets.ts
ADDED
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { and, desc, eq, isNull, like, or, sql } from "drizzle-orm";
|
|
3
|
+
import { nanoid } from "nanoid";
|
|
4
|
+
|
|
5
|
+
import { logAudit, type AuditActor } from "./audit";
|
|
6
|
+
import { getDb, getStorage } from "./runtime";
|
|
7
|
+
import { getSchema } from "./schema";
|
|
8
|
+
|
|
9
|
+
export type AssetContext = { actor?: AuditActor };
|
|
10
|
+
|
|
11
|
+
export type AssetRecord = {
|
|
12
|
+
_id: string;
|
|
13
|
+
filename: string;
|
|
14
|
+
mimeType: string;
|
|
15
|
+
size: number;
|
|
16
|
+
width: number | null;
|
|
17
|
+
height: number | null;
|
|
18
|
+
focalX: number | null;
|
|
19
|
+
focalY: number | null;
|
|
20
|
+
alt: string | null;
|
|
21
|
+
folder: string | null;
|
|
22
|
+
storagePath: string;
|
|
23
|
+
url: string;
|
|
24
|
+
_createdAt: string;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type FolderRecord = {
|
|
28
|
+
_id: string;
|
|
29
|
+
name: string;
|
|
30
|
+
parent: string | null;
|
|
31
|
+
_createdAt: string;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const EXTENSION_BY_MIME: Record<string, string> = {
|
|
35
|
+
"image/jpeg": ".jpg",
|
|
36
|
+
"image/png": ".png",
|
|
37
|
+
"image/gif": ".gif",
|
|
38
|
+
"image/webp": ".webp",
|
|
39
|
+
"image/avif": ".avif",
|
|
40
|
+
"image/svg+xml": ".svg",
|
|
41
|
+
"application/pdf": ".pdf",
|
|
42
|
+
"video/mp4": ".mp4",
|
|
43
|
+
"video/webm": ".webm",
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// Served from the admin's own origin, so any of these would execute as script there.
|
|
47
|
+
const ACTIVE_CONTENT_EXTENSIONS = new Set([
|
|
48
|
+
"html",
|
|
49
|
+
"htm",
|
|
50
|
+
"xhtml",
|
|
51
|
+
"shtml",
|
|
52
|
+
"xml",
|
|
53
|
+
"svg",
|
|
54
|
+
"js",
|
|
55
|
+
"mjs",
|
|
56
|
+
"cjs",
|
|
57
|
+
"php",
|
|
58
|
+
"phtml",
|
|
59
|
+
"asp",
|
|
60
|
+
"aspx",
|
|
61
|
+
"jsp",
|
|
62
|
+
]);
|
|
63
|
+
|
|
64
|
+
/** The stored extension decides how the file is served, so derive it from the verified type. */
|
|
65
|
+
const storedExtension = (file: File): string => {
|
|
66
|
+
const known = EXTENSION_BY_MIME[file.type];
|
|
67
|
+
if (known) return known;
|
|
68
|
+
|
|
69
|
+
// Unknown type — only reachable via a custom `admin.uploads.allowedTypes`.
|
|
70
|
+
const raw = file.name.includes(".") ? file.name.slice(file.name.lastIndexOf(".") + 1) : "";
|
|
71
|
+
const cleaned = raw.toLowerCase().replace(/[^a-z0-9]/g, "");
|
|
72
|
+
if (!cleaned || cleaned.length > 8 || ACTIVE_CONTENT_EXTENSIONS.has(cleaned)) return "";
|
|
73
|
+
return `.${cleaned}`;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export const assets = {
|
|
77
|
+
async upload(
|
|
78
|
+
file: File,
|
|
79
|
+
options?: { alt?: string; folder?: string; dedupe?: boolean },
|
|
80
|
+
context?: AssetContext,
|
|
81
|
+
): Promise<AssetRecord> {
|
|
82
|
+
const db = await getDb();
|
|
83
|
+
const schema = getSchema();
|
|
84
|
+
const storage = getStorage();
|
|
85
|
+
|
|
86
|
+
const bytes = new Uint8Array(await file.arrayBuffer());
|
|
87
|
+
const hash = createHash("sha256").update(bytes).digest("hex");
|
|
88
|
+
|
|
89
|
+
// Content-hash dedupe — re-running a media import won't re-upload identical
|
|
90
|
+
// files. Returns the existing asset, so importers stay idempotent without an
|
|
91
|
+
// external id→storagePath map.
|
|
92
|
+
if (options?.dedupe) {
|
|
93
|
+
const existing = await this.findByHash(hash);
|
|
94
|
+
if (existing) return existing;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const safeName = `${nanoid(12)}${storedExtension(file)}`;
|
|
98
|
+
const storagePath = `/uploads/${safeName}`;
|
|
99
|
+
|
|
100
|
+
await storage.putFile(storagePath, bytes);
|
|
101
|
+
|
|
102
|
+
// Capture intrinsic dimensions for rasters so consumers can prevent layout shift.
|
|
103
|
+
let width: number | null = null;
|
|
104
|
+
let height: number | null = null;
|
|
105
|
+
if ((file.type || "").startsWith("image/") && file.type !== "image/svg+xml") {
|
|
106
|
+
try {
|
|
107
|
+
const sharpModule = "sharp";
|
|
108
|
+
const sharp = (await import(/* @vite-ignore */ sharpModule)).default;
|
|
109
|
+
const meta = await sharp(bytes).metadata();
|
|
110
|
+
width = meta.width ?? null;
|
|
111
|
+
height = meta.height ?? null;
|
|
112
|
+
} catch {
|
|
113
|
+
// Unreadable/unsupported image — leave dimensions null.
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const id = nanoid();
|
|
118
|
+
const createdAt = new Date().toISOString();
|
|
119
|
+
const folder = options?.folder || null;
|
|
120
|
+
|
|
121
|
+
await db.insert(schema.cmsAssets).values({
|
|
122
|
+
_id: id,
|
|
123
|
+
filename: file.name,
|
|
124
|
+
mimeType: file.type || "application/octet-stream",
|
|
125
|
+
size: file.size,
|
|
126
|
+
width,
|
|
127
|
+
height,
|
|
128
|
+
alt: options?.alt ?? null,
|
|
129
|
+
folder,
|
|
130
|
+
storagePath,
|
|
131
|
+
hash,
|
|
132
|
+
_createdAt: createdAt,
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
logAudit({
|
|
136
|
+
action: "asset.upload",
|
|
137
|
+
resourceType: "asset",
|
|
138
|
+
resourceId: id,
|
|
139
|
+
actor: context?.actor ?? null,
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
return {
|
|
143
|
+
_id: id,
|
|
144
|
+
filename: file.name,
|
|
145
|
+
mimeType: file.type || "application/octet-stream",
|
|
146
|
+
size: file.size,
|
|
147
|
+
width,
|
|
148
|
+
height,
|
|
149
|
+
focalX: null,
|
|
150
|
+
focalY: null,
|
|
151
|
+
alt: options?.alt ?? null,
|
|
152
|
+
folder,
|
|
153
|
+
storagePath,
|
|
154
|
+
url: storagePath,
|
|
155
|
+
_createdAt: createdAt,
|
|
156
|
+
};
|
|
157
|
+
},
|
|
158
|
+
|
|
159
|
+
async find(
|
|
160
|
+
options: { limit?: number; offset?: number; folder?: string | null; search?: string } = {},
|
|
161
|
+
): Promise<AssetRecord[]> {
|
|
162
|
+
const db = await getDb();
|
|
163
|
+
const schema = getSchema();
|
|
164
|
+
|
|
165
|
+
let query = db
|
|
166
|
+
.select()
|
|
167
|
+
.from(schema.cmsAssets)
|
|
168
|
+
.where(buildAssetFilter(schema, options))
|
|
169
|
+
.orderBy(desc(schema.cmsAssets._createdAt));
|
|
170
|
+
if (options.limit) query = query.limit(options.limit) as any;
|
|
171
|
+
if (options.offset) query = query.offset(options.offset) as any;
|
|
172
|
+
|
|
173
|
+
const rows = await query;
|
|
174
|
+
return rows.map((row: any) => ({ ...row, url: row.storagePath }));
|
|
175
|
+
},
|
|
176
|
+
|
|
177
|
+
async findById(id: string): Promise<AssetRecord | null> {
|
|
178
|
+
const db = await getDb();
|
|
179
|
+
const schema = getSchema();
|
|
180
|
+
const rows = await db.select().from(schema.cmsAssets).where(eq(schema.cmsAssets._id, id)).limit(1);
|
|
181
|
+
if (rows.length === 0) return null;
|
|
182
|
+
const row = rows[0] as any;
|
|
183
|
+
return { ...row, url: row.storagePath };
|
|
184
|
+
},
|
|
185
|
+
|
|
186
|
+
async findByUrl(url: string): Promise<AssetRecord | null> {
|
|
187
|
+
const db = await getDb();
|
|
188
|
+
const schema = getSchema();
|
|
189
|
+
const rows = await db.select().from(schema.cmsAssets).where(eq(schema.cmsAssets.storagePath, url)).limit(1);
|
|
190
|
+
if (rows.length === 0) return null;
|
|
191
|
+
const row = rows[0] as any;
|
|
192
|
+
return { ...row, url: row.storagePath };
|
|
193
|
+
},
|
|
194
|
+
|
|
195
|
+
async findByHash(hash: string): Promise<AssetRecord | null> {
|
|
196
|
+
const db = await getDb();
|
|
197
|
+
const schema = getSchema();
|
|
198
|
+
const rows = await db.select().from(schema.cmsAssets).where(eq(schema.cmsAssets.hash, hash)).limit(1);
|
|
199
|
+
if (rows.length === 0) return null;
|
|
200
|
+
const row = rows[0] as any;
|
|
201
|
+
return { ...row, url: row.storagePath };
|
|
202
|
+
},
|
|
203
|
+
|
|
204
|
+
async delete(id: string, context?: AssetContext): Promise<void> {
|
|
205
|
+
const db = await getDb();
|
|
206
|
+
const schema = getSchema();
|
|
207
|
+
const storage = getStorage();
|
|
208
|
+
const rows = await db.select().from(schema.cmsAssets).where(eq(schema.cmsAssets._id, id)).limit(1);
|
|
209
|
+
if (rows.length === 0) return;
|
|
210
|
+
const asset = rows[0] as any;
|
|
211
|
+
await storage.deleteFile(asset.storagePath);
|
|
212
|
+
await db.delete(schema.cmsAssets).where(eq(schema.cmsAssets._id, id));
|
|
213
|
+
|
|
214
|
+
logAudit({
|
|
215
|
+
action: "asset.delete",
|
|
216
|
+
resourceType: "asset",
|
|
217
|
+
resourceId: id,
|
|
218
|
+
actor: context?.actor ?? null,
|
|
219
|
+
});
|
|
220
|
+
},
|
|
221
|
+
|
|
222
|
+
async update(
|
|
223
|
+
id: string,
|
|
224
|
+
data: { alt?: string; filename?: string; folder?: string | null; focalX?: number | null; focalY?: number | null },
|
|
225
|
+
context?: AssetContext,
|
|
226
|
+
): Promise<AssetRecord | null> {
|
|
227
|
+
const db = await getDb();
|
|
228
|
+
const schema = getSchema();
|
|
229
|
+
const rows = await db.select().from(schema.cmsAssets).where(eq(schema.cmsAssets._id, id)).limit(1);
|
|
230
|
+
if (rows.length === 0) return null;
|
|
231
|
+
|
|
232
|
+
const updateValues: Record<string, unknown> = {};
|
|
233
|
+
if (data.alt !== undefined) updateValues.alt = data.alt;
|
|
234
|
+
if (data.filename !== undefined) updateValues.filename = data.filename;
|
|
235
|
+
if (data.folder !== undefined) updateValues.folder = data.folder;
|
|
236
|
+
if (data.focalX !== undefined) updateValues.focalX = data.focalX;
|
|
237
|
+
if (data.focalY !== undefined) updateValues.focalY = data.focalY;
|
|
238
|
+
|
|
239
|
+
if (Object.keys(updateValues).length > 0) {
|
|
240
|
+
await db.update(schema.cmsAssets).set(updateValues).where(eq(schema.cmsAssets._id, id));
|
|
241
|
+
logAudit({
|
|
242
|
+
action: "asset.update",
|
|
243
|
+
resourceType: "asset",
|
|
244
|
+
resourceId: id,
|
|
245
|
+
actor: context?.actor ?? null,
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return this.findById(id);
|
|
250
|
+
},
|
|
251
|
+
|
|
252
|
+
async count(options: { folder?: string | null; search?: string } = {}): Promise<number> {
|
|
253
|
+
const db = await getDb();
|
|
254
|
+
const schema = getSchema();
|
|
255
|
+
const rows = await db
|
|
256
|
+
.select({ count: sql<number>`count(*)` })
|
|
257
|
+
.from(schema.cmsAssets)
|
|
258
|
+
.where(buildAssetFilter(schema, options));
|
|
259
|
+
return Number(rows[0]?.count ?? 0);
|
|
260
|
+
},
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
// Shared WHERE builder for asset find/count: optional folder scope + filename search.
|
|
264
|
+
function buildAssetFilter(schema: ReturnType<typeof getSchema>, options: { folder?: string | null; search?: string }) {
|
|
265
|
+
const conditions = [];
|
|
266
|
+
if (options.folder !== undefined) {
|
|
267
|
+
conditions.push(
|
|
268
|
+
options.folder === null ? isNull(schema.cmsAssets.folder) : eq(schema.cmsAssets.folder, options.folder),
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
if (options.search?.trim()) {
|
|
272
|
+
// Match the term against both the filename and the alt text.
|
|
273
|
+
const term = `%${options.search.trim()}%`;
|
|
274
|
+
conditions.push(or(like(schema.cmsAssets.filename, term), like(schema.cmsAssets.alt, term)));
|
|
275
|
+
}
|
|
276
|
+
return conditions.length ? and(...conditions) : undefined;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export const folders = {
|
|
280
|
+
async create(name: string, parent?: string | null): Promise<FolderRecord> {
|
|
281
|
+
const db = await getDb();
|
|
282
|
+
const schema = getSchema();
|
|
283
|
+
const id = nanoid();
|
|
284
|
+
const createdAt = new Date().toISOString();
|
|
285
|
+
|
|
286
|
+
await db.insert(schema.cmsAssetFolders).values({
|
|
287
|
+
_id: id,
|
|
288
|
+
name,
|
|
289
|
+
parent: parent ?? null,
|
|
290
|
+
_createdAt: createdAt,
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
return { _id: id, name, parent: parent ?? null, _createdAt: createdAt };
|
|
294
|
+
},
|
|
295
|
+
|
|
296
|
+
async findByParent(parent: string | null): Promise<FolderRecord[]> {
|
|
297
|
+
const db = await getDb();
|
|
298
|
+
const schema = getSchema();
|
|
299
|
+
const condition =
|
|
300
|
+
parent === null ? isNull(schema.cmsAssetFolders.parent) : eq(schema.cmsAssetFolders.parent, parent);
|
|
301
|
+
const rows = await db.select().from(schema.cmsAssetFolders).where(condition).orderBy(schema.cmsAssetFolders.name);
|
|
302
|
+
return rows as FolderRecord[];
|
|
303
|
+
},
|
|
304
|
+
|
|
305
|
+
async findById(id: string): Promise<FolderRecord | null> {
|
|
306
|
+
const db = await getDb();
|
|
307
|
+
const schema = getSchema();
|
|
308
|
+
const rows = await db.select().from(schema.cmsAssetFolders).where(eq(schema.cmsAssetFolders._id, id)).limit(1);
|
|
309
|
+
return rows.length > 0 ? (rows[0] as FolderRecord) : null;
|
|
310
|
+
},
|
|
311
|
+
|
|
312
|
+
async findAll(): Promise<FolderRecord[]> {
|
|
313
|
+
const db = await getDb();
|
|
314
|
+
const schema = getSchema();
|
|
315
|
+
const rows = await db.select().from(schema.cmsAssetFolders).orderBy(schema.cmsAssetFolders.name);
|
|
316
|
+
return rows as FolderRecord[];
|
|
317
|
+
},
|
|
318
|
+
|
|
319
|
+
async rename(id: string, name: string): Promise<FolderRecord | null> {
|
|
320
|
+
const db = await getDb();
|
|
321
|
+
const schema = getSchema();
|
|
322
|
+
await db.update(schema.cmsAssetFolders).set({ name }).where(eq(schema.cmsAssetFolders._id, id));
|
|
323
|
+
return this.findById(id);
|
|
324
|
+
},
|
|
325
|
+
|
|
326
|
+
async delete(id: string): Promise<void> {
|
|
327
|
+
const db = await getDb();
|
|
328
|
+
const schema = getSchema();
|
|
329
|
+
// Un-file assets in this folder so they resurface under "Unfiled" rather than
|
|
330
|
+
// pointing at a folder that no longer exists (which would orphan them).
|
|
331
|
+
await db.update(schema.cmsAssets).set({ folder: null }).where(eq(schema.cmsAssets.folder, id));
|
|
332
|
+
// Promote any direct subfolders to the top level so the tree stays reachable.
|
|
333
|
+
await db.update(schema.cmsAssetFolders).set({ parent: null }).where(eq(schema.cmsAssetFolders.parent, id));
|
|
334
|
+
await db.delete(schema.cmsAssetFolders).where(eq(schema.cmsAssetFolders._id, id));
|
|
335
|
+
},
|
|
336
|
+
};
|
package/core/audit.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { lt } from "drizzle-orm";
|
|
2
|
+
import { nanoid } from "nanoid";
|
|
3
|
+
|
|
4
|
+
import { log } from "./log";
|
|
5
|
+
import { trackTask } from "./request-scope";
|
|
6
|
+
import { getDb } from "./runtime";
|
|
7
|
+
import { getSchema } from "./schema";
|
|
8
|
+
|
|
9
|
+
export type AuditActor = { id: string; email: string; role: string } | null;
|
|
10
|
+
|
|
11
|
+
export type AuditEvent = {
|
|
12
|
+
action: string;
|
|
13
|
+
resourceType: "content" | "asset" | "user" | "invite" | "session" | "password_reset";
|
|
14
|
+
resourceCollection?: string | null;
|
|
15
|
+
resourceId?: string | null;
|
|
16
|
+
actor?: AuditActor;
|
|
17
|
+
/** Used when the actor is unknown but we still want to record an attempted identifier (e.g. failed login). */
|
|
18
|
+
attemptedEmail?: string | null;
|
|
19
|
+
ipAddress?: string | null;
|
|
20
|
+
userAgent?: string | null;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const extractIp = (request: Request): string | null => {
|
|
24
|
+
const forwarded = request.headers.get("x-forwarded-for");
|
|
25
|
+
if (forwarded) return forwarded.split(",")[0]!.trim();
|
|
26
|
+
return request.headers.get("x-real-ip");
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const auditRequestMeta = (request: Request) => ({
|
|
30
|
+
ipAddress: extractIp(request),
|
|
31
|
+
userAgent: request.headers.get("user-agent"),
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
/** One-way reference: audit rows reach the log stream, so the token itself must not. */
|
|
35
|
+
export const tokenReference = async (token: string): Promise<string> => {
|
|
36
|
+
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(token));
|
|
37
|
+
const hex = [...new Uint8Array(digest)]
|
|
38
|
+
.slice(0, 8)
|
|
39
|
+
.map((byte) => byte.toString(16).padStart(2, "0"))
|
|
40
|
+
.join("");
|
|
41
|
+
return `sha256:${hex}`;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/** Fire-and-forget audit write, deferred to the active request scope (or script flush). */
|
|
45
|
+
export const logAudit = (event: AuditEvent): void => {
|
|
46
|
+
trackTask(recordAudit(event));
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const recordAudit = async (event: AuditEvent): Promise<void> => {
|
|
50
|
+
const row = {
|
|
51
|
+
_id: nanoid(),
|
|
52
|
+
timestamp: Date.now(),
|
|
53
|
+
actorId: event.actor?.id ?? null,
|
|
54
|
+
actorEmail: event.actor?.email ?? event.attemptedEmail ?? null,
|
|
55
|
+
actorRole: event.actor?.role ?? null,
|
|
56
|
+
action: event.action,
|
|
57
|
+
resourceType: event.resourceType,
|
|
58
|
+
resourceCollection: event.resourceCollection ?? null,
|
|
59
|
+
resourceId: event.resourceId ?? null,
|
|
60
|
+
ipAddress: event.ipAddress ?? null,
|
|
61
|
+
userAgent: event.userAgent ?? null,
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// Structured log line. Filter by `kind: "audit"` in Cloudflare Workers Logs,
|
|
65
|
+
// `wrangler tail`, or any stdout-to-log pipeline (Logpush, Datadog, Axiom…).
|
|
66
|
+
// Emitted at "info" — set CMS_LOG_LEVEL=warn to silence during bulk scripts.
|
|
67
|
+
log.info(JSON.stringify({ kind: "audit", ...row }));
|
|
68
|
+
|
|
69
|
+
try {
|
|
70
|
+
const db = await getDb();
|
|
71
|
+
const schema = getSchema();
|
|
72
|
+
await db.insert(schema.cmsAuditLog).values(row);
|
|
73
|
+
} catch (error) {
|
|
74
|
+
log.warn("[audit] failed to persist event", event.action, error);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export const pruneAuditLog = async (olderThanMs: number): Promise<number> => {
|
|
79
|
+
const db = await getDb();
|
|
80
|
+
const schema = getSchema();
|
|
81
|
+
const cutoff = Date.now() - olderThanMs;
|
|
82
|
+
const result: any = await db.delete(schema.cmsAuditLog).where(lt(schema.cmsAuditLog.timestamp, cutoff));
|
|
83
|
+
return Number(result?.changes ?? result?.rowsAffected ?? 0);
|
|
84
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { AdminAuthConfig, AdminAuthSsoProviderConfig, CMSConfig } from "./define";
|
|
2
|
+
|
|
3
|
+
export type ResolvedAdminAuthConfig = {
|
|
4
|
+
provider: "local" | "better-auth" | "workos" | "custom";
|
|
5
|
+
password: {
|
|
6
|
+
enabled: boolean;
|
|
7
|
+
forgotPassword: boolean;
|
|
8
|
+
emailVerification: boolean;
|
|
9
|
+
};
|
|
10
|
+
mfa: {
|
|
11
|
+
totp: boolean;
|
|
12
|
+
backupCodes: boolean;
|
|
13
|
+
passkeys: boolean;
|
|
14
|
+
};
|
|
15
|
+
ssoProviders: AdminAuthSsoProviderConfig[];
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const customAuth = (provider: Extract<NonNullable<AdminAuthConfig["provider"]>, { kind: "custom" }>) => provider;
|
|
19
|
+
|
|
20
|
+
export const resolveAdminAuth = (config: CMSConfig): ResolvedAdminAuthConfig => {
|
|
21
|
+
const auth = config.admin?.auth;
|
|
22
|
+
const providerConfig = auth?.provider;
|
|
23
|
+
const provider = typeof providerConfig === "object" ? "custom" : (providerConfig ?? "local");
|
|
24
|
+
const passwordEnabled = auth?.password?.enabled ?? provider !== "workos";
|
|
25
|
+
const forgotPassword = auth?.password?.forgotPassword ?? (provider === "local" || provider === "better-auth");
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
provider,
|
|
29
|
+
password: {
|
|
30
|
+
enabled: passwordEnabled,
|
|
31
|
+
forgotPassword: passwordEnabled && forgotPassword,
|
|
32
|
+
emailVerification: auth?.password?.emailVerification ?? false,
|
|
33
|
+
},
|
|
34
|
+
mfa: {
|
|
35
|
+
totp: auth?.mfa?.totp ?? false,
|
|
36
|
+
backupCodes: auth?.mfa?.backupCodes ?? false,
|
|
37
|
+
passkeys: auth?.mfa?.passkeys ?? false,
|
|
38
|
+
},
|
|
39
|
+
ssoProviders: auth?.sso?.providers ?? [],
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const getSsoProvider = (config: CMSConfig, providerId: string) =>
|
|
44
|
+
resolveAdminAuth(config).ssoProviders.find((provider) => provider.id === providerId) ?? null;
|