@tracewayapp/frontend 0.2.0 → 1.0.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 CHANGED
@@ -1,24 +1,37 @@
1
1
  # @tracewayapp/frontend
2
2
 
3
- Traceway SDK for browser environments. Reports exceptions and messages only (no traces or metrics).
3
+ Traceway SDK for browser environments. Captures exceptions and messages with automatic batching and retry.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @tracewayapp/frontend
9
+ ```
4
10
 
5
11
  ## Quick Start
6
12
 
7
- ```ts
8
- import * as traceway from "@tracewayapp/frontend";
13
+ ```typescript
14
+ import { init, captureException, captureMessage, flush } from "@tracewayapp/frontend";
9
15
 
10
- traceway.init("your-token@https://your-traceway-server.com/api/report");
16
+ // Initialize once at app startup
17
+ init("your-token@https://traceway.example.com/api/report");
11
18
 
12
- // Capture an error
13
- traceway.captureException(new Error("something broke"));
19
+ // Capture errors
20
+ try {
21
+ riskyOperation();
22
+ } catch (error) {
23
+ captureException(error);
24
+ }
14
25
 
15
- // Capture a message
16
- traceway.captureMessage("User completed onboarding");
26
+ // Capture custom messages
27
+ captureMessage("User completed checkout");
17
28
 
18
- // Flush pending exceptions immediately
19
- await traceway.flush();
29
+ // Force-send all pending exceptions
30
+ await flush();
20
31
  ```
21
32
 
33
+ `init()` automatically installs `window.onerror` and `window.onunhandledrejection` handlers to capture uncaught errors.
34
+
22
35
  ## API
23
36
 
24
37
  | Function | Description |
@@ -29,7 +42,7 @@ await traceway.flush();
29
42
  | `captureMessage(msg)` | Capture an informational message |
30
43
  | `flush()` | Force-send all pending exceptions immediately |
31
44
 
32
- ## Options
45
+ ## Configuration Options
33
46
 
34
47
  | Option | Type | Default | Description |
35
48
  |--------|------|---------|-------------|
@@ -37,53 +50,7 @@ await traceway.flush();
37
50
  | `debounceMs` | `number` | `1500` | Debounce interval before sending |
38
51
  | `version` | `string` | `""` | Application version |
39
52
 
40
- ## Sync Queue Behavior
41
-
42
- ```
43
- captureException()
44
- |
45
- v
46
- pendingExceptions[] --> scheduleSync() (1.5s debounce)
47
- |
48
- v (debounce fires)
49
- doSync()
50
- |
51
- +---------+---------+
52
- | |
53
- isSyncing? send batch
54
- | |
55
- return +-----+-----+
56
- success failure
57
- | |
58
- done re-queue batch
59
- |
60
- if pending > 0
61
- |
62
- doSync() again
63
- ```
64
-
65
- - Only one sync at a time (no concurrent fetches)
66
- - New exceptions during an in-flight request are queued for the next batch
67
- - On failure, the batch is re-queued at the front of the pending list
68
- - On success after re-queue, any new pending items trigger an immediate sync
69
-
70
- ## Global Error Handlers
71
-
72
- `init()` automatically installs `window.onerror` and `window.onunhandledrejection` handlers. Previous handlers are chained and called after Traceway captures the error.
73
-
74
- ## Browser Requirements
53
+ ## Requirements
75
54
 
76
55
  - `fetch` API
77
- - `CompressionStream` API (gzip) — available in Chrome 80+, Firefox 113+, Safari 16.4+
78
-
79
- ## Stack Trace Format
80
-
81
- Handles both V8 format (`at func (file:line:col)`) and Firefox format (`func@file:line:col`), producing Go-like output:
82
-
83
- ```
84
- TypeError: Cannot read properties of null
85
- handleClick()
86
- app.js:42
87
- render()
88
- component.js:15
89
- ```
56
+ - `CompressionStream` API (gzip) — Chrome 80+, Firefox 113+, Safari 16.4+
package/dist/index.d.mts CHANGED
@@ -6,6 +6,8 @@ interface TracewayFrontendOptions {
6
6
  debounceMs?: number;
7
7
  retryDelayMs?: number;
8
8
  version?: string;
9
+ sessionRecording?: boolean;
10
+ sessionRecordingSegmentDuration?: number;
9
11
  }
10
12
  declare class TracewayFrontendClient {
11
13
  private apiUrl;
@@ -15,15 +17,17 @@ declare class TracewayFrontendClient {
15
17
  private retryDelayMs;
16
18
  private version;
17
19
  private pendingExceptions;
20
+ private pendingRecordings;
18
21
  private isSyncing;
19
22
  private debounceTimer;
20
23
  private retryTimer;
24
+ private recorder;
21
25
  constructor(connectionString: string, options?: TracewayFrontendOptions);
22
26
  addException(exception: ExceptionStackTrace): void;
23
27
  private scheduleSync;
24
28
  private doSync;
25
29
  private scheduleRetry;
26
- flush(): Promise<void>;
30
+ flush(timeoutMs?: number): Promise<void>;
27
31
  }
28
32
 
29
33
  declare function formatBrowserStackTrace(error: Error): string;
@@ -34,6 +38,6 @@ declare function init(connectionString: string, options?: TracewayFrontendOption
34
38
  declare function captureException(error: Error): void;
35
39
  declare function captureExceptionWithAttributes(error: Error, attributes?: Record<string, string>): void;
36
40
  declare function captureMessage(msg: string): void;
37
- declare function flush(): Promise<void>;
41
+ declare function flush(timeoutMs?: number): Promise<void>;
38
42
 
39
43
  export { TracewayFrontendClient, type TracewayFrontendOptions, captureException, captureExceptionWithAttributes, captureMessage, flush, formatBrowserStackTrace, init, installGlobalHandlers };
package/dist/index.d.ts CHANGED
@@ -6,6 +6,8 @@ interface TracewayFrontendOptions {
6
6
  debounceMs?: number;
7
7
  retryDelayMs?: number;
8
8
  version?: string;
9
+ sessionRecording?: boolean;
10
+ sessionRecordingSegmentDuration?: number;
9
11
  }
10
12
  declare class TracewayFrontendClient {
11
13
  private apiUrl;
@@ -15,15 +17,17 @@ declare class TracewayFrontendClient {
15
17
  private retryDelayMs;
16
18
  private version;
17
19
  private pendingExceptions;
20
+ private pendingRecordings;
18
21
  private isSyncing;
19
22
  private debounceTimer;
20
23
  private retryTimer;
24
+ private recorder;
21
25
  constructor(connectionString: string, options?: TracewayFrontendOptions);
22
26
  addException(exception: ExceptionStackTrace): void;
23
27
  private scheduleSync;
24
28
  private doSync;
25
29
  private scheduleRetry;
26
- flush(): Promise<void>;
30
+ flush(timeoutMs?: number): Promise<void>;
27
31
  }
28
32
 
29
33
  declare function formatBrowserStackTrace(error: Error): string;
@@ -34,6 +38,6 @@ declare function init(connectionString: string, options?: TracewayFrontendOption
34
38
  declare function captureException(error: Error): void;
35
39
  declare function captureExceptionWithAttributes(error: Error, attributes?: Record<string, string>): void;
36
40
  declare function captureMessage(msg: string): void;
37
- declare function flush(): Promise<void>;
41
+ declare function flush(timeoutMs?: number): Promise<void>;
38
42
 
39
43
  export { TracewayFrontendClient, type TracewayFrontendOptions, captureException, captureExceptionWithAttributes, captureMessage, flush, formatBrowserStackTrace, init, installGlobalHandlers };
package/dist/index.js CHANGED
@@ -74,6 +74,74 @@ async function sendReport(apiUrl, token, body) {
74
74
  return resp.status === 200;
75
75
  }
76
76
 
77
+ // src/session-recorder.ts
78
+ var import_rrweb = require("rrweb");
79
+ var SessionRecorder = class {
80
+ segmentDuration;
81
+ current;
82
+ previous = null;
83
+ stopFn = null;
84
+ rotateInterval = null;
85
+ constructor(options = {}) {
86
+ this.segmentDuration = options.segmentDuration ?? 1e4;
87
+ this.current = { events: [], startedAt: (/* @__PURE__ */ new Date()).toISOString() };
88
+ }
89
+ start() {
90
+ this.stopFn = (0, import_rrweb.record)({
91
+ emit: (event) => {
92
+ this.onEvent(event);
93
+ }
94
+ });
95
+ this.rotateInterval = setInterval(() => {
96
+ this.rotateSegment();
97
+ }, this.segmentDuration);
98
+ }
99
+ stop() {
100
+ if (this.stopFn) {
101
+ this.stopFn();
102
+ this.stopFn = null;
103
+ }
104
+ if (this.rotateInterval) {
105
+ clearInterval(this.rotateInterval);
106
+ this.rotateInterval = null;
107
+ }
108
+ }
109
+ onEvent(event) {
110
+ this.current.events.push(event);
111
+ }
112
+ rotateSegment() {
113
+ this.previous = this.current;
114
+ this.current = { events: [], startedAt: (/* @__PURE__ */ new Date()).toISOString() };
115
+ import_rrweb.record.takeFullSnapshot();
116
+ }
117
+ getSegments() {
118
+ const segments = [];
119
+ if (this.previous && this.previous.events.length > 0) {
120
+ segments.push({
121
+ events: this.previous.events,
122
+ timestamp: this.previous.startedAt
123
+ });
124
+ }
125
+ if (this.current.events.length > 0) {
126
+ segments.push({
127
+ events: this.current.events,
128
+ timestamp: this.current.startedAt
129
+ });
130
+ }
131
+ return segments;
132
+ }
133
+ hasSegments() {
134
+ return this.current.events.length > 0 || this.previous !== null && this.previous.events.length > 0;
135
+ }
136
+ flush() {
137
+ this.previous = null;
138
+ this.current = { events: [], startedAt: (/* @__PURE__ */ new Date()).toISOString() };
139
+ if (this.stopFn) {
140
+ import_rrweb.record.takeFullSnapshot();
141
+ }
142
+ }
143
+ };
144
+
77
145
  // src/client.ts
78
146
  var TracewayFrontendClient = class {
79
147
  apiUrl;
@@ -83,9 +151,11 @@ var TracewayFrontendClient = class {
83
151
  retryDelayMs;
84
152
  version;
85
153
  pendingExceptions = [];
154
+ pendingRecordings = [];
86
155
  isSyncing = false;
87
156
  debounceTimer = null;
88
157
  retryTimer = null;
158
+ recorder = null;
89
159
  constructor(connectionString, options = {}) {
90
160
  const { token, apiUrl } = (0, import_core.parseConnectionString)(connectionString);
91
161
  this.apiUrl = apiUrl;
@@ -94,8 +164,21 @@ var TracewayFrontendClient = class {
94
164
  this.debounceMs = options.debounceMs ?? 1500;
95
165
  this.retryDelayMs = options.retryDelayMs ?? 1e4;
96
166
  this.version = options.version ?? "";
167
+ if (options.sessionRecording !== false && typeof window !== "undefined") {
168
+ this.recorder = new SessionRecorder({
169
+ segmentDuration: options.sessionRecordingSegmentDuration
170
+ });
171
+ this.recorder.start();
172
+ }
97
173
  }
98
174
  addException(exception) {
175
+ if (this.recorder && this.recorder.hasSegments()) {
176
+ const segments = this.recorder.getSegments();
177
+ const allEvents = segments.flatMap((s) => s.events);
178
+ const exceptionId = (0, import_core.generateUUID)();
179
+ exception.sessionRecordingId = exceptionId;
180
+ this.pendingRecordings.push({ exceptionId, events: allEvents });
181
+ }
99
182
  this.pendingExceptions.push(exception);
100
183
  this.scheduleSync();
101
184
  }
@@ -113,10 +196,12 @@ var TracewayFrontendClient = class {
113
196
  if (this.pendingExceptions.length === 0) return;
114
197
  this.isSyncing = true;
115
198
  const batch = this.pendingExceptions.splice(0);
199
+ const recordings = this.pendingRecordings.splice(0);
116
200
  const frame = {
117
201
  stackTraces: batch,
118
202
  metrics: [],
119
- traces: []
203
+ traces: [],
204
+ sessionRecordings: recordings.length > 0 ? recordings : void 0
120
205
  };
121
206
  const payload = {
122
207
  collectionFrames: [frame],
@@ -133,6 +218,7 @@ var TracewayFrontendClient = class {
133
218
  if (!success) {
134
219
  failed = true;
135
220
  this.pendingExceptions.unshift(...batch);
221
+ this.pendingRecordings.unshift(...recordings);
136
222
  if (this.debug) {
137
223
  console.error("Traceway: sync failed, re-queued exceptions");
138
224
  }
@@ -140,6 +226,7 @@ var TracewayFrontendClient = class {
140
226
  } catch (err) {
141
227
  failed = true;
142
228
  this.pendingExceptions.unshift(...batch);
229
+ this.pendingRecordings.unshift(...recordings);
143
230
  if (this.debug) {
144
231
  console.error("Traceway: sync error:", err);
145
232
  }
@@ -161,7 +248,7 @@ var TracewayFrontendClient = class {
161
248
  this.doSync();
162
249
  }, this.retryDelayMs);
163
250
  }
164
- async flush() {
251
+ async flush(timeoutMs) {
165
252
  if (this.debounceTimer !== null) {
166
253
  clearTimeout(this.debounceTimer);
167
254
  this.debounceTimer = null;
@@ -170,7 +257,18 @@ var TracewayFrontendClient = class {
170
257
  clearTimeout(this.retryTimer);
171
258
  this.retryTimer = null;
172
259
  }
173
- await this.doSync();
260
+ if (this.recorder) {
261
+ this.recorder.stop();
262
+ }
263
+ const syncPromise = this.doSync();
264
+ if (timeoutMs !== void 0) {
265
+ await Promise.race([
266
+ syncPromise,
267
+ new Promise((resolve) => setTimeout(resolve, timeoutMs))
268
+ ]);
269
+ } else {
270
+ await syncPromise;
271
+ }
174
272
  }
175
273
  };
176
274
 
@@ -182,34 +280,34 @@ function formatBrowserStackTrace(error) {
182
280
  if (error.stack) {
183
281
  const stackLines = error.stack.split("\n");
184
282
  for (const line of stackLines) {
185
- const v8Match = line.match(/^\s+at\s+(.+?)\s+\((.+):(\d+):\d+\)$/);
283
+ const v8Match = line.match(/^\s+at\s+(.+?)\s+\((.+):(\d+):(\d+)\)$/);
186
284
  if (v8Match) {
187
285
  const funcName = shortenFunctionName(v8Match[1]);
188
286
  const file = shortenFilePath(v8Match[2]);
189
287
  lines.push(`${funcName}()`);
190
- lines.push(` ${file}:${v8Match[3]}`);
288
+ lines.push(` ${file}:${v8Match[3]}:${v8Match[4]}`);
191
289
  continue;
192
290
  }
193
- const v8AnonMatch = line.match(/^\s+at\s+(.+):(\d+):\d+$/);
291
+ const v8AnonMatch = line.match(/^\s+at\s+(.+):(\d+):(\d+)$/);
194
292
  if (v8AnonMatch) {
195
293
  const file = shortenFilePath(v8AnonMatch[1]);
196
294
  lines.push(`<anonymous>()`);
197
- lines.push(` ${file}:${v8AnonMatch[2]}`);
295
+ lines.push(` ${file}:${v8AnonMatch[2]}:${v8AnonMatch[3]}`);
198
296
  continue;
199
297
  }
200
- const ffMatch = line.match(/^(.+)@(.+):(\d+):\d+$/);
298
+ const ffMatch = line.match(/^(.+)@(.+):(\d+):(\d+)$/);
201
299
  if (ffMatch) {
202
300
  const funcName = shortenFunctionName(ffMatch[1]) || "<anonymous>";
203
301
  const file = shortenFilePath(ffMatch[2]);
204
302
  lines.push(`${funcName}()`);
205
- lines.push(` ${file}:${ffMatch[3]}`);
303
+ lines.push(` ${file}:${ffMatch[3]}:${ffMatch[4]}`);
206
304
  continue;
207
305
  }
208
- const ffAnonMatch = line.match(/^@(.+):(\d+):\d+$/);
306
+ const ffAnonMatch = line.match(/^@(.+):(\d+):(\d+)$/);
209
307
  if (ffAnonMatch) {
210
308
  const file = shortenFilePath(ffAnonMatch[1]);
211
309
  lines.push(`<anonymous>()`);
212
- lines.push(` ${file}:${ffAnonMatch[2]}`);
310
+ lines.push(` ${file}:${ffAnonMatch[2]}:${ffAnonMatch[3]}`);
213
311
  continue;
214
312
  }
215
313
  }
@@ -317,8 +415,8 @@ function captureMessage(msg) {
317
415
  isMessage: true
318
416
  });
319
417
  }
320
- async function flush() {
418
+ async function flush(timeoutMs) {
321
419
  if (!client) return;
322
- await client.flush();
420
+ await client.flush(timeoutMs);
323
421
  }
324
422
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/client.ts","../src/transport.ts","../src/stack-trace.ts","../src/global-handlers.ts"],"sourcesContent":["import { nowISO } from \"@tracewayapp/core\";\nimport type { ExceptionStackTrace } from \"@tracewayapp/core\";\nimport {\n TracewayFrontendClient,\n type TracewayFrontendOptions,\n} from \"./client.js\";\nimport { formatBrowserStackTrace } from \"./stack-trace.js\";\nimport { installGlobalHandlers } from \"./global-handlers.js\";\n\nlet client: TracewayFrontendClient | null = null;\n\nexport function init(\n connectionString: string,\n options: TracewayFrontendOptions = {},\n): void {\n client = new TracewayFrontendClient(connectionString, options);\n if (typeof window !== \"undefined\") {\n installGlobalHandlers(client);\n }\n}\n\nexport function captureException(error: Error): void {\n if (!client) return;\n client.addException({\n traceId: null,\n stackTrace: formatBrowserStackTrace(error),\n recordedAt: nowISO(),\n isMessage: false,\n });\n}\n\nexport function captureExceptionWithAttributes(\n error: Error,\n attributes?: Record<string, string>,\n): void {\n if (!client) return;\n client.addException({\n traceId: null,\n stackTrace: formatBrowserStackTrace(error),\n recordedAt: nowISO(),\n attributes,\n isMessage: false,\n });\n}\n\nexport function captureMessage(msg: string): void {\n if (!client) return;\n client.addException({\n traceId: null,\n stackTrace: msg,\n recordedAt: nowISO(),\n isMessage: true,\n });\n}\n\nexport async function flush(): Promise<void> {\n if (!client) return;\n await client.flush();\n}\n\nexport { TracewayFrontendClient } from \"./client.js\";\nexport type { TracewayFrontendOptions } from \"./client.js\";\nexport { formatBrowserStackTrace } from \"./stack-trace.js\";\nexport { installGlobalHandlers } from \"./global-handlers.js\";\n\nexport type {\n ExceptionStackTrace,\n CollectionFrame,\n ReportRequest,\n} from \"@tracewayapp/core\";\n","import type {\n ExceptionStackTrace,\n ReportRequest,\n CollectionFrame,\n} from \"@tracewayapp/core\";\nimport { parseConnectionString, nowISO } from \"@tracewayapp/core\";\nimport { sendReport } from \"./transport.js\";\n\nexport interface TracewayFrontendOptions {\n debug?: boolean;\n debounceMs?: number;\n retryDelayMs?: number;\n version?: string;\n}\n\nexport class TracewayFrontendClient {\n private apiUrl: string;\n private token: string;\n private debug: boolean;\n private debounceMs: number;\n private retryDelayMs: number;\n private version: string;\n\n private pendingExceptions: ExceptionStackTrace[] = [];\n private isSyncing = false;\n private debounceTimer: ReturnType<typeof setTimeout> | null = null;\n private retryTimer: ReturnType<typeof setTimeout> | null = null;\n\n constructor(connectionString: string, options: TracewayFrontendOptions = {}) {\n const { token, apiUrl } = parseConnectionString(connectionString);\n this.apiUrl = apiUrl;\n this.token = token;\n this.debug = options.debug ?? false;\n this.debounceMs = options.debounceMs ?? 1500;\n this.retryDelayMs = options.retryDelayMs ?? 10000;\n this.version = options.version ?? \"\";\n }\n\n addException(exception: ExceptionStackTrace): void {\n this.pendingExceptions.push(exception);\n this.scheduleSync();\n }\n\n private scheduleSync(): void {\n if (this.debounceTimer !== null) {\n clearTimeout(this.debounceTimer);\n }\n this.debounceTimer = setTimeout(() => {\n this.debounceTimer = null;\n this.doSync();\n }, this.debounceMs);\n }\n\n private async doSync(): Promise<void> {\n if (this.isSyncing) return;\n if (this.pendingExceptions.length === 0) return;\n\n this.isSyncing = true;\n const batch = this.pendingExceptions.splice(0);\n\n const frame: CollectionFrame = {\n stackTraces: batch,\n metrics: [],\n traces: [],\n };\n\n const payload: ReportRequest = {\n collectionFrames: [frame],\n appVersion: this.version,\n serverName: \"\",\n };\n\n let failed = false;\n try {\n const success = await sendReport(\n this.apiUrl,\n this.token,\n JSON.stringify(payload),\n );\n if (!success) {\n failed = true;\n this.pendingExceptions.unshift(...batch);\n if (this.debug) {\n console.error(\"Traceway: sync failed, re-queued exceptions\");\n }\n }\n } catch (err) {\n failed = true;\n this.pendingExceptions.unshift(...batch);\n if (this.debug) {\n console.error(\"Traceway: sync error:\", err);\n }\n } finally {\n this.isSyncing = false;\n if (this.pendingExceptions.length > 0) {\n if (failed) {\n // On failure, wait before retrying to avoid hammering the server\n this.scheduleRetry();\n } else {\n // On success, process remaining items immediately\n this.doSync();\n }\n }\n }\n }\n\n private scheduleRetry(): void {\n if (this.retryTimer !== null) return; // Already scheduled\n this.retryTimer = setTimeout(() => {\n this.retryTimer = null;\n this.doSync();\n }, this.retryDelayMs);\n }\n\n async flush(): Promise<void> {\n if (this.debounceTimer !== null) {\n clearTimeout(this.debounceTimer);\n this.debounceTimer = null;\n }\n if (this.retryTimer !== null) {\n clearTimeout(this.retryTimer);\n this.retryTimer = null;\n }\n await this.doSync();\n }\n}\n","export async function compressGzip(data: string): Promise<Uint8Array> {\n const encoder = new TextEncoder();\n const inputBytes = encoder.encode(data);\n\n const cs = new CompressionStream(\"gzip\");\n const writer = cs.writable.getWriter();\n writer.write(inputBytes);\n writer.close();\n\n const reader = cs.readable.getReader();\n const chunks: Uint8Array[] = [];\n let totalLength = 0;\n\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n chunks.push(value);\n totalLength += value.length;\n }\n\n const result = new Uint8Array(totalLength);\n let offset = 0;\n for (const chunk of chunks) {\n result.set(chunk, offset);\n offset += chunk.length;\n }\n\n return result;\n}\n\nexport async function sendReport(\n apiUrl: string,\n token: string,\n body: string,\n): Promise<boolean> {\n const compressed = await compressGzip(body);\n\n const resp = await fetch(apiUrl, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"Content-Encoding\": \"gzip\",\n Authorization: `Bearer ${token}`,\n },\n body: compressed,\n });\n\n return resp.status === 200;\n}\n","export function formatBrowserStackTrace(error: Error): string {\n const lines: string[] = [];\n const typeName = error.constructor?.name || \"Error\";\n lines.push(`${typeName}: ${error.message}`);\n\n if (error.stack) {\n const stackLines = error.stack.split(\"\\n\");\n for (const line of stackLines) {\n // V8 format: \" at funcName (file:line:col)\"\n const v8Match = line.match(/^\\s+at\\s+(.+?)\\s+\\((.+):(\\d+):\\d+\\)$/);\n if (v8Match) {\n const funcName = shortenFunctionName(v8Match[1]);\n const file = shortenFilePath(v8Match[2]);\n lines.push(`${funcName}()`);\n lines.push(` ${file}:${v8Match[3]}`);\n continue;\n }\n\n // V8 anonymous: \" at file:line:col\"\n const v8AnonMatch = line.match(/^\\s+at\\s+(.+):(\\d+):\\d+$/);\n if (v8AnonMatch) {\n const file = shortenFilePath(v8AnonMatch[1]);\n lines.push(`<anonymous>()`);\n lines.push(` ${file}:${v8AnonMatch[2]}`);\n continue;\n }\n\n // Firefox format: \"funcName@file:line:col\"\n const ffMatch = line.match(/^(.+)@(.+):(\\d+):\\d+$/);\n if (ffMatch) {\n const funcName = shortenFunctionName(ffMatch[1]) || \"<anonymous>\";\n const file = shortenFilePath(ffMatch[2]);\n lines.push(`${funcName}()`);\n lines.push(` ${file}:${ffMatch[3]}`);\n continue;\n }\n\n // Firefox anonymous: \"@file:line:col\"\n const ffAnonMatch = line.match(/^@(.+):(\\d+):\\d+$/);\n if (ffAnonMatch) {\n const file = shortenFilePath(ffAnonMatch[1]);\n lines.push(`<anonymous>()`);\n lines.push(` ${file}:${ffAnonMatch[2]}`);\n continue;\n }\n }\n }\n\n return lines.join(\"\\n\") + \"\\n\";\n}\n\nfunction shortenFunctionName(fn: string): string {\n const slashIdx = fn.lastIndexOf(\"/\");\n if (slashIdx >= 0) {\n fn = fn.slice(slashIdx + 1);\n }\n const dotIdx = fn.indexOf(\".\");\n if (dotIdx >= 0) {\n fn = fn.slice(dotIdx + 1);\n }\n return fn;\n}\n\nfunction shortenFilePath(filePath: string): string {\n const parts = filePath.split(\"/\");\n return parts[parts.length - 1];\n}\n","import type { TracewayFrontendClient } from \"./client.js\";\nimport { formatBrowserStackTrace } from \"./stack-trace.js\";\nimport { nowISO } from \"@tracewayapp/core\";\n\nexport function installGlobalHandlers(client: TracewayFrontendClient): void {\n const prevOnError = window.onerror;\n window.onerror = (message, source, lineno, colno, error) => {\n if (error) {\n client.addException({\n traceId: null,\n stackTrace: formatBrowserStackTrace(error),\n recordedAt: nowISO(),\n isMessage: false,\n });\n } else {\n client.addException({\n traceId: null,\n stackTrace: String(message),\n recordedAt: nowISO(),\n isMessage: false,\n });\n }\n if (typeof prevOnError === \"function\") {\n return prevOnError(message, source, lineno, colno, error);\n }\n return false;\n };\n\n const prevOnUnhandledRejection = window.onunhandledrejection;\n window.onunhandledrejection = (event: PromiseRejectionEvent) => {\n const reason = event.reason;\n if (reason instanceof Error) {\n client.addException({\n traceId: null,\n stackTrace: formatBrowserStackTrace(reason),\n recordedAt: nowISO(),\n isMessage: false,\n });\n } else {\n client.addException({\n traceId: null,\n stackTrace: String(reason),\n recordedAt: nowISO(),\n isMessage: false,\n });\n }\n if (typeof prevOnUnhandledRejection === \"function\") {\n prevOnUnhandledRejection.call(window, event);\n }\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,eAAuB;;;ACKvB,kBAA8C;;;ACL9C,eAAsB,aAAa,MAAmC;AACpE,QAAM,UAAU,IAAI,YAAY;AAChC,QAAM,aAAa,QAAQ,OAAO,IAAI;AAEtC,QAAM,KAAK,IAAI,kBAAkB,MAAM;AACvC,QAAM,SAAS,GAAG,SAAS,UAAU;AACrC,SAAO,MAAM,UAAU;AACvB,SAAO,MAAM;AAEb,QAAM,SAAS,GAAG,SAAS,UAAU;AACrC,QAAM,SAAuB,CAAC;AAC9B,MAAI,cAAc;AAElB,SAAO,MAAM;AACX,UAAM,EAAE,MAAM,MAAM,IAAI,MAAM,OAAO,KAAK;AAC1C,QAAI,KAAM;AACV,WAAO,KAAK,KAAK;AACjB,mBAAe,MAAM;AAAA,EACvB;AAEA,QAAM,SAAS,IAAI,WAAW,WAAW;AACzC,MAAI,SAAS;AACb,aAAW,SAAS,QAAQ;AAC1B,WAAO,IAAI,OAAO,MAAM;AACxB,cAAU,MAAM;AAAA,EAClB;AAEA,SAAO;AACT;AAEA,eAAsB,WACpB,QACA,OACA,MACkB;AAClB,QAAM,aAAa,MAAM,aAAa,IAAI;AAE1C,QAAM,OAAO,MAAM,MAAM,QAAQ;AAAA,IAC/B,QAAQ;AAAA,IACR,S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K,QAAQ,KAAK;AAAA,IAC7C;AAAA,EACF;AACF;;;AJzCA,IAAI,SAAwC;AAErC,SAAS,KACd,kBACA,UAAmC,CAAC,GAC9B;AACN,WAAS,IAAI,uBAAuB,kBAAkB,OAAO;AAC7D,MAAI,OAAO,WAAW,aAAa;AACjC,0BAAsB,MAAM;AAAA,EAC9B;AACF;AAEO,SAAS,iBAAiB,OAAoB;AACnD,MAAI,CAAC,OAAQ;AACb,SAAO,aAAa;AAAA,IAClB,SAAS;AAAA,IACT,YAAY,wBAAwB,KAAK;AAAA,IACzC,gBAAY,qBAAO;AAAA,IACnB,WAAW;AAAA,EACb,CAAC;AACH;AAEO,SAAS,+BACd,OACA,YACM;AACN,MAAI,CAAC,OAAQ;AACb,SAAO,aAAa;AAAA,IAClB,SAAS;AAAA,IACT,YAAY,wBAAwB,KAAK;AAAA,IACzC,gBAAY,qBAAO;AAAA,IACnB;AAAA,IACA,WAAW;AAAA,EACb,CAAC;AACH;AAEO,SAAS,eAAe,KAAmB;AAChD,MAAI,CAAC,OAAQ;AACb,SAAO,aAAa;AAAA,IAClB,SAAS;AAAA,IACT,YAAY;AAAA,IACZ,gBAAY,qBAAO;AAAA,IACnB,WAAW;AAAA,EACb,CAAC;AACH;AAEA,eAAsB,QAAuB;AAC3C,MAAI,CAAC,OAAQ;AACb,QAAM,OAAO,MAAM;AACrB;","names":["import_core","import_core","client"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/client.ts","../src/transport.ts","../src/session-recorder.ts","../src/stack-trace.ts","../src/global-handlers.ts"],"sourcesContent":["import { nowISO } from \"@tracewayapp/core\";\nimport type { ExceptionStackTrace } from \"@tracewayapp/core\";\nimport {\n TracewayFrontendClient,\n type TracewayFrontendOptions,\n} from \"./client.js\";\nimport { formatBrowserStackTrace } from \"./stack-trace.js\";\nimport { installGlobalHandlers } from \"./global-handlers.js\";\n\nlet client: TracewayFrontendClient | null = null;\n\nexport function init(\n connectionString: string,\n options: TracewayFrontendOptions = {},\n): void {\n client = new TracewayFrontendClient(connectionString, options);\n if (typeof window !== \"undefined\") {\n installGlobalHandlers(client);\n }\n}\n\nexport function captureException(error: Error): void {\n if (!client) return;\n client.addException({\n traceId: null,\n stackTrace: formatBrowserStackTrace(error),\n recordedAt: nowISO(),\n isMessage: false,\n });\n}\n\nexport function captureExceptionWithAttributes(\n error: Error,\n attributes?: Record<string, string>,\n): void {\n if (!client) return;\n client.addException({\n traceId: null,\n stackTrace: formatBrowserStackTrace(error),\n recordedAt: nowISO(),\n attributes,\n isMessage: false,\n });\n}\n\nexport function captureMessage(msg: string): void {\n if (!client) return;\n client.addException({\n traceId: null,\n stackTrace: msg,\n recordedAt: nowISO(),\n isMessage: true,\n });\n}\n\nexport async function flush(timeoutMs?: number): Promise<void> {\n if (!client) return;\n await client.flush(timeoutMs);\n}\n\nexport { TracewayFrontendClient } from \"./client.js\";\nexport type { TracewayFrontendOptions } from \"./client.js\";\nexport { formatBrowserStackTrace } from \"./stack-trace.js\";\nexport { installGlobalHandlers } from \"./global-handlers.js\";\n\nexport type {\n ExceptionStackTrace,\n CollectionFrame,\n ReportRequest,\n} from \"@tracewayapp/core\";\n","import type {\n ExceptionStackTrace,\n ReportRequest,\n CollectionFrame,\n SessionRecordingPayload,\n} from \"@tracewayapp/core\";\nimport { parseConnectionString, generateUUID } from \"@tracewayapp/core\";\nimport { sendReport } from \"./transport.js\";\nimport { SessionRecorder } from \"./session-recorder.js\";\n\nexport interface TracewayFrontendOptions {\n debug?: boolean;\n debounceMs?: number;\n retryDelayMs?: number;\n version?: string;\n sessionRecording?: boolean;\n sessionRecordingSegmentDuration?: number;\n}\n\nexport class TracewayFrontendClient {\n private apiUrl: string;\n private token: string;\n private debug: boolean;\n private debounceMs: number;\n private retryDelayMs: number;\n private version: string;\n\n private pendingExceptions: ExceptionStackTrace[] = [];\n private pendingRecordings: SessionRecordingPayload[] = [];\n private isSyncing = false;\n private debounceTimer: ReturnType<typeof setTimeout> | null = null;\n private retryTimer: ReturnType<typeof setTimeout> | null = null;\n\n private recorder: SessionRecorder | null = null;\n\n constructor(connectionString: string, options: TracewayFrontendOptions = {}) {\n const { token, apiUrl } = parseConnectionString(connectionString);\n this.apiUrl = apiUrl;\n this.token = token;\n this.debug = options.debug ?? false;\n this.debounceMs = options.debounceMs ?? 1500;\n this.retryDelayMs = options.retryDelayMs ?? 10000;\n this.version = options.version ?? \"\";\n\n if (options.sessionRecording !== false && typeof window !== \"undefined\") {\n this.recorder = new SessionRecorder({\n segmentDuration: options.sessionRecordingSegmentDuration,\n });\n this.recorder.start();\n }\n }\n\n addException(exception: ExceptionStackTrace): void {\n if (this.recorder && this.recorder.hasSegments()) {\n const segments = this.recorder.getSegments();\n const allEvents = segments.flatMap((s) => s.events);\n const exceptionId = generateUUID();\n exception.sessionRecordingId = exceptionId;\n this.pendingRecordings.push({ exceptionId, events: allEvents });\n }\n\n this.pendingExceptions.push(exception);\n this.scheduleSync();\n }\n\n private scheduleSync(): void {\n if (this.debounceTimer !== null) {\n clearTimeout(this.debounceTimer);\n }\n this.debounceTimer = setTimeout(() => {\n this.debounceTimer = null;\n this.doSync();\n }, this.debounceMs);\n }\n\n private async doSync(): Promise<void> {\n if (this.isSyncing) return;\n if (this.pendingExceptions.length === 0) return;\n\n this.isSyncing = true;\n const batch = this.pendingExceptions.splice(0);\n const recordings = this.pendingRecordings.splice(0);\n\n const frame: CollectionFrame = {\n stackTraces: batch,\n metrics: [],\n traces: [],\n sessionRecordings: recordings.length > 0 ? recordings : undefined,\n };\n\n const payload: ReportRequest = {\n collectionFrames: [frame],\n appVersion: this.version,\n serverName: \"\",\n };\n\n let failed = false;\n try {\n const success = await sendReport(\n this.apiUrl,\n this.token,\n JSON.stringify(payload),\n );\n if (!success) {\n failed = true;\n this.pendingExceptions.unshift(...batch);\n this.pendingRecordings.unshift(...recordings);\n if (this.debug) {\n console.error(\"Traceway: sync failed, re-queued exceptions\");\n }\n }\n } catch (err) {\n failed = true;\n this.pendingExceptions.unshift(...batch);\n this.pendingRecordings.unshift(...recordings);\n if (this.debug) {\n console.error(\"Traceway: sync error:\", err);\n }\n } finally {\n this.isSyncing = false;\n if (this.pendingExceptions.length > 0) {\n if (failed) {\n this.scheduleRetry();\n } else {\n this.doSync();\n }\n }\n }\n }\n\n private scheduleRetry(): void {\n if (this.retryTimer !== null) return;\n this.retryTimer = setTimeout(() => {\n this.retryTimer = null;\n this.doSync();\n }, this.retryDelayMs);\n }\n\n async flush(timeoutMs?: number): Promise<void> {\n if (this.debounceTimer !== null) {\n clearTimeout(this.debounceTimer);\n this.debounceTimer = null;\n }\n if (this.retryTimer !== null) {\n clearTimeout(this.retryTimer);\n this.retryTimer = null;\n }\n if (this.recorder) {\n this.recorder.stop();\n }\n\n const syncPromise = this.doSync();\n\n if (timeoutMs !== undefined) {\n await Promise.race([\n syncPromise,\n new Promise<void>((resolve) => setTimeout(resolve, timeoutMs)),\n ]);\n } else {\n await syncPromise;\n }\n }\n}\n","export async function compressGzip(data: string): Promise<Uint8Array> {\n const encoder = new TextEncoder();\n const inputBytes = encoder.encode(data);\n\n const cs = new CompressionStream(\"gzip\");\n const writer = cs.writable.getWriter();\n writer.write(inputBytes);\n writer.close();\n\n const reader = cs.readable.getReader();\n const chunks: Uint8Array[] = [];\n let totalLength = 0;\n\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n chunks.push(value);\n totalLength += value.length;\n }\n\n const result = new Uint8Array(totalLength);\n let offset = 0;\n for (const chunk of chunks) {\n result.set(chunk, offset);\n offset += chunk.length;\n }\n\n return result;\n}\n\nexport async function sendReport(\n apiUrl: string,\n token: string,\n body: string,\n): Promise<boolean> {\n const compressed = await compressGzip(body);\n\n const resp = await fetch(apiUrl, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"Content-Encoding\": \"gzip\",\n Authorization: `Bearer ${token}`,\n },\n body: compressed,\n });\n\n return resp.status === 200;\n}\n","import type { eventWithTime } from \"rrweb\";\nimport { record } from \"rrweb\";\n\nexport interface SessionRecorderOptions {\n segmentDuration?: number;\n}\n\ninterface Segment {\n events: eventWithTime[];\n startedAt: string;\n}\n\nexport class SessionRecorder {\n private segmentDuration: number;\n private current: Segment;\n private previous: Segment | null = null;\n private stopFn: (() => void) | null = null;\n private rotateInterval: ReturnType<typeof setInterval> | null = null;\n\n constructor(options: SessionRecorderOptions = {}) {\n this.segmentDuration = options.segmentDuration ?? 10_000;\n this.current = { events: [], startedAt: new Date().toISOString() };\n }\n\n start(): void {\n this.stopFn = record({\n emit: (event) => {\n this.onEvent(event);\n },\n });\n\n this.rotateInterval = setInterval(() => {\n this.rotateSegment();\n }, this.segmentDuration);\n }\n\n stop(): void {\n if (this.stopFn) {\n this.stopFn();\n this.stopFn = null;\n }\n if (this.rotateInterval) {\n clearInterval(this.rotateInterval);\n this.rotateInterval = null;\n }\n }\n\n private onEvent(event: eventWithTime): void {\n this.current.events.push(event);\n }\n\n private rotateSegment(): void {\n this.previous = this.current;\n this.current = { events: [], startedAt: new Date().toISOString() };\n record.takeFullSnapshot();\n }\n\n getSegments(): { events: eventWithTime[]; timestamp: string }[] {\n const segments: { events: eventWithTime[]; timestamp: string }[] = [];\n if (this.previous && this.previous.events.length > 0) {\n segments.push({\n events: this.previous.events,\n timestamp: this.previous.startedAt,\n });\n }\n if (this.current.events.length > 0) {\n segments.push({\n events: this.current.events,\n timestamp: this.current.startedAt,\n });\n }\n return segments;\n }\n\n hasSegments(): boolean {\n return (\n this.current.events.length > 0 ||\n (this.previous !== null && this.previous.events.length > 0)\n );\n }\n\n flush(): void {\n this.previous = null;\n this.current = { events: [], startedAt: new Date().toISOString() };\n if (this.stopFn) {\n record.takeFullSnapshot();\n }\n }\n}\n","export function formatBrowserStackTrace(error: Error): string {\n const lines: string[] = [];\n const typeName = error.constructor?.name || \"Error\";\n lines.push(`${typeName}: ${error.message}`);\n\n if (error.stack) {\n const stackLines = error.stack.split(\"\\n\");\n for (const line of stackLines) {\n // V8 format: \" at funcName (file:line:col)\"\n const v8Match = line.match(/^\\s+at\\s+(.+?)\\s+\\((.+):(\\d+):(\\d+)\\)$/);\n if (v8Match) {\n const funcName = shortenFunctionName(v8Match[1]);\n const file = shortenFilePath(v8Match[2]);\n lines.push(`${funcName}()`);\n lines.push(` ${file}:${v8Match[3]}:${v8Match[4]}`);\n continue;\n }\n\n // V8 anonymous: \" at file:line:col\"\n const v8AnonMatch = line.match(/^\\s+at\\s+(.+):(\\d+):(\\d+)$/);\n if (v8AnonMatch) {\n const file = shortenFilePath(v8AnonMatch[1]);\n lines.push(`<anonymous>()`);\n lines.push(` ${file}:${v8AnonMatch[2]}:${v8AnonMatch[3]}`);\n continue;\n }\n\n // Firefox format: \"funcName@file:line:col\"\n const ffMatch = line.match(/^(.+)@(.+):(\\d+):(\\d+)$/);\n if (ffMatch) {\n const funcName = shortenFunctionName(ffMatch[1]) || \"<anonymous>\";\n const file = shortenFilePath(ffMatch[2]);\n lines.push(`${funcName}()`);\n lines.push(` ${file}:${ffMatch[3]}:${ffMatch[4]}`);\n continue;\n }\n\n // Firefox anonymous: \"@file:line:col\"\n const ffAnonMatch = line.match(/^@(.+):(\\d+):(\\d+)$/);\n if (ffAnonMatch) {\n const file = shortenFilePath(ffAnonMatch[1]);\n lines.push(`<anonymous>()`);\n lines.push(` ${file}:${ffAnonMatch[2]}:${ffAnonMatch[3]}`);\n continue;\n }\n }\n }\n\n return lines.join(\"\\n\") + \"\\n\";\n}\n\nfunction shortenFunctionName(fn: string): string {\n const slashIdx = fn.lastIndexOf(\"/\");\n if (slashIdx >= 0) {\n fn = fn.slice(slashIdx + 1);\n }\n const dotIdx = fn.indexOf(\".\");\n if (dotIdx >= 0) {\n fn = fn.slice(dotIdx + 1);\n }\n return fn;\n}\n\nfunction shortenFilePath(filePath: string): string {\n const parts = filePath.split(\"/\");\n return parts[parts.length - 1];\n}\n","import type { TracewayFrontendClient } from \"./client.js\";\nimport { formatBrowserStackTrace } from \"./stack-trace.js\";\nimport { nowISO } from \"@tracewayapp/core\";\n\nexport function installGlobalHandlers(client: TracewayFrontendClient): void {\n const prevOnError = window.onerror;\n window.onerror = (message, source, lineno, colno, error) => {\n if (error) {\n client.addException({\n traceId: null,\n stackTrace: formatBrowserStackTrace(error),\n recordedAt: nowISO(),\n isMessage: false,\n });\n } else {\n client.addException({\n traceId: null,\n stackTrace: String(message),\n recordedAt: nowISO(),\n isMessage: false,\n });\n }\n if (typeof prevOnError === \"function\") {\n return prevOnError(message, source, lineno, colno, error);\n }\n return false;\n };\n\n const prevOnUnhandledRejection = window.onunhandledrejection;\n window.onunhandledrejection = (event: PromiseRejectionEvent) => {\n const reason = event.reason;\n if (reason instanceof Error) {\n client.addException({\n traceId: null,\n stackTrace: formatBrowserStackTrace(reason),\n recordedAt: nowISO(),\n isMessage: false,\n });\n } else {\n client.addException({\n traceId: null,\n stackTrace: String(reason),\n recordedAt: nowISO(),\n isMessage: false,\n });\n }\n if (typeof prevOnUnhandledRejection === \"function\") {\n prevOnUnhandledRejection.call(window, event);\n }\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,eAAuB;;;ACMvB,kBAAoD;;;ACNpD,eAAsB,aAAa,MAAmC;AACpE,QAAM,UAAU,IAAI,YAAY;AAChC,QAAM,aAAa,QAAQ,OAAO,IAAI;AAEtC,QAAM,KAAK,IAAI,kBAAkB,MAAM;AACvC,QAAM,SAAS,GAAG,SAAS,UAAU;AACrC,SAAO,MAAM,UAAU;AACvB,SAAO,MAAM;AAEb,QAAM,SAAS,GAAG,SAAS,UAAU;AACrC,QAAM,SAAuB,CAAC;AAC9B,MAAI,cAAc;AAElB,SAAO,MAAM;AACX,UAAM,EAAE,MAAM,MAAM,IAAI,MAAM,OAAO,KAAK;AAC1C,QAAI,KAAM;AACV,WAAO,KAAK,KAAK;AACjB,mBAAe,MAAM;AAAA,EACvB;AAEA,QAAM,SAAS,IAAI,WAA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D,MAAI,CAAC,OAAQ;AACb,QAAM,OAAO,MAAM,SAAS;AAC9B;","names":["import_core","import_core","client"]}
package/dist/index.mjs CHANGED
@@ -1,8 +1,8 @@
1
1
  // src/index.ts
2
- import { nowISO as nowISO3 } from "@tracewayapp/core";
2
+ import { nowISO as nowISO2 } from "@tracewayapp/core";
3
3
 
4
4
  // src/client.ts
5
- import { parseConnectionString } from "@tracewayapp/core";
5
+ import { parseConnectionString, generateUUID } from "@tracewayapp/core";
6
6
 
7
7
  // src/transport.ts
8
8
  async function compressGzip(data) {
@@ -43,6 +43,74 @@ async function sendReport(apiUrl, token, body) {
43
43
  return resp.status === 200;
44
44
  }
45
45
 
46
+ // src/session-recorder.ts
47
+ import { record } from "rrweb";
48
+ var SessionRecorder = class {
49
+ segmentDuration;
50
+ current;
51
+ previous = null;
52
+ stopFn = null;
53
+ rotateInterval = null;
54
+ constructor(options = {}) {
55
+ this.segmentDuration = options.segmentDuration ?? 1e4;
56
+ this.current = { events: [], startedAt: (/* @__PURE__ */ new Date()).toISOString() };
57
+ }
58
+ start() {
59
+ this.stopFn = record({
60
+ emit: (event) => {
61
+ this.onEvent(event);
62
+ }
63
+ });
64
+ this.rotateInterval = setInterval(() => {
65
+ this.rotateSegment();
66
+ }, this.segmentDuration);
67
+ }
68
+ stop() {
69
+ if (this.stopFn) {
70
+ this.stopFn();
71
+ this.stopFn = null;
72
+ }
73
+ if (this.rotateInterval) {
74
+ clearInterval(this.rotateInterval);
75
+ this.rotateInterval = null;
76
+ }
77
+ }
78
+ onEvent(event) {
79
+ this.current.events.push(event);
80
+ }
81
+ rotateSegment() {
82
+ this.previous = this.current;
83
+ this.current = { events: [], startedAt: (/* @__PURE__ */ new Date()).toISOString() };
84
+ record.takeFullSnapshot();
85
+ }
86
+ getSegments() {
87
+ const segments = [];
88
+ if (this.previous && this.previous.events.length > 0) {
89
+ segments.push({
90
+ events: this.previous.events,
91
+ timestamp: this.previous.startedAt
92
+ });
93
+ }
94
+ if (this.current.events.length > 0) {
95
+ segments.push({
96
+ events: this.current.events,
97
+ timestamp: this.current.startedAt
98
+ });
99
+ }
100
+ return segments;
101
+ }
102
+ hasSegments() {
103
+ return this.current.events.length > 0 || this.previous !== null && this.previous.events.length > 0;
104
+ }
105
+ flush() {
106
+ this.previous = null;
107
+ this.current = { events: [], startedAt: (/* @__PURE__ */ new Date()).toISOString() };
108
+ if (this.stopFn) {
109
+ record.takeFullSnapshot();
110
+ }
111
+ }
112
+ };
113
+
46
114
  // src/client.ts
47
115
  var TracewayFrontendClient = class {
48
116
  apiUrl;
@@ -52,9 +120,11 @@ var TracewayFrontendClient = class {
52
120
  retryDelayMs;
53
121
  version;
54
122
  pendingExceptions = [];
123
+ pendingRecordings = [];
55
124
  isSyncing = false;
56
125
  debounceTimer = null;
57
126
  retryTimer = null;
127
+ recorder = null;
58
128
  constructor(connectionString, options = {}) {
59
129
  const { token, apiUrl } = parseConnectionString(connectionString);
60
130
  this.apiUrl = apiUrl;
@@ -63,8 +133,21 @@ var TracewayFrontendClient = class {
63
133
  this.debounceMs = options.debounceMs ?? 1500;
64
134
  this.retryDelayMs = options.retryDelayMs ?? 1e4;
65
135
  this.version = options.version ?? "";
136
+ if (options.sessionRecording !== false && typeof window !== "undefined") {
137
+ this.recorder = new SessionRecorder({
138
+ segmentDuration: options.sessionRecordingSegmentDuration
139
+ });
140
+ this.recorder.start();
141
+ }
66
142
  }
67
143
  addException(exception) {
144
+ if (this.recorder && this.recorder.hasSegments()) {
145
+ const segments = this.recorder.getSegments();
146
+ const allEvents = segments.flatMap((s) => s.events);
147
+ const exceptionId = generateUUID();
148
+ exception.sessionRecordingId = exceptionId;
149
+ this.pendingRecordings.push({ exceptionId, events: allEvents });
150
+ }
68
151
  this.pendingExceptions.push(exception);
69
152
  this.scheduleSync();
70
153
  }
@@ -82,10 +165,12 @@ var TracewayFrontendClient = class {
82
165
  if (this.pendingExceptions.length === 0) return;
83
166
  this.isSyncing = true;
84
167
  const batch = this.pendingExceptions.splice(0);
168
+ const recordings = this.pendingRecordings.splice(0);
85
169
  const frame = {
86
170
  stackTraces: batch,
87
171
  metrics: [],
88
- traces: []
172
+ traces: [],
173
+ sessionRecordings: recordings.length > 0 ? recordings : void 0
89
174
  };
90
175
  const payload = {
91
176
  collectionFrames: [frame],
@@ -102,6 +187,7 @@ var TracewayFrontendClient = class {
102
187
  if (!success) {
103
188
  failed = true;
104
189
  this.pendingExceptions.unshift(...batch);
190
+ this.pendingRecordings.unshift(...recordings);
105
191
  if (this.debug) {
106
192
  console.error("Traceway: sync failed, re-queued exceptions");
107
193
  }
@@ -109,6 +195,7 @@ var TracewayFrontendClient = class {
109
195
  } catch (err) {
110
196
  failed = true;
111
197
  this.pendingExceptions.unshift(...batch);
198
+ this.pendingRecordings.unshift(...recordings);
112
199
  if (this.debug) {
113
200
  console.error("Traceway: sync error:", err);
114
201
  }
@@ -130,7 +217,7 @@ var TracewayFrontendClient = class {
130
217
  this.doSync();
131
218
  }, this.retryDelayMs);
132
219
  }
133
- async flush() {
220
+ async flush(timeoutMs) {
134
221
  if (this.debounceTimer !== null) {
135
222
  clearTimeout(this.debounceTimer);
136
223
  this.debounceTimer = null;
@@ -139,7 +226,18 @@ var TracewayFrontendClient = class {
139
226
  clearTimeout(this.retryTimer);
140
227
  this.retryTimer = null;
141
228
  }
142
- await this.doSync();
229
+ if (this.recorder) {
230
+ this.recorder.stop();
231
+ }
232
+ const syncPromise = this.doSync();
233
+ if (timeoutMs !== void 0) {
234
+ await Promise.race([
235
+ syncPromise,
236
+ new Promise((resolve) => setTimeout(resolve, timeoutMs))
237
+ ]);
238
+ } else {
239
+ await syncPromise;
240
+ }
143
241
  }
144
242
  };
145
243
 
@@ -151,34 +249,34 @@ function formatBrowserStackTrace(error) {
151
249
  if (error.stack) {
152
250
  const stackLines = error.stack.split("\n");
153
251
  for (const line of stackLines) {
154
- const v8Match = line.match(/^\s+at\s+(.+?)\s+\((.+):(\d+):\d+\)$/);
252
+ const v8Match = line.match(/^\s+at\s+(.+?)\s+\((.+):(\d+):(\d+)\)$/);
155
253
  if (v8Match) {
156
254
  const funcName = shortenFunctionName(v8Match[1]);
157
255
  const file = shortenFilePath(v8Match[2]);
158
256
  lines.push(`${funcName}()`);
159
- lines.push(` ${file}:${v8Match[3]}`);
257
+ lines.push(` ${file}:${v8Match[3]}:${v8Match[4]}`);
160
258
  continue;
161
259
  }
162
- const v8AnonMatch = line.match(/^\s+at\s+(.+):(\d+):\d+$/);
260
+ const v8AnonMatch = line.match(/^\s+at\s+(.+):(\d+):(\d+)$/);
163
261
  if (v8AnonMatch) {
164
262
  const file = shortenFilePath(v8AnonMatch[1]);
165
263
  lines.push(`<anonymous>()`);
166
- lines.push(` ${file}:${v8AnonMatch[2]}`);
264
+ lines.push(` ${file}:${v8AnonMatch[2]}:${v8AnonMatch[3]}`);
167
265
  continue;
168
266
  }
169
- const ffMatch = line.match(/^(.+)@(.+):(\d+):\d+$/);
267
+ const ffMatch = line.match(/^(.+)@(.+):(\d+):(\d+)$/);
170
268
  if (ffMatch) {
171
269
  const funcName = shortenFunctionName(ffMatch[1]) || "<anonymous>";
172
270
  const file = shortenFilePath(ffMatch[2]);
173
271
  lines.push(`${funcName}()`);
174
- lines.push(` ${file}:${ffMatch[3]}`);
272
+ lines.push(` ${file}:${ffMatch[3]}:${ffMatch[4]}`);
175
273
  continue;
176
274
  }
177
- const ffAnonMatch = line.match(/^@(.+):(\d+):\d+$/);
275
+ const ffAnonMatch = line.match(/^@(.+):(\d+):(\d+)$/);
178
276
  if (ffAnonMatch) {
179
277
  const file = shortenFilePath(ffAnonMatch[1]);
180
278
  lines.push(`<anonymous>()`);
181
- lines.push(` ${file}:${ffAnonMatch[2]}`);
279
+ lines.push(` ${file}:${ffAnonMatch[2]}:${ffAnonMatch[3]}`);
182
280
  continue;
183
281
  }
184
282
  }
@@ -202,7 +300,7 @@ function shortenFilePath(filePath) {
202
300
  }
203
301
 
204
302
  // src/global-handlers.ts
205
- import { nowISO as nowISO2 } from "@tracewayapp/core";
303
+ import { nowISO } from "@tracewayapp/core";
206
304
  function installGlobalHandlers(client2) {
207
305
  const prevOnError = window.onerror;
208
306
  window.onerror = (message, source, lineno, colno, error) => {
@@ -210,14 +308,14 @@ function installGlobalHandlers(client2) {
210
308
  client2.addException({
211
309
  traceId: null,
212
310
  stackTrace: formatBrowserStackTrace(error),
213
- recordedAt: nowISO2(),
311
+ recordedAt: nowISO(),
214
312
  isMessage: false
215
313
  });
216
314
  } else {
217
315
  client2.addException({
218
316
  traceId: null,
219
317
  stackTrace: String(message),
220
- recordedAt: nowISO2(),
318
+ recordedAt: nowISO(),
221
319
  isMessage: false
222
320
  });
223
321
  }
@@ -233,14 +331,14 @@ function installGlobalHandlers(client2) {
233
331
  client2.addException({
234
332
  traceId: null,
235
333
  stackTrace: formatBrowserStackTrace(reason),
236
- recordedAt: nowISO2(),
334
+ recordedAt: nowISO(),
237
335
  isMessage: false
238
336
  });
239
337
  } else {
240
338
  client2.addException({
241
339
  traceId: null,
242
340
  stackTrace: String(reason),
243
- recordedAt: nowISO2(),
341
+ recordedAt: nowISO(),
244
342
  isMessage: false
245
343
  });
246
344
  }
@@ -263,7 +361,7 @@ function captureException(error) {
263
361
  client.addException({
264
362
  traceId: null,
265
363
  stackTrace: formatBrowserStackTrace(error),
266
- recordedAt: nowISO3(),
364
+ recordedAt: nowISO2(),
267
365
  isMessage: false
268
366
  });
269
367
  }
@@ -272,7 +370,7 @@ function captureExceptionWithAttributes(error, attributes) {
272
370
  client.addException({
273
371
  traceId: null,
274
372
  stackTrace: formatBrowserStackTrace(error),
275
- recordedAt: nowISO3(),
373
+ recordedAt: nowISO2(),
276
374
  attributes,
277
375
  isMessage: false
278
376
  });
@@ -282,13 +380,13 @@ function captureMessage(msg) {
282
380
  client.addException({
283
381
  traceId: null,
284
382
  stackTrace: msg,
285
- recordedAt: nowISO3(),
383
+ recordedAt: nowISO2(),
286
384
  isMessage: true
287
385
  });
288
386
  }
289
- async function flush() {
387
+ async function flush(timeoutMs) {
290
388
  if (!client) return;
291
- await client.flush();
389
+ await client.flush(timeoutMs);
292
390
  }
293
391
  export {
294
392
  TracewayFrontendClient,
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/client.ts","../src/transport.ts","../src/stack-trace.ts","../src/global-handlers.ts"],"sourcesContent":["import { nowISO } from \"@tracewayapp/core\";\nimport type { ExceptionStackTrace } from \"@tracewayapp/core\";\nimport {\n TracewayFrontendClient,\n type TracewayFrontendOptions,\n} from \"./client.js\";\nimport { formatBrowserStackTrace } from \"./stack-trace.js\";\nimport { installGlobalHandlers } from \"./global-handlers.js\";\n\nlet client: TracewayFrontendClient | null = null;\n\nexport function init(\n connectionString: string,\n options: TracewayFrontendOptions = {},\n): void {\n client = new TracewayFrontendClient(connectionString, options);\n if (typeof window !== \"undefined\") {\n installGlobalHandlers(client);\n }\n}\n\nexport function captureException(error: Error): void {\n if (!client) return;\n client.addException({\n traceId: null,\n stackTrace: formatBrowserStackTrace(error),\n recordedAt: nowISO(),\n isMessage: false,\n });\n}\n\nexport function captureExceptionWithAttributes(\n error: Error,\n attributes?: Record<string, string>,\n): void {\n if (!client) return;\n client.addException({\n traceId: null,\n stackTrace: formatBrowserStackTrace(error),\n recordedAt: nowISO(),\n attributes,\n isMessage: false,\n });\n}\n\nexport function captureMessage(msg: string): void {\n if (!client) return;\n client.addException({\n traceId: null,\n stackTrace: msg,\n recordedAt: nowISO(),\n isMessage: true,\n });\n}\n\nexport async function flush(): Promise<void> {\n if (!client) return;\n await client.flush();\n}\n\nexport { TracewayFrontendClient } from \"./client.js\";\nexport type { TracewayFrontendOptions } from \"./client.js\";\nexport { formatBrowserStackTrace } from \"./stack-trace.js\";\nexport { installGlobalHandlers } from \"./global-handlers.js\";\n\nexport type {\n ExceptionStackTrace,\n CollectionFrame,\n ReportRequest,\n} from \"@tracewayapp/core\";\n","import type {\n ExceptionStackTrace,\n ReportRequest,\n CollectionFrame,\n} from \"@tracewayapp/core\";\nimport { parseConnectionString, nowISO } from \"@tracewayapp/core\";\nimport { sendReport } from \"./transport.js\";\n\nexport interface TracewayFrontendOptions {\n debug?: boolean;\n debounceMs?: number;\n retryDelayMs?: number;\n version?: string;\n}\n\nexport class TracewayFrontendClient {\n private apiUrl: string;\n private token: string;\n private debug: boolean;\n private debounceMs: number;\n private retryDelayMs: number;\n private version: string;\n\n private pendingExceptions: ExceptionStackTrace[] = [];\n private isSyncing = false;\n private debounceTimer: ReturnType<typeof setTimeout> | null = null;\n private retryTimer: ReturnType<typeof setTimeout> | null = null;\n\n constructor(connectionString: string, options: TracewayFrontendOptions = {}) {\n const { token, apiUrl } = parseConnectionString(connectionString);\n this.apiUrl = apiUrl;\n this.token = token;\n this.debug = options.debug ?? false;\n this.debounceMs = options.debounceMs ?? 1500;\n this.retryDelayMs = options.retryDelayMs ?? 10000;\n this.version = options.version ?? \"\";\n }\n\n addException(exception: ExceptionStackTrace): void {\n this.pendingExceptions.push(exception);\n this.scheduleSync();\n }\n\n private scheduleSync(): void {\n if (this.debounceTimer !== null) {\n clearTimeout(this.debounceTimer);\n }\n this.debounceTimer = setTimeout(() => {\n this.debounceTimer = null;\n this.doSync();\n }, this.debounceMs);\n }\n\n private async doSync(): Promise<void> {\n if (this.isSyncing) return;\n if (this.pendingExceptions.length === 0) return;\n\n this.isSyncing = true;\n const batch = this.pendingExceptions.splice(0);\n\n const frame: CollectionFrame = {\n stackTraces: batch,\n metrics: [],\n traces: [],\n };\n\n const payload: ReportRequest = {\n collectionFrames: [frame],\n appVersion: this.version,\n serverName: \"\",\n };\n\n let failed = false;\n try {\n const success = await sendReport(\n this.apiUrl,\n this.token,\n JSON.stringify(payload),\n );\n if (!success) {\n failed = true;\n this.pendingExceptions.unshift(...batch);\n if (this.debug) {\n console.error(\"Traceway: sync failed, re-queued exceptions\");\n }\n }\n } catch (err) {\n failed = true;\n this.pendingExceptions.unshift(...batch);\n if (this.debug) {\n console.error(\"Traceway: sync error:\", err);\n }\n } finally {\n this.isSyncing = false;\n if (this.pendingExceptions.length > 0) {\n if (failed) {\n // On failure, wait before retrying to avoid hammering the server\n this.scheduleRetry();\n } else {\n // On success, process remaining items immediately\n this.doSync();\n }\n }\n }\n }\n\n private scheduleRetry(): void {\n if (this.retryTimer !== null) return; // Already scheduled\n this.retryTimer = setTimeout(() => {\n this.retryTimer = null;\n this.doSync();\n }, this.retryDelayMs);\n }\n\n async flush(): Promise<void> {\n if (this.debounceTimer !== null) {\n clearTimeout(this.debounceTimer);\n this.debounceTimer = null;\n }\n if (this.retryTimer !== null) {\n clearTimeout(this.retryTimer);\n this.retryTimer = null;\n }\n await this.doSync();\n }\n}\n","export async function compressGzip(data: string): Promise<Uint8Array> {\n const encoder = new TextEncoder();\n const inputBytes = encoder.encode(data);\n\n const cs = new CompressionStream(\"gzip\");\n const writer = cs.writable.getWriter();\n writer.write(inputBytes);\n writer.close();\n\n const reader = cs.readable.getReader();\n const chunks: Uint8Array[] = [];\n let totalLength = 0;\n\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n chunks.push(value);\n totalLength += value.length;\n }\n\n const result = new Uint8Array(totalLength);\n let offset = 0;\n for (const chunk of chunks) {\n result.set(chunk, offset);\n offset += chunk.length;\n }\n\n return result;\n}\n\nexport async function sendReport(\n apiUrl: string,\n token: string,\n body: string,\n): Promise<boolean> {\n const compressed = await compressGzip(body);\n\n const resp = await fetch(apiUrl, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"Content-Encoding\": \"gzip\",\n Authorization: `Bearer ${token}`,\n },\n body: compressed,\n });\n\n return resp.status === 200;\n}\n","export function formatBrowserStackTrace(error: Error): string {\n const lines: string[] = [];\n const typeName = error.constructor?.name || \"Error\";\n lines.push(`${typeName}: ${error.message}`);\n\n if (error.stack) {\n const stackLines = error.stack.split(\"\\n\");\n for (const line of stackLines) {\n // V8 format: \" at funcName (file:line:col)\"\n const v8Match = line.match(/^\\s+at\\s+(.+?)\\s+\\((.+):(\\d+):\\d+\\)$/);\n if (v8Match) {\n const funcName = shortenFunctionName(v8Match[1]);\n const file = shortenFilePath(v8Match[2]);\n lines.push(`${funcName}()`);\n lines.push(` ${file}:${v8Match[3]}`);\n continue;\n }\n\n // V8 anonymous: \" at file:line:col\"\n const v8AnonMatch = line.match(/^\\s+at\\s+(.+):(\\d+):\\d+$/);\n if (v8AnonMatch) {\n const file = shortenFilePath(v8AnonMatch[1]);\n lines.push(`<anonymous>()`);\n lines.push(` ${file}:${v8AnonMatch[2]}`);\n continue;\n }\n\n // Firefox format: \"funcName@file:line:col\"\n const ffMatch = line.match(/^(.+)@(.+):(\\d+):\\d+$/);\n if (ffMatch) {\n const funcName = shortenFunctionName(ffMatch[1]) || \"<anonymous>\";\n const file = shortenFilePath(ffMatch[2]);\n lines.push(`${funcName}()`);\n lines.push(` ${file}:${ffMatch[3]}`);\n continue;\n }\n\n // Firefox anonymous: \"@file:line:col\"\n const ffAnonMatch = line.match(/^@(.+):(\\d+):\\d+$/);\n if (ffAnonMatch) {\n const file = shortenFilePath(ffAnonMatch[1]);\n lines.push(`<anonymous>()`);\n lines.push(` ${file}:${ffAnonMatch[2]}`);\n continue;\n }\n }\n }\n\n return lines.join(\"\\n\") + \"\\n\";\n}\n\nfunction shortenFunctionName(fn: string): string {\n const slashIdx = fn.lastIndexOf(\"/\");\n if (slashIdx >= 0) {\n fn = fn.slice(slashIdx + 1);\n }\n const dotIdx = fn.indexOf(\".\");\n if (dotIdx >= 0) {\n fn = fn.slice(dotIdx + 1);\n }\n return fn;\n}\n\nfunction shortenFilePath(filePath: string): string {\n const parts = filePath.split(\"/\");\n return parts[parts.length - 1];\n}\n","import type { TracewayFrontendClient } from \"./client.js\";\nimport { formatBrowserStackTrace } from \"./stack-trace.js\";\nimport { nowISO } from \"@tracewayapp/core\";\n\nexport function installGlobalHandlers(client: TracewayFrontendClient): void {\n const prevOnError = window.onerror;\n window.onerror = (message, source, lineno, colno, error) => {\n if (error) {\n client.addException({\n traceId: null,\n stackTrace: formatBrowserStackTrace(error),\n recordedAt: nowISO(),\n isMessage: false,\n });\n } else {\n client.addException({\n traceId: null,\n stackTrace: String(message),\n recordedAt: nowISO(),\n isMessage: false,\n });\n }\n if (typeof prevOnError === \"function\") {\n return prevOnError(message, source, lineno, colno, error);\n }\n return false;\n };\n\n const prevOnUnhandledRejection = window.onunhandledrejection;\n window.onunhandledrejection = (event: PromiseRejectionEvent) => {\n const reason = event.reason;\n if (reason instanceof Error) {\n client.addException({\n traceId: null,\n stackTrace: formatBrowserStackTrace(reason),\n recordedAt: nowISO(),\n isMessage: false,\n });\n } else {\n client.addException({\n traceId: null,\n stackTrace: String(reason),\n recordedAt: nowISO(),\n isMessage: false,\n });\n }\n if (typeof prevOnUnhandledRejection === \"function\") {\n prevOnUnhandledRejection.call(window, event);\n }\n };\n}\n"],"mappings":";AAAA,SAAS,UAAAA,eAAc;;;ACKvB,SAAS,6BAAqC;;;ACL9C,eAAsB,aAAa,MAAmC;AACpE,QAAM,UAAU,IAAI,YAAY;AAChC,QAAM,aAAa,QAAQ,OAAO,IAAI;AAEtC,QAAM,KAAK,IAAI,kBAAkB,MAAM;AACvC,QAAM,SAAS,GAAG,SAAS,UAAU;AACrC,SAAO,MAAM,UAAU;AACvB,SAAO,MAAM;AAEb,QAAM,SAAS,GAAG,SAAS,UAAU;AACrC,QAAM,SAAuB,CAAC;AAC9B,MAAI,cAAc;AAElB,SAAO,MAAM;AACX,UAAM,EAAE,MAAM,MAAM,IAAI,MAAM,OAAO,KAAK;AAC1C,QAAI,KAAM;AACV,WAAO,KAAK,KAAK;AACjB,mBAAe,MAAM;AAAA,EACvB;AAEA,QAAM,SAAS,IAAI,WAAW,WAAW;AACzC,MAAI,SAAS;AACb,aAAW,SAAS,QAAQ;AAC1B,WAAO,IAAI,OAAO,MAAM;AACxB,cAAU,MAAM;AAAA,EAClB;AAEA,SAAO;AACT;AAEA,eAAsB,WACpB,QACA,OACA,MACkB;AAClB,QAAM,aAAa,MAAM,aAAa,IAAI;AAE1C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AAC;AAAA,IACH;AACA,QAAI,OAAO,6BAA6B,YAAY;AAClD,+BAAyB,KAAK,QAAQ,KAAK;AAAA,IAC7C;AAAA,EACF;AACF;;;AJzCA,IAAI,SAAwC;AAErC,SAAS,KACd,kBACA,UAAmC,CAAC,GAC9B;AACN,WAAS,IAAI,uBAAuB,kBAAkB,OAAO;AAC7D,MAAI,OAAO,WAAW,aAAa;AACjC,0BAAsB,MAAM;AAAA,EAC9B;AACF;AAEO,SAAS,iBAAiB,OAAoB;AACnD,MAAI,CAAC,OAAQ;AACb,SAAO,aAAa;AAAA,IAClB,SAAS;AAAA,IACT,YAAY,wBAAwB,KAAK;AAAA,IACzC,YAAYE,QAAO;AAAA,IACnB,WAAW;AAAA,EACb,CAAC;AACH;AAEO,SAAS,+BACd,OACA,YACM;AACN,MAAI,CAAC,OAAQ;AACb,SAAO,aAAa;AAAA,IAClB,SAAS;AAAA,IACT,YAAY,wBAAwB,KAAK;AAAA,IACzC,YAAYA,QAAO;AAAA,IACnB;AAAA,IACA,WAAW;AAAA,EACb,CAAC;AACH;AAEO,SAAS,eAAe,KAAmB;AAChD,MAAI,CAAC,OAAQ;AACb,SAAO,aAAa;AAAA,IAClB,SAAS;AAAA,IACT,YAAY;AAAA,IACZ,YAAYA,QAAO;AAAA,IACnB,WAAW;AAAA,EACb,CAAC;AACH;AAEA,eAAsB,QAAuB;AAC3C,MAAI,CAAC,OAAQ;AACb,QAAM,OAAO,MAAM;AACrB;","names":["nowISO","nowISO","client","nowISO"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/client.ts","../src/transport.ts","../src/session-recorder.ts","../src/stack-trace.ts","../src/global-handlers.ts"],"sourcesContent":["import { nowISO } from \"@tracewayapp/core\";\nimport type { ExceptionStackTrace } from \"@tracewayapp/core\";\nimport {\n TracewayFrontendClient,\n type TracewayFrontendOptions,\n} from \"./client.js\";\nimport { formatBrowserStackTrace } from \"./stack-trace.js\";\nimport { installGlobalHandlers } from \"./global-handlers.js\";\n\nlet client: TracewayFrontendClient | null = null;\n\nexport function init(\n connectionString: string,\n options: TracewayFrontendOptions = {},\n): void {\n client = new TracewayFrontendClient(connectionString, options);\n if (typeof window !== \"undefined\") {\n installGlobalHandlers(client);\n }\n}\n\nexport function captureException(error: Error): void {\n if (!client) return;\n client.addException({\n traceId: null,\n stackTrace: formatBrowserStackTrace(error),\n recordedAt: nowISO(),\n isMessage: false,\n });\n}\n\nexport function captureExceptionWithAttributes(\n error: Error,\n attributes?: Record<string, string>,\n): void {\n if (!client) return;\n client.addException({\n traceId: null,\n stackTrace: formatBrowserStackTrace(error),\n recordedAt: nowISO(),\n attributes,\n isMessage: false,\n });\n}\n\nexport function captureMessage(msg: string): void {\n if (!client) return;\n client.addException({\n traceId: null,\n stackTrace: msg,\n recordedAt: nowISO(),\n isMessage: true,\n });\n}\n\nexport async function flush(timeoutMs?: number): Promise<void> {\n if (!client) return;\n await client.flush(timeoutMs);\n}\n\nexport { TracewayFrontendClient } from \"./client.js\";\nexport type { TracewayFrontendOptions } from \"./client.js\";\nexport { formatBrowserStackTrace } from \"./stack-trace.js\";\nexport { installGlobalHandlers } from \"./global-handlers.js\";\n\nexport type {\n ExceptionStackTrace,\n CollectionFrame,\n ReportRequest,\n} from \"@tracewayapp/core\";\n","import type {\n ExceptionStackTrace,\n ReportRequest,\n CollectionFrame,\n SessionRecordingPayload,\n} from \"@tracewayapp/core\";\nimport { parseConnectionString, generateUUID } from \"@tracewayapp/core\";\nimport { sendReport } from \"./transport.js\";\nimport { SessionRecorder } from \"./session-recorder.js\";\n\nexport interface TracewayFrontendOptions {\n debug?: boolean;\n debounceMs?: number;\n retryDelayMs?: number;\n version?: string;\n sessionRecording?: boolean;\n sessionRecordingSegmentDuration?: number;\n}\n\nexport class TracewayFrontendClient {\n private apiUrl: string;\n private token: string;\n private debug: boolean;\n private debounceMs: number;\n private retryDelayMs: number;\n private version: string;\n\n private pendingExceptions: ExceptionStackTrace[] = [];\n private pendingRecordings: SessionRecordingPayload[] = [];\n private isSyncing = false;\n private debounceTimer: ReturnType<typeof setTimeout> | null = null;\n private retryTimer: ReturnType<typeof setTimeout> | null = null;\n\n private recorder: SessionRecorder | null = null;\n\n constructor(connectionString: string, options: TracewayFrontendOptions = {}) {\n const { token, apiUrl } = parseConnectionString(connectionString);\n this.apiUrl = apiUrl;\n this.token = token;\n this.debug = options.debug ?? false;\n this.debounceMs = options.debounceMs ?? 1500;\n this.retryDelayMs = options.retryDelayMs ?? 10000;\n this.version = options.version ?? \"\";\n\n if (options.sessionRecording !== false && typeof window !== \"undefined\") {\n this.recorder = new SessionRecorder({\n segmentDuration: options.sessionRecordingSegmentDuration,\n });\n this.recorder.start();\n }\n }\n\n addException(exception: ExceptionStackTrace): void {\n if (this.recorder && this.recorder.hasSegments()) {\n const segments = this.recorder.getSegments();\n const allEvents = segments.flatMap((s) => s.events);\n const exceptionId = generateUUID();\n exception.sessionRecordingId = exceptionId;\n this.pendingRecordings.push({ exceptionId, events: allEvents });\n }\n\n this.pendingExceptions.push(exception);\n this.scheduleSync();\n }\n\n private scheduleSync(): void {\n if (this.debounceTimer !== null) {\n clearTimeout(this.debounceTimer);\n }\n this.debounceTimer = setTimeout(() => {\n this.debounceTimer = null;\n this.doSync();\n }, this.debounceMs);\n }\n\n private async doSync(): Promise<void> {\n if (this.isSyncing) return;\n if (this.pendingExceptions.length === 0) return;\n\n this.isSyncing = true;\n const batch = this.pendingExceptions.splice(0);\n const recordings = this.pendingRecordings.splice(0);\n\n const frame: CollectionFrame = {\n stackTraces: batch,\n metrics: [],\n traces: [],\n sessionRecordings: recordings.length > 0 ? recordings : undefined,\n };\n\n const payload: ReportRequest = {\n collectionFrames: [frame],\n appVersion: this.version,\n serverName: \"\",\n };\n\n let failed = false;\n try {\n const success = await sendReport(\n this.apiUrl,\n this.token,\n JSON.stringify(payload),\n );\n if (!success) {\n failed = true;\n this.pendingExceptions.unshift(...batch);\n this.pendingRecordings.unshift(...recordings);\n if (this.debug) {\n console.error(\"Traceway: sync failed, re-queued exceptions\");\n }\n }\n } catch (err) {\n failed = true;\n this.pendingExceptions.unshift(...batch);\n this.pendingRecordings.unshift(...recordings);\n if (this.debug) {\n console.error(\"Traceway: sync error:\", err);\n }\n } finally {\n this.isSyncing = false;\n if (this.pendingExceptions.length > 0) {\n if (failed) {\n this.scheduleRetry();\n } else {\n this.doSync();\n }\n }\n }\n }\n\n private scheduleRetry(): void {\n if (this.retryTimer !== null) return;\n this.retryTimer = setTimeout(() => {\n this.retryTimer = null;\n this.doSync();\n }, this.retryDelayMs);\n }\n\n async flush(timeoutMs?: number): Promise<void> {\n if (this.debounceTimer !== null) {\n clearTimeout(this.debounceTimer);\n this.debounceTimer = null;\n }\n if (this.retryTimer !== null) {\n clearTimeout(this.retryTimer);\n this.retryTimer = null;\n }\n if (this.recorder) {\n this.recorder.stop();\n }\n\n const syncPromise = this.doSync();\n\n if (timeoutMs !== undefined) {\n await Promise.race([\n syncPromise,\n new Promise<void>((resolve) => setTimeout(resolve, timeoutMs)),\n ]);\n } else {\n await syncPromise;\n }\n }\n}\n","export async function compressGzip(data: string): Promise<Uint8Array> {\n const encoder = new TextEncoder();\n const inputBytes = encoder.encode(data);\n\n const cs = new CompressionStream(\"gzip\");\n const writer = cs.writable.getWriter();\n writer.write(inputBytes);\n writer.close();\n\n const reader = cs.readable.getReader();\n const chunks: Uint8Array[] = [];\n let totalLength = 0;\n\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n chunks.push(value);\n totalLength += value.length;\n }\n\n const result = new Uint8Array(totalLength);\n let offset = 0;\n for (const chunk of chunks) {\n result.set(chunk, offset);\n offset += chunk.length;\n }\n\n return result;\n}\n\nexport async function sendReport(\n apiUrl: string,\n token: string,\n body: string,\n): Promise<boolean> {\n const compressed = await compressGzip(body);\n\n const resp = await fetch(apiUrl, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"Content-Encoding\": \"gzip\",\n Authorization: `Bearer ${token}`,\n },\n body: compressed,\n });\n\n return resp.status === 200;\n}\n","import type { eventWithTime } from \"rrweb\";\nimport { record } from \"rrweb\";\n\nexport interface SessionRecorderOptions {\n segmentDuration?: number;\n}\n\ninterface Segment {\n events: eventWithTime[];\n startedAt: string;\n}\n\nexport class SessionRecorder {\n private segmentDuration: number;\n private current: Segment;\n private previous: Segment | null = null;\n private stopFn: (() => void) | null = null;\n private rotateInterval: ReturnType<typeof setInterval> | null = null;\n\n constructor(options: SessionRecorderOptions = {}) {\n this.segmentDuration = options.segmentDuration ?? 10_000;\n this.current = { events: [], startedAt: new Date().toISOString() };\n }\n\n start(): void {\n this.stopFn = record({\n emit: (event) => {\n this.onEvent(event);\n },\n });\n\n this.rotateInterval = setInterval(() => {\n this.rotateSegment();\n }, this.segmentDuration);\n }\n\n stop(): void {\n if (this.stopFn) {\n this.stopFn();\n this.stopFn = null;\n }\n if (this.rotateInterval) {\n clearInterval(this.rotateInterval);\n this.rotateInterval = null;\n }\n }\n\n private onEvent(event: eventWithTime): void {\n this.current.events.push(event);\n }\n\n private rotateSegment(): void {\n this.previous = this.current;\n this.current = { events: [], startedAt: new Date().toISOString() };\n record.takeFullSnapshot();\n }\n\n getSegments(): { events: eventWithTime[]; timestamp: string }[] {\n const segments: { events: eventWithTime[]; timestamp: string }[] = [];\n if (this.previous && this.previous.events.length > 0) {\n segments.push({\n events: this.previous.events,\n timestamp: this.previous.startedAt,\n });\n }\n if (this.current.events.length > 0) {\n segments.push({\n events: this.current.events,\n timestamp: this.current.startedAt,\n });\n }\n return segments;\n }\n\n hasSegments(): boolean {\n return (\n this.current.events.length > 0 ||\n (this.previous !== null && this.previous.events.length > 0)\n );\n }\n\n flush(): void {\n this.previous = null;\n this.current = { events: [], startedAt: new Date().toISOString() };\n if (this.stopFn) {\n record.takeFullSnapshot();\n }\n }\n}\n","export function formatBrowserStackTrace(error: Error): string {\n const lines: string[] = [];\n const typeName = error.constructor?.name || \"Error\";\n lines.push(`${typeName}: ${error.message}`);\n\n if (error.stack) {\n const stackLines = error.stack.split(\"\\n\");\n for (const line of stackLines) {\n // V8 format: \" at funcName (file:line:col)\"\n const v8Match = line.match(/^\\s+at\\s+(.+?)\\s+\\((.+):(\\d+):(\\d+)\\)$/);\n if (v8Match) {\n const funcName = shortenFunctionName(v8Match[1]);\n const file = shortenFilePath(v8Match[2]);\n lines.push(`${funcName}()`);\n lines.push(` ${file}:${v8Match[3]}:${v8Match[4]}`);\n continue;\n }\n\n // V8 anonymous: \" at file:line:col\"\n const v8AnonMatch = line.match(/^\\s+at\\s+(.+):(\\d+):(\\d+)$/);\n if (v8AnonMatch) {\n const file = shortenFilePath(v8AnonMatch[1]);\n lines.push(`<anonymous>()`);\n lines.push(` ${file}:${v8AnonMatch[2]}:${v8AnonMatch[3]}`);\n continue;\n }\n\n // Firefox format: \"funcName@file:line:col\"\n const ffMatch = line.match(/^(.+)@(.+):(\\d+):(\\d+)$/);\n if (ffMatch) {\n const funcName = shortenFunctionName(ffMatch[1]) || \"<anonymous>\";\n const file = shortenFilePath(ffMatch[2]);\n lines.push(`${funcName}()`);\n lines.push(` ${file}:${ffMatch[3]}:${ffMatch[4]}`);\n continue;\n }\n\n // Firefox anonymous: \"@file:line:col\"\n const ffAnonMatch = line.match(/^@(.+):(\\d+):(\\d+)$/);\n if (ffAnonMatch) {\n const file = shortenFilePath(ffAnonMatch[1]);\n lines.push(`<anonymous>()`);\n lines.push(` ${file}:${ffAnonMatch[2]}:${ffAnonMatch[3]}`);\n continue;\n }\n }\n }\n\n return lines.join(\"\\n\") + \"\\n\";\n}\n\nfunction shortenFunctionName(fn: string): string {\n const slashIdx = fn.lastIndexOf(\"/\");\n if (slashIdx >= 0) {\n fn = fn.slice(slashIdx + 1);\n }\n const dotIdx = fn.indexOf(\".\");\n if (dotIdx >= 0) {\n fn = fn.slice(dotIdx + 1);\n }\n return fn;\n}\n\nfunction shortenFilePath(filePath: string): string {\n const parts = filePath.split(\"/\");\n return parts[parts.length - 1];\n}\n","import type { TracewayFrontendClient } from \"./client.js\";\nimport { formatBrowserStackTrace } from \"./stack-trace.js\";\nimport { nowISO } from \"@tracewayapp/core\";\n\nexport function installGlobalHandlers(client: TracewayFrontendClient): void {\n const prevOnError = window.onerror;\n window.onerror = (message, source, lineno, colno, error) => {\n if (error) {\n client.addException({\n traceId: null,\n stackTrace: formatBrowserStackTrace(error),\n recordedAt: nowISO(),\n isMessage: false,\n });\n } else {\n client.addException({\n traceId: null,\n stackTrace: String(message),\n recordedAt: nowISO(),\n isMessage: false,\n });\n }\n if (typeof prevOnError === \"function\") {\n return prevOnError(message, source, lineno, colno, error);\n }\n return false;\n };\n\n const prevOnUnhandledRejection = window.onunhandledrejection;\n window.onunhandledrejection = (event: PromiseRejectionEvent) => {\n const reason = event.reason;\n if (reason instanceof Error) {\n client.addException({\n traceId: null,\n stackTrace: formatBrowserStackTrace(reason),\n recordedAt: nowISO(),\n isMessage: false,\n });\n } else {\n client.addException({\n traceId: null,\n stackTrace: String(reason),\n recordedAt: nowISO(),\n isMessage: false,\n });\n }\n if (typeof prevOnUnhandledRejection === \"function\") {\n prevOnUnhandledRejection.call(window, event);\n }\n };\n}\n"],"mappings":";AAAA,SAAS,UAAAA,eAAc;;;ACMvB,SAAS,uBAAuB,oBAAoB;;;ACNpD,eAAsB,aAAa,MAAmC;AACpE,QAAM,UAAU,IAAI,YAAY;AAChC,QAAM,aAAa,QAAQ,OAAO,IAAI;AAEtC,QAAM,KAAK,IAAI,kBAAkB,MAAM;AACvC,QAAM,SAAS,GAAG,SAAS,UAAU;AACrC,SAAO,MAAM,UAAU;AACvB,SAAO,MAAM;AAEb,QAAM,SAAS,GAAG,SAAS,UAAU;AACrC,QAAM,SAAuB,CAAC;AAC9B,MAAI,cAAc;AAElB,SAAO,MAAM;AACX,UAAM,EAAE,MAAM,MAAM,IAAI,MAAM,OAAO,KAAK;AAC1C,QAAI,KAAM;AACV,WAAO,KAAK,KAAK;AACjB,mBAAe,MAAM;AAAA,EACvB;AAEA,QAAM,SAAS,IAAI,WAAW,WAA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b,QAAM,OAAO,MAAM,SAAS;AAC9B;","names":["nowISO","client","nowISO"]}
package/package.json CHANGED
@@ -1,15 +1,15 @@
1
1
  {
2
2
  "name": "@tracewayapp/frontend",
3
- "version": "0.2.0",
3
+ "version": "1.0.0",
4
4
  "description": "Traceway SDK for browser environments",
5
- "main": "./dist/index.cjs",
6
- "module": "./dist/index.js",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
7
  "types": "./dist/index.d.ts",
8
8
  "exports": {
9
9
  ".": {
10
10
  "types": "./dist/index.d.ts",
11
- "import": "./dist/index.js",
12
- "require": "./dist/index.cjs"
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js"
13
13
  }
14
14
  },
15
15
  "files": [
@@ -23,6 +23,7 @@
23
23
  "dev": "tsup --watch"
24
24
  },
25
25
  "dependencies": {
26
- "@tracewayapp/core": "0.2.0"
26
+ "@tracewayapp/core": "1.0.0",
27
+ "rrweb": "^2.0.0-alpha.18"
27
28
  }
28
29
  }