@uploadista/data-store-s3 0.0.3 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +86 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +85 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -0
- package/package.json +7 -6
- package/tsdown.config.ts +11 -0
- package/.turbo/turbo-build.log +0 -5
- package/.turbo/turbo-check.log +0 -5
- package/dist/observability.d.ts +0 -45
- package/dist/observability.d.ts.map +0 -1
- package/dist/observability.js +0 -155
- package/dist/s3-store-old.d.ts +0 -51
- package/dist/s3-store-old.d.ts.map +0 -1
- package/dist/s3-store-old.js +0 -765
- package/dist/s3-store.d.ts +0 -9
- package/dist/s3-store.d.ts.map +0 -1
- package/dist/s3-store.js +0 -666
- package/dist/services/__mocks__/s3-client-mock.service.d.ts +0 -44
- package/dist/services/__mocks__/s3-client-mock.service.d.ts.map +0 -1
- package/dist/services/__mocks__/s3-client-mock.service.js +0 -379
- package/dist/services/index.d.ts +0 -2
- package/dist/services/index.d.ts.map +0 -1
- package/dist/services/index.js +0 -1
- package/dist/services/s3-client.service.d.ts +0 -68
- package/dist/services/s3-client.service.d.ts.map +0 -1
- package/dist/services/s3-client.service.js +0 -209
- package/dist/test-observability.d.ts +0 -6
- package/dist/test-observability.d.ts.map +0 -1
- package/dist/test-observability.js +0 -62
- package/dist/types.d.ts +0 -81
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -1
- package/dist/utils/calculations.d.ts +0 -7
- package/dist/utils/calculations.d.ts.map +0 -1
- package/dist/utils/calculations.js +0 -41
- package/dist/utils/error-handling.d.ts +0 -7
- package/dist/utils/error-handling.d.ts.map +0 -1
- package/dist/utils/error-handling.js +0 -29
- package/dist/utils/index.d.ts +0 -4
- package/dist/utils/index.d.ts.map +0 -1
- package/dist/utils/index.js +0 -3
- package/dist/utils/stream-adapter.d.ts +0 -14
- package/dist/utils/stream-adapter.d.ts.map +0 -1
- package/dist/utils/stream-adapter.js +0 -41
- package/tsconfig.tsbuildinfo +0 -1
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));let c=require(`@uploadista/core/errors`);c=s(c);let l=require(`@uploadista/core/types`);l=s(l);let u=require(`@uploadista/observability`);u=s(u);let d=require(`effect`);d=s(d);let f=require(`@aws-sdk/client-s3`);f=s(f);const p=e=>e&&e.length>0?e.reduce((e,t)=>e+(t?.Size??0),0):0,m=(e,t,n,r,i=5497558138880)=>{let a=e??i,o;o=a<=t?a:a<=t*r?t:Math.ceil(a/r);let s=e&&e<n?o:Math.max(o,n),c=1024;return Math.ceil(s/c)*c},h=e=>`${e}.part`,g=(e,t)=>{let n=new Date(e);return new Date(n.getTime()+t)},_=(e,t,n={})=>(d.Effect.runSync((0,u.trackS3Error)(e,t,n)),c.UploadistaError.fromCode(`FILE_WRITE_ERROR`,t)),v=(e,t,n={})=>typeof t==`object`&&t&&`code`in t&&typeof t.code==`string`&&[`NotFound`,`NoSuchKey`,`NoSuchUpload`].includes(t.code)?(d.Effect.runSync(d.Effect.logWarning(`File not found during ${e} operation`).pipe(d.Effect.annotateLogs({error_code:t.code,...n}))),c.UploadistaError.fromCode(`FILE_NOT_FOUND`)):_(e,t,n),y=e=>typeof e==`object`&&!!e&&`code`in e&&typeof e.code==`string`&&(e.code===`NoSuchUpload`||e.code===`NoSuchKey`);function b(e){if(e instanceof ReadableStream||e&&typeof e==`object`&&`getReader`in e)return e;if(e&&typeof e==`object`&&`pipe`in e&&`on`in e){let t=e;return new ReadableStream({start(e){t.on(`data`,t=>{e.enqueue(new Uint8Array(t))}),t.on(`end`,()=>{e.close()}),t.on(`error`,t=>{e.error(t)})}})}throw Error(`Unsupported body type: ${typeof e}. Expected ReadableStream or Node.js Readable.`)}var x=class extends d.Context.Tag(`S3ClientService`)(){};const S=(e,t)=>{let n=new f.S3(e),r=e=>d.Effect.tryPromise({try:async()=>b((await n.getObject({Bucket:t,Key:e})).Body),catch:n=>_(`getObject`,n,{key:e,bucket:t})}),i=e=>d.Effect.tryPromise({try:async()=>{try{return(await n.headObject({Bucket:t,Key:e})).ContentLength}catch(e){if(e instanceof f.NotFound)return;throw e}},catch:n=>_(`headObject`,n,{key:e,bucket:t})}),a=(e,r)=>d.Effect.tryPromise({try:async()=>(await n.putObject({Bucket:t,Key:e,Body:r})).ETag||``,catch:n=>_(`putObject`,n,{key:e,bucket:t,size:r.length})}),o=e=>d.Effect.tryPromise({try:async()=>{await n.deleteObject({Bucket:t,Key:e})},catch:n=>_(`deleteObject`,n,{key:e,bucket:t})});return{bucket:t,getObject:r,headObject:i,putObject:a,deleteObject:o,deleteObjects:e=>d.Effect.tryPromise({try:()=>n.deleteObjects({Bucket:t,Delete:{Objects:e.map(e=>({Key:e}))}}),catch:n=>_(`deleteObjects`,n,{keys:e.length,bucket:t})}),createMultipartUpload:e=>(0,u.withS3ApiMetrics)(`createMultipartUpload`,d.Effect.tryPromise({try:async()=>{let t={Bucket:e.bucket,Key:e.key};e.contentType&&(t.ContentType=e.contentType),e.cacheControl&&(t.CacheControl=e.cacheControl);let r=await n.createMultipartUpload(t);if(!r.UploadId)throw Error(`Upload ID is undefined`);if(!r.Key)throw Error(`Key is undefined`);return{uploadId:r.UploadId,bucket:e.bucket,key:r.Key}},catch:t=>_(`createMultipartUpload`,t,e)})),uploadPart:e=>(0,u.withS3ApiMetrics)(`uploadPart`,d.Effect.tryPromise({try:()=>n.uploadPart({Bucket:e.bucket,Key:e.key,UploadId:e.uploadId,PartNumber:e.partNumber,Body:e.data}),catch:t=>_(`uploadPart`,t,{upload_id:e.key,part_number:e.partNumber,part_size:e.data.length,s3_bucket:e.bucket})}).pipe(d.Effect.map(e=>e.ETag))),completeMultipartUpload:(e,t)=>(0,u.withS3ApiMetrics)(`completeMultipartUpload`,d.Effect.tryPromise({try:()=>n.completeMultipartUpload({Bucket:e.bucket,Key:e.key,UploadId:e.uploadId,MultipartUpload:{Parts:t.map(e=>({ETag:e.ETag,PartNumber:e.PartNumber}))}}).then(e=>e.Location),catch:n=>_(`completeMultipartUpload`,n,{upload_id:e.key,parts_count:t.length,s3_bucket:e.bucket})})),abortMultipartUpload:e=>d.Effect.tryPromise({try:async()=>{await n.abortMultipartUpload({Bucket:e.bucket,Key:e.key,UploadId:e.uploadId})},catch:t=>v(`abortMultipartUpload`,t,{upload_id:e.key,s3_bucket:e.bucket})}),listParts:e=>d.Effect.tryPromise({try:async()=>{let t={Bucket:e.bucket,Key:e.key,UploadId:e.uploadId,PartNumberMarker:e.partNumberMarker},r=await n.listParts(t);return{parts:r.Parts??[],isTruncated:r.IsTruncated??!1,nextPartNumberMarker:r.NextPartNumberMarker}},catch:t=>_(`listParts`,t,{upload_id:e.key,s3_bucket:e.bucket})}),listMultipartUploads:(e,r)=>d.Effect.tryPromise({try:()=>n.listMultipartUploads({Bucket:t,KeyMarker:e,UploadIdMarker:r}),catch:e=>_(`listMultipartUploads`,e,{bucket:t})}),getIncompletePart:e=>d.Effect.tryPromise({try:async()=>{try{return b((await n.getObject({Bucket:t,Key:h(e)})).Body)}catch(e){if(e instanceof f.NoSuchKey)return;throw e}},catch:n=>_(`getIncompletePart`,n,{upload_id:e,bucket:t})}),getIncompletePartSize:e=>i(h(e)),putIncompletePart:(e,t)=>a(h(e),t).pipe(d.Effect.tap(()=>d.Effect.logInfo(`Incomplete part uploaded`).pipe(d.Effect.annotateLogs({upload_id:e})))),deleteIncompletePart:e=>o(h(e))}},C=(e,t)=>d.Layer.succeed(x,S(e,t)),w=e=>{let{id:t,metadata:n}=e;if(!n)return t;let r=n.filename||n.fileName||n.name;return typeof r==`string`&&r.includes(`.`)?`${t}${r.substring(r.lastIndexOf(`.`))}`:t};function T(e){let{deliveryUrl:t,partSize:n,minPartSize:r=5242880,useTags:i=!0,maxMultipartParts:a=1e4,kvStore:o,maxConcurrentPartUploads:s=60,expirationPeriodInMilliseconds:l=1e3*60*60*24*7,s3ClientConfig:{bucket:f}}=e;return d.Effect.gen(function*(){let e=yield*x,h=n||8*1024*1024,_=e=>{let t=e.storage.uploadId;return t?d.Effect.succeed(t):d.Effect.fail(c.UploadistaError.fromCode(`FILE_WRITE_ERROR`,Error(`Upload ID is undefined`)))},v=(t,n,r)=>{let i=w(t);return(0,u.withS3TimingMetrics)(u.s3PartUploadDurationHistogram,d.Effect.gen(function*(){let a=yield*_(t),o=yield*e.uploadPart({bucket:e.bucket,key:i,uploadId:a,partNumber:r,data:n}).pipe(d.Effect.retry(d.Schedule.exponential(`1 second`,2).pipe(d.Schedule.intersect(d.Schedule.recurs(3)))),d.Effect.tapError(i=>d.Effect.logWarning(`Retrying part upload`).pipe(d.Effect.annotateLogs({upload_id:t.id,part_number:r,error_message:i.message,retry_attempt:`unknown`,part_size:n.length,s3_bucket:e.bucket}))));return yield*(0,u.s3UploadPartsTotal)(d.Effect.succeed(1)),yield*d.Effect.logInfo(`Part uploaded successfully`).pipe(d.Effect.annotateLogs({upload_id:t.id,part_number:r,part_size:n.length,etag:o})),o})).pipe(d.Effect.withSpan(`s3-upload-part-${r}`,{attributes:{"upload.id":t.id,"upload.part_number":r,"upload.part_size":n.length,"s3.bucket":e.bucket,"s3.key":i}}))},b=(t,n)=>e.putIncompletePart(t,n),S=t=>d.Effect.gen(function*(){let n=yield*e.getIncompletePart(t);if(!n)return;let r=n.getReader(),i=[],a=0;try{for(;;){let{done:e,value:t}=yield*d.Effect.promise(()=>r.read());if(e)break;i.push(t),a+=t.length}}finally{r.releaseLock()}let o=d.Stream.fromIterable(i);return{size:a,stream:o}}),C=t=>e.deleteIncompletePart(t),T=t=>e.getIncompletePartSize(t),E=(t,n)=>{let r=w(t);return d.Effect.gen(function*(){let i=yield*_(t);return yield*e.completeMultipartUpload({bucket:e.bucket,key:r,uploadId:i},n)}).pipe(d.Effect.tap(()=>(0,u.s3UploadSuccessTotal)(d.Effect.succeed(1))),d.Effect.withSpan(`s3-complete-multipart-upload`,{attributes:{"upload.id":t.id,"upload.parts_count":n.length,"s3.bucket":e.bucket,"s3.key":r}}))},D=t=>{let n=w(t);return d.Effect.gen(function*(){let r=yield*_(t);yield*e.abortMultipartUpload({bucket:e.bucket,key:n,uploadId:r}),yield*e.deleteObjects([n])})},O=(t,n,r,i)=>d.Effect.gen(function*(){try{let a=yield*e.listParts({bucket:e.bucket,key:t,uploadId:n,partNumberMarker:i}),o=a.parts;if(a.isTruncated){let e=yield*O(t,n,r,a.nextPartNumberMarker);o=[...o,...e.parts]}return i||o.sort((e,t)=>(e.PartNumber??0)-(t.PartNumber??0)),{uploadFound:!0,parts:o}}catch(e){if(y(e))return yield*d.Effect.logWarning(`S3 upload not found during listParts`).pipe(d.Effect.annotateLogs({upload_id:r,error_code:e.code})),{uploadFound:!1,parts:[]};throw e}}),k=(e,t)=>d.Effect.gen(function*(){let n=yield*o.get(e),r=yield*_(n);return yield*O(w(n),r,e,t)}),A=(e,t)=>d.Effect.gen(function*(){if(!t)return 0;let n=yield*o.get(e.id),r=n.storage.uploadId;return r&&(yield*o.set(e.id,{...n,storage:{...n.storage,uploadId:r}})),0}),j=e=>d.Effect.gen(function*(){yield*d.Effect.logInfo(`Clearing cache`).pipe(d.Effect.annotateLogs({upload_id:e})),yield*o.delete(e)}),M=n=>{let r=w(n);return d.Effect.gen(function*(){yield*d.Effect.logInfo(`Initializing multipart upload`).pipe(d.Effect.annotateLogs({upload_id:n.id}));let i=yield*e.createMultipartUpload({bucket:e.bucket,key:r,uploadId:``,contentType:n.metadata?.contentType?.toString(),cacheControl:n.metadata?.cacheControl?.toString()}),a={...n,storage:{...n.storage,path:i.key,uploadId:i.uploadId,bucket:i.bucket},url:`${t}/${r}`};return yield*o.set(n.id,a),yield*d.Effect.logInfo(`Multipart upload created`).pipe(d.Effect.annotateLogs({upload_id:n.id,s3_upload_id:a.storage.uploadId,s3_key:r})),yield*(0,u.s3UploadRequestsTotal)(d.Effect.succeed(1)),yield*(0,u.s3FileSizeHistogram)(d.Effect.succeed(n.size||0)),a}).pipe(d.Effect.withSpan(`s3-create-upload`,{attributes:{"upload.id":n.id,"upload.size":n.size||0,"s3.bucket":e.bucket,"s3.key":r}}))},N=e=>d.Effect.gen(function*(){yield*d.Effect.logInfo(`Initializing multipart upload`).pipe(d.Effect.annotateLogs({upload_id:e.id}));let t=yield*M(e);return yield*o.set(e.id,t),yield*d.Effect.logInfo(`Multipart upload created`).pipe(d.Effect.annotateLogs({upload_id:e.id,s3_upload_id:t.storage.uploadId})),yield*(0,u.s3UploadRequestsTotal)(d.Effect.succeed(1)),t}).pipe(d.Effect.withSpan(`s3-create-upload`,{attributes:{"upload.id":e.id,"upload.size":e.size||0,"s3.bucket":f}})),P=e=>d.Effect.gen(function*(){yield*D(yield*o.get(e)),yield*j(e)}),F=(e,t)=>(0,u.withS3UploadMetrics)(e.file_id,(0,u.withS3TimingMetrics)(u.s3UploadDurationHistogram,d.Effect.gen(function*(){let{stream:n,file_id:i,offset:o}=e,{onProgress:c}=t,l=Date.now();yield*(0,u.s3ActiveUploadsGauge)(d.Effect.succeed(1));let{uploadFile:f,nextPartNumber:p,offset:g,data:_,existingPartSize:v}=yield*L(i,o,n),y=v||m(f.size,h,r,a);yield*d.Effect.logInfo(`Part size decision`).pipe(d.Effect.annotateLogs({upload_id:i,existing_part_size:v,calculated_part_size:m(f.size,h,r,a),final_part_size:y,next_part_number:p}));let b=g+(yield*H(f,_,p,g,y,r,s,c));return b>s&&f.size===b&&(yield*R(i,f,l)),b}).pipe(d.Effect.ensuring((0,u.s3ActiveUploadsGauge)(d.Effect.succeed(0)))))),I=e=>d.Effect.gen(function*(){let t=yield*o.get(e),{parts:n,uploadFound:r}=yield*k(e);if(!r)return{...t,offset:t.size,size:t.size};let i=p(n),a=yield*T(e);return{...t,offset:i+(a??0),size:t.size,storage:t.storage}}),L=(e,t,n)=>d.Effect.gen(function*(){let r=yield*o.get(e),{parts:i}=yield*k(e),a=(i.length>0&&i[i.length-1].PartNumber?i[i.length-1].PartNumber??0:0)+1,s=i.length>0&&i[0].Size?i[0].Size:null;if(s&&i.length>1){let t=i.slice(0,-1).find(e=>e.Size!==s);t&&(yield*d.Effect.logWarning(`Inconsistent part sizes detected in existing upload`).pipe(d.Effect.annotateLogs({upload_id:e,expected_size:s,inconsistent_part:t.PartNumber,inconsistent_size:t.Size})))}let c=yield*S(e);if(c){yield*C(e);let i=t-c.size,o=c.stream.pipe(d.Stream.concat(n));return{uploadFile:r,nextPartNumber:a,offset:i,incompletePartSize:c.size,data:o,existingPartSize:s}}else return{uploadFile:r,nextPartNumber:a,offset:t,incompletePartSize:0,data:n,existingPartSize:s}}),R=(e,t,n)=>d.Effect.gen(function*(){let{parts:r}=yield*k(e);yield*d.Effect.logInfo(`Attempting to complete multipart upload`).pipe(d.Effect.annotateLogs({upload_id:e,parts_count:r.length,parts_info:r.map((e,t)=>({part_number:e.PartNumber,size:e.Size,etag:e.ETag,is_final_part:t===r.length-1}))})),yield*E(t,r),yield*A(t,i);let a=Date.now()-n,o=t.size||0,s=a>0?o*1e3/a:0,c=r.length>0?r.reduce((e,t)=>e+(t.Size||0),0)/r.length:void 0;yield*(0,u.logS3UploadCompletion)(e,{fileSize:o,totalDurationMs:a,partsCount:r.length,averagePartSize:c,throughputBps:s})}).pipe(d.Effect.tapError(t=>d.Effect.gen(function*(){yield*(0,u.s3UploadErrorsTotal)(d.Effect.succeed(1)),yield*d.Effect.logError(`Failed to finish upload`).pipe(d.Effect.annotateLogs({upload_id:e,error:String(t)}))}))),z=d.Effect.gen(function*(){if(l===0)return 0;let t,n,r=!0,i=0;for(;r;){let a=yield*e.listMultipartUploads(t,n),o=a.Uploads?.filter(e=>{let t=e.Initiated;return t&&Date.now()>g(t.toISOString(),l).getTime()})||[],s=o.filter(e=>!!e.Key).map(e=>e.Key);s.length>0&&(yield*e.deleteObjects(s),yield*d.Effect.forEach(o,t=>d.Effect.gen(function*(){!t.Key||!t.UploadId||(yield*e.abortMultipartUpload({bucket:f,key:t.Key,uploadId:t.UploadId}))})),i+=s.length),r=a.IsTruncated??!1,r&&(t=a.NextKeyMarker,n=a.NextUploadIdMarker)}return i}),B=e=>t=>d.Stream.async(n=>{let r=new Uint8Array,i=1,a=0,o=(t,r=!1)=>{d.Effect.runSync(d.Effect.logInfo(`Creating chunk`).pipe(d.Effect.annotateLogs({part_number:i,chunk_size:t.length,expected_size:e,is_final_chunk:r,total_bytes_processed:a+t.length}))),n.single({partNumber:i++,data:t,size:t.length})},s=t=>{let n=new Uint8Array(r.length+t.length);for(n.set(r),n.set(t,r.length),r=n,a+=t.length;r.length>=e;){let t=r.slice(0,e);r=r.slice(e),o(t,!1)}};d.Effect.runFork(t.pipe(d.Stream.runForEach(e=>d.Effect.sync(()=>s(e))),d.Effect.andThen(()=>d.Effect.sync(()=>{r.length>0&&o(r,!0),n.end()})),d.Effect.catchAll(e=>d.Effect.sync(()=>n.fail(e)))))}),V=(e,t=0)=>n=>e?d.Effect.gen(function*(){let r=yield*d.Ref.make(t);return n.pipe(d.Stream.tap(t=>d.Effect.gen(function*(){e(yield*d.Ref.updateAndGet(r,e=>e+t.length))})))}).pipe(d.Stream.unwrap):n,H=(e,t,n,r,i,a,o,s)=>d.Effect.gen(function*(){yield*d.Effect.logInfo(`Starting part uploads`).pipe(d.Effect.annotateLogs({upload_id:e.id,init_offset:r,file_size:e.size,part_size:i,min_part_size:a}));let l=t.pipe(V(s,r),B(i)),f=yield*d.Ref.make(r),p=yield*d.Ref.make(0),m=t=>d.Effect.gen(function*(){let r=yield*d.Ref.updateAndGet(f,e=>e+t.size),o=r>=(e.size||0);yield*d.Effect.logDebug(`Processing chunk`).pipe(d.Effect.annotateLogs({upload_id:e.id,cumulative_offset:r,file_size:e.size,chunk_size:t.size,is_final_part:o}));let s=n+t.partNumber-1;t.size>i&&(yield*d.Effect.fail(c.UploadistaError.fromCode(`FILE_WRITE_ERROR`,Error(`Part size ${t.size} exceeds upload part size ${i}`)))),t.size>=a||o?(yield*d.Effect.logDebug(`Uploading multipart chunk`).pipe(d.Effect.annotateLogs({upload_id:e.id,part_number:s,chunk_size:t.size,min_part_size:a,is_final_part:o})),yield*v(e,t.data,s),yield*(0,u.s3PartSizeHistogram)(d.Effect.succeed(t.size))):yield*b(e.id,t.data),yield*d.Ref.update(p,e=>e+t.size)});return yield*l.pipe(d.Stream.runForEach(e=>m(e)),d.Effect.withConcurrency(o)),yield*d.Ref.get(p)}),U=()=>({supportsParallelUploads:!0,supportsConcatenation:!0,supportsDeferredLength:!0,supportsResumableUploads:!0,supportsTransactionalUploads:!0,maxConcurrentUploads:s,minChunkSize:r,maxChunkSize:5368709120,maxParts:a,optimalChunkSize:h,requiresOrderedChunks:!1,requiresMimeTypeValidation:!0,maxValidationSize:void 0}),W=()=>({minChunkSize:r,maxChunkSize:5368709120,optimalChunkSize:h,requiresOrderedChunks:!1}),G=e=>{let t=U(),n=(()=>{switch(e){case`parallel`:return t.supportsParallelUploads;case`single`:return!0;default:return!1}})();return d.Effect.succeed(n)},K=e=>{let t=new Uint8Array(e.reduce((e,t)=>e+t.length,0)),n=0;for(let r of e)t.set(r,n),n+=r.length;return t},q=async e=>{let t=e.getReader(),n=[];for(;;){let{done:e,value:r}=await t.read();if(e)break;n.push(r)}return K(n)};return{bucket:f,create:N,remove:P,write:F,getUpload:I,read:t=>d.Effect.gen(function*(){let n=yield*o.get(t);if(console.log(n),!n.id)return yield*d.Effect.fail(c.UploadistaError.fromCode(`FILE_READ_ERROR`,Error(`Upload Key is undefined`)));let r=w(n),i=yield*e.getObject(r);return yield*d.Effect.promise(()=>q(i))}),deleteExpired:z,getCapabilities:U,getChunkerConstraints:W,validateUploadStrategy:G}})}const E=e=>d.Effect.gen(function*(){let t=yield*l.UploadFileKVStore,{s3ClientConfig:{bucket:n,...r}}=e;return yield*T({...e,kvStore:t}).pipe(d.Effect.provide(C(r,n)))}),D=e=>{let{s3ClientConfig:{bucket:t,...n}}=e;return d.Effect.runPromise(T(e).pipe(d.Effect.provide(C(n,t))))};exports.createS3Store=E,exports.createS3StoreImplementation=T,exports.s3Store=D;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { DataStore, KvStore, UploadFile, UploadFileKVStore } from "@uploadista/core/types";
|
|
2
|
+
import { Context, Effect } from "effect";
|
|
3
|
+
import AWS, { S3ClientConfig } from "@aws-sdk/client-s3";
|
|
4
|
+
import { UploadistaError } from "@uploadista/core/errors";
|
|
5
|
+
|
|
6
|
+
//#region src/types.d.ts
|
|
7
|
+
type S3StoreOptions = {
|
|
8
|
+
deliveryUrl: string;
|
|
9
|
+
/**
|
|
10
|
+
* The preferred part size for parts send to S3. Can not be lower than 5MiB or more than 5GiB.
|
|
11
|
+
* The server calculates the optimal part size, which takes this size into account,
|
|
12
|
+
* but may increase it to not exceed the S3 10K parts limit.
|
|
13
|
+
*/
|
|
14
|
+
partSize?: number;
|
|
15
|
+
/**
|
|
16
|
+
* The minimal part size for parts.
|
|
17
|
+
* Can be used to ensure that all non-trailing parts are exactly the same size.
|
|
18
|
+
* Can not be lower than 5MiB or more than 5GiB.
|
|
19
|
+
*/
|
|
20
|
+
minPartSize?: number;
|
|
21
|
+
/**
|
|
22
|
+
* The maximum number of parts allowed in a multipart upload. Defaults to 10,000.
|
|
23
|
+
*/
|
|
24
|
+
maxMultipartParts?: number;
|
|
25
|
+
useTags?: boolean;
|
|
26
|
+
maxConcurrentPartUploads?: number;
|
|
27
|
+
expirationPeriodInMilliseconds?: number;
|
|
28
|
+
s3ClientConfig: S3ClientConfig & {
|
|
29
|
+
bucket: string;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
type S3OperationContext = {
|
|
33
|
+
uploadId: string;
|
|
34
|
+
bucket: string;
|
|
35
|
+
key: string;
|
|
36
|
+
partNumber?: number;
|
|
37
|
+
partSize?: number;
|
|
38
|
+
contentType?: string;
|
|
39
|
+
cacheControl?: string;
|
|
40
|
+
};
|
|
41
|
+
type MultipartUploadInfo = {
|
|
42
|
+
uploadId: string;
|
|
43
|
+
bucket: string;
|
|
44
|
+
key: string;
|
|
45
|
+
};
|
|
46
|
+
type S3StoreConfig = S3StoreOptions & {
|
|
47
|
+
kvStore: KvStore<UploadFile>;
|
|
48
|
+
};
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region src/services/s3-client.service.d.ts
|
|
51
|
+
declare const S3ClientService_base: Context.TagClass<S3ClientService, "S3ClientService", {
|
|
52
|
+
readonly bucket: string;
|
|
53
|
+
readonly getObject: (key: string) => Effect.Effect<ReadableStream, UploadistaError>;
|
|
54
|
+
readonly headObject: (key: string) => Effect.Effect<number | undefined, UploadistaError>;
|
|
55
|
+
readonly putObject: (key: string, body: Uint8Array) => Effect.Effect<string, UploadistaError>;
|
|
56
|
+
readonly deleteObject: (key: string) => Effect.Effect<void, UploadistaError>;
|
|
57
|
+
readonly deleteObjects: (keys: string[]) => Effect.Effect<AWS.DeleteObjectsCommandOutput, UploadistaError>;
|
|
58
|
+
readonly createMultipartUpload: (context: S3OperationContext) => Effect.Effect<MultipartUploadInfo, UploadistaError>;
|
|
59
|
+
readonly uploadPart: (context: S3OperationContext & {
|
|
60
|
+
partNumber: number;
|
|
61
|
+
data: Uint8Array;
|
|
62
|
+
}) => Effect.Effect<string, UploadistaError>;
|
|
63
|
+
readonly completeMultipartUpload: (context: S3OperationContext, parts: Array<AWS.Part>) => Effect.Effect<string | undefined, UploadistaError>;
|
|
64
|
+
readonly abortMultipartUpload: (context: S3OperationContext) => Effect.Effect<void, UploadistaError>;
|
|
65
|
+
readonly listParts: (context: S3OperationContext & {
|
|
66
|
+
partNumberMarker?: string;
|
|
67
|
+
}) => Effect.Effect<{
|
|
68
|
+
parts: AWS.Part[];
|
|
69
|
+
isTruncated: boolean;
|
|
70
|
+
nextPartNumberMarker?: string;
|
|
71
|
+
}, UploadistaError>;
|
|
72
|
+
readonly listMultipartUploads: (keyMarker?: string, uploadIdMarker?: string) => Effect.Effect<AWS.ListMultipartUploadsCommandOutput, UploadistaError>;
|
|
73
|
+
readonly getIncompletePart: (id: string) => Effect.Effect<ReadableStream | undefined, UploadistaError>;
|
|
74
|
+
readonly getIncompletePartSize: (id: string) => Effect.Effect<number | undefined, UploadistaError>;
|
|
75
|
+
readonly putIncompletePart: (id: string, data: Uint8Array) => Effect.Effect<string, UploadistaError>;
|
|
76
|
+
readonly deleteIncompletePart: (id: string) => Effect.Effect<void, UploadistaError>;
|
|
77
|
+
}>;
|
|
78
|
+
declare class S3ClientService extends S3ClientService_base {}
|
|
79
|
+
//#endregion
|
|
80
|
+
//#region src/s3-store.d.ts
|
|
81
|
+
declare function createS3StoreImplementation(config: S3StoreConfig): Effect.Effect<DataStore<UploadFile>, never, S3ClientService>;
|
|
82
|
+
declare const createS3Store: (options: S3StoreConfig) => Effect.Effect<DataStore<UploadFile>, never, UploadFileKVStore>;
|
|
83
|
+
declare const s3Store: (config: S3StoreConfig) => Promise<DataStore<UploadFile>>;
|
|
84
|
+
//#endregion
|
|
85
|
+
export { createS3Store, createS3StoreImplementation, s3Store };
|
|
86
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/types.ts","../src/services/s3-client.service.ts","../src/s3-store.ts"],"sourcesContent":[],"mappings":";;;;;;KAWY,cAAA;;EAAA;AAgCZ;AAeA;AAmCA;;EACmB,QA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,MAAA,EAAA,GAwCP,MAAA,CAAO,MAxCA,CAAA,MAAA,GAAA,SAAA,EAwC2B,eAxC3B,CAAA;EAGO,SAAI,iBAAA,EAAA,CAAA,EAAA,EAAA,MAAA,EAAA,IAAA,EAwCf,UAxCe,EAAA,GAyClB,MAAA,CAAO,MAzCW,CAAA,MAAA,EAyCI,eAzCJ,CAAA;EAA4B,SAAA,oBAAA,EAAA,CAAA,EAAA,EAAA,MAAA,EAAA,GA4C9C,MAAA,CAAO,MA5CuC,CAAA,IAAA,EA4C1B,eA5C0B,CAAA;CAA9C,CAAA;AAIM,cAzBF,eAAA,SAAwB,oBAAA,CAyBtB;;;iBCmBC,2BAAA,SAAoC,gBAAa,MAAA,CAAA,OAAA,UAAA,oBAAA;AF/CrD,cEigCC,aF3+BK,EAAA,CAAA,OAAA,EE2+BqB,aF3+BP,EAAA,GE2+BoB,MAAA,CAAA,MF3+BpB,CE2+BoB,SF3+BpB,CE2+BoB,UF3+BpB,CAAA,EAAA,KAAA,EE2+BoB,iBF3+BpB,CAAA;AAUpB,cE8+BC,OF9+BiB,EAAA,CAAA,MAAA,EE8+BE,aF9+BF,EAAA,GE8+Be,OF9+Bf,CE8+Be,SF9+Bf,CE8+Be,UF9+Bf,CAAA,CAAA"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,86 @@
|
|
|
1
|
-
|
|
1
|
+
import { UploadistaError } from "@uploadista/core/errors";
|
|
2
|
+
import { DataStore, KvStore, UploadFile, UploadFileKVStore } from "@uploadista/core/types";
|
|
3
|
+
import { Context, Effect, Layer } from "effect";
|
|
4
|
+
import AWS, { S3ClientConfig } from "@aws-sdk/client-s3";
|
|
5
|
+
|
|
6
|
+
//#region src/types.d.ts
|
|
7
|
+
type S3StoreOptions = {
|
|
8
|
+
deliveryUrl: string;
|
|
9
|
+
/**
|
|
10
|
+
* The preferred part size for parts send to S3. Can not be lower than 5MiB or more than 5GiB.
|
|
11
|
+
* The server calculates the optimal part size, which takes this size into account,
|
|
12
|
+
* but may increase it to not exceed the S3 10K parts limit.
|
|
13
|
+
*/
|
|
14
|
+
partSize?: number;
|
|
15
|
+
/**
|
|
16
|
+
* The minimal part size for parts.
|
|
17
|
+
* Can be used to ensure that all non-trailing parts are exactly the same size.
|
|
18
|
+
* Can not be lower than 5MiB or more than 5GiB.
|
|
19
|
+
*/
|
|
20
|
+
minPartSize?: number;
|
|
21
|
+
/**
|
|
22
|
+
* The maximum number of parts allowed in a multipart upload. Defaults to 10,000.
|
|
23
|
+
*/
|
|
24
|
+
maxMultipartParts?: number;
|
|
25
|
+
useTags?: boolean;
|
|
26
|
+
maxConcurrentPartUploads?: number;
|
|
27
|
+
expirationPeriodInMilliseconds?: number;
|
|
28
|
+
s3ClientConfig: S3ClientConfig & {
|
|
29
|
+
bucket: string;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
type S3OperationContext = {
|
|
33
|
+
uploadId: string;
|
|
34
|
+
bucket: string;
|
|
35
|
+
key: string;
|
|
36
|
+
partNumber?: number;
|
|
37
|
+
partSize?: number;
|
|
38
|
+
contentType?: string;
|
|
39
|
+
cacheControl?: string;
|
|
40
|
+
};
|
|
41
|
+
type MultipartUploadInfo = {
|
|
42
|
+
uploadId: string;
|
|
43
|
+
bucket: string;
|
|
44
|
+
key: string;
|
|
45
|
+
};
|
|
46
|
+
type S3StoreConfig = S3StoreOptions & {
|
|
47
|
+
kvStore: KvStore<UploadFile>;
|
|
48
|
+
};
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region src/services/s3-client.service.d.ts
|
|
51
|
+
declare const S3ClientService_base: Context.TagClass<S3ClientService, "S3ClientService", {
|
|
52
|
+
readonly bucket: string;
|
|
53
|
+
readonly getObject: (key: string) => Effect.Effect<ReadableStream, UploadistaError>;
|
|
54
|
+
readonly headObject: (key: string) => Effect.Effect<number | undefined, UploadistaError>;
|
|
55
|
+
readonly putObject: (key: string, body: Uint8Array) => Effect.Effect<string, UploadistaError>;
|
|
56
|
+
readonly deleteObject: (key: string) => Effect.Effect<void, UploadistaError>;
|
|
57
|
+
readonly deleteObjects: (keys: string[]) => Effect.Effect<AWS.DeleteObjectsCommandOutput, UploadistaError>;
|
|
58
|
+
readonly createMultipartUpload: (context: S3OperationContext) => Effect.Effect<MultipartUploadInfo, UploadistaError>;
|
|
59
|
+
readonly uploadPart: (context: S3OperationContext & {
|
|
60
|
+
partNumber: number;
|
|
61
|
+
data: Uint8Array;
|
|
62
|
+
}) => Effect.Effect<string, UploadistaError>;
|
|
63
|
+
readonly completeMultipartUpload: (context: S3OperationContext, parts: Array<AWS.Part>) => Effect.Effect<string | undefined, UploadistaError>;
|
|
64
|
+
readonly abortMultipartUpload: (context: S3OperationContext) => Effect.Effect<void, UploadistaError>;
|
|
65
|
+
readonly listParts: (context: S3OperationContext & {
|
|
66
|
+
partNumberMarker?: string;
|
|
67
|
+
}) => Effect.Effect<{
|
|
68
|
+
parts: AWS.Part[];
|
|
69
|
+
isTruncated: boolean;
|
|
70
|
+
nextPartNumberMarker?: string;
|
|
71
|
+
}, UploadistaError>;
|
|
72
|
+
readonly listMultipartUploads: (keyMarker?: string, uploadIdMarker?: string) => Effect.Effect<AWS.ListMultipartUploadsCommandOutput, UploadistaError>;
|
|
73
|
+
readonly getIncompletePart: (id: string) => Effect.Effect<ReadableStream | undefined, UploadistaError>;
|
|
74
|
+
readonly getIncompletePartSize: (id: string) => Effect.Effect<number | undefined, UploadistaError>;
|
|
75
|
+
readonly putIncompletePart: (id: string, data: Uint8Array) => Effect.Effect<string, UploadistaError>;
|
|
76
|
+
readonly deleteIncompletePart: (id: string) => Effect.Effect<void, UploadistaError>;
|
|
77
|
+
}>;
|
|
78
|
+
declare class S3ClientService extends S3ClientService_base {}
|
|
79
|
+
//#endregion
|
|
80
|
+
//#region src/s3-store.d.ts
|
|
81
|
+
declare function createS3StoreImplementation(config: S3StoreConfig): Effect.Effect<DataStore<UploadFile>, never, S3ClientService>;
|
|
82
|
+
declare const createS3Store: (options: S3StoreConfig) => Effect.Effect<DataStore<UploadFile>, never, UploadFileKVStore>;
|
|
83
|
+
declare const s3Store: (config: S3StoreConfig) => Promise<DataStore<UploadFile>>;
|
|
84
|
+
//#endregion
|
|
85
|
+
export { createS3Store, createS3StoreImplementation, s3Store };
|
|
2
86
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/types.ts","../src/services/s3-client.service.ts","../src/s3-store.ts"],"sourcesContent":[],"mappings":";;;;;;KAWY,cAAA;;EAAA;AAgCZ;AAeA;AAmCA;;EACmB,QAAA,CAAA,EAAA,MAAA;EAAR;;;;;ECxF6D,WAAA,CAAA,EAAA,MAAA;;;;EAgB/D,iBAAO,CAAA,EAAA,MAAA;EAG2B,OAAA,CAAA,EAAA,OAAA;EAAlC,wBAAO,CAAA,EAAA,MAAA;EAGJ,8BAAA,CAAA,EAAA,MAAA;EACmB,cAAA,EDIb,cCJa,GAAA;IAAf,MAAA,EAAA,MAAA;EAGa,CAAA;CAApB;AAQA,KDGG,kBAAA,GCHI;EAED,QAAA,EAAA,MAAA;EAAiD,MAAA,EAAA,MAAA;EACjC,GAAA,EAAA,MAAA;EAAtB,UAAO,CAAA,EAAA,MAAA;EAED,QAAA,CAAA,EAAA,MAAA;EACI,WAAI,CAAA,EAAA,MAAA;EAAV,YAAA,CAAA,EAAA,MAAA;CAC8B;AAK5B,KDMH,mBAAA,GCNG;EAGA,QAAI,EAAA,MAAA;EAIb,MAAA,EAAA,MAAA;EANG,GAAA,EAAO,MAAA;CAWO;ACN0C,KFmCrD,aAAA,GAAgB,cEnCqC,GAAA;EAk9BpD,OAAA,EF96BF,OEw7BP,CFx7Be,UEw7Bf,CAAA;CAVmC;;;cDtgCiC;EDK5D,SAAA,MAAA,EAAc,MAAA;EAgCd,SAAA,SAAA,EAAA,CAAkB,GAAA,EAAA,M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.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export * from "./s3-store";
|
|
1
|
+
import{UploadistaError as e}from"@uploadista/core/errors";import{UploadFileKVStore as t}from"@uploadista/core/types";import{logS3UploadCompletion as n,s3ActiveUploadsGauge as r,s3FileSizeHistogram as i,s3PartSizeHistogram as a,s3PartUploadDurationHistogram as o,s3UploadDurationHistogram as s,s3UploadErrorsTotal as c,s3UploadPartsTotal as l,s3UploadRequestsTotal as u,s3UploadSuccessTotal as d,trackS3Error as f,withS3ApiMetrics as p,withS3TimingMetrics as m,withS3UploadMetrics as h}from"@uploadista/observability";import{Context as g,Effect as _,Layer as v,Ref as y,Schedule as b,Stream as x}from"effect";import{NoSuchKey as S,NotFound as C,S3 as w}from"@aws-sdk/client-s3";const T=e=>e&&e.length>0?e.reduce((e,t)=>e+(t?.Size??0),0):0,E=(e,t,n,r,i=5497558138880)=>{let a=e??i,o;o=a<=t?a:a<=t*r?t:Math.ceil(a/r);let s=e&&e<n?o:Math.max(o,n),c=1024;return Math.ceil(s/c)*c},D=e=>`${e}.part`,O=(e,t)=>{let n=new Date(e);return new Date(n.getTime()+t)},k=(t,n,r={})=>(_.runSync(f(t,n,r)),e.fromCode(`FILE_WRITE_ERROR`,n)),A=(t,n,r={})=>typeof n==`object`&&n&&`code`in n&&typeof n.code==`string`&&[`NotFound`,`NoSuchKey`,`NoSuchUpload`].includes(n.code)?(_.runSync(_.logWarning(`File not found during ${t} operation`).pipe(_.annotateLogs({error_code:n.code,...r}))),e.fromCode(`FILE_NOT_FOUND`)):k(t,n,r),ee=e=>typeof e==`object`&&!!e&&`code`in e&&typeof e.code==`string`&&(e.code===`NoSuchUpload`||e.code===`NoSuchKey`);function j(e){if(e instanceof ReadableStream||e&&typeof e==`object`&&`getReader`in e)return e;if(e&&typeof e==`object`&&`pipe`in e&&`on`in e){let t=e;return new ReadableStream({start(e){t.on(`data`,t=>{e.enqueue(new Uint8Array(t))}),t.on(`end`,()=>{e.close()}),t.on(`error`,t=>{e.error(t)})}})}throw Error(`Unsupported body type: ${typeof e}. Expected ReadableStream or Node.js Readable.`)}var M=class extends g.Tag(`S3ClientService`)(){};const N=(e,t)=>{let n=new w(e),r=e=>_.tryPromise({try:async()=>j((await n.getObject({Bucket:t,Key:e})).Body),catch:n=>k(`getObject`,n,{key:e,bucket:t})}),i=e=>_.tryPromise({try:async()=>{try{return(await n.headObject({Bucket:t,Key:e})).ContentLength}catch(e){if(e instanceof C)return;throw e}},catch:n=>k(`headObject`,n,{key:e,bucket:t})}),a=(e,r)=>_.tryPromise({try:async()=>(await n.putObject({Bucket:t,Key:e,Body:r})).ETag||``,catch:n=>k(`putObject`,n,{key:e,bucket:t,size:r.length})}),o=e=>_.tryPromise({try:async()=>{await n.deleteObject({Bucket:t,Key:e})},catch:n=>k(`deleteObject`,n,{key:e,bucket:t})});return{bucket:t,getObject:r,headObject:i,putObject:a,deleteObject:o,deleteObjects:e=>_.tryPromise({try:()=>n.deleteObjects({Bucket:t,Delete:{Objects:e.map(e=>({Key:e}))}}),catch:n=>k(`deleteObjects`,n,{keys:e.length,bucket:t})}),createMultipartUpload:e=>p(`createMultipartUpload`,_.tryPromise({try:async()=>{let t={Bucket:e.bucket,Key:e.key};e.contentType&&(t.ContentType=e.contentType),e.cacheControl&&(t.CacheControl=e.cacheControl);let r=await n.createMultipartUpload(t);if(!r.UploadId)throw Error(`Upload ID is undefined`);if(!r.Key)throw Error(`Key is undefined`);return{uploadId:r.UploadId,bucket:e.bucket,key:r.Key}},catch:t=>k(`createMultipartUpload`,t,e)})),uploadPart:e=>p(`uploadPart`,_.tryPromise({try:()=>n.uploadPart({Bucket:e.bucket,Key:e.key,UploadId:e.uploadId,PartNumber:e.partNumber,Body:e.data}),catch:t=>k(`uploadPart`,t,{upload_id:e.key,part_number:e.partNumber,part_size:e.data.length,s3_bucket:e.bucket})}).pipe(_.map(e=>e.ETag))),completeMultipartUpload:(e,t)=>p(`completeMultipartUpload`,_.tryPromise({try:()=>n.completeMultipartUpload({Bucket:e.bucket,Key:e.key,UploadId:e.uploadId,MultipartUpload:{Parts:t.map(e=>({ETag:e.ETag,PartNumber:e.PartNumber}))}}).then(e=>e.Location),catch:n=>k(`completeMultipartUpload`,n,{upload_id:e.key,parts_count:t.length,s3_bucket:e.bucket})})),abortMultipartUpload:e=>_.tryPromise({try:async()=>{await n.abortMultipartUpload({Bucket:e.bucket,Key:e.key,UploadId:e.uploadId})},catch:t=>A(`abortMultipartUpload`,t,{upload_id:e.key,s3_bucket:e.bucket})}),listParts:e=>_.tryPromise({try:async()=>{let t={Bucket:e.bucket,Key:e.key,UploadId:e.uploadId,PartNumberMarker:e.partNumberMarker},r=await n.listParts(t);return{parts:r.Parts??[],isTruncated:r.IsTruncated??!1,nextPartNumberMarker:r.NextPartNumberMarker}},catch:t=>k(`listParts`,t,{upload_id:e.key,s3_bucket:e.bucket})}),listMultipartUploads:(e,r)=>_.tryPromise({try:()=>n.listMultipartUploads({Bucket:t,KeyMarker:e,UploadIdMarker:r}),catch:e=>k(`listMultipartUploads`,e,{bucket:t})}),getIncompletePart:e=>_.tryPromise({try:async()=>{try{return j((await n.getObject({Bucket:t,Key:D(e)})).Body)}catch(e){if(e instanceof S)return;throw e}},catch:n=>k(`getIncompletePart`,n,{upload_id:e,bucket:t})}),getIncompletePartSize:e=>i(D(e)),putIncompletePart:(e,t)=>a(D(e),t).pipe(_.tap(()=>_.logInfo(`Incomplete part uploaded`).pipe(_.annotateLogs({upload_id:e})))),deleteIncompletePart:e=>o(D(e))}},P=(e,t)=>v.succeed(M,N(e,t)),F=e=>{let{id:t,metadata:n}=e;if(!n)return t;let r=n.filename||n.fileName||n.name;return typeof r==`string`&&r.includes(`.`)?`${t}${r.substring(r.lastIndexOf(`.`))}`:t};function I(t){let{deliveryUrl:f,partSize:p,minPartSize:g=5242880,useTags:v=!0,maxMultipartParts:S=1e4,kvStore:C,maxConcurrentPartUploads:w=60,expirationPeriodInMilliseconds:D=1e3*60*60*24*7,s3ClientConfig:{bucket:k}}=t;return _.gen(function*(){let t=yield*M,A=p||8*1024*1024,j=t=>{let n=t.storage.uploadId;return n?_.succeed(n):_.fail(e.fromCode(`FILE_WRITE_ERROR`,Error(`Upload ID is undefined`)))},N=(e,n,r)=>{let i=F(e);return m(o,_.gen(function*(){let a=yield*j(e),o=yield*t.uploadPart({bucket:t.bucket,key:i,uploadId:a,partNumber:r,data:n}).pipe(_.retry(b.exponential(`1 second`,2).pipe(b.intersect(b.recurs(3)))),_.tapError(i=>_.logWarning(`Retrying part upload`).pipe(_.annotateLogs({upload_id:e.id,part_number:r,error_message:i.message,retry_attempt:`unknown`,part_size:n.length,s3_bucket:t.bucket}))));return yield*l(_.succeed(1)),yield*_.logInfo(`Part uploaded successfully`).pipe(_.annotateLogs({upload_id:e.id,part_number:r,part_size:n.length,etag:o})),o})).pipe(_.withSpan(`s3-upload-part-${r}`,{attributes:{"upload.id":e.id,"upload.part_number":r,"upload.part_size":n.length,"s3.bucket":t.bucket,"s3.key":i}}))},P=(e,n)=>t.putIncompletePart(e,n),I=e=>_.gen(function*(){let n=yield*t.getIncompletePart(e);if(!n)return;let r=n.getReader(),i=[],a=0;try{for(;;){let{done:e,value:t}=yield*_.promise(()=>r.read());if(e)break;i.push(t),a+=t.length}}finally{r.releaseLock()}let o=x.fromIterable(i);return{size:a,stream:o}}),L=e=>t.deleteIncompletePart(e),R=e=>t.getIncompletePartSize(e),z=(e,n)=>{let r=F(e);return _.gen(function*(){let i=yield*j(e);return yield*t.completeMultipartUpload({bucket:t.bucket,key:r,uploadId:i},n)}).pipe(_.tap(()=>d(_.succeed(1))),_.withSpan(`s3-complete-multipart-upload`,{attributes:{"upload.id":e.id,"upload.parts_count":n.length,"s3.bucket":t.bucket,"s3.key":r}}))},B=e=>{let n=F(e);return _.gen(function*(){let r=yield*j(e);yield*t.abortMultipartUpload({bucket:t.bucket,key:n,uploadId:r}),yield*t.deleteObjects([n])})},V=(e,n,r,i)=>_.gen(function*(){try{let a=yield*t.listParts({bucket:t.bucket,key:e,uploadId:n,partNumberMarker:i}),o=a.parts;if(a.isTruncated){let t=yield*V(e,n,r,a.nextPartNumberMarker);o=[...o,...t.parts]}return i||o.sort((e,t)=>(e.PartNumber??0)-(t.PartNumber??0)),{uploadFound:!0,parts:o}}catch(e){if(ee(e))return yield*_.logWarning(`S3 upload not found during listParts`).pipe(_.annotateLogs({upload_id:r,error_code:e.code})),{uploadFound:!1,parts:[]};throw e}}),H=(e,t)=>_.gen(function*(){let n=yield*C.get(e),r=yield*j(n);return yield*V(F(n),r,e,t)}),U=(e,t)=>_.gen(function*(){if(!t)return 0;let n=yield*C.get(e.id),r=n.storage.uploadId;return r&&(yield*C.set(e.id,{...n,storage:{...n.storage,uploadId:r}})),0}),W=e=>_.gen(function*(){yield*_.logInfo(`Clearing cache`).pipe(_.annotateLogs({upload_id:e})),yield*C.delete(e)}),G=e=>{let n=F(e);return _.gen(function*(){yield*_.logInfo(`Initializing multipart upload`).pipe(_.annotateLogs({upload_id:e.id}));let r=yield*t.createMultipartUpload({bucket:t.bucket,key:n,uploadId:``,contentType:e.metadata?.contentType?.toString(),cacheControl:e.metadata?.cacheControl?.toString()}),a={...e,storage:{...e.storage,path:r.key,uploadId:r.uploadId,bucket:r.bucket},url:`${f}/${n}`};return yield*C.set(e.id,a),yield*_.logInfo(`Multipart upload created`).pipe(_.annotateLogs({upload_id:e.id,s3_upload_id:a.storage.uploadId,s3_key:n})),yield*u(_.succeed(1)),yield*i(_.succeed(e.size||0)),a}).pipe(_.withSpan(`s3-create-upload`,{attributes:{"upload.id":e.id,"upload.size":e.size||0,"s3.bucket":t.bucket,"s3.key":n}}))},K=e=>_.gen(function*(){yield*_.logInfo(`Initializing multipart upload`).pipe(_.annotateLogs({upload_id:e.id}));let t=yield*G(e);return yield*C.set(e.id,t),yield*_.logInfo(`Multipart upload created`).pipe(_.annotateLogs({upload_id:e.id,s3_upload_id:t.storage.uploadId})),yield*u(_.succeed(1)),t}).pipe(_.withSpan(`s3-create-upload`,{attributes:{"upload.id":e.id,"upload.size":e.size||0,"s3.bucket":k}})),q=e=>_.gen(function*(){yield*B(yield*C.get(e)),yield*W(e)}),J=(e,t)=>h(e.file_id,m(s,_.gen(function*(){let{stream:n,file_id:i,offset:a}=e,{onProgress:o}=t,s=Date.now();yield*r(_.succeed(1));let{uploadFile:c,nextPartNumber:l,offset:u,data:d,existingPartSize:f}=yield*X(i,a,n),p=f||E(c.size,A,g,S);yield*_.logInfo(`Part size decision`).pipe(_.annotateLogs({upload_id:i,existing_part_size:f,calculated_part_size:E(c.size,A,g,S),final_part_size:p,next_part_number:l}));let m=u+(yield*re(c,d,l,u,p,g,w,o));return m>w&&c.size===m&&(yield*Z(i,c,s)),m}).pipe(_.ensuring(r(_.succeed(0)))))),Y=e=>_.gen(function*(){let t=yield*C.get(e),{parts:n,uploadFound:r}=yield*H(e);if(!r)return{...t,offset:t.size,size:t.size};let i=T(n),a=yield*R(e);return{...t,offset:i+(a??0),size:t.size,storage:t.storage}}),X=(e,t,n)=>_.gen(function*(){let r=yield*C.get(e),{parts:i}=yield*H(e),a=(i.length>0&&i[i.length-1].PartNumber?i[i.length-1].PartNumber??0:0)+1,o=i.length>0&&i[0].Size?i[0].Size:null;if(o&&i.length>1){let t=i.slice(0,-1).find(e=>e.Size!==o);t&&(yield*_.logWarning(`Inconsistent part sizes detected in existing upload`).pipe(_.annotateLogs({upload_id:e,expected_size:o,inconsistent_part:t.PartNumber,inconsistent_size:t.Size})))}let s=yield*I(e);if(s){yield*L(e);let i=t-s.size,c=s.stream.pipe(x.concat(n));return{uploadFile:r,nextPartNumber:a,offset:i,incompletePartSize:s.size,data:c,existingPartSize:o}}else return{uploadFile:r,nextPartNumber:a,offset:t,incompletePartSize:0,data:n,existingPartSize:o}}),Z=(e,t,r)=>_.gen(function*(){let{parts:i}=yield*H(e);yield*_.logInfo(`Attempting to complete multipart upload`).pipe(_.annotateLogs({upload_id:e,parts_count:i.length,parts_info:i.map((e,t)=>({part_number:e.PartNumber,size:e.Size,etag:e.ETag,is_final_part:t===i.length-1}))})),yield*z(t,i),yield*U(t,v);let a=Date.now()-r,o=t.size||0,s=a>0?o*1e3/a:0,c=i.length>0?i.reduce((e,t)=>e+(t.Size||0),0)/i.length:void 0;yield*n(e,{fileSize:o,totalDurationMs:a,partsCount:i.length,averagePartSize:c,throughputBps:s})}).pipe(_.tapError(t=>_.gen(function*(){yield*c(_.succeed(1)),yield*_.logError(`Failed to finish upload`).pipe(_.annotateLogs({upload_id:e,error:String(t)}))}))),Q=_.gen(function*(){if(D===0)return 0;let e,n,r=!0,i=0;for(;r;){let a=yield*t.listMultipartUploads(e,n),o=a.Uploads?.filter(e=>{let t=e.Initiated;return t&&Date.now()>O(t.toISOString(),D).getTime()})||[],s=o.filter(e=>!!e.Key).map(e=>e.Key);s.length>0&&(yield*t.deleteObjects(s),yield*_.forEach(o,e=>_.gen(function*(){!e.Key||!e.UploadId||(yield*t.abortMultipartUpload({bucket:k,key:e.Key,uploadId:e.UploadId}))})),i+=s.length),r=a.IsTruncated??!1,r&&(e=a.NextKeyMarker,n=a.NextUploadIdMarker)}return i}),te=e=>t=>x.async(n=>{let r=new Uint8Array,i=1,a=0,o=(t,r=!1)=>{_.runSync(_.logInfo(`Creating chunk`).pipe(_.annotateLogs({part_number:i,chunk_size:t.length,expected_size:e,is_final_chunk:r,total_bytes_processed:a+t.length}))),n.single({partNumber:i++,data:t,size:t.length})},s=t=>{let n=new Uint8Array(r.length+t.length);for(n.set(r),n.set(t,r.length),r=n,a+=t.length;r.length>=e;){let t=r.slice(0,e);r=r.slice(e),o(t,!1)}};_.runFork(t.pipe(x.runForEach(e=>_.sync(()=>s(e))),_.andThen(()=>_.sync(()=>{r.length>0&&o(r,!0),n.end()})),_.catchAll(e=>_.sync(()=>n.fail(e)))))}),ne=(e,t=0)=>n=>e?_.gen(function*(){let r=yield*y.make(t);return n.pipe(x.tap(t=>_.gen(function*(){e(yield*y.updateAndGet(r,e=>e+t.length))})))}).pipe(x.unwrap):n,re=(t,n,r,i,o,s,c,l)=>_.gen(function*(){yield*_.logInfo(`Starting part uploads`).pipe(_.annotateLogs({upload_id:t.id,init_offset:i,file_size:t.size,part_size:o,min_part_size:s}));let u=n.pipe(ne(l,i),te(o)),d=yield*y.make(i),f=yield*y.make(0),p=n=>_.gen(function*(){let i=yield*y.updateAndGet(d,e=>e+n.size),c=i>=(t.size||0);yield*_.logDebug(`Processing chunk`).pipe(_.annotateLogs({upload_id:t.id,cumulative_offset:i,file_size:t.size,chunk_size:n.size,is_final_part:c}));let l=r+n.partNumber-1;n.size>o&&(yield*_.fail(e.fromCode(`FILE_WRITE_ERROR`,Error(`Part size ${n.size} exceeds upload part size ${o}`)))),n.size>=s||c?(yield*_.logDebug(`Uploading multipart chunk`).pipe(_.annotateLogs({upload_id:t.id,part_number:l,chunk_size:n.size,min_part_size:s,is_final_part:c})),yield*N(t,n.data,l),yield*a(_.succeed(n.size))):yield*P(t.id,n.data),yield*y.update(f,e=>e+n.size)});return yield*u.pipe(x.runForEach(e=>p(e)),_.withConcurrency(c)),yield*y.get(f)}),$=()=>({supportsParallelUploads:!0,supportsConcatenation:!0,supportsDeferredLength:!0,supportsResumableUploads:!0,supportsTransactionalUploads:!0,maxConcurrentUploads:w,minChunkSize:g,maxChunkSize:5368709120,maxParts:S,optimalChunkSize:A,requiresOrderedChunks:!1,requiresMimeTypeValidation:!0,maxValidationSize:void 0}),ie=()=>({minChunkSize:g,maxChunkSize:5368709120,optimalChunkSize:A,requiresOrderedChunks:!1}),ae=e=>{let t=$(),n=(()=>{switch(e){case`parallel`:return t.supportsParallelUploads;case`single`:return!0;default:return!1}})();return _.succeed(n)},oe=e=>{let t=new Uint8Array(e.reduce((e,t)=>e+t.length,0)),n=0;for(let r of e)t.set(r,n),n+=r.length;return t},se=async e=>{let t=e.getReader(),n=[];for(;;){let{done:e,value:r}=await t.read();if(e)break;n.push(r)}return oe(n)};return{bucket:k,create:K,remove:q,write:J,getUpload:Y,read:n=>_.gen(function*(){let r=yield*C.get(n);if(console.log(r),!r.id)return yield*_.fail(e.fromCode(`FILE_READ_ERROR`,Error(`Upload Key is undefined`)));let i=F(r),a=yield*t.getObject(i);return yield*_.promise(()=>se(a))}),deleteExpired:Q,getCapabilities:$,getChunkerConstraints:ie,validateUploadStrategy:ae}})}const L=e=>_.gen(function*(){let n=yield*t,{s3ClientConfig:{bucket:r,...i}}=e;return yield*I({...e,kvStore:n}).pipe(_.provide(P(i,r)))}),R=e=>{let{s3ClientConfig:{bucket:t,...n}}=e;return _.runPromise(I(e).pipe(_.provide(P(n,t))))};export{L as createS3Store,I as createS3StoreImplementation,R as s3Store};
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["optimalPartSize: number","logS3Error","request: AWS.CreateMultipartUploadCommandInput","params: AWS.ListPartsCommandInput","withTimingMetrics","partUploadDurationHistogram","uploadPartsTotal","chunks: Uint8Array[]","uploadSuccessTotal","useTags","uploadRequestsTotal","fileSizeHistogram","withUploadMetrics","uploadDurationHistogram","activeUploadsGauge","uploadErrorsTotal","keyMarker: string | undefined","uploadIdMarker: string | undefined","minPartSize","partSizeHistogram","maxConcurrentPartUploads"],"sources":["../src/utils/calculations.ts","../src/utils/error-handling.ts","../src/utils/stream-adapter.ts","../src/services/s3-client.service.ts","../src/s3-store.ts"],"sourcesContent":["import type AWS from \"@aws-sdk/client-s3\";\n\nexport const calcOffsetFromParts = (parts?: Array<AWS.Part>): number => {\n return parts && parts.length > 0\n ? parts.reduce((a, b) => a + (b?.Size ?? 0), 0)\n : 0;\n};\n\nexport const calcOptimalPartSize = (\n initSize: number | undefined,\n preferredPartSize: number,\n minPartSize: number,\n maxMultipartParts: number,\n maxUploadSize = 5_497_558_138_880, // 5TiB\n): number => {\n const size = initSize ?? maxUploadSize;\n let optimalPartSize: number;\n\n if (size <= preferredPartSize) {\n // For files smaller than preferred part size, use the file size\n // but ensure it meets S3's minimum requirements for multipart uploads\n optimalPartSize = size;\n } else if (size <= preferredPartSize * maxMultipartParts) {\n // File fits within max parts limit using preferred part size\n optimalPartSize = preferredPartSize;\n } else {\n // File is too large for preferred part size, calculate minimum needed\n optimalPartSize = Math.ceil(size / maxMultipartParts);\n }\n\n // Ensure we respect minimum part size for multipart uploads\n // Exception: if the file is smaller than minPartSize, use the file size directly\n const finalPartSize =\n initSize && initSize < minPartSize\n ? optimalPartSize // Single part upload for small files\n : Math.max(optimalPartSize, minPartSize); // Enforce minimum for multipart\n\n // Round up to ensure consistent part sizes and align to reasonable boundaries\n // This helps ensure all parts except the last one will have exactly the same size\n const alignment = 1024; // 1KB alignment for better consistency\n return Math.ceil(finalPartSize / alignment) * alignment;\n};\n\nexport const partKey = (id: string): string => {\n return `${id}.part`;\n};\n\nexport const shouldUseExpirationTags = (\n expirationPeriodInMilliseconds: number,\n useTags: boolean,\n): boolean => {\n return expirationPeriodInMilliseconds !== 0 && useTags;\n};\n\nexport const getExpirationDate = (\n createdAt: string,\n expirationPeriodInMilliseconds: number,\n): Date => {\n const date = new Date(createdAt);\n return new Date(date.getTime() + expirationPeriodInMilliseconds);\n};\n","import { UploadistaError } from \"@uploadista/core/errors\";\nimport { trackS3Error as logS3Error } from \"@uploadista/observability\";\nimport { Effect } from \"effect\";\n\nexport const handleS3Error = (\n operation: string,\n error: unknown,\n context: Record<string, unknown> = {},\n): UploadistaError => {\n // Log the error with context\n Effect.runSync(logS3Error(operation, error, context));\n\n return UploadistaError.fromCode(\"FILE_WRITE_ERROR\", error as Error);\n};\n\nexport const handleS3NotFoundError = (\n operation: string,\n error: unknown,\n context: Record<string, unknown> = {},\n): UploadistaError => {\n if (\n typeof error === \"object\" &&\n error !== null &&\n \"code\" in error &&\n typeof error.code === \"string\" &&\n [\"NotFound\", \"NoSuchKey\", \"NoSuchUpload\"].includes(error.code)\n ) {\n Effect.runSync(\n Effect.logWarning(`File not found during ${operation} operation`).pipe(\n Effect.annotateLogs({\n error_code: error.code,\n ...context,\n }),\n ),\n );\n return UploadistaError.fromCode(\"FILE_NOT_FOUND\");\n }\n\n return handleS3Error(operation, error, context);\n};\n\nexport const isUploadNotFoundError = (\n error: unknown,\n): error is { code: \"NoSuchUpload\" | \"NoSuchKey\" } => {\n return (\n typeof error === \"object\" &&\n error !== null &&\n \"code\" in error &&\n typeof error.code === \"string\" &&\n (error.code === \"NoSuchUpload\" || error.code === \"NoSuchKey\")\n );\n};\n","/**\n * Stream adapter utility to handle AWS SDK Body responses across different environments.\n *\n * In Node.js environments, AWS SDK returns Node.js Readable streams.\n * In Cloudflare Workers, it returns Web Streams API ReadableStreams.\n * This utility normalizes both to Web Streams API ReadableStreams.\n */\n\n/**\n * Converts various stream types to a Web Streams API ReadableStream\n * @param body The body from AWS SDK response (could be Node.js Readable or Web ReadableStream)\n * @returns A Web Streams API ReadableStream\n */\nexport function toReadableStream(body: unknown): ReadableStream {\n // If it's already a Web ReadableStream, return as-is\n if (body instanceof ReadableStream) {\n return body;\n }\n\n // If it has a getReader method, it's likely already a ReadableStream\n if (body && typeof body === \"object\" && \"getReader\" in body) {\n return body as ReadableStream;\n }\n\n // Check if it's a Node.js Readable stream\n if (body && typeof body === \"object\" && \"pipe\" in body && \"on\" in body) {\n const nodeStream = body as NodeJS.ReadableStream;\n\n return new ReadableStream({\n start(controller) {\n nodeStream.on(\"data\", (chunk) => {\n controller.enqueue(new Uint8Array(chunk));\n });\n\n nodeStream.on(\"end\", () => {\n controller.close();\n });\n\n nodeStream.on(\"error\", (error) => {\n controller.error(error);\n });\n },\n });\n }\n\n // If it's some other type, try to handle it gracefully\n throw new Error(\n `Unsupported body type: ${typeof body}. Expected ReadableStream or Node.js Readable.`,\n );\n}\n","import type AWS from \"@aws-sdk/client-s3\";\nimport type { S3ClientConfig } from \"@aws-sdk/client-s3\";\nimport { NoSuchKey, NotFound, S3 } from \"@aws-sdk/client-s3\";\nimport type { UploadistaError } from \"@uploadista/core/errors\";\nimport { withS3ApiMetrics } from \"@uploadista/observability\";\nimport { Context, Effect, Layer } from \"effect\";\nimport type { MultipartUploadInfo, S3OperationContext } from \"../types\";\nimport {\n handleS3Error,\n handleS3NotFoundError,\n partKey,\n toReadableStream,\n} from \"../utils\";\n\nexport class S3ClientService extends Context.Tag(\"S3ClientService\")<\n S3ClientService,\n {\n readonly bucket: string;\n\n // Basic S3 operations\n readonly getObject: (\n key: string,\n ) => Effect.Effect<ReadableStream, UploadistaError>;\n readonly headObject: (\n key: string,\n ) => Effect.Effect<number | undefined, UploadistaError>;\n readonly putObject: (\n key: string,\n body: Uint8Array,\n ) => Effect.Effect<string, UploadistaError>;\n readonly deleteObject: (\n key: string,\n ) => Effect.Effect<void, UploadistaError>;\n readonly deleteObjects: (\n keys: string[],\n ) => Effect.Effect<AWS.DeleteObjectsCommandOutput, UploadistaError>;\n\n // Multipart upload operations\n readonly createMultipartUpload: (\n context: S3OperationContext,\n ) => Effect.Effect<MultipartUploadInfo, UploadistaError>;\n readonly uploadPart: (\n context: S3OperationContext & { partNumber: number; data: Uint8Array },\n ) => Effect.Effect<string, UploadistaError>;\n readonly completeMultipartUpload: (\n context: S3OperationContext,\n parts: Array<AWS.Part>,\n ) => Effect.Effect<string | undefined, UploadistaError>;\n readonly abortMultipartUpload: (\n context: S3OperationContext,\n ) => Effect.Effect<void, UploadistaError>;\n readonly listParts: (\n context: S3OperationContext & { partNumberMarker?: string },\n ) => Effect.Effect<\n {\n parts: AWS.Part[];\n isTruncated: boolean;\n nextPartNumberMarker?: string;\n },\n UploadistaError\n >;\n readonly listMultipartUploads: (\n keyMarker?: string,\n uploadIdMarker?: string,\n ) => Effect.Effect<AWS.ListMultipartUploadsCommandOutput, UploadistaError>;\n\n // Incomplete part operations\n readonly getIncompletePart: (\n id: string,\n ) => Effect.Effect<ReadableStream | undefined, UploadistaError>;\n readonly getIncompletePartSize: (\n id: string,\n ) => Effect.Effect<number | undefined, UploadistaError>;\n readonly putIncompletePart: (\n id: string,\n data: Uint8Array,\n ) => Effect.Effect<string, UploadistaError>;\n readonly deleteIncompletePart: (\n id: string,\n ) => Effect.Effect<void, UploadistaError>;\n }\n>() {}\n\nexport const makeS3ClientService = (\n s3ClientConfig: S3ClientConfig,\n bucket: string,\n) => {\n const s3Client = new S3(s3ClientConfig);\n const getObject = (key: string) =>\n Effect.tryPromise({\n try: async () => {\n const data = await s3Client.getObject({\n Bucket: bucket,\n Key: key,\n });\n return toReadableStream(data.Body);\n },\n catch: (error) => handleS3Error(\"getObject\", error, { key, bucket }),\n });\n\n const headObject = (key: string) =>\n Effect.tryPromise({\n try: async () => {\n try {\n const data = await s3Client.headObject({\n Bucket: bucket,\n Key: key,\n });\n return data.ContentLength;\n } catch (error) {\n if (error instanceof NotFound) {\n return undefined;\n }\n throw error;\n }\n },\n catch: (error) => handleS3Error(\"headObject\", error, { key, bucket }),\n });\n\n const putObject = (key: string, body: Uint8Array) =>\n Effect.tryPromise({\n try: async () => {\n const response = await s3Client.putObject({\n Bucket: bucket,\n Key: key,\n Body: body,\n });\n return response.ETag || \"\";\n },\n catch: (error) =>\n handleS3Error(\"putObject\", error, { key, bucket, size: body.length }),\n });\n\n const deleteObject = (key: string) =>\n Effect.tryPromise({\n try: async () => {\n await s3Client.deleteObject({\n Bucket: bucket,\n Key: key,\n });\n },\n catch: (error) => handleS3Error(\"deleteObject\", error, { key, bucket }),\n });\n\n const deleteObjects = (keys: string[]) =>\n Effect.tryPromise({\n try: () =>\n s3Client.deleteObjects({\n Bucket: bucket,\n Delete: {\n Objects: keys.map((key) => ({ Key: key })),\n },\n }),\n catch: (error) =>\n handleS3Error(\"deleteObjects\", error, { keys: keys.length, bucket }),\n });\n\n const createMultipartUpload = (context: S3OperationContext) =>\n withS3ApiMetrics(\n \"createMultipartUpload\",\n Effect.tryPromise({\n try: async () => {\n const request: AWS.CreateMultipartUploadCommandInput = {\n Bucket: context.bucket,\n Key: context.key,\n };\n\n if (context.contentType) {\n request.ContentType = context.contentType;\n }\n\n if (context.cacheControl) {\n request.CacheControl = context.cacheControl;\n }\n\n const res = await s3Client.createMultipartUpload(request);\n\n if (!res.UploadId) {\n throw new Error(\"Upload ID is undefined\");\n }\n if (!res.Key) {\n throw new Error(\"Key is undefined\");\n }\n\n return {\n uploadId: res.UploadId,\n bucket: context.bucket,\n key: res.Key,\n };\n },\n catch: (error) =>\n handleS3Error(\"createMultipartUpload\", error, context),\n }),\n );\n\n const uploadPart = (\n context: S3OperationContext & { partNumber: number; data: Uint8Array },\n ) =>\n withS3ApiMetrics(\n \"uploadPart\",\n Effect.tryPromise({\n try: () =>\n s3Client.uploadPart({\n Bucket: context.bucket,\n Key: context.key,\n UploadId: context.uploadId,\n PartNumber: context.partNumber,\n Body: context.data,\n }),\n catch: (error) =>\n handleS3Error(\"uploadPart\", error, {\n upload_id: context.key,\n part_number: context.partNumber,\n part_size: context.data.length,\n s3_bucket: context.bucket,\n }),\n }).pipe(Effect.map((response) => response.ETag as string)),\n );\n\n const completeMultipartUpload = (\n context: S3OperationContext,\n parts: Array<AWS.Part>,\n ) =>\n withS3ApiMetrics(\n \"completeMultipartUpload\",\n Effect.tryPromise({\n try: () =>\n s3Client\n .completeMultipartUpload({\n Bucket: context.bucket,\n Key: context.key,\n UploadId: context.uploadId,\n MultipartUpload: {\n Parts: parts.map((part) => ({\n ETag: part.ETag,\n PartNumber: part.PartNumber,\n })),\n },\n })\n .then((response) => response.Location),\n catch: (error) =>\n handleS3Error(\"completeMultipartUpload\", error, {\n upload_id: context.key,\n parts_count: parts.length,\n s3_bucket: context.bucket,\n }),\n }),\n );\n\n const abortMultipartUpload = (context: S3OperationContext) =>\n Effect.tryPromise({\n try: async () => {\n await s3Client.abortMultipartUpload({\n Bucket: context.bucket,\n Key: context.key,\n UploadId: context.uploadId,\n });\n },\n catch: (error) =>\n handleS3NotFoundError(\"abortMultipartUpload\", error, {\n upload_id: context.key,\n s3_bucket: context.bucket,\n }),\n });\n\n const listParts = (\n context: S3OperationContext & { partNumberMarker?: string },\n ) =>\n Effect.tryPromise({\n try: async () => {\n const params: AWS.ListPartsCommandInput = {\n Bucket: context.bucket,\n Key: context.key,\n UploadId: context.uploadId,\n PartNumberMarker: context.partNumberMarker,\n };\n\n const data = await s3Client.listParts(params);\n\n return {\n parts: data.Parts ?? [],\n isTruncated: data.IsTruncated ?? false,\n nextPartNumberMarker: data.NextPartNumberMarker,\n };\n },\n catch: (error) =>\n handleS3Error(\"listParts\", error, {\n upload_id: context.key,\n s3_bucket: context.bucket,\n }),\n });\n\n const listMultipartUploads = (keyMarker?: string, uploadIdMarker?: string) =>\n Effect.tryPromise({\n try: () =>\n s3Client.listMultipartUploads({\n Bucket: bucket,\n KeyMarker: keyMarker,\n UploadIdMarker: uploadIdMarker,\n }),\n catch: (error) =>\n handleS3Error(\"listMultipartUploads\", error, { bucket }),\n });\n\n const getIncompletePart = (id: string) =>\n Effect.tryPromise({\n try: async () => {\n try {\n const data = await s3Client.getObject({\n Bucket: bucket,\n Key: partKey(id),\n });\n return toReadableStream(data.Body);\n } catch (error) {\n if (error instanceof NoSuchKey) {\n return undefined;\n }\n throw error;\n }\n },\n catch: (error) =>\n handleS3Error(\"getIncompletePart\", error, { upload_id: id, bucket }),\n });\n\n const getIncompletePartSize = (id: string) => headObject(partKey(id));\n\n const putIncompletePart = (id: string, data: Uint8Array) =>\n putObject(partKey(id), data).pipe(\n Effect.tap(() =>\n Effect.logInfo(\"Incomplete part uploaded\").pipe(\n Effect.annotateLogs({ upload_id: id }),\n ),\n ),\n );\n\n const deleteIncompletePart = (id: string) => deleteObject(partKey(id));\n\n return {\n bucket,\n getObject,\n headObject,\n putObject,\n deleteObject,\n deleteObjects,\n createMultipartUpload,\n uploadPart,\n completeMultipartUpload,\n abortMultipartUpload,\n listParts,\n listMultipartUploads,\n getIncompletePart,\n getIncompletePartSize,\n putIncompletePart,\n deleteIncompletePart,\n };\n};\n\nexport const S3ClientLayer = (s3ClientConfig: S3ClientConfig, bucket: string) =>\n Layer.succeed(S3ClientService, makeS3ClientService(s3ClientConfig, bucket));\n","import type AWS from \"@aws-sdk/client-s3\";\nimport { UploadistaError } from \"@uploadista/core/errors\";\nimport type {\n DataStore,\n DataStoreCapabilities,\n DataStoreWriteOptions,\n UploadFile,\n UploadStrategy,\n} from \"@uploadista/core/types\";\nimport { UploadFileKVStore } from \"@uploadista/core/types\";\nimport {\n s3ActiveUploadsGauge as activeUploadsGauge,\n s3FileSizeHistogram as fileSizeHistogram,\n logS3UploadCompletion,\n s3PartSizeHistogram as partSizeHistogram,\n s3PartUploadDurationHistogram as partUploadDurationHistogram,\n s3UploadDurationHistogram as uploadDurationHistogram,\n s3UploadErrorsTotal as uploadErrorsTotal,\n s3UploadPartsTotal as uploadPartsTotal,\n s3UploadRequestsTotal as uploadRequestsTotal,\n s3UploadSuccessTotal as uploadSuccessTotal,\n withS3TimingMetrics as withTimingMetrics,\n withS3UploadMetrics as withUploadMetrics,\n} from \"@uploadista/observability\";\nimport { Effect, Ref, Schedule, Stream } from \"effect\";\nimport { S3ClientLayer, S3ClientService } from \"./services/s3-client.service\";\nimport type { ChunkInfo, S3StoreConfig } from \"./types\";\nimport {\n calcOffsetFromParts,\n calcOptimalPartSize,\n getExpirationDate,\n isUploadNotFoundError,\n} from \"./utils\";\n\n/**\n * Generates an S3 key from an upload file, preserving the file extension if available.\n * Looks for filename in metadata under common keys: 'filename', 'fileName', or 'name'.\n * Falls back to just the upload ID if no filename is found.\n */\nconst getS3Key = (uploadFile: UploadFile): string => {\n const { id, metadata } = uploadFile;\n\n if (!metadata) {\n return id;\n }\n\n // Try common metadata keys for filename\n const filename = metadata.filename || metadata.fileName || metadata.name;\n\n if (typeof filename === \"string\" && filename.includes(\".\")) {\n const extension = filename.substring(filename.lastIndexOf(\".\"));\n return `${id}${extension}`;\n }\n\n return id;\n};\n\n// Clean implementation using composed services\nexport function createS3StoreImplementation(config: S3StoreConfig) {\n const {\n deliveryUrl,\n partSize,\n minPartSize = 5_242_880,\n useTags = true,\n maxMultipartParts = 10_000,\n kvStore,\n maxConcurrentPartUploads = 60,\n expirationPeriodInMilliseconds = 1000 * 60 * 60 * 24 * 7, // 1 week\n s3ClientConfig: { bucket },\n } = config;\n\n return Effect.gen(function* () {\n const s3Client = yield* S3ClientService;\n\n const preferredPartSize = partSize || 8 * 1024 * 1024;\n\n const getUploadId = (\n uploadFile: UploadFile,\n ): Effect.Effect<string, UploadistaError> => {\n const uploadId = uploadFile.storage.uploadId;\n if (!uploadId) {\n return Effect.fail(\n UploadistaError.fromCode(\n \"FILE_WRITE_ERROR\",\n new Error(\"Upload ID is undefined\"),\n ),\n );\n }\n return Effect.succeed(uploadId);\n };\n\n const uploadPart = (\n uploadFile: UploadFile,\n data: Uint8Array,\n partNumber: number,\n ) => {\n const s3Key = getS3Key(uploadFile);\n\n return withTimingMetrics(\n partUploadDurationHistogram,\n Effect.gen(function* () {\n const uploadId = yield* getUploadId(uploadFile);\n\n const etag = yield* s3Client\n .uploadPart({\n bucket: s3Client.bucket,\n key: s3Key,\n uploadId,\n partNumber,\n data,\n })\n .pipe(\n Effect.retry(\n Schedule.exponential(\"1 second\", 2.0).pipe(\n Schedule.intersect(Schedule.recurs(3)),\n ),\n ),\n Effect.tapError((error) =>\n Effect.logWarning(\"Retrying part upload\").pipe(\n Effect.annotateLogs({\n upload_id: uploadFile.id,\n part_number: partNumber,\n error_message: error.message,\n retry_attempt: \"unknown\", // Will be overridden by the retry schedule\n part_size: data.length,\n s3_bucket: s3Client.bucket,\n }),\n ),\n ),\n );\n\n yield* uploadPartsTotal(Effect.succeed(1));\n yield* Effect.logInfo(\"Part uploaded successfully\").pipe(\n Effect.annotateLogs({\n upload_id: uploadFile.id,\n part_number: partNumber,\n part_size: data.length,\n etag: etag,\n }),\n );\n\n return etag;\n }),\n ).pipe(\n Effect.withSpan(`s3-upload-part-${partNumber}`, {\n attributes: {\n \"upload.id\": uploadFile.id,\n \"upload.part_number\": partNumber,\n \"upload.part_size\": data.length,\n \"s3.bucket\": s3Client.bucket,\n \"s3.key\": s3Key,\n },\n }),\n );\n };\n\n const uploadIncompletePart = (id: string, data: Uint8Array) =>\n s3Client.putIncompletePart(id, data);\n\n const downloadIncompletePart = (id: string) =>\n Effect.gen(function* () {\n const incompletePart = yield* s3Client.getIncompletePart(id);\n\n if (!incompletePart) {\n return undefined;\n }\n\n // Read the stream and collect all chunks to calculate size\n const reader = incompletePart.getReader();\n const chunks: Uint8Array[] = [];\n let incompletePartSize = 0;\n\n try {\n while (true) {\n const { done, value } = yield* Effect.promise(() => reader.read());\n if (done) break;\n chunks.push(value);\n incompletePartSize += value.length;\n }\n } finally {\n reader.releaseLock();\n }\n\n const stream = Stream.fromIterable(chunks);\n\n return {\n size: incompletePartSize,\n stream,\n };\n });\n\n const deleteIncompletePart = (id: string) =>\n s3Client.deleteIncompletePart(id);\n\n const getIncompletePartSize = (id: string) =>\n s3Client.getIncompletePartSize(id);\n\n const complete = (uploadFile: UploadFile, parts: Array<AWS.Part>) => {\n const s3Key = getS3Key(uploadFile);\n\n return Effect.gen(function* () {\n const uploadId = yield* getUploadId(uploadFile);\n\n return yield* s3Client.completeMultipartUpload(\n {\n bucket: s3Client.bucket,\n key: s3Key,\n uploadId,\n },\n parts,\n );\n }).pipe(\n Effect.tap(() => uploadSuccessTotal(Effect.succeed(1))),\n Effect.withSpan(\"s3-complete-multipart-upload\", {\n attributes: {\n \"upload.id\": uploadFile.id,\n \"upload.parts_count\": parts.length,\n \"s3.bucket\": s3Client.bucket,\n \"s3.key\": s3Key,\n },\n }),\n );\n };\n\n const abort = (uploadFile: UploadFile) => {\n const s3Key = getS3Key(uploadFile);\n\n return Effect.gen(function* () {\n const uploadId = yield* getUploadId(uploadFile);\n\n yield* s3Client.abortMultipartUpload({\n bucket: s3Client.bucket,\n key: s3Key,\n uploadId,\n });\n\n yield* s3Client.deleteObjects([s3Key]);\n });\n };\n\n const retrievePartsRecursive = (\n s3Key: string,\n uploadId: string,\n uploadFileId: string,\n partNumberMarker?: string,\n ): Effect.Effect<\n { uploadFound: boolean; parts: AWS.Part[] },\n UploadistaError\n > =>\n Effect.gen(function* () {\n try {\n const result = yield* s3Client.listParts({\n bucket: s3Client.bucket,\n key: s3Key,\n uploadId,\n partNumberMarker,\n });\n\n let parts = result.parts;\n\n if (result.isTruncated) {\n const rest = yield* retrievePartsRecursive(\n s3Key,\n uploadId,\n uploadFileId,\n result.nextPartNumberMarker,\n );\n parts = [...parts, ...rest.parts];\n }\n\n if (!partNumberMarker) {\n parts.sort((a, b) => (a.PartNumber ?? 0) - (b.PartNumber ?? 0));\n }\n\n return { uploadFound: true, parts };\n } catch (error) {\n if (isUploadNotFoundError(error)) {\n yield* Effect.logWarning(\n \"S3 upload not found during listParts\",\n ).pipe(\n Effect.annotateLogs({\n upload_id: uploadFileId,\n error_code: error.code,\n }),\n );\n return { uploadFound: false, parts: [] };\n }\n throw error;\n }\n });\n\n const retrieveParts = (id: string, partNumberMarker?: string) =>\n Effect.gen(function* () {\n const metadata = yield* kvStore.get(id);\n const uploadId = yield* getUploadId(metadata);\n const s3Key = getS3Key(metadata);\n\n return yield* retrievePartsRecursive(\n s3Key,\n uploadId,\n id,\n partNumberMarker,\n );\n });\n\n const completeMetadata = (upload: UploadFile, useTags: boolean) =>\n Effect.gen(function* () {\n if (!useTags) {\n return 0;\n }\n\n const uploadFile = yield* kvStore.get(upload.id);\n const uploadId = uploadFile.storage.uploadId;\n if (!uploadId) {\n return 0;\n }\n\n yield* kvStore.set(upload.id, {\n ...uploadFile,\n storage: { ...uploadFile.storage, uploadId },\n });\n\n return 0;\n });\n\n const clearCache = (id: string) =>\n Effect.gen(function* () {\n yield* Effect.logInfo(\"Clearing cache\").pipe(\n Effect.annotateLogs({ upload_id: id }),\n );\n yield* kvStore.delete(id);\n });\n\n const createMultipartUpload = (upload: UploadFile) => {\n const s3Key = getS3Key(upload);\n\n return Effect.gen(function* () {\n yield* Effect.logInfo(\"Initializing multipart upload\").pipe(\n Effect.annotateLogs({ upload_id: upload.id }),\n );\n\n const multipartInfo = yield* s3Client.createMultipartUpload({\n bucket: s3Client.bucket,\n key: s3Key,\n uploadId: \"\", // Not needed for create\n contentType: upload.metadata?.contentType?.toString(),\n cacheControl: upload.metadata?.cacheControl?.toString(),\n });\n\n const uploadCreated = {\n ...upload,\n storage: {\n ...upload.storage,\n path: multipartInfo.key,\n uploadId: multipartInfo.uploadId,\n bucket: multipartInfo.bucket,\n },\n url: `${deliveryUrl}/${s3Key}`,\n };\n\n yield* kvStore.set(upload.id, uploadCreated);\n\n yield* Effect.logInfo(\"Multipart upload created\").pipe(\n Effect.annotateLogs({\n upload_id: upload.id,\n s3_upload_id: uploadCreated.storage.uploadId,\n s3_key: s3Key,\n }),\n );\n\n yield* uploadRequestsTotal(Effect.succeed(1));\n yield* fileSizeHistogram(Effect.succeed(upload.size || 0));\n\n return uploadCreated;\n }).pipe(\n Effect.withSpan(\"s3-create-upload\", {\n attributes: {\n \"upload.id\": upload.id,\n \"upload.size\": upload.size || 0,\n \"s3.bucket\": s3Client.bucket,\n \"s3.key\": s3Key,\n },\n }),\n );\n };\n\n /**\n * Creates a multipart upload on S3 attaching any metadata to it.\n * Also, a `${file_id}.info` file is created which holds some information\n * about the upload itself like: `upload-id`, `upload-length`, etc.\n */\n const create = (upload: UploadFile) => {\n return Effect.gen(function* () {\n yield* Effect.logInfo(\"Initializing multipart upload\").pipe(\n Effect.annotateLogs({ upload_id: upload.id }),\n );\n const uploadCreated = yield* createMultipartUpload(upload);\n yield* kvStore.set(upload.id, uploadCreated);\n yield* Effect.logInfo(\"Multipart upload created\").pipe(\n Effect.annotateLogs({\n upload_id: upload.id,\n s3_upload_id: uploadCreated.storage.uploadId,\n }),\n );\n yield* uploadRequestsTotal(Effect.succeed(1));\n\n return uploadCreated;\n }).pipe(\n Effect.withSpan(\"s3-create-upload\", {\n attributes: {\n \"upload.id\": upload.id,\n \"upload.size\": upload.size || 0,\n \"s3.bucket\": bucket,\n },\n }),\n );\n };\n\n const remove = (id: string) =>\n Effect.gen(function* () {\n const uploadFile = yield* kvStore.get(id);\n yield* abort(uploadFile);\n yield* clearCache(id);\n });\n\n const write = (\n options: DataStoreWriteOptions,\n dependencies: { onProgress?: (currentOffset: number) => void },\n ) =>\n withUploadMetrics(\n options.file_id,\n withTimingMetrics(\n uploadDurationHistogram,\n Effect.gen(function* () {\n const {\n stream: initialData,\n file_id,\n offset: initialOffset,\n } = options;\n const { onProgress } = dependencies;\n\n // Capture start time for upload completion metrics\n const startTime = Date.now();\n\n // Track active upload\n yield* activeUploadsGauge(Effect.succeed(1));\n\n const prepareResult = yield* prepareUpload(\n file_id,\n initialOffset,\n initialData,\n );\n\n const {\n uploadFile,\n nextPartNumber,\n offset,\n data,\n existingPartSize,\n } = prepareResult;\n\n // Use existing part size if parts already exist, otherwise calculate optimal size\n const uploadPartSize =\n existingPartSize ||\n calcOptimalPartSize(\n uploadFile.size,\n preferredPartSize,\n minPartSize,\n maxMultipartParts,\n );\n\n // Log part size decision for debugging\n yield* Effect.logInfo(\"Part size decision\").pipe(\n Effect.annotateLogs({\n upload_id: file_id,\n existing_part_size: existingPartSize,\n calculated_part_size: calcOptimalPartSize(\n uploadFile.size,\n preferredPartSize,\n minPartSize,\n maxMultipartParts,\n ),\n final_part_size: uploadPartSize,\n next_part_number: nextPartNumber,\n }),\n );\n\n const bytesUploaded = yield* uploadParts(\n uploadFile,\n data,\n nextPartNumber,\n offset,\n uploadPartSize,\n minPartSize,\n maxConcurrentPartUploads,\n onProgress,\n );\n\n const newOffset = offset + bytesUploaded;\n\n if (newOffset > maxConcurrentPartUploads)\n if (uploadFile.size === newOffset) {\n yield* finishUpload(file_id, uploadFile, startTime);\n }\n\n return newOffset;\n }).pipe(Effect.ensuring(activeUploadsGauge(Effect.succeed(0)))),\n ),\n );\n\n const getUpload = (id: string) =>\n Effect.gen(function* () {\n const uploadFile = yield* kvStore.get(id);\n\n const { parts, uploadFound } = yield* retrieveParts(id);\n if (!uploadFound) {\n return {\n ...uploadFile,\n offset: uploadFile.size as number,\n size: uploadFile.size,\n };\n }\n\n const offset = calcOffsetFromParts(parts);\n const incompletePartSize = yield* getIncompletePartSize(id);\n\n return {\n ...uploadFile,\n offset: offset + (incompletePartSize ?? 0),\n size: uploadFile.size,\n storage: uploadFile.storage,\n };\n });\n\n // const read = (id: string) =>\n // Effect.gen(function* () {\n // return yield* s3Client.getObject(id);\n // });\n\n // Helper functions\n const prepareUpload = (\n fileId: string,\n initialOffset: number,\n initialData: Stream.Stream<Uint8Array, UploadistaError>,\n ) =>\n Effect.gen(function* () {\n const uploadFile = yield* kvStore.get(fileId);\n const { parts } = yield* retrieveParts(fileId);\n\n const partNumber: number =\n parts.length > 0 && parts[parts.length - 1].PartNumber\n ? (parts[parts.length - 1].PartNumber ?? 0)\n : 0;\n const nextPartNumber = partNumber + 1;\n\n // Detect existing part size to maintain consistency\n // We check the first part's size to ensure all subsequent parts match\n const existingPartSize =\n parts.length > 0 && parts[0].Size ? parts[0].Size : null;\n\n // Validate that all existing parts (except potentially the last one) have the same size\n if (existingPartSize && parts.length > 1) {\n const inconsistentPart = parts\n .slice(0, -1)\n .find((part) => part.Size !== existingPartSize);\n if (inconsistentPart) {\n yield* Effect.logWarning(\n \"Inconsistent part sizes detected in existing upload\",\n ).pipe(\n Effect.annotateLogs({\n upload_id: fileId,\n expected_size: existingPartSize,\n inconsistent_part: inconsistentPart.PartNumber,\n inconsistent_size: inconsistentPart.Size,\n }),\n );\n }\n }\n\n const incompletePart = yield* downloadIncompletePart(fileId);\n\n if (incompletePart) {\n yield* deleteIncompletePart(fileId);\n const offset = initialOffset - incompletePart.size;\n const data = incompletePart.stream.pipe(Stream.concat(initialData));\n return {\n uploadFile,\n nextPartNumber,\n offset,\n incompletePartSize: incompletePart.size,\n data,\n existingPartSize,\n };\n } else {\n return {\n uploadFile,\n nextPartNumber,\n offset: initialOffset,\n incompletePartSize: 0,\n data: initialData,\n existingPartSize,\n };\n }\n });\n\n const finishUpload = (\n fileId: string,\n uploadFile: UploadFile,\n startTime: number,\n ) =>\n Effect.gen(function* () {\n const { parts } = yield* retrieveParts(fileId);\n\n // Log all parts for debugging S3 multipart upload requirements\n yield* Effect.logInfo(\"Attempting to complete multipart upload\").pipe(\n Effect.annotateLogs({\n upload_id: fileId,\n parts_count: parts.length,\n parts_info: parts.map((part, index) => ({\n part_number: part.PartNumber,\n size: part.Size,\n etag: part.ETag,\n is_final_part: index === parts.length - 1,\n })),\n }),\n );\n\n yield* complete(uploadFile, parts);\n yield* completeMetadata(uploadFile, useTags);\n // yield* clearCache(fileId);\n\n // Log upload completion metrics\n const endTime = Date.now();\n const totalDurationMs = endTime - startTime;\n const fileSize = uploadFile.size || 0;\n const throughputBps =\n totalDurationMs > 0 ? (fileSize * 1000) / totalDurationMs : 0;\n\n // Calculate average part size if we have parts\n const averagePartSize =\n parts.length > 0\n ? parts.reduce((sum, part) => sum + (part.Size || 0), 0) /\n parts.length\n : undefined;\n\n yield* logS3UploadCompletion(fileId, {\n fileSize,\n totalDurationMs,\n partsCount: parts.length,\n averagePartSize,\n throughputBps,\n });\n }).pipe(\n Effect.tapError((error) =>\n Effect.gen(function* () {\n yield* uploadErrorsTotal(Effect.succeed(1));\n yield* Effect.logError(\"Failed to finish upload\").pipe(\n Effect.annotateLogs({\n upload_id: fileId,\n error: String(error),\n }),\n );\n }),\n ),\n );\n\n const deleteExpired = Effect.gen(function* () {\n if (expirationPeriodInMilliseconds === 0) {\n return 0;\n }\n\n let keyMarker: string | undefined;\n let uploadIdMarker: string | undefined;\n let isTruncated = true;\n let deleted = 0;\n\n while (isTruncated) {\n const listResponse = yield* s3Client.listMultipartUploads(\n keyMarker,\n uploadIdMarker,\n );\n\n const expiredUploads =\n listResponse.Uploads?.filter((multiPartUpload) => {\n const initiatedDate = multiPartUpload.Initiated;\n return (\n initiatedDate &&\n Date.now() >\n getExpirationDate(\n initiatedDate.toISOString(),\n expirationPeriodInMilliseconds,\n ).getTime()\n );\n }) || [];\n\n const objectsToDelete = expiredUploads\n .filter((upload): upload is { Key: string } => {\n return !!upload.Key;\n })\n .map((upload) => upload.Key);\n\n if (objectsToDelete.length > 0) {\n yield* s3Client.deleteObjects(objectsToDelete);\n\n // Abort multipart uploads\n yield* Effect.forEach(expiredUploads, (upload) => {\n return Effect.gen(function* () {\n if (!upload.Key || !upload.UploadId) {\n return;\n }\n yield* s3Client.abortMultipartUpload({\n bucket,\n key: upload.Key,\n uploadId: upload.UploadId,\n });\n return;\n });\n });\n\n deleted += objectsToDelete.length;\n }\n\n isTruncated = listResponse.IsTruncated ?? false;\n\n if (isTruncated) {\n keyMarker = listResponse.NextKeyMarker;\n uploadIdMarker = listResponse.NextUploadIdMarker;\n }\n }\n\n return deleted;\n });\n\n // Proper single-pass chunking using Effect's async stream constructor\n // Ensures all parts except the final part are exactly the same size (S3 requirement)\n const createChunkedStream =\n (chunkSize: number) =>\n <E>(\n stream: Stream.Stream<Uint8Array, E>,\n ): Stream.Stream<ChunkInfo, E> => {\n return Stream.async<ChunkInfo, E>((emit) => {\n let buffer = new Uint8Array(0);\n let partNumber = 1;\n let totalBytesProcessed = 0;\n\n const emitChunk = (data: Uint8Array, isFinalChunk = false) => {\n // Log chunk information for debugging - use INFO level to see in logs\n Effect.runSync(\n Effect.logInfo(\"Creating chunk\").pipe(\n Effect.annotateLogs({\n part_number: partNumber,\n chunk_size: data.length,\n expected_size: chunkSize,\n is_final_chunk: isFinalChunk,\n total_bytes_processed: totalBytesProcessed + data.length,\n }),\n ),\n );\n emit.single({\n partNumber: partNumber++,\n data,\n size: data.length,\n });\n };\n\n const processChunk = (newData: Uint8Array) => {\n // Combine buffer with new data\n const combined = new Uint8Array(buffer.length + newData.length);\n combined.set(buffer);\n combined.set(newData, buffer.length);\n buffer = combined;\n totalBytesProcessed += newData.length;\n\n // Emit full chunks of exactly chunkSize bytes\n // This ensures S3 multipart upload rule: all parts except last must be same size\n while (buffer.length >= chunkSize) {\n const chunk = buffer.slice(0, chunkSize);\n buffer = buffer.slice(chunkSize);\n emitChunk(chunk, false);\n }\n };\n\n // Process the stream\n Effect.runFork(\n stream.pipe(\n Stream.runForEach((chunk) =>\n Effect.sync(() => processChunk(chunk)),\n ),\n Effect.andThen(() =>\n Effect.sync(() => {\n // Emit final chunk if there's remaining data\n // The final chunk can be any size < chunkSize (S3 allows this)\n if (buffer.length > 0) {\n emitChunk(buffer, true);\n }\n emit.end();\n }),\n ),\n Effect.catchAll((error) => Effect.sync(() => emit.fail(error))),\n ),\n );\n });\n };\n\n // Byte-level progress tracking during streaming\n // This provides smooth, immediate progress feedback by tracking bytes as they\n // flow through the stream, before they reach S3. This solves the issue where\n // small files (< 5MB) would jump from 0% to 100% instantly.\n const withByteProgressTracking =\n (onProgress?: (totalBytes: number) => void, initialOffset = 0) =>\n <E, R>(stream: Stream.Stream<Uint8Array, E, R>) => {\n if (!onProgress) return stream;\n\n return Effect.gen(function* () {\n const totalBytesProcessedRef = yield* Ref.make(initialOffset);\n\n return stream.pipe(\n Stream.tap((chunk) =>\n Effect.gen(function* () {\n const newTotal = yield* Ref.updateAndGet(\n totalBytesProcessedRef,\n (total) => total + chunk.length,\n );\n onProgress(newTotal);\n }),\n ),\n );\n }).pipe(Stream.unwrap);\n };\n\n const uploadParts = (\n uploadFile: UploadFile,\n readStream: Stream.Stream<Uint8Array, UploadistaError>,\n initCurrentPartNumber: number,\n initOffset: number,\n uploadPartSize: number,\n minPartSize: number,\n maxConcurrentPartUploads: number,\n onProgress?: (newOffset: number) => void,\n ) =>\n Effect.gen(function* () {\n yield* Effect.logInfo(\"Starting part uploads\").pipe(\n Effect.annotateLogs({\n upload_id: uploadFile.id,\n init_offset: initOffset,\n file_size: uploadFile.size,\n part_size: uploadPartSize,\n min_part_size: minPartSize,\n }),\n );\n\n // Enhanced Progress Tracking Strategy:\n // 1. Byte-level progress during streaming - provides immediate, smooth feedback\n // as data flows through the pipeline (even for small files)\n // 2. This tracks progress BEFORE S3 upload, giving users immediate feedback\n // 3. For large files with multiple parts, this provides granular updates\n // 4. For small files (single part), this prevents 0%->100% jumps\n const chunkStream = readStream.pipe(\n // Add byte-level progress tracking during streaming (immediate feedback)\n withByteProgressTracking(onProgress, initOffset),\n // Create chunks for S3 multipart upload with uniform part sizes\n createChunkedStream(uploadPartSize),\n );\n\n // Track cumulative offset and total bytes with Effect Refs\n const cumulativeOffsetRef = yield* Ref.make(initOffset);\n const totalBytesUploadedRef = yield* Ref.make(0);\n\n // Create a chunk upload function for the sink\n const uploadChunk = (chunkInfo: ChunkInfo) =>\n Effect.gen(function* () {\n // Calculate cumulative bytes to determine if this is the final part\n const cumulativeOffset = yield* Ref.updateAndGet(\n cumulativeOffsetRef,\n (offset) => offset + chunkInfo.size,\n );\n const isFinalPart = cumulativeOffset >= (uploadFile.size || 0);\n\n yield* Effect.logDebug(\"Processing chunk\").pipe(\n Effect.annotateLogs({\n upload_id: uploadFile.id,\n cumulative_offset: cumulativeOffset,\n file_size: uploadFile.size,\n chunk_size: chunkInfo.size,\n is_final_part: isFinalPart,\n }),\n );\n\n const actualPartNumber =\n initCurrentPartNumber + chunkInfo.partNumber - 1;\n\n if (chunkInfo.size > uploadPartSize) {\n yield* Effect.fail(\n UploadistaError.fromCode(\n \"FILE_WRITE_ERROR\",\n new Error(\n `Part size ${chunkInfo.size} exceeds upload part size ${uploadPartSize}`,\n ),\n ),\n );\n }\n\n // For parts that meet the minimum part size (5MB) or are the final part,\n // upload them as regular multipart parts\n if (chunkInfo.size >= minPartSize || isFinalPart) {\n yield* Effect.logDebug(\"Uploading multipart chunk\").pipe(\n Effect.annotateLogs({\n upload_id: uploadFile.id,\n part_number: actualPartNumber,\n chunk_size: chunkInfo.size,\n min_part_size: minPartSize,\n is_final_part: isFinalPart,\n }),\n );\n yield* uploadPart(uploadFile, chunkInfo.data, actualPartNumber);\n yield* partSizeHistogram(Effect.succeed(chunkInfo.size));\n } else {\n // Only upload as incomplete part if it's smaller than minimum and not final\n yield* uploadIncompletePart(uploadFile.id, chunkInfo.data);\n }\n\n yield* Ref.update(\n totalBytesUploadedRef,\n (total) => total + chunkInfo.size,\n );\n\n // Note: Byte-level progress is now tracked during streaming phase\n // This ensures smooth progress updates regardless of part size\n // S3 upload completion is tracked via totalBytesUploadedRef for accuracy\n });\n\n // Process chunks concurrently with controlled concurrency\n yield* chunkStream.pipe(\n Stream.runForEach((chunkInfo) => uploadChunk(chunkInfo)),\n Effect.withConcurrency(maxConcurrentPartUploads),\n );\n\n return yield* Ref.get(totalBytesUploadedRef);\n });\n\n const getCapabilities = (): DataStoreCapabilities => ({\n supportsParallelUploads: true,\n supportsConcatenation: true,\n supportsDeferredLength: true,\n supportsResumableUploads: true,\n supportsTransactionalUploads: true,\n maxConcurrentUploads: maxConcurrentPartUploads,\n minChunkSize: minPartSize,\n maxChunkSize: 5_368_709_120, // 5GiB S3 limit\n maxParts: maxMultipartParts,\n optimalChunkSize: preferredPartSize,\n requiresOrderedChunks: false,\n requiresMimeTypeValidation: true,\n maxValidationSize: undefined, // no size limit\n });\n\n const getChunkerConstraints = () => ({\n minChunkSize: minPartSize,\n maxChunkSize: 5_368_709_120, // 5GiB S3 limit\n optimalChunkSize: preferredPartSize,\n requiresOrderedChunks: false,\n });\n\n const validateUploadStrategy = (\n strategy: UploadStrategy,\n ): Effect.Effect<boolean, never> => {\n const capabilities = getCapabilities();\n const result = (() => {\n switch (strategy) {\n case \"parallel\":\n return capabilities.supportsParallelUploads;\n case \"single\":\n return true;\n default:\n return false;\n }\n })();\n return Effect.succeed(result);\n };\n\n const concatArrayBuffers = (chunks: Uint8Array[]): Uint8Array => {\n const result = new Uint8Array(chunks.reduce((a, c) => a + c.length, 0));\n let offset = 0;\n for (const chunk of chunks) {\n result.set(chunk, offset);\n offset += chunk.length;\n }\n return result;\n };\n\n const streamToArray = async (\n stream: ReadableStream<Uint8Array>,\n ): Promise<Uint8Array> => {\n const reader = stream.getReader();\n const chunks: Uint8Array[] = [];\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n chunks.push(value);\n }\n return concatArrayBuffers(chunks);\n };\n\n const read = (id: string) =>\n Effect.gen(function* () {\n const upload = yield* kvStore.get(id);\n console.log(upload);\n if (!upload.id) {\n return yield* Effect.fail(\n UploadistaError.fromCode(\n \"FILE_READ_ERROR\",\n new Error(\"Upload Key is undefined\"),\n ),\n );\n }\n const s3Key = getS3Key(upload);\n const stream = yield* s3Client.getObject(s3Key);\n return yield* Effect.promise(() => streamToArray(stream));\n });\n\n return {\n bucket,\n create,\n remove,\n write,\n getUpload,\n read,\n deleteExpired,\n getCapabilities,\n getChunkerConstraints,\n validateUploadStrategy,\n } as DataStore<UploadFile>;\n });\n}\n\n// Effect-based factory that uses services\nexport const createS3Store = (options: S3StoreConfig) =>\n Effect.gen(function* () {\n const kvStore = yield* UploadFileKVStore;\n const {\n s3ClientConfig: { bucket, ...restS3ClientConfig },\n } = options;\n return yield* createS3StoreImplementation({\n ...options,\n kvStore,\n }).pipe(Effect.provide(S3ClientLayer(restS3ClientConfig, bucket)));\n });\n\n// Backward compatibility: keep the original function for existing code\nexport const s3Store = (config: S3StoreConfig) => {\n const {\n s3ClientConfig: { bucket, ...restS3ClientConfig },\n } = config;\n return Effect.runPromise(\n createS3StoreImplementation(config).pipe(\n Effect.provide(S3ClientLayer(restS3ClientConfig, bucket)),\n ),\n );\n};\n"],"mappings":"qqBAEA,MAAa,EAAuB,GAC3B,GAAS,EAAM,OAAS,EAC3B,EAAM,QAAQ,EAAG,IAAM,GAAK,GAAG,MAAQ,GAAI,EAAE,CAC7C,EAGO,GACX,EACA,EACA,EACA,EACA,EAAgB,gBACL,CACX,IAAM,EAAO,GAAY,EACrBA,EAEJ,AASE,EATE,GAAQ,EAGQ,EACT,GAAQ,EAAoB,EAEnB,EAGA,KAAK,KAAK,EAAO,EAAkB,CAKvD,IAAM,EACJ,GAAY,EAAW,EACnB,EACA,KAAK,IAAI,EAAiB,EAAY,CAItC,EAAY,KAClB,OAAO,KAAK,KAAK,EAAgB,EAAU,CAAG,GAGnC,EAAW,GACf,GAAG,EAAG,OAUF,GACX,EACA,I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name": "@uploadista/data-store-s3",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.6",
|
|
5
5
|
"description": "AWS S3 data store for Uploadista",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": "Uploadista",
|
|
@@ -15,16 +15,17 @@
|
|
|
15
15
|
"dependencies": {
|
|
16
16
|
"@aws-sdk/client-s3": "3.913.0",
|
|
17
17
|
"effect": "3.18.4",
|
|
18
|
-
"@uploadista/core": "0.0.
|
|
19
|
-
"@uploadista/
|
|
20
|
-
"@uploadista/
|
|
18
|
+
"@uploadista/core": "0.0.6",
|
|
19
|
+
"@uploadista/kv-store-memory": "0.0.6",
|
|
20
|
+
"@uploadista/observability": "0.0.6"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
+
"tsdown": "0.15.9",
|
|
23
24
|
"vitest": "3.2.4",
|
|
24
|
-
"@uploadista/typescript-config": "0.0.
|
|
25
|
+
"@uploadista/typescript-config": "0.0.6"
|
|
25
26
|
},
|
|
26
27
|
"scripts": {
|
|
27
|
-
"build": "
|
|
28
|
+
"build": "tsdown",
|
|
28
29
|
"format": "biome format --write ./src",
|
|
29
30
|
"lint": "biome lint --write ./src",
|
|
30
31
|
"check": "biome check --write ./src",
|
package/tsdown.config.ts
ADDED
package/.turbo/turbo-build.log
DELETED
package/.turbo/turbo-check.log
DELETED
package/dist/observability.d.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { Context, Effect, Layer, Metric } from "effect";
|
|
2
|
-
export declare const uploadRequestsTotal: Metric.Metric.Counter<number>;
|
|
3
|
-
export declare const uploadPartsTotal: Metric.Metric.Counter<number>;
|
|
4
|
-
export declare const uploadSuccessTotal: Metric.Metric.Counter<number>;
|
|
5
|
-
export declare const uploadErrorsTotal: Metric.Metric.Counter<number>;
|
|
6
|
-
export declare const s3ApiCallsTotal: Metric.Metric.Counter<number>;
|
|
7
|
-
export declare const uploadDurationHistogram: Metric.Metric<import("effect/MetricKeyType").MetricKeyType.Histogram, number, import("effect/MetricState").MetricState.Histogram>;
|
|
8
|
-
export declare const partUploadDurationHistogram: Metric.Metric<import("effect/MetricKeyType").MetricKeyType.Histogram, number, import("effect/MetricState").MetricState.Histogram>;
|
|
9
|
-
export declare const fileSizeHistogram: Metric.Metric<import("effect/MetricKeyType").MetricKeyType.Histogram, number, import("effect/MetricState").MetricState.Histogram>;
|
|
10
|
-
export declare const partSizeHistogram: Metric.Metric<import("effect/MetricKeyType").MetricKeyType.Histogram, number, import("effect/MetricState").MetricState.Histogram>;
|
|
11
|
-
export declare const activeUploadsGauge: Metric.Metric.Gauge<number>;
|
|
12
|
-
export declare const uploadThroughputGauge: Metric.Metric.Gauge<number>;
|
|
13
|
-
export declare const uploadLatencySummary: Metric.Metric.Summary<number>;
|
|
14
|
-
export declare const TracingService: Context.Tag<{
|
|
15
|
-
serviceName: string;
|
|
16
|
-
}, {
|
|
17
|
-
serviceName: string;
|
|
18
|
-
}>;
|
|
19
|
-
export declare const createTracingLayer: (options?: {
|
|
20
|
-
serviceName?: string;
|
|
21
|
-
}) => Layer.Layer<{
|
|
22
|
-
serviceName: string;
|
|
23
|
-
}, never, never>;
|
|
24
|
-
export declare const TracingLayerLive: Layer.Layer<{
|
|
25
|
-
serviceName: string;
|
|
26
|
-
}, never, never>;
|
|
27
|
-
export declare const ObservabilityLayer: Layer.Layer<{
|
|
28
|
-
serviceName: string;
|
|
29
|
-
}, never, never>;
|
|
30
|
-
export declare const withUploadMetrics: <A, E, R>(uploadId: string, effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
|
|
31
|
-
export declare const withS3ApiMetrics: <A, E, R>(operation: string, effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
|
|
32
|
-
export declare const withTimingMetrics: <A, E, R>(metric: Metric.Metric.Histogram<number>, effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
|
|
33
|
-
export type S3ErrorCategory = "network_error" | "authentication_error" | "authorization_error" | "throttling_error" | "server_error" | "client_error" | "unknown_error";
|
|
34
|
-
export declare const classifyS3Error: (error: unknown) => S3ErrorCategory;
|
|
35
|
-
export declare const trackS3Error: (operation: string, error: unknown, context?: Record<string, unknown>) => Effect.Effect<void, never, never>;
|
|
36
|
-
export declare const logWithContext: (message: string, context: Record<string, unknown>) => Effect.Effect<void, never, never>;
|
|
37
|
-
export declare const logUploadProgress: (uploadId: string, progress: {
|
|
38
|
-
uploadedBytes: number;
|
|
39
|
-
totalBytes: number;
|
|
40
|
-
partNumber?: number;
|
|
41
|
-
speed?: number;
|
|
42
|
-
}) => Effect.Effect<void, never, never>;
|
|
43
|
-
export declare const logS3Operation: (operation: string, uploadId: string, metadata?: Record<string, unknown>) => Effect.Effect<void, never, never>;
|
|
44
|
-
export declare const logS3Error: (operation: string, error: unknown, context?: Record<string, unknown>) => Effect.Effect<void, never, never>;
|
|
45
|
-
//# sourceMappingURL=observability.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"observability.d.ts","sourceRoot":"","sources":["../src/observability.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAoB,MAAM,QAAQ,CAAA;AAOzE,eAAO,MAAM,mBAAmB,+BAE9B,CAAA;AAEF,eAAO,MAAM,gBAAgB,+BAE3B,CAAA;AAEF,eAAO,MAAM,kBAAkB,+BAE7B,CAAA;AAEF,eAAO,MAAM,iBAAiB,+BAE5B,CAAA;AAEF,eAAO,MAAM,eAAe,+BAE1B,CAAA;AAGF,eAAO,MAAM,uBAAuB,mIAQnC,CAAA;AAED,eAAO,MAAM,2BAA2B,mIAQvC,CAAA;AAED,eAAO,MAAM,iBAAiB,mIAQ7B,CAAA;AAED,eAAO,MAAM,iBAAiB,mIAQ7B,CAAA;AAGD,eAAO,MAAM,kBAAkB,6BAE7B,CAAA;AAEF,eAAO,MAAM,qBAAqB,6BAEhC,CAA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