aissue 0.0.0-stage → 2.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.
Files changed (82) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +390 -2
  3. package/dist/cjs/browser.d.ts +16 -0
  4. package/dist/cjs/browser.js +80 -0
  5. package/dist/cjs/core/breadcrumbs.d.ts +48 -0
  6. package/dist/cjs/core/breadcrumbs.js +313 -0
  7. package/dist/cjs/core/client.d.ts +54 -0
  8. package/dist/cjs/core/client.js +293 -0
  9. package/dist/cjs/core/env.d.ts +19 -0
  10. package/dist/cjs/core/env.js +71 -0
  11. package/dist/cjs/core/event.d.ts +35 -0
  12. package/dist/cjs/core/event.js +187 -0
  13. package/dist/cjs/core/hub.d.ts +20 -0
  14. package/dist/cjs/core/hub.js +21 -0
  15. package/dist/cjs/core/instrument-browser.d.ts +16 -0
  16. package/dist/cjs/core/instrument-browser.js +208 -0
  17. package/dist/cjs/core/instrument-node.d.ts +5 -0
  18. package/dist/cjs/core/instrument-node.js +250 -0
  19. package/dist/cjs/core/instrument.d.ts +12 -0
  20. package/dist/cjs/core/instrument.js +137 -0
  21. package/dist/cjs/core/privacy.d.ts +25 -0
  22. package/dist/cjs/core/privacy.js +269 -0
  23. package/dist/cjs/core/stack.d.ts +13 -0
  24. package/dist/cjs/core/stack.js +147 -0
  25. package/dist/cjs/core/transport.d.ts +31 -0
  26. package/dist/cjs/core/transport.js +154 -0
  27. package/dist/cjs/core/types.d.ts +149 -0
  28. package/dist/cjs/core/types.js +2 -0
  29. package/dist/cjs/core/version.d.ts +2 -0
  30. package/dist/cjs/core/version.js +6 -0
  31. package/dist/cjs/index.d.ts +27 -0
  32. package/dist/cjs/index.js +83 -0
  33. package/dist/cjs/nextjs/client.d.ts +10 -0
  34. package/dist/cjs/nextjs/client.js +99 -0
  35. package/dist/cjs/nextjs/index.d.ts +31 -0
  36. package/dist/cjs/nextjs/index.js +99 -0
  37. package/dist/cjs/node.d.ts +48 -0
  38. package/dist/cjs/node.js +223 -0
  39. package/dist/cjs/package.json +3 -0
  40. package/dist/cjs/react.d.ts +40 -0
  41. package/dist/cjs/react.js +90 -0
  42. package/dist/esm/browser.d.ts +16 -0
  43. package/dist/esm/browser.js +71 -0
  44. package/dist/esm/cli.d.ts +2 -0
  45. package/dist/esm/cli.js +386 -0
  46. package/dist/esm/core/breadcrumbs.d.ts +48 -0
  47. package/dist/esm/core/breadcrumbs.js +296 -0
  48. package/dist/esm/core/client.d.ts +54 -0
  49. package/dist/esm/core/client.js +285 -0
  50. package/dist/esm/core/env.d.ts +19 -0
  51. package/dist/esm/core/env.js +62 -0
  52. package/dist/esm/core/event.d.ts +35 -0
  53. package/dist/esm/core/event.js +179 -0
  54. package/dist/esm/core/hub.d.ts +20 -0
  55. package/dist/esm/core/hub.js +17 -0
  56. package/dist/esm/core/instrument-browser.d.ts +16 -0
  57. package/dist/esm/core/instrument-browser.js +203 -0
  58. package/dist/esm/core/instrument-node.d.ts +5 -0
  59. package/dist/esm/core/instrument-node.js +214 -0
  60. package/dist/esm/core/instrument.d.ts +12 -0
  61. package/dist/esm/core/instrument.js +132 -0
  62. package/dist/esm/core/privacy.d.ts +25 -0
  63. package/dist/esm/core/privacy.js +257 -0
  64. package/dist/esm/core/stack.d.ts +13 -0
  65. package/dist/esm/core/stack.js +140 -0
  66. package/dist/esm/core/transport.d.ts +31 -0
  67. package/dist/esm/core/transport.js +149 -0
  68. package/dist/esm/core/types.d.ts +149 -0
  69. package/dist/esm/core/types.js +1 -0
  70. package/dist/esm/core/version.d.ts +2 -0
  71. package/dist/esm/core/version.js +3 -0
  72. package/dist/esm/index.d.ts +27 -0
  73. package/dist/esm/index.js +72 -0
  74. package/dist/esm/nextjs/client.d.ts +10 -0
  75. package/dist/esm/nextjs/client.js +90 -0
  76. package/dist/esm/nextjs/index.d.ts +31 -0
  77. package/dist/esm/nextjs/index.js +88 -0
  78. package/dist/esm/node.d.ts +48 -0
  79. package/dist/esm/node.js +210 -0
  80. package/dist/esm/react.d.ts +40 -0
  81. package/dist/esm/react.js +47 -0
  82. package/package.json +141 -4
@@ -0,0 +1,62 @@
1
+ /** Release env vars, first one set wins (ingestion protocol "Release detection"). */
2
+ export const RELEASE_ENV_VARS = [
3
+ 'AISSUE_RELEASE',
4
+ 'VERCEL_GIT_COMMIT_SHA',
5
+ 'NEXT_PUBLIC_VERCEL_GIT_COMMIT_SHA',
6
+ 'RENDER_GIT_COMMIT',
7
+ 'RAILWAY_GIT_COMMIT_SHA',
8
+ 'HEROKU_SLUG_COMMIT',
9
+ 'SOURCE_VERSION',
10
+ 'CF_PAGES_COMMIT_SHA',
11
+ 'GITHUB_SHA',
12
+ 'COMMIT_SHA',
13
+ 'GIT_SHA',
14
+ 'GIT_COMMIT',
15
+ ];
16
+ /** The global `process`, if any (looked up dynamically so bundlers and edge runtimes don't complain). */
17
+ export function getProcess() {
18
+ const p = globalThis.process;
19
+ return p && typeof p === 'object' ? p : undefined;
20
+ }
21
+ /** Read a server-side environment variable (trimmed; undefined when empty). */
22
+ export function readEnv(name) {
23
+ try {
24
+ const value = getProcess()?.env?.[name];
25
+ if (typeof value !== 'string')
26
+ return undefined;
27
+ const trimmed = value.trim();
28
+ return trimmed ? trimmed : undefined;
29
+ }
30
+ catch {
31
+ return undefined;
32
+ }
33
+ }
34
+ export function detectRelease() {
35
+ for (const name of RELEASE_ENV_VARS) {
36
+ const value = readEnv(name);
37
+ if (value)
38
+ return value.slice(0, 100);
39
+ }
40
+ return undefined;
41
+ }
42
+ /**
43
+ * NODE_ENV. `process.env.NODE_ENV` is referenced literally so bundlers (webpack, Next.js, Vite)
44
+ * can inline it in browser builds; a missing `process` global is caught.
45
+ */
46
+ export function nodeEnv() {
47
+ try {
48
+ const value = process.env.NODE_ENV;
49
+ return typeof value === 'string' && value ? value : undefined;
50
+ }
51
+ catch {
52
+ return undefined;
53
+ }
54
+ }
55
+ export function isBrowser() {
56
+ const g = globalThis;
57
+ return typeof g.window === 'object' && g.window !== null && typeof g.window.document === 'object';
58
+ }
59
+ export function isNode() {
60
+ const versions = getProcess()?.versions;
61
+ return !!versions && typeof versions.node === 'string';
62
+ }
@@ -0,0 +1,35 @@
1
+ import type { AIssueEvent, CaptureContext, HeadersInput, Platform } from './types.js';
2
+ export declare const MAX_MESSAGE_CHARS = 2000;
3
+ export declare const MAX_TRACEBACK_CHARS = 20000;
4
+ /** The protocol limit is 256 KB; keep a little headroom. */
5
+ export declare const MAX_BODY_BYTES: number;
6
+ export interface NormalizedError {
7
+ type: string;
8
+ message: string;
9
+ stack?: string;
10
+ }
11
+ /** Turn anything that was thrown into {type, message, stack}. Never throws. */
12
+ export declare function normalizeError(value: unknown): NormalizedError;
13
+ /** HTTP status attached to an error (err.status / err.statusCode), or `fallback`. */
14
+ export declare function statusFromError(error: unknown, fallback?: number): number;
15
+ export interface EventDefaults {
16
+ platform: Platform;
17
+ errorCode: number;
18
+ release?: string;
19
+ environment?: string;
20
+ userId?: string;
21
+ path?: string;
22
+ headers?: HeadersInput;
23
+ }
24
+ export interface BuildExtras {
25
+ /** Appended to the traceback text (e.g. the React component stack). */
26
+ tracebackSuffix?: string;
27
+ }
28
+ /** Build a protocol event from a thrown value plus context. Applies all privacy filtering. */
29
+ export declare function buildEvent(error: unknown, context: CaptureContext | undefined, defaults: EventDefaults, extras?: BuildExtras): AIssueEvent;
30
+ export declare function byteLength(text: string): number;
31
+ /**
32
+ * JSON body for the event, trimmed to fit the 256 KB limit (fewer frames, shorter traceback).
33
+ * Returns null when it still does not fit or cannot be serialised.
34
+ */
35
+ export declare function serializeEvent(event: AIssueEvent, maxBytes?: number): string | null;
@@ -0,0 +1,179 @@
1
+ import { filterHeaders, filterQuery, queryOf, scrub, stripQuery, truncate } from './privacy.js';
2
+ import { isV8Stack, parseStack } from './stack.js';
3
+ import { SDK_NAME, SDK_VERSION } from './version.js';
4
+ export const MAX_MESSAGE_CHARS = 2000;
5
+ export const MAX_TRACEBACK_CHARS = 20000;
6
+ /** The protocol limit is 256 KB; keep a little headroom. */
7
+ export const MAX_BODY_BYTES = 256 * 1000;
8
+ function isErrorLike(value) {
9
+ if (value instanceof Error)
10
+ return true;
11
+ if (typeof value !== 'object' || value === null)
12
+ return false;
13
+ const v = value;
14
+ return typeof v.message === 'string' && (typeof v.stack === 'string' || typeof v.name === 'string');
15
+ }
16
+ function describeNonError(value) {
17
+ if (typeof value === 'string')
18
+ return value || 'Non-Error thrown: empty string';
19
+ if (value === null || value === undefined)
20
+ return `Non-Error thrown: ${String(value)}`;
21
+ const type = typeof value;
22
+ if (type === 'number' || type === 'boolean' || type === 'bigint' || type === 'symbol') {
23
+ return `Non-Error thrown: ${String(value)}`;
24
+ }
25
+ if (type === 'function')
26
+ return 'Non-Error thrown: function';
27
+ try {
28
+ const proto = Object.getPrototypeOf(value);
29
+ if (proto === Object.prototype || proto === null) {
30
+ const keys = Object.keys(value).slice(0, 10);
31
+ return keys.length ? `Non-Error object thrown with keys: ${keys.join(', ')}` : 'Non-Error object thrown';
32
+ }
33
+ const ctor = value.constructor;
34
+ const name = ctor && typeof ctor.name === 'string' && ctor.name ? ctor.name : Object.prototype.toString.call(value);
35
+ const eventType = value.type;
36
+ return typeof eventType === 'string' && 'isTrusted' in value
37
+ ? `Non-Error ${name} thrown (type: ${eventType})`
38
+ : `Non-Error ${name} thrown`;
39
+ }
40
+ catch {
41
+ return 'Non-Error value thrown';
42
+ }
43
+ }
44
+ /** Turn anything that was thrown into {type, message, stack}. Never throws. */
45
+ export function normalizeError(value) {
46
+ try {
47
+ if (isErrorLike(value)) {
48
+ const name = typeof value.name === 'string' && value.name ? value.name : 'Error';
49
+ const message = typeof value.message === 'string' ? value.message : String(value.message);
50
+ let stack;
51
+ try {
52
+ stack = typeof value.stack === 'string' ? value.stack : undefined;
53
+ }
54
+ catch {
55
+ stack = undefined;
56
+ }
57
+ return { type: truncate(name, 200), message: truncate(message, MAX_MESSAGE_CHARS), stack };
58
+ }
59
+ }
60
+ catch {
61
+ // fall through to the generic description
62
+ }
63
+ return { type: 'Error', message: truncate(describeNonError(value), MAX_MESSAGE_CHARS) };
64
+ }
65
+ /** HTTP status attached to an error (err.status / err.statusCode), or `fallback`. */
66
+ export function statusFromError(error, fallback = 500) {
67
+ if (error && typeof error === 'object') {
68
+ const e = error;
69
+ for (const candidate of [e.status, e.statusCode]) {
70
+ const n = typeof candidate === 'string' ? parseInt(candidate, 10) : candidate;
71
+ if (typeof n === 'number' && Number.isInteger(n) && n >= 100 && n <= 599)
72
+ return n;
73
+ }
74
+ }
75
+ return fallback;
76
+ }
77
+ /** Build a protocol event from a thrown value plus context. Applies all privacy filtering. */
78
+ export function buildEvent(error, context = {}, defaults, extras = {}) {
79
+ const err = normalizeError(error);
80
+ const frames = parseStack(err.stack);
81
+ let traceback;
82
+ if (err.stack) {
83
+ traceback = isV8Stack(err.stack) ? err.stack : `${err.type}: ${err.message}\n${err.stack}`;
84
+ }
85
+ else {
86
+ traceback = `${err.type}: ${err.message}`;
87
+ }
88
+ if (extras.tracebackSuffix)
89
+ traceback += extras.tracebackSuffix;
90
+ traceback = truncate(traceback, MAX_TRACEBACK_CHARS);
91
+ const event = {
92
+ error_code: typeof context.errorCode === 'number' && Number.isFinite(context.errorCode)
93
+ ? Math.trunc(context.errorCode)
94
+ : defaults.errorCode,
95
+ traceback,
96
+ exception: { type: err.type, message: err.message },
97
+ frames,
98
+ platform: defaults.platform,
99
+ sdk: { name: SDK_NAME, version: SDK_VERSION },
100
+ };
101
+ const rawPath = typeof context.path === 'string' ? context.path : defaults.path;
102
+ if (rawPath)
103
+ event.path = truncate(stripQuery(rawPath), 2048);
104
+ if (typeof context.method === 'string' && context.method)
105
+ event.method = context.method.toUpperCase().slice(0, 16);
106
+ if (defaults.release)
107
+ event.release = defaults.release;
108
+ if (defaults.environment)
109
+ event.environment = defaults.environment;
110
+ const userId = context.userId !== undefined && context.userId !== null ? String(context.userId) : defaults.userId;
111
+ if (userId)
112
+ event.user_id = userId.slice(0, 200);
113
+ const query = context.query !== undefined ? context.query : rawPath ? queryOf(rawPath) : undefined;
114
+ const GET = query ? filterQuery(query) : {};
115
+ const requestData = {};
116
+ if (Object.keys(GET).length)
117
+ requestData.GET = GET;
118
+ if (context.extra && typeof context.extra === 'object') {
119
+ const extra = scrub(context.extra);
120
+ if (extra && Object.keys(extra).length)
121
+ requestData.context = extra;
122
+ }
123
+ if (requestData.GET || requestData.context)
124
+ event.request_data = requestData;
125
+ const headers = filterHeaders(context.headers !== undefined ? context.headers : defaults.headers);
126
+ if (Object.keys(headers).length)
127
+ event.headers = headers;
128
+ return event;
129
+ }
130
+ let encoder;
131
+ export function byteLength(text) {
132
+ try {
133
+ encoder = encoder || new TextEncoder();
134
+ return encoder.encode(text).length;
135
+ }
136
+ catch {
137
+ return text.length * 3;
138
+ }
139
+ }
140
+ /**
141
+ * JSON body for the event, trimmed to fit the 256 KB limit (fewer frames, shorter traceback).
142
+ * Returns null when it still does not fit or cannot be serialised.
143
+ */
144
+ export function serializeEvent(event, maxBytes = MAX_BODY_BYTES) {
145
+ try {
146
+ let body = JSON.stringify(event);
147
+ if (byteLength(body) <= maxBytes)
148
+ return body;
149
+ const trimmed = {
150
+ ...event,
151
+ frames: (event.frames || []).slice(-20),
152
+ traceback: truncate(String(event.traceback || ''), 4000),
153
+ };
154
+ if (Array.isArray(event.breadcrumbs))
155
+ trimmed.breadcrumbs = event.breadcrumbs.slice(-20);
156
+ body = JSON.stringify(trimmed);
157
+ if (byteLength(body) <= maxBytes)
158
+ return body;
159
+ delete trimmed.breadcrumbs;
160
+ trimmed.frames = trimmed.frames.slice(-5);
161
+ trimmed.traceback = truncate(trimmed.traceback, 1000);
162
+ trimmed.exception = {
163
+ type: truncate(String(event.exception?.type ?? 'Error'), 200),
164
+ message: truncate(String(event.exception?.message ?? ''), 1000),
165
+ };
166
+ if (trimmed.request_data) {
167
+ trimmed.request_data = trimmed.request_data.GET ? { GET: trimmed.request_data.GET } : undefined;
168
+ }
169
+ body = JSON.stringify(trimmed);
170
+ if (byteLength(body) <= maxBytes)
171
+ return body;
172
+ delete trimmed.request_data;
173
+ body = JSON.stringify(trimmed);
174
+ return byteLength(body) <= maxBytes ? body : null;
175
+ }
176
+ catch {
177
+ return null;
178
+ }
179
+ }
@@ -0,0 +1,20 @@
1
+ import type { BreadcrumbStorage } from './breadcrumbs.js';
2
+ import type { Client } from './client.js';
3
+ import type { Breadcrumb } from './types.js';
4
+ /**
5
+ * Process / page wide SDK state. Kept on globalThis so that the ESM and CommonJS builds
6
+ * (and duplicated copies inside bundles) share one client, one rate limiter and one set
7
+ * of global handlers.
8
+ */
9
+ export interface Hub {
10
+ client?: Client;
11
+ userId?: string;
12
+ installed: Record<string, boolean>;
13
+ /** Global breadcrumb buffer (used when there is no per-request buffer). */
14
+ breadcrumbs?: Breadcrumb[];
15
+ /** Node: AsyncLocalStorage holding the current request's breadcrumbs. */
16
+ breadcrumbStorage?: BreadcrumbStorage;
17
+ }
18
+ export declare function getHub(): Hub;
19
+ /** Test helper: forget the client, user and global breadcrumbs (global handlers stay installed). */
20
+ export declare function resetHub(): void;
@@ -0,0 +1,17 @@
1
+ const HUB_KEY = Symbol.for('aissue.hub.v1');
2
+ export function getHub() {
3
+ const g = globalThis;
4
+ let hub = g[HUB_KEY];
5
+ if (!hub) {
6
+ hub = { installed: {}, breadcrumbs: [] };
7
+ g[HUB_KEY] = hub;
8
+ }
9
+ return hub;
10
+ }
11
+ /** Test helper: forget the client, user and global breadcrumbs (global handlers stay installed). */
12
+ export function resetHub() {
13
+ const hub = getHub();
14
+ hub.client = undefined;
15
+ hub.userId = undefined;
16
+ hub.breadcrumbs = [];
17
+ }
@@ -0,0 +1,16 @@
1
+ interface ElementLike {
2
+ tagName?: unknown;
3
+ id?: unknown;
4
+ className?: unknown;
5
+ textContent?: unknown;
6
+ isContentEditable?: unknown;
7
+ getAttribute?: (name: string) => string | null;
8
+ closest?: (selector: string) => ElementLike | null;
9
+ }
10
+ /** `button#pay.btn.primary`, `input[type="submit"]`: tag, id and up to two classes. */
11
+ export declare function elementSelector(el: ElementLike): string;
12
+ /** Visible label of a clicked element: aria-label or text, never the value of a form field. */
13
+ export declare function elementText(el: ElementLike): string;
14
+ /** Install every browser breadcrumb source (idempotent). */
15
+ export declare function instrumentBrowser(): void;
16
+ export {};
@@ -0,0 +1,203 @@
1
+ /**
2
+ * Browser-only automatic breadcrumbs: clicks, XMLHttpRequest and history navigation.
3
+ * Same guarantees as ./instrument.ts: idempotent, never throws, original behaviour kept,
4
+ * nothing recorded while the SDK is disabled.
5
+ */
6
+ import { breadcrumbsActive, clip, currentBuffer, isAIssueUrl, recordBreadcrumb, wrapMethod } from './breadcrumbs.js';
7
+ import { getHub } from './hub.js';
8
+ import { httpBreadcrumb, instrumentConsole, instrumentFetch } from './instrument.js';
9
+ import { sanitizeUrl, stripQuery } from './privacy.js';
10
+ const INTERACTIVE = 'button,a,input,select,textarea,label,summary,[role="button"],[role="link"],[role="menuitem"],[role="tab"],[role="checkbox"],[role="option"]';
11
+ const NO_TEXT_TAGS = new Set(['input', 'textarea', 'select', 'option']);
12
+ const MAX_CLICK_TEXT = 60;
13
+ function attr(el, name) {
14
+ try {
15
+ const value = typeof el.getAttribute === 'function' ? el.getAttribute(name) : null;
16
+ return typeof value === 'string' ? value : '';
17
+ }
18
+ catch {
19
+ return '';
20
+ }
21
+ }
22
+ function cssIdent(value) {
23
+ return value.replace(/[^A-Za-z0-9_-]/g, '').slice(0, 40);
24
+ }
25
+ /** `button#pay.btn.primary`, `input[type="submit"]`: tag, id and up to two classes. */
26
+ export function elementSelector(el) {
27
+ const tag = (typeof el.tagName === 'string' ? el.tagName : 'element').toLowerCase();
28
+ let selector = tag;
29
+ const id = typeof el.id === 'string' ? cssIdent(el.id) : '';
30
+ if (id)
31
+ selector += `#${id}`;
32
+ const rawClass = typeof el.className === 'string' ? el.className : attr(el, 'class');
33
+ const classes = rawClass
34
+ .split(/\s+/)
35
+ .map(cssIdent)
36
+ .filter(Boolean)
37
+ .slice(0, 2);
38
+ for (const c of classes)
39
+ selector += `.${c}`;
40
+ if (tag === 'input') {
41
+ const type = cssIdent(attr(el, 'type'));
42
+ if (type)
43
+ selector += `[type="${type}"]`;
44
+ }
45
+ return clip(selector, 120);
46
+ }
47
+ /** Visible label of a clicked element: aria-label or text, never the value of a form field. */
48
+ export function elementText(el) {
49
+ const tag = (typeof el.tagName === 'string' ? el.tagName : '').toLowerCase();
50
+ const label = attr(el, 'aria-label');
51
+ let text = label;
52
+ if (!text && !NO_TEXT_TAGS.has(tag) && el.isContentEditable !== true) {
53
+ text = typeof el.textContent === 'string' ? el.textContent : '';
54
+ }
55
+ return clip(text.replace(/\s+/g, ' ').trim(), MAX_CLICK_TEXT);
56
+ }
57
+ function onClick(event) {
58
+ try {
59
+ if (!breadcrumbsActive('clicks'))
60
+ return;
61
+ let el = event && event.target;
62
+ if (!el || typeof el !== 'object' || typeof el.tagName !== 'string')
63
+ return;
64
+ try {
65
+ const interactive = typeof el.closest === 'function' ? el.closest(INTERACTIVE) : null;
66
+ if (interactive)
67
+ el = interactive;
68
+ }
69
+ catch {
70
+ // keep the original target
71
+ }
72
+ const selector = elementSelector(el);
73
+ const text = elementText(el);
74
+ recordBreadcrumb({
75
+ type: 'ui',
76
+ category: 'ui.click',
77
+ level: 'info',
78
+ message: text ? `${selector} "${text}"` : selector,
79
+ data: text ? { selector, text } : { selector },
80
+ });
81
+ }
82
+ catch {
83
+ // never throw into the host app
84
+ }
85
+ }
86
+ const XHR_INFO = Symbol.for('aissue.xhr');
87
+ function instrumentXhr() {
88
+ const Xhr = globalThis.XMLHttpRequest;
89
+ const proto = Xhr && Xhr.prototype;
90
+ if (!proto)
91
+ return;
92
+ wrapMethod(proto, 'open', (original) => function aissueXhrOpen(...args) {
93
+ try {
94
+ const method = typeof args[0] === 'string' ? args[0] : 'GET';
95
+ const rawUrl = args[1];
96
+ const url = typeof rawUrl === 'string' ? rawUrl : rawUrl && typeof rawUrl.href === 'string' ? rawUrl.href : '';
97
+ this[XHR_INFO] = url && !isAIssueUrl(url) ? { method: method.toUpperCase().slice(0, 16), url: sanitizeUrl(url) } : undefined;
98
+ }
99
+ catch {
100
+ // ignore
101
+ }
102
+ return original.apply(this, args);
103
+ });
104
+ wrapMethod(proto, 'send', (original) => function aissueXhrSend(...args) {
105
+ try {
106
+ const base = this[XHR_INFO];
107
+ if (base && breadcrumbsActive('xhr') && typeof this.addEventListener === 'function') {
108
+ const info = { ...base, start: Date.now() };
109
+ const buffer = currentBuffer();
110
+ const xhr = this;
111
+ // Listening to loadend does not change how the request behaves.
112
+ this.addEventListener('loadend', function onLoadEnd() {
113
+ try {
114
+ const status = typeof xhr.status === 'number' ? xhr.status : 0;
115
+ httpBreadcrumb('xhr', info, status || undefined, buffer, status ? undefined : 'NetworkError');
116
+ }
117
+ catch {
118
+ // ignore
119
+ }
120
+ });
121
+ }
122
+ }
123
+ catch {
124
+ // ignore
125
+ }
126
+ return original.apply(this, args);
127
+ });
128
+ }
129
+ let lastPath;
130
+ function currentPath() {
131
+ try {
132
+ const loc = globalThis.location;
133
+ return loc && typeof loc.pathname === 'string' ? stripQuery(loc.pathname) : undefined;
134
+ }
135
+ catch {
136
+ return undefined;
137
+ }
138
+ }
139
+ function navigated() {
140
+ try {
141
+ const to = currentPath();
142
+ const from = lastPath;
143
+ lastPath = to;
144
+ if (!to || from === to || !breadcrumbsActive('navigation'))
145
+ return;
146
+ recordBreadcrumb({
147
+ type: 'navigation',
148
+ category: 'navigation',
149
+ level: 'info',
150
+ message: `${from ?? ''} -> ${to}`,
151
+ data: from !== undefined ? { from, to } : { to },
152
+ });
153
+ }
154
+ catch {
155
+ // never throw into the host app
156
+ }
157
+ }
158
+ function instrumentHistory(w) {
159
+ const history = globalThis.history ?? w.history;
160
+ if (lastPath === undefined)
161
+ lastPath = currentPath();
162
+ for (const method of ['pushState', 'replaceState']) {
163
+ wrapMethod(history, method, (original) => function aissueHistory(...args) {
164
+ const result = original.apply(this, args);
165
+ navigated();
166
+ return result;
167
+ });
168
+ }
169
+ const installed = getHub().installed;
170
+ if (!installed.popstate && typeof w.addEventListener === 'function') {
171
+ installed.popstate = true;
172
+ w.addEventListener('popstate', navigated);
173
+ }
174
+ }
175
+ /** Install every browser breadcrumb source (idempotent). */
176
+ export function instrumentBrowser() {
177
+ const w = globalThis.window;
178
+ if (!w)
179
+ return;
180
+ const steps = [
181
+ instrumentConsole,
182
+ instrumentFetch,
183
+ instrumentXhr,
184
+ () => instrumentHistory(w),
185
+ () => {
186
+ const doc = w.document;
187
+ const installed = getHub().installed;
188
+ if (!installed.clicks && doc && typeof doc.addEventListener === 'function') {
189
+ installed.clicks = true;
190
+ // Capture phase: still seen when the app calls stopPropagation().
191
+ doc.addEventListener('click', onClick, true);
192
+ }
193
+ },
194
+ ];
195
+ for (const step of steps) {
196
+ try {
197
+ step();
198
+ }
199
+ catch {
200
+ // one failing source must not stop the others
201
+ }
202
+ }
203
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Install Node breadcrumbs (idempotent). Synchronous on Node 20.16+/22.3+ (process.getBuiltinModule);
3
+ * on older versions the built-ins are imported first, so installation completes a moment later.
4
+ */
5
+ export declare function instrumentNode(): void;