qarpeo 0.2.1 → 0.3.1
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/dist/chunks/tools.mjs +993 -31
- package/dist/cli/index.mjs +1071 -31
- package/dist/fate/index.d.ts +9 -1
- package/dist/fate/index.mjs +20 -3
- package/dist/module.mjs +296 -750
- package/dist/runtime/browser.mjs +2 -2
- package/dist/runtime/comments-server.d.ts +3 -0
- package/dist/runtime/comments-server.mjs +2 -0
- package/dist/runtime/comments.d.ts +0 -2
- package/dist/runtime/comments.mjs +0 -2
- package/dist/runtime/db.d.ts +10 -9
- package/dist/runtime/db.mjs +79 -8
- package/dist/runtime/drizzle.mjs +21 -6
- package/dist/runtime/index.d.ts +4 -0
- package/dist/runtime/realtime-server.d.ts +4 -0
- package/dist/runtime/realtime-server.mjs +13 -0
- package/dist/runtime/realtime.d.ts +1 -4
- package/dist/runtime/realtime.mjs +0 -13
- package/dist/runtime/replica.d.ts +27 -0
- package/dist/runtime/replica.mjs +229 -0
- package/dist/runtime/server/durable-objects/agent.mjs +14 -2
- package/dist/runtime/server/durable-objects/room.d.ts +10 -0
- package/dist/runtime/server/durable-objects/room.mjs +119 -28
- package/dist/runtime/server/middleware/devtools.mjs +1 -1
- package/dist/runtime/server/middleware/tenancy.mjs +7 -4
- package/dist/runtime/server/plugins/store.mjs +1 -1
- package/dist/runtime/server/routes/__qarpeo/mail/track/click/[id].get.mjs +57 -17
- package/dist/runtime/server/routes/agents/[agent]/chat.post.mjs +19 -12
- package/dist/runtime/server/routes/agents/[agent]/clear.post.mjs +8 -9
- package/dist/runtime/server/routes/agents/[agent]/history.get.mjs +8 -9
- package/dist/runtime/server/routes/analytics/events.get.mjs +12 -2
- package/dist/runtime/server/routes/analytics/pageviews.get.mjs +10 -1
- package/dist/runtime/server/routes/analytics/summary.get.mjs +10 -1
- package/dist/runtime/server/routes/auth/2fa/disable.post.mjs +11 -2
- package/dist/runtime/server/routes/auth/2fa/setup.post.mjs +7 -0
- package/dist/runtime/server/routes/auth/2fa/verify.post.mjs +11 -2
- package/dist/runtime/server/routes/auth/account.delete.mjs +7 -0
- package/dist/runtime/server/routes/auth/magic-link/verify.get.mjs +5 -3
- package/dist/runtime/server/routes/auth/organization/sso/index.post.mjs +10 -0
- package/dist/runtime/server/routes/auth/otp/verify.post.mjs +5 -3
- package/dist/runtime/server/routes/auth/sso/saml/acs.post.mjs +18 -0
- package/dist/runtime/server/routes/auth/webauthn/login/options.post.d.ts +1 -1
- package/dist/runtime/server/routes/auth/webauthn/login/options.post.mjs +11 -2
- package/dist/runtime/server/routes/auth/webauthn/login/verify.post.mjs +10 -1
- package/dist/runtime/server/routes/auth/webauthn/register/options.post.d.ts +1 -1
- package/dist/runtime/server/routes/auth/webauthn/register/options.post.mjs +10 -1
- package/dist/runtime/server/routes/auth/webauthn/register/verify.post.mjs +10 -1
- package/dist/runtime/server/routes/browser/crawl.post.mjs +13 -0
- package/dist/runtime/server/routes/browser/invoice.post.mjs +15 -1
- package/dist/runtime/server/routes/browser/og-image.post.mjs +13 -0
- package/dist/runtime/server/routes/browser/pdf.post.mjs +15 -1
- package/dist/runtime/server/routes/browser/screenshot.post.mjs +13 -0
- package/dist/runtime/server/routes/cms/[collection]/[id]/preview-token.post.mjs +23 -8
- package/dist/runtime/server/routes/cms/[collection]/[id]/publish.post.mjs +2 -1
- package/dist/runtime/server/routes/cms/[collection]/[id]/review.post.mjs +2 -1
- package/dist/runtime/server/routes/cms/[collection]/[id]/revisions/[revId]/restore.post.mjs +2 -1
- package/dist/runtime/server/routes/cms/[collection]/[id].delete.mjs +2 -1
- package/dist/runtime/server/routes/cms/[collection]/[id].put.mjs +2 -1
- package/dist/runtime/server/routes/cms/[collection]/index.post.mjs +2 -1
- package/dist/runtime/server/routes/cms/categories/[id].delete.mjs +2 -1
- package/dist/runtime/server/routes/cms/categories/[id].put.mjs +2 -1
- package/dist/runtime/server/routes/cms/categories.post.mjs +2 -1
- package/dist/runtime/server/routes/cms/collections/[id].delete.mjs +2 -1
- package/dist/runtime/server/routes/cms/collections/[id].put.mjs +2 -1
- package/dist/runtime/server/routes/cms/collections/index.post.mjs +2 -1
- package/dist/runtime/server/routes/cms/export.post.mjs +1 -1
- package/dist/runtime/server/routes/cms/subscribers/[id].delete.mjs +2 -1
- package/dist/runtime/server/routes/cms/subscribers/[id].patch.mjs +2 -1
- package/dist/runtime/server/routes/cms/tags/[id].delete.mjs +2 -1
- package/dist/runtime/server/routes/cms/tags.post.mjs +2 -1
- package/dist/runtime/server/routes/comments/[id]/react.post.mjs +7 -3
- package/dist/runtime/server/routes/comments/[id]/report.post.mjs +3 -0
- package/dist/runtime/server/routes/comments/[id].delete.mjs +1 -1
- package/dist/runtime/server/routes/comments/[id].get.mjs +13 -0
- package/dist/runtime/server/routes/comments/[id].patch.mjs +1 -1
- package/dist/runtime/server/routes/fate.d.ts +6 -0
- package/dist/runtime/server/routes/fate.mjs +32 -3
- package/dist/runtime/server/routes/mail/bounce.post.mjs +14 -2
- package/dist/runtime/server/routes/mail/unsubscribe.mjs +2 -1
- package/dist/runtime/server/routes/mail/webhooks.post.mjs +18 -4
- package/dist/runtime/server/routes/notifications/[id].get.mjs +3 -4
- package/dist/runtime/server/routes/notifications/index.get.mjs +4 -2
- package/dist/runtime/server/routes/notifications/push/subscribe.post.mjs +3 -1
- package/dist/runtime/server/routes/notifications/push/unsubscribe.post.mjs +5 -3
- package/dist/runtime/server/routes/notifications/read-all.post.mjs +6 -2
- package/dist/runtime/server/routes/notifications/send.post.mjs +13 -2
- package/dist/runtime/server/routes/payments/cancel.post.mjs +1 -1
- package/dist/runtime/server/routes/payments/change-plan.post.mjs +1 -1
- package/dist/runtime/server/routes/payments/checkout.post.mjs +1 -1
- package/dist/runtime/server/routes/payments/pause.post.mjs +1 -1
- package/dist/runtime/server/routes/payments/portal.post.mjs +1 -1
- package/dist/runtime/server/routes/payments/subscription.get.mjs +1 -1
- package/dist/runtime/server/routes/payments/usage.get.mjs +1 -1
- package/dist/runtime/server/routes/payments/usage.post.mjs +1 -1
- package/dist/runtime/server/routes/payments/webhook.post.mjs +8 -1
- package/dist/runtime/server/routes/realtime/broadcast.post.mjs +3 -2
- package/dist/runtime/server/routes/realtime/history.delete.mjs +2 -2
- package/dist/runtime/server/routes/realtime/history.get.mjs +2 -2
- package/dist/runtime/server/routes/realtime/presence.get.mjs +3 -3
- package/dist/runtime/server/routes/realtime/state.get.mjs +3 -3
- package/dist/runtime/server/routes/realtime/state.post.mjs +13 -4
- package/dist/runtime/server/routes/realtime/ws.get.mjs +2 -2
- package/dist/runtime/server/routes/storage/file/[...key].delete.mjs +1 -1
- package/dist/runtime/server/routes/storage/file/[...key].get.mjs +7 -0
- package/dist/runtime/server/routes/storage/files.get.d.ts +1 -1
- package/dist/runtime/server/routes/storage/files.get.mjs +24 -4
- package/dist/runtime/server/routes/storage/multipart/abort.post.mjs +16 -1
- package/dist/runtime/server/routes/storage/multipart/complete.post.d.ts +1 -1
- package/dist/runtime/server/routes/storage/multipart/complete.post.mjs +15 -1
- package/dist/runtime/server/routes/storage/multipart/create.post.mjs +7 -0
- package/dist/runtime/server/routes/storage/multipart/part.put.mjs +15 -1
- package/dist/runtime/server/routes/storage/presigned-r2.post.mjs +8 -1
- package/dist/runtime/server/routes/storage/presigned.post.d.ts +1 -1
- package/dist/runtime/server/routes/storage/presigned.post.mjs +8 -1
- package/dist/runtime/server/routes/storage/sign.post.mjs +7 -0
- package/dist/runtime/server/routes/storage/upload.post.d.ts +2 -2
- package/dist/runtime/server/routes/store/cart/index.delete.mjs +3 -4
- package/dist/runtime/server/routes/store/cart/index.get.mjs +3 -10
- package/dist/runtime/server/routes/store/cart/items/[id].delete.mjs +6 -4
- package/dist/runtime/server/routes/store/cart/items/[id].patch.mjs +12 -4
- package/dist/runtime/server/routes/store/cart/items.post.mjs +9 -10
- package/dist/runtime/server/routes/store/categories/index.get.mjs +4 -2
- package/dist/runtime/server/routes/store/checkout.post.mjs +13 -3
- package/dist/runtime/server/routes/store/coupons/apply.post.mjs +6 -5
- package/dist/runtime/server/routes/store/orders/[id].get.mjs +2 -2
- package/dist/runtime/server/routes/store/orders/download/[token].get.mjs +6 -3
- package/dist/runtime/server/routes/store/orders/index.get.mjs +1 -1
- package/dist/runtime/server/routes/store/products/[slug].get.mjs +2 -2
- package/dist/runtime/server/routes/store/products/index.get.mjs +1 -1
- package/dist/runtime/server/routes/store/tax/calculate.post.mjs +10 -0
- package/dist/runtime/server/routes/tenants/[tenant]/domains/[domain]/index.delete.mjs +26 -25
- package/dist/runtime/server/routes/tenants/[tenant]/domains/[domain]/verify.post.mjs +26 -25
- package/dist/runtime/server/utils/ai.d.ts +6 -0
- package/dist/runtime/server/utils/ai.mjs +12 -2
- package/dist/runtime/server/utils/analytics-query.mjs +8 -4
- package/dist/runtime/server/utils/browser-utils.d.ts +9 -0
- package/dist/runtime/server/utils/browser-utils.mjs +135 -0
- package/dist/runtime/server/utils/cms/cache.d.ts +13 -0
- package/dist/runtime/server/utils/cms/cache.mjs +95 -0
- package/dist/runtime/server/utils/cms/common.mjs +1 -1
- package/dist/runtime/server/utils/cms/markdown.mjs +38 -8
- package/dist/runtime/server/utils/cms/mutations.d.ts +26 -0
- package/dist/runtime/server/utils/cms/mutations.mjs +569 -0
- package/dist/runtime/server/utils/cms/path.d.ts +8 -0
- package/dist/runtime/server/utils/cms/path.mjs +23 -0
- package/dist/runtime/server/utils/cms/queries.d.ts +27 -0
- package/dist/runtime/server/utils/cms/queries.mjs +366 -0
- package/dist/runtime/server/utils/cms/references.d.ts +48 -0
- package/dist/runtime/server/utils/cms/references.mjs +309 -0
- package/dist/runtime/server/utils/cms/revisions.d.ts +7 -0
- package/dist/runtime/server/utils/cms/revisions.mjs +60 -0
- package/dist/runtime/server/utils/cms/sync.mjs +3 -3
- package/dist/runtime/server/utils/cms.d.ts +5 -82
- package/dist/runtime/server/utils/cms.mjs +28 -1367
- package/dist/runtime/server/utils/comments.d.ts +11 -0
- package/dist/runtime/server/utils/comments.mjs +65 -17
- package/dist/runtime/server/utils/crypto.mjs +1 -1
- package/dist/runtime/server/utils/domains.mjs +9 -0
- package/dist/runtime/server/utils/drizzle.d.ts +26 -1
- package/dist/runtime/server/utils/drizzle.mjs +78 -19
- package/dist/runtime/server/utils/email-inbound.mjs +9 -1
- package/dist/runtime/server/utils/impersonation.mjs +58 -1
- package/dist/runtime/server/utils/lock.d.ts +11 -0
- package/dist/runtime/server/utils/lock.mjs +4 -0
- package/dist/runtime/server/utils/mail-suppression.mjs +48 -13
- package/dist/runtime/server/utils/mail.d.ts +2 -1
- package/dist/runtime/server/utils/mail.mjs +17 -11
- package/dist/runtime/server/utils/notifications.d.ts +13 -4
- package/dist/runtime/server/utils/notifications.mjs +20 -4
- package/dist/runtime/server/utils/organization.mjs +36 -1
- package/dist/runtime/server/utils/passwordless.mjs +8 -0
- package/dist/runtime/server/utils/payments/checkout.d.ts +34 -0
- package/dist/runtime/server/utils/payments/checkout.mjs +158 -0
- package/dist/runtime/server/utils/payments/common.d.ts +45 -0
- package/dist/runtime/server/utils/payments/common.mjs +234 -0
- package/dist/runtime/server/utils/payments/index.d.ts +9 -0
- package/dist/runtime/server/utils/payments/index.mjs +21 -0
- package/dist/runtime/server/utils/payments/lemonsqueezy.d.ts +7 -0
- package/dist/runtime/server/utils/payments/lemonsqueezy.mjs +55 -0
- package/dist/runtime/server/utils/payments/paddle.d.ts +7 -0
- package/dist/runtime/server/utils/payments/paddle.mjs +92 -0
- package/dist/runtime/server/utils/payments/polar.d.ts +11 -0
- package/dist/runtime/server/utils/payments/polar.mjs +66 -0
- package/dist/runtime/server/utils/payments/stripe.d.ts +7 -0
- package/dist/runtime/server/utils/payments/stripe.mjs +159 -0
- package/dist/runtime/server/utils/payments/subscriptions.d.ts +89 -0
- package/dist/runtime/server/utils/payments/subscriptions.mjs +236 -0
- package/dist/runtime/server/utils/payments/usage.d.ts +12 -0
- package/dist/runtime/server/utils/payments/usage.mjs +143 -0
- package/dist/runtime/server/utils/payments/webhooks.d.ts +10 -0
- package/dist/runtime/server/utils/payments/webhooks.mjs +200 -0
- package/dist/runtime/server/utils/payments.d.ts +1 -158
- package/dist/runtime/server/utils/payments.mjs +1 -1289
- package/dist/runtime/server/utils/queue.d.ts +2 -0
- package/dist/runtime/server/utils/queue.mjs +34 -21
- package/dist/runtime/server/utils/rag.mjs +30 -17
- package/dist/runtime/server/utils/realtime.d.ts +5 -1
- package/dist/runtime/server/utils/realtime.mjs +43 -7
- package/dist/runtime/server/utils/session.mjs +16 -3
- package/dist/runtime/server/utils/sso.d.ts +3 -0
- package/dist/runtime/server/utils/sso.mjs +43 -0
- package/dist/runtime/server/utils/storage.mjs +12 -1
- package/dist/runtime/server/utils/store/cart.d.ts +63 -0
- package/dist/runtime/server/utils/store/cart.mjs +542 -0
- package/dist/runtime/server/utils/store/checkout.d.ts +14 -0
- package/dist/runtime/server/utils/store/checkout.mjs +118 -0
- package/dist/runtime/server/utils/store/common.d.ts +29 -0
- package/dist/runtime/server/utils/store/common.mjs +479 -0
- package/dist/runtime/server/utils/store/index.d.ts +7 -0
- package/dist/runtime/server/utils/store/index.mjs +30 -0
- package/dist/runtime/server/utils/store/inventory.d.ts +25 -0
- package/dist/runtime/server/utils/store/inventory.mjs +190 -0
- package/dist/runtime/server/utils/store/orders.d.ts +60 -0
- package/dist/runtime/server/utils/store/orders.mjs +590 -0
- package/dist/runtime/server/utils/store/products.d.ts +35 -0
- package/dist/runtime/server/utils/store/products.mjs +461 -0
- package/dist/runtime/server/utils/store/vendor.d.ts +18 -0
- package/dist/runtime/server/utils/store/vendor.mjs +68 -0
- package/dist/runtime/server/utils/store.d.ts +1 -207
- package/dist/runtime/server/utils/store.mjs +1 -2227
- package/dist/runtime/server/utils/string.d.ts +1 -1
- package/dist/runtime/server/utils/string.mjs +12 -4
- package/dist/runtime/server/utils/tenant.mjs +19 -5
- package/dist/runtime/server/utils/tracker.mjs +2 -0
- package/dist/runtime/server.d.ts +3 -3
- package/dist/runtime/server.mjs +4 -4
- package/dist/runtime/storage-server.d.ts +2 -0
- package/dist/runtime/storage-server.mjs +1 -0
- package/dist/runtime/storage.d.ts +1 -1
- package/dist/runtime/storage.mjs +0 -1
- package/dist/runtime/store.d.ts +0 -1
- package/dist/runtime/store.mjs +0 -1
- package/dist/runtime/test-utils.d.ts +4 -0
- package/dist/runtime/test-utils.mjs +54 -0
- package/dist/runtime/types/d1.d.ts +43 -0
- package/dist/runtime/types/drizzle.d.ts +6 -0
- package/dist/runtime/types/store.d.ts +1 -1
- package/dist/types/analytics.d.ts +80 -0
- package/dist/types/auth.d.ts +282 -0
- package/dist/types/cache.d.ts +55 -0
- package/dist/types/comments.d.ts +130 -0
- package/dist/types/db.d.ts +29 -0
- package/dist/types/email.d.ts +182 -0
- package/dist/types/index.d.ts +22 -0
- package/dist/types/jobs.d.ts +58 -0
- package/dist/types/notifications.d.ts +77 -0
- package/dist/types/options.d.ts +363 -0
- package/dist/types/payments.d.ts +145 -0
- package/dist/types/ratelimit.d.ts +71 -0
- package/dist/types/realtime.d.ts +136 -0
- package/dist/types/site.d.ts +121 -0
- package/dist/types/storage.d.ts +164 -0
- package/dist/types/store-options.d.ts +199 -0
- package/dist/types/tenant.d.ts +100 -0
- package/dist/types.d.ts +1 -2174
- package/dist/utils/migration-lock.d.ts +139 -0
- package/dist/utils/pkg.d.ts +14 -1
- package/dist/utils/schema-diff.d.ts +39 -0
- package/package.json +55 -17
- package/dist/chunks/auth-schema.mjs +0 -364
|
@@ -2,7 +2,7 @@ import { defineEventHandler, getRouterParam, createError } from "nuxt/server";
|
|
|
2
2
|
import { getD1Binding } from "../../../../../../db.mjs";
|
|
3
3
|
import { getCustomDomain, verifyCustomDomain } from "../../../../../utils/domains.mjs";
|
|
4
4
|
import { getAuthSession } from "../../../../../utils/session.mjs";
|
|
5
|
-
import { getTenantById } from "../../../../../utils/tenant.mjs";
|
|
5
|
+
import { getTenantById, getTenantBySlug } from "../../../../../utils/tenant.mjs";
|
|
6
6
|
export default defineEventHandler(async (event) => {
|
|
7
7
|
const session = await getAuthSession(event).catch(() => null);
|
|
8
8
|
const userId = session?.user?.id;
|
|
@@ -18,32 +18,33 @@ export default defineEventHandler(async (event) => {
|
|
|
18
18
|
throw createError({ statusCode: 400, message: "Missing domain parameter." });
|
|
19
19
|
}
|
|
20
20
|
const domainRecord = await getCustomDomain(domain, event);
|
|
21
|
-
if (domainRecord) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
if (err.statusCode === 403) throw err;
|
|
39
|
-
throw createError({ statusCode: 500, message: "Authorization check failed." });
|
|
21
|
+
if (!domainRecord) {
|
|
22
|
+
throw createError({ statusCode: 404, message: `Domain "${domain}" not found.` });
|
|
23
|
+
}
|
|
24
|
+
const tenant = await getTenantById(domainRecord.tenantId, event) || await getTenantBySlug(domainRecord.tenantId, event);
|
|
25
|
+
if (!tenant || tenant.id !== tenantParam && tenant.slug !== tenantParam) {
|
|
26
|
+
throw createError({ statusCode: 403, message: "Tenant mismatch: domain does not belong to this tenant." });
|
|
27
|
+
}
|
|
28
|
+
if (tenant.organizationId) {
|
|
29
|
+
try {
|
|
30
|
+
const bindingName = globalThis.process?.env?.QARPEO_DEFAULT_DB_BINDING || "DB";
|
|
31
|
+
const d1 = getD1Binding(bindingName, event);
|
|
32
|
+
const membership = await d1.prepare("SELECT role FROM member WHERE organization_id = ? AND user_id = ? LIMIT 1").bind(tenant.organizationId, userId).first();
|
|
33
|
+
if (!membership || !["owner", "admin"].includes(membership.role)) {
|
|
34
|
+
throw createError({
|
|
35
|
+
statusCode: 403,
|
|
36
|
+
message: "Forbidden: insufficient permissions for this tenant."
|
|
37
|
+
});
|
|
40
38
|
}
|
|
41
|
-
}
|
|
42
|
-
throw
|
|
43
|
-
|
|
44
|
-
message: "Tenant has no organization; domain management requires an organization."
|
|
45
|
-
});
|
|
39
|
+
} catch (err) {
|
|
40
|
+
if (err.statusCode === 403) throw err;
|
|
41
|
+
throw createError({ statusCode: 500, message: "Authorization check failed." });
|
|
46
42
|
}
|
|
43
|
+
} else {
|
|
44
|
+
throw createError({
|
|
45
|
+
statusCode: 403,
|
|
46
|
+
message: "Tenant has no organization; domain management requires an organization."
|
|
47
|
+
});
|
|
47
48
|
}
|
|
48
49
|
const record = await verifyCustomDomain(domain, event);
|
|
49
50
|
return { domain: record };
|
|
@@ -133,6 +133,12 @@ export declare function resolveAiGateway(optsGateway?: {
|
|
|
133
133
|
skipCache?: boolean;
|
|
134
134
|
cacheTtl?: number;
|
|
135
135
|
} | undefined;
|
|
136
|
+
/**
|
|
137
|
+
* Safely resolves the namespaced Durable Object instance ID for an agent session.
|
|
138
|
+
* Enforces strict user isolation: authenticated users are locked to their own `usr_<userId>__`
|
|
139
|
+
* namespace, preventing cross-user session enumeration or hijacking.
|
|
140
|
+
*/
|
|
141
|
+
export declare function resolveAgentInstanceId(userId?: string | null, rawId?: unknown): string;
|
|
136
142
|
/**
|
|
137
143
|
* Accesses an Edge AI Agent backed by Cloudflare Durable Objects.
|
|
138
144
|
*
|
|
@@ -173,6 +173,10 @@ export function resolveAiGateway(optsGateway) {
|
|
|
173
173
|
}
|
|
174
174
|
return void 0;
|
|
175
175
|
}
|
|
176
|
+
export function resolveAgentInstanceId(userId, rawId) {
|
|
177
|
+
const rawSuffix = rawId ? String(rawId).replace(/[^a-zA-Z0-9_-]/g, "").slice(0, 64) : "default";
|
|
178
|
+
return userId ? `usr_${userId}__${rawSuffix || "default"}` : `anon_${rawSuffix || "default"}`;
|
|
179
|
+
}
|
|
176
180
|
export function useAgent(agentName, id = "default", event) {
|
|
177
181
|
const stub = getAgentDurableObject(agentName, id, "QARPEO_AGENT_DO", event);
|
|
178
182
|
const def = getAgentDefinition(agentName);
|
|
@@ -361,14 +365,20 @@ export function synthesizeMockFromSchema(jsonSchema) {
|
|
|
361
365
|
const result = {};
|
|
362
366
|
const props = jsonSchema.properties || {};
|
|
363
367
|
for (const [key, propDef] of Object.entries(props)) {
|
|
364
|
-
if (propDef.
|
|
368
|
+
if (propDef.enum && Array.isArray(propDef.enum) && propDef.enum.length > 0) {
|
|
369
|
+
result[key] = propDef.default ?? propDef.enum[0];
|
|
370
|
+
} else if (propDef.type === "string") {
|
|
365
371
|
result[key] = propDef.default ?? `mock_${key}`;
|
|
366
372
|
} else if (propDef.type === "number" || propDef.type === "integer") {
|
|
367
373
|
result[key] = propDef.default ?? 42;
|
|
368
374
|
} else if (propDef.type === "boolean") {
|
|
369
375
|
result[key] = propDef.default ?? true;
|
|
370
376
|
} else if (propDef.type === "array") {
|
|
371
|
-
|
|
377
|
+
if (propDef.items?.enum && Array.isArray(propDef.items.enum) && propDef.items.enum.length > 0) {
|
|
378
|
+
result[key] = propDef.default ?? [propDef.items.enum[0]];
|
|
379
|
+
} else {
|
|
380
|
+
result[key] = propDef.default ?? (propDef.items?.type === "string" ? ["item_1", "item_2"] : []);
|
|
381
|
+
}
|
|
372
382
|
} else if (propDef.type === "object" || propDef.properties) {
|
|
373
383
|
result[key] = synthesizeMockFromSchema(propDef);
|
|
374
384
|
} else {
|
|
@@ -110,6 +110,7 @@ export function useAnalyticsDashboard(options = {}) {
|
|
|
110
110
|
*/
|
|
111
111
|
async topPages(limit = 10, range = "7d") {
|
|
112
112
|
const interval = rangeToInterval(range);
|
|
113
|
+
const safeLimit = Math.min(1e3, Math.max(1, Number.isFinite(limit) ? Math.trunc(limit) : 10));
|
|
113
114
|
const rows = await query(`
|
|
114
115
|
SELECT blob2 AS path, count() AS views
|
|
115
116
|
FROM ${dataset}
|
|
@@ -118,7 +119,7 @@ export function useAnalyticsDashboard(options = {}) {
|
|
|
118
119
|
${siteFilter}
|
|
119
120
|
GROUP BY path
|
|
120
121
|
ORDER BY views DESC
|
|
121
|
-
LIMIT ${
|
|
122
|
+
LIMIT ${safeLimit}
|
|
122
123
|
`);
|
|
123
124
|
return rows.map((r) => ({ path: String(r.path || "/"), views: Number(r.views) }));
|
|
124
125
|
},
|
|
@@ -130,6 +131,7 @@ export function useAnalyticsDashboard(options = {}) {
|
|
|
130
131
|
*/
|
|
131
132
|
async topReferrers(limit = 10, range = "7d") {
|
|
132
133
|
const interval = rangeToInterval(range);
|
|
134
|
+
const safeLimit = Math.min(1e3, Math.max(1, Number.isFinite(limit) ? Math.trunc(limit) : 10));
|
|
133
135
|
const rows = await query(`
|
|
134
136
|
SELECT blob3 AS referrer, count() AS count
|
|
135
137
|
FROM ${dataset}
|
|
@@ -139,7 +141,7 @@ export function useAnalyticsDashboard(options = {}) {
|
|
|
139
141
|
${siteFilter}
|
|
140
142
|
GROUP BY referrer
|
|
141
143
|
ORDER BY count DESC
|
|
142
|
-
LIMIT ${
|
|
144
|
+
LIMIT ${safeLimit}
|
|
143
145
|
`);
|
|
144
146
|
return rows.map((r) => ({ referrer: String(r.referrer), count: Number(r.count) }));
|
|
145
147
|
},
|
|
@@ -151,6 +153,7 @@ export function useAnalyticsDashboard(options = {}) {
|
|
|
151
153
|
*/
|
|
152
154
|
async countries(limit = 10, range = "7d") {
|
|
153
155
|
const interval = rangeToInterval(range);
|
|
156
|
+
const safeLimit = Math.min(1e3, Math.max(1, Number.isFinite(limit) ? Math.trunc(limit) : 10));
|
|
154
157
|
const rows = await query(`
|
|
155
158
|
SELECT blob4 AS country, count() AS count
|
|
156
159
|
FROM ${dataset}
|
|
@@ -159,7 +162,7 @@ export function useAnalyticsDashboard(options = {}) {
|
|
|
159
162
|
${siteFilter}
|
|
160
163
|
GROUP BY country
|
|
161
164
|
ORDER BY count DESC
|
|
162
|
-
LIMIT ${
|
|
165
|
+
LIMIT ${safeLimit}
|
|
163
166
|
`);
|
|
164
167
|
return rows.map((r) => ({ country: String(r.country || "XX"), count: Number(r.count) }));
|
|
165
168
|
},
|
|
@@ -194,6 +197,7 @@ export function useAnalyticsDashboard(options = {}) {
|
|
|
194
197
|
*/
|
|
195
198
|
async events(name, limit = 20, range = "7d") {
|
|
196
199
|
const interval = rangeToInterval(range);
|
|
200
|
+
const safeLimit = Math.min(1e3, Math.max(1, Number.isFinite(limit) ? Math.trunc(limit) : 20));
|
|
197
201
|
const nameFilter = name ? `AND blob1 = '${escapeAEString(name)}'` : "AND blob1 != 'pageview'";
|
|
198
202
|
const rows = await query(`
|
|
199
203
|
SELECT
|
|
@@ -206,7 +210,7 @@ export function useAnalyticsDashboard(options = {}) {
|
|
|
206
210
|
${siteFilter}
|
|
207
211
|
GROUP BY name
|
|
208
212
|
ORDER BY count DESC
|
|
209
|
-
LIMIT ${
|
|
213
|
+
LIMIT ${safeLimit}
|
|
210
214
|
`);
|
|
211
215
|
return rows.map((r) => ({
|
|
212
216
|
name: String(r.name),
|
|
@@ -155,6 +155,15 @@ export interface PdfResult {
|
|
|
155
155
|
/** Whether this was produced by the dev-mode mock (no real browser). */
|
|
156
156
|
mock?: boolean;
|
|
157
157
|
}
|
|
158
|
+
/**
|
|
159
|
+
* Detects whether a string target is inline HTML rather than a URL.
|
|
160
|
+
*/
|
|
161
|
+
export declare function isHtmlTarget(input: string): boolean;
|
|
162
|
+
/**
|
|
163
|
+
* Validates a browser target URL against Server-Side Request Forgery (SSRF) and protocol injection.
|
|
164
|
+
* Throws 400 Bad Request if the target URL is invalid, uses a forbidden scheme, or points to private/internal networks.
|
|
165
|
+
*/
|
|
166
|
+
export declare function assertSafeBrowserUrl(url: string): URL;
|
|
158
167
|
/**
|
|
159
168
|
* Captures a screenshot of a URL or inline HTML string using Cloudflare Browser Rendering.
|
|
160
169
|
*
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createError } from "nuxt/server";
|
|
1
2
|
import { getCloudflareEnv } from "../../bindings.mjs";
|
|
2
3
|
function getBrowserBinding(name = "BROWSER") {
|
|
3
4
|
try {
|
|
@@ -34,7 +35,137 @@ function base64ToUint8Array(b64) {
|
|
|
34
35
|
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
35
36
|
return bytes;
|
|
36
37
|
}
|
|
38
|
+
export function isHtmlTarget(input) {
|
|
39
|
+
const trimmed = (input || "").trim();
|
|
40
|
+
return /^\s*<!?[a-z]/i.test(trimmed) || trimmed.startsWith("<");
|
|
41
|
+
}
|
|
42
|
+
function parseIpv4(host) {
|
|
43
|
+
if (/^\d+$/.test(host)) {
|
|
44
|
+
const num = Number(host);
|
|
45
|
+
if (num >= 0 && num <= 4294967295) {
|
|
46
|
+
return [num >>> 24 & 255, num >>> 16 & 255, num >>> 8 & 255, num & 255];
|
|
47
|
+
}
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
const parts = host.split(".");
|
|
51
|
+
if (parts.length >= 1 && parts.length <= 4) {
|
|
52
|
+
const octets = [];
|
|
53
|
+
for (let i = 0; i < parts.length; i++) {
|
|
54
|
+
const p = parts[i];
|
|
55
|
+
let val;
|
|
56
|
+
if (/^0x[0-9a-f]+$/i.test(p)) {
|
|
57
|
+
val = parseInt(p, 16);
|
|
58
|
+
} else if (/^0[0-7]+$/.test(p)) {
|
|
59
|
+
val = parseInt(p, 8);
|
|
60
|
+
} else if (/^\d+$/.test(p)) {
|
|
61
|
+
val = parseInt(p, 10);
|
|
62
|
+
} else {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
if (isNaN(val) || val < 0) return null;
|
|
66
|
+
octets.push(val);
|
|
67
|
+
}
|
|
68
|
+
if (octets.length === 4) {
|
|
69
|
+
if (octets.every((o) => o <= 255)) {
|
|
70
|
+
return octets;
|
|
71
|
+
}
|
|
72
|
+
} else if (octets.length === 2) {
|
|
73
|
+
if (octets[0] <= 255 && octets[1] <= 16777215) {
|
|
74
|
+
return [octets[0], octets[1] >>> 16 & 255, octets[1] >>> 8 & 255, octets[1] & 255];
|
|
75
|
+
}
|
|
76
|
+
} else if (octets.length === 1) {
|
|
77
|
+
const num = octets[0];
|
|
78
|
+
if (num <= 4294967295) {
|
|
79
|
+
return [num >>> 24 & 255, num >>> 16 & 255, num >>> 8 & 255, num & 255];
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
export function assertSafeBrowserUrl(url) {
|
|
86
|
+
if (!url || typeof url !== "string" || !url.trim()) {
|
|
87
|
+
throw createError({
|
|
88
|
+
statusCode: 400,
|
|
89
|
+
statusMessage: "Bad Request",
|
|
90
|
+
message: "Target URL cannot be empty."
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
let parsed;
|
|
94
|
+
try {
|
|
95
|
+
parsed = new URL(url.trim());
|
|
96
|
+
} catch {
|
|
97
|
+
throw createError({
|
|
98
|
+
statusCode: 400,
|
|
99
|
+
statusMessage: "Bad Request",
|
|
100
|
+
message: `Invalid browser target URL: "${url}"`
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
104
|
+
throw createError({
|
|
105
|
+
statusCode: 400,
|
|
106
|
+
statusMessage: "Bad Request",
|
|
107
|
+
message: `Forbidden URL scheme "${parsed.protocol}". Only http: and https: protocols are permitted for browser rendering.`
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
const hostname = parsed.hostname.toLowerCase().trim();
|
|
111
|
+
if (hostname === "localhost" || hostname.endsWith(".localhost") || hostname.endsWith(".local") || hostname.endsWith(".internal") || hostname.endsWith(".lan") || hostname.endsWith(".home.arpa")) {
|
|
112
|
+
throw createError({
|
|
113
|
+
statusCode: 400,
|
|
114
|
+
statusMessage: "Bad Request",
|
|
115
|
+
message: `SSRF protection: Navigation to internal/local hostname "${hostname}" is forbidden.`
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
if (!hostname.includes(".") && !hostname.includes(":")) {
|
|
119
|
+
throw createError({
|
|
120
|
+
statusCode: 400,
|
|
121
|
+
statusMessage: "Bad Request",
|
|
122
|
+
message: `SSRF protection: Navigation to unqualified internal hostname "${hostname}" is forbidden.`
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
if (hostname === "metadata.google.internal" || hostname === "instance-data" || hostname === "metadata") {
|
|
126
|
+
throw createError({
|
|
127
|
+
statusCode: 400,
|
|
128
|
+
statusMessage: "Bad Request",
|
|
129
|
+
message: "SSRF protection: Navigation to cloud metadata services is forbidden."
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
const ipv4 = parseIpv4(hostname);
|
|
133
|
+
if (ipv4) {
|
|
134
|
+
const [a, b, c] = ipv4;
|
|
135
|
+
const isPrivate = a === 0 || // 0.0.0.0/8 (Current network)
|
|
136
|
+
a === 10 || // 10.0.0.0/8 (Private)
|
|
137
|
+
a === 127 || // 127.0.0.0/8 (Loopback)
|
|
138
|
+
a === 100 && b >= 64 && b <= 127 || // 100.64.0.0/10 (CGNAT)
|
|
139
|
+
a === 169 && b === 254 || // 169.254.0.0/16 (Link-local & metadata)
|
|
140
|
+
a === 172 && b >= 16 && b <= 31 || // 172.16.0.0/12 (Private)
|
|
141
|
+
a === 192 && b === 168 || // 192.168.0.0/16 (Private)
|
|
142
|
+
a === 192 && b === 0 && c === 2 || // 192.0.2.0/24 (TEST-NET-1)
|
|
143
|
+
a === 198 && (b === 18 || b === 19) || // 198.18.0.0/15 (Benchmarking)
|
|
144
|
+
a === 198 && b === 51 && c === 100 || // 198.51.100.0/24 (TEST-NET-2)
|
|
145
|
+
a === 203 && b === 0 && c === 113 || // 203.0.113.0/24 (TEST-NET-3)
|
|
146
|
+
a >= 224;
|
|
147
|
+
if (isPrivate) {
|
|
148
|
+
throw createError({
|
|
149
|
+
statusCode: 400,
|
|
150
|
+
statusMessage: "Bad Request",
|
|
151
|
+
message: `SSRF protection: Navigation to private/reserved IP address "${hostname}" is forbidden.`
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const cleanV6 = hostname.replace(/^\[/, "").replace(/\]$/, "").toLowerCase();
|
|
156
|
+
if (cleanV6 === "::1" || cleanV6 === "::" || cleanV6.startsWith("fe80:") || cleanV6.startsWith("fc") || cleanV6.startsWith("fd") || cleanV6.startsWith("::ffff:127.") || cleanV6.startsWith("::ffff:10.") || cleanV6.startsWith("::ffff:172.") || cleanV6.startsWith("::ffff:192.168.") || cleanV6.startsWith("::ffff:169.254.") || cleanV6.startsWith("::ffff:0.")) {
|
|
157
|
+
throw createError({
|
|
158
|
+
statusCode: 400,
|
|
159
|
+
statusMessage: "Bad Request",
|
|
160
|
+
message: `SSRF protection: Navigation to private/local IPv6 address "${hostname}" is forbidden.`
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
return parsed;
|
|
164
|
+
}
|
|
37
165
|
export async function captureScreenshot(urlOrHtml, options = {}) {
|
|
166
|
+
if (!isHtmlTarget(urlOrHtml)) {
|
|
167
|
+
assertSafeBrowserUrl(urlOrHtml);
|
|
168
|
+
}
|
|
38
169
|
const {
|
|
39
170
|
format = "png",
|
|
40
171
|
quality = 85,
|
|
@@ -107,6 +238,9 @@ export async function captureScreenshot(urlOrHtml, options = {}) {
|
|
|
107
238
|
}
|
|
108
239
|
}
|
|
109
240
|
export async function generatePdf(urlOrHtml, options = {}) {
|
|
241
|
+
if (!isHtmlTarget(urlOrHtml)) {
|
|
242
|
+
assertSafeBrowserUrl(urlOrHtml);
|
|
243
|
+
}
|
|
110
244
|
const {
|
|
111
245
|
format: paperFormat = "A4",
|
|
112
246
|
orientation = "portrait",
|
|
@@ -164,6 +298,7 @@ export async function generatePdf(urlOrHtml, options = {}) {
|
|
|
164
298
|
}
|
|
165
299
|
}
|
|
166
300
|
export async function crawlPage(startUrl, options = {}) {
|
|
301
|
+
assertSafeBrowserUrl(startUrl);
|
|
167
302
|
const start = Date.now();
|
|
168
303
|
const {
|
|
169
304
|
linkSelector = "a[href]",
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { RequestEvent } from "nuxt/server";
|
|
2
|
+
import type { CMSEntry } from "../../../../types";
|
|
3
|
+
export declare function runCMSBackgroundTask(event: RequestEvent, task: Promise<any>): void;
|
|
4
|
+
export declare function getCachedEntry(event: RequestEvent, collectionId: string, slugOrId: string): Promise<CMSEntry | null>;
|
|
5
|
+
export declare function setCachedEntry(event: RequestEvent, collectionId: string, slugOrId: string, entry: CMSEntry): Promise<void>;
|
|
6
|
+
export declare function getCachedList(event: RequestEvent, key: string): Promise<{
|
|
7
|
+
entries: CMSEntry[];
|
|
8
|
+
total: number;
|
|
9
|
+
page: number;
|
|
10
|
+
limit: number;
|
|
11
|
+
} | null>;
|
|
12
|
+
export declare function setCachedList(event: RequestEvent, key: string, data: any): Promise<void>;
|
|
13
|
+
export declare function invalidateCMSEntryCache(event: RequestEvent, collectionId: string, slug?: string | string[]): Promise<void>;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { getKVBinding } from "../../../kv.mjs";
|
|
2
|
+
import { getCMSConfig, getCMSTenantPrefix } from "./common.mjs";
|
|
3
|
+
export function runCMSBackgroundTask(event, task) {
|
|
4
|
+
const ev = event;
|
|
5
|
+
const ctx = ev?.context?.cloudflare?.context || ev?.node?.req?.context?.cloudflare?.context || ev?.context?.waitUntil;
|
|
6
|
+
if (ctx && typeof ctx.waitUntil === "function") {
|
|
7
|
+
ctx.waitUntil(
|
|
8
|
+
task.catch((err) => console.warn("[qarpeo:cms:cache] Background task error:", err))
|
|
9
|
+
);
|
|
10
|
+
} else if (typeof ctx === "function") {
|
|
11
|
+
ctx(task.catch((err) => console.warn("[qarpeo:cms:cache] Background task error:", err)));
|
|
12
|
+
} else {
|
|
13
|
+
task.catch(() => {
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export async function getCachedEntry(event, collectionId, slugOrId) {
|
|
18
|
+
const config = getCMSConfig(event);
|
|
19
|
+
if (!config.cache) return null;
|
|
20
|
+
try {
|
|
21
|
+
const kv = getKVBinding("KV", event);
|
|
22
|
+
const tenantPrefix = getCMSTenantPrefix(event);
|
|
23
|
+
const cached = await kv.get(`${tenantPrefix}cms:entry:${collectionId}:${slugOrId}`, {
|
|
24
|
+
type: "json"
|
|
25
|
+
});
|
|
26
|
+
return cached;
|
|
27
|
+
} catch {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export async function setCachedEntry(event, collectionId, slugOrId, entry) {
|
|
32
|
+
const config = getCMSConfig(event);
|
|
33
|
+
if (!config.cache) return;
|
|
34
|
+
const tenantPrefix = getCMSTenantPrefix(event);
|
|
35
|
+
const task = (async () => {
|
|
36
|
+
try {
|
|
37
|
+
const kv = getKVBinding("KV", event);
|
|
38
|
+
await kv.put(`${tenantPrefix}cms:entry:${collectionId}:${slugOrId}`, JSON.stringify(entry), {
|
|
39
|
+
expirationTtl: 60
|
|
40
|
+
});
|
|
41
|
+
} catch {
|
|
42
|
+
}
|
|
43
|
+
})();
|
|
44
|
+
runCMSBackgroundTask(event, task);
|
|
45
|
+
return task;
|
|
46
|
+
}
|
|
47
|
+
export async function getCachedList(event, key) {
|
|
48
|
+
const config = getCMSConfig(event);
|
|
49
|
+
if (!config.cache) return null;
|
|
50
|
+
try {
|
|
51
|
+
const kv = getKVBinding("KV", event);
|
|
52
|
+
const tenantPrefix = getCMSTenantPrefix(event);
|
|
53
|
+
const cached = await kv.get(`${tenantPrefix}${key}`, { type: "json" });
|
|
54
|
+
return cached;
|
|
55
|
+
} catch {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export async function setCachedList(event, key, data) {
|
|
60
|
+
const config = getCMSConfig(event);
|
|
61
|
+
if (!config.cache) return;
|
|
62
|
+
const tenantPrefix = getCMSTenantPrefix(event);
|
|
63
|
+
const task = (async () => {
|
|
64
|
+
try {
|
|
65
|
+
const kv = getKVBinding("KV", event);
|
|
66
|
+
await kv.put(`${tenantPrefix}${key}`, JSON.stringify(data), { expirationTtl: 30 });
|
|
67
|
+
} catch {
|
|
68
|
+
}
|
|
69
|
+
})();
|
|
70
|
+
runCMSBackgroundTask(event, task);
|
|
71
|
+
return task;
|
|
72
|
+
}
|
|
73
|
+
export async function invalidateCMSEntryCache(event, collectionId, slug) {
|
|
74
|
+
const config = getCMSConfig(event);
|
|
75
|
+
if (!config.cache) return;
|
|
76
|
+
const tenantPrefix = getCMSTenantPrefix(event);
|
|
77
|
+
const task = (async () => {
|
|
78
|
+
try {
|
|
79
|
+
const kv = getKVBinding("KV", event);
|
|
80
|
+
const slugs = Array.isArray(slug) ? slug : slug ? [slug] : [];
|
|
81
|
+
for (const s of slugs) {
|
|
82
|
+
if (s) {
|
|
83
|
+
await kv.delete(`${tenantPrefix}cms:entry:${collectionId}:${s}`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
const listRes = await kv.list({ prefix: `${tenantPrefix}cms:list:${collectionId}:` });
|
|
87
|
+
if (listRes?.keys?.length) {
|
|
88
|
+
await Promise.all(listRes.keys.map((k) => kv.delete(k.name)));
|
|
89
|
+
}
|
|
90
|
+
} catch {
|
|
91
|
+
}
|
|
92
|
+
})();
|
|
93
|
+
runCMSBackgroundTask(event, task);
|
|
94
|
+
return task;
|
|
95
|
+
}
|
|
@@ -45,7 +45,7 @@ export function getCMSConfig(event) {
|
|
|
45
45
|
apiPath: cfg.apiPath || "/api/cms",
|
|
46
46
|
contentDir: cfg.contentDir || "content",
|
|
47
47
|
hybrid: cfg.hybrid !== false,
|
|
48
|
-
roles: cfg.roles
|
|
48
|
+
roles: Array.isArray(cfg.roles) && cfg.roles.length > 0 ? cfg.roles : ["admin", "editor"],
|
|
49
49
|
comments: cfg.comments || { autoApprove: false },
|
|
50
50
|
revisions: cfg.revisions || { max: 10 },
|
|
51
51
|
cache: cfg.cache !== false,
|
|
@@ -1,8 +1,30 @@
|
|
|
1
|
-
import { parseMarkdown as comarkParseMarkdown } from "comark";
|
|
2
|
-
import { render as comarkRender } from "comark/render";
|
|
3
1
|
import { useRuntimeConfig } from "nuxt/server";
|
|
4
|
-
import { parse as parseYaml, stringify as stringifyYaml } from "yaml";
|
|
5
2
|
import { getQarpeoConfig } from "../config.mjs";
|
|
3
|
+
function _requireComark() {
|
|
4
|
+
return Promise.all([
|
|
5
|
+
import("comark"),
|
|
6
|
+
import("comark/render")
|
|
7
|
+
]).then(([comarkMod, renderMod]) => ({
|
|
8
|
+
parseMarkdown: comarkMod.parseMarkdown,
|
|
9
|
+
render: renderMod.render
|
|
10
|
+
})).catch(() => {
|
|
11
|
+
throw new Error(
|
|
12
|
+
`[qarpeo] CMS markdown parsing requires the \`comark\` package.
|
|
13
|
+
Install it with: pnpm add comark`
|
|
14
|
+
);
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
function _requireYamlSync() {
|
|
18
|
+
try {
|
|
19
|
+
const m = require("yaml");
|
|
20
|
+
return { parse: m.parse, stringify: m.stringify };
|
|
21
|
+
} catch {
|
|
22
|
+
throw new Error(
|
|
23
|
+
`[qarpeo] CMS frontmatter parsing requires the \`yaml\` package.
|
|
24
|
+
Install it with: pnpm add yaml`
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
6
28
|
const _cachedPluginsMap = /* @__PURE__ */ new Map();
|
|
7
29
|
async function _resolveMarkdownPlugins(event) {
|
|
8
30
|
const rc = useRuntimeConfig(event);
|
|
@@ -29,8 +51,11 @@ async function _resolveMarkdownPlugins(event) {
|
|
|
29
51
|
export async function parseMarkdownAST(md, event) {
|
|
30
52
|
if (!md) return { nodes: [], frontmatter: {}, meta: {} };
|
|
31
53
|
try {
|
|
32
|
-
const plugins = await
|
|
33
|
-
|
|
54
|
+
const [plugins, { parseMarkdown }] = await Promise.all([
|
|
55
|
+
_resolveMarkdownPlugins(event),
|
|
56
|
+
_requireComark()
|
|
57
|
+
]);
|
|
58
|
+
return await parseMarkdown(md, { plugins });
|
|
34
59
|
} catch (err) {
|
|
35
60
|
console.error("[qarpeo:cms] parseMarkdownAST failed:", err);
|
|
36
61
|
return { nodes: [], frontmatter: {}, meta: {} };
|
|
@@ -43,9 +68,12 @@ export function sanitizeHtml(html) {
|
|
|
43
68
|
export async function processMarkdown(md, event) {
|
|
44
69
|
if (!md) return { ast: { nodes: [], frontmatter: {}, meta: {} }, html: "" };
|
|
45
70
|
try {
|
|
46
|
-
const plugins = await
|
|
47
|
-
|
|
48
|
-
|
|
71
|
+
const [plugins, { parseMarkdown, render }] = await Promise.all([
|
|
72
|
+
_resolveMarkdownPlugins(event),
|
|
73
|
+
_requireComark()
|
|
74
|
+
]);
|
|
75
|
+
const ast = await parseMarkdown(md, { plugins });
|
|
76
|
+
const rawHtml = (await render(ast, { format: "text/html" })).trim();
|
|
49
77
|
const html = sanitizeHtml(rawHtml);
|
|
50
78
|
return { ast, html };
|
|
51
79
|
} catch {
|
|
@@ -74,6 +102,7 @@ export function parseFrontmatter(raw) {
|
|
|
74
102
|
const body = fmMatch[2] || "";
|
|
75
103
|
let data = {};
|
|
76
104
|
try {
|
|
105
|
+
const { parse: parseYaml } = _requireYamlSync();
|
|
77
106
|
const parsed = parseYaml(yamlBlock);
|
|
78
107
|
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
79
108
|
const sanitized = {};
|
|
@@ -95,6 +124,7 @@ export function formatFrontmatter(data, body) {
|
|
|
95
124
|
if (v === void 0 || v === null || k.startsWith("_")) continue;
|
|
96
125
|
cleanData[k] = v;
|
|
97
126
|
}
|
|
127
|
+
const { stringify: stringifyYaml } = _requireYamlSync();
|
|
98
128
|
const yamlStr = Object.keys(cleanData).length > 0 ? stringifyYaml(cleanData).trim() : "";
|
|
99
129
|
if (!yamlStr) {
|
|
100
130
|
return body;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type RequestEvent } from "nuxt/server";
|
|
2
|
+
import type { CMSEntry, CMSEntryStatus, CMSCreateInput, CMSUpdateInput } from "../../../../types";
|
|
3
|
+
/** Creates a new entry in D1. Renders contentHtml from contentMd on write. */
|
|
4
|
+
export declare function createCMSEntry(event: RequestEvent, collectionId: string, data: CMSCreateInput): Promise<CMSEntry>;
|
|
5
|
+
/**
|
|
6
|
+
* Updates an existing entry. Re-renders contentHtml if contentMd changed. Saves a revision and
|
|
7
|
+
* prunes beyond max.
|
|
8
|
+
*/
|
|
9
|
+
export declare function updateCMSEntry(event: RequestEvent, entryId: string, data: CMSUpdateInput): Promise<CMSEntry>;
|
|
10
|
+
/** Deletes an entry and all its revisions. */
|
|
11
|
+
export declare function deleteCMSEntry(event: RequestEvent, entryId: string): Promise<void>;
|
|
12
|
+
/**
|
|
13
|
+
* Toggles entry status between `draft`, `pending_review`, `scheduled`, `published`, or `archived`.
|
|
14
|
+
* Validates approval policies if approval workflow is enabled for the collection.
|
|
15
|
+
*/
|
|
16
|
+
export declare function publishCMSEntry(event: RequestEvent, entryId: string, targetStatus: CMSEntryStatus, targetPublishedAt?: number | null): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Scans for entries with status = 'scheduled' and published_at <= Date.now(). Transitions them to
|
|
19
|
+
* 'published' and triggers git-sync, webhooks, and deploy hooks. Returns the list of entries
|
|
20
|
+
* published.
|
|
21
|
+
*/
|
|
22
|
+
export declare function publishDueScheduledEntries(event?: RequestEvent): Promise<CMSEntry[]>;
|
|
23
|
+
/** Submits an entry for editorial review (transitions status from draft to pending_review). */
|
|
24
|
+
export declare function submitCMSEntryForReview(event: RequestEvent, entryId: string, note?: string): Promise<CMSEntry>;
|
|
25
|
+
/** Rejects a pending review entry and reverts its status back to draft with reviewer feedback. */
|
|
26
|
+
export declare function rejectCMSEntryReview(event: RequestEvent, entryId: string, reason?: string): Promise<CMSEntry>;
|