@newtalaria/browser 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,39 @@
1
+ import type { FailedRequestStatusCode } from '../types.js';
1
2
  export type Teardown = () => void;
3
+ export interface NetworkMeta {
4
+ method: string;
5
+ url: string;
6
+ status?: number;
7
+ durationMs?: number;
8
+ ok?: boolean;
9
+ }
10
+ export interface NetworkHookOptions {
11
+ /** Still emit rrweb breadcrumbs for all requests. */
12
+ onNetwork?: (meta: NetworkMeta) => void;
13
+ /**
14
+ * Called when a completed request should become a Talaria event.
15
+ * Network transport errors (no status) are not promoted here — they rethrow as usual.
16
+ */
17
+ onFailedRequest?: (meta: NetworkMeta & {
18
+ status: number;
19
+ }) => void;
20
+ captureFailedRequests?: boolean;
21
+ failedRequestStatusCodes?: FailedRequestStatusCode[];
22
+ failedRequestIgnoreUrls?: string[];
23
+ /** SDK base URL — requests under this host + /events|/replays are never promoted. */
24
+ talariaBaseUrl?: string;
25
+ }
2
26
  /** Mirror console output into rrweb as `talaria-console` custom events. */
3
27
  export declare function installConsoleHook(): Teardown;
4
- /** Capture fetch / XHR metadata only (no bodies, no auth headers). */
5
- export declare function installNetworkHook(): Teardown;
28
+ export declare function statusMatches(status: number, codes: FailedRequestStatusCode[]): boolean;
29
+ export declare function shouldPromoteFailedRequest(meta: NetworkMeta, opts: {
30
+ captureFailedRequests: boolean;
31
+ failedRequestStatusCodes: FailedRequestStatusCode[];
32
+ failedRequestIgnoreUrls: string[];
33
+ talariaBaseUrl?: string;
34
+ }): meta is NetworkMeta & {
35
+ status: number;
36
+ };
37
+ /** Capture fetch / XHR metadata (no bodies, no auth headers); optionally promote failures. */
38
+ export declare function installNetworkHook(options?: NetworkHookOptions): Teardown;
6
39
  //# sourceMappingURL=hooks.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../src/replay/hooks.ts"],"names":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AC/B,wBAAwB,EAAE,uBAAuB,EAAE,CAAC;IACpD,uBAAuB,EAAE,MAAM,EAAE,CAAC;IAClC,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB,GACA,IAAI,IAAI,WAAW,GAAG;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,CAoB1C;AAED,8FAA8F;AAC9F,wBAAgB,kBAAkB,CAAC,OAAO,GAAE,kBAAuB,GAAG,QAAQ,CA+F7E"}
@@ -13216,11 +13216,52 @@
13216
13216
  } catch {
13217
13217
  }
13218
13218
  }
13219
- function installNetworkHook() {
13219
+ function statusMatches(status, codes) {
13220
+ for (const entry of codes) {
13221
+ if (typeof entry === "number") {
13222
+ if (status === entry) return true;
13223
+ } else if (Array.isArray(entry) && entry.length >= 2) {
13224
+ const [min, max] = entry;
13225
+ if (status >= min && status <= max) return true;
13226
+ }
13227
+ }
13228
+ return false;
13229
+ }
13230
+ function shouldPromoteFailedRequest(meta, opts) {
13231
+ if (!opts.captureFailedRequests) return false;
13232
+ if (typeof meta.status !== "number" || Number.isNaN(meta.status)) return false;
13233
+ if (!statusMatches(meta.status, opts.failedRequestStatusCodes)) return false;
13234
+ const url = meta.url || "";
13235
+ const ignore = [...opts.failedRequestIgnoreUrls];
13236
+ const base = (opts.talariaBaseUrl ?? "").replace(/\/+$/, "");
13237
+ if (base) {
13238
+ ignore.push(`${base}/events/`, `${base}/replays/`);
13239
+ }
13240
+ ignore.push("/events/ingest", "/events/ingestBatch", "/replays/");
13241
+ const lower = url.toLowerCase();
13242
+ for (const part of ignore) {
13243
+ if (part && lower.includes(part.toLowerCase())) return false;
13244
+ }
13245
+ return true;
13246
+ }
13247
+ function installNetworkHook(options = {}) {
13220
13248
  const originalFetch = typeof fetch === "function" ? fetch.bind(globalThis) : null;
13221
13249
  const XHR = typeof XMLHttpRequest !== "undefined" ? XMLHttpRequest : null;
13222
13250
  const originalOpen = XHR?.prototype.open;
13223
13251
  const originalSend = XHR?.prototype.send;
13252
+ const matchOpts = {
13253
+ captureFailedRequests: options.captureFailedRequests ?? true,
13254
+ failedRequestStatusCodes: options.failedRequestStatusCodes ?? [[500, 599]],
13255
+ failedRequestIgnoreUrls: options.failedRequestIgnoreUrls ?? [],
13256
+ talariaBaseUrl: options.talariaBaseUrl
13257
+ };
13258
+ const handleMeta = (meta) => {
13259
+ emitNetwork(meta);
13260
+ options.onNetwork?.(meta);
13261
+ if (shouldPromoteFailedRequest(meta, matchOpts)) {
13262
+ options.onFailedRequest?.(meta);
13263
+ }
13264
+ };
13224
13265
  if (originalFetch) {
13225
13266
  globalThis.fetch = async (input2, init) => {
13226
13267
  const started = Date.now();
@@ -13228,7 +13269,7 @@
13228
13269
  const url = typeof input2 === "string" ? input2 : input2 instanceof URL ? input2.toString() : input2.url;
13229
13270
  try {
13230
13271
  const response = await originalFetch(input2, init);
13231
- emitNetwork({
13272
+ handleMeta({
13232
13273
  method,
13233
13274
  url,
13234
13275
  status: response.status,
@@ -13237,7 +13278,7 @@
13237
13278
  });
13238
13279
  return response;
13239
13280
  } catch (error) {
13240
- emitNetwork({
13281
+ handleMeta({
13241
13282
  method,
13242
13283
  url,
13243
13284
  durationMs: Date.now() - started,
@@ -13256,7 +13297,7 @@
13256
13297
  XHR.prototype.send = function(body) {
13257
13298
  this.__talariaStarted = Date.now();
13258
13299
  const onDone = () => {
13259
- emitNetwork({
13300
+ handleMeta({
13260
13301
  method: this.__talariaMethod ?? "GET",
13261
13302
  url: this.__talariaUrl ?? "",
13262
13303
  status: this.status,
@@ -13300,7 +13341,10 @@
13300
13341
  blockSelector: options.blockSelector ?? "",
13301
13342
  userId: options.userId,
13302
13343
  tags: options.tags,
13303
- disableDefaultIntegrations: options.disableDefaultIntegrations ?? false
13344
+ disableDefaultIntegrations: options.disableDefaultIntegrations ?? false,
13345
+ captureFailedRequests: options.captureFailedRequests ?? true,
13346
+ failedRequestStatusCodes: options.failedRequestStatusCodes ?? [[500, 599]],
13347
+ failedRequestIgnoreUrls: options.failedRequestIgnoreUrls ?? []
13304
13348
  };
13305
13349
  }
13306
13350
  function clamp01(n2) {
@@ -13388,7 +13432,35 @@
13388
13432
  blockSelector: this.options.blockSelector,
13389
13433
  onEvent: (event) => this.onRrwebEvent(event)
13390
13434
  });
13391
- this.teardowns.push(installConsoleHook(), installNetworkHook());
13435
+ this.teardowns.push(
13436
+ installConsoleHook(),
13437
+ installNetworkHook({
13438
+ captureFailedRequests: this.options.captureFailedRequests,
13439
+ failedRequestStatusCodes: this.options.failedRequestStatusCodes,
13440
+ failedRequestIgnoreUrls: this.options.failedRequestIgnoreUrls,
13441
+ talariaBaseUrl: this.options.baseUrl,
13442
+ onFailedRequest: (meta) => {
13443
+ const status = meta.status;
13444
+ const method = meta.method || "GET";
13445
+ const url = meta.url || "(unknown url)";
13446
+ void this.captureMessage(
13447
+ `HTTP ${status}: ${method} ${url}`,
13448
+ status >= 500 ? "error" : "warning",
13449
+ {
13450
+ tags: {
13451
+ "http.status_code": String(status),
13452
+ "http.method": method
13453
+ },
13454
+ extra: {
13455
+ url,
13456
+ durationMs: meta.durationMs,
13457
+ ok: meta.ok
13458
+ }
13459
+ }
13460
+ );
13461
+ }
13462
+ })
13463
+ );
13392
13464
  if (!this.options.disableDefaultIntegrations) {
13393
13465
  this.installGlobalHandlers();
13394
13466
  }