@kidecms/core 0.13.0 → 0.14.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.
@@ -67,7 +67,7 @@ const {
67
67
  };
68
68
 
69
69
  const user = Astro.locals.user;
70
- const runtimeContext = user ? { user: { id: user.id, role: user.role, email: user.email } } : {};
70
+ const runtimeContext = user ? { user } : {};
71
71
  const cmsRuntime = cms as Record<string, any> & { meta: typeof cms.meta };
72
72
  const locales = config.locales?.supported ?? [];
73
73
  const defaultLocale = config.locales?.default ?? "en";
@@ -28,7 +28,7 @@ type CollectionLike = {
28
28
 
29
29
  const { collection } = Astro.props as { collection: CollectionLike };
30
30
  const user = Astro.locals.user;
31
- const runtimeContext = user ? { user: { id: user.id, role: user.role, email: user.email } } : {};
31
+ const runtimeContext = user ? { user } : {};
32
32
  const cmsRuntime = cms as Record<string, any>;
33
33
  const defaultLocale = config.locales?.default ?? "en";
34
34
  const locales = config.locales?.supported ?? [];
@@ -6,7 +6,7 @@ import { formatDate, getLabelField } from "../../core";
6
6
  import { canRead, getVisualStatus } from "../lib/access";
7
7
 
8
8
  const user = Astro.locals.user;
9
- const runtimeContext = user ? { user: { id: user.id, role: user.role, email: user.email } } : {};
9
+ const runtimeContext = user ? { user } : {};
10
10
  const cmsRuntime = cms as Record<string, any>;
11
11
  const defaultLocale = config.locales?.default ?? "en";
12
12
 
@@ -6,7 +6,7 @@ import { formatDate } from "../../core";
6
6
  import { canRead } from "../lib/access";
7
7
 
8
8
  const user = Astro.locals.user;
9
- const runtimeContext = user ? { user: { id: user.id, role: user.role, email: user.email } } : {};
9
+ const runtimeContext = user ? { user } : {};
10
10
  const cmsRuntime = cms as Record<string, any>;
11
11
  const defaultLocale = config.locales?.default ?? "en";
12
12
 
package/core/audit.ts CHANGED
@@ -75,7 +75,9 @@ export const recordAudit = async (event: AuditEvent): Promise<void> => {
75
75
  }
76
76
  };
77
77
 
78
- export const pruneAuditLog = async (olderThanMs: number): Promise<number> => {
78
+ const AUDIT_RETENTION_MS = 90 * 24 * 60 * 60 * 1000;
79
+
80
+ export const pruneAuditLog = async (olderThanMs: number = AUDIT_RETENTION_MS): Promise<number> => {
79
81
  const db = await getDb();
80
82
  const schema = getSchema();
81
83
  const cutoff = Date.now() - olderThanMs;
package/core/auth.ts CHANGED
@@ -4,12 +4,15 @@ import { nanoid } from "nanoid";
4
4
  import { getDb } from "./runtime";
5
5
  import { getSchema } from "./schema";
6
6
 
7
- // 600k per OWASP. Iteration count is stored per-hash, so existing 100k hashes still verify.
8
- const ITERATIONS = 600_000;
7
+ // 600k per OWASP, but Cloudflare Workers rejects counts above 100k in production
8
+ // (not in wrangler dev, so only deploys hit it). Iteration count is stored
9
+ // per-hash, so hashes created at either count keep verifying on both runtimes.
10
+ const isWorkers = typeof navigator !== "undefined" && navigator.userAgent === "Cloudflare-Workers";
11
+ const ITERATIONS = isWorkers ? 100_000 : 600_000;
9
12
  const HASH_LENGTH = 32;
10
13
  const SALT_LENGTH = 16;
11
14
 
12
- export const MIN_PASSWORD_LENGTH = 12;
15
+ export const MIN_PASSWORD_LENGTH = 8;
13
16
 
14
17
  const encode = (buffer: ArrayBuffer) => btoa(String.fromCharCode(...new Uint8Array(buffer)));
15
18
 
@@ -66,13 +69,21 @@ export const verifyPassword = async (hash: string, plain: string): Promise<boole
66
69
  const keyMaterial = await crypto.subtle.importKey("raw", new TextEncoder().encode(plain), "PBKDF2", false, [
67
70
  "deriveBits",
68
71
  ]);
69
- const derived = new Uint8Array(
70
- await crypto.subtle.deriveBits(
71
- { name: "PBKDF2", salt, iterations, hash: "SHA-256" },
72
- keyMaterial,
73
- expected.length * 8,
74
- ),
75
- );
72
+ let derived: Uint8Array;
73
+ try {
74
+ derived = new Uint8Array(
75
+ await crypto.subtle.deriveBits(
76
+ { name: "PBKDF2", salt, iterations, hash: "SHA-256" },
77
+ keyMaterial,
78
+ expected.length * 8,
79
+ ),
80
+ );
81
+ } catch (error) {
82
+ // Workers rejects iteration counts above 100k — a hash created on Node can
83
+ // land here after a database move. Fail the login instead of crashing.
84
+ console.error("[auth] verifyPassword failed to derive", error);
85
+ return false;
86
+ }
76
87
 
77
88
  if (derived.length !== expected.length) return false;
78
89
  let diff = 0;
package/core/define.ts CHANGED
@@ -144,7 +144,6 @@ export type ArrayFieldConfig = BaseFieldConfig<"array", unknown[]> & {
144
144
  };
145
145
 
146
146
  export type JsonFieldConfig = BaseFieldConfig<"json", unknown> & {
147
- schema?: string;
148
147
  /**
149
148
  * Typed repeater rows. When set together with `admin.component: "repeater"`,
150
149
  * the block editor renders each array item as these named sub-fields (text,
@@ -218,7 +217,6 @@ export type CollectionConfig = {
218
217
  fields: CollectionFieldMap;
219
218
  views?: CollectionViewConfig;
220
219
  admin?: CollectionAdminConfig;
221
- seed?: SeedDocument[];
222
220
  };
223
221
 
224
222
  export type AdminNavItem = {
@@ -5,10 +5,11 @@
5
5
  * installed as a package dependency — a static import of a project file would
6
6
  * only exist in the embedded case.
7
7
  */
8
+ import { existsSync } from "node:fs";
8
9
  import path from "node:path";
9
10
  import { pathToFileURL } from "node:url";
10
11
 
11
- import type { CMSConfig } from "../core";
12
+ import type { CMSConfig, SeedDocument } from "../core";
12
13
 
13
14
  const projectRoot = process.cwd();
14
15
 
@@ -23,4 +24,8 @@ export const loadProjectConfig = async (): Promise<CMSConfig> => (await importPr
23
24
 
24
25
  export const loadGeneratedApi = () => importProject("src/cms/.generated/api.ts");
25
26
 
27
+ /** Seed content is optional — projects without src/cms/seed.ts seed nothing. */
28
+ export const loadProjectSeedData = async (): Promise<Record<string, SeedDocument[]>> =>
29
+ existsSync(projectPath("src/cms/seed.ts")) ? (await importProject("src/cms/seed.ts")).default : {};
30
+
26
31
  export const loadGeneratedSchema = () => importProject("src/cms/.generated/schema.ts");
package/internals/seed.ts CHANGED
@@ -1,11 +1,15 @@
1
1
  import { closeDb, seedDatabase } from "../core";
2
- import { loadProjectConfig, loadProjectRuntime } from "./project";
3
- import seedData from "./seed.data";
2
+ import { loadProjectConfig, loadProjectRuntime, loadProjectSeedData } from "./project";
4
3
 
5
4
  await loadProjectRuntime();
6
5
  const config = await loadProjectConfig();
6
+ const seedData = await loadProjectSeedData();
7
7
 
8
- await seedDatabase(config, seedData);
8
+ if (Object.keys(seedData).length === 0) {
9
+ console.log("No seed content found. Define documents in src/cms/seed.ts (default export keyed by collection slug).");
10
+ } else {
11
+ await seedDatabase(config, seedData);
12
+ }
9
13
  // Release the DB / local platform proxy so the process can exit (matters on the
10
14
  // Cloudflare target, where the binding proxy otherwise keeps Node alive).
11
15
  await closeDb();
@@ -239,12 +239,16 @@ const resolveCommit = (repoDir: string, ref: string) => {
239
239
  return result.stdout;
240
240
  };
241
241
 
242
+ // Template-only dirs that never exist in scaffolded projects — excluded from packets.
243
+ const TEMPLATE_ONLY_PREFIXES = ["starters/"];
244
+
242
245
  const listChangedFiles = (repoDir: string, fromCommit: string, toCommit: string) => {
243
246
  const output = run("git", ["diff", "--name-only", fromCommit, toCommit], { cwd: repoDir });
244
247
  return output
245
248
  .split("\n")
246
249
  .map((line) => line.trim())
247
- .filter(Boolean);
250
+ .filter(Boolean)
251
+ .filter((file) => !TEMPLATE_ONLY_PREFIXES.some((prefix) => file.startsWith(prefix)));
248
252
  };
249
253
 
250
254
  const diffForPaths = (repoDir: string, fromCommit: string, toCommit: string, files: string[]) => {
@@ -265,6 +269,9 @@ const changedFilesJson = (repoDir: string, fromCommit: string, toCommit: string)
265
269
  };
266
270
 
267
271
  const isManagedRuntimePath = (file: string, corePath: string) => {
272
+ // Legacy seed-data file may carry user customizations — careful review, not auto-apply.
273
+ if (file === `${corePath}/internals/seed.data.ts`) return false;
274
+
268
275
  const managedPrefixes = [
269
276
  `${corePath}/admin`,
270
277
  `${corePath}/client`,
@@ -288,6 +295,8 @@ const isCarefulPath = (file: string, corePath: string) => {
288
295
  "src/env.d.ts",
289
296
  "src/styles/admin.css",
290
297
  `${corePath}/runtime.ts`,
298
+ `${corePath}/seed.ts`,
299
+ `${corePath}/internals/seed.data.ts`,
291
300
  ]);
292
301
  const carefulPrefixes = [
293
302
  `${corePath}/adapters`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kidecms/core",
3
- "version": "0.13.0",
3
+ "version": "0.14.1",
4
4
  "description": "Kide CMS runtime — core library, admin UI, routes, and middleware for Astro. Shipped as TypeScript source.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -16,7 +16,7 @@ initDateFormat(config, Astro.cookies.get("tz")?.value);
16
16
 
17
17
  const route = resolveAdminRoute(Astro.params.path);
18
18
  const user = Astro.locals.user;
19
- const runtimeContext = user ? { user: { id: user.id, role: user.role, email: user.email } } : {};
19
+ const runtimeContext = user ? { user } : {};
20
20
  const isEmbed = Astro.url.searchParams.get("_embed") === "1";
21
21
  const accessibleCollections = config.collections.filter((c) => canRead(config, user, c.slug));
22
22
 
@@ -6,6 +6,7 @@ import { Input } from "../../admin/components/ui/input";
6
6
  import { Label } from "../../admin/components/ui/label";
7
7
  import { buttonVariants } from "../../admin/components/ui/button";
8
8
  import { validateInvite } from "virtual:kide/runtime";
9
+ import { MIN_PASSWORD_LENGTH } from "../../core";
9
10
  import { getDb } from "virtual:kide/db";
10
11
  import { eq } from "drizzle-orm";
11
12
 
@@ -37,7 +38,7 @@ const errorMessage =
37
38
  : error === "password"
38
39
  ? "Passwords do not match."
39
40
  : error === "short"
40
- ? "Password must be at least 12 characters."
41
+ ? `Password must be at least ${MIN_PASSWORD_LENGTH} characters.`
41
42
  : error === "expired" || error === "invalid"
42
43
  ? "This invite link has expired or already been used."
43
44
  : null;
@@ -89,7 +90,13 @@ const errorMessage =
89
90
  </div>
90
91
  <div class="grid gap-2">
91
92
  <Label htmlFor="password">Password</Label>
92
- <Input id="password" name="password" type="password" placeholder="Min 12 characters" required />
93
+ <Input
94
+ id="password"
95
+ name="password"
96
+ type="password"
97
+ placeholder={`Min ${MIN_PASSWORD_LENGTH} characters`}
98
+ required
99
+ />
93
100
  </div>
94
101
  <div class="grid gap-2">
95
102
  <Label htmlFor="confirmPassword">Confirm password</Label>
@@ -5,7 +5,7 @@ import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "../..
5
5
  import { Input } from "../../admin/components/ui/input";
6
6
  import { Label } from "../../admin/components/ui/label";
7
7
  import { buttonVariants } from "../../admin/components/ui/button";
8
- import { resolveAdminAuth } from "../../core";
8
+ import { resolveAdminAuth, MIN_PASSWORD_LENGTH } from "../../core";
9
9
  import config from "virtual:kide/config";
10
10
 
11
11
  export const prerender = false;
@@ -21,7 +21,7 @@ const errorMessage =
21
21
  : error === "password"
22
22
  ? "Passwords do not match."
23
23
  : error === "short"
24
- ? "Password must be at least 12 characters."
24
+ ? `Password must be at least ${MIN_PASSWORD_LENGTH} characters.`
25
25
  : error === "invalid"
26
26
  ? "This reset link is invalid or expired."
27
27
  : null;
@@ -6,6 +6,7 @@ import { Input } from "../../admin/components/ui/input";
6
6
  import { Label } from "../../admin/components/ui/label";
7
7
  import { buttonVariants } from "../../admin/components/ui/button";
8
8
  import { getDb } from "virtual:kide/db";
9
+ import { MIN_PASSWORD_LENGTH } from "../../core";
9
10
 
10
11
  export const prerender = false;
11
12
 
@@ -27,7 +28,7 @@ const errorMessage =
27
28
  : error === "password"
28
29
  ? "Passwords do not match."
29
30
  : error === "short"
30
- ? "Password must be at least 12 characters."
31
+ ? `Password must be at least ${MIN_PASSWORD_LENGTH} characters.`
31
32
  : null;
32
33
  ---
33
34
 
@@ -87,7 +88,7 @@ const errorMessage =
87
88
  id="password"
88
89
  name="password"
89
90
  type="password"
90
- placeholder="Min 12 characters"
91
+ placeholder={`Min ${MIN_PASSWORD_LENGTH} characters`}
91
92
  autoComplete="new-password"
92
93
  required
93
94
  />
@@ -228,7 +228,7 @@ const getRuntimeContext = (
228
228
  ) => {
229
229
  const user = locals.user;
230
230
  return {
231
- ...(user ? { user: { id: user.id, role: user.role, email: user.email } } : {}),
231
+ ...(user ? { user } : {}),
232
232
  ...(cache ? { cache } : {}),
233
233
  };
234
234
  };
@@ -1,7 +1,7 @@
1
1
  import type { APIRoute } from "astro";
2
2
 
3
3
  import { cms } from "virtual:kide/api";
4
- import { pruneRateLimits } from "../../../core";
4
+ import { pruneAuditLog, pruneRateLimits } from "../../../core";
5
5
 
6
6
  import { isAuthorized, unauthorized } from "./_authorize";
7
7
 
@@ -18,6 +18,7 @@ const handler: APIRoute = async ({ request }) => {
18
18
  const result = await cmsRuntime.tasks.drain();
19
19
  await cmsRuntime.tasks.prune();
20
20
  await pruneRateLimits();
21
+ await pruneAuditLog();
21
22
 
22
23
  return Response.json({
23
24
  ok: true,
@@ -1,7 +0,0 @@
1
- import type { SeedDocument } from "../core";
2
-
3
- // Kide ships no seed content. Define your own documents here (keyed by
4
- // collection slug) and run `kide seed` to load them into an empty database.
5
- const seedData: Record<string, SeedDocument[]> = {};
6
-
7
- export default seedData;