@scalemule/nextjs 0.1.33 → 0.1.34

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/client.js CHANGED
@@ -1,400 +1,8 @@
1
1
  'use strict';
2
2
 
3
- // node_modules/@scalemule/sdk/dist/index.mjs
4
- var __defProp = Object.defineProperty;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __esm = (fn, res) => function __init() {
7
- return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
8
- };
9
- var __export = (target, all) => {
10
- for (var name in all)
11
- __defProp(target, name, { get: all[name], enumerable: true });
12
- };
13
- var upload_resume_exports = {};
14
- __export(upload_resume_exports, {
15
- UploadResumeStore: () => UploadResumeStore
16
- });
17
- var DB_NAME;
18
- var STORE_NAME;
19
- var DB_VERSION;
20
- var MAX_AGE_MS;
21
- var UploadResumeStore;
22
- var init_upload_resume = __esm({
23
- "src/services/upload-resume.ts"() {
24
- DB_NAME = "sm_upload_sessions_v1";
25
- STORE_NAME = "sessions";
26
- DB_VERSION = 1;
27
- MAX_AGE_MS = 24 * 60 * 60 * 1e3;
28
- UploadResumeStore = class {
29
- constructor() {
30
- this.db = null;
31
- }
32
- /** Generate a deterministic resume key from upload identity */
33
- static async generateResumeKey(appId, userId, filename, size, lastModified) {
34
- const raw = `${appId}:${userId}:${filename}:${size}:${lastModified ?? 0}`;
35
- if (typeof crypto !== "undefined" && crypto.subtle) {
36
- const buffer = new TextEncoder().encode(raw);
37
- const hash2 = await crypto.subtle.digest("SHA-256", buffer);
38
- return Array.from(new Uint8Array(hash2)).map((b) => b.toString(16).padStart(2, "0")).join("");
39
- }
40
- let hash = 0;
41
- for (let i = 0; i < raw.length; i++) {
42
- const chr = raw.charCodeAt(i);
43
- hash = (hash << 5) - hash + chr;
44
- hash |= 0;
45
- }
46
- return `fallback_${Math.abs(hash).toString(36)}`;
47
- }
48
- /** Open the IndexedDB store. No-ops if IndexedDB is unavailable. */
49
- async open() {
50
- if (typeof indexedDB === "undefined") return;
51
- return new Promise((resolve, reject) => {
52
- const request = indexedDB.open(DB_NAME, DB_VERSION);
53
- request.onupgradeneeded = () => {
54
- const db = request.result;
55
- if (!db.objectStoreNames.contains(STORE_NAME)) {
56
- const store = db.createObjectStore(STORE_NAME, { keyPath: "key" });
57
- store.createIndex("updated_at", "updated_at");
58
- }
59
- };
60
- request.onsuccess = () => {
61
- this.db = request.result;
62
- resolve();
63
- };
64
- request.onerror = () => {
65
- reject(request.error);
66
- };
67
- });
68
- }
69
- /** Get a resume session by key. Returns null if not found or stale. */
70
- async get(key) {
71
- if (!this.db) return null;
72
- return new Promise((resolve) => {
73
- const tx = this.db.transaction(STORE_NAME, "readonly");
74
- const store = tx.objectStore(STORE_NAME);
75
- const request = store.get(key);
76
- request.onsuccess = () => {
77
- const entry = request.result;
78
- if (!entry) {
79
- resolve(null);
80
- return;
81
- }
82
- if (Date.now() - entry.updated_at > MAX_AGE_MS) {
83
- this.remove(key).catch(() => {
84
- });
85
- resolve(null);
86
- return;
87
- }
88
- resolve(entry.session);
89
- };
90
- request.onerror = () => resolve(null);
91
- });
92
- }
93
- /** Save a new resume session. */
94
- async save(key, session) {
95
- if (!this.db) return;
96
- return new Promise((resolve, reject) => {
97
- const tx = this.db.transaction(STORE_NAME, "readwrite");
98
- const store = tx.objectStore(STORE_NAME);
99
- const entry = {
100
- key,
101
- session: { ...session, created_at: Date.now() },
102
- updated_at: Date.now()
103
- };
104
- const request = store.put(entry);
105
- request.onsuccess = () => resolve();
106
- request.onerror = () => reject(request.error);
107
- });
108
- }
109
- /** Update a single completed part in an existing session. */
110
- async updatePart(key, partNumber, etag) {
111
- if (!this.db) return;
112
- return new Promise((resolve) => {
113
- const tx = this.db.transaction(STORE_NAME, "readwrite");
114
- const store = tx.objectStore(STORE_NAME);
115
- const getRequest = store.get(key);
116
- getRequest.onsuccess = () => {
117
- const entry = getRequest.result;
118
- if (!entry) {
119
- resolve();
120
- return;
121
- }
122
- const existing = entry.session.completed_parts.find((p) => p.part_number === partNumber);
123
- if (!existing) {
124
- entry.session.completed_parts.push({ part_number: partNumber, etag });
125
- }
126
- entry.updated_at = Date.now();
127
- const putRequest = store.put(entry);
128
- putRequest.onsuccess = () => resolve();
129
- putRequest.onerror = () => resolve();
130
- };
131
- getRequest.onerror = () => resolve();
132
- });
133
- }
134
- /** Remove a resume session (e.g., after successful completion). */
135
- async remove(key) {
136
- if (!this.db) return;
137
- return new Promise((resolve) => {
138
- const tx = this.db.transaction(STORE_NAME, "readwrite");
139
- const store = tx.objectStore(STORE_NAME);
140
- const request = store.delete(key);
141
- request.onsuccess = () => resolve();
142
- request.onerror = () => resolve();
143
- });
144
- }
145
- /** Purge all stale entries (older than MAX_AGE_MS). */
146
- async purgeStale() {
147
- if (!this.db) return 0;
148
- return new Promise((resolve) => {
149
- const cutoff = Date.now() - MAX_AGE_MS;
150
- const tx = this.db.transaction(STORE_NAME, "readwrite");
151
- const store = tx.objectStore(STORE_NAME);
152
- const index = store.index("updated_at");
153
- const range = IDBKeyRange.upperBound(cutoff);
154
- const request = index.openCursor(range);
155
- let count = 0;
156
- request.onsuccess = () => {
157
- const cursor = request.result;
158
- if (cursor) {
159
- cursor.delete();
160
- count++;
161
- cursor.continue();
162
- } else {
163
- resolve(count);
164
- }
165
- };
166
- request.onerror = () => resolve(0);
167
- });
168
- }
169
- /** Close the database connection. */
170
- close() {
171
- if (this.db) {
172
- this.db.close();
173
- this.db = null;
174
- }
175
- }
176
- };
177
- }
178
- });
179
- var upload_compression_exports = {};
180
- __export(upload_compression_exports, {
181
- maybeCompressImage: () => maybeCompressImage
182
- });
183
- async function maybeCompressImage(file, userConfig, sessionId, telemetry) {
184
- const type = file.type?.toLowerCase() || "";
185
- if (!type.startsWith("image/")) return null;
186
- if (SKIP_TYPES.has(type)) {
187
- telemetry?.emit(sessionId, "upload.compression.skipped", { reason: "format", type });
188
- return null;
189
- }
190
- if (!COMPRESSIBLE_TYPES.has(type)) {
191
- telemetry?.emit(sessionId, "upload.compression.skipped", { reason: "unsupported_type", type });
192
- return null;
193
- }
194
- if (file.size < MIN_COMPRESS_SIZE) {
195
- telemetry?.emit(sessionId, "upload.compression.skipped", { reason: "too_small", size: file.size });
196
- return null;
197
- }
198
- const networkType = getNetworkEffectiveType();
199
- const defaultProfile = { maxWidth: 3840, maxHeight: 3840, quality: 0.85, maxSizeMB: 5 };
200
- const networkProfile = NETWORK_PROFILES[networkType] ?? defaultProfile;
201
- const config = {
202
- maxWidth: userConfig?.maxWidth ?? networkProfile.maxWidth,
203
- maxHeight: userConfig?.maxHeight ?? networkProfile.maxHeight,
204
- quality: userConfig?.quality ?? networkProfile.quality,
205
- maxSizeMB: userConfig?.maxSizeMB ?? networkProfile.maxSizeMB
206
- };
207
- telemetry?.emit(sessionId, "upload.compression.started", {
208
- original_size: file.size,
209
- network: networkType,
210
- target_quality: config.quality
211
- });
212
- try {
213
- const imageCompression = await loadImageCompression();
214
- if (!imageCompression) {
215
- telemetry?.emit(sessionId, "upload.compression.skipped", { reason: "library_unavailable" });
216
- return null;
217
- }
218
- const compressed = await imageCompression(file, {
219
- maxSizeMB: config.maxSizeMB,
220
- maxWidthOrHeight: Math.max(config.maxWidth, config.maxHeight),
221
- initialQuality: config.quality,
222
- useWebWorker: true,
223
- fileType: type === "image/png" ? "image/webp" : void 0
224
- });
225
- if (compressed.size >= file.size * 0.95) {
226
- telemetry?.emit(sessionId, "upload.compression.skipped", {
227
- reason: "no_size_reduction",
228
- original_size: file.size,
229
- compressed_size: compressed.size
230
- });
231
- return null;
232
- }
233
- telemetry?.emit(sessionId, "upload.compression.completed", {
234
- original_size: file.size,
235
- compressed_size: compressed.size,
236
- ratio: (compressed.size / file.size).toFixed(2)
237
- });
238
- return compressed;
239
- } catch (err) {
240
- telemetry?.emit(sessionId, "upload.compression.skipped", {
241
- reason: "error",
242
- error: err instanceof Error ? err.message : "Unknown compression error"
243
- });
244
- return null;
245
- }
246
- }
247
- async function loadImageCompression() {
248
- if (cachedImport === false) return null;
249
- if (cachedImport) return cachedImport;
250
- try {
251
- const mod = await Function('return import("browser-image-compression")')();
252
- cachedImport = mod.default || mod;
253
- return cachedImport;
254
- } catch {
255
- cachedImport = false;
256
- return null;
257
- }
258
- }
259
- function getNetworkEffectiveType() {
260
- if (typeof navigator !== "undefined" && "connection" in navigator) {
261
- const conn = navigator.connection;
262
- return conn?.effectiveType || "4g";
263
- }
264
- return "4g";
265
- }
266
- var MIN_COMPRESS_SIZE;
267
- var COMPRESSIBLE_TYPES;
268
- var SKIP_TYPES;
269
- var NETWORK_PROFILES;
270
- var cachedImport;
271
- var STORAGE_KEYS = {
272
- SESSION: "scalemule_session",
273
- USER_ID: "scalemule_user_id",
274
- WORKSPACE_ID: "scalemule_workspace_id",
275
- ANONYMOUS_ID: "scalemule_anonymous_id",
276
- SESSION_POOL: "scalemule_session_pool",
277
- ACTIVE_ACCOUNT: "scalemule_active_account",
278
- KNOWN_ACCOUNTS: "scalemule_known_accounts",
279
- OFFLINE_QUEUE: "scalemule_offline_queue"
280
- };
281
- var LEGACY_ANONYMOUS_ID_KEYS = ["sm_anonymous_id"];
282
- function generateAnonymousId() {
283
- if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
284
- return crypto.randomUUID();
285
- }
286
- return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
287
- const r = Math.random() * 16 | 0;
288
- const v = c === "x" ? r : r & 3 | 8;
289
- return v.toString(16);
290
- });
291
- }
292
- var inFlight = /* @__PURE__ */ new WeakMap();
293
- function ensureAnonymousId(storage) {
294
- const existing = inFlight.get(storage);
295
- if (existing) return existing;
296
- const promise = (async () => {
297
- try {
298
- return await resolveAnonymousId(storage);
299
- } finally {
300
- inFlight.delete(storage);
301
- }
302
- })();
303
- inFlight.set(storage, promise);
304
- return promise;
305
- }
306
- async function resolveAnonymousId(storage) {
307
- const canonical = await storage.getItem(STORAGE_KEYS.ANONYMOUS_ID);
308
- if (canonical) {
309
- for (const legacyKey of LEGACY_ANONYMOUS_ID_KEYS) {
310
- const legacy = await storage.getItem(legacyKey);
311
- if (legacy !== canonical) {
312
- try {
313
- await storage.setItem(legacyKey, canonical);
314
- } catch {
315
- }
316
- }
317
- }
318
- return canonical;
319
- }
320
- for (const legacyKey of LEGACY_ANONYMOUS_ID_KEYS) {
321
- const legacy = await storage.getItem(legacyKey);
322
- if (legacy) {
323
- try {
324
- await storage.setItem(STORAGE_KEYS.ANONYMOUS_ID, legacy);
325
- } catch {
326
- }
327
- return legacy;
328
- }
329
- }
330
- const fresh = generateAnonymousId();
331
- try {
332
- await storage.setItem(STORAGE_KEYS.ANONYMOUS_ID, fresh);
333
- } catch {
334
- }
335
- for (const legacyKey of LEGACY_ANONYMOUS_ID_KEYS) {
336
- try {
337
- await storage.setItem(legacyKey, fresh);
338
- } catch {
339
- }
340
- }
341
- return fresh;
342
- }
343
- var PHONE_COUNTRIES = [
344
- { code: "US", name: "United States", dialCode: "+1" },
345
- { code: "CA", name: "Canada", dialCode: "+1" },
346
- { code: "GB", name: "United Kingdom", dialCode: "+44" },
347
- { code: "AU", name: "Australia", dialCode: "+61" },
348
- { code: "DE", name: "Germany", dialCode: "+49" },
349
- { code: "FR", name: "France", dialCode: "+33" },
350
- { code: "IT", name: "Italy", dialCode: "+39" },
351
- { code: "ES", name: "Spain", dialCode: "+34" },
352
- { code: "NL", name: "Netherlands", dialCode: "+31" },
353
- { code: "BE", name: "Belgium", dialCode: "+32" },
354
- { code: "CH", name: "Switzerland", dialCode: "+41" },
355
- { code: "AT", name: "Austria", dialCode: "+43" },
356
- { code: "SE", name: "Sweden", dialCode: "+46" },
357
- { code: "NO", name: "Norway", dialCode: "+47" },
358
- { code: "DK", name: "Denmark", dialCode: "+45" },
359
- { code: "FI", name: "Finland", dialCode: "+358" },
360
- { code: "IE", name: "Ireland", dialCode: "+353" },
361
- { code: "PT", name: "Portugal", dialCode: "+351" },
362
- { code: "PL", name: "Poland", dialCode: "+48" },
363
- { code: "CZ", name: "Czech Republic", dialCode: "+420" },
364
- { code: "GR", name: "Greece", dialCode: "+30" },
365
- { code: "RU", name: "Russia", dialCode: "+7" },
366
- { code: "JP", name: "Japan", dialCode: "+81" },
367
- { code: "KR", name: "South Korea", dialCode: "+82" },
368
- { code: "CN", name: "China", dialCode: "+86" },
369
- { code: "HK", name: "Hong Kong", dialCode: "+852" },
370
- { code: "TW", name: "Taiwan", dialCode: "+886" },
371
- { code: "SG", name: "Singapore", dialCode: "+65" },
372
- { code: "MY", name: "Malaysia", dialCode: "+60" },
373
- { code: "TH", name: "Thailand", dialCode: "+66" },
374
- { code: "VN", name: "Vietnam", dialCode: "+84" },
375
- { code: "PH", name: "Philippines", dialCode: "+63" },
376
- { code: "ID", name: "Indonesia", dialCode: "+62" },
377
- { code: "IN", name: "India", dialCode: "+91" },
378
- { code: "PK", name: "Pakistan", dialCode: "+92" },
379
- { code: "BD", name: "Bangladesh", dialCode: "+880" },
380
- { code: "AE", name: "UAE", dialCode: "+971" },
381
- { code: "SA", name: "Saudi Arabia", dialCode: "+966" },
382
- { code: "IL", name: "Israel", dialCode: "+972" },
383
- { code: "TR", name: "Turkey", dialCode: "+90" },
384
- { code: "EG", name: "Egypt", dialCode: "+20" },
385
- { code: "ZA", name: "South Africa", dialCode: "+27" },
386
- { code: "NG", name: "Nigeria", dialCode: "+234" },
387
- { code: "KE", name: "Kenya", dialCode: "+254" },
388
- { code: "BR", name: "Brazil", dialCode: "+55" },
389
- { code: "MX", name: "Mexico", dialCode: "+52" },
390
- { code: "AR", name: "Argentina", dialCode: "+54" },
391
- { code: "CL", name: "Chile", dialCode: "+56" },
392
- { code: "CO", name: "Colombia", dialCode: "+57" },
393
- { code: "PE", name: "Peru", dialCode: "+51" },
394
- { code: "NZ", name: "New Zealand", dialCode: "+64" }
395
- ];
396
- [...PHONE_COUNTRIES].sort((a, b) => b.dialCode.length - a.dialCode.length);
397
- init_upload_resume();
3
+ var sdk = require('@scalemule/sdk');
4
+
5
+ // src/client.ts
398
6
 
399
7
  // src/types/index.ts
400
8
  var ScaleMuleApiError = class extends Error {
@@ -412,9 +20,9 @@ var GATEWAY_URLS = {
412
20
  dev: "https://api-dev.scalemule.com",
413
21
  prod: "https://api.scalemule.com"
414
22
  };
415
- var SESSION_STORAGE_KEY2 = STORAGE_KEYS.SESSION;
416
- var USER_ID_STORAGE_KEY2 = STORAGE_KEYS.USER_ID;
417
- var WORKSPACE_STORAGE_KEY2 = STORAGE_KEYS.WORKSPACE_ID;
23
+ var SESSION_STORAGE_KEY = sdk.STORAGE_KEYS.SESSION;
24
+ var USER_ID_STORAGE_KEY = sdk.STORAGE_KEYS.USER_ID;
25
+ var WORKSPACE_STORAGE_KEY = sdk.STORAGE_KEYS.WORKSPACE_ID;
418
26
  var RETRYABLE_STATUS_CODES = /* @__PURE__ */ new Set([408, 429, 500, 502, 503, 504]);
419
27
  function sleep(ms) {
420
28
  return new Promise((resolve) => setTimeout(resolve, ms));
@@ -764,9 +372,9 @@ var ScaleMuleClient = class {
764
372
  setWorkspaceContext(id) {
765
373
  this.workspaceId = id;
766
374
  if (id) {
767
- this.storage.setItem(WORKSPACE_STORAGE_KEY2, id);
375
+ this.storage.setItem(WORKSPACE_STORAGE_KEY, id);
768
376
  } else {
769
- this.storage.removeItem(WORKSPACE_STORAGE_KEY2);
377
+ this.storage.removeItem(WORKSPACE_STORAGE_KEY);
770
378
  }
771
379
  }
772
380
  /**
@@ -813,11 +421,11 @@ var ScaleMuleClient = class {
813
421
  * Initialize client by loading persisted session
814
422
  */
815
423
  async initialize() {
816
- const token = await this.storage.getItem(SESSION_STORAGE_KEY2);
817
- const userId = await this.storage.getItem(USER_ID_STORAGE_KEY2);
424
+ const token = await this.storage.getItem(SESSION_STORAGE_KEY);
425
+ const userId = await this.storage.getItem(USER_ID_STORAGE_KEY);
818
426
  if (token) this.sessionToken = token;
819
427
  if (userId) this.userId = userId;
820
- const wsId = await this.storage.getItem(WORKSPACE_STORAGE_KEY2);
428
+ const wsId = await this.storage.getItem(WORKSPACE_STORAGE_KEY);
821
429
  if (wsId) this.workspaceId = wsId;
822
430
  try {
823
431
  await this.ensureAnonymousId();
@@ -847,7 +455,7 @@ var ScaleMuleClient = class {
847
455
  async ensureAnonymousId() {
848
456
  if (this.anonymousId) return this.anonymousId;
849
457
  if (!this.anonymousIdPromise) {
850
- this.anonymousIdPromise = ensureAnonymousId(this.storage).then((id) => {
458
+ this.anonymousIdPromise = sdk.ensureAnonymousId(this.storage).then((id) => {
851
459
  this.anonymousId = id;
852
460
  return id;
853
461
  }).finally(() => {
@@ -862,8 +470,8 @@ var ScaleMuleClient = class {
862
470
  async setSession(token, userId) {
863
471
  this.sessionToken = token;
864
472
  this.userId = userId;
865
- await this.storage.setItem(SESSION_STORAGE_KEY2, token);
866
- await this.storage.setItem(USER_ID_STORAGE_KEY2, userId);
473
+ await this.storage.setItem(SESSION_STORAGE_KEY, token);
474
+ await this.storage.setItem(USER_ID_STORAGE_KEY, userId);
867
475
  if (this.debug) {
868
476
  console.log("[ScaleMule] Session set for user:", userId);
869
477
  }
@@ -893,9 +501,9 @@ var ScaleMuleClient = class {
893
501
  this.sessionToken = null;
894
502
  this.userId = null;
895
503
  this.workspaceId = null;
896
- await this.storage.removeItem(SESSION_STORAGE_KEY2);
897
- await this.storage.removeItem(USER_ID_STORAGE_KEY2);
898
- await this.storage.removeItem(WORKSPACE_STORAGE_KEY2);
504
+ await this.storage.removeItem(SESSION_STORAGE_KEY);
505
+ await this.storage.removeItem(USER_ID_STORAGE_KEY);
506
+ await this.storage.removeItem(WORKSPACE_STORAGE_KEY);
899
507
  if (this.debug) {
900
508
  console.log("[ScaleMule] Session cleared");
901
509
  }