@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,6 +1,315 @@
1
- import { CopyObjectCommand } from '@aws-sdk/client-s3';
2
- import { mapS3Error, s3, } from 'files-sdk/s3';
3
- import { createFilesSdkDriver, mapFilesSdkError, } from '../files-sdk.driver.js';
1
+ import { AbortMultipartUploadCommand, CompleteMultipartUploadCommand, CopyObjectCommand, CreateMultipartUploadCommand, DeleteObjectCommand, GetObjectCommand, PutObjectCommand, UploadPartCommand, } from '@aws-sdk/client-s3';
2
+ import { createPresignedPost, } from '@aws-sdk/s3-presigned-post';
3
+ import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
4
+ import { Readable } from 'node:stream';
5
+ import { mapS3Error, s3 as createFilesSdkS3Adapter, } from 'files-sdk/s3';
6
+ import { StorageError, StorageErrorCode, isStorageError, } from '../../storage.error.js';
7
+ import { normalizeProviderStorageEtag, storageEtagHeader, } from '../../storage-etag.js';
8
+ import { assertFilesSdkS3AdapterHasNoReservedExtensions, createFilesSdkDriver, markFilesSdkS3AdapterUndecorated, markFilesSdkS3RawClientProvenance, mapFilesSdkError, } from '../files-sdk.driver.js';
9
+ import { getS3ConstructionMetadata, recordS3ConstructionMetadata, } from './construction-metadata.js';
10
+ const verifiedS3ProviderProfile = Symbol('verifiedS3ProviderProfile');
11
+ const verifiedS3ProviderProfiles = new WeakSet();
12
+ function assertCapabilityBoolean(value, label) {
13
+ if (typeof value !== 'boolean') {
14
+ throw new TypeError(`${label} must be a boolean.`);
15
+ }
16
+ }
17
+ /** Validates and deeply freezes an explicitly verified S3-compatible profile. */
18
+ export function defineS3ProviderProfile(profile) {
19
+ if (typeof profile.name !== 'string' || profile.name.trim().length === 0) {
20
+ throw new TypeError('S3 provider profile name must be a non-empty string.');
21
+ }
22
+ if (!Number.isSafeInteger(profile.physicalKey.maxBytes) ||
23
+ profile.physicalKey.maxBytes <= 0) {
24
+ throw new TypeError('S3 provider profile physicalKey.maxBytes must be a positive safe integer.');
25
+ }
26
+ const booleanFields = [
27
+ [
28
+ profile.signedUploadPolicy !== undefined,
29
+ 'signedUploadPolicy.contentType',
30
+ profile.signedUploadPolicy?.contentType,
31
+ ],
32
+ [
33
+ profile.signedUploadPolicy !== undefined,
34
+ 'signedUploadPolicy.sizeRange',
35
+ profile.signedUploadPolicy?.sizeRange,
36
+ ],
37
+ [
38
+ profile.conditionalCreate !== undefined,
39
+ 'conditionalCreate.resultEtag',
40
+ profile.conditionalCreate?.resultEtag,
41
+ ],
42
+ [
43
+ profile.conditionalReplace !== undefined,
44
+ 'conditionalReplace.resultEtag',
45
+ profile.conditionalReplace?.resultEtag,
46
+ ],
47
+ [
48
+ profile.conditionalDelete !== undefined,
49
+ 'conditionalDelete.etag',
50
+ profile.conditionalDelete?.etag,
51
+ ],
52
+ [
53
+ profile.conditionalRead !== undefined,
54
+ 'conditionalRead.etag',
55
+ profile.conditionalRead?.etag,
56
+ ],
57
+ [
58
+ profile.conditionalRead !== undefined,
59
+ 'conditionalRead.version',
60
+ profile.conditionalRead?.version,
61
+ ],
62
+ [
63
+ profile.conditionalCopySource !== undefined,
64
+ 'conditionalCopySource.etag',
65
+ profile.conditionalCopySource?.etag,
66
+ ],
67
+ [
68
+ profile.conditionalCopySource !== undefined,
69
+ 'conditionalCopySource.version',
70
+ profile.conditionalCopySource?.version,
71
+ ],
72
+ [
73
+ profile.conditionalCopyDestination !== undefined,
74
+ 'conditionalCopyDestination.create',
75
+ profile.conditionalCopyDestination?.create,
76
+ ],
77
+ [
78
+ profile.conditionalCopyDestination !== undefined,
79
+ 'conditionalCopyDestination.replace',
80
+ profile.conditionalCopyDestination?.replace,
81
+ ],
82
+ [
83
+ profile.conditionalCopyDestination !== undefined,
84
+ 'conditionalCopyDestination.atomicWithSource',
85
+ profile.conditionalCopyDestination?.atomicWithSource,
86
+ ],
87
+ [
88
+ profile.conditionalMultipartCompletion !== undefined,
89
+ 'conditionalMultipartCompletion.create',
90
+ profile.conditionalMultipartCompletion?.create,
91
+ ],
92
+ [
93
+ profile.conditionalMultipartCompletion !== undefined,
94
+ 'conditionalMultipartCompletion.replace',
95
+ profile.conditionalMultipartCompletion?.replace,
96
+ ],
97
+ ];
98
+ for (const [declared, label, value] of booleanFields) {
99
+ if (declared)
100
+ assertCapabilityBoolean(value, label);
101
+ }
102
+ if (profile.conditionalCopyDestination?.atomicWithSource === true &&
103
+ profile.conditionalCopySource?.etag !== true &&
104
+ profile.conditionalCopySource?.version !== true) {
105
+ throw new TypeError('An atomic destination-copy profile must enable at least one source-copy condition.');
106
+ }
107
+ if (profile.conditionalCopyDestination?.atomicWithSource === true &&
108
+ profile.conditionalCopyDestination.create !== true &&
109
+ profile.conditionalCopyDestination.replace !== true) {
110
+ throw new TypeError('An atomic destination-copy profile must declare create or replace support.');
111
+ }
112
+ if (profile.conditionalMultipartCompletion?.create === true &&
113
+ profile.conditionalCreate === undefined) {
114
+ throw new TypeError('Conditional multipart create requires conditional create support.');
115
+ }
116
+ if (profile.conditionalMultipartCompletion?.replace === true &&
117
+ profile.conditionalReplace === undefined) {
118
+ throw new TypeError('Conditional multipart replace requires conditional replace support.');
119
+ }
120
+ const clone = {
121
+ name: profile.name.trim(),
122
+ physicalKey: Object.freeze({ ...profile.physicalKey }),
123
+ signedUploadPolicy: Object.freeze(profile.signedUploadPolicy === undefined
124
+ ? { contentType: false, sizeRange: false }
125
+ : { ...profile.signedUploadPolicy }),
126
+ ...(profile.conditionalCreate !== undefined && {
127
+ conditionalCreate: Object.freeze({ ...profile.conditionalCreate }),
128
+ }),
129
+ ...(profile.conditionalReplace !== undefined && {
130
+ conditionalReplace: Object.freeze({ ...profile.conditionalReplace }),
131
+ }),
132
+ ...(profile.conditionalDelete !== undefined && {
133
+ conditionalDelete: Object.freeze({ ...profile.conditionalDelete }),
134
+ }),
135
+ ...(profile.conditionalRead !== undefined && {
136
+ conditionalRead: Object.freeze({ ...profile.conditionalRead }),
137
+ }),
138
+ ...(profile.conditionalCopySource !== undefined && {
139
+ conditionalCopySource: Object.freeze({
140
+ ...profile.conditionalCopySource,
141
+ }),
142
+ }),
143
+ ...(profile.conditionalCopyDestination !== undefined && {
144
+ conditionalCopyDestination: Object.freeze({
145
+ ...profile.conditionalCopyDestination,
146
+ }),
147
+ }),
148
+ ...(profile.conditionalMultipartCompletion !== undefined && {
149
+ conditionalMultipartCompletion: Object.freeze({
150
+ ...profile.conditionalMultipartCompletion,
151
+ }),
152
+ }),
153
+ };
154
+ Object.defineProperty(clone, verifiedS3ProviderProfile, {
155
+ configurable: false,
156
+ enumerable: false,
157
+ value: true,
158
+ writable: false,
159
+ });
160
+ const verified = Object.freeze(clone);
161
+ verifiedS3ProviderProfiles.add(verified);
162
+ return verified;
163
+ }
164
+ function assertVerifiedS3ProviderProfile(profile) {
165
+ if (typeof profile !== 'object' ||
166
+ profile === null ||
167
+ !verifiedS3ProviderProfiles.has(profile)) {
168
+ throw new TypeError('providerProfile must be created with defineS3ProviderProfile().');
169
+ }
170
+ }
171
+ function assertS3ProviderProfileContainedBy(profile, ceiling) {
172
+ if (profile.physicalKey.maxBytes > ceiling.physicalKey.maxBytes) {
173
+ throw new TypeError(`S3 provider profile "${profile.name}" cannot widen ${ceiling.name} physicalKey.maxBytes beyond ${ceiling.physicalKey.maxBytes}.`);
174
+ }
175
+ const operationCapabilities = [
176
+ ['conditionalCreate', profile.conditionalCreate, ceiling.conditionalCreate],
177
+ [
178
+ 'conditionalReplace',
179
+ profile.conditionalReplace,
180
+ ceiling.conditionalReplace,
181
+ ],
182
+ ['conditionalDelete', profile.conditionalDelete, ceiling.conditionalDelete],
183
+ ['conditionalRead', profile.conditionalRead, ceiling.conditionalRead],
184
+ [
185
+ 'conditionalCopySource',
186
+ profile.conditionalCopySource,
187
+ ceiling.conditionalCopySource,
188
+ ],
189
+ [
190
+ 'conditionalCopyDestination',
191
+ profile.conditionalCopyDestination,
192
+ ceiling.conditionalCopyDestination,
193
+ ],
194
+ [
195
+ 'conditionalMultipartCompletion',
196
+ profile.conditionalMultipartCompletion,
197
+ ceiling.conditionalMultipartCompletion,
198
+ ],
199
+ ];
200
+ for (const [name, declared, supported] of operationCapabilities) {
201
+ if (declared !== undefined && supported === undefined) {
202
+ throw new TypeError(`S3 provider profile "${profile.name}" cannot widen ${ceiling.name} with ${name}.`);
203
+ }
204
+ }
205
+ const booleanClaims = [
206
+ [
207
+ 'signedUploadPolicy.contentType',
208
+ profile.signedUploadPolicy.contentType,
209
+ ceiling.signedUploadPolicy.contentType,
210
+ ],
211
+ [
212
+ 'signedUploadPolicy.sizeRange',
213
+ profile.signedUploadPolicy.sizeRange,
214
+ ceiling.signedUploadPolicy.sizeRange,
215
+ ],
216
+ [
217
+ 'conditionalCreate.resultEtag',
218
+ profile.conditionalCreate?.resultEtag,
219
+ ceiling.conditionalCreate?.resultEtag,
220
+ ],
221
+ [
222
+ 'conditionalReplace.resultEtag',
223
+ profile.conditionalReplace?.resultEtag,
224
+ ceiling.conditionalReplace?.resultEtag,
225
+ ],
226
+ [
227
+ 'conditionalDelete.etag',
228
+ profile.conditionalDelete?.etag,
229
+ ceiling.conditionalDelete?.etag,
230
+ ],
231
+ [
232
+ 'conditionalRead.etag',
233
+ profile.conditionalRead?.etag,
234
+ ceiling.conditionalRead?.etag,
235
+ ],
236
+ [
237
+ 'conditionalRead.version',
238
+ profile.conditionalRead?.version,
239
+ ceiling.conditionalRead?.version,
240
+ ],
241
+ [
242
+ 'conditionalCopySource.etag',
243
+ profile.conditionalCopySource?.etag,
244
+ ceiling.conditionalCopySource?.etag,
245
+ ],
246
+ [
247
+ 'conditionalCopySource.version',
248
+ profile.conditionalCopySource?.version,
249
+ ceiling.conditionalCopySource?.version,
250
+ ],
251
+ [
252
+ 'conditionalCopyDestination.create',
253
+ profile.conditionalCopyDestination?.create,
254
+ ceiling.conditionalCopyDestination?.create,
255
+ ],
256
+ [
257
+ 'conditionalCopyDestination.replace',
258
+ profile.conditionalCopyDestination?.replace,
259
+ ceiling.conditionalCopyDestination?.replace,
260
+ ],
261
+ [
262
+ 'conditionalCopyDestination.atomicWithSource',
263
+ profile.conditionalCopyDestination?.atomicWithSource,
264
+ ceiling.conditionalCopyDestination?.atomicWithSource,
265
+ ],
266
+ [
267
+ 'conditionalMultipartCompletion.create',
268
+ profile.conditionalMultipartCompletion?.create,
269
+ ceiling.conditionalMultipartCompletion?.create,
270
+ ],
271
+ [
272
+ 'conditionalMultipartCompletion.replace',
273
+ profile.conditionalMultipartCompletion?.replace,
274
+ ceiling.conditionalMultipartCompletion?.replace,
275
+ ],
276
+ ];
277
+ for (const [name, declared, supported] of booleanClaims) {
278
+ if (declared === true && supported !== true) {
279
+ throw new TypeError(`S3 provider profile "${profile.name}" cannot widen ${ceiling.name} with ${name}.`);
280
+ }
281
+ }
282
+ }
283
+ export const AWS_S3_PROVIDER_PROFILE = defineS3ProviderProfile({
284
+ name: 'aws-s3-general-purpose',
285
+ physicalKey: { maxBytes: 1024 },
286
+ signedUploadPolicy: { contentType: true, sizeRange: true },
287
+ conditionalCreate: { resultEtag: true },
288
+ conditionalReplace: { resultEtag: true },
289
+ conditionalDelete: { etag: true },
290
+ conditionalRead: { etag: true, version: true },
291
+ conditionalCopySource: { etag: true, version: true },
292
+ conditionalCopyDestination: {
293
+ atomicWithSource: true,
294
+ create: true,
295
+ replace: true,
296
+ },
297
+ conditionalMultipartCompletion: { create: true, replace: true },
298
+ });
299
+ export const CLOUDFLARE_R2_PROVIDER_PROFILE = defineS3ProviderProfile({
300
+ name: 'cloudflare-r2-stable',
301
+ physicalKey: { maxBytes: 1024 },
302
+ signedUploadPolicy: { contentType: true, sizeRange: false },
303
+ conditionalCreate: { resultEtag: true },
304
+ conditionalReplace: { resultEtag: true },
305
+ conditionalRead: { etag: true, version: false },
306
+ conditionalCopySource: { etag: true, version: false },
307
+ });
308
+ const UNVERIFIED_S3_PROVIDER_PROFILE = defineS3ProviderProfile({
309
+ name: 'unverified-s3-compatible',
310
+ physicalKey: { maxBytes: 1024 },
311
+ signedUploadPolicy: { contentType: false, sizeRange: false },
312
+ });
4
313
  function copySource(bucket, key, version) {
5
314
  const source = `${encodeURIComponent(bucket)}/${encodeURIComponent(key)}`;
6
315
  return version === undefined
@@ -24,6 +333,166 @@ function maxRetries(options) {
24
333
  : options.retries?.max;
25
334
  return Math.max(0, Math.floor(configured ?? 0));
26
335
  }
336
+ function etagHeader(etag, key, operation, label) {
337
+ const header = storageEtagHeader(etag);
338
+ if (header === undefined) {
339
+ throw new StorageError(`${label} must be a canonical storage ETag.`, {
340
+ code: StorageErrorCode.INVALID_ARGUMENT,
341
+ key,
342
+ operation,
343
+ permanent: true,
344
+ });
345
+ }
346
+ return header;
347
+ }
348
+ function providerEtag(etag, key, operation) {
349
+ if (etag === undefined) {
350
+ return undefined;
351
+ }
352
+ const normalized = normalizeProviderStorageEtag(etag);
353
+ if (normalized === undefined) {
354
+ throw new StorageError('S3 returned an invalid ETag.', {
355
+ code: StorageErrorCode.PROVIDER,
356
+ key,
357
+ operation,
358
+ permanent: true,
359
+ });
360
+ }
361
+ return normalized;
362
+ }
363
+ function contentTypeOf(body, override) {
364
+ if (override !== undefined) {
365
+ return override;
366
+ }
367
+ if (typeof body === 'string') {
368
+ return 'text/plain; charset=utf-8';
369
+ }
370
+ if (body instanceof Blob && body.type.length > 0) {
371
+ return body.type;
372
+ }
373
+ return 'application/octet-stream';
374
+ }
375
+ async function normalizeConditionalBody(body, onProgress) {
376
+ let bytes;
377
+ if (typeof body === 'string') {
378
+ bytes = new TextEncoder().encode(body);
379
+ }
380
+ else if (body instanceof Uint8Array) {
381
+ bytes = body;
382
+ }
383
+ else if (body instanceof ArrayBuffer) {
384
+ bytes = new Uint8Array(body);
385
+ }
386
+ else if (ArrayBuffer.isView(body)) {
387
+ bytes = new Uint8Array(body.buffer, body.byteOffset, body.byteLength);
388
+ }
389
+ else if (body instanceof Blob) {
390
+ bytes = new Uint8Array(await body.arrayBuffer());
391
+ }
392
+ if (bytes !== undefined) {
393
+ const total = bytes.byteLength;
394
+ onProgress?.({ loaded: 0, total });
395
+ return { body: bytes, contentLength: total, size: () => total };
396
+ }
397
+ const stream = body instanceof Readable
398
+ ? Readable.toWeb(body)
399
+ : body;
400
+ if (!(stream instanceof ReadableStream)) {
401
+ throw new StorageError('Unsupported conditional upload body.', {
402
+ code: StorageErrorCode.INVALID_ARGUMENT,
403
+ operation: 'upload',
404
+ permanent: true,
405
+ });
406
+ }
407
+ const reader = stream.getReader();
408
+ let loaded = 0;
409
+ return {
410
+ body: new ReadableStream({
411
+ cancel(reason) {
412
+ return reader.cancel(reason);
413
+ },
414
+ async pull(controller) {
415
+ const result = await reader.read();
416
+ if (result.done) {
417
+ controller.close();
418
+ return;
419
+ }
420
+ loaded += result.value.byteLength;
421
+ controller.enqueue(result.value);
422
+ onProgress?.({ loaded });
423
+ },
424
+ }),
425
+ size: () => loaded,
426
+ };
427
+ }
428
+ function s3ErrorIdentity(error) {
429
+ if (typeof error !== 'object' || error === null) {
430
+ return { code: undefined, status: undefined };
431
+ }
432
+ const candidate = error;
433
+ const serviceCode = candidate.name ?? candidate.Code ?? candidate.code;
434
+ const status = candidate.$metadata?.httpStatusCode;
435
+ return {
436
+ code: typeof serviceCode === 'string' ? serviceCode : undefined,
437
+ status: typeof status === 'number' ? status : undefined,
438
+ };
439
+ }
440
+ function mapS3ConditionalError(error, operation) {
441
+ if (isStorageError(error)) {
442
+ return error;
443
+ }
444
+ const identity = s3ErrorIdentity(error);
445
+ const provider = mapFilesSdkError(mapS3Error(error));
446
+ const precondition = identity.status === 412 || identity.code === 'PreconditionFailed';
447
+ const conditionalConflict = identity.status === 409 || identity.code === 'ConditionalRequestConflict';
448
+ const notFound = identity.status === 404 ||
449
+ identity.code === 'NoSuchKey' ||
450
+ identity.code === 'NoSuchUpload' ||
451
+ provider.code === StorageErrorCode.NOT_FOUND;
452
+ const code = precondition || conditionalConflict
453
+ ? StorageErrorCode.CONFLICT
454
+ : notFound
455
+ ? StorageErrorCode.NOT_FOUND
456
+ : provider.code;
457
+ return new StorageError(`Conditional S3 ${operation} failed.`, {
458
+ aborted: provider.aborted,
459
+ code,
460
+ operation,
461
+ permanent: precondition || notFound
462
+ ? true
463
+ : conditionalConflict
464
+ ? false
465
+ : provider.permanent,
466
+ timedOut: provider.timedOut,
467
+ });
468
+ }
469
+ async function withS3Retry(options, operationName, operation) {
470
+ const retries = maxRetries(options);
471
+ for (let attempt = 0;; attempt += 1) {
472
+ const signal = operationSignal(options);
473
+ try {
474
+ return await operation(signal);
475
+ }
476
+ catch (error) {
477
+ const mapped = mapS3ConditionalError(error, operationName);
478
+ if (attempt >= retries ||
479
+ mapped.code !== StorageErrorCode.PROVIDER ||
480
+ mapped.aborted ||
481
+ mapped.permanent ||
482
+ signal?.aborted === true) {
483
+ throw mapped;
484
+ }
485
+ const delay = typeof options.retries === 'object' &&
486
+ options.retries.backoff !== undefined
487
+ ? options.retries.backoff({
488
+ attempt: attempt + 1,
489
+ error: mapped,
490
+ })
491
+ : Math.min(1000, 100 * 2 ** attempt);
492
+ await waitForRetry(delay, options.signal);
493
+ }
494
+ }
495
+ }
27
496
  async function waitForRetry(milliseconds, signal) {
28
497
  if (signal?.aborted === true) {
29
498
  throw signal.reason;
@@ -42,78 +511,598 @@ async function waitForRetry(milliseconds, signal) {
42
511
  signal?.addEventListener('abort', abort, { once: true });
43
512
  });
44
513
  }
514
+ const S3_MIN_MULTIPART_PART_BYTES = 5 * 1024 * 1024;
515
+ const S3_MAX_MULTIPART_PARTS = 10_000;
516
+ function conditionalHeaders(condition, key) {
517
+ return condition.type === 'create'
518
+ ? { IfNoneMatch: '*' }
519
+ : {
520
+ IfMatch: etagHeader(condition.etag, key, 'upload', 'condition.etag'),
521
+ };
522
+ }
523
+ function rangeHeader(range) {
524
+ if (range === undefined)
525
+ return undefined;
526
+ return `bytes=${range.start}-${range.end ?? ''}`;
527
+ }
528
+ function s3ResponseStream(body) {
529
+ let stream;
530
+ if (body instanceof ReadableStream) {
531
+ stream = body;
532
+ }
533
+ else if (body instanceof Readable) {
534
+ stream = Readable.toWeb(body);
535
+ }
536
+ else if (typeof body === 'object' &&
537
+ body !== null &&
538
+ 'transformToWebStream' in body &&
539
+ typeof body.transformToWebStream === 'function') {
540
+ stream = body.transformToWebStream();
541
+ }
542
+ else {
543
+ throw new StorageError('Conditional S3 download returned no body stream.', {
544
+ code: StorageErrorCode.PROVIDER,
545
+ operation: 'download',
546
+ permanent: true,
547
+ });
548
+ }
549
+ const reader = stream.getReader();
550
+ return new ReadableStream({
551
+ async cancel(reason) {
552
+ try {
553
+ await reader.cancel(reason);
554
+ }
555
+ catch (error) {
556
+ throw mapS3ConditionalError(error, 'download');
557
+ }
558
+ finally {
559
+ reader.releaseLock();
560
+ }
561
+ },
562
+ async pull(controller) {
563
+ try {
564
+ const result = await reader.read();
565
+ if (result.done) {
566
+ reader.releaseLock();
567
+ controller.close();
568
+ return;
569
+ }
570
+ controller.enqueue(result.value);
571
+ }
572
+ catch (error) {
573
+ reader.releaseLock();
574
+ controller.error(mapS3ConditionalError(error, 'download'));
575
+ }
576
+ },
577
+ });
578
+ }
579
+ async function* multipartParts(source, partSize) {
580
+ if (source instanceof Uint8Array) {
581
+ for (let offset = 0; offset < source.byteLength; offset += partSize) {
582
+ yield source.subarray(offset, Math.min(offset + partSize, source.byteLength));
583
+ }
584
+ return;
585
+ }
586
+ const reader = source.getReader();
587
+ let buffered = new Uint8Array(0);
588
+ try {
589
+ for (;;) {
590
+ const result = await reader.read();
591
+ if (result.done)
592
+ break;
593
+ const combined = new Uint8Array(buffered.byteLength + result.value.byteLength);
594
+ combined.set(buffered);
595
+ combined.set(result.value, buffered.byteLength);
596
+ buffered = combined;
597
+ while (buffered.byteLength >= partSize) {
598
+ yield buffered.slice(0, partSize);
599
+ buffered = buffered.slice(partSize);
600
+ }
601
+ }
602
+ if (buffered.byteLength > 0)
603
+ yield buffered;
604
+ }
605
+ finally {
606
+ reader.releaseLock();
607
+ }
608
+ }
609
+ function missingResultEtag(key) {
610
+ return new StorageError('S3 committed a conditional upload without returning the ETag required to identify its result; reconcile the destination before retrying.', {
611
+ code: StorageErrorCode.PROVIDER,
612
+ key,
613
+ operation: 'upload',
614
+ permanent: true,
615
+ });
616
+ }
617
+ async function uploadConditionalSingle(base, key, body, conditional) {
618
+ const normalized = await normalizeConditionalBody(body, conditional.onProgress);
619
+ const contentType = contentTypeOf(body, conditional.contentType);
620
+ const retryOptions = normalized.contentLength === undefined
621
+ ? { ...conditional, retries: 0 }
622
+ : conditional;
623
+ const result = await withS3Retry(retryOptions, 'upload', (signal) => base.raw.send(new PutObjectCommand({
624
+ Body: normalized.body,
625
+ Bucket: base.bucket,
626
+ ...(conditional.cacheControl !== undefined && {
627
+ CacheControl: conditional.cacheControl,
628
+ }),
629
+ ...(normalized.contentLength !== undefined && {
630
+ ContentLength: normalized.contentLength,
631
+ }),
632
+ ContentType: contentType,
633
+ ...conditionalHeaders(conditional.condition, key),
634
+ Key: key,
635
+ ...(conditional.metadata !== undefined && {
636
+ Metadata: conditional.metadata,
637
+ }),
638
+ }), signal === undefined ? undefined : { abortSignal: signal }));
639
+ const size = normalized.size();
640
+ conditional.onProgress?.({
641
+ loaded: size,
642
+ ...(normalized.contentLength !== undefined && {
643
+ total: normalized.contentLength,
644
+ }),
645
+ });
646
+ const etag = providerEtag(result.ETag, key, 'upload');
647
+ if (etag === undefined)
648
+ throw missingResultEtag(key);
649
+ return { contentType, etag, key, size };
650
+ }
651
+ async function uploadConditionalMultipart(base, key, body, conditional) {
652
+ const configuredPartSize = typeof conditional.multipart === 'object'
653
+ ? conditional.multipart.partSize
654
+ : undefined;
655
+ if (configuredPartSize !== undefined &&
656
+ (!Number.isSafeInteger(configuredPartSize) || configuredPartSize <= 0)) {
657
+ throw new StorageError('multipart.partSize must be a positive safe integer.', {
658
+ code: StorageErrorCode.INVALID_ARGUMENT,
659
+ key,
660
+ operation: 'upload',
661
+ permanent: true,
662
+ });
663
+ }
664
+ const partSize = Math.max(S3_MIN_MULTIPART_PART_BYTES, configuredPartSize ?? S3_MIN_MULTIPART_PART_BYTES);
665
+ const normalized = await normalizeConditionalBody(body, undefined);
666
+ if (normalized.contentLength === 0) {
667
+ return uploadConditionalSingle(base, key, new Uint8Array(), {
668
+ ...conditional,
669
+ multipart: false,
670
+ });
671
+ }
672
+ const contentType = contentTypeOf(body, conditional.contentType);
673
+ let uploadId;
674
+ let size = 0;
675
+ try {
676
+ const created = await withS3Retry(conditional, 'upload', (signal) => base.raw.send(new CreateMultipartUploadCommand({
677
+ Bucket: base.bucket,
678
+ ...(conditional.cacheControl !== undefined && {
679
+ CacheControl: conditional.cacheControl,
680
+ }),
681
+ ContentType: contentType,
682
+ Key: key,
683
+ ...(conditional.metadata !== undefined && {
684
+ Metadata: conditional.metadata,
685
+ }),
686
+ }), signal === undefined ? undefined : { abortSignal: signal }));
687
+ uploadId = created.UploadId;
688
+ if (uploadId === undefined || uploadId.length === 0) {
689
+ throw new StorageError('S3 did not return a multipart upload ID.', {
690
+ code: StorageErrorCode.PROVIDER,
691
+ key,
692
+ operation: 'upload',
693
+ permanent: true,
694
+ });
695
+ }
696
+ const completedParts = [];
697
+ let partNumber = 0;
698
+ for await (const part of multipartParts(normalized.body, partSize)) {
699
+ partNumber += 1;
700
+ if (partNumber > S3_MAX_MULTIPART_PARTS) {
701
+ throw new StorageError(`Conditional multipart upload exceeds the ${S3_MAX_MULTIPART_PARTS}-part S3 limit.`, {
702
+ code: StorageErrorCode.LIMIT_EXCEEDED,
703
+ key,
704
+ operation: 'upload',
705
+ permanent: true,
706
+ });
707
+ }
708
+ const uploaded = await withS3Retry(conditional, 'upload', (signal) => base.raw.send(new UploadPartCommand({
709
+ Body: part,
710
+ Bucket: base.bucket,
711
+ ContentLength: part.byteLength,
712
+ Key: key,
713
+ PartNumber: partNumber,
714
+ UploadId: uploadId,
715
+ }), signal === undefined ? undefined : { abortSignal: signal }));
716
+ const partEtag = providerEtag(uploaded.ETag, key, 'upload');
717
+ if (partEtag === undefined) {
718
+ throw new StorageError('S3 upload part returned no ETag.', {
719
+ code: StorageErrorCode.PROVIDER,
720
+ key,
721
+ operation: 'upload',
722
+ permanent: true,
723
+ });
724
+ }
725
+ completedParts.push({
726
+ ETag: storageEtagHeader(partEtag),
727
+ PartNumber: partNumber,
728
+ });
729
+ size += part.byteLength;
730
+ conditional.onProgress?.({
731
+ loaded: size,
732
+ ...(normalized.contentLength !== undefined && {
733
+ total: normalized.contentLength,
734
+ }),
735
+ });
736
+ }
737
+ if (completedParts.length === 0) {
738
+ await base.raw.send(new AbortMultipartUploadCommand({
739
+ Bucket: base.bucket,
740
+ Key: key,
741
+ UploadId: uploadId,
742
+ }));
743
+ uploadId = undefined;
744
+ return uploadConditionalSingle(base, key, new Uint8Array(), {
745
+ ...conditional,
746
+ multipart: false,
747
+ });
748
+ }
749
+ const completed = await withS3Retry(conditional, 'upload', (signal) => base.raw.send(new CompleteMultipartUploadCommand({
750
+ Bucket: base.bucket,
751
+ ...conditionalHeaders(conditional.condition, key),
752
+ Key: key,
753
+ MultipartUpload: { Parts: completedParts },
754
+ UploadId: uploadId,
755
+ }), signal === undefined ? undefined : { abortSignal: signal }));
756
+ const etag = providerEtag(completed.ETag, key, 'upload');
757
+ if (etag === undefined)
758
+ throw missingResultEtag(key);
759
+ uploadId = undefined;
760
+ return { contentType, etag, key, size };
761
+ }
762
+ catch (error) {
763
+ if (uploadId !== undefined) {
764
+ await base.raw
765
+ .send(new AbortMultipartUploadCommand({
766
+ Bucket: base.bucket,
767
+ Key: key,
768
+ UploadId: uploadId,
769
+ }))
770
+ .catch(() => undefined);
771
+ }
772
+ throw error;
773
+ }
774
+ }
775
+ const configuredS3Clients = new WeakSet();
776
+ /** Constructs the upstream S3 adapter while retaining security-relevant metadata. */
777
+ export function s3(options) {
778
+ const adapter = createFilesSdkS3Adapter(options);
779
+ markFilesSdkS3AdapterUndecorated(adapter);
780
+ recordS3ConstructionMetadata(adapter.raw, {
781
+ publicBaseUrlConfigured: options.publicBaseUrl !== undefined,
782
+ });
783
+ return adapter;
784
+ }
45
785
  /**
46
- * Adds the S3-only capabilities to an adapter already built by `s3(...)`: an
47
- * ETag/version-conditional server-side promotion, and the signed upload and
48
- * download policies the bridge advertises through `capabilities`.
49
- *
50
- * Exported so the provider factory can apply them to the adapter `loadFiles`
51
- * resolved for the `s3` slug instead of re-deriving {@link S3AdapterOptions}
52
- * from flat provider config. It expects an adapter whose `raw` is an `S3Client`
53
- * — pass one built by `s3(...)`, not an S3-compatible wrapper.
786
+ * Adds only the exact operations declared by one verified provider profile.
787
+ * Each raw adapter may be decorated once so capability state cannot depend on
788
+ * profile application order. A native AWS SDK endpoint also bounds every
789
+ * explicit profile by the built-in AWS capability ceiling.
54
790
  */
55
791
  export function withS3Capabilities(base, options = {}) {
56
- return Object.assign(base, {
57
- conditionalCopy: Object.freeze({
58
- etag: true,
59
- supported: true,
60
- version: true,
61
- }),
62
- signedUploadPolicy: Object.freeze({
63
- contentType: true,
64
- sizeRange: true,
65
- }),
792
+ const raw = base.raw;
793
+ if (configuredS3Clients.has(raw)) {
794
+ throw new TypeError('withS3Capabilities may only be applied once to an S3 adapter.');
795
+ }
796
+ assertFilesSdkS3AdapterHasNoReservedExtensions(base);
797
+ const sdkHasCustomEndpoint = raw.config.isCustomEndpoint === true;
798
+ if (options.endpoint !== undefined && !sdkHasCustomEndpoint) {
799
+ throw new TypeError('The declared S3 endpoint does not match the SDK client endpoint provenance.');
800
+ }
801
+ if (!sdkHasCustomEndpoint) {
802
+ // files-sdk does not expose this S3Client constructor option. Endpoint
803
+ // resolution is lazy, so fixing the resolved SDK config before the adapter
804
+ // is exposed prevents environment and shared-config endpoint redirection.
805
+ raw.config.ignoreConfiguredEndpointUrls = true;
806
+ }
807
+ const inferredNativeAws = base.name === 's3' && !sdkHasCustomEndpoint;
808
+ const profile = options.providerProfile ??
809
+ (inferredNativeAws
810
+ ? AWS_S3_PROVIDER_PROFILE
811
+ : UNVERIFIED_S3_PROVIDER_PROFILE);
812
+ assertVerifiedS3ProviderProfile(profile);
813
+ if (!sdkHasCustomEndpoint) {
814
+ assertS3ProviderProfileContainedBy(profile, AWS_S3_PROVIDER_PROFILE);
815
+ }
816
+ const provenance = options.providerProfile === undefined
817
+ ? inferredNativeAws
818
+ ? 'native'
819
+ : 'unverified'
820
+ : inferredNativeAws
821
+ ? 'native'
822
+ : 'verified';
823
+ const constructionMetadata = getS3ConstructionMetadata(raw);
824
+ const publicBaseUrlConfigured = options.publicBaseUrl !== undefined ||
825
+ constructionMetadata?.publicBaseUrlConfigured === true;
826
+ const bucket = base.bucket;
827
+ const requestAdapter = { bucket, raw };
828
+ const signedUploadUrl = async (key, signOptions) => {
829
+ if (signOptions.minSize !== undefined &&
830
+ signOptions.maxSize === undefined) {
831
+ throw new StorageError('S3 signed uploads cannot enforce minSize without maxSize.', {
832
+ code: StorageErrorCode.NOT_SUPPORTED,
833
+ key,
834
+ operation: 'signUpload',
835
+ permanent: true,
836
+ });
837
+ }
838
+ if (signOptions.maxSize !== undefined && key.endsWith('${filename}')) {
839
+ throw new StorageError('Signed POST keys must not end with the AWS ${filename} template.', {
840
+ code: StorageErrorCode.INVALID_ARGUMENT,
841
+ key,
842
+ operation: 'signUpload',
843
+ permanent: true,
844
+ });
845
+ }
846
+ if (signOptions.contentType !== undefined &&
847
+ profile.signedUploadPolicy.contentType !== true) {
848
+ throw new StorageError(`S3 provider profile "${profile.name}" cannot enforce signed-upload contentType.`, {
849
+ code: StorageErrorCode.NOT_SUPPORTED,
850
+ key,
851
+ operation: 'signUpload',
852
+ permanent: true,
853
+ });
854
+ }
855
+ if (signOptions.maxSize !== undefined &&
856
+ profile.signedUploadPolicy.sizeRange !== true) {
857
+ throw new StorageError(`S3 provider profile "${profile.name}" cannot enforce signed-upload sizeRange.`, {
858
+ code: StorageErrorCode.NOT_SUPPORTED,
859
+ key,
860
+ operation: 'signUpload',
861
+ permanent: true,
862
+ });
863
+ }
864
+ try {
865
+ if (signOptions.maxSize !== undefined) {
866
+ const conditions = [
867
+ [
868
+ 'content-length-range',
869
+ signOptions.minSize ?? 1,
870
+ signOptions.maxSize,
871
+ ],
872
+ ];
873
+ if (signOptions.contentType !== undefined) {
874
+ conditions.push(['eq', '$Content-Type', signOptions.contentType]);
875
+ }
876
+ const post = await createPresignedPost(raw, {
877
+ Bucket: bucket,
878
+ Conditions: conditions,
879
+ Expires: signOptions.expiresIn,
880
+ Key: key,
881
+ ...(signOptions.contentType !== undefined && {
882
+ Fields: { 'Content-Type': signOptions.contentType },
883
+ }),
884
+ });
885
+ return { fields: post.fields, method: 'POST', url: post.url };
886
+ }
887
+ const url = await getSignedUrl(raw, new PutObjectCommand({
888
+ Bucket: bucket,
889
+ Key: key,
890
+ ...(signOptions.contentType !== undefined && {
891
+ ContentType: signOptions.contentType,
892
+ }),
893
+ }), {
894
+ expiresIn: signOptions.expiresIn,
895
+ ...(signOptions.contentType !== undefined && {
896
+ signableHeaders: new Set(['content-type']),
897
+ }),
898
+ });
899
+ return {
900
+ ...(signOptions.contentType !== undefined && {
901
+ headers: { 'Content-Type': signOptions.contentType },
902
+ }),
903
+ method: 'PUT',
904
+ url,
905
+ };
906
+ }
907
+ catch (error) {
908
+ throw mapS3Error(error);
909
+ }
910
+ };
911
+ const adapter = Object.assign(base, {
912
+ physicalKey: profile.physicalKey,
913
+ signedUploadPolicy: profile.signedUploadPolicy,
914
+ signedUploadUrl,
66
915
  signedDownloadPolicy: Object.freeze({
67
- expiresIn: options.publicBaseUrl === undefined,
916
+ expiresIn: constructionMetadata !== undefined && !publicBaseUrlConfigured,
68
917
  }),
69
- async promote(sourceKey, destinationKey, promotion) {
70
- const retries = maxRetries(promotion);
71
- for (let attempt = 0;; attempt += 1) {
72
- const signal = operationSignal(promotion);
73
- try {
74
- await base.raw.send(new CopyObjectCommand({
75
- Bucket: base.bucket,
76
- CopySource: copySource(base.bucket, sourceKey, promotion.sourceVersion),
918
+ });
919
+ if (profile.conditionalCopySource !== undefined ||
920
+ profile.conditionalCopyDestination !== undefined) {
921
+ Object.assign(adapter, {
922
+ ...(profile.conditionalCopySource !== undefined && {
923
+ conditionalCopySource: profile.conditionalCopySource,
924
+ }),
925
+ ...(profile.conditionalCopyDestination !== undefined && {
926
+ conditionalCopyDestination: profile.conditionalCopyDestination,
927
+ }),
928
+ async promote(sourceKey, destinationKey, promotion) {
929
+ const destination = promotion.destination;
930
+ if (destination !== undefined &&
931
+ (typeof destination !== 'object' ||
932
+ destination === null ||
933
+ (destination.type !== 'create' && destination.type !== 'replace'))) {
934
+ throw new StorageError('destination.type must be "create" or "replace".', {
935
+ code: StorageErrorCode.INVALID_ARGUMENT,
936
+ key: destinationKey,
937
+ operation: 'promote',
938
+ permanent: true,
939
+ });
940
+ }
941
+ if ((promotion.sourceEtag !== undefined &&
942
+ profile.conditionalCopySource?.etag !== true) ||
943
+ (promotion.sourceVersion !== undefined &&
944
+ profile.conditionalCopySource?.version !== true) ||
945
+ (promotion.destination?.type === 'create' &&
946
+ profile.conditionalCopyDestination?.create !== true) ||
947
+ (promotion.destination?.type === 'replace' &&
948
+ profile.conditionalCopyDestination?.replace !== true) ||
949
+ (promotion.destination !== undefined &&
950
+ (promotion.sourceEtag !== undefined ||
951
+ promotion.sourceVersion !== undefined) &&
952
+ profile.conditionalCopyDestination?.atomicWithSource !== true)) {
953
+ throw new StorageError(`S3 provider profile "${profile.name}" does not support the requested conditional promotion.`, {
954
+ code: StorageErrorCode.NOT_SUPPORTED,
955
+ key: sourceKey,
956
+ operation: 'promote',
957
+ permanent: true,
958
+ });
959
+ }
960
+ await withS3Retry(promotion, 'copy', async (signal) => {
961
+ await raw.send(new CopyObjectCommand({
962
+ Bucket: bucket,
963
+ CopySource: copySource(bucket, sourceKey, promotion.sourceVersion),
77
964
  ...(promotion.sourceEtag !== undefined && {
78
- CopySourceIfMatch: promotion.sourceEtag,
965
+ CopySourceIfMatch: etagHeader(promotion.sourceEtag, sourceKey, 'copy', 'sourceEtag'),
79
966
  }),
967
+ ...(promotion.destination?.type === 'create'
968
+ ? { IfNoneMatch: '*' }
969
+ : promotion.destination?.type === 'replace'
970
+ ? {
971
+ IfMatch: etagHeader(promotion.destination.etag, destinationKey, 'copy', 'destination.etag'),
972
+ }
973
+ : {}),
80
974
  Key: destinationKey,
81
975
  }), signal === undefined ? undefined : { abortSignal: signal });
82
- return;
976
+ });
977
+ },
978
+ });
979
+ }
980
+ if (profile.conditionalRead !== undefined) {
981
+ Object.assign(adapter, {
982
+ conditionalRead: profile.conditionalRead,
983
+ async downloadConditional(key, options) {
984
+ if ((options.condition.etag !== undefined &&
985
+ profile.conditionalRead?.etag !== true) ||
986
+ (options.condition.version !== undefined &&
987
+ profile.conditionalRead?.version !== true)) {
988
+ throw new StorageError(`S3 provider profile "${profile.name}" does not support the requested conditional read.`, {
989
+ code: StorageErrorCode.NOT_SUPPORTED,
990
+ key,
991
+ operation: 'download',
992
+ permanent: true,
993
+ });
994
+ }
995
+ const result = await withS3Retry(options, 'download', (signal) => raw.send(new GetObjectCommand({
996
+ Bucket: bucket,
997
+ ...(options.condition.etag !== undefined && {
998
+ IfMatch: etagHeader(options.condition.etag, key, 'download', 'condition.etag'),
999
+ }),
1000
+ Key: key,
1001
+ ...(rangeHeader(options.range) !== undefined && {
1002
+ Range: rangeHeader(options.range),
1003
+ }),
1004
+ ...(options.condition.version !== undefined && {
1005
+ VersionId: options.condition.version,
1006
+ }),
1007
+ }), signal === undefined ? undefined : { abortSignal: signal }));
1008
+ const etag = providerEtag(result.ETag, key, 'download');
1009
+ return {
1010
+ body: s3ResponseStream(result.Body),
1011
+ contentType: result.ContentType ?? 'application/octet-stream',
1012
+ ...(etag !== undefined && { etag }),
1013
+ key,
1014
+ ...(result.LastModified !== undefined && {
1015
+ lastModified: result.LastModified,
1016
+ }),
1017
+ ...(result.Metadata !== undefined && { metadata: result.Metadata }),
1018
+ name: key.split('/').at(-1) ?? key,
1019
+ size: Number(result.ContentLength ?? 0),
1020
+ };
1021
+ },
1022
+ });
1023
+ }
1024
+ if (profile.conditionalDelete !== undefined) {
1025
+ Object.assign(adapter, {
1026
+ conditionalDelete: profile.conditionalDelete,
1027
+ async deleteConditional(key, options) {
1028
+ await withS3Retry(options, 'delete', async (signal) => {
1029
+ await raw.send(new DeleteObjectCommand({
1030
+ Bucket: bucket,
1031
+ IfMatch: etagHeader(options.condition.etag, key, 'delete', 'condition.etag'),
1032
+ Key: key,
1033
+ }), signal === undefined ? undefined : { abortSignal: signal });
1034
+ });
1035
+ },
1036
+ });
1037
+ }
1038
+ if (profile.conditionalCreate !== undefined ||
1039
+ profile.conditionalReplace !== undefined) {
1040
+ Object.assign(adapter, {
1041
+ ...(profile.conditionalCreate !== undefined && {
1042
+ conditionalCreate: profile.conditionalCreate,
1043
+ }),
1044
+ ...(profile.conditionalReplace !== undefined && {
1045
+ conditionalReplace: profile.conditionalReplace,
1046
+ }),
1047
+ ...(profile.conditionalMultipartCompletion !== undefined && {
1048
+ conditionalMultipartCompletion: profile.conditionalMultipartCompletion,
1049
+ }),
1050
+ async uploadConditional(key, body, conditional) {
1051
+ if (conditional.condition.type === 'replace') {
1052
+ etagHeader(conditional.condition.etag, key, 'upload', 'condition.etag');
83
1053
  }
84
- catch (error) {
85
- const mapped = mapS3Error(error);
86
- if (attempt >= retries ||
87
- mapped.code !== 'Provider' ||
88
- mapped.aborted ||
89
- mapped.permanent ||
90
- signal?.aborted === true) {
91
- throw mapped;
92
- }
93
- const storageError = mapFilesSdkError(mapped);
94
- const delay = typeof promotion.retries === 'object' &&
95
- promotion.retries.backoff !== undefined
96
- ? promotion.retries.backoff({
97
- attempt: attempt + 1,
98
- error: storageError,
99
- })
100
- : Math.min(1000, 100 * 2 ** attempt);
101
- await waitForRetry(delay, promotion.signal);
1054
+ const supported = conditional.condition.type === 'create'
1055
+ ? profile.conditionalCreate !== undefined
1056
+ : profile.conditionalReplace !== undefined;
1057
+ const multipartRequested = conditional.multipart !== undefined &&
1058
+ conditional.multipart !== false;
1059
+ const multipartSupported = !multipartRequested ||
1060
+ (conditional.condition.type === 'create'
1061
+ ? profile.conditionalMultipartCompletion?.create === true
1062
+ : profile.conditionalMultipartCompletion?.replace === true);
1063
+ if (!supported || !multipartSupported) {
1064
+ throw new StorageError(`S3 provider profile "${profile.name}" does not support the requested conditional upload.`, {
1065
+ code: StorageErrorCode.NOT_SUPPORTED,
1066
+ key,
1067
+ operation: 'upload',
1068
+ permanent: true,
1069
+ });
102
1070
  }
103
- }
104
- },
105
- });
1071
+ if (conditional.control !== undefined) {
1072
+ throw new StorageError('Conditional S3 uploads do not support resumable control.', {
1073
+ code: StorageErrorCode.NOT_SUPPORTED,
1074
+ key,
1075
+ operation: 'upload',
1076
+ permanent: true,
1077
+ });
1078
+ }
1079
+ return multipartRequested
1080
+ ? uploadConditionalMultipart(requestAdapter, key, body, conditional)
1081
+ : uploadConditionalSingle(requestAdapter, key, body, conditional);
1082
+ },
1083
+ });
1084
+ }
1085
+ Object.freeze(adapter);
1086
+ markFilesSdkS3RawClientProvenance(raw, adapter, provenance);
1087
+ configuredS3Clients.add(raw);
1088
+ return adapter;
106
1089
  }
107
1090
  /**
108
1091
  * Creates the files-sdk S3 driver from the storage package's own dependency
109
- * context and adds an ETag/version-conditional server-side promotion.
1092
+ * context and exposes only the operations in its verified provider profile.
110
1093
  */
111
1094
  export function createS3StorageDriver(options) {
112
1095
  const { adapter: adapterOptions, ...filesOptions } = options;
1096
+ const { providerProfile, ...plainFilesOptions } = filesOptions;
1097
+ const unverifiedCustomEndpoint = adapterOptions.endpoint !== undefined && providerProfile === undefined;
113
1098
  return createFilesSdkDriver({
114
- ...filesOptions,
115
- adapter: withS3Capabilities(s3(adapterOptions), adapterOptions),
1099
+ ...plainFilesOptions,
1100
+ ...(unverifiedCustomEndpoint && { readonly: true }),
1101
+ adapter: withS3Capabilities(s3(adapterOptions), {
1102
+ ...adapterOptions,
1103
+ ...(providerProfile !== undefined && { providerProfile }),
1104
+ }),
116
1105
  });
117
1106
  }
118
- export { mapS3Error, s3 } from 'files-sdk/s3';
1107
+ export { mapS3Error };
119
1108
  //# sourceMappingURL=index.js.map