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