@uploadista/core 0.2.0 → 1.0.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{checksum-BjP9nb5b.mjs → checksum-BRjFmTRk.mjs} +2 -2
- package/dist/{checksum-BjP9nb5b.mjs.map → checksum-BRjFmTRk.mjs.map} +1 -1
- package/dist/{checksum-B7RDiO7V.cjs → checksum-BrjQ8GJL.cjs} +1 -1
- package/dist/errors/index.cjs +1 -1
- package/dist/errors/index.d.cts +1 -1
- package/dist/errors/index.d.mts +1 -1
- package/dist/errors/index.mjs +1 -1
- package/dist/flow/index.cjs +1 -1
- package/dist/flow/index.d.cts +3 -2
- package/dist/flow/index.d.mts +8 -5
- package/dist/flow/index.mjs +1 -1
- package/dist/generate-id-BAMRQzMr.d.cts +34 -0
- package/dist/generate-id-BAMRQzMr.d.cts.map +1 -0
- package/dist/generate-id-DuZwLm4m.d.mts +34 -0
- package/dist/generate-id-DuZwLm4m.d.mts.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +8 -5
- package/dist/index.d.mts +8 -5
- package/dist/index.mjs +1 -1
- package/dist/middleware-BghazxzH.d.cts +4129 -0
- package/dist/middleware-BghazxzH.d.cts.map +1 -0
- package/dist/middleware-CYizzAhP.d.mts +4129 -0
- package/dist/middleware-CYizzAhP.d.mts.map +1 -0
- package/dist/resolve-upload-metadata-CYl2PHIs.d.mts +4542 -0
- package/dist/resolve-upload-metadata-CYl2PHIs.d.mts.map +1 -0
- package/dist/resolve-upload-metadata-D0qFuyWc.d.cts +4542 -0
- package/dist/resolve-upload-metadata-D0qFuyWc.d.cts.map +1 -0
- package/dist/run-args-CM14Vtzu.cjs +1 -0
- package/dist/run-args-DSKHoSWs.mjs +2 -0
- package/dist/run-args-DSKHoSWs.mjs.map +1 -0
- package/dist/{stream-limiter-BCFULdAM.d.cts → stream-limiter-7wkBVLWT.d.mts} +2 -2
- package/dist/{stream-limiter-BCFULdAM.d.cts.map → stream-limiter-7wkBVLWT.d.mts.map} +1 -1
- package/dist/{stream-limiter-DZ22uIqf.cjs → stream-limiter-B-Y0DTgA.cjs} +1 -1
- package/dist/{stream-limiter-CTJPEJqE.mjs → stream-limiter-CvDuNIyd.mjs} +2 -2
- package/dist/{stream-limiter-CTJPEJqE.mjs.map → stream-limiter-CvDuNIyd.mjs.map} +1 -1
- package/dist/{stream-limiter-Bi7OTbRp.d.mts → stream-limiter-D1KC-6pK.d.cts} +2 -2
- package/dist/{stream-limiter-Bi7OTbRp.d.mts.map → stream-limiter-D1KC-6pK.d.cts.map} +1 -1
- package/dist/streams/index.cjs +1 -1
- package/dist/streams/index.d.cts +1 -1
- package/dist/streams/index.d.mts +2 -2
- package/dist/streams/index.mjs +1 -1
- package/dist/testing/index.cjs +1 -1
- package/dist/testing/index.d.cts +2 -1
- package/dist/testing/index.d.cts.map +1 -1
- package/dist/testing/index.d.mts +7 -4
- package/dist/testing/index.d.mts.map +1 -1
- package/dist/testing/index.mjs +1 -1
- package/dist/{throttle-Da0OA8JT.d.cts → throttle-3FRcr7MU.d.mts} +4 -34
- package/dist/throttle-3FRcr7MU.d.mts.map +1 -0
- package/dist/{throttle-ibiT6E4U.d.mts → throttle-BlH27EGu.d.cts} +4 -34
- package/dist/throttle-BlH27EGu.d.cts.map +1 -0
- package/dist/{throttle-KnkRgZPi.cjs → throttle-Dp59f37i.cjs} +1 -1
- package/dist/{throttle-CnDa3v1k.mjs → throttle-TFY-V41R.mjs} +2 -2
- package/dist/{throttle-CnDa3v1k.mjs.map → throttle-TFY-V41R.mjs.map} +1 -1
- package/dist/types/index.cjs +1 -1
- package/dist/types/index.d.cts +2 -2
- package/dist/types/index.d.mts +3 -5
- package/dist/types/index.mjs +1 -1
- package/dist/upload/index.cjs +1 -1
- package/dist/upload/index.d.cts +1 -1
- package/dist/upload/index.d.mts +4 -4
- package/dist/upload/index.mjs +1 -1
- package/dist/upload-strategy-negotiator-0-dpNIce.d.cts +455 -0
- package/dist/upload-strategy-negotiator-0-dpNIce.d.cts.map +1 -0
- package/dist/upload-strategy-negotiator-BR_o1Ez8.cjs +1 -0
- package/dist/upload-strategy-negotiator-C9MeoOnW.mjs +2 -0
- package/dist/upload-strategy-negotiator-C9MeoOnW.mjs.map +1 -0
- package/dist/upload-strategy-negotiator-CEnlfVgJ.d.mts +455 -0
- package/dist/upload-strategy-negotiator-CEnlfVgJ.d.mts.map +1 -0
- package/dist/{uploadista-error-B-geDgi8.cjs → uploadista-error-CZx1JU_L.cjs} +3 -1
- package/dist/{uploadista-error-Fsfvr2Bb.mjs → uploadista-error-DQ7V1FlX.mjs} +3 -1
- package/dist/uploadista-error-DQ7V1FlX.mjs.map +1 -0
- package/dist/{uploadista-error-BragVhIs.d.mts → uploadista-error-LtiZn-R_.d.mts} +2 -2
- package/dist/{uploadista-error-BragVhIs.d.mts.map → uploadista-error-LtiZn-R_.d.mts.map} +1 -1
- package/dist/{uploadista-error-Cj_pAFck.d.cts → uploadista-error-eZtG4iyf.d.cts} +2 -2
- package/dist/{uploadista-error-Cj_pAFck.d.cts.map → uploadista-error-eZtG4iyf.d.cts.map} +1 -1
- package/dist/utils/index.cjs +1 -1
- package/dist/utils/index.d.cts +2 -1
- package/dist/utils/index.d.mts +3 -2
- package/dist/utils/index.mjs +1 -1
- package/dist/websocket-Br0ijEZA.cjs +1 -0
- package/dist/websocket-DftnHFfN.mjs +2 -0
- package/dist/websocket-DftnHFfN.mjs.map +1 -0
- package/package.json +3 -3
- package/src/errors/uploadista-error.ts +11 -1
- package/src/flow/README.md +115 -0
- package/src/flow/flow-engine.ts +36 -2
- package/src/flow/flow-queue-store.ts +155 -0
- package/src/flow/flow-queue.ts +640 -0
- package/src/flow/index.ts +4 -0
- package/src/flow/types/flow-queue-item.ts +154 -0
- package/src/types/data-store.ts +3 -3
- package/src/types/kv-store.ts +31 -1
- package/src/upload/write-to-store.ts +24 -29
- package/tests/flow-queue-store.test.ts +150 -0
- package/tests/flow-queue.test.ts +308 -0
- package/dist/resolve-upload-metadata-BUVl1LoS.d.cts +0 -8723
- package/dist/resolve-upload-metadata-BUVl1LoS.d.cts.map +0 -1
- package/dist/resolve-upload-metadata-MPDmDfOZ.d.mts +0 -8723
- package/dist/resolve-upload-metadata-MPDmDfOZ.d.mts.map +0 -1
- package/dist/run-args-WD1otVrz.mjs +0 -2
- package/dist/run-args-WD1otVrz.mjs.map +0 -1
- package/dist/run-args-g74p8pEZ.cjs +0 -1
- package/dist/throttle-Da0OA8JT.d.cts.map +0 -1
- package/dist/throttle-ibiT6E4U.d.mts.map +0 -1
- package/dist/upload-strategy-negotiator-BuxPf1sa.mjs +0 -2
- package/dist/upload-strategy-negotiator-BuxPf1sa.mjs.map +0 -1
- package/dist/upload-strategy-negotiator-DfiQ0Fy0.cjs +0 -1
- package/dist/uploadista-error-Fsfvr2Bb.mjs.map +0 -1
- package/dist/websocket-Avz4T8YB.cjs +0 -1
- package/dist/websocket-CdgVhVJs.mjs +0 -2
- package/dist/websocket-CdgVhVJs.mjs.map +0 -1
package/dist/types/index.d.mts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import "../uploadista-error-
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
import "../stream-limiter-Bi7OTbRp.mjs";
|
|
5
|
-
export { BaseEventEmitter, BaseEventEmitterService, BaseKvStore, BaseKvStoreService, BufferedUploadFileDataStore, CircuitBreakerHealthSummary, CircuitBreakerStateData, CircuitBreakerStateValue, CircuitBreakerStats, CircuitBreakerStore, CircuitBreakerStoreService, ComponentHealth, DEFAULT_HEALTH_CHECK_CONFIG, DEFAULT_MULTIPART_PART_SIZE, DEFAULT_STREAMING_CONFIG, DataStore, DataStoreCapabilities, DataStoreConfig, DataStoreWriteOptions, DeadLetterQueueKVStore, DlqHealthSummary, EventBroadcaster, EventBroadcasterService, EventEmitter, FlowEventEmitter, FlowJobKVStore, HealthCheckConfig, HealthComponents, HealthResponse, HealthResponseFormat, HealthStatus, InputFile, JsonValue, KvStore, Middleware, MiddlewareContext, MiddlewareNext, MiddlewareService, MiddlewareServiceLive, StreamWriteOptions, StreamWriteResult, StreamingConfig, TypedEventEmitter, TypedKvStore, UploadEvent, UploadEventEmitter, UploadEventType, UploadFile, UploadFileDataStore, UploadFileDataStores, UploadFileDataStoresShape, UploadFileKVStore, UploadFileTraceContext, UploadStrategy, WebSocketConnection, WebSocketMessage, createDataStoreLayer, createInitialCircuitBreakerState, deadLetterQueueKvStore, eventToMessageSerializer, flowEventEmitter, flowJobKvStore, formatHealthAsText, getHealthResponseFormat, inputFileSchema, isDataStore, jsonSerializer, jsonValueSchema, traceContextSchema, uploadEventEmitter, uploadEventSchema, uploadFileKvStore, uploadFileSchema, webSocketMessageSchema };
|
|
1
|
+
import "../uploadista-error-LtiZn-R_.mjs";
|
|
2
|
+
import { $ as BaseKvStoreService, A as webSocketMessageSchema, An as JsonValue, B as DataStoreCapabilities, C as TypedEventEmitter, D as uploadEventEmitter, E as flowEventEmitter, F as EventBroadcasterService, Fn as uploadFileSchema, G as StreamingConfig, H as DataStoreWriteOptions, I as BufferedUploadFileDataStore, J as UploadFileDataStoresShape, Jn as CircuitBreakerStats, K as UploadFileDataStore, Kn as CircuitBreakerStateData, L as DEFAULT_MULTIPART_PART_SIZE, M as UploadEventType, Mn as UploadFileTraceContext, N as uploadEventSchema, Nn as jsonValueSchema, O as WebSocketConnection, P as EventBroadcaster, Pn as traceContextSchema, Q as BaseKvStore, R as DEFAULT_STREAMING_CONFIG, S as FlowEventEmitter, T as eventToMessageSerializer, U as StreamWriteOptions, V as DataStoreConfig, W as StreamWriteResult, X as createDataStoreLayer, Xn as CircuitBreakerStoreService, Y as UploadStrategy, Yn as CircuitBreakerStore, Z as isDataStore, Zn as createInitialCircuitBreakerState, _ as formatHealthAsText, a as MiddlewareServiceLive, at as UploadFileKVStore, b as BaseEventEmitterService, c as CircuitBreakerHealthSummary, ct as flowQueueKvStore, d as DlqHealthSummary, et as DeadLetterQueueKVStore, f as HealthCheckConfig, g as HealthStatus, h as HealthResponseFormat, i as MiddlewareService, it as TypedKvStore, j as UploadEvent, jn as UploadFile, k as WebSocketMessage, l as ComponentHealth, lt as jsonSerializer, m as HealthResponse, n as MiddlewareContext, nt as FlowQueueKVStore, o as InputFile, ot as deadLetterQueueKvStore, p as HealthComponents, q as UploadFileDataStores, qn as CircuitBreakerStateValue, r as MiddlewareNext, rt as KvStore, s as inputFileSchema, st as flowJobKvStore, t as Middleware, tt as FlowJobKVStore, u as DEFAULT_HEALTH_CHECK_CONFIG, ut as uploadFileKvStore, v as getHealthResponseFormat, w as UploadEventEmitter, x as EventEmitter, y as BaseEventEmitter, z as DataStore } from "../middleware-CYizzAhP.mjs";
|
|
3
|
+
export { BaseEventEmitter, BaseEventEmitterService, BaseKvStore, BaseKvStoreService, BufferedUploadFileDataStore, CircuitBreakerHealthSummary, CircuitBreakerStateData, CircuitBreakerStateValue, CircuitBreakerStats, CircuitBreakerStore, CircuitBreakerStoreService, ComponentHealth, DEFAULT_HEALTH_CHECK_CONFIG, DEFAULT_MULTIPART_PART_SIZE, DEFAULT_STREAMING_CONFIG, DataStore, DataStoreCapabilities, DataStoreConfig, DataStoreWriteOptions, DeadLetterQueueKVStore, DlqHealthSummary, EventBroadcaster, EventBroadcasterService, EventEmitter, FlowEventEmitter, FlowJobKVStore, FlowQueueKVStore, HealthCheckConfig, HealthComponents, HealthResponse, HealthResponseFormat, HealthStatus, InputFile, JsonValue, KvStore, Middleware, MiddlewareContext, MiddlewareNext, MiddlewareService, MiddlewareServiceLive, StreamWriteOptions, StreamWriteResult, StreamingConfig, TypedEventEmitter, TypedKvStore, UploadEvent, UploadEventEmitter, UploadEventType, UploadFile, UploadFileDataStore, UploadFileDataStores, UploadFileDataStoresShape, UploadFileKVStore, UploadFileTraceContext, UploadStrategy, WebSocketConnection, WebSocketMessage, createDataStoreLayer, createInitialCircuitBreakerState, deadLetterQueueKvStore, eventToMessageSerializer, flowEventEmitter, flowJobKvStore, flowQueueKvStore, formatHealthAsText, getHealthResponseFormat, inputFileSchema, isDataStore, jsonSerializer, jsonValueSchema, traceContextSchema, uploadEventEmitter, uploadEventSchema, uploadFileKvStore, uploadFileSchema, webSocketMessageSchema };
|
package/dist/types/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import"../uploadista-error-
|
|
1
|
+
import"../uploadista-error-DQ7V1FlX.mjs";import{A as e,B as t,C as n,D as r,E as i,F as a,I as o,L as s,M as c,N as l,O as u,P as d,R as f,S as p,T as m,_ as h,a as g,b as _,c as v,d as y,f as b,g as x,h as S,i as C,j as w,k as T,l as E,m as D,n as O,o as k,p as A,r as j,s as M,t as N,u as P,v as F,w as I,x as L,y as R,z}from"../websocket-DftnHFfN.mjs";export{P as BaseEventEmitterService,u as BaseKvStoreService,F as BufferedUploadFileDataStore,z as CircuitBreakerStoreService,M as DEFAULT_HEALTH_CHECK_CONFIG,R as DEFAULT_MULTIPART_PART_SIZE,_ as DEFAULT_STREAMING_CONFIG,T as DeadLetterQueueKVStore,h as EventBroadcasterService,y as FlowEventEmitter,e as FlowJobKVStore,w as FlowQueueKVStore,C as MiddlewareService,g as MiddlewareServiceLive,b as TypedEventEmitter,c as TypedKvStore,A as UploadEventEmitter,O as UploadEventType,L as UploadFileDataStore,p as UploadFileDataStores,l as UploadFileKVStore,n as createDataStoreLayer,t as createInitialCircuitBreakerState,d as deadLetterQueueKvStore,D as eventToMessageSerializer,S as flowEventEmitter,a as flowJobKvStore,o as flowQueueKvStore,v as formatHealthAsText,E as getHealthResponseFormat,k as inputFileSchema,I as isDataStore,s as jsonSerializer,m as jsonValueSchema,i as traceContextSchema,x as uploadEventEmitter,j as uploadEventSchema,f as uploadFileKvStore,r as uploadFileSchema,N as webSocketMessageSchema};
|
package/dist/upload/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`}),require(`../websocket-
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`}),require(`../websocket-Br0ijEZA.cjs`),require(`../uploadista-error-CZx1JU_L.cjs`);const e=require(`../upload-strategy-negotiator-BR_o1Ez8.cjs`);require(`../checksum-BrjQ8GJL.cjs`),require(`../stream-limiter-B-Y0DTgA.cjs`),exports.UploadEngine=e.n,exports.UploadStrategyNegotiator=e.t,exports.compareMimeTypes=e.s,exports.createUploadEngine=e.r,exports.detectMimeType=e.c,exports.uploadEngine=e.i;
|
package/dist/upload/index.d.cts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as UploadEngineOptions, c as uploadEngine, i as UploadEngine, l as compareMimeTypes, n as UploadStrategyNegotiator, o as UploadEngineShape, r as UploadStrategyOptions, s as createUploadEngine, t as NegotiatedStrategy, u as detectMimeType } from "../upload-strategy-negotiator-0-dpNIce.cjs";
|
|
2
2
|
export { NegotiatedStrategy, UploadEngine, UploadEngineOptions, UploadEngineShape, UploadStrategyNegotiator, UploadStrategyOptions, compareMimeTypes, createUploadEngine, detectMimeType, uploadEngine };
|
package/dist/upload/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import "../uploadista-error-
|
|
2
|
-
import
|
|
3
|
-
import "../
|
|
4
|
-
import "../
|
|
1
|
+
import "../uploadista-error-LtiZn-R_.mjs";
|
|
2
|
+
import "../middleware-CYizzAhP.mjs";
|
|
3
|
+
import { a as UploadEngineOptions, c as uploadEngine, i as UploadEngine, l as compareMimeTypes, n as UploadStrategyNegotiator, o as UploadEngineShape, r as UploadStrategyOptions, s as createUploadEngine, t as NegotiatedStrategy, u as detectMimeType } from "../upload-strategy-negotiator-CEnlfVgJ.mjs";
|
|
4
|
+
import "../generate-id-DuZwLm4m.mjs";
|
|
5
5
|
export { NegotiatedStrategy, UploadEngine, UploadEngineOptions, UploadEngineShape, UploadStrategyNegotiator, UploadStrategyOptions, compareMimeTypes, createUploadEngine, detectMimeType, uploadEngine };
|
package/dist/upload/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import"../uploadista-error-
|
|
1
|
+
import"../uploadista-error-DQ7V1FlX.mjs";import"../websocket-DftnHFfN.mjs";import{c as e,i as t,n,r,s as i,t as a}from"../upload-strategy-negotiator-C9MeoOnW.mjs";import"../checksum-BRjFmTRk.mjs";import"../stream-limiter-CvDuNIyd.mjs";export{n as UploadEngine,a as UploadStrategyNegotiator,i as compareMimeTypes,r as createUploadEngine,e as detectMimeType,t as uploadEngine};
|
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
import { n as UploadistaError } from "./uploadista-error-eZtG4iyf.cjs";
|
|
2
|
+
import { B as DataStoreCapabilities, G as StreamingConfig, O as WebSocketConnection, Y as UploadStrategy, at as UploadFileKVStore, j as UploadEvent, jn as UploadFile, o as InputFile, q as UploadFileDataStores, rt as KvStore, t as Middleware, w as UploadEventEmitter, x as EventEmitter, z as DataStore } from "./middleware-BghazxzH.cjs";
|
|
3
|
+
import { a as GenerateIdShape, t as GenerateId } from "./generate-id-BAMRQzMr.cjs";
|
|
4
|
+
import { Context, Effect, Layer, Stream } from "effect";
|
|
5
|
+
|
|
6
|
+
//#region src/upload/mime.d.ts
|
|
7
|
+
/**
|
|
8
|
+
* Detect MIME type from buffer using magic bytes (file signatures).
|
|
9
|
+
* Supports a wide range of common file types including images, videos, audio, documents, and archives.
|
|
10
|
+
*
|
|
11
|
+
* @param buffer - File content as Uint8Array
|
|
12
|
+
* @param filename - Optional filename for extension-based fallback
|
|
13
|
+
* @returns Detected MIME type or "application/octet-stream" if unknown
|
|
14
|
+
*/
|
|
15
|
+
declare const detectMimeType: (buffer: Uint8Array, filename?: string) => string;
|
|
16
|
+
/**
|
|
17
|
+
* Compare two MIME types with lenient matching.
|
|
18
|
+
* Matches on major type (e.g., "image/*") to allow for minor variations.
|
|
19
|
+
*
|
|
20
|
+
* @param declared - MIME type provided by client
|
|
21
|
+
* @param detected - MIME type detected from file content
|
|
22
|
+
* @returns true if MIME types are compatible
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* compareMimeTypes("image/png", "image/apng") // true
|
|
26
|
+
* compareMimeTypes("image/jpeg", "image/png") // true (both images)
|
|
27
|
+
* compareMimeTypes("image/png", "application/pdf") // false
|
|
28
|
+
*/
|
|
29
|
+
declare function compareMimeTypes(declared: string, detected: string): boolean;
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/upload/upload-engine.d.ts
|
|
32
|
+
/**
|
|
33
|
+
* Legacy configuration options for UploadEngine.
|
|
34
|
+
*
|
|
35
|
+
* @deprecated Use Effect Layers instead of this configuration object.
|
|
36
|
+
* This type is kept for backward compatibility.
|
|
37
|
+
*
|
|
38
|
+
* @property dataStore - DataStore instance or factory function
|
|
39
|
+
* @property kvStore - KV store for upload metadata
|
|
40
|
+
* @property eventEmitter - Event emitter for upload progress
|
|
41
|
+
* @property generateId - Optional ID generator (defaults to UUID)
|
|
42
|
+
* @property middlewares - Optional request middlewares
|
|
43
|
+
* @property withTracing - Enable Effect tracing for debugging
|
|
44
|
+
*/
|
|
45
|
+
type UploadEngineOptions = {
|
|
46
|
+
dataStore: ((storageId: string) => Promise<DataStore<UploadFile>>) | DataStore<UploadFile>;
|
|
47
|
+
kvStore: KvStore<UploadFile>;
|
|
48
|
+
eventEmitter: EventEmitter<UploadEvent>;
|
|
49
|
+
generateId?: GenerateIdShape;
|
|
50
|
+
middlewares?: Middleware[];
|
|
51
|
+
withTracing?: boolean;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* UploadEngine service interface.
|
|
55
|
+
*
|
|
56
|
+
* This is the core upload handling service that provides all file upload operations.
|
|
57
|
+
* It manages upload lifecycle, resumable uploads, progress tracking, and storage integration.
|
|
58
|
+
*
|
|
59
|
+
* All operations return Effect types for composable, type-safe error handling.
|
|
60
|
+
*
|
|
61
|
+
* @property createUpload - Initiates a new upload and returns metadata
|
|
62
|
+
* @property uploadChunk - Uploads a chunk of data for an existing upload
|
|
63
|
+
* @property getCapabilities - Returns storage backend capabilities
|
|
64
|
+
* @property upload - Complete upload in one operation (create + upload data)
|
|
65
|
+
* @property uploadFromUrl - Uploads a file from a remote URL
|
|
66
|
+
* @property getUpload - Retrieves upload metadata by ID
|
|
67
|
+
* @property read - Reads the complete uploaded file data
|
|
68
|
+
* @property delete - Deletes an upload and its data
|
|
69
|
+
* @property subscribeToUploadEvents - Subscribes WebSocket to upload progress events
|
|
70
|
+
* @property unsubscribeFromUploadEvents - Unsubscribes from upload events
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* // Basic upload flow
|
|
75
|
+
* const program = Effect.gen(function* () {
|
|
76
|
+
* const server = yield* UploadEngine;
|
|
77
|
+
*
|
|
78
|
+
* // 1. Create upload
|
|
79
|
+
* const inputFile: InputFile = {
|
|
80
|
+
* storageId: "s3-production",
|
|
81
|
+
* size: 1024000,
|
|
82
|
+
* type: "image/jpeg",
|
|
83
|
+
* fileName: "photo.jpg"
|
|
84
|
+
* };
|
|
85
|
+
* const upload = yield* server.createUpload(inputFile, "client123");
|
|
86
|
+
*
|
|
87
|
+
* // 2. Upload chunks
|
|
88
|
+
* const chunk = new ReadableStream(...);
|
|
89
|
+
* const updated = yield* server.uploadChunk(upload.id, "client123", chunk);
|
|
90
|
+
*
|
|
91
|
+
* // 3. Read the uploaded file
|
|
92
|
+
* const data = yield* server.read(upload.id, "client123");
|
|
93
|
+
*
|
|
94
|
+
* return upload;
|
|
95
|
+
* });
|
|
96
|
+
*
|
|
97
|
+
* // Upload with WebSocket progress tracking
|
|
98
|
+
* const uploadWithProgress = Effect.gen(function* () {
|
|
99
|
+
* const server = yield* UploadEngine;
|
|
100
|
+
*
|
|
101
|
+
* // Subscribe to progress events
|
|
102
|
+
* yield* server.subscribeToUploadEvents(uploadId, websocket);
|
|
103
|
+
*
|
|
104
|
+
* // Upload (events will be emitted automatically)
|
|
105
|
+
* const result = yield* server.upload(inputFile, clientId, stream);
|
|
106
|
+
*
|
|
107
|
+
* // Unsubscribe when done
|
|
108
|
+
* yield* server.unsubscribeFromUploadEvents(uploadId);
|
|
109
|
+
*
|
|
110
|
+
* return result;
|
|
111
|
+
* });
|
|
112
|
+
*
|
|
113
|
+
* // Upload from URL
|
|
114
|
+
* const urlUpload = Effect.gen(function* () {
|
|
115
|
+
* const server = yield* UploadEngine;
|
|
116
|
+
*
|
|
117
|
+
* const inputFile: InputFile = {
|
|
118
|
+
* storageId: "s3-production",
|
|
119
|
+
* size: 0, // Unknown initially
|
|
120
|
+
* type: "image/png",
|
|
121
|
+
* fileName: "remote-image.png"
|
|
122
|
+
* };
|
|
123
|
+
*
|
|
124
|
+
* const upload = yield* server.uploadFromUrl(
|
|
125
|
+
* inputFile,
|
|
126
|
+
* "client123",
|
|
127
|
+
* "https://example.com/image.png"
|
|
128
|
+
* );
|
|
129
|
+
*
|
|
130
|
+
* return upload;
|
|
131
|
+
* });
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
type UploadEngineShape = {
|
|
135
|
+
createUpload: (inputFile: InputFile, clientId: string | null) => Effect.Effect<UploadFile, UploadistaError>;
|
|
136
|
+
uploadChunk: (uploadId: string, clientId: string | null, chunk: ReadableStream) => Effect.Effect<UploadFile, UploadistaError>;
|
|
137
|
+
getCapabilities: (storageId: string, clientId: string | null) => Effect.Effect<DataStoreCapabilities, UploadistaError>;
|
|
138
|
+
upload: (file: InputFile, clientId: string | null, stream: ReadableStream) => Effect.Effect<UploadFile, UploadistaError>;
|
|
139
|
+
uploadFromUrl: (inputFile: InputFile, clientId: string | null, url: string) => Effect.Effect<UploadFile, UploadistaError>;
|
|
140
|
+
getUpload: (uploadId: string) => Effect.Effect<UploadFile, UploadistaError>;
|
|
141
|
+
/**
|
|
142
|
+
* Reads the complete uploaded file data as bytes (buffered mode).
|
|
143
|
+
* For large files, consider using readStream() for memory efficiency.
|
|
144
|
+
*/
|
|
145
|
+
read: (uploadId: string, clientId: string | null) => Effect.Effect<Uint8Array, UploadistaError>;
|
|
146
|
+
/**
|
|
147
|
+
* Reads file content as a stream of chunks for memory-efficient processing.
|
|
148
|
+
* Falls back to buffered read if the underlying DataStore doesn't support streaming.
|
|
149
|
+
*
|
|
150
|
+
* @param uploadId - The unique identifier of the upload to read
|
|
151
|
+
* @param clientId - Client identifier for multi-tenant routing
|
|
152
|
+
* @param config - Optional streaming configuration (chunk size)
|
|
153
|
+
* @returns An Effect that resolves to a Stream of byte chunks
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```typescript
|
|
157
|
+
* const server = yield* UploadEngine;
|
|
158
|
+
* const stream = yield* server.readStream(uploadId, clientId, { chunkSize: 65536 });
|
|
159
|
+
* // Process stream chunk by chunk with bounded memory
|
|
160
|
+
* yield* Stream.runForEach(stream, (chunk) => processChunk(chunk));
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
readStream: (uploadId: string, clientId: string | null, config?: StreamingConfig) => Effect.Effect<Stream.Stream<Uint8Array, UploadistaError>, UploadistaError>;
|
|
164
|
+
/**
|
|
165
|
+
* Uploads file content from a stream with unknown final size.
|
|
166
|
+
* Creates upload with deferred length, streams content to storage,
|
|
167
|
+
* and updates the upload record with final size when complete.
|
|
168
|
+
*
|
|
169
|
+
* Falls back to buffered upload if the underlying DataStore
|
|
170
|
+
* doesn't support streaming writes.
|
|
171
|
+
*
|
|
172
|
+
* @param file - Input file configuration (size is optional)
|
|
173
|
+
* @param clientId - Client identifier for multi-tenant routing
|
|
174
|
+
* @param stream - Effect Stream of byte chunks to upload
|
|
175
|
+
* @returns The completed UploadFile with final size
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```typescript
|
|
179
|
+
* const server = yield* UploadEngine;
|
|
180
|
+
* const result = yield* server.uploadStream(
|
|
181
|
+
* {
|
|
182
|
+
* storageId: "s3-production",
|
|
183
|
+
* type: "image/webp",
|
|
184
|
+
* uploadLengthDeferred: true,
|
|
185
|
+
* fileName: "optimized.webp",
|
|
186
|
+
* },
|
|
187
|
+
* clientId,
|
|
188
|
+
* transformedStream,
|
|
189
|
+
* );
|
|
190
|
+
* console.log(`Uploaded ${result.size} bytes`);
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
193
|
+
uploadStream: (file: Omit<InputFile, "size"> & {
|
|
194
|
+
size?: number;
|
|
195
|
+
sizeHint?: number;
|
|
196
|
+
}, clientId: string | null, stream: Stream.Stream<Uint8Array, UploadistaError>) => Effect.Effect<UploadFile, UploadistaError>;
|
|
197
|
+
delete: (uploadId: string, clientId: string | null) => Effect.Effect<void, UploadistaError>;
|
|
198
|
+
subscribeToUploadEvents: (uploadId: string, connection: WebSocketConnection) => Effect.Effect<void, UploadistaError>;
|
|
199
|
+
unsubscribeFromUploadEvents: (uploadId: string) => Effect.Effect<void, UploadistaError>;
|
|
200
|
+
};
|
|
201
|
+
declare const UploadEngine_base: Context.TagClass<UploadEngine, "UploadEngine", UploadEngineShape>;
|
|
202
|
+
/**
|
|
203
|
+
* Effect-TS context tag for the UploadEngine service.
|
|
204
|
+
*
|
|
205
|
+
* Use this tag to access the UploadEngine in an Effect context.
|
|
206
|
+
* The server must be provided via a Layer or dependency injection.
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* ```typescript
|
|
210
|
+
* // Access UploadEngine in an Effect
|
|
211
|
+
* const uploadEffect = Effect.gen(function* () {
|
|
212
|
+
* const server = yield* UploadEngine;
|
|
213
|
+
* const upload = yield* server.createUpload(inputFile, clientId);
|
|
214
|
+
* return upload;
|
|
215
|
+
* });
|
|
216
|
+
*
|
|
217
|
+
* // Provide UploadEngine layer
|
|
218
|
+
* const program = uploadEffect.pipe(
|
|
219
|
+
* Effect.provide(uploadEngine),
|
|
220
|
+
* Effect.provide(uploadFileKvStore),
|
|
221
|
+
* Effect.provide(dataStoreLayer),
|
|
222
|
+
* Effect.provide(eventEmitterLayer)
|
|
223
|
+
* );
|
|
224
|
+
* ```
|
|
225
|
+
*/
|
|
226
|
+
declare class UploadEngine extends UploadEngine_base {}
|
|
227
|
+
/**
|
|
228
|
+
* Creates the UploadEngine implementation.
|
|
229
|
+
*
|
|
230
|
+
* This function constructs the UploadEngine service by composing all required
|
|
231
|
+
* dependencies (KV store, data stores, event emitter, ID generator). It implements
|
|
232
|
+
* all upload operations defined in UploadEngineShape.
|
|
233
|
+
*
|
|
234
|
+
* The server automatically handles:
|
|
235
|
+
* - Upload lifecycle management (create, resume, complete)
|
|
236
|
+
* - Progress tracking and event emission
|
|
237
|
+
* - Storage backend routing based on storageId
|
|
238
|
+
* - Error handling with proper UploadistaError types
|
|
239
|
+
*
|
|
240
|
+
* @returns An Effect that yields the UploadEngineShape implementation
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* ```typescript
|
|
244
|
+
* // Create a custom UploadEngine layer
|
|
245
|
+
* const myUploadEngine = Layer.effect(
|
|
246
|
+
* UploadEngine,
|
|
247
|
+
* createUploadEngine()
|
|
248
|
+
* );
|
|
249
|
+
*
|
|
250
|
+
* // Use in a program
|
|
251
|
+
* const program = Effect.gen(function* () {
|
|
252
|
+
* const server = yield* UploadEngine;
|
|
253
|
+
* // Use server operations...
|
|
254
|
+
* }).pipe(Effect.provide(myUploadEngine));
|
|
255
|
+
* ```
|
|
256
|
+
*/
|
|
257
|
+
declare function createUploadEngine(): Effect.Effect<{
|
|
258
|
+
upload: (inputFile: InputFile, clientId: string | null, stream: ReadableStream) => Effect.Effect<UploadFile, UploadistaError, never>;
|
|
259
|
+
uploadFromUrl: (inputFile: InputFile, clientId: string | null, url: string) => Effect.Effect<UploadFile, UploadistaError, never>;
|
|
260
|
+
createUpload: (inputFile: InputFile, clientId: string | null) => Effect.Effect<UploadFile, UploadistaError, never>;
|
|
261
|
+
uploadChunk: (uploadId: string, clientId: string | null, chunk: ReadableStream) => Effect.Effect<UploadFile, UploadistaError, never>;
|
|
262
|
+
getUpload: (uploadId: string) => Effect.Effect<UploadFile, UploadistaError, never>;
|
|
263
|
+
read: (uploadId: string, clientId: string | null) => Effect.Effect<Uint8Array<ArrayBufferLike>, UploadistaError, never>;
|
|
264
|
+
readStream: (uploadId: string, clientId: string | null, config?: StreamingConfig) => Effect.Effect<Stream.Stream<Uint8Array<ArrayBufferLike>, UploadistaError, never>, UploadistaError, never>;
|
|
265
|
+
uploadStream: (file: Omit<InputFile, "size"> & {
|
|
266
|
+
size?: number;
|
|
267
|
+
sizeHint?: number;
|
|
268
|
+
}, clientId: string | null, stream: Stream.Stream<Uint8Array, UploadistaError>) => Effect.Effect<UploadFile, UploadistaError, never>;
|
|
269
|
+
delete: (uploadId: string, clientId: string | null) => Effect.Effect<void, UploadistaError, never>;
|
|
270
|
+
getCapabilities: (storageId: string, clientId: string | null) => Effect.Effect<DataStoreCapabilities, UploadistaError, never>;
|
|
271
|
+
subscribeToUploadEvents: (uploadId: string, connection: WebSocketConnection) => Effect.Effect<void, UploadistaError, never>;
|
|
272
|
+
unsubscribeFromUploadEvents: (uploadId: string) => Effect.Effect<void, UploadistaError, never>;
|
|
273
|
+
}, never, UploadFileDataStores | UploadFileKVStore | UploadEventEmitter | GenerateId>;
|
|
274
|
+
/**
|
|
275
|
+
* Pre-built UploadEngine Effect Layer.
|
|
276
|
+
*
|
|
277
|
+
* This layer provides a ready-to-use UploadEngine implementation that can be
|
|
278
|
+
* composed with other layers to build a complete upload system.
|
|
279
|
+
*
|
|
280
|
+
* Required dependencies:
|
|
281
|
+
* - UploadFileKVStore: For storing upload metadata
|
|
282
|
+
* - UploadFileDataStores: For routing to storage backends
|
|
283
|
+
* - UploadEventEmitter: For progress events
|
|
284
|
+
* - GenerateId: For creating upload IDs
|
|
285
|
+
*
|
|
286
|
+
* @example
|
|
287
|
+
* ```typescript
|
|
288
|
+
* // Compose a complete upload system
|
|
289
|
+
* const fullUploadSystem = Layer.mergeAll(
|
|
290
|
+
* uploadEngine,
|
|
291
|
+
* uploadFileKvStore,
|
|
292
|
+
* dataStoreLayer,
|
|
293
|
+
* uploadEventEmitter,
|
|
294
|
+
* generateIdLayer
|
|
295
|
+
* );
|
|
296
|
+
*
|
|
297
|
+
* // Use in application
|
|
298
|
+
* const app = Effect.gen(function* () {
|
|
299
|
+
* const server = yield* UploadEngine;
|
|
300
|
+
* // Perform uploads...
|
|
301
|
+
* }).pipe(Effect.provide(fullUploadSystem));
|
|
302
|
+
* ```
|
|
303
|
+
*/
|
|
304
|
+
declare const uploadEngine: Layer.Layer<UploadEngine, never, UploadFileDataStores | UploadFileKVStore | UploadEventEmitter | GenerateId>;
|
|
305
|
+
//#endregion
|
|
306
|
+
//#region src/upload/upload-strategy-negotiator.d.ts
|
|
307
|
+
/**
|
|
308
|
+
* Configuration options for upload strategy negotiation.
|
|
309
|
+
*
|
|
310
|
+
* @property fileSize - Size of the file to be uploaded in bytes
|
|
311
|
+
* @property preferredStrategy - Preferred upload strategy (single, parallel, resumable)
|
|
312
|
+
* @property preferredChunkSize - Preferred chunk size in bytes
|
|
313
|
+
* @property parallelUploads - Number of parallel upload connections
|
|
314
|
+
* @property minChunkSizeForParallel - Minimum file size to consider parallel uploads
|
|
315
|
+
*/
|
|
316
|
+
type UploadStrategyOptions = {
|
|
317
|
+
fileSize: number;
|
|
318
|
+
preferredStrategy?: UploadStrategy;
|
|
319
|
+
preferredChunkSize?: number;
|
|
320
|
+
parallelUploads?: number;
|
|
321
|
+
minChunkSizeForParallel?: number;
|
|
322
|
+
};
|
|
323
|
+
/**
|
|
324
|
+
* Result of upload strategy negotiation.
|
|
325
|
+
*
|
|
326
|
+
* @property strategy - The negotiated upload strategy
|
|
327
|
+
* @property chunkSize - The negotiated chunk size in bytes
|
|
328
|
+
* @property parallelUploads - The negotiated number of parallel uploads
|
|
329
|
+
* @property reasoning - Array of reasoning strings explaining the decisions
|
|
330
|
+
* @property warnings - Array of warning messages about adjustments made
|
|
331
|
+
*/
|
|
332
|
+
type NegotiatedStrategy = {
|
|
333
|
+
strategy: UploadStrategy;
|
|
334
|
+
chunkSize: number;
|
|
335
|
+
parallelUploads: number;
|
|
336
|
+
reasoning: string[];
|
|
337
|
+
warnings: string[];
|
|
338
|
+
};
|
|
339
|
+
/**
|
|
340
|
+
* Negotiates the optimal upload strategy based on data store capabilities and file characteristics.
|
|
341
|
+
*
|
|
342
|
+
* This class analyzes data store capabilities, file size, and user preferences to determine
|
|
343
|
+
* the best upload strategy (single, parallel, resumable) and optimal parameters like chunk size
|
|
344
|
+
* and parallel connection count.
|
|
345
|
+
*
|
|
346
|
+
* The negotiator considers:
|
|
347
|
+
* - Data store capabilities (parallel uploads, resumable uploads, concatenation)
|
|
348
|
+
* - File size and chunk size constraints
|
|
349
|
+
* - User preferences and requirements
|
|
350
|
+
* - Performance optimization opportunities
|
|
351
|
+
*
|
|
352
|
+
* @example
|
|
353
|
+
* ```typescript
|
|
354
|
+
* // Create negotiator for S3 data store
|
|
355
|
+
* const negotiator = new UploadStrategyNegotiator(
|
|
356
|
+
* s3Capabilities,
|
|
357
|
+
* (strategy) => s3Capabilities.supportsStrategy(strategy)
|
|
358
|
+
* );
|
|
359
|
+
*
|
|
360
|
+
* // Negotiate strategy for large file
|
|
361
|
+
* const result = negotiator.negotiateStrategy({
|
|
362
|
+
* fileSize: 100_000_000, // 100MB
|
|
363
|
+
* preferredStrategy: "parallel",
|
|
364
|
+
* preferredChunkSize: 5_000_000, // 5MB chunks
|
|
365
|
+
* parallelUploads: 4
|
|
366
|
+
* });
|
|
367
|
+
*
|
|
368
|
+
* console.log(result.strategy); // "parallel"
|
|
369
|
+
* console.log(result.chunkSize); // 5_000_000
|
|
370
|
+
* console.log(result.reasoning); // ["Using preferred strategy: parallel", ...]
|
|
371
|
+
* ```
|
|
372
|
+
*/
|
|
373
|
+
declare class UploadStrategyNegotiator {
|
|
374
|
+
private capabilities;
|
|
375
|
+
private validateUploadStrategy;
|
|
376
|
+
/**
|
|
377
|
+
* Creates a new upload strategy negotiator.
|
|
378
|
+
*
|
|
379
|
+
* @param capabilities - Data store capabilities and constraints
|
|
380
|
+
* @param validateUploadStrategy - Function to validate if a strategy is supported
|
|
381
|
+
*/
|
|
382
|
+
constructor(capabilities: DataStoreCapabilities, validateUploadStrategy: (strategy: UploadStrategy) => boolean);
|
|
383
|
+
/**
|
|
384
|
+
* Negotiates the optimal upload strategy based on options and data store capabilities.
|
|
385
|
+
*
|
|
386
|
+
* This method analyzes the provided options and data store capabilities to determine
|
|
387
|
+
* the best upload strategy, chunk size, and parallel upload settings. It considers
|
|
388
|
+
* user preferences, file size, and data store constraints to make optimal decisions.
|
|
389
|
+
*
|
|
390
|
+
* The negotiation process:
|
|
391
|
+
* 1. Validates preferred strategy against data store capabilities
|
|
392
|
+
* 2. Automatically selects strategy based on file size and capabilities
|
|
393
|
+
* 3. Adjusts chunk size to fit within data store constraints
|
|
394
|
+
* 4. Validates parallel upload settings
|
|
395
|
+
* 5. Ensures final strategy is supported by the data store
|
|
396
|
+
*
|
|
397
|
+
* @param options - Upload strategy options including file size and preferences
|
|
398
|
+
* @returns Negotiated strategy with reasoning and warnings
|
|
399
|
+
*
|
|
400
|
+
* @example
|
|
401
|
+
* ```typescript
|
|
402
|
+
* const result = negotiator.negotiateStrategy({
|
|
403
|
+
* fileSize: 50_000_000, // 50MB
|
|
404
|
+
* preferredStrategy: "parallel",
|
|
405
|
+
* preferredChunkSize: 5_000_000, // 5MB
|
|
406
|
+
* parallelUploads: 3
|
|
407
|
+
* });
|
|
408
|
+
*
|
|
409
|
+
* console.log(result.strategy); // "parallel"
|
|
410
|
+
* console.log(result.chunkSize); // 5_000_000
|
|
411
|
+
* console.log(result.parallelUploads); // 3
|
|
412
|
+
* console.log(result.reasoning); // ["Using preferred strategy: parallel", ...]
|
|
413
|
+
* console.log(result.warnings); // [] (no warnings)
|
|
414
|
+
* ```
|
|
415
|
+
*/
|
|
416
|
+
negotiateStrategy(options: UploadStrategyOptions): NegotiatedStrategy;
|
|
417
|
+
/**
|
|
418
|
+
* Gets the data store capabilities used by this negotiator.
|
|
419
|
+
*
|
|
420
|
+
* @returns The data store capabilities and constraints
|
|
421
|
+
*/
|
|
422
|
+
getDataStoreCapabilities(): DataStoreCapabilities;
|
|
423
|
+
/**
|
|
424
|
+
* Validates upload strategy configuration against data store capabilities.
|
|
425
|
+
*
|
|
426
|
+
* This method checks if the provided configuration is valid for the current
|
|
427
|
+
* data store capabilities without performing the actual negotiation. It's
|
|
428
|
+
* useful for pre-validation before attempting to negotiate a strategy.
|
|
429
|
+
*
|
|
430
|
+
* @param options - Upload strategy options to validate
|
|
431
|
+
* @returns Validation result with validity flag and error messages
|
|
432
|
+
*
|
|
433
|
+
* @example
|
|
434
|
+
* ```typescript
|
|
435
|
+
* const validation = negotiator.validateConfiguration({
|
|
436
|
+
* fileSize: 10_000_000,
|
|
437
|
+
* preferredStrategy: "parallel",
|
|
438
|
+
* preferredChunkSize: 1_000_000,
|
|
439
|
+
* parallelUploads: 5
|
|
440
|
+
* });
|
|
441
|
+
*
|
|
442
|
+
* if (!validation.valid) {
|
|
443
|
+
* console.log("Configuration errors:", validation.errors);
|
|
444
|
+
* // Handle validation errors
|
|
445
|
+
* }
|
|
446
|
+
* ```
|
|
447
|
+
*/
|
|
448
|
+
validateConfiguration(options: UploadStrategyOptions): {
|
|
449
|
+
valid: boolean;
|
|
450
|
+
errors: string[];
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
//#endregion
|
|
454
|
+
export { UploadEngineOptions as a, uploadEngine as c, UploadEngine as i, compareMimeTypes as l, UploadStrategyNegotiator as n, UploadEngineShape as o, UploadStrategyOptions as r, createUploadEngine as s, NegotiatedStrategy as t, detectMimeType as u };
|
|
455
|
+
//# sourceMappingURL=upload-strategy-negotiator-0-dpNIce.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"upload-strategy-negotiator-0-dpNIce.d.cts","names":[],"sources":["../src/upload/mime.ts","../src/upload/upload-engine.ts","../src/upload/upload-strategy-negotiator.ts"],"mappings":";;;;;;;;;;;;AA+BA;;cAAa,cAAA,GACX,MAAA,EAAQ,UAAA,EACR,QAAA;;;;;;;AAqYF;;;;;;;iBAAgB,gBAAA,CAAiB,QAAA,UAAkB,QAAA;;;;AAvYnD;;;;;;;;;AAuYA;;;KChYY,mBAAA;EACV,SAAA,IACM,SAAA,aAAsB,OAAA,CAAQ,SAAA,CAAU,UAAA,MAC1C,SAAA,CAAU,UAAA;EACd,OAAA,EAAS,OAAA,CAAQ,UAAA;EACjB,YAAA,EAAc,YAAA,CAAa,WAAA;EAC3B,UAAA,GAAa,eAAA;EACb,WAAA,GAAc,UAAA;EACd,WAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoFF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KAAY,iBAAA;EACV,YAAA,GACE,SAAA,EAAW,SAAA,EACX,QAAA,oBACG,MAAA,CAAO,MAAA,CAAO,UAAA,EAAY,eAAA;EAC/B,WAAA,GACE,QAAA,UACA,QAAA,iBACA,KAAA,EAAO,cAAA,KACJ,MAAA,CAAO,MAAA,CAAO,UAAA,EAAY,eAAA;EAC/B,eAAA,GACE,SAAA,UACA,QAAA,oBACG,MAAA,CAAO,MAAA,CAAO,qBAAA,EAAuB,eAAA;EAC1C,MAAA,GACE,IAAA,EAAM,SAAA,EACN,QAAA,iBACA,MAAA,EAAQ,cAAA,KACL,MAAA,CAAO,MAAA,CAAO,UAAA,EAAY,eAAA;EAC/B,aAAA,GACE,SAAA,EAAW,SAAA,EACX,QAAA,iBACA,GAAA,aACG,MAAA,CAAO,MAAA,CAAO,UAAA,EAAY,eAAA;EAC/B,SAAA,GAAY,QAAA,aAAqB,MAAA,CAAO,MAAA,CAAO,UAAA,EAAY,eAAA;EArBzD;;;;EA0BF,IAAA,GACE,QAAA,UACA,QAAA,oBACG,MAAA,CAAO,MAAA,CAAO,UAAA,EAAY,eAAA;EA3B/B;;;;;;;;;;;;;;;;;EA6CA,UAAA,GACE,QAAA,UACA,QAAA,iBACA,MAAA,GAAS,eAAA,KACN,MAAA,CAAO,MAAA,CACV,MAAA,CAAO,MAAA,CAAO,UAAA,EAAY,eAAA,GAC1B,eAAA;EAzCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAwEF,YAAA,GACE,IAAA,EAAM,IAAA,CAAK,SAAA;IAAuB,IAAA;IAAe,QAAA;EAAA,GACjD,QAAA,iBACA,MAAA,EAAQ,MAAA,CAAO,MAAA,CAAO,UAAA,EAAY,eAAA,MAC/B,MAAA,CAAO,MAAA,CAAO,UAAA,EAAY,eAAA;EAC/B,MAAA,GACE,QAAA,UACA,QAAA,oBACG,MAAA,CAAO,MAAA,OAAa,eAAA;EACzB,uBAAA,GACE,QAAA,UACA,UAAA,EAAY,mBAAA,KACT,MAAA,CAAO,MAAA,OAAa,eAAA;EACzB,2BAAA,GACE,QAAA,aACG,MAAA,CAAO,MAAA,OAAa,eAAA;AAAA;AAAA,cACzB,iBAAA;;;;;;;;;;;;;;;;;;;;;;;;;cA0BW,YAAA,SAAqB,iBAAA;;;;;;;;;;;;;;;AA1BhC;;;;;;;;;;;;;;AA0BF;;iBAmCgB,kBAAA,CAAA,GAAkB,MAAA,CAAA,MAAA;sBASf,SAAA,EAAS,QAAA,iBACG,MAAA,EACf,cAAA,KAAc,MAAA,CAAA,MAAA,CAAA,UAAA,EAAA,eAAA;6BAgBX,SAAA,EAAS,QAAA,iBACG,GAAA,aACZ,MAAA,CAAA,MAAA,CAAA,UAAA,EAAA,eAAA;4BA8Ba,SAAA,EAAS,QAAA,oBAAyB,MAAA,CAAA,MAAA,CAAA,UAAA,EAAA,eAAA;kCAW1C,QAAA,iBACO,KAAA,EAChB,cAAA,KAAc,MAAA,CAAA,MAAA,CAAA,UAAA,EAAA,eAAA;mCAUK,MAAA,CAAA,MAAA,CAAA,UAAA,EAAA,eAAA;2BAKL,QAAA,oBAAyB,MAAA,CAAA,MAAA,CAAA,UAAA,CAAA,eAAA,GAAA,eAAA;iCAU9B,QAAA,iBACO,MAAA,GACd,eAAA,KAAe,MAAA,CAAA,MAAA,CAAA,MAAA,CAAA,MAAA,CAAA,UAAA,CAAA,eAAA,GAAA,eAAA,UAAA,eAAA;uBA2BlB,IAAA,CAAK,SAAA;IAAuB,IAAA;IAAe,QAAA;EAAA,GAAmB,QAAA,iBAC7C,MAAA,EACf,MAAA,CAAO,MAAA,CAAO,UAAA,EAAY,eAAA,MAAgB,MAAA,CAAA,MAAA,CAAA,UAAA,EAAA,eAAA;6BA4I3B,QAAA,oBAAyB,MAAA,CAAA,MAAA,OAAA,eAAA;uCAWf,QAAA,oBAAyB,MAAA,CAAA,MAAA,CAAA,qBAAA,EAAA,eAAA;8CAS1C,UAAA,EACJ,mBAAA,KAAmB,MAAA,CAAA,MAAA,OAAA,eAAA;qDAKa,MAAA,CAAA,MAAA,OAAA,eAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAsCvC,YAAA,EAAY,KAAA,CAAA,KAAA,CAAA,YAAA,SAAA,oBAAA,GAAA,iBAAA,GAAA,kBAAA,GAAA,UAAA;;;;;;;;ADnlBzB;;;;KEpBY,qBAAA;EACV,QAAA;EACA,iBAAA,GAAoB,cAAA;EACpB,kBAAA;EACA,eAAA;EACA,uBAAA;AAAA;;;;;;;ADsBF;;;KCVY,kBAAA;EACV,QAAA,EAAU,cAAA;EACV,SAAA;EACA,eAAA;EACA,SAAA;EACA,QAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;ADiGF;;;;;;;;cC5Da,wBAAA;EAAA,QAQD,YAAA;EAAA,QACA,sBAAA;EDgES;;;;;;cCjET,YAAA,EAAc,qBAAA,EACd,sBAAA,GAAyB,QAAA,EAAU,cAAA;EDqExC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ECjCL,iBAAA,CAAkB,OAAA,EAAS,qBAAA,GAAwB,kBAAA;EDgBnD;;;;;EC0GA,wBAAA,CAAA,GAA4B,qBAAA;EDvGT;;;;;;;;;;;;;;;;;;;;;;;;;ECoInB,qBAAA,CAAsB,OAAA,EAAS,qBAAA;IAC7B,KAAA;IACA,MAAA;EAAA;AAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e=require(`./websocket-Br0ijEZA.cjs`),t=require(`./uploadista-error-CZx1JU_L.cjs`),n=require(`./checksum-BrjQ8GJL.cjs`),r=require(`./stream-limiter-B-Y0DTgA.cjs`);let i=require(`effect`);function a(e,t,n=0){return e.length<n+t.length?!1:t.every((t,r)=>e[n+r]===t)}function o(e,t,n=0){if(e.length<n+t.length)return!1;for(let r=0;r<t.length;r++)if(e[n+r]!==t.charCodeAt(r))return!1;return!0}const s=(e,t)=>{if(e.length===0)return`application/octet-stream`;if(a(e,[137,80,78,71,13,10,26,10]))return`image/png`;if(a(e,[255,216,255]))return`image/jpeg`;if(o(e,`GIF87a`)||o(e,`GIF89a`))return`image/gif`;if(a(e,[82,73,70,70])&&e.length>=12&&o(e,`WEBP`,8))return`image/webp`;if(e.length>=12&&a(e,[0,0,0],0)&&o(e,`ftyp`,4)&&(o(e,`avif`,8)||o(e,`avis`,8)))return`image/avif`;if(e.length>=12&&o(e,`ftyp`,4)&&(o(e,`heic`,8)||o(e,`heif`,8)||o(e,`mif1`,8)))return`image/heic`;if(a(e,[66,77]))return`image/bmp`;if(a(e,[73,73,42,0])||a(e,[77,77,0,42]))return`image/tiff`;if(a(e,[0,0,1,0]))return`image/x-icon`;if(e.length>=5){let t=new TextDecoder(`utf-8`,{fatal:!1}).decode(e.slice(0,Math.min(1024,e.length)));if(t.includes(`<svg`)||t.includes(`<?xml`)&&t.includes(`<svg`))return`image/svg+xml`}if(e.length>=12&&o(e,`ftyp`,4)){let t=new TextDecoder().decode(e.slice(8,12));if(t.startsWith(`mp4`)||t.startsWith(`M4`)||t.startsWith(`isom`))return`video/mp4`}if(a(e,[26,69,223,163]))return`video/webm`;if(a(e,[82,73,70,70])&&e.length>=12&&o(e,`AVI `,8))return`video/x-msvideo`;if(e.length>=8&&(o(e,`moov`,4)||o(e,`mdat`,4)||o(e,`free`,4)))return`video/quicktime`;if(a(e,[26,69,223,163])&&e.length>=100&&new TextDecoder(`utf-8`,{fatal:!1}).decode(e.slice(0,100)).includes(`matroska`))return`video/x-matroska`;if(a(e,[255,251])||a(e,[255,243])||a(e,[255,242])||o(e,`ID3`))return`audio/mpeg`;if(a(e,[82,73,70,70])&&e.length>=12&&o(e,`WAVE`,8))return`audio/wav`;if(o(e,`fLaC`))return`audio/flac`;if(o(e,`OggS`))return`audio/ogg`;if(e.length>=12&&o(e,`ftyp`,4)&&o(e,`M4A`,8))return`audio/mp4`;if(o(e,`%PDF`))return`application/pdf`;if(a(e,[80,75,3,4])||a(e,[80,75,5,6])||a(e,[80,75,7,8])){if(e.length>=1024){let t=new TextDecoder(`utf-8`,{fatal:!1}).decode(e);if(t.includes(`word/`))return`application/vnd.openxmlformats-officedocument.wordprocessingml.document`;if(t.includes(`xl/`))return`application/vnd.openxmlformats-officedocument.spreadsheetml.sheet`;if(t.includes(`ppt/`))return`application/vnd.openxmlformats-officedocument.presentationml.presentation`}return`application/zip`}if(a(e,[82,97,114,33,26,7]))return`application/x-rar-compressed`;if(a(e,[55,122,188,175,39,28]))return`application/x-7z-compressed`;if(a(e,[31,139]))return`application/gzip`;if(e.length>=262&&o(e,`ustar`,257))return`application/x-tar`;if(o(e,`wOFF`))return`font/woff`;if(o(e,`wOF2`))return`font/woff2`;if(a(e,[0,1,0,0,0]))return`font/ttf`;if(o(e,`OTTO`))return`font/otf`;if(e.length>=1){let t=e[0];if(t===123||t===91)try{let t=new TextDecoder(`utf-8`).decode(e.slice(0,Math.min(1024,e.length)));return JSON.parse(t.trim()),`application/json`}catch{}}if(t)switch(t.split(`.`).pop()?.toLowerCase()){case`jpg`:case`jpeg`:return`image/jpeg`;case`png`:return`image/png`;case`gif`:return`image/gif`;case`webp`:return`image/webp`;case`avif`:return`image/avif`;case`heic`:case`heif`:return`image/heic`;case`bmp`:return`image/bmp`;case`tiff`:case`tif`:return`image/tiff`;case`ico`:return`image/x-icon`;case`svg`:return`image/svg+xml`;case`mp4`:case`m4v`:return`video/mp4`;case`webm`:return`video/webm`;case`avi`:return`video/x-msvideo`;case`mov`:return`video/quicktime`;case`mkv`:return`video/x-matroska`;case`mp3`:return`audio/mpeg`;case`wav`:return`audio/wav`;case`flac`:return`audio/flac`;case`ogg`:return`audio/ogg`;case`m4a`:return`audio/mp4`;case`pdf`:return`application/pdf`;case`docx`:return`application/vnd.openxmlformats-officedocument.wordprocessingml.document`;case`xlsx`:return`application/vnd.openxmlformats-officedocument.spreadsheetml.sheet`;case`pptx`:return`application/vnd.openxmlformats-officedocument.presentationml.presentation`;case`zip`:return`application/zip`;case`rar`:return`application/x-rar-compressed`;case`7z`:return`application/x-7z-compressed`;case`gz`:case`gzip`:return`application/gzip`;case`tar`:return`application/x-tar`;case`woff`:return`font/woff`;case`woff2`:return`font/woff2`;case`ttf`:return`font/ttf`;case`otf`:return`font/otf`;case`txt`:return`text/plain`;case`json`:return`application/json`;case`xml`:return`application/xml`;case`html`:case`htm`:return`text/html`;case`css`:return`text/css`;case`js`:return`application/javascript`;case`csv`:return`text/csv`;default:return`application/octet-stream`}return`application/octet-stream`};function c(e,t){return e===t?!0:e.split(`/`)[0]===t.split(`/`)[0]}const l=i.Effect.gen(function*(){let e=yield*i.Effect.currentSpan.pipe(i.Effect.option);return i.Option.match(e,{onNone:()=>void 0,onSome:e=>({traceId:e.traceId,spanId:e.spanId,traceFlags:e.sampled?1:0})})}),u=(t,n,{dataStoreService:r,kvStore:a,eventEmitter:o,generateId:s})=>i.Effect.gen(function*(){let c=yield*l,u=new Date().toISOString();return yield*i.Effect.gen(function*(){let i=yield*r.getDataStore(t.storageId,n),l=yield*s.generateId(),{size:d,type:f,fileName:p,lastModified:m,metadata:h,flow:g}=t,_={};if(h)try{_=JSON.parse(h)}catch{_={}}let v={..._,type:f,fileName:p??``};m&&(v.lastModified=m.toString());let y={id:l,size:d,metadata:v,offset:0,creationDate:u,storage:{id:t.storageId,type:f,path:``,bucket:i.bucket},flow:g,traceContext:c},b=yield*i.create(y);return yield*a.set(l,b),yield*o.emit(l,{type:e.n.UPLOAD_STARTED,data:b,flow:b.flow}),b}).pipe(i.Effect.withSpan(`upload-create`,{attributes:{"upload.file_name":t.fileName??`unknown`,"upload.file_size":t.size?.toString()??`0`,"upload.storage_id":t.storageId,"upload.mime_type":t.type,"upload.has_flow":t.flow?`true`:`false`}}))}).pipe(i.Effect.withSpan(`upload`,{attributes:{"upload.file_name":t.fileName??`unknown`,"upload.file_size":t.size?.toString()??`0`,"upload.storage_id":t.storageId,"upload.mime_type":t.type,"upload.has_flow":t.flow?`true`:`false`}}),i.Effect.tap(e=>i.Effect.gen(function*(){if(yield*i.Metric.increment(i.Metric.counter(`upload_created_total`,{description:`Total number of uploads created`})),e.size){let t=i.Metric.histogram(`upload_file_size_bytes`,i.MetricBoundaries.exponential({start:1024,factor:2,count:25}));yield*i.Metric.update(t,e.size)}let t=i.Metric.gauge(`active_uploads`);yield*i.Metric.increment(t)})),i.Effect.tap(e=>i.Effect.logInfo(`Upload created`).pipe(i.Effect.annotateLogs({"upload.id":e.id,"upload.file_name":t.fileName??`unknown`,"upload.file_size":t.size?.toString()??`0`,"upload.storage_id":t.storageId}))),i.Effect.tapError(e=>i.Effect.gen(function*(){yield*i.Effect.logError(`Upload creation failed`).pipe(i.Effect.annotateLogs({"upload.file_name":t.fileName??`unknown`,"upload.storage_id":t.storageId,error:String(e)})),yield*i.Metric.increment(i.Metric.counter(`upload_failed_total`,{description:`Total number of uploads that failed`}))})));function d(e){return i.Stream.fromReadableStream(()=>e,e=>new t.n({code:`UNKNOWN_ERROR`,status:500,body:String(e)}))}function f({data:n,upload:a,dataStore:o,maxFileSize:s,controller:c,eventEmitter:l,uploadProgressInterval:u=200}){return i.Effect.gen(function*(){let f=d(n);if(c.signal.aborted)return yield*i.Effect.fail(t.n.fromCode(`ABORTED`));let p=new AbortController,{signal:m}=p,h=()=>{p.abort()};return c.signal.addEventListener(`abort`,h,{once:!0}),yield*i.Effect.acquireUseRelease(i.Effect.sync(()=>({signal:m,onAbort:h})),({signal:n})=>i.Effect.gen(function*(){let t=0,n=r.t.limit({maxSize:s})(f);return yield*o.write({stream:n,file_id:a.id,offset:a.offset},{onProgress:n=>{let r=Date.now();return r-t>=u?(t=r,l.emit(a.id,{type:e.n.UPLOAD_PROGRESS,data:{id:a.id,progress:n,total:a.size??0},flow:a.flow}).pipe(i.Effect.catchAll(()=>i.Effect.void))):i.Effect.void}})}).pipe(i.Effect.catchAll(e=>e instanceof Error&&e.name===`AbortError`?i.Effect.fail(t.n.fromCode(`ABORTED`)):e instanceof t.n?i.Effect.fail(e):i.Effect.fail(t.n.fromCode(`FILE_WRITE_ERROR`,{cause:e})))),({onAbort:e})=>i.Effect.sync(()=>{c.signal.removeEventListener(`abort`,e)}))}).pipe(i.Effect.withSpan(`upload-write-to-store`,{attributes:{"upload.id":a.id,"upload.offset":a.offset.toString(),"upload.max_file_size":s.toString(),"upload.file_size":a.size?.toString()??`0`}}),i.Effect.tap(e=>i.Effect.logDebug(`Data written to store`).pipe(i.Effect.annotateLogs({"upload.id":a.id,"write.offset":e.toString(),"write.bytes_written":(e-a.offset).toString()}))),i.Effect.tapError(e=>i.Effect.logError(`Failed to write to store`).pipe(i.Effect.annotateLogs({"upload.id":a.id,"upload.offset":a.offset.toString(),error:e instanceof t.n?e.code:String(e)}))))}function p(e){return i.Tracer.externalSpan({traceId:e.traceId,spanId:e.spanId,sampled:e.traceFlags===1})}const m=(e,t)=>{let n=new Date(e.creationDate).getTime(),r=Date.now()-n;return i.Effect.void.pipe(i.Effect.withSpan(`upload-complete`,{attributes:{"upload.id":e.id,"upload.size":e.size??0,"upload.total_duration_ms":r,"upload.storage_id":e.storage.id,"upload.file_name":e.metadata?.fileName?.toString()??`unknown`,"upload.creation_date":e.creationDate,"upload.completion_date":new Date().toISOString()},parent:t}))},h=(t,n,r,{dataStoreService:a,kvStore:o,eventEmitter:s})=>i.Effect.gen(function*(){let c=yield*o.get(t),l=c.traceContext?p(c.traceContext):void 0;return yield*i.Effect.gen(function*(){let i=yield*a.getDataStore(c.storage.id,n);return c.offset=yield*f({dataStore:i,data:r,upload:c,maxFileSize:1e8,controller:new AbortController,uploadProgressInterval:200,eventEmitter:s}),yield*o.set(t,c),yield*s.emit(c.id,{type:e.n.UPLOAD_PROGRESS,data:{id:c.id,progress:c.offset,total:c.size??0},flow:c.flow}),c.size&&c.offset===c.size&&(yield*g({file:c,dataStore:i,eventEmitter:s}),c.traceContext&&(yield*m(c,p(c.traceContext)))),c}).pipe(i.Effect.withSpan(`upload-chunk`,{attributes:{"upload.id":t,"chunk.upload_id":t,"upload.has_trace_context":c.traceContext?`true`:`false`},parent:l}))}).pipe(i.Effect.tap(e=>i.Effect.gen(function*(){yield*i.Metric.increment(i.Metric.counter(`chunk_uploaded_total`,{description:`Total number of chunks uploaded`}));let t=e.offset,n=i.Metric.histogram(`chunk_size_bytes`,i.MetricBoundaries.linear({start:262144,width:262144,count:20}));if(yield*i.Metric.update(n,t),e.size&&e.size>0){let e=t,n=i.Metric.gauge(`upload_throughput_bytes_per_second`);yield*i.Metric.set(n,e)}})),i.Effect.tap(e=>i.Effect.logDebug(`Chunk uploaded`).pipe(i.Effect.annotateLogs({"upload.id":e.id,"chunk.size":e.offset.toString(),"chunk.progress":e.size&&e.size>0?(e.offset/e.size*100).toFixed(2):`0`,"upload.total_size":e.size?.toString()??`0`}))),i.Effect.tapError(e=>i.Effect.logError(`Chunk upload failed`).pipe(i.Effect.annotateLogs({"upload.id":t,error:String(e)})))),g=({file:r,dataStore:a,eventEmitter:o})=>i.Effect.gen(function*(){let i=a.getCapabilities();if(i.maxValidationSize&&r.size&&r.size>i.maxValidationSize){yield*o.emit(r.id,{type:e.n.UPLOAD_VALIDATION_WARNING,data:{id:r.id,message:`File size (${r.size} bytes) exceeds max validation size (${i.maxValidationSize} bytes). Validation skipped.`},flow:r.flow});return}let l=yield*a.read(r.id);if(r.checksum&&r.checksumAlgorithm){let i=yield*n.t(l,r.checksumAlgorithm);if(i!==r.checksum)return yield*o.emit(r.id,{type:e.n.UPLOAD_VALIDATION_FAILED,data:{id:r.id,reason:`checksum_mismatch`,expected:r.checksum,actual:i},flow:r.flow}),yield*a.remove(r.id),yield*t.n.fromCode(`CHECKSUM_MISMATCH`,{body:`Checksum validation failed. Expected: ${r.checksum}, Got: ${i}`,details:{uploadId:r.id,expected:r.checksum,actual:i,algorithm:r.checksumAlgorithm}}).toEffect();yield*o.emit(r.id,{type:e.n.UPLOAD_VALIDATION_SUCCESS,data:{id:r.id,validationType:`checksum`,algorithm:r.checksumAlgorithm},flow:r.flow})}if(i.requiresMimeTypeValidation){let n=s(l),i=r.metadata?.type;if(i&&!c(i,n))return yield*o.emit(r.id,{type:e.n.UPLOAD_VALIDATION_FAILED,data:{id:r.id,reason:`mimetype_mismatch`,expected:i,actual:n},flow:r.flow}),yield*a.remove(r.id),yield*t.n.fromCode(`MIMETYPE_MISMATCH`,{body:`MIME type validation failed. Expected: ${i}, Detected: ${n}`,details:{uploadId:r.id,expected:i,actual:n}}).toEffect();yield*o.emit(r.id,{type:e.n.UPLOAD_VALIDATION_SUCCESS,data:{id:r.id,validationType:`mimetype`},flow:r.flow})}}).pipe(i.Effect.withSpan(`validate-upload`,{attributes:{"upload.id":r.id,"validation.checksum_provided":r.checksum?`true`:`false`,"validation.mime_required":a.getCapabilities().requiresMimeTypeValidation?`true`:`false`}})),_=e=>i.Effect.tryPromise({try:async()=>await fetch(e),catch:e=>t.n.fromCode(`UNKNOWN_ERROR`,{cause:e})}).pipe(i.Effect.withSpan(`upload-fetch-url`,{attributes:{"upload.url":e,"upload.operation":`fetch`}}),i.Effect.tap(e=>i.Effect.gen(function*(){yield*i.Metric.increment(i.Metric.counter(`upload_from_url_total`,{description:`Total number of URL-based uploads`})),e.ok&&(yield*i.Metric.increment(i.Metric.counter(`upload_from_url_success_total`,{description:`Total number of successful URL-based uploads`})))})),i.Effect.tap(t=>i.Effect.logInfo(`URL fetch completed`).pipe(i.Effect.annotateLogs({"upload.url":e,"response.status":t.status.toString(),"response.ok":t.ok.toString(),"response.content_length":t.headers.get(`content-length`)??`unknown`}))),i.Effect.tapError(t=>i.Effect.gen(function*(){yield*i.Metric.increment(i.Metric.counter(`upload_from_url_failed_total`,{description:`Total number of failed URL-based uploads`})),yield*i.Effect.logError(`URL fetch failed`).pipe(i.Effect.annotateLogs({"upload.url":e,error:String(t)}))}))),v=e=>i.Effect.tryPromise({try:async()=>await e.arrayBuffer(),catch:e=>t.n.fromCode(`UNKNOWN_ERROR`,{cause:e})}).pipe(i.Effect.withSpan(`upload-convert-to-buffer`,{attributes:{"upload.operation":`arrayBuffer`}}),i.Effect.tap(e=>i.Effect.logDebug(`Response converted to array buffer`).pipe(i.Effect.annotateLogs({"buffer.size":e.byteLength.toString()}))),i.Effect.tapError(e=>i.Effect.logError(`Failed to convert response to array buffer`).pipe(i.Effect.annotateLogs({error:String(e)}))));var y=class extends i.Context.Tag(`UploadEngine`)(){};function b(){return i.Effect.gen(function*(){let t=yield*e.N,r=yield*e.p,a=yield*n.r,o=yield*e.S;return{upload:(e,n,s)=>i.Effect.gen(function*(){return yield*h((yield*u(e,n,{dataStoreService:o,kvStore:t,eventEmitter:r,generateId:a})).id,n,s,{dataStoreService:o,kvStore:t,eventEmitter:r})}),uploadFromUrl:(e,n,s)=>i.Effect.gen(function*(){let i=yield*v(yield*_(s)),c=new ReadableStream({start(e){e.enqueue(new Uint8Array(i)),e.close()}});return yield*h((yield*u({...e,size:i.byteLength},n,{dataStoreService:o,kvStore:t,eventEmitter:r,generateId:a})).id,n,c,{dataStoreService:o,kvStore:t,eventEmitter:r})}),createUpload:(e,n)=>i.Effect.gen(function*(){return yield*u(e,n,{dataStoreService:o,kvStore:t,eventEmitter:r,generateId:a})}),uploadChunk:(e,n,a)=>i.Effect.gen(function*(){return yield*h(e,n,a,{dataStoreService:o,kvStore:t,eventEmitter:r})}),getUpload:e=>i.Effect.gen(function*(){return yield*t.get(e)}),read:(e,n)=>i.Effect.gen(function*(){let r=yield*t.get(e);return yield*(yield*o.getDataStore(r.storage.id,n)).read(e)}),readStream:(e,n,r)=>i.Effect.gen(function*(){let a=yield*t.get(e),s=yield*o.getDataStore(a.storage.id,n);if(s.getCapabilities().supportsStreamingRead&&s.readStream)return yield*i.Effect.logDebug(`Using streaming read for file ${e}`),yield*s.readStream(e,r);yield*i.Effect.logDebug(`Falling back to buffered read for file ${e} (streaming not supported)`);let c=yield*s.read(e);return i.Stream.succeed(c)}),uploadStream:(n,s,c)=>i.Effect.gen(function*(){let l=yield*o.getDataStore(n.storageId,s),d=l.getCapabilities(),f=yield*a.generateId();if(d.supportsStreamingWrite&&l.writeStream){yield*i.Effect.logDebug(`Using streaming write for file ${f}`);let a=typeof n.metadata==`string`?JSON.parse(n.metadata):n.metadata||{},o=Object.fromEntries(Object.entries(a).map(([e,t])=>[e,String(t)])),s={id:f,offset:0,size:n.size??0,storage:{id:n.storageId,type:l.getCapabilities().supportsStreamingWrite?`streaming`:`default`},metadata:a,creationDate:new Date().toISOString()};yield*t.set(f,s),yield*r.emit(f,{type:e.n.UPLOAD_STARTED,data:s});let u=yield*l.writeStream(f,{stream:c,contentType:n.type,sizeHint:n.sizeHint,metadata:o}),d={...s,size:u.size,offset:u.size,storage:{...s.storage,path:u.path},...u.url&&{url:u.url}};return yield*t.set(f,d),yield*r.emit(f,{type:e.n.UPLOAD_COMPLETE,data:d}),d}yield*i.Effect.logWarning(`Falling back to buffered upload for file ${f} (streaming write not supported)`);let p=[];yield*i.Stream.runForEach(c,e=>i.Effect.sync(()=>{p.push(e)}));let m=p.reduce((e,t)=>e+t.length,0),g=new Uint8Array(m),_=0;for(let e of p)g.set(e,_),_+=e.length;let v=new ReadableStream({start(e){e.enqueue(g),e.close()}});return yield*u({...n,size:m},s,{dataStoreService:o,kvStore:t,eventEmitter:r,generateId:{generateId:()=>i.Effect.succeed(f)}}),yield*h(f,s,v,{dataStoreService:o,kvStore:t,eventEmitter:r})}),delete:(e,n)=>i.Effect.gen(function*(){let r=yield*t.get(e);yield*(yield*o.getDataStore(r.storage.id,n)).remove(e),yield*t.delete(e)}),getCapabilities:(e,t)=>i.Effect.gen(function*(){return(yield*o.getDataStore(e,t)).getCapabilities()}),subscribeToUploadEvents:(e,t)=>i.Effect.gen(function*(){yield*r.subscribe(e,t)}),unsubscribeFromUploadEvents:e=>i.Effect.gen(function*(){yield*r.unsubscribe(e)})}})}const x=i.Layer.effect(y,b());var S=class{constructor(e,t){this.capabilities=e,this.validateUploadStrategy=t}negotiateStrategy(e){let t=[],n=[],r=`single`,i=e.preferredChunkSize??this.capabilities.optimalChunkSize??1024*1024,a=e.parallelUploads??1;if(e.preferredStrategy&&(this.validateUploadStrategy(e.preferredStrategy)?(r=e.preferredStrategy,t.push(`Using preferred strategy: ${r}`)):n.push(`Preferred strategy '${e.preferredStrategy}' not supported by data store, falling back`)),(!e.preferredStrategy||!this.validateUploadStrategy(e.preferredStrategy))&&(this.capabilities.supportsParallelUploads&&e.fileSize>(e.minChunkSizeForParallel??10*1024*1024)?(r=`parallel`,t.push(`Selected parallel upload for large file (${e.fileSize} bytes)`)):(r=`single`,t.push(this.capabilities.supportsParallelUploads?`Selected single upload for small file (${e.fileSize} bytes)`:`Selected single upload (parallel not supported by data store)`))),this.capabilities.minChunkSize&&i<this.capabilities.minChunkSize&&(n.push(`Chunk size ${i} below minimum ${this.capabilities.minChunkSize}, adjusting`),i=this.capabilities.minChunkSize),this.capabilities.maxChunkSize&&i>this.capabilities.maxChunkSize&&(n.push(`Chunk size ${i} above maximum ${this.capabilities.maxChunkSize}, adjusting`),i=this.capabilities.maxChunkSize),r===`parallel`&&(this.capabilities.maxConcurrentUploads&&a>this.capabilities.maxConcurrentUploads&&(n.push(`Parallel uploads ${a} exceeds maximum ${this.capabilities.maxConcurrentUploads}, adjusting`),a=this.capabilities.maxConcurrentUploads),this.capabilities.maxParts)){let t=Math.ceil(e.fileSize/i);if(t>this.capabilities.maxParts){let r=Math.ceil(e.fileSize/this.capabilities.maxParts);n.push(`Estimated parts ${t} exceeds maximum ${this.capabilities.maxParts}, increasing chunk size`),i=Math.max(i,r)}}return this.validateUploadStrategy(r)||(n.push(`Final strategy validation failed, falling back to single upload`),r=`single`,a=1),t.push(`Data store capabilities: parallel=${this.capabilities.supportsParallelUploads}, concatenation=${this.capabilities.supportsConcatenation}, resumable=${this.capabilities.supportsResumableUploads}`),{strategy:r,chunkSize:i,parallelUploads:r===`parallel`?a:1,reasoning:t,warnings:n}}getDataStoreCapabilities(){return this.capabilities}validateConfiguration(e){let t=[];return e.preferredStrategy&&!this.validateUploadStrategy(e.preferredStrategy)&&t.push(`Preferred strategy '${e.preferredStrategy}' not supported by data store`),e.preferredChunkSize&&(this.capabilities.minChunkSize&&e.preferredChunkSize<this.capabilities.minChunkSize&&t.push(`Chunk size ${e.preferredChunkSize} below data store minimum ${this.capabilities.minChunkSize}`),this.capabilities.maxChunkSize&&e.preferredChunkSize>this.capabilities.maxChunkSize&&t.push(`Chunk size ${e.preferredChunkSize} above data store maximum ${this.capabilities.maxChunkSize}`)),e.parallelUploads&&this.capabilities.maxConcurrentUploads&&e.parallelUploads>this.capabilities.maxConcurrentUploads&&t.push(`Parallel uploads ${e.parallelUploads} exceeds data store maximum ${this.capabilities.maxConcurrentUploads}`),{valid:t.length===0,errors:t}}};Object.defineProperty(exports,`a`,{enumerable:!0,get:function(){return v}}),Object.defineProperty(exports,`c`,{enumerable:!0,get:function(){return s}}),Object.defineProperty(exports,`i`,{enumerable:!0,get:function(){return x}}),Object.defineProperty(exports,`n`,{enumerable:!0,get:function(){return y}}),Object.defineProperty(exports,`o`,{enumerable:!0,get:function(){return _}}),Object.defineProperty(exports,`r`,{enumerable:!0,get:function(){return b}}),Object.defineProperty(exports,`s`,{enumerable:!0,get:function(){return c}}),Object.defineProperty(exports,`t`,{enumerable:!0,get:function(){return S}});
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{n as e}from"./uploadista-error-DQ7V1FlX.mjs";import{N as t,S as n,n as r,p as i}from"./websocket-DftnHFfN.mjs";import{r as a,t as o}from"./checksum-BRjFmTRk.mjs";import{t as s}from"./stream-limiter-CvDuNIyd.mjs";import{Context as c,Effect as l,Layer as u,Metric as d,MetricBoundaries as f,Option as p,Stream as m,Tracer as h}from"effect";function g(e,t,n=0){return e.length<n+t.length?!1:t.every((t,r)=>e[n+r]===t)}function _(e,t,n=0){if(e.length<n+t.length)return!1;for(let r=0;r<t.length;r++)if(e[n+r]!==t.charCodeAt(r))return!1;return!0}const v=(e,t)=>{if(e.length===0)return`application/octet-stream`;if(g(e,[137,80,78,71,13,10,26,10]))return`image/png`;if(g(e,[255,216,255]))return`image/jpeg`;if(_(e,`GIF87a`)||_(e,`GIF89a`))return`image/gif`;if(g(e,[82,73,70,70])&&e.length>=12&&_(e,`WEBP`,8))return`image/webp`;if(e.length>=12&&g(e,[0,0,0],0)&&_(e,`ftyp`,4)&&(_(e,`avif`,8)||_(e,`avis`,8)))return`image/avif`;if(e.length>=12&&_(e,`ftyp`,4)&&(_(e,`heic`,8)||_(e,`heif`,8)||_(e,`mif1`,8)))return`image/heic`;if(g(e,[66,77]))return`image/bmp`;if(g(e,[73,73,42,0])||g(e,[77,77,0,42]))return`image/tiff`;if(g(e,[0,0,1,0]))return`image/x-icon`;if(e.length>=5){let t=new TextDecoder(`utf-8`,{fatal:!1}).decode(e.slice(0,Math.min(1024,e.length)));if(t.includes(`<svg`)||t.includes(`<?xml`)&&t.includes(`<svg`))return`image/svg+xml`}if(e.length>=12&&_(e,`ftyp`,4)){let t=new TextDecoder().decode(e.slice(8,12));if(t.startsWith(`mp4`)||t.startsWith(`M4`)||t.startsWith(`isom`))return`video/mp4`}if(g(e,[26,69,223,163]))return`video/webm`;if(g(e,[82,73,70,70])&&e.length>=12&&_(e,`AVI `,8))return`video/x-msvideo`;if(e.length>=8&&(_(e,`moov`,4)||_(e,`mdat`,4)||_(e,`free`,4)))return`video/quicktime`;if(g(e,[26,69,223,163])&&e.length>=100&&new TextDecoder(`utf-8`,{fatal:!1}).decode(e.slice(0,100)).includes(`matroska`))return`video/x-matroska`;if(g(e,[255,251])||g(e,[255,243])||g(e,[255,242])||_(e,`ID3`))return`audio/mpeg`;if(g(e,[82,73,70,70])&&e.length>=12&&_(e,`WAVE`,8))return`audio/wav`;if(_(e,`fLaC`))return`audio/flac`;if(_(e,`OggS`))return`audio/ogg`;if(e.length>=12&&_(e,`ftyp`,4)&&_(e,`M4A`,8))return`audio/mp4`;if(_(e,`%PDF`))return`application/pdf`;if(g(e,[80,75,3,4])||g(e,[80,75,5,6])||g(e,[80,75,7,8])){if(e.length>=1024){let t=new TextDecoder(`utf-8`,{fatal:!1}).decode(e);if(t.includes(`word/`))return`application/vnd.openxmlformats-officedocument.wordprocessingml.document`;if(t.includes(`xl/`))return`application/vnd.openxmlformats-officedocument.spreadsheetml.sheet`;if(t.includes(`ppt/`))return`application/vnd.openxmlformats-officedocument.presentationml.presentation`}return`application/zip`}if(g(e,[82,97,114,33,26,7]))return`application/x-rar-compressed`;if(g(e,[55,122,188,175,39,28]))return`application/x-7z-compressed`;if(g(e,[31,139]))return`application/gzip`;if(e.length>=262&&_(e,`ustar`,257))return`application/x-tar`;if(_(e,`wOFF`))return`font/woff`;if(_(e,`wOF2`))return`font/woff2`;if(g(e,[0,1,0,0,0]))return`font/ttf`;if(_(e,`OTTO`))return`font/otf`;if(e.length>=1){let t=e[0];if(t===123||t===91)try{let t=new TextDecoder(`utf-8`).decode(e.slice(0,Math.min(1024,e.length)));return JSON.parse(t.trim()),`application/json`}catch{}}if(t)switch(t.split(`.`).pop()?.toLowerCase()){case`jpg`:case`jpeg`:return`image/jpeg`;case`png`:return`image/png`;case`gif`:return`image/gif`;case`webp`:return`image/webp`;case`avif`:return`image/avif`;case`heic`:case`heif`:return`image/heic`;case`bmp`:return`image/bmp`;case`tiff`:case`tif`:return`image/tiff`;case`ico`:return`image/x-icon`;case`svg`:return`image/svg+xml`;case`mp4`:case`m4v`:return`video/mp4`;case`webm`:return`video/webm`;case`avi`:return`video/x-msvideo`;case`mov`:return`video/quicktime`;case`mkv`:return`video/x-matroska`;case`mp3`:return`audio/mpeg`;case`wav`:return`audio/wav`;case`flac`:return`audio/flac`;case`ogg`:return`audio/ogg`;case`m4a`:return`audio/mp4`;case`pdf`:return`application/pdf`;case`docx`:return`application/vnd.openxmlformats-officedocument.wordprocessingml.document`;case`xlsx`:return`application/vnd.openxmlformats-officedocument.spreadsheetml.sheet`;case`pptx`:return`application/vnd.openxmlformats-officedocument.presentationml.presentation`;case`zip`:return`application/zip`;case`rar`:return`application/x-rar-compressed`;case`7z`:return`application/x-7z-compressed`;case`gz`:case`gzip`:return`application/gzip`;case`tar`:return`application/x-tar`;case`woff`:return`font/woff`;case`woff2`:return`font/woff2`;case`ttf`:return`font/ttf`;case`otf`:return`font/otf`;case`txt`:return`text/plain`;case`json`:return`application/json`;case`xml`:return`application/xml`;case`html`:case`htm`:return`text/html`;case`css`:return`text/css`;case`js`:return`application/javascript`;case`csv`:return`text/csv`;default:return`application/octet-stream`}return`application/octet-stream`};function y(e,t){return e===t?!0:e.split(`/`)[0]===t.split(`/`)[0]}const b=l.gen(function*(){let e=yield*l.currentSpan.pipe(l.option);return p.match(e,{onNone:()=>void 0,onSome:e=>({traceId:e.traceId,spanId:e.spanId,traceFlags:e.sampled?1:0})})}),x=(e,t,{dataStoreService:n,kvStore:i,eventEmitter:a,generateId:o})=>l.gen(function*(){let s=yield*b,c=new Date().toISOString();return yield*l.gen(function*(){let l=yield*n.getDataStore(e.storageId,t),u=yield*o.generateId(),{size:d,type:f,fileName:p,lastModified:m,metadata:h,flow:g}=e,_={};if(h)try{_=JSON.parse(h)}catch{_={}}let v={..._,type:f,fileName:p??``};m&&(v.lastModified=m.toString());let y={id:u,size:d,metadata:v,offset:0,creationDate:c,storage:{id:e.storageId,type:f,path:``,bucket:l.bucket},flow:g,traceContext:s},b=yield*l.create(y);return yield*i.set(u,b),yield*a.emit(u,{type:r.UPLOAD_STARTED,data:b,flow:b.flow}),b}).pipe(l.withSpan(`upload-create`,{attributes:{"upload.file_name":e.fileName??`unknown`,"upload.file_size":e.size?.toString()??`0`,"upload.storage_id":e.storageId,"upload.mime_type":e.type,"upload.has_flow":e.flow?`true`:`false`}}))}).pipe(l.withSpan(`upload`,{attributes:{"upload.file_name":e.fileName??`unknown`,"upload.file_size":e.size?.toString()??`0`,"upload.storage_id":e.storageId,"upload.mime_type":e.type,"upload.has_flow":e.flow?`true`:`false`}}),l.tap(e=>l.gen(function*(){if(yield*d.increment(d.counter(`upload_created_total`,{description:`Total number of uploads created`})),e.size){let t=d.histogram(`upload_file_size_bytes`,f.exponential({start:1024,factor:2,count:25}));yield*d.update(t,e.size)}let t=d.gauge(`active_uploads`);yield*d.increment(t)})),l.tap(t=>l.logInfo(`Upload created`).pipe(l.annotateLogs({"upload.id":t.id,"upload.file_name":e.fileName??`unknown`,"upload.file_size":e.size?.toString()??`0`,"upload.storage_id":e.storageId}))),l.tapError(t=>l.gen(function*(){yield*l.logError(`Upload creation failed`).pipe(l.annotateLogs({"upload.file_name":e.fileName??`unknown`,"upload.storage_id":e.storageId,error:String(t)})),yield*d.increment(d.counter(`upload_failed_total`,{description:`Total number of uploads that failed`}))})));function S(t){return m.fromReadableStream(()=>t,t=>new e({code:`UNKNOWN_ERROR`,status:500,body:String(t)}))}function C({data:t,upload:n,dataStore:i,maxFileSize:a,controller:o,eventEmitter:c,uploadProgressInterval:u=200}){return l.gen(function*(){let d=S(t);if(o.signal.aborted)return yield*l.fail(e.fromCode(`ABORTED`));let f=new AbortController,{signal:p}=f,m=()=>{f.abort()};return o.signal.addEventListener(`abort`,m,{once:!0}),yield*l.acquireUseRelease(l.sync(()=>({signal:p,onAbort:m})),({signal:t})=>l.gen(function*(){let e=0,t=s.limit({maxSize:a})(d);return yield*i.write({stream:t,file_id:n.id,offset:n.offset},{onProgress:t=>{let i=Date.now();return i-e>=u?(e=i,c.emit(n.id,{type:r.UPLOAD_PROGRESS,data:{id:n.id,progress:t,total:n.size??0},flow:n.flow}).pipe(l.catchAll(()=>l.void))):l.void}})}).pipe(l.catchAll(t=>t instanceof Error&&t.name===`AbortError`?l.fail(e.fromCode(`ABORTED`)):t instanceof e?l.fail(t):l.fail(e.fromCode(`FILE_WRITE_ERROR`,{cause:t})))),({onAbort:e})=>l.sync(()=>{o.signal.removeEventListener(`abort`,e)}))}).pipe(l.withSpan(`upload-write-to-store`,{attributes:{"upload.id":n.id,"upload.offset":n.offset.toString(),"upload.max_file_size":a.toString(),"upload.file_size":n.size?.toString()??`0`}}),l.tap(e=>l.logDebug(`Data written to store`).pipe(l.annotateLogs({"upload.id":n.id,"write.offset":e.toString(),"write.bytes_written":(e-n.offset).toString()}))),l.tapError(t=>l.logError(`Failed to write to store`).pipe(l.annotateLogs({"upload.id":n.id,"upload.offset":n.offset.toString(),error:t instanceof e?t.code:String(t)}))))}function w(e){return h.externalSpan({traceId:e.traceId,spanId:e.spanId,sampled:e.traceFlags===1})}const T=(e,t)=>{let n=new Date(e.creationDate).getTime(),r=Date.now()-n;return l.void.pipe(l.withSpan(`upload-complete`,{attributes:{"upload.id":e.id,"upload.size":e.size??0,"upload.total_duration_ms":r,"upload.storage_id":e.storage.id,"upload.file_name":e.metadata?.fileName?.toString()??`unknown`,"upload.creation_date":e.creationDate,"upload.completion_date":new Date().toISOString()},parent:t}))},E=(e,t,n,{dataStoreService:i,kvStore:a,eventEmitter:o})=>l.gen(function*(){let s=yield*a.get(e),c=s.traceContext?w(s.traceContext):void 0;return yield*l.gen(function*(){let c=yield*i.getDataStore(s.storage.id,t);return s.offset=yield*C({dataStore:c,data:n,upload:s,maxFileSize:1e8,controller:new AbortController,uploadProgressInterval:200,eventEmitter:o}),yield*a.set(e,s),yield*o.emit(s.id,{type:r.UPLOAD_PROGRESS,data:{id:s.id,progress:s.offset,total:s.size??0},flow:s.flow}),s.size&&s.offset===s.size&&(yield*D({file:s,dataStore:c,eventEmitter:o}),s.traceContext&&(yield*T(s,w(s.traceContext)))),s}).pipe(l.withSpan(`upload-chunk`,{attributes:{"upload.id":e,"chunk.upload_id":e,"upload.has_trace_context":s.traceContext?`true`:`false`},parent:c}))}).pipe(l.tap(e=>l.gen(function*(){yield*d.increment(d.counter(`chunk_uploaded_total`,{description:`Total number of chunks uploaded`}));let t=e.offset,n=d.histogram(`chunk_size_bytes`,f.linear({start:262144,width:262144,count:20}));if(yield*d.update(n,t),e.size&&e.size>0){let e=t,n=d.gauge(`upload_throughput_bytes_per_second`);yield*d.set(n,e)}})),l.tap(e=>l.logDebug(`Chunk uploaded`).pipe(l.annotateLogs({"upload.id":e.id,"chunk.size":e.offset.toString(),"chunk.progress":e.size&&e.size>0?(e.offset/e.size*100).toFixed(2):`0`,"upload.total_size":e.size?.toString()??`0`}))),l.tapError(t=>l.logError(`Chunk upload failed`).pipe(l.annotateLogs({"upload.id":e,error:String(t)})))),D=({file:t,dataStore:n,eventEmitter:i})=>l.gen(function*(){let a=n.getCapabilities();if(a.maxValidationSize&&t.size&&t.size>a.maxValidationSize){yield*i.emit(t.id,{type:r.UPLOAD_VALIDATION_WARNING,data:{id:t.id,message:`File size (${t.size} bytes) exceeds max validation size (${a.maxValidationSize} bytes). Validation skipped.`},flow:t.flow});return}let s=yield*n.read(t.id);if(t.checksum&&t.checksumAlgorithm){let a=yield*o(s,t.checksumAlgorithm);if(a!==t.checksum)return yield*i.emit(t.id,{type:r.UPLOAD_VALIDATION_FAILED,data:{id:t.id,reason:`checksum_mismatch`,expected:t.checksum,actual:a},flow:t.flow}),yield*n.remove(t.id),yield*e.fromCode(`CHECKSUM_MISMATCH`,{body:`Checksum validation failed. Expected: ${t.checksum}, Got: ${a}`,details:{uploadId:t.id,expected:t.checksum,actual:a,algorithm:t.checksumAlgorithm}}).toEffect();yield*i.emit(t.id,{type:r.UPLOAD_VALIDATION_SUCCESS,data:{id:t.id,validationType:`checksum`,algorithm:t.checksumAlgorithm},flow:t.flow})}if(a.requiresMimeTypeValidation){let a=v(s),o=t.metadata?.type;if(o&&!y(o,a))return yield*i.emit(t.id,{type:r.UPLOAD_VALIDATION_FAILED,data:{id:t.id,reason:`mimetype_mismatch`,expected:o,actual:a},flow:t.flow}),yield*n.remove(t.id),yield*e.fromCode(`MIMETYPE_MISMATCH`,{body:`MIME type validation failed. Expected: ${o}, Detected: ${a}`,details:{uploadId:t.id,expected:o,actual:a}}).toEffect();yield*i.emit(t.id,{type:r.UPLOAD_VALIDATION_SUCCESS,data:{id:t.id,validationType:`mimetype`},flow:t.flow})}}).pipe(l.withSpan(`validate-upload`,{attributes:{"upload.id":t.id,"validation.checksum_provided":t.checksum?`true`:`false`,"validation.mime_required":n.getCapabilities().requiresMimeTypeValidation?`true`:`false`}})),O=t=>l.tryPromise({try:async()=>await fetch(t),catch:t=>e.fromCode(`UNKNOWN_ERROR`,{cause:t})}).pipe(l.withSpan(`upload-fetch-url`,{attributes:{"upload.url":t,"upload.operation":`fetch`}}),l.tap(e=>l.gen(function*(){yield*d.increment(d.counter(`upload_from_url_total`,{description:`Total number of URL-based uploads`})),e.ok&&(yield*d.increment(d.counter(`upload_from_url_success_total`,{description:`Total number of successful URL-based uploads`})))})),l.tap(e=>l.logInfo(`URL fetch completed`).pipe(l.annotateLogs({"upload.url":t,"response.status":e.status.toString(),"response.ok":e.ok.toString(),"response.content_length":e.headers.get(`content-length`)??`unknown`}))),l.tapError(e=>l.gen(function*(){yield*d.increment(d.counter(`upload_from_url_failed_total`,{description:`Total number of failed URL-based uploads`})),yield*l.logError(`URL fetch failed`).pipe(l.annotateLogs({"upload.url":t,error:String(e)}))}))),k=t=>l.tryPromise({try:async()=>await t.arrayBuffer(),catch:t=>e.fromCode(`UNKNOWN_ERROR`,{cause:t})}).pipe(l.withSpan(`upload-convert-to-buffer`,{attributes:{"upload.operation":`arrayBuffer`}}),l.tap(e=>l.logDebug(`Response converted to array buffer`).pipe(l.annotateLogs({"buffer.size":e.byteLength.toString()}))),l.tapError(e=>l.logError(`Failed to convert response to array buffer`).pipe(l.annotateLogs({error:String(e)}))));var A=class extends c.Tag(`UploadEngine`)(){};function j(){return l.gen(function*(){let e=yield*t,o=yield*i,s=yield*a,c=yield*n;return{upload:(t,n,r)=>l.gen(function*(){return yield*E((yield*x(t,n,{dataStoreService:c,kvStore:e,eventEmitter:o,generateId:s})).id,n,r,{dataStoreService:c,kvStore:e,eventEmitter:o})}),uploadFromUrl:(t,n,r)=>l.gen(function*(){let i=yield*k(yield*O(r)),a=new ReadableStream({start(e){e.enqueue(new Uint8Array(i)),e.close()}});return yield*E((yield*x({...t,size:i.byteLength},n,{dataStoreService:c,kvStore:e,eventEmitter:o,generateId:s})).id,n,a,{dataStoreService:c,kvStore:e,eventEmitter:o})}),createUpload:(t,n)=>l.gen(function*(){return yield*x(t,n,{dataStoreService:c,kvStore:e,eventEmitter:o,generateId:s})}),uploadChunk:(t,n,r)=>l.gen(function*(){return yield*E(t,n,r,{dataStoreService:c,kvStore:e,eventEmitter:o})}),getUpload:t=>l.gen(function*(){return yield*e.get(t)}),read:(t,n)=>l.gen(function*(){let r=yield*e.get(t);return yield*(yield*c.getDataStore(r.storage.id,n)).read(t)}),readStream:(t,n,r)=>l.gen(function*(){let i=yield*e.get(t),a=yield*c.getDataStore(i.storage.id,n);if(a.getCapabilities().supportsStreamingRead&&a.readStream)return yield*l.logDebug(`Using streaming read for file ${t}`),yield*a.readStream(t,r);yield*l.logDebug(`Falling back to buffered read for file ${t} (streaming not supported)`);let o=yield*a.read(t);return m.succeed(o)}),uploadStream:(t,n,i)=>l.gen(function*(){let a=yield*c.getDataStore(t.storageId,n),u=a.getCapabilities(),d=yield*s.generateId();if(u.supportsStreamingWrite&&a.writeStream){yield*l.logDebug(`Using streaming write for file ${d}`);let n=typeof t.metadata==`string`?JSON.parse(t.metadata):t.metadata||{},s=Object.fromEntries(Object.entries(n).map(([e,t])=>[e,String(t)])),c={id:d,offset:0,size:t.size??0,storage:{id:t.storageId,type:a.getCapabilities().supportsStreamingWrite?`streaming`:`default`},metadata:n,creationDate:new Date().toISOString()};yield*e.set(d,c),yield*o.emit(d,{type:r.UPLOAD_STARTED,data:c});let u=yield*a.writeStream(d,{stream:i,contentType:t.type,sizeHint:t.sizeHint,metadata:s}),f={...c,size:u.size,offset:u.size,storage:{...c.storage,path:u.path},...u.url&&{url:u.url}};return yield*e.set(d,f),yield*o.emit(d,{type:r.UPLOAD_COMPLETE,data:f}),f}yield*l.logWarning(`Falling back to buffered upload for file ${d} (streaming write not supported)`);let f=[];yield*m.runForEach(i,e=>l.sync(()=>{f.push(e)}));let p=f.reduce((e,t)=>e+t.length,0),h=new Uint8Array(p),g=0;for(let e of f)h.set(e,g),g+=e.length;let _=new ReadableStream({start(e){e.enqueue(h),e.close()}});return yield*x({...t,size:p},n,{dataStoreService:c,kvStore:e,eventEmitter:o,generateId:{generateId:()=>l.succeed(d)}}),yield*E(d,n,_,{dataStoreService:c,kvStore:e,eventEmitter:o})}),delete:(t,n)=>l.gen(function*(){let r=yield*e.get(t);yield*(yield*c.getDataStore(r.storage.id,n)).remove(t),yield*e.delete(t)}),getCapabilities:(e,t)=>l.gen(function*(){return(yield*c.getDataStore(e,t)).getCapabilities()}),subscribeToUploadEvents:(e,t)=>l.gen(function*(){yield*o.subscribe(e,t)}),unsubscribeFromUploadEvents:e=>l.gen(function*(){yield*o.unsubscribe(e)})}})}const M=u.effect(A,j());var N=class{constructor(e,t){this.capabilities=e,this.validateUploadStrategy=t}negotiateStrategy(e){let t=[],n=[],r=`single`,i=e.preferredChunkSize??this.capabilities.optimalChunkSize??1024*1024,a=e.parallelUploads??1;if(e.preferredStrategy&&(this.validateUploadStrategy(e.preferredStrategy)?(r=e.preferredStrategy,t.push(`Using preferred strategy: ${r}`)):n.push(`Preferred strategy '${e.preferredStrategy}' not supported by data store, falling back`)),(!e.preferredStrategy||!this.validateUploadStrategy(e.preferredStrategy))&&(this.capabilities.supportsParallelUploads&&e.fileSize>(e.minChunkSizeForParallel??10*1024*1024)?(r=`parallel`,t.push(`Selected parallel upload for large file (${e.fileSize} bytes)`)):(r=`single`,t.push(this.capabilities.supportsParallelUploads?`Selected single upload for small file (${e.fileSize} bytes)`:`Selected single upload (parallel not supported by data store)`))),this.capabilities.minChunkSize&&i<this.capabilities.minChunkSize&&(n.push(`Chunk size ${i} below minimum ${this.capabilities.minChunkSize}, adjusting`),i=this.capabilities.minChunkSize),this.capabilities.maxChunkSize&&i>this.capabilities.maxChunkSize&&(n.push(`Chunk size ${i} above maximum ${this.capabilities.maxChunkSize}, adjusting`),i=this.capabilities.maxChunkSize),r===`parallel`&&(this.capabilities.maxConcurrentUploads&&a>this.capabilities.maxConcurrentUploads&&(n.push(`Parallel uploads ${a} exceeds maximum ${this.capabilities.maxConcurrentUploads}, adjusting`),a=this.capabilities.maxConcurrentUploads),this.capabilities.maxParts)){let t=Math.ceil(e.fileSize/i);if(t>this.capabilities.maxParts){let r=Math.ceil(e.fileSize/this.capabilities.maxParts);n.push(`Estimated parts ${t} exceeds maximum ${this.capabilities.maxParts}, increasing chunk size`),i=Math.max(i,r)}}return this.validateUploadStrategy(r)||(n.push(`Final strategy validation failed, falling back to single upload`),r=`single`,a=1),t.push(`Data store capabilities: parallel=${this.capabilities.supportsParallelUploads}, concatenation=${this.capabilities.supportsConcatenation}, resumable=${this.capabilities.supportsResumableUploads}`),{strategy:r,chunkSize:i,parallelUploads:r===`parallel`?a:1,reasoning:t,warnings:n}}getDataStoreCapabilities(){return this.capabilities}validateConfiguration(e){let t=[];return e.preferredStrategy&&!this.validateUploadStrategy(e.preferredStrategy)&&t.push(`Preferred strategy '${e.preferredStrategy}' not supported by data store`),e.preferredChunkSize&&(this.capabilities.minChunkSize&&e.preferredChunkSize<this.capabilities.minChunkSize&&t.push(`Chunk size ${e.preferredChunkSize} below data store minimum ${this.capabilities.minChunkSize}`),this.capabilities.maxChunkSize&&e.preferredChunkSize>this.capabilities.maxChunkSize&&t.push(`Chunk size ${e.preferredChunkSize} above data store maximum ${this.capabilities.maxChunkSize}`)),e.parallelUploads&&this.capabilities.maxConcurrentUploads&&e.parallelUploads>this.capabilities.maxConcurrentUploads&&t.push(`Parallel uploads ${e.parallelUploads} exceeds data store maximum ${this.capabilities.maxConcurrentUploads}`),{valid:t.length===0,errors:t}}};export{k as a,v as c,M as i,A as n,O as o,j as r,y as s,N as t};
|
|
2
|
+
//# sourceMappingURL=upload-strategy-negotiator-C9MeoOnW.mjs.map
|