sandcms 0.6.0 → 0.6.2
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 +1 -1
- package/dist/cli/cloudflare.js +6 -6
- package/dist/cli/doctor.js +2 -2
- package/dist/cli/files.js +11 -6
- package/dist/cli/index.js +1 -1
- package/dist/cli/upgrade.js +2 -1
- package/dist/env.d.ts +80 -0
- package/dist/env.js +103 -0
- package/dist/server/admin-load.d.ts +46 -76
- package/dist/server/admin-load.js +98 -24
- package/dist/server/api-pagination.d.ts +2 -0
- package/dist/server/api-pagination.js +51 -7
- package/dist/server/cms.d.ts +19 -2
- package/dist/server/cms.js +13 -1
- package/dist/server/content.d.ts +15 -2
- package/dist/server/content.js +72 -30
- package/dist/server/db/auto-migrate.d.ts +12 -0
- package/dist/server/db/auto-migrate.js +23 -0
- package/dist/server/db/migration-data.js +8 -0
- package/dist/server/db/migrations/0007_content_and_job_indexes.sql +2 -0
- package/dist/server/db/migrations/0008_search_text_cache.sql +45 -0
- package/dist/server/db/migrations/meta/0007_snapshot.json +1643 -0
- package/dist/server/db/migrations/meta/0008_snapshot.json +1711 -0
- package/dist/server/db/migrations/meta/_journal.json +14 -0
- package/dist/server/db/schema.d.ts +83 -0
- package/dist/server/db/schema.js +23 -5
- package/dist/server/db/store.js +24 -9
- package/dist/server/email/index.d.ts +5 -1
- package/dist/server/email/index.js +12 -11
- package/dist/server/index.d.ts +1 -0
- package/dist/server/index.js +1 -0
- package/dist/server/media/runner.js +217 -191
- package/dist/server/media/service.d.ts +6 -0
- package/dist/server/media/service.js +36 -3
- package/dist/server/search.d.ts +2 -0
- package/dist/server/search.js +7 -0
- package/dist/worker/index.d.ts +1 -1
- package/dist/worker/index.js +134 -81
- package/dist/worker/local.js +5 -5
- package/dist/worker/uploads.d.ts +5 -5
- package/dist/worker/uploads.js +11 -9
- package/docs/cloudflare.md +7 -7
- package/docs/deployment.md +18 -0
- package/docs/email.md +5 -5
- package/docs/media.md +2 -2
- package/docs/package.md +4 -4
- package/docs/releases.md +18 -0
- package/docs/upgrades.md +66 -0
- package/package.json +5 -1
- package/templates/env.ts +8 -0
- package/templates/integration.ts +6 -5
|
@@ -1,225 +1,251 @@
|
|
|
1
|
-
import { and, eq, lte, lt,
|
|
1
|
+
import { and, eq, inArray, lte, lt, notExists, or } from 'drizzle-orm';
|
|
2
2
|
import { media, mediaDeletions, jobs, uploads, uploadSessions } from '../db/schema.js';
|
|
3
|
-
const uploadTimeout = 120_000
|
|
3
|
+
const uploadTimeout = 120_000, jobTimeout = 120_000,
|
|
4
|
+
// Covers a typical preset set (sizes x formats plus the editor preview) in one request.
|
|
5
|
+
batchSize = 16;
|
|
4
6
|
export function createRunner(store, worker, enqueue, deletePending) {
|
|
5
7
|
const { db } = store;
|
|
6
8
|
const notDeleting = (id) => notExists(db.select().from(mediaDeletions).where(eq(mediaDeletions.mediaId, id)));
|
|
7
|
-
|
|
9
|
+
// Every claim is a synchronous SQLite transaction with a lease, so ticks may run concurrently.
|
|
8
10
|
async function tick() {
|
|
9
|
-
if (
|
|
10
|
-
return
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
11
|
+
if (await deletePending())
|
|
12
|
+
return true;
|
|
13
|
+
// Unfinished browser transfers expire without retaining file bytes in the database.
|
|
14
|
+
const expired = db.transaction(() => {
|
|
15
|
+
const pending = db
|
|
16
|
+
.select()
|
|
17
|
+
.from(uploadSessions)
|
|
18
|
+
.where(and(notDeleting(uploadSessions.mediaId), or(and(eq(uploadSessions.status, 'uploading'), lte(uploadSessions.expiresAt, Date.now())), eq(uploadSessions.status, 'expired')), lt(uploadSessions.attempts, 4), lte(uploadSessions.nextAttempt, Date.now()), lte(uploadSessions.leaseUntil, Date.now())))
|
|
19
|
+
.get();
|
|
20
|
+
if (!pending)
|
|
21
|
+
return null;
|
|
22
|
+
// Claim expiry before awaiting storage cleanup so completion cannot race it.
|
|
23
|
+
db.update(uploadSessions)
|
|
24
|
+
.set({
|
|
25
|
+
status: 'expired',
|
|
26
|
+
attempts: pending.attempts + 1,
|
|
27
|
+
leaseUntil: Date.now() + 90_000
|
|
28
|
+
})
|
|
29
|
+
.where(eq(uploadSessions.mediaId, pending.mediaId))
|
|
30
|
+
.run();
|
|
31
|
+
db.update(media)
|
|
32
|
+
.set({ status: 'failed', error: 'Upload expired. Upload this file again.' })
|
|
33
|
+
.where(eq(media.id, pending.mediaId))
|
|
34
|
+
.run();
|
|
35
|
+
return pending;
|
|
36
|
+
});
|
|
37
|
+
if (expired) {
|
|
38
|
+
try {
|
|
39
|
+
await worker('assets/' + expired.mediaId, { method: 'DELETE' });
|
|
25
40
|
db.update(uploadSessions)
|
|
26
|
-
.set({
|
|
27
|
-
|
|
28
|
-
attempts: pending.attempts + 1,
|
|
29
|
-
leaseUntil: Date.now() + 90_000
|
|
30
|
-
})
|
|
31
|
-
.where(eq(uploadSessions.mediaId, pending.mediaId))
|
|
41
|
+
.set({ attempts: 4, leaseUntil: 0 })
|
|
42
|
+
.where(eq(uploadSessions.mediaId, expired.mediaId))
|
|
32
43
|
.run();
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
db.update(uploadSessions)
|
|
47
|
+
.set({ leaseUntil: 0, nextAttempt: Date.now() + 2 ** expired.attempts * 2000 })
|
|
48
|
+
.where(eq(uploadSessions.mediaId, expired.mediaId))
|
|
36
49
|
.run();
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
}
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
const session = db.transaction(() => {
|
|
54
|
+
const pending = db
|
|
55
|
+
.select()
|
|
56
|
+
.from(uploadSessions)
|
|
57
|
+
.where(and(notDeleting(uploadSessions.mediaId), eq(uploadSessions.status, 'pending'), lte(uploadSessions.nextAttempt, Date.now()), lte(uploadSessions.leaseUntil, Date.now()), lt(uploadSessions.attempts, 4)))
|
|
58
|
+
.get();
|
|
59
|
+
if (!pending)
|
|
60
|
+
return null;
|
|
61
|
+
db.update(uploadSessions)
|
|
62
|
+
.set({ leaseUntil: Date.now() + uploadTimeout + 30_000, attempts: pending.attempts + 1 })
|
|
63
|
+
.where(eq(uploadSessions.mediaId, pending.mediaId))
|
|
64
|
+
.run();
|
|
65
|
+
return pending;
|
|
66
|
+
});
|
|
67
|
+
if (session) {
|
|
68
|
+
const asset = db.select().from(media).where(eq(media.id, session.mediaId)).get();
|
|
69
|
+
if (!asset)
|
|
53
70
|
return true;
|
|
71
|
+
try {
|
|
72
|
+
await worker(`uploads/${asset.id}/complete`, {
|
|
73
|
+
method: 'POST',
|
|
74
|
+
signal: AbortSignal.timeout(uploadTimeout),
|
|
75
|
+
headers: { 'content-type': 'application/json' },
|
|
76
|
+
body: JSON.stringify({ attempt: session.attempt, mime: asset.mime, bytes: asset.bytes })
|
|
77
|
+
});
|
|
78
|
+
db.transaction(() => {
|
|
79
|
+
db.delete(uploadSessions).where(eq(uploadSessions.mediaId, asset.id)).run();
|
|
80
|
+
enqueue(asset.id, asset.mime);
|
|
81
|
+
});
|
|
54
82
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
.from(uploadSessions)
|
|
59
|
-
.where(and(notDeleting(uploadSessions.mediaId), eq(uploadSessions.status, 'pending'), lte(uploadSessions.nextAttempt, Date.now()), lte(uploadSessions.leaseUntil, Date.now()), lt(uploadSessions.attempts, 4)))
|
|
60
|
-
.get();
|
|
61
|
-
if (!pending)
|
|
62
|
-
return null;
|
|
83
|
+
catch (e) {
|
|
84
|
+
if (db.select().from(mediaDeletions).where(eq(mediaDeletions.mediaId, asset.id)).get())
|
|
85
|
+
return true;
|
|
63
86
|
db.update(uploadSessions)
|
|
64
|
-
.set({ leaseUntil: Date.now() +
|
|
65
|
-
.where(eq(uploadSessions.mediaId,
|
|
87
|
+
.set({ leaseUntil: 0, nextAttempt: Date.now() + 2 ** session.attempts * 2000 })
|
|
88
|
+
.where(eq(uploadSessions.mediaId, asset.id))
|
|
66
89
|
.run();
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
if (!asset)
|
|
72
|
-
return true;
|
|
73
|
-
try {
|
|
74
|
-
await worker(`uploads/${asset.id}/complete`, {
|
|
75
|
-
method: 'POST',
|
|
76
|
-
signal: AbortSignal.timeout(uploadTimeout),
|
|
77
|
-
headers: { 'content-type': 'application/json' },
|
|
78
|
-
body: JSON.stringify({ attempt: session.attempt, mime: asset.mime, bytes: asset.bytes })
|
|
79
|
-
});
|
|
80
|
-
db.transaction(() => {
|
|
81
|
-
db.delete(uploadSessions).where(eq(uploadSessions.mediaId, asset.id)).run();
|
|
82
|
-
enqueue(asset.id, asset.mime);
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
catch (e) {
|
|
86
|
-
if (db.select().from(mediaDeletions).where(eq(mediaDeletions.mediaId, asset.id)).get())
|
|
87
|
-
return true;
|
|
88
|
-
db.update(uploadSessions)
|
|
89
|
-
.set({ leaseUntil: 0, nextAttempt: Date.now() + 2 ** session.attempts * 2000 })
|
|
90
|
-
.where(eq(uploadSessions.mediaId, asset.id))
|
|
90
|
+
if (session.attempts >= 3)
|
|
91
|
+
db.update(media)
|
|
92
|
+
.set({ status: 'failed', error: e instanceof Error ? e.message : 'Upload failed' })
|
|
93
|
+
.where(eq(media.id, asset.id))
|
|
91
94
|
.run();
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
95
|
+
}
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
const original = db.transaction(() => {
|
|
99
|
+
const pending = db
|
|
100
|
+
.select()
|
|
101
|
+
.from(uploads)
|
|
102
|
+
.where(and(notDeleting(uploads.mediaId), lte(uploads.nextAttempt, Date.now()), lte(uploads.leaseUntil, Date.now()), lt(uploads.attempts, 4)))
|
|
103
|
+
.get();
|
|
104
|
+
if (!pending || pending.attempts >= 4)
|
|
105
|
+
return null;
|
|
106
|
+
db.update(uploads)
|
|
107
|
+
.set({ leaseUntil: Date.now() + uploadTimeout + 30_000, attempts: pending.attempts + 1 })
|
|
108
|
+
.where(eq(uploads.mediaId, pending.mediaId))
|
|
109
|
+
.run();
|
|
110
|
+
return pending;
|
|
111
|
+
});
|
|
112
|
+
if (original) {
|
|
113
|
+
const asset = db.select().from(media).where(eq(media.id, original.mediaId)).get();
|
|
114
|
+
if (!asset)
|
|
98
115
|
return true;
|
|
116
|
+
try {
|
|
117
|
+
await worker('assets/' + asset.id, {
|
|
118
|
+
method: 'PUT',
|
|
119
|
+
signal: AbortSignal.timeout(uploadTimeout),
|
|
120
|
+
body: new Uint8Array(original.body),
|
|
121
|
+
headers: {
|
|
122
|
+
'content-type': asset.mime,
|
|
123
|
+
'content-length': String(original.body.byteLength)
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
db.transaction(() => {
|
|
127
|
+
db.delete(uploads).where(eq(uploads.mediaId, asset.id)).run();
|
|
128
|
+
enqueue(asset.id, asset.mime);
|
|
129
|
+
});
|
|
99
130
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
.where(and(notDeleting(uploads.mediaId), lte(uploads.nextAttempt, Date.now()), lte(uploads.leaseUntil, Date.now()), lt(uploads.attempts, 4)))
|
|
105
|
-
.get();
|
|
106
|
-
if (!pending || pending.attempts >= 4)
|
|
107
|
-
return null;
|
|
131
|
+
catch (e) {
|
|
132
|
+
if (db.select().from(mediaDeletions).where(eq(mediaDeletions.mediaId, asset.id)).get())
|
|
133
|
+
return true;
|
|
134
|
+
const message = e instanceof Error ? e.message : 'Upload failed';
|
|
108
135
|
db.update(uploads)
|
|
109
|
-
.set({ leaseUntil: Date.now() +
|
|
110
|
-
.where(eq(uploads.mediaId,
|
|
136
|
+
.set({ leaseUntil: 0, nextAttempt: Date.now() + 2 ** original.attempts * 2000 })
|
|
137
|
+
.where(eq(uploads.mediaId, asset.id))
|
|
111
138
|
.run();
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
if (!asset)
|
|
117
|
-
return true;
|
|
118
|
-
try {
|
|
119
|
-
await worker('assets/' + asset.id, {
|
|
120
|
-
method: 'PUT',
|
|
121
|
-
signal: AbortSignal.timeout(uploadTimeout),
|
|
122
|
-
body: new Uint8Array(original.body),
|
|
123
|
-
headers: {
|
|
124
|
-
'content-type': asset.mime,
|
|
125
|
-
'content-length': String(original.body.byteLength)
|
|
126
|
-
}
|
|
127
|
-
});
|
|
128
|
-
db.transaction(() => {
|
|
129
|
-
db.delete(uploads).where(eq(uploads.mediaId, asset.id)).run();
|
|
130
|
-
enqueue(asset.id, asset.mime);
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
catch (e) {
|
|
134
|
-
if (db.select().from(mediaDeletions).where(eq(mediaDeletions.mediaId, asset.id)).get())
|
|
135
|
-
return true;
|
|
136
|
-
const message = e instanceof Error ? e.message : 'Upload failed';
|
|
137
|
-
db.update(uploads)
|
|
138
|
-
.set({ leaseUntil: 0, nextAttempt: Date.now() + 2 ** original.attempts * 2000 })
|
|
139
|
-
.where(eq(uploads.mediaId, asset.id))
|
|
139
|
+
if (original.attempts >= 3)
|
|
140
|
+
db.update(media)
|
|
141
|
+
.set({ status: 'failed', error: message })
|
|
142
|
+
.where(eq(media.id, asset.id))
|
|
140
143
|
.run();
|
|
141
|
-
if (original.attempts >= 3)
|
|
142
|
-
db.update(media)
|
|
143
|
-
.set({ status: 'failed', error: message })
|
|
144
|
-
.where(eq(media.id, asset.id))
|
|
145
|
-
.run();
|
|
146
|
-
}
|
|
147
|
-
return true;
|
|
148
144
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
const now = Date.now();
|
|
148
|
+
const batch = db.transaction(() => {
|
|
149
|
+
const due = and(notDeleting(jobs.mediaId), or(and(eq(jobs.status, 'pending'), lte(jobs.nextAttempt, now)), and(eq(jobs.status, 'running'), lte(jobs.leaseUntil, now))));
|
|
150
|
+
const next = db.select().from(jobs).where(due).limit(1).get();
|
|
151
|
+
if (!next)
|
|
152
|
+
return [];
|
|
153
|
+
// Claim the asset's other due variants too so one Worker request reads the original once.
|
|
154
|
+
const claimed = db
|
|
155
|
+
.select()
|
|
156
|
+
.from(jobs)
|
|
157
|
+
.where(and(due, eq(jobs.mediaId, next.mediaId), eq(jobs.version, next.version)))
|
|
158
|
+
.limit(batchSize)
|
|
159
|
+
.all();
|
|
160
|
+
for (const job of claimed)
|
|
159
161
|
db.update(jobs)
|
|
160
|
-
.set({
|
|
161
|
-
|
|
162
|
+
.set({
|
|
163
|
+
status: 'running',
|
|
164
|
+
leaseUntil: now + jobTimeout + 30_000,
|
|
165
|
+
attempts: job.attempts + 1
|
|
166
|
+
})
|
|
167
|
+
.where(eq(jobs.id, job.id))
|
|
162
168
|
.run();
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
169
|
+
return claimed;
|
|
170
|
+
});
|
|
171
|
+
if (!batch.length)
|
|
172
|
+
return false;
|
|
173
|
+
const { mediaId, version } = batch[0];
|
|
174
|
+
let results, completed = true;
|
|
175
|
+
try {
|
|
176
|
+
({ results } = (await (await worker(`assets/${mediaId}/variants`, {
|
|
177
|
+
method: 'POST',
|
|
178
|
+
signal: AbortSignal.timeout(jobTimeout),
|
|
179
|
+
body: JSON.stringify({ version, variants: batch.map((job) => job.spec) }),
|
|
180
|
+
headers: { 'content-type': 'application/json' }
|
|
181
|
+
})).json()));
|
|
182
|
+
if (!Array.isArray(results) || results.length !== batch.length)
|
|
183
|
+
throw new Error('Media Worker returned an incomplete variant batch');
|
|
184
|
+
}
|
|
185
|
+
catch (e) {
|
|
186
|
+
if (db.select().from(mediaDeletions).where(eq(mediaDeletions.mediaId, mediaId)).get())
|
|
187
|
+
return true;
|
|
188
|
+
const message = e instanceof Error ? e.message : 'Processing failed';
|
|
189
|
+
results = batch.map(() => ({ error: message }));
|
|
190
|
+
completed = false;
|
|
191
|
+
}
|
|
192
|
+
db.transaction(() => {
|
|
193
|
+
const asset = db.select().from(media).where(eq(media.id, mediaId)).get();
|
|
194
|
+
if (!asset || asset.status === 'deleting') {
|
|
195
|
+
if (completed)
|
|
196
|
+
db.delete(jobs)
|
|
197
|
+
.where(inArray(jobs.id, batch.map((job) => job.id)))
|
|
198
|
+
.run();
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
let failure;
|
|
202
|
+
batch.forEach((job, i) => {
|
|
203
|
+
const result = results[i];
|
|
204
|
+
if ('variant' in result) {
|
|
179
205
|
db.update(jobs)
|
|
180
|
-
.set({ status: 'ready', result, error: null, leaseUntil: 0 })
|
|
206
|
+
.set({ status: 'ready', result: result.variant, error: null, leaseUntil: 0 })
|
|
181
207
|
.where(eq(jobs.id, job.id))
|
|
182
208
|
.run();
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
if (set.length && set.every((j) => j.status === 'ready'))
|
|
189
|
-
db.update(media)
|
|
190
|
-
.set({
|
|
191
|
-
status: 'ready',
|
|
192
|
-
activeVersion: job.version,
|
|
193
|
-
variants: set.map((j) => j.result),
|
|
194
|
-
error: null
|
|
195
|
-
})
|
|
196
|
-
.where(and(eq(media.id, job.mediaId), eq(media.targetVersion, job.version)))
|
|
197
|
-
.run();
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
catch (e) {
|
|
201
|
-
if (db.select().from(mediaDeletions).where(eq(mediaDeletions.mediaId, job.mediaId)).get())
|
|
202
|
-
return true;
|
|
203
|
-
const message = e instanceof Error ? e.message : 'Processing failed', failed = job.attempts >= 3;
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
const failed = job.attempts >= 3;
|
|
212
|
+
if (failed)
|
|
213
|
+
failure ??= result.error;
|
|
204
214
|
db.update(jobs)
|
|
205
215
|
.set({
|
|
206
216
|
status: failed ? 'failed' : 'pending',
|
|
207
|
-
error:
|
|
208
|
-
nextAttempt: Date.now() + 2 ** job.attempts * 2000
|
|
217
|
+
error: result.error,
|
|
218
|
+
nextAttempt: Date.now() + 2 ** job.attempts * 2000,
|
|
219
|
+
// An interrupted request may still be writing; keep its lease so deletion waits.
|
|
220
|
+
...(completed ? { leaseUntil: 0 } : {})
|
|
209
221
|
})
|
|
210
222
|
.where(eq(jobs.id, job.id))
|
|
211
223
|
.run();
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
224
|
+
});
|
|
225
|
+
if (failure) {
|
|
226
|
+
db.update(media)
|
|
227
|
+
.set({ status: 'failed', error: failure })
|
|
228
|
+
.where(and(eq(media.id, mediaId), eq(media.targetVersion, version)))
|
|
229
|
+
.run();
|
|
230
|
+
return;
|
|
217
231
|
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
232
|
+
const set = db
|
|
233
|
+
.select()
|
|
234
|
+
.from(jobs)
|
|
235
|
+
.where(and(eq(jobs.mediaId, mediaId), eq(jobs.version, version)))
|
|
236
|
+
.all();
|
|
237
|
+
if (set.length && set.every((j) => j.status === 'ready'))
|
|
238
|
+
db.update(media)
|
|
239
|
+
.set({
|
|
240
|
+
status: 'ready',
|
|
241
|
+
activeVersion: version,
|
|
242
|
+
variants: set.map((j) => j.result),
|
|
243
|
+
error: null
|
|
244
|
+
})
|
|
245
|
+
.where(and(eq(media.id, mediaId), eq(media.targetVersion, version)))
|
|
246
|
+
.run();
|
|
247
|
+
});
|
|
248
|
+
return true;
|
|
223
249
|
}
|
|
224
250
|
return tick;
|
|
225
251
|
}
|
|
@@ -15,6 +15,7 @@ export declare function createMedia(store: Store, definition: SiteDefinition, co
|
|
|
15
15
|
variants: import("../db/schema.js").Variant[];
|
|
16
16
|
error: string | null;
|
|
17
17
|
createdAt: number;
|
|
18
|
+
searchText: string | null;
|
|
18
19
|
} | undefined;
|
|
19
20
|
upload: (file: File, blueprintId?: string) => Promise<{
|
|
20
21
|
blueprint: string;
|
|
@@ -29,6 +30,7 @@ export declare function createMedia(store: Store, definition: SiteDefinition, co
|
|
|
29
30
|
variants: import("../db/schema.js").Variant[];
|
|
30
31
|
error: string | null;
|
|
31
32
|
createdAt: number;
|
|
33
|
+
searchText: string | null;
|
|
32
34
|
}>;
|
|
33
35
|
initiateUpload: (input: {
|
|
34
36
|
name: string;
|
|
@@ -53,8 +55,10 @@ export declare function createMedia(store: Store, definition: SiteDefinition, co
|
|
|
53
55
|
variants: import("../db/schema.js").Variant[];
|
|
54
56
|
error: string | null;
|
|
55
57
|
createdAt: number;
|
|
58
|
+
searchText: string | null;
|
|
56
59
|
};
|
|
57
60
|
tick: () => Promise<boolean>;
|
|
61
|
+
drain: () => Promise<void>;
|
|
58
62
|
retry: (id: string) => void;
|
|
59
63
|
version: string;
|
|
60
64
|
versionForAsset: (asset: NonNullable<ReturnType<(id: string) => {
|
|
@@ -70,6 +74,7 @@ export declare function createMedia(store: Store, definition: SiteDefinition, co
|
|
|
70
74
|
variants: import("../db/schema.js").Variant[];
|
|
71
75
|
error: string | null;
|
|
72
76
|
createdAt: number;
|
|
77
|
+
searchText: string | null;
|
|
73
78
|
} | undefined>>) => string;
|
|
74
79
|
workerUrl: string;
|
|
75
80
|
list: () => {
|
|
@@ -85,6 +90,7 @@ export declare function createMedia(store: Store, definition: SiteDefinition, co
|
|
|
85
90
|
variants: import("../db/schema.js").Variant[];
|
|
86
91
|
error: string | null;
|
|
87
92
|
createdAt: number;
|
|
93
|
+
searchText: string | null;
|
|
88
94
|
}[];
|
|
89
95
|
updateMetadata: (id: string, input: unknown) => void;
|
|
90
96
|
health(): Promise<{
|
|
@@ -30,7 +30,7 @@ export function createMedia(store, definition, content, token, workerUrl = defin
|
|
|
30
30
|
throw new Error('Configure SANDCMS_WORKER_TOKEN before uploading media');
|
|
31
31
|
const response = await fetch(workerUrl.replace(/\/$/, '') + '/internal/' + path, {
|
|
32
32
|
...init,
|
|
33
|
-
headers: { ...init.headers, authorization: `Bearer ${token}`, 'x-sandcms-protocol': '
|
|
33
|
+
headers: { ...init.headers, authorization: `Bearer ${token}`, 'x-sandcms-protocol': '6' },
|
|
34
34
|
signal: init.signal ?? AbortSignal.timeout(60_000)
|
|
35
35
|
});
|
|
36
36
|
if (!response.ok && !(path.startsWith('media/') && response.status === 304))
|
|
@@ -170,6 +170,14 @@ export function createMedia(store, definition, content, token, workerUrl = defin
|
|
|
170
170
|
}
|
|
171
171
|
const deletePending = createDeletionRunner(store, worker);
|
|
172
172
|
const tick = createRunner(store, worker, enqueue, deletePending);
|
|
173
|
+
// Independent images process in parallel; each Worker request already fans out its variants.
|
|
174
|
+
const concurrency = 4;
|
|
175
|
+
async function drain() {
|
|
176
|
+
await Promise.all(Array.from({ length: concurrency }, async () => {
|
|
177
|
+
while (await tick())
|
|
178
|
+
;
|
|
179
|
+
}));
|
|
180
|
+
}
|
|
173
181
|
function retry(id) {
|
|
174
182
|
const asset = get(id);
|
|
175
183
|
if (!asset)
|
|
@@ -294,6 +302,7 @@ export function createMedia(store, definition, content, token, workerUrl = defin
|
|
|
294
302
|
initiateUpload,
|
|
295
303
|
completeUpload,
|
|
296
304
|
tick,
|
|
305
|
+
drain,
|
|
297
306
|
retry,
|
|
298
307
|
version,
|
|
299
308
|
versionForAsset: assetVersion,
|
|
@@ -385,9 +394,33 @@ export function createMedia(store, definition, content, token, workerUrl = defin
|
|
|
385
394
|
publicGallery: (rows, field) => galleryData(rows, field),
|
|
386
395
|
previewGallery: (rows, field) => galleryData(rows, field, true),
|
|
387
396
|
start: () => {
|
|
388
|
-
|
|
397
|
+
let active = 0, stopped = false;
|
|
398
|
+
// Poll with one loop while idle; each loop that finds work adds another up to the limit.
|
|
399
|
+
async function loop() {
|
|
400
|
+
if (stopped)
|
|
401
|
+
return;
|
|
402
|
+
active++;
|
|
403
|
+
try {
|
|
404
|
+
while (!stopped && (await tick()))
|
|
405
|
+
if (!stopped && active < concurrency)
|
|
406
|
+
void loop();
|
|
407
|
+
}
|
|
408
|
+
catch (e) {
|
|
409
|
+
console.error('SandCMS media job failed', e);
|
|
410
|
+
}
|
|
411
|
+
finally {
|
|
412
|
+
active--;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
const timer = setInterval(() => {
|
|
416
|
+
if (!stopped && active < concurrency)
|
|
417
|
+
void loop();
|
|
418
|
+
}, 1000);
|
|
389
419
|
timer.unref();
|
|
390
|
-
return () =>
|
|
420
|
+
return () => {
|
|
421
|
+
stopped = true;
|
|
422
|
+
clearInterval(timer);
|
|
423
|
+
};
|
|
391
424
|
}
|
|
392
425
|
};
|
|
393
426
|
}
|
package/dist/server/search.d.ts
CHANGED
|
@@ -8,6 +8,8 @@ export declare function searchableText(fields: Record<string, Field>, payload: P
|
|
|
8
8
|
export declare function matchScore(query: string, title: string, body: string): number;
|
|
9
9
|
export declare function documentText(cms: CMS, row: typeof documents.$inferSelect): string;
|
|
10
10
|
export declare function assetText(cms: CMS, row: typeof media.$inferSelect, labels?: Map<string, string>): string;
|
|
11
|
+
export declare function documentSearchText(cms: CMS, row: typeof documents.$inferSelect): string;
|
|
12
|
+
export declare function assetSearchText(cms: CMS, row: typeof media.$inferSelect, labels?: Map<string, string>): string;
|
|
11
13
|
export declare function globalSearch(cms: CMS, params: URLSearchParams): {
|
|
12
14
|
items: SearchResult[];
|
|
13
15
|
};
|
package/dist/server/search.js
CHANGED
|
@@ -56,6 +56,13 @@ export function assetText(cms, row, labels = new Map()) {
|
|
|
56
56
|
return labels.get(key);
|
|
57
57
|
});
|
|
58
58
|
}
|
|
59
|
+
// Cached superset of what matchScore checks, so SQL can pre-filter without changing results.
|
|
60
|
+
export function documentSearchText(cms, row) {
|
|
61
|
+
return normalizeSearch(String(row.draft.title ?? '') + ' ' + documentText(cms, row));
|
|
62
|
+
}
|
|
63
|
+
export function assetSearchText(cms, row, labels = new Map()) {
|
|
64
|
+
return normalizeSearch(row.filename + ' ' + assetText(cms, row, labels));
|
|
65
|
+
}
|
|
59
66
|
function excerpt(text, query) {
|
|
60
67
|
const term = query.split(' ')[0].replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
61
68
|
const position = Math.max(0, text.search(new RegExp(term, 'i')));
|
package/dist/worker/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { R2Bucket } from '@cloudflare/workers-types/index.ts';
|
|
|
2
2
|
import { type UploadSigningEnv } from './uploads.js';
|
|
3
3
|
export interface WorkerEnv extends UploadSigningEnv {
|
|
4
4
|
MEDIA: R2Bucket;
|
|
5
|
-
|
|
5
|
+
SANDCMS_WORKER_TOKEN: string;
|
|
6
6
|
}
|
|
7
7
|
declare const _default: {
|
|
8
8
|
fetch(request: Request, env: WorkerEnv): Promise<Response>;
|