@scalemule/sdk 0.0.59 → 0.0.60
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.mjs +295 -16
- package/package.json +1 -1
- package/dist/chunk-3FTGBRLU.mjs +0 -158
- package/dist/upload-compression-VOUJRAIM.mjs +0 -97
- package/dist/upload-resume-RXLHBH5E.mjs +0 -6
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,282 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
+
var __esm = (fn, res) => function __init() {
|
|
4
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
5
|
+
};
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// src/services/upload-resume.ts
|
|
12
|
+
var upload_resume_exports = {};
|
|
13
|
+
__export(upload_resume_exports, {
|
|
14
|
+
UploadResumeStore: () => UploadResumeStore
|
|
15
|
+
});
|
|
16
|
+
var DB_NAME, STORE_NAME, DB_VERSION, MAX_AGE_MS, UploadResumeStore;
|
|
17
|
+
var init_upload_resume = __esm({
|
|
18
|
+
"src/services/upload-resume.ts"() {
|
|
19
|
+
"use strict";
|
|
20
|
+
DB_NAME = "sm_upload_sessions_v1";
|
|
21
|
+
STORE_NAME = "sessions";
|
|
22
|
+
DB_VERSION = 1;
|
|
23
|
+
MAX_AGE_MS = 24 * 60 * 60 * 1e3;
|
|
24
|
+
UploadResumeStore = class {
|
|
25
|
+
constructor() {
|
|
26
|
+
this.db = null;
|
|
27
|
+
}
|
|
28
|
+
/** Generate a deterministic resume key from upload identity */
|
|
29
|
+
static async generateResumeKey(appId, userId, filename, size, lastModified) {
|
|
30
|
+
const raw = `${appId}:${userId}:${filename}:${size}:${lastModified ?? 0}`;
|
|
31
|
+
if (typeof crypto !== "undefined" && crypto.subtle) {
|
|
32
|
+
const buffer = new TextEncoder().encode(raw);
|
|
33
|
+
const hash2 = await crypto.subtle.digest("SHA-256", buffer);
|
|
34
|
+
return Array.from(new Uint8Array(hash2)).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
35
|
+
}
|
|
36
|
+
let hash = 0;
|
|
37
|
+
for (let i = 0; i < raw.length; i++) {
|
|
38
|
+
const chr = raw.charCodeAt(i);
|
|
39
|
+
hash = (hash << 5) - hash + chr;
|
|
40
|
+
hash |= 0;
|
|
41
|
+
}
|
|
42
|
+
return `fallback_${Math.abs(hash).toString(36)}`;
|
|
43
|
+
}
|
|
44
|
+
/** Open the IndexedDB store. No-ops if IndexedDB is unavailable. */
|
|
45
|
+
async open() {
|
|
46
|
+
if (typeof indexedDB === "undefined") return;
|
|
47
|
+
return new Promise((resolve, reject) => {
|
|
48
|
+
const request = indexedDB.open(DB_NAME, DB_VERSION);
|
|
49
|
+
request.onupgradeneeded = () => {
|
|
50
|
+
const db = request.result;
|
|
51
|
+
if (!db.objectStoreNames.contains(STORE_NAME)) {
|
|
52
|
+
const store = db.createObjectStore(STORE_NAME, { keyPath: "key" });
|
|
53
|
+
store.createIndex("updated_at", "updated_at");
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
request.onsuccess = () => {
|
|
57
|
+
this.db = request.result;
|
|
58
|
+
resolve();
|
|
59
|
+
};
|
|
60
|
+
request.onerror = () => {
|
|
61
|
+
reject(request.error);
|
|
62
|
+
};
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
/** Get a resume session by key. Returns null if not found or stale. */
|
|
66
|
+
async get(key) {
|
|
67
|
+
if (!this.db) return null;
|
|
68
|
+
return new Promise((resolve) => {
|
|
69
|
+
const tx = this.db.transaction(STORE_NAME, "readonly");
|
|
70
|
+
const store = tx.objectStore(STORE_NAME);
|
|
71
|
+
const request = store.get(key);
|
|
72
|
+
request.onsuccess = () => {
|
|
73
|
+
const entry = request.result;
|
|
74
|
+
if (!entry) {
|
|
75
|
+
resolve(null);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
if (Date.now() - entry.updated_at > MAX_AGE_MS) {
|
|
79
|
+
this.remove(key).catch(() => {
|
|
80
|
+
});
|
|
81
|
+
resolve(null);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
resolve(entry.session);
|
|
85
|
+
};
|
|
86
|
+
request.onerror = () => resolve(null);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
/** Save a new resume session. */
|
|
90
|
+
async save(key, session) {
|
|
91
|
+
if (!this.db) return;
|
|
92
|
+
return new Promise((resolve, reject) => {
|
|
93
|
+
const tx = this.db.transaction(STORE_NAME, "readwrite");
|
|
94
|
+
const store = tx.objectStore(STORE_NAME);
|
|
95
|
+
const entry = {
|
|
96
|
+
key,
|
|
97
|
+
session: { ...session, created_at: Date.now() },
|
|
98
|
+
updated_at: Date.now()
|
|
99
|
+
};
|
|
100
|
+
const request = store.put(entry);
|
|
101
|
+
request.onsuccess = () => resolve();
|
|
102
|
+
request.onerror = () => reject(request.error);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
/** Update a single completed part in an existing session. */
|
|
106
|
+
async updatePart(key, partNumber, etag) {
|
|
107
|
+
if (!this.db) return;
|
|
108
|
+
return new Promise((resolve) => {
|
|
109
|
+
const tx = this.db.transaction(STORE_NAME, "readwrite");
|
|
110
|
+
const store = tx.objectStore(STORE_NAME);
|
|
111
|
+
const getRequest = store.get(key);
|
|
112
|
+
getRequest.onsuccess = () => {
|
|
113
|
+
const entry = getRequest.result;
|
|
114
|
+
if (!entry) {
|
|
115
|
+
resolve();
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const existing = entry.session.completed_parts.find((p) => p.part_number === partNumber);
|
|
119
|
+
if (!existing) {
|
|
120
|
+
entry.session.completed_parts.push({ part_number: partNumber, etag });
|
|
121
|
+
}
|
|
122
|
+
entry.updated_at = Date.now();
|
|
123
|
+
const putRequest = store.put(entry);
|
|
124
|
+
putRequest.onsuccess = () => resolve();
|
|
125
|
+
putRequest.onerror = () => resolve();
|
|
126
|
+
};
|
|
127
|
+
getRequest.onerror = () => resolve();
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
/** Remove a resume session (e.g., after successful completion). */
|
|
131
|
+
async remove(key) {
|
|
132
|
+
if (!this.db) return;
|
|
133
|
+
return new Promise((resolve) => {
|
|
134
|
+
const tx = this.db.transaction(STORE_NAME, "readwrite");
|
|
135
|
+
const store = tx.objectStore(STORE_NAME);
|
|
136
|
+
const request = store.delete(key);
|
|
137
|
+
request.onsuccess = () => resolve();
|
|
138
|
+
request.onerror = () => resolve();
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
/** Purge all stale entries (older than MAX_AGE_MS). */
|
|
142
|
+
async purgeStale() {
|
|
143
|
+
if (!this.db) return 0;
|
|
144
|
+
return new Promise((resolve) => {
|
|
145
|
+
const cutoff = Date.now() - MAX_AGE_MS;
|
|
146
|
+
const tx = this.db.transaction(STORE_NAME, "readwrite");
|
|
147
|
+
const store = tx.objectStore(STORE_NAME);
|
|
148
|
+
const index = store.index("updated_at");
|
|
149
|
+
const range = IDBKeyRange.upperBound(cutoff);
|
|
150
|
+
const request = index.openCursor(range);
|
|
151
|
+
let count = 0;
|
|
152
|
+
request.onsuccess = () => {
|
|
153
|
+
const cursor = request.result;
|
|
154
|
+
if (cursor) {
|
|
155
|
+
cursor.delete();
|
|
156
|
+
count++;
|
|
157
|
+
cursor.continue();
|
|
158
|
+
} else {
|
|
159
|
+
resolve(count);
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
request.onerror = () => resolve(0);
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
/** Close the database connection. */
|
|
166
|
+
close() {
|
|
167
|
+
if (this.db) {
|
|
168
|
+
this.db.close();
|
|
169
|
+
this.db = null;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
// src/services/upload-compression.ts
|
|
177
|
+
var upload_compression_exports = {};
|
|
178
|
+
__export(upload_compression_exports, {
|
|
179
|
+
maybeCompressImage: () => maybeCompressImage
|
|
180
|
+
});
|
|
181
|
+
async function maybeCompressImage(file, userConfig, sessionId, telemetry) {
|
|
182
|
+
const type = file.type?.toLowerCase() || "";
|
|
183
|
+
if (!type.startsWith("image/")) return null;
|
|
184
|
+
if (SKIP_TYPES.has(type)) {
|
|
185
|
+
telemetry?.emit(sessionId, "upload.compression.skipped", { reason: "format", type });
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
if (!COMPRESSIBLE_TYPES.has(type)) {
|
|
189
|
+
telemetry?.emit(sessionId, "upload.compression.skipped", { reason: "unsupported_type", type });
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
if (file.size < MIN_COMPRESS_SIZE) {
|
|
193
|
+
telemetry?.emit(sessionId, "upload.compression.skipped", { reason: "too_small", size: file.size });
|
|
194
|
+
return null;
|
|
195
|
+
}
|
|
196
|
+
const networkType = getNetworkEffectiveType();
|
|
197
|
+
const defaultProfile = { maxWidth: 3840, maxHeight: 3840, quality: 0.85, maxSizeMB: 5 };
|
|
198
|
+
const networkProfile = NETWORK_PROFILES[networkType] ?? defaultProfile;
|
|
199
|
+
const config = {
|
|
200
|
+
maxWidth: userConfig?.maxWidth ?? networkProfile.maxWidth,
|
|
201
|
+
maxHeight: userConfig?.maxHeight ?? networkProfile.maxHeight,
|
|
202
|
+
quality: userConfig?.quality ?? networkProfile.quality,
|
|
203
|
+
maxSizeMB: userConfig?.maxSizeMB ?? networkProfile.maxSizeMB
|
|
204
|
+
};
|
|
205
|
+
telemetry?.emit(sessionId, "upload.compression.started", {
|
|
206
|
+
original_size: file.size,
|
|
207
|
+
network: networkType,
|
|
208
|
+
target_quality: config.quality
|
|
209
|
+
});
|
|
210
|
+
try {
|
|
211
|
+
const imageCompression = await loadImageCompression();
|
|
212
|
+
if (!imageCompression) {
|
|
213
|
+
telemetry?.emit(sessionId, "upload.compression.skipped", { reason: "library_unavailable" });
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
216
|
+
const compressed = await imageCompression(file, {
|
|
217
|
+
maxSizeMB: config.maxSizeMB,
|
|
218
|
+
maxWidthOrHeight: Math.max(config.maxWidth, config.maxHeight),
|
|
219
|
+
initialQuality: config.quality,
|
|
220
|
+
useWebWorker: true,
|
|
221
|
+
fileType: type === "image/png" ? "image/webp" : void 0
|
|
222
|
+
});
|
|
223
|
+
if (compressed.size >= file.size * 0.95) {
|
|
224
|
+
telemetry?.emit(sessionId, "upload.compression.skipped", {
|
|
225
|
+
reason: "no_size_reduction",
|
|
226
|
+
original_size: file.size,
|
|
227
|
+
compressed_size: compressed.size
|
|
228
|
+
});
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
telemetry?.emit(sessionId, "upload.compression.completed", {
|
|
232
|
+
original_size: file.size,
|
|
233
|
+
compressed_size: compressed.size,
|
|
234
|
+
ratio: (compressed.size / file.size).toFixed(2)
|
|
235
|
+
});
|
|
236
|
+
return compressed;
|
|
237
|
+
} catch (err) {
|
|
238
|
+
telemetry?.emit(sessionId, "upload.compression.skipped", {
|
|
239
|
+
reason: "error",
|
|
240
|
+
error: err instanceof Error ? err.message : "Unknown compression error"
|
|
241
|
+
});
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
async function loadImageCompression() {
|
|
246
|
+
if (cachedImport === false) return null;
|
|
247
|
+
if (cachedImport) return cachedImport;
|
|
248
|
+
try {
|
|
249
|
+
const mod = await Function('return import("browser-image-compression")')();
|
|
250
|
+
cachedImport = mod.default || mod;
|
|
251
|
+
return cachedImport;
|
|
252
|
+
} catch {
|
|
253
|
+
cachedImport = false;
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
function getNetworkEffectiveType() {
|
|
258
|
+
if (typeof navigator !== "undefined" && "connection" in navigator) {
|
|
259
|
+
const conn = navigator.connection;
|
|
260
|
+
return conn?.effectiveType || "4g";
|
|
261
|
+
}
|
|
262
|
+
return "4g";
|
|
263
|
+
}
|
|
264
|
+
var MIN_COMPRESS_SIZE, COMPRESSIBLE_TYPES, SKIP_TYPES, NETWORK_PROFILES, cachedImport;
|
|
265
|
+
var init_upload_compression = __esm({
|
|
266
|
+
"src/services/upload-compression.ts"() {
|
|
267
|
+
"use strict";
|
|
268
|
+
MIN_COMPRESS_SIZE = 100 * 1024;
|
|
269
|
+
COMPRESSIBLE_TYPES = /* @__PURE__ */ new Set(["image/jpeg", "image/jpg", "image/png", "image/bmp", "image/tiff"]);
|
|
270
|
+
SKIP_TYPES = /* @__PURE__ */ new Set(["image/gif", "image/svg+xml", "image/webp", "image/avif"]);
|
|
271
|
+
NETWORK_PROFILES = {
|
|
272
|
+
"slow-2g": { maxWidth: 1280, maxHeight: 1280, quality: 0.6, maxSizeMB: 0.5 },
|
|
273
|
+
"2g": { maxWidth: 1600, maxHeight: 1600, quality: 0.65, maxSizeMB: 1 },
|
|
274
|
+
"3g": { maxWidth: 2048, maxHeight: 2048, quality: 0.75, maxSizeMB: 2 },
|
|
275
|
+
"4g": { maxWidth: 3840, maxHeight: 3840, quality: 0.85, maxSizeMB: 5 }
|
|
276
|
+
};
|
|
277
|
+
cachedImport = null;
|
|
278
|
+
}
|
|
279
|
+
});
|
|
4
280
|
|
|
5
281
|
// src/types.ts
|
|
6
282
|
var ErrorCodes = {
|
|
@@ -2128,7 +2404,7 @@ var StorageService = class extends ServiceModule {
|
|
|
2128
2404
|
size_bytes: file.size,
|
|
2129
2405
|
content_type: file.type,
|
|
2130
2406
|
strategy: this.shouldUseMultipart(file, options) ? "multipart" : "direct",
|
|
2131
|
-
network_type:
|
|
2407
|
+
network_type: getNetworkEffectiveType2()
|
|
2132
2408
|
});
|
|
2133
2409
|
try {
|
|
2134
2410
|
let uploadFile = file;
|
|
@@ -2344,7 +2620,7 @@ var StorageService = class extends ServiceModule {
|
|
|
2344
2620
|
let resumeData = null;
|
|
2345
2621
|
if (options?.resume !== "off" && typeof window !== "undefined") {
|
|
2346
2622
|
try {
|
|
2347
|
-
const { UploadResumeStore: UploadResumeStore2 } = await
|
|
2623
|
+
const { UploadResumeStore: UploadResumeStore2 } = await Promise.resolve().then(() => (init_upload_resume(), upload_resume_exports));
|
|
2348
2624
|
resumeStore = new UploadResumeStore2();
|
|
2349
2625
|
await resumeStore.open();
|
|
2350
2626
|
const resumeKey = await UploadResumeStore2.generateResumeKey(
|
|
@@ -2392,7 +2668,7 @@ var StorageService = class extends ServiceModule {
|
|
|
2392
2668
|
} else {
|
|
2393
2669
|
let clientUploadKey;
|
|
2394
2670
|
try {
|
|
2395
|
-
const { UploadResumeStore: UploadResumeStore2 } = await
|
|
2671
|
+
const { UploadResumeStore: UploadResumeStore2 } = await Promise.resolve().then(() => (init_upload_resume(), upload_resume_exports));
|
|
2396
2672
|
clientUploadKey = await UploadResumeStore2.generateResumeKey(
|
|
2397
2673
|
this.client.getApiKey?.() || "",
|
|
2398
2674
|
this.client.getUserId?.() || "",
|
|
@@ -2425,7 +2701,7 @@ var StorageService = class extends ServiceModule {
|
|
|
2425
2701
|
const { upload_session_id, file_id, part_size_bytes, total_parts } = startData;
|
|
2426
2702
|
if (resumeStore && !resumeData) {
|
|
2427
2703
|
try {
|
|
2428
|
-
const { UploadResumeStore: UploadResumeStore2 } = await
|
|
2704
|
+
const { UploadResumeStore: UploadResumeStore2 } = await Promise.resolve().then(() => (init_upload_resume(), upload_resume_exports));
|
|
2429
2705
|
const resumeKey = await UploadResumeStore2.generateResumeKey(
|
|
2430
2706
|
this.client.getApiKey?.() || "",
|
|
2431
2707
|
this.client.getUserId?.() || "",
|
|
@@ -2576,7 +2852,7 @@ var StorageService = class extends ServiceModule {
|
|
|
2576
2852
|
telemetry?.emit(sessionId, "upload.multipart.part_completed", { part_number: result.partNum });
|
|
2577
2853
|
if (resumeStore) {
|
|
2578
2854
|
try {
|
|
2579
|
-
const { UploadResumeStore: UploadResumeStore2 } = await
|
|
2855
|
+
const { UploadResumeStore: UploadResumeStore2 } = await Promise.resolve().then(() => (init_upload_resume(), upload_resume_exports));
|
|
2580
2856
|
const resumeKey = await UploadResumeStore2.generateResumeKey(
|
|
2581
2857
|
this.client.getApiKey?.() || "",
|
|
2582
2858
|
this.client.getUserId?.() || "",
|
|
@@ -2628,7 +2904,7 @@ var StorageService = class extends ServiceModule {
|
|
|
2628
2904
|
);
|
|
2629
2905
|
if (resumeStore) {
|
|
2630
2906
|
try {
|
|
2631
|
-
const { UploadResumeStore: UploadResumeStore2 } = await
|
|
2907
|
+
const { UploadResumeStore: UploadResumeStore2 } = await Promise.resolve().then(() => (init_upload_resume(), upload_resume_exports));
|
|
2632
2908
|
const resumeKey = await UploadResumeStore2.generateResumeKey(
|
|
2633
2909
|
this.client.getApiKey?.() || "",
|
|
2634
2910
|
this.client.getUserId?.() || "",
|
|
@@ -3267,21 +3543,21 @@ var StorageService = class extends ServiceModule {
|
|
|
3267
3543
|
}
|
|
3268
3544
|
defaultChunkSize(fileSize) {
|
|
3269
3545
|
if (fileSize > 512 * 1024 * 1024) return 16 * 1024 * 1024;
|
|
3270
|
-
const effectiveType =
|
|
3546
|
+
const effectiveType = getNetworkEffectiveType2();
|
|
3271
3547
|
if (effectiveType === "slow-2g" || effectiveType === "2g") return 5 * 1024 * 1024;
|
|
3272
3548
|
if (effectiveType === "3g") return 5 * 1024 * 1024;
|
|
3273
3549
|
return 8 * 1024 * 1024;
|
|
3274
3550
|
}
|
|
3275
3551
|
defaultConcurrency() {
|
|
3276
|
-
const effectiveType =
|
|
3552
|
+
const effectiveType = getNetworkEffectiveType2();
|
|
3277
3553
|
if (effectiveType === "slow-2g" || effectiveType === "2g") return 1;
|
|
3278
3554
|
if (effectiveType === "3g") return 2;
|
|
3279
3555
|
return 4;
|
|
3280
3556
|
}
|
|
3281
3557
|
async maybeCompress(file, config, sessionId, telemetry) {
|
|
3282
3558
|
try {
|
|
3283
|
-
const { maybeCompressImage } = await
|
|
3284
|
-
return await
|
|
3559
|
+
const { maybeCompressImage: maybeCompressImage2 } = await Promise.resolve().then(() => (init_upload_compression(), upload_compression_exports));
|
|
3560
|
+
return await maybeCompressImage2(file, config, sessionId, telemetry);
|
|
3285
3561
|
} catch {
|
|
3286
3562
|
return null;
|
|
3287
3563
|
}
|
|
@@ -3321,10 +3597,10 @@ function getStallTimeout() {
|
|
|
3321
3597
|
return isSlowNetwork() ? SLOW_NETWORK_STALL_TIMEOUT_MS : DEFAULT_STALL_TIMEOUT_MS;
|
|
3322
3598
|
}
|
|
3323
3599
|
function isSlowNetwork() {
|
|
3324
|
-
const effectiveType =
|
|
3600
|
+
const effectiveType = getNetworkEffectiveType2();
|
|
3325
3601
|
return effectiveType === "slow-2g" || effectiveType === "2g" || effectiveType === "3g";
|
|
3326
3602
|
}
|
|
3327
|
-
function
|
|
3603
|
+
function getNetworkEffectiveType2() {
|
|
3328
3604
|
if (typeof navigator !== "undefined" && "connection" in navigator) {
|
|
3329
3605
|
const conn = navigator.connection;
|
|
3330
3606
|
return conn?.effectiveType || "4g";
|
|
@@ -3337,7 +3613,7 @@ function getOnlineStatus() {
|
|
|
3337
3613
|
}
|
|
3338
3614
|
function getUploadEnvironmentDiagnostics() {
|
|
3339
3615
|
const diagnostics = {
|
|
3340
|
-
network_type:
|
|
3616
|
+
network_type: getNetworkEffectiveType2(),
|
|
3341
3617
|
online: getOnlineStatus()
|
|
3342
3618
|
};
|
|
3343
3619
|
if (typeof navigator !== "undefined") {
|
|
@@ -3369,6 +3645,9 @@ function extractS3ErrorCode(body) {
|
|
|
3369
3645
|
return m ? m[1] : void 0;
|
|
3370
3646
|
}
|
|
3371
3647
|
|
|
3648
|
+
// src/index.ts
|
|
3649
|
+
init_upload_resume();
|
|
3650
|
+
|
|
3372
3651
|
// src/services/upload-strategy.ts
|
|
3373
3652
|
var MULTIPART_THRESHOLD2 = 8 * 1024 * 1024;
|
|
3374
3653
|
var MULTIPART_THRESHOLD_SLOW2 = 1 * 1024 * 1024;
|
package/package.json
CHANGED
package/dist/chunk-3FTGBRLU.mjs
DELETED
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
// src/services/upload-resume.ts
|
|
2
|
-
var DB_NAME = "sm_upload_sessions_v1";
|
|
3
|
-
var STORE_NAME = "sessions";
|
|
4
|
-
var DB_VERSION = 1;
|
|
5
|
-
var MAX_AGE_MS = 24 * 60 * 60 * 1e3;
|
|
6
|
-
var UploadResumeStore = class {
|
|
7
|
-
constructor() {
|
|
8
|
-
this.db = null;
|
|
9
|
-
}
|
|
10
|
-
/** Generate a deterministic resume key from upload identity */
|
|
11
|
-
static async generateResumeKey(appId, userId, filename, size, lastModified) {
|
|
12
|
-
const raw = `${appId}:${userId}:${filename}:${size}:${lastModified ?? 0}`;
|
|
13
|
-
if (typeof crypto !== "undefined" && crypto.subtle) {
|
|
14
|
-
const buffer = new TextEncoder().encode(raw);
|
|
15
|
-
const hash2 = await crypto.subtle.digest("SHA-256", buffer);
|
|
16
|
-
return Array.from(new Uint8Array(hash2)).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
17
|
-
}
|
|
18
|
-
let hash = 0;
|
|
19
|
-
for (let i = 0; i < raw.length; i++) {
|
|
20
|
-
const chr = raw.charCodeAt(i);
|
|
21
|
-
hash = (hash << 5) - hash + chr;
|
|
22
|
-
hash |= 0;
|
|
23
|
-
}
|
|
24
|
-
return `fallback_${Math.abs(hash).toString(36)}`;
|
|
25
|
-
}
|
|
26
|
-
/** Open the IndexedDB store. No-ops if IndexedDB is unavailable. */
|
|
27
|
-
async open() {
|
|
28
|
-
if (typeof indexedDB === "undefined") return;
|
|
29
|
-
return new Promise((resolve, reject) => {
|
|
30
|
-
const request = indexedDB.open(DB_NAME, DB_VERSION);
|
|
31
|
-
request.onupgradeneeded = () => {
|
|
32
|
-
const db = request.result;
|
|
33
|
-
if (!db.objectStoreNames.contains(STORE_NAME)) {
|
|
34
|
-
const store = db.createObjectStore(STORE_NAME, { keyPath: "key" });
|
|
35
|
-
store.createIndex("updated_at", "updated_at");
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
request.onsuccess = () => {
|
|
39
|
-
this.db = request.result;
|
|
40
|
-
resolve();
|
|
41
|
-
};
|
|
42
|
-
request.onerror = () => {
|
|
43
|
-
reject(request.error);
|
|
44
|
-
};
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
/** Get a resume session by key. Returns null if not found or stale. */
|
|
48
|
-
async get(key) {
|
|
49
|
-
if (!this.db) return null;
|
|
50
|
-
return new Promise((resolve) => {
|
|
51
|
-
const tx = this.db.transaction(STORE_NAME, "readonly");
|
|
52
|
-
const store = tx.objectStore(STORE_NAME);
|
|
53
|
-
const request = store.get(key);
|
|
54
|
-
request.onsuccess = () => {
|
|
55
|
-
const entry = request.result;
|
|
56
|
-
if (!entry) {
|
|
57
|
-
resolve(null);
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
if (Date.now() - entry.updated_at > MAX_AGE_MS) {
|
|
61
|
-
this.remove(key).catch(() => {
|
|
62
|
-
});
|
|
63
|
-
resolve(null);
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
resolve(entry.session);
|
|
67
|
-
};
|
|
68
|
-
request.onerror = () => resolve(null);
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
/** Save a new resume session. */
|
|
72
|
-
async save(key, session) {
|
|
73
|
-
if (!this.db) return;
|
|
74
|
-
return new Promise((resolve, reject) => {
|
|
75
|
-
const tx = this.db.transaction(STORE_NAME, "readwrite");
|
|
76
|
-
const store = tx.objectStore(STORE_NAME);
|
|
77
|
-
const entry = {
|
|
78
|
-
key,
|
|
79
|
-
session: { ...session, created_at: Date.now() },
|
|
80
|
-
updated_at: Date.now()
|
|
81
|
-
};
|
|
82
|
-
const request = store.put(entry);
|
|
83
|
-
request.onsuccess = () => resolve();
|
|
84
|
-
request.onerror = () => reject(request.error);
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
/** Update a single completed part in an existing session. */
|
|
88
|
-
async updatePart(key, partNumber, etag) {
|
|
89
|
-
if (!this.db) return;
|
|
90
|
-
return new Promise((resolve) => {
|
|
91
|
-
const tx = this.db.transaction(STORE_NAME, "readwrite");
|
|
92
|
-
const store = tx.objectStore(STORE_NAME);
|
|
93
|
-
const getRequest = store.get(key);
|
|
94
|
-
getRequest.onsuccess = () => {
|
|
95
|
-
const entry = getRequest.result;
|
|
96
|
-
if (!entry) {
|
|
97
|
-
resolve();
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
const existing = entry.session.completed_parts.find((p) => p.part_number === partNumber);
|
|
101
|
-
if (!existing) {
|
|
102
|
-
entry.session.completed_parts.push({ part_number: partNumber, etag });
|
|
103
|
-
}
|
|
104
|
-
entry.updated_at = Date.now();
|
|
105
|
-
const putRequest = store.put(entry);
|
|
106
|
-
putRequest.onsuccess = () => resolve();
|
|
107
|
-
putRequest.onerror = () => resolve();
|
|
108
|
-
};
|
|
109
|
-
getRequest.onerror = () => resolve();
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
/** Remove a resume session (e.g., after successful completion). */
|
|
113
|
-
async remove(key) {
|
|
114
|
-
if (!this.db) return;
|
|
115
|
-
return new Promise((resolve) => {
|
|
116
|
-
const tx = this.db.transaction(STORE_NAME, "readwrite");
|
|
117
|
-
const store = tx.objectStore(STORE_NAME);
|
|
118
|
-
const request = store.delete(key);
|
|
119
|
-
request.onsuccess = () => resolve();
|
|
120
|
-
request.onerror = () => resolve();
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
/** Purge all stale entries (older than MAX_AGE_MS). */
|
|
124
|
-
async purgeStale() {
|
|
125
|
-
if (!this.db) return 0;
|
|
126
|
-
return new Promise((resolve) => {
|
|
127
|
-
const cutoff = Date.now() - MAX_AGE_MS;
|
|
128
|
-
const tx = this.db.transaction(STORE_NAME, "readwrite");
|
|
129
|
-
const store = tx.objectStore(STORE_NAME);
|
|
130
|
-
const index = store.index("updated_at");
|
|
131
|
-
const range = IDBKeyRange.upperBound(cutoff);
|
|
132
|
-
const request = index.openCursor(range);
|
|
133
|
-
let count = 0;
|
|
134
|
-
request.onsuccess = () => {
|
|
135
|
-
const cursor = request.result;
|
|
136
|
-
if (cursor) {
|
|
137
|
-
cursor.delete();
|
|
138
|
-
count++;
|
|
139
|
-
cursor.continue();
|
|
140
|
-
} else {
|
|
141
|
-
resolve(count);
|
|
142
|
-
}
|
|
143
|
-
};
|
|
144
|
-
request.onerror = () => resolve(0);
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
/** Close the database connection. */
|
|
148
|
-
close() {
|
|
149
|
-
if (this.db) {
|
|
150
|
-
this.db.close();
|
|
151
|
-
this.db = null;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
export {
|
|
157
|
-
UploadResumeStore
|
|
158
|
-
};
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
// src/services/upload-compression.ts
|
|
2
|
-
var MIN_COMPRESS_SIZE = 100 * 1024;
|
|
3
|
-
var COMPRESSIBLE_TYPES = /* @__PURE__ */ new Set(["image/jpeg", "image/jpg", "image/png", "image/bmp", "image/tiff"]);
|
|
4
|
-
var SKIP_TYPES = /* @__PURE__ */ new Set(["image/gif", "image/svg+xml", "image/webp", "image/avif"]);
|
|
5
|
-
var NETWORK_PROFILES = {
|
|
6
|
-
"slow-2g": { maxWidth: 1280, maxHeight: 1280, quality: 0.6, maxSizeMB: 0.5 },
|
|
7
|
-
"2g": { maxWidth: 1600, maxHeight: 1600, quality: 0.65, maxSizeMB: 1 },
|
|
8
|
-
"3g": { maxWidth: 2048, maxHeight: 2048, quality: 0.75, maxSizeMB: 2 },
|
|
9
|
-
"4g": { maxWidth: 3840, maxHeight: 3840, quality: 0.85, maxSizeMB: 5 }
|
|
10
|
-
};
|
|
11
|
-
async function maybeCompressImage(file, userConfig, sessionId, telemetry) {
|
|
12
|
-
const type = file.type?.toLowerCase() || "";
|
|
13
|
-
if (!type.startsWith("image/")) return null;
|
|
14
|
-
if (SKIP_TYPES.has(type)) {
|
|
15
|
-
telemetry?.emit(sessionId, "upload.compression.skipped", { reason: "format", type });
|
|
16
|
-
return null;
|
|
17
|
-
}
|
|
18
|
-
if (!COMPRESSIBLE_TYPES.has(type)) {
|
|
19
|
-
telemetry?.emit(sessionId, "upload.compression.skipped", { reason: "unsupported_type", type });
|
|
20
|
-
return null;
|
|
21
|
-
}
|
|
22
|
-
if (file.size < MIN_COMPRESS_SIZE) {
|
|
23
|
-
telemetry?.emit(sessionId, "upload.compression.skipped", { reason: "too_small", size: file.size });
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
const networkType = getNetworkEffectiveType();
|
|
27
|
-
const defaultProfile = { maxWidth: 3840, maxHeight: 3840, quality: 0.85, maxSizeMB: 5 };
|
|
28
|
-
const networkProfile = NETWORK_PROFILES[networkType] ?? defaultProfile;
|
|
29
|
-
const config = {
|
|
30
|
-
maxWidth: userConfig?.maxWidth ?? networkProfile.maxWidth,
|
|
31
|
-
maxHeight: userConfig?.maxHeight ?? networkProfile.maxHeight,
|
|
32
|
-
quality: userConfig?.quality ?? networkProfile.quality,
|
|
33
|
-
maxSizeMB: userConfig?.maxSizeMB ?? networkProfile.maxSizeMB
|
|
34
|
-
};
|
|
35
|
-
telemetry?.emit(sessionId, "upload.compression.started", {
|
|
36
|
-
original_size: file.size,
|
|
37
|
-
network: networkType,
|
|
38
|
-
target_quality: config.quality
|
|
39
|
-
});
|
|
40
|
-
try {
|
|
41
|
-
const imageCompression = await loadImageCompression();
|
|
42
|
-
if (!imageCompression) {
|
|
43
|
-
telemetry?.emit(sessionId, "upload.compression.skipped", { reason: "library_unavailable" });
|
|
44
|
-
return null;
|
|
45
|
-
}
|
|
46
|
-
const compressed = await imageCompression(file, {
|
|
47
|
-
maxSizeMB: config.maxSizeMB,
|
|
48
|
-
maxWidthOrHeight: Math.max(config.maxWidth, config.maxHeight),
|
|
49
|
-
initialQuality: config.quality,
|
|
50
|
-
useWebWorker: true,
|
|
51
|
-
fileType: type === "image/png" ? "image/webp" : void 0
|
|
52
|
-
});
|
|
53
|
-
if (compressed.size >= file.size * 0.95) {
|
|
54
|
-
telemetry?.emit(sessionId, "upload.compression.skipped", {
|
|
55
|
-
reason: "no_size_reduction",
|
|
56
|
-
original_size: file.size,
|
|
57
|
-
compressed_size: compressed.size
|
|
58
|
-
});
|
|
59
|
-
return null;
|
|
60
|
-
}
|
|
61
|
-
telemetry?.emit(sessionId, "upload.compression.completed", {
|
|
62
|
-
original_size: file.size,
|
|
63
|
-
compressed_size: compressed.size,
|
|
64
|
-
ratio: (compressed.size / file.size).toFixed(2)
|
|
65
|
-
});
|
|
66
|
-
return compressed;
|
|
67
|
-
} catch (err) {
|
|
68
|
-
telemetry?.emit(sessionId, "upload.compression.skipped", {
|
|
69
|
-
reason: "error",
|
|
70
|
-
error: err instanceof Error ? err.message : "Unknown compression error"
|
|
71
|
-
});
|
|
72
|
-
return null;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
var cachedImport = null;
|
|
76
|
-
async function loadImageCompression() {
|
|
77
|
-
if (cachedImport === false) return null;
|
|
78
|
-
if (cachedImport) return cachedImport;
|
|
79
|
-
try {
|
|
80
|
-
const mod = await Function('return import("browser-image-compression")')();
|
|
81
|
-
cachedImport = mod.default || mod;
|
|
82
|
-
return cachedImport;
|
|
83
|
-
} catch {
|
|
84
|
-
cachedImport = false;
|
|
85
|
-
return null;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
function getNetworkEffectiveType() {
|
|
89
|
-
if (typeof navigator !== "undefined" && "connection" in navigator) {
|
|
90
|
-
const conn = navigator.connection;
|
|
91
|
-
return conn?.effectiveType || "4g";
|
|
92
|
-
}
|
|
93
|
-
return "4g";
|
|
94
|
-
}
|
|
95
|
-
export {
|
|
96
|
-
maybeCompressImage
|
|
97
|
-
};
|