@chrom-ar/collector 0.3.0 → 0.3.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/CHANGELOG.md +8 -1
- package/README.md +18 -0
- package/dist/index.cjs +100 -9
- package/dist/index.d.cts +17 -2
- package/dist/index.d.ts +17 -2
- package/dist/index.js +100 -10
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
-
## 0.3.
|
|
3
|
+
## 0.3.1 (2026-11-07)
|
|
4
|
+
|
|
5
|
+
- `Collector.test()` sends one test error and resolves to `{ ok: true, id }`
|
|
6
|
+
or `{ ok: false, reason }`, logging one console line either way. With
|
|
7
|
+
`debug: true`, it's also `window.__collector.test()` for the browser
|
|
8
|
+
console.
|
|
9
|
+
|
|
10
|
+
## 0.3.0 (2026-11-07)
|
|
4
11
|
|
|
5
12
|
- Heartbeats: once a day per release and environment, a few seconds after
|
|
6
13
|
`init()`, the SDK tells Collector the app is running, so a healthy app
|
package/README.md
CHANGED
|
@@ -66,6 +66,24 @@ browser console:
|
|
|
66
66
|
|
|
67
67
|
- **Vue:** add `app.config.errorHandler = (error) => Collector.captureException(error);`
|
|
68
68
|
|
|
69
|
+
## Checking your setup
|
|
70
|
+
|
|
71
|
+
Send one test error and see what Collector answered:
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
const result = await Collector.test();
|
|
75
|
+
// { ok: true, id: "…" } or { ok: false, reason: "Collector rejected the key (HTTP 401)…" }
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
It also logs one line to the console, `[collector] Test event sent (202)…`
|
|
79
|
+
or why it wasn't. The event is a `CollectorTest` error tagged
|
|
80
|
+
`collector.test: "true"`, and every test groups into one issue. It skips
|
|
81
|
+
`ignoreErrors`, duplicate detection and the rate cap; `beforeSend` edits
|
|
82
|
+
apply, but if `beforeSend` drops it, it's sent anyway.
|
|
83
|
+
|
|
84
|
+
With `debug: true`, it's also on `window.__collector.test()`, so you can
|
|
85
|
+
run it from the browser's console without importing anything.
|
|
86
|
+
|
|
69
87
|
## Reporting things yourself
|
|
70
88
|
|
|
71
89
|
```ts
|
package/dist/index.cjs
CHANGED
|
@@ -219,7 +219,7 @@ function baseEvent({ options, scope: scope2, href, userAgent }, ctx = {}) {
|
|
|
219
219
|
platform: "javascript",
|
|
220
220
|
level: toLevel(ctx.level),
|
|
221
221
|
request: { url: options.stripQuery === false ? href : stripUrl(href), headers: { "user-agent": userAgent } },
|
|
222
|
-
sdk: { name: SDK_NAME, version: "0.3.
|
|
222
|
+
sdk: { name: SDK_NAME, version: "0.3.1" }
|
|
223
223
|
};
|
|
224
224
|
const environment = environmentOf(options);
|
|
225
225
|
if (environment !== void 0) event.environment = environment;
|
|
@@ -348,7 +348,7 @@ async function beat({ url, key, fetch, options, logger }) {
|
|
|
348
348
|
if (storedMark(storageKey) === mark || sentThisPage.has(storageKey + mark)) return;
|
|
349
349
|
let status;
|
|
350
350
|
try {
|
|
351
|
-
const body = JSON.stringify({ v: 1, sdk: { name: SDK_NAME, version: "0.3.
|
|
351
|
+
const body = JSON.stringify({ v: 1, sdk: { name: SDK_NAME, version: "0.3.1" }, environment, release });
|
|
352
352
|
const response = await fetch(url, { method: "POST", body, headers: { "Content-Type": "text/plain;charset=UTF-8" }, keepalive: true, credentials: "omit" });
|
|
353
353
|
({ status } = response);
|
|
354
354
|
} catch (error) {
|
|
@@ -601,7 +601,7 @@ function instrumentXhr(win, record, opts) {
|
|
|
601
601
|
// src/logger.ts
|
|
602
602
|
function createLogger(isDebug) {
|
|
603
603
|
const warned = /* @__PURE__ */ new Set();
|
|
604
|
-
const { debug, warn } = console;
|
|
604
|
+
const { debug, info, warn } = console;
|
|
605
605
|
return {
|
|
606
606
|
debug(...args) {
|
|
607
607
|
if (isDebug()) debug.call(console, "[collector]", ...args);
|
|
@@ -610,6 +610,12 @@ function createLogger(isDebug) {
|
|
|
610
610
|
if (warned.has(message)) return;
|
|
611
611
|
warned.add(message);
|
|
612
612
|
warn.call(console, `[collector] ${message}`);
|
|
613
|
+
},
|
|
614
|
+
info(message) {
|
|
615
|
+
info.call(console, `[collector] ${message}`);
|
|
616
|
+
},
|
|
617
|
+
warn(message) {
|
|
618
|
+
warn.call(console, `[collector] ${message}`);
|
|
613
619
|
}
|
|
614
620
|
};
|
|
615
621
|
}
|
|
@@ -651,6 +657,11 @@ var Scope = class {
|
|
|
651
657
|
};
|
|
652
658
|
|
|
653
659
|
// src/transport.ts
|
|
660
|
+
function answer(pending, result) {
|
|
661
|
+
const { onAnswer } = pending;
|
|
662
|
+
pending.onAnswer = void 0;
|
|
663
|
+
onAnswer?.(result);
|
|
664
|
+
}
|
|
654
665
|
var FIRST_BACKOFF = 1e3;
|
|
655
666
|
var MAX_BACKOFF = 6e4;
|
|
656
667
|
var MAX_ATTEMPTS = 8;
|
|
@@ -672,14 +683,15 @@ var Transport = class {
|
|
|
672
683
|
this.idleWaiters = [];
|
|
673
684
|
this.maxQueue = deps.maxQueue ?? 30;
|
|
674
685
|
}
|
|
675
|
-
/** Queues the event. Never blocks, never throws. */
|
|
676
|
-
send(event) {
|
|
686
|
+
/** Queues the event. Never blocks, never throws. `onAnswer` gets the first attempt's answer. */
|
|
687
|
+
send(event, onAnswer) {
|
|
677
688
|
if (this.disabled) return;
|
|
678
689
|
if (this.queue.length >= this.maxQueue) {
|
|
679
|
-
this.queue.shift();
|
|
690
|
+
const dropped = this.queue.shift();
|
|
691
|
+
if (dropped) answer(dropped, { error: new Error("dropped: too many events queued") });
|
|
680
692
|
this.deps.logger.debug("queue full, dropped the oldest event");
|
|
681
693
|
}
|
|
682
|
-
this.queue.push({ event, attempts: 0 });
|
|
694
|
+
this.queue.push({ event, attempts: 0, ...onAnswer ? { onAnswer } : {} });
|
|
683
695
|
this.dispatch();
|
|
684
696
|
}
|
|
685
697
|
/** Resolves `true` once everything queued has been sent (or given up on), `false` after `timeoutMs`. */
|
|
@@ -729,6 +741,7 @@ var Transport = class {
|
|
|
729
741
|
const body = encode(pending.event);
|
|
730
742
|
if (body === void 0) {
|
|
731
743
|
this.deps.logger.debug("dropped an event that is over 1 MB or not JSON-encodable", pending.event.uuid);
|
|
744
|
+
answer(pending, { error: new Error("dropped: over 1 MB or not JSON-encodable") });
|
|
732
745
|
this.dispatch();
|
|
733
746
|
return;
|
|
734
747
|
}
|
|
@@ -761,6 +774,7 @@ var Transport = class {
|
|
|
761
774
|
this.inFlight = false;
|
|
762
775
|
this.inFlightKeepaliveBytes = 0;
|
|
763
776
|
const { status } = response;
|
|
777
|
+
answer(pending, { status });
|
|
764
778
|
if (status >= 200 && status < 300) {
|
|
765
779
|
this.backoff = FIRST_BACKOFF;
|
|
766
780
|
} else if (status === 429) {
|
|
@@ -785,6 +799,7 @@ var Transport = class {
|
|
|
785
799
|
this.dispatch();
|
|
786
800
|
}
|
|
787
801
|
retry(pending, error) {
|
|
802
|
+
answer(pending, { error });
|
|
788
803
|
this.inFlight = false;
|
|
789
804
|
this.inFlightKeepaliveBytes = 0;
|
|
790
805
|
pending.attempts += 1;
|
|
@@ -807,7 +822,7 @@ var Transport = class {
|
|
|
807
822
|
}
|
|
808
823
|
disable(status) {
|
|
809
824
|
this.disabled = true;
|
|
810
|
-
this.queue
|
|
825
|
+
for (const pending of this.queue.splice(0)) answer(pending, { error: new Error(`dropped: Collector rejected this DSN (HTTP ${String(status)})`) });
|
|
811
826
|
this.deps.onDisable?.();
|
|
812
827
|
const where = this.deps.origin ?? "unknown origin";
|
|
813
828
|
this.deps.logger.warnOnce(
|
|
@@ -856,6 +871,20 @@ var INSTRUMENTS = {
|
|
|
856
871
|
history: instrumentHistory
|
|
857
872
|
};
|
|
858
873
|
var DEFAULT_MAX_BREADCRUMBS = 50;
|
|
874
|
+
var TEST_TIMEOUT_MS = 1e4;
|
|
875
|
+
function testFailure(result) {
|
|
876
|
+
if ("error" in result) {
|
|
877
|
+
const message = result.error instanceof Error ? result.error.message : String(result.error);
|
|
878
|
+
return `couldn't reach Collector (${message}). Check the DSN's host, your network, and your Content-Security-Policy's connect-src.`;
|
|
879
|
+
}
|
|
880
|
+
const { status } = result;
|
|
881
|
+
if (status >= 200 && status < 300) return void 0;
|
|
882
|
+
if (status === 401) return "Collector rejected the key (HTTP 401). Check the DSN on your project's setup page.";
|
|
883
|
+
if (status === 403) return "this site's domain is blocked for the project (HTTP 403). A project admin can unblock it in the project's settings.";
|
|
884
|
+
if (status === 429) return "Collector asked to slow down (HTTP 429). Try again in a minute.";
|
|
885
|
+
if (status >= 500) return `Collector had an error (HTTP ${String(status)}). The event will be retried.`;
|
|
886
|
+
return `Collector rejected the event (HTTP ${String(status)}).`;
|
|
887
|
+
}
|
|
859
888
|
var active;
|
|
860
889
|
var installed = /* @__PURE__ */ new Map();
|
|
861
890
|
var sharedOptions = {
|
|
@@ -884,15 +913,18 @@ var Client = class {
|
|
|
884
913
|
this.ignore = [...DEFAULT_IGNORE, ...options.ignoreErrors ?? []];
|
|
885
914
|
if (options.enabled === false) {
|
|
886
915
|
this.logger.debug("disabled (enabled: false): nothing will be sent");
|
|
916
|
+
this.inactive = "the SDK is off (enabled: false).";
|
|
887
917
|
return;
|
|
888
918
|
}
|
|
889
919
|
if (typeof options.dsn !== "string" || options.dsn === "") {
|
|
890
920
|
this.logger.debug("no DSN given: nothing will be sent");
|
|
921
|
+
this.inactive = "no DSN given to init().";
|
|
891
922
|
return;
|
|
892
923
|
}
|
|
893
924
|
const endpoint = parseDsn(options.dsn);
|
|
894
925
|
if (!endpoint) {
|
|
895
926
|
this.logger.debug("invalid DSN: nothing will be sent", options.dsn);
|
|
927
|
+
this.inactive = "invalid DSN. Copy it from your project's setup page.";
|
|
896
928
|
return;
|
|
897
929
|
}
|
|
898
930
|
this.endpointBase = endpoint.base;
|
|
@@ -939,6 +971,52 @@ var Client = class {
|
|
|
939
971
|
const context = typeof ctx === "string" ? { level: ctx } : ctx;
|
|
940
972
|
return this.capture(() => eventFromMessage(message, this.env(), context));
|
|
941
973
|
}
|
|
974
|
+
/**
|
|
975
|
+
* Sends one test error to check the setup end to end. Skips the filters
|
|
976
|
+
* (ignoreErrors, duplicates, the rate cap); `beforeSend` edits apply, but
|
|
977
|
+
* if it drops the event the original is sent anyway.
|
|
978
|
+
*/
|
|
979
|
+
async test() {
|
|
980
|
+
const fail = (reason) => {
|
|
981
|
+
this.logger.warn(`Test event not sent: ${reason}`);
|
|
982
|
+
return { ok: false, reason };
|
|
983
|
+
};
|
|
984
|
+
const { transport } = this;
|
|
985
|
+
if (this.closed) return Promise.resolve(fail("the SDK was closed."));
|
|
986
|
+
if (!transport) return Promise.resolve(fail(this.inactive ?? "the SDK isn't sending."));
|
|
987
|
+
if (transport.disabled) return Promise.resolve(fail("Collector already rejected this DSN on this page (HTTP 401/403)."));
|
|
988
|
+
let event;
|
|
989
|
+
try {
|
|
990
|
+
const error = new Error("Test event from Collector.test(). If you can see it in Collector, your setup works.");
|
|
991
|
+
error.name = "CollectorTest";
|
|
992
|
+
throw error;
|
|
993
|
+
} catch (error) {
|
|
994
|
+
event = eventFromException(error, this.env(), { tags: { "collector.test": "true" }, fingerprint: ["collector.test"] });
|
|
995
|
+
}
|
|
996
|
+
const final = this.beforeSend(event) ?? event;
|
|
997
|
+
return new Promise((resolve) => {
|
|
998
|
+
let settled = false;
|
|
999
|
+
const settle = (result) => {
|
|
1000
|
+
if (settled) return;
|
|
1001
|
+
settled = true;
|
|
1002
|
+
clearTimeout(timer);
|
|
1003
|
+
resolve(result);
|
|
1004
|
+
};
|
|
1005
|
+
const timer = setTimeout(() => {
|
|
1006
|
+
settle(fail(`no answer from Collector within ${String(TEST_TIMEOUT_MS / 1e3)} s.`));
|
|
1007
|
+
}, TEST_TIMEOUT_MS);
|
|
1008
|
+
transport.send(final, (answer2) => {
|
|
1009
|
+
if (settled) return;
|
|
1010
|
+
const reason = testFailure(answer2);
|
|
1011
|
+
if (reason !== void 0) {
|
|
1012
|
+
settle(fail(reason));
|
|
1013
|
+
return;
|
|
1014
|
+
}
|
|
1015
|
+
this.logger.info(`Test event sent (${"status" in answer2 ? String(answer2.status) : "?"}). Look for "CollectorTest" in your project: ${final.uuid}`);
|
|
1016
|
+
settle({ ok: true, id: final.uuid });
|
|
1017
|
+
});
|
|
1018
|
+
});
|
|
1019
|
+
}
|
|
942
1020
|
addBreadcrumb(crumb) {
|
|
943
1021
|
this.scope.addBreadcrumb(crumb, this.options.maxBreadcrumbs ?? DEFAULT_MAX_BREADCRUMBS);
|
|
944
1022
|
}
|
|
@@ -1022,7 +1100,7 @@ var Client = class {
|
|
|
1022
1100
|
};
|
|
1023
1101
|
|
|
1024
1102
|
// src/index.ts
|
|
1025
|
-
var SDK_VERSION = "0.3.
|
|
1103
|
+
var SDK_VERSION = "0.3.1";
|
|
1026
1104
|
var client;
|
|
1027
1105
|
var pendingScope = new Scope();
|
|
1028
1106
|
function scope() {
|
|
@@ -1042,8 +1120,19 @@ function init(options) {
|
|
|
1042
1120
|
const kept = scope();
|
|
1043
1121
|
client?.close();
|
|
1044
1122
|
client = new Client(options, window, kept);
|
|
1123
|
+
if (options.debug === true) Object.defineProperty(window, CONSOLE_HANDLE, { value: { test }, configurable: true, writable: true });
|
|
1124
|
+
else Reflect.deleteProperty(window, CONSOLE_HANDLE);
|
|
1045
1125
|
}, void 0);
|
|
1046
1126
|
}
|
|
1127
|
+
var CONSOLE_HANDLE = "__collector";
|
|
1128
|
+
async function test() {
|
|
1129
|
+
const notInitialized = () => {
|
|
1130
|
+
const reason = "init() hasn't run on this page (or this isn't a browser).";
|
|
1131
|
+
console.warn(`[collector] Test event not sent: ${reason}`);
|
|
1132
|
+
return { ok: false, reason };
|
|
1133
|
+
};
|
|
1134
|
+
return safely(async () => client ? client.test() : Promise.resolve(notInitialized()), Promise.resolve({ ok: false, reason: "internal error." }));
|
|
1135
|
+
}
|
|
1047
1136
|
function captureException(error, context) {
|
|
1048
1137
|
return safely(() => client?.captureException(error, context), void 0);
|
|
1049
1138
|
}
|
|
@@ -1079,6 +1168,7 @@ function close() {
|
|
|
1079
1168
|
client?.close();
|
|
1080
1169
|
client = void 0;
|
|
1081
1170
|
pendingScope = new Scope();
|
|
1171
|
+
if (typeof window !== "undefined") Reflect.deleteProperty(window, CONSOLE_HANDLE);
|
|
1082
1172
|
}, void 0);
|
|
1083
1173
|
}
|
|
1084
1174
|
|
|
@@ -1092,3 +1182,4 @@ exports.init = init;
|
|
|
1092
1182
|
exports.setTag = setTag;
|
|
1093
1183
|
exports.setTags = setTags;
|
|
1094
1184
|
exports.setUser = setUser;
|
|
1185
|
+
exports.test = test;
|
package/dist/index.d.cts
CHANGED
|
@@ -62,6 +62,14 @@ interface CaptureContext {
|
|
|
62
62
|
id: string | number;
|
|
63
63
|
};
|
|
64
64
|
}
|
|
65
|
+
/** What `test()` resolves to: the event's id, or why it wasn't accepted. */
|
|
66
|
+
type TestResult = {
|
|
67
|
+
ok: true;
|
|
68
|
+
id: string;
|
|
69
|
+
} | {
|
|
70
|
+
ok: false;
|
|
71
|
+
reason: string;
|
|
72
|
+
};
|
|
65
73
|
type BreadcrumbKind = "console" | "dom" | "fetch" | "xhr" | "history";
|
|
66
74
|
interface CollectorOptions {
|
|
67
75
|
/** Your project's DSN, from its setup page in Collector. Without it, nothing is sent. */
|
|
@@ -124,6 +132,13 @@ declare const SDK_VERSION: string;
|
|
|
124
132
|
* options. Safe to import during server-side rendering: it does nothing there.
|
|
125
133
|
*/
|
|
126
134
|
declare function init(options: CollectorOptions): void;
|
|
135
|
+
/**
|
|
136
|
+
* Sends one test error, to check your setup end to end. Resolves to
|
|
137
|
+
* `{ ok: true, id }` once Collector accepts it, or `{ ok: false, reason }`,
|
|
138
|
+
* and logs one line to the console either way. With `debug: true` it's also
|
|
139
|
+
* `window.__collector.test()`, for the browser console.
|
|
140
|
+
*/
|
|
141
|
+
declare function test(): Promise<TestResult>;
|
|
127
142
|
/**
|
|
128
143
|
* Reports an error you caught. Returns the event id, or `undefined` if it
|
|
129
144
|
* wasn't sent (no DSN, ignored, duplicate, dropped by `beforeSend`).
|
|
@@ -146,5 +161,5 @@ declare function flush(timeoutMs?: number): Promise<boolean>;
|
|
|
146
161
|
/** Stops capturing and removes everything the SDK installed. */
|
|
147
162
|
declare function close(): void;
|
|
148
163
|
|
|
149
|
-
export { SDK_VERSION, addBreadcrumb, captureException, captureMessage, close, flush, init, setTag, setTags, setUser };
|
|
150
|
-
export type { Breadcrumb, BreadcrumbKind, CaptureContext, CollectorEvent, CollectorOptions, Frame, Level };
|
|
164
|
+
export { SDK_VERSION, addBreadcrumb, captureException, captureMessage, close, flush, init, setTag, setTags, setUser, test };
|
|
165
|
+
export type { Breadcrumb, BreadcrumbKind, CaptureContext, CollectorEvent, CollectorOptions, Frame, Level, TestResult };
|
package/dist/index.d.ts
CHANGED
|
@@ -62,6 +62,14 @@ interface CaptureContext {
|
|
|
62
62
|
id: string | number;
|
|
63
63
|
};
|
|
64
64
|
}
|
|
65
|
+
/** What `test()` resolves to: the event's id, or why it wasn't accepted. */
|
|
66
|
+
type TestResult = {
|
|
67
|
+
ok: true;
|
|
68
|
+
id: string;
|
|
69
|
+
} | {
|
|
70
|
+
ok: false;
|
|
71
|
+
reason: string;
|
|
72
|
+
};
|
|
65
73
|
type BreadcrumbKind = "console" | "dom" | "fetch" | "xhr" | "history";
|
|
66
74
|
interface CollectorOptions {
|
|
67
75
|
/** Your project's DSN, from its setup page in Collector. Without it, nothing is sent. */
|
|
@@ -124,6 +132,13 @@ declare const SDK_VERSION: string;
|
|
|
124
132
|
* options. Safe to import during server-side rendering: it does nothing there.
|
|
125
133
|
*/
|
|
126
134
|
declare function init(options: CollectorOptions): void;
|
|
135
|
+
/**
|
|
136
|
+
* Sends one test error, to check your setup end to end. Resolves to
|
|
137
|
+
* `{ ok: true, id }` once Collector accepts it, or `{ ok: false, reason }`,
|
|
138
|
+
* and logs one line to the console either way. With `debug: true` it's also
|
|
139
|
+
* `window.__collector.test()`, for the browser console.
|
|
140
|
+
*/
|
|
141
|
+
declare function test(): Promise<TestResult>;
|
|
127
142
|
/**
|
|
128
143
|
* Reports an error you caught. Returns the event id, or `undefined` if it
|
|
129
144
|
* wasn't sent (no DSN, ignored, duplicate, dropped by `beforeSend`).
|
|
@@ -146,5 +161,5 @@ declare function flush(timeoutMs?: number): Promise<boolean>;
|
|
|
146
161
|
/** Stops capturing and removes everything the SDK installed. */
|
|
147
162
|
declare function close(): void;
|
|
148
163
|
|
|
149
|
-
export { SDK_VERSION, addBreadcrumb, captureException, captureMessage, close, flush, init, setTag, setTags, setUser };
|
|
150
|
-
export type { Breadcrumb, BreadcrumbKind, CaptureContext, CollectorEvent, CollectorOptions, Frame, Level };
|
|
164
|
+
export { SDK_VERSION, addBreadcrumb, captureException, captureMessage, close, flush, init, setTag, setTags, setUser, test };
|
|
165
|
+
export type { Breadcrumb, BreadcrumbKind, CaptureContext, CollectorEvent, CollectorOptions, Frame, Level, TestResult };
|
package/dist/index.js
CHANGED
|
@@ -217,7 +217,7 @@ function baseEvent({ options, scope: scope2, href, userAgent }, ctx = {}) {
|
|
|
217
217
|
platform: "javascript",
|
|
218
218
|
level: toLevel(ctx.level),
|
|
219
219
|
request: { url: options.stripQuery === false ? href : stripUrl(href), headers: { "user-agent": userAgent } },
|
|
220
|
-
sdk: { name: SDK_NAME, version: "0.3.
|
|
220
|
+
sdk: { name: SDK_NAME, version: "0.3.1" }
|
|
221
221
|
};
|
|
222
222
|
const environment = environmentOf(options);
|
|
223
223
|
if (environment !== void 0) event.environment = environment;
|
|
@@ -346,7 +346,7 @@ async function beat({ url, key, fetch, options, logger }) {
|
|
|
346
346
|
if (storedMark(storageKey) === mark || sentThisPage.has(storageKey + mark)) return;
|
|
347
347
|
let status;
|
|
348
348
|
try {
|
|
349
|
-
const body = JSON.stringify({ v: 1, sdk: { name: SDK_NAME, version: "0.3.
|
|
349
|
+
const body = JSON.stringify({ v: 1, sdk: { name: SDK_NAME, version: "0.3.1" }, environment, release });
|
|
350
350
|
const response = await fetch(url, { method: "POST", body, headers: { "Content-Type": "text/plain;charset=UTF-8" }, keepalive: true, credentials: "omit" });
|
|
351
351
|
({ status } = response);
|
|
352
352
|
} catch (error) {
|
|
@@ -599,7 +599,7 @@ function instrumentXhr(win, record, opts) {
|
|
|
599
599
|
// src/logger.ts
|
|
600
600
|
function createLogger(isDebug) {
|
|
601
601
|
const warned = /* @__PURE__ */ new Set();
|
|
602
|
-
const { debug, warn } = console;
|
|
602
|
+
const { debug, info, warn } = console;
|
|
603
603
|
return {
|
|
604
604
|
debug(...args) {
|
|
605
605
|
if (isDebug()) debug.call(console, "[collector]", ...args);
|
|
@@ -608,6 +608,12 @@ function createLogger(isDebug) {
|
|
|
608
608
|
if (warned.has(message)) return;
|
|
609
609
|
warned.add(message);
|
|
610
610
|
warn.call(console, `[collector] ${message}`);
|
|
611
|
+
},
|
|
612
|
+
info(message) {
|
|
613
|
+
info.call(console, `[collector] ${message}`);
|
|
614
|
+
},
|
|
615
|
+
warn(message) {
|
|
616
|
+
warn.call(console, `[collector] ${message}`);
|
|
611
617
|
}
|
|
612
618
|
};
|
|
613
619
|
}
|
|
@@ -649,6 +655,11 @@ var Scope = class {
|
|
|
649
655
|
};
|
|
650
656
|
|
|
651
657
|
// src/transport.ts
|
|
658
|
+
function answer(pending, result) {
|
|
659
|
+
const { onAnswer } = pending;
|
|
660
|
+
pending.onAnswer = void 0;
|
|
661
|
+
onAnswer?.(result);
|
|
662
|
+
}
|
|
652
663
|
var FIRST_BACKOFF = 1e3;
|
|
653
664
|
var MAX_BACKOFF = 6e4;
|
|
654
665
|
var MAX_ATTEMPTS = 8;
|
|
@@ -670,14 +681,15 @@ var Transport = class {
|
|
|
670
681
|
this.idleWaiters = [];
|
|
671
682
|
this.maxQueue = deps.maxQueue ?? 30;
|
|
672
683
|
}
|
|
673
|
-
/** Queues the event. Never blocks, never throws. */
|
|
674
|
-
send(event) {
|
|
684
|
+
/** Queues the event. Never blocks, never throws. `onAnswer` gets the first attempt's answer. */
|
|
685
|
+
send(event, onAnswer) {
|
|
675
686
|
if (this.disabled) return;
|
|
676
687
|
if (this.queue.length >= this.maxQueue) {
|
|
677
|
-
this.queue.shift();
|
|
688
|
+
const dropped = this.queue.shift();
|
|
689
|
+
if (dropped) answer(dropped, { error: new Error("dropped: too many events queued") });
|
|
678
690
|
this.deps.logger.debug("queue full, dropped the oldest event");
|
|
679
691
|
}
|
|
680
|
-
this.queue.push({ event, attempts: 0 });
|
|
692
|
+
this.queue.push({ event, attempts: 0, ...onAnswer ? { onAnswer } : {} });
|
|
681
693
|
this.dispatch();
|
|
682
694
|
}
|
|
683
695
|
/** Resolves `true` once everything queued has been sent (or given up on), `false` after `timeoutMs`. */
|
|
@@ -727,6 +739,7 @@ var Transport = class {
|
|
|
727
739
|
const body = encode(pending.event);
|
|
728
740
|
if (body === void 0) {
|
|
729
741
|
this.deps.logger.debug("dropped an event that is over 1 MB or not JSON-encodable", pending.event.uuid);
|
|
742
|
+
answer(pending, { error: new Error("dropped: over 1 MB or not JSON-encodable") });
|
|
730
743
|
this.dispatch();
|
|
731
744
|
return;
|
|
732
745
|
}
|
|
@@ -759,6 +772,7 @@ var Transport = class {
|
|
|
759
772
|
this.inFlight = false;
|
|
760
773
|
this.inFlightKeepaliveBytes = 0;
|
|
761
774
|
const { status } = response;
|
|
775
|
+
answer(pending, { status });
|
|
762
776
|
if (status >= 200 && status < 300) {
|
|
763
777
|
this.backoff = FIRST_BACKOFF;
|
|
764
778
|
} else if (status === 429) {
|
|
@@ -783,6 +797,7 @@ var Transport = class {
|
|
|
783
797
|
this.dispatch();
|
|
784
798
|
}
|
|
785
799
|
retry(pending, error) {
|
|
800
|
+
answer(pending, { error });
|
|
786
801
|
this.inFlight = false;
|
|
787
802
|
this.inFlightKeepaliveBytes = 0;
|
|
788
803
|
pending.attempts += 1;
|
|
@@ -805,7 +820,7 @@ var Transport = class {
|
|
|
805
820
|
}
|
|
806
821
|
disable(status) {
|
|
807
822
|
this.disabled = true;
|
|
808
|
-
this.queue
|
|
823
|
+
for (const pending of this.queue.splice(0)) answer(pending, { error: new Error(`dropped: Collector rejected this DSN (HTTP ${String(status)})`) });
|
|
809
824
|
this.deps.onDisable?.();
|
|
810
825
|
const where = this.deps.origin ?? "unknown origin";
|
|
811
826
|
this.deps.logger.warnOnce(
|
|
@@ -854,6 +869,20 @@ var INSTRUMENTS = {
|
|
|
854
869
|
history: instrumentHistory
|
|
855
870
|
};
|
|
856
871
|
var DEFAULT_MAX_BREADCRUMBS = 50;
|
|
872
|
+
var TEST_TIMEOUT_MS = 1e4;
|
|
873
|
+
function testFailure(result) {
|
|
874
|
+
if ("error" in result) {
|
|
875
|
+
const message = result.error instanceof Error ? result.error.message : String(result.error);
|
|
876
|
+
return `couldn't reach Collector (${message}). Check the DSN's host, your network, and your Content-Security-Policy's connect-src.`;
|
|
877
|
+
}
|
|
878
|
+
const { status } = result;
|
|
879
|
+
if (status >= 200 && status < 300) return void 0;
|
|
880
|
+
if (status === 401) return "Collector rejected the key (HTTP 401). Check the DSN on your project's setup page.";
|
|
881
|
+
if (status === 403) return "this site's domain is blocked for the project (HTTP 403). A project admin can unblock it in the project's settings.";
|
|
882
|
+
if (status === 429) return "Collector asked to slow down (HTTP 429). Try again in a minute.";
|
|
883
|
+
if (status >= 500) return `Collector had an error (HTTP ${String(status)}). The event will be retried.`;
|
|
884
|
+
return `Collector rejected the event (HTTP ${String(status)}).`;
|
|
885
|
+
}
|
|
857
886
|
var active;
|
|
858
887
|
var installed = /* @__PURE__ */ new Map();
|
|
859
888
|
var sharedOptions = {
|
|
@@ -882,15 +911,18 @@ var Client = class {
|
|
|
882
911
|
this.ignore = [...DEFAULT_IGNORE, ...options.ignoreErrors ?? []];
|
|
883
912
|
if (options.enabled === false) {
|
|
884
913
|
this.logger.debug("disabled (enabled: false): nothing will be sent");
|
|
914
|
+
this.inactive = "the SDK is off (enabled: false).";
|
|
885
915
|
return;
|
|
886
916
|
}
|
|
887
917
|
if (typeof options.dsn !== "string" || options.dsn === "") {
|
|
888
918
|
this.logger.debug("no DSN given: nothing will be sent");
|
|
919
|
+
this.inactive = "no DSN given to init().";
|
|
889
920
|
return;
|
|
890
921
|
}
|
|
891
922
|
const endpoint = parseDsn(options.dsn);
|
|
892
923
|
if (!endpoint) {
|
|
893
924
|
this.logger.debug("invalid DSN: nothing will be sent", options.dsn);
|
|
925
|
+
this.inactive = "invalid DSN. Copy it from your project's setup page.";
|
|
894
926
|
return;
|
|
895
927
|
}
|
|
896
928
|
this.endpointBase = endpoint.base;
|
|
@@ -937,6 +969,52 @@ var Client = class {
|
|
|
937
969
|
const context = typeof ctx === "string" ? { level: ctx } : ctx;
|
|
938
970
|
return this.capture(() => eventFromMessage(message, this.env(), context));
|
|
939
971
|
}
|
|
972
|
+
/**
|
|
973
|
+
* Sends one test error to check the setup end to end. Skips the filters
|
|
974
|
+
* (ignoreErrors, duplicates, the rate cap); `beforeSend` edits apply, but
|
|
975
|
+
* if it drops the event the original is sent anyway.
|
|
976
|
+
*/
|
|
977
|
+
async test() {
|
|
978
|
+
const fail = (reason) => {
|
|
979
|
+
this.logger.warn(`Test event not sent: ${reason}`);
|
|
980
|
+
return { ok: false, reason };
|
|
981
|
+
};
|
|
982
|
+
const { transport } = this;
|
|
983
|
+
if (this.closed) return Promise.resolve(fail("the SDK was closed."));
|
|
984
|
+
if (!transport) return Promise.resolve(fail(this.inactive ?? "the SDK isn't sending."));
|
|
985
|
+
if (transport.disabled) return Promise.resolve(fail("Collector already rejected this DSN on this page (HTTP 401/403)."));
|
|
986
|
+
let event;
|
|
987
|
+
try {
|
|
988
|
+
const error = new Error("Test event from Collector.test(). If you can see it in Collector, your setup works.");
|
|
989
|
+
error.name = "CollectorTest";
|
|
990
|
+
throw error;
|
|
991
|
+
} catch (error) {
|
|
992
|
+
event = eventFromException(error, this.env(), { tags: { "collector.test": "true" }, fingerprint: ["collector.test"] });
|
|
993
|
+
}
|
|
994
|
+
const final = this.beforeSend(event) ?? event;
|
|
995
|
+
return new Promise((resolve) => {
|
|
996
|
+
let settled = false;
|
|
997
|
+
const settle = (result) => {
|
|
998
|
+
if (settled) return;
|
|
999
|
+
settled = true;
|
|
1000
|
+
clearTimeout(timer);
|
|
1001
|
+
resolve(result);
|
|
1002
|
+
};
|
|
1003
|
+
const timer = setTimeout(() => {
|
|
1004
|
+
settle(fail(`no answer from Collector within ${String(TEST_TIMEOUT_MS / 1e3)} s.`));
|
|
1005
|
+
}, TEST_TIMEOUT_MS);
|
|
1006
|
+
transport.send(final, (answer2) => {
|
|
1007
|
+
if (settled) return;
|
|
1008
|
+
const reason = testFailure(answer2);
|
|
1009
|
+
if (reason !== void 0) {
|
|
1010
|
+
settle(fail(reason));
|
|
1011
|
+
return;
|
|
1012
|
+
}
|
|
1013
|
+
this.logger.info(`Test event sent (${"status" in answer2 ? String(answer2.status) : "?"}). Look for "CollectorTest" in your project: ${final.uuid}`);
|
|
1014
|
+
settle({ ok: true, id: final.uuid });
|
|
1015
|
+
});
|
|
1016
|
+
});
|
|
1017
|
+
}
|
|
940
1018
|
addBreadcrumb(crumb) {
|
|
941
1019
|
this.scope.addBreadcrumb(crumb, this.options.maxBreadcrumbs ?? DEFAULT_MAX_BREADCRUMBS);
|
|
942
1020
|
}
|
|
@@ -1020,7 +1098,7 @@ var Client = class {
|
|
|
1020
1098
|
};
|
|
1021
1099
|
|
|
1022
1100
|
// src/index.ts
|
|
1023
|
-
var SDK_VERSION = "0.3.
|
|
1101
|
+
var SDK_VERSION = "0.3.1";
|
|
1024
1102
|
var client;
|
|
1025
1103
|
var pendingScope = new Scope();
|
|
1026
1104
|
function scope() {
|
|
@@ -1040,8 +1118,19 @@ function init(options) {
|
|
|
1040
1118
|
const kept = scope();
|
|
1041
1119
|
client?.close();
|
|
1042
1120
|
client = new Client(options, window, kept);
|
|
1121
|
+
if (options.debug === true) Object.defineProperty(window, CONSOLE_HANDLE, { value: { test }, configurable: true, writable: true });
|
|
1122
|
+
else Reflect.deleteProperty(window, CONSOLE_HANDLE);
|
|
1043
1123
|
}, void 0);
|
|
1044
1124
|
}
|
|
1125
|
+
var CONSOLE_HANDLE = "__collector";
|
|
1126
|
+
async function test() {
|
|
1127
|
+
const notInitialized = () => {
|
|
1128
|
+
const reason = "init() hasn't run on this page (or this isn't a browser).";
|
|
1129
|
+
console.warn(`[collector] Test event not sent: ${reason}`);
|
|
1130
|
+
return { ok: false, reason };
|
|
1131
|
+
};
|
|
1132
|
+
return safely(async () => client ? client.test() : Promise.resolve(notInitialized()), Promise.resolve({ ok: false, reason: "internal error." }));
|
|
1133
|
+
}
|
|
1045
1134
|
function captureException(error, context) {
|
|
1046
1135
|
return safely(() => client?.captureException(error, context), void 0);
|
|
1047
1136
|
}
|
|
@@ -1077,7 +1166,8 @@ function close() {
|
|
|
1077
1166
|
client?.close();
|
|
1078
1167
|
client = void 0;
|
|
1079
1168
|
pendingScope = new Scope();
|
|
1169
|
+
if (typeof window !== "undefined") Reflect.deleteProperty(window, CONSOLE_HANDLE);
|
|
1080
1170
|
}, void 0);
|
|
1081
1171
|
}
|
|
1082
1172
|
|
|
1083
|
-
export { SDK_VERSION, addBreadcrumb, captureException, captureMessage, close, flush, init, setTag, setTags, setUser };
|
|
1173
|
+
export { SDK_VERSION, addBreadcrumb, captureException, captureMessage, close, flush, init, setTag, setTags, setUser, test };
|