@temps-sdk/node-sdk 0.0.1-alpha.3 → 0.0.1-alpha.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.
- package/dist/client/client/client.gen.d.ts +2 -0
- package/dist/client/client/client.gen.js +217 -0
- package/dist/client/client/index.d.ts +7 -0
- package/dist/client/client/index.js +5 -0
- package/dist/client/client/types.gen.d.ts +124 -0
- package/dist/client/client/types.gen.js +2 -0
- package/dist/client/client/utils.gen.d.ts +38 -0
- package/dist/client/client/utils.gen.js +229 -0
- package/dist/client/client.gen.d.ts +12 -0
- package/dist/client/client.gen.js +5 -0
- package/dist/client/core/auth.gen.d.ts +18 -0
- package/dist/client/core/auth.gen.js +14 -0
- package/dist/client/core/bodySerializer.gen.d.ts +17 -0
- package/dist/client/core/bodySerializer.gen.js +57 -0
- package/dist/client/core/params.gen.d.ts +33 -0
- package/dist/client/core/params.gen.js +88 -0
- package/dist/client/core/pathSerializer.gen.d.ts +33 -0
- package/dist/client/core/pathSerializer.gen.js +114 -0
- package/dist/client/core/serverSentEvents.gen.d.ts +71 -0
- package/dist/client/core/serverSentEvents.gen.js +135 -0
- package/dist/client/core/types.gen.d.ts +78 -0
- package/dist/client/core/types.gen.js +2 -0
- package/dist/client/core/utils.gen.d.ts +14 -0
- package/dist/client/core/utils.gen.js +69 -0
- package/dist/client/index.d.ts +2 -0
- package/dist/client/index.js +3 -0
- package/dist/client/sdk.gen.d.ts +1323 -0
- package/dist/client/sdk.gen.js +4659 -0
- package/dist/client/types.gen.d.ts +16792 -0
- package/dist/client/types.gen.js +2 -0
- package/dist/errors/client.d.ts +46 -0
- package/dist/errors/client.js +261 -0
- package/dist/errors/hub.d.ts +22 -0
- package/dist/errors/hub.js +102 -0
- package/dist/errors/index.d.ts +35 -0
- package/dist/errors/index.js +167 -0
- package/dist/errors/scope.d.ts +23 -0
- package/dist/errors/scope.js +82 -0
- package/dist/errors/stacktrace.d.ts +5 -0
- package/dist/errors/stacktrace.js +113 -0
- package/dist/errors/transaction.d.ts +57 -0
- package/dist/errors/transaction.js +163 -0
- package/dist/errors/transport.d.ts +23 -0
- package/dist/errors/transport.js +72 -0
- package/dist/errors/types.d.ts +200 -0
- package/dist/errors/types.js +1 -0
- package/dist/index.d.ts +4796 -0
- package/dist/index.js +595 -0
- package/package.json +6 -4
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
export class Scope {
|
|
2
|
+
user = null;
|
|
3
|
+
tags = {};
|
|
4
|
+
extra = {};
|
|
5
|
+
contexts = {};
|
|
6
|
+
level;
|
|
7
|
+
breadcrumbs = [];
|
|
8
|
+
maxBreadcrumbs = 100;
|
|
9
|
+
constructor(maxBreadcrumbs = 100) {
|
|
10
|
+
this.maxBreadcrumbs = maxBreadcrumbs;
|
|
11
|
+
}
|
|
12
|
+
setUser(user) {
|
|
13
|
+
this.user = user;
|
|
14
|
+
}
|
|
15
|
+
setTag(key, value) {
|
|
16
|
+
this.tags[key] = value;
|
|
17
|
+
}
|
|
18
|
+
setTags(tags) {
|
|
19
|
+
this.tags = { ...this.tags, ...tags };
|
|
20
|
+
}
|
|
21
|
+
setExtra(key, value) {
|
|
22
|
+
this.extra[key] = value;
|
|
23
|
+
}
|
|
24
|
+
setExtras(extras) {
|
|
25
|
+
this.extra = { ...this.extra, ...extras };
|
|
26
|
+
}
|
|
27
|
+
setContext(key, context) {
|
|
28
|
+
if (context === null) {
|
|
29
|
+
delete this.contexts[key];
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
this.contexts[key] = context;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
setLevel(level) {
|
|
36
|
+
this.level = level;
|
|
37
|
+
}
|
|
38
|
+
addBreadcrumb(breadcrumb) {
|
|
39
|
+
const mergedBreadcrumb = {
|
|
40
|
+
timestamp: Date.now(),
|
|
41
|
+
...breadcrumb,
|
|
42
|
+
};
|
|
43
|
+
this.breadcrumbs.push(mergedBreadcrumb);
|
|
44
|
+
if (this.breadcrumbs.length > this.maxBreadcrumbs) {
|
|
45
|
+
this.breadcrumbs.shift();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
clearBreadcrumbs() {
|
|
49
|
+
this.breadcrumbs = [];
|
|
50
|
+
}
|
|
51
|
+
clear() {
|
|
52
|
+
this.user = null;
|
|
53
|
+
this.tags = {};
|
|
54
|
+
this.extra = {};
|
|
55
|
+
this.contexts = {};
|
|
56
|
+
this.level = undefined;
|
|
57
|
+
this.breadcrumbs = [];
|
|
58
|
+
}
|
|
59
|
+
applyToEvent(event) {
|
|
60
|
+
if (this.user) {
|
|
61
|
+
event.user = { ...this.user, ...event.user };
|
|
62
|
+
}
|
|
63
|
+
event.tags = { ...(event.tags || {}), ...this.tags };
|
|
64
|
+
event.extra = { ...(event.extra || {}), ...this.extra };
|
|
65
|
+
event.contexts = { ...(event.contexts || {}), ...this.contexts };
|
|
66
|
+
if (this.level && !event.level) {
|
|
67
|
+
event.level = this.level;
|
|
68
|
+
}
|
|
69
|
+
event.breadcrumbs = [...this.breadcrumbs, ...(event.breadcrumbs || [])];
|
|
70
|
+
return event;
|
|
71
|
+
}
|
|
72
|
+
clone() {
|
|
73
|
+
const newScope = new Scope(this.maxBreadcrumbs);
|
|
74
|
+
newScope.user = this.user ? { ...this.user } : null;
|
|
75
|
+
newScope.tags = { ...this.tags };
|
|
76
|
+
newScope.extra = { ...this.extra };
|
|
77
|
+
newScope.contexts = { ...this.contexts };
|
|
78
|
+
newScope.level = this.level;
|
|
79
|
+
newScope.breadcrumbs = [...this.breadcrumbs];
|
|
80
|
+
return newScope;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { StackFrame, Exception } from './types.js';
|
|
2
|
+
export declare function parseStackTrace(error: Error): StackFrame[];
|
|
3
|
+
export declare function exceptionFromError(error: Error): Exception;
|
|
4
|
+
export declare function extractErrorMessage(error: unknown): string;
|
|
5
|
+
export declare function isError(error: unknown): error is Error;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
const STACKTRACE_LIMIT = 50;
|
|
2
|
+
export function parseStackTrace(error) {
|
|
3
|
+
if (!error.stack) {
|
|
4
|
+
return [];
|
|
5
|
+
}
|
|
6
|
+
const lines = error.stack.split('\n');
|
|
7
|
+
const frames = [];
|
|
8
|
+
for (const line of lines) {
|
|
9
|
+
if (line.includes('at ')) {
|
|
10
|
+
const parsed = parseStackLine(line);
|
|
11
|
+
if (parsed) {
|
|
12
|
+
frames.push(createStackFrame(parsed));
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
if (frames.length >= STACKTRACE_LIMIT) {
|
|
16
|
+
break;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return frames.reverse();
|
|
20
|
+
}
|
|
21
|
+
function parseStackLine(line) {
|
|
22
|
+
// Try eval pattern first since it's more specific
|
|
23
|
+
const evalPattern = /^\s*at eval \(eval at (.*?) \((.*?):(\d+):(\d+)\), .*?\)$/;
|
|
24
|
+
let match = line.match(evalPattern);
|
|
25
|
+
if (match) {
|
|
26
|
+
return {
|
|
27
|
+
function: `eval at ${match[1]}`,
|
|
28
|
+
filename: match[2],
|
|
29
|
+
lineno: parseInt(match[3], 10),
|
|
30
|
+
colno: parseInt(match[4], 10),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
const nodePattern = /^\s*at (?:(.*?) \()?(.*?):(\d+):(\d+)\)?$/;
|
|
34
|
+
const nodePatternWithoutColumn = /^\s*at (?:(.*?) \()?(.*?):(\d+)\)?$/;
|
|
35
|
+
match = line.match(nodePattern);
|
|
36
|
+
if (match) {
|
|
37
|
+
return {
|
|
38
|
+
function: match[1] || '<anonymous>',
|
|
39
|
+
filename: match[2],
|
|
40
|
+
lineno: parseInt(match[3], 10),
|
|
41
|
+
colno: parseInt(match[4], 10),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
match = line.match(nodePatternWithoutColumn);
|
|
45
|
+
if (match) {
|
|
46
|
+
return {
|
|
47
|
+
function: match[1] || '<anonymous>',
|
|
48
|
+
filename: match[2],
|
|
49
|
+
lineno: parseInt(match[3], 10),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
function createStackFrame(parsed) {
|
|
55
|
+
const frame = {
|
|
56
|
+
filename: parsed.filename,
|
|
57
|
+
function: parsed.function,
|
|
58
|
+
lineno: parsed.lineno,
|
|
59
|
+
colno: parsed.colno,
|
|
60
|
+
};
|
|
61
|
+
if (parsed.filename) {
|
|
62
|
+
frame.abs_path = parsed.filename;
|
|
63
|
+
const isNodeInternal = parsed.filename.includes('node_modules') ||
|
|
64
|
+
parsed.filename.startsWith('internal/') ||
|
|
65
|
+
parsed.filename.startsWith('node:');
|
|
66
|
+
frame.in_app = !isNodeInternal;
|
|
67
|
+
}
|
|
68
|
+
return frame;
|
|
69
|
+
}
|
|
70
|
+
export function exceptionFromError(error) {
|
|
71
|
+
const exception = {
|
|
72
|
+
type: error.name || 'Error',
|
|
73
|
+
value: error.message,
|
|
74
|
+
stacktrace: {
|
|
75
|
+
frames: error.stack ? parseStackTrace(error) : [],
|
|
76
|
+
},
|
|
77
|
+
mechanism: {
|
|
78
|
+
type: 'generic',
|
|
79
|
+
handled: true,
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
return exception;
|
|
83
|
+
}
|
|
84
|
+
export function extractErrorMessage(error) {
|
|
85
|
+
if (error instanceof Error) {
|
|
86
|
+
return error.message || 'Unknown error';
|
|
87
|
+
}
|
|
88
|
+
if (typeof error === 'string') {
|
|
89
|
+
return error;
|
|
90
|
+
}
|
|
91
|
+
if (error && typeof error === 'object' && 'message' in error) {
|
|
92
|
+
return String(error.message);
|
|
93
|
+
}
|
|
94
|
+
if (error === null) {
|
|
95
|
+
return 'null';
|
|
96
|
+
}
|
|
97
|
+
if (error === undefined) {
|
|
98
|
+
return 'undefined';
|
|
99
|
+
}
|
|
100
|
+
try {
|
|
101
|
+
return JSON.stringify(error);
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
return String(error);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
export function isError(error) {
|
|
108
|
+
return error instanceof Error ||
|
|
109
|
+
(error !== null &&
|
|
110
|
+
typeof error === 'object' &&
|
|
111
|
+
'message' in error &&
|
|
112
|
+
'stack' in error);
|
|
113
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { Transaction, Span, SpanContext, TransactionContext, Event, Measurements } from './types.js';
|
|
2
|
+
export declare class TransactionImpl implements Transaction {
|
|
3
|
+
private onFinish?;
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly op: string;
|
|
6
|
+
readonly traceId: string;
|
|
7
|
+
readonly spanId: string;
|
|
8
|
+
readonly parentSpanId?: string;
|
|
9
|
+
readonly startTimestamp: number;
|
|
10
|
+
endTimestamp?: number;
|
|
11
|
+
status?: Transaction['status'];
|
|
12
|
+
tags: Record<string, string>;
|
|
13
|
+
data: Record<string, any>;
|
|
14
|
+
sampled?: boolean;
|
|
15
|
+
private children;
|
|
16
|
+
private finished;
|
|
17
|
+
private measurements;
|
|
18
|
+
constructor(context: TransactionContext, onFinish?: ((transaction: Transaction) => void) | undefined);
|
|
19
|
+
finish(): void;
|
|
20
|
+
setStatus(status: Transaction['status']): void;
|
|
21
|
+
setTag(key: string, value: string): void;
|
|
22
|
+
setData(key: string, value: any): void;
|
|
23
|
+
setMeasurement(name: string, value: number, unit?: string): void;
|
|
24
|
+
getMeasurements(): Measurements;
|
|
25
|
+
startChild(spanContext: SpanContext): Span;
|
|
26
|
+
getSpans(): Span[];
|
|
27
|
+
toEvent(): Event;
|
|
28
|
+
private generateTraceId;
|
|
29
|
+
private generateSpanId;
|
|
30
|
+
}
|
|
31
|
+
export declare class SpanImpl implements Span {
|
|
32
|
+
readonly spanId: string;
|
|
33
|
+
readonly traceId: string;
|
|
34
|
+
readonly parentSpanId?: string;
|
|
35
|
+
readonly op: string;
|
|
36
|
+
readonly description?: string;
|
|
37
|
+
readonly startTimestamp: number;
|
|
38
|
+
endTimestamp?: number;
|
|
39
|
+
status?: Transaction['status'];
|
|
40
|
+
tags: Record<string, string>;
|
|
41
|
+
data: Record<string, any>;
|
|
42
|
+
sampled?: boolean;
|
|
43
|
+
private children;
|
|
44
|
+
private finished;
|
|
45
|
+
constructor(context: SpanContext & {
|
|
46
|
+
traceId: string;
|
|
47
|
+
parentSpanId?: string;
|
|
48
|
+
sampled?: boolean;
|
|
49
|
+
});
|
|
50
|
+
finish(): void;
|
|
51
|
+
setStatus(status: Transaction['status']): void;
|
|
52
|
+
setTag(key: string, value: string): void;
|
|
53
|
+
setData(key: string, value: any): void;
|
|
54
|
+
startChild(spanContext: SpanContext): Span;
|
|
55
|
+
getChildren(): Span[];
|
|
56
|
+
private generateSpanId;
|
|
57
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
export class TransactionImpl {
|
|
2
|
+
onFinish;
|
|
3
|
+
name;
|
|
4
|
+
op;
|
|
5
|
+
traceId;
|
|
6
|
+
spanId;
|
|
7
|
+
parentSpanId;
|
|
8
|
+
startTimestamp;
|
|
9
|
+
endTimestamp;
|
|
10
|
+
status;
|
|
11
|
+
tags = {};
|
|
12
|
+
data = {};
|
|
13
|
+
sampled;
|
|
14
|
+
children = [];
|
|
15
|
+
finished = false;
|
|
16
|
+
measurements = {};
|
|
17
|
+
constructor(context, onFinish) {
|
|
18
|
+
this.onFinish = onFinish;
|
|
19
|
+
this.name = context.name;
|
|
20
|
+
this.op = context.op;
|
|
21
|
+
this.traceId = context.traceId || this.generateTraceId();
|
|
22
|
+
this.spanId = this.generateSpanId();
|
|
23
|
+
this.parentSpanId = context.parentSpanId;
|
|
24
|
+
this.startTimestamp = Date.now() / 1000;
|
|
25
|
+
this.sampled = context.parentSampled !== false;
|
|
26
|
+
if (context.tags) {
|
|
27
|
+
this.tags = { ...context.tags };
|
|
28
|
+
}
|
|
29
|
+
if (context.data) {
|
|
30
|
+
this.data = { ...context.data };
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
finish() {
|
|
34
|
+
if (this.finished) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
this.endTimestamp = Date.now() / 1000;
|
|
38
|
+
this.finished = true;
|
|
39
|
+
if (this.onFinish) {
|
|
40
|
+
this.onFinish(this);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
setStatus(status) {
|
|
44
|
+
this.status = status;
|
|
45
|
+
}
|
|
46
|
+
setTag(key, value) {
|
|
47
|
+
this.tags[key] = value;
|
|
48
|
+
}
|
|
49
|
+
setData(key, value) {
|
|
50
|
+
this.data[key] = value;
|
|
51
|
+
}
|
|
52
|
+
setMeasurement(name, value, unit = 'ms') {
|
|
53
|
+
this.measurements[name] = { value, unit };
|
|
54
|
+
}
|
|
55
|
+
getMeasurements() {
|
|
56
|
+
return { ...this.measurements };
|
|
57
|
+
}
|
|
58
|
+
startChild(spanContext) {
|
|
59
|
+
const span = new SpanImpl({
|
|
60
|
+
...spanContext,
|
|
61
|
+
traceId: this.traceId,
|
|
62
|
+
parentSpanId: this.spanId,
|
|
63
|
+
sampled: this.sampled,
|
|
64
|
+
});
|
|
65
|
+
this.children.push(span);
|
|
66
|
+
return span;
|
|
67
|
+
}
|
|
68
|
+
getSpans() {
|
|
69
|
+
return [...this.children];
|
|
70
|
+
}
|
|
71
|
+
toEvent() {
|
|
72
|
+
return {
|
|
73
|
+
type: 'transaction',
|
|
74
|
+
transaction: this.name,
|
|
75
|
+
trace_id: this.traceId,
|
|
76
|
+
span_id: this.spanId,
|
|
77
|
+
parent_span_id: this.parentSpanId,
|
|
78
|
+
start_timestamp: this.startTimestamp,
|
|
79
|
+
timestamp: this.endTimestamp || Date.now() / 1000,
|
|
80
|
+
spans: this.children,
|
|
81
|
+
measurements: this.measurements,
|
|
82
|
+
tags: this.tags,
|
|
83
|
+
extra: this.data,
|
|
84
|
+
contexts: {
|
|
85
|
+
trace: {
|
|
86
|
+
trace_id: this.traceId,
|
|
87
|
+
span_id: this.spanId,
|
|
88
|
+
parent_span_id: this.parentSpanId,
|
|
89
|
+
op: this.op,
|
|
90
|
+
status: this.status,
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
generateTraceId() {
|
|
96
|
+
return Array.from({ length: 32 }, () => Math.floor(Math.random() * 16).toString(16)).join('');
|
|
97
|
+
}
|
|
98
|
+
generateSpanId() {
|
|
99
|
+
return Array.from({ length: 16 }, () => Math.floor(Math.random() * 16).toString(16)).join('');
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
export class SpanImpl {
|
|
103
|
+
spanId;
|
|
104
|
+
traceId;
|
|
105
|
+
parentSpanId;
|
|
106
|
+
op;
|
|
107
|
+
description;
|
|
108
|
+
startTimestamp;
|
|
109
|
+
endTimestamp;
|
|
110
|
+
status;
|
|
111
|
+
tags = {};
|
|
112
|
+
data = {};
|
|
113
|
+
sampled;
|
|
114
|
+
children = [];
|
|
115
|
+
finished = false;
|
|
116
|
+
constructor(context) {
|
|
117
|
+
this.spanId = this.generateSpanId();
|
|
118
|
+
this.traceId = context.traceId;
|
|
119
|
+
this.parentSpanId = context.parentSpanId;
|
|
120
|
+
this.op = context.op;
|
|
121
|
+
this.description = context.description;
|
|
122
|
+
this.startTimestamp = Date.now() / 1000;
|
|
123
|
+
this.sampled = context.sampled;
|
|
124
|
+
if (context.tags) {
|
|
125
|
+
this.tags = { ...context.tags };
|
|
126
|
+
}
|
|
127
|
+
if (context.data) {
|
|
128
|
+
this.data = { ...context.data };
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
finish() {
|
|
132
|
+
if (this.finished) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
this.endTimestamp = Date.now() / 1000;
|
|
136
|
+
this.finished = true;
|
|
137
|
+
}
|
|
138
|
+
setStatus(status) {
|
|
139
|
+
this.status = status;
|
|
140
|
+
}
|
|
141
|
+
setTag(key, value) {
|
|
142
|
+
this.tags[key] = value;
|
|
143
|
+
}
|
|
144
|
+
setData(key, value) {
|
|
145
|
+
this.data[key] = value;
|
|
146
|
+
}
|
|
147
|
+
startChild(spanContext) {
|
|
148
|
+
const span = new SpanImpl({
|
|
149
|
+
...spanContext,
|
|
150
|
+
traceId: this.traceId,
|
|
151
|
+
parentSpanId: this.spanId,
|
|
152
|
+
sampled: this.sampled,
|
|
153
|
+
});
|
|
154
|
+
this.children.push(span);
|
|
155
|
+
return span;
|
|
156
|
+
}
|
|
157
|
+
getChildren() {
|
|
158
|
+
return [...this.children];
|
|
159
|
+
}
|
|
160
|
+
generateSpanId() {
|
|
161
|
+
return Array.from({ length: 16 }, () => Math.floor(Math.random() * 16).toString(16)).join('');
|
|
162
|
+
}
|
|
163
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Event, Transport } from './types.js';
|
|
2
|
+
export interface TransportOptions {
|
|
3
|
+
url: string;
|
|
4
|
+
headers?: Record<string, string>;
|
|
5
|
+
timeout?: number;
|
|
6
|
+
}
|
|
7
|
+
export declare class HttpTransport implements Transport {
|
|
8
|
+
private url;
|
|
9
|
+
private headers;
|
|
10
|
+
private timeout;
|
|
11
|
+
constructor(options: TransportOptions);
|
|
12
|
+
sendEvent(event: Event): Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
export declare class ConsoleTransport implements Transport {
|
|
15
|
+
sendEvent(event: Event): Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
export declare function parseDsn(dsn: string): {
|
|
18
|
+
protocol: string;
|
|
19
|
+
host: string;
|
|
20
|
+
projectId: string;
|
|
21
|
+
publicKey: string;
|
|
22
|
+
};
|
|
23
|
+
export declare function createTransportFromDsn(dsn: string, debug?: boolean): Transport;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
export class HttpTransport {
|
|
2
|
+
url;
|
|
3
|
+
headers;
|
|
4
|
+
timeout;
|
|
5
|
+
constructor(options) {
|
|
6
|
+
this.url = options.url;
|
|
7
|
+
this.headers = {
|
|
8
|
+
'Content-Type': 'application/json',
|
|
9
|
+
...options.headers,
|
|
10
|
+
};
|
|
11
|
+
this.timeout = options.timeout || 30000;
|
|
12
|
+
}
|
|
13
|
+
async sendEvent(event) {
|
|
14
|
+
const controller = new AbortController();
|
|
15
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
16
|
+
try {
|
|
17
|
+
const response = await fetch(this.url, {
|
|
18
|
+
method: 'POST',
|
|
19
|
+
headers: this.headers,
|
|
20
|
+
body: JSON.stringify(event),
|
|
21
|
+
signal: controller.signal,
|
|
22
|
+
});
|
|
23
|
+
if (!response.ok) {
|
|
24
|
+
throw new Error(`Failed to send event: ${response.status} ${response.statusText}`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
if (error instanceof Error) {
|
|
29
|
+
if (error.name === 'AbortError') {
|
|
30
|
+
throw new Error(`Request timeout after ${this.timeout}ms`);
|
|
31
|
+
}
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
throw new Error('Unknown error occurred while sending event');
|
|
35
|
+
}
|
|
36
|
+
finally {
|
|
37
|
+
clearTimeout(timeoutId);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
export class ConsoleTransport {
|
|
42
|
+
async sendEvent(event) {
|
|
43
|
+
console.log('[ErrorTracking]', JSON.stringify(event, null, 2));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export function parseDsn(dsn) {
|
|
47
|
+
const regex = /^(https?):\/\/([^@]+)@([^/]+)\/(\d+)$/;
|
|
48
|
+
const match = dsn.match(regex);
|
|
49
|
+
if (!match) {
|
|
50
|
+
throw new Error('Invalid DSN format');
|
|
51
|
+
}
|
|
52
|
+
const [, protocol, publicKey, host, projectId] = match;
|
|
53
|
+
return {
|
|
54
|
+
protocol,
|
|
55
|
+
publicKey,
|
|
56
|
+
host,
|
|
57
|
+
projectId,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
export function createTransportFromDsn(dsn, debug = false) {
|
|
61
|
+
if (debug) {
|
|
62
|
+
return new ConsoleTransport();
|
|
63
|
+
}
|
|
64
|
+
const { protocol, host, projectId, publicKey } = parseDsn(dsn);
|
|
65
|
+
const url = `${protocol}://${host}/api/${projectId}/store/`;
|
|
66
|
+
return new HttpTransport({
|
|
67
|
+
url,
|
|
68
|
+
headers: {
|
|
69
|
+
'X-Sentry-Auth': `Sentry sentry_key=${publicKey}, sentry_version=7`,
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
}
|