@snowcone-app/sdk 0.29.1 → 0.31.0
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/CHANGELOG.md +12 -0
- package/README.md +19 -1
- package/dist/{chunk-4FZOZLWF.js → chunk-KMDKCI4T.js} +5 -1
- package/dist/{chunk-ZBJBBIH3.js → chunk-O6I5SJT5.js} +78 -4
- package/dist/index.cjs +324 -247
- package/dist/index.d.cts +21 -14
- package/dist/index.d.ts +21 -14
- package/dist/index.js +16 -11
- package/dist/react.cjs +130 -2
- package/dist/react.js +4 -2
- package/dist/server.cjs +66 -2
- package/dist/server.d.cts +2 -2
- package/dist/server.d.ts +2 -2
- package/dist/server.js +4 -3
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -58,6 +58,8 @@ __export(src_exports, {
|
|
|
58
58
|
RealtimeMockupService: () => RealtimeMockupService,
|
|
59
59
|
RenderSession: () => RenderSession,
|
|
60
60
|
SIZE_PICTURE: () => SIZE_PICTURE,
|
|
61
|
+
SNOWCONE_HOSTS: () => SNOWCONE_HOSTS,
|
|
62
|
+
SNOWCONE_REALTIME_WS_URL: () => SNOWCONE_REALTIME_WS_URL,
|
|
61
63
|
SnowconeSdkError: () => SnowconeSdkError,
|
|
62
64
|
StandardComponents: () => StandardComponents,
|
|
63
65
|
StandardEvents: () => StandardEvents,
|
|
@@ -258,242 +260,32 @@ var CatalogProductSchema = import_zod.z.object({
|
|
|
258
260
|
defaultGvid: import_zod.z.string().optional()
|
|
259
261
|
});
|
|
260
262
|
|
|
261
|
-
// src/mockup/cache.ts
|
|
262
|
-
var SignatureCache = class {
|
|
263
|
-
memoryCache = /* @__PURE__ */ new Map();
|
|
264
|
-
maxMemoryEntries;
|
|
265
|
-
maxLocalStorageEntries;
|
|
266
|
-
localStorageKey = "snowcone_signature_cache";
|
|
267
|
-
localStorageAvailable;
|
|
268
|
-
constructor(maxMemoryEntries = 500, maxLocalStorageEntries = 100) {
|
|
269
|
-
this.maxMemoryEntries = maxMemoryEntries;
|
|
270
|
-
this.maxLocalStorageEntries = maxLocalStorageEntries;
|
|
271
|
-
this.localStorageAvailable = this.checkLocalStorageAvailability();
|
|
272
|
-
if (this.localStorageAvailable) {
|
|
273
|
-
this.loadFromLocalStorage();
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
checkLocalStorageAvailability() {
|
|
277
|
-
try {
|
|
278
|
-
const test = "__storage_test__";
|
|
279
|
-
localStorage.setItem(test, test);
|
|
280
|
-
localStorage.removeItem(test);
|
|
281
|
-
return true;
|
|
282
|
-
} catch {
|
|
283
|
-
return false;
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
loadFromLocalStorage() {
|
|
287
|
-
try {
|
|
288
|
-
const stored = localStorage.getItem(this.localStorageKey);
|
|
289
|
-
if (stored) {
|
|
290
|
-
const parsed = JSON.parse(stored);
|
|
291
|
-
if (Array.isArray(parsed)) {
|
|
292
|
-
parsed.forEach(([key, value]) => {
|
|
293
|
-
if (this.memoryCache.size < this.maxMemoryEntries) {
|
|
294
|
-
this.memoryCache.set(key, value);
|
|
295
|
-
}
|
|
296
|
-
});
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
} catch (error) {
|
|
300
|
-
console.warn("Failed to load signature cache from localStorage:", error);
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
saveToLocalStorage() {
|
|
304
|
-
if (!this.localStorageAvailable) return;
|
|
305
|
-
try {
|
|
306
|
-
const entries = Array.from(this.memoryCache.entries()).sort((a, b) => b[1].timestamp - a[1].timestamp).slice(0, this.maxLocalStorageEntries);
|
|
307
|
-
localStorage.setItem(this.localStorageKey, JSON.stringify(entries));
|
|
308
|
-
} catch (error) {
|
|
309
|
-
console.warn("Failed to save signature cache to localStorage:", error);
|
|
310
|
-
this.localStorageAvailable = false;
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
get(key) {
|
|
314
|
-
const entry = this.memoryCache.get(key);
|
|
315
|
-
if (entry) {
|
|
316
|
-
entry.timestamp = Date.now();
|
|
317
|
-
this.memoryCache.delete(key);
|
|
318
|
-
this.memoryCache.set(key, entry);
|
|
319
|
-
return entry.url;
|
|
320
|
-
}
|
|
321
|
-
return null;
|
|
322
|
-
}
|
|
323
|
-
set(key, url) {
|
|
324
|
-
if (this.memoryCache.size >= this.maxMemoryEntries) {
|
|
325
|
-
const oldestKey = this.findOldestKey();
|
|
326
|
-
if (oldestKey) {
|
|
327
|
-
this.memoryCache.delete(oldestKey);
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
this.memoryCache.set(key, {
|
|
331
|
-
url,
|
|
332
|
-
timestamp: Date.now()
|
|
333
|
-
});
|
|
334
|
-
this.saveToLocalStorage();
|
|
335
|
-
}
|
|
336
|
-
findOldestKey() {
|
|
337
|
-
let oldestKey = null;
|
|
338
|
-
let oldestTime = Infinity;
|
|
339
|
-
for (const [key, entry] of this.memoryCache.entries()) {
|
|
340
|
-
if (entry.timestamp < oldestTime) {
|
|
341
|
-
oldestTime = entry.timestamp;
|
|
342
|
-
oldestKey = key;
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
return oldestKey;
|
|
346
|
-
}
|
|
347
|
-
clear() {
|
|
348
|
-
this.memoryCache.clear();
|
|
349
|
-
if (this.localStorageAvailable) {
|
|
350
|
-
try {
|
|
351
|
-
localStorage.removeItem(this.localStorageKey);
|
|
352
|
-
} catch (error) {
|
|
353
|
-
console.warn("Failed to clear localStorage cache:", error);
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
size() {
|
|
358
|
-
return this.memoryCache.size;
|
|
359
|
-
}
|
|
360
|
-
};
|
|
361
|
-
|
|
362
|
-
// src/mockup/service.ts
|
|
363
|
-
var DEFAULT_IMAGE_URL = typeof process !== "undefined" && process.env?.SNOWCONE_IMAGE_URL || "https://cdn.snowcone.app";
|
|
364
|
-
var DEFAULT_SIGNER_URL = typeof process !== "undefined" && process.env?.SNOWCONE_SIGNER_URL || "https://s.snowcone.app/sign";
|
|
365
|
-
var RATE_LIMIT_PER_MINUTE = 450;
|
|
366
|
-
var RATE_LIMIT_PER_SECOND = 20;
|
|
367
|
-
var MockupServiceImpl = class {
|
|
368
|
-
config;
|
|
369
|
-
cache;
|
|
370
|
-
rateLimitState;
|
|
371
|
-
fetch;
|
|
372
|
-
constructor(config, customFetch) {
|
|
373
|
-
this.config = {
|
|
374
|
-
imageUrl: config.imageUrl || DEFAULT_IMAGE_URL,
|
|
375
|
-
signerUrl: config.signerUrl || DEFAULT_SIGNER_URL,
|
|
376
|
-
shop: config.shop || typeof process !== "undefined" && process.env?.SNOWCONE_SHOP_ID || ""
|
|
377
|
-
};
|
|
378
|
-
this.cache = new SignatureCache();
|
|
379
|
-
this.fetch = customFetch || fetch.bind(globalThis);
|
|
380
|
-
this.rateLimitState = {
|
|
381
|
-
perMinute: { count: 0, resetTime: Date.now() + 6e4 },
|
|
382
|
-
perSecond: { count: 0, resetTime: Date.now() + 1e3 }
|
|
383
|
-
};
|
|
384
|
-
}
|
|
385
|
-
async getMockupUrl(options) {
|
|
386
|
-
if (!options.design || !options.product) {
|
|
387
|
-
throw new Error("Missing required options: design and product are required");
|
|
388
|
-
}
|
|
389
|
-
if (!this.config.shop) {
|
|
390
|
-
throw new Error("Shop ID is required for mockup generation");
|
|
391
|
-
}
|
|
392
|
-
for (const element of options.design) {
|
|
393
|
-
if (element.type === "image" && element.imageUrl) {
|
|
394
|
-
if (element.imageUrl.startsWith("blob:")) {
|
|
395
|
-
throw new Error(
|
|
396
|
-
`[snowcone-sdk] Cannot generate mockup: design element for placement "${element.placement}" uses a blob URL ("${element.imageUrl.slice(0, 60)}..."). Blob URLs are local to this browser tab and cannot be downloaded by the mockup rendering server. Upload the image to a publicly accessible URL or convert it to a data URL before requesting a mockup.`
|
|
397
|
-
);
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
this.checkRateLimits();
|
|
402
|
-
const relativeUrl = this.buildMockupUrlWithParams(options);
|
|
403
|
-
const cacheKey = `${relativeUrl}:${this.config.shop}`;
|
|
404
|
-
const cachedUrl = this.cache.get(cacheKey);
|
|
405
|
-
if (cachedUrl) {
|
|
406
|
-
return cachedUrl;
|
|
407
|
-
}
|
|
408
|
-
const signedUrl = await this.getSignedUrl(relativeUrl);
|
|
409
|
-
this.cache.set(cacheKey, signedUrl);
|
|
410
|
-
this.incrementRateLimitCounters();
|
|
411
|
-
return signedUrl;
|
|
412
|
-
}
|
|
413
|
-
buildMockupUrlWithParams(options) {
|
|
414
|
-
const { design, product, effects, ar } = options;
|
|
415
|
-
const designBase64 = typeof window !== "undefined" && window.btoa ? window.btoa(JSON.stringify(design)) : Buffer.from(JSON.stringify(design)).toString("base64");
|
|
416
|
-
const encodedDesign = encodeURIComponent(designBase64);
|
|
417
|
-
let url = `/mockup?productId=${encodeURIComponent(product.productId)}`;
|
|
418
|
-
url += `&mockupId=${encodeURIComponent(product.mockupId)}`;
|
|
419
|
-
url += `&variantId=${encodeURIComponent(product.variantId)}`;
|
|
420
|
-
url += `&design=${encodedDesign}`;
|
|
421
|
-
url += `&width=${product.width}`;
|
|
422
|
-
if (effects?.grain) {
|
|
423
|
-
url += `&grain=${effects.grain}`;
|
|
424
|
-
}
|
|
425
|
-
if (ar && ar !== "16:9") {
|
|
426
|
-
url += `&ar=${encodeURIComponent(ar)}`;
|
|
427
|
-
}
|
|
428
|
-
if (product.optionSelections && Object.keys(product.optionSelections).length > 0) {
|
|
429
|
-
const optionSelectionsBase64 = typeof window !== "undefined" && window.btoa ? window.btoa(JSON.stringify(product.optionSelections)) : Buffer.from(JSON.stringify(product.optionSelections)).toString("base64");
|
|
430
|
-
url += `&optionSelections=${encodeURIComponent(optionSelectionsBase64)}`;
|
|
431
|
-
}
|
|
432
|
-
return url;
|
|
433
|
-
}
|
|
434
|
-
async getSignedUrl(relativeUrl) {
|
|
435
|
-
const urlWithAccountId = relativeUrl + (relativeUrl.includes("?") ? "&" : "?") + `shop=${encodeURIComponent(this.config.shop)}`;
|
|
436
|
-
const cacheKey = urlWithAccountId;
|
|
437
|
-
const cachedUrl = this.cache.get(cacheKey);
|
|
438
|
-
if (cachedUrl) {
|
|
439
|
-
return cachedUrl;
|
|
440
|
-
}
|
|
441
|
-
const signerUrl = new URL(this.config.signerUrl);
|
|
442
|
-
signerUrl.searchParams.set("url", urlWithAccountId);
|
|
443
|
-
try {
|
|
444
|
-
const response = await this.fetch(signerUrl.toString(), {
|
|
445
|
-
method: "GET",
|
|
446
|
-
headers: {
|
|
447
|
-
"Accept": "application/json"
|
|
448
|
-
}
|
|
449
|
-
});
|
|
450
|
-
if (!response.ok) {
|
|
451
|
-
throw new Error(`Failed to sign URL: ${response.statusText}`);
|
|
452
|
-
}
|
|
453
|
-
const data = await response.json();
|
|
454
|
-
const signedUrl = this.config.imageUrl + (data.urlWithSignature || data.url);
|
|
455
|
-
this.cache.set(cacheKey, signedUrl);
|
|
456
|
-
return signedUrl;
|
|
457
|
-
} catch (error) {
|
|
458
|
-
throw new Error(`Failed to get signed URL: ${error}`);
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
checkRateLimits() {
|
|
462
|
-
const now = Date.now();
|
|
463
|
-
if (now >= this.rateLimitState.perMinute.resetTime) {
|
|
464
|
-
this.rateLimitState.perMinute.count = 0;
|
|
465
|
-
this.rateLimitState.perMinute.resetTime = now + 6e4;
|
|
466
|
-
}
|
|
467
|
-
if (now >= this.rateLimitState.perSecond.resetTime) {
|
|
468
|
-
this.rateLimitState.perSecond.count = 0;
|
|
469
|
-
this.rateLimitState.perSecond.resetTime = now + 1e3;
|
|
470
|
-
}
|
|
471
|
-
if (this.rateLimitState.perMinute.count >= RATE_LIMIT_PER_MINUTE) {
|
|
472
|
-
throw new Error("Rate limit exceeded: Too many requests per minute");
|
|
473
|
-
}
|
|
474
|
-
if (this.rateLimitState.perSecond.count >= RATE_LIMIT_PER_SECOND) {
|
|
475
|
-
throw new Error("Rate limit exceeded: Too many requests per second");
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
incrementRateLimitCounters() {
|
|
479
|
-
this.rateLimitState.perMinute.count++;
|
|
480
|
-
this.rateLimitState.perSecond.count++;
|
|
481
|
-
}
|
|
482
|
-
getConfig() {
|
|
483
|
-
return { ...this.config };
|
|
484
|
-
}
|
|
485
|
-
};
|
|
486
|
-
|
|
487
|
-
// src/dev-fetcher.ts
|
|
488
|
-
function createDevFetcher(baseUrl) {
|
|
489
|
-
return async function devFetch(input, init) {
|
|
490
|
-
const url = input.startsWith("http") ? input : `${baseUrl}${input}`;
|
|
491
|
-
const res = await fetch(url, init);
|
|
492
|
-
return res;
|
|
493
|
-
};
|
|
494
|
-
}
|
|
495
|
-
|
|
496
263
|
// ../mockup-url/src/index.ts
|
|
264
|
+
var SNOWCONE_HOSTS = {
|
|
265
|
+
/** REST API: shops, orders, AI generation, realtime grants. */
|
|
266
|
+
api: "https://api.snowcone.app",
|
|
267
|
+
/** Public mockup-image resolver (`/{productCode}?shop=…&asset=…`). */
|
|
268
|
+
img: "https://img.snowcone.app",
|
|
269
|
+
/** Hosted buy page (same grammar as `img`, opens checkout). */
|
|
270
|
+
buy: "https://buy.snowcone.app",
|
|
271
|
+
/** Public product / designs catalog (Meilisearch-compatible). */
|
|
272
|
+
search: "https://search.snowcone.app",
|
|
273
|
+
/** Render CDN — signed render URLs and the realtime socket. */
|
|
274
|
+
cdn: "https://cdn.snowcone.app",
|
|
275
|
+
/** Legacy URL signer. */
|
|
276
|
+
sign: "https://s.snowcone.app",
|
|
277
|
+
/** Content-addressed artwork store (ingest handles). */
|
|
278
|
+
raws: "https://raws.snowcone.app",
|
|
279
|
+
/** Developer docs. */
|
|
280
|
+
docs: "https://developers.snowcone.app",
|
|
281
|
+
/** The storefront site; `/api/launch` says whether orders are open yet. */
|
|
282
|
+
site: "https://snowcone.app",
|
|
283
|
+
/** The hosted MCP endpoint; it also serves the photo upload page (`/upload/<token>`). */
|
|
284
|
+
mcp: "https://mcp.snowcone.app",
|
|
285
|
+
/** Where draft designs made for a search are stored (a draft's `image_url`). */
|
|
286
|
+
drafts: "https://agentdraft.storage.snowcone.app"
|
|
287
|
+
};
|
|
288
|
+
var SNOWCONE_REALTIME_WS_URL = `${SNOWCONE_HOSTS.cdn.replace(/^http/, "ws")}/realtime`;
|
|
497
289
|
var PARAMS = {
|
|
498
290
|
shop: "shop",
|
|
499
291
|
asset: "asset",
|
|
@@ -543,6 +335,51 @@ var PARAMS = {
|
|
|
543
335
|
seal: "seal"
|
|
544
336
|
};
|
|
545
337
|
var BG_TRANSPARENT = "transparent";
|
|
338
|
+
var MOCKUP_PRESETS = {
|
|
339
|
+
tiktok: { aspect: "9:16", width: 1200 },
|
|
340
|
+
// 1080x1920
|
|
341
|
+
"instagram-story": { aspect: "9:16", width: 1200 },
|
|
342
|
+
// 1080x1920 (and Reels)
|
|
343
|
+
"youtube-shorts": { aspect: "9:16", width: 1200 },
|
|
344
|
+
// 1080x1920
|
|
345
|
+
"instagram-feed": { aspect: "4:5", width: 1200 },
|
|
346
|
+
// 1080x1350
|
|
347
|
+
"instagram-square": { aspect: "1:1", width: 1200 },
|
|
348
|
+
// 1080x1080
|
|
349
|
+
"facebook-feed": { aspect: "4:5", width: 1200 },
|
|
350
|
+
// 1080x1350
|
|
351
|
+
"facebook-link": { aspect: "1.91:1", width: 1200 },
|
|
352
|
+
// 1200x628
|
|
353
|
+
"linkedin-link": { aspect: "1.91:1", width: 1200 },
|
|
354
|
+
// 1200x627
|
|
355
|
+
pinterest: { aspect: "2:3", width: 1e3 },
|
|
356
|
+
// 1000x1500
|
|
357
|
+
"youtube-thumbnail": { aspect: "16:9", width: 1400 },
|
|
358
|
+
// 1280x720
|
|
359
|
+
"x-post": { aspect: "16:9", width: 1600 },
|
|
360
|
+
// 1600x900
|
|
361
|
+
amazon: { aspect: "1:1", width: 2e3 },
|
|
362
|
+
// 2000x2000 main image
|
|
363
|
+
etsy: { aspect: "1:1", width: 2e3 },
|
|
364
|
+
// 2000x2000
|
|
365
|
+
shopify: { aspect: "1:1", width: 2e3 },
|
|
366
|
+
// 2048x2048 product image, nearest rung
|
|
367
|
+
"site-hero": { aspect: "16:9", width: 2500 },
|
|
368
|
+
// 2560x1440
|
|
369
|
+
"web-banner": { aspect: "21:9", width: 2500 },
|
|
370
|
+
// ultra-wide banner
|
|
371
|
+
"app-header": { aspect: "app-header", width: 1200 }
|
|
372
|
+
// the hero under a phone's status bar (8:9)
|
|
373
|
+
};
|
|
374
|
+
var MOCKUP_PRESET_NAMES = Object.keys(MOCKUP_PRESETS);
|
|
375
|
+
function resolvePreset(preset, aspect, width) {
|
|
376
|
+
const p = MOCKUP_PRESETS[preset];
|
|
377
|
+
if (!p) throw new Error(`[snowcone] E_PRESET_UNKNOWN: preset ${JSON.stringify(preset)}; the presets: ${MOCKUP_PRESET_NAMES.join(", ")}`);
|
|
378
|
+
if (aspect !== void 0 && aspect !== p.aspect) {
|
|
379
|
+
throw new Error(`[snowcone] E_PRESET_ASPECT_CONFLICT: preset "${preset}" is aspect ${p.aspect}, but aspect "${aspect}" was also given; drop one`);
|
|
380
|
+
}
|
|
381
|
+
return { aspect: p.aspect, width: width ?? p.width };
|
|
382
|
+
}
|
|
546
383
|
function normalizeBgColor(raw) {
|
|
547
384
|
const value = raw.trim().replace(/^#/, "").toLowerCase();
|
|
548
385
|
if (value === BG_TRANSPARENT) return BG_TRANSPARENT;
|
|
@@ -581,6 +418,7 @@ var ALLOWED_WIDTHS = [
|
|
|
581
418
|
4e3
|
|
582
419
|
];
|
|
583
420
|
var MAX_RENDER_URL_LENGTH = 12 * 1024;
|
|
421
|
+
var INGEST_HANDLE_BASE = `${SNOWCONE_HOSTS.raws}/art-ingest/`;
|
|
584
422
|
function normalizeWidth(width) {
|
|
585
423
|
for (let i = ALLOWED_WIDTHS.length - 1; i >= 0; i--) {
|
|
586
424
|
if (ALLOWED_WIDTHS[i] <= width) return ALLOWED_WIDTHS[i];
|
|
@@ -665,7 +503,7 @@ var PARAM_PREFIX = {
|
|
|
665
503
|
/** ADR-0099 bundles: `pick.<role>=<productCode>` — which alternative fills a slot. */
|
|
666
504
|
pick: "pick."
|
|
667
505
|
};
|
|
668
|
-
var DEFAULT_PUBLIC_BASE_URL =
|
|
506
|
+
var DEFAULT_PUBLIC_BASE_URL = SNOWCONE_HOSTS.img;
|
|
669
507
|
function buildPublicMockupUrl(options, cfg = {}) {
|
|
670
508
|
const base = (cfg.baseUrl ?? DEFAULT_PUBLIC_BASE_URL).replace(/\/+$/, "");
|
|
671
509
|
const path = `/${encodeURIComponent(options.productCode)}`;
|
|
@@ -716,9 +554,10 @@ function buildPublicMockupUrl(options, cfg = {}) {
|
|
|
716
554
|
if (options.mockup !== void 0) {
|
|
717
555
|
parts.push(`${PARAMS.mockup}=${enc(options.mockup)}`);
|
|
718
556
|
}
|
|
719
|
-
|
|
720
|
-
if (
|
|
721
|
-
|
|
557
|
+
const shape = options.preset !== void 0 ? resolvePreset(options.preset, options.aspect, options.width) : { aspect: options.aspect, width: options.width };
|
|
558
|
+
if (shape.width !== void 0) parts.push(`${PARAMS.width}=${shape.width}`);
|
|
559
|
+
if (shape.aspect !== void 0 && shape.aspect !== "16:9") {
|
|
560
|
+
parts.push(`${PARAMS.aspect}=${enc(shape.aspect)}`);
|
|
722
561
|
}
|
|
723
562
|
if (options.bg) {
|
|
724
563
|
parts.push(`${PARAMS.bg}=${requireBgColor(options.bg)}`);
|
|
@@ -771,6 +610,241 @@ function canonicalForSig(url) {
|
|
|
771
610
|
return `${pathname}?${canonicalQuery}`;
|
|
772
611
|
}
|
|
773
612
|
|
|
613
|
+
// src/mockup/cache.ts
|
|
614
|
+
var SignatureCache = class {
|
|
615
|
+
memoryCache = /* @__PURE__ */ new Map();
|
|
616
|
+
maxMemoryEntries;
|
|
617
|
+
maxLocalStorageEntries;
|
|
618
|
+
localStorageKey = "snowcone_signature_cache";
|
|
619
|
+
localStorageAvailable;
|
|
620
|
+
constructor(maxMemoryEntries = 500, maxLocalStorageEntries = 100) {
|
|
621
|
+
this.maxMemoryEntries = maxMemoryEntries;
|
|
622
|
+
this.maxLocalStorageEntries = maxLocalStorageEntries;
|
|
623
|
+
this.localStorageAvailable = this.checkLocalStorageAvailability();
|
|
624
|
+
if (this.localStorageAvailable) {
|
|
625
|
+
this.loadFromLocalStorage();
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
checkLocalStorageAvailability() {
|
|
629
|
+
try {
|
|
630
|
+
const test = "__storage_test__";
|
|
631
|
+
localStorage.setItem(test, test);
|
|
632
|
+
localStorage.removeItem(test);
|
|
633
|
+
return true;
|
|
634
|
+
} catch {
|
|
635
|
+
return false;
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
loadFromLocalStorage() {
|
|
639
|
+
try {
|
|
640
|
+
const stored = localStorage.getItem(this.localStorageKey);
|
|
641
|
+
if (stored) {
|
|
642
|
+
const parsed = JSON.parse(stored);
|
|
643
|
+
if (Array.isArray(parsed)) {
|
|
644
|
+
parsed.forEach(([key, value]) => {
|
|
645
|
+
if (this.memoryCache.size < this.maxMemoryEntries) {
|
|
646
|
+
this.memoryCache.set(key, value);
|
|
647
|
+
}
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
} catch (error) {
|
|
652
|
+
console.warn("Failed to load signature cache from localStorage:", error);
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
saveToLocalStorage() {
|
|
656
|
+
if (!this.localStorageAvailable) return;
|
|
657
|
+
try {
|
|
658
|
+
const entries = Array.from(this.memoryCache.entries()).sort((a, b) => b[1].timestamp - a[1].timestamp).slice(0, this.maxLocalStorageEntries);
|
|
659
|
+
localStorage.setItem(this.localStorageKey, JSON.stringify(entries));
|
|
660
|
+
} catch (error) {
|
|
661
|
+
console.warn("Failed to save signature cache to localStorage:", error);
|
|
662
|
+
this.localStorageAvailable = false;
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
get(key) {
|
|
666
|
+
const entry = this.memoryCache.get(key);
|
|
667
|
+
if (entry) {
|
|
668
|
+
entry.timestamp = Date.now();
|
|
669
|
+
this.memoryCache.delete(key);
|
|
670
|
+
this.memoryCache.set(key, entry);
|
|
671
|
+
return entry.url;
|
|
672
|
+
}
|
|
673
|
+
return null;
|
|
674
|
+
}
|
|
675
|
+
set(key, url) {
|
|
676
|
+
if (this.memoryCache.size >= this.maxMemoryEntries) {
|
|
677
|
+
const oldestKey = this.findOldestKey();
|
|
678
|
+
if (oldestKey) {
|
|
679
|
+
this.memoryCache.delete(oldestKey);
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
this.memoryCache.set(key, {
|
|
683
|
+
url,
|
|
684
|
+
timestamp: Date.now()
|
|
685
|
+
});
|
|
686
|
+
this.saveToLocalStorage();
|
|
687
|
+
}
|
|
688
|
+
findOldestKey() {
|
|
689
|
+
let oldestKey = null;
|
|
690
|
+
let oldestTime = Infinity;
|
|
691
|
+
for (const [key, entry] of this.memoryCache.entries()) {
|
|
692
|
+
if (entry.timestamp < oldestTime) {
|
|
693
|
+
oldestTime = entry.timestamp;
|
|
694
|
+
oldestKey = key;
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
return oldestKey;
|
|
698
|
+
}
|
|
699
|
+
clear() {
|
|
700
|
+
this.memoryCache.clear();
|
|
701
|
+
if (this.localStorageAvailable) {
|
|
702
|
+
try {
|
|
703
|
+
localStorage.removeItem(this.localStorageKey);
|
|
704
|
+
} catch (error) {
|
|
705
|
+
console.warn("Failed to clear localStorage cache:", error);
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
size() {
|
|
710
|
+
return this.memoryCache.size;
|
|
711
|
+
}
|
|
712
|
+
};
|
|
713
|
+
|
|
714
|
+
// src/mockup/service.ts
|
|
715
|
+
var DEFAULT_IMAGE_URL = typeof process !== "undefined" && process.env?.SNOWCONE_IMAGE_URL || SNOWCONE_HOSTS.cdn;
|
|
716
|
+
var DEFAULT_SIGNER_URL = typeof process !== "undefined" && process.env?.SNOWCONE_SIGNER_URL || `${SNOWCONE_HOSTS.sign}/sign`;
|
|
717
|
+
var RATE_LIMIT_PER_MINUTE = 450;
|
|
718
|
+
var RATE_LIMIT_PER_SECOND = 20;
|
|
719
|
+
var MockupServiceImpl = class {
|
|
720
|
+
config;
|
|
721
|
+
cache;
|
|
722
|
+
rateLimitState;
|
|
723
|
+
fetch;
|
|
724
|
+
constructor(config, customFetch) {
|
|
725
|
+
this.config = {
|
|
726
|
+
imageUrl: config.imageUrl || DEFAULT_IMAGE_URL,
|
|
727
|
+
signerUrl: config.signerUrl || DEFAULT_SIGNER_URL,
|
|
728
|
+
shop: config.shop || typeof process !== "undefined" && process.env?.SNOWCONE_SHOP_ID || ""
|
|
729
|
+
};
|
|
730
|
+
this.cache = new SignatureCache();
|
|
731
|
+
this.fetch = customFetch || fetch.bind(globalThis);
|
|
732
|
+
this.rateLimitState = {
|
|
733
|
+
perMinute: { count: 0, resetTime: Date.now() + 6e4 },
|
|
734
|
+
perSecond: { count: 0, resetTime: Date.now() + 1e3 }
|
|
735
|
+
};
|
|
736
|
+
}
|
|
737
|
+
async getMockupUrl(options) {
|
|
738
|
+
if (!options.design || !options.product) {
|
|
739
|
+
throw new Error("Missing required options: design and product are required");
|
|
740
|
+
}
|
|
741
|
+
if (!this.config.shop) {
|
|
742
|
+
throw new Error("Shop ID is required for mockup generation");
|
|
743
|
+
}
|
|
744
|
+
for (const element of options.design) {
|
|
745
|
+
if (element.type === "image" && element.imageUrl) {
|
|
746
|
+
if (element.imageUrl.startsWith("blob:")) {
|
|
747
|
+
throw new Error(
|
|
748
|
+
`[snowcone-sdk] Cannot generate mockup: design element for placement "${element.placement}" uses a blob URL ("${element.imageUrl.slice(0, 60)}..."). Blob URLs are local to this browser tab and cannot be downloaded by the mockup rendering server. Upload the image to a publicly accessible URL or convert it to a data URL before requesting a mockup.`
|
|
749
|
+
);
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
this.checkRateLimits();
|
|
754
|
+
const relativeUrl = this.buildMockupUrlWithParams(options);
|
|
755
|
+
const cacheKey = `${relativeUrl}:${this.config.shop}`;
|
|
756
|
+
const cachedUrl = this.cache.get(cacheKey);
|
|
757
|
+
if (cachedUrl) {
|
|
758
|
+
return cachedUrl;
|
|
759
|
+
}
|
|
760
|
+
const signedUrl = await this.getSignedUrl(relativeUrl);
|
|
761
|
+
this.cache.set(cacheKey, signedUrl);
|
|
762
|
+
this.incrementRateLimitCounters();
|
|
763
|
+
return signedUrl;
|
|
764
|
+
}
|
|
765
|
+
buildMockupUrlWithParams(options) {
|
|
766
|
+
const { design, product, effects, ar } = options;
|
|
767
|
+
const designBase64 = typeof window !== "undefined" && window.btoa ? window.btoa(JSON.stringify(design)) : Buffer.from(JSON.stringify(design)).toString("base64");
|
|
768
|
+
const encodedDesign = encodeURIComponent(designBase64);
|
|
769
|
+
let url = `/mockup?productId=${encodeURIComponent(product.productId)}`;
|
|
770
|
+
url += `&mockupId=${encodeURIComponent(product.mockupId)}`;
|
|
771
|
+
url += `&variantId=${encodeURIComponent(product.variantId)}`;
|
|
772
|
+
url += `&design=${encodedDesign}`;
|
|
773
|
+
url += `&width=${product.width}`;
|
|
774
|
+
if (effects?.grain) {
|
|
775
|
+
url += `&grain=${effects.grain}`;
|
|
776
|
+
}
|
|
777
|
+
if (ar && ar !== "16:9") {
|
|
778
|
+
url += `&ar=${encodeURIComponent(ar)}`;
|
|
779
|
+
}
|
|
780
|
+
if (product.optionSelections && Object.keys(product.optionSelections).length > 0) {
|
|
781
|
+
const optionSelectionsBase64 = typeof window !== "undefined" && window.btoa ? window.btoa(JSON.stringify(product.optionSelections)) : Buffer.from(JSON.stringify(product.optionSelections)).toString("base64");
|
|
782
|
+
url += `&optionSelections=${encodeURIComponent(optionSelectionsBase64)}`;
|
|
783
|
+
}
|
|
784
|
+
return url;
|
|
785
|
+
}
|
|
786
|
+
async getSignedUrl(relativeUrl) {
|
|
787
|
+
const urlWithAccountId = relativeUrl + (relativeUrl.includes("?") ? "&" : "?") + `shop=${encodeURIComponent(this.config.shop)}`;
|
|
788
|
+
const cacheKey = urlWithAccountId;
|
|
789
|
+
const cachedUrl = this.cache.get(cacheKey);
|
|
790
|
+
if (cachedUrl) {
|
|
791
|
+
return cachedUrl;
|
|
792
|
+
}
|
|
793
|
+
const signerUrl = new URL(this.config.signerUrl);
|
|
794
|
+
signerUrl.searchParams.set("url", urlWithAccountId);
|
|
795
|
+
try {
|
|
796
|
+
const response = await this.fetch(signerUrl.toString(), {
|
|
797
|
+
method: "GET",
|
|
798
|
+
headers: {
|
|
799
|
+
"Accept": "application/json"
|
|
800
|
+
}
|
|
801
|
+
});
|
|
802
|
+
if (!response.ok) {
|
|
803
|
+
throw new Error(`Failed to sign URL: ${response.statusText}`);
|
|
804
|
+
}
|
|
805
|
+
const data = await response.json();
|
|
806
|
+
const signedUrl = this.config.imageUrl + (data.urlWithSignature || data.url);
|
|
807
|
+
this.cache.set(cacheKey, signedUrl);
|
|
808
|
+
return signedUrl;
|
|
809
|
+
} catch (error) {
|
|
810
|
+
throw new Error(`Failed to get signed URL: ${error}`);
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
checkRateLimits() {
|
|
814
|
+
const now = Date.now();
|
|
815
|
+
if (now >= this.rateLimitState.perMinute.resetTime) {
|
|
816
|
+
this.rateLimitState.perMinute.count = 0;
|
|
817
|
+
this.rateLimitState.perMinute.resetTime = now + 6e4;
|
|
818
|
+
}
|
|
819
|
+
if (now >= this.rateLimitState.perSecond.resetTime) {
|
|
820
|
+
this.rateLimitState.perSecond.count = 0;
|
|
821
|
+
this.rateLimitState.perSecond.resetTime = now + 1e3;
|
|
822
|
+
}
|
|
823
|
+
if (this.rateLimitState.perMinute.count >= RATE_LIMIT_PER_MINUTE) {
|
|
824
|
+
throw new Error("Rate limit exceeded: Too many requests per minute");
|
|
825
|
+
}
|
|
826
|
+
if (this.rateLimitState.perSecond.count >= RATE_LIMIT_PER_SECOND) {
|
|
827
|
+
throw new Error("Rate limit exceeded: Too many requests per second");
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
incrementRateLimitCounters() {
|
|
831
|
+
this.rateLimitState.perMinute.count++;
|
|
832
|
+
this.rateLimitState.perSecond.count++;
|
|
833
|
+
}
|
|
834
|
+
getConfig() {
|
|
835
|
+
return { ...this.config };
|
|
836
|
+
}
|
|
837
|
+
};
|
|
838
|
+
|
|
839
|
+
// src/dev-fetcher.ts
|
|
840
|
+
function createDevFetcher(baseUrl) {
|
|
841
|
+
return async function devFetch(input, init) {
|
|
842
|
+
const url = input.startsWith("http") ? input : `${baseUrl}${input}`;
|
|
843
|
+
const res = await fetch(url, init);
|
|
844
|
+
return res;
|
|
845
|
+
};
|
|
846
|
+
}
|
|
847
|
+
|
|
774
848
|
// src/mockup/hmac.ts
|
|
775
849
|
var K = new Uint32Array([
|
|
776
850
|
1116352408,
|
|
@@ -982,7 +1056,7 @@ function hmacSha256Hex(secret, message) {
|
|
|
982
1056
|
}
|
|
983
1057
|
|
|
984
1058
|
// src/mockup/getMockupUrl.ts
|
|
985
|
-
var DEFAULT_MOCKUP_BASE =
|
|
1059
|
+
var DEFAULT_MOCKUP_BASE = SNOWCONE_HOSTS.img;
|
|
986
1060
|
function getMockupUrl(a, b, c) {
|
|
987
1061
|
let productCode;
|
|
988
1062
|
let opts;
|
|
@@ -1011,6 +1085,7 @@ function getMockupUrl(a, b, c) {
|
|
|
1011
1085
|
...opts.options ? { options: opts.options } : {},
|
|
1012
1086
|
...opts.width !== void 0 ? { width: opts.width } : {},
|
|
1013
1087
|
...opts.aspect !== void 0 ? { aspect: opts.aspect } : {},
|
|
1088
|
+
...opts.preset !== void 0 ? { preset: opts.preset } : {},
|
|
1014
1089
|
...opts.bg !== void 0 ? { bg: opts.bg } : {},
|
|
1015
1090
|
...opts.mockup ?? opts.view ? { mockup: opts.mockup ?? opts.view } : {},
|
|
1016
1091
|
...opts.variant !== void 0 ? { variant: opts.variant } : {},
|
|
@@ -1022,7 +1097,7 @@ function getMockupUrl(a, b, c) {
|
|
|
1022
1097
|
const signature = hmacSha256Hex(opts.secret, canonicalForSig(url)).slice(0, 16);
|
|
1023
1098
|
return `${url}&signature=${signature}`;
|
|
1024
1099
|
}
|
|
1025
|
-
var DEFAULT_BUY_BASE =
|
|
1100
|
+
var DEFAULT_BUY_BASE = SNOWCONE_HOSTS.buy;
|
|
1026
1101
|
function getBuyUrl(productCode, opts) {
|
|
1027
1102
|
return getMockupUrl(productCode, {
|
|
1028
1103
|
...opts,
|
|
@@ -1937,7 +2012,7 @@ function buildMockupUrl2(options, cfg) {
|
|
|
1937
2012
|
}
|
|
1938
2013
|
function resolveMockupBaseUrl() {
|
|
1939
2014
|
const winConfig = typeof window !== "undefined" && window.snowcone || {};
|
|
1940
|
-
return winConfig.resolver || typeof process !== "undefined" && process.env?.NEXT_PUBLIC_MOCKUP_RESOLVER_URL || typeof process !== "undefined" && process.env?.SNOWCONE_IMAGE_URL ||
|
|
2015
|
+
return winConfig.resolver || typeof process !== "undefined" && process.env?.NEXT_PUBLIC_MOCKUP_RESOLVER_URL || typeof process !== "undefined" && process.env?.SNOWCONE_IMAGE_URL || SNOWCONE_HOSTS.img;
|
|
1941
2016
|
}
|
|
1942
2017
|
function resolveShop() {
|
|
1943
2018
|
const winConfig = typeof window !== "undefined" && window.snowcone || {};
|
|
@@ -5072,7 +5147,7 @@ function createSvelteComponent(descriptor, svelte) {
|
|
|
5072
5147
|
}
|
|
5073
5148
|
|
|
5074
5149
|
// src/realtime/constants.ts
|
|
5075
|
-
var REALTIME_WS_URL =
|
|
5150
|
+
var REALTIME_WS_URL = SNOWCONE_REALTIME_WS_URL;
|
|
5076
5151
|
|
|
5077
5152
|
// src/realtime/websocket.ts
|
|
5078
5153
|
function pruneUndefined(obj) {
|
|
@@ -6063,7 +6138,7 @@ ${versionToSend}
|
|
|
6063
6138
|
};
|
|
6064
6139
|
|
|
6065
6140
|
// src/realtime/grant.ts
|
|
6066
|
-
var DEFAULT_GRANT_BASE =
|
|
6141
|
+
var DEFAULT_GRANT_BASE = SNOWCONE_HOSTS.api;
|
|
6067
6142
|
async function mintRealtimeGrant(opts) {
|
|
6068
6143
|
const f = opts.fetch ?? globalThis.fetch;
|
|
6069
6144
|
const headers = { "Content-Type": "application/json" };
|
|
@@ -6547,7 +6622,7 @@ function validateProductLoose(product) {
|
|
|
6547
6622
|
}
|
|
6548
6623
|
async function listProducts(config) {
|
|
6549
6624
|
const f = getFetcher(config);
|
|
6550
|
-
const meilisearchHost = config?.meilisearch?.host || typeof process !== "undefined" && process.env?.NEXT_PUBLIC_MEILISEARCH_HOST ||
|
|
6625
|
+
const meilisearchHost = config?.meilisearch?.host || typeof process !== "undefined" && process.env?.NEXT_PUBLIC_MEILISEARCH_HOST || SNOWCONE_HOSTS.search;
|
|
6551
6626
|
const meilisearchApiKey = config?.meilisearch?.apiKey || typeof process !== "undefined" && process.env?.NEXT_PUBLIC_MEILISEARCH_API_KEY || "73f573ead9be75531c56ee89d104caa49d79154db387f119a013d33eccfe654b";
|
|
6552
6627
|
const meilisearchIndex = config?.meilisearch?.index || typeof process !== "undefined" && process.env?.NEXT_PUBLIC_MEILISEARCH_INDEX || "snowcone";
|
|
6553
6628
|
const headers = {
|
|
@@ -6573,7 +6648,7 @@ async function listProducts(config) {
|
|
|
6573
6648
|
}
|
|
6574
6649
|
async function getProduct(idOrSlug, config) {
|
|
6575
6650
|
const f = getFetcher(config);
|
|
6576
|
-
const meilisearchHost = config?.meilisearch?.host || typeof process !== "undefined" && process.env?.NEXT_PUBLIC_MEILISEARCH_HOST ||
|
|
6651
|
+
const meilisearchHost = config?.meilisearch?.host || typeof process !== "undefined" && process.env?.NEXT_PUBLIC_MEILISEARCH_HOST || SNOWCONE_HOSTS.search;
|
|
6577
6652
|
const meilisearchApiKey = config?.meilisearch?.apiKey || typeof process !== "undefined" && process.env?.NEXT_PUBLIC_MEILISEARCH_API_KEY || "73f573ead9be75531c56ee89d104caa49d79154db387f119a013d33eccfe654b";
|
|
6578
6653
|
const meilisearchIndex = config?.meilisearch?.index || typeof process !== "undefined" && process.env?.NEXT_PUBLIC_MEILISEARCH_INDEX || "snowcone";
|
|
6579
6654
|
const headers = {
|
|
@@ -6597,7 +6672,7 @@ async function getProduct(idOrSlug, config) {
|
|
|
6597
6672
|
}
|
|
6598
6673
|
async function searchDesigns(query = "", options) {
|
|
6599
6674
|
const f = getFetcher(options);
|
|
6600
|
-
const meilisearchHost = options?.meilisearch?.host || typeof process !== "undefined" && process.env?.NEXT_PUBLIC_MEILISEARCH_HOST ||
|
|
6675
|
+
const meilisearchHost = options?.meilisearch?.host || typeof process !== "undefined" && process.env?.NEXT_PUBLIC_MEILISEARCH_HOST || SNOWCONE_HOSTS.search;
|
|
6601
6676
|
const meilisearchApiKey = options?.meilisearch?.apiKey || typeof process !== "undefined" && process.env?.NEXT_PUBLIC_MEILISEARCH_API_KEY || "73f573ead9be75531c56ee89d104caa49d79154db387f119a013d33eccfe654b";
|
|
6602
6677
|
const designsIndex = options?.meilisearch?.designsIndex || typeof process !== "undefined" && process.env?.NEXT_PUBLIC_MEILISEARCH_DESIGNS_INDEX || "snowcone-designs";
|
|
6603
6678
|
const headers = {
|
|
@@ -6684,6 +6759,8 @@ function createClient(config) {
|
|
|
6684
6759
|
RealtimeMockupService,
|
|
6685
6760
|
RenderSession,
|
|
6686
6761
|
SIZE_PICTURE,
|
|
6762
|
+
SNOWCONE_HOSTS,
|
|
6763
|
+
SNOWCONE_REALTIME_WS_URL,
|
|
6687
6764
|
SnowconeSdkError,
|
|
6688
6765
|
StandardComponents,
|
|
6689
6766
|
StandardEvents,
|