@hexclave/tanstack-start 1.0.26 → 1.0.27

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.
Files changed (28) hide show
  1. package/dist/components/api-key-dialogs.d.ts +1 -1
  2. package/dist/components/elements/ssr-layout-effect.d.ts.map +1 -1
  3. package/dist/components/elements/ssr-layout-effect.js +5 -3
  4. package/dist/components/elements/ssr-layout-effect.js.map +1 -1
  5. package/dist/esm/components/api-key-dialogs.d.ts +1 -1
  6. package/dist/esm/components/elements/ssr-layout-effect.d.ts.map +1 -1
  7. package/dist/esm/components/elements/ssr-layout-effect.js +5 -3
  8. package/dist/esm/components/elements/ssr-layout-effect.js.map +1 -1
  9. package/dist/esm/generated/quetzal-translations.d.ts +2 -2
  10. package/dist/esm/lib/hexclave-app/apps/implementations/common.js +1 -1
  11. package/dist/esm/lib/hexclave-app/apps/implementations/session-replay.d.ts +1 -0
  12. package/dist/esm/lib/hexclave-app/apps/implementations/session-replay.d.ts.map +1 -1
  13. package/dist/esm/lib/hexclave-app/apps/implementations/session-replay.js +44 -19
  14. package/dist/esm/lib/hexclave-app/apps/implementations/session-replay.js.map +1 -1
  15. package/dist/esm/lib/hexclave-app/apps/implementations/session-replay.test.js +97 -8
  16. package/dist/esm/lib/hexclave-app/apps/implementations/session-replay.test.js.map +1 -1
  17. package/dist/generated/quetzal-translations.d.ts +2 -2
  18. package/dist/lib/hexclave-app/apps/implementations/common.js +1 -1
  19. package/dist/lib/hexclave-app/apps/implementations/session-replay.d.ts +1 -0
  20. package/dist/lib/hexclave-app/apps/implementations/session-replay.d.ts.map +1 -1
  21. package/dist/lib/hexclave-app/apps/implementations/session-replay.js +43 -18
  22. package/dist/lib/hexclave-app/apps/implementations/session-replay.js.map +1 -1
  23. package/dist/lib/hexclave-app/apps/implementations/session-replay.test.js +97 -8
  24. package/dist/lib/hexclave-app/apps/implementations/session-replay.test.js.map +1 -1
  25. package/package.json +3 -3
  26. package/src/components/elements/ssr-layout-effect.tsx +15 -3
  27. package/src/lib/hexclave-app/apps/implementations/session-replay.test.ts +123 -4
  28. package/src/lib/hexclave-app/apps/implementations/session-replay.ts +63 -29
@@ -74,8 +74,11 @@ describe("SessionRecorder flush", () => {
74
74
  const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
75
75
 
76
76
  try {
77
+ const event1 = { type: 2, timestamp: Date.now(), data: {} };
77
78
  // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
78
- (recorder as any)._events = [{ type: 2, timestamp: Date.now(), data: {} }];
79
+ (recorder as any)._events = [event1];
80
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
81
+ (recorder as any)._eventSizes = [JSON.stringify(event1).length];
79
82
 
80
83
  // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
81
84
  (recorder as any)._tick();
@@ -86,8 +89,11 @@ describe("SessionRecorder flush", () => {
86
89
 
87
90
  // Unlike ANALYTICS_NOT_ENABLED, ad blocker errors do NOT disable the
88
91
  // recorder — subsequent flushes continue attempting delivery.
92
+ const event2 = { type: 3, timestamp: Date.now(), data: {} };
93
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
94
+ (recorder as any)._events = [event2];
89
95
  // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
90
- (recorder as any)._events = [{ type: 3, timestamp: Date.now(), data: {} }];
96
+ (recorder as any)._eventSizes = [JSON.stringify(event2).length];
91
97
  // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
92
98
  (recorder as any)._tick();
93
99
  await vi.advanceTimersByTimeAsync(0);
@@ -101,6 +107,113 @@ describe("SessionRecorder flush", () => {
101
107
  }
102
108
  });
103
109
 
110
+ it("splits large batches into multiple requests to stay under server 1MB limit", async () => {
111
+ vi.useFakeTimers();
112
+
113
+ const storageKey = `hexclave:session-replay:v1:test-project`;
114
+ localStorage.setItem(storageKey, JSON.stringify({
115
+ session_id: "test-session",
116
+ created_at_ms: Date.now(),
117
+ last_activity_ms: Date.now(),
118
+ }));
119
+
120
+ const sentBodies: string[] = [];
121
+ const recorder = new SessionRecorder(
122
+ {
123
+ projectId: "test-project",
124
+ sendBatch: async (body) => {
125
+ sentBodies.push(body);
126
+ return Result.ok(new Response("ok", { status: 200 }));
127
+ },
128
+ },
129
+ {},
130
+ );
131
+
132
+ try {
133
+ // Create events that together exceed 900KB (the per-batch cap).
134
+ // Each event is ~500KB, so two events (~1MB) must be split into two batches.
135
+ const largeData = "x".repeat(500_000);
136
+ const event1 = { type: 2, timestamp: Date.now(), data: largeData };
137
+ const event2 = { type: 3, timestamp: Date.now(), data: largeData };
138
+
139
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
140
+ (recorder as any)._events = [event1, event2];
141
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
142
+ (recorder as any)._eventSizes = [JSON.stringify(event1).length, JSON.stringify(event2).length];
143
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
144
+ (recorder as any)._approxBytes = JSON.stringify(event1).length + JSON.stringify(event2).length;
145
+
146
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
147
+ (recorder as any)._tick();
148
+ await vi.advanceTimersByTimeAsync(0);
149
+
150
+ // Should have sent two separate batches
151
+ expect(sentBodies).toHaveLength(2);
152
+
153
+ // Each batch should contain exactly one event
154
+ const batch1 = JSON.parse(sentBodies[0]);
155
+ const batch2 = JSON.parse(sentBodies[1]);
156
+ expect(batch1.events).toHaveLength(1);
157
+ expect(batch2.events).toHaveLength(1);
158
+
159
+ // They should have different batch IDs
160
+ expect(batch1.batch_id).not.toBe(batch2.batch_id);
161
+ } finally {
162
+ recorder.stop();
163
+ localStorage.removeItem(storageKey);
164
+ vi.useRealTimers();
165
+ }
166
+ });
167
+
168
+ it("sends a single oversized event alone without dropping it", async () => {
169
+ vi.useFakeTimers();
170
+
171
+ const storageKey = `hexclave:session-replay:v1:test-project`;
172
+ localStorage.setItem(storageKey, JSON.stringify({
173
+ session_id: "test-session",
174
+ created_at_ms: Date.now(),
175
+ last_activity_ms: Date.now(),
176
+ }));
177
+
178
+ const sentBodies: string[] = [];
179
+ const recorder = new SessionRecorder(
180
+ {
181
+ projectId: "test-project",
182
+ sendBatch: async (body) => {
183
+ sentBodies.push(body);
184
+ return Result.ok(new Response("ok", { status: 200 }));
185
+ },
186
+ },
187
+ {},
188
+ );
189
+
190
+ try {
191
+ // A single event larger than 900KB — should still be sent (not dropped)
192
+ const hugeData = "y".repeat(1_000_000);
193
+ const hugeEvent = { type: 2, timestamp: Date.now(), data: hugeData };
194
+
195
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
196
+ (recorder as any)._events = [hugeEvent];
197
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
198
+ (recorder as any)._eventSizes = [JSON.stringify(hugeEvent).length];
199
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
200
+ (recorder as any)._approxBytes = JSON.stringify(hugeEvent).length;
201
+
202
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
203
+ (recorder as any)._tick();
204
+ await vi.advanceTimersByTimeAsync(0);
205
+
206
+ // Should still send the event (the server may reject it, but we don't drop it client-side)
207
+ expect(sentBodies).toHaveLength(1);
208
+ const batch = JSON.parse(sentBodies[0]);
209
+ expect(batch.events).toHaveLength(1);
210
+ } finally {
211
+ recorder.stop();
212
+ localStorage.removeItem(storageKey);
213
+ vi.useRealTimers();
214
+ }
215
+ });
216
+
104
217
  it("silently disables when client interface returns ANALYTICS_NOT_ENABLED as an error", async () => {
105
218
  vi.useFakeTimers();
106
219
 
@@ -126,8 +239,11 @@ describe("SessionRecorder flush", () => {
126
239
  const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
127
240
 
128
241
  try {
242
+ const event1 = { type: 2, timestamp: Date.now(), data: {} };
129
243
  // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
130
- (recorder as any)._events = [{ type: 2, timestamp: Date.now(), data: {} }];
244
+ (recorder as any)._events = [event1];
245
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
246
+ (recorder as any)._eventSizes = [JSON.stringify(event1).length];
131
247
 
132
248
  // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
133
249
  (recorder as any)._tick();
@@ -136,8 +252,11 @@ describe("SessionRecorder flush", () => {
136
252
  expect(sentBodies).toHaveLength(1);
137
253
  expect(warnSpy).not.toHaveBeenCalled();
138
254
 
255
+ const event2 = { type: 3, timestamp: Date.now(), data: {} };
256
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
257
+ (recorder as any)._events = [event2];
139
258
  // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
140
- (recorder as any)._events = [{ type: 3, timestamp: Date.now(), data: {} }];
259
+ (recorder as any)._eventSizes = [JSON.stringify(event2).length];
141
260
  // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
142
261
  (recorder as any)._tick();
143
262
  await vi.advanceTimersByTimeAsync(0);
@@ -4,7 +4,7 @@
4
4
  //===========================================
5
5
  import { KnownErrors } from "@hexclave/shared/dist/known-errors";
6
6
  import { isBrowserLike } from "@hexclave/shared/dist/utils/env";
7
- import { captureWarning } from "@hexclave/shared/dist/utils/errors";
7
+ import { captureWarning, throwErr } from "@hexclave/shared/dist/utils/errors";
8
8
  import { runAsynchronously } from "@hexclave/shared/dist/utils/promises";
9
9
  import { Result } from "@hexclave/shared/dist/utils/results";
10
10
 
@@ -110,6 +110,9 @@ const IDLE_TTL_MS = 3 * 60 * 1000;
110
110
  const FLUSH_INTERVAL_MS = 5_000;
111
111
  const MAX_EVENTS_PER_BATCH = 200;
112
112
  const MAX_APPROX_BYTES_PER_BATCH = 512_000;
113
+ // The server rejects payloads > 1MB. Stay well under to account for JSON
114
+ // envelope overhead (browser_session_id, timestamps, wrapper keys, etc.).
115
+ const MAX_FLUSH_PAYLOAD_BYTES = 900_000;
113
116
 
114
117
  export type StoredSession = {
115
118
  session_id: string,
@@ -193,6 +196,7 @@ export class SessionRecorder {
193
196
  private _detachListeners: (() => void) | null = null;
194
197
  private _flushTimer: ReturnType<typeof setInterval> | null = null;
195
198
  private _events: unknown[] = [];
199
+ private _eventSizes: number[] = [];
196
200
  private _approxBytes = 0;
197
201
  private _lastPersistActivity = 0;
198
202
  private _recording = false;
@@ -243,6 +247,7 @@ export class SessionRecorder {
243
247
 
244
248
  clearBuffer() {
245
249
  this._events = [];
250
+ this._eventSizes = [];
246
251
  this._approxBytes = 0;
247
252
  }
248
253
 
@@ -268,42 +273,68 @@ export class SessionRecorder {
268
273
  const nowMs = Date.now();
269
274
  const stored = getOrRotateSession({ key: this._storageKey, legacyKey: this._legacyStorageKey, nowMs });
270
275
 
271
- const batchId = generateUuid();
272
- const payload = {
273
- browser_session_id: stored.session_id,
274
- session_replay_segment_id: this._sessionReplaySegmentId,
275
- batch_id: batchId,
276
- started_at_ms: stored.created_at_ms,
277
- sent_at_ms: nowMs,
278
- events: this._events,
279
- };
280
-
276
+ // Capture all buffered events upfront (before any await) so that
277
+ // stop() / _stopCurrentRecording() clearing this._events cannot race
278
+ // with the async send loop below and silently discard overflow batches.
279
+ const allEvents = this._events;
280
+ const allSizes = this._eventSizes;
281
281
  this._events = [];
282
+ this._eventSizes = [];
282
283
  this._approxBytes = 0;
283
284
 
284
285
  this._flushInProgress = true;
285
286
  try {
286
- const res = await this._deps.sendBatch(
287
- JSON.stringify(payload),
288
- { keepalive: options.keepalive },
289
- );
290
-
291
- if (res.status === "error") {
292
- if (isAnalyticsNotEnabledError(res.error)) {
293
- this._disable();
294
- return;
287
+ let offset = 0;
288
+ while (offset < allEvents.length) {
289
+ // Build a batch that fits under the server's payload limit.
290
+ // When _flushInProgress blocked earlier flushes, events can accumulate
291
+ // well past MAX_APPROX_BYTES_PER_BATCH; sending them all at once would
292
+ // exceed the server's 1MB body limit (413).
293
+ let batchBytes = 0;
294
+ let batchEnd = offset;
295
+ for (let i = offset; i < allEvents.length; i++) {
296
+ const nextSize = allSizes[i] ?? throwErr("_eventSizes out of sync with _events — this should never happen");
297
+ if (batchBytes + nextSize > MAX_FLUSH_PAYLOAD_BYTES && batchEnd > offset) break;
298
+ batchBytes += nextSize;
299
+ batchEnd = i + 1;
295
300
  }
296
- // Ad blockers commonly block analytics endpoints, causing network
297
- // errors. These are expected and should not pollute the console.
298
- if (isAdBlockerNetworkError(res.error)) {
301
+
302
+ const batchEvents = allEvents.slice(offset, batchEnd);
303
+ offset = batchEnd;
304
+
305
+ const batchId = generateUuid();
306
+ const payload = {
307
+ browser_session_id: stored.session_id,
308
+ session_replay_segment_id: this._sessionReplaySegmentId,
309
+ batch_id: batchId,
310
+ started_at_ms: stored.created_at_ms,
311
+ sent_at_ms: nowMs,
312
+ events: batchEvents,
313
+ };
314
+
315
+ const res = await this._deps.sendBatch(
316
+ JSON.stringify(payload),
317
+ { keepalive: options.keepalive },
318
+ );
319
+
320
+ if (res.status === "error") {
321
+ if (isAnalyticsNotEnabledError(res.error)) {
322
+ this._disable();
323
+ return;
324
+ }
325
+ // Ad blockers commonly block analytics endpoints, causing network
326
+ // errors. These are expected and should not pollute the console.
327
+ if (isAdBlockerNetworkError(res.error)) {
328
+ return;
329
+ }
330
+ captureWarning("SessionRecorder.flush", res.error);
299
331
  return;
300
332
  }
301
- captureWarning("SessionRecorder.flush", res.error);
302
- return;
303
- }
304
333
 
305
- if (!res.data.ok) {
306
- captureWarning("SessionRecorder.flush", new Error(`SessionRecorder flush failed: ${res.data.status} ${await res.data.text()}`));
334
+ if (!res.data.ok) {
335
+ captureWarning("SessionRecorder.flush", new Error(`SessionRecorder flush failed: ${res.data.status} ${await res.data.text()}`));
336
+ return;
337
+ }
307
338
  }
308
339
  } finally {
309
340
  this._flushInProgress = false;
@@ -357,8 +388,10 @@ export class SessionRecorder {
357
388
  }
358
389
  }
359
390
 
391
+ const eventSize = JSON.stringify(event).length;
360
392
  this._events.push(event);
361
- this._approxBytes += JSON.stringify(event).length;
393
+ this._eventSizes.push(eventSize);
394
+ this._approxBytes += eventSize;
362
395
  if (this._events.length >= MAX_EVENTS_PER_BATCH || this._approxBytes >= MAX_APPROX_BYTES_PER_BATCH) {
363
396
  runAsynchronously(() => this._flush({ keepalive: false }));
364
397
  }
@@ -391,6 +424,7 @@ export class SessionRecorder {
391
424
  this._stopRecording = null;
392
425
  }
393
426
  this._events = [];
427
+ this._eventSizes = [];
394
428
  this._approxBytes = 0;
395
429
  this._recording = false;
396
430
  }