@amplifyup/sdk 0.1.33 → 0.1.35

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.
@@ -206,6 +206,7 @@ var AWSAdapter = class {
206
206
  // src/storage.ts
207
207
  var INSTANCE_ID_KEY = "amplifyup_instance_id";
208
208
  var USER_ID_KEY = "amplifyup_user_id";
209
+ var ANON_COOKIE_NAME = "amplifyup_anon_id";
209
210
  function generateGuid() {
210
211
  if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
211
212
  return crypto.randomUUID();
@@ -216,16 +217,39 @@ function generateGuid() {
216
217
  return v.toString(16);
217
218
  });
218
219
  }
220
+ function readCookie(name) {
221
+ if (typeof document === "undefined") return null;
222
+ const parts = document.cookie.split(";");
223
+ for (const part of parts) {
224
+ const idx = part.indexOf("=");
225
+ if (idx === -1) continue;
226
+ if (part.slice(0, idx).trim() !== name) continue;
227
+ const value = part.slice(idx + 1).trim();
228
+ try {
229
+ return value ? decodeURIComponent(value) : null;
230
+ } catch {
231
+ return value || null;
232
+ }
233
+ }
234
+ return null;
235
+ }
236
+ function writeCookie(name, value) {
237
+ if (typeof document === "undefined") return;
238
+ document.cookie = `${name}=${encodeURIComponent(value)}; Path=/; Max-Age=31536000; SameSite=Lax`;
239
+ }
219
240
  function getInstanceId() {
220
241
  if (typeof window === "undefined") {
221
242
  return "";
222
243
  }
223
- const stored = localStorage.getItem(INSTANCE_ID_KEY);
224
- if (stored) {
225
- return stored;
244
+ const fromCookie = readCookie(ANON_COOKIE_NAME);
245
+ const fromLs = localStorage.getItem(INSTANCE_ID_KEY);
246
+ const instanceId = fromLs || fromCookie || `browser_${generateGuid()}`;
247
+ if (fromLs !== instanceId) {
248
+ localStorage.setItem(INSTANCE_ID_KEY, instanceId);
249
+ }
250
+ if (fromCookie !== instanceId) {
251
+ writeCookie(ANON_COOKIE_NAME, instanceId);
226
252
  }
227
- const instanceId = `browser_${generateGuid()}`;
228
- localStorage.setItem(INSTANCE_ID_KEY, instanceId);
229
253
  return instanceId;
230
254
  }
231
255
  function getUserId() {
@@ -246,6 +270,7 @@ function clearIds() {
246
270
  }
247
271
  localStorage.removeItem(INSTANCE_ID_KEY);
248
272
  localStorage.removeItem(USER_ID_KEY);
273
+ document.cookie = `${ANON_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
249
274
  }
250
275
 
251
276
  // src/targets.ts