@nestm/storage 0.1.0-alpha.6 → 0.1.0-alpha.8

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 (87) hide show
  1. package/CHANGELOG.md +128 -0
  2. package/README.md +398 -19
  3. package/SECURITY.md +69 -0
  4. package/dist/ai-sdk/ai-sdk-workspace-tools.d.ts +74 -0
  5. package/dist/ai-sdk/ai-sdk-workspace-tools.d.ts.map +1 -0
  6. package/dist/ai-sdk/ai-sdk-workspace-tools.js +414 -0
  7. package/dist/ai-sdk/ai-sdk-workspace-tools.js.map +1 -0
  8. package/dist/ai-sdk/index.d.ts +2 -0
  9. package/dist/ai-sdk/index.d.ts.map +1 -0
  10. package/dist/ai-sdk/index.js +2 -0
  11. package/dist/ai-sdk/index.js.map +1 -0
  12. package/dist/files-sdk/files-sdk.driver.d.ts +64 -6
  13. package/dist/files-sdk/files-sdk.driver.d.ts.map +1 -1
  14. package/dist/files-sdk/files-sdk.driver.js +918 -38
  15. package/dist/files-sdk/files-sdk.driver.js.map +1 -1
  16. package/dist/files-sdk/fs/index.d.ts +21 -2
  17. package/dist/files-sdk/fs/index.d.ts.map +1 -1
  18. package/dist/files-sdk/fs/index.js +833 -1
  19. package/dist/files-sdk/fs/index.js.map +1 -1
  20. package/dist/files-sdk/index.d.ts +1 -1
  21. package/dist/files-sdk/index.d.ts.map +1 -1
  22. package/dist/files-sdk/index.js.map +1 -1
  23. package/dist/files-sdk/provider/index.d.ts +9 -5
  24. package/dist/files-sdk/provider/index.d.ts.map +1 -1
  25. package/dist/files-sdk/provider/index.js +60 -15
  26. package/dist/files-sdk/provider/index.js.map +1 -1
  27. package/dist/files-sdk/s3/construction-metadata.d.ts +7 -0
  28. package/dist/files-sdk/s3/construction-metadata.d.ts.map +1 -0
  29. package/dist/files-sdk/s3/construction-metadata.js +13 -0
  30. package/dist/files-sdk/s3/construction-metadata.js.map +1 -0
  31. package/dist/files-sdk/s3/index.d.ts +43 -14
  32. package/dist/files-sdk/s3/index.d.ts.map +1 -1
  33. package/dist/files-sdk/s3/index.js +1046 -57
  34. package/dist/files-sdk/s3/index.js.map +1 -1
  35. package/dist/gateway/storage-gateway.controller.d.ts.map +1 -1
  36. package/dist/gateway/storage-gateway.controller.js +25 -5
  37. package/dist/gateway/storage-gateway.controller.js.map +1 -1
  38. package/dist/storage-etag.d.ts +13 -0
  39. package/dist/storage-etag.d.ts.map +1 -0
  40. package/dist/storage-etag.js +31 -0
  41. package/dist/storage-etag.js.map +1 -0
  42. package/dist/storage.client.d.ts +7 -1
  43. package/dist/storage.client.d.ts.map +1 -1
  44. package/dist/storage.client.js +124 -10
  45. package/dist/storage.client.js.map +1 -1
  46. package/dist/storage.driver.d.ts +23 -1
  47. package/dist/storage.driver.d.ts.map +1 -1
  48. package/dist/storage.driver.js.map +1 -1
  49. package/dist/storage.service.d.ts.map +1 -1
  50. package/dist/storage.service.js +28 -3
  51. package/dist/storage.service.js.map +1 -1
  52. package/dist/storage.types.d.ts +113 -6
  53. package/dist/storage.types.d.ts.map +1 -1
  54. package/dist/storage.types.js.map +1 -1
  55. package/dist/testing/index.d.ts +1 -0
  56. package/dist/testing/index.d.ts.map +1 -1
  57. package/dist/testing/index.js +1 -0
  58. package/dist/testing/index.js.map +1 -1
  59. package/dist/testing/provider-conformance.d.ts +65 -0
  60. package/dist/testing/provider-conformance.d.ts.map +1 -0
  61. package/dist/testing/provider-conformance.js +853 -0
  62. package/dist/testing/provider-conformance.js.map +1 -0
  63. package/dist/workspace/index.d.ts +5 -0
  64. package/dist/workspace/index.d.ts.map +1 -0
  65. package/dist/workspace/index.js +5 -0
  66. package/dist/workspace/index.js.map +1 -0
  67. package/dist/workspace/storage-workspace.cursor.d.ts +71 -0
  68. package/dist/workspace/storage-workspace.cursor.d.ts.map +1 -0
  69. package/dist/workspace/storage-workspace.cursor.js +408 -0
  70. package/dist/workspace/storage-workspace.cursor.js.map +1 -0
  71. package/dist/workspace/storage-workspace.d.ts +6 -0
  72. package/dist/workspace/storage-workspace.d.ts.map +1 -0
  73. package/dist/workspace/storage-workspace.error.d.ts +28 -0
  74. package/dist/workspace/storage-workspace.error.d.ts.map +1 -0
  75. package/dist/workspace/storage-workspace.error.js +67 -0
  76. package/dist/workspace/storage-workspace.error.js.map +1 -0
  77. package/dist/workspace/storage-workspace.js +755 -0
  78. package/dist/workspace/storage-workspace.js.map +1 -0
  79. package/dist/workspace/storage-workspace.path.d.ts +9 -0
  80. package/dist/workspace/storage-workspace.path.d.ts.map +1 -0
  81. package/dist/workspace/storage-workspace.path.js +68 -0
  82. package/dist/workspace/storage-workspace.path.js.map +1 -0
  83. package/dist/workspace/storage-workspace.types.d.ts +105 -0
  84. package/dist/workspace/storage-workspace.types.d.ts.map +1 -0
  85. package/dist/workspace/storage-workspace.types.js +21 -0
  86. package/dist/workspace/storage-workspace.types.js.map +1 -0
  87. package/package.json +24 -5
@@ -1,5 +1,830 @@
1
+ import { createHash, randomUUID } from 'node:crypto';
2
+ import { constants as fsConstants } from 'node:fs';
3
+ import * as fsp from 'node:fs/promises';
4
+ import path from 'node:path';
5
+ import { Readable } from 'node:stream';
1
6
  import { fs } from 'files-sdk/fs';
7
+ import { StorageError, StorageErrorCode } from '../../storage.error.js';
8
+ import { isCanonicalStorageEtag, normalizeProviderStorageEtag, } from '../../storage-etag.js';
2
9
  import { createFilesSdkDriver, } from '../files-sdk.driver.js';
10
+ const SIDECAR_SUFFIX = '.meta.json';
11
+ const TEMP_SUFFIX = '.fls-part';
12
+ const MAX_SIDECAR_BYTES = 1024 * 1024;
13
+ function fsErrorCode(error) {
14
+ if (typeof error === 'object' && error !== null && 'code' in error) {
15
+ return typeof error.code === 'string' ? error.code : undefined;
16
+ }
17
+ return undefined;
18
+ }
19
+ function storageFsError(error, key, operation) {
20
+ if (error instanceof StorageError) {
21
+ return error;
22
+ }
23
+ const systemCode = fsErrorCode(error);
24
+ const code = systemCode === 'ENOENT' || systemCode === 'ENOTDIR'
25
+ ? StorageErrorCode.NOT_FOUND
26
+ : systemCode === 'EEXIST'
27
+ ? StorageErrorCode.CONFLICT
28
+ : systemCode === 'EACCES' || systemCode === 'EPERM'
29
+ ? StorageErrorCode.UNAUTHORIZED
30
+ : StorageErrorCode.PROVIDER;
31
+ return new StorageError(`Filesystem ${operation} failed for "${key}".`, {
32
+ code,
33
+ key,
34
+ operation,
35
+ permanent: code === StorageErrorCode.NOT_FOUND ||
36
+ code === StorageErrorCode.CONFLICT ||
37
+ code === StorageErrorCode.UNAUTHORIZED,
38
+ });
39
+ }
40
+ function invalidFsPath(key, message) {
41
+ throw new StorageError(`Invalid filesystem storage key: ${message}.`, {
42
+ code: StorageErrorCode.INVALID_ARGUMENT,
43
+ key,
44
+ permanent: true,
45
+ });
46
+ }
47
+ function strictSegments(key) {
48
+ if (key.length === 0 || key.includes('\0')) {
49
+ invalidFsPath(key, 'the key is empty or contains a null byte');
50
+ }
51
+ if (path.isAbsolute(key) || key.includes('\\')) {
52
+ invalidFsPath(key, 'only relative POSIX paths are accepted');
53
+ }
54
+ const segments = key.split('/');
55
+ if (segments.some((segment) => segment.length === 0 || segment === '.' || segment === '..')) {
56
+ invalidFsPath(key, 'empty, dot, and parent segments are not accepted');
57
+ }
58
+ if (segments.some((segment) => /[. ]$/u.test(segment))) {
59
+ invalidFsPath(key, 'segments ending in a dot or space are not accepted');
60
+ }
61
+ const leaf = segments.at(-1)?.toLowerCase() ?? '';
62
+ if (leaf.endsWith(SIDECAR_SUFFIX) || leaf.endsWith(TEMP_SUFFIX)) {
63
+ invalidFsPath(key, 'the final segment uses a reserved adapter suffix');
64
+ }
65
+ return segments;
66
+ }
67
+ function symlinkError(key) {
68
+ return new StorageError(`Filesystem storage key "${key}" crosses a symbolic link.`, {
69
+ code: StorageErrorCode.INVALID_ARGUMENT,
70
+ key,
71
+ permanent: true,
72
+ });
73
+ }
74
+ function unsupportedUpload(key, message) {
75
+ throw new StorageError(message, {
76
+ code: StorageErrorCode.NOT_SUPPORTED,
77
+ key,
78
+ operation: 'upload',
79
+ permanent: true,
80
+ });
81
+ }
82
+ function conflict(key) {
83
+ throw new StorageError(`Conditional mutation conflicted for "${key}".`, {
84
+ code: StorageErrorCode.CONFLICT,
85
+ key,
86
+ permanent: true,
87
+ });
88
+ }
89
+ function notFound(key) {
90
+ throw new StorageError(`Storage object "${key}" was not found.`, {
91
+ code: StorageErrorCode.NOT_FOUND,
92
+ key,
93
+ permanent: true,
94
+ });
95
+ }
96
+ function runtimeOf(options) {
97
+ const timeoutSignal = options.timeout === undefined || options.timeout <= 0
98
+ ? undefined
99
+ : AbortSignal.timeout(options.timeout);
100
+ return {
101
+ callerSignal: options.signal,
102
+ signal: options.signal === undefined
103
+ ? timeoutSignal
104
+ : timeoutSignal === undefined
105
+ ? options.signal
106
+ : AbortSignal.any([options.signal, timeoutSignal]),
107
+ timeoutSignal,
108
+ };
109
+ }
110
+ function assertActive(runtime, key) {
111
+ if (runtime.signal?.aborted !== true) {
112
+ return;
113
+ }
114
+ const timedOut = runtime.timeoutSignal?.aborted === true &&
115
+ runtime.callerSignal?.aborted !== true;
116
+ throw new StorageError(timedOut
117
+ ? `Filesystem mutation timed out for "${key}".`
118
+ : `Filesystem mutation was aborted for "${key}".`, {
119
+ aborted: true,
120
+ code: timedOut ? StorageErrorCode.TIMEOUT : StorageErrorCode.ABORTED,
121
+ key,
122
+ permanent: true,
123
+ timedOut,
124
+ });
125
+ }
126
+ async function ensureRoot(root, key) {
127
+ await fsp.mkdir(root, { mode: 0o700, recursive: true });
128
+ const stat = await fsp.lstat(root);
129
+ if (stat.isSymbolicLink()) {
130
+ throw symlinkError(key);
131
+ }
132
+ if (!stat.isDirectory()) {
133
+ invalidFsPath(key, 'the configured root is not a directory');
134
+ }
135
+ }
136
+ async function ensureParents(root, segments, key, create, runtime) {
137
+ await ensureRoot(root, key);
138
+ let current = root;
139
+ for (const segment of segments.slice(0, -1)) {
140
+ assertActive(runtime, key);
141
+ current = path.join(current, segment);
142
+ let stat;
143
+ try {
144
+ stat = await fsp.lstat(current);
145
+ }
146
+ catch (error) {
147
+ if (fsErrorCode(error) !== 'ENOENT') {
148
+ throw error;
149
+ }
150
+ if (!create) {
151
+ notFound(key);
152
+ }
153
+ try {
154
+ await fsp.mkdir(current, { mode: 0o700 });
155
+ }
156
+ catch (mkdirError) {
157
+ if (fsErrorCode(mkdirError) !== 'EEXIST') {
158
+ throw mkdirError;
159
+ }
160
+ }
161
+ stat = await fsp.lstat(current);
162
+ }
163
+ if (stat.isSymbolicLink()) {
164
+ throw symlinkError(key);
165
+ }
166
+ if (!stat.isDirectory()) {
167
+ invalidFsPath(key, 'a parent segment is not a directory');
168
+ }
169
+ }
170
+ return path.join(root, ...segments);
171
+ }
172
+ async function regularFileOrMissing(target, key) {
173
+ try {
174
+ const stat = await fsp.lstat(target);
175
+ if (stat.isSymbolicLink()) {
176
+ throw symlinkError(key);
177
+ }
178
+ if (!stat.isFile()) {
179
+ invalidFsPath(key, 'an object path is not a regular file');
180
+ }
181
+ if (stat.nlink !== 1) {
182
+ invalidFsPath(key, 'hard-linked object files are not accepted');
183
+ }
184
+ return stat;
185
+ }
186
+ catch (error) {
187
+ if (fsErrorCode(error) === 'ENOENT') {
188
+ return undefined;
189
+ }
190
+ throw error;
191
+ }
192
+ }
193
+ async function assertSymlinkFreeReadPath(root, key) {
194
+ const segments = strictSegments(key);
195
+ let rootStat;
196
+ try {
197
+ rootStat = await fsp.lstat(root);
198
+ }
199
+ catch (error) {
200
+ if (fsErrorCode(error) === 'ENOENT') {
201
+ return;
202
+ }
203
+ throw error;
204
+ }
205
+ if (rootStat.isSymbolicLink()) {
206
+ throw symlinkError(key);
207
+ }
208
+ if (!rootStat.isDirectory()) {
209
+ invalidFsPath(key, 'the configured root is not a directory');
210
+ }
211
+ let current = root;
212
+ for (const [index, segment] of segments.entries()) {
213
+ current = path.join(current, segment);
214
+ let stat;
215
+ try {
216
+ stat = await fsp.lstat(current);
217
+ }
218
+ catch (error) {
219
+ if (fsErrorCode(error) === 'ENOENT') {
220
+ return;
221
+ }
222
+ throw error;
223
+ }
224
+ if (stat.isSymbolicLink()) {
225
+ throw symlinkError(key);
226
+ }
227
+ const leaf = index === segments.length - 1;
228
+ if ((!leaf && !stat.isDirectory()) || (leaf && !stat.isFile())) {
229
+ invalidFsPath(key, leaf
230
+ ? 'an object path is not a regular file'
231
+ : 'a parent segment is not a directory');
232
+ }
233
+ if (leaf && stat.nlink !== 1) {
234
+ invalidFsPath(key, 'hard-linked object files are not accepted');
235
+ }
236
+ }
237
+ const sidecar = await regularFileOrMissing(current + SIDECAR_SUFFIX, key);
238
+ if (sidecar?.isSymbolicLink()) {
239
+ throw symlinkError(key);
240
+ }
241
+ }
242
+ async function readSidecar(target, key) {
243
+ const handle = await fsp.open(target, fsConstants.O_RDONLY | fsConstants.O_NOFOLLOW);
244
+ try {
245
+ const stat = await handle.stat();
246
+ if (!stat.isFile() || stat.size > MAX_SIDECAR_BYTES) {
247
+ throw new StorageError(`Filesystem metadata is invalid for "${key}".`, {
248
+ code: StorageErrorCode.PROVIDER,
249
+ key,
250
+ permanent: true,
251
+ });
252
+ }
253
+ const parsed = JSON.parse(await handle.readFile('utf8'));
254
+ if (typeof parsed !== 'object' ||
255
+ parsed === null ||
256
+ !('etag' in parsed) ||
257
+ typeof parsed.etag !== 'string' ||
258
+ !('contentType' in parsed) ||
259
+ typeof parsed.contentType !== 'string' ||
260
+ !('lastModified' in parsed) ||
261
+ typeof parsed.lastModified !== 'number') {
262
+ throw new StorageError(`Filesystem metadata is invalid for "${key}".`, {
263
+ code: StorageErrorCode.PROVIDER,
264
+ key,
265
+ permanent: true,
266
+ });
267
+ }
268
+ const sidecar = parsed;
269
+ const etag = normalizeProviderStorageEtag(sidecar.etag);
270
+ if (etag === undefined) {
271
+ throw new StorageError(`Filesystem metadata is invalid for "${key}".`, {
272
+ code: StorageErrorCode.PROVIDER,
273
+ key,
274
+ permanent: true,
275
+ });
276
+ }
277
+ return { ...sidecar, etag };
278
+ }
279
+ finally {
280
+ await handle.close();
281
+ }
282
+ }
283
+ function bodyBytes(body) {
284
+ if (typeof body === 'string') {
285
+ return new TextEncoder().encode(body);
286
+ }
287
+ if (body instanceof Uint8Array) {
288
+ return body;
289
+ }
290
+ if (body instanceof ArrayBuffer) {
291
+ return new Uint8Array(body);
292
+ }
293
+ if (ArrayBuffer.isView(body)) {
294
+ return new Uint8Array(body.buffer, body.byteOffset, body.byteLength);
295
+ }
296
+ return undefined;
297
+ }
298
+ function defaultContentType(body, override) {
299
+ if (override !== undefined) {
300
+ return override;
301
+ }
302
+ if (typeof body === 'string') {
303
+ return 'text/plain; charset=utf-8';
304
+ }
305
+ if (body instanceof Blob && body.type.length > 0) {
306
+ return body.type;
307
+ }
308
+ return 'application/octet-stream';
309
+ }
310
+ async function writeAll(handle, bytes) {
311
+ let offset = 0;
312
+ while (offset < bytes.byteLength) {
313
+ const { bytesWritten } = await handle.write(bytes, offset, bytes.byteLength - offset);
314
+ offset += bytesWritten;
315
+ }
316
+ }
317
+ function tempPath(directory) {
318
+ return path.join(directory, `.nestm-${randomUUID()}${TEMP_SUFFIX}`);
319
+ }
320
+ async function openTemp(directory) {
321
+ const target = tempPath(directory);
322
+ const handle = await fsp.open(target, fsConstants.O_CREAT |
323
+ fsConstants.O_EXCL |
324
+ fsConstants.O_WRONLY |
325
+ fsConstants.O_NOFOLLOW, 0o600);
326
+ return { handle, path: target };
327
+ }
328
+ async function removeTemp(target) {
329
+ if (target === undefined) {
330
+ return;
331
+ }
332
+ try {
333
+ await fsp.unlink(target);
334
+ }
335
+ catch (error) {
336
+ if (fsErrorCode(error) !== 'ENOENT') {
337
+ throw error;
338
+ }
339
+ }
340
+ }
341
+ async function stageBody(directory, key, body, options, runtime) {
342
+ const staged = await openTemp(directory);
343
+ const hash = createHash('sha256');
344
+ let size = 0;
345
+ const knownBytes = bodyBytes(body);
346
+ const knownSize = knownBytes?.byteLength ?? (body instanceof Blob ? body.size : undefined);
347
+ options.onProgress?.({
348
+ loaded: 0,
349
+ ...(knownSize !== undefined && { total: knownSize }),
350
+ });
351
+ try {
352
+ if (knownBytes !== undefined) {
353
+ assertActive(runtime, key);
354
+ await writeAll(staged.handle, knownBytes);
355
+ hash.update(knownBytes);
356
+ size = knownBytes.byteLength;
357
+ options.onProgress?.({ loaded: size, total: size });
358
+ }
359
+ else if (body instanceof Blob) {
360
+ const bytes = new Uint8Array(await body.arrayBuffer());
361
+ assertActive(runtime, key);
362
+ await writeAll(staged.handle, bytes);
363
+ hash.update(bytes);
364
+ size = bytes.byteLength;
365
+ options.onProgress?.({ loaded: size, total: size });
366
+ }
367
+ else {
368
+ const reader = body.getReader();
369
+ try {
370
+ while (true) {
371
+ assertActive(runtime, key);
372
+ const chunk = await reader.read();
373
+ if (chunk.done) {
374
+ break;
375
+ }
376
+ await writeAll(staged.handle, chunk.value);
377
+ hash.update(chunk.value);
378
+ size += chunk.value.byteLength;
379
+ options.onProgress?.({ loaded: size });
380
+ }
381
+ }
382
+ catch (error) {
383
+ await reader.cancel(error).catch(() => undefined);
384
+ throw error;
385
+ }
386
+ finally {
387
+ reader.releaseLock();
388
+ }
389
+ }
390
+ await staged.handle.sync();
391
+ await staged.handle.close();
392
+ return {
393
+ etag: hash.digest('hex'),
394
+ path: staged.path,
395
+ size,
396
+ };
397
+ }
398
+ catch (error) {
399
+ await staged.handle.close().catch(() => undefined);
400
+ await removeTemp(staged.path).catch(() => undefined);
401
+ throw error;
402
+ }
403
+ }
404
+ async function stageSidecar(directory, sidecar) {
405
+ const staged = await openTemp(directory);
406
+ try {
407
+ await writeAll(staged.handle, new TextEncoder().encode(JSON.stringify(sidecar)));
408
+ await staged.handle.sync();
409
+ await staged.handle.close();
410
+ return staged.path;
411
+ }
412
+ catch (error) {
413
+ await staged.handle.close().catch(() => undefined);
414
+ await removeTemp(staged.path).catch(() => undefined);
415
+ throw error;
416
+ }
417
+ }
418
+ const CONDITIONAL_FS_TAILS = new Map();
419
+ async function serializeFsKey(lockKey, operation) {
420
+ const previous = CONDITIONAL_FS_TAILS.get(lockKey) ?? Promise.resolve();
421
+ let release = () => undefined;
422
+ const gate = new Promise((resolve) => {
423
+ release = resolve;
424
+ });
425
+ const tail = previous.then(() => gate);
426
+ CONDITIONAL_FS_TAILS.set(lockKey, tail);
427
+ await previous;
428
+ try {
429
+ return await operation();
430
+ }
431
+ finally {
432
+ release();
433
+ if (CONDITIONAL_FS_TAILS.get(lockKey) === tail) {
434
+ CONDITIONAL_FS_TAILS.delete(lockKey);
435
+ }
436
+ }
437
+ }
438
+ /**
439
+ * Adds symlink-defended reads and process-local exact conditional operations
440
+ * to a files-sdk fs adapter.
441
+ *
442
+ * The configured root must be dedicated to this driver: no other process and
443
+ * no unconditional filesystem writer may mutate it concurrently. Each call
444
+ * rejects symlinks in the existing key path and uses exclusive same-directory
445
+ * temporary files plus atomic renames, but Node has no portable `openat2`
446
+ * equivalent that could make those checks safe against a hostile concurrent
447
+ * tree rewrite. This is a CAS implementation for an exclusively-owned local
448
+ * workspace, not a host-filesystem sandbox. Body and metadata sidecars are two
449
+ * files, so a process crash between their renames can still require cleanup.
450
+ */
451
+ export function withFsConditionalMutation(base) {
452
+ const root = path.resolve(base.root);
453
+ const download = base.download.bind(base);
454
+ const exists = base.exists.bind(base);
455
+ const head = base.head.bind(base);
456
+ const list = base.list.bind(base);
457
+ const serialize = (key, operation) => serializeFsKey(`${root}\0${key.normalize('NFC').toLowerCase()}`, operation);
458
+ const serializePair = (first, second, operation) => {
459
+ const identity = (key) => key.normalize('NFC').toLowerCase();
460
+ if (identity(first) === identity(second)) {
461
+ throw new StorageError('Conditional filesystem copy requires distinct source and destination keys.', {
462
+ code: StorageErrorCode.INVALID_ARGUMENT,
463
+ key: first,
464
+ operation: 'copy',
465
+ permanent: true,
466
+ });
467
+ }
468
+ const ordered = [first, second].sort((left, right) => identity(left).localeCompare(identity(right)));
469
+ const [headKey, tailKey] = ordered;
470
+ if (headKey === undefined || tailKey === undefined) {
471
+ throw new StorageError('Conditional filesystem copy requires distinct source and destination keys.', {
472
+ code: StorageErrorCode.INVALID_ARGUMENT,
473
+ key: first,
474
+ operation: 'copy',
475
+ permanent: true,
476
+ });
477
+ }
478
+ return serialize(headKey, () => serialize(tailKey, operation));
479
+ };
480
+ const assertEtag = (value, label, key, operation) => {
481
+ if (!isCanonicalStorageEtag(value)) {
482
+ throw new StorageError(`${label} must be a canonical storage ETag.`, {
483
+ code: StorageErrorCode.INVALID_ARGUMENT,
484
+ key,
485
+ operation,
486
+ permanent: true,
487
+ });
488
+ }
489
+ };
490
+ return Object.assign(base, {
491
+ conditionalCreate: Object.freeze({ resultEtag: true }),
492
+ conditionalReplace: Object.freeze({ resultEtag: true }),
493
+ conditionalDelete: Object.freeze({ etag: true }),
494
+ conditionalRead: Object.freeze({ etag: true, version: false }),
495
+ conditionalCopySource: Object.freeze({ etag: true, version: false }),
496
+ conditionalCopyDestination: Object.freeze({
497
+ atomicWithSource: true,
498
+ create: true,
499
+ replace: true,
500
+ }),
501
+ physicalKey: Object.freeze({ maxBytes: 4096 }),
502
+ async download(key, options) {
503
+ await assertSymlinkFreeReadPath(root, key);
504
+ return download(key, options);
505
+ },
506
+ async downloadConditional(key, options) {
507
+ if (options.condition.etag !== undefined) {
508
+ assertEtag(options.condition.etag, 'condition.etag', key, 'download');
509
+ }
510
+ if (options.condition.version !== undefined) {
511
+ throw new StorageError('The filesystem provider has no immutable object versions.', {
512
+ code: StorageErrorCode.NOT_SUPPORTED,
513
+ key,
514
+ operation: 'download',
515
+ permanent: true,
516
+ });
517
+ }
518
+ return serialize(key, async () => {
519
+ let handle;
520
+ try {
521
+ const runtime = runtimeOf(options);
522
+ const segments = strictSegments(key);
523
+ const bodyPath = await ensureParents(root, segments, key, false, runtime);
524
+ const sidecarPath = bodyPath + SIDECAR_SUFFIX;
525
+ if ((await regularFileOrMissing(bodyPath, key)) === undefined ||
526
+ (await regularFileOrMissing(sidecarPath, key)) === undefined) {
527
+ notFound(key);
528
+ }
529
+ const sidecar = await readSidecar(sidecarPath, key);
530
+ if (options.condition.etag !== undefined &&
531
+ sidecar.etag !== options.condition.etag) {
532
+ conflict(key);
533
+ }
534
+ assertActive(runtime, key);
535
+ handle = await fsp.open(bodyPath, fsConstants.O_RDONLY | fsConstants.O_NOFOLLOW);
536
+ const stat = await handle.stat();
537
+ if (!stat.isFile() || stat.nlink !== 1) {
538
+ invalidFsPath(key, 'an object path is not a regular file');
539
+ }
540
+ const start = options.range?.start ?? 0;
541
+ const requestedEnd = options.range?.end ?? Math.max(0, stat.size - 1);
542
+ const end = Math.min(requestedEnd, Math.max(0, stat.size - 1));
543
+ const size = start >= stat.size ? 0 : Math.max(0, end - start + 1);
544
+ if (size === 0) {
545
+ await handle.close();
546
+ handle = undefined;
547
+ return {
548
+ body: new ReadableStream({
549
+ start(controller) {
550
+ controller.close();
551
+ },
552
+ }),
553
+ contentType: sidecar.contentType,
554
+ etag: sidecar.etag,
555
+ key,
556
+ lastModified: new Date(sidecar.lastModified),
557
+ ...(sidecar.metadata !== undefined && {
558
+ metadata: { ...sidecar.metadata },
559
+ }),
560
+ name: path.basename(key),
561
+ size: 0,
562
+ };
563
+ }
564
+ const nodeStream = handle.createReadStream({
565
+ autoClose: true,
566
+ ...(options.range !== undefined && { end, start }),
567
+ });
568
+ handle = undefined;
569
+ return {
570
+ body: Readable.toWeb(nodeStream),
571
+ contentType: sidecar.contentType,
572
+ etag: sidecar.etag,
573
+ key,
574
+ lastModified: new Date(sidecar.lastModified),
575
+ ...(sidecar.metadata !== undefined && {
576
+ metadata: { ...sidecar.metadata },
577
+ }),
578
+ name: path.basename(key),
579
+ size,
580
+ };
581
+ }
582
+ catch (error) {
583
+ await handle?.close().catch(() => undefined);
584
+ throw storageFsError(error, key, 'download');
585
+ }
586
+ });
587
+ },
588
+ async exists(key, options) {
589
+ await assertSymlinkFreeReadPath(root, key);
590
+ return exists(key, options);
591
+ },
592
+ async head(key, options) {
593
+ await assertSymlinkFreeReadPath(root, key);
594
+ return head(key, options);
595
+ },
596
+ async list(options) {
597
+ const result = await list(options);
598
+ for (const item of result.items) {
599
+ await assertSymlinkFreeReadPath(root, item.key);
600
+ }
601
+ return result;
602
+ },
603
+ async deleteConditional(key, options) {
604
+ assertEtag(options.condition.etag, 'condition.etag', key, 'delete');
605
+ return serialize(key, async () => {
606
+ const runtime = runtimeOf(options);
607
+ try {
608
+ const segments = strictSegments(key);
609
+ const bodyPath = await ensureParents(root, segments, key, false, runtime);
610
+ const sidecarPath = bodyPath + SIDECAR_SUFFIX;
611
+ if ((await regularFileOrMissing(bodyPath, key)) === undefined ||
612
+ (await regularFileOrMissing(sidecarPath, key)) === undefined) {
613
+ notFound(key);
614
+ }
615
+ const sidecar = await readSidecar(sidecarPath, key);
616
+ if (sidecar.etag !== options.condition.etag) {
617
+ conflict(key);
618
+ }
619
+ assertActive(runtime, key);
620
+ await ensureParents(root, segments, key, false, runtime);
621
+ if ((await regularFileOrMissing(bodyPath, key)) === undefined ||
622
+ (await regularFileOrMissing(sidecarPath, key)) === undefined ||
623
+ (await readSidecar(sidecarPath, key)).etag !==
624
+ options.condition.etag) {
625
+ conflict(key);
626
+ }
627
+ await fsp.unlink(bodyPath);
628
+ try {
629
+ await fsp.unlink(sidecarPath);
630
+ }
631
+ catch (error) {
632
+ throw storageFsError(error, key, 'delete');
633
+ }
634
+ }
635
+ catch (error) {
636
+ throw storageFsError(error, key, 'delete');
637
+ }
638
+ });
639
+ },
640
+ async promote(sourceKey, destinationKey, options) {
641
+ const destination = options.destination;
642
+ if (destination !== undefined &&
643
+ (typeof destination !== 'object' ||
644
+ destination === null ||
645
+ (destination.type !== 'create' && destination.type !== 'replace'))) {
646
+ throw new StorageError('destination.type must be "create" or "replace".', {
647
+ code: StorageErrorCode.INVALID_ARGUMENT,
648
+ key: destinationKey,
649
+ operation: 'copy',
650
+ permanent: true,
651
+ });
652
+ }
653
+ if (options.sourceEtag !== undefined) {
654
+ assertEtag(options.sourceEtag, 'sourceEtag', sourceKey, 'copy');
655
+ }
656
+ if (destination?.type === 'replace') {
657
+ assertEtag(destination.etag, 'destination.etag', destinationKey, 'copy');
658
+ }
659
+ if (options.sourceVersion !== undefined) {
660
+ throw new StorageError('The filesystem provider has no immutable object versions.', {
661
+ code: StorageErrorCode.NOT_SUPPORTED,
662
+ key: sourceKey,
663
+ operation: 'copy',
664
+ permanent: true,
665
+ });
666
+ }
667
+ return serializePair(sourceKey, destinationKey, async () => {
668
+ let bodyTemp;
669
+ let sidecarTemp;
670
+ const runtime = runtimeOf(options);
671
+ try {
672
+ const sourceSegments = strictSegments(sourceKey);
673
+ const destinationSegments = strictSegments(destinationKey);
674
+ const sourcePath = await ensureParents(root, sourceSegments, sourceKey, false, runtime);
675
+ const sourceSidecarPath = sourcePath + SIDECAR_SUFFIX;
676
+ if ((await regularFileOrMissing(sourcePath, sourceKey)) === undefined ||
677
+ (await regularFileOrMissing(sourceSidecarPath, sourceKey)) ===
678
+ undefined) {
679
+ notFound(sourceKey);
680
+ }
681
+ const sourceSidecar = await readSidecar(sourceSidecarPath, sourceKey);
682
+ if (options.sourceEtag !== undefined &&
683
+ sourceSidecar.etag !== options.sourceEtag) {
684
+ conflict(sourceKey);
685
+ }
686
+ const destinationPath = await ensureParents(root, destinationSegments, destinationKey, true, runtime);
687
+ const destinationSidecarPath = destinationPath + SIDECAR_SUFFIX;
688
+ const assertDestination = async () => {
689
+ const body = await regularFileOrMissing(destinationPath, destinationKey);
690
+ const sidecar = await regularFileOrMissing(destinationSidecarPath, destinationKey);
691
+ if (options.destination?.type === 'create') {
692
+ if (body !== undefined || sidecar !== undefined) {
693
+ conflict(destinationKey);
694
+ }
695
+ return;
696
+ }
697
+ if (options.destination?.type === 'replace') {
698
+ if (body === undefined || sidecar === undefined) {
699
+ notFound(destinationKey);
700
+ }
701
+ if ((await readSidecar(destinationSidecarPath, destinationKey))
702
+ .etag !== options.destination.etag) {
703
+ conflict(destinationKey);
704
+ }
705
+ }
706
+ };
707
+ await assertDestination();
708
+ const staged = await openTemp(path.dirname(destinationPath));
709
+ await staged.handle.close();
710
+ bodyTemp = staged.path;
711
+ await fsp.copyFile(sourcePath, bodyTemp);
712
+ const lastModified = Date.now();
713
+ sidecarTemp = await stageSidecar(path.dirname(destinationPath), {
714
+ ...sourceSidecar,
715
+ lastModified,
716
+ });
717
+ assertActive(runtime, sourceKey);
718
+ if ((await regularFileOrMissing(sourcePath, sourceKey)) === undefined ||
719
+ (await regularFileOrMissing(sourceSidecarPath, sourceKey)) ===
720
+ undefined ||
721
+ (options.sourceEtag !== undefined &&
722
+ (await readSidecar(sourceSidecarPath, sourceKey)).etag !==
723
+ options.sourceEtag)) {
724
+ conflict(sourceKey);
725
+ }
726
+ await assertDestination();
727
+ await fsp.rename(bodyTemp, destinationPath);
728
+ bodyTemp = undefined;
729
+ await fsp.rename(sidecarTemp, destinationSidecarPath);
730
+ sidecarTemp = undefined;
731
+ }
732
+ catch (error) {
733
+ throw storageFsError(error, sourceKey, 'copy');
734
+ }
735
+ finally {
736
+ await removeTemp(bodyTemp).catch(() => undefined);
737
+ await removeTemp(sidecarTemp).catch(() => undefined);
738
+ }
739
+ });
740
+ },
741
+ async uploadConditional(key, body, options) {
742
+ if (options.condition.type === 'replace') {
743
+ assertEtag(options.condition.etag, 'condition.etag', key, 'upload');
744
+ }
745
+ return serialize(key, async () => {
746
+ let bodyTemp;
747
+ let sidecarTemp;
748
+ const runtime = runtimeOf(options);
749
+ try {
750
+ if (options.multipart !== undefined && options.multipart !== false) {
751
+ unsupportedUpload(key, 'Conditional filesystem uploads do not support multipart mode.');
752
+ }
753
+ if (options.control !== undefined) {
754
+ unsupportedUpload(key, 'Conditional filesystem uploads do not support resumable control.');
755
+ }
756
+ const segments = strictSegments(key);
757
+ const bodyPath = await ensureParents(root, segments, key, true, runtime);
758
+ const sidecarPath = bodyPath + SIDECAR_SUFFIX;
759
+ const existingBody = await regularFileOrMissing(bodyPath, key);
760
+ const existingSidecar = await regularFileOrMissing(sidecarPath, key);
761
+ if (options.condition.type === 'create') {
762
+ if (existingBody !== undefined || existingSidecar !== undefined) {
763
+ conflict(key);
764
+ }
765
+ }
766
+ else {
767
+ if (existingBody === undefined || existingSidecar === undefined) {
768
+ notFound(key);
769
+ }
770
+ if ((await readSidecar(sidecarPath, key)).etag !==
771
+ options.condition.etag) {
772
+ conflict(key);
773
+ }
774
+ }
775
+ const staged = await stageBody(path.dirname(bodyPath), key, body, options, runtime);
776
+ bodyTemp = staged.path;
777
+ const lastModified = Date.now();
778
+ const contentType = defaultContentType(body, options.contentType);
779
+ sidecarTemp = await stageSidecar(path.dirname(bodyPath), {
780
+ contentType,
781
+ etag: staged.etag,
782
+ lastModified,
783
+ ...(options.cacheControl !== undefined && {
784
+ cacheControl: options.cacheControl,
785
+ }),
786
+ ...(options.metadata !== undefined && {
787
+ metadata: options.metadata,
788
+ }),
789
+ });
790
+ assertActive(runtime, key);
791
+ await ensureParents(root, segments, key, false, runtime);
792
+ const currentBody = await regularFileOrMissing(bodyPath, key);
793
+ const currentSidecar = await regularFileOrMissing(sidecarPath, key);
794
+ if (options.condition.type === 'create') {
795
+ if (currentBody !== undefined || currentSidecar !== undefined) {
796
+ conflict(key);
797
+ }
798
+ }
799
+ else if (currentBody === undefined ||
800
+ currentSidecar === undefined ||
801
+ (await readSidecar(sidecarPath, key)).etag !==
802
+ options.condition.etag) {
803
+ conflict(key);
804
+ }
805
+ await fsp.rename(bodyTemp, bodyPath);
806
+ bodyTemp = undefined;
807
+ await fsp.rename(sidecarTemp, sidecarPath);
808
+ sidecarTemp = undefined;
809
+ return {
810
+ contentType,
811
+ etag: staged.etag,
812
+ key,
813
+ lastModified: new Date(lastModified),
814
+ size: staged.size,
815
+ };
816
+ }
817
+ catch (error) {
818
+ throw storageFsError(error, key, 'upload');
819
+ }
820
+ finally {
821
+ await removeTemp(bodyTemp).catch(() => undefined);
822
+ await removeTemp(sidecarTemp).catch(() => undefined);
823
+ }
824
+ });
825
+ },
826
+ });
827
+ }
3
828
  /**
4
829
  * Creates the files-sdk filesystem driver from the storage package's own
5
830
  * dependency context. The adapter reaches only `node:fs`, so this entry point
@@ -11,10 +836,17 @@ import { createFilesSdkDriver, } from '../files-sdk.driver.js';
11
836
  * put. Sidecars never surface as keys: `list` and `search` skip them, and
12
837
  * uploading a key that ends in `.meta.json` fails closed rather than colliding
13
838
  * with one.
839
+ *
840
+ * Conditional mutations additionally require an exclusively-owned root. See
841
+ * {@link withFsConditionalMutation}; use an OS sandbox as well when running an
842
+ * agent that has direct shell or filesystem tools.
14
843
  */
15
844
  export function createFsStorageDriver(options) {
16
845
  const { adapter: adapterOptions, ...filesOptions } = options;
17
- return createFilesSdkDriver({ ...filesOptions, adapter: fs(adapterOptions) });
846
+ return createFilesSdkDriver({
847
+ ...filesOptions,
848
+ adapter: withFsConditionalMutation(fs(adapterOptions)),
849
+ });
18
850
  }
19
851
  export { fs, mapFsError } from 'files-sdk/fs';
20
852
  //# sourceMappingURL=index.js.map