@extn/segi-js 0.2.17 → 0.2.18

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 CHANGED
@@ -1,15 +1,21 @@
1
1
  # @extn/segi-js
2
2
 
3
- Browser JavaScript SDK for [segi](https://segi.extn.ai) — error monitoring for teams that ship.
3
+ Browser JavaScript SDK for [segi](https://segi.extn.ai) — error monitoring and session recording for teams that ship.
4
4
 
5
5
  ## Install
6
6
 
7
7
  ```sh
8
+ npm install @extn/segi-js
9
+ # or
8
10
  pnpm add @extn/segi-js
9
- # or: npm install @extn/segi-js
10
- # or: yarn add @extn/segi-js
11
+ # or
12
+ yarn add @extn/segi-js
11
13
  ```
12
14
 
15
+ Minimum published versions for the features below: `enabled`, `Segi.withReplay`,
16
+ CDN declarative replay and History-based page tracking need **@extn/segi-js 0.2.18**.
17
+ The dashboard recording deny-list needs 0.2.16 or later.
18
+
13
19
  ## Use
14
20
 
15
21
  ```ts
@@ -25,17 +31,45 @@ Segi.init({
25
31
  // anywhere
26
32
  Segi.captureException(new Error("boom"));
27
33
  Segi.captureMessage("checkout finished", { level: "info" });
28
- Segi.setUser({ id: "user_123", email: "alice@example.com" }); // attaches to error events AND the session recording (0.2.14+); Segi.setUser(null) on logout
34
+ Segi.setUser({ id: "user_123", email: "alice@example.com" }); // error events AND the session recording; Segi.setUser(null) on logout
29
35
  Segi.addBreadcrumb({ type: "click", message: "checkout button", timestamp: new Date().toISOString() });
30
36
  ```
31
37
 
32
- `Segi.init()` uses the first configuration. To add replay, merge it into the existing initializer; a second call does not enable replay.
38
+ ### First init wins
39
+
40
+ `Segi.init()` is idempotent: the **first** call configures the SDK and every
41
+ later call is ignored with a console warning, unless you pass
42
+ `Segi.init(options, { force: true })`. Put everything — including `replay` —
43
+ into one initializer. If a later call asks for replay and the first one did
44
+ not, the warning says so explicitly ("init() was already called WITHOUT
45
+ session replay …").
46
+
47
+ ### Only in production: `enabled`
48
+
49
+ ```ts
50
+ Segi.init({
51
+ projectKey: "segi_pk_live_...",
52
+ enabled: process.env.NODE_ENV === "production", // default true
53
+ });
54
+ ```
55
+
56
+ `enabled: false` makes init a complete no-op: no network requests, no
57
+ recorder, no global error listeners, and no "already initialized" lock — a
58
+ later `Segi.init({ ..., enabled: true })` works normally. All public methods
59
+ (`captureException`, `setUser`, `Segi.replay.*`, …) stay safe to call.
60
+ `Segi.init({ enabled: false }, { force: true })` after a real init tears it
61
+ down (same as `Segi.close()`).
33
62
 
34
- `Segi.setUser({ id })` (0.2.14+) attributes both pipelines with one call: error events and the active session recording share the same system user id, so the dashboard can filter and cross-link them per user. Pass the id as a string (`String(user.id)` — numbers and GUIDs are fine; matching is exact). Calls made before the recorder finishes activating are buffered and applied automatically.
63
+ `Segi.setUser({ id })` attributes both pipelines with one call: error events
64
+ and the active session recording share the same system user id, so the
65
+ dashboard can filter and cross-link them per user. Pass the id as a string
66
+ (`String(user.id)`). Calls made before the recorder finishes activating are
67
+ buffered and applied automatically.
35
68
 
36
- ## Session recording
69
+ ## Session recording (npm)
37
70
 
38
- A separate, tree-shakeable replay module ships alongside the error SDK — importing only `Segi` won't pull rrweb. Opt in per app, gated by your plan's recording quota (Free 30 / Team 100 / Enterprise ∞ per organization per month (KST)).
71
+ The replay module is loaded with a dynamic import only when you enable it, so
72
+ importing only `Segi` does not pull rrweb into your main chunk.
39
73
 
40
74
  ```ts
41
75
  import Segi from "@extn/segi-js";
@@ -44,7 +78,6 @@ import { withReplay } from "@extn/segi-js/replay";
44
78
  Segi.init({
45
79
  projectKey: "segi_pk_live_...",
46
80
  replay: withReplay({
47
- enabled: true,
48
81
  sample: 100, // 0–100 probabilistic gate
49
82
  blockRoutes: ["/checkout", "/auth"],
50
83
  mask: { inputs: "all", class: "segi-mask" },
@@ -56,14 +89,24 @@ Segi.init({
56
89
  // Segi.replay.resume();
57
90
  ```
58
91
 
92
+ `Segi.withReplay(options)` is the same helper, available on the `Segi` object
93
+ itself. Always build the `replay` value with one of these helpers; a value that
94
+ is not a replay token is ignored (replay stays off, error capture keeps
95
+ working).
96
+
59
97
  An OWNER or ADMIN enables recording in the dashboard. Input mask modes:
60
98
  `all` (default: all inputs), `sensitive` (password/email/tel/number), `none`
61
99
  (password only). Use `segi-mask` to exclude DOM regions and SDK `blockRoutes`
62
- for path prefixes; the console deny-list additionally requires segi-js 0.2.16 / segi-nextjs 0.2.11 and the updated server. The recorder pauses after 3 minutes idle.
100
+ for path prefixes; the dashboard recording deny-list is applied in addition.
101
+ The recorder pauses after 3 minutes idle and stops 30 minutes after the
102
+ session started.
103
+
104
+ ## CDN (`<script>` tag)
63
105
 
64
- ## CDN
106
+ The CDN bundle includes **both** error capture and session recording (rrweb is
107
+ inlined). It exposes `window.Segi`.
65
108
 
66
- Drop a `<script>` tag and skip the bundler:
109
+ ### Errors only
67
110
 
68
111
  ```html
69
112
  <script>
@@ -72,22 +115,127 @@ Drop a `<script>` tag and skip the bundler:
72
115
  <script src="https://cdn.extn.ai/segi/segi.min.js" async></script>
73
116
  ```
74
117
 
75
- The CDN bundle is error-only; use npm for replay.
118
+ or, without a config object:
119
+
120
+ ```html
121
+ <script src="https://cdn.extn.ai/segi/segi.min.js" data-project-key="segi_pk_live_..." async></script>
122
+ ```
123
+
124
+ ### Errors + session recording
125
+
126
+ ```html
127
+ <script>
128
+ window.SegiConfig = {
129
+ projectKey: "segi_pk_live_...",
130
+ replay: { sample: 20, blockRoutes: ["/account"], mask: { inputs: "all" } }, // or replay: true
131
+ };
132
+ </script>
133
+ <script src="https://cdn.extn.ai/segi/segi.min.js" async></script>
134
+ ```
135
+
136
+ or with script attributes:
137
+
138
+ ```html
139
+ <script src="https://cdn.extn.ai/segi/segi.min.js"
140
+ data-project-key="segi_pk_live_..."
141
+ data-replay="true"
142
+ data-replay-sample="20"
143
+ async></script>
144
+ ```
145
+
146
+ `window.SegiConfig` fields: `projectKey`, `endpoint`, `environment`, `release`,
147
+ `enabled`, `beforeSend`, `maxBreadcrumbs`, and `replay` (`true` or an options
148
+ object: `sample`, `blockRoutes`, `mask`, `trackRoutes`, `endpoint`).
149
+ Script attributes: `data-project-key`, `data-endpoint`, `data-environment`,
150
+ `data-release`, `data-replay` (`"true"`), `data-replay-sample` (0–100).
151
+ `SegiConfig` wins over attributes; `SegiConfig.replay` (even `false`) wins over
152
+ `data-replay`.
153
+
154
+ On the CDN path, SPA navigations made through the History API
155
+ (`pushState`/`replaceState`/`popstate`) are reported as page views
156
+ automatically (`trackRoutes`, default `true` on the CDN; set
157
+ `replay: { trackRoutes: false }` if you call `Segi.replay.notifyRouteChange()`
158
+ yourself).
76
159
 
160
+ ### The first-init-wins trap
161
+
162
+ When `SegiConfig.projectKey` or `data-project-key` is present, the bundle
163
+ initializes itself **as soon as it loads**. That auto-init is the first init;
164
+ a later `Segi.init({ ..., replay })` is ignored with a warning. Either:
165
+
166
+ - configure replay declaratively (`SegiConfig.replay` / `data-replay`) — recommended, or
167
+ - omit `projectKey` from `SegiConfig`/the tag and call init yourself once:
168
+
169
+ ```html
170
+ <script src="https://cdn.extn.ai/segi/segi.min.js"></script>
171
+ <script>
172
+ Segi.init({ projectKey: "segi_pk_live_...", replay: Segi.withReplay({ sample: 20 }) });
173
+ </script>
174
+ ```
175
+
176
+ ### Version-pinned URL and SRI
177
+
178
+ `https://cdn.extn.ai/segi/segi.min.js` always serves the latest release (short
179
+ cache). For a pinned, immutable file that works with Subresource Integrity, use
180
+ jsDelivr (served from the published npm package):
181
+
182
+ ```html
183
+ <script src="https://cdn.jsdelivr.net/npm/@extn/segi-js@0.2.18/dist/cdn.global.js"
184
+ integrity="sha384-…" crossorigin="anonymous"
185
+ data-project-key="segi_pk_live_..." async></script>
186
+ ```
187
+
188
+ jsDelivr shows the SRI hash for each file. If you self-host or use jsDelivr,
189
+ add that host to `script-src` instead of `cdn.extn.ai`.
190
+
191
+ ## Content Security Policy
192
+
193
+ | Directive | Value | When |
194
+ | --- | --- | --- |
195
+ | `connect-src` | `https://segiapi.extn.ai` | Always (errors and recording). The SDK never calls `cdn.extn.ai`. |
196
+ | `script-src` | `https://cdn.extn.ai` | Only for the CDN `<script>` tag (or `https://cdn.jsdelivr.net` for the pinned URL). |
197
+
198
+ No `worker-src`, `blob:` or `wasm-unsafe-eval` is needed: canvas recording is
199
+ off, so rrweb never starts a worker. If you set `endpoint` for self-hosting,
200
+ allow that origin in `connect-src` instead.
201
+
202
+ ## Recording quota
203
+
204
+ Recordings are counted per organization per month (KST): Free 30, Team 100,
205
+ Enterprise unlimited. A session is counted when its first replay chunk is
206
+ stored. Once the quota is used up the server answers replay uploads with
207
+ `429 quota_exceeded`; the SDK then stops the recorder for the rest of that
208
+ page view (a later full page load tries once and is refused the same way).
209
+ Nothing is recorded until the quota resets. Error events are unaffected (they
210
+ have their own event quota).
211
+
212
+ On the Free plan, keep a public marketing site from burning the 30 recordings
213
+ in a day: use a low `sample`, e.g. `sample: 5` (≈ 1 in 20 visits), and raise it
214
+ only on pages where you need replays. Sampling is rolled on every page load.
215
+
216
+ ## Static export (`output: 'export'`) / SSG-only sites
217
+
218
+ Static sites have no server runtime: initialize in the browser only (npm init
219
+ or the CDN tag). There is no server-side init and server errors are not
220
+ collected (there is no server).
221
+
222
+ ## Source maps
223
+
224
+ Source-map upload is **not supported yet**. Stack traces show the deployed
225
+ (minified) code. `release` is only a tag on events.
77
226
 
78
227
  ## Options and SDK support
79
228
 
80
229
  | Option | Default / usage | Support |
81
230
  | --- | --- | --- |
82
231
  | projectKey | Required project public key | Browser, Next.js server, NestJS |
232
+ | enabled | `true`; `false` = complete no-op (0.2.18+) | All three |
83
233
  | environment | Optional label, e.g. development or production | All three |
84
234
  | release | Optional application version or commit ID | All three |
85
- | beforeSend | Synchronous event transform; return null to drop | Browser only |
235
+ | beforeSend | Synchronous event transform; return null to drop | Browser; Next.js server (0.2.13+); NestJS (0.3.0+) |
86
236
  | maxBreadcrumbs | 50; use a nonnegative integer, 0 retains none | Browser only |
87
237
 
88
- Next.js server and NestJS accept the shared option type, but do not execute
89
- beforeSend or retain breadcrumbs. Filter and sanitize data before calling
90
- server capture functions. beforeSend does not mask DOM recordings.
238
+ beforeSend does not mask DOM recordings.
91
239
 
92
240
  Browser captureException/captureMessage accept event context overrides such
93
241
  as level, tags, extra and user. Server captureException/captureMessage accept
@@ -100,72 +248,52 @@ Strings and numbers become the message; objects with a message (and optional
100
248
  name/code/stack) keep those fields; event-like wrappers (`{ error }`, `{ reason }`)
101
249
  are unwrapped; plain data is serialised. A value with nothing to report — `{}`,
102
250
  `undefined`, `null`, an empty array — is dropped unless you pass
103
- `{ message }` in the context, so a third-party callback handing you an empty
104
- object no longer produces an "Error: {}" issue. `normalizeError(value)` is
105
- exported if you want the same conversion in your own code.
106
-
107
- ```ts
108
- // Browser: include maxBreadcrumbs in your single initialization.
109
- Segi.init({ projectKey: "segi_pk_live_xxxxx", maxBreadcrumbs: 30 });
110
- Segi.addBreadcrumb({ type: "navigation", message: "Checkout opened",
111
- timestamp: new Date().toISOString() });
112
- Segi.setUser({ id: String(user.id) }); // after login
113
- Segi.setUser(null); // after logout
114
- ```
251
+ `{ message }` in the context. `normalizeError(value)` is exported if you want
252
+ the same conversion in your own code.
115
253
 
116
254
  ## Recording option reference
117
255
 
118
256
  | Option | Default / behavior |
119
257
  | --- | --- |
120
- | enabled | withReplay only; true by default. false skips replay activation, not errors. Project recording must also be enabled by OWNER/ADMIN. |
121
- | sample | 100; probability from 0 to 100 percent. 20 selects approximately 20%. Initialization rolls again, including reloads; this is not error sampling. |
122
- | blockRoutes | Empty array; pathname prefix match. /auth also matches /author. Configure before sensitive routes. |
258
+ | enabled | `true`. `false` skips replay activation, not errors. Project recording must also be enabled by OWNER/ADMIN. |
259
+ | sample | 100; probability from 0 to 100 percent. 20 selects approximately 20%. Rolled again on every page load; this is not error sampling. |
260
+ | blockRoutes | Empty array; pathname prefix match. /auth also matches /author. Combined with the dashboard deny-list. |
123
261
  | mask.inputs | all by default; sensitive masks password/email/tel/number; none still masks passwords. |
124
262
  | mask.class | segi-mask; blocks the matching DOM region and descendants. |
125
- | user | Next.js provider: { id, email? }; null clears identity; undefined leaves manual Segi.setUser management unchanged. |
126
-
127
- SegiReplayProvider does not expose beforeSend/maxBreadcrumbs props. Use a
128
- single Segi.init with withReplay if you need these browser options and replay.
129
- SegiReplayProvider applies configuration at initial mount. Changes to user
130
- are synchronized, but changing other props does not reinitialize the SDK.
263
+ | trackRoutes | Automatic page_view on History API navigation. `true` on the CDN path, `false` for npm imports (0.2.18+). |
264
+ | endpoint | Overrides the API base for replay traffic only (self-hosting). |
131
265
 
132
266
  ```ts
133
- // Add these options to the existing withReplay call, not another initializer.
134
- replay: withReplay({ sample: 20, blockRoutes: ["/auth", "/account/payment"],
135
- mask: { inputs: "all", class: "segi-mask" } })
136
-
137
267
  Segi.replay.pause();
138
268
  Segi.replay.resume();
139
269
  Segi.replay.event("checkout_started", { itemCount: 2 });
140
- Segi.replay.notifyRouteChange(); // after non-Next.js SPA navigation
270
+ Segi.replay.notifyRouteChange(); // after SPA navigation when trackRoutes is off
141
271
  ```
142
272
 
143
- pause/resume control DOM recording, not all error capture, and do not delete
273
+ pause/resume control DOM recording, not error capture, and do not delete
144
274
  previously collected data. Use blockRoutes for persistent route exclusions.
145
275
  Custom event names are limited to 64 characters; meta must not contain passwords
146
276
  or payment details. Events send only while replay is active and unpaused.
147
- The Next.js provider reports pathname changes automatically. Other SPAs must
148
- notify the SDK after the URL changes for page tracking and exclusions.
149
- Starting on a blocked route defers replay. After rendering an allowed SPA page, notifyRouteChange starts it. Manual pause persists across navigation.
277
+ Repeated notifications for the same URL do not create duplicate page views.
278
+ Starting on a blocked route defers replay; after navigating to an allowed page
279
+ it starts. Manual pause persists across navigation.
150
280
 
151
- ```tsx
152
- <SegiReplayProvider publicKey={process.env.NEXT_PUBLIC_SEGI_PROJECT_KEY!}
153
- user={user ? { id: String(user.id) } : null} />
154
- ```
281
+ ## Dashboard recording deny-list
282
+
283
+ The SDK reads the project's recording policy (on/off and the deny-list) before
284
+ recording, retries failures every 30 seconds, and records nothing until a valid
285
+ response arrives. Error monitoring is independent. Dashboard paths and SDK
286
+ `blockRoutes` are combined; policy changes take effect on the next full page
287
+ load. Prefix matching is case-sensitive. Saved data is not deleted. For routers
288
+ that render sensitive content before updating the URL, pause before the
289
+ transition or mask the content.
155
290
 
156
291
  ## Docs
157
292
 
158
293
  Full integration guide: <https://segi.extn.ai/docs>
159
294
 
160
-
161
295
  ## Licence
162
296
 
163
297
  Copyright (c) 2026 Extension Co., Ltd. All rights reserved. Use of this package
164
298
  is governed by the LICENSE file in this package and the segi service Terms of
165
299
  Service at https://segi.extn.ai/tnc — it is **not** open source.
166
-
167
- ## Console exclusion policy compatibility
168
-
169
- The source versions introducing console path exclusions are segi-js 0.2.16 and segi-nextjs 0.2.11; this does not certify npm publication. Deploy the updated server before upgrading the SDK. The SDK reads policy before collection, retries failures every 30 seconds, and records nothing until a valid response arrives. Error monitoring is independent.
170
-
171
- Console paths and SDK blockRoutes are combined. Policy changes take effect on the next full page load, not immediately in open tabs. Prefix matching is case-sensitive: /auth also matches /author. Saved data is not deleted. On SPA navigation, change the URL before rendering excluded DOM; after rendering an allowed page call notifyRouteChange (automatic in the Next.js Provider). For routers rendering sensitive content before updating the URL, pause before transition or mask the content. Saving settings does not confirm SDK adoption.