cosmic-eye 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 CosmicEye
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,161 @@
1
+ # CosmicEye
2
+
3
+ A set of RUM metrics for SPAs. Two independent modules:
4
+
5
+ - **RDR** (RUM Duplicate Requests) — detects and logs duplicate API requests.
6
+ - **RT** (Route Transition Metrics) — measures route transition timing (render + TTI).
7
+
8
+ **Key properties:**
9
+
10
+ - Zero runtime dependencies for the core (React is an optional peer for extensions)
11
+ - RDR: deterministic sampling ~5% in production, always enabled in dev
12
+ - RT: dev-only, measures `route_render_ms` and `route_tti_ms`
13
+ - `RouteTracker` — "dumb" React provider for both modules (post-render)
14
+ - `observeHistory` — neutral pre-render navigation observer (history v4/v5)
15
+
16
+ ## Installation
17
+
18
+ ```bash
19
+ npm install cosmic-eye
20
+ ```
21
+
22
+ ## Quick start
23
+
24
+ ### RDR
25
+
26
+ ```ts
27
+ import rdr, { initRDR } from 'cosmic-eye';
28
+
29
+ initRDR();
30
+ rdr.reqHandler({ s: 'UserService', m: 'getProfile', p: { id: 42 }, b: {} });
31
+ ```
32
+
33
+ ### RT + observeHistory + RouteTracker
34
+
35
+ ```ts
36
+ import { initRT, rt, observeHistory } from 'cosmic-eye';
37
+ import { createBrowserHistory } from 'history';
38
+
39
+ const history = createBrowserHistory();
40
+ initRT();
41
+
42
+ // Pre-render: observer emits navigation events before React render
43
+ const observer = observeHistory(history);
44
+ observer.subscribe(({ pathname, search }) => {
45
+ rt.startTransition(pathname, search);
46
+ rdr.resetTiming();
47
+ rdr.resetActions();
48
+ });
49
+ ```
50
+
51
+ ```tsx
52
+ import { RouteTracker } from 'cosmic-eye/react';
53
+
54
+ // Post-render: RouteTracker fires after React commit
55
+ <Router history={history}>
56
+ <RouteTracker
57
+ onRouteChange={[
58
+ (pathname) => { rt.markRendered(pathname); },
59
+ ]}
60
+ >
61
+ <Switch>...</Switch>
62
+ </RouteTracker>
63
+ </Router>
64
+ ```
65
+
66
+ ## How duplicate detection works
67
+
68
+ 1. Each incoming payload is hashed into a `reqHash` (combining endpoint + params hash + body hash)
69
+ 2. If the same `reqHash` was seen within `TIMINGS.DUPLICATE_THRESHOLD_MS` (default 1 000 ms), a log entry is created
70
+ 3. Log entries accumulate in a buffer and are flushed:
71
+ - Every `FLUSH.INTERVAL_MS` (15 s)
72
+ - When buffer reaches `FLUSH.MAX_EVENTS` (50)
73
+ - On `visibilitychange` (tab hidden)
74
+ - On `pagehide` (page close)
75
+ - On `destroy()`
76
+
77
+ ## Sampling
78
+
79
+ In **production** (`NODE_ENV=production`), only ~5% of users are sampled. The decision is deterministic and stable across page reloads — a client ID is persisted in `localStorage` under key `rum_user_id`, hashed, and checked against the sampling rate.
80
+
81
+ In **development** (`NODE_ENV !== 'production'`), sampling is always enabled.
82
+
83
+ ## API
84
+
85
+ ### RDR
86
+
87
+ | Function | Description |
88
+ |----------|-------------|
89
+ | `initRDR()` | Initialize. Safe to call multiple times. |
90
+ | `rdr.reqHandler(payload)` | Pass an API request. `{ s, m, p?, b? }` |
91
+ | `rdr.resetTiming()` | Reset timer (SPA route change) |
92
+ | `rdr.resetActions()` | Clear action buffer |
93
+ | `rdr.destroy()` | Stop timers, flush, clean up |
94
+
95
+ ### RT
96
+
97
+ | Function | Description |
98
+ |----------|-------------|
99
+ | `initRT()` | Enable module (dev-only) |
100
+ | `rt.startTransition(pathname, search)` | Start transition timing (pre-render) |
101
+ | `rt.markRendered(pathname)` | Mark route render (post-render) |
102
+ | `trackCritical(promise?)` | Track a critical async operation |
103
+ | `rt.destroy()` | Clean up |
104
+
105
+ ### observeHistory
106
+
107
+ | Function | Description |
108
+ |----------|-------------|
109
+ | `observeHistory(history)` | Create observer (idempotent via WeakMap) |
110
+ | `observer.subscribe(listener)` | Subscribe to `INIT/PUSH/REPLACE/POP`. Returns `unsubscribe` |
111
+ | `observer.unpatch()` | Remove patch, clean up |
112
+
113
+ ### RouteTracker (`cosmic-eye/react`)
114
+
115
+ | Prop | Type | Description |
116
+ |------|------|-------------|
117
+ | `children` | `ReactNode` | Child elements |
118
+ | `onRouteChange` | `Array<(pathname, search) => void>` | Callbacks on route change |
119
+
120
+ See [src/rdr/README.md](src/rdr/README.md) and [src/rt/README.md](src/rt/README.md) for log schemas and configuration reference.
121
+
122
+ ## Project structure
123
+
124
+ ```
125
+ src/
126
+ index.ts — public API (re-exports only, no React)
127
+ react.ts — React extensions entry point (cosmic-eye/react)
128
+ rdr/ — RDR module: duplicate request detection
129
+ rt/ — RT module: route transition metrics
130
+ extensions/
131
+ history-route-observer/ — navigation observer (pre-render)
132
+ route-tracker/ — RouteTracker React component (post-render)
133
+ tests/
134
+ rdr/ — RDR tests (49)
135
+ rt/ — RT tests (19)
136
+ extensions/ — extension tests (32)
137
+ ```
138
+
139
+ ## Test commands
140
+
141
+ | Command | Scope |
142
+ |---------|-------|
143
+ | `npm run test` | all tests |
144
+ | `npm run test:rdr` | RDR only |
145
+ | `npm run test:rt` | RT only |
146
+ | `npm run test:extensions` | extensions only |
147
+ | `npm run test:watch` | all, watch mode |
148
+
149
+ ## Documentation
150
+
151
+ Each module has its own README with integration guide, configuration reference, and event schema:
152
+
153
+ - [src/rdr/README.md](src/rdr/README.md) — RDR documentation
154
+ - [src/rt/README.md](src/rt/README.md) — RT documentation
155
+ - [src/extensions/history-route-observer/README.md](src/extensions/history-route-observer/README.md) — observer documentation
156
+ - [src/extensions/route-tracker/README.md](src/extensions/route-tracker/README.md) — RouteTracker documentation
157
+ - [CHANGELOG.md](CHANGELOG.md) — change history
158
+
159
+ ## License
160
+
161
+ MIT