@uploadista/data-store-s3 0.0.3
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/.turbo/turbo-build.log +5 -0
- package/.turbo/turbo-check.log +5 -0
- package/LICENSE +21 -0
- package/README.md +588 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -0
- package/dist/observability.d.ts +45 -0
- package/dist/observability.d.ts.map +1 -0
- package/dist/observability.js +155 -0
- package/dist/s3-store-old.d.ts +51 -0
- package/dist/s3-store-old.d.ts.map +1 -0
- package/dist/s3-store-old.js +765 -0
- package/dist/s3-store.d.ts +9 -0
- package/dist/s3-store.d.ts.map +1 -0
- package/dist/s3-store.js +666 -0
- package/dist/services/__mocks__/s3-client-mock.service.d.ts +44 -0
- package/dist/services/__mocks__/s3-client-mock.service.d.ts.map +1 -0
- package/dist/services/__mocks__/s3-client-mock.service.js +379 -0
- package/dist/services/index.d.ts +2 -0
- package/dist/services/index.d.ts.map +1 -0
- package/dist/services/index.js +1 -0
- package/dist/services/s3-client.service.d.ts +68 -0
- package/dist/services/s3-client.service.d.ts.map +1 -0
- package/dist/services/s3-client.service.js +209 -0
- package/dist/test-observability.d.ts +6 -0
- package/dist/test-observability.d.ts.map +1 -0
- package/dist/test-observability.js +62 -0
- package/dist/types.d.ts +81 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/dist/utils/calculations.d.ts +7 -0
- package/dist/utils/calculations.d.ts.map +1 -0
- package/dist/utils/calculations.js +41 -0
- package/dist/utils/error-handling.d.ts +7 -0
- package/dist/utils/error-handling.d.ts.map +1 -0
- package/dist/utils/error-handling.js +29 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +3 -0
- package/dist/utils/stream-adapter.d.ts +14 -0
- package/dist/utils/stream-adapter.d.ts.map +1 -0
- package/dist/utils/stream-adapter.js +41 -0
- package/package.json +36 -0
- package/src/__tests__/integration/s3-store.integration.test.ts +548 -0
- package/src/__tests__/multipart-logic.test.ts +395 -0
- package/src/__tests__/s3-store.edge-cases.test.ts +681 -0
- package/src/__tests__/s3-store.performance.test.ts +622 -0
- package/src/__tests__/s3-store.test.ts +662 -0
- package/src/__tests__/utils/performance-helpers.ts +459 -0
- package/src/__tests__/utils/test-data-generator.ts +331 -0
- package/src/__tests__/utils/test-setup.ts +256 -0
- package/src/index.ts +1 -0
- package/src/s3-store.ts +1059 -0
- package/src/services/__mocks__/s3-client-mock.service.ts +604 -0
- package/src/services/index.ts +1 -0
- package/src/services/s3-client.service.ts +359 -0
- package/src/types.ts +96 -0
- package/src/utils/calculations.ts +61 -0
- package/src/utils/error-handling.ts +52 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/stream-adapter.ts +50 -0
- package/tsconfig.json +19 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/vitest.config.ts +15 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { DataStore, UploadFile } from "@uploadista/core/types";
|
|
2
|
+
import { UploadFileKVStore } from "@uploadista/core/types";
|
|
3
|
+
import { Effect } from "effect";
|
|
4
|
+
import { S3ClientService } from "./services/s3-client.service";
|
|
5
|
+
import type { S3StoreConfig } from "./types";
|
|
6
|
+
export declare function createS3StoreImplementation(config: S3StoreConfig): Effect.Effect<DataStore<UploadFile>, never, S3ClientService>;
|
|
7
|
+
export declare const createS3Store: (options: S3StoreConfig) => Effect.Effect<DataStore<UploadFile>, never, UploadFileKVStore>;
|
|
8
|
+
export declare const s3Store: (config: S3StoreConfig) => Promise<DataStore<UploadFile>>;
|
|
9
|
+
//# sourceMappingURL=s3-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"s3-store.d.ts","sourceRoot":"","sources":["../src/s3-store.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,SAAS,EAGT,UAAU,EAEX,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAe3D,OAAO,EAAE,MAAM,EAAyB,MAAM,QAAQ,CAAC;AACvD,OAAO,EAAiB,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAC9E,OAAO,KAAK,EAAa,aAAa,EAAE,MAAM,SAAS,CAAC;AAgCxD,wBAAgB,2BAA2B,CAAC,MAAM,EAAE,aAAa,gEA+8BhE;AAGD,eAAO,MAAM,aAAa,GAAI,SAAS,aAAa,mEAUhD,CAAC;AAGL,eAAO,MAAM,OAAO,GAAI,QAAQ,aAAa,mCAS5C,CAAC"}
|
package/dist/s3-store.js
ADDED
|
@@ -0,0 +1,666 @@
|
|
|
1
|
+
import { UploadistaError } from "@uploadista/core/errors";
|
|
2
|
+
import { UploadFileKVStore } from "@uploadista/core/types";
|
|
3
|
+
import { s3ActiveUploadsGauge as activeUploadsGauge, s3FileSizeHistogram as fileSizeHistogram, logS3UploadCompletion, s3PartSizeHistogram as partSizeHistogram, s3PartUploadDurationHistogram as partUploadDurationHistogram, s3UploadDurationHistogram as uploadDurationHistogram, s3UploadErrorsTotal as uploadErrorsTotal, s3UploadPartsTotal as uploadPartsTotal, s3UploadRequestsTotal as uploadRequestsTotal, s3UploadSuccessTotal as uploadSuccessTotal, withS3TimingMetrics as withTimingMetrics, withS3UploadMetrics as withUploadMetrics, } from "@uploadista/observability";
|
|
4
|
+
import { Effect, Ref, Schedule, Stream } from "effect";
|
|
5
|
+
import { S3ClientLayer, S3ClientService } from "./services/s3-client.service";
|
|
6
|
+
import { calcOffsetFromParts, calcOptimalPartSize, getExpirationDate, isUploadNotFoundError, } from "./utils";
|
|
7
|
+
/**
|
|
8
|
+
* Generates an S3 key from an upload file, preserving the file extension if available.
|
|
9
|
+
* Looks for filename in metadata under common keys: 'filename', 'fileName', or 'name'.
|
|
10
|
+
* Falls back to just the upload ID if no filename is found.
|
|
11
|
+
*/
|
|
12
|
+
const getS3Key = (uploadFile) => {
|
|
13
|
+
const { id, metadata } = uploadFile;
|
|
14
|
+
if (!metadata) {
|
|
15
|
+
return id;
|
|
16
|
+
}
|
|
17
|
+
// Try common metadata keys for filename
|
|
18
|
+
const filename = metadata.filename || metadata.fileName || metadata.name;
|
|
19
|
+
if (typeof filename === "string" && filename.includes(".")) {
|
|
20
|
+
const extension = filename.substring(filename.lastIndexOf("."));
|
|
21
|
+
return `${id}${extension}`;
|
|
22
|
+
}
|
|
23
|
+
return id;
|
|
24
|
+
};
|
|
25
|
+
// Clean implementation using composed services
|
|
26
|
+
export function createS3StoreImplementation(config) {
|
|
27
|
+
const { deliveryUrl, partSize, minPartSize = 5_242_880, useTags = true, maxMultipartParts = 10_000, kvStore, maxConcurrentPartUploads = 60, expirationPeriodInMilliseconds = 1000 * 60 * 60 * 24 * 7, // 1 week
|
|
28
|
+
s3ClientConfig: { bucket }, } = config;
|
|
29
|
+
return Effect.gen(function* () {
|
|
30
|
+
const s3Client = yield* S3ClientService;
|
|
31
|
+
const preferredPartSize = partSize || 8 * 1024 * 1024;
|
|
32
|
+
const getUploadId = (uploadFile) => {
|
|
33
|
+
const uploadId = uploadFile.storage.uploadId;
|
|
34
|
+
if (!uploadId) {
|
|
35
|
+
return Effect.fail(UploadistaError.fromCode("FILE_WRITE_ERROR", new Error("Upload ID is undefined")));
|
|
36
|
+
}
|
|
37
|
+
return Effect.succeed(uploadId);
|
|
38
|
+
};
|
|
39
|
+
const uploadPart = (uploadFile, data, partNumber) => {
|
|
40
|
+
const s3Key = getS3Key(uploadFile);
|
|
41
|
+
return withTimingMetrics(partUploadDurationHistogram, Effect.gen(function* () {
|
|
42
|
+
const uploadId = yield* getUploadId(uploadFile);
|
|
43
|
+
const etag = yield* s3Client
|
|
44
|
+
.uploadPart({
|
|
45
|
+
bucket: s3Client.bucket,
|
|
46
|
+
key: s3Key,
|
|
47
|
+
uploadId,
|
|
48
|
+
partNumber,
|
|
49
|
+
data,
|
|
50
|
+
})
|
|
51
|
+
.pipe(Effect.retry(Schedule.exponential("1 second", 2.0).pipe(Schedule.intersect(Schedule.recurs(3)))), Effect.tapError((error) => Effect.logWarning("Retrying part upload").pipe(Effect.annotateLogs({
|
|
52
|
+
upload_id: uploadFile.id,
|
|
53
|
+
part_number: partNumber,
|
|
54
|
+
error_message: error.message,
|
|
55
|
+
retry_attempt: "unknown", // Will be overridden by the retry schedule
|
|
56
|
+
part_size: data.length,
|
|
57
|
+
s3_bucket: s3Client.bucket,
|
|
58
|
+
}))));
|
|
59
|
+
yield* uploadPartsTotal(Effect.succeed(1));
|
|
60
|
+
yield* Effect.logInfo("Part uploaded successfully").pipe(Effect.annotateLogs({
|
|
61
|
+
upload_id: uploadFile.id,
|
|
62
|
+
part_number: partNumber,
|
|
63
|
+
part_size: data.length,
|
|
64
|
+
etag: etag,
|
|
65
|
+
}));
|
|
66
|
+
return etag;
|
|
67
|
+
})).pipe(Effect.withSpan(`s3-upload-part-${partNumber}`, {
|
|
68
|
+
attributes: {
|
|
69
|
+
"upload.id": uploadFile.id,
|
|
70
|
+
"upload.part_number": partNumber,
|
|
71
|
+
"upload.part_size": data.length,
|
|
72
|
+
"s3.bucket": s3Client.bucket,
|
|
73
|
+
"s3.key": s3Key,
|
|
74
|
+
},
|
|
75
|
+
}));
|
|
76
|
+
};
|
|
77
|
+
const uploadIncompletePart = (id, data) => s3Client.putIncompletePart(id, data);
|
|
78
|
+
const downloadIncompletePart = (id) => Effect.gen(function* () {
|
|
79
|
+
const incompletePart = yield* s3Client.getIncompletePart(id);
|
|
80
|
+
if (!incompletePart) {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
// Read the stream and collect all chunks to calculate size
|
|
84
|
+
const reader = incompletePart.getReader();
|
|
85
|
+
const chunks = [];
|
|
86
|
+
let incompletePartSize = 0;
|
|
87
|
+
try {
|
|
88
|
+
while (true) {
|
|
89
|
+
const { done, value } = yield* Effect.promise(() => reader.read());
|
|
90
|
+
if (done)
|
|
91
|
+
break;
|
|
92
|
+
chunks.push(value);
|
|
93
|
+
incompletePartSize += value.length;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
finally {
|
|
97
|
+
reader.releaseLock();
|
|
98
|
+
}
|
|
99
|
+
const stream = Stream.fromIterable(chunks);
|
|
100
|
+
return {
|
|
101
|
+
size: incompletePartSize,
|
|
102
|
+
stream,
|
|
103
|
+
};
|
|
104
|
+
});
|
|
105
|
+
const deleteIncompletePart = (id) => s3Client.deleteIncompletePart(id);
|
|
106
|
+
const getIncompletePartSize = (id) => s3Client.getIncompletePartSize(id);
|
|
107
|
+
const complete = (uploadFile, parts) => {
|
|
108
|
+
const s3Key = getS3Key(uploadFile);
|
|
109
|
+
return Effect.gen(function* () {
|
|
110
|
+
const uploadId = yield* getUploadId(uploadFile);
|
|
111
|
+
return yield* s3Client.completeMultipartUpload({
|
|
112
|
+
bucket: s3Client.bucket,
|
|
113
|
+
key: s3Key,
|
|
114
|
+
uploadId,
|
|
115
|
+
}, parts);
|
|
116
|
+
}).pipe(Effect.tap(() => uploadSuccessTotal(Effect.succeed(1))), Effect.withSpan("s3-complete-multipart-upload", {
|
|
117
|
+
attributes: {
|
|
118
|
+
"upload.id": uploadFile.id,
|
|
119
|
+
"upload.parts_count": parts.length,
|
|
120
|
+
"s3.bucket": s3Client.bucket,
|
|
121
|
+
"s3.key": s3Key,
|
|
122
|
+
},
|
|
123
|
+
}));
|
|
124
|
+
};
|
|
125
|
+
const abort = (uploadFile) => {
|
|
126
|
+
const s3Key = getS3Key(uploadFile);
|
|
127
|
+
return Effect.gen(function* () {
|
|
128
|
+
const uploadId = yield* getUploadId(uploadFile);
|
|
129
|
+
yield* s3Client.abortMultipartUpload({
|
|
130
|
+
bucket: s3Client.bucket,
|
|
131
|
+
key: s3Key,
|
|
132
|
+
uploadId,
|
|
133
|
+
});
|
|
134
|
+
yield* s3Client.deleteObjects([s3Key]);
|
|
135
|
+
});
|
|
136
|
+
};
|
|
137
|
+
const retrievePartsRecursive = (s3Key, uploadId, uploadFileId, partNumberMarker) => Effect.gen(function* () {
|
|
138
|
+
try {
|
|
139
|
+
const result = yield* s3Client.listParts({
|
|
140
|
+
bucket: s3Client.bucket,
|
|
141
|
+
key: s3Key,
|
|
142
|
+
uploadId,
|
|
143
|
+
partNumberMarker,
|
|
144
|
+
});
|
|
145
|
+
let parts = result.parts;
|
|
146
|
+
if (result.isTruncated) {
|
|
147
|
+
const rest = yield* retrievePartsRecursive(s3Key, uploadId, uploadFileId, result.nextPartNumberMarker);
|
|
148
|
+
parts = [...parts, ...rest.parts];
|
|
149
|
+
}
|
|
150
|
+
if (!partNumberMarker) {
|
|
151
|
+
parts.sort((a, b) => (a.PartNumber ?? 0) - (b.PartNumber ?? 0));
|
|
152
|
+
}
|
|
153
|
+
return { uploadFound: true, parts };
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
if (isUploadNotFoundError(error)) {
|
|
157
|
+
yield* Effect.logWarning("S3 upload not found during listParts").pipe(Effect.annotateLogs({
|
|
158
|
+
upload_id: uploadFileId,
|
|
159
|
+
error_code: error.code,
|
|
160
|
+
}));
|
|
161
|
+
return { uploadFound: false, parts: [] };
|
|
162
|
+
}
|
|
163
|
+
throw error;
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
const retrieveParts = (id, partNumberMarker) => Effect.gen(function* () {
|
|
167
|
+
const metadata = yield* kvStore.get(id);
|
|
168
|
+
const uploadId = yield* getUploadId(metadata);
|
|
169
|
+
const s3Key = getS3Key(metadata);
|
|
170
|
+
return yield* retrievePartsRecursive(s3Key, uploadId, id, partNumberMarker);
|
|
171
|
+
});
|
|
172
|
+
const completeMetadata = (upload, useTags) => Effect.gen(function* () {
|
|
173
|
+
if (!useTags) {
|
|
174
|
+
return 0;
|
|
175
|
+
}
|
|
176
|
+
const uploadFile = yield* kvStore.get(upload.id);
|
|
177
|
+
const uploadId = uploadFile.storage.uploadId;
|
|
178
|
+
if (!uploadId) {
|
|
179
|
+
return 0;
|
|
180
|
+
}
|
|
181
|
+
yield* kvStore.set(upload.id, {
|
|
182
|
+
...uploadFile,
|
|
183
|
+
storage: { ...uploadFile.storage, uploadId },
|
|
184
|
+
});
|
|
185
|
+
return 0;
|
|
186
|
+
});
|
|
187
|
+
const clearCache = (id) => Effect.gen(function* () {
|
|
188
|
+
yield* Effect.logInfo("Clearing cache").pipe(Effect.annotateLogs({ upload_id: id }));
|
|
189
|
+
yield* kvStore.delete(id);
|
|
190
|
+
});
|
|
191
|
+
const createMultipartUpload = (upload) => {
|
|
192
|
+
const s3Key = getS3Key(upload);
|
|
193
|
+
return Effect.gen(function* () {
|
|
194
|
+
yield* Effect.logInfo("Initializing multipart upload").pipe(Effect.annotateLogs({ upload_id: upload.id }));
|
|
195
|
+
const multipartInfo = yield* s3Client.createMultipartUpload({
|
|
196
|
+
bucket: s3Client.bucket,
|
|
197
|
+
key: s3Key,
|
|
198
|
+
uploadId: "", // Not needed for create
|
|
199
|
+
contentType: upload.metadata?.contentType?.toString(),
|
|
200
|
+
cacheControl: upload.metadata?.cacheControl?.toString(),
|
|
201
|
+
});
|
|
202
|
+
const uploadCreated = {
|
|
203
|
+
...upload,
|
|
204
|
+
storage: {
|
|
205
|
+
...upload.storage,
|
|
206
|
+
path: multipartInfo.key,
|
|
207
|
+
uploadId: multipartInfo.uploadId,
|
|
208
|
+
bucket: multipartInfo.bucket,
|
|
209
|
+
},
|
|
210
|
+
url: `${deliveryUrl}/${s3Key}`,
|
|
211
|
+
};
|
|
212
|
+
yield* kvStore.set(upload.id, uploadCreated);
|
|
213
|
+
yield* Effect.logInfo("Multipart upload created").pipe(Effect.annotateLogs({
|
|
214
|
+
upload_id: upload.id,
|
|
215
|
+
s3_upload_id: uploadCreated.storage.uploadId,
|
|
216
|
+
s3_key: s3Key,
|
|
217
|
+
}));
|
|
218
|
+
yield* uploadRequestsTotal(Effect.succeed(1));
|
|
219
|
+
yield* fileSizeHistogram(Effect.succeed(upload.size || 0));
|
|
220
|
+
return uploadCreated;
|
|
221
|
+
}).pipe(Effect.withSpan("s3-create-upload", {
|
|
222
|
+
attributes: {
|
|
223
|
+
"upload.id": upload.id,
|
|
224
|
+
"upload.size": upload.size || 0,
|
|
225
|
+
"s3.bucket": s3Client.bucket,
|
|
226
|
+
"s3.key": s3Key,
|
|
227
|
+
},
|
|
228
|
+
}));
|
|
229
|
+
};
|
|
230
|
+
/**
|
|
231
|
+
* Creates a multipart upload on S3 attaching any metadata to it.
|
|
232
|
+
* Also, a `${file_id}.info` file is created which holds some information
|
|
233
|
+
* about the upload itself like: `upload-id`, `upload-length`, etc.
|
|
234
|
+
*/
|
|
235
|
+
const create = (upload) => {
|
|
236
|
+
return Effect.gen(function* () {
|
|
237
|
+
yield* Effect.logInfo("Initializing multipart upload").pipe(Effect.annotateLogs({ upload_id: upload.id }));
|
|
238
|
+
const uploadCreated = yield* createMultipartUpload(upload);
|
|
239
|
+
yield* kvStore.set(upload.id, uploadCreated);
|
|
240
|
+
yield* Effect.logInfo("Multipart upload created").pipe(Effect.annotateLogs({
|
|
241
|
+
upload_id: upload.id,
|
|
242
|
+
s3_upload_id: uploadCreated.storage.uploadId,
|
|
243
|
+
}));
|
|
244
|
+
yield* uploadRequestsTotal(Effect.succeed(1));
|
|
245
|
+
return uploadCreated;
|
|
246
|
+
}).pipe(Effect.withSpan("s3-create-upload", {
|
|
247
|
+
attributes: {
|
|
248
|
+
"upload.id": upload.id,
|
|
249
|
+
"upload.size": upload.size || 0,
|
|
250
|
+
"s3.bucket": bucket,
|
|
251
|
+
},
|
|
252
|
+
}));
|
|
253
|
+
};
|
|
254
|
+
const remove = (id) => Effect.gen(function* () {
|
|
255
|
+
const uploadFile = yield* kvStore.get(id);
|
|
256
|
+
yield* abort(uploadFile);
|
|
257
|
+
yield* clearCache(id);
|
|
258
|
+
});
|
|
259
|
+
const write = (options, dependencies) => withUploadMetrics(options.file_id, withTimingMetrics(uploadDurationHistogram, Effect.gen(function* () {
|
|
260
|
+
const { stream: initialData, file_id, offset: initialOffset, } = options;
|
|
261
|
+
const { onProgress } = dependencies;
|
|
262
|
+
// Capture start time for upload completion metrics
|
|
263
|
+
const startTime = Date.now();
|
|
264
|
+
// Track active upload
|
|
265
|
+
yield* activeUploadsGauge(Effect.succeed(1));
|
|
266
|
+
const prepareResult = yield* prepareUpload(file_id, initialOffset, initialData);
|
|
267
|
+
const { uploadFile, nextPartNumber, offset, data, existingPartSize, } = prepareResult;
|
|
268
|
+
// Use existing part size if parts already exist, otherwise calculate optimal size
|
|
269
|
+
const uploadPartSize = existingPartSize ||
|
|
270
|
+
calcOptimalPartSize(uploadFile.size, preferredPartSize, minPartSize, maxMultipartParts);
|
|
271
|
+
// Log part size decision for debugging
|
|
272
|
+
yield* Effect.logInfo("Part size decision").pipe(Effect.annotateLogs({
|
|
273
|
+
upload_id: file_id,
|
|
274
|
+
existing_part_size: existingPartSize,
|
|
275
|
+
calculated_part_size: calcOptimalPartSize(uploadFile.size, preferredPartSize, minPartSize, maxMultipartParts),
|
|
276
|
+
final_part_size: uploadPartSize,
|
|
277
|
+
next_part_number: nextPartNumber,
|
|
278
|
+
}));
|
|
279
|
+
const bytesUploaded = yield* uploadParts(uploadFile, data, nextPartNumber, offset, uploadPartSize, minPartSize, maxConcurrentPartUploads, onProgress);
|
|
280
|
+
const newOffset = offset + bytesUploaded;
|
|
281
|
+
if (newOffset > maxConcurrentPartUploads)
|
|
282
|
+
if (uploadFile.size === newOffset) {
|
|
283
|
+
yield* finishUpload(file_id, uploadFile, startTime);
|
|
284
|
+
}
|
|
285
|
+
return newOffset;
|
|
286
|
+
}).pipe(Effect.ensuring(activeUploadsGauge(Effect.succeed(0))))));
|
|
287
|
+
const getUpload = (id) => Effect.gen(function* () {
|
|
288
|
+
const uploadFile = yield* kvStore.get(id);
|
|
289
|
+
const { parts, uploadFound } = yield* retrieveParts(id);
|
|
290
|
+
if (!uploadFound) {
|
|
291
|
+
return {
|
|
292
|
+
...uploadFile,
|
|
293
|
+
offset: uploadFile.size,
|
|
294
|
+
size: uploadFile.size,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
const offset = calcOffsetFromParts(parts);
|
|
298
|
+
const incompletePartSize = yield* getIncompletePartSize(id);
|
|
299
|
+
return {
|
|
300
|
+
...uploadFile,
|
|
301
|
+
offset: offset + (incompletePartSize ?? 0),
|
|
302
|
+
size: uploadFile.size,
|
|
303
|
+
storage: uploadFile.storage,
|
|
304
|
+
};
|
|
305
|
+
});
|
|
306
|
+
// const read = (id: string) =>
|
|
307
|
+
// Effect.gen(function* () {
|
|
308
|
+
// return yield* s3Client.getObject(id);
|
|
309
|
+
// });
|
|
310
|
+
// Helper functions
|
|
311
|
+
const prepareUpload = (fileId, initialOffset, initialData) => Effect.gen(function* () {
|
|
312
|
+
const uploadFile = yield* kvStore.get(fileId);
|
|
313
|
+
const { parts } = yield* retrieveParts(fileId);
|
|
314
|
+
const partNumber = parts.length > 0 && parts[parts.length - 1].PartNumber
|
|
315
|
+
? (parts[parts.length - 1].PartNumber ?? 0)
|
|
316
|
+
: 0;
|
|
317
|
+
const nextPartNumber = partNumber + 1;
|
|
318
|
+
// Detect existing part size to maintain consistency
|
|
319
|
+
// We check the first part's size to ensure all subsequent parts match
|
|
320
|
+
const existingPartSize = parts.length > 0 && parts[0].Size ? parts[0].Size : null;
|
|
321
|
+
// Validate that all existing parts (except potentially the last one) have the same size
|
|
322
|
+
if (existingPartSize && parts.length > 1) {
|
|
323
|
+
const inconsistentPart = parts
|
|
324
|
+
.slice(0, -1)
|
|
325
|
+
.find((part) => part.Size !== existingPartSize);
|
|
326
|
+
if (inconsistentPart) {
|
|
327
|
+
yield* Effect.logWarning("Inconsistent part sizes detected in existing upload").pipe(Effect.annotateLogs({
|
|
328
|
+
upload_id: fileId,
|
|
329
|
+
expected_size: existingPartSize,
|
|
330
|
+
inconsistent_part: inconsistentPart.PartNumber,
|
|
331
|
+
inconsistent_size: inconsistentPart.Size,
|
|
332
|
+
}));
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
const incompletePart = yield* downloadIncompletePart(fileId);
|
|
336
|
+
if (incompletePart) {
|
|
337
|
+
yield* deleteIncompletePart(fileId);
|
|
338
|
+
const offset = initialOffset - incompletePart.size;
|
|
339
|
+
const data = incompletePart.stream.pipe(Stream.concat(initialData));
|
|
340
|
+
return {
|
|
341
|
+
uploadFile,
|
|
342
|
+
nextPartNumber,
|
|
343
|
+
offset,
|
|
344
|
+
incompletePartSize: incompletePart.size,
|
|
345
|
+
data,
|
|
346
|
+
existingPartSize,
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
else {
|
|
350
|
+
return {
|
|
351
|
+
uploadFile,
|
|
352
|
+
nextPartNumber,
|
|
353
|
+
offset: initialOffset,
|
|
354
|
+
incompletePartSize: 0,
|
|
355
|
+
data: initialData,
|
|
356
|
+
existingPartSize,
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
const finishUpload = (fileId, uploadFile, startTime) => Effect.gen(function* () {
|
|
361
|
+
const { parts } = yield* retrieveParts(fileId);
|
|
362
|
+
// Log all parts for debugging S3 multipart upload requirements
|
|
363
|
+
yield* Effect.logInfo("Attempting to complete multipart upload").pipe(Effect.annotateLogs({
|
|
364
|
+
upload_id: fileId,
|
|
365
|
+
parts_count: parts.length,
|
|
366
|
+
parts_info: parts.map((part, index) => ({
|
|
367
|
+
part_number: part.PartNumber,
|
|
368
|
+
size: part.Size,
|
|
369
|
+
etag: part.ETag,
|
|
370
|
+
is_final_part: index === parts.length - 1,
|
|
371
|
+
})),
|
|
372
|
+
}));
|
|
373
|
+
yield* complete(uploadFile, parts);
|
|
374
|
+
yield* completeMetadata(uploadFile, useTags);
|
|
375
|
+
// yield* clearCache(fileId);
|
|
376
|
+
// Log upload completion metrics
|
|
377
|
+
const endTime = Date.now();
|
|
378
|
+
const totalDurationMs = endTime - startTime;
|
|
379
|
+
const fileSize = uploadFile.size || 0;
|
|
380
|
+
const throughputBps = totalDurationMs > 0 ? (fileSize * 1000) / totalDurationMs : 0;
|
|
381
|
+
// Calculate average part size if we have parts
|
|
382
|
+
const averagePartSize = parts.length > 0
|
|
383
|
+
? parts.reduce((sum, part) => sum + (part.Size || 0), 0) /
|
|
384
|
+
parts.length
|
|
385
|
+
: undefined;
|
|
386
|
+
yield* logS3UploadCompletion(fileId, {
|
|
387
|
+
fileSize,
|
|
388
|
+
totalDurationMs,
|
|
389
|
+
partsCount: parts.length,
|
|
390
|
+
averagePartSize,
|
|
391
|
+
throughputBps,
|
|
392
|
+
});
|
|
393
|
+
}).pipe(Effect.tapError((error) => Effect.gen(function* () {
|
|
394
|
+
yield* uploadErrorsTotal(Effect.succeed(1));
|
|
395
|
+
yield* Effect.logError("Failed to finish upload").pipe(Effect.annotateLogs({
|
|
396
|
+
upload_id: fileId,
|
|
397
|
+
error: String(error),
|
|
398
|
+
}));
|
|
399
|
+
})));
|
|
400
|
+
const deleteExpired = Effect.gen(function* () {
|
|
401
|
+
if (expirationPeriodInMilliseconds === 0) {
|
|
402
|
+
return 0;
|
|
403
|
+
}
|
|
404
|
+
let keyMarker;
|
|
405
|
+
let uploadIdMarker;
|
|
406
|
+
let isTruncated = true;
|
|
407
|
+
let deleted = 0;
|
|
408
|
+
while (isTruncated) {
|
|
409
|
+
const listResponse = yield* s3Client.listMultipartUploads(keyMarker, uploadIdMarker);
|
|
410
|
+
const expiredUploads = listResponse.Uploads?.filter((multiPartUpload) => {
|
|
411
|
+
const initiatedDate = multiPartUpload.Initiated;
|
|
412
|
+
return (initiatedDate &&
|
|
413
|
+
Date.now() >
|
|
414
|
+
getExpirationDate(initiatedDate.toISOString(), expirationPeriodInMilliseconds).getTime());
|
|
415
|
+
}) || [];
|
|
416
|
+
const objectsToDelete = expiredUploads
|
|
417
|
+
.filter((upload) => {
|
|
418
|
+
return !!upload.Key;
|
|
419
|
+
})
|
|
420
|
+
.map((upload) => upload.Key);
|
|
421
|
+
if (objectsToDelete.length > 0) {
|
|
422
|
+
yield* s3Client.deleteObjects(objectsToDelete);
|
|
423
|
+
// Abort multipart uploads
|
|
424
|
+
yield* Effect.forEach(expiredUploads, (upload) => {
|
|
425
|
+
return Effect.gen(function* () {
|
|
426
|
+
if (!upload.Key || !upload.UploadId) {
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
yield* s3Client.abortMultipartUpload({
|
|
430
|
+
bucket,
|
|
431
|
+
key: upload.Key,
|
|
432
|
+
uploadId: upload.UploadId,
|
|
433
|
+
});
|
|
434
|
+
return;
|
|
435
|
+
});
|
|
436
|
+
});
|
|
437
|
+
deleted += objectsToDelete.length;
|
|
438
|
+
}
|
|
439
|
+
isTruncated = listResponse.IsTruncated ?? false;
|
|
440
|
+
if (isTruncated) {
|
|
441
|
+
keyMarker = listResponse.NextKeyMarker;
|
|
442
|
+
uploadIdMarker = listResponse.NextUploadIdMarker;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
return deleted;
|
|
446
|
+
});
|
|
447
|
+
// Proper single-pass chunking using Effect's async stream constructor
|
|
448
|
+
// Ensures all parts except the final part are exactly the same size (S3 requirement)
|
|
449
|
+
const createChunkedStream = (chunkSize) => (stream) => {
|
|
450
|
+
return Stream.async((emit) => {
|
|
451
|
+
let buffer = new Uint8Array(0);
|
|
452
|
+
let partNumber = 1;
|
|
453
|
+
let totalBytesProcessed = 0;
|
|
454
|
+
const emitChunk = (data, isFinalChunk = false) => {
|
|
455
|
+
// Log chunk information for debugging - use INFO level to see in logs
|
|
456
|
+
Effect.runSync(Effect.logInfo("Creating chunk").pipe(Effect.annotateLogs({
|
|
457
|
+
part_number: partNumber,
|
|
458
|
+
chunk_size: data.length,
|
|
459
|
+
expected_size: chunkSize,
|
|
460
|
+
is_final_chunk: isFinalChunk,
|
|
461
|
+
total_bytes_processed: totalBytesProcessed + data.length,
|
|
462
|
+
})));
|
|
463
|
+
emit.single({
|
|
464
|
+
partNumber: partNumber++,
|
|
465
|
+
data,
|
|
466
|
+
size: data.length,
|
|
467
|
+
});
|
|
468
|
+
};
|
|
469
|
+
const processChunk = (newData) => {
|
|
470
|
+
// Combine buffer with new data
|
|
471
|
+
const combined = new Uint8Array(buffer.length + newData.length);
|
|
472
|
+
combined.set(buffer);
|
|
473
|
+
combined.set(newData, buffer.length);
|
|
474
|
+
buffer = combined;
|
|
475
|
+
totalBytesProcessed += newData.length;
|
|
476
|
+
// Emit full chunks of exactly chunkSize bytes
|
|
477
|
+
// This ensures S3 multipart upload rule: all parts except last must be same size
|
|
478
|
+
while (buffer.length >= chunkSize) {
|
|
479
|
+
const chunk = buffer.slice(0, chunkSize);
|
|
480
|
+
buffer = buffer.slice(chunkSize);
|
|
481
|
+
emitChunk(chunk, false);
|
|
482
|
+
}
|
|
483
|
+
};
|
|
484
|
+
// Process the stream
|
|
485
|
+
Effect.runFork(stream.pipe(Stream.runForEach((chunk) => Effect.sync(() => processChunk(chunk))), Effect.andThen(() => Effect.sync(() => {
|
|
486
|
+
// Emit final chunk if there's remaining data
|
|
487
|
+
// The final chunk can be any size < chunkSize (S3 allows this)
|
|
488
|
+
if (buffer.length > 0) {
|
|
489
|
+
emitChunk(buffer, true);
|
|
490
|
+
}
|
|
491
|
+
emit.end();
|
|
492
|
+
})), Effect.catchAll((error) => Effect.sync(() => emit.fail(error)))));
|
|
493
|
+
});
|
|
494
|
+
};
|
|
495
|
+
// Byte-level progress tracking during streaming
|
|
496
|
+
// This provides smooth, immediate progress feedback by tracking bytes as they
|
|
497
|
+
// flow through the stream, before they reach S3. This solves the issue where
|
|
498
|
+
// small files (< 5MB) would jump from 0% to 100% instantly.
|
|
499
|
+
const withByteProgressTracking = (onProgress, initialOffset = 0) => (stream) => {
|
|
500
|
+
if (!onProgress)
|
|
501
|
+
return stream;
|
|
502
|
+
return Effect.gen(function* () {
|
|
503
|
+
const totalBytesProcessedRef = yield* Ref.make(initialOffset);
|
|
504
|
+
return stream.pipe(Stream.tap((chunk) => Effect.gen(function* () {
|
|
505
|
+
const newTotal = yield* Ref.updateAndGet(totalBytesProcessedRef, (total) => total + chunk.length);
|
|
506
|
+
onProgress(newTotal);
|
|
507
|
+
})));
|
|
508
|
+
}).pipe(Stream.unwrap);
|
|
509
|
+
};
|
|
510
|
+
const uploadParts = (uploadFile, readStream, initCurrentPartNumber, initOffset, uploadPartSize, minPartSize, maxConcurrentPartUploads, onProgress) => Effect.gen(function* () {
|
|
511
|
+
yield* Effect.logInfo("Starting part uploads").pipe(Effect.annotateLogs({
|
|
512
|
+
upload_id: uploadFile.id,
|
|
513
|
+
init_offset: initOffset,
|
|
514
|
+
file_size: uploadFile.size,
|
|
515
|
+
part_size: uploadPartSize,
|
|
516
|
+
min_part_size: minPartSize,
|
|
517
|
+
}));
|
|
518
|
+
// Enhanced Progress Tracking Strategy:
|
|
519
|
+
// 1. Byte-level progress during streaming - provides immediate, smooth feedback
|
|
520
|
+
// as data flows through the pipeline (even for small files)
|
|
521
|
+
// 2. This tracks progress BEFORE S3 upload, giving users immediate feedback
|
|
522
|
+
// 3. For large files with multiple parts, this provides granular updates
|
|
523
|
+
// 4. For small files (single part), this prevents 0%->100% jumps
|
|
524
|
+
const chunkStream = readStream.pipe(
|
|
525
|
+
// Add byte-level progress tracking during streaming (immediate feedback)
|
|
526
|
+
withByteProgressTracking(onProgress, initOffset),
|
|
527
|
+
// Create chunks for S3 multipart upload with uniform part sizes
|
|
528
|
+
createChunkedStream(uploadPartSize));
|
|
529
|
+
// Track cumulative offset and total bytes with Effect Refs
|
|
530
|
+
const cumulativeOffsetRef = yield* Ref.make(initOffset);
|
|
531
|
+
const totalBytesUploadedRef = yield* Ref.make(0);
|
|
532
|
+
// Create a chunk upload function for the sink
|
|
533
|
+
const uploadChunk = (chunkInfo) => Effect.gen(function* () {
|
|
534
|
+
// Calculate cumulative bytes to determine if this is the final part
|
|
535
|
+
const cumulativeOffset = yield* Ref.updateAndGet(cumulativeOffsetRef, (offset) => offset + chunkInfo.size);
|
|
536
|
+
const isFinalPart = cumulativeOffset >= (uploadFile.size || 0);
|
|
537
|
+
yield* Effect.logDebug("Processing chunk").pipe(Effect.annotateLogs({
|
|
538
|
+
upload_id: uploadFile.id,
|
|
539
|
+
cumulative_offset: cumulativeOffset,
|
|
540
|
+
file_size: uploadFile.size,
|
|
541
|
+
chunk_size: chunkInfo.size,
|
|
542
|
+
is_final_part: isFinalPart,
|
|
543
|
+
}));
|
|
544
|
+
const actualPartNumber = initCurrentPartNumber + chunkInfo.partNumber - 1;
|
|
545
|
+
if (chunkInfo.size > uploadPartSize) {
|
|
546
|
+
yield* Effect.fail(UploadistaError.fromCode("FILE_WRITE_ERROR", new Error(`Part size ${chunkInfo.size} exceeds upload part size ${uploadPartSize}`)));
|
|
547
|
+
}
|
|
548
|
+
// For parts that meet the minimum part size (5MB) or are the final part,
|
|
549
|
+
// upload them as regular multipart parts
|
|
550
|
+
if (chunkInfo.size >= minPartSize || isFinalPart) {
|
|
551
|
+
yield* Effect.logDebug("Uploading multipart chunk").pipe(Effect.annotateLogs({
|
|
552
|
+
upload_id: uploadFile.id,
|
|
553
|
+
part_number: actualPartNumber,
|
|
554
|
+
chunk_size: chunkInfo.size,
|
|
555
|
+
min_part_size: minPartSize,
|
|
556
|
+
is_final_part: isFinalPart,
|
|
557
|
+
}));
|
|
558
|
+
yield* uploadPart(uploadFile, chunkInfo.data, actualPartNumber);
|
|
559
|
+
yield* partSizeHistogram(Effect.succeed(chunkInfo.size));
|
|
560
|
+
}
|
|
561
|
+
else {
|
|
562
|
+
// Only upload as incomplete part if it's smaller than minimum and not final
|
|
563
|
+
yield* uploadIncompletePart(uploadFile.id, chunkInfo.data);
|
|
564
|
+
}
|
|
565
|
+
yield* Ref.update(totalBytesUploadedRef, (total) => total + chunkInfo.size);
|
|
566
|
+
// Note: Byte-level progress is now tracked during streaming phase
|
|
567
|
+
// This ensures smooth progress updates regardless of part size
|
|
568
|
+
// S3 upload completion is tracked via totalBytesUploadedRef for accuracy
|
|
569
|
+
});
|
|
570
|
+
// Process chunks concurrently with controlled concurrency
|
|
571
|
+
yield* chunkStream.pipe(Stream.runForEach((chunkInfo) => uploadChunk(chunkInfo)), Effect.withConcurrency(maxConcurrentPartUploads));
|
|
572
|
+
return yield* Ref.get(totalBytesUploadedRef);
|
|
573
|
+
});
|
|
574
|
+
const getCapabilities = () => ({
|
|
575
|
+
supportsParallelUploads: true,
|
|
576
|
+
supportsConcatenation: true,
|
|
577
|
+
supportsDeferredLength: true,
|
|
578
|
+
supportsResumableUploads: true,
|
|
579
|
+
supportsTransactionalUploads: true,
|
|
580
|
+
maxConcurrentUploads: maxConcurrentPartUploads,
|
|
581
|
+
minChunkSize: minPartSize,
|
|
582
|
+
maxChunkSize: 5_368_709_120, // 5GiB S3 limit
|
|
583
|
+
maxParts: maxMultipartParts,
|
|
584
|
+
optimalChunkSize: preferredPartSize,
|
|
585
|
+
requiresOrderedChunks: false,
|
|
586
|
+
requiresMimeTypeValidation: true,
|
|
587
|
+
maxValidationSize: undefined, // no size limit
|
|
588
|
+
});
|
|
589
|
+
const getChunkerConstraints = () => ({
|
|
590
|
+
minChunkSize: minPartSize,
|
|
591
|
+
maxChunkSize: 5_368_709_120, // 5GiB S3 limit
|
|
592
|
+
optimalChunkSize: preferredPartSize,
|
|
593
|
+
requiresOrderedChunks: false,
|
|
594
|
+
});
|
|
595
|
+
const validateUploadStrategy = (strategy) => {
|
|
596
|
+
const capabilities = getCapabilities();
|
|
597
|
+
const result = (() => {
|
|
598
|
+
switch (strategy) {
|
|
599
|
+
case "parallel":
|
|
600
|
+
return capabilities.supportsParallelUploads;
|
|
601
|
+
case "single":
|
|
602
|
+
return true;
|
|
603
|
+
default:
|
|
604
|
+
return false;
|
|
605
|
+
}
|
|
606
|
+
})();
|
|
607
|
+
return Effect.succeed(result);
|
|
608
|
+
};
|
|
609
|
+
const concatArrayBuffers = (chunks) => {
|
|
610
|
+
const result = new Uint8Array(chunks.reduce((a, c) => a + c.length, 0));
|
|
611
|
+
let offset = 0;
|
|
612
|
+
for (const chunk of chunks) {
|
|
613
|
+
result.set(chunk, offset);
|
|
614
|
+
offset += chunk.length;
|
|
615
|
+
}
|
|
616
|
+
return result;
|
|
617
|
+
};
|
|
618
|
+
const streamToArray = async (stream) => {
|
|
619
|
+
const reader = stream.getReader();
|
|
620
|
+
const chunks = [];
|
|
621
|
+
while (true) {
|
|
622
|
+
const { done, value } = await reader.read();
|
|
623
|
+
if (done)
|
|
624
|
+
break;
|
|
625
|
+
chunks.push(value);
|
|
626
|
+
}
|
|
627
|
+
return concatArrayBuffers(chunks);
|
|
628
|
+
};
|
|
629
|
+
const read = (id) => Effect.gen(function* () {
|
|
630
|
+
const upload = yield* kvStore.get(id);
|
|
631
|
+
console.log(upload);
|
|
632
|
+
if (!upload.id) {
|
|
633
|
+
return yield* Effect.fail(UploadistaError.fromCode("FILE_READ_ERROR", new Error("Upload Key is undefined")));
|
|
634
|
+
}
|
|
635
|
+
const s3Key = getS3Key(upload);
|
|
636
|
+
const stream = yield* s3Client.getObject(s3Key);
|
|
637
|
+
return yield* Effect.promise(() => streamToArray(stream));
|
|
638
|
+
});
|
|
639
|
+
return {
|
|
640
|
+
bucket,
|
|
641
|
+
create,
|
|
642
|
+
remove,
|
|
643
|
+
write,
|
|
644
|
+
getUpload,
|
|
645
|
+
read,
|
|
646
|
+
deleteExpired,
|
|
647
|
+
getCapabilities,
|
|
648
|
+
getChunkerConstraints,
|
|
649
|
+
validateUploadStrategy,
|
|
650
|
+
};
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
// Effect-based factory that uses services
|
|
654
|
+
export const createS3Store = (options) => Effect.gen(function* () {
|
|
655
|
+
const kvStore = yield* UploadFileKVStore;
|
|
656
|
+
const { s3ClientConfig: { bucket, ...restS3ClientConfig }, } = options;
|
|
657
|
+
return yield* createS3StoreImplementation({
|
|
658
|
+
...options,
|
|
659
|
+
kvStore,
|
|
660
|
+
}).pipe(Effect.provide(S3ClientLayer(restS3ClientConfig, bucket)));
|
|
661
|
+
});
|
|
662
|
+
// Backward compatibility: keep the original function for existing code
|
|
663
|
+
export const s3Store = (config) => {
|
|
664
|
+
const { s3ClientConfig: { bucket, ...restS3ClientConfig }, } = config;
|
|
665
|
+
return Effect.runPromise(createS3StoreImplementation(config).pipe(Effect.provide(S3ClientLayer(restS3ClientConfig, bucket))));
|
|
666
|
+
};
|