@spectosh/browser 0.2.1 → 0.2.3
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 +45 -3
- package/dist/react.cjs +1 -0
- package/dist/react.js +71 -0
- package/dist/specto-browser.cjs +1 -136
- package/dist/specto-browser.js +2 -8054
- package/dist/src-KEdhVklE.js +8199 -0
- package/dist/src-VtKEKQLX.cjs +138 -0
- package/package.json +28 -3
- package/src/destination.js +55 -0
- package/src/index.js +16 -9
- package/src/react.js +82 -0
- package/src/recorder.js +182 -64
- package/types/destination.d.ts +9 -0
- package/types/index.d.ts +113 -0
- package/types/react.d.ts +54 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spectosh/browser",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "Specto session-replay recorder for bundler users — rrweb DOM capture, console/network/error/rage signals, privacy-masked in the browser.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"publishConfig": {
|
|
@@ -14,27 +14,52 @@
|
|
|
14
14
|
"homepage": "https://specto.sh",
|
|
15
15
|
"main": "./dist/specto-browser.cjs",
|
|
16
16
|
"module": "./dist/specto-browser.js",
|
|
17
|
+
"types": "./types/index.d.ts",
|
|
17
18
|
"exports": {
|
|
18
19
|
".": {
|
|
20
|
+
"types": "./types/index.d.ts",
|
|
19
21
|
"import": "./dist/specto-browser.js",
|
|
20
22
|
"require": "./dist/specto-browser.cjs"
|
|
21
23
|
},
|
|
24
|
+
"./react": {
|
|
25
|
+
"types": "./types/react.d.ts",
|
|
26
|
+
"import": "./dist/react.js",
|
|
27
|
+
"require": "./dist/react.cjs"
|
|
28
|
+
},
|
|
22
29
|
"./recorder": "./src/recorder.js",
|
|
23
|
-
"./recorder-worker": "./src/recorder.worker.js"
|
|
30
|
+
"./recorder-worker": "./src/recorder.worker.js",
|
|
31
|
+
"./destination": {
|
|
32
|
+
"types": "./types/destination.d.ts",
|
|
33
|
+
"default": "./src/destination.js"
|
|
34
|
+
}
|
|
24
35
|
},
|
|
25
36
|
"files": [
|
|
26
37
|
"dist",
|
|
27
38
|
"src",
|
|
39
|
+
"types",
|
|
28
40
|
"README.md"
|
|
29
41
|
],
|
|
30
42
|
"sideEffects": false,
|
|
31
43
|
"scripts": {
|
|
32
|
-
"build": "vite build -c vite.config.js"
|
|
44
|
+
"build": "vite build -c vite.config.js",
|
|
45
|
+
"test": "node --test \"test/*.test.mjs\" && tsc -p test/types"
|
|
33
46
|
},
|
|
34
47
|
"dependencies": {
|
|
35
48
|
"rrweb": "^2.1.6"
|
|
36
49
|
},
|
|
50
|
+
"peerDependencies": {
|
|
51
|
+
"react": ">=17"
|
|
52
|
+
},
|
|
53
|
+
"peerDependenciesMeta": {
|
|
54
|
+
"react": {
|
|
55
|
+
"optional": true
|
|
56
|
+
}
|
|
57
|
+
},
|
|
37
58
|
"devDependencies": {
|
|
59
|
+
"@types/react": "^19.2.0",
|
|
60
|
+
"react": "^19.2.0",
|
|
61
|
+
"react-dom": "^19.2.0",
|
|
62
|
+
"typescript": "^5.9.3",
|
|
38
63
|
"vite": "^8.3.1"
|
|
39
64
|
},
|
|
40
65
|
"keywords": [
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// Where a project's recordings go, decided by its publishable key.
|
|
2
|
+
//
|
|
3
|
+
// pk_8f2a91c4 → the hosted service (specto.sh)
|
|
4
|
+
// pk_8f2a91c4@eu.specto.sh → its EU region
|
|
5
|
+
// pk_8f2a91c4@replay.acme.internal → a self-hosted instance
|
|
6
|
+
//
|
|
7
|
+
// A key carries its home, so the one value an install needs can't send data
|
|
8
|
+
// to the wrong place. `host` (an origin, optionally with a path) overrides
|
|
9
|
+
// it — e.g. to route through your own domain — and the legacy full
|
|
10
|
+
// `endpoint` overrides both. No DOM, no rrweb: safe to import anywhere.
|
|
11
|
+
export const DEFAULT_HOST = 'https://specto.sh'
|
|
12
|
+
|
|
13
|
+
// development hosts are plain http; everything else defaults to https
|
|
14
|
+
const LOCAL_HOST = /^(localhost|127(?:\.\d{1,3}){3}|\[::1\]|0\.0\.0\.0|[^/:]+\.localhost)(?::\d+)?(?:\/|$)/i
|
|
15
|
+
|
|
16
|
+
// 'pk_x@host' → { key: 'pk_x', host: 'host' }; a bare key has no host
|
|
17
|
+
export function parseKey(raw) {
|
|
18
|
+
const s = String(raw ?? '').trim()
|
|
19
|
+
const at = s.indexOf('@')
|
|
20
|
+
if (at <= 0) return { key: s, host: null }
|
|
21
|
+
return { key: s.slice(0, at), host: s.slice(at + 1).trim() || null }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// 'eu.specto.sh' → 'https://eu.specto.sh'; keeps a path (a host served
|
|
25
|
+
// under /specto), drops a trailing slash; throws on anything not http(s)
|
|
26
|
+
export function normalizeHost(h) {
|
|
27
|
+
let s = String(h ?? '').trim().replace(/\/+$/, '')
|
|
28
|
+
if (!s) throw new Error('Specto: host is empty')
|
|
29
|
+
if (!/^[a-z][a-z0-9+.-]*:\/\//i.test(s)) s = (LOCAL_HOST.test(s) ? 'http://' : 'https://') + s
|
|
30
|
+
let u
|
|
31
|
+
try { u = new URL(s) } catch { throw new Error(`Specto: "${h}" is not a host — use e.g. eu.specto.sh or https://replay.example.com`) }
|
|
32
|
+
if (u.protocol !== 'https:' && u.protocol !== 'http:') throw new Error(`Specto: host must be http(s) — got "${h}"`)
|
|
33
|
+
return (u.origin + u.pathname).replace(/\/+$/, '')
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// The destination for startRecording's options:
|
|
37
|
+
// { key, endpoint, configUrl, host, guessed }
|
|
38
|
+
// `fallback` is used when neither the options nor the key name a host: the
|
|
39
|
+
// npm package passes the hosted service (guessed: true — the SDK then
|
|
40
|
+
// records only once that host confirms the key), the script tag the origin
|
|
41
|
+
// that served it (guessed: false — it came from there). Without a fallback
|
|
42
|
+
// the endpoint is the page's own /api/ingest.
|
|
43
|
+
export function resolveDestination(opts = {}, { fallback = null, guessed = false } = {}) {
|
|
44
|
+
const { key, host: keyHost } = parseKey(opts.key)
|
|
45
|
+
const configUrl = (endpoint) => (opts.configUrl === false ? null : (opts.configUrl || String(endpoint).replace(/\/api\/ingest$/, '/api/sdk/config')))
|
|
46
|
+
if (opts.endpoint) {
|
|
47
|
+
const endpoint = String(opts.endpoint)
|
|
48
|
+
return { key, endpoint, configUrl: configUrl(endpoint), host: null, guessed: false }
|
|
49
|
+
}
|
|
50
|
+
const named = opts.host || keyHost
|
|
51
|
+
if (!named && !fallback) return { key, endpoint: '/api/ingest', configUrl: configUrl('/api/ingest'), host: null, guessed: false }
|
|
52
|
+
const host = normalizeHost(named || fallback)
|
|
53
|
+
const endpoint = `${host}/api/ingest`
|
|
54
|
+
return { key, endpoint, configUrl: configUrl(endpoint), host, guessed: !named && Boolean(guessed) }
|
|
55
|
+
}
|
package/src/index.js
CHANGED
|
@@ -2,31 +2,38 @@
|
|
|
2
2
|
// the script-tag snippet; you control when it starts.
|
|
3
3
|
//
|
|
4
4
|
// import { startRecording, identify, track } from '@spectosh/browser'
|
|
5
|
-
//
|
|
6
|
-
// key: 'pk_your_key',
|
|
7
|
-
//
|
|
5
|
+
// startRecording({
|
|
6
|
+
// key: 'pk_your_key', // pk_…@eu.specto.sh for the EU region,
|
|
7
|
+
// // pk_…@your-host for a self-hosted instance
|
|
8
8
|
// privacy: 'strict', // optional: mask all text
|
|
9
9
|
// sampleRate: 0.25, // optional: record a quarter of sessions
|
|
10
10
|
// consent: 'required', // optional: wait for grantConsent()
|
|
11
11
|
// })
|
|
12
12
|
// identify('user_42', { email: 'jane@acme.io', plan: 'pro' })
|
|
13
13
|
// track('checkout_started', { cart: 3 })
|
|
14
|
-
//
|
|
14
|
+
//
|
|
15
|
+
// Start it before your app renders (your entry file) — capture begins at
|
|
16
|
+
// once, so the first requests and errors of a page load are in the replay.
|
|
17
|
+
// React: @spectosh/browser/react.
|
|
15
18
|
import { startRecording as start, isOptedOut, hasConsent, scrubText, SDK_VERSION } from './recorder.js'
|
|
19
|
+
import { DEFAULT_HOST, parseKey } from './destination.js'
|
|
16
20
|
import RecorderWorker from './recorder.worker.js?worker&inline'
|
|
17
21
|
|
|
18
22
|
let lastOpts = null
|
|
19
23
|
|
|
20
24
|
export function startRecording(opts = {}) {
|
|
21
|
-
if (!opts.
|
|
22
|
-
if (!opts.key) throw new Error('@spectosh/browser: opts.key is required (your publishable pk_… key)')
|
|
25
|
+
if (!opts.key) throw new Error('@spectosh/browser: opts.key is required — your pk_… key from the project\'s Install page')
|
|
23
26
|
lastOpts = opts
|
|
24
|
-
|
|
27
|
+
// a key that names no host belongs to the hosted service — which must
|
|
28
|
+
// confirm it before anything is sent (an EU or self-hosted key without its
|
|
29
|
+
// host records nothing rather than reaching the wrong place)
|
|
30
|
+
return start({ createWorker: () => new RecorderWorker(), _fallback: DEFAULT_HOST, _guessed: true, ...opts })
|
|
25
31
|
}
|
|
26
32
|
|
|
27
33
|
const current = () => (typeof window !== 'undefined' ? window.__spectoRec : null)
|
|
28
34
|
export const identify = (id, traits) => current()?.identify(id, traits)
|
|
29
35
|
export const track = (name, props) => current()?.track(name, props)
|
|
36
|
+
export const captureError = (error, info) => current()?.captureError?.(error, info)
|
|
30
37
|
export const sessionUrl = () => current()?.sessionUrl?.() || null
|
|
31
38
|
export const stopRecording = () => current()?.stop()
|
|
32
39
|
|
|
@@ -47,5 +54,5 @@ export function optIn() {
|
|
|
47
54
|
try { localStorage.removeItem('specto-optout') } catch { /* unavailable */ }
|
|
48
55
|
}
|
|
49
56
|
|
|
50
|
-
export { isOptedOut, hasConsent, scrubText, SDK_VERSION }
|
|
51
|
-
export default { startRecording, identify, track, sessionUrl, stopRecording, grantConsent, revokeConsent, optOut, optIn }
|
|
57
|
+
export { isOptedOut, hasConsent, scrubText, parseKey, DEFAULT_HOST, SDK_VERSION }
|
|
58
|
+
export default { startRecording, identify, track, captureError, sessionUrl, stopRecording, grantConsent, revokeConsent, optOut, optIn }
|
package/src/react.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// @spectosh/browser/react — Specto for React apps.
|
|
2
|
+
//
|
|
3
|
+
// import { SpectoProvider } from '@spectosh/browser/react'
|
|
4
|
+
// createRoot(el, {
|
|
5
|
+
// onCaughtError: spectoErrorHandler(), // React 19: errors boundaries catch
|
|
6
|
+
// onUncaughtError: spectoErrorHandler(),
|
|
7
|
+
// }).render(<SpectoProvider apiKey="pk_…" user={user}><App /></SpectoProvider>)
|
|
8
|
+
//
|
|
9
|
+
// const { identify, track, sessionUrl } = useSpecto()
|
|
10
|
+
//
|
|
11
|
+
// The Provider starts recording during its first render — before any child
|
|
12
|
+
// component's effects run, so a page load's first requests and errors are
|
|
13
|
+
// captured. Once per page: StrictMode's double render, remounts and hot
|
|
14
|
+
// reloads reuse the running recorder, and unmounting never stops it (it
|
|
15
|
+
// records the page, not a component). On the server it renders its children
|
|
16
|
+
// and does nothing else. React is a peer dependency.
|
|
17
|
+
import { Component, createContext, createElement, useContext, useEffect, useState } from 'react'
|
|
18
|
+
import { startRecording, identify, track, captureError, sessionUrl, stopRecording, grantConsent, revokeConsent, optOut, optIn } from './index.js'
|
|
19
|
+
|
|
20
|
+
const api = { identify, track, captureError, sessionUrl, stop: stopRecording, grantConsent, revokeConsent, optOut, optIn }
|
|
21
|
+
const SpectoContext = createContext(api)
|
|
22
|
+
|
|
23
|
+
function begin(options) {
|
|
24
|
+
if (typeof window === 'undefined') return null
|
|
25
|
+
try { return startRecording(options) } catch (e) {
|
|
26
|
+
// a missing or malformed key must never take the app down
|
|
27
|
+
try { console.error(`[specto] ${e?.message || e}`) } catch { /* no console */ }
|
|
28
|
+
return null
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// <SpectoProvider apiKey="pk_…" user={{ id, email, plan }} privacy="strict">
|
|
33
|
+
// Every other prop is a startRecording option (host, sampleRate, consent…).
|
|
34
|
+
export function SpectoProvider({ apiKey, user, children, ...options }) {
|
|
35
|
+
useState(() => begin({ key: apiKey, ...options }))
|
|
36
|
+
// the signed-in user, attached whenever it changes
|
|
37
|
+
const id = user?.id ?? null
|
|
38
|
+
const traits = user ? JSON.stringify(Object.fromEntries(Object.entries(user).filter(([k]) => k !== 'id'))) : ''
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
if (id != null && id !== '') identify(id, traits ? JSON.parse(traits) : undefined)
|
|
41
|
+
}, [id, traits])
|
|
42
|
+
return createElement(SpectoContext.Provider, { value: api }, children)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// identify, track, captureError, sessionUrl, stop and the consent controls
|
|
46
|
+
export const useSpecto = () => useContext(SpectoContext)
|
|
47
|
+
|
|
48
|
+
// An error boundary that reports what it catches — with the component stack
|
|
49
|
+
// — as an error on the replay's timeline and in Issues.
|
|
50
|
+
// <SpectoErrorBoundary fallback={<p>Something went wrong</p>}>…
|
|
51
|
+
// fallback can also be ({ error, reset }) => …
|
|
52
|
+
export class SpectoErrorBoundary extends Component {
|
|
53
|
+
constructor(props) {
|
|
54
|
+
super(props)
|
|
55
|
+
this.state = { error: null }
|
|
56
|
+
this.reset = () => this.setState({ error: null })
|
|
57
|
+
}
|
|
58
|
+
static getDerivedStateFromError(error) { return { error } }
|
|
59
|
+
componentDidCatch(error, info) {
|
|
60
|
+
captureError(error, { componentStack: info?.componentStack, source: 'react-boundary' })
|
|
61
|
+
this.props.onError?.(error, info)
|
|
62
|
+
}
|
|
63
|
+
render() {
|
|
64
|
+
const { error } = this.state
|
|
65
|
+
if (!error) return this.props.children
|
|
66
|
+
const { fallback } = this.props
|
|
67
|
+
return typeof fallback === 'function' ? fallback({ error, reset: this.reset }) : (fallback ?? null)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export { captureError }
|
|
72
|
+
|
|
73
|
+
// React 19 root error hooks: createRoot(el, { onCaughtError, onUncaughtError,
|
|
74
|
+
// onRecoverableError }). Reports the error, then hands it to `next` — or
|
|
75
|
+
// logs it, as React does by default once a handler is set.
|
|
76
|
+
export function spectoErrorHandler(next) {
|
|
77
|
+
return (error, errorInfo) => {
|
|
78
|
+
captureError(error, { componentStack: errorInfo?.componentStack, source: 'react-root' })
|
|
79
|
+
if (next) next(error, errorInfo)
|
|
80
|
+
else { try { console.error(error) } catch { /* no console */ } }
|
|
81
|
+
}
|
|
82
|
+
}
|