@scalemule/sdk 0.0.58 → 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.
@@ -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
- };
@@ -1,6 +0,0 @@
1
- import {
2
- UploadResumeStore
3
- } from "./chunk-3FTGBRLU.mjs";
4
- export {
5
- UploadResumeStore
6
- };