dsh-artifact-studio 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +41 -2
- package/client.doc.js +241 -0
- package/client.js +564 -0
- package/cordis.patch.yml +10 -0
- package/dist/contract.js +178 -0
- package/dist/host/index.js +14 -0
- package/dist/host/lib/checks.js +52 -0
- package/dist/host/lib/design-system.js +180 -0
- package/dist/host/lib/errors.js +27 -0
- package/dist/host/lib/export.js +319 -0
- package/dist/host/lib/fsutil.js +86 -0
- package/dist/host/lib/http.js +156 -0
- package/dist/host/lib/paths.js +241 -0
- package/dist/host/lib/rpc.js +126 -0
- package/dist/host/lib/service.js +136 -0
- package/dist/host/lib/setup.js +14 -0
- package/dist/host/lib/skill.js +74 -0
- package/dist/host/lib/store.js +519 -0
- package/dist/host/lib/tools.js +418 -0
- package/dist/host/lib/types.js +1 -0
- package/dist/host/lib/validate.js +127 -0
- package/dist/host/skill-plugin.js +11 -0
- package/dist/host/tools-plugin.js +15 -0
- package/dist/render.js +111 -0
- package/icon.svg +1 -0
- package/locale/en.json +4 -0
- package/locale/zh.json +4 -0
- package/package.json +79 -4
|
@@ -0,0 +1,519 @@
|
|
|
1
|
+
import { createHash, randomBytes } from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { CONTENT_FILE, ID_PATTERN, MAX_LIST_LIMIT, MAX_TITLE, isArtifactType, isManifest, isVersionSource } from '../../contract.js';
|
|
5
|
+
import { ArtifactError, errnoCode } from './errors.js';
|
|
6
|
+
import { assertWithin, withLock, writeAtomic } from './fsutil.js';
|
|
7
|
+
import { artifactDir, assertPlainFile } from './paths.js';
|
|
8
|
+
export const DEFAULT_LIMITS = {
|
|
9
|
+
maxContentBytes: 2 * 1024 * 1024,
|
|
10
|
+
maxVersions: 50,
|
|
11
|
+
maxVersionsBytes: 20 * 1024 * 1024,
|
|
12
|
+
trashRetentionDays: 7,
|
|
13
|
+
maxScan: 500,
|
|
14
|
+
};
|
|
15
|
+
const MANIFEST = 'artifact.json';
|
|
16
|
+
const MAX_MANIFEST_BYTES = 8 * 1024;
|
|
17
|
+
const TRASH = '.trash';
|
|
18
|
+
/**
|
|
19
|
+
* A sha256 content hash with its algorithm prefix.
|
|
20
|
+
* @param text the content.
|
|
21
|
+
* @returns the hash.
|
|
22
|
+
*/
|
|
23
|
+
export function hashOf(text) {
|
|
24
|
+
return `sha256:${createHash('sha256').update(text, 'utf8').digest('hex')}`;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* A new id: scope prefix plus 16 random base-36 characters.
|
|
28
|
+
* @param scope the scope.
|
|
29
|
+
* @returns the id.
|
|
30
|
+
*/
|
|
31
|
+
export function newId(scope) {
|
|
32
|
+
const alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
|
33
|
+
const bytes = randomBytes(16);
|
|
34
|
+
let suffix = '';
|
|
35
|
+
for (const byte of bytes)
|
|
36
|
+
suffix += alphabet[byte % alphabet.length];
|
|
37
|
+
return `${scope === 'workspace' ? 'art_ws_' : 'art_lib_'}${suffix}`;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Trim and check a title.
|
|
41
|
+
* @param raw the title.
|
|
42
|
+
* @returns the clean title.
|
|
43
|
+
*/
|
|
44
|
+
export function cleanTitle(raw) {
|
|
45
|
+
const title = raw.replace(/\s+/g, ' ').trim();
|
|
46
|
+
if (title.length === 0 || title.length > MAX_TITLE) {
|
|
47
|
+
throw new ArtifactError('artifact/bad-request', `The title must be 1 to ${MAX_TITLE} characters.`, {
|
|
48
|
+
length: title.length,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
return title;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Narrow version metadata read from disk.
|
|
55
|
+
* @param value the parsed value.
|
|
56
|
+
* @returns whether it is a version entry.
|
|
57
|
+
*/
|
|
58
|
+
function isVersionEntry(value) {
|
|
59
|
+
if (value === null || typeof value !== 'object')
|
|
60
|
+
return false;
|
|
61
|
+
const record = { ...value };
|
|
62
|
+
return (typeof record.version === 'number' &&
|
|
63
|
+
typeof record.createdAt === 'string' &&
|
|
64
|
+
typeof record.contentHash === 'string' &&
|
|
65
|
+
typeof record.contentBytes === 'number' &&
|
|
66
|
+
isVersionSource(record.source));
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Apply replace-once edits to a text.
|
|
70
|
+
* @param text the current content.
|
|
71
|
+
* @param edits the edits, applied in order.
|
|
72
|
+
* @returns the edited content.
|
|
73
|
+
*/
|
|
74
|
+
export function applyEdits(text, edits) {
|
|
75
|
+
let result = text;
|
|
76
|
+
edits.forEach((edit, index) => {
|
|
77
|
+
if (edit.oldText === '') {
|
|
78
|
+
throw new ArtifactError('artifact/bad-request', `Edit ${index + 1} has an empty oldText.`);
|
|
79
|
+
}
|
|
80
|
+
const first = result.indexOf(edit.oldText);
|
|
81
|
+
if (first < 0) {
|
|
82
|
+
throw new ArtifactError('artifact/edit-not-found', `Edit ${index + 1}: oldText was not found.`, { index });
|
|
83
|
+
}
|
|
84
|
+
if (result.indexOf(edit.oldText, first + 1) >= 0) {
|
|
85
|
+
throw new ArtifactError('artifact/edit-ambiguous', `Edit ${index + 1}: oldText occurs more than once; include more surrounding text.`, { index });
|
|
86
|
+
}
|
|
87
|
+
result = result.slice(0, first) + edit.newText + result.slice(first + edit.oldText.length);
|
|
88
|
+
});
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
/** Files-only artifact storage shared by both scopes. */
|
|
92
|
+
export class ArtifactStore {
|
|
93
|
+
limits;
|
|
94
|
+
now;
|
|
95
|
+
/**
|
|
96
|
+
* @param limits store limits.
|
|
97
|
+
* @param now the clock, replaceable in tests.
|
|
98
|
+
*/
|
|
99
|
+
constructor(limits = {}, now = () => new Date()) {
|
|
100
|
+
this.limits = { ...DEFAULT_LIMITS, ...limits };
|
|
101
|
+
this.now = now;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Refuse content over the size cap.
|
|
105
|
+
* @param content the content.
|
|
106
|
+
* @returns the byte length.
|
|
107
|
+
*/
|
|
108
|
+
checkSize(content) {
|
|
109
|
+
const bytes = Buffer.byteLength(content, 'utf8');
|
|
110
|
+
if (bytes > this.limits.maxContentBytes) {
|
|
111
|
+
throw new ArtifactError('artifact/too-large', `The content is ${bytes} bytes; the limit is ${this.limits.maxContentBytes}.`, { bytes, limit: this.limits.maxContentBytes });
|
|
112
|
+
}
|
|
113
|
+
return bytes;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Read and check a manifest.
|
|
117
|
+
* @param dir the artifact directory.
|
|
118
|
+
* @returns the manifest.
|
|
119
|
+
*/
|
|
120
|
+
async readManifest(dir) {
|
|
121
|
+
const file = path.join(dir, MANIFEST);
|
|
122
|
+
if (!(await assertPlainFile(file))) {
|
|
123
|
+
throw new ArtifactError('artifact/not-found', 'The artifact has no manifest.', { id: path.basename(dir) });
|
|
124
|
+
}
|
|
125
|
+
const text = await fs.readFile(file, 'utf8');
|
|
126
|
+
if (Buffer.byteLength(text) > MAX_MANIFEST_BYTES) {
|
|
127
|
+
throw new ArtifactError('artifact/corrupt', 'The artifact manifest is too large.', { id: path.basename(dir) });
|
|
128
|
+
}
|
|
129
|
+
let parsed;
|
|
130
|
+
try {
|
|
131
|
+
parsed = JSON.parse(text);
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
throw new ArtifactError('artifact/corrupt', 'The artifact manifest is not valid JSON.', { id: path.basename(dir) });
|
|
135
|
+
}
|
|
136
|
+
if (!isManifest(parsed) || parsed.id !== path.basename(dir)) {
|
|
137
|
+
throw new ArtifactError('artifact/corrupt', 'The artifact manifest is invalid.', { id: path.basename(dir) });
|
|
138
|
+
}
|
|
139
|
+
if (parsed.contentFile !== CONTENT_FILE[parsed.type]) {
|
|
140
|
+
throw new ArtifactError('artifact/corrupt', 'The artifact manifest names an unexpected content file.', {
|
|
141
|
+
id: parsed.id,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
return parsed;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Read the content of one version, or the current content.
|
|
148
|
+
* @param dir the artifact directory.
|
|
149
|
+
* @param manifest the manifest.
|
|
150
|
+
* @param version the version, or undefined for the current content.
|
|
151
|
+
* @returns the content.
|
|
152
|
+
*/
|
|
153
|
+
async readContent(dir, manifest, version) {
|
|
154
|
+
if (version !== undefined && version !== manifest.version) {
|
|
155
|
+
const file = path.join(dir, 'versions', String(version), 'content', manifest.contentFile);
|
|
156
|
+
if (!(await assertPlainFile(file))) {
|
|
157
|
+
throw new ArtifactError('artifact/version-not-found', `Version ${version} is not stored.`, { version });
|
|
158
|
+
}
|
|
159
|
+
return fs.readFile(file, 'utf8');
|
|
160
|
+
}
|
|
161
|
+
const file = path.join(dir, 'content', manifest.contentFile);
|
|
162
|
+
if (!(await assertPlainFile(file))) {
|
|
163
|
+
throw new ArtifactError('artifact/version-not-found', `Version ${manifest.version} is not stored.`, {
|
|
164
|
+
version: manifest.version,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
const content = await fs.readFile(file, 'utf8');
|
|
168
|
+
if (hashOf(content) === manifest.contentHash)
|
|
169
|
+
return content;
|
|
170
|
+
// A crash between the content rename and the manifest rename leaves new
|
|
171
|
+
// content under the old hash; the immutable snapshot is the authority.
|
|
172
|
+
const snapshot = path.join(dir, 'versions', String(manifest.version), 'content', manifest.contentFile);
|
|
173
|
+
if ((await assertPlainFile(snapshot)) && hashOf(await fs.readFile(snapshot, 'utf8')) === manifest.contentHash) {
|
|
174
|
+
return fs.readFile(snapshot, 'utf8');
|
|
175
|
+
}
|
|
176
|
+
throw new ArtifactError('artifact/corrupt', 'The stored content does not match its recorded hash.', { id: manifest.id });
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Store one immutable snapshot of a version.
|
|
180
|
+
* @param dir the artifact directory.
|
|
181
|
+
* @param manifest the manifest of that version.
|
|
182
|
+
* @param content its content.
|
|
183
|
+
*/
|
|
184
|
+
async snapshot(root, dir, manifest, content) {
|
|
185
|
+
const versionDir = path.join(dir, 'versions', String(manifest.version));
|
|
186
|
+
await assertWithin(root, versionDir);
|
|
187
|
+
await fs.mkdir(path.join(versionDir, 'content'), { recursive: true });
|
|
188
|
+
await writeAtomic(path.join(versionDir, 'content', manifest.contentFile), content);
|
|
189
|
+
const entry = {
|
|
190
|
+
version: manifest.version,
|
|
191
|
+
createdAt: manifest.updatedAt,
|
|
192
|
+
contentHash: manifest.contentHash,
|
|
193
|
+
contentBytes: manifest.contentBytes,
|
|
194
|
+
source: manifest.source,
|
|
195
|
+
};
|
|
196
|
+
await writeAtomic(path.join(versionDir, 'meta.json'), `${JSON.stringify(entry, null, 2)}\n`);
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Write the next version: snapshot, then content, then the manifest last, so
|
|
200
|
+
* the manifest never names a version whose content is missing.
|
|
201
|
+
* @param dir the artifact directory.
|
|
202
|
+
* @param manifest the next manifest.
|
|
203
|
+
* @param content the next content.
|
|
204
|
+
*/
|
|
205
|
+
async commit(dir, manifest, content, root) {
|
|
206
|
+
await assertWithin(root, dir);
|
|
207
|
+
await assertPlainFile(path.join(dir, 'content', manifest.contentFile));
|
|
208
|
+
await assertPlainFile(path.join(dir, MANIFEST));
|
|
209
|
+
await this.snapshot(root, dir, manifest, content);
|
|
210
|
+
const contentFile = path.join(dir, 'content', manifest.contentFile);
|
|
211
|
+
const manifestFile = path.join(dir, MANIFEST);
|
|
212
|
+
await assertWithin(root, contentFile);
|
|
213
|
+
await assertWithin(root, manifestFile);
|
|
214
|
+
await writeAtomic(contentFile, content);
|
|
215
|
+
await writeAtomic(manifestFile, `${JSON.stringify(manifest, null, 2)}\n`);
|
|
216
|
+
try {
|
|
217
|
+
await this.prune(dir, manifest.version);
|
|
218
|
+
}
|
|
219
|
+
catch (error) {
|
|
220
|
+
// The version is saved; a prune failure must not make the caller retry a write that succeeded.
|
|
221
|
+
console.warn('[dsh-artifact-studio] prune failed', manifest.id, error instanceof Error ? error.message : error);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Drop the oldest snapshots past the count and byte caps. The current
|
|
226
|
+
* version is never dropped.
|
|
227
|
+
* @param dir the artifact directory.
|
|
228
|
+
* @param current the current version.
|
|
229
|
+
*/
|
|
230
|
+
async prune(dir, current) {
|
|
231
|
+
const entries = await this.versionEntries(dir);
|
|
232
|
+
const newestFirst = [...entries].sort((a, b) => b.version - a.version);
|
|
233
|
+
let total = 0;
|
|
234
|
+
const drop = [];
|
|
235
|
+
newestFirst.forEach((entry, index) => {
|
|
236
|
+
total += entry.contentBytes;
|
|
237
|
+
if (entry.version === current)
|
|
238
|
+
return;
|
|
239
|
+
// A snapshot newer than the manifest is a crash leftover, not history.
|
|
240
|
+
if (entry.version > current || index >= this.limits.maxVersions || total > this.limits.maxVersionsBytes) {
|
|
241
|
+
drop.push(entry.version);
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
for (const version of drop) {
|
|
245
|
+
const target = path.join(dir, 'versions', String(version));
|
|
246
|
+
await assertWithin(dir, target);
|
|
247
|
+
await fs.rm(target, { recursive: true, force: true });
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Read every snapshot's metadata.
|
|
252
|
+
* @param dir the artifact directory.
|
|
253
|
+
* @returns the entries, oldest first.
|
|
254
|
+
*/
|
|
255
|
+
async versionEntries(dir) {
|
|
256
|
+
let names;
|
|
257
|
+
try {
|
|
258
|
+
names = await fs.readdir(path.join(dir, 'versions'));
|
|
259
|
+
}
|
|
260
|
+
catch (error) {
|
|
261
|
+
if (errnoCode(error) === 'ENOENT')
|
|
262
|
+
return [];
|
|
263
|
+
throw error;
|
|
264
|
+
}
|
|
265
|
+
const entries = [];
|
|
266
|
+
for (const name of names) {
|
|
267
|
+
if (!/^[1-9][0-9]*$/.test(name))
|
|
268
|
+
continue;
|
|
269
|
+
const file = path.join(dir, 'versions', name, 'meta.json');
|
|
270
|
+
try {
|
|
271
|
+
if (!(await assertPlainFile(file)))
|
|
272
|
+
continue;
|
|
273
|
+
const parsed = JSON.parse(await fs.readFile(file, 'utf8'));
|
|
274
|
+
if (isVersionEntry(parsed) && parsed.version === Number(name))
|
|
275
|
+
entries.push(parsed);
|
|
276
|
+
}
|
|
277
|
+
catch (error) {
|
|
278
|
+
if (error instanceof SyntaxError)
|
|
279
|
+
continue;
|
|
280
|
+
throw error;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
return entries.sort((a, b) => a.version - b.version);
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Create an artifact at version 1.
|
|
287
|
+
* @param root the scope root.
|
|
288
|
+
* @param input type, title, content, and source.
|
|
289
|
+
* @returns the manifest.
|
|
290
|
+
*/
|
|
291
|
+
async create(root, input) {
|
|
292
|
+
// The type arrives from the route, so it is checked at runtime, not only by its declared type.
|
|
293
|
+
const declared = input.type;
|
|
294
|
+
if (!isArtifactType(declared)) {
|
|
295
|
+
throw new ArtifactError('artifact/bad-request', 'Unknown artifact type.', { type: String(declared) });
|
|
296
|
+
}
|
|
297
|
+
const type = declared;
|
|
298
|
+
const title = cleanTitle(input.title);
|
|
299
|
+
const bytes = this.checkSize(input.content);
|
|
300
|
+
const id = newId(root.scope);
|
|
301
|
+
const dir = path.join(root.root, id);
|
|
302
|
+
const now = this.now().toISOString();
|
|
303
|
+
const manifest = {
|
|
304
|
+
schemaVersion: 1,
|
|
305
|
+
id,
|
|
306
|
+
scope: root.scope,
|
|
307
|
+
type,
|
|
308
|
+
title,
|
|
309
|
+
createdAt: now,
|
|
310
|
+
updatedAt: now,
|
|
311
|
+
version: 1,
|
|
312
|
+
contentFile: CONTENT_FILE[type],
|
|
313
|
+
contentBytes: bytes,
|
|
314
|
+
contentHash: hashOf(input.content),
|
|
315
|
+
source: input.source,
|
|
316
|
+
};
|
|
317
|
+
try {
|
|
318
|
+
await assertWithin(root.root, dir);
|
|
319
|
+
await fs.mkdir(path.join(dir, 'content'), { recursive: true });
|
|
320
|
+
await this.commit(dir, manifest, input.content, root.root);
|
|
321
|
+
}
|
|
322
|
+
catch (error) {
|
|
323
|
+
await fs.rm(dir, { recursive: true, force: true });
|
|
324
|
+
if (error instanceof ArtifactError)
|
|
325
|
+
throw error;
|
|
326
|
+
throw new ArtifactError('artifact/storage-failure', 'The artifact could not be written.', {
|
|
327
|
+
errno: errnoCode(error),
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
return manifest;
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Read the manifest and content of the current or an earlier version.
|
|
334
|
+
* @param root the scope root.
|
|
335
|
+
* @param id the artifact id.
|
|
336
|
+
* @param version an optional earlier version.
|
|
337
|
+
* @returns the manifest, content, and version read.
|
|
338
|
+
*/
|
|
339
|
+
async read(root, id, version) {
|
|
340
|
+
const dir = await artifactDir(root, id);
|
|
341
|
+
const manifest = await this.readManifest(dir);
|
|
342
|
+
if (version !== undefined && (version < 1 || version > manifest.version)) {
|
|
343
|
+
throw new ArtifactError('artifact/version-not-found', `Version ${version} does not exist.`, {
|
|
344
|
+
version,
|
|
345
|
+
current: manifest.version,
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
const content = await this.readContent(dir, manifest, version);
|
|
349
|
+
return { manifest, content, version: version ?? manifest.version };
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Accept a write only when it names the current version.
|
|
353
|
+
* @param root the scope root.
|
|
354
|
+
* @param id the artifact id.
|
|
355
|
+
* @param input the update.
|
|
356
|
+
* @returns the new manifest.
|
|
357
|
+
*/
|
|
358
|
+
async update(root, id, input) {
|
|
359
|
+
const dir = await artifactDir(root, id);
|
|
360
|
+
return withLock(dir, async () => {
|
|
361
|
+
const current = await this.readManifest(dir);
|
|
362
|
+
if (input.baseVersion !== current.version) {
|
|
363
|
+
throw new ArtifactError('artifact/stale-version', `The artifact is at version ${current.version}, not ${input.baseVersion}. Read it again and reapply the change.`, { current: current.version, baseVersion: input.baseVersion });
|
|
364
|
+
}
|
|
365
|
+
const title = input.title === undefined ? current.title : cleanTitle(input.title);
|
|
366
|
+
let content;
|
|
367
|
+
if (input.content !== undefined && input.edits !== undefined) {
|
|
368
|
+
throw new ArtifactError('artifact/bad-request', 'Send either content or edits, not both.');
|
|
369
|
+
}
|
|
370
|
+
else if (input.content !== undefined) {
|
|
371
|
+
content = input.content;
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
const existing = await this.readContent(dir, current);
|
|
375
|
+
content = input.edits === undefined ? existing : applyEdits(existing, input.edits);
|
|
376
|
+
}
|
|
377
|
+
return this.write(root.root, dir, current, content, title, input.source);
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Append a version that repeats an earlier version's content.
|
|
382
|
+
* @param root the scope root.
|
|
383
|
+
* @param id the artifact id.
|
|
384
|
+
* @param version the version to restore.
|
|
385
|
+
* @param source who restored it.
|
|
386
|
+
* @param baseVersion the version the caller saw, if it checks.
|
|
387
|
+
* @returns the new manifest.
|
|
388
|
+
*/
|
|
389
|
+
async restore(root, id, version, source, baseVersion) {
|
|
390
|
+
const dir = await artifactDir(root, id);
|
|
391
|
+
return withLock(dir, async () => {
|
|
392
|
+
const current = await this.readManifest(dir);
|
|
393
|
+
if (baseVersion !== undefined && baseVersion !== current.version) {
|
|
394
|
+
throw new ArtifactError('artifact/stale-version', `The artifact is at version ${current.version}, not ${baseVersion}.`, { current: current.version, baseVersion });
|
|
395
|
+
}
|
|
396
|
+
if (version < 1 || version >= current.version) {
|
|
397
|
+
throw new ArtifactError('artifact/version-not-found', `Version ${version} cannot be restored.`, { version });
|
|
398
|
+
}
|
|
399
|
+
const content = await this.readContent(dir, current, version);
|
|
400
|
+
return this.write(root.root, dir, current, content, current.title, { ...source, action: 'restore', restoredFrom: version });
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Build and commit the next manifest.
|
|
405
|
+
* @param dir the artifact directory.
|
|
406
|
+
* @param current the current manifest.
|
|
407
|
+
* @param content the next content.
|
|
408
|
+
* @param title the next title.
|
|
409
|
+
* @param source who wrote it.
|
|
410
|
+
* @returns the next manifest.
|
|
411
|
+
*/
|
|
412
|
+
async write(root, dir, current, content, title, source) {
|
|
413
|
+
const bytes = this.checkSize(content);
|
|
414
|
+
const next = {
|
|
415
|
+
...current,
|
|
416
|
+
title,
|
|
417
|
+
version: current.version + 1,
|
|
418
|
+
updatedAt: this.now().toISOString(),
|
|
419
|
+
contentBytes: bytes,
|
|
420
|
+
contentHash: hashOf(content),
|
|
421
|
+
source,
|
|
422
|
+
};
|
|
423
|
+
try {
|
|
424
|
+
await this.commit(dir, next, content, root);
|
|
425
|
+
}
|
|
426
|
+
catch (error) {
|
|
427
|
+
if (error instanceof ArtifactError)
|
|
428
|
+
throw error;
|
|
429
|
+
throw new ArtifactError('artifact/storage-failure', 'The artifact could not be written.', {
|
|
430
|
+
errno: errnoCode(error),
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
return next;
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* List the stored versions, newest first.
|
|
437
|
+
* @param root the scope root.
|
|
438
|
+
* @param id the artifact id.
|
|
439
|
+
* @returns the versions.
|
|
440
|
+
*/
|
|
441
|
+
async history(root, id) {
|
|
442
|
+
const dir = await artifactDir(root, id);
|
|
443
|
+
const manifest = await this.readManifest(dir);
|
|
444
|
+
return (await this.versionEntries(dir))
|
|
445
|
+
.filter((entry) => entry.version <= manifest.version)
|
|
446
|
+
.reverse();
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* List artifacts by `updatedAt`, newest first. A corrupt manifest skips its
|
|
450
|
+
* row instead of failing the list.
|
|
451
|
+
* @param root the scope root.
|
|
452
|
+
* @param input filters and paging.
|
|
453
|
+
* @returns one page.
|
|
454
|
+
*/
|
|
455
|
+
async list(root, input = {}) {
|
|
456
|
+
const names = (await fs.readdir(root.root, { withFileTypes: true }))
|
|
457
|
+
.filter((entry) => entry.isDirectory() && ID_PATTERN.test(entry.name))
|
|
458
|
+
.map((entry) => entry.name);
|
|
459
|
+
const truncated = names.length > this.limits.maxScan;
|
|
460
|
+
const manifests = [];
|
|
461
|
+
for (const name of names.slice(0, this.limits.maxScan)) {
|
|
462
|
+
try {
|
|
463
|
+
const manifest = await this.readManifest(path.join(root.root, name));
|
|
464
|
+
if (manifest.scope === root.scope)
|
|
465
|
+
manifests.push(manifest);
|
|
466
|
+
}
|
|
467
|
+
catch (error) {
|
|
468
|
+
if (!(error instanceof ArtifactError))
|
|
469
|
+
throw error;
|
|
470
|
+
console.warn('[dsh-artifact-studio] skipped artifact', name, error.code);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
const query = input.query?.trim().toLowerCase() ?? '';
|
|
474
|
+
const filtered = manifests
|
|
475
|
+
.filter((item) => input.type === undefined || item.type === input.type)
|
|
476
|
+
.filter((item) => query === '' || item.title.toLowerCase().includes(query))
|
|
477
|
+
.sort((a, b) => (a.updatedAt < b.updatedAt ? 1 : a.updatedAt > b.updatedAt ? -1 : 0));
|
|
478
|
+
const offset = Math.max(0, Math.floor(input.offset ?? 0));
|
|
479
|
+
const limit = Math.min(MAX_LIST_LIMIT, Math.max(1, Math.floor(input.limit ?? MAX_LIST_LIMIT)));
|
|
480
|
+
const items = filtered.slice(offset, offset + limit);
|
|
481
|
+
return { items, total: filtered.length, hasMore: offset + items.length < filtered.length, truncated };
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* Move an artifact into the scope's trash, then sweep trash entries past retention.
|
|
485
|
+
* @param root the scope root.
|
|
486
|
+
* @param id the artifact id.
|
|
487
|
+
* @returns the trash location.
|
|
488
|
+
*/
|
|
489
|
+
async remove(root, id) {
|
|
490
|
+
const dir = await artifactDir(root, id);
|
|
491
|
+
return withLock(dir, async () => {
|
|
492
|
+
await this.readManifest(dir);
|
|
493
|
+
const trashRoot = path.join(root.root, TRASH);
|
|
494
|
+
await assertWithin(root.root, trashRoot);
|
|
495
|
+
await fs.mkdir(trashRoot, { recursive: true });
|
|
496
|
+
const target = path.join(trashRoot, `${id}-${this.now().getTime()}`);
|
|
497
|
+
await assertWithin(root.root, target);
|
|
498
|
+
await fs.rename(dir, target);
|
|
499
|
+
await this.sweepTrash(trashRoot);
|
|
500
|
+
return { trashed: true, trashPath: target };
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* Remove trash entries older than the retention window.
|
|
505
|
+
* @param trashRoot the trash directory.
|
|
506
|
+
*/
|
|
507
|
+
async sweepTrash(trashRoot) {
|
|
508
|
+
const cutoff = this.now().getTime() - this.limits.trashRetentionDays * 24 * 60 * 60 * 1000;
|
|
509
|
+
for (const entry of await fs.readdir(trashRoot, { withFileTypes: true })) {
|
|
510
|
+
// The trash time is in the name because a rename does not reliably touch mtime.
|
|
511
|
+
const match = /-(\d{13,})$/.exec(entry.name);
|
|
512
|
+
if (!entry.isDirectory() || entry.isSymbolicLink() || match === null)
|
|
513
|
+
continue;
|
|
514
|
+
const target = path.join(trashRoot, entry.name);
|
|
515
|
+
if (Number(match[1]) < cutoff)
|
|
516
|
+
await fs.rm(target, { recursive: true, force: true });
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
}
|