@playcraft/cli 0.1.49-beta → 0.1.50-beta
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/dist/workspace-runtime/previews/publish.js +16 -7
- package/dist/workspace-runtime/previews/runtime-preview.js +177 -0
- package/dist/workspace-runtime/serve.js +102 -4
- package/dist/workspace-runtime/server/asset-transfers.js +63 -0
- package/dist/workspace-runtime/server/dispatch.js +282 -43
- package/dist/workspace-runtime/server/http.js +32 -8
- package/dist/workspace-runtime/workspaces/asset-transfer-dependencies.js +81 -0
- package/dist/workspace-runtime/workspaces/asset-transfer-publish.js +434 -0
- package/dist/workspace-runtime/workspaces/asset-transfer-storage.js +389 -0
- package/dist/workspace-runtime/workspaces/directory-mutations.js +238 -0
- package/dist/workspace-runtime/workspaces/directory-revisions.js +56 -0
- package/dist/workspace-runtime/workspaces/disk-snapshot.js +3 -3
- package/dist/workspace-runtime/workspaces/json5-edit.js +144 -1
- package/dist/workspace-runtime/workspaces/parameter-agent-inputs.js +87 -0
- package/dist/workspace-runtime/workspaces/parameter-agent-protocol.js +29 -0
- package/dist/workspace-runtime/workspaces/parameter-agent-store.js +58 -0
- package/dist/workspace-runtime/workspaces/parameter-agent.js +282 -0
- package/dist/workspace-runtime/workspaces/parameters.js +77 -54
- package/package.json +3 -3
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
+
import { constants } from 'node:fs';
|
|
3
|
+
import { link, lstat, mkdir, open, readdir, realpath, rename, rm, unlink } from 'node:fs/promises';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { ASSET_TRANSFER_BODY_MAX_BYTES, ASSET_TRANSFER_CHUNK_BYTES, ASSET_TRANSFER_FILE_MAX_BYTES, assetBundleReceiptSchema, assetTransferChunkSchema, assetTransferManifestSchema, assetTransferStatusSchema, hashAssetTransferManifest, isAssetBundleReceiptForManifest, } from '@playcraft/common/cloud-workspace/runtime';
|
|
6
|
+
export class AssetTransferError extends Error {
|
|
7
|
+
code;
|
|
8
|
+
status;
|
|
9
|
+
constructor(code, status = 409) {
|
|
10
|
+
super(code);
|
|
11
|
+
this.code = code;
|
|
12
|
+
this.status = status;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
const locks = new Map();
|
|
16
|
+
export const ASSET_TRANSFER_MAX_ACTIVE = 4;
|
|
17
|
+
export const ASSET_TRANSFER_TTL_MS = 24 * 60 * 60 * 1000;
|
|
18
|
+
export const assetTransferMissing = (error) => error?.code === 'ENOENT';
|
|
19
|
+
const failPath = () => { throw new AssetTransferError('invalid_workspace_path', 400); };
|
|
20
|
+
/** Every path component is checked before use, including the private transfer directory. */
|
|
21
|
+
export async function safeAssetTransferDirectory(root, path = '', create = false) {
|
|
22
|
+
const rootStat = await lstat(root);
|
|
23
|
+
if (!rootStat.isDirectory() || rootStat.isSymbolicLink() || await realpath(root) !== root)
|
|
24
|
+
failPath();
|
|
25
|
+
let current = root;
|
|
26
|
+
for (const part of path.split('/').filter(Boolean)) {
|
|
27
|
+
if (part === '.' || part === '..' || /[\\:\x00-\x1f\x7f]/.test(part))
|
|
28
|
+
failPath();
|
|
29
|
+
current = join(current, part);
|
|
30
|
+
if (create)
|
|
31
|
+
await mkdir(current, { mode: 0o700 }).catch(error => { if (error.code !== 'EEXIST')
|
|
32
|
+
throw error; });
|
|
33
|
+
const stat = await lstat(current);
|
|
34
|
+
if (!stat.isDirectory() || stat.isSymbolicLink() || await realpath(current) !== current)
|
|
35
|
+
failPath();
|
|
36
|
+
}
|
|
37
|
+
return current;
|
|
38
|
+
}
|
|
39
|
+
export async function syncAssetTransferDirectory(path) {
|
|
40
|
+
const handle = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
41
|
+
try {
|
|
42
|
+
await handle.sync();
|
|
43
|
+
}
|
|
44
|
+
finally {
|
|
45
|
+
await handle.close();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export async function hashAssetTransferFile(path, limit = ASSET_TRANSFER_FILE_MAX_BYTES, allowLinks = false, expectedChunks) {
|
|
49
|
+
const handle = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
50
|
+
try {
|
|
51
|
+
const before = await handle.stat();
|
|
52
|
+
if (!before.isFile() || !allowLinks && before.nlink !== 1 || before.size > limit)
|
|
53
|
+
failPath();
|
|
54
|
+
const hash = createHash('sha256');
|
|
55
|
+
let chunkHash = createHash('sha256'), chunkBytes = 0, chunkIndex = 0;
|
|
56
|
+
const buffer = Buffer.allocUnsafe(64 * 1024);
|
|
57
|
+
let size = 0;
|
|
58
|
+
for (;;) {
|
|
59
|
+
const { bytesRead } = await handle.read(buffer, 0, buffer.length, size);
|
|
60
|
+
if (!bytesRead)
|
|
61
|
+
break;
|
|
62
|
+
size += bytesRead;
|
|
63
|
+
if (size > limit)
|
|
64
|
+
throw new AssetTransferError('asset_transfer_too_large', 413);
|
|
65
|
+
hash.update(buffer.subarray(0, bytesRead));
|
|
66
|
+
if (expectedChunks) {
|
|
67
|
+
let offset = 0;
|
|
68
|
+
while (offset < bytesRead) {
|
|
69
|
+
const length = Math.min(bytesRead - offset, ASSET_TRANSFER_CHUNK_BYTES - chunkBytes);
|
|
70
|
+
chunkHash.update(buffer.subarray(offset, offset + length));
|
|
71
|
+
chunkBytes += length;
|
|
72
|
+
offset += length;
|
|
73
|
+
if (chunkBytes === ASSET_TRANSFER_CHUNK_BYTES) {
|
|
74
|
+
if (chunkHash.digest('hex') !== expectedChunks[chunkIndex++])
|
|
75
|
+
throw new AssetTransferError('asset_transfer_content_mismatch');
|
|
76
|
+
chunkHash = createHash('sha256');
|
|
77
|
+
chunkBytes = 0;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const after = await handle.stat();
|
|
83
|
+
if (size !== before.size || before.size !== after.size || before.mtimeMs !== after.mtimeMs || before.ctimeMs !== after.ctimeMs)
|
|
84
|
+
throw new AssetTransferError('content_conflict');
|
|
85
|
+
if (expectedChunks && (chunkBytes && chunkHash.digest('hex') !== expectedChunks[chunkIndex++] || chunkIndex !== expectedChunks.length))
|
|
86
|
+
throw new AssetTransferError('asset_transfer_content_mismatch');
|
|
87
|
+
return { dev: before.dev, ino: before.ino, size, contentHash: hash.digest('hex') };
|
|
88
|
+
}
|
|
89
|
+
finally {
|
|
90
|
+
await handle.close();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
export async function saveAssetTransfer(transfer) {
|
|
94
|
+
await safeAssetTransferDirectory(transfer.directory);
|
|
95
|
+
const temporary = join(transfer.directory, `record-${randomUUID()}.tmp`);
|
|
96
|
+
const handle = await open(temporary, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);
|
|
97
|
+
try {
|
|
98
|
+
await handle.writeFile(JSON.stringify(transfer.record));
|
|
99
|
+
await handle.sync();
|
|
100
|
+
}
|
|
101
|
+
finally {
|
|
102
|
+
await handle.close();
|
|
103
|
+
}
|
|
104
|
+
await safeAssetTransferDirectory(transfer.directory);
|
|
105
|
+
await rename(temporary, join(transfer.directory, 'record.json'));
|
|
106
|
+
await syncAssetTransferDirectory(transfer.directory);
|
|
107
|
+
}
|
|
108
|
+
async function readRecord(directory) {
|
|
109
|
+
const handle = await open(join(directory, 'record.json'), constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
110
|
+
try {
|
|
111
|
+
const stat = await handle.stat();
|
|
112
|
+
if (!stat.isFile() || ![1, 2].includes(stat.nlink) || stat.size > ASSET_TRANSFER_BODY_MAX_BYTES * 4)
|
|
113
|
+
throw new AssetTransferError('invalid_asset_transfer', 400);
|
|
114
|
+
const value = JSON.parse(await handle.readFile('utf8'));
|
|
115
|
+
const manifest = assetTransferManifestSchema.parse(value.manifest);
|
|
116
|
+
if (value.version !== 1 || value.manifestHash !== hashAssetTransferManifest(manifest)
|
|
117
|
+
|| !['uploading', 'publishing', 'completed'].includes(value.status))
|
|
118
|
+
throw new AssetTransferError('invalid_asset_transfer', 400);
|
|
119
|
+
if (value.receipt)
|
|
120
|
+
value.receipt = assetBundleReceiptSchema.parse(value.receipt);
|
|
121
|
+
if (!Number.isFinite(value.createdAt) || value.status === 'completed'
|
|
122
|
+
&& !isAssetBundleReceiptForManifest(manifest, value.manifestHash, value.receipt)
|
|
123
|
+
|| value.status !== 'completed' && value.receipt)
|
|
124
|
+
throw new AssetTransferError('invalid_asset_transfer', 400);
|
|
125
|
+
if (stat.nlink === 2) {
|
|
126
|
+
// Creation uses an exclusive link so the same identifier can never replace a session.
|
|
127
|
+
// A crash before unlinking that private temporary name must remain resumable.
|
|
128
|
+
let repaired = false;
|
|
129
|
+
for (const name of await readdir(directory)) {
|
|
130
|
+
if (!/^create-[a-f0-9-]{36}\.tmp$/i.test(name))
|
|
131
|
+
continue;
|
|
132
|
+
const alias = join(directory, name), linked = await lstat(alias);
|
|
133
|
+
if (!linked.isFile() || linked.isSymbolicLink() || linked.dev !== stat.dev || linked.ino !== stat.ino || linked.nlink !== 2)
|
|
134
|
+
continue;
|
|
135
|
+
await unlink(alias);
|
|
136
|
+
await syncAssetTransferDirectory(directory);
|
|
137
|
+
repaired = true;
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
if (!repaired || (await handle.stat()).nlink !== 1)
|
|
141
|
+
throw new AssetTransferError('invalid_asset_transfer', 400);
|
|
142
|
+
}
|
|
143
|
+
value.manifest = manifest;
|
|
144
|
+
return value;
|
|
145
|
+
}
|
|
146
|
+
finally {
|
|
147
|
+
await handle.close();
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
function validId(id) {
|
|
151
|
+
if (!/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(id))
|
|
152
|
+
throw new AssetTransferError('invalid_asset_transfer', 400);
|
|
153
|
+
}
|
|
154
|
+
export async function withAssetTransfer(root, scope, id, action) {
|
|
155
|
+
validId(id);
|
|
156
|
+
const key = `${root}\0${id}`;
|
|
157
|
+
const previous = locks.get(key) ?? Promise.resolve();
|
|
158
|
+
const next = previous.catch(() => undefined).then(async () => {
|
|
159
|
+
const rootReal = await realpath(root);
|
|
160
|
+
let directory;
|
|
161
|
+
try {
|
|
162
|
+
directory = await safeAssetTransferDirectory(rootReal, `.playcraft-temp/asset-transfers/${id}`);
|
|
163
|
+
}
|
|
164
|
+
catch (error) {
|
|
165
|
+
if (assetTransferMissing(error))
|
|
166
|
+
throw new AssetTransferError('asset_transfer_not_found', 404);
|
|
167
|
+
throw error;
|
|
168
|
+
}
|
|
169
|
+
let record;
|
|
170
|
+
try {
|
|
171
|
+
record = await readRecord(directory);
|
|
172
|
+
}
|
|
173
|
+
catch (error) {
|
|
174
|
+
if (assetTransferMissing(error))
|
|
175
|
+
throw new AssetTransferError('asset_transfer_not_found', 404);
|
|
176
|
+
throw error;
|
|
177
|
+
}
|
|
178
|
+
if (record.manifest.transferId !== id || Object.keys(scope).some(key => record.scope?.[key] !== scope[key]))
|
|
179
|
+
throw new AssetTransferError('asset_transfer_not_found', 404);
|
|
180
|
+
if (record.status === 'uploading' && !record.publication && Date.now() - record.createdAt > ASSET_TRANSFER_TTL_MS) {
|
|
181
|
+
await rm(directory, { recursive: true });
|
|
182
|
+
throw new AssetTransferError('asset_transfer_expired', 410);
|
|
183
|
+
}
|
|
184
|
+
return action({ root: rootReal, directory, record });
|
|
185
|
+
});
|
|
186
|
+
locks.set(key, next);
|
|
187
|
+
try {
|
|
188
|
+
return await next;
|
|
189
|
+
}
|
|
190
|
+
finally {
|
|
191
|
+
if (locks.get(key) === next)
|
|
192
|
+
locks.delete(key);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
async function saveProgress(transfer, files) {
|
|
196
|
+
const temporary = join(transfer.directory, `progress-${randomUUID()}.tmp`);
|
|
197
|
+
const handle = await open(temporary, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);
|
|
198
|
+
try {
|
|
199
|
+
await handle.writeFile(JSON.stringify(files));
|
|
200
|
+
await handle.sync();
|
|
201
|
+
}
|
|
202
|
+
finally {
|
|
203
|
+
await handle.close();
|
|
204
|
+
}
|
|
205
|
+
await safeAssetTransferDirectory(transfer.directory);
|
|
206
|
+
await rename(temporary, join(transfer.directory, 'progress.json'));
|
|
207
|
+
await syncAssetTransferDirectory(transfer.directory);
|
|
208
|
+
}
|
|
209
|
+
async function readProgress(transfer) {
|
|
210
|
+
try {
|
|
211
|
+
const handle = await open(join(transfer.directory, 'progress.json'), constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
212
|
+
try {
|
|
213
|
+
const stat = await handle.stat();
|
|
214
|
+
if (!stat.isFile() || stat.nlink !== 1 || stat.size > 256 * 1024)
|
|
215
|
+
return undefined;
|
|
216
|
+
const files = JSON.parse(await handle.readFile('utf8'));
|
|
217
|
+
if (!Array.isArray(files) || files.length !== transfer.record.manifest.files.length
|
|
218
|
+
|| files.some((file, index) => file.fileIndex !== index || !Array.isArray(file.receivedChunks)
|
|
219
|
+
|| file.receivedChunks.some(chunk => !Number.isInteger(chunk) || chunk < 0 || chunk >= transfer.record.manifest.files[index].chunkHashes.length)))
|
|
220
|
+
return undefined;
|
|
221
|
+
return files;
|
|
222
|
+
}
|
|
223
|
+
finally {
|
|
224
|
+
await handle.close();
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
catch {
|
|
228
|
+
return undefined;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
export async function assetTransferView(transfer, scan = true) {
|
|
232
|
+
const { record } = transfer;
|
|
233
|
+
let files = record.status === 'completed'
|
|
234
|
+
? record.manifest.files.map((file, fileIndex) => ({ fileIndex, receivedChunks: file.chunkHashes.map((_, index) => index) }))
|
|
235
|
+
: scan ? undefined : await readProgress(transfer);
|
|
236
|
+
if (!files) {
|
|
237
|
+
files = [];
|
|
238
|
+
for (const [fileIndex, file] of record.manifest.files.entries()) {
|
|
239
|
+
const receivedChunks = [];
|
|
240
|
+
for (const [chunkIndex, expected] of file.chunkHashes.entries()) {
|
|
241
|
+
try {
|
|
242
|
+
await safeAssetTransferDirectory(transfer.directory, 'chunks');
|
|
243
|
+
const chunk = await hashAssetTransferFile(join(transfer.directory, 'chunks', `${fileIndex}-${chunkIndex}`), ASSET_TRANSFER_CHUNK_BYTES);
|
|
244
|
+
const size = Math.min(ASSET_TRANSFER_CHUNK_BYTES, file.size - chunkIndex * ASSET_TRANSFER_CHUNK_BYTES);
|
|
245
|
+
if (chunk.contentHash === expected && chunk.size === size)
|
|
246
|
+
receivedChunks.push(chunkIndex);
|
|
247
|
+
}
|
|
248
|
+
catch (error) {
|
|
249
|
+
if (!assetTransferMissing(error))
|
|
250
|
+
throw error;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
files.push({ fileIndex, receivedChunks });
|
|
254
|
+
}
|
|
255
|
+
await saveProgress(transfer, files);
|
|
256
|
+
}
|
|
257
|
+
const ready = files.every(file => file.receivedChunks.length === record.manifest.files[file.fileIndex].chunkHashes.length);
|
|
258
|
+
return assetTransferStatusSchema.parse({ transferId: record.manifest.transferId, manifestHash: record.manifestHash,
|
|
259
|
+
status: record.status === 'uploading' ? ready ? 'ready' : 'uploading' : record.status, files,
|
|
260
|
+
...(record.receipt ? { receipt: record.receipt } : {}) });
|
|
261
|
+
}
|
|
262
|
+
async function createAssetTransferUnlocked(root, scope, input) {
|
|
263
|
+
const manifest = assetTransferManifestSchema.parse(input);
|
|
264
|
+
if ([...manifest.files.map(file => file.path), ...(manifest.directories ?? [])].some(path => /[\x00-\x1f\x7f]/.test(path)))
|
|
265
|
+
failPath();
|
|
266
|
+
const rootReal = await realpath(root);
|
|
267
|
+
const parent = await safeAssetTransferDirectory(rootReal, '.playcraft-temp/asset-transfers', true);
|
|
268
|
+
const present = await lstat(join(parent, manifest.transferId)).catch(error => { if (assetTransferMissing(error))
|
|
269
|
+
return undefined; throw error; });
|
|
270
|
+
if (!present) {
|
|
271
|
+
let active = 0;
|
|
272
|
+
for (const item of await readdir(parent, { withFileTypes: true })) {
|
|
273
|
+
if (!item.isDirectory())
|
|
274
|
+
continue;
|
|
275
|
+
try {
|
|
276
|
+
const directory = await safeAssetTransferDirectory(parent, item.name);
|
|
277
|
+
const record = await readRecord(directory);
|
|
278
|
+
if (record.status === 'uploading' && !record.publication && Date.now() - record.createdAt > ASSET_TRANSFER_TTL_MS) {
|
|
279
|
+
await rm(directory, { recursive: true });
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
if (record.status !== 'completed')
|
|
283
|
+
active++;
|
|
284
|
+
}
|
|
285
|
+
catch {
|
|
286
|
+
active++;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
if (active >= ASSET_TRANSFER_MAX_ACTIVE)
|
|
290
|
+
throw new AssetTransferError('asset_transfer_session_limit');
|
|
291
|
+
}
|
|
292
|
+
const directory = await safeAssetTransferDirectory(rootReal, `.playcraft-temp/asset-transfers/${manifest.transferId}`, true);
|
|
293
|
+
const record = { version: 1, createdAt: Date.now(), scope, manifest, manifestHash: hashAssetTransferManifest(manifest), status: 'uploading' };
|
|
294
|
+
const temporary = join(directory, `create-${randomUUID()}.tmp`);
|
|
295
|
+
const handle = await open(temporary, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);
|
|
296
|
+
try {
|
|
297
|
+
await handle.writeFile(JSON.stringify(record));
|
|
298
|
+
await handle.sync();
|
|
299
|
+
}
|
|
300
|
+
finally {
|
|
301
|
+
await handle.close();
|
|
302
|
+
}
|
|
303
|
+
try {
|
|
304
|
+
await link(temporary, join(directory, 'record.json'));
|
|
305
|
+
await syncAssetTransferDirectory(directory);
|
|
306
|
+
}
|
|
307
|
+
catch (error) {
|
|
308
|
+
if (error.code !== 'EEXIST')
|
|
309
|
+
throw error;
|
|
310
|
+
}
|
|
311
|
+
finally {
|
|
312
|
+
await unlink(temporary).catch(() => undefined);
|
|
313
|
+
}
|
|
314
|
+
return withAssetTransfer(rootReal, scope, manifest.transferId, async (transfer) => {
|
|
315
|
+
if (transfer.record.manifestHash !== record.manifestHash)
|
|
316
|
+
throw new AssetTransferError('asset_transfer_manifest_conflict');
|
|
317
|
+
return assetTransferView(transfer);
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
export async function createAssetTransfer(root, scope, input) {
|
|
321
|
+
const key = `${await realpath(root)}\0create`;
|
|
322
|
+
const previous = locks.get(key) ?? Promise.resolve();
|
|
323
|
+
const next = previous.catch(() => undefined).then(() => createAssetTransferUnlocked(root, scope, input));
|
|
324
|
+
locks.set(key, next);
|
|
325
|
+
try {
|
|
326
|
+
return await next;
|
|
327
|
+
}
|
|
328
|
+
finally {
|
|
329
|
+
if (locks.get(key) === next)
|
|
330
|
+
locks.delete(key);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
export const getAssetTransfer = (root, scope, id) => withAssetTransfer(root, scope, id, assetTransferView);
|
|
334
|
+
async function acknowledgeChunk(transfer, fileIndex, chunkIndex) {
|
|
335
|
+
const files = await readProgress(transfer);
|
|
336
|
+
if (!files)
|
|
337
|
+
return assetTransferView(transfer);
|
|
338
|
+
if (!files[fileIndex].receivedChunks.includes(chunkIndex)) {
|
|
339
|
+
files[fileIndex].receivedChunks = [...files[fileIndex].receivedChunks, chunkIndex].sort((a, b) => a - b);
|
|
340
|
+
await saveProgress(transfer, files);
|
|
341
|
+
}
|
|
342
|
+
return assetTransferView(transfer, false);
|
|
343
|
+
}
|
|
344
|
+
export async function putAssetTransferChunk(root, scope, id, input) {
|
|
345
|
+
const chunk = assetTransferChunkSchema.parse(input);
|
|
346
|
+
return withAssetTransfer(root, scope, id, async (transfer) => {
|
|
347
|
+
if (transfer.record.status === 'publishing')
|
|
348
|
+
throw new AssetTransferError('asset_transfer_busy');
|
|
349
|
+
const file = transfer.record.manifest.files[chunk.fileIndex];
|
|
350
|
+
const expected = file?.chunkHashes[chunk.chunkIndex];
|
|
351
|
+
const bytes = Buffer.from(chunk.contentBase64, 'base64');
|
|
352
|
+
if (!file || !expected || bytes.length !== Math.min(ASSET_TRANSFER_CHUNK_BYTES, file.size - chunk.chunkIndex * ASSET_TRANSFER_CHUNK_BYTES)
|
|
353
|
+
|| createHash('sha256').update(bytes).digest('hex') !== expected || bytes.toString('base64') !== chunk.contentBase64)
|
|
354
|
+
throw new AssetTransferError('asset_transfer_chunk_mismatch');
|
|
355
|
+
if (transfer.record.status === 'completed')
|
|
356
|
+
return assetTransferView(transfer, false);
|
|
357
|
+
const directory = await safeAssetTransferDirectory(transfer.directory, 'chunks', true);
|
|
358
|
+
const destination = join(directory, `${chunk.fileIndex}-${chunk.chunkIndex}`);
|
|
359
|
+
try {
|
|
360
|
+
const stored = await hashAssetTransferFile(destination, ASSET_TRANSFER_CHUNK_BYTES);
|
|
361
|
+
if (stored.size === bytes.length && stored.contentHash === expected)
|
|
362
|
+
return acknowledgeChunk(transfer, chunk.fileIndex, chunk.chunkIndex);
|
|
363
|
+
}
|
|
364
|
+
catch (error) {
|
|
365
|
+
if (!assetTransferMissing(error))
|
|
366
|
+
throw error;
|
|
367
|
+
}
|
|
368
|
+
const temporary = join(directory, `chunk-${randomUUID()}.tmp`);
|
|
369
|
+
const handle = await open(temporary, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);
|
|
370
|
+
try {
|
|
371
|
+
await handle.writeFile(bytes);
|
|
372
|
+
await handle.sync();
|
|
373
|
+
}
|
|
374
|
+
finally {
|
|
375
|
+
await handle.close();
|
|
376
|
+
}
|
|
377
|
+
await safeAssetTransferDirectory(transfer.directory, 'chunks');
|
|
378
|
+
await rename(temporary, destination);
|
|
379
|
+
await syncAssetTransferDirectory(directory);
|
|
380
|
+
return acknowledgeChunk(transfer, chunk.fileIndex, chunk.chunkIndex);
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
export const cancelAssetTransfer = (root, scope, id) => withAssetTransfer(root, scope, id, async (transfer) => {
|
|
384
|
+
if (transfer.record.status === 'publishing')
|
|
385
|
+
throw new AssetTransferError('asset_transfer_busy');
|
|
386
|
+
await safeAssetTransferDirectory(transfer.directory);
|
|
387
|
+
await rm(transfer.directory, { recursive: true });
|
|
388
|
+
return { cancelled: true };
|
|
389
|
+
});
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
+
import { constants } from 'node:fs';
|
|
3
|
+
import { lstat, mkdir, open, readdir, realpath, rename, rm } from 'node:fs/promises';
|
|
4
|
+
import { dirname, join } from 'node:path';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
import { directoryDeletePayloadSchema, directoryRenamePayloadSchema, hashOperationPayload, isMutableWorkspaceDirectoryPath, } from '@playcraft/common/cloud-workspace/runtime';
|
|
7
|
+
import { safeAssetTransferDirectory, syncAssetTransferDirectory } from './asset-transfer-storage.js';
|
|
8
|
+
export class DirectoryMutationError extends Error {
|
|
9
|
+
code;
|
|
10
|
+
constructor(code) {
|
|
11
|
+
super(code);
|
|
12
|
+
this.code = code;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
const fail = (code) => { throw new DirectoryMutationError(code); };
|
|
16
|
+
const missing = (error) => error?.code === 'ENOENT';
|
|
17
|
+
const identitySchema = z.object({ dev: z.number().nonnegative(), ino: z.number().nonnegative() }).strict();
|
|
18
|
+
const receiptSchema = z.object({ operationId: z.string().min(1).max(128), kind: z.enum(['directory.rename', 'directory.delete']),
|
|
19
|
+
path: z.string(), newPath: z.string().optional(), revision: z.number().int().nonnegative(), recoveryPath: z.string().optional() }).strict();
|
|
20
|
+
const recordSchema = z.object({ version: z.literal(1), scope: z.string(), operationId: z.string().min(1).max(128),
|
|
21
|
+
payloadHash: z.string().regex(/^[a-f0-9]{64}$/), kind: z.enum(['directory.rename', 'directory.delete']),
|
|
22
|
+
operationPayloadHash: z.string().regex(/^[a-f0-9]{64}$/), payload: z.unknown(),
|
|
23
|
+
path: z.string(), newPath: z.string().optional(), source: identitySchema, reservation: identitySchema.optional(),
|
|
24
|
+
revision: z.number().int().nonnegative(), phase: z.enum(['prepared', 'completed']), receipt: receiptSchema.optional() }).strict();
|
|
25
|
+
async function identity(path) {
|
|
26
|
+
const stat = await lstat(path).catch(error => { if (!missing(error))
|
|
27
|
+
throw error; return undefined; });
|
|
28
|
+
if (!stat)
|
|
29
|
+
return undefined;
|
|
30
|
+
if (!stat.isDirectory() || stat.isSymbolicLink() || await realpath(path) !== path)
|
|
31
|
+
fail('invalid_workspace_path');
|
|
32
|
+
return { dev: stat.dev, ino: stat.ino };
|
|
33
|
+
}
|
|
34
|
+
function same(left, right) { return Boolean(left && right && left.dev === right.dev && left.ino === right.ino); }
|
|
35
|
+
/** Reject redirected entries and managed descendants too: renaming their parent must not bypass path protection. */
|
|
36
|
+
async function validateTree(root, path) {
|
|
37
|
+
await safeAssetTransferDirectory(root, path);
|
|
38
|
+
const target = join(root, ...path.split('/'));
|
|
39
|
+
const original = await identity(target);
|
|
40
|
+
if (!original)
|
|
41
|
+
return fail('directory_not_found');
|
|
42
|
+
const visit = async (current, relative) => {
|
|
43
|
+
for (const entry of await readdir(current, { withFileTypes: true })) {
|
|
44
|
+
const child = join(current, entry.name), childPath = `${relative}/${entry.name}`;
|
|
45
|
+
const stat = await lstat(child);
|
|
46
|
+
if (stat.isSymbolicLink() || !stat.isDirectory() && !stat.isFile()
|
|
47
|
+
|| stat.isDirectory() && !isMutableWorkspaceDirectoryPath(childPath))
|
|
48
|
+
fail('invalid_workspace_path');
|
|
49
|
+
if (stat.isDirectory()) {
|
|
50
|
+
await safeAssetTransferDirectory(root, childPath);
|
|
51
|
+
await visit(child, childPath);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
await visit(target, path);
|
|
56
|
+
if (!same(original, await identity(target)))
|
|
57
|
+
fail('directory_conflict');
|
|
58
|
+
return original;
|
|
59
|
+
}
|
|
60
|
+
async function saveRecord(directory, record) {
|
|
61
|
+
await safeAssetTransferDirectory(directory);
|
|
62
|
+
const temporary = join(directory, `${randomUUID()}.tmp`);
|
|
63
|
+
const file = await open(temporary, constants.O_CREAT | constants.O_EXCL | constants.O_WRONLY | constants.O_NOFOLLOW, 0o600);
|
|
64
|
+
try {
|
|
65
|
+
await file.writeFile(JSON.stringify(record));
|
|
66
|
+
await file.sync();
|
|
67
|
+
}
|
|
68
|
+
finally {
|
|
69
|
+
await file.close();
|
|
70
|
+
}
|
|
71
|
+
await safeAssetTransferDirectory(directory);
|
|
72
|
+
await rename(temporary, join(directory, 'record.json'));
|
|
73
|
+
await syncAssetTransferDirectory(directory);
|
|
74
|
+
}
|
|
75
|
+
async function readRecord(directory) {
|
|
76
|
+
let handle;
|
|
77
|
+
try {
|
|
78
|
+
handle = await open(join(directory, 'record.json'), constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
if (missing(error))
|
|
82
|
+
return undefined;
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
85
|
+
try {
|
|
86
|
+
const stat = await handle.stat();
|
|
87
|
+
if (!stat.isFile() || stat.nlink !== 1 || stat.size > 64 * 1024)
|
|
88
|
+
fail('directory_recovery_required');
|
|
89
|
+
return recordSchema.parse(JSON.parse(await handle.readFile('utf8')));
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
return fail('directory_recovery_required');
|
|
93
|
+
}
|
|
94
|
+
finally {
|
|
95
|
+
await handle.close();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async function cleanupRemoved(directory, record) {
|
|
99
|
+
if (record.kind !== 'directory.delete' || !record.receipt?.recoveryPath)
|
|
100
|
+
return;
|
|
101
|
+
try {
|
|
102
|
+
await safeAssetTransferDirectory(directory);
|
|
103
|
+
const destination = join(directory, 'removed'), stored = await identity(destination);
|
|
104
|
+
if (stored) {
|
|
105
|
+
if (!same(stored, record.source))
|
|
106
|
+
return;
|
|
107
|
+
await rm(destination, { recursive: true, force: false });
|
|
108
|
+
await syncAssetTransferDirectory(directory);
|
|
109
|
+
}
|
|
110
|
+
delete record.receipt.recoveryPath;
|
|
111
|
+
await saveRecord(directory, record);
|
|
112
|
+
}
|
|
113
|
+
catch { /* A retained private backup is disclosed in the receipt, never mistaken for a fresh source. */ }
|
|
114
|
+
}
|
|
115
|
+
/** A durable identity-bound intent makes a retry independent of any directory recreated at the old path. */
|
|
116
|
+
export async function mutateWorkspaceDirectory(root, kind, input, options) {
|
|
117
|
+
const parsed = kind === 'directory.rename' ? directoryRenamePayloadSchema.safeParse(input) : directoryDeletePayloadSchema.safeParse(input);
|
|
118
|
+
if (!parsed.success || !options.operationId || options.operationId.length > 128)
|
|
119
|
+
return fail('invalid_workspace_path');
|
|
120
|
+
const payload = parsed.data, newPath = 'newName' in payload
|
|
121
|
+
? [...payload.path.split('/').slice(0, -1), payload.newName].join('/') : undefined;
|
|
122
|
+
if (newPath === payload.path)
|
|
123
|
+
return fail('directory_exists');
|
|
124
|
+
if ((await lstat(root)).isSymbolicLink())
|
|
125
|
+
return fail('invalid_workspace_path');
|
|
126
|
+
root = await realpath(root);
|
|
127
|
+
await safeAssetTransferDirectory(root);
|
|
128
|
+
const key = createHash('sha256').update(`${options.scope}\0${options.operationId}`).digest('hex');
|
|
129
|
+
const relativeDirectory = `.playcraft-temp/directory-mutations/${key}`;
|
|
130
|
+
const directory = options.recoverOnly
|
|
131
|
+
? await safeAssetTransferDirectory(root, relativeDirectory).catch(error => { if (missing(error))
|
|
132
|
+
return undefined; throw error; })
|
|
133
|
+
: await safeAssetTransferDirectory(root, relativeDirectory, true);
|
|
134
|
+
if (!directory)
|
|
135
|
+
return undefined;
|
|
136
|
+
let record = await readRecord(directory);
|
|
137
|
+
const payloadHash = hashOperationPayload({ kind, payload });
|
|
138
|
+
if (record && (record.operationId !== options.operationId || record.scope !== options.scope || record.payloadHash !== payloadHash
|
|
139
|
+
|| record.operationPayloadHash !== (options.operationPayloadHash ?? hashOperationPayload(input))
|
|
140
|
+
|| record.path !== payload.path || record.kind !== kind || record.newPath !== newPath))
|
|
141
|
+
return fail('directory_recovery_required');
|
|
142
|
+
if (record?.phase === 'completed') {
|
|
143
|
+
const expected = { operationId: options.operationId, kind, path: payload.path, ...(newPath ? { newPath } : {}), revision: record.revision };
|
|
144
|
+
if (!record.receipt || Object.entries(expected).some(([key, value]) => record.receipt[key] !== value)
|
|
145
|
+
|| record.receipt.recoveryPath && record.receipt.recoveryPath !== `${relativeDirectory}/removed`)
|
|
146
|
+
return fail('directory_recovery_required');
|
|
147
|
+
await cleanupRemoved(directory, record);
|
|
148
|
+
return record.receipt;
|
|
149
|
+
}
|
|
150
|
+
if (!record) {
|
|
151
|
+
if (options.recoverOnly)
|
|
152
|
+
return undefined;
|
|
153
|
+
let source;
|
|
154
|
+
try {
|
|
155
|
+
source = await validateTree(root, payload.path);
|
|
156
|
+
}
|
|
157
|
+
catch (error) {
|
|
158
|
+
if (missing(error))
|
|
159
|
+
return fail('directory_not_found');
|
|
160
|
+
throw error;
|
|
161
|
+
}
|
|
162
|
+
const revision = await options.observeRevision();
|
|
163
|
+
if (revision === undefined)
|
|
164
|
+
return fail('directory_revision_unavailable');
|
|
165
|
+
if (payload.expectedRevision !== undefined && payload.expectedRevision !== revision)
|
|
166
|
+
return fail('revision_conflict');
|
|
167
|
+
if (newPath && await lstat(join(root, ...newPath.split('/'))).then(() => true, error => { if (missing(error))
|
|
168
|
+
return false; throw error; }))
|
|
169
|
+
return fail('directory_exists');
|
|
170
|
+
record = { version: 1, scope: options.scope, operationId: options.operationId, payloadHash,
|
|
171
|
+
operationPayloadHash: options.operationPayloadHash ?? hashOperationPayload(input), payload, kind, path: payload.path,
|
|
172
|
+
...(newPath ? { newPath } : {}), source, revision: revision + 1, phase: 'prepared' };
|
|
173
|
+
await saveRecord(directory, record);
|
|
174
|
+
}
|
|
175
|
+
const source = join(root, ...record.path.split('/'));
|
|
176
|
+
const destination = newPath ? join(root, ...newPath.split('/')) : join(directory, 'removed');
|
|
177
|
+
await safeAssetTransferDirectory(root, record.path.split('/').slice(0, -1).join('/'));
|
|
178
|
+
let destinationIdentity = await identity(destination);
|
|
179
|
+
if (!same(destinationIdentity, record.source)) {
|
|
180
|
+
if (!same(await identity(source), record.source))
|
|
181
|
+
return fail('directory_recovery_required');
|
|
182
|
+
await validateTree(root, record.path);
|
|
183
|
+
if (newPath) {
|
|
184
|
+
// Reserve an absent destination atomically; only our recorded empty placeholder may be replaced.
|
|
185
|
+
if (!destinationIdentity) {
|
|
186
|
+
await mkdir(destination, { mode: 0o700 }).catch(error => { if (error.code === 'EEXIST')
|
|
187
|
+
return fail('directory_exists'); throw error; });
|
|
188
|
+
record.reservation = await identity(destination);
|
|
189
|
+
await saveRecord(directory, record);
|
|
190
|
+
}
|
|
191
|
+
else if (!same(destinationIdentity, record.reservation))
|
|
192
|
+
return fail('directory_exists');
|
|
193
|
+
destinationIdentity = await identity(destination);
|
|
194
|
+
if (!same(destinationIdentity, record.reservation) || (await readdir(destination)).length)
|
|
195
|
+
return fail('directory_exists');
|
|
196
|
+
}
|
|
197
|
+
else if (destinationIdentity)
|
|
198
|
+
return fail('directory_recovery_required');
|
|
199
|
+
await options.assertCurrent?.();
|
|
200
|
+
if (!same(await identity(source), record.source))
|
|
201
|
+
return fail('directory_conflict');
|
|
202
|
+
if (newPath && !same(await identity(destination), record.reservation))
|
|
203
|
+
return fail('directory_exists');
|
|
204
|
+
await rename(source, destination);
|
|
205
|
+
await syncAssetTransferDirectory(dirname(source));
|
|
206
|
+
await syncAssetTransferDirectory(dirname(destination));
|
|
207
|
+
}
|
|
208
|
+
await options.onDetached?.();
|
|
209
|
+
const receipt = { operationId: options.operationId, kind, path: record.path,
|
|
210
|
+
...(newPath ? { newPath } : {}), revision: record.revision };
|
|
211
|
+
if (!newPath) {
|
|
212
|
+
// Mark the detach durably before recursive cleanup; a crash during cleanup cannot revive the old path.
|
|
213
|
+
receipt.recoveryPath = `${relativeDirectory}/removed`;
|
|
214
|
+
}
|
|
215
|
+
record = { ...record, phase: 'completed', receipt };
|
|
216
|
+
await saveRecord(directory, record);
|
|
217
|
+
await cleanupRemoved(directory, record);
|
|
218
|
+
return receipt;
|
|
219
|
+
}
|
|
220
|
+
/** Recover only an existing intent whose scope and original operation payload still agree. Never start new work. */
|
|
221
|
+
export async function recoverWorkspaceDirectoryMutation(root, kind, options) {
|
|
222
|
+
if ((await lstat(root)).isSymbolicLink())
|
|
223
|
+
return fail('invalid_workspace_path');
|
|
224
|
+
root = await realpath(root);
|
|
225
|
+
const key = createHash('sha256').update(`${options.scope}\0${options.operationId}`).digest('hex');
|
|
226
|
+
const directory = await safeAssetTransferDirectory(root, `.playcraft-temp/directory-mutations/${key}`)
|
|
227
|
+
.catch(error => { if (missing(error))
|
|
228
|
+
return undefined; throw error; });
|
|
229
|
+
if (!directory)
|
|
230
|
+
return undefined;
|
|
231
|
+
const record = await readRecord(directory);
|
|
232
|
+
if (!record)
|
|
233
|
+
return undefined;
|
|
234
|
+
const payload = kind === 'directory.rename' ? directoryRenamePayloadSchema.safeParse(record.payload) : directoryDeletePayloadSchema.safeParse(record.payload);
|
|
235
|
+
if (!payload.success)
|
|
236
|
+
return fail('directory_recovery_required');
|
|
237
|
+
return mutateWorkspaceDirectory(root, kind, payload.data, { ...options, recoverOnly: true, observeRevision: async () => undefined });
|
|
238
|
+
}
|