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.
- package/README.md +14 -10
- package/dist/admin/Account.svelte +2 -0
- package/dist/admin/Admin.svelte +14 -1
- package/dist/admin/AssetAttachment.svelte +1 -3
- package/dist/admin/AssetAttachment.svelte.d.ts +0 -1
- package/dist/admin/BlueprintFields.svelte +0 -3
- package/dist/admin/BlueprintFields.svelte.d.ts +0 -1
- package/dist/admin/Editor.svelte +0 -1
- package/dist/admin/Fields.svelte +0 -7
- package/dist/admin/Fields.svelte.d.ts +0 -1
- package/dist/admin/Gallery.svelte +2 -6
- package/dist/admin/Gallery.svelte.d.ts +0 -1
- package/dist/admin/LinkField.svelte +0 -3
- package/dist/admin/LinkField.svelte.d.ts +0 -1
- package/dist/admin/MediaLibrary.svelte +10 -7
- package/dist/admin/MediaPicker.svelte +7 -15
- package/dist/admin/MediaPicker.svelte.d.ts +0 -1
- package/dist/admin/ThemePicker.svelte +72 -0
- package/dist/admin/ThemePicker.svelte.d.ts +18 -0
- package/dist/admin/UploadButton.svelte +2 -1
- package/dist/admin/UploadProgress.svelte +24 -0
- package/dist/admin/UploadProgress.svelte.d.ts +7 -0
- package/dist/admin/client.d.ts +3 -670
- package/dist/admin/client.js +2 -1
- package/dist/admin/components/ui/alert/alert.svelte.d.ts +3 -13
- package/dist/admin/components/ui/attachment/attachment-media.svelte.d.ts +3 -13
- package/dist/admin/components/ui/attachment/attachment.svelte.d.ts +6 -26
- package/dist/admin/components/ui/badge/badge.svelte.d.ts +7 -25
- package/dist/admin/components/ui/button/button.svelte.d.ts +15 -53
- package/dist/admin/components/ui/toggle/toggle.svelte.d.ts +6 -26
- package/dist/admin/generated/admin.css +529 -101
- package/dist/admin/theme-context.d.ts +7 -0
- package/dist/admin/theme-context.js +3 -0
- package/dist/admin/theme.d.ts +7 -0
- package/dist/admin/theme.js +14 -0
- package/dist/admin/types.d.ts +2 -1
- package/dist/admin/uploads.d.ts +8 -1
- package/dist/admin/uploads.js +94 -16
- package/dist/blueprints/files.js +1 -0
- package/dist/blueprints/load.js +7 -2
- package/dist/blueprints/types.d.ts +4 -1
- package/dist/cli/doctor.d.ts +3 -1
- package/dist/cli/doctor.js +85 -30
- package/dist/cli/files.d.ts +2 -6
- package/dist/cli/files.js +11 -2
- package/dist/cli/index.js +99 -31
- package/dist/cli/scaffold.js +8 -8
- package/dist/cli/setup.js +16 -26
- package/dist/cli/upgrade.js +21 -2
- package/dist/env.d.ts +32 -0
- package/dist/env.js +53 -3
- package/dist/server/admin-load.d.ts +13 -5
- package/dist/server/admin-load.js +6 -1
- package/dist/server/api-access.d.ts +4 -0
- package/dist/server/api-access.js +15 -2
- package/dist/server/api-pagination.d.ts +3 -0
- package/dist/server/api.js +5 -2
- package/dist/server/auth.d.ts +10 -9
- package/dist/server/cms.d.ts +65 -18
- package/dist/server/cms.js +62 -6
- package/dist/server/db/migration-data.js +4 -0
- package/dist/server/db/migrations/0009_local_media.sql +9 -0
- package/dist/server/db/migrations/meta/0009_snapshot.json +1763 -0
- package/dist/server/db/migrations/meta/_journal.json +7 -0
- package/dist/server/db/schema.d.ts +119 -2
- package/dist/server/db/schema.js +15 -3
- package/dist/server/hooks.js +155 -81
- package/dist/server/media/contracts.d.ts +115 -0
- package/dist/server/media/contracts.js +12 -0
- package/dist/server/media/deletions.d.ts +2 -1
- package/dist/server/media/deletions.js +3 -3
- package/dist/server/media/delivery.js +7 -11
- package/dist/server/media/import-r2.d.ts +56 -0
- package/dist/server/media/import-r2.js +342 -0
- package/dist/server/media/processor-child.d.ts +2 -0
- package/dist/server/media/processor-child.js +236 -0
- package/dist/server/media/processor.d.ts +51 -0
- package/dist/server/media/processor.js +135 -0
- package/dist/server/media/render-math.d.ts +24 -0
- package/dist/server/media/render-math.js +50 -0
- package/dist/server/media/runner.d.ts +30 -1
- package/dist/server/media/runner.js +418 -165
- package/dist/server/media/service.d.ts +56 -10
- package/dist/server/media/service.js +309 -98
- package/dist/server/media/storage.d.ts +5 -0
- package/dist/server/media/storage.js +279 -0
- package/dist/server/media/svg.d.ts +5 -0
- package/dist/server/media/svg.js +142 -0
- package/dist/server/media/upload-ticket.d.ts +12 -0
- package/dist/server/media/upload-ticket.js +35 -0
- package/dist/server/media/verify.d.ts +36 -0
- package/dist/server/media/verify.js +109 -0
- package/dist/server/platform/http.d.ts +27 -0
- package/dist/server/platform/http.js +77 -0
- package/dist/server/platform/index.d.ts +52 -0
- package/dist/server/platform/index.js +98 -0
- package/dist/server/platform/snapshots.d.ts +18 -0
- package/dist/server/platform/snapshots.js +110 -0
- package/dist/server/platform/volume.d.ts +20 -0
- package/dist/server/platform/volume.js +60 -0
- package/dist/worker/svg.d.ts +1 -5
- package/dist/worker/svg.js +2 -142
- package/docs/acceptance.md +6 -4
- package/docs/api.md +41 -3
- package/docs/cloudflare.md +48 -142
- package/docs/content.md +2 -2
- package/docs/deployment.md +159 -57
- package/docs/forms.md +1 -1
- package/docs/media.md +85 -21
- package/docs/package.md +21 -18
- package/docs/plans/0.7-self-hosted.md +264 -0
- package/docs/releases.md +17 -1
- package/docs/upgrades.md +35 -6
- package/package.json +36 -37
- package/templates/Dockerfile +28 -0
- package/templates/integration.ts +18 -2
- package/templates/railpack.json +1 -1
- package/templates/sandcms/blueprints/files/image.yaml +1 -1
- package/templates/sandcms/config.yaml +0 -1
- package/dist/cli/cloudflare.d.ts +0 -8
- package/dist/cli/cloudflare.js +0 -135
- package/templates/sandcms/worker/index.ts +0 -1
- package/templates/sandcms/worker/local.ts +0 -1
- package/templates/sandcms/worker/wrangler.jsonc +0 -6
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type InspectResult, type MediaProcessor, type RenderReport, type VariantSpec } from './contracts.js';
|
|
2
|
+
export interface RenderInput {
|
|
3
|
+
id: string;
|
|
4
|
+
version: string;
|
|
5
|
+
sourcePath: string;
|
|
6
|
+
mime: string;
|
|
7
|
+
maxPixels: number;
|
|
8
|
+
workDir: string;
|
|
9
|
+
specs: VariantSpec[];
|
|
10
|
+
}
|
|
11
|
+
export type ParentMessage = {
|
|
12
|
+
type: 'inspect';
|
|
13
|
+
id: number;
|
|
14
|
+
path: string;
|
|
15
|
+
mime: string;
|
|
16
|
+
maxPixels: number;
|
|
17
|
+
} | {
|
|
18
|
+
type: 'render';
|
|
19
|
+
id: number;
|
|
20
|
+
input: RenderInput;
|
|
21
|
+
} | {
|
|
22
|
+
type: 'continue';
|
|
23
|
+
id: number;
|
|
24
|
+
};
|
|
25
|
+
export type ChildMessage = {
|
|
26
|
+
type: 'ready';
|
|
27
|
+
} | {
|
|
28
|
+
type: 'report';
|
|
29
|
+
id: number;
|
|
30
|
+
report: RenderReport;
|
|
31
|
+
} | {
|
|
32
|
+
type: 'result';
|
|
33
|
+
id: number;
|
|
34
|
+
value: InspectResult | null;
|
|
35
|
+
} | {
|
|
36
|
+
type: 'error';
|
|
37
|
+
id: number;
|
|
38
|
+
status: number;
|
|
39
|
+
message: string;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Child entry lookup: explicit option, env, a sibling with this module's extension (source tree or unbundled
|
|
43
|
+
* dist), the `sandcms/media-processor` package export (when this module is bundled into an app), then any sibling.
|
|
44
|
+
*/
|
|
45
|
+
export declare function resolveProcessorEntry(entry?: string): string;
|
|
46
|
+
/** The child sees only what sharp and Bun need; secrets (SANDCMS_SECRET, SMTP, API tokens) stay in the parent. */
|
|
47
|
+
export declare function childEnv(source?: Record<string, string | undefined>): Record<string, string>;
|
|
48
|
+
export declare function createProcessor(options?: {
|
|
49
|
+
entry?: string;
|
|
50
|
+
timeoutMs?: number;
|
|
51
|
+
}): MediaProcessor;
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// Parent side of the media processor: one sharp child process per inspect/render call (see processor-child.ts).
|
|
2
|
+
// The child loads sharp at runtime, so consumers must keep `sharp` external in their Vite SSR build
|
|
3
|
+
// (`ssr.external: ['sharp']`); the child entry itself is resolved from disk, never bundled.
|
|
4
|
+
import { existsSync } from 'node:fs';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import { MediaRequestError } from './contracts.js';
|
|
7
|
+
const RENDER_TIMEOUT = 10 * 60_000;
|
|
8
|
+
const INSPECT_TIMEOUT = 2 * 60_000;
|
|
9
|
+
const fileOf = (url) => {
|
|
10
|
+
try {
|
|
11
|
+
const path = fileURLToPath(url);
|
|
12
|
+
return existsSync(path) ? path : undefined;
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Child entry lookup: explicit option, env, a sibling with this module's extension (source tree or unbundled
|
|
20
|
+
* dist), the `sandcms/media-processor` package export (when this module is bundled into an app), then any sibling.
|
|
21
|
+
*/
|
|
22
|
+
export function resolveProcessorEntry(entry) {
|
|
23
|
+
if (entry)
|
|
24
|
+
return entry;
|
|
25
|
+
if (process.env.SANDCMS_PROCESSOR_ENTRY)
|
|
26
|
+
return process.env.SANDCMS_PROCESSOR_ENTRY;
|
|
27
|
+
const ext = import.meta.url.endsWith('.ts') ? '.ts' : '.js';
|
|
28
|
+
const own = fileOf(new URL(`./processor-child${ext}`, import.meta.url));
|
|
29
|
+
if (own)
|
|
30
|
+
return own;
|
|
31
|
+
let exported;
|
|
32
|
+
try {
|
|
33
|
+
exported = fileOf(import.meta.resolve('sandcms/media-processor'));
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
// Not installed as a package (or export missing).
|
|
37
|
+
}
|
|
38
|
+
const found = exported ??
|
|
39
|
+
fileOf(new URL('./processor-child.js', import.meta.url)) ??
|
|
40
|
+
fileOf(new URL('./processor-child.ts', import.meta.url));
|
|
41
|
+
if (!found)
|
|
42
|
+
throw new Error('Media processor entry not found; set SANDCMS_PROCESSOR_ENTRY to processor-child.js');
|
|
43
|
+
return found;
|
|
44
|
+
}
|
|
45
|
+
/** The child sees only what sharp and Bun need; secrets (SANDCMS_SECRET, SMTP, API tokens) stay in the parent. */
|
|
46
|
+
export function childEnv(source = process.env) {
|
|
47
|
+
const env = {};
|
|
48
|
+
for (const [name, value] of Object.entries(source))
|
|
49
|
+
if (value !== undefined &&
|
|
50
|
+
(['PATH', 'HOME', 'TMPDIR', 'TMP', 'TEMP', 'LANG', 'LC_ALL', 'TZ', 'NODE_ENV'].includes(name) ||
|
|
51
|
+
[
|
|
52
|
+
'SANDCMS_PROCESSOR_TRACE',
|
|
53
|
+
'SANDCMS_MEDIA_THREADS',
|
|
54
|
+
'LD_LIBRARY_PATH',
|
|
55
|
+
'DYLD_LIBRARY_PATH'
|
|
56
|
+
].includes(name) ||
|
|
57
|
+
/^(MALLOC_|VIPS_|SHARP_|UV_THREADPOOL_SIZE$)/.test(name)))
|
|
58
|
+
env[name] = value;
|
|
59
|
+
return env;
|
|
60
|
+
}
|
|
61
|
+
export function createProcessor(options = {}) {
|
|
62
|
+
const children = new Set();
|
|
63
|
+
let entry;
|
|
64
|
+
function run(request, timeoutMs, onReport) {
|
|
65
|
+
entry ??= resolveProcessorEntry(options.entry);
|
|
66
|
+
return new Promise((resolve, reject) => {
|
|
67
|
+
let settled = false;
|
|
68
|
+
let timer;
|
|
69
|
+
const arm = () => {
|
|
70
|
+
clearTimeout(timer);
|
|
71
|
+
timer = setTimeout(() => finish(new Error(`Media processor timed out after ${timeoutMs} ms`)), timeoutMs);
|
|
72
|
+
};
|
|
73
|
+
const finish = (error, value) => {
|
|
74
|
+
if (settled)
|
|
75
|
+
return;
|
|
76
|
+
settled = true;
|
|
77
|
+
clearTimeout(timer);
|
|
78
|
+
children.delete(child);
|
|
79
|
+
child.kill('SIGKILL');
|
|
80
|
+
if (error)
|
|
81
|
+
reject(error);
|
|
82
|
+
else
|
|
83
|
+
resolve(value);
|
|
84
|
+
};
|
|
85
|
+
const child = Bun.spawn([process.execPath, entry], {
|
|
86
|
+
ipc(raw) {
|
|
87
|
+
const message = raw;
|
|
88
|
+
if (settled)
|
|
89
|
+
return;
|
|
90
|
+
if (message.type === 'ready')
|
|
91
|
+
return void child.send(request);
|
|
92
|
+
if (message.type === 'result')
|
|
93
|
+
return finish(null, message.value);
|
|
94
|
+
if (message.type === 'error')
|
|
95
|
+
return finish(request.type === 'inspect'
|
|
96
|
+
? new MediaRequestError(message.status, message.message)
|
|
97
|
+
: new Error(message.message));
|
|
98
|
+
if (message.type === 'report') {
|
|
99
|
+
clearTimeout(timer);
|
|
100
|
+
Promise.resolve()
|
|
101
|
+
.then(() => onReport?.(message.report))
|
|
102
|
+
.then(() => {
|
|
103
|
+
if (settled)
|
|
104
|
+
return;
|
|
105
|
+
arm();
|
|
106
|
+
child.send({ type: 'continue', id: message.id });
|
|
107
|
+
}, (error) => finish(error instanceof Error ? error : new Error(String(error))));
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
serialization: 'json',
|
|
111
|
+
env: childEnv(),
|
|
112
|
+
stdin: 'ignore',
|
|
113
|
+
stdout: 'inherit',
|
|
114
|
+
stderr: 'inherit'
|
|
115
|
+
});
|
|
116
|
+
children.add(child);
|
|
117
|
+
arm();
|
|
118
|
+
void child.exited.then((code) => finish(new Error(`Media processor exited unexpectedly (${child.signalCode ?? `code ${code}`})`)));
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
let next = 0;
|
|
122
|
+
return {
|
|
123
|
+
inspect(path, mime, maxPixels) {
|
|
124
|
+
return run({ type: 'inspect', id: ++next, path, mime, maxPixels }, options.timeoutMs ?? INSPECT_TIMEOUT);
|
|
125
|
+
},
|
|
126
|
+
render(input, onReport) {
|
|
127
|
+
return run({ type: 'render', id: ++next, input }, options.timeoutMs ?? RENDER_TIMEOUT, onReport).then(() => undefined);
|
|
128
|
+
},
|
|
129
|
+
close() {
|
|
130
|
+
for (const child of children)
|
|
131
|
+
child.kill('SIGKILL');
|
|
132
|
+
children.clear();
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { VariantSpec } from './contracts.js';
|
|
2
|
+
export type SizeSpec = Pick<VariantSpec, 'size' | 'aspectRatio'>;
|
|
3
|
+
export type PlanSpec = Pick<VariantSpec, 'size' | 'aspectRatio' | 'focus'>;
|
|
4
|
+
export interface Box {
|
|
5
|
+
width: number;
|
|
6
|
+
height: number;
|
|
7
|
+
}
|
|
8
|
+
export interface RenderPlan extends Box {
|
|
9
|
+
/** Scale the source to exactly this size (fit: fill) before cropping. Equals the output size without a crop. */
|
|
10
|
+
resize: Box;
|
|
11
|
+
/** Region of the resized image to keep, present only for aspect-ratio presets. */
|
|
12
|
+
crop?: {
|
|
13
|
+
left: number;
|
|
14
|
+
top: number;
|
|
15
|
+
width: number;
|
|
16
|
+
height: number;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/** Output dimensions, identical to the Worker: scale-down to `size` on the long edge, or a cover crop for aspect presets. */
|
|
20
|
+
export declare function variantSize(width: number, height: number, spec: SizeSpec): Box;
|
|
21
|
+
/** Resize + optional crop that produces `variantSize`, keeping the focus point (default centre) as central as possible. */
|
|
22
|
+
export declare function renderPlan(width: number, height: number, spec: PlanSpec): RenderPlan;
|
|
23
|
+
/** Smallest intermediate every spec can be derived from without upscaling. */
|
|
24
|
+
export declare function masterSize(width: number, height: number, specs: PlanSpec[]): Box;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/** Output dimensions, identical to the Worker: scale-down to `size` on the long edge, or a cover crop for aspect presets. */
|
|
2
|
+
export function variantSize(width, height, spec) {
|
|
3
|
+
if (spec.aspectRatio) {
|
|
4
|
+
const targetWidth = Math.max(1, Math.round(spec.size * Math.min(1, spec.aspectRatio)));
|
|
5
|
+
const targetHeight = Math.max(1, Math.round(spec.size / Math.max(1, spec.aspectRatio)));
|
|
6
|
+
const cropScale = Math.min(1, width / targetWidth, height / targetHeight);
|
|
7
|
+
return {
|
|
8
|
+
width: Math.max(1, Math.round(targetWidth * cropScale)),
|
|
9
|
+
height: Math.max(1, Math.round(targetHeight * cropScale))
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
const scale = Math.min(1, spec.size / Math.max(width, height));
|
|
13
|
+
return {
|
|
14
|
+
width: Math.max(1, Math.round(width * scale)),
|
|
15
|
+
height: Math.max(1, Math.round(height * scale))
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
const clamp = (value, min, max) => Math.min(max, Math.max(min, value));
|
|
19
|
+
/** Resize + optional crop that produces `variantSize`, keeping the focus point (default centre) as central as possible. */
|
|
20
|
+
export function renderPlan(width, height, spec) {
|
|
21
|
+
const out = variantSize(width, height, spec);
|
|
22
|
+
if (!spec.aspectRatio)
|
|
23
|
+
return { ...out, resize: out };
|
|
24
|
+
const scale = Math.min(1, Math.max(out.width / width, out.height / height));
|
|
25
|
+
const resize = {
|
|
26
|
+
width: clamp(Math.round(width * scale), out.width, width),
|
|
27
|
+
height: clamp(Math.round(height * scale), out.height, height)
|
|
28
|
+
};
|
|
29
|
+
const focus = spec.focus ?? { x: 0.5, y: 0.5 };
|
|
30
|
+
return {
|
|
31
|
+
...out,
|
|
32
|
+
resize,
|
|
33
|
+
crop: {
|
|
34
|
+
left: clamp(Math.round(focus.x * resize.width - out.width / 2), 0, resize.width - out.width),
|
|
35
|
+
top: clamp(Math.round(focus.y * resize.height - out.height / 2), 0, resize.height - out.height),
|
|
36
|
+
width: out.width,
|
|
37
|
+
height: out.height
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/** Smallest intermediate every spec can be derived from without upscaling. */
|
|
42
|
+
export function masterSize(width, height, specs) {
|
|
43
|
+
let w = 1, h = 1;
|
|
44
|
+
for (const spec of specs) {
|
|
45
|
+
const { resize } = renderPlan(width, height, spec);
|
|
46
|
+
w = Math.max(w, resize.width);
|
|
47
|
+
h = Math.max(h, resize.height);
|
|
48
|
+
}
|
|
49
|
+
return { width: Math.min(width, w), height: Math.min(height, h) };
|
|
50
|
+
}
|
|
@@ -1,2 +1,31 @@
|
|
|
1
1
|
import type { Store } from '../db/store.js';
|
|
2
|
-
|
|
2
|
+
import { type MediaProcessor, type MediaStorage, type VariantSpec } from './contracts.js';
|
|
3
|
+
/**
|
|
4
|
+
* Upload sessions that can still write or validate files: they reserve disk space and block an R2 import.
|
|
5
|
+
* Expired, idle-past-expiry and permanently failed sessions are dead rows kept only for their error.
|
|
6
|
+
*/
|
|
7
|
+
export declare const liveUploadSession: (now?: number) => import("drizzle-orm").SQL<unknown> | undefined;
|
|
8
|
+
/** Legacy BLOB uploads the runner will still try to validate. */
|
|
9
|
+
export declare const liveLegacyUpload: () => import("drizzle-orm").SQL<unknown>;
|
|
10
|
+
/** Take or renew a named exclusive lease. Returns false while another owner holds it. */
|
|
11
|
+
export declare function takeLease(store: Store, name: string, owner: string, ttl: number): boolean;
|
|
12
|
+
export declare function releaseLease(store: Store, name: string, owner: string): void;
|
|
13
|
+
/** Serializes processor use within one process: one decode at a time, uploads and renders alike. */
|
|
14
|
+
export declare function processorQueue(load: () => Promise<MediaProcessor>): {
|
|
15
|
+
readonly busy: boolean;
|
|
16
|
+
run<T>(work: (processor: MediaProcessor) => Promise<T>): Promise<T>;
|
|
17
|
+
};
|
|
18
|
+
export declare const variantKey: (id: string, version: string, spec: VariantSpec) => string;
|
|
19
|
+
export declare function createRunner(store: Store, deps: {
|
|
20
|
+
storage: MediaStorage;
|
|
21
|
+
processor: ReturnType<typeof processorQueue>;
|
|
22
|
+
maxPixels: number;
|
|
23
|
+
owner: string;
|
|
24
|
+
enqueue: (id: string, mime: string) => void;
|
|
25
|
+
deletePending: () => Promise<boolean>;
|
|
26
|
+
/** Lease renewal interval while waiting on the processor (tests shorten it). */
|
|
27
|
+
heartbeat?: number;
|
|
28
|
+
}): {
|
|
29
|
+
maintain: () => Promise<boolean>;
|
|
30
|
+
render: () => Promise<boolean>;
|
|
31
|
+
};
|