bugstack-sdk 2.2.2 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/CHANGELOG.md +57 -0
  2. package/README.md +117 -2
  3. package/dist/adapters/express/index.d.mts +2 -2
  4. package/dist/adapters/express/index.d.ts +2 -2
  5. package/dist/adapters/express/index.js +408 -42
  6. package/dist/adapters/express/index.js.map +1 -1
  7. package/dist/adapters/express/index.mjs +408 -42
  8. package/dist/adapters/express/index.mjs.map +1 -1
  9. package/dist/adapters/generic/index.d.mts +2 -2
  10. package/dist/adapters/generic/index.d.ts +2 -2
  11. package/dist/adapters/generic/index.js +456 -42
  12. package/dist/adapters/generic/index.js.map +1 -1
  13. package/dist/adapters/generic/index.mjs +453 -43
  14. package/dist/adapters/generic/index.mjs.map +1 -1
  15. package/dist/adapters/nextjs/index.d.mts +2 -2
  16. package/dist/adapters/nextjs/index.d.ts +2 -2
  17. package/dist/adapters/nextjs/index.js +408 -42
  18. package/dist/adapters/nextjs/index.js.map +1 -1
  19. package/dist/adapters/nextjs/index.mjs +408 -42
  20. package/dist/adapters/nextjs/index.mjs.map +1 -1
  21. package/dist/browser/index.d.mts +131 -0
  22. package/dist/browser/index.d.ts +131 -0
  23. package/dist/browser/index.js +992 -0
  24. package/dist/browser/index.js.map +1 -0
  25. package/dist/browser/index.mjs +984 -0
  26. package/dist/browser/index.mjs.map +1 -0
  27. package/dist/{client-DK_ItP55.d.mts → client-B9WaTEYj.d.mts} +148 -1
  28. package/dist/{client-DK_ItP55.d.ts → client-B9WaTEYj.d.ts} +148 -1
  29. package/dist/index.d.mts +2 -2
  30. package/dist/index.d.ts +2 -2
  31. package/dist/index.js +468 -54
  32. package/dist/index.js.map +1 -1
  33. package/dist/index.mjs +465 -55
  34. package/dist/index.mjs.map +1 -1
  35. package/package.json +17 -7
package/CHANGELOG.md CHANGED
@@ -3,6 +3,63 @@
3
3
  All notable changes to `bugstack-sdk` are documented here.
4
4
  Format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
5
5
 
6
+ ## [2.4.0] — unreleased
7
+
8
+ ### Added
9
+ - **Browser capture: `bugstack-sdk/browser`.** A dependency-free entry for
10
+ the customer's page: uncaught errors and unhandled rejections, failed
11
+ requests (5xx or no answer) and the route trail, sent to Bugstack's session
12
+ ingest with the repository's public BROWSER key (never the server key).
13
+ Nothing runs until you call `init()`; it is a no-op on the server and
14
+ never throws. Ported from the Bugstack monorepo's `@bugstack/browser`,
15
+ which was never published.
16
+ - **Frustration signals, OFF by default.** `init({ key, frustration: true })`
17
+ also records rage clicks (3+ on one element within 1 s), dead clicks (a
18
+ button or link that changed nothing within 1 s, or a link that landed on a
19
+ not-found page), error clicks (a click followed within 1 s by an uncaught
20
+ error), abandoned forms (a submit that hit a validation error, then left
21
+ without a submit that passed) and thrashing (4+ route changes in 10 s
22
+ alternating between two routes). Each carries a redacted element
23
+ descriptor: a stable selector (tag, id, stable classes, `data-testid`) and
24
+ a visible label cut to 60 characters. Never an input's value, a query
25
+ string or a request body. `frustration: { sampleRate, maxPerPage }` samples
26
+ sessions (once per session) and caps events per page view (default 20).
27
+ The DOM observer the dead-click check needs is installed only when
28
+ frustration is on.
29
+
30
+ ### Changed
31
+ - `npm run build` cleans `dist` once before tsup (two build configs now).
32
+
33
+ ## [2.3.0] — 2026-11-23
34
+
35
+ ### Added
36
+ - **Caught-and-logged errors reach Bugstack.** A `catch` block that logs
37
+ an error with `console.error` and carries on was invisible: only uncaught
38
+ exceptions and the adapters' own wrappers reported anything. Once the
39
+ client is initialised, a `console.error` call carrying an `Error` (or a
40
+ string holding a stack trace) is now reported, anchored at the line that
41
+ logged it, with the original error as `metadata.causeName` /
42
+ `metadata.causeStack`. On by default; `captureConsoleErrors: false` turns
43
+ it off, and an options object tunes it (`messages`, `maxPerMinute`,
44
+ `repeatWindowMs`, `ignore`). The console line is written exactly as before.
45
+ Plain message lines, Node's process warnings and the SDK's own lines stay
46
+ console lines; forwards are capped at 20 a minute and once per site and
47
+ message per 5 minutes, then deduplicated by fingerprint as usual; logged
48
+ text is redacted with `redactFields`; forwarding never throws.
49
+ - **pino and winston.** `bugstackPinoHooks()` (pino `hooks`) and
50
+ `bugstackWinstonFormat()` (first in a winston format chain) forward
51
+ error-level logs under the same rules.
52
+ - **`markErrorReported(error)`** says an error was already sent, so the
53
+ console line for it is not sent again. The Next.js, Express and generic
54
+ adapters call it, so an error they report is not reported a second time
55
+ when the framework logs it. **`loggedErrorStats()`** returns forwarded and
56
+ dropped counts per rule.
57
+
58
+ ### Fixed
59
+ - **Windows paths are app frames.** The stack parser treated any file path
60
+ without a `/` as Node-internal, so on Windows no frame was an app frame and
61
+ `metadata.sourceLocation` was never set.
62
+
6
63
  ## [2.2.2] — 2026-10-29
7
64
 
8
65
  ### Fixed
package/README.md CHANGED
@@ -2,13 +2,14 @@
2
2
 
3
3
  The [Bugstack](https://bugstack.ai) SDK for Node.js: Next.js, Express and any Node process.
4
4
 
5
- Bugstack is autonomous software maintenance. It watches your app and your repository, fixes what breaks, and sends every change as a pull request you control. This SDK is how your app tells Bugstack about an uncaught server error: it sends the error, its stack trace and the route, and Bugstack takes it from there.
5
+ Bugstack is autonomous software maintenance. It watches your app and your repository, fixes what breaks, and sends every change as a pull request you control. This SDK is how your app tells Bugstack about a server error: an uncaught one, or one your code caught and logged with `console.error`. It sends the error, its stack trace and where it happened, and Bugstack takes it from there.
6
6
 
7
7
  **Get your API key at:** [dashboard.bugstack.ai](https://dashboard.bugstack.ai)
8
8
 
9
9
  ## Features
10
10
 
11
11
  - **Multiple frameworks** — First-class support for Next.js, Express, and generic Node.js
12
+ - **Caught-and-logged errors** — A `console.error(err)` in a `catch` block reaches Bugstack too, on by default; pino and winston with one line each
12
13
  - **Full error context** — Captures stack traces, request body, headers, query params, route params
13
14
  - **Automatic sanitization** — Sensitive data (passwords, tokens, API keys) is redacted automatically
14
15
  - **Non-blocking** — Error reporting is async and never slows down your responses
@@ -178,6 +179,116 @@ try {
178
179
 
179
180
  ---
180
181
 
182
+ ## Caught-and-Logged Errors
183
+
184
+ Most failures never reach an uncaught-exception handler. A `catch` block logs them and the app carries on:
185
+
186
+ ```typescript
187
+ try {
188
+ await grantCredits(order);
189
+ } catch (err) {
190
+ console.error("[billing] grant failed", err); // a log line nobody reads
191
+ }
192
+ ```
193
+
194
+ Once the client is initialised, the SDK forwards these to Bugstack. It is on by default, and the console line is written exactly as before.
195
+
196
+ **What is forwarded:** a `console.error` call that carries an `Error` (as an argument, or as the `err` / `error` field of an object argument), or a string holding a stack trace (`console.error("failed", err.stack)`). The report is anchored at the line that logged it, so each logging site is its own issue; the original error's name and stack ride along in `metadata.causeName` / `metadata.causeStack`. A `[billing]` style tag at the start of the line becomes the route (`log:billing`).
197
+
198
+ **What stays a console line:** a plain message with no error and no stack (unless you set `messages: true`), Node's own process warnings (`(node:1) [DEP0169] DeprecationWarning: …`), the SDK's own `[BugStack]` lines, and lines matching `ignore`. An error the SDK already reported (through `withErrorCapture`, `bugstackErrorHandler` or `captureError`) is not sent again when your framework logs it.
199
+
200
+ **Bounded:** at most 20 forwards a minute per process, the same site and message at most once per 5 minutes, then the usual fingerprint dedup. Logged text goes through the same `redactFields` as request data (`password=…`, `token: …`), and bearer tokens are masked. Only the message and stacks are sent, never the other log arguments. Forwarding never throws into your code.
201
+
202
+ ```typescript
203
+ ErrorCaptureClient.init({
204
+ apiKey: "your-api-key",
205
+
206
+ // captureConsoleErrors: false turns it off. To tune it instead
207
+ // (every field optional, defaults shown):
208
+ captureConsoleErrors: {
209
+ messages: false, // also forward console.error lines that carry only a message
210
+ maxPerMinute: 20,
211
+ repeatWindowMs: 5 * 60 * 1000,
212
+ ignore: [/ECONNRESET/], // strings or RegExps; matching lines stay console lines
213
+ },
214
+ });
215
+ ```
216
+
217
+ ### pino
218
+
219
+ ```typescript
220
+ import pino from "pino";
221
+ import { bugstackPinoHooks } from "bugstack-sdk";
222
+
223
+ const logger = pino({ hooks: bugstackPinoHooks() });
224
+
225
+ logger.error({ err, orderId }, "[checkout] charge failed"); // forwarded
226
+ ```
227
+
228
+ `error` and `fatal` logs are forwarded under the same rules. If you already use `hooks.logMethod`, call the returned `logMethod` from yours.
229
+
230
+ ### winston
231
+
232
+ ```typescript
233
+ import winston from "winston";
234
+ import { bugstackWinstonFormat } from "bugstack-sdk";
235
+
236
+ const logger = winston.createLogger({
237
+ format: winston.format.combine(bugstackWinstonFormat(), winston.format.json()),
238
+ transports: [new winston.transports.Console()],
239
+ });
240
+
241
+ logger.error("[upload] write failed", err); // forwarded
242
+ ```
243
+
244
+ Put `bugstackWinstonFormat()` first in the chain. `error` and higher levels are forwarded, and the log entry passes through unchanged.
245
+
246
+ `loggedErrorStats()` returns how many logged errors were forwarded and how many were left as log lines, counted per rule.
247
+
248
+ ---
249
+
250
+ ## Browser Capture and Frustration Signals
251
+
252
+ `bugstack-sdk/browser` runs in your users' browsers. It reports uncaught
253
+ errors, failed requests and the route trail that led there, and, if you opt
254
+ in, **frustration signals**: rage clicks, dead clicks, clicks followed by an
255
+ error, forms abandoned after a validation error, and rapid back-and-forth
256
+ between two pages. Bugstack groups them across sessions and opens an
257
+ incident only when many users hit the same element far more than the rest
258
+ of that page, then anchors it to the page and component that render it.
259
+
260
+ Use the repository's **browser key** from its settings in Bugstack. It is
261
+ public by design and accepted only from your allowed origins; never put the
262
+ server `apiKey` in the browser.
263
+
264
+ ```ts
265
+ // instrumentation-client.ts (Next.js 15.3+, App Router or Pages Router)
266
+ import { init } from "bugstack-sdk/browser";
267
+
268
+ init({
269
+ key: process.env.NEXT_PUBLIC_BUGSTACK_BROWSER_KEY!,
270
+ frustration: true, // OFF unless set
271
+ });
272
+ ```
273
+
274
+ On older Next.js versions, call `init()` once from a `"use client"`
275
+ component in the root layout (App Router) or inside `useEffect` in
276
+ `pages/_app.tsx` (Pages Router). `setUser({ id, email })` names the user;
277
+ Bugstack stores only a keyed hash of it.
278
+
279
+ | Option | Default | What it does |
280
+ |---|---|---|
281
+ | `key` | required | The repository's browser key |
282
+ | `sampleRate` | `1` | Share of sessions recorded at all, decided once per session |
283
+ | `frustration` | off | `true`, or `{ sampleRate, maxPerPage }`: share of recorded sessions that also record frustration, and the most frustration events per page view (default 20) |
284
+ | `release`, `environment` | none | Sent with every batch |
285
+
286
+ What is recorded: element selectors (tag, id, stable classes,
287
+ `data-testid`) and visible labels cut to 60 characters, route paths without
288
+ query strings, request method, path and status. Never input values, text you
289
+ type, query strings, headers or bodies. Every handler is wrapped: the SDK
290
+ never throws into your page and hands back the same responses and errors.
291
+
181
292
  ## Source Maps for Production (Next.js)
182
293
 
183
294
  By default, server-side errors in a Next.js production build come back with stack frames that point at compiled chunks like `.next/server/chunks/8393.js:1:8734`. The Bugstack orchestrator can't fix what it can't locate — so without source maps, those errors land in your dashboard as "Could not identify primary file from error" and never auto-fix.
@@ -309,6 +420,9 @@ ErrorCaptureClient.init({
309
420
  return true;
310
421
  },
311
422
 
423
+ // Caught-and-logged errors (console.error); see "Caught-and-Logged Errors"
424
+ captureConsoleErrors: true, // false to turn off, or an options object
425
+
312
426
  // Transform errors before sending
313
427
  beforeSend: (capturedError) => {
314
428
  capturedError.metadata = { ...capturedError.metadata, custom: "value" };
@@ -346,7 +460,7 @@ export const POST = withErrorCapture(
346
460
 
347
461
  ## Automatic Sanitization
348
462
 
349
- The SDK redacts sensitive data from headers, request bodies, and query params.
463
+ The SDK redacts sensitive data from headers, request bodies, and query params, and from the text of caught-and-logged errors.
350
464
 
351
465
  **Built-in redacted fields:**
352
466
  `password`, `secret`, `token`, `apikey`, `api_key`, `apiKey`, `authorization`, `auth`, `credential`, `private`, `credit_card`, `creditcard`, `card_number`, `cvv`, `ssn`, `social_security`
@@ -365,6 +479,7 @@ The SDK never interferes with your application:
365
479
  - **Network failures** — Silently retried (up to 5 times with exponential backoff)
366
480
  - **Timeouts** — Configurable, defaults to 5 seconds
367
481
  - **SDK errors** — Caught and logged in debug mode, never thrown to your app
482
+ - **Console behaviour preserved** — `console.error` writes exactly what it wrote before; forwarding happens after, one tick later
368
483
  - **Original error preserved** — The wrapper re-throws the original error so your framework handles the response normally
369
484
 
370
485
  ---
@@ -1,6 +1,6 @@
1
1
  import { ErrorRequestHandler, Request, Response, NextFunction } from 'express';
2
- import { E as ErrorCaptureConfig } from '../../client-DK_ItP55.mjs';
3
- export { B as BugStackClient, C as CapturedError, a as ErrorCaptureClient } from '../../client-DK_ItP55.mjs';
2
+ import { E as ErrorCaptureConfig } from '../../client-B9WaTEYj.mjs';
3
+ export { B as BugStackClient, C as CapturedError, a as ErrorCaptureClient } from '../../client-B9WaTEYj.mjs';
4
4
  import 'next/server';
5
5
 
6
6
  /**
@@ -1,6 +1,6 @@
1
1
  import { ErrorRequestHandler, Request, Response, NextFunction } from 'express';
2
- import { E as ErrorCaptureConfig } from '../../client-DK_ItP55.js';
3
- export { B as BugStackClient, C as CapturedError, a as ErrorCaptureClient } from '../../client-DK_ItP55.js';
2
+ import { E as ErrorCaptureConfig } from '../../client-B9WaTEYj.js';
3
+ export { B as BugStackClient, C as CapturedError, a as ErrorCaptureClient } from '../../client-B9WaTEYj.js';
4
4
  import 'next/server';
5
5
 
6
6
  /**