@chrom-ar/collector 0.2.0 → 0.3.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.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,14 @@
1
1
  # Changelog
2
2
 
3
- ## 0.2.0 (unreleased)
3
+ ## 0.3.0 (unreleased)
4
+
5
+ - Heartbeats: once a day per release and environment, a few seconds after
6
+ `init()`, the SDK tells Collector the app is running, so a healthy app
7
+ shows as set up before its first error. It sends only the SDK name and
8
+ version, release and environment, and remembers the day in
9
+ `localStorage`. Turn it off with `heartbeat: false`.
10
+
11
+ ## 0.2.0 (2026-11-05)
4
12
 
5
13
  - Source maps: `init()` uses the release injected by
6
14
  `@chrom-ar/collector-cli/vite` (the `__COLLECTOR_RELEASE__` build constant)
package/README.md CHANGED
@@ -107,6 +107,12 @@ Collector.setTag("plan", "pro");
107
107
  as they are, errors as `Type: message`, and only the first level of an
108
108
  object's keys (`{id: 4812, email: "…"}`). If your app logs personal
109
109
  data, turn them off with `breadcrumbs: { console: false }`.
110
+ - Once a day per release and environment, a few seconds after `init()`,
111
+ the SDK sends a heartbeat so Collector knows the app is running even
112
+ before its first error. It carries only the SDK's name and version, the
113
+ release and the environment: no user, URL, user agent or browser id. The
114
+ day it was sent is kept in `localStorage` (`collector:heartbeat:…`).
115
+ Turn it off with `heartbeat: false`.
110
116
  - `beforeSend` sees every event last, and can edit it or drop it:
111
117
 
112
118
  ```ts
@@ -135,6 +141,7 @@ Collector.setTag("plan", "pro");
135
141
  | `breadcrumbs` | all on | `{ console, dom, fetch, xhr, history }`, each `true`/`false` |
136
142
  | `maxBreadcrumbs` | `50` | How many breadcrumbs to keep |
137
143
  | `autoCapture` | both on | `{ onerror, onunhandledrejection }`, or `false` for neither |
144
+ | `heartbeat` | `true` | `false` stops the daily heartbeat (see [Privacy](#privacy)) |
138
145
 
139
146
  Other functions: `setTags(tags)`, `addBreadcrumb({ message, category?, level? })`,
140
147
  `flush(timeoutMs?)` (waits until queued events are sent) and `close()`
package/dist/index.cjs CHANGED
@@ -10,7 +10,8 @@ function parseDsn(dsn) {
10
10
  }
11
11
  if (parsed.protocol !== "https:" && parsed.protocol !== "http:" || parsed.username === "") return null;
12
12
  const base = parsed.origin + parsed.pathname.replace(/\/+$/, "");
13
- return { url: `${base}/api/events?key=${parsed.username}`, base };
13
+ const key = parsed.username;
14
+ return { url: `${base}/api/events?key=${key}`, heartbeatUrl: `${base}/api/heartbeats?key=${key}`, base, key };
14
15
  }
15
16
 
16
17
  // src/util.ts
@@ -203,6 +204,13 @@ function eventFromMessage(message, env, ctx) {
203
204
  function injectedRelease() {
204
205
  return typeof __COLLECTOR_RELEASE__ === "string" ? __COLLECTOR_RELEASE__ : void 0;
205
206
  }
207
+ function releaseOf(options) {
208
+ const release = options.release ?? injectedRelease();
209
+ return release === "" ? void 0 : release;
210
+ }
211
+ function environmentOf(options) {
212
+ return typeof options.environment === "string" && options.environment !== "" ? options.environment : void 0;
213
+ }
206
214
  function baseEvent({ options, scope: scope2, href, userAgent }, ctx = {}) {
207
215
  const event = {
208
216
  v: 1,
@@ -211,11 +219,12 @@ function baseEvent({ options, scope: scope2, href, userAgent }, ctx = {}) {
211
219
  platform: "javascript",
212
220
  level: toLevel(ctx.level),
213
221
  request: { url: options.stripQuery === false ? href : stripUrl(href), headers: { "user-agent": userAgent } },
214
- sdk: { name: SDK_NAME, version: "0.2.0" }
222
+ sdk: { name: SDK_NAME, version: "0.3.0" }
215
223
  };
216
- if (typeof options.environment === "string" && options.environment !== "") event.environment = options.environment;
217
- const release = options.release ?? injectedRelease();
218
- if (release !== void 0 && release !== "") event.release = release;
224
+ const environment = environmentOf(options);
225
+ if (environment !== void 0) event.environment = environment;
226
+ const release = releaseOf(options);
227
+ if (release !== void 0) event.release = release;
219
228
  if (ctx.fingerprint && ctx.fingerprint.length > 0) event.fingerprint = ctx.fingerprint.map(String);
220
229
  const user = ctx.user ? { id: String(ctx.user.id) } : scope2.user;
221
230
  if (user) event.user = user;
@@ -306,6 +315,54 @@ function errorFromLocation(e) {
306
315
  return error;
307
316
  }
308
317
 
318
+ // src/heartbeat.ts
319
+ var HEARTBEAT_DELAY_MS = 5e3;
320
+ var STORAGE_PREFIX = "collector:heartbeat:";
321
+ var sentThisPage = /* @__PURE__ */ new Set();
322
+ function scheduleHeartbeat(deps) {
323
+ let idle;
324
+ const run = () => {
325
+ void beat(deps);
326
+ };
327
+ const timer = setTimeout(() => {
328
+ if ("requestIdleCallback" in globalThis) idle = requestIdleCallback(run, { timeout: HEARTBEAT_DELAY_MS });
329
+ else run();
330
+ }, HEARTBEAT_DELAY_MS);
331
+ return () => {
332
+ clearTimeout(timer);
333
+ if (idle !== void 0) cancelIdleCallback(idle);
334
+ };
335
+ }
336
+ function storedMark(storageKey) {
337
+ try {
338
+ return localStorage.getItem(storageKey);
339
+ } catch {
340
+ return null;
341
+ }
342
+ }
343
+ async function beat({ url, key, fetch, options, logger }) {
344
+ const release = releaseOf(options);
345
+ const environment = environmentOf(options);
346
+ const storageKey = STORAGE_PREFIX + key;
347
+ const mark = JSON.stringify([(/* @__PURE__ */ new Date()).toISOString().slice(0, 10), release ?? "", environment ?? ""]);
348
+ if (storedMark(storageKey) === mark || sentThisPage.has(storageKey + mark)) return;
349
+ let status;
350
+ try {
351
+ const body = JSON.stringify({ v: 1, sdk: { name: SDK_NAME, version: "0.3.0" }, environment, release });
352
+ const response = await fetch(url, { method: "POST", body, headers: { "Content-Type": "text/plain;charset=UTF-8" }, keepalive: true, credentials: "omit" });
353
+ ({ status } = response);
354
+ } catch (error) {
355
+ logger.debug("heartbeat not sent", error);
356
+ return;
357
+ }
358
+ if (status === 429 || status >= 500) return;
359
+ try {
360
+ localStorage.setItem(storageKey, mark);
361
+ } catch {
362
+ sentThisPage.add(storageKey + mark);
363
+ }
364
+ }
365
+
309
366
  // src/instrument/common.ts
310
367
  var ORIGINAL = "__collector_original__";
311
368
  function wrap(obj, name, make) {
@@ -845,6 +902,8 @@ var Client = class {
845
902
  if (rejected.has(url)) {
846
903
  this.transport.disabled = true;
847
904
  this.logger.debug("Collector already rejected this DSN on this page: nothing will be sent");
905
+ } else if (options.heartbeat !== false) {
906
+ this.cleanups.push(scheduleHeartbeat({ url: endpoint.heartbeatUrl, key: endpoint.key, fetch: fetchFn, options, logger: this.logger }));
848
907
  }
849
908
  const auto = options.autoCapture;
850
909
  if (auto !== false) {
@@ -963,7 +1022,7 @@ var Client = class {
963
1022
  };
964
1023
 
965
1024
  // src/index.ts
966
- var SDK_VERSION = "0.2.0";
1025
+ var SDK_VERSION = "0.3.0";
967
1026
  var client;
968
1027
  var pendingScope = new Scope();
969
1028
  function scope() {
package/dist/index.d.cts CHANGED
@@ -95,6 +95,12 @@ interface CollectorOptions {
95
95
  breadcrumbs?: Partial<Record<BreadcrumbKind, boolean>>;
96
96
  /** How many breadcrumbs to keep. Default `50`. */
97
97
  maxBreadcrumbs?: number;
98
+ /**
99
+ * Tell Collector once a day that the app is running (its release and
100
+ * environment, nothing about the user), so it shows as set up before any
101
+ * error. Default `true`.
102
+ */
103
+ heartbeat?: boolean;
98
104
  /** Report uncaught errors and unhandled promise rejections. Default: both on. */
99
105
  autoCapture?: false | {
100
106
  onerror?: boolean;
package/dist/index.d.ts CHANGED
@@ -95,6 +95,12 @@ interface CollectorOptions {
95
95
  breadcrumbs?: Partial<Record<BreadcrumbKind, boolean>>;
96
96
  /** How many breadcrumbs to keep. Default `50`. */
97
97
  maxBreadcrumbs?: number;
98
+ /**
99
+ * Tell Collector once a day that the app is running (its release and
100
+ * environment, nothing about the user), so it shows as set up before any
101
+ * error. Default `true`.
102
+ */
103
+ heartbeat?: boolean;
98
104
  /** Report uncaught errors and unhandled promise rejections. Default: both on. */
99
105
  autoCapture?: false | {
100
106
  onerror?: boolean;
package/dist/index.js CHANGED
@@ -8,7 +8,8 @@ function parseDsn(dsn) {
8
8
  }
9
9
  if (parsed.protocol !== "https:" && parsed.protocol !== "http:" || parsed.username === "") return null;
10
10
  const base = parsed.origin + parsed.pathname.replace(/\/+$/, "");
11
- return { url: `${base}/api/events?key=${parsed.username}`, base };
11
+ const key = parsed.username;
12
+ return { url: `${base}/api/events?key=${key}`, heartbeatUrl: `${base}/api/heartbeats?key=${key}`, base, key };
12
13
  }
13
14
 
14
15
  // src/util.ts
@@ -201,6 +202,13 @@ function eventFromMessage(message, env, ctx) {
201
202
  function injectedRelease() {
202
203
  return typeof __COLLECTOR_RELEASE__ === "string" ? __COLLECTOR_RELEASE__ : void 0;
203
204
  }
205
+ function releaseOf(options) {
206
+ const release = options.release ?? injectedRelease();
207
+ return release === "" ? void 0 : release;
208
+ }
209
+ function environmentOf(options) {
210
+ return typeof options.environment === "string" && options.environment !== "" ? options.environment : void 0;
211
+ }
204
212
  function baseEvent({ options, scope: scope2, href, userAgent }, ctx = {}) {
205
213
  const event = {
206
214
  v: 1,
@@ -209,11 +217,12 @@ function baseEvent({ options, scope: scope2, href, userAgent }, ctx = {}) {
209
217
  platform: "javascript",
210
218
  level: toLevel(ctx.level),
211
219
  request: { url: options.stripQuery === false ? href : stripUrl(href), headers: { "user-agent": userAgent } },
212
- sdk: { name: SDK_NAME, version: "0.2.0" }
220
+ sdk: { name: SDK_NAME, version: "0.3.0" }
213
221
  };
214
- if (typeof options.environment === "string" && options.environment !== "") event.environment = options.environment;
215
- const release = options.release ?? injectedRelease();
216
- if (release !== void 0 && release !== "") event.release = release;
222
+ const environment = environmentOf(options);
223
+ if (environment !== void 0) event.environment = environment;
224
+ const release = releaseOf(options);
225
+ if (release !== void 0) event.release = release;
217
226
  if (ctx.fingerprint && ctx.fingerprint.length > 0) event.fingerprint = ctx.fingerprint.map(String);
218
227
  const user = ctx.user ? { id: String(ctx.user.id) } : scope2.user;
219
228
  if (user) event.user = user;
@@ -304,6 +313,54 @@ function errorFromLocation(e) {
304
313
  return error;
305
314
  }
306
315
 
316
+ // src/heartbeat.ts
317
+ var HEARTBEAT_DELAY_MS = 5e3;
318
+ var STORAGE_PREFIX = "collector:heartbeat:";
319
+ var sentThisPage = /* @__PURE__ */ new Set();
320
+ function scheduleHeartbeat(deps) {
321
+ let idle;
322
+ const run = () => {
323
+ void beat(deps);
324
+ };
325
+ const timer = setTimeout(() => {
326
+ if ("requestIdleCallback" in globalThis) idle = requestIdleCallback(run, { timeout: HEARTBEAT_DELAY_MS });
327
+ else run();
328
+ }, HEARTBEAT_DELAY_MS);
329
+ return () => {
330
+ clearTimeout(timer);
331
+ if (idle !== void 0) cancelIdleCallback(idle);
332
+ };
333
+ }
334
+ function storedMark(storageKey) {
335
+ try {
336
+ return localStorage.getItem(storageKey);
337
+ } catch {
338
+ return null;
339
+ }
340
+ }
341
+ async function beat({ url, key, fetch, options, logger }) {
342
+ const release = releaseOf(options);
343
+ const environment = environmentOf(options);
344
+ const storageKey = STORAGE_PREFIX + key;
345
+ const mark = JSON.stringify([(/* @__PURE__ */ new Date()).toISOString().slice(0, 10), release ?? "", environment ?? ""]);
346
+ if (storedMark(storageKey) === mark || sentThisPage.has(storageKey + mark)) return;
347
+ let status;
348
+ try {
349
+ const body = JSON.stringify({ v: 1, sdk: { name: SDK_NAME, version: "0.3.0" }, environment, release });
350
+ const response = await fetch(url, { method: "POST", body, headers: { "Content-Type": "text/plain;charset=UTF-8" }, keepalive: true, credentials: "omit" });
351
+ ({ status } = response);
352
+ } catch (error) {
353
+ logger.debug("heartbeat not sent", error);
354
+ return;
355
+ }
356
+ if (status === 429 || status >= 500) return;
357
+ try {
358
+ localStorage.setItem(storageKey, mark);
359
+ } catch {
360
+ sentThisPage.add(storageKey + mark);
361
+ }
362
+ }
363
+
307
364
  // src/instrument/common.ts
308
365
  var ORIGINAL = "__collector_original__";
309
366
  function wrap(obj, name, make) {
@@ -843,6 +900,8 @@ var Client = class {
843
900
  if (rejected.has(url)) {
844
901
  this.transport.disabled = true;
845
902
  this.logger.debug("Collector already rejected this DSN on this page: nothing will be sent");
903
+ } else if (options.heartbeat !== false) {
904
+ this.cleanups.push(scheduleHeartbeat({ url: endpoint.heartbeatUrl, key: endpoint.key, fetch: fetchFn, options, logger: this.logger }));
846
905
  }
847
906
  const auto = options.autoCapture;
848
907
  if (auto !== false) {
@@ -961,7 +1020,7 @@ var Client = class {
961
1020
  };
962
1021
 
963
1022
  // src/index.ts
964
- var SDK_VERSION = "0.2.0";
1023
+ var SDK_VERSION = "0.3.0";
965
1024
  var client;
966
1025
  var pendingScope = new Scope();
967
1026
  function scope() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chrom-ar/collector",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Reports your browser app's errors to Collector.",
5
5
  "keywords": [
6
6
  "error-tracking",
@@ -11,6 +11,11 @@
11
11
  "collector"
12
12
  ],
13
13
  "license": "MIT",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/chrom-ar/collector-js.git",
17
+ "directory": "packages/collector"
18
+ },
14
19
  "type": "module",
15
20
  "sideEffects": false,
16
21
  "main": "./dist/index.cjs",