sandcms 0.6.3 → 0.7.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.
Files changed (124) hide show
  1. package/README.md +14 -10
  2. package/dist/admin/Account.svelte +2 -0
  3. package/dist/admin/Admin.svelte +14 -1
  4. package/dist/admin/AssetAttachment.svelte +1 -3
  5. package/dist/admin/AssetAttachment.svelte.d.ts +0 -1
  6. package/dist/admin/BlueprintFields.svelte +0 -3
  7. package/dist/admin/BlueprintFields.svelte.d.ts +0 -1
  8. package/dist/admin/Editor.svelte +0 -1
  9. package/dist/admin/Fields.svelte +0 -7
  10. package/dist/admin/Fields.svelte.d.ts +0 -1
  11. package/dist/admin/Gallery.svelte +2 -6
  12. package/dist/admin/Gallery.svelte.d.ts +0 -1
  13. package/dist/admin/LinkField.svelte +0 -3
  14. package/dist/admin/LinkField.svelte.d.ts +0 -1
  15. package/dist/admin/MediaLibrary.svelte +10 -7
  16. package/dist/admin/MediaPicker.svelte +7 -15
  17. package/dist/admin/MediaPicker.svelte.d.ts +0 -1
  18. package/dist/admin/ThemePicker.svelte +72 -0
  19. package/dist/admin/ThemePicker.svelte.d.ts +18 -0
  20. package/dist/admin/UploadButton.svelte +2 -1
  21. package/dist/admin/UploadProgress.svelte +24 -0
  22. package/dist/admin/UploadProgress.svelte.d.ts +7 -0
  23. package/dist/admin/client.d.ts +3 -670
  24. package/dist/admin/client.js +2 -1
  25. package/dist/admin/components/ui/alert/alert.svelte.d.ts +3 -13
  26. package/dist/admin/components/ui/attachment/attachment-media.svelte.d.ts +3 -13
  27. package/dist/admin/components/ui/attachment/attachment.svelte.d.ts +6 -26
  28. package/dist/admin/components/ui/badge/badge.svelte.d.ts +7 -25
  29. package/dist/admin/components/ui/button/button.svelte.d.ts +15 -53
  30. package/dist/admin/components/ui/toggle/toggle.svelte.d.ts +6 -26
  31. package/dist/admin/generated/admin.css +529 -101
  32. package/dist/admin/theme-context.d.ts +7 -0
  33. package/dist/admin/theme-context.js +3 -0
  34. package/dist/admin/theme.d.ts +7 -0
  35. package/dist/admin/theme.js +14 -0
  36. package/dist/admin/types.d.ts +2 -1
  37. package/dist/admin/uploads.d.ts +8 -1
  38. package/dist/admin/uploads.js +94 -16
  39. package/dist/blueprints/files.js +1 -0
  40. package/dist/blueprints/load.js +7 -2
  41. package/dist/blueprints/types.d.ts +4 -1
  42. package/dist/cli/doctor.d.ts +3 -1
  43. package/dist/cli/doctor.js +85 -30
  44. package/dist/cli/files.d.ts +2 -6
  45. package/dist/cli/files.js +11 -2
  46. package/dist/cli/index.js +99 -31
  47. package/dist/cli/scaffold.js +8 -8
  48. package/dist/cli/setup.js +16 -26
  49. package/dist/cli/upgrade.js +21 -2
  50. package/dist/env.d.ts +32 -0
  51. package/dist/env.js +53 -3
  52. package/dist/server/admin-load.d.ts +13 -5
  53. package/dist/server/admin-load.js +6 -1
  54. package/dist/server/api-access.d.ts +4 -0
  55. package/dist/server/api-access.js +15 -2
  56. package/dist/server/api-pagination.d.ts +3 -0
  57. package/dist/server/api.js +5 -2
  58. package/dist/server/auth.d.ts +10 -9
  59. package/dist/server/cms.d.ts +65 -18
  60. package/dist/server/cms.js +62 -6
  61. package/dist/server/db/migration-data.js +4 -0
  62. package/dist/server/db/migrations/0009_local_media.sql +9 -0
  63. package/dist/server/db/migrations/meta/0009_snapshot.json +1763 -0
  64. package/dist/server/db/migrations/meta/_journal.json +7 -0
  65. package/dist/server/db/schema.d.ts +119 -2
  66. package/dist/server/db/schema.js +15 -3
  67. package/dist/server/hooks.js +155 -81
  68. package/dist/server/media/contracts.d.ts +115 -0
  69. package/dist/server/media/contracts.js +12 -0
  70. package/dist/server/media/deletions.d.ts +2 -1
  71. package/dist/server/media/deletions.js +3 -3
  72. package/dist/server/media/delivery.js +7 -11
  73. package/dist/server/media/import-r2.d.ts +56 -0
  74. package/dist/server/media/import-r2.js +342 -0
  75. package/dist/server/media/processor-child.d.ts +2 -0
  76. package/dist/server/media/processor-child.js +236 -0
  77. package/dist/server/media/processor.d.ts +51 -0
  78. package/dist/server/media/processor.js +135 -0
  79. package/dist/server/media/render-math.d.ts +24 -0
  80. package/dist/server/media/render-math.js +50 -0
  81. package/dist/server/media/runner.d.ts +30 -1
  82. package/dist/server/media/runner.js +418 -165
  83. package/dist/server/media/service.d.ts +56 -10
  84. package/dist/server/media/service.js +309 -98
  85. package/dist/server/media/storage.d.ts +5 -0
  86. package/dist/server/media/storage.js +279 -0
  87. package/dist/server/media/svg.d.ts +5 -0
  88. package/dist/server/media/svg.js +142 -0
  89. package/dist/server/media/upload-ticket.d.ts +12 -0
  90. package/dist/server/media/upload-ticket.js +35 -0
  91. package/dist/server/media/verify.d.ts +36 -0
  92. package/dist/server/media/verify.js +109 -0
  93. package/dist/server/platform/http.d.ts +27 -0
  94. package/dist/server/platform/http.js +77 -0
  95. package/dist/server/platform/index.d.ts +52 -0
  96. package/dist/server/platform/index.js +98 -0
  97. package/dist/server/platform/snapshots.d.ts +18 -0
  98. package/dist/server/platform/snapshots.js +110 -0
  99. package/dist/server/platform/volume.d.ts +20 -0
  100. package/dist/server/platform/volume.js +60 -0
  101. package/dist/worker/svg.d.ts +1 -5
  102. package/dist/worker/svg.js +2 -142
  103. package/docs/acceptance.md +6 -4
  104. package/docs/api.md +41 -3
  105. package/docs/cloudflare.md +48 -142
  106. package/docs/content.md +2 -2
  107. package/docs/deployment.md +159 -57
  108. package/docs/forms.md +1 -1
  109. package/docs/media.md +85 -21
  110. package/docs/package.md +21 -18
  111. package/docs/plans/0.7-self-hosted.md +264 -0
  112. package/docs/releases.md +17 -1
  113. package/docs/upgrades.md +35 -6
  114. package/package.json +36 -37
  115. package/templates/Dockerfile +28 -0
  116. package/templates/integration.ts +18 -2
  117. package/templates/railpack.json +1 -1
  118. package/templates/sandcms/blueprints/files/image.yaml +1 -1
  119. package/templates/sandcms/config.yaml +0 -1
  120. package/dist/cli/cloudflare.d.ts +0 -8
  121. package/dist/cli/cloudflare.js +0 -135
  122. package/templates/sandcms/worker/index.ts +0 -1
  123. package/templates/sandcms/worker/local.ts +0 -1
  124. package/templates/sandcms/worker/wrangler.jsonc +0 -6
@@ -0,0 +1,7 @@
1
+ import type { Theme } from './theme.js';
2
+ export declare const themeContext: unique symbol;
3
+ export interface ThemeState {
4
+ readonly current: Theme;
5
+ set(theme: Theme): void;
6
+ }
7
+ export declare const useTheme: () => ThemeState;
@@ -0,0 +1,3 @@
1
+ import { getContext } from 'svelte';
2
+ export const themeContext = Symbol('sandcms-theme');
3
+ export const useTheme = () => getContext(themeContext);
@@ -0,0 +1,7 @@
1
+ /** Admin colour themes. `system` follows the operating system's light/dark preference. */
2
+ export declare const themes: readonly ["system", "light", "dark"];
3
+ export type Theme = (typeof themes)[number];
4
+ export declare const themeCookie = "sandcms-theme";
5
+ export declare function parseTheme(value: unknown): Theme;
6
+ /** Remember the choice for this browser; `system` clears the cookie. */
7
+ export declare function persistTheme(theme: Theme): void;
@@ -0,0 +1,14 @@
1
+ /** Admin colour themes. `system` follows the operating system's light/dark preference. */
2
+ export const themes = ['system', 'light', 'dark'];
3
+ export const themeCookie = 'sandcms-theme';
4
+ export function parseTheme(value) {
5
+ return themes.includes(value) ? value : 'system';
6
+ }
7
+ /** Remember the choice for this browser; `system` clears the cookie. */
8
+ export function persistTheme(theme) {
9
+ const secure = location.protocol === 'https:' ? '; Secure' : '';
10
+ document.cookie =
11
+ theme === 'system'
12
+ ? `${themeCookie}=; Path=/admin; Max-Age=0; SameSite=Lax${secure}`
13
+ : `${themeCookie}=${theme}; Path=/admin; Max-Age=31536000; SameSite=Lax${secure}`;
14
+ }
@@ -3,6 +3,7 @@ import type { Blueprint, FileBlueprint } from '../blueprints/types.js';
3
3
  import type { LucideIconData } from '@lucide/svelte';
4
4
  import type { documents, media, submissions, NotificationSettings } from '../server/db/schema.js';
5
5
  import type { CmsUser } from '../server/auth.js';
6
+ import type { Theme } from './theme.js';
6
7
  export type Entry = typeof documents.$inferSelect;
7
8
  export type Asset = typeof media.$inferSelect & {
8
9
  expectedVersion?: string;
@@ -14,6 +15,7 @@ export type Relations = Record<string, {
14
15
  }[]>;
15
16
  export interface AdminData {
16
17
  screen: string;
18
+ theme?: Theme;
17
19
  emailEnabled?: boolean;
18
20
  next?: string;
19
21
  invitations?: {
@@ -37,7 +39,6 @@ export interface AdminData {
37
39
  blueprints?: (Blueprint & {
38
40
  navigationIcon: LucideIconData;
39
41
  })[];
40
- workerUrl?: string;
41
42
  fileBlueprints?: FileBlueprint[];
42
43
  defaultFileBlueprint?: string;
43
44
  mediaVersion?: string;
@@ -2,6 +2,8 @@ import type { Asset } from './types.js';
2
2
  export type UploadItem = {
3
3
  name: string;
4
4
  bytes: number;
5
+ /** Bytes the server has confirmed so far. */
6
+ sent?: number;
5
7
  state: 'idle' | 'uploading' | 'error' | 'done';
6
8
  error?: string;
7
9
  };
@@ -9,4 +11,9 @@ export type UploadFailure = {
9
11
  file: File;
10
12
  message: string;
11
13
  };
12
- export declare function uploadImages(files: File[], onAsset: (asset: Asset) => void, onProgress: (completed: number, total: number, failed: number) => void, blueprint?: string, onState?: (index: number, state: UploadItem['state'], error?: string) => void): Promise<UploadFailure[]>;
14
+ /** Attempts per chunk and the base backoff delay in milliseconds (doubled per retry). */
15
+ export declare const chunkRetry: {
16
+ attempts: number;
17
+ delay: number;
18
+ };
19
+ export declare function uploadImages(files: File[], onAsset: (asset: Asset) => void, onProgress: (completed: number, total: number, failed: number) => void, blueprint?: string, onState?: (index: number, state: UploadItem['state'], error?: string) => void, onBytes?: (index: number, sent: number, total: number) => void): Promise<UploadFailure[]>;
@@ -1,29 +1,106 @@
1
1
  import { api, message } from './client.js';
2
- export async function uploadImages(files, onAsset, onProgress, blueprint, onState) {
2
+ /** Attempts per chunk and the base backoff delay in milliseconds (doubled per retry). */
3
+ export const chunkRetry = { attempts: 3, delay: 500 };
4
+ /** Unfinished uploads, so retrying the same file resumes instead of starting over. */
5
+ const pending = new WeakMap();
6
+ const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
7
+ /**
8
+ * What the server holds for this upload, or null when it can't be read (expired, unknown or unreachable).
9
+ * `complete` means the chunks were already assembled, e.g. after a lost completion response.
10
+ */
11
+ async function stored(ticket) {
12
+ try {
13
+ const response = await fetch(ticket.url, { headers: ticket.headers });
14
+ if (response.ok) {
15
+ const data = (await response.json());
16
+ return { received: new Set(data.received), complete: !!data.complete };
17
+ }
18
+ }
19
+ catch {
20
+ // Unreachable; callers keep their own view.
21
+ }
22
+ return null;
23
+ }
24
+ /** PUT one chunk. Resolves to the stored indexes, or a failure marked transient when a retry may help. */
25
+ async function put(ticket, index, body) {
26
+ try {
27
+ const response = await fetch(`${ticket.url}?index=${index}`, {
28
+ method: 'PUT',
29
+ headers: ticket.headers,
30
+ body
31
+ });
32
+ if (response.ok)
33
+ return (await response.json()).received;
34
+ const data = await response.json().catch(() => ({}));
35
+ return {
36
+ error: new Error(data.error ?? data.message ?? 'Upload failed. Please try again.'),
37
+ transient: response.status >= 500 || [408, 429].includes(response.status)
38
+ };
39
+ }
40
+ catch {
41
+ return {
42
+ error: new Error('Upload interrupted. Check your connection and try again.'),
43
+ transient: true
44
+ };
45
+ }
46
+ }
47
+ /** Send the chunks the server lacks, in order, retrying transient failures with backoff. */
48
+ async function transfer(file, ticket, received, onBytes) {
49
+ const count = Math.max(1, Math.ceil(file.size / ticket.chunkSize));
50
+ const chunk = (index) => file.slice(index * ticket.chunkSize, (index + 1) * ticket.chunkSize);
51
+ const sent = () => [...received].reduce((total, index) => total + chunk(index).size, 0);
52
+ onBytes(sent());
53
+ for (let index = 0; index < count; index++) {
54
+ if (received.has(index))
55
+ continue;
56
+ for (let attempt = 1;; attempt++) {
57
+ const result = await put(ticket, index, chunk(index));
58
+ if (Array.isArray(result)) {
59
+ received = new Set([...received, ...result, index]);
60
+ break;
61
+ }
62
+ if (!result.transient || attempt >= chunkRetry.attempts)
63
+ throw result.error;
64
+ await wait(chunkRetry.delay * 2 ** (attempt - 1));
65
+ const status = await stored(ticket);
66
+ if (status?.complete)
67
+ return;
68
+ received = status?.received ?? received;
69
+ if (received.has(index))
70
+ break;
71
+ }
72
+ onBytes(sent());
73
+ }
74
+ }
75
+ export async function uploadImages(files, onAsset, onProgress, blueprint, onState, onBytes) {
3
76
  const failures = [];
4
77
  onProgress(0, files.length, 0);
5
- // Transfer one file at a time directly to private R2 staging.
78
+ // Upload one file at a time, in chunks the site stores until completion.
6
79
  for (const [index, file] of files.entries()) {
7
80
  onState?.(index, 'uploading');
8
81
  try {
9
- const signed = await api('media/upload', {
10
- name: file.name,
11
- type: file.type,
12
- size: file.size,
13
- blueprint
14
- });
15
- const response = await fetch(signed.url, {
16
- method: 'PUT',
17
- headers: signed.headers,
18
- body: file
19
- });
20
- if (!response.ok)
21
- throw new Error('Upload failed. Please try again.');
82
+ // Resume when the server still knows this upload; an expired or unknown one (404/410) starts over.
83
+ let ticket = pending.get(file);
84
+ let status = ticket ? await stored(ticket) : null;
85
+ if (!ticket || !status) {
86
+ ticket = await api('media/upload', {
87
+ name: file.name,
88
+ type: file.type,
89
+ size: file.size,
90
+ blueprint
91
+ });
92
+ pending.set(file, ticket);
93
+ status = { received: new Set(), complete: false };
94
+ }
95
+ if (status.complete)
96
+ onBytes?.(index, file.size, file.size);
97
+ else
98
+ await transfer(file, ticket, status.received, (sent) => onBytes?.(index, sent, file.size));
22
99
  // Completion is idempotent; retry a lost response without transferring the file again.
23
100
  let asset;
24
101
  for (let attempt = 0; attempt < 3; attempt++) {
25
102
  try {
26
- asset = await api('media/upload/complete', { id: signed.id });
103
+ asset = await api('media/upload/complete', { id: ticket.id });
27
104
  break;
28
105
  }
29
106
  catch (error) {
@@ -31,6 +108,7 @@ export async function uploadImages(files, onAsset, onProgress, blueprint, onStat
31
108
  throw error;
32
109
  }
33
110
  }
111
+ pending.delete(file);
34
112
  onAsset(asset);
35
113
  onState?.(index, 'done');
36
114
  }
@@ -4,6 +4,7 @@ export const imageMimeTypes = [
4
4
  'image/webp',
5
5
  'image/avif',
6
6
  'image/gif',
7
+ 'image/tiff',
7
8
  'image/svg+xml'
8
9
  ];
9
10
  export function fileBlueprints(definition) {
@@ -206,10 +206,15 @@ const config = z
206
206
  name: z.string().min(1),
207
207
  media: z
208
208
  .object({
209
- workerUrl: z.url(),
209
+ /** @deprecated 0.7.0: media is stored and processed locally. Only `sandcms media import-r2` reads it. */
210
+ workerUrl: z.url().optional(),
210
211
  photographerCollection: key.optional(),
211
212
  defaultFileBlueprint: key.optional(),
212
- uploadLimit: z.number().int().min(1).max(100_000_000).default(100_000_000),
213
+ uploadLimit: z.number().int().min(1).max(500_000_000).default(100_000_000),
214
+ /** Largest accepted source image area in pixels. */
215
+ maxPixels: z.number().int().min(1_000_000).max(1_000_000_000).default(100_000_000),
216
+ /** Optional per-site quota for originals and variants, in bytes. */
217
+ maxStorage: z.number().int().min(1).optional(),
213
218
  presets: z
214
219
  .record(z.string().regex(/^[a-z][a-z0-9_-]*$/, 'Use a lowercase media preset key'), z
215
220
  .object({
@@ -81,10 +81,13 @@ export interface ImagePreset {
81
81
  export interface SiteConfig {
82
82
  name: string;
83
83
  media: {
84
- workerUrl: string;
84
+ /** @deprecated 0.7.0: only read by `sandcms media import-r2`. */
85
+ workerUrl?: string;
85
86
  photographerCollection?: string;
86
87
  defaultFileBlueprint?: string;
87
88
  uploadLimit: number;
89
+ maxPixels: number;
90
+ maxStorage?: number;
88
91
  presets: Record<string, ImagePreset>;
89
92
  };
90
93
  }
@@ -1 +1,3 @@
1
- export declare function doctor(root: string, database: string): Promise<void>;
1
+ /** The Bun adapter's BODY_SIZE_LIMIT format: bytes with an optional K/M/G suffix (default 512K). */
2
+ export declare function parseBodySizeLimit(value?: string): number;
3
+ export declare function doctor(root: string, database: string, env?: NodeJS.ProcessEnv): Promise<void>;
@@ -1,20 +1,46 @@
1
- import { existsSync, readFileSync, accessSync, constants } from 'node:fs';
2
- import { join, dirname } from 'node:path';
3
- import { createCMS } from '../server/cms.js';
1
+ import { existsSync, readFileSync, accessSync, constants, statSync, statfsSync } from 'node:fs';
2
+ import { join, dirname, resolve } from 'node:path';
4
3
  import { loadDefinition } from '../blueprints/load.js';
5
4
  import { inspectMigrations } from '../server/db/store.js';
6
- export async function doctor(root, database) {
5
+ import { UPLOAD_CHUNK_SIZE } from '../server/media/contracts.js';
6
+ import { onMountedVolume, lastSnapshot, SNAPSHOT_INTERVAL } from '../server/platform/index.js';
7
+ import { walk } from './files.js';
8
+ const GB = 1024 ** 3;
9
+ const size = (bytes) => `${(bytes / GB).toFixed(1)} GB`;
10
+ /** The Bun adapter's BODY_SIZE_LIMIT format: bytes with an optional K/M/G suffix (default 512K). */
11
+ export function parseBodySizeLimit(value = '512K') {
12
+ if (value === 'Infinity')
13
+ return Infinity;
14
+ const multiplier = { K: 1024, M: 1024 ** 2, G: 1024 ** 3 }[value.at(-1).toUpperCase()];
15
+ return Number(multiplier ? value.slice(0, -1) : value) * (multiplier ?? 1);
16
+ }
17
+ const writable = (path) => {
18
+ try {
19
+ accessSync(path, constants.W_OK);
20
+ return true;
21
+ }
22
+ catch {
23
+ return false;
24
+ }
25
+ };
26
+ export async function doctor(root, database, env = process.env) {
7
27
  const issues = [];
8
28
  const check = (ok, message) => {
9
29
  console.log(`${ok ? 'OK' : 'FAIL'}: ${message}`);
10
30
  if (!ok)
11
31
  issues.push(message);
12
32
  };
33
+ const warn = (message) => console.warn(`WARN: ${message}`);
13
34
  const source = (file) => existsSync(join(root, file)) ? readFileSync(join(root, file), 'utf8') : '';
14
35
  const pkg = JSON.parse(source('package.json') || '{}');
15
36
  check(pkg.packageManager === `bun@${Bun.version}`, 'packageManager pins the running Bun version');
16
- check(Boolean(source('railpack.json')), 'Railpack configuration exists');
17
- check(/@sveltejs\/adapter-bun/.test(source('vite.config.ts') + source('vite.config.js')), 'SvelteKit uses the Bun adapter');
37
+ check(Boolean(source('Dockerfile') || source('railpack.json')), 'Container build configuration exists (Dockerfile or railpack.json)');
38
+ const vite = source('vite.config.ts') + source('vite.config.js');
39
+ check(/@sveltejs\/adapter-bun/.test(vite), 'SvelteKit uses the Bun adapter');
40
+ if (/paths\s*:\s*\{[^}]*\borigin\b/.test(vite))
41
+ warn('vite config sets paths.origin. Remove it: the site derives its origin from the request and redirects other hosts to SANDCMS_ORIGIN.');
42
+ if (!/['"]sharp['"]/.test(vite))
43
+ warn('vite config does not list sharp in ssr.external.');
18
44
  check(/bun\s+.*build\/index\.js/.test(pkg.scripts?.start ?? ''), 'Production start command runs the Bun adapter output');
19
45
  for (const file of [
20
46
  'src/routes/(sandcms)/admin/[...path]/+page.server.ts',
@@ -22,38 +48,67 @@ export async function doctor(root, database) {
22
48
  'src/lib/server/sandcms/index.ts'
23
49
  ])
24
50
  check(Boolean(source(file)), `Integration exists: ${file}`);
25
- check(/cmsHandle/.test(source('src/hooks.server.ts') + source('src/hooks.server.js')), 'Server hook composes cmsHandle');
51
+ const hooks = source('src/hooks.server.ts') + source('src/hooks.server.js');
52
+ check(/cmsHandle/.test(hooks), 'Server hook composes cmsHandle');
53
+ if (/sequence\(/.test(hooks) && !/sequence\(\s*cmsHandle\b/.test(hooks))
54
+ warn('cmsHandle should run first in sequence() so request limits and redirects apply first.');
55
+ if (existsSync(join(root, 'static/robots.txt')))
56
+ warn('static/robots.txt bypasses SandCMS: SANDCMS_INDEXING=false and the SEO robots.txt cannot apply. Remove it.');
57
+ const prerendered = walk(join(root, 'src/routes')).filter((file) => /\+(page|layout)(\.server)?\.[jt]s$/.test(file) &&
58
+ /export\s+const\s+prerender\s*=\s*true/.test(readFileSync(file, 'utf8')));
59
+ if (prerendered.length)
60
+ warn(`Prerendered routes bypass server hooks (no host redirect or noindex header): ${prerendered.map((f) => f.slice(root.length + 1)).join(', ')}`);
26
61
  const definition = loadDefinition(join(root, 'sandcms'));
27
62
  console.log(`Blueprints: ${definition.blueprints.length} valid; project: ${definition.config.name}`);
28
- check((process.env.SANDCMS_SECRET?.length ?? 0) >= 32, 'SANDCMS_SECRET contains at least 32 characters');
29
- check(Boolean(process.env.SANDCMS_ORIGIN), 'SANDCMS_ORIGIN is configured');
30
- if (/^https:/i.test(process.env.SANDCMS_ORIGIN ?? '') &&
31
- !process.env.SANDCMS_TRUSTED_PROXY_HEADER?.trim())
32
- console.warn('WARN: HTTPS origin without SANDCMS_TRUSTED_PROXY_HEADER. Behind a proxy, visitors may share form and login rate limits. Configure a trusted header and the adapter ADDRESS_HEADER after securing the proxy boundary; see docs/deployment.md#client-ip-behind-dokploy-and-cloudflare.');
63
+ check((env.SANDCMS_SECRET?.length ?? 0) >= 32, 'SANDCMS_SECRET contains at least 32 characters');
64
+ check(Boolean(env.SANDCMS_ORIGIN), 'SANDCMS_ORIGIN is configured');
65
+ const minimumBody = UPLOAD_CHUNK_SIZE + 1024 ** 2;
66
+ check(parseBodySizeLimit(env.BODY_SIZE_LIMIT?.trim() || undefined) >= minimumBody, `BODY_SIZE_LIMIT (${env.BODY_SIZE_LIMIT || 'unset: 512K'}) fits an 8 MiB upload chunk; use 10M`);
67
+ const trusted = env.SANDCMS_TRUSTED_PROXY_HEADER?.trim().toLowerCase() || '', address = env.ADDRESS_HEADER?.trim().toLowerCase() || '';
68
+ if (trusted !== address)
69
+ warn(`SANDCMS_TRUSTED_PROXY_HEADER (${trusted || 'unset'}) and ADDRESS_HEADER (${address || 'unset'}) differ. Set both to the client IP header your reverse proxy sets and overwrites; see docs/deployment.md.`);
70
+ else if (/^https:/i.test(env.SANDCMS_ORIGIN ?? '') && !trusted)
71
+ warn('HTTPS origin without SANDCMS_TRUSTED_PROXY_HEADER. Behind a proxy, visitors may share form and login rate limits. Configure a trusted header and the adapter ADDRESS_HEADER; see docs/deployment.md.');
33
72
  check(existsSync(database), 'SQLite database exists');
34
73
  if (existsSync(database)) {
35
74
  accessSync(database, constants.R_OK | constants.W_OK);
36
75
  accessSync(dirname(database), constants.W_OK);
37
76
  check(inspectMigrations(database).length === 0, 'Database migrations are current');
38
77
  }
39
- console.log(`Volume target: ${database}. Confirm this directory is a persistent local volume in production; this cannot be established from application files.`);
40
- check(Boolean(process.env.SANDCMS_WORKER_TOKEN), 'Media Worker token is configured');
41
- if (issues.length)
42
- throw new Error(`Doctor found ${issues.length} issue(s). Resolve the FAIL items above.`);
43
- const cms = createCMS({
44
- database,
45
- origin: process.env.SANDCMS_ORIGIN,
46
- secret: process.env.SANDCMS_SECRET,
47
- definition,
48
- workerToken: process.env.SANDCMS_WORKER_TOKEN,
49
- workerUrl: process.env.SANDCMS_WORKER_URL
50
- });
51
- try {
52
- if ((await cms.media.health()).protocol !== 6)
53
- throw new Error('Worker protocol is incompatible');
54
- console.log('OK: Worker protocol 6 is reachable');
78
+ const dataDir = dirname(resolve(database));
79
+ if (existsSync(dataDir)) {
80
+ if (onMountedVolume(dataDir))
81
+ console.log(`OK: Data directory ${dataDir} is on a mounted volume`);
82
+ else
83
+ warn(`Data directory ${dataDir} is not a mount point. In a container, mount one persistent volume there.`);
84
+ const siteFile = join(dataDir, 'site-id');
85
+ if (existsSync(siteFile)) {
86
+ const site = JSON.parse(readFileSync(siteFile, 'utf8'));
87
+ check(site.project === definition.config.name, `site-id ${site.id} belongs to this project (${site.project})`);
88
+ }
89
+ else
90
+ console.log('INFO: site-id is written on the first website start');
91
+ const last = lastSnapshot(dataDir);
92
+ if (!last)
93
+ warn('No database snapshot yet: the running website writes backups/latest.sqlite hourly.');
94
+ else if (Date.now() - last.getTime() > 2 * SNAPSHOT_INTERVAL)
95
+ warn(`Latest snapshot is from ${last.toISOString()}; the running website writes one hourly.`);
96
+ else
97
+ console.log(`OK: Latest snapshot ${last.toISOString()}`);
55
98
  }
56
- finally {
57
- cms.close();
99
+ const mediaDir = resolve(env.SANDCMS_MEDIA_DIR?.trim() || join(dataDir, 'media'));
100
+ const existing = existsSync(mediaDir) ? mediaDir : dirname(mediaDir);
101
+ check(existsSync(existing) && writable(existing), `Media directory ${mediaDir} is writable`);
102
+ if (existsSync(existing)) {
103
+ const fs = statfsSync(existing), free = fs.bavail * fs.bsize;
104
+ const used = walk(mediaDir).reduce((total, file) => total + statSync(file).size, 0);
105
+ const quota = definition.config.media.maxStorage;
106
+ console.log(`Media: ${size(used)} used${quota ? ` of a ${size(quota)} quota` : ''}; ${size(free)} free on the volume`);
107
+ if (free < 5 * GB)
108
+ warn('Less than 5 GB free: uploads are refused when free space runs low.');
109
+ if (quota && used > quota * 0.9)
110
+ warn('Media uses more than 90% of media.maxStorage.');
58
111
  }
112
+ if (issues.length)
113
+ throw new Error(`Doctor found ${issues.length} issue(s). Resolve the FAIL items above.`);
59
114
  }
@@ -7,12 +7,8 @@ export interface OwnedFile {
7
7
  export interface Manifest {
8
8
  version: string;
9
9
  files: Record<string, OwnedFile>;
10
- cloudflare?: {
11
- accountId: string;
12
- worker: string;
13
- bucket: string;
14
- phase: 'bucket-created' | 'ready';
15
- };
10
+ /** Written by setup before 0.7.0 (Cloudflare Worker resources). Preserved, no longer used. */
11
+ cloudflare?: unknown;
16
12
  }
17
13
  export interface PlannedFile {
18
14
  path: string;
package/dist/cli/files.js CHANGED
@@ -27,7 +27,7 @@ export function walk(dir) {
27
27
  }
28
28
  export function planFiles(root, name = 'My website') {
29
29
  const templates = {
30
- 'railpack.json': 'railpack.json',
30
+ Dockerfile: 'Dockerfile',
31
31
  '.dockerignore': 'dockerignore',
32
32
  'src/routes/(sandcms)/admin/[...path]/+page.server.ts': 'routes/admin/+page.server.ts',
33
33
  'src/routes/(sandcms)/admin/[...path]/+page.svelte': 'routes/admin/+page.svelte',
@@ -36,16 +36,25 @@ export function planFiles(root, name = 'My website') {
36
36
  'src/hooks.server.ts': 'hooks.server.ts',
37
37
  'src/env.ts': 'env.ts'
38
38
  };
39
+ const manifest = readManifest(root);
40
+ // Railpack stays supported for sites that already build with it; new sites build from the Dockerfile.
41
+ // A Railpack site gets no new Dockerfile (hosts that detect one switch builders); one it already has
42
+ // is managed as usual.
43
+ if (existsSync(join(root, 'railpack.json'))) {
44
+ templates['railpack.json'] = 'railpack.json';
45
+ if (!manifest.files.Dockerfile && !existsSync(join(root, 'Dockerfile')))
46
+ delete templates.Dockerfile;
47
+ }
39
48
  // Both templates are also valid JavaScript; keep a JavaScript project's existing file.
40
49
  for (const file of ['src/hooks.server', 'src/env'])
41
50
  if (existsSync(join(root, file + '.js')) && !existsSync(join(root, file + '.ts'))) {
42
51
  templates[file + '.js'] = templates[file + '.ts'];
43
52
  delete templates[file + '.ts'];
44
53
  }
54
+ // From 0.7.0 there are no sandcms/worker/* templates; existing project copies are left alone.
45
55
  for (const file of walk(join(templateRoot, 'sandcms')))
46
56
  templates['sandcms/' + relative(join(templateRoot, 'sandcms'), file)] =
47
57
  'sandcms/' + relative(join(templateRoot, 'sandcms'), file);
48
- const manifest = readManifest(root);
49
58
  return Object.entries(templates).map(([path, template]) => {
50
59
  let content = readFileSync(join(templateRoot, template), 'utf8');
51
60
  if (path === 'sandcms/config.yaml')
package/dist/cli/index.js CHANGED
@@ -11,8 +11,9 @@ import { createUser, recoverUser } from '../server/auth.js';
11
11
  import { user } from '../server/db/schema.js';
12
12
  import { loadDefinition } from '../blueprints/load.js';
13
13
  import { text, password, confirm } from './prompts.js';
14
- import { updateWorker } from './cloudflare.js';
15
14
  import { doctor } from './doctor.js';
15
+ import { importFromR2 } from '../server/media/import-r2.js';
16
+ import { forceRebuild, verifyMedia } from '../server/media/verify.js';
16
17
  const { values, positionals } = parseArgs({
17
18
  args: process.argv.slice(2),
18
19
  allowPositionals: true,
@@ -25,10 +26,14 @@ const { values, positionals } = parseArgs({
25
26
  output: { type: 'string' },
26
27
  from: { type: 'string' },
27
28
  package: { type: 'string' },
29
+ 'worker-url': { type: 'string' },
28
30
  check: { type: 'boolean' },
29
31
  'dry-run': { type: 'boolean' },
30
32
  'skip-install': { type: 'boolean' },
31
33
  'password-stdin': { type: 'boolean' },
34
+ 'worker-token-stdin': { type: 'boolean' },
35
+ force: { type: 'boolean' },
36
+ repair: { type: 'boolean' },
32
37
  yes: { type: 'boolean' },
33
38
  help: { type: 'boolean' }
34
39
  }
@@ -44,7 +49,7 @@ async function suppliedPassword() {
44
49
  async function main() {
45
50
  const [command, subcommand] = positionals;
46
51
  if (values.help) {
47
- console.log('sandcms setup | validate | doctor | upgrade [--check] | migrate [--check] | worker update | media rebuild | backup --output PATH | restore --from PATH | user bootstrap/add/reset-password/reset-2fa\nUse --database PATH to select the target database. Secrets are never accepted as arguments.');
52
+ console.log('sandcms setup | validate | doctor | upgrade [--check] | migrate [--check] | media rebuild [--force] | media verify [--repair] | media import-r2 [--worker-url URL] [--worker-token-stdin] | backup --output PATH | restore --from PATH | user bootstrap/add/reset-password/reset-2fa\nUse --database PATH to select the target database. Secrets are never accepted as arguments.');
48
53
  return;
49
54
  }
50
55
  if (!command) {
@@ -75,8 +80,6 @@ async function main() {
75
80
  });
76
81
  if (command === 'upgrade')
77
82
  return upgrade(root, values.check, database);
78
- if (command === 'worker' && subcommand === 'update')
79
- return updateWorker(root);
80
83
  if (command === 'backup') {
81
84
  if (!values.output)
82
85
  throw new Error('Use --output PATH');
@@ -93,33 +96,8 @@ async function main() {
93
96
  }
94
97
  if (command === 'doctor')
95
98
  return doctor(root, database);
96
- if (command === 'media' && subcommand === 'rebuild') {
97
- const cms = createCMS({
98
- database,
99
- root: join(root, 'sandcms'),
100
- origin: process.env.SANDCMS_ORIGIN ?? 'http://localhost:5410',
101
- secret: process.env.SANDCMS_SECRET ?? '',
102
- workerToken: process.env.SANDCMS_WORKER_TOKEN,
103
- workerUrl: process.env.SANDCMS_WORKER_URL
104
- });
105
- try {
106
- cms.media.rebuild();
107
- while (cms.media.list().some((a) => a.status === 'processing')) {
108
- await cms.media.drain();
109
- await Bun.sleep(1000);
110
- console.log(cms.media
111
- .list()
112
- .map((a) => `${a.filename}: ${a.status}`)
113
- .join('\n'));
114
- }
115
- if (cms.media.list().some((a) => a.status === 'failed'))
116
- throw new Error('Some images failed; retry with media rebuild');
117
- }
118
- finally {
119
- cms.close();
120
- }
121
- return;
122
- }
99
+ if (command === 'media')
100
+ return mediaCommand(subcommand);
123
101
  const store = openStore(database);
124
102
  try {
125
103
  if (command === 'migrate') {
@@ -176,6 +154,96 @@ async function main() {
176
154
  store.close();
177
155
  }
178
156
  }
157
+ // CLI media commands never call media.start(): no background tasks, schedulers or snapshots.
158
+ function openCMS() {
159
+ const mediaDir = process.env.SANDCMS_MEDIA_DIR;
160
+ return createCMS({
161
+ database,
162
+ root: join(root, 'sandcms'),
163
+ origin: process.env.SANDCMS_ORIGIN ?? 'http://localhost:5410',
164
+ secret: process.env.SANDCMS_SECRET ?? '',
165
+ ...(mediaDir ? { mediaDir: resolve(root, mediaDir) } : {})
166
+ });
167
+ }
168
+ async function mediaCommand(subcommand) {
169
+ if (subcommand === 'import-r2') {
170
+ const token = values['worker-token-stdin']
171
+ ? (await Bun.stdin.text()).trim()
172
+ : (process.env.SANDCMS_WORKER_TOKEN ?? '');
173
+ const cms = openCMS();
174
+ try {
175
+ const summary = await importFromR2(cms, {
176
+ workerUrl: values['worker-url'] ??
177
+ process.env.SANDCMS_WORKER_URL ??
178
+ cms.definition.config.media.workerUrl ??
179
+ '',
180
+ workerToken: token
181
+ });
182
+ for (const s of summary.skipped)
183
+ console.log(`Skipped ${s.id}: ${s.reason}`);
184
+ for (const f of summary.failures)
185
+ console.error(`Failed ${f.id}${f.key ? ` ${f.key}` : ''}: ${f.error}`);
186
+ console.log(`Assets: ${summary.assets}. Originals: ${summary.originals.imported} imported, ${summary.originals.present} already present. ` +
187
+ `Variants: ${summary.variants.imported} imported, ${summary.variants.present} already present. ` +
188
+ `Dimensions filled: ${summary.dimensions}. Verified ${summary.verified.originals} originals and ${summary.verified.variants} variants.`);
189
+ if (!summary.ok)
190
+ throw new Error(`${summary.failures.length} files failed. Fix the cause and run media import-r2 again; finished files are kept.`);
191
+ console.log('Import complete. Check the site, then remove the Worker settings.');
192
+ }
193
+ finally {
194
+ cms.close();
195
+ }
196
+ return;
197
+ }
198
+ if (subcommand === 'verify') {
199
+ const cms = openCMS();
200
+ try {
201
+ const report = verifyMedia(cms, { repair: values.repair });
202
+ for (const id of report.missingOriginals)
203
+ console.error(`Original missing: ${id}`);
204
+ for (const v of report.missingVariants)
205
+ console.error(`Variant missing: ${v.key}`);
206
+ for (const v of report.unrepairable)
207
+ console.error(`No job to re-render ${v.key}; run media rebuild --force or upload again`);
208
+ console.log(`Checked ${report.checked} assets: ${report.missingOriginals.length} missing originals, ${report.missingVariants.length} missing variants.` +
209
+ (values.repair
210
+ ? ` Re-rendering ${report.requeued.length} assets; marked ${report.failed.length} failed. The website renders them, or run media rebuild.`
211
+ : ''));
212
+ if (!values.repair && (report.missingOriginals.length || report.missingVariants.length))
213
+ throw new Error('Media files are missing; run media verify --repair');
214
+ }
215
+ finally {
216
+ cms.close();
217
+ }
218
+ return;
219
+ }
220
+ if (subcommand !== 'rebuild')
221
+ throw new Error('Unknown command. Run sandcms --help');
222
+ const cms = openCMS();
223
+ try {
224
+ if (values.force)
225
+ console.log(`Re-rendering ${forceRebuild(cms)} assets with the local processor.`);
226
+ cms.media.rebuild();
227
+ const counts = () => {
228
+ const assets = cms.media.list();
229
+ return {
230
+ processing: assets.filter((a) => a.status === 'processing').length,
231
+ failed: assets.filter((a) => a.status === 'failed').length
232
+ };
233
+ };
234
+ while (counts().processing) {
235
+ await cms.media.drain();
236
+ await Bun.sleep(1000);
237
+ const { processing, failed } = counts();
238
+ console.log(`Processing: ${processing}. Failed: ${failed}.`);
239
+ }
240
+ if (counts().failed)
241
+ throw new Error('Some images failed; retry with media rebuild');
242
+ }
243
+ finally {
244
+ cms.close();
245
+ }
246
+ }
179
247
  main().catch((e) => {
180
248
  console.error(e instanceof Error ? e.message : e);
181
249
  process.exitCode = 1;