@guidelytech/sdk-core 0.9.0 → 0.10.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/README.md +15 -4
- package/dist/{capture-C3faAYb7.d.cts → capture-369s4YYT.d.ts} +2 -1
- package/dist/{capture-Dqd7up2n.d.ts → capture-DVJyQ2JZ.d.cts} +2 -1
- package/dist/capture.cjs +2 -0
- package/dist/capture.cjs.map +1 -1
- package/dist/capture.d.cts +3 -2
- package/dist/capture.d.ts +3 -2
- package/dist/capture.js +4 -6
- package/dist/capture.js.map +1 -1
- package/dist/chunk-C4WWSF67.js +374 -0
- package/dist/chunk-C4WWSF67.js.map +1 -0
- package/dist/{chunk-544KSOC3.js → chunk-GDJB466J.js} +115 -18
- package/dist/chunk-GDJB466J.js.map +1 -0
- package/dist/headless.cjs +275 -40
- package/dist/headless.cjs.map +1 -1
- package/dist/headless.d.cts +43 -3
- package/dist/headless.d.ts +43 -3
- package/dist/headless.js +14 -8
- package/dist/health-DFHUstWd.d.cts +88 -0
- package/dist/health-DFHUstWd.d.ts +88 -0
- package/dist/{http-47tiAlOM.d.ts → http-D0dCcpLN.d.cts} +10 -35
- package/dist/{http-47tiAlOM.d.cts → http-DJd8jRhK.d.ts} +10 -35
- package/dist/index.cjs +342 -77
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +10 -5
- package/dist/index.d.ts +10 -5
- package/dist/index.js +71 -32
- package/dist/index.js.map +1 -1
- package/dist/recorder.cjs +70 -9
- package/dist/recorder.cjs.map +1 -1
- package/dist/recorder.d.cts +9 -0
- package/dist/recorder.d.ts +9 -0
- package/dist/recorder.js +57 -11
- package/dist/recorder.js.map +1 -1
- package/dist/{types-DnJW_4ck.d.ts → types-B5ecgHUV.d.cts} +10 -3
- package/dist/{types-zM5e3loY.d.cts → types-BtPovimB.d.ts} +10 -3
- package/package.json +4 -4
- package/dist/chunk-544KSOC3.js.map +0 -1
- package/dist/chunk-AKNE7D5M.js +0 -160
- package/dist/chunk-AKNE7D5M.js.map +0 -1
- package/dist/chunk-CYAG6ZYX.js +0 -85
- package/dist/chunk-CYAG6ZYX.js.map +0 -1
- package/dist/chunk-GDF6RCHQ.js +0 -209
- package/dist/chunk-GDF6RCHQ.js.map +0 -1
- package/dist/types-DbKQPvFt.d.cts +0 -238
- package/dist/types-DqhNl5Am.d.ts +0 -238
- package/dist/types-RyeRQd9f.d.ts +0 -226
- package/dist/types-d4eOerxi.d.cts +0 -226
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/queue.ts","../src/outbox.ts","../src/emitter.ts","../src/frequency.ts"],"sourcesContent":["import type { SdkEvent } from \"@guidelytech/shared\";\nimport type { ServerHealth } from \"./health\";\nimport { backoffMs } from \"./http\";\nimport { readJson, writeJson, type KeyValueStore } from \"./storage\";\n\nexport interface EventQueueOptions {\n store: KeyValueStore;\n storageKey: string;\n /** Sends one batch. Resolve `{ ok: true }` on success, `{ ok: false, retry }` otherwise. */\n send: (events: SdkEvent[]) => Promise<{ ok: boolean; retry?: boolean }>;\n /** Synchronous best-effort send used on page hide. */\n beacon?: (events: SdkEvent[]) => boolean;\n /** Should events be flushed right now? (analytics enabled + consent granted). */\n enabled: () => boolean;\n /** API reachability: sends wait while its circuit is open, and beaconed events are only forgotten while it is reachable. */\n health?: ServerHealth;\n flushIntervalMs?: number;\n maxQueue?: number;\n batchSize?: number;\n maxAttempts?: number;\n onError?: (err: unknown) => void;\n}\n\n/**\n * Bounded, persisted event queue. Batches ≤50 events every 2s (or on demand), dedupes by\n * `eventId`, and keeps failed events (also across page loads) until the server takes them:\n * retries back off 1s→60s, wait while the API's circuit is open, and start again as soon as it\n * recovers. Drops the oldest events once the queue exceeds 500.\n */\nexport class EventQueue {\n private events: SdkEvent[] = [];\n private timer: ReturnType<typeof setTimeout> | null = null;\n private retryTimer: ReturnType<typeof setTimeout> | null = null;\n private inflight = false;\n private inflightIds = new Set<string>();\n private attempt = 0;\n private destroyed = false;\n private offRecover: (() => void) | null = null;\n private readonly flushIntervalMs: number;\n private readonly maxQueue: number;\n private readonly batchSize: number;\n private readonly maxAttempts: number;\n\n constructor(private readonly opts: EventQueueOptions) {\n this.flushIntervalMs = opts.flushIntervalMs ?? 2000;\n this.maxQueue = opts.maxQueue ?? 500;\n this.batchSize = opts.batchSize ?? 50;\n this.maxAttempts = opts.maxAttempts ?? 5;\n const persisted = readJson<SdkEvent[]>(opts.store, opts.storageKey, []);\n if (Array.isArray(persisted)) this.events = persisted.filter((e) => e && typeof e.eventId === \"string\").slice(-this.maxQueue);\n // Events left over from a previous page load are flushed on the next tick.\n if (this.events.length) this.schedule();\n this.offRecover = opts.health?.onRecover(() => this.retryNow()) ?? null;\n }\n\n /** The API is back: drop any pending backoff and send what is queued. */\n private retryNow(): void {\n if (this.destroyed) return;\n if (this.retryTimer) clearTimeout(this.retryTimer);\n this.retryTimer = null;\n this.attempt = 0;\n void this.flush();\n }\n\n private retryIn(ms: number): void {\n if (this.retryTimer || this.destroyed) return;\n this.retryTimer = setTimeout(() => {\n this.retryTimer = null;\n void this.flush();\n }, ms);\n }\n\n get size(): number {\n return this.events.length;\n }\n\n /** Pending events (read-only copy, for debugging). */\n peek(): SdkEvent[] {\n return this.events.slice();\n }\n\n enqueue(event: SdkEvent): void {\n if (this.destroyed) return;\n if (this.events.some((e) => e.eventId === event.eventId)) return;\n this.events.push(event);\n if (this.events.length > this.maxQueue) this.events.splice(0, this.events.length - this.maxQueue);\n this.persist();\n this.schedule();\n }\n\n private persist(): void {\n writeJson(this.opts.store, this.opts.storageKey, this.events);\n }\n\n private schedule(): void {\n if (this.timer || this.retryTimer || this.destroyed) return;\n this.timer = setTimeout(() => {\n this.timer = null;\n void this.flush();\n }, this.flushIntervalMs);\n }\n\n /** Sends queued events now (one batch at a time). Safe to call at any time. */\n async flush(): Promise<void> {\n if (this.destroyed || this.inflight || !this.events.length || !this.opts.enabled()) return;\n if (this.timer) {\n clearTimeout(this.timer);\n this.timer = null;\n }\n const wait = this.opts.health?.waitMs() ?? 0;\n if (wait > 0) return this.retryIn(wait);\n this.inflight = true;\n const batch = this.events.slice(0, this.batchSize);\n this.inflightIds = new Set(batch.map((e) => e.eventId));\n let ok = false;\n let retry = true;\n try {\n const res = await this.opts.send(batch);\n ok = res.ok;\n retry = res.retry ?? !res.ok;\n } catch (err) {\n this.opts.onError?.(err);\n }\n this.inflight = false;\n this.inflightIds.clear();\n if (this.destroyed) return;\n if (ok || !retry) {\n const ids = new Set(batch.map((e) => e.eventId));\n this.events = this.events.filter((e) => !ids.has(e.eventId));\n this.attempt = 0;\n this.persist();\n if (this.events.length) this.schedule();\n return;\n }\n // Fast retries first, then about once a minute until the server takes the events (or the\n // API's circuit says to wait longer); a recovery or the next page load sends them sooner.\n this.attempt = Math.min(this.attempt + 1, this.maxAttempts);\n const delay = this.attempt >= this.maxAttempts ? 60_000 : backoffMs(this.attempt - 1);\n this.retryIn(Math.max(delay, this.opts.health?.waitMs() ?? 0));\n }\n\n /** Best-effort synchronous flush on unload using `sendBeacon`. */\n flushSync(): void {\n if (this.destroyed || !this.opts.enabled() || !this.opts.beacon) return;\n // Events already in flight (fetch with keepalive) survive unload; only beacon the rest to avoid duplicate batches.\n const batch = this.events.filter((e) => !this.inflightIds.has(e.eventId)).slice(0, this.batchSize);\n if (!batch.length) return;\n // A beacon that was queued may still never arrive (the server is down), and nothing reports\n // back. Only forget its events while the API is answering; otherwise they stay persisted and the\n // next page load sends them again (the server drops events it already has, by eventId).\n if (this.opts.beacon(batch) && (this.opts.health?.reachable ?? true)) {\n const ids = new Set(batch.map((e) => e.eventId));\n this.events = this.events.filter((e) => !ids.has(e.eventId));\n }\n this.persist();\n }\n\n clear(): void {\n this.events = [];\n this.persist();\n }\n\n destroy(): void {\n this.destroyed = true;\n this.offRecover?.();\n if (this.timer) clearTimeout(this.timer);\n if (this.retryTimer) clearTimeout(this.retryTimer);\n this.timer = null;\n this.retryTimer = null;\n this.persist();\n }\n}\n","import type { ServerHealth } from \"./health\";\nimport { backoffMs, type Api } from \"./http\";\nimport { readJson, writeJson, type KeyValueStore } from \"./storage\";\n\n/** One request waiting to reach the server. */\ninterface OutboxItem {\n /** Same key → the newer request replaces the older one (e.g. a checklist's latest progress). */\n key: string;\n path: string;\n body: unknown;\n createdAt: number;\n}\n\nexport interface OutboxOptions {\n api: Api;\n store: KeyValueStore;\n storageKey: string;\n health?: ServerHealth;\n maxItems?: number;\n maxAgeMs?: number;\n onError?: (err: unknown) => void;\n}\n\n/**\n * Persisted queue for writes that must not be lost when the API is down: survey answers, guide\n * completions and checklist progress. Each request is kept (also across page loads) until the\n * server answers it; 5xx, 429 and network errors are retried with backoff, waiting while the API's\n * circuit is open. Every request is idempotent on the server (survey answers dedupe by\n * `responseId`, progress merges), so sending one twice is harmless. Bounded to 50 requests and 7 days.\n */\nexport class Outbox {\n private items: OutboxItem[];\n private sending = false;\n private attempt = 0;\n private timer: ReturnType<typeof setTimeout> | null = null;\n private destroyed = false;\n private readonly offRecover: (() => void) | null;\n private readonly maxItems: number;\n private readonly maxAgeMs: number;\n\n constructor(private readonly opts: OutboxOptions) {\n this.maxItems = opts.maxItems ?? 50;\n this.maxAgeMs = opts.maxAgeMs ?? 7 * 24 * 60 * 60 * 1000;\n const stored = readJson<OutboxItem[]>(opts.store, opts.storageKey, []);\n const cutoff = Date.now() - this.maxAgeMs;\n this.items = Array.isArray(stored) ? stored.filter((i) => i && typeof i.path === \"string\" && typeof i.key === \"string\" && i.createdAt > cutoff).slice(-this.maxItems) : [];\n this.offRecover = opts.health?.onRecover(() => {\n this.attempt = 0;\n this.retryIn(0);\n }) ?? null;\n if (this.items.length) this.retryIn(0);\n }\n\n get size(): number {\n return this.items.length;\n }\n\n /** Queues `POST path` and sends it now (or as soon as the API is reachable). */\n add(key: string, path: string, body: unknown): void {\n if (this.destroyed) return;\n this.items = this.items.filter((i) => i.key !== key);\n this.items.push({ key, path, body, createdAt: Date.now() });\n if (this.items.length > this.maxItems) this.items.splice(0, this.items.length - this.maxItems);\n this.persist();\n void this.flush();\n }\n\n private persist(): void {\n writeJson(this.opts.store, this.opts.storageKey, this.items);\n }\n\n private retryIn(ms: number): void {\n if (this.destroyed) return;\n if (this.timer) clearTimeout(this.timer);\n this.timer = setTimeout(() => {\n this.timer = null;\n void this.flush();\n }, ms);\n }\n\n /** Sends queued requests in order, one at a time. */\n async flush(): Promise<void> {\n if (this.destroyed || this.sending || !this.items.length) return;\n const wait = this.opts.health?.waitMs() ?? 0;\n if (wait > 0) return this.retryIn(wait);\n this.sending = true;\n try {\n while (this.items.length && !this.destroyed) {\n const item = this.items[0] as OutboxItem;\n let retry = false;\n try {\n const res = await this.opts.api.post(item.path, item.body);\n retry = !res.ok && (res.status === 429 || res.status >= 500);\n } catch (err) {\n retry = true;\n this.opts.onError?.(err);\n }\n if (retry) {\n this.attempt += 1;\n this.retryIn(Math.max(backoffMs(this.attempt - 1, 1000, 60_000), this.opts.health?.waitMs() ?? 0));\n return;\n }\n // Answered (or refused for good, e.g. the survey was deleted): done with it. A newer request\n // under the same key may have replaced it while this one was in flight; keep that one.\n this.attempt = 0;\n this.items = this.items.filter((i) => i !== item);\n this.persist();\n }\n } finally {\n this.sending = false;\n }\n }\n\n destroy(): void {\n this.destroyed = true;\n this.offRecover?.();\n if (this.timer) clearTimeout(this.timer);\n this.timer = null;\n }\n}\n","type AnyFn = (...args: any[]) => void;\n\n/** Tiny typed event emitter. Listener errors are reported, never thrown into the caller. */\nexport class Emitter<Events extends { [K in keyof Events]: AnyFn }> {\n private listeners = new Map<keyof Events, Set<AnyFn>>();\n constructor(private readonly onError?: (err: unknown) => void) {}\n\n on<K extends keyof Events>(event: K, cb: Events[K]): () => void {\n let set = this.listeners.get(event);\n if (!set) {\n set = new Set();\n this.listeners.set(event, set);\n }\n set.add(cb);\n return () => this.off(event, cb);\n }\n\n off<K extends keyof Events>(event: K, cb: Events[K]): void {\n this.listeners.get(event)?.delete(cb);\n }\n\n emit<K extends keyof Events>(event: K, ...args: Parameters<Events[K]>): void {\n const set = this.listeners.get(event);\n if (!set) return;\n for (const cb of Array.from(set)) {\n try {\n cb(...args);\n } catch (err) {\n this.onError?.(err);\n }\n }\n }\n\n clear(): void {\n this.listeners.clear();\n }\n}\n","import type { FrequencyConfig, PublishedGuide } from \"@guidelytech/shared\";\n\n/** Per-guide state kept locally and merged with the server's `IdentifyResponse.guideState`. */\nexport interface GuideLocalState {\n impressions: number;\n completedAt?: string | null;\n dismissedAt?: string | null;\n lastShownAt?: string | null;\n /** Last step reached in a multi-step flow (local only), so the messenger can offer to resume it. */\n lastStepId?: string | null;\n}\n\nconst HOUR = 3600 * 1000;\n\n/**\n * Pure frequency evaluation. `shownThisSession` comes from session storage.\n * - `once`: a single impression ever (unless `reshowAfterDismiss` and it was only dismissed).\n * - `once_per_session`: at most once per browser session.\n * - `every_time`: on every trigger (still blocked by completion/dismissal rules below).\n * - `interval`: at most once every `intervalHours`.\n * `maxImpressions` caps total impressions; dismissal blocks re-showing unless `reshowAfterDismiss`\n * (then `cooldownHours` must have elapsed); completion blocks unless mode is `every_time`.\n */\nexport function isFrequencyEligible(freq: FrequencyConfig, state: GuideLocalState | undefined, shownThisSession: boolean, now: number = Date.now()): boolean {\n const s = state ?? { impressions: 0 };\n if (freq.maxImpressions != null && s.impressions >= freq.maxImpressions) return false;\n if (s.completedAt && freq.mode !== \"every_time\") return false;\n if (s.dismissedAt) {\n if (!freq.reshowAfterDismiss) return false;\n if (freq.cooldownHours > 0 && now - Date.parse(s.dismissedAt) < freq.cooldownHours * HOUR) return false;\n }\n switch (freq.mode) {\n case \"once\":\n return s.impressions === 0 || (!!s.dismissedAt && freq.reshowAfterDismiss);\n case \"once_per_session\":\n return !shownThisSession;\n case \"interval\": {\n if (!s.lastShownAt) return true;\n const hours = freq.intervalHours ?? 24;\n return now - Date.parse(s.lastShownAt) >= hours * HOUR;\n }\n case \"every_time\":\n default:\n return true;\n }\n}\n\n/**\n * Exclusive guides (flows, modals, tooltips, announcements, surveys) run one at a time.\n * Banners, checklists and hotspot-only tooltips can coexist with an active flow.\n */\nexport function isExclusiveGuide(guide: PublishedGuide): boolean {\n if (guide.kind === \"banner\" || guide.kind === \"checklist\") return false;\n const steps = guide.config.steps;\n if (steps.length > 0 && steps.every((s) => s.type === \"hotspot\" || s.type === \"banner\")) return false;\n return true;\n}\n\n/** Is `a` mutually exclusive with `b` (by id or key, in either direction)? */\nexport function areMutuallyExclusive(a: PublishedGuide, b: PublishedGuide): boolean {\n const la = a.config.frequency.mutuallyExclusiveWith;\n const lb = b.config.frequency.mutuallyExclusiveWith;\n return la.includes(b.id) || la.includes(b.key) || lb.includes(a.id) || lb.includes(a.key);\n}\n\n/** Survey scheduling window. */\nexport function isWithinSchedule(guide: PublishedGuide, now: number = Date.now()): boolean {\n const s = guide.config.survey;\n if (!s) return true;\n if (s.startAt && now < Date.parse(s.startAt)) return false;\n if (s.endAt && now > Date.parse(s.endAt)) return false;\n return true;\n}\n\n/**\n * A checklist's dismissal after merging server state. A local dismissal normally wins (it may not have\n * synced yet), but when the server reports a reset this device hasn't applied, the server's value wins:\n * null, or a dismissal made on another device after the reset. Compared as tokens, not times, so a\n * skewed device clock can't keep a reset dismissal.\n */\nexport function dismissedAfterMerge(local: { dismissedAt?: string | null; dismissResetAt?: string | null } | undefined, server: { dismissedAt?: string | null; dismissResetAt?: string | null }): string | null {\n if (server.dismissResetAt && server.dismissResetAt !== local?.dismissResetAt) return server.dismissedAt ?? null;\n return local?.dismissedAt ?? server.dismissedAt ?? null;\n}\n"],"mappings":";;;;;;;;AA6BO,IAAM,aAAN,MAAiB;AAAA,EActB,YAA6B,MAAyB;AAAzB;AAb7B,wBAAQ,UAAqB,CAAC;AAC9B,wBAAQ,SAA8C;AACtD,wBAAQ,cAAmD;AAC3D,wBAAQ,YAAW;AACnB,wBAAQ,eAAc,oBAAI,IAAY;AACtC,wBAAQ,WAAU;AAClB,wBAAQ,aAAY;AACpB,wBAAQ,cAAkC;AAC1C,wBAAiB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ueue: () => EventQueue,
|
|
28
28
|
KEYS: () => KEYS,
|
|
29
|
+
Outbox: () => Outbox,
|
|
30
|
+
ServerHealth: () => ServerHealth,
|
|
29
31
|
areMutuallyExclusive: () => areMutuallyExclusive,
|
|
30
32
|
backoffMs: () => backoffMs,
|
|
31
33
|
dismissedAfterMerge: () => dismissedAfterMerge,
|
|
32
34
|
isExclusiveGuide: () => isExclusiveGuide,
|
|
33
35
|
isFrequencyEligible: () => isFrequencyEligible,
|
|
36
|
+
isServerFailure: () => isServerFailure,
|
|
34
37
|
isWithinSchedule: () => isWithinSchedule,
|
|
35
38
|
memoryStore: () => memoryStore,
|
|
39
|
+
parseRetryAfter: () => parseRetryAfter,
|
|
36
40
|
postWithRetry: () => postWithRetry,
|
|
37
41
|
readJson: () => readJson,
|
|
38
42
|
sleep: () => sleep,
|
|
@@ -40,12 +44,108 @@ __export(headless_exports, {
|
|
|
40
44
|
});
|
|
41
45
|
module.exports = __toCommonJS(headless_exports);
|
|
42
46
|
|
|
47
|
+
// src/health.ts
|
|
48
|
+
var MAX_RETRY_AFTER_MS = 60 * 60 * 1e3;
|
|
49
|
+
var OFFLINE_WAIT_MS = 3e4;
|
|
50
|
+
var ServerHealth = class {
|
|
51
|
+
constructor(opts = {}) {
|
|
52
|
+
__publicField(this, "failures", 0);
|
|
53
|
+
__publicField(this, "openUntil", 0);
|
|
54
|
+
__publicField(this, "lastOkAt", 0);
|
|
55
|
+
__publicField(this, "listeners", /* @__PURE__ */ new Set());
|
|
56
|
+
__publicField(this, "threshold");
|
|
57
|
+
__publicField(this, "baseMs");
|
|
58
|
+
__publicField(this, "maxMs");
|
|
59
|
+
__publicField(this, "now");
|
|
60
|
+
__publicField(this, "random");
|
|
61
|
+
this.threshold = opts.threshold ?? 3;
|
|
62
|
+
this.baseMs = opts.baseMs ?? 5e3;
|
|
63
|
+
this.maxMs = opts.maxMs ?? 5 * 60 * 1e3;
|
|
64
|
+
this.now = opts.now ?? Date.now;
|
|
65
|
+
this.random = opts.random ?? Math.random;
|
|
66
|
+
}
|
|
67
|
+
/** The server answered (2xx, 3xx, or a 4xx other than 429): it is up. */
|
|
68
|
+
success() {
|
|
69
|
+
const wasDown = this.failures > 0 || this.openUntil > 0;
|
|
70
|
+
this.failures = 0;
|
|
71
|
+
this.openUntil = 0;
|
|
72
|
+
this.lastOkAt = this.now();
|
|
73
|
+
if (wasDown) this.notify();
|
|
74
|
+
}
|
|
75
|
+
/** The request failed or the server is overloaded. `retryAfterMs` comes from a `Retry-After` header. */
|
|
76
|
+
failure(retryAfterMs) {
|
|
77
|
+
this.failures += 1;
|
|
78
|
+
const now = this.now();
|
|
79
|
+
if (retryAfterMs != null && retryAfterMs > 0) {
|
|
80
|
+
this.openUntil = Math.max(this.openUntil, now + Math.min(retryAfterMs, MAX_RETRY_AFTER_MS));
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (this.failures < this.threshold) return;
|
|
84
|
+
const cooldown = Math.min(this.maxMs, this.baseMs * 2 ** (this.failures - this.threshold));
|
|
85
|
+
this.openUntil = Math.max(this.openUntil, now + cooldown * (0.8 + this.random() * 0.4));
|
|
86
|
+
}
|
|
87
|
+
/** Milliseconds background traffic should wait before trying again (0: send now). */
|
|
88
|
+
waitMs() {
|
|
89
|
+
if (typeof navigator !== "undefined" && navigator.onLine === false) return OFFLINE_WAIT_MS;
|
|
90
|
+
return Math.max(0, this.openUntil - this.now());
|
|
91
|
+
}
|
|
92
|
+
canSend() {
|
|
93
|
+
return this.waitMs() === 0;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* The last request got an answer. Unload beacons can't report back, so what they carry is only
|
|
97
|
+
* forgotten while this holds; otherwise it stays queued and is sent again (the server drops duplicates).
|
|
98
|
+
*/
|
|
99
|
+
get reachable() {
|
|
100
|
+
return this.failures === 0 && this.lastOkAt > 0;
|
|
101
|
+
}
|
|
102
|
+
/** The browser came back online: end the cooldown and let queued traffic try straight away. */
|
|
103
|
+
online() {
|
|
104
|
+
this.openUntil = 0;
|
|
105
|
+
this.notify();
|
|
106
|
+
}
|
|
107
|
+
/** Called when the server answers again after failures, or the browser comes back online. */
|
|
108
|
+
onRecover(cb) {
|
|
109
|
+
this.listeners.add(cb);
|
|
110
|
+
return () => this.listeners.delete(cb);
|
|
111
|
+
}
|
|
112
|
+
notify() {
|
|
113
|
+
for (const cb of Array.from(this.listeners)) {
|
|
114
|
+
try {
|
|
115
|
+
cb();
|
|
116
|
+
} catch {
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
function parseRetryAfter(value, now = Date.now()) {
|
|
122
|
+
if (!value) return null;
|
|
123
|
+
const trimmed = value.trim();
|
|
124
|
+
if (/^\d+$/.test(trimmed)) return Number(trimmed) * 1e3;
|
|
125
|
+
const at = Date.parse(trimmed);
|
|
126
|
+
return Number.isNaN(at) ? null : Math.max(0, at - now);
|
|
127
|
+
}
|
|
128
|
+
function isServerFailure(status) {
|
|
129
|
+
return status === 429 || status >= 500;
|
|
130
|
+
}
|
|
131
|
+
|
|
43
132
|
// src/http.ts
|
|
133
|
+
var STREAM_RESPONSE_TIMEOUT_MS = 3e4;
|
|
134
|
+
var STREAM_IDLE_TIMEOUT_MS = 6e4;
|
|
44
135
|
var Api = class {
|
|
45
|
-
constructor(baseUrl, key, fetchImpl) {
|
|
136
|
+
constructor(baseUrl, key, fetchImpl, health) {
|
|
46
137
|
__publicField(this, "baseUrl", baseUrl);
|
|
47
138
|
__publicField(this, "key", key);
|
|
48
139
|
__publicField(this, "fetchImpl", fetchImpl);
|
|
140
|
+
/** Reachability of the API, fed by every answer (see {@link ServerHealth}). */
|
|
141
|
+
__publicField(this, "health");
|
|
142
|
+
this.health = health ?? new ServerHealth();
|
|
143
|
+
}
|
|
144
|
+
/** Feeds an answer (or a failed request, `null`) into {@link health}. */
|
|
145
|
+
observe(res) {
|
|
146
|
+
if (!res) this.health.failure();
|
|
147
|
+
else if (isServerFailure(res.status)) this.health.failure(parseRetryAfter(res.headers?.get?.("retry-after")));
|
|
148
|
+
else this.health.success();
|
|
49
149
|
}
|
|
50
150
|
url(path, params = {}) {
|
|
51
151
|
const query = Object.entries({ key: this.key, ...params }).filter((e) => e[1] != null).map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`).join("&");
|
|
@@ -65,15 +165,22 @@ var Api = class {
|
|
|
65
165
|
const ac = typeof AbortController !== "undefined" ? new AbortController() : null;
|
|
66
166
|
const timer = ac ? setTimeout(() => ac.abort(), options.timeoutMs ?? 1e4) : null;
|
|
67
167
|
try {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
168
|
+
let res;
|
|
169
|
+
try {
|
|
170
|
+
res = await f(this.url(path, options.params), {
|
|
171
|
+
method,
|
|
172
|
+
headers,
|
|
173
|
+
body: options.body === void 0 ? void 0 : form ? options.body : JSON.stringify(options.body),
|
|
174
|
+
keepalive: options.keepalive,
|
|
175
|
+
signal: ac?.signal,
|
|
176
|
+
credentials: "omit",
|
|
177
|
+
mode: "cors"
|
|
178
|
+
});
|
|
179
|
+
} catch (err) {
|
|
180
|
+
this.observe(null);
|
|
181
|
+
throw err;
|
|
182
|
+
}
|
|
183
|
+
this.observe(res);
|
|
77
184
|
let data = null;
|
|
78
185
|
if (res.status !== 304 && res.status !== 204) {
|
|
79
186
|
try {
|
|
@@ -99,30 +206,53 @@ var Api = class {
|
|
|
99
206
|
}
|
|
100
207
|
/**
|
|
101
208
|
* POST that may answer with server-sent events. Each event is passed to `onEvent`; a JSON answer is
|
|
102
|
-
* returned instead.
|
|
209
|
+
* returned instead. Scout replies can take a while, so there is no overall timeout: the response
|
|
210
|
+
* must start within 30 s and then never go quiet for more than 60 s (the server sends keepalives),
|
|
211
|
+
* otherwise the request is aborted and rejects.
|
|
103
212
|
*/
|
|
104
213
|
async stream(path, body, onEvent, headers = {}) {
|
|
105
214
|
const f = this.fetcher;
|
|
106
215
|
if (!f) throw new Error("fetch is not available");
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
let
|
|
216
|
+
const ac = typeof AbortController !== "undefined" ? new AbortController() : null;
|
|
217
|
+
let timer = null;
|
|
218
|
+
const arm = (ms) => {
|
|
219
|
+
if (!ac) return;
|
|
220
|
+
if (timer) clearTimeout(timer);
|
|
221
|
+
timer = setTimeout(() => ac.abort(), ms);
|
|
222
|
+
};
|
|
223
|
+
arm(STREAM_RESPONSE_TIMEOUT_MS);
|
|
224
|
+
try {
|
|
225
|
+
let res;
|
|
117
226
|
try {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
227
|
+
res = await f(this.url(path), {
|
|
228
|
+
method: "POST",
|
|
229
|
+
headers: { ...headers, "X-Guidely-Key": this.key, Accept: "text/event-stream, application/json", "Content-Type": "application/json" },
|
|
230
|
+
body: JSON.stringify(body),
|
|
231
|
+
signal: ac?.signal,
|
|
232
|
+
credentials: "omit",
|
|
233
|
+
mode: "cors"
|
|
234
|
+
});
|
|
235
|
+
} catch (err) {
|
|
236
|
+
this.observe(null);
|
|
237
|
+
throw err;
|
|
121
238
|
}
|
|
122
|
-
|
|
239
|
+
this.observe(res);
|
|
240
|
+
const type = res.headers?.get?.("content-type") ?? "";
|
|
241
|
+
if (!type.includes("text/event-stream") || !res.body) {
|
|
242
|
+
let data = null;
|
|
243
|
+
try {
|
|
244
|
+
data = await res.json();
|
|
245
|
+
} catch {
|
|
246
|
+
data = null;
|
|
247
|
+
}
|
|
248
|
+
return { status: res.status, ok: res.ok, data, etag: null };
|
|
249
|
+
}
|
|
250
|
+
arm(STREAM_IDLE_TIMEOUT_MS);
|
|
251
|
+
await readEventStream(res.body, onEvent, () => arm(STREAM_IDLE_TIMEOUT_MS));
|
|
252
|
+
return { status: res.status, ok: res.ok, data: null, etag: null };
|
|
253
|
+
} finally {
|
|
254
|
+
if (timer) clearTimeout(timer);
|
|
123
255
|
}
|
|
124
|
-
await readEventStream(res.body, onEvent);
|
|
125
|
-
return { status: res.status, ok: res.ok, data: null, etag: null };
|
|
126
256
|
}
|
|
127
257
|
/** `navigator.sendBeacon` when available (page unload); returns false when it could not be queued. */
|
|
128
258
|
beacon(path, body) {
|
|
@@ -153,7 +283,7 @@ async function withRetry(send, attempts = 3, baseMs = 500) {
|
|
|
153
283
|
function postWithRetry(api, path, body, attempts = 3, baseMs = 500) {
|
|
154
284
|
return withRetry(() => api.post(path, body), attempts, baseMs);
|
|
155
285
|
}
|
|
156
|
-
async function readEventStream(body, onEvent) {
|
|
286
|
+
async function readEventStream(body, onEvent, onChunk) {
|
|
157
287
|
const reader = body.getReader();
|
|
158
288
|
const decoder = new TextDecoder();
|
|
159
289
|
let buffer = "";
|
|
@@ -176,6 +306,7 @@ async function readEventStream(body, onEvent) {
|
|
|
176
306
|
};
|
|
177
307
|
for (; ; ) {
|
|
178
308
|
const { done, value } = await reader.read();
|
|
309
|
+
if (value) onChunk?.();
|
|
179
310
|
if (value) buffer += decoder.decode(value, { stream: true }).replace(/\r\n?/g, "\n");
|
|
180
311
|
let sep;
|
|
181
312
|
while ((sep = buffer.indexOf("\n\n")) >= 0) {
|
|
@@ -215,6 +346,8 @@ var KEYS = {
|
|
|
215
346
|
state: (key) => `guidely:state:${key}`,
|
|
216
347
|
user: (key) => `guidely:user:${key}`,
|
|
217
348
|
queue: (key) => `guidely:queue:${key}`,
|
|
349
|
+
/** Survey answers and guide/checklist progress not yet accepted by the server. */
|
|
350
|
+
outbox: (key) => `guidely:outbox:${key}`,
|
|
218
351
|
session: "guidely:session",
|
|
219
352
|
active: "guidely:active",
|
|
220
353
|
authoring: "guidely:authoring",
|
|
@@ -238,17 +371,34 @@ var EventQueue = class {
|
|
|
238
371
|
__publicField(this, "inflightIds", /* @__PURE__ */ new Set());
|
|
239
372
|
__publicField(this, "attempt", 0);
|
|
240
373
|
__publicField(this, "destroyed", false);
|
|
374
|
+
__publicField(this, "offRecover", null);
|
|
241
375
|
__publicField(this, "flushIntervalMs");
|
|
242
376
|
__publicField(this, "maxQueue");
|
|
243
377
|
__publicField(this, "batchSize");
|
|
244
378
|
__publicField(this, "maxAttempts");
|
|
245
379
|
this.flushIntervalMs = opts.flushIntervalMs ?? 2e3;
|
|
246
|
-
this.maxQueue = opts.maxQueue ??
|
|
380
|
+
this.maxQueue = opts.maxQueue ?? 500;
|
|
247
381
|
this.batchSize = opts.batchSize ?? 50;
|
|
248
382
|
this.maxAttempts = opts.maxAttempts ?? 5;
|
|
249
383
|
const persisted = readJson(opts.store, opts.storageKey, []);
|
|
250
384
|
if (Array.isArray(persisted)) this.events = persisted.filter((e) => e && typeof e.eventId === "string").slice(-this.maxQueue);
|
|
251
385
|
if (this.events.length) this.schedule();
|
|
386
|
+
this.offRecover = opts.health?.onRecover(() => this.retryNow()) ?? null;
|
|
387
|
+
}
|
|
388
|
+
/** The API is back: drop any pending backoff and send what is queued. */
|
|
389
|
+
retryNow() {
|
|
390
|
+
if (this.destroyed) return;
|
|
391
|
+
if (this.retryTimer) clearTimeout(this.retryTimer);
|
|
392
|
+
this.retryTimer = null;
|
|
393
|
+
this.attempt = 0;
|
|
394
|
+
void this.flush();
|
|
395
|
+
}
|
|
396
|
+
retryIn(ms) {
|
|
397
|
+
if (this.retryTimer || this.destroyed) return;
|
|
398
|
+
this.retryTimer = setTimeout(() => {
|
|
399
|
+
this.retryTimer = null;
|
|
400
|
+
void this.flush();
|
|
401
|
+
}, ms);
|
|
252
402
|
}
|
|
253
403
|
get size() {
|
|
254
404
|
return this.events.length;
|
|
@@ -282,6 +432,8 @@ var EventQueue = class {
|
|
|
282
432
|
clearTimeout(this.timer);
|
|
283
433
|
this.timer = null;
|
|
284
434
|
}
|
|
435
|
+
const wait = this.opts.health?.waitMs() ?? 0;
|
|
436
|
+
if (wait > 0) return this.retryIn(wait);
|
|
285
437
|
this.inflight = true;
|
|
286
438
|
const batch = this.events.slice(0, this.batchSize);
|
|
287
439
|
this.inflightIds = new Set(batch.map((e) => e.eventId));
|
|
@@ -305,26 +457,20 @@ var EventQueue = class {
|
|
|
305
457
|
if (this.events.length) this.schedule();
|
|
306
458
|
return;
|
|
307
459
|
}
|
|
308
|
-
this.attempt
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
return;
|
|
312
|
-
}
|
|
313
|
-
this.retryTimer = setTimeout(() => {
|
|
314
|
-
this.retryTimer = null;
|
|
315
|
-
void this.flush();
|
|
316
|
-
}, backoffMs(this.attempt - 1));
|
|
460
|
+
this.attempt = Math.min(this.attempt + 1, this.maxAttempts);
|
|
461
|
+
const delay = this.attempt >= this.maxAttempts ? 6e4 : backoffMs(this.attempt - 1);
|
|
462
|
+
this.retryIn(Math.max(delay, this.opts.health?.waitMs() ?? 0));
|
|
317
463
|
}
|
|
318
464
|
/** Best-effort synchronous flush on unload using `sendBeacon`. */
|
|
319
465
|
flushSync() {
|
|
320
466
|
if (this.destroyed || !this.opts.enabled() || !this.opts.beacon) return;
|
|
321
467
|
const batch = this.events.filter((e) => !this.inflightIds.has(e.eventId)).slice(0, this.batchSize);
|
|
322
468
|
if (!batch.length) return;
|
|
323
|
-
if (this.opts.beacon(batch)) {
|
|
469
|
+
if (this.opts.beacon(batch) && (this.opts.health?.reachable ?? true)) {
|
|
324
470
|
const ids = new Set(batch.map((e) => e.eventId));
|
|
325
471
|
this.events = this.events.filter((e) => !ids.has(e.eventId));
|
|
326
|
-
this.persist();
|
|
327
472
|
}
|
|
473
|
+
this.persist();
|
|
328
474
|
}
|
|
329
475
|
clear() {
|
|
330
476
|
this.events = [];
|
|
@@ -332,6 +478,7 @@ var EventQueue = class {
|
|
|
332
478
|
}
|
|
333
479
|
destroy() {
|
|
334
480
|
this.destroyed = true;
|
|
481
|
+
this.offRecover?.();
|
|
335
482
|
if (this.timer) clearTimeout(this.timer);
|
|
336
483
|
if (this.retryTimer) clearTimeout(this.retryTimer);
|
|
337
484
|
this.timer = null;
|
|
@@ -340,6 +487,90 @@ var EventQueue = class {
|
|
|
340
487
|
}
|
|
341
488
|
};
|
|
342
489
|
|
|
490
|
+
// src/outbox.ts
|
|
491
|
+
var Outbox = class {
|
|
492
|
+
constructor(opts) {
|
|
493
|
+
__publicField(this, "opts", opts);
|
|
494
|
+
__publicField(this, "items");
|
|
495
|
+
__publicField(this, "sending", false);
|
|
496
|
+
__publicField(this, "attempt", 0);
|
|
497
|
+
__publicField(this, "timer", null);
|
|
498
|
+
__publicField(this, "destroyed", false);
|
|
499
|
+
__publicField(this, "offRecover");
|
|
500
|
+
__publicField(this, "maxItems");
|
|
501
|
+
__publicField(this, "maxAgeMs");
|
|
502
|
+
this.maxItems = opts.maxItems ?? 50;
|
|
503
|
+
this.maxAgeMs = opts.maxAgeMs ?? 7 * 24 * 60 * 60 * 1e3;
|
|
504
|
+
const stored = readJson(opts.store, opts.storageKey, []);
|
|
505
|
+
const cutoff = Date.now() - this.maxAgeMs;
|
|
506
|
+
this.items = Array.isArray(stored) ? stored.filter((i) => i && typeof i.path === "string" && typeof i.key === "string" && i.createdAt > cutoff).slice(-this.maxItems) : [];
|
|
507
|
+
this.offRecover = opts.health?.onRecover(() => {
|
|
508
|
+
this.attempt = 0;
|
|
509
|
+
this.retryIn(0);
|
|
510
|
+
}) ?? null;
|
|
511
|
+
if (this.items.length) this.retryIn(0);
|
|
512
|
+
}
|
|
513
|
+
get size() {
|
|
514
|
+
return this.items.length;
|
|
515
|
+
}
|
|
516
|
+
/** Queues `POST path` and sends it now (or as soon as the API is reachable). */
|
|
517
|
+
add(key, path, body) {
|
|
518
|
+
if (this.destroyed) return;
|
|
519
|
+
this.items = this.items.filter((i) => i.key !== key);
|
|
520
|
+
this.items.push({ key, path, body, createdAt: Date.now() });
|
|
521
|
+
if (this.items.length > this.maxItems) this.items.splice(0, this.items.length - this.maxItems);
|
|
522
|
+
this.persist();
|
|
523
|
+
void this.flush();
|
|
524
|
+
}
|
|
525
|
+
persist() {
|
|
526
|
+
writeJson(this.opts.store, this.opts.storageKey, this.items);
|
|
527
|
+
}
|
|
528
|
+
retryIn(ms) {
|
|
529
|
+
if (this.destroyed) return;
|
|
530
|
+
if (this.timer) clearTimeout(this.timer);
|
|
531
|
+
this.timer = setTimeout(() => {
|
|
532
|
+
this.timer = null;
|
|
533
|
+
void this.flush();
|
|
534
|
+
}, ms);
|
|
535
|
+
}
|
|
536
|
+
/** Sends queued requests in order, one at a time. */
|
|
537
|
+
async flush() {
|
|
538
|
+
if (this.destroyed || this.sending || !this.items.length) return;
|
|
539
|
+
const wait = this.opts.health?.waitMs() ?? 0;
|
|
540
|
+
if (wait > 0) return this.retryIn(wait);
|
|
541
|
+
this.sending = true;
|
|
542
|
+
try {
|
|
543
|
+
while (this.items.length && !this.destroyed) {
|
|
544
|
+
const item = this.items[0];
|
|
545
|
+
let retry = false;
|
|
546
|
+
try {
|
|
547
|
+
const res = await this.opts.api.post(item.path, item.body);
|
|
548
|
+
retry = !res.ok && (res.status === 429 || res.status >= 500);
|
|
549
|
+
} catch (err) {
|
|
550
|
+
retry = true;
|
|
551
|
+
this.opts.onError?.(err);
|
|
552
|
+
}
|
|
553
|
+
if (retry) {
|
|
554
|
+
this.attempt += 1;
|
|
555
|
+
this.retryIn(Math.max(backoffMs(this.attempt - 1, 1e3, 6e4), this.opts.health?.waitMs() ?? 0));
|
|
556
|
+
return;
|
|
557
|
+
}
|
|
558
|
+
this.attempt = 0;
|
|
559
|
+
this.items = this.items.filter((i) => i !== item);
|
|
560
|
+
this.persist();
|
|
561
|
+
}
|
|
562
|
+
} finally {
|
|
563
|
+
this.sending = false;
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
destroy() {
|
|
567
|
+
this.destroyed = true;
|
|
568
|
+
this.offRecover?.();
|
|
569
|
+
if (this.timer) clearTimeout(this.timer);
|
|
570
|
+
this.timer = null;
|
|
571
|
+
}
|
|
572
|
+
};
|
|
573
|
+
|
|
343
574
|
// src/emitter.ts
|
|
344
575
|
var Emitter = class {
|
|
345
576
|
constructor(onError) {
|
|
@@ -427,13 +658,17 @@ function dismissedAfterMerge(local, server) {
|
|
|
427
658
|
Emitter,
|
|
428
659
|
EventQueue,
|
|
429
660
|
KEYS,
|
|
661
|
+
Outbox,
|
|
662
|
+
ServerHealth,
|
|
430
663
|
areMutuallyExclusive,
|
|
431
664
|
backoffMs,
|
|
432
665
|
dismissedAfterMerge,
|
|
433
666
|
isExclusiveGuide,
|
|
434
667
|
isFrequencyEligible,
|
|
668
|
+
isServerFailure,
|
|
435
669
|
isWithinSchedule,
|
|
436
670
|
memoryStore,
|
|
671
|
+
parseRetryAfter,
|
|
437
672
|
postWithRetry,
|
|
438
673
|
readJson,
|
|
439
674
|
sleep,
|