@kidecms/core 0.16.0 → 0.16.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/core/runtime.ts CHANGED
@@ -2,6 +2,8 @@ export type CmsStorageAdapter = {
2
2
  putFile: (storagePath: string, data: ArrayBuffer | Uint8Array) => Promise<void>;
3
3
  getFile: (storagePath: string) => Promise<ArrayBuffer | null>;
4
4
  deleteFile: (storagePath: string) => Promise<void>;
5
+ /** Optional streaming read — the /uploads route buffers via getFile when absent. */
6
+ getFileStream?: (storagePath: string) => Promise<{ body: ReadableStream; size: number } | null>;
5
7
  };
6
8
 
7
9
  export type CmsEmailAdapter = {
@@ -107,7 +107,8 @@ export default function cmsIntegration(options?: CmsIntegrationOptions): AstroIn
107
107
  const generatedPath = options?.generatedPath ?? "src/cms/.generated";
108
108
  const adaptersPath = options?.adaptersPath ?? "src/cms/adapters";
109
109
  const generatorPath = options?.generatorPath ?? packagePath("internals", "generator.ts");
110
- const platform = options?.platform ?? "node";
110
+ // options.platform stays accepted for compat (CF configs pass it) but nothing
111
+ // branches on it anymore — the /uploads route now serves both platforms.
111
112
 
112
113
  return {
113
114
  name: "kide-cms",
@@ -255,13 +256,13 @@ export default function cmsIntegration(options?: CmsIntegrationOptions): AstroIn
255
256
  entrypoint: new URL("../routes/admin/[...path].astro", import.meta.url),
256
257
  });
257
258
 
258
- // Cloudflare serves /uploads/* from R2 through a route (Node uses static files).
259
- if (platform === "cloudflare") {
260
- injectRoute({
261
- pattern: "/uploads/[...path]",
262
- entrypoint: new URL("../platform/cloudflare/uploads-route.ts", import.meta.url),
263
- });
264
- }
259
+ // /uploads/* streams from the storage adapter (Node: uploads dir, CF: R2).
260
+ // Both platforms need it: Node's static layer only serves files that
261
+ // existed at build time, so runtime uploads 404 in production without it.
262
+ injectRoute({
263
+ pattern: "/uploads/[...path]",
264
+ entrypoint: new URL("../routes/uploads/[...path].ts", import.meta.url),
265
+ });
265
266
 
266
267
  // Inject API routes
267
268
  injectRoute({
@@ -148,11 +148,18 @@ const handle = async (context: APIContext, next: MiddlewareNext) => {
148
148
  hasUsers = true;
149
149
  }
150
150
  } catch (error) {
151
- // Only a missing users table means first run (schema not pushed yet) — anything else
152
- // (corruption, I/O) must surface, not funnel visitors to /admin/setup.
151
+ // Anything but a missing users table (corruption, I/O) must surface.
153
152
  const message = error instanceof Error ? error.message : String(error);
154
153
  if (!/no such table: cms_users/i.test(message)) throw error;
155
- hasUsers = false;
154
+ // Schema not pushed. Setup can't succeed against a table-less database
155
+ // (its INSERT would fail too), so say what's actually wrong instead of
156
+ // redirecting there. Typical cause: `pnpm build && pnpm preview` without
157
+ // a prior `pnpm cms:push` (dev pushes on boot, production never does).
158
+ // hasUsers stays unset so the check re-runs once the schema exists.
159
+ return new Response(
160
+ "Database schema is not initialized. Run `pnpm cms:push` against this environment's database (Cloudflare D1: apply your migrations), then reload.",
161
+ { status: 503, headers: { "Content-Type": "text/plain", "Cache-Control": "no-store" } },
162
+ );
156
163
  }
157
164
  }
158
165
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kidecms/core",
3
- "version": "0.16.0",
3
+ "version": "0.16.1",
4
4
  "description": "Kide CMS runtime — core library, admin UI, routes, and middleware for Astro. Shipped as TypeScript source.",
5
5
  "keywords": [
6
6
  "cms",
@@ -0,0 +1,61 @@
1
+ import type { APIRoute } from "astro";
2
+ // Narrow import (not the core barrel): this route compiles in the Cloudflare
3
+ // profile too, and the barrel drags in Node-only modules.
4
+ import { getStorage } from "../../core/runtime";
5
+
6
+ export const prerender = false;
7
+
8
+ const MIME_TYPES: Record<string, string> = {
9
+ ".jpg": "image/jpeg",
10
+ ".jpeg": "image/jpeg",
11
+ ".png": "image/png",
12
+ ".gif": "image/gif",
13
+ ".webp": "image/webp",
14
+ ".avif": "image/avif",
15
+ ".svg": "image/svg+xml",
16
+ ".pdf": "application/pdf",
17
+ ".mp4": "video/mp4",
18
+ ".webm": "video/webm",
19
+ };
20
+
21
+ // Serves runtime-uploaded files through the storage adapter (Node: uploads dir,
22
+ // Cloudflare: R2). Node's static layer only knows files present at build time,
23
+ // so anything uploaded after `astro build` is invisible to it; this route is the
24
+ // fallback that makes production uploads servable. Static files win when both
25
+ // exist — same bytes either way.
26
+ export const GET: APIRoute = async ({ params }) => {
27
+ const storagePath = `/uploads/${params.path}`;
28
+ const storage = getStorage();
29
+
30
+ let body: ReadableStream | ArrayBuffer | null;
31
+ let size: number;
32
+ try {
33
+ if (storage.getFileStream) {
34
+ const file = await storage.getFileStream(storagePath);
35
+ body = file?.body ?? null;
36
+ size = file?.size ?? 0;
37
+ } else {
38
+ // Custom adapters implement only the three-method contract — buffer instead.
39
+ body = await storage.getFile(storagePath);
40
+ size = body instanceof ArrayBuffer ? body.byteLength : 0;
41
+ }
42
+ } catch {
43
+ body = null; // traversal attempt or adapter error — same 404 as a missing file
44
+ size = 0;
45
+ }
46
+
47
+ if (!body) {
48
+ return new Response("Not found", { status: 404 });
49
+ }
50
+
51
+ const ext = storagePath.substring(storagePath.lastIndexOf(".")).toLowerCase();
52
+ const contentType = MIME_TYPES[ext] || "application/octet-stream";
53
+
54
+ return new Response(body, {
55
+ headers: {
56
+ "Content-Type": contentType,
57
+ "Content-Length": String(size),
58
+ "Cache-Control": "public, max-age=31536000, immutable",
59
+ },
60
+ });
61
+ };
@@ -1,37 +0,0 @@
1
- import type { APIRoute } from "astro";
2
- import { getFileStream } from "./storage";
3
-
4
- export const prerender = false;
5
-
6
- const MIME_TYPES: Record<string, string> = {
7
- ".jpg": "image/jpeg",
8
- ".jpeg": "image/jpeg",
9
- ".png": "image/png",
10
- ".gif": "image/gif",
11
- ".webp": "image/webp",
12
- ".avif": "image/avif",
13
- ".svg": "image/svg+xml",
14
- ".pdf": "application/pdf",
15
- ".mp4": "video/mp4",
16
- ".webm": "video/webm",
17
- };
18
-
19
- export const GET: APIRoute = async ({ params }) => {
20
- const filePath = `/uploads/${params.path}`;
21
- const file = await getFileStream(filePath);
22
-
23
- if (!file) {
24
- return new Response("Not found", { status: 404 });
25
- }
26
-
27
- const ext = filePath.substring(filePath.lastIndexOf(".")).toLowerCase();
28
- const contentType = MIME_TYPES[ext] || "application/octet-stream";
29
-
30
- return new Response(file.body, {
31
- headers: {
32
- "Content-Type": contentType,
33
- "Content-Length": String(file.size),
34
- "Cache-Control": "public, max-age=31536000, immutable",
35
- },
36
- });
37
- };