@hexclave/next 1.0.20 → 1.0.21
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/components/elements/sidebar-layout.js +1 -1
- package/dist/esm/components/elements/sidebar-layout.js +1 -1
- package/dist/esm/generated/quetzal-translations.d.ts +2 -2
- package/dist/esm/lib/hexclave-app/apps/implementations/common.js +1 -1
- package/dist/esm/lib/hexclave-app/apps/implementations/event-tracker.d.ts +1 -0
- package/dist/esm/lib/hexclave-app/apps/implementations/event-tracker.d.ts.map +1 -1
- package/dist/esm/lib/hexclave-app/apps/implementations/event-tracker.js +17 -13
- package/dist/esm/lib/hexclave-app/apps/implementations/event-tracker.js.map +1 -1
- package/dist/esm/lib/hexclave-app/apps/implementations/event-tracker.test.js +4 -8
- package/dist/esm/lib/hexclave-app/apps/implementations/event-tracker.test.js.map +1 -1
- package/dist/esm/lib/hexclave-app/apps/implementations/server-app-impl.d.ts +1 -1
- package/dist/esm/lib/hexclave-app/apps/implementations/session-replay.d.ts +3 -1
- package/dist/esm/lib/hexclave-app/apps/implementations/session-replay.d.ts.map +1 -1
- package/dist/esm/lib/hexclave-app/apps/implementations/session-replay.js +19 -13
- package/dist/esm/lib/hexclave-app/apps/implementations/session-replay.js.map +1 -1
- package/dist/esm/lib/hexclave-app/apps/implementations/session-replay.test.js +4 -9
- package/dist/esm/lib/hexclave-app/apps/implementations/session-replay.test.js.map +1 -1
- package/dist/generated/quetzal-translations.d.ts +2 -2
- package/dist/lib/hexclave-app/apps/implementations/common.js +1 -1
- package/dist/lib/hexclave-app/apps/implementations/event-tracker.d.ts +1 -0
- package/dist/lib/hexclave-app/apps/implementations/event-tracker.d.ts.map +1 -1
- package/dist/lib/hexclave-app/apps/implementations/event-tracker.js +16 -12
- package/dist/lib/hexclave-app/apps/implementations/event-tracker.js.map +1 -1
- package/dist/lib/hexclave-app/apps/implementations/event-tracker.test.js +4 -8
- package/dist/lib/hexclave-app/apps/implementations/event-tracker.test.js.map +1 -1
- package/dist/lib/hexclave-app/apps/implementations/server-app-impl.d.ts +1 -1
- package/dist/lib/hexclave-app/apps/implementations/session-replay.d.ts +3 -1
- package/dist/lib/hexclave-app/apps/implementations/session-replay.d.ts.map +1 -1
- package/dist/lib/hexclave-app/apps/implementations/session-replay.js +19 -12
- package/dist/lib/hexclave-app/apps/implementations/session-replay.js.map +1 -1
- package/dist/lib/hexclave-app/apps/implementations/session-replay.test.js +4 -9
- package/dist/lib/hexclave-app/apps/implementations/session-replay.test.js.map +1 -1
- package/package.json +4 -4
- package/src/lib/hexclave-app/apps/implementations/event-tracker.test.ts +5 -13
- package/src/lib/hexclave-app/apps/implementations/event-tracker.ts +19 -14
- package/src/lib/hexclave-app/apps/implementations/session-replay.test.ts +4 -20
- package/src/lib/hexclave-app/apps/implementations/session-replay.ts +19 -12
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const require_chunk = require('../../../../chunk-BE-pF4vm.js');
|
|
2
2
|
let vitest = require("vitest");
|
|
3
3
|
let _hexclave_shared_dist_utils_results = require("@hexclave/shared/dist/utils/results");
|
|
4
|
+
let _hexclave_shared_dist_known_errors = require("@hexclave/shared/dist/known-errors");
|
|
4
5
|
let __session_replay_js = require("./session-replay.js");
|
|
5
6
|
|
|
6
7
|
//#region src/lib/hexclave-app/apps/implementations/session-replay.test.ts
|
|
@@ -37,7 +38,7 @@ let __session_replay_js = require("./session-replay.js");
|
|
|
37
38
|
});
|
|
38
39
|
});
|
|
39
40
|
(0, vitest.describe)("SessionRecorder flush", () => {
|
|
40
|
-
(0, vitest.it)("silently disables when
|
|
41
|
+
(0, vitest.it)("silently disables when client interface returns ANALYTICS_NOT_ENABLED as an error", async () => {
|
|
41
42
|
vitest.vi.useFakeTimers();
|
|
42
43
|
const storageKey = `hexclave:session-replay:v1:test-project`;
|
|
43
44
|
localStorage.setItem(storageKey, JSON.stringify({
|
|
@@ -50,13 +51,7 @@ let __session_replay_js = require("./session-replay.js");
|
|
|
50
51
|
projectId: "test-project",
|
|
51
52
|
sendBatch: async (body) => {
|
|
52
53
|
sentBodies.push(body);
|
|
53
|
-
return _hexclave_shared_dist_utils_results.Result.
|
|
54
|
-
code: "ANALYTICS_NOT_ENABLED",
|
|
55
|
-
error: "Analytics is not enabled for this project."
|
|
56
|
-
}), {
|
|
57
|
-
status: 400,
|
|
58
|
-
headers: { "x-stack-known-error": "ANALYTICS_NOT_ENABLED" }
|
|
59
|
-
}));
|
|
54
|
+
return _hexclave_shared_dist_utils_results.Result.error(new _hexclave_shared_dist_known_errors.KnownErrors.AnalyticsNotEnabled());
|
|
60
55
|
}
|
|
61
56
|
}, {});
|
|
62
57
|
const warnSpy = vitest.vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
@@ -69,7 +64,7 @@ let __session_replay_js = require("./session-replay.js");
|
|
|
69
64
|
recorder._tick();
|
|
70
65
|
await vitest.vi.advanceTimersByTimeAsync(0);
|
|
71
66
|
(0, vitest.expect)(sentBodies).toHaveLength(1);
|
|
72
|
-
(0, vitest.expect)(warnSpy
|
|
67
|
+
(0, vitest.expect)(warnSpy).not.toHaveBeenCalled();
|
|
73
68
|
recorder._events = [{
|
|
74
69
|
type: 3,
|
|
75
70
|
timestamp: Date.now(),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session-replay.test.js","names":["SessionRecorder","Result","vi"],"sources":["../../../../../src/lib/hexclave-app/apps/implementations/session-replay.test.ts"],"sourcesContent":["\n//===========================================\n// THIS FILE IS AUTO-GENERATED FROM TEMPLATE. DO NOT EDIT IT DIRECTLY UNLESS YOU ALSO EDIT THE CORRESPONDING FILE IN packages/template\n//===========================================\n// @vitest-environment jsdom\n\nimport { describe, expect, it, vi } from \"vitest\";\nimport { Result } from \"@hexclave/shared/dist/utils/results\";\nimport { analyticsOptionsFromJson, analyticsOptionsToJson, getSessionReplayOptions, SessionRecorder } from \"./session-replay\";\n\ndescribe(\"session replay options\", () => {\n it(\"enables replays by default\", () => {\n expect(getSessionReplayOptions(undefined).enabled).toBe(true);\n expect(getSessionReplayOptions({}).enabled).toBe(true);\n expect(getSessionReplayOptions({ replays: {} }).enabled).toBe(true);\n });\n\n it(\"preserves explicit replay opt-out\", () => {\n expect(getSessionReplayOptions({ replays: { enabled: false } }).enabled).toBe(false);\n });\n});\n\ndescribe(\"analytics option JSON conversion\", () => {\n it(\"preserves top-level analytics options when serializing replay block classes\", () => {\n const json = analyticsOptionsToJson({\n enabled: false,\n replays: {\n enabled: true,\n blockClass: /stack-sensitive/u,\n },\n });\n\n expect(json?.enabled).toBe(false);\n expect(json?.replays?.enabled).toBe(true);\n });\n\n it(\"preserves top-level analytics options when deserializing replay block classes\", () => {\n const roundTripped = analyticsOptionsFromJson(analyticsOptionsToJson({\n enabled: false,\n replays: {\n blockClass: /stack-sensitive/u,\n },\n }));\n\n expect(roundTripped?.enabled).toBe(false);\n expect(roundTripped?.replays?.blockClass).toEqual(/stack-sensitive/u);\n });\n});\n\ndescribe(\"SessionRecorder flush\", () => {\n it(\"silently disables when
|
|
1
|
+
{"version":3,"file":"session-replay.test.js","names":["SessionRecorder","Result","KnownErrors","vi"],"sources":["../../../../../src/lib/hexclave-app/apps/implementations/session-replay.test.ts"],"sourcesContent":["\n//===========================================\n// THIS FILE IS AUTO-GENERATED FROM TEMPLATE. DO NOT EDIT IT DIRECTLY UNLESS YOU ALSO EDIT THE CORRESPONDING FILE IN packages/template\n//===========================================\n// @vitest-environment jsdom\n\nimport { KnownErrors } from \"@hexclave/shared/dist/known-errors\";\nimport { describe, expect, it, vi } from \"vitest\";\nimport { Result } from \"@hexclave/shared/dist/utils/results\";\nimport { analyticsOptionsFromJson, analyticsOptionsToJson, getSessionReplayOptions, SessionRecorder } from \"./session-replay\";\n\ndescribe(\"session replay options\", () => {\n it(\"enables replays by default\", () => {\n expect(getSessionReplayOptions(undefined).enabled).toBe(true);\n expect(getSessionReplayOptions({}).enabled).toBe(true);\n expect(getSessionReplayOptions({ replays: {} }).enabled).toBe(true);\n });\n\n it(\"preserves explicit replay opt-out\", () => {\n expect(getSessionReplayOptions({ replays: { enabled: false } }).enabled).toBe(false);\n });\n});\n\ndescribe(\"analytics option JSON conversion\", () => {\n it(\"preserves top-level analytics options when serializing replay block classes\", () => {\n const json = analyticsOptionsToJson({\n enabled: false,\n replays: {\n enabled: true,\n blockClass: /stack-sensitive/u,\n },\n });\n\n expect(json?.enabled).toBe(false);\n expect(json?.replays?.enabled).toBe(true);\n });\n\n it(\"preserves top-level analytics options when deserializing replay block classes\", () => {\n const roundTripped = analyticsOptionsFromJson(analyticsOptionsToJson({\n enabled: false,\n replays: {\n blockClass: /stack-sensitive/u,\n },\n }));\n\n expect(roundTripped?.enabled).toBe(false);\n expect(roundTripped?.replays?.blockClass).toEqual(/stack-sensitive/u);\n });\n});\n\ndescribe(\"SessionRecorder flush\", () => {\n it(\"silently disables when client interface returns ANALYTICS_NOT_ENABLED as an error\", async () => {\n vi.useFakeTimers();\n\n const storageKey = `hexclave:session-replay:v1:test-project`;\n localStorage.setItem(storageKey, JSON.stringify({\n session_id: \"test-session\",\n created_at_ms: Date.now(),\n last_activity_ms: Date.now(),\n }));\n\n const sentBodies: string[] = [];\n const recorder = new SessionRecorder(\n {\n projectId: \"test-project\",\n sendBatch: async (body) => {\n sentBodies.push(body);\n return Result.error(new KnownErrors.AnalyticsNotEnabled());\n },\n },\n {},\n );\n\n const warnSpy = vi.spyOn(console, \"warn\").mockImplementation(() => {});\n\n try {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\n (recorder as any)._events = [{ type: 2, timestamp: Date.now(), data: {} }];\n\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call\n (recorder as any)._tick();\n await vi.advanceTimersByTimeAsync(0);\n\n expect(sentBodies).toHaveLength(1);\n expect(warnSpy).not.toHaveBeenCalled();\n\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\n (recorder as any)._events = [{ type: 3, timestamp: Date.now(), data: {} }];\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call\n (recorder as any)._tick();\n await vi.advanceTimersByTimeAsync(0);\n expect(sentBodies).toHaveLength(1);\n } finally {\n recorder.stop();\n warnSpy.mockRestore();\n localStorage.removeItem(storageKey);\n vi.useRealTimers();\n }\n });\n});\n"],"mappings":";;;;;;;;qBAWS,gCAAgC;AACvC,gBAAG,oCAAoC;AACrC,sEAA+B,OAAU,CAAC,QAAQ,CAAC,KAAK,KAAK;AAC7D,sEAA+B,EAAE,CAAC,CAAC,QAAQ,CAAC,KAAK,KAAK;AACtD,sEAA+B,EAAE,SAAS,EAAE,EAAE,CAAC,CAAC,QAAQ,CAAC,KAAK,KAAK;GACnE;AAEF,gBAAG,2CAA2C;AAC5C,sEAA+B,EAAE,SAAS,EAAE,SAAS,OAAO,EAAE,CAAC,CAAC,QAAQ,CAAC,KAAK,MAAM;GACpF;EACF;qBAEO,0CAA0C;AACjD,gBAAG,qFAAqF;EACtF,MAAM,uDAA8B;GAClC,SAAS;GACT,SAAS;IACP,SAAS;IACT,YAAY;IACb;GACF,CAAC;AAEF,qBAAO,MAAM,QAAQ,CAAC,KAAK,MAAM;AACjC,qBAAO,MAAM,SAAS,QAAQ,CAAC,KAAK,KAAK;GACzC;AAEF,gBAAG,uFAAuF;EACxF,MAAM,iHAA+D;GACnE,SAAS;GACT,SAAS,EACP,YAAY,oBACb;GACF,CAAC,CAAC;AAEH,qBAAO,cAAc,QAAQ,CAAC,KAAK,MAAM;AACzC,qBAAO,cAAc,SAAS,WAAW,CAAC,QAAQ,mBAAmB;GACrE;EACF;qBAEO,+BAA+B;AACtC,gBAAG,qFAAqF,YAAY;AAClG,YAAG,eAAe;EAElB,MAAM,aAAa;AACnB,eAAa,QAAQ,YAAY,KAAK,UAAU;GAC9C,YAAY;GACZ,eAAe,KAAK,KAAK;GACzB,kBAAkB,KAAK,KAAK;GAC7B,CAAC,CAAC;EAEH,MAAM,aAAuB,EAAE;EAC/B,MAAM,WAAW,IAAIA,oCACnB;GACE,WAAW;GACX,WAAW,OAAO,SAAS;AACzB,eAAW,KAAK,KAAK;AACrB,WAAOC,2CAAO,MAAM,IAAIC,+CAAY,qBAAqB,CAAC;;GAE7D,EACD,EAAE,CACH;EAED,MAAM,UAAUC,UAAG,MAAM,SAAS,OAAO,CAAC,yBAAyB,GAAG;AAEtE,MAAI;AAEF,GAAC,SAAiB,UAAU,CAAC;IAAE,MAAM;IAAG,WAAW,KAAK,KAAK;IAAE,MAAM,EAAE;IAAE,CAAC;AAG1E,GAAC,SAAiB,OAAO;AACzB,SAAMA,UAAG,yBAAyB,EAAE;AAEpC,sBAAO,WAAW,CAAC,aAAa,EAAE;AAClC,sBAAO,QAAQ,CAAC,IAAI,kBAAkB;AAGtC,GAAC,SAAiB,UAAU,CAAC;IAAE,MAAM;IAAG,WAAW,KAAK,KAAK;IAAE,MAAM,EAAE;IAAE,CAAC;AAE1E,GAAC,SAAiB,OAAO;AACzB,SAAMA,UAAG,yBAAyB,EAAE;AACpC,sBAAO,WAAW,CAAC,aAAa,EAAE;YAC1B;AACR,YAAS,MAAM;AACf,WAAQ,aAAa;AACrB,gBAAa,WAAW,WAAW;AACnC,aAAG,eAAe;;GAEpB;EACF"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"//": "THIS FILE IS AUTO-GENERATED FROM TEMPLATE. DO NOT EDIT IT DIRECTLY UNLESS YOU ALSO EDIT THE CORRESPONDING FILE IN packages/template (FOR package.json FILES, PLEASE EDIT package-template.json)",
|
|
3
3
|
"name": "@hexclave/next",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.21",
|
|
5
5
|
"repository": "https://github.com/hexclave/hexclave",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"main": "./dist/index.js",
|
|
@@ -75,9 +75,9 @@
|
|
|
75
75
|
"rrweb": "^1.1.3",
|
|
76
76
|
"tsx": "^4.21.0",
|
|
77
77
|
"yup": "^1.7.1",
|
|
78
|
-
"@hexclave/sc": "1.0.
|
|
79
|
-
"@hexclave/
|
|
80
|
-
"@hexclave/
|
|
78
|
+
"@hexclave/sc": "1.0.21",
|
|
79
|
+
"@hexclave/ui": "1.0.21",
|
|
80
|
+
"@hexclave/shared": "1.0.21"
|
|
81
81
|
},
|
|
82
82
|
"peerDependencies": {
|
|
83
83
|
"@types/react": ">=18.0.0",
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
//===========================================
|
|
5
5
|
// @vitest-environment jsdom
|
|
6
6
|
|
|
7
|
+
import { KnownErrors } from "@hexclave/shared/dist/known-errors";
|
|
7
8
|
import { Result } from "@hexclave/shared/dist/utils/results";
|
|
8
9
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
9
10
|
import { EventTracker } from "./event-tracker";
|
|
@@ -390,7 +391,7 @@ describe("EventTracker", () => {
|
|
|
390
391
|
}
|
|
391
392
|
});
|
|
392
393
|
|
|
393
|
-
it("silently disables when
|
|
394
|
+
it("silently disables when client interface returns ANALYTICS_NOT_ENABLED as an error", async () => {
|
|
394
395
|
vi.useFakeTimers();
|
|
395
396
|
document.body.innerHTML = "<button>Click me</button>";
|
|
396
397
|
|
|
@@ -400,28 +401,19 @@ describe("EventTracker", () => {
|
|
|
400
401
|
projectId: "internal",
|
|
401
402
|
sendBatch: async (body) => {
|
|
402
403
|
sentBodies.push(body);
|
|
403
|
-
return Result.
|
|
404
|
-
JSON.stringify({ code: "ANALYTICS_NOT_ENABLED", error: "Analytics is not enabled for this project." }),
|
|
405
|
-
{
|
|
406
|
-
status: 400,
|
|
407
|
-
headers: { "x-stack-known-error": "ANALYTICS_NOT_ENABLED" },
|
|
408
|
-
},
|
|
409
|
-
));
|
|
404
|
+
return Result.error(new KnownErrors.AnalyticsNotEnabled());
|
|
410
405
|
},
|
|
411
406
|
});
|
|
412
407
|
|
|
413
408
|
try {
|
|
414
409
|
tracker.start();
|
|
415
410
|
|
|
416
|
-
// First flush sends the initial page-view event; server rejects it.
|
|
417
411
|
await advancePastFlush();
|
|
418
412
|
expect(sentBodies).toHaveLength(1);
|
|
419
|
-
|
|
420
|
-
// No console.warn should have been emitted.
|
|
421
413
|
expect(warnSpy).not.toHaveBeenCalled();
|
|
414
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
415
|
+
expect((tracker as any)._flushTimer).toBeNull();
|
|
422
416
|
|
|
423
|
-
// After disabling, new events should not accumulate or trigger further
|
|
424
|
-
// flushes.
|
|
425
417
|
document.querySelector("button")?.dispatchEvent(new MouseEvent("click", { bubbles: true }));
|
|
426
418
|
await advancePastFlush();
|
|
427
419
|
expect(sentBodies).toHaveLength(1);
|
|
@@ -8,7 +8,7 @@ import { cssEscapeIdent } from "@hexclave/shared/dist/utils/dom";
|
|
|
8
8
|
import { buildElementsChain, ELEMENTS_CHAIN_MAX_DEPTH } from "@hexclave/shared/dist/utils/elements-chain";
|
|
9
9
|
import { runAsynchronously } from "@hexclave/shared/dist/utils/promises";
|
|
10
10
|
import { Result } from "@hexclave/shared/dist/utils/results";
|
|
11
|
-
import { generateUuid } from "./session-replay";
|
|
11
|
+
import { generateUuid, isAnalyticsNotEnabledError } from "./session-replay";
|
|
12
12
|
|
|
13
13
|
const FLUSH_INTERVAL_MS = 10_000;
|
|
14
14
|
const MAX_EVENTS_PER_BATCH = 50;
|
|
@@ -34,6 +34,10 @@ function hasHistoryMethods(value: unknown): value is { pushState: History["pushS
|
|
|
34
34
|
return typeof value.pushState === "function" && typeof value.replaceState === "function";
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
function getTextSnippet(textContent: string | null): string {
|
|
38
|
+
return textContent == null ? "" : textContent.trim().substring(0, 200);
|
|
39
|
+
}
|
|
40
|
+
|
|
37
41
|
// Pixel quantization factor for x/y/viewport in stored click events. Matches the
|
|
38
42
|
// SCALE_FACTOR used by the ClickHouse clickmap_events MV — keep them in sync.
|
|
39
43
|
const CLICKMAP_SCALE_FACTOR = 16;
|
|
@@ -321,7 +325,7 @@ export class EventTracker {
|
|
|
321
325
|
event_at_ms: Date.now(),
|
|
322
326
|
data: {
|
|
323
327
|
tag_name: target.tagName.toLowerCase(),
|
|
324
|
-
text: target.textContent
|
|
328
|
+
text: getTextSnippet(target.textContent),
|
|
325
329
|
href: this._findNearestAnchorHref(target),
|
|
326
330
|
selector: this._buildSelector(target),
|
|
327
331
|
elements_chain: buildElementsChain(target),
|
|
@@ -503,27 +507,28 @@ export class EventTracker {
|
|
|
503
507
|
);
|
|
504
508
|
|
|
505
509
|
if (res.status === "error") {
|
|
510
|
+
if (isAnalyticsNotEnabledError(res.error)) {
|
|
511
|
+
this._disable();
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
506
514
|
console.warn("EventTracker flush failed:", res.error);
|
|
507
515
|
return;
|
|
508
516
|
}
|
|
509
517
|
|
|
510
518
|
if (!res.data.ok) {
|
|
511
|
-
// If the server tells us analytics is not enabled for this project,
|
|
512
|
-
// silently disable the tracker — no point retrying or warning the user.
|
|
513
|
-
const knownError = res.data.headers.get("x-hexclave-known-error") ?? res.data.headers.get("x-stack-known-error");
|
|
514
|
-
if (knownError === "ANALYTICS_NOT_ENABLED") {
|
|
515
|
-
this._disabled = true;
|
|
516
|
-
if (this._flushTimer !== null) {
|
|
517
|
-
clearInterval(this._flushTimer);
|
|
518
|
-
this._flushTimer = null;
|
|
519
|
-
}
|
|
520
|
-
this._teardown();
|
|
521
|
-
return;
|
|
522
|
-
}
|
|
523
519
|
console.warn("EventTracker flush failed:", res.data.status, await res.data.text());
|
|
524
520
|
}
|
|
525
521
|
}
|
|
526
522
|
|
|
523
|
+
private _disable() {
|
|
524
|
+
this._disabled = true;
|
|
525
|
+
if (this._flushTimer !== null) {
|
|
526
|
+
clearInterval(this._flushTimer);
|
|
527
|
+
this._flushTimer = null;
|
|
528
|
+
}
|
|
529
|
+
this._teardown();
|
|
530
|
+
}
|
|
531
|
+
|
|
527
532
|
private _tick() {
|
|
528
533
|
if (this._cancelled) return;
|
|
529
534
|
if (this._events.length > 0) {
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
//===========================================
|
|
5
5
|
// @vitest-environment jsdom
|
|
6
6
|
|
|
7
|
+
import { KnownErrors } from "@hexclave/shared/dist/known-errors";
|
|
7
8
|
import { describe, expect, it, vi } from "vitest";
|
|
8
9
|
import { Result } from "@hexclave/shared/dist/utils/results";
|
|
9
10
|
import { analyticsOptionsFromJson, analyticsOptionsToJson, getSessionReplayOptions, SessionRecorder } from "./session-replay";
|
|
@@ -48,10 +49,9 @@ describe("analytics option JSON conversion", () => {
|
|
|
48
49
|
});
|
|
49
50
|
|
|
50
51
|
describe("SessionRecorder flush", () => {
|
|
51
|
-
it("silently disables when
|
|
52
|
+
it("silently disables when client interface returns ANALYTICS_NOT_ENABLED as an error", async () => {
|
|
52
53
|
vi.useFakeTimers();
|
|
53
54
|
|
|
54
|
-
// Seed localStorage with a valid session so _flush doesn't fail on getOrRotateSession
|
|
55
55
|
const storageKey = `hexclave:session-replay:v1:test-project`;
|
|
56
56
|
localStorage.setItem(storageKey, JSON.stringify({
|
|
57
57
|
session_id: "test-session",
|
|
@@ -65,13 +65,7 @@ describe("SessionRecorder flush", () => {
|
|
|
65
65
|
projectId: "test-project",
|
|
66
66
|
sendBatch: async (body) => {
|
|
67
67
|
sentBodies.push(body);
|
|
68
|
-
return Result.
|
|
69
|
-
JSON.stringify({ code: "ANALYTICS_NOT_ENABLED", error: "Analytics is not enabled for this project." }),
|
|
70
|
-
{
|
|
71
|
-
status: 400,
|
|
72
|
-
headers: { "x-stack-known-error": "ANALYTICS_NOT_ENABLED" },
|
|
73
|
-
},
|
|
74
|
-
));
|
|
68
|
+
return Result.error(new KnownErrors.AnalyticsNotEnabled());
|
|
75
69
|
},
|
|
76
70
|
},
|
|
77
71
|
{},
|
|
@@ -80,26 +74,16 @@ describe("SessionRecorder flush", () => {
|
|
|
80
74
|
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
81
75
|
|
|
82
76
|
try {
|
|
83
|
-
// Inject an event directly into the recorder's buffer to test flush behavior
|
|
84
|
-
// without needing rrweb. We access private fields for testing purposes.
|
|
85
77
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
86
78
|
(recorder as any)._events = [{ type: 2, timestamp: Date.now(), data: {} }];
|
|
87
79
|
|
|
88
|
-
// Manually trigger a tick (which calls _flush)
|
|
89
80
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
|
|
90
81
|
(recorder as any)._tick();
|
|
91
82
|
await vi.advanceTimersByTimeAsync(0);
|
|
92
83
|
|
|
93
|
-
// One batch should have been sent
|
|
94
84
|
expect(sentBodies).toHaveLength(1);
|
|
85
|
+
expect(warnSpy).not.toHaveBeenCalled();
|
|
95
86
|
|
|
96
|
-
// No console.warn about "SessionRecorder flush failed" should have been emitted
|
|
97
|
-
const flushWarnings = warnSpy.mock.calls.filter(
|
|
98
|
-
(args) => typeof args[0] === "string" && args[0].includes("SessionRecorder")
|
|
99
|
-
);
|
|
100
|
-
expect(flushWarnings).toHaveLength(0);
|
|
101
|
-
|
|
102
|
-
// After disabling, pushing new events and triggering another tick should not send
|
|
103
87
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
104
88
|
(recorder as any)._events = [{ type: 3, timestamp: Date.now(), data: {} }];
|
|
105
89
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
//===========================================
|
|
3
3
|
// THIS FILE IS AUTO-GENERATED FROM TEMPLATE. DO NOT EDIT IT DIRECTLY UNLESS YOU ALSO EDIT THE CORRESPONDING FILE IN packages/template
|
|
4
4
|
//===========================================
|
|
5
|
+
import { KnownErrors } from "@hexclave/shared/dist/known-errors";
|
|
5
6
|
import { isBrowserLike } from "@hexclave/shared/dist/utils/env";
|
|
6
7
|
import { captureWarning } from "@hexclave/shared/dist/utils/errors";
|
|
7
8
|
import { runAsynchronously } from "@hexclave/shared/dist/utils/promises";
|
|
@@ -165,6 +166,10 @@ export type SessionRecorderDeps = {
|
|
|
165
166
|
sendBatch: (body: string, options: { keepalive: boolean }) => Promise<Result<Response, Error>>,
|
|
166
167
|
};
|
|
167
168
|
|
|
169
|
+
export function isAnalyticsNotEnabledError(error: unknown): boolean {
|
|
170
|
+
return KnownErrors.AnalyticsNotEnabled.isInstance(error);
|
|
171
|
+
}
|
|
172
|
+
|
|
168
173
|
export class SessionRecorder {
|
|
169
174
|
private _started = false;
|
|
170
175
|
private _cancelled = false;
|
|
@@ -269,23 +274,15 @@ export class SessionRecorder {
|
|
|
269
274
|
);
|
|
270
275
|
|
|
271
276
|
if (res.status === "error") {
|
|
277
|
+
if (isAnalyticsNotEnabledError(res.error)) {
|
|
278
|
+
this._disable();
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
272
281
|
captureWarning("SessionRecorder.flush", res.error);
|
|
273
282
|
return;
|
|
274
283
|
}
|
|
275
284
|
|
|
276
285
|
if (!res.data.ok) {
|
|
277
|
-
// If the server tells us analytics is not enabled for this project,
|
|
278
|
-
// silently disable the recorder — no point retrying or warning the user.
|
|
279
|
-
const knownError = res.data.headers.get("x-hexclave-known-error") ?? res.data.headers.get("x-stack-known-error");
|
|
280
|
-
if (knownError === "ANALYTICS_NOT_ENABLED") {
|
|
281
|
-
this._disabled = true;
|
|
282
|
-
if (this._flushTimer !== null) {
|
|
283
|
-
clearInterval(this._flushTimer);
|
|
284
|
-
this._flushTimer = null;
|
|
285
|
-
}
|
|
286
|
-
this._stopCurrentRecording();
|
|
287
|
-
return;
|
|
288
|
-
}
|
|
289
286
|
captureWarning("SessionRecorder.flush", new Error(`SessionRecorder flush failed: ${res.data.status} ${await res.data.text()}`));
|
|
290
287
|
}
|
|
291
288
|
} finally {
|
|
@@ -293,6 +290,16 @@ export class SessionRecorder {
|
|
|
293
290
|
}
|
|
294
291
|
}
|
|
295
292
|
|
|
293
|
+
private _disable() {
|
|
294
|
+
this._disabled = true;
|
|
295
|
+
this.clearBuffer();
|
|
296
|
+
if (this._flushTimer !== null) {
|
|
297
|
+
clearInterval(this._flushTimer);
|
|
298
|
+
this._flushTimer = null;
|
|
299
|
+
}
|
|
300
|
+
this._stopCurrentRecording();
|
|
301
|
+
}
|
|
302
|
+
|
|
296
303
|
private async _startRecording() {
|
|
297
304
|
if (this._recording || this._cancelled) return;
|
|
298
305
|
|