@kidecms/core 0.1.11 → 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/{pages/admin/login.astro → admin/forgot-password.astro} +21 -19
- package/routes/{pages/admin → admin}/invite.astro +6 -6
- package/routes/admin/login.astro +103 -0
- package/routes/admin/reset-password.astro +73 -0
- package/routes/{pages/admin → admin}/setup.astro +31 -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/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/dist/seed.js
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import { sql } from "drizzle-orm";
|
|
2
|
-
import { nanoid } from "nanoid";
|
|
3
|
-
import { hashPassword } from "./auth";
|
|
4
|
-
import { getDb } from "./runtime";
|
|
5
|
-
import { getSchema } from "./schema";
|
|
6
|
-
import { cloneValue, slugify } from "./values";
|
|
7
|
-
const isJsonField = (field) => field.type === "richText" ||
|
|
8
|
-
field.type === "array" ||
|
|
9
|
-
field.type === "json" ||
|
|
10
|
-
field.type === "blocks" ||
|
|
11
|
-
(field.type === "relation" && field.hasMany);
|
|
12
|
-
const serializeForDb = (collection, data) => {
|
|
13
|
-
const result = {};
|
|
14
|
-
for (const [key, value] of Object.entries(data)) {
|
|
15
|
-
const field = collection.fields[key];
|
|
16
|
-
result[key] = field && isJsonField(field) && value !== undefined && value !== null ? JSON.stringify(value) : value;
|
|
17
|
-
}
|
|
18
|
-
return result;
|
|
19
|
-
};
|
|
20
|
-
export const seedDatabase = async (config, seedData) => {
|
|
21
|
-
const db = await getDb();
|
|
22
|
-
const schema = getSchema();
|
|
23
|
-
for (const collection of config.collections) {
|
|
24
|
-
const collectionSeed = seedData[collection.slug];
|
|
25
|
-
if (!collectionSeed || collectionSeed.length === 0)
|
|
26
|
-
continue;
|
|
27
|
-
const tables = schema.cmsTables[collection.slug];
|
|
28
|
-
if (!tables)
|
|
29
|
-
continue;
|
|
30
|
-
const countResult = await db.select({ count: sql `count(*)` }).from(tables.main);
|
|
31
|
-
const rowCount = Number(countResult[0]?.count ?? 0);
|
|
32
|
-
if (rowCount > 0)
|
|
33
|
-
continue;
|
|
34
|
-
console.log(` Seeding ${collection.labels.plural}...`);
|
|
35
|
-
for (const seedDoc of collectionSeed) {
|
|
36
|
-
const timestamp = new Date().toISOString();
|
|
37
|
-
const docId = typeof seedDoc._id === "string" ? String(seedDoc._id) : nanoid();
|
|
38
|
-
const { _id, _status, ...fieldData } = seedDoc;
|
|
39
|
-
for (const [fieldName, field] of Object.entries(collection.fields)) {
|
|
40
|
-
if (field.type !== "slug")
|
|
41
|
-
continue;
|
|
42
|
-
if (fieldData[fieldName]) {
|
|
43
|
-
fieldData[fieldName] = slugify(String(fieldData[fieldName]));
|
|
44
|
-
}
|
|
45
|
-
else if (field.from && fieldData[field.from]) {
|
|
46
|
-
fieldData[fieldName] = slugify(String(fieldData[field.from]));
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
for (const [fieldName, field] of Object.entries(collection.fields)) {
|
|
50
|
-
if (fieldData[fieldName] === undefined && field.defaultValue !== undefined) {
|
|
51
|
-
fieldData[fieldName] = cloneValue(field.defaultValue);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
if (collection.auth && typeof fieldData.password === "string") {
|
|
55
|
-
fieldData.password = await hashPassword(fieldData.password);
|
|
56
|
-
}
|
|
57
|
-
const serialized = serializeForDb(collection, fieldData);
|
|
58
|
-
const docValues = {
|
|
59
|
-
_id: docId,
|
|
60
|
-
...serialized,
|
|
61
|
-
};
|
|
62
|
-
if (collection.drafts) {
|
|
63
|
-
const status = _status === "published" ? "published" : "draft";
|
|
64
|
-
docValues._status = status;
|
|
65
|
-
if (status === "published")
|
|
66
|
-
docValues._publishedAt = timestamp;
|
|
67
|
-
}
|
|
68
|
-
if (collection.timestamps !== false) {
|
|
69
|
-
docValues._createdAt = timestamp;
|
|
70
|
-
docValues._updatedAt = timestamp;
|
|
71
|
-
}
|
|
72
|
-
await db.insert(tables.main).values(docValues);
|
|
73
|
-
if (collection.versions && tables.versions) {
|
|
74
|
-
await db.insert(tables.versions).values({
|
|
75
|
-
_id: nanoid(),
|
|
76
|
-
_docId: docId,
|
|
77
|
-
_version: 1,
|
|
78
|
-
_snapshot: JSON.stringify({ ...fieldData, _status: docValues._status }),
|
|
79
|
-
_createdAt: timestamp,
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
};
|
package/dist/values.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { FieldConfig, RichTextDocument } from "./define";
|
|
2
|
-
export declare const cloneValue: <T>(value: T) => T;
|
|
3
|
-
export declare const slugify: (value: string) => string;
|
|
4
|
-
export declare const escapeHtml: (value: string) => string;
|
|
5
|
-
export declare const createRichTextFromPlainText: (text: string) => RichTextDocument;
|
|
6
|
-
export declare const renderRichText: (document?: RichTextDocument | null) => string;
|
|
7
|
-
export declare const richTextToPlainText: (document?: RichTextDocument | null) => string;
|
|
8
|
-
export declare const serializeFieldValue: (field: FieldConfig, value: unknown) => string;
|
package/dist/values.js
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import { cmsImage, cmsSrcset } from "./image";
|
|
2
|
-
export const cloneValue = (value) => JSON.parse(JSON.stringify(value));
|
|
3
|
-
export const slugify = (value) => value
|
|
4
|
-
.normalize("NFKD")
|
|
5
|
-
.replace(/[^\w\s-]/g, "")
|
|
6
|
-
.trim()
|
|
7
|
-
.toLowerCase()
|
|
8
|
-
.replace(/[\s_-]+/g, "-")
|
|
9
|
-
.replace(/^-+|-+$/g, "");
|
|
10
|
-
export const escapeHtml = (value) => value
|
|
11
|
-
.replaceAll("&", "&")
|
|
12
|
-
.replaceAll("<", "<")
|
|
13
|
-
.replaceAll(">", ">")
|
|
14
|
-
.replaceAll('"', """)
|
|
15
|
-
.replaceAll("'", "'");
|
|
16
|
-
export const createRichTextFromPlainText = (text) => ({
|
|
17
|
-
type: "root",
|
|
18
|
-
children: text
|
|
19
|
-
.split(/\n{2,}/)
|
|
20
|
-
.map((paragraph) => paragraph.trim())
|
|
21
|
-
.filter(Boolean)
|
|
22
|
-
.map((paragraph) => ({
|
|
23
|
-
type: "paragraph",
|
|
24
|
-
children: paragraph
|
|
25
|
-
.split("\n")
|
|
26
|
-
.map((line, index, lines) => `${line}${index < lines.length - 1 ? "\n" : ""}`)
|
|
27
|
-
.filter(Boolean)
|
|
28
|
-
.map((line) => ({ type: "text", value: line })),
|
|
29
|
-
})),
|
|
30
|
-
});
|
|
31
|
-
const renderNode = (node) => {
|
|
32
|
-
if (node.type === "text") {
|
|
33
|
-
let content = escapeHtml(String(node.value ?? ""));
|
|
34
|
-
if (node.bold)
|
|
35
|
-
content = `<strong>${content}</strong>`;
|
|
36
|
-
if (node.italic)
|
|
37
|
-
content = `<em>${content}</em>`;
|
|
38
|
-
if (node.href) {
|
|
39
|
-
const href = escapeHtml(String(node.href));
|
|
40
|
-
const isExternal = String(node.href).startsWith("http://") || String(node.href).startsWith("https://");
|
|
41
|
-
const target = isExternal ? ' target="_blank" rel="noopener noreferrer"' : "";
|
|
42
|
-
content = `<a href="${href}"${target}>${content}</a>`;
|
|
43
|
-
}
|
|
44
|
-
return content;
|
|
45
|
-
}
|
|
46
|
-
if (node.type === "paragraph") {
|
|
47
|
-
return `<p>${(node.children ?? []).map(renderNode).join("")}</p>`;
|
|
48
|
-
}
|
|
49
|
-
if (node.type === "heading") {
|
|
50
|
-
const level = Math.min(Math.max(Number(node.level ?? 2), 1), 6);
|
|
51
|
-
return `<h${level}>${(node.children ?? []).map(renderNode).join("")}</h${level}>`;
|
|
52
|
-
}
|
|
53
|
-
if (node.type === "list") {
|
|
54
|
-
const tag = node.ordered ? "ol" : "ul";
|
|
55
|
-
return `<${tag}>${(node.children ?? []).map(renderNode).join("")}</${tag}>`;
|
|
56
|
-
}
|
|
57
|
-
if (node.type === "list-item") {
|
|
58
|
-
return `<li>${(node.children ?? []).map(renderNode).join("")}</li>`;
|
|
59
|
-
}
|
|
60
|
-
if (node.type === "quote") {
|
|
61
|
-
return `<blockquote>${(node.children ?? []).map(renderNode).join("")}</blockquote>`;
|
|
62
|
-
}
|
|
63
|
-
if (node.type === "image") {
|
|
64
|
-
const src = String(node.src ?? "");
|
|
65
|
-
const alt = escapeHtml(String(node.alt ?? ""));
|
|
66
|
-
const isLocal = src.startsWith("/uploads/");
|
|
67
|
-
if (isLocal) {
|
|
68
|
-
return `<img src="${escapeHtml(cmsImage(src, 1024))}" srcset="${escapeHtml(cmsSrcset(src))}" sizes="(max-width: 768px) 100vw, 768px" alt="${alt}" loading="lazy" class="h-auto max-w-full rounded-lg" />`;
|
|
69
|
-
}
|
|
70
|
-
return `<img src="${escapeHtml(src)}" alt="${alt}" loading="lazy" class="max-w-full rounded-lg" />`;
|
|
71
|
-
}
|
|
72
|
-
return "";
|
|
73
|
-
};
|
|
74
|
-
export const renderRichText = (document) => {
|
|
75
|
-
if (!document || document.type !== "root")
|
|
76
|
-
return "";
|
|
77
|
-
return document.children.map(renderNode).join("");
|
|
78
|
-
};
|
|
79
|
-
export const richTextToPlainText = (document) => {
|
|
80
|
-
if (!document?.children)
|
|
81
|
-
return "";
|
|
82
|
-
const flatten = (node) => {
|
|
83
|
-
if (node.type === "text") {
|
|
84
|
-
return String(node.value ?? "");
|
|
85
|
-
}
|
|
86
|
-
return (node.children ?? []).map(flatten).join("");
|
|
87
|
-
};
|
|
88
|
-
return document.children.map(flatten).join("\n\n").trim();
|
|
89
|
-
};
|
|
90
|
-
export const serializeFieldValue = (field, value) => {
|
|
91
|
-
if (value === null || value === undefined)
|
|
92
|
-
return "";
|
|
93
|
-
if (field.type === "richText")
|
|
94
|
-
return richTextToPlainText(value);
|
|
95
|
-
if (field.type === "array")
|
|
96
|
-
return Array.isArray(value) ? value.map((item) => String(item ?? "")).join(", ") : "";
|
|
97
|
-
if (field.type === "json" || field.type === "blocks")
|
|
98
|
-
return JSON.stringify(value, null, 2);
|
|
99
|
-
if (field.type === "boolean")
|
|
100
|
-
return value ? "true" : "false";
|
|
101
|
-
return String(value);
|
|
102
|
-
};
|
package/dist/webhooks.d.ts
DELETED
package/dist/webhooks.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
const MAX_RETRIES = 3;
|
|
2
|
-
const TIMEOUT_MS = 5000;
|
|
3
|
-
const RETRY_DELAYS = [1000, 3000, 9000];
|
|
4
|
-
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
5
|
-
async function deliverOnce(webhook, body) {
|
|
6
|
-
const controller = new AbortController();
|
|
7
|
-
const timeout = setTimeout(() => controller.abort(), TIMEOUT_MS);
|
|
8
|
-
try {
|
|
9
|
-
const response = await fetch(webhook.url, {
|
|
10
|
-
method: webhook.method ?? "POST",
|
|
11
|
-
headers: { "Content-Type": "application/json", ...webhook.headers },
|
|
12
|
-
body,
|
|
13
|
-
signal: controller.signal,
|
|
14
|
-
});
|
|
15
|
-
return { ok: response.ok, status: response.status };
|
|
16
|
-
}
|
|
17
|
-
catch (err) {
|
|
18
|
-
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
19
|
-
}
|
|
20
|
-
finally {
|
|
21
|
-
clearTimeout(timeout);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
async function deliverWebhook(webhook, body) {
|
|
25
|
-
for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
|
|
26
|
-
const result = await deliverOnce(webhook, body);
|
|
27
|
-
if (result.ok) {
|
|
28
|
-
if (attempt > 0) {
|
|
29
|
-
console.log(` [webhook] ${webhook.name} delivered after ${attempt + 1} attempts`);
|
|
30
|
-
}
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
const detail = result.status ? `HTTP ${result.status}` : result.error;
|
|
34
|
-
if (attempt < MAX_RETRIES) {
|
|
35
|
-
console.warn(` [webhook] ${webhook.name} failed (${detail}) — retrying in ${RETRY_DELAYS[attempt]}ms`);
|
|
36
|
-
await sleep(RETRY_DELAYS[attempt]);
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
console.error(` [webhook] ${webhook.name} failed permanently after ${MAX_RETRIES + 1} attempts: ${detail}`);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
export async function dispatchWebhooks(config, event, collectionSlug, doc, user) {
|
|
44
|
-
const webhooks = config.admin?.webhooks;
|
|
45
|
-
if (!webhooks || webhooks.length === 0)
|
|
46
|
-
return;
|
|
47
|
-
const matching = webhooks.filter((webhook) => {
|
|
48
|
-
if (!webhook.events.includes(event))
|
|
49
|
-
return false;
|
|
50
|
-
if (webhook.collections && !webhook.collections.includes(collectionSlug))
|
|
51
|
-
return false;
|
|
52
|
-
return true;
|
|
53
|
-
});
|
|
54
|
-
if (matching.length === 0)
|
|
55
|
-
return;
|
|
56
|
-
const context = {
|
|
57
|
-
user: user ?? null,
|
|
58
|
-
event,
|
|
59
|
-
collection: collectionSlug,
|
|
60
|
-
timestamp: new Date().toISOString(),
|
|
61
|
-
};
|
|
62
|
-
// Fire all matching webhooks in parallel — don't block the operation
|
|
63
|
-
for (const webhook of matching) {
|
|
64
|
-
const payload = webhook.payload
|
|
65
|
-
? webhook.payload(doc, context)
|
|
66
|
-
: { event, collection: collectionSlug, doc, user, timestamp: context.timestamp };
|
|
67
|
-
const body = JSON.stringify(payload);
|
|
68
|
-
// Fire and forget — failures are logged but don't bubble up
|
|
69
|
-
deliverWebhook(webhook, body).catch((err) => {
|
|
70
|
-
console.error(` [webhook] ${webhook.name} dispatcher error:`, err);
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
}
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import type { APIRoute } from "astro";
|
|
2
|
-
import { assets } from "virtual:kide/runtime";
|
|
3
|
-
import config from "virtual:kide/config";
|
|
4
|
-
|
|
5
|
-
export const prerender = false;
|
|
6
|
-
|
|
7
|
-
const DEFAULT_ALLOWED_TYPES = [
|
|
8
|
-
"image/jpeg",
|
|
9
|
-
"image/png",
|
|
10
|
-
"image/gif",
|
|
11
|
-
"image/webp",
|
|
12
|
-
"image/avif",
|
|
13
|
-
"image/svg+xml",
|
|
14
|
-
"application/pdf",
|
|
15
|
-
"video/mp4",
|
|
16
|
-
"video/webm",
|
|
17
|
-
];
|
|
18
|
-
|
|
19
|
-
const ALLOWED_TYPES = new Set(config.admin?.uploads?.allowedTypes ?? DEFAULT_ALLOWED_TYPES);
|
|
20
|
-
const MAX_FILE_SIZE = config.admin?.uploads?.maxFileSize ?? 50 * 1024 * 1024; // 50 MB
|
|
21
|
-
|
|
22
|
-
// Magic number signatures for binary file type verification
|
|
23
|
-
const MAGIC_SIGNATURES: Array<{ type: string; bytes: number[]; offset?: number }> = [
|
|
24
|
-
{ type: "image/jpeg", bytes: [0xff, 0xd8, 0xff] },
|
|
25
|
-
{ type: "image/png", bytes: [0x89, 0x50, 0x4e, 0x47] },
|
|
26
|
-
{ type: "image/gif", bytes: [0x47, 0x49, 0x46, 0x38] },
|
|
27
|
-
{ type: "image/webp", bytes: [0x52, 0x49, 0x46, 0x46], offset: 0 }, // RIFF....WEBP
|
|
28
|
-
{ type: "application/pdf", bytes: [0x25, 0x50, 0x44, 0x46] },
|
|
29
|
-
{ type: "video/mp4", bytes: [0x66, 0x74, 0x79, 0x70], offset: 4 }, // ....ftyp
|
|
30
|
-
];
|
|
31
|
-
|
|
32
|
-
function verifyMagicBytes(buffer: ArrayBuffer, declaredType: string): boolean {
|
|
33
|
-
// SVG and text-based formats can't be verified by magic bytes
|
|
34
|
-
if (declaredType === "image/svg+xml") {
|
|
35
|
-
const text = new TextDecoder().decode(buffer.slice(0, 256));
|
|
36
|
-
return text.includes("<svg") || text.trimStart().startsWith("<?xml");
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const header = new Uint8Array(buffer.slice(0, 16));
|
|
40
|
-
for (const sig of MAGIC_SIGNATURES) {
|
|
41
|
-
if (sig.type !== declaredType) continue;
|
|
42
|
-
const offset = sig.offset ?? 0;
|
|
43
|
-
const match = sig.bytes.every((byte, i) => header[offset + i] === byte);
|
|
44
|
-
if (match) return true;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// AVIF is an ISOBMFF container — check for ftyp box with avif brand
|
|
48
|
-
if (declaredType === "image/avif") {
|
|
49
|
-
const offset4 = new Uint8Array(buffer.slice(4, 12));
|
|
50
|
-
const ftypStr = String.fromCharCode(...offset4);
|
|
51
|
-
return ftypStr.startsWith("ftyp") && ftypStr.includes("avif");
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// video/webm starts with EBML header
|
|
55
|
-
if (declaredType === "video/webm") {
|
|
56
|
-
return header[0] === 0x1a && header[1] === 0x45 && header[2] === 0xdf && header[3] === 0xa3;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return false;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export const POST: APIRoute = async ({ request }) => {
|
|
63
|
-
const contentType = request.headers.get("content-type") ?? "";
|
|
64
|
-
|
|
65
|
-
if (!contentType.includes("multipart/form-data")) {
|
|
66
|
-
return Response.json({ error: "Expected multipart/form-data." }, { status: 400 });
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const formData = await request.formData();
|
|
70
|
-
const file = formData.get("file");
|
|
71
|
-
const alt = formData.get("alt");
|
|
72
|
-
const folder = formData.get("folder");
|
|
73
|
-
|
|
74
|
-
if (!file || !(file instanceof File)) {
|
|
75
|
-
return Response.json({ error: "No file provided." }, { status: 400 });
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (!ALLOWED_TYPES.has(file.type)) {
|
|
79
|
-
return Response.json({ error: `File type "${file.type}" is not allowed.` }, { status: 400 });
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (file.size > MAX_FILE_SIZE) {
|
|
83
|
-
return Response.json({ error: `File exceeds the ${MAX_FILE_SIZE / 1024 / 1024} MB size limit.` }, { status: 400 });
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// Verify file content matches declared type
|
|
87
|
-
const buffer = await file.arrayBuffer();
|
|
88
|
-
if (!verifyMagicBytes(buffer, file.type)) {
|
|
89
|
-
return Response.json({ error: "File content does not match declared type." }, { status: 400 });
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// Reconstruct File from buffer since arrayBuffer() consumed the stream
|
|
93
|
-
const verifiedFile = new File([buffer], file.name, { type: file.type });
|
|
94
|
-
|
|
95
|
-
const asset = await assets.upload(verifiedFile, {
|
|
96
|
-
alt: alt ? String(alt) : undefined,
|
|
97
|
-
folder: folder ? String(folder) : undefined,
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
const redirectTo = formData.get("redirectTo");
|
|
101
|
-
|
|
102
|
-
if (redirectTo) {
|
|
103
|
-
// Delay so Vite's dev server picks up the new file before the redirect
|
|
104
|
-
await new Promise((r) => setTimeout(r, 1000));
|
|
105
|
-
return new Response(null, {
|
|
106
|
-
status: 303,
|
|
107
|
-
headers: { Location: `/admin/assets/${asset._id}?_toast=success&_msg=Asset+uploaded` },
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return Response.json(asset, { status: 201 });
|
|
112
|
-
};
|
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import type { APIRoute } from "astro";
|
|
2
|
-
import { eq } from "drizzle-orm";
|
|
3
|
-
|
|
4
|
-
import { getDb } from "virtual:kide/db";
|
|
5
|
-
import { verifyPassword, createSession, setSessionCookie } from "virtual:kide/runtime";
|
|
6
|
-
import config from "virtual:kide/config";
|
|
7
|
-
|
|
8
|
-
export const prerender = false;
|
|
9
|
-
|
|
10
|
-
// Simple in-memory rate limiter
|
|
11
|
-
const attempts = new Map<string, { count: number; resetAt: number }>();
|
|
12
|
-
const MAX_ATTEMPTS = config.admin?.rateLimit?.maxAttempts ?? 5;
|
|
13
|
-
const WINDOW_MS = config.admin?.rateLimit?.windowMs ?? 15 * 60 * 1000;
|
|
14
|
-
|
|
15
|
-
function isRateLimited(ip: string): boolean {
|
|
16
|
-
const now = Date.now();
|
|
17
|
-
const entry = attempts.get(ip);
|
|
18
|
-
if (!entry || now > entry.resetAt) {
|
|
19
|
-
attempts.set(ip, { count: 1, resetAt: now + WINDOW_MS });
|
|
20
|
-
return false;
|
|
21
|
-
}
|
|
22
|
-
entry.count++;
|
|
23
|
-
return entry.count > MAX_ATTEMPTS;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export const POST: APIRoute = async ({ request, clientAddress }) => {
|
|
27
|
-
const contentType = request.headers.get("content-type") ?? "";
|
|
28
|
-
|
|
29
|
-
if (isRateLimited(clientAddress)) {
|
|
30
|
-
if (contentType.includes("application/json")) {
|
|
31
|
-
return Response.json({ error: "Too many login attempts. Try again later." }, { status: 429 });
|
|
32
|
-
}
|
|
33
|
-
return new Response(null, {
|
|
34
|
-
status: 303,
|
|
35
|
-
headers: { Location: "/admin/login?error=rate-limited" },
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
let email: string;
|
|
40
|
-
let password: string;
|
|
41
|
-
|
|
42
|
-
if (contentType.includes("application/json")) {
|
|
43
|
-
const body = await request.json();
|
|
44
|
-
email = String(body.email ?? "");
|
|
45
|
-
password = String(body.password ?? "");
|
|
46
|
-
} else {
|
|
47
|
-
const formData = await request.formData();
|
|
48
|
-
email = String(formData.get("email") ?? "");
|
|
49
|
-
password = String(formData.get("password") ?? "");
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
if (!email || !password) {
|
|
53
|
-
if (contentType.includes("application/json")) {
|
|
54
|
-
return Response.json({ error: "Email and password are required." }, { status: 400 });
|
|
55
|
-
}
|
|
56
|
-
return new Response(null, {
|
|
57
|
-
status: 303,
|
|
58
|
-
headers: { Location: "/admin/login?error=missing" },
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const db = await getDb();
|
|
63
|
-
const schema = await import("virtual:kide/schema");
|
|
64
|
-
const tables = schema.cmsTables as Record<string, { main: any }>;
|
|
65
|
-
|
|
66
|
-
if (!tables.users) {
|
|
67
|
-
return Response.json({ error: "Users collection not configured." }, { status: 500 });
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const rows = await db.select().from(tables.users.main).where(eq(tables.users.main.email, email)).limit(1);
|
|
71
|
-
|
|
72
|
-
if (rows.length === 0) {
|
|
73
|
-
if (contentType.includes("application/json")) {
|
|
74
|
-
return Response.json({ error: "Invalid credentials." }, { status: 401 });
|
|
75
|
-
}
|
|
76
|
-
return new Response(null, {
|
|
77
|
-
status: 303,
|
|
78
|
-
headers: { Location: "/admin/login?error=invalid" },
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const user = rows[0] as Record<string, unknown>;
|
|
83
|
-
const storedHash = String(user.password ?? "");
|
|
84
|
-
|
|
85
|
-
let valid = false;
|
|
86
|
-
try {
|
|
87
|
-
valid = await verifyPassword(storedHash, password);
|
|
88
|
-
} catch {
|
|
89
|
-
// valid remains false
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if (!valid) {
|
|
93
|
-
if (contentType.includes("application/json")) {
|
|
94
|
-
return Response.json({ error: "Invalid credentials." }, { status: 401 });
|
|
95
|
-
}
|
|
96
|
-
return new Response(null, {
|
|
97
|
-
status: 303,
|
|
98
|
-
headers: { Location: "/admin/login?error=invalid" },
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// Successful login — clear rate limit
|
|
103
|
-
attempts.delete(clientAddress);
|
|
104
|
-
|
|
105
|
-
const session = await createSession(String(user._id));
|
|
106
|
-
|
|
107
|
-
if (contentType.includes("application/json")) {
|
|
108
|
-
return new Response(JSON.stringify({ ok: true }), {
|
|
109
|
-
status: 200,
|
|
110
|
-
headers: {
|
|
111
|
-
"Content-Type": "application/json",
|
|
112
|
-
"Set-Cookie": setSessionCookie(session.token, session.expiresAt),
|
|
113
|
-
},
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
return new Response(null, {
|
|
118
|
-
status: 303,
|
|
119
|
-
headers: {
|
|
120
|
-
Location: "/admin",
|
|
121
|
-
"Set-Cookie": setSessionCookie(session.token, session.expiresAt),
|
|
122
|
-
},
|
|
123
|
-
});
|
|
124
|
-
};
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import type { APIRoute } from "astro";
|
|
2
|
-
import { nanoid } from "nanoid";
|
|
3
|
-
|
|
4
|
-
import { getDb } from "virtual:kide/db";
|
|
5
|
-
import { hashPassword, createSession, setSessionCookie } from "virtual:kide/runtime";
|
|
6
|
-
|
|
7
|
-
export const prerender = false;
|
|
8
|
-
|
|
9
|
-
export const POST: APIRoute = async ({ request }) => {
|
|
10
|
-
const formData = await request.formData();
|
|
11
|
-
const name = String(formData.get("name") ?? "").trim();
|
|
12
|
-
const email = String(formData.get("email") ?? "").trim();
|
|
13
|
-
const password = String(formData.get("password") ?? "");
|
|
14
|
-
const confirmPassword = String(formData.get("confirmPassword") ?? "");
|
|
15
|
-
|
|
16
|
-
if (!name || !email || !password) {
|
|
17
|
-
return new Response(null, {
|
|
18
|
-
status: 303,
|
|
19
|
-
headers: { Location: "/admin/setup?error=missing" },
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
if (password !== confirmPassword) {
|
|
24
|
-
return new Response(null, {
|
|
25
|
-
status: 303,
|
|
26
|
-
headers: { Location: "/admin/setup?error=password" },
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if (password.length < 8) {
|
|
31
|
-
return new Response(null, {
|
|
32
|
-
status: 303,
|
|
33
|
-
headers: { Location: "/admin/setup?error=short" },
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const db = await getDb();
|
|
38
|
-
const schema = await import("virtual:kide/schema");
|
|
39
|
-
const tables = schema.cmsTables as Record<string, { main: any }>;
|
|
40
|
-
|
|
41
|
-
if (!tables.users) {
|
|
42
|
-
return Response.json({ error: "Users collection not configured." }, { status: 500 });
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Prevent setup if users already exist
|
|
46
|
-
const existing = await db.select().from(tables.users.main).limit(1);
|
|
47
|
-
if (existing.length > 0) {
|
|
48
|
-
return new Response(null, {
|
|
49
|
-
status: 303,
|
|
50
|
-
headers: { Location: "/admin/login" },
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const id = nanoid();
|
|
55
|
-
const now = new Date().toISOString();
|
|
56
|
-
const hashedPassword = await hashPassword(password);
|
|
57
|
-
|
|
58
|
-
await db.insert(tables.users.main).values({
|
|
59
|
-
_id: id,
|
|
60
|
-
name,
|
|
61
|
-
email,
|
|
62
|
-
password: hashedPassword,
|
|
63
|
-
role: "admin",
|
|
64
|
-
_createdAt: now,
|
|
65
|
-
_updatedAt: now,
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
const session = await createSession(id);
|
|
69
|
-
|
|
70
|
-
return new Response(null, {
|
|
71
|
-
status: 303,
|
|
72
|
-
headers: {
|
|
73
|
-
Location: "/admin",
|
|
74
|
-
"Set-Cookie": setSessionCookie(session.token, session.expiresAt),
|
|
75
|
-
},
|
|
76
|
-
});
|
|
77
|
-
};
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import type { APIRoute } from "astro";
|
|
2
|
-
import { transformImage } from "@kidecms/core";
|
|
3
|
-
|
|
4
|
-
export const prerender = false;
|
|
5
|
-
|
|
6
|
-
export const GET: APIRoute = async ({ params, url }) => {
|
|
7
|
-
const src = `/${params.path}`;
|
|
8
|
-
const width = url.searchParams.get("w") ? Number(url.searchParams.get("w")) : undefined;
|
|
9
|
-
const format = url.searchParams.get("f") || "webp";
|
|
10
|
-
const quality = url.searchParams.get("q") ? Number(url.searchParams.get("q")) : undefined;
|
|
11
|
-
|
|
12
|
-
const result = await transformImage(src, width, format, quality);
|
|
13
|
-
|
|
14
|
-
if (!result) {
|
|
15
|
-
return new Response("Not found", { status: 404 });
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return new Response(new Uint8Array(result.buffer), {
|
|
19
|
-
headers: {
|
|
20
|
-
"Content-Type": result.contentType,
|
|
21
|
-
"Cache-Control": "public, max-age=31536000, immutable",
|
|
22
|
-
},
|
|
23
|
-
});
|
|
24
|
-
};
|