@nestm/storage 0.1.0-alpha.0 → 0.1.0-alpha.10
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/CHANGELOG.md +264 -0
- package/README.md +724 -25
- package/SECURITY.md +101 -0
- package/dist/ai-sdk/ai-sdk-workspace-tools.d.ts +91 -0
- package/dist/ai-sdk/ai-sdk-workspace-tools.d.ts.map +1 -0
- package/dist/ai-sdk/ai-sdk-workspace-tools.js +491 -0
- package/dist/ai-sdk/ai-sdk-workspace-tools.js.map +1 -0
- package/dist/ai-sdk/index.d.ts +2 -0
- package/dist/ai-sdk/index.d.ts.map +1 -0
- package/dist/ai-sdk/index.js +2 -0
- package/dist/ai-sdk/index.js.map +1 -0
- package/dist/core/index.d.ts +6 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +4 -0
- package/dist/core/index.js.map +1 -0
- package/dist/files-sdk/files-sdk.driver.d.ts +85 -3
- package/dist/files-sdk/files-sdk.driver.d.ts.map +1 -1
- package/dist/files-sdk/files-sdk.driver.js +1437 -37
- package/dist/files-sdk/files-sdk.driver.js.map +1 -1
- package/dist/files-sdk/fs/index.d.ts +42 -0
- package/dist/files-sdk/fs/index.d.ts.map +1 -0
- package/dist/files-sdk/fs/index.js +1117 -0
- package/dist/files-sdk/fs/index.js.map +1 -0
- package/dist/files-sdk/index.d.ts +1 -1
- package/dist/files-sdk/index.d.ts.map +1 -1
- package/dist/files-sdk/index.js.map +1 -1
- package/dist/files-sdk/provider/index.d.ts +58 -0
- package/dist/files-sdk/provider/index.d.ts.map +1 -0
- package/dist/files-sdk/provider/index.js +147 -0
- package/dist/files-sdk/provider/index.js.map +1 -0
- package/dist/files-sdk/s3/construction-metadata.d.ts +9 -0
- package/dist/files-sdk/s3/construction-metadata.d.ts.map +1 -0
- package/dist/files-sdk/s3/construction-metadata.js +24 -0
- package/dist/files-sdk/s3/construction-metadata.js.map +1 -0
- package/dist/files-sdk/s3/index.d.ts +54 -0
- package/dist/files-sdk/s3/index.d.ts.map +1 -0
- package/dist/files-sdk/s3/index.js +1505 -0
- package/dist/files-sdk/s3/index.js.map +1 -0
- package/dist/gateway/index.d.ts +1 -1
- package/dist/gateway/index.d.ts.map +1 -1
- package/dist/gateway/index.js.map +1 -1
- package/dist/gateway/storage-gateway-fastify-parser.d.ts.map +1 -1
- package/dist/gateway/storage-gateway-fastify-parser.js.map +1 -1
- package/dist/gateway/storage-gateway.controller.d.ts +12 -11
- package/dist/gateway/storage-gateway.controller.d.ts.map +1 -1
- package/dist/gateway/storage-gateway.controller.js +240 -70
- package/dist/gateway/storage-gateway.controller.js.map +1 -1
- package/dist/gateway/storage-gateway.guard.d.ts.map +1 -1
- package/dist/gateway/storage-gateway.guard.js.map +1 -1
- package/dist/gateway/storage-gateway.module.d.ts.map +1 -1
- package/dist/gateway/storage-gateway.module.js +60 -1
- package/dist/gateway/storage-gateway.module.js.map +1 -1
- package/dist/gateway/storage-gateway.tokens.d.ts +1 -0
- package/dist/gateway/storage-gateway.tokens.d.ts.map +1 -1
- package/dist/gateway/storage-gateway.tokens.js +1 -0
- package/dist/gateway/storage-gateway.tokens.js.map +1 -1
- package/dist/gateway/storage-gateway.types.d.ts +53 -10
- package/dist/gateway/storage-gateway.types.d.ts.map +1 -1
- package/dist/gateway/storage-gateway.types.js.map +1 -1
- package/dist/index.d.ts +1 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -3
- package/dist/index.js.map +1 -1
- package/dist/inject-storage.decorator.js.map +1 -1
- package/dist/storage-etag.d.ts +13 -0
- package/dist/storage-etag.d.ts.map +1 -0
- package/dist/storage-etag.js +31 -0
- package/dist/storage-etag.js.map +1 -0
- package/dist/storage-upload-control.d.ts.map +1 -1
- package/dist/storage.client.d.ts +15 -3
- package/dist/storage.client.d.ts.map +1 -1
- package/dist/storage.client.js +204 -2
- package/dist/storage.client.js.map +1 -1
- package/dist/storage.driver.d.ts +28 -1
- package/dist/storage.driver.d.ts.map +1 -1
- package/dist/storage.driver.js.map +1 -1
- package/dist/storage.error.d.ts +17 -10
- package/dist/storage.error.d.ts.map +1 -1
- package/dist/storage.error.js +47 -1
- package/dist/storage.error.js.map +1 -1
- package/dist/storage.module.d.ts.map +1 -1
- package/dist/storage.module.js.map +1 -1
- package/dist/storage.service.d.ts.map +1 -1
- package/dist/storage.service.js +28 -3
- package/dist/storage.service.js.map +1 -1
- package/dist/storage.tokens.js.map +1 -1
- package/dist/storage.types.d.ts +150 -1
- package/dist/storage.types.d.ts.map +1 -1
- package/dist/storage.types.js.map +1 -1
- package/dist/testing/index.d.ts +1 -0
- package/dist/testing/index.d.ts.map +1 -1
- package/dist/testing/index.js +1 -0
- package/dist/testing/index.js.map +1 -1
- package/dist/testing/provider-conformance.d.ts +65 -0
- package/dist/testing/provider-conformance.d.ts.map +1 -0
- package/dist/testing/provider-conformance.js +869 -0
- package/dist/testing/provider-conformance.js.map +1 -0
- package/dist/workspace/index.d.ts +5 -0
- package/dist/workspace/index.d.ts.map +1 -0
- package/dist/workspace/index.js +5 -0
- package/dist/workspace/index.js.map +1 -0
- package/dist/workspace/storage-workspace.cursor.d.ts +71 -0
- package/dist/workspace/storage-workspace.cursor.d.ts.map +1 -0
- package/dist/workspace/storage-workspace.cursor.js +408 -0
- package/dist/workspace/storage-workspace.cursor.js.map +1 -0
- package/dist/workspace/storage-workspace.d.ts +6 -0
- package/dist/workspace/storage-workspace.d.ts.map +1 -0
- package/dist/workspace/storage-workspace.error.d.ts +32 -0
- package/dist/workspace/storage-workspace.error.d.ts.map +1 -0
- package/dist/workspace/storage-workspace.error.js +75 -0
- package/dist/workspace/storage-workspace.error.js.map +1 -0
- package/dist/workspace/storage-workspace.js +849 -0
- package/dist/workspace/storage-workspace.js.map +1 -0
- package/dist/workspace/storage-workspace.path.d.ts +9 -0
- package/dist/workspace/storage-workspace.path.d.ts.map +1 -0
- package/dist/workspace/storage-workspace.path.js +68 -0
- package/dist/workspace/storage-workspace.path.js.map +1 -0
- package/dist/workspace/storage-workspace.types.d.ts +123 -0
- package/dist/workspace/storage-workspace.types.d.ts.map +1 -0
- package/dist/workspace/storage-workspace.types.js +22 -0
- package/dist/workspace/storage-workspace.types.js.map +1 -0
- package/package.json +103 -35
|
@@ -0,0 +1,1505 @@
|
|
|
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 { createStoredFile, } from 'files-sdk';
|
|
6
|
+
import { mapS3Error, s3 as createFilesSdkS3Adapter, } from 'files-sdk/s3';
|
|
7
|
+
import { StorageError, StorageErrorCode, isStorageError, } from '../../storage.error.js';
|
|
8
|
+
import { normalizeProviderStorageEtag, storageEtagHeader, } from '../../storage-etag.js';
|
|
9
|
+
import { assertFilesSdkS3AdapterHasNoReservedExtensions, createFilesSdkDriver, markFilesSdkS3AdapterUndecorated, markFilesSdkS3RawClientProvenance, mapFilesSdkError, mapStorageErrorToFilesSdkError, } from '../files-sdk.driver.js';
|
|
10
|
+
import { getS3ConstructionMetadata, getS3ConditionalRequestPermission, recordS3ConstructionMetadata, recordS3ConditionalRequestPermission, } from './construction-metadata.js';
|
|
11
|
+
const verifiedS3ProviderProfile = Symbol('verifiedS3ProviderProfile');
|
|
12
|
+
const verifiedS3ProviderProfiles = new WeakSet();
|
|
13
|
+
function assertCapabilityBoolean(value, label) {
|
|
14
|
+
if (typeof value !== 'boolean') {
|
|
15
|
+
throw new TypeError(`${label} must be a boolean.`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/** Validates and deeply freezes an explicitly verified S3-compatible profile. */
|
|
19
|
+
export function defineS3ProviderProfile(profile) {
|
|
20
|
+
if (typeof profile.name !== 'string' || profile.name.trim().length === 0) {
|
|
21
|
+
throw new TypeError('S3 provider profile name must be a non-empty string.');
|
|
22
|
+
}
|
|
23
|
+
if (!Number.isSafeInteger(profile.physicalKey.maxBytes) ||
|
|
24
|
+
profile.physicalKey.maxBytes <= 0) {
|
|
25
|
+
throw new TypeError('S3 provider profile physicalKey.maxBytes must be a positive safe integer.');
|
|
26
|
+
}
|
|
27
|
+
const booleanFields = [
|
|
28
|
+
[
|
|
29
|
+
profile.signedUploadPolicy !== undefined,
|
|
30
|
+
'signedUploadPolicy.contentType',
|
|
31
|
+
profile.signedUploadPolicy?.contentType,
|
|
32
|
+
],
|
|
33
|
+
[
|
|
34
|
+
profile.signedUploadPolicy !== undefined,
|
|
35
|
+
'signedUploadPolicy.sizeRange',
|
|
36
|
+
profile.signedUploadPolicy?.sizeRange,
|
|
37
|
+
],
|
|
38
|
+
[
|
|
39
|
+
profile.conditionalCreate !== undefined,
|
|
40
|
+
'conditionalCreate.resultEtag',
|
|
41
|
+
profile.conditionalCreate?.resultEtag,
|
|
42
|
+
],
|
|
43
|
+
[
|
|
44
|
+
profile.conditionalReplace !== undefined,
|
|
45
|
+
'conditionalReplace.resultEtag',
|
|
46
|
+
profile.conditionalReplace?.resultEtag,
|
|
47
|
+
],
|
|
48
|
+
[
|
|
49
|
+
profile.conditionalDelete !== undefined,
|
|
50
|
+
'conditionalDelete.etag',
|
|
51
|
+
profile.conditionalDelete?.etag,
|
|
52
|
+
],
|
|
53
|
+
[
|
|
54
|
+
profile.conditionalRead !== undefined,
|
|
55
|
+
'conditionalRead.etag',
|
|
56
|
+
profile.conditionalRead?.etag,
|
|
57
|
+
],
|
|
58
|
+
[
|
|
59
|
+
profile.conditionalRead !== undefined,
|
|
60
|
+
'conditionalRead.version',
|
|
61
|
+
profile.conditionalRead?.version,
|
|
62
|
+
],
|
|
63
|
+
[
|
|
64
|
+
profile.conditionalCopySource !== undefined,
|
|
65
|
+
'conditionalCopySource.etag',
|
|
66
|
+
profile.conditionalCopySource?.etag,
|
|
67
|
+
],
|
|
68
|
+
[
|
|
69
|
+
profile.conditionalCopySource !== undefined,
|
|
70
|
+
'conditionalCopySource.version',
|
|
71
|
+
profile.conditionalCopySource?.version,
|
|
72
|
+
],
|
|
73
|
+
[
|
|
74
|
+
profile.conditionalCopySource?.requiresDestinationPredicate !== undefined,
|
|
75
|
+
'conditionalCopySource.requiresDestinationPredicate',
|
|
76
|
+
profile.conditionalCopySource?.requiresDestinationPredicate,
|
|
77
|
+
],
|
|
78
|
+
[
|
|
79
|
+
profile.conditionalCopyDestination !== undefined,
|
|
80
|
+
'conditionalCopyDestination.create',
|
|
81
|
+
profile.conditionalCopyDestination?.create,
|
|
82
|
+
],
|
|
83
|
+
[
|
|
84
|
+
profile.conditionalCopyDestination !== undefined,
|
|
85
|
+
'conditionalCopyDestination.replace',
|
|
86
|
+
profile.conditionalCopyDestination?.replace,
|
|
87
|
+
],
|
|
88
|
+
[
|
|
89
|
+
profile.conditionalCopyDestination !== undefined,
|
|
90
|
+
'conditionalCopyDestination.atomicWithSource',
|
|
91
|
+
profile.conditionalCopyDestination?.atomicWithSource,
|
|
92
|
+
],
|
|
93
|
+
[
|
|
94
|
+
profile.conditionalCopyDestination?.requiresSourcePredicate !== undefined,
|
|
95
|
+
'conditionalCopyDestination.requiresSourcePredicate',
|
|
96
|
+
profile.conditionalCopyDestination?.requiresSourcePredicate,
|
|
97
|
+
],
|
|
98
|
+
[
|
|
99
|
+
profile.conditionalMultipartCompletion !== undefined,
|
|
100
|
+
'conditionalMultipartCompletion.create',
|
|
101
|
+
profile.conditionalMultipartCompletion?.create,
|
|
102
|
+
],
|
|
103
|
+
[
|
|
104
|
+
profile.conditionalMultipartCompletion !== undefined,
|
|
105
|
+
'conditionalMultipartCompletion.replace',
|
|
106
|
+
profile.conditionalMultipartCompletion?.replace,
|
|
107
|
+
],
|
|
108
|
+
];
|
|
109
|
+
for (const [declared, label, value] of booleanFields) {
|
|
110
|
+
if (declared)
|
|
111
|
+
assertCapabilityBoolean(value, label);
|
|
112
|
+
}
|
|
113
|
+
if (profile.conditionalCopyDestination?.atomicWithSource === true &&
|
|
114
|
+
profile.conditionalCopySource?.etag !== true &&
|
|
115
|
+
profile.conditionalCopySource?.version !== true) {
|
|
116
|
+
throw new TypeError('An atomic destination-copy profile must enable at least one source-copy condition.');
|
|
117
|
+
}
|
|
118
|
+
if (profile.conditionalCopyDestination?.atomicWithSource === true &&
|
|
119
|
+
profile.conditionalCopyDestination.create !== true &&
|
|
120
|
+
profile.conditionalCopyDestination.replace !== true) {
|
|
121
|
+
throw new TypeError('An atomic destination-copy profile must declare create or replace support.');
|
|
122
|
+
}
|
|
123
|
+
if (profile.conditionalMultipartCompletion?.create === true &&
|
|
124
|
+
profile.conditionalCreate === undefined) {
|
|
125
|
+
throw new TypeError('Conditional multipart create requires conditional create support.');
|
|
126
|
+
}
|
|
127
|
+
if (profile.conditionalMultipartCompletion?.replace === true &&
|
|
128
|
+
profile.conditionalReplace === undefined) {
|
|
129
|
+
throw new TypeError('Conditional multipart replace requires conditional replace support.');
|
|
130
|
+
}
|
|
131
|
+
const clone = {
|
|
132
|
+
name: profile.name.trim(),
|
|
133
|
+
physicalKey: Object.freeze({ ...profile.physicalKey }),
|
|
134
|
+
signedUploadPolicy: Object.freeze(profile.signedUploadPolicy === undefined
|
|
135
|
+
? { contentType: false, sizeRange: false }
|
|
136
|
+
: { ...profile.signedUploadPolicy }),
|
|
137
|
+
...(profile.conditionalCreate !== undefined && {
|
|
138
|
+
conditionalCreate: Object.freeze({ ...profile.conditionalCreate }),
|
|
139
|
+
}),
|
|
140
|
+
...(profile.conditionalReplace !== undefined && {
|
|
141
|
+
conditionalReplace: Object.freeze({ ...profile.conditionalReplace }),
|
|
142
|
+
}),
|
|
143
|
+
...(profile.conditionalDelete !== undefined && {
|
|
144
|
+
conditionalDelete: Object.freeze({ ...profile.conditionalDelete }),
|
|
145
|
+
}),
|
|
146
|
+
...(profile.conditionalRead !== undefined && {
|
|
147
|
+
conditionalRead: Object.freeze({ ...profile.conditionalRead }),
|
|
148
|
+
}),
|
|
149
|
+
...(profile.conditionalCopySource !== undefined && {
|
|
150
|
+
conditionalCopySource: Object.freeze({
|
|
151
|
+
...profile.conditionalCopySource,
|
|
152
|
+
}),
|
|
153
|
+
}),
|
|
154
|
+
...(profile.conditionalCopyDestination !== undefined && {
|
|
155
|
+
conditionalCopyDestination: Object.freeze({
|
|
156
|
+
...profile.conditionalCopyDestination,
|
|
157
|
+
}),
|
|
158
|
+
}),
|
|
159
|
+
...(profile.conditionalMultipartCompletion !== undefined && {
|
|
160
|
+
conditionalMultipartCompletion: Object.freeze({
|
|
161
|
+
...profile.conditionalMultipartCompletion,
|
|
162
|
+
}),
|
|
163
|
+
}),
|
|
164
|
+
};
|
|
165
|
+
Object.defineProperty(clone, verifiedS3ProviderProfile, {
|
|
166
|
+
configurable: false,
|
|
167
|
+
enumerable: false,
|
|
168
|
+
value: true,
|
|
169
|
+
writable: false,
|
|
170
|
+
});
|
|
171
|
+
const verified = Object.freeze(clone);
|
|
172
|
+
verifiedS3ProviderProfiles.add(verified);
|
|
173
|
+
return verified;
|
|
174
|
+
}
|
|
175
|
+
function assertVerifiedS3ProviderProfile(profile) {
|
|
176
|
+
if (typeof profile !== 'object' ||
|
|
177
|
+
profile === null ||
|
|
178
|
+
!verifiedS3ProviderProfiles.has(profile)) {
|
|
179
|
+
throw new TypeError('providerProfile must be created with defineS3ProviderProfile().');
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
function assertS3ProviderProfileContainedBy(profile, ceiling) {
|
|
183
|
+
if (profile.physicalKey.maxBytes > ceiling.physicalKey.maxBytes) {
|
|
184
|
+
throw new TypeError(`S3 provider profile "${profile.name}" cannot widen ${ceiling.name} physicalKey.maxBytes beyond ${ceiling.physicalKey.maxBytes}.`);
|
|
185
|
+
}
|
|
186
|
+
const operationCapabilities = [
|
|
187
|
+
['conditionalCreate', profile.conditionalCreate, ceiling.conditionalCreate],
|
|
188
|
+
[
|
|
189
|
+
'conditionalReplace',
|
|
190
|
+
profile.conditionalReplace,
|
|
191
|
+
ceiling.conditionalReplace,
|
|
192
|
+
],
|
|
193
|
+
['conditionalDelete', profile.conditionalDelete, ceiling.conditionalDelete],
|
|
194
|
+
['conditionalRead', profile.conditionalRead, ceiling.conditionalRead],
|
|
195
|
+
[
|
|
196
|
+
'conditionalCopySource',
|
|
197
|
+
profile.conditionalCopySource,
|
|
198
|
+
ceiling.conditionalCopySource,
|
|
199
|
+
],
|
|
200
|
+
[
|
|
201
|
+
'conditionalCopyDestination',
|
|
202
|
+
profile.conditionalCopyDestination,
|
|
203
|
+
ceiling.conditionalCopyDestination,
|
|
204
|
+
],
|
|
205
|
+
[
|
|
206
|
+
'conditionalMultipartCompletion',
|
|
207
|
+
profile.conditionalMultipartCompletion,
|
|
208
|
+
ceiling.conditionalMultipartCompletion,
|
|
209
|
+
],
|
|
210
|
+
];
|
|
211
|
+
for (const [name, declared, supported] of operationCapabilities) {
|
|
212
|
+
if (declared !== undefined && supported === undefined) {
|
|
213
|
+
throw new TypeError(`S3 provider profile "${profile.name}" cannot widen ${ceiling.name} with ${name}.`);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
const booleanClaims = [
|
|
217
|
+
[
|
|
218
|
+
'signedUploadPolicy.contentType',
|
|
219
|
+
profile.signedUploadPolicy.contentType,
|
|
220
|
+
ceiling.signedUploadPolicy.contentType,
|
|
221
|
+
],
|
|
222
|
+
[
|
|
223
|
+
'signedUploadPolicy.sizeRange',
|
|
224
|
+
profile.signedUploadPolicy.sizeRange,
|
|
225
|
+
ceiling.signedUploadPolicy.sizeRange,
|
|
226
|
+
],
|
|
227
|
+
[
|
|
228
|
+
'conditionalCreate.resultEtag',
|
|
229
|
+
profile.conditionalCreate?.resultEtag,
|
|
230
|
+
ceiling.conditionalCreate?.resultEtag,
|
|
231
|
+
],
|
|
232
|
+
[
|
|
233
|
+
'conditionalReplace.resultEtag',
|
|
234
|
+
profile.conditionalReplace?.resultEtag,
|
|
235
|
+
ceiling.conditionalReplace?.resultEtag,
|
|
236
|
+
],
|
|
237
|
+
[
|
|
238
|
+
'conditionalDelete.etag',
|
|
239
|
+
profile.conditionalDelete?.etag,
|
|
240
|
+
ceiling.conditionalDelete?.etag,
|
|
241
|
+
],
|
|
242
|
+
[
|
|
243
|
+
'conditionalRead.etag',
|
|
244
|
+
profile.conditionalRead?.etag,
|
|
245
|
+
ceiling.conditionalRead?.etag,
|
|
246
|
+
],
|
|
247
|
+
[
|
|
248
|
+
'conditionalRead.version',
|
|
249
|
+
profile.conditionalRead?.version,
|
|
250
|
+
ceiling.conditionalRead?.version,
|
|
251
|
+
],
|
|
252
|
+
[
|
|
253
|
+
'conditionalCopySource.etag',
|
|
254
|
+
profile.conditionalCopySource?.etag,
|
|
255
|
+
ceiling.conditionalCopySource?.etag,
|
|
256
|
+
],
|
|
257
|
+
[
|
|
258
|
+
'conditionalCopySource.version',
|
|
259
|
+
profile.conditionalCopySource?.version,
|
|
260
|
+
ceiling.conditionalCopySource?.version,
|
|
261
|
+
],
|
|
262
|
+
[
|
|
263
|
+
'conditionalCopyDestination.create',
|
|
264
|
+
profile.conditionalCopyDestination?.create,
|
|
265
|
+
ceiling.conditionalCopyDestination?.create,
|
|
266
|
+
],
|
|
267
|
+
[
|
|
268
|
+
'conditionalCopyDestination.replace',
|
|
269
|
+
profile.conditionalCopyDestination?.replace,
|
|
270
|
+
ceiling.conditionalCopyDestination?.replace,
|
|
271
|
+
],
|
|
272
|
+
[
|
|
273
|
+
'conditionalCopyDestination.atomicWithSource',
|
|
274
|
+
profile.conditionalCopyDestination?.atomicWithSource,
|
|
275
|
+
ceiling.conditionalCopyDestination?.atomicWithSource,
|
|
276
|
+
],
|
|
277
|
+
[
|
|
278
|
+
'conditionalMultipartCompletion.create',
|
|
279
|
+
profile.conditionalMultipartCompletion?.create,
|
|
280
|
+
ceiling.conditionalMultipartCompletion?.create,
|
|
281
|
+
],
|
|
282
|
+
[
|
|
283
|
+
'conditionalMultipartCompletion.replace',
|
|
284
|
+
profile.conditionalMultipartCompletion?.replace,
|
|
285
|
+
ceiling.conditionalMultipartCompletion?.replace,
|
|
286
|
+
],
|
|
287
|
+
];
|
|
288
|
+
for (const [name, declared, supported] of booleanClaims) {
|
|
289
|
+
if (declared === true && supported !== true) {
|
|
290
|
+
throw new TypeError(`S3 provider profile "${profile.name}" cannot widen ${ceiling.name} with ${name}.`);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
if (profile.conditionalCopySource !== undefined &&
|
|
294
|
+
(profile.conditionalCopySource.etag ||
|
|
295
|
+
profile.conditionalCopySource.version) &&
|
|
296
|
+
ceiling.conditionalCopySource?.requiresDestinationPredicate === true &&
|
|
297
|
+
profile.conditionalCopySource.requiresDestinationPredicate !== true) {
|
|
298
|
+
throw new TypeError(`S3 provider profile "${profile.name}" cannot remove ${ceiling.name} conditionalCopySource.requiresDestinationPredicate.`);
|
|
299
|
+
}
|
|
300
|
+
if (profile.conditionalCopyDestination !== undefined &&
|
|
301
|
+
(profile.conditionalCopyDestination.create ||
|
|
302
|
+
profile.conditionalCopyDestination.replace) &&
|
|
303
|
+
ceiling.conditionalCopyDestination?.requiresSourcePredicate === true &&
|
|
304
|
+
profile.conditionalCopyDestination.requiresSourcePredicate !== true) {
|
|
305
|
+
throw new TypeError(`S3 provider profile "${profile.name}" cannot remove ${ceiling.name} conditionalCopyDestination.requiresSourcePredicate.`);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
export const AWS_S3_PROVIDER_PROFILE = defineS3ProviderProfile({
|
|
309
|
+
name: 'aws-s3-general-purpose',
|
|
310
|
+
physicalKey: { maxBytes: 1024 },
|
|
311
|
+
signedUploadPolicy: { contentType: true, sizeRange: true },
|
|
312
|
+
conditionalCreate: { resultEtag: true },
|
|
313
|
+
conditionalReplace: { resultEtag: true },
|
|
314
|
+
conditionalDelete: { etag: true },
|
|
315
|
+
conditionalRead: { etag: true, version: true },
|
|
316
|
+
conditionalCopySource: { etag: true, version: true },
|
|
317
|
+
conditionalCopyDestination: {
|
|
318
|
+
atomicWithSource: true,
|
|
319
|
+
create: true,
|
|
320
|
+
replace: true,
|
|
321
|
+
},
|
|
322
|
+
conditionalMultipartCompletion: { create: true, replace: true },
|
|
323
|
+
});
|
|
324
|
+
export const CLOUDFLARE_R2_PROVIDER_PROFILE = defineS3ProviderProfile({
|
|
325
|
+
name: 'cloudflare-r2-stable',
|
|
326
|
+
physicalKey: { maxBytes: 1024 },
|
|
327
|
+
signedUploadPolicy: { contentType: true, sizeRange: false },
|
|
328
|
+
conditionalCreate: { resultEtag: true },
|
|
329
|
+
conditionalReplace: { resultEtag: true },
|
|
330
|
+
conditionalRead: { etag: true, version: false },
|
|
331
|
+
conditionalCopySource: { etag: true, version: false },
|
|
332
|
+
});
|
|
333
|
+
const UNVERIFIED_S3_PROVIDER_PROFILE = defineS3ProviderProfile({
|
|
334
|
+
name: 'unverified-s3-compatible',
|
|
335
|
+
physicalKey: { maxBytes: 1024 },
|
|
336
|
+
signedUploadPolicy: { contentType: false, sizeRange: false },
|
|
337
|
+
});
|
|
338
|
+
function filesAttemptOptions(options) {
|
|
339
|
+
if (options === undefined)
|
|
340
|
+
return undefined;
|
|
341
|
+
// Files owns retry settlement. Its adapter primitives receive a single
|
|
342
|
+
// merged attempt signal and must not start a second retry loop.
|
|
343
|
+
const { retries: _retries, ...attempt } = options;
|
|
344
|
+
return attempt;
|
|
345
|
+
}
|
|
346
|
+
async function throughFilesConditionalBoundary(operationName, operation) {
|
|
347
|
+
try {
|
|
348
|
+
return await operation();
|
|
349
|
+
}
|
|
350
|
+
catch (error) {
|
|
351
|
+
throw mapStorageErrorToFilesSdkError(mapS3ConditionalError(error, operationName));
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
function storedFileOfStorageObject(object) {
|
|
355
|
+
return createStoredFile({
|
|
356
|
+
...(object.etag !== undefined && { etag: object.etag }),
|
|
357
|
+
key: object.key,
|
|
358
|
+
...(object.lastModified !== undefined && {
|
|
359
|
+
lastModified: object.lastModified.getTime(),
|
|
360
|
+
}),
|
|
361
|
+
...(object.metadata !== undefined && {
|
|
362
|
+
metadata: { ...object.metadata },
|
|
363
|
+
}),
|
|
364
|
+
size: object.size,
|
|
365
|
+
type: object.contentType,
|
|
366
|
+
}, { factory: () => object.body, kind: 'stream' });
|
|
367
|
+
}
|
|
368
|
+
function copySource(bucket, key, version) {
|
|
369
|
+
const source = `${encodeURIComponent(bucket)}/${encodeURIComponent(key)}`;
|
|
370
|
+
return version === undefined
|
|
371
|
+
? source
|
|
372
|
+
: `${source}?versionId=${encodeURIComponent(version)}`;
|
|
373
|
+
}
|
|
374
|
+
function operationSignal(options) {
|
|
375
|
+
const timeoutSignal = options.timeout === undefined || options.timeout <= 0
|
|
376
|
+
? undefined
|
|
377
|
+
: AbortSignal.timeout(options.timeout);
|
|
378
|
+
if (options.signal === undefined) {
|
|
379
|
+
return timeoutSignal;
|
|
380
|
+
}
|
|
381
|
+
return timeoutSignal === undefined
|
|
382
|
+
? options.signal
|
|
383
|
+
: AbortSignal.any([options.signal, timeoutSignal]);
|
|
384
|
+
}
|
|
385
|
+
function maxRetries(options) {
|
|
386
|
+
const configured = typeof options.retries === 'number'
|
|
387
|
+
? options.retries
|
|
388
|
+
: options.retries?.max;
|
|
389
|
+
return Math.max(0, Math.floor(configured ?? 0));
|
|
390
|
+
}
|
|
391
|
+
function invalidS3Argument(message, operation, key) {
|
|
392
|
+
throw new StorageError(message, {
|
|
393
|
+
code: StorageErrorCode.INVALID_ARGUMENT,
|
|
394
|
+
...(typeof key === 'string' && { key }),
|
|
395
|
+
operation,
|
|
396
|
+
permanent: true,
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
function assertS3Key(key, label, operation) {
|
|
400
|
+
if (typeof key !== 'string' || key.length === 0) {
|
|
401
|
+
invalidS3Argument(`${label} must be a non-empty string.`, operation, key);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
function etagHeader(etag, key, operation, label) {
|
|
405
|
+
const header = storageEtagHeader(etag);
|
|
406
|
+
if (header === undefined) {
|
|
407
|
+
throw new StorageError(`${label} must be a canonical storage ETag.`, {
|
|
408
|
+
code: StorageErrorCode.INVALID_ARGUMENT,
|
|
409
|
+
key,
|
|
410
|
+
operation,
|
|
411
|
+
permanent: true,
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
return header;
|
|
415
|
+
}
|
|
416
|
+
function assertConditionalReadInput(key, options) {
|
|
417
|
+
const candidate = options;
|
|
418
|
+
if (typeof candidate !== 'object' || candidate === null) {
|
|
419
|
+
invalidS3Argument('Conditional read options must be an object.', 'download', key);
|
|
420
|
+
}
|
|
421
|
+
const condition = candidate.condition;
|
|
422
|
+
if (typeof condition !== 'object' || condition === null) {
|
|
423
|
+
invalidS3Argument('Conditional read requires an ETag or version predicate.', 'download', key);
|
|
424
|
+
}
|
|
425
|
+
const { etag, version } = condition;
|
|
426
|
+
if (etag === undefined && version === undefined) {
|
|
427
|
+
invalidS3Argument('Conditional read requires an ETag or version predicate.', 'download', key);
|
|
428
|
+
}
|
|
429
|
+
if (etag !== undefined) {
|
|
430
|
+
etagHeader(etag, key, 'download', 'condition.etag');
|
|
431
|
+
}
|
|
432
|
+
if (version !== undefined &&
|
|
433
|
+
(typeof version !== 'string' || version.length === 0)) {
|
|
434
|
+
invalidS3Argument('condition.version must be a non-empty string.', 'download', key);
|
|
435
|
+
}
|
|
436
|
+
const range = candidate.range;
|
|
437
|
+
if (range === undefined)
|
|
438
|
+
return;
|
|
439
|
+
if (typeof range !== 'object' || range === null) {
|
|
440
|
+
invalidS3Argument('range must be an object.', 'download', key);
|
|
441
|
+
}
|
|
442
|
+
const { end, start } = range;
|
|
443
|
+
if (!Number.isSafeInteger(start) || start < 0) {
|
|
444
|
+
invalidS3Argument('range.start must be a non-negative safe integer.', 'download', key);
|
|
445
|
+
}
|
|
446
|
+
if (end !== undefined &&
|
|
447
|
+
(!Number.isSafeInteger(end) || end < start)) {
|
|
448
|
+
invalidS3Argument('range.end must be a safe integer greater than or equal to range.start.', 'download', key);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
function assertPromotionInput(sourceKey, destinationKey, promotion) {
|
|
452
|
+
const candidate = promotion;
|
|
453
|
+
if (typeof candidate !== 'object' || candidate === null) {
|
|
454
|
+
invalidS3Argument('Promotion options must be an object.', 'copy', sourceKey);
|
|
455
|
+
}
|
|
456
|
+
const { destination, sourceEtag, sourceVersion } = candidate;
|
|
457
|
+
if (sourceEtag !== undefined) {
|
|
458
|
+
etagHeader(sourceEtag, sourceKey, 'copy', 'sourceEtag');
|
|
459
|
+
}
|
|
460
|
+
if (sourceVersion !== undefined &&
|
|
461
|
+
(typeof sourceVersion !== 'string' || sourceVersion.length === 0)) {
|
|
462
|
+
invalidS3Argument('sourceVersion must be a non-empty string.', 'copy', sourceKey);
|
|
463
|
+
}
|
|
464
|
+
if (destination !== undefined &&
|
|
465
|
+
(typeof destination !== 'object' ||
|
|
466
|
+
destination === null ||
|
|
467
|
+
(destination.type !== 'create' &&
|
|
468
|
+
destination.type !== 'replace'))) {
|
|
469
|
+
invalidS3Argument('destination.type must be "create" or "replace".', 'copy', destinationKey);
|
|
470
|
+
}
|
|
471
|
+
if (typeof destination === 'object' &&
|
|
472
|
+
destination !== null &&
|
|
473
|
+
destination.type === 'replace') {
|
|
474
|
+
etagHeader(destination.etag, destinationKey, 'copy', 'destination.etag');
|
|
475
|
+
}
|
|
476
|
+
if (sourceEtag === undefined &&
|
|
477
|
+
sourceVersion === undefined &&
|
|
478
|
+
destination === undefined) {
|
|
479
|
+
invalidS3Argument('promote requires a source or destination precondition.', 'copy', sourceKey);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
function assertConditionalUploadInput(key, conditional) {
|
|
483
|
+
const candidate = conditional;
|
|
484
|
+
if (typeof candidate !== 'object' || candidate === null) {
|
|
485
|
+
invalidS3Argument('Conditional upload options must be an object.', 'upload', key);
|
|
486
|
+
}
|
|
487
|
+
const condition = candidate.condition;
|
|
488
|
+
if (typeof condition !== 'object' ||
|
|
489
|
+
condition === null ||
|
|
490
|
+
(condition.type !== 'create' &&
|
|
491
|
+
condition.type !== 'replace')) {
|
|
492
|
+
invalidS3Argument('condition.type must be "create" or "replace".', 'upload', key);
|
|
493
|
+
}
|
|
494
|
+
if (condition.type === 'replace') {
|
|
495
|
+
etagHeader(condition.etag, key, 'upload', 'condition.etag');
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
function providerEtag(etag, key, operation) {
|
|
499
|
+
if (etag === undefined) {
|
|
500
|
+
return undefined;
|
|
501
|
+
}
|
|
502
|
+
const normalized = normalizeProviderStorageEtag(etag);
|
|
503
|
+
if (normalized === undefined) {
|
|
504
|
+
throw new StorageError('S3 returned an invalid ETag.', {
|
|
505
|
+
code: StorageErrorCode.PROVIDER,
|
|
506
|
+
key,
|
|
507
|
+
operation,
|
|
508
|
+
permanent: true,
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
return normalized;
|
|
512
|
+
}
|
|
513
|
+
function contentTypeOf(body, override) {
|
|
514
|
+
if (override !== undefined) {
|
|
515
|
+
return override;
|
|
516
|
+
}
|
|
517
|
+
if (typeof body === 'string') {
|
|
518
|
+
return 'text/plain; charset=utf-8';
|
|
519
|
+
}
|
|
520
|
+
if (body instanceof Blob && body.type.length > 0) {
|
|
521
|
+
return body.type;
|
|
522
|
+
}
|
|
523
|
+
return 'application/octet-stream';
|
|
524
|
+
}
|
|
525
|
+
function assertBufferedFilesConditionalS3Body(body, key) {
|
|
526
|
+
if (body instanceof ReadableStream || body instanceof Readable) {
|
|
527
|
+
throw new StorageError('Conditional S3 uploads through Files require a buffered body.', {
|
|
528
|
+
code: StorageErrorCode.PROVIDER,
|
|
529
|
+
key,
|
|
530
|
+
operation: 'upload',
|
|
531
|
+
permanent: true,
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
async function normalizeConditionalBody(body, onProgress) {
|
|
536
|
+
let bytes;
|
|
537
|
+
if (typeof body === 'string') {
|
|
538
|
+
bytes = new TextEncoder().encode(body);
|
|
539
|
+
}
|
|
540
|
+
else if (body instanceof Uint8Array) {
|
|
541
|
+
bytes = body;
|
|
542
|
+
}
|
|
543
|
+
else if (body instanceof ArrayBuffer) {
|
|
544
|
+
bytes = new Uint8Array(body);
|
|
545
|
+
}
|
|
546
|
+
else if (ArrayBuffer.isView(body)) {
|
|
547
|
+
bytes = new Uint8Array(body.buffer, body.byteOffset, body.byteLength);
|
|
548
|
+
}
|
|
549
|
+
else if (body instanceof Blob) {
|
|
550
|
+
bytes = new Uint8Array(await body.arrayBuffer());
|
|
551
|
+
}
|
|
552
|
+
if (bytes !== undefined) {
|
|
553
|
+
const total = bytes.byteLength;
|
|
554
|
+
onProgress?.({ loaded: 0, total });
|
|
555
|
+
return { body: bytes, contentLength: total, size: () => total };
|
|
556
|
+
}
|
|
557
|
+
const stream = body instanceof Readable
|
|
558
|
+
? Readable.toWeb(body)
|
|
559
|
+
: body;
|
|
560
|
+
if (!(stream instanceof ReadableStream)) {
|
|
561
|
+
throw new StorageError('Unsupported conditional upload body.', {
|
|
562
|
+
code: StorageErrorCode.INVALID_ARGUMENT,
|
|
563
|
+
operation: 'upload',
|
|
564
|
+
permanent: true,
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
const reader = stream.getReader();
|
|
568
|
+
let loaded = 0;
|
|
569
|
+
return {
|
|
570
|
+
body: new ReadableStream({
|
|
571
|
+
cancel(reason) {
|
|
572
|
+
return reader.cancel(reason);
|
|
573
|
+
},
|
|
574
|
+
async pull(controller) {
|
|
575
|
+
const result = await reader.read();
|
|
576
|
+
if (result.done) {
|
|
577
|
+
controller.close();
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
loaded += result.value.byteLength;
|
|
581
|
+
controller.enqueue(result.value);
|
|
582
|
+
onProgress?.({ loaded });
|
|
583
|
+
},
|
|
584
|
+
}),
|
|
585
|
+
size: () => loaded,
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
function s3ErrorIdentity(error) {
|
|
589
|
+
let current = error;
|
|
590
|
+
const seen = new Set();
|
|
591
|
+
for (let depth = 0; depth < 8; depth += 1) {
|
|
592
|
+
if (typeof current !== 'object' || current === null || seen.has(current)) {
|
|
593
|
+
break;
|
|
594
|
+
}
|
|
595
|
+
seen.add(current);
|
|
596
|
+
const candidate = current;
|
|
597
|
+
const serviceCode = candidate.name ?? candidate.Code ?? candidate.code;
|
|
598
|
+
const status = candidate.$metadata?.httpStatusCode;
|
|
599
|
+
if (typeof status === 'number' ||
|
|
600
|
+
(typeof serviceCode === 'string' && serviceCode !== 'FilesError')) {
|
|
601
|
+
return {
|
|
602
|
+
code: typeof serviceCode === 'string' ? serviceCode : undefined,
|
|
603
|
+
status: typeof status === 'number' ? status : undefined,
|
|
604
|
+
};
|
|
605
|
+
}
|
|
606
|
+
current = candidate.cause;
|
|
607
|
+
}
|
|
608
|
+
return { code: undefined, status: undefined };
|
|
609
|
+
}
|
|
610
|
+
function mapS3ConditionalError(error, operation) {
|
|
611
|
+
if (isStorageError(error)) {
|
|
612
|
+
return error;
|
|
613
|
+
}
|
|
614
|
+
const identity = s3ErrorIdentity(error);
|
|
615
|
+
const provider = mapFilesSdkError(mapS3Error(error));
|
|
616
|
+
const precondition = identity.status === 412 || identity.code === 'PreconditionFailed';
|
|
617
|
+
const conditionalConflict = identity.status === 409 || identity.code === 'ConditionalRequestConflict';
|
|
618
|
+
const mappedPrecondition = provider.code === StorageErrorCode.CONFLICT && !conditionalConflict;
|
|
619
|
+
const notFound = identity.status === 404 ||
|
|
620
|
+
identity.code === 'NoSuchKey' ||
|
|
621
|
+
identity.code === 'NoSuchUpload' ||
|
|
622
|
+
provider.code === StorageErrorCode.NOT_FOUND;
|
|
623
|
+
const code = precondition || mappedPrecondition || conditionalConflict
|
|
624
|
+
? StorageErrorCode.CONFLICT
|
|
625
|
+
: notFound
|
|
626
|
+
? StorageErrorCode.NOT_FOUND
|
|
627
|
+
: provider.code;
|
|
628
|
+
return new StorageError(`Conditional S3 ${operation} failed.`, {
|
|
629
|
+
aborted: provider.aborted,
|
|
630
|
+
applied: provider.applied,
|
|
631
|
+
...(provider.appliedEtag !== undefined && {
|
|
632
|
+
appliedEtag: provider.appliedEtag,
|
|
633
|
+
}),
|
|
634
|
+
code,
|
|
635
|
+
operation,
|
|
636
|
+
permanent: precondition || mappedPrecondition || notFound
|
|
637
|
+
? true
|
|
638
|
+
: conditionalConflict
|
|
639
|
+
? false
|
|
640
|
+
: provider.permanent,
|
|
641
|
+
timedOut: provider.timedOut,
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
function s3InterruptionError(options, signal, operation) {
|
|
645
|
+
if (signal?.aborted !== true)
|
|
646
|
+
return undefined;
|
|
647
|
+
const timedOut = options.signal?.aborted !== true;
|
|
648
|
+
return new StorageError(timedOut
|
|
649
|
+
? `Conditional S3 ${operation} timed out.`
|
|
650
|
+
: `Conditional S3 ${operation} was aborted.`, {
|
|
651
|
+
aborted: !timedOut,
|
|
652
|
+
code: timedOut ? StorageErrorCode.TIMEOUT : StorageErrorCode.ABORTED,
|
|
653
|
+
operation,
|
|
654
|
+
timedOut,
|
|
655
|
+
});
|
|
656
|
+
}
|
|
657
|
+
async function withS3Retry(options, operationName, operation) {
|
|
658
|
+
const retries = maxRetries(options);
|
|
659
|
+
for (let attempt = 0;; attempt += 1) {
|
|
660
|
+
const signal = operationSignal(options);
|
|
661
|
+
try {
|
|
662
|
+
return await operation(signal);
|
|
663
|
+
}
|
|
664
|
+
catch (error) {
|
|
665
|
+
const interrupted = s3InterruptionError(options, signal, operationName);
|
|
666
|
+
if (interrupted !== undefined)
|
|
667
|
+
throw interrupted;
|
|
668
|
+
const mapped = mapS3ConditionalError(error, operationName);
|
|
669
|
+
const retryableCode = mapped.code === StorageErrorCode.PROVIDER ||
|
|
670
|
+
mapped.code === StorageErrorCode.CONFLICT;
|
|
671
|
+
if (attempt >= retries ||
|
|
672
|
+
!retryableCode ||
|
|
673
|
+
mapped.aborted ||
|
|
674
|
+
mapped.permanent ||
|
|
675
|
+
signal?.aborted === true) {
|
|
676
|
+
throw mapped;
|
|
677
|
+
}
|
|
678
|
+
const delay = typeof options.retries === 'object' &&
|
|
679
|
+
options.retries.backoff !== undefined
|
|
680
|
+
? options.retries.backoff({
|
|
681
|
+
attempt: attempt + 1,
|
|
682
|
+
error: mapped,
|
|
683
|
+
})
|
|
684
|
+
: Math.min(1000, 100 * 2 ** attempt);
|
|
685
|
+
try {
|
|
686
|
+
await waitForRetry(delay, signal);
|
|
687
|
+
}
|
|
688
|
+
catch (waitError) {
|
|
689
|
+
throw (s3InterruptionError(options, signal, operationName) ??
|
|
690
|
+
mapS3ConditionalError(waitError, operationName));
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
async function waitForRetry(milliseconds, signal) {
|
|
696
|
+
if (signal?.aborted === true) {
|
|
697
|
+
throw signal.reason;
|
|
698
|
+
}
|
|
699
|
+
await new Promise((resolve, reject) => {
|
|
700
|
+
const cleanup = () => signal?.removeEventListener('abort', abort);
|
|
701
|
+
const timer = setTimeout(() => {
|
|
702
|
+
cleanup();
|
|
703
|
+
resolve();
|
|
704
|
+
}, Math.max(0, milliseconds));
|
|
705
|
+
const abort = () => {
|
|
706
|
+
clearTimeout(timer);
|
|
707
|
+
cleanup();
|
|
708
|
+
reject(signal?.reason);
|
|
709
|
+
};
|
|
710
|
+
signal?.addEventListener('abort', abort, { once: true });
|
|
711
|
+
if (signal?.aborted === true)
|
|
712
|
+
abort();
|
|
713
|
+
});
|
|
714
|
+
}
|
|
715
|
+
const S3_MIN_MULTIPART_PART_BYTES = 5 * 1024 * 1024;
|
|
716
|
+
const S3_MAX_MULTIPART_PARTS = 10_000;
|
|
717
|
+
const S3_MULTIPART_CLEANUP_TIMEOUT_MS = 5_000;
|
|
718
|
+
async function waitForS3Cleanup(request, signal) {
|
|
719
|
+
return new Promise((resolve, reject) => {
|
|
720
|
+
const cleanup = () => signal.removeEventListener('abort', abort);
|
|
721
|
+
const abort = () => {
|
|
722
|
+
cleanup();
|
|
723
|
+
reject(signal.reason);
|
|
724
|
+
};
|
|
725
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
726
|
+
void request.then((result) => {
|
|
727
|
+
cleanup();
|
|
728
|
+
resolve(result);
|
|
729
|
+
}, (error) => {
|
|
730
|
+
cleanup();
|
|
731
|
+
reject(error);
|
|
732
|
+
});
|
|
733
|
+
if (signal.aborted)
|
|
734
|
+
abort();
|
|
735
|
+
});
|
|
736
|
+
}
|
|
737
|
+
async function abortMultipartUpload(base, key, uploadId, callerSignal) {
|
|
738
|
+
const cleanupController = new AbortController();
|
|
739
|
+
const cleanupTimeout = setTimeout(() => cleanupController.abort(), S3_MULTIPART_CLEANUP_TIMEOUT_MS);
|
|
740
|
+
const cleanupSignal = callerSignal === undefined
|
|
741
|
+
? cleanupController.signal
|
|
742
|
+
: AbortSignal.any([callerSignal, cleanupController.signal]);
|
|
743
|
+
try {
|
|
744
|
+
await waitForS3Cleanup(base.raw.send(new AbortMultipartUploadCommand({
|
|
745
|
+
Bucket: base.bucket,
|
|
746
|
+
Key: key,
|
|
747
|
+
UploadId: uploadId,
|
|
748
|
+
}), { abortSignal: cleanupSignal }), cleanupSignal);
|
|
749
|
+
}
|
|
750
|
+
finally {
|
|
751
|
+
clearTimeout(cleanupTimeout);
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
function conditionalHeaders(condition, key) {
|
|
755
|
+
return condition.type === 'create'
|
|
756
|
+
? { IfNoneMatch: '*' }
|
|
757
|
+
: {
|
|
758
|
+
IfMatch: etagHeader(condition.etag, key, 'upload', 'condition.etag'),
|
|
759
|
+
};
|
|
760
|
+
}
|
|
761
|
+
function rangeHeader(range) {
|
|
762
|
+
if (range === undefined)
|
|
763
|
+
return undefined;
|
|
764
|
+
return `bytes=${range.start}-${range.end ?? ''}`;
|
|
765
|
+
}
|
|
766
|
+
function s3ResponseStream(body) {
|
|
767
|
+
let stream;
|
|
768
|
+
if (body instanceof ReadableStream) {
|
|
769
|
+
stream = body;
|
|
770
|
+
}
|
|
771
|
+
else if (body instanceof Readable) {
|
|
772
|
+
stream = Readable.toWeb(body);
|
|
773
|
+
}
|
|
774
|
+
else if (typeof body === 'object' &&
|
|
775
|
+
body !== null &&
|
|
776
|
+
'transformToWebStream' in body &&
|
|
777
|
+
typeof body.transformToWebStream === 'function') {
|
|
778
|
+
stream = body.transformToWebStream();
|
|
779
|
+
}
|
|
780
|
+
else {
|
|
781
|
+
throw new StorageError('Conditional S3 download returned no body stream.', {
|
|
782
|
+
code: StorageErrorCode.PROVIDER,
|
|
783
|
+
operation: 'download',
|
|
784
|
+
permanent: true,
|
|
785
|
+
});
|
|
786
|
+
}
|
|
787
|
+
const reader = stream.getReader();
|
|
788
|
+
return new ReadableStream({
|
|
789
|
+
async cancel(reason) {
|
|
790
|
+
try {
|
|
791
|
+
await reader.cancel(reason);
|
|
792
|
+
}
|
|
793
|
+
catch (error) {
|
|
794
|
+
throw mapS3ConditionalError(error, 'download');
|
|
795
|
+
}
|
|
796
|
+
finally {
|
|
797
|
+
reader.releaseLock();
|
|
798
|
+
}
|
|
799
|
+
},
|
|
800
|
+
async pull(controller) {
|
|
801
|
+
try {
|
|
802
|
+
const result = await reader.read();
|
|
803
|
+
if (result.done) {
|
|
804
|
+
reader.releaseLock();
|
|
805
|
+
controller.close();
|
|
806
|
+
return;
|
|
807
|
+
}
|
|
808
|
+
controller.enqueue(result.value);
|
|
809
|
+
}
|
|
810
|
+
catch (error) {
|
|
811
|
+
reader.releaseLock();
|
|
812
|
+
controller.error(mapS3ConditionalError(error, 'download'));
|
|
813
|
+
}
|
|
814
|
+
},
|
|
815
|
+
});
|
|
816
|
+
}
|
|
817
|
+
async function* multipartParts(source, partSize) {
|
|
818
|
+
if (source instanceof Uint8Array) {
|
|
819
|
+
for (let offset = 0; offset < source.byteLength; offset += partSize) {
|
|
820
|
+
yield source.subarray(offset, Math.min(offset + partSize, source.byteLength));
|
|
821
|
+
}
|
|
822
|
+
return;
|
|
823
|
+
}
|
|
824
|
+
const reader = source.getReader();
|
|
825
|
+
let buffered = new Uint8Array(0);
|
|
826
|
+
try {
|
|
827
|
+
for (;;) {
|
|
828
|
+
const result = await reader.read();
|
|
829
|
+
if (result.done)
|
|
830
|
+
break;
|
|
831
|
+
const combined = new Uint8Array(buffered.byteLength + result.value.byteLength);
|
|
832
|
+
combined.set(buffered);
|
|
833
|
+
combined.set(result.value, buffered.byteLength);
|
|
834
|
+
buffered = combined;
|
|
835
|
+
while (buffered.byteLength >= partSize) {
|
|
836
|
+
yield buffered.slice(0, partSize);
|
|
837
|
+
buffered = buffered.slice(partSize);
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
if (buffered.byteLength > 0)
|
|
841
|
+
yield buffered;
|
|
842
|
+
}
|
|
843
|
+
finally {
|
|
844
|
+
reader.releaseLock();
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
function missingResultEtag(key) {
|
|
848
|
+
return new StorageError('S3 committed a conditional upload without returning the ETag required to identify its result; reconcile the destination before retrying.', {
|
|
849
|
+
applied: true,
|
|
850
|
+
code: StorageErrorCode.PROVIDER,
|
|
851
|
+
key,
|
|
852
|
+
operation: 'upload',
|
|
853
|
+
permanent: true,
|
|
854
|
+
});
|
|
855
|
+
}
|
|
856
|
+
function conditionalResultEtag(etag, key) {
|
|
857
|
+
try {
|
|
858
|
+
const normalized = providerEtag(etag, key, 'upload');
|
|
859
|
+
if (normalized === undefined)
|
|
860
|
+
throw missingResultEtag(key);
|
|
861
|
+
return normalized;
|
|
862
|
+
}
|
|
863
|
+
catch (error) {
|
|
864
|
+
if (!isStorageError(error) || error.applied)
|
|
865
|
+
throw error;
|
|
866
|
+
throw new StorageError(error.message, {
|
|
867
|
+
aborted: error.aborted,
|
|
868
|
+
applied: true,
|
|
869
|
+
code: error.code,
|
|
870
|
+
key,
|
|
871
|
+
operation: 'upload',
|
|
872
|
+
permanent: true,
|
|
873
|
+
timedOut: error.timedOut,
|
|
874
|
+
});
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
function filesConditionalUploadResultOf(result) {
|
|
878
|
+
const etag = conditionalResultEtag(result.etag, result.key);
|
|
879
|
+
return {
|
|
880
|
+
contentType: result.contentType,
|
|
881
|
+
etag,
|
|
882
|
+
key: result.key,
|
|
883
|
+
...(result.lastModified !== undefined && {
|
|
884
|
+
lastModified: result.lastModified.getTime(),
|
|
885
|
+
}),
|
|
886
|
+
size: result.size,
|
|
887
|
+
};
|
|
888
|
+
}
|
|
889
|
+
async function uploadConditionalSingle(base, key, body, conditional) {
|
|
890
|
+
const normalized = await normalizeConditionalBody(body, conditional.onProgress);
|
|
891
|
+
const contentType = contentTypeOf(body, conditional.contentType);
|
|
892
|
+
const retryOptions = normalized.contentLength === undefined
|
|
893
|
+
? { ...conditional, retries: 0 }
|
|
894
|
+
: conditional;
|
|
895
|
+
const result = await withS3Retry(retryOptions, 'upload', (signal) => base.raw.send(new PutObjectCommand({
|
|
896
|
+
Body: normalized.body,
|
|
897
|
+
Bucket: base.bucket,
|
|
898
|
+
...(conditional.cacheControl !== undefined && {
|
|
899
|
+
CacheControl: conditional.cacheControl,
|
|
900
|
+
}),
|
|
901
|
+
...(normalized.contentLength !== undefined && {
|
|
902
|
+
ContentLength: normalized.contentLength,
|
|
903
|
+
}),
|
|
904
|
+
ContentType: contentType,
|
|
905
|
+
...conditionalHeaders(conditional.condition, key),
|
|
906
|
+
Key: key,
|
|
907
|
+
...(conditional.metadata !== undefined && {
|
|
908
|
+
Metadata: conditional.metadata,
|
|
909
|
+
}),
|
|
910
|
+
}), signal === undefined ? undefined : { abortSignal: signal }));
|
|
911
|
+
const size = normalized.size();
|
|
912
|
+
conditional.onProgress?.({
|
|
913
|
+
loaded: size,
|
|
914
|
+
...(normalized.contentLength !== undefined && {
|
|
915
|
+
total: normalized.contentLength,
|
|
916
|
+
}),
|
|
917
|
+
});
|
|
918
|
+
const etag = conditionalResultEtag(result.ETag, key);
|
|
919
|
+
return { contentType, etag, key, size };
|
|
920
|
+
}
|
|
921
|
+
async function uploadConditionalMultipart(base, key, body, conditional) {
|
|
922
|
+
const configuredPartSize = typeof conditional.multipart === 'object'
|
|
923
|
+
? conditional.multipart.partSize
|
|
924
|
+
: undefined;
|
|
925
|
+
if (configuredPartSize !== undefined &&
|
|
926
|
+
(!Number.isSafeInteger(configuredPartSize) || configuredPartSize <= 0)) {
|
|
927
|
+
throw new StorageError('multipart.partSize must be a positive safe integer.', {
|
|
928
|
+
code: StorageErrorCode.INVALID_ARGUMENT,
|
|
929
|
+
key,
|
|
930
|
+
operation: 'upload',
|
|
931
|
+
permanent: true,
|
|
932
|
+
});
|
|
933
|
+
}
|
|
934
|
+
const partSize = Math.max(S3_MIN_MULTIPART_PART_BYTES, configuredPartSize ?? S3_MIN_MULTIPART_PART_BYTES);
|
|
935
|
+
const cleanupSignal = operationSignal(conditional);
|
|
936
|
+
const normalized = await normalizeConditionalBody(body, undefined);
|
|
937
|
+
if (normalized.contentLength === 0) {
|
|
938
|
+
return uploadConditionalSingle(base, key, new Uint8Array(), {
|
|
939
|
+
...conditional,
|
|
940
|
+
multipart: false,
|
|
941
|
+
});
|
|
942
|
+
}
|
|
943
|
+
const contentType = contentTypeOf(body, conditional.contentType);
|
|
944
|
+
let uploadId;
|
|
945
|
+
let size = 0;
|
|
946
|
+
try {
|
|
947
|
+
const created = await withS3Retry(conditional, 'upload', (signal) => base.raw.send(new CreateMultipartUploadCommand({
|
|
948
|
+
Bucket: base.bucket,
|
|
949
|
+
...(conditional.cacheControl !== undefined && {
|
|
950
|
+
CacheControl: conditional.cacheControl,
|
|
951
|
+
}),
|
|
952
|
+
ContentType: contentType,
|
|
953
|
+
Key: key,
|
|
954
|
+
...(conditional.metadata !== undefined && {
|
|
955
|
+
Metadata: conditional.metadata,
|
|
956
|
+
}),
|
|
957
|
+
}), signal === undefined ? undefined : { abortSignal: signal }));
|
|
958
|
+
uploadId = created.UploadId;
|
|
959
|
+
if (uploadId === undefined || uploadId.length === 0) {
|
|
960
|
+
throw new StorageError('S3 did not return a multipart upload ID.', {
|
|
961
|
+
code: StorageErrorCode.PROVIDER,
|
|
962
|
+
key,
|
|
963
|
+
operation: 'upload',
|
|
964
|
+
permanent: true,
|
|
965
|
+
});
|
|
966
|
+
}
|
|
967
|
+
const completedParts = [];
|
|
968
|
+
let partNumber = 0;
|
|
969
|
+
for await (const part of multipartParts(normalized.body, partSize)) {
|
|
970
|
+
partNumber += 1;
|
|
971
|
+
if (partNumber > S3_MAX_MULTIPART_PARTS) {
|
|
972
|
+
throw new StorageError(`Conditional multipart upload exceeds the ${S3_MAX_MULTIPART_PARTS}-part S3 limit.`, {
|
|
973
|
+
code: StorageErrorCode.LIMIT_EXCEEDED,
|
|
974
|
+
key,
|
|
975
|
+
operation: 'upload',
|
|
976
|
+
permanent: true,
|
|
977
|
+
});
|
|
978
|
+
}
|
|
979
|
+
const uploaded = await withS3Retry(conditional, 'upload', (signal) => base.raw.send(new UploadPartCommand({
|
|
980
|
+
Body: part,
|
|
981
|
+
Bucket: base.bucket,
|
|
982
|
+
ContentLength: part.byteLength,
|
|
983
|
+
Key: key,
|
|
984
|
+
PartNumber: partNumber,
|
|
985
|
+
UploadId: uploadId,
|
|
986
|
+
}), signal === undefined ? undefined : { abortSignal: signal }));
|
|
987
|
+
const partEtag = providerEtag(uploaded.ETag, key, 'upload');
|
|
988
|
+
if (partEtag === undefined) {
|
|
989
|
+
throw new StorageError('S3 upload part returned no ETag.', {
|
|
990
|
+
code: StorageErrorCode.PROVIDER,
|
|
991
|
+
key,
|
|
992
|
+
operation: 'upload',
|
|
993
|
+
permanent: true,
|
|
994
|
+
});
|
|
995
|
+
}
|
|
996
|
+
completedParts.push({
|
|
997
|
+
ETag: storageEtagHeader(partEtag),
|
|
998
|
+
PartNumber: partNumber,
|
|
999
|
+
});
|
|
1000
|
+
size += part.byteLength;
|
|
1001
|
+
conditional.onProgress?.({
|
|
1002
|
+
loaded: size,
|
|
1003
|
+
...(normalized.contentLength !== undefined && {
|
|
1004
|
+
total: normalized.contentLength,
|
|
1005
|
+
}),
|
|
1006
|
+
});
|
|
1007
|
+
}
|
|
1008
|
+
if (completedParts.length === 0) {
|
|
1009
|
+
const emptyUploadId = uploadId;
|
|
1010
|
+
uploadId = undefined;
|
|
1011
|
+
await abortMultipartUpload(base, key, emptyUploadId, cleanupSignal);
|
|
1012
|
+
return uploadConditionalSingle(base, key, new Uint8Array(), {
|
|
1013
|
+
...conditional,
|
|
1014
|
+
multipart: false,
|
|
1015
|
+
});
|
|
1016
|
+
}
|
|
1017
|
+
const completed = await withS3Retry(conditional, 'upload', (signal) => base.raw.send(new CompleteMultipartUploadCommand({
|
|
1018
|
+
Bucket: base.bucket,
|
|
1019
|
+
...conditionalHeaders(conditional.condition, key),
|
|
1020
|
+
Key: key,
|
|
1021
|
+
MultipartUpload: { Parts: completedParts },
|
|
1022
|
+
UploadId: uploadId,
|
|
1023
|
+
}), signal === undefined ? undefined : { abortSignal: signal }));
|
|
1024
|
+
const etag = conditionalResultEtag(completed.ETag, key);
|
|
1025
|
+
uploadId = undefined;
|
|
1026
|
+
return { contentType, etag, key, size };
|
|
1027
|
+
}
|
|
1028
|
+
catch (error) {
|
|
1029
|
+
if (uploadId !== undefined) {
|
|
1030
|
+
await abortMultipartUpload(base, key, uploadId, cleanupSignal).catch(() => undefined);
|
|
1031
|
+
}
|
|
1032
|
+
throw error;
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
function profileUsesConditionalHeaders(profile) {
|
|
1036
|
+
return (profile.conditionalCreate !== undefined ||
|
|
1037
|
+
profile.conditionalReplace !== undefined ||
|
|
1038
|
+
profile.conditionalDelete?.etag === true ||
|
|
1039
|
+
profile.conditionalRead?.etag === true ||
|
|
1040
|
+
profile.conditionalCopySource?.etag === true ||
|
|
1041
|
+
profile.conditionalCopyDestination?.create === true ||
|
|
1042
|
+
profile.conditionalCopyDestination?.replace === true ||
|
|
1043
|
+
profile.conditionalMultipartCompletion?.create === true ||
|
|
1044
|
+
profile.conditionalMultipartCompletion?.replace === true);
|
|
1045
|
+
}
|
|
1046
|
+
function conditionalOperationsForProfile(profile, adapter, upstream) {
|
|
1047
|
+
const conditional = {};
|
|
1048
|
+
const legacyUpload = adapter.uploadConditional;
|
|
1049
|
+
const legacyRead = adapter.downloadConditional;
|
|
1050
|
+
const legacyDelete = adapter.deleteConditional;
|
|
1051
|
+
const legacyCopy = adapter.promote;
|
|
1052
|
+
if (profile.conditionalCreate?.resultEtag === true) {
|
|
1053
|
+
if (legacyUpload !== undefined) {
|
|
1054
|
+
conditional.create = (key, body, options) => throughFilesConditionalBoundary('upload', async () => {
|
|
1055
|
+
assertBufferedFilesConditionalS3Body(body, key);
|
|
1056
|
+
return filesConditionalUploadResultOf(await legacyUpload(key, body, {
|
|
1057
|
+
...filesAttemptOptions(options),
|
|
1058
|
+
condition: { type: 'create' },
|
|
1059
|
+
}));
|
|
1060
|
+
});
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
if (profile.conditionalReplace?.resultEtag === true) {
|
|
1064
|
+
if (legacyUpload !== undefined) {
|
|
1065
|
+
conditional.replace = (key, body, etag, options) => throughFilesConditionalBoundary('upload', async () => {
|
|
1066
|
+
assertBufferedFilesConditionalS3Body(body, key);
|
|
1067
|
+
return filesConditionalUploadResultOf(await legacyUpload(key, body, {
|
|
1068
|
+
...filesAttemptOptions(options),
|
|
1069
|
+
condition: { etag, type: 'replace' },
|
|
1070
|
+
}));
|
|
1071
|
+
});
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
if (profile.conditionalRead?.etag === true) {
|
|
1075
|
+
const nativeExactRead = upstream?.exactRead;
|
|
1076
|
+
if (nativeExactRead !== undefined) {
|
|
1077
|
+
conditional.exactRead = (key, etag, options) => throughFilesConditionalBoundary('download', () => nativeExactRead.call(upstream, key, etag, options));
|
|
1078
|
+
}
|
|
1079
|
+
else if (legacyRead !== undefined) {
|
|
1080
|
+
conditional.exactRead = (key, etag, options) => throughFilesConditionalBoundary('download', async () => storedFileOfStorageObject(await legacyRead(key, {
|
|
1081
|
+
...filesAttemptOptions(options),
|
|
1082
|
+
condition: { etag },
|
|
1083
|
+
})));
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
if (profile.conditionalDelete?.etag === true) {
|
|
1087
|
+
const nativeDelete = upstream?.delete;
|
|
1088
|
+
if (nativeDelete !== undefined) {
|
|
1089
|
+
conditional.delete = (key, etag, options) => throughFilesConditionalBoundary('delete', () => nativeDelete.call(upstream, key, etag, options));
|
|
1090
|
+
}
|
|
1091
|
+
else if (legacyDelete !== undefined) {
|
|
1092
|
+
conditional.delete = (key, etag, options) => throughFilesConditionalBoundary('delete', () => legacyDelete(key, {
|
|
1093
|
+
...filesAttemptOptions(options),
|
|
1094
|
+
condition: { etag },
|
|
1095
|
+
}));
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
const destinationCreate = profile.conditionalCopyDestination?.atomicWithSource === true &&
|
|
1099
|
+
profile.conditionalCopyDestination.create === true;
|
|
1100
|
+
const destinationReplace = profile.conditionalCopyDestination?.atomicWithSource === true &&
|
|
1101
|
+
profile.conditionalCopyDestination.replace === true;
|
|
1102
|
+
if (profile.conditionalCopySource?.etag === true &&
|
|
1103
|
+
(destinationCreate || destinationReplace) &&
|
|
1104
|
+
legacyCopy !== undefined) {
|
|
1105
|
+
const nativeCopy = upstream?.copy;
|
|
1106
|
+
conditional.copy = Object.freeze({
|
|
1107
|
+
atomicSourceDestination: true,
|
|
1108
|
+
destinationCreate,
|
|
1109
|
+
destinationReplace,
|
|
1110
|
+
async run(sourceKey, destinationKey, condition, options) {
|
|
1111
|
+
const destinationSupported = condition.destination.type === 'create'
|
|
1112
|
+
? destinationCreate
|
|
1113
|
+
: destinationReplace;
|
|
1114
|
+
if (!destinationSupported) {
|
|
1115
|
+
throw mapStorageErrorToFilesSdkError(new StorageError(`S3 provider profile "${profile.name}" does not support the requested conditional copy.`, {
|
|
1116
|
+
code: StorageErrorCode.NOT_SUPPORTED,
|
|
1117
|
+
key: destinationKey,
|
|
1118
|
+
operation: 'copy',
|
|
1119
|
+
permanent: true,
|
|
1120
|
+
}));
|
|
1121
|
+
}
|
|
1122
|
+
const nativeDestinationSupported = condition.destination.type === 'create'
|
|
1123
|
+
? nativeCopy?.destinationCreate === true
|
|
1124
|
+
: nativeCopy?.destinationReplace === true;
|
|
1125
|
+
if (nativeCopy?.sourceEtag === true &&
|
|
1126
|
+
nativeCopy.atomicSourceDestination === true &&
|
|
1127
|
+
nativeDestinationSupported) {
|
|
1128
|
+
return throughFilesConditionalBoundary('copy', () => nativeCopy.run.call(nativeCopy, sourceKey, destinationKey, condition, options));
|
|
1129
|
+
}
|
|
1130
|
+
return throughFilesConditionalBoundary('copy', () => legacyCopy(sourceKey, destinationKey, {
|
|
1131
|
+
...filesAttemptOptions(options),
|
|
1132
|
+
destination: condition.destination,
|
|
1133
|
+
sourceEtag: condition.source.etag,
|
|
1134
|
+
}));
|
|
1135
|
+
},
|
|
1136
|
+
sourceEtag: true,
|
|
1137
|
+
});
|
|
1138
|
+
}
|
|
1139
|
+
return Object.keys(conditional).length === 0
|
|
1140
|
+
? undefined
|
|
1141
|
+
: Object.freeze(conditional);
|
|
1142
|
+
}
|
|
1143
|
+
const configuredS3Clients = new WeakSet();
|
|
1144
|
+
const withheldCustomConditionalOperations = new WeakMap();
|
|
1145
|
+
/** Constructs the upstream S3 adapter while retaining security-relevant metadata. */
|
|
1146
|
+
export function s3(options) {
|
|
1147
|
+
// A verified provider profile is applied only by withS3Capabilities, after
|
|
1148
|
+
// construction. Opt the raw custom-endpoint client into upstream's wire
|
|
1149
|
+
// header guard here, but hide its broad conditional surface until that
|
|
1150
|
+
// profile has narrowed the exact operations below.
|
|
1151
|
+
const implicitCustomConditional = options.endpoint !== undefined && options.conditional === undefined;
|
|
1152
|
+
const adapter = createFilesSdkS3Adapter(implicitCustomConditional ? { ...options, conditional: true } : options);
|
|
1153
|
+
if (options.endpoint !== undefined && adapter.conditional !== undefined) {
|
|
1154
|
+
if (options.conditional === true) {
|
|
1155
|
+
withheldCustomConditionalOperations.set(adapter.raw, adapter.conditional);
|
|
1156
|
+
}
|
|
1157
|
+
if (!Reflect.deleteProperty(adapter, 'conditional')) {
|
|
1158
|
+
adapter.raw.destroy();
|
|
1159
|
+
throw new TypeError('The custom-endpoint S3 conditional surface could not be withheld before provider verification.');
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
recordS3ConditionalRequestPermission(adapter.raw, options.conditional !== false);
|
|
1163
|
+
markFilesSdkS3AdapterUndecorated(adapter);
|
|
1164
|
+
recordS3ConstructionMetadata(adapter.raw, {
|
|
1165
|
+
publicBaseUrlConfigured: options.publicBaseUrl !== undefined,
|
|
1166
|
+
});
|
|
1167
|
+
return adapter;
|
|
1168
|
+
}
|
|
1169
|
+
/**
|
|
1170
|
+
* Adds only the exact operations declared by one verified provider profile.
|
|
1171
|
+
* Each raw adapter may be decorated once so capability state cannot depend on
|
|
1172
|
+
* profile application order. A native AWS SDK endpoint also bounds every
|
|
1173
|
+
* explicit profile by the built-in AWS capability ceiling.
|
|
1174
|
+
*/
|
|
1175
|
+
export function withS3Capabilities(base, options = {}) {
|
|
1176
|
+
const raw = base.raw;
|
|
1177
|
+
if (configuredS3Clients.has(raw)) {
|
|
1178
|
+
throw new TypeError('withS3Capabilities may only be applied once to an S3 adapter.');
|
|
1179
|
+
}
|
|
1180
|
+
assertFilesSdkS3AdapterHasNoReservedExtensions(base);
|
|
1181
|
+
const sdkHasCustomEndpoint = raw.config.isCustomEndpoint === true;
|
|
1182
|
+
if (options.endpoint !== undefined && !sdkHasCustomEndpoint) {
|
|
1183
|
+
throw new TypeError('The declared S3 endpoint does not match the SDK client endpoint provenance.');
|
|
1184
|
+
}
|
|
1185
|
+
if (!sdkHasCustomEndpoint) {
|
|
1186
|
+
// files-sdk does not expose this S3Client constructor option. Endpoint
|
|
1187
|
+
// resolution is lazy, so fixing the resolved SDK config before the adapter
|
|
1188
|
+
// is exposed prevents environment and shared-config endpoint redirection.
|
|
1189
|
+
raw.config.ignoreConfiguredEndpointUrls = true;
|
|
1190
|
+
}
|
|
1191
|
+
const inferredNativeAws = base.name === 's3' && !sdkHasCustomEndpoint;
|
|
1192
|
+
const profile = options.providerProfile ??
|
|
1193
|
+
(inferredNativeAws
|
|
1194
|
+
? AWS_S3_PROVIDER_PROFILE
|
|
1195
|
+
: UNVERIFIED_S3_PROVIDER_PROFILE);
|
|
1196
|
+
assertVerifiedS3ProviderProfile(profile);
|
|
1197
|
+
const upstreamConditional = base.conditional ?? withheldCustomConditionalOperations.get(raw);
|
|
1198
|
+
if (profileUsesConditionalHeaders(profile) &&
|
|
1199
|
+
upstreamConditional === undefined &&
|
|
1200
|
+
getS3ConditionalRequestPermission(raw) !== true) {
|
|
1201
|
+
throw new TypeError(`S3 provider profile "${profile.name}" requires conditional request headers, but the adapter was not constructed with conditional requests enabled.`);
|
|
1202
|
+
}
|
|
1203
|
+
if (!sdkHasCustomEndpoint) {
|
|
1204
|
+
assertS3ProviderProfileContainedBy(profile, AWS_S3_PROVIDER_PROFILE);
|
|
1205
|
+
}
|
|
1206
|
+
const provenance = options.providerProfile === undefined
|
|
1207
|
+
? inferredNativeAws
|
|
1208
|
+
? 'native'
|
|
1209
|
+
: 'unverified'
|
|
1210
|
+
: inferredNativeAws
|
|
1211
|
+
? 'native'
|
|
1212
|
+
: 'verified';
|
|
1213
|
+
const constructionMetadata = getS3ConstructionMetadata(raw);
|
|
1214
|
+
const publicBaseUrlConfigured = options.publicBaseUrl !== undefined ||
|
|
1215
|
+
constructionMetadata?.publicBaseUrlConfigured === true;
|
|
1216
|
+
const bucket = base.bucket;
|
|
1217
|
+
const requestAdapter = { bucket, raw };
|
|
1218
|
+
const signedUploadUrl = async (key, signOptions) => {
|
|
1219
|
+
if (signOptions.minSize !== undefined &&
|
|
1220
|
+
signOptions.maxSize === undefined) {
|
|
1221
|
+
throw new StorageError('S3 signed uploads cannot enforce minSize without maxSize.', {
|
|
1222
|
+
code: StorageErrorCode.NOT_SUPPORTED,
|
|
1223
|
+
key,
|
|
1224
|
+
operation: 'signUpload',
|
|
1225
|
+
permanent: true,
|
|
1226
|
+
});
|
|
1227
|
+
}
|
|
1228
|
+
if (signOptions.maxSize !== undefined && key.endsWith('${filename}')) {
|
|
1229
|
+
throw new StorageError('Signed POST keys must not end with the AWS ${filename} template.', {
|
|
1230
|
+
code: StorageErrorCode.INVALID_ARGUMENT,
|
|
1231
|
+
key,
|
|
1232
|
+
operation: 'signUpload',
|
|
1233
|
+
permanent: true,
|
|
1234
|
+
});
|
|
1235
|
+
}
|
|
1236
|
+
if (signOptions.contentType !== undefined &&
|
|
1237
|
+
profile.signedUploadPolicy.contentType !== true) {
|
|
1238
|
+
throw new StorageError(`S3 provider profile "${profile.name}" cannot enforce signed-upload contentType.`, {
|
|
1239
|
+
code: StorageErrorCode.NOT_SUPPORTED,
|
|
1240
|
+
key,
|
|
1241
|
+
operation: 'signUpload',
|
|
1242
|
+
permanent: true,
|
|
1243
|
+
});
|
|
1244
|
+
}
|
|
1245
|
+
if (signOptions.maxSize !== undefined &&
|
|
1246
|
+
profile.signedUploadPolicy.sizeRange !== true) {
|
|
1247
|
+
throw new StorageError(`S3 provider profile "${profile.name}" cannot enforce signed-upload sizeRange.`, {
|
|
1248
|
+
code: StorageErrorCode.NOT_SUPPORTED,
|
|
1249
|
+
key,
|
|
1250
|
+
operation: 'signUpload',
|
|
1251
|
+
permanent: true,
|
|
1252
|
+
});
|
|
1253
|
+
}
|
|
1254
|
+
try {
|
|
1255
|
+
if (signOptions.maxSize !== undefined) {
|
|
1256
|
+
const conditions = [
|
|
1257
|
+
[
|
|
1258
|
+
'content-length-range',
|
|
1259
|
+
signOptions.minSize ?? 1,
|
|
1260
|
+
signOptions.maxSize,
|
|
1261
|
+
],
|
|
1262
|
+
];
|
|
1263
|
+
if (signOptions.contentType !== undefined) {
|
|
1264
|
+
conditions.push(['eq', '$Content-Type', signOptions.contentType]);
|
|
1265
|
+
}
|
|
1266
|
+
const post = await createPresignedPost(raw, {
|
|
1267
|
+
Bucket: bucket,
|
|
1268
|
+
Conditions: conditions,
|
|
1269
|
+
Expires: signOptions.expiresIn,
|
|
1270
|
+
Key: key,
|
|
1271
|
+
...(signOptions.contentType !== undefined && {
|
|
1272
|
+
Fields: { 'Content-Type': signOptions.contentType },
|
|
1273
|
+
}),
|
|
1274
|
+
});
|
|
1275
|
+
return { fields: post.fields, method: 'POST', url: post.url };
|
|
1276
|
+
}
|
|
1277
|
+
const url = await getSignedUrl(raw, new PutObjectCommand({
|
|
1278
|
+
Bucket: bucket,
|
|
1279
|
+
Key: key,
|
|
1280
|
+
...(signOptions.contentType !== undefined && {
|
|
1281
|
+
ContentType: signOptions.contentType,
|
|
1282
|
+
}),
|
|
1283
|
+
}), {
|
|
1284
|
+
expiresIn: signOptions.expiresIn,
|
|
1285
|
+
...(signOptions.contentType !== undefined && {
|
|
1286
|
+
signableHeaders: new Set(['content-type']),
|
|
1287
|
+
}),
|
|
1288
|
+
});
|
|
1289
|
+
return {
|
|
1290
|
+
...(signOptions.contentType !== undefined && {
|
|
1291
|
+
headers: { 'Content-Type': signOptions.contentType },
|
|
1292
|
+
}),
|
|
1293
|
+
method: 'PUT',
|
|
1294
|
+
url,
|
|
1295
|
+
};
|
|
1296
|
+
}
|
|
1297
|
+
catch (error) {
|
|
1298
|
+
throw mapS3Error(error);
|
|
1299
|
+
}
|
|
1300
|
+
};
|
|
1301
|
+
const adapter = Object.assign(base, {
|
|
1302
|
+
physicalKey: profile.physicalKey,
|
|
1303
|
+
signedUploadPolicy: profile.signedUploadPolicy,
|
|
1304
|
+
signedUploadUrl,
|
|
1305
|
+
signedDownloadPolicy: Object.freeze({
|
|
1306
|
+
expiresIn: constructionMetadata !== undefined && !publicBaseUrlConfigured,
|
|
1307
|
+
}),
|
|
1308
|
+
});
|
|
1309
|
+
if (profile.conditionalCopySource !== undefined ||
|
|
1310
|
+
profile.conditionalCopyDestination !== undefined) {
|
|
1311
|
+
Object.assign(adapter, {
|
|
1312
|
+
...(profile.conditionalCopySource !== undefined && {
|
|
1313
|
+
conditionalCopySource: profile.conditionalCopySource,
|
|
1314
|
+
}),
|
|
1315
|
+
...(profile.conditionalCopyDestination !== undefined && {
|
|
1316
|
+
conditionalCopyDestination: profile.conditionalCopyDestination,
|
|
1317
|
+
}),
|
|
1318
|
+
async promote(sourceKey, destinationKey, promotion) {
|
|
1319
|
+
assertS3Key(sourceKey, 'source key', 'copy');
|
|
1320
|
+
assertS3Key(destinationKey, 'destination key', 'copy');
|
|
1321
|
+
assertPromotionInput(sourceKey, destinationKey, promotion);
|
|
1322
|
+
if ((promotion.sourceEtag !== undefined &&
|
|
1323
|
+
profile.conditionalCopySource?.etag !== true) ||
|
|
1324
|
+
(promotion.sourceVersion !== undefined &&
|
|
1325
|
+
profile.conditionalCopySource?.version !== true) ||
|
|
1326
|
+
((promotion.sourceEtag !== undefined ||
|
|
1327
|
+
promotion.sourceVersion !== undefined) &&
|
|
1328
|
+
promotion.destination === undefined &&
|
|
1329
|
+
profile.conditionalCopySource?.requiresDestinationPredicate ===
|
|
1330
|
+
true) ||
|
|
1331
|
+
(promotion.destination?.type === 'create' &&
|
|
1332
|
+
profile.conditionalCopyDestination?.create !== true) ||
|
|
1333
|
+
(promotion.destination?.type === 'replace' &&
|
|
1334
|
+
profile.conditionalCopyDestination?.replace !== true) ||
|
|
1335
|
+
(promotion.destination !== undefined &&
|
|
1336
|
+
promotion.sourceEtag === undefined &&
|
|
1337
|
+
promotion.sourceVersion === undefined &&
|
|
1338
|
+
profile.conditionalCopyDestination?.requiresSourcePredicate ===
|
|
1339
|
+
true) ||
|
|
1340
|
+
(promotion.destination !== undefined &&
|
|
1341
|
+
(promotion.sourceEtag !== undefined ||
|
|
1342
|
+
promotion.sourceVersion !== undefined) &&
|
|
1343
|
+
profile.conditionalCopyDestination?.atomicWithSource !== true)) {
|
|
1344
|
+
throw new StorageError(`S3 provider profile "${profile.name}" does not support the requested conditional promotion.`, {
|
|
1345
|
+
code: StorageErrorCode.NOT_SUPPORTED,
|
|
1346
|
+
key: sourceKey,
|
|
1347
|
+
operation: 'promote',
|
|
1348
|
+
permanent: true,
|
|
1349
|
+
});
|
|
1350
|
+
}
|
|
1351
|
+
await withS3Retry(promotion, 'copy', async (signal) => {
|
|
1352
|
+
await raw.send(new CopyObjectCommand({
|
|
1353
|
+
Bucket: bucket,
|
|
1354
|
+
CopySource: copySource(bucket, sourceKey, promotion.sourceVersion),
|
|
1355
|
+
...(promotion.sourceEtag !== undefined && {
|
|
1356
|
+
CopySourceIfMatch: etagHeader(promotion.sourceEtag, sourceKey, 'copy', 'sourceEtag'),
|
|
1357
|
+
}),
|
|
1358
|
+
...(promotion.destination?.type === 'create'
|
|
1359
|
+
? { IfNoneMatch: '*' }
|
|
1360
|
+
: promotion.destination?.type === 'replace'
|
|
1361
|
+
? {
|
|
1362
|
+
IfMatch: etagHeader(promotion.destination.etag, destinationKey, 'copy', 'destination.etag'),
|
|
1363
|
+
}
|
|
1364
|
+
: {}),
|
|
1365
|
+
Key: destinationKey,
|
|
1366
|
+
}), signal === undefined ? undefined : { abortSignal: signal });
|
|
1367
|
+
});
|
|
1368
|
+
},
|
|
1369
|
+
});
|
|
1370
|
+
}
|
|
1371
|
+
if (profile.conditionalRead !== undefined) {
|
|
1372
|
+
Object.assign(adapter, {
|
|
1373
|
+
conditionalRead: profile.conditionalRead,
|
|
1374
|
+
async downloadConditional(key, options) {
|
|
1375
|
+
assertS3Key(key, 'key', 'download');
|
|
1376
|
+
assertConditionalReadInput(key, options);
|
|
1377
|
+
if ((options.condition.etag !== undefined &&
|
|
1378
|
+
profile.conditionalRead?.etag !== true) ||
|
|
1379
|
+
(options.condition.version !== undefined &&
|
|
1380
|
+
profile.conditionalRead?.version !== true)) {
|
|
1381
|
+
throw new StorageError(`S3 provider profile "${profile.name}" does not support the requested conditional read.`, {
|
|
1382
|
+
code: StorageErrorCode.NOT_SUPPORTED,
|
|
1383
|
+
key,
|
|
1384
|
+
operation: 'download',
|
|
1385
|
+
permanent: true,
|
|
1386
|
+
});
|
|
1387
|
+
}
|
|
1388
|
+
const result = await withS3Retry(options, 'download', (signal) => raw.send(new GetObjectCommand({
|
|
1389
|
+
Bucket: bucket,
|
|
1390
|
+
...(options.condition.etag !== undefined && {
|
|
1391
|
+
IfMatch: etagHeader(options.condition.etag, key, 'download', 'condition.etag'),
|
|
1392
|
+
}),
|
|
1393
|
+
Key: key,
|
|
1394
|
+
...(rangeHeader(options.range) !== undefined && {
|
|
1395
|
+
Range: rangeHeader(options.range),
|
|
1396
|
+
}),
|
|
1397
|
+
...(options.condition.version !== undefined && {
|
|
1398
|
+
VersionId: options.condition.version,
|
|
1399
|
+
}),
|
|
1400
|
+
}), signal === undefined ? undefined : { abortSignal: signal }));
|
|
1401
|
+
const etag = providerEtag(result.ETag, key, 'download');
|
|
1402
|
+
return {
|
|
1403
|
+
body: s3ResponseStream(result.Body),
|
|
1404
|
+
contentType: result.ContentType ?? 'application/octet-stream',
|
|
1405
|
+
...(etag !== undefined && { etag }),
|
|
1406
|
+
key,
|
|
1407
|
+
...(result.LastModified !== undefined && {
|
|
1408
|
+
lastModified: result.LastModified,
|
|
1409
|
+
}),
|
|
1410
|
+
...(result.Metadata !== undefined && { metadata: result.Metadata }),
|
|
1411
|
+
name: key.split('/').at(-1) ?? key,
|
|
1412
|
+
size: Number(result.ContentLength ?? 0),
|
|
1413
|
+
};
|
|
1414
|
+
},
|
|
1415
|
+
});
|
|
1416
|
+
}
|
|
1417
|
+
if (profile.conditionalDelete?.etag === true) {
|
|
1418
|
+
Object.assign(adapter, {
|
|
1419
|
+
conditionalDelete: profile.conditionalDelete,
|
|
1420
|
+
async deleteConditional(key, options) {
|
|
1421
|
+
assertS3Key(key, 'key', 'delete');
|
|
1422
|
+
etagHeader(options?.condition?.etag, key, 'delete', 'condition.etag');
|
|
1423
|
+
await withS3Retry(options, 'delete', async (signal) => {
|
|
1424
|
+
await raw.send(new DeleteObjectCommand({
|
|
1425
|
+
Bucket: bucket,
|
|
1426
|
+
IfMatch: etagHeader(options.condition.etag, key, 'delete', 'condition.etag'),
|
|
1427
|
+
Key: key,
|
|
1428
|
+
}), signal === undefined ? undefined : { abortSignal: signal });
|
|
1429
|
+
});
|
|
1430
|
+
},
|
|
1431
|
+
});
|
|
1432
|
+
}
|
|
1433
|
+
if (profile.conditionalCreate !== undefined ||
|
|
1434
|
+
profile.conditionalReplace !== undefined) {
|
|
1435
|
+
Object.assign(adapter, {
|
|
1436
|
+
...(profile.conditionalCreate !== undefined && {
|
|
1437
|
+
conditionalCreate: profile.conditionalCreate,
|
|
1438
|
+
}),
|
|
1439
|
+
...(profile.conditionalReplace !== undefined && {
|
|
1440
|
+
conditionalReplace: profile.conditionalReplace,
|
|
1441
|
+
}),
|
|
1442
|
+
...(profile.conditionalMultipartCompletion !== undefined && {
|
|
1443
|
+
conditionalMultipartCompletion: profile.conditionalMultipartCompletion,
|
|
1444
|
+
}),
|
|
1445
|
+
async uploadConditional(key, body, conditional) {
|
|
1446
|
+
assertS3Key(key, 'key', 'upload');
|
|
1447
|
+
assertConditionalUploadInput(key, conditional);
|
|
1448
|
+
const supported = conditional.condition.type === 'create'
|
|
1449
|
+
? profile.conditionalCreate !== undefined
|
|
1450
|
+
: profile.conditionalReplace !== undefined;
|
|
1451
|
+
const multipartRequested = conditional.multipart !== undefined &&
|
|
1452
|
+
conditional.multipart !== false;
|
|
1453
|
+
const multipartSupported = !multipartRequested ||
|
|
1454
|
+
(conditional.condition.type === 'create'
|
|
1455
|
+
? profile.conditionalMultipartCompletion?.create === true
|
|
1456
|
+
: profile.conditionalMultipartCompletion?.replace === true);
|
|
1457
|
+
if (!supported || !multipartSupported) {
|
|
1458
|
+
throw new StorageError(`S3 provider profile "${profile.name}" does not support the requested conditional upload.`, {
|
|
1459
|
+
code: StorageErrorCode.NOT_SUPPORTED,
|
|
1460
|
+
key,
|
|
1461
|
+
operation: 'upload',
|
|
1462
|
+
permanent: true,
|
|
1463
|
+
});
|
|
1464
|
+
}
|
|
1465
|
+
if (conditional.control !== undefined) {
|
|
1466
|
+
throw new StorageError('Conditional S3 uploads do not support resumable control.', {
|
|
1467
|
+
code: StorageErrorCode.NOT_SUPPORTED,
|
|
1468
|
+
key,
|
|
1469
|
+
operation: 'upload',
|
|
1470
|
+
permanent: true,
|
|
1471
|
+
});
|
|
1472
|
+
}
|
|
1473
|
+
return multipartRequested
|
|
1474
|
+
? uploadConditionalMultipart(requestAdapter, key, body, conditional)
|
|
1475
|
+
: uploadConditionalSingle(requestAdapter, key, body, conditional);
|
|
1476
|
+
},
|
|
1477
|
+
});
|
|
1478
|
+
}
|
|
1479
|
+
Object.assign(adapter, {
|
|
1480
|
+
conditional: conditionalOperationsForProfile(profile, adapter, upstreamConditional),
|
|
1481
|
+
});
|
|
1482
|
+
Object.freeze(adapter);
|
|
1483
|
+
markFilesSdkS3RawClientProvenance(raw, adapter, provenance);
|
|
1484
|
+
configuredS3Clients.add(raw);
|
|
1485
|
+
return adapter;
|
|
1486
|
+
}
|
|
1487
|
+
/**
|
|
1488
|
+
* Creates the files-sdk S3 driver from the storage package's own dependency
|
|
1489
|
+
* context and exposes only the operations in its verified provider profile.
|
|
1490
|
+
*/
|
|
1491
|
+
export function createS3StorageDriver(options) {
|
|
1492
|
+
const { adapter: adapterOptions, ...filesOptions } = options;
|
|
1493
|
+
const { providerProfile, ...plainFilesOptions } = filesOptions;
|
|
1494
|
+
const unverifiedCustomEndpoint = adapterOptions.endpoint !== undefined && providerProfile === undefined;
|
|
1495
|
+
return createFilesSdkDriver({
|
|
1496
|
+
...plainFilesOptions,
|
|
1497
|
+
...(unverifiedCustomEndpoint && { readonly: true }),
|
|
1498
|
+
adapter: withS3Capabilities(s3(adapterOptions), {
|
|
1499
|
+
...adapterOptions,
|
|
1500
|
+
...(providerProfile !== undefined && { providerProfile }),
|
|
1501
|
+
}),
|
|
1502
|
+
});
|
|
1503
|
+
}
|
|
1504
|
+
export { mapS3Error };
|
|
1505
|
+
//# sourceMappingURL=index.js.map
|