@offscript/sdk 0.1.5 → 0.1.6

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,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var chunkHPDLEAAL_cjs = require('./chunk-HPDLEAAL.cjs');
3
+ var chunkMZXF2LF2_cjs = require('./chunk-MZXF2LF2.cjs');
4
4
 
5
5
  // src/types/config.ts
6
6
  var DEFAULT_RECORDING = {
@@ -347,7 +347,7 @@ var ActionRecorder = class {
347
347
  this.scrollState = /* @__PURE__ */ new Map();
348
348
  this.move = [];
349
349
  const r = sink.config.recording;
350
- this.gen = new chunkHPDLEAAL_cjs.SelectorGenerator({ testIdAttributes: r.testIdAttributes, stableAttributes: r.stableAttributes });
350
+ this.gen = new chunkMZXF2LF2_cjs.SelectorGenerator({ testIdAttributes: r.testIdAttributes, stableAttributes: r.stableAttributes });
351
351
  }
352
352
  start() {
353
353
  const d = this.d;
@@ -411,7 +411,7 @@ var ActionRecorder = class {
411
411
  emit(draft, el, extra) {
412
412
  const s = this.sink.sanitizer;
413
413
  const data = {
414
- schemaVersion: chunkHPDLEAAL_cjs.ACTION_SCHEMA_VERSION,
414
+ schemaVersion: chunkMZXF2LF2_cjs.ACTION_SCHEMA_VERSION,
415
415
  type: draft.type,
416
416
  url: s.sanitizeUrl(location.href),
417
417
  viewport: { width: window.innerWidth, height: window.innerHeight },
@@ -672,7 +672,7 @@ function collectContext(reason, captureSensitiveValues = false) {
672
672
  timezone: safe(() => Intl.DateTimeFormat().resolvedOptions().timeZone),
673
673
  online: nav.onLine,
674
674
  visibility: document.visibilityState,
675
- sdkVersion: chunkHPDLEAAL_cjs.SDK_VERSION,
675
+ sdkVersion: chunkMZXF2LF2_cjs.SDK_VERSION,
676
676
  captureSensitiveValues
677
677
  };
678
678
  }
@@ -1533,6 +1533,7 @@ function resolveConfig(input) {
1533
1533
  privacy: resolvePrivacy(input.privacy),
1534
1534
  correlationWindowMs: input.correlationWindowMs ?? 3e3,
1535
1535
  controls: input.controls ?? true,
1536
+ endpoint: input.endpoint,
1536
1537
  persistenceKey: input.persistenceKey,
1537
1538
  onComplete: input.onComplete
1538
1539
  };
@@ -2211,6 +2212,19 @@ var SUCCESS_TEMPLATE = `
2211
2212
  `;
2212
2213
 
2213
2214
  // src/core/OffscriptSDK.ts
2215
+ var TOKEN_STORAGE_KEY = "offscript-session-token";
2216
+ var DRAFT_STORAGE_KEY = "offscript-draft-key";
2217
+ var COMPLETED_TOKEN_STORAGE_KEY = "offscript-completed-token";
2218
+ function tokenFingerprint(token) {
2219
+ let first = 2166136261;
2220
+ let second = 2654435769;
2221
+ for (let index = 0; index < token.length; index++) {
2222
+ const character = token.charCodeAt(index);
2223
+ first = Math.imul(first ^ character, 16777619);
2224
+ second = Math.imul(second ^ character, 2246822519);
2225
+ }
2226
+ return `${first >>> 0}.${second >>> 0}`;
2227
+ }
2214
2228
  var Offscript = class {
2215
2229
  constructor(config = {}) {
2216
2230
  this.bus = new Emitter();
@@ -2226,6 +2240,7 @@ var Offscript = class {
2226
2240
  this.sequence = 0;
2227
2241
  this.pageId = uid("page_");
2228
2242
  this.submitted = false;
2243
+ this.launchTokenChanged = false;
2229
2244
  this.config = resolveConfig(config);
2230
2245
  this.sanitizer = new DataSanitizer(this.config.privacy);
2231
2246
  }
@@ -2238,17 +2253,70 @@ var Offscript = class {
2238
2253
  async init() {
2239
2254
  this.assertAlive();
2240
2255
  if (typeof window === "undefined") throw new OffscriptError("NOT_SUPPORTED", "O SDK requer um navegador");
2241
- this.initPromise ?? (this.initPromise = this.initializeDraft());
2256
+ this.initPromise ?? (this.initPromise = this.initialize());
2242
2257
  await this.initPromise;
2243
2258
  if (this.restorePromise) await this.restorePromise;
2244
- if (this.config.controls && !this.controls) {
2259
+ if (this.config.controls && this.launchToken && !this.controls) {
2245
2260
  this.controls = new RecordingControls(this);
2246
2261
  this.controls.mount();
2247
2262
  }
2248
2263
  }
2264
+ async initialize() {
2265
+ this.resolveLaunchToken();
2266
+ if (this.launchToken && !this.config.persistenceKey) {
2267
+ this.config.persistenceKey = this.getOrCreateDraftKey();
2268
+ }
2269
+ await this.initializeDraft();
2270
+ }
2271
+ resolveLaunchToken() {
2272
+ const incomingToken = new URLSearchParams(window.location.search).get("offscript_token")?.trim();
2273
+ const previousToken = this.readSessionValue(TOKEN_STORAGE_KEY);
2274
+ const completedToken = this.readSessionValue(COMPLETED_TOKEN_STORAGE_KEY);
2275
+ const legacyCompletion = completedToken === "1" && Boolean(previousToken);
2276
+ const completedFingerprint = legacyCompletion && previousToken ? tokenFingerprint(previousToken) : completedToken;
2277
+ const candidateToken = incomingToken || previousToken || void 0;
2278
+ this.launchTokenChanged = Boolean(incomingToken && previousToken && incomingToken !== previousToken) || Boolean(incomingToken && completedFingerprint && completedFingerprint !== "1" && tokenFingerprint(incomingToken) !== completedFingerprint);
2279
+ if (this.launchTokenChanged) {
2280
+ this.removeSessionValue(COMPLETED_TOKEN_STORAGE_KEY);
2281
+ }
2282
+ if (legacyCompletion && previousToken && !this.launchTokenChanged) {
2283
+ this.writeSessionValue(COMPLETED_TOKEN_STORAGE_KEY, tokenFingerprint(previousToken));
2284
+ this.removeSessionValue(TOKEN_STORAGE_KEY);
2285
+ }
2286
+ const isCompleted = Boolean(candidateToken && completedFingerprint === tokenFingerprint(candidateToken));
2287
+ this.launchToken = isCompleted ? void 0 : candidateToken;
2288
+ if (incomingToken && !isCompleted) this.writeSessionValue(TOKEN_STORAGE_KEY, incomingToken);
2289
+ }
2290
+ getOrCreateDraftKey() {
2291
+ const saved = this.readSessionValue(DRAFT_STORAGE_KEY);
2292
+ if (saved) return saved;
2293
+ const key = uid("draft_");
2294
+ this.writeSessionValue(DRAFT_STORAGE_KEY, key);
2295
+ return key;
2296
+ }
2297
+ readSessionValue(key) {
2298
+ try {
2299
+ return window.sessionStorage.getItem(key);
2300
+ } catch {
2301
+ return null;
2302
+ }
2303
+ }
2304
+ writeSessionValue(key, value) {
2305
+ try {
2306
+ window.sessionStorage.setItem(key, value);
2307
+ } catch {
2308
+ }
2309
+ }
2310
+ removeSessionValue(key) {
2311
+ try {
2312
+ window.sessionStorage.removeItem(key);
2313
+ } catch {
2314
+ }
2315
+ }
2249
2316
  async initializeDraft() {
2250
2317
  if (!this.config.persistenceKey) return;
2251
2318
  this.store = await RecordingStore.open(this.config.persistenceKey);
2319
+ if (this.launchTokenChanged) await this.store.clear();
2252
2320
  await this.restoreDraft();
2253
2321
  }
2254
2322
  async restoreDraft() {
@@ -2358,7 +2426,7 @@ var Offscript = class {
2358
2426
  this.bus.emit("recordingEnd", this.payload);
2359
2427
  return this.payload;
2360
2428
  }
2361
- complete(options) {
2429
+ complete(options = {}) {
2362
2430
  this.assertAlive();
2363
2431
  if (this.completePromise) return this.completePromise;
2364
2432
  if (this.submitted) return Promise.resolve(this.response);
@@ -2367,19 +2435,27 @@ var Offscript = class {
2367
2435
  });
2368
2436
  return this.completePromise;
2369
2437
  }
2370
- async doComplete({ endpoint, token }) {
2438
+ async doComplete(options) {
2439
+ const endpoint = options.endpoint ?? this.config.endpoint;
2440
+ const token = options.token?.trim() || this.launchToken;
2441
+ if (!endpoint) throw new ConfigError("Endpoint HTTP obrigat\xF3rio para envio");
2442
+ if (!token) throw new ConfigError("Token obrigat\xF3rio para envio");
2371
2443
  const url = new URL(endpoint, location.href);
2372
2444
  if (!["https:", "http:"].includes(url.protocol)) throw new ConfigError("Endpoint HTTP inv\xE1lido");
2373
- if (!token?.trim()) throw new ConfigError("Token obrigat\xF3rio para envio");
2374
2445
  const payload = await this.stopRecording();
2375
2446
  try {
2376
2447
  const response = await fetch(url.href, {
2377
2448
  method: "POST",
2378
- headers: { "Content-Type": "application/json", Accept: "application/json", Authorization: `Bearer ${token.trim()}` },
2449
+ headers: { "Content-Type": "application/json", Accept: "application/json", Authorization: `Bearer ${token}` },
2379
2450
  body: JSON.stringify(payload)
2380
2451
  });
2381
2452
  if (!response.ok) throw new OffscriptError("TRANSPORT_FAILED", `Falha ao enviar grava\xE7\xE3o: HTTP ${response.status}`, { status: response.status });
2382
2453
  this.submitted = true;
2454
+ const completedToken = this.launchToken ?? token;
2455
+ this.writeSessionValue(COMPLETED_TOKEN_STORAGE_KEY, tokenFingerprint(completedToken));
2456
+ this.removeSessionValue(TOKEN_STORAGE_KEY);
2457
+ this.removeSessionValue(DRAFT_STORAGE_KEY);
2458
+ this.launchToken = void 0;
2383
2459
  await this.store?.clear();
2384
2460
  const text = await response.text();
2385
2461
  try {
@@ -2389,6 +2465,8 @@ var Offscript = class {
2389
2465
  }
2390
2466
  this.lastError = void 0;
2391
2467
  this.controls?.showSuccess();
2468
+ this.controls?.unmount();
2469
+ this.controls = void 0;
2392
2470
  return this.response;
2393
2471
  } catch (error) {
2394
2472
  const err = error instanceof Error ? error : new Error(String(error));
@@ -2399,6 +2477,7 @@ var Offscript = class {
2399
2477
  }
2400
2478
  async finishRecording() {
2401
2479
  if (this.config.onComplete) return this.config.onComplete();
2480
+ if (this.launchToken) return this.complete();
2402
2481
  return this.stopRecording();
2403
2482
  }
2404
2483
  async discardRecording() {
@@ -2430,7 +2509,7 @@ var Offscript = class {
2430
2509
  this.destroyed = true;
2431
2510
  }
2432
2511
  exportPlaywrightData() {
2433
- return chunkHPDLEAAL_cjs.buildPlaywrightContract({ actions: this.actionLog, contexts: this.contextLog });
2512
+ return chunkMZXF2LF2_cjs.buildPlaywrightContract({ actions: this.actionLog, contexts: this.contextLog });
2434
2513
  }
2435
2514
  getRecordedActions() {
2436
2515
  return this.actionLog;
@@ -2572,7 +2651,7 @@ var Offscript = class {
2572
2651
  this.bus.emit("statusChange", { from, to });
2573
2652
  }
2574
2653
  };
2575
- Offscript.version = chunkHPDLEAAL_cjs.SDK_VERSION;
2654
+ Offscript.version = chunkMZXF2LF2_cjs.SDK_VERSION;
2576
2655
 
2577
2656
  exports.ConfigError = ConfigError;
2578
2657
  exports.DEFAULT_PRIVACY = DEFAULT_PRIVACY;
@@ -2581,5 +2660,5 @@ exports.DataSanitizer = DataSanitizer;
2581
2660
  exports.InvalidStateError = InvalidStateError;
2582
2661
  exports.Offscript = Offscript;
2583
2662
  exports.OffscriptError = OffscriptError;
2584
- //# sourceMappingURL=chunk-VYCDQLP7.cjs.map
2585
- //# sourceMappingURL=chunk-VYCDQLP7.cjs.map
2663
+ //# sourceMappingURL=chunk-PCJRHW66.cjs.map
2664
+ //# sourceMappingURL=chunk-PCJRHW66.cjs.map