@edraj/sauron-browser 1.0.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/README.md ADDED
@@ -0,0 +1,109 @@
1
+ # @edraj/sauron-browser
2
+
3
+ Browser SDK for **Sauron** — error reporting + product analytics in one small
4
+ package. Captures uncaught errors and unhandled promise rejections
5
+ automatically, records breadcrumbs, exposes `track()` / `identify()`, and
6
+ batches → gzips → queues envelopes (offline-safe) before POSTing them to the
7
+ Sauron ingest gateway.
8
+
9
+ - Zero-config auto-instrumentation: `window.onerror`, `onunhandledrejection`,
10
+ `console`, DOM clicks, `fetch`, `XMLHttpRequest`, and SPA history navigation.
11
+ - One runtime dependency (`fflate`, used only as a gzip fallback).
12
+ - Ships ESM + CJS + type definitions. `sideEffects: false`, tree-shakeable.
13
+
14
+ ## Install
15
+
16
+ ```bash
17
+ npm install @edraj/sauron-browser
18
+ ```
19
+
20
+ ## Quick start
21
+
22
+ ```ts
23
+ import { Sauron } from '@edraj/sauron-browser';
24
+
25
+ Sauron.init({
26
+ dsn: 'https://pk_test@ingest.sauron.dev/42',
27
+ environment: 'production',
28
+ release: 'web@1.4.2',
29
+ sampleRate: 1, // fraction of errors to send
30
+ maxBreadcrumbs: 50,
31
+ beforeSend(item) { // PII escape hatch — return null to drop
32
+ return item;
33
+ },
34
+ });
35
+
36
+ Sauron.identify('u_123', { plan: 'pro' });
37
+ Sauron.track('checkout_completed', { cart_value: 42.5 });
38
+
39
+ try {
40
+ doRiskyThing();
41
+ } catch (err) {
42
+ Sauron.captureException(err);
43
+ }
44
+ ```
45
+
46
+ ## API
47
+
48
+ | Function | Description |
49
+ | --- | --- |
50
+ | `init(options)` | Initialize the SDK (idempotent). |
51
+ | `captureException(err, hint?)` | Report an exception or any thrown value. |
52
+ | `captureMessage(msg, level?)` | Report a plain message. |
53
+ | `track(name, props?)` | Record a product-analytics event. |
54
+ | `trackTransaction(input)` | Record a performance transaction (navigation / http / screen load). |
55
+ | `identify(id, traits?)` | Associate the session with a user. |
56
+ | `addBreadcrumb(crumb)` | Manually add a breadcrumb. |
57
+ | `setUser(user \| null)` | Set or clear the current user. |
58
+ | `flush(timeoutMs?)` | Send everything pending; resolves `false` on timeout. |
59
+ | `close(timeoutMs?)` | Flush, then restore all patched globals. |
60
+
61
+ ### `init` options
62
+
63
+ ```ts
64
+ Sauron.init({
65
+ dsn: string, // https://<public_key>@<host>/<project_id>
66
+ environment?: string, // default "production"
67
+ release?: string, // e.g. "web@1.4.2"
68
+ sampleRate?: number, // default 1
69
+ maxBreadcrumbs?: number, // default 50
70
+ beforeSend?: (item, hint) => item | null,
71
+ beforeBreadcrumb?: (crumb, hint) => crumb | null,
72
+ transport?: {
73
+ flushIntervalMs?: number, // default 5000
74
+ maxBatch?: number, // default 30
75
+ maxQueueBytes?: number, // default 1048576
76
+ },
77
+ performance?: boolean, // auto-capture perf transactions (opt-in), default false
78
+ debug?: boolean, // default false
79
+ });
80
+ ```
81
+
82
+ ## Wire contract
83
+
84
+ The SDK POSTs a canonical envelope to `POST /api/{project_id}/envelope`:
85
+
86
+ ```
87
+ Content-Type: application/json
88
+ Content-Encoding: gzip # only when compressed (payloads ≳ 1 KB)
89
+ X-Sauron-Key: <public_key>
90
+ ```
91
+
92
+ On page unload, the pending batch is delivered via `navigator.sendBeacon` to
93
+ `POST /api/{project_id}/envelope?k=<public_key>` (uncompressed JSON blob).
94
+
95
+ The envelope shape (`header` + `context` + `items[]`) is identical across the
96
+ JavaScript, Flutter, and Rust implementations — see `src/types.ts`.
97
+
98
+ ## Development
99
+
100
+ ```bash
101
+ npm install
102
+ npm run typecheck # tsc --noEmit
103
+ npm run build # tsup -> dist/ (esm + cjs + d.ts)
104
+ npm test # vitest
105
+ ```
106
+
107
+ ## License
108
+
109
+ AGPL-3.0-only — GNU Affero General Public License v3.0.