sandcms 0.5.3 → 0.6.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.
Files changed (147) hide show
  1. package/README.md +5 -3
  2. package/dist/admin/Account.svelte +4 -1
  3. package/dist/admin/ApiKeys.svelte +159 -0
  4. package/dist/admin/ApiKeys.svelte.d.ts +3 -0
  5. package/dist/admin/AssetAttachment.svelte +6 -4
  6. package/dist/admin/BlueprintFields.svelte +86 -0
  7. package/dist/admin/BlueprintFields.svelte.d.ts +15 -0
  8. package/dist/admin/Collection.svelte +40 -7
  9. package/dist/admin/Editor.svelte +57 -17
  10. package/dist/admin/Fields.svelte +217 -144
  11. package/dist/admin/Fields.svelte.d.ts +1 -0
  12. package/dist/admin/FocusPicker.svelte +136 -0
  13. package/dist/admin/FocusPicker.svelte.d.ts +11 -0
  14. package/dist/admin/Gallery.svelte +40 -34
  15. package/dist/admin/GlobalSearch.svelte +56 -0
  16. package/dist/admin/GlobalSearch.svelte.d.ts +3 -0
  17. package/dist/admin/Inbox.svelte +8 -5
  18. package/dist/admin/LinkField.svelte +116 -0
  19. package/dist/admin/LinkField.svelte.d.ts +17 -0
  20. package/dist/admin/MediaLibrary.svelte +41 -14
  21. package/dist/admin/MediaPicker.svelte +12 -2
  22. package/dist/admin/NotificationSettings.svelte +2 -2
  23. package/dist/admin/SearchList.svelte +55 -0
  24. package/dist/admin/SearchList.svelte.d.ts +42 -0
  25. package/dist/admin/SelectField.svelte +3 -1
  26. package/dist/admin/SelectField.svelte.d.ts +1 -0
  27. package/dist/admin/Shell.svelte +4 -2
  28. package/dist/admin/UnsavedChanges.svelte +1 -1
  29. package/dist/admin/Users.svelte +11 -5
  30. package/dist/admin/client.js +2 -1
  31. package/dist/admin/generated/admin.css +109 -0
  32. package/dist/admin/types.d.ts +5 -1
  33. package/dist/admin/uploads.js +27 -6
  34. package/dist/blueprints/conditions.d.ts +2 -0
  35. package/dist/blueprints/conditions.js +4 -0
  36. package/dist/blueprints/extends.d.ts +5 -0
  37. package/dist/blueprints/extends.js +76 -0
  38. package/dist/blueprints/files.d.ts +3 -1
  39. package/dist/blueprints/files.js +35 -2
  40. package/dist/blueprints/layout.d.ts +4 -0
  41. package/dist/blueprints/layout.js +12 -0
  42. package/dist/blueprints/load.d.ts +1 -1
  43. package/dist/blueprints/load.js +123 -12
  44. package/dist/blueprints/types.d.ts +35 -1
  45. package/dist/blueprints/validate.js +30 -6
  46. package/dist/cli/cloudflare.js +17 -5
  47. package/dist/cli/doctor.js +6 -3
  48. package/dist/cli/files.js +1 -1
  49. package/dist/cli/scaffold.js +8 -6
  50. package/dist/cli/setup.js +9 -2
  51. package/dist/cli/upgrade.js +2 -1
  52. package/dist/email-failure.d.ts +17 -0
  53. package/dist/email-failure.js +22 -0
  54. package/dist/index.d.ts +2 -1
  55. package/dist/index.js +1 -0
  56. package/dist/link.d.ts +17 -0
  57. package/dist/link.js +44 -0
  58. package/dist/notification-fields.js +1 -1
  59. package/dist/search.d.ts +8 -0
  60. package/dist/search.js +1 -0
  61. package/dist/server/access.d.ts +2 -0
  62. package/dist/server/access.js +59 -23
  63. package/dist/server/admin-load.d.ts +11 -2
  64. package/dist/server/admin-load.js +4 -2
  65. package/dist/server/api-access.d.ts +31 -0
  66. package/dist/server/api-access.js +52 -0
  67. package/dist/server/api-pagination.d.ts +34 -0
  68. package/dist/server/api-pagination.js +87 -0
  69. package/dist/server/api.js +224 -32
  70. package/dist/server/auth.d.ts +1150 -0
  71. package/dist/server/auth.js +11 -1
  72. package/dist/server/cms.d.ts +1210 -4
  73. package/dist/server/cms.js +12 -0
  74. package/dist/server/content.d.ts +17 -2
  75. package/dist/server/content.js +89 -14
  76. package/dist/server/db/auto-migrate.d.ts +12 -0
  77. package/dist/server/db/auto-migrate.js +23 -0
  78. package/dist/server/db/migration-data.js +24 -0
  79. package/dist/server/db/migrations/0001_independent_password_resets.sql +3 -0
  80. package/dist/server/db/migrations/0002_email_failure_reason.sql +1 -0
  81. package/dist/server/db/migrations/0003_direct_r2_uploads.sql +10 -0
  82. package/dist/server/db/migrations/0004_agent_api_keys.sql +38 -0
  83. package/dist/server/db/migrations/0005_api_retention_and_pagination.sql +3 -0
  84. package/dist/server/db/migrations/0006_durable_media_deletion.sql +7 -0
  85. package/dist/server/db/migrations/meta/0001_snapshot.json +1197 -0
  86. package/dist/server/db/migrations/meta/0002_snapshot.json +1204 -0
  87. package/dist/server/db/migrations/meta/0003_snapshot.json +1281 -0
  88. package/dist/server/db/migrations/meta/0004_snapshot.json +1546 -0
  89. package/dist/server/db/migrations/meta/0005_snapshot.json +1572 -0
  90. package/dist/server/db/migrations/meta/0006_snapshot.json +1627 -0
  91. package/dist/server/db/migrations/meta/_journal.json +42 -0
  92. package/dist/server/db/schema.d.ts +742 -2
  93. package/dist/server/db/schema.js +77 -5
  94. package/dist/server/db/store.js +24 -9
  95. package/dist/server/email/index.d.ts +11 -1
  96. package/dist/server/email/index.js +69 -6
  97. package/dist/server/forms.js +12 -2
  98. package/dist/server/hooks.d.ts +1 -1
  99. package/dist/server/hooks.js +37 -14
  100. package/dist/server/index.d.ts +2 -1
  101. package/dist/server/index.js +2 -1
  102. package/dist/server/links.d.ts +5 -0
  103. package/dist/server/links.js +44 -0
  104. package/dist/server/media/deletions.d.ts +2 -0
  105. package/dist/server/media/deletions.js +58 -0
  106. package/dist/server/media/presets.d.ts +10 -2
  107. package/dist/server/media/presets.js +14 -2
  108. package/dist/server/media/runner.d.ts +1 -1
  109. package/dist/server/media/runner.js +105 -8
  110. package/dist/server/media/service.d.ts +41 -3
  111. package/dist/server/media/service.js +146 -33
  112. package/dist/server/notifications.d.ts +1 -0
  113. package/dist/server/notifications.js +4 -2
  114. package/dist/server/request.d.ts +2 -0
  115. package/dist/server/request.js +5 -0
  116. package/dist/server/search.d.ts +13 -0
  117. package/dist/server/search.js +97 -0
  118. package/dist/worker/index.d.ts +4 -4
  119. package/dist/worker/index.js +208 -67
  120. package/dist/worker/local.d.ts +5 -0
  121. package/dist/worker/local.js +57 -0
  122. package/dist/worker/source.d.ts +1 -1
  123. package/dist/worker/source.js +2 -2
  124. package/dist/worker/uploads.d.ts +22 -0
  125. package/dist/worker/uploads.js +48 -0
  126. package/docs/api.md +73 -0
  127. package/docs/cloudflare.md +37 -10
  128. package/docs/content.md +162 -4
  129. package/docs/deployment.md +33 -4
  130. package/docs/email.md +11 -7
  131. package/docs/forms.md +3 -1
  132. package/docs/media.md +33 -4
  133. package/docs/package.md +6 -4
  134. package/docs/releases.md +54 -1
  135. package/docs/upgrades.md +91 -3
  136. package/package.json +19 -10
  137. package/templates/hooks.server.ts +2 -2
  138. package/templates/integration.ts +3 -2
  139. package/templates/routes/admin/+page.server.ts +1 -1
  140. package/templates/routes/api/+server.ts +1 -1
  141. package/templates/sandcms/blueprints/collections/projects.yaml +16 -8
  142. package/templates/sandcms/blueprints/fields/seo.yaml +5 -0
  143. package/templates/sandcms/blueprints/files/image.yaml +1 -0
  144. package/templates/sandcms/blueprints/pages/home.yaml +1 -0
  145. package/templates/sandcms/config.yaml +1 -0
  146. package/templates/sandcms/worker/local.ts +1 -0
  147. package/templates/sandcms/worker/wrangler.jsonc +1 -2
@@ -0,0 +1,97 @@
1
+ import { error } from '@sveltejs/kit';
2
+ import { contentPage, mediaPage } from './api-pagination.js';
3
+ export const normalizeSearch = (value) => value.normalize('NFKD').replace(/\p{M}/gu, '').toLocaleLowerCase('en').replace(/\s+/g, ' ').trim();
4
+ export function searchQuery(params) {
5
+ const query = params.get('q') ?? '';
6
+ if (query.length > 200)
7
+ error(400, 'Search query must be at most 200 characters');
8
+ return normalizeSearch(query);
9
+ }
10
+ function richText(node) {
11
+ return node.type === 'text' ? node.text ?? '' : (node.content ?? []).map(richText).join(' ');
12
+ }
13
+ export function searchableText(fields, payload, relationLabel) {
14
+ const parts = [];
15
+ for (const [key, field] of Object.entries(fields)) {
16
+ const value = payload[key];
17
+ if (value === null || value === undefined)
18
+ continue;
19
+ if (['text', 'slug', 'textarea', 'select'].includes(field.type) && typeof value === 'string')
20
+ parts.push(value);
21
+ else if (field.type === 'multiselect' && Array.isArray(value))
22
+ parts.push(...value.filter((v) => typeof v === 'string'));
23
+ else if (field.type === 'richtext' && typeof value === 'object' && !Array.isArray(value))
24
+ parts.push(richText(value));
25
+ else if (field.type === 'relation' && field.collection && typeof value === 'string' && relationLabel)
26
+ parts.push(relationLabel(field.collection, value));
27
+ else if (field.type === 'group' && typeof value === 'object' && !Array.isArray(value))
28
+ parts.push(searchableText(field.fields ?? {}, value, relationLabel));
29
+ else if (['repeatable', 'gallery'].includes(field.type) && Array.isArray(value))
30
+ for (const row of value)
31
+ if (row && typeof row === 'object' && !Array.isArray(row))
32
+ parts.push(searchableText(field.fields ?? {}, row, relationLabel));
33
+ }
34
+ return parts.join(' ').replace(/\s+/g, ' ').trim();
35
+ }
36
+ export function matchScore(query, title, body) {
37
+ if (!query)
38
+ return 0;
39
+ const heading = normalizeSearch(title), text = normalizeSearch(body);
40
+ if (!query.split(' ').every((term) => (heading + ' ' + text).includes(term)))
41
+ return -1;
42
+ return heading === query ? 3 : heading.includes(query) ? 2 : 1;
43
+ }
44
+ export function documentText(cms, row) {
45
+ const blueprint = cms.definition.blueprints.find((b) => b.id === row.blueprint && b.kind !== 'form');
46
+ return blueprint ? blueprint.label + ' ' + searchableText(blueprint.fields, row.draft) : '';
47
+ }
48
+ export function assetText(cms, row, labels = new Map()) {
49
+ const blueprint = cms.definition.fileBlueprints?.find((b) => b.id === row.blueprint);
50
+ return (blueprint?.label ?? '') + ' ' + searchableText(blueprint?.fields ?? {}, row.metadata, (collection, id) => {
51
+ const key = collection + '\0' + id;
52
+ if (!labels.has(key)) {
53
+ const target = cms.content.get(id);
54
+ labels.set(key, target?.blueprint === collection ? String(target.draft.title ?? target.draft.name ?? '') : '');
55
+ }
56
+ return labels.get(key);
57
+ });
58
+ }
59
+ function excerpt(text, query) {
60
+ const term = query.split(' ')[0].replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
61
+ const position = Math.max(0, text.search(new RegExp(term, 'i')));
62
+ const start = Math.max(0, position - 45);
63
+ return (start ? '…' : '') + text.slice(start, start + 160) + (text.length > start + 160 ? '…' : '');
64
+ }
65
+ const globalLimit = 30;
66
+ export function globalSearch(cms, params) {
67
+ const query = searchQuery(params);
68
+ if (!query)
69
+ return { items: [] };
70
+ const q = params.get('q') ?? '', limit = String(globalLimit);
71
+ const results = [];
72
+ for (const blueprint of cms.definition.blueprints.filter((b) => b.kind !== 'form')) {
73
+ const entries = contentPage(cms, new URLSearchParams({ blueprint: blueprint.id, q, limit })).items;
74
+ // Every entry's text includes the label, so a label match with no entries means the page is unopened.
75
+ if (!entries.length && blueprint.kind !== 'collection') {
76
+ const score = matchScore(query, blueprint.label, '');
77
+ if (score >= 0)
78
+ results.push({ id: blueprint.id, kind: 'page', title: blueprint.label,
79
+ label: blueprint.label, href: '/admin/content/' + blueprint.id, excerpt: '', score, time: 0 });
80
+ }
81
+ for (const row of entries) {
82
+ const title = String(row.draft.title ?? blueprint.label), body = documentText(cms, row);
83
+ results.push({ id: row.id, kind: blueprint.kind === 'collection' ? 'content' : 'page', title,
84
+ label: blueprint.label, href: '/admin/content/' + blueprint.id + (blueprint.kind === 'collection' ? '/' + row.id : ''),
85
+ excerpt: excerpt(body, query), score: matchScore(query, title, body), time: row.updatedAt });
86
+ }
87
+ }
88
+ const labels = new Map();
89
+ for (const row of mediaPage(cms, new URLSearchParams({ q, limit })).items) {
90
+ const body = assetText(cms, row, labels);
91
+ results.push({ id: row.id, kind: 'media', title: row.filename, label: 'Media library',
92
+ href: '/admin/media?asset=' + encodeURIComponent(row.id), excerpt: excerpt(body, query),
93
+ score: matchScore(query, row.filename, body), time: row.createdAt });
94
+ }
95
+ return { items: results.sort((a, b) => b.score - a.score || b.time - a.time || b.id.localeCompare(a.id))
96
+ .slice(0, globalLimit).map(({ score, time, ...result }) => result) };
97
+ }
@@ -1,8 +1,8 @@
1
- import type { R2Bucket, ImagesBinding } from '@cloudflare/workers-types/index.ts';
2
- export interface WorkerEnv {
1
+ import type { R2Bucket } from '@cloudflare/workers-types/index.ts';
2
+ import { type UploadSigningEnv } from './uploads.js';
3
+ export interface WorkerEnv extends UploadSigningEnv {
3
4
  MEDIA: R2Bucket;
4
- IMAGES: ImagesBinding;
5
- CMS_TOKEN: string;
5
+ SANDCMS_WORKER_TOKEN: string;
6
6
  }
7
7
  declare const _default: {
8
8
  fetch(request: Request, env: WorkerEnv): Promise<Response>;
@@ -1,14 +1,28 @@
1
+ import { isFocusPoint } from '../blueprints/files.js';
1
2
  import { sanitizeSvg, svgDimensions } from './svg.js';
2
3
  import { sourceURL, validSource } from './source.js';
3
- const PROTOCOL = 3;
4
+ import { presignUpload, uploadInput } from './uploads.js';
5
+ const PROTOCOL = 5;
4
6
  const json = (v, status = 200) => Response.json(v, { status, headers: { 'Cache-Control': 'private, no-store' } });
5
7
  const idPattern = /^[a-f0-9-]{36}$/;
6
8
  const variantPattern = /^([a-f0-9-]{36})\/([a-f0-9]{16})\/([a-z][a-z0-9_-]*)-(\d+)\.(webp|avif|jpeg|svg)$/;
9
+ const isDeleted = (env, id) => env.MEDIA.head('deleted/' + id);
10
+ async function putAsset(env, id, key, value, options) {
11
+ if (await isDeleted(env, id))
12
+ throw new Error('Asset has been deleted');
13
+ const stored = await env.MEDIA.put(key, value, options);
14
+ // A deletion may arrive while R2 is committing the write.
15
+ if (await isDeleted(env, id)) {
16
+ await env.MEDIA.delete(key);
17
+ throw new Error('Asset has been deleted');
18
+ }
19
+ return stored;
20
+ }
7
21
  async function authenticated(request, env) {
8
- if (!env.CMS_TOKEN || env.CMS_TOKEN.length < 32)
22
+ if (!env.SANDCMS_WORKER_TOKEN || env.SANDCMS_WORKER_TOKEN.length < 32)
9
23
  return false;
10
24
  const got = request.headers.get('authorization') ?? '';
11
- const a = new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(got))), b = new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(`Bearer ${env.CMS_TOKEN}`)));
25
+ const a = new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(got))), b = new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(`Bearer ${env.SANDCMS_WORKER_TOKEN}`)));
12
26
  let diff = 0;
13
27
  for (let i = 0; i < a.length; i++)
14
28
  diff |= a[i] ^ b[i];
@@ -39,20 +53,81 @@ async function boundedBody(request, limit) {
39
53
  }
40
54
  return data;
41
55
  }
56
+ // Both inspection and encoding use remote transformations, which accept large originals.
57
+ async function transform(source, image, mime) {
58
+ let output;
59
+ try {
60
+ const options = {
61
+ redirect: 'manual',
62
+ cf: { image }
63
+ };
64
+ output = await fetch(source, options);
65
+ }
66
+ catch {
67
+ // Fetch errors can include the signed URL; keep capabilities out of job errors.
68
+ throw new Error('Remote image transformation request failed');
69
+ }
70
+ let error;
71
+ if (!output.ok)
72
+ error = `Remote image transformation failed (${output.status})`;
73
+ else if (output.headers.get('content-type')?.split(';')[0] !== mime)
74
+ error = 'Remote image transformation returned an unexpected format';
75
+ // JSON inspection is verified by MIME type and schema; local emulation omits cf-resized.
76
+ else if ((mime !== 'application/json' && !output.headers.has('cf-resized')) ||
77
+ /err=/i.test(output.headers.get('cf-resized') ?? ''))
78
+ error = 'Remote image transformation was not applied';
79
+ if (error) {
80
+ await output.body?.cancel();
81
+ throw new Error(error);
82
+ }
83
+ return output;
84
+ }
85
+ function dimensions(width, height) {
86
+ if (typeof width !== 'number' ||
87
+ typeof height !== 'number' ||
88
+ !Number.isSafeInteger(width) ||
89
+ !Number.isSafeInteger(height) ||
90
+ width < 1 ||
91
+ height < 1 ||
92
+ width * height > 100_000_000)
93
+ throw new Error('Image must be at most 100 megapixels');
94
+ return { width, height };
95
+ }
96
+ async function inspect(source) {
97
+ const output = await transform(source, { format: 'json', anim: false }, 'application/json');
98
+ const bytes = await boundedBody(output, 4096);
99
+ let info;
100
+ try {
101
+ info = JSON.parse(new TextDecoder().decode(bytes));
102
+ }
103
+ catch {
104
+ throw new Error('Invalid remote image metadata');
105
+ }
106
+ // No resize is requested: top-level dimensions include EXIF orientation.
107
+ dimensions(info?.original?.width, info?.original?.height);
108
+ return dimensions(info?.width, info?.height);
109
+ }
42
110
  export default {
43
111
  async fetch(request, env) {
44
112
  const url = new URL(request.url), path = url.pathname;
45
113
  try {
46
114
  if (path.startsWith('/internal/source/')) {
47
- const id = path.slice('/internal/source/'.length);
115
+ const [id, staging, ...extra] = path.slice('/internal/source/'.length).split('/');
48
116
  if (request.method !== 'GET' ||
49
117
  !idPattern.test(id) ||
50
- !(await validSource(url, env.CMS_TOKEN)))
118
+ (staging !== undefined && !idPattern.test(staging)) ||
119
+ extra.length > 0 ||
120
+ !(await validSource(url, env.SANDCMS_WORKER_TOKEN)))
51
121
  return new Response('Not found', {
52
122
  status: 404,
53
123
  headers: { 'Cache-Control': 'private, no-store' }
54
124
  });
55
- const original = await env.MEDIA.get('originals/' + id);
125
+ if (await isDeleted(env, id))
126
+ return new Response('Not found', {
127
+ status: 404,
128
+ headers: { 'Cache-Control': 'private, no-store' }
129
+ });
130
+ const original = await env.MEDIA.get(staging ? `incoming/${id}/${staging}` : 'originals/' + id);
56
131
  if (!original || original.httpMetadata?.contentType === 'image/svg+xml')
57
132
  return new Response('Not found', {
58
133
  status: 404,
@@ -72,14 +147,88 @@ export default {
72
147
  return new Response('Not found', { status: 404, headers: { 'Cache-Control': 'no-store' } });
73
148
  if (path === '/internal/health')
74
149
  return json({ protocol: PROTOCOL });
75
- if (request.headers.get('x-sandcms-protocol') !== String(PROTOCOL))
150
+ if (!['3', '4', String(PROTOCOL)].includes(request.headers.get('x-sandcms-protocol') ?? ''))
76
151
  return json({ error: 'Incompatible CMS media protocol' }, 409);
152
+ const uploadMatch = /^\/internal\/uploads\/([a-f0-9-]{36})\/(sign|complete)$/.exec(path);
153
+ if (uploadMatch && request.method === 'POST') {
154
+ if (!['4', String(PROTOCOL)].includes(request.headers.get('x-sandcms-protocol') ?? ''))
155
+ return json({ error: 'Direct uploads require media protocol 4' }, 409);
156
+ const id = uploadMatch[1];
157
+ if (await isDeleted(env, id))
158
+ throw new Error('Asset has been deleted');
159
+ const input = uploadInput(JSON.parse(new TextDecoder().decode(await boundedBody(request, 4096))));
160
+ const staging = `incoming/${id}/${input.attempt}`;
161
+ if (uploadMatch[2] === 'sign')
162
+ return json(await presignUpload(env, staging, input.mime));
163
+ // Recover if promotion succeeded but the CMS never received the response.
164
+ const accepted = await env.MEDIA.head('originals/' + id);
165
+ if (accepted) {
166
+ if (accepted.customMetadata?.uploadAttempt !== input.attempt)
167
+ throw new Error('Original already exists for another upload');
168
+ await env.MEDIA.delete(staging);
169
+ return json({ stored: true });
170
+ }
171
+ const object = await env.MEDIA.get(staging);
172
+ if (!object)
173
+ throw new Error('Upload missing; upload the file again');
174
+ if (object.size !== input.bytes || object.httpMetadata?.contentType !== input.mime) {
175
+ await object.body.cancel();
176
+ throw new Error('Upload size or type does not match');
177
+ }
178
+ // The PUT capability only targets staging. Inspection uses a separate immutable snapshot.
179
+ const validationAttempt = crypto.randomUUID();
180
+ const validation = `incoming/${id}/${validationAttempt}`;
181
+ try {
182
+ if (input.mime === 'image/svg+xml') {
183
+ const data = await boundedBody(new Response(object.body), 1_000_000);
184
+ const svg = sanitizeSvg(new TextDecoder().decode(data));
185
+ const promoted = await putAsset(env, id, 'originals/' + id, svg, {
186
+ httpMetadata: { contentType: input.mime },
187
+ customMetadata: { uploadAttempt: input.attempt },
188
+ onlyIf: { etagDoesNotMatch: '*' }
189
+ });
190
+ if (!promoted &&
191
+ (await env.MEDIA.head('originals/' + id))?.customMetadata?.uploadAttempt !==
192
+ input.attempt)
193
+ throw new Error('Original already exists for another upload');
194
+ }
195
+ else {
196
+ await env.MEDIA.put(validation, object.body, {
197
+ httpMetadata: { contentType: input.mime }
198
+ });
199
+ const info = await inspect(await sourceURL(url.origin, id, env.SANDCMS_WORKER_TOKEN, validationAttempt));
200
+ const validated = await env.MEDIA.get(validation);
201
+ if (!validated)
202
+ throw new Error('Upload snapshot missing');
203
+ const promoted = await putAsset(env, id, 'originals/' + id, validated.body, {
204
+ httpMetadata: { contentType: input.mime },
205
+ customMetadata: {
206
+ width: String(info.width),
207
+ height: String(info.height),
208
+ uploadAttempt: input.attempt
209
+ },
210
+ onlyIf: { etagDoesNotMatch: '*' }
211
+ });
212
+ if (!promoted &&
213
+ (await env.MEDIA.head('originals/' + id))?.customMetadata?.uploadAttempt !==
214
+ input.attempt)
215
+ throw new Error('Original already exists for another upload');
216
+ }
217
+ await env.MEDIA.delete(staging);
218
+ return json({ stored: true });
219
+ }
220
+ finally {
221
+ await env.MEDIA.delete(validation);
222
+ }
223
+ }
77
224
  if (path.startsWith('/internal/media/') && ['GET', 'HEAD'].includes(request.method)) {
78
225
  if (url.search)
79
226
  return json({ error: 'Only named variants are available' }, 400);
80
227
  const key = path.slice('/internal/media/'.length);
81
228
  if (!variantPattern.test(key))
82
229
  return new Response('Not found', { status: 404 });
230
+ if (await isDeleted(env, key.split('/')[0]))
231
+ return new Response('Not found', { status: 404 });
83
232
  const object = await env.MEDIA.get('variants/' + key);
84
233
  if (!object || object.customMetadata?.ready !== 'true')
85
234
  return new Response('Not found', { status: 404 });
@@ -108,6 +257,8 @@ export default {
108
257
  return json({ error: 'Invalid asset' }, 400);
109
258
  const id = match[1];
110
259
  if (request.method === 'PUT' && !match[2]) {
260
+ if (await isDeleted(env, id))
261
+ throw new Error('Asset has been deleted');
111
262
  const mime = request.headers.get('content-type') ?? '';
112
263
  if (![
113
264
  'image/jpeg',
@@ -121,37 +272,29 @@ export default {
121
272
  if (mime === 'image/svg+xml') {
122
273
  const data = await boundedBody(request, 1_000_000);
123
274
  sanitizeSvg(new TextDecoder().decode(data));
124
- await env.MEDIA.put('originals/' + id, data, { httpMetadata: { contentType: mime } });
275
+ await putAsset(env, id, 'originals/' + id, data, { httpMetadata: { contentType: mime } });
125
276
  }
126
277
  else {
127
278
  const length = Number(request.headers.get('content-length'));
128
279
  if (!Number.isSafeInteger(length) || length < 1 || length > 100_000_000 || !request.body)
129
280
  throw new Error('Raster uploads require Content-Length between 1 and 100000000 bytes');
130
281
  // Request and R2 bodies have a known length: stream without buffering a 100 MB copy.
131
- // Validate a private staging object before making it available to the source endpoint.
132
- const staging = `incoming/${id}/${crypto.randomUUID()}`;
282
+ // Validate a private staging object before promoting it to an original.
283
+ const attempt = crypto.randomUUID();
284
+ const staging = `incoming/${id}/${attempt}`;
133
285
  try {
134
286
  const stored = await env.MEDIA.put(staging, request.body, {
135
287
  httpMetadata: { contentType: mime }
136
288
  });
137
289
  if (!stored || stored.size !== length)
138
290
  throw new Error('Upload length mismatch');
139
- const input = await env.MEDIA.get(staging);
140
- if (!input)
141
- throw new Error('Upload missing');
142
- const info = await env.IMAGES.info(input.body);
143
- if (!('width' in info) ||
144
- !Number.isFinite(info.width) ||
145
- !Number.isFinite(info.height) ||
146
- info.width < 1 ||
147
- info.height < 1 ||
148
- info.width * info.height > 100_000_000)
149
- throw new Error('Image must be at most 100 megapixels');
291
+ const info = await inspect(await sourceURL(url.origin, id, env.SANDCMS_WORKER_TOKEN, attempt));
150
292
  const validated = await env.MEDIA.get(staging);
151
293
  if (!validated)
152
294
  throw new Error('Upload missing');
153
- await env.MEDIA.put('originals/' + id, validated.body, {
154
- httpMetadata: { contentType: mime }
295
+ await putAsset(env, id, 'originals/' + id, validated.body, {
296
+ httpMetadata: { contentType: mime },
297
+ customMetadata: { width: String(info.width), height: String(info.height) }
155
298
  });
156
299
  }
157
300
  finally {
@@ -161,8 +304,16 @@ export default {
161
304
  return json({ stored: true });
162
305
  }
163
306
  if (request.method === 'POST' && match[2]) {
307
+ if (await isDeleted(env, id))
308
+ throw new Error('Asset has been deleted');
164
309
  const body = JSON.parse(new TextDecoder().decode(await boundedBody(request, 4096)));
165
- if (!/^[a-f0-9]{16}$/.test(body.version) ||
310
+ if ((body.aspectRatio !== undefined &&
311
+ (typeof body.aspectRatio !== 'number' ||
312
+ !Number.isFinite(body.aspectRatio) ||
313
+ body.aspectRatio < 0.01 ||
314
+ body.aspectRatio > 100)) ||
315
+ (body.focus !== undefined && !isFocusPoint(body.focus)) ||
316
+ !/^[a-f0-9]{16}$/.test(body.version) ||
166
317
  !/^([a-z][a-z0-9_-]*)$/.test(body.preset) ||
167
318
  !Number.isInteger(body.size) ||
168
319
  body.size < 1 ||
@@ -190,45 +341,31 @@ export default {
190
341
  else {
191
342
  if (body.format === 'svg')
192
343
  throw new Error('Raster input cannot become SVG');
193
- const info = await env.IMAGES.info(original.body);
194
- if (!('width' in info))
195
- throw new Error('Raster dimensions unavailable');
344
+ await original.body.cancel();
345
+ const source = await sourceURL(url.origin, id, env.SANDCMS_WORKER_TOKEN);
346
+ // Older originals have no saved dimensions; inspect them remotely on rebuild.
347
+ const metadata = original.customMetadata;
348
+ const info = metadata?.width && metadata?.height
349
+ ? dimensions(Number(metadata.width), Number(metadata.height))
350
+ : await inspect(source);
196
351
  const scale = Math.min(1, body.size / Math.max(info.width, info.height));
197
352
  width = Math.max(1, Math.round(info.width * scale));
198
353
  height = Math.max(1, Math.round(info.height * scale));
199
- const source = await sourceURL(url.origin, id, env.CMS_TOKEN);
200
- const options = {
201
- redirect: 'manual',
202
- cf: {
203
- image: {
204
- width,
205
- height,
206
- fit: 'scale-down',
207
- format: body.format,
208
- quality: body.quality
209
- }
210
- }
211
- };
212
- try {
213
- output = await fetch(source, options);
214
- }
215
- catch {
216
- // Fetch errors can include the URL; never expose a source capability in job errors.
217
- throw new Error('Remote image transformation request failed');
218
- }
219
- if (!output.ok) {
220
- await output.body?.cancel();
221
- throw new Error(`Remote image transformation failed (${output.status})`);
222
- }
223
- if (output.headers.get('content-type')?.split(';')[0] !== `image/${body.format}`) {
224
- await output.body?.cancel();
225
- throw new Error('Remote image transformation returned an unexpected format');
226
- }
227
- if (!output.headers.has('cf-resized') ||
228
- /err=/i.test(output.headers.get('cf-resized'))) {
229
- await output.body?.cancel();
230
- throw new Error('Remote image transformation was not applied');
354
+ if (body.aspectRatio) {
355
+ const targetWidth = Math.max(1, Math.round(body.size * Math.min(1, body.aspectRatio)));
356
+ const targetHeight = Math.max(1, Math.round(body.size / Math.max(1, body.aspectRatio)));
357
+ const cropScale = Math.min(1, info.width / targetWidth, info.height / targetHeight);
358
+ width = Math.max(1, Math.round(targetWidth * cropScale));
359
+ height = Math.max(1, Math.round(targetHeight * cropScale));
231
360
  }
361
+ output = await transform(source, {
362
+ width,
363
+ height,
364
+ fit: body.aspectRatio ? 'cover' : 'scale-down',
365
+ ...(body.aspectRatio && body.focus ? { gravity: body.focus } : {}),
366
+ format: body.format,
367
+ quality: body.quality
368
+ }, `image/${body.format}`);
232
369
  }
233
370
  const result = {
234
371
  key,
@@ -240,21 +377,25 @@ export default {
240
377
  };
241
378
  // R2 requires a known stream length; the Images result may be streamed without one.
242
379
  const bytes = await boundedBody(output, 20_000_000);
243
- await env.MEDIA.put('variants/' + key, bytes, {
380
+ await putAsset(env, id, 'variants/' + key, bytes, {
244
381
  httpMetadata: { contentType: output.headers.get('content-type') },
245
382
  customMetadata: { ready: 'true', result: JSON.stringify(result) }
246
383
  });
247
384
  return json(result);
248
385
  }
249
386
  if (request.method === 'DELETE' && !match[2]) {
387
+ // Keep the small marker so late requests can never recreate this asset ID.
388
+ await env.MEDIA.put('deleted/' + id, '', { httpMetadata: { contentType: 'text/plain' } });
250
389
  await env.MEDIA.delete('originals/' + id);
251
- let cursor;
252
- do {
253
- const list = await env.MEDIA.list({ prefix: 'variants/' + id + '/', cursor });
254
- if (list.objects.length)
255
- await env.MEDIA.delete(list.objects.map((o) => o.key));
256
- cursor = list.truncated ? list.cursor : undefined;
257
- } while (cursor);
390
+ for (const prefix of ['variants/' + id + '/', 'incoming/' + id + '/']) {
391
+ let cursor;
392
+ do {
393
+ const list = await env.MEDIA.list({ prefix, cursor });
394
+ if (list.objects.length)
395
+ await env.MEDIA.delete(list.objects.map((o) => o.key));
396
+ cursor = list.truncated ? list.cursor : undefined;
397
+ } while (cursor);
398
+ }
258
399
  return json({ deleted: true });
259
400
  }
260
401
  return new Response('Not found', { status: 404 });
@@ -0,0 +1,5 @@
1
+ import { type WorkerEnv } from './index.js';
2
+ declare const _default: {
3
+ fetch(request: Request, env: WorkerEnv): Promise<Response>;
4
+ };
5
+ export default _default;
@@ -0,0 +1,57 @@
1
+ // Loopback-only S3 upload emulator for wrangler dev. Production uses the standard Worker entry.
2
+ import worker, {} from './index.js';
3
+ import { uploadSigner } from './uploads.js';
4
+ export default {
5
+ async fetch(request, env) {
6
+ const url = new URL(request.url);
7
+ if (!['localhost', '127.0.0.1', '[::1]'].includes(url.hostname))
8
+ return new Response('Local media Worker requires a loopback hostname', { status: 403 });
9
+ const local = {
10
+ ...env,
11
+ SANDCMS_R2_ENDPOINT: url.origin,
12
+ SANDCMS_R2_BUCKET_NAME: 'local-media',
13
+ SANDCMS_R2_ACCESS_KEY_ID: 'local-only',
14
+ SANDCMS_R2_SECRET_ACCESS_KEY: env.SANDCMS_WORKER_TOKEN
15
+ };
16
+ if (!url.pathname.startsWith('/local-media/'))
17
+ return worker.fetch(request, local);
18
+ const origin = request.headers.get('origin');
19
+ const headers = new Headers({ 'Cache-Control': 'private, no-store' });
20
+ if (origin && ['localhost', '127.0.0.1', '[::1]'].includes(new URL(origin).hostname)) {
21
+ headers.set('Access-Control-Allow-Origin', origin);
22
+ headers.set('Access-Control-Allow-Methods', 'PUT');
23
+ headers.set('Access-Control-Allow-Headers', 'Content-Type');
24
+ headers.set('Vary', 'Origin');
25
+ }
26
+ if (request.method === 'OPTIONS')
27
+ return new Response(null, { headers });
28
+ const key = url.pathname.slice('/local-media/'.length);
29
+ const date = url.searchParams.get('X-Amz-Date') ?? '';
30
+ const signature = url.searchParams.get('X-Amz-Signature');
31
+ const expires = Number(url.searchParams.get('X-Amz-Expires'));
32
+ const timestamp = Date.parse(date.replace(/^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z$/, '$1-$2-$3T$4:$5:$6Z'));
33
+ if (!env.SANDCMS_WORKER_TOKEN ||
34
+ request.method !== 'PUT' ||
35
+ !/^incoming\/[a-f0-9-]{36}\/[a-f0-9-]{36}$/.test(key) ||
36
+ !Number.isFinite(timestamp) ||
37
+ expires !== 900 ||
38
+ Date.now() > timestamp + expires * 1000 ||
39
+ timestamp > Date.now() + 60_000)
40
+ return new Response('Invalid upload capability', { status: 403, headers });
41
+ url.searchParams.delete('X-Amz-Signature');
42
+ const signed = await uploadSigner(local).sign(url, {
43
+ method: 'PUT',
44
+ headers: { 'content-type': request.headers.get('content-type') ?? '' },
45
+ aws: { signQuery: true, allHeaders: true, datetime: date }
46
+ });
47
+ if (new URL(signed.url).searchParams.get('X-Amz-Signature') !== signature)
48
+ return new Response('Invalid upload signature', { status: 403, headers });
49
+ const length = Number(request.headers.get('content-length'));
50
+ if (!request.body || !Number.isSafeInteger(length) || length < 1 || length > 100_000_000)
51
+ return new Response('Invalid upload size', { status: 400, headers });
52
+ await env.MEDIA.put(key, request.body, {
53
+ httpMetadata: { contentType: request.headers.get('content-type') ?? '' }
54
+ });
55
+ return new Response(null, { headers });
56
+ }
57
+ };
@@ -1,2 +1,2 @@
1
- export declare function sourceURL(origin: string, id: string, secret: string): Promise<URL>;
1
+ export declare function sourceURL(origin: string, id: string, secret: string, staging?: string): Promise<URL>;
2
2
  export declare function validSource(url: URL, secret: string): Promise<boolean>;
@@ -6,8 +6,8 @@ async function key(secret) {
6
6
  function message(url, expires) {
7
7
  return new TextEncoder().encode(`${url.origin}${url.pathname}\n${expires}`);
8
8
  }
9
- export async function sourceURL(origin, id, secret) {
10
- const url = new URL('/internal/source/' + id, origin);
9
+ export async function sourceURL(origin, id, secret, staging) {
10
+ const url = new URL('/internal/source/' + id + (staging ? '/' + staging : ''), origin);
11
11
  const expires = String(Math.floor(Date.now() / 1000) + lifetime);
12
12
  const signature = new Uint8Array(await crypto.subtle.sign('HMAC', await key(secret), message(url, expires)));
13
13
  url.searchParams.set('expires', expires);
@@ -0,0 +1,22 @@
1
+ import { AwsClient } from 'aws4fetch';
2
+ export interface UploadSigningEnv {
3
+ SANDCMS_CF_ACCOUNT_ID?: string;
4
+ SANDCMS_R2_BUCKET_NAME?: string;
5
+ SANDCMS_R2_ACCESS_KEY_ID?: string;
6
+ SANDCMS_R2_SECRET_ACCESS_KEY?: string;
7
+ SANDCMS_R2_ENDPOINT?: string;
8
+ }
9
+ export declare const uploadLifetime = 900;
10
+ export declare function uploadSigner(env: UploadSigningEnv): AwsClient;
11
+ export declare function presignUpload(env: UploadSigningEnv, key: string, mime: string): Promise<{
12
+ url: string;
13
+ headers: {
14
+ 'content-type': string;
15
+ };
16
+ expiresAt: number;
17
+ }>;
18
+ export declare function uploadInput(input: unknown): {
19
+ mime: string;
20
+ bytes: number;
21
+ attempt: string;
22
+ };
@@ -0,0 +1,48 @@
1
+ import { AwsClient } from 'aws4fetch';
2
+ export const uploadLifetime = 900;
3
+ export function uploadSigner(env) {
4
+ if (!env.SANDCMS_R2_ACCESS_KEY_ID ||
5
+ !env.SANDCMS_R2_SECRET_ACCESS_KEY ||
6
+ !env.SANDCMS_R2_BUCKET_NAME)
7
+ throw new Error('Configure SANDCMS_R2_BUCKET_NAME, SANDCMS_R2_ACCESS_KEY_ID and SANDCMS_R2_SECRET_ACCESS_KEY on the media Worker');
8
+ return new AwsClient({
9
+ service: 's3',
10
+ region: 'auto',
11
+ accessKeyId: env.SANDCMS_R2_ACCESS_KEY_ID,
12
+ secretAccessKey: env.SANDCMS_R2_SECRET_ACCESS_KEY
13
+ });
14
+ }
15
+ export async function presignUpload(env, key, mime) {
16
+ const signer = uploadSigner(env);
17
+ if (!env.SANDCMS_R2_ENDPOINT && !env.SANDCMS_CF_ACCOUNT_ID)
18
+ throw new Error('Configure SANDCMS_CF_ACCOUNT_ID on the media Worker');
19
+ const endpoint = env.SANDCMS_R2_ENDPOINT ?? `https://${env.SANDCMS_CF_ACCOUNT_ID}.r2.cloudflarestorage.com`;
20
+ const url = new URL(`${endpoint.replace(/\/$/, '')}/${encodeURIComponent(env.SANDCMS_R2_BUCKET_NAME)}/${key}`);
21
+ if (url.protocol !== 'https:' && !['localhost', '127.0.0.1', '[::1]'].includes(url.hostname))
22
+ throw new Error('SANDCMS_R2_ENDPOINT must use HTTPS');
23
+ url.searchParams.set('X-Amz-Expires', String(uploadLifetime));
24
+ const signed = await signer.sign(url, {
25
+ method: 'PUT',
26
+ headers: { 'content-type': mime },
27
+ aws: { signQuery: true, allHeaders: true }
28
+ });
29
+ return {
30
+ url: signed.url,
31
+ headers: { 'content-type': mime },
32
+ expiresAt: Date.now() + uploadLifetime * 1000
33
+ };
34
+ }
35
+ export function uploadInput(input) {
36
+ const body = input;
37
+ if (!body ||
38
+ typeof body.mime !== 'string' ||
39
+ !['image/jpeg', 'image/png', 'image/webp', 'image/avif', 'image/gif', 'image/svg+xml'].includes(body.mime) ||
40
+ typeof body.bytes !== 'number' ||
41
+ !Number.isSafeInteger(body.bytes) ||
42
+ body.bytes < 1 ||
43
+ body.bytes > (body.mime === 'image/svg+xml' ? 1_000_000 : 100_000_000) ||
44
+ typeof body.attempt !== 'string' ||
45
+ !/^[a-f0-9-]{36}$/.test(body.attempt))
46
+ throw new Error('Invalid upload metadata');
47
+ return { mime: body.mime, bytes: body.bytes, attempt: body.attempt };
48
+ }