@tracewayapp/core 1.0.4 → 1.0.6

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,6 +1,32 @@
1
- # @tracewayapp/core
1
+ <p align="center">
2
+ <picture>
3
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/tracewayapp/traceway/main/Traceway%20Logo%20White.png" />
4
+ <source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/tracewayapp/traceway/main/Traceway%20Logo.png" />
5
+ <img src="https://raw.githubusercontent.com/tracewayapp/traceway/main/Traceway%20Logo.png" alt="Traceway" width="200" />
6
+ </picture>
7
+ </p>
2
8
 
3
- Shared types and utilities for all Traceway JavaScript SDKs. Zero runtime dependencies.
9
+ <p align="center">
10
+ <a href="https://www.npmjs.com/package/@tracewayapp/core"><img src="https://img.shields.io/npm/v/@tracewayapp/core.svg" alt="npm"></a>
11
+ <a href="https://github.com/tracewayapp/traceway-js/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="License"></a>
12
+ </p>
13
+
14
+ # Traceway JS Core
15
+
16
+ Shared types and utilities consumed by every Traceway JavaScript SDK. Zero runtime dependencies, framework-agnostic, safe to import anywhere — browser, Node, React Native, Hermes, Cloudflare Workers.
17
+
18
+ [Traceway](https://tracewayapp.com) is a completely open-source error tracking platform. You can [self-host](https://docs.tracewayapp.com/server) it or use [Traceway Cloud](https://tracewayapp.com).
19
+
20
+ > **You probably don't need to install this directly.** It's a transitive dependency of `@tracewayapp/frontend`, `@tracewayapp/react-native`, and the framework wrappers. Install one of those instead. Reach for `@tracewayapp/core` directly only if you're building your own SDK or rendering Traceway's wire types in a dashboard.
21
+
22
+ ## Features
23
+
24
+ - Type definitions for the full Traceway wire format — exceptions, metrics, traces, spans, session recordings
25
+ - Discriminated union for the timeline event types (logs, network, navigation, custom breadcrumbs)
26
+ - A bounded rolling `EventBuffer<T>` keyed by time window + max size, used by every higher-level SDK to capture the last ~10 seconds of activity
27
+ - Connection-string parser, UUID v4 generator, ISO-8601 timestamp helper
28
+ - Constants for built-in metric names (`mem.used`, `cpu.used_pcnt`, etc.)
29
+ - Pure ESM + CJS dual output, full TypeScript declarations
4
30
 
5
31
  ## Installation
6
32
 
@@ -8,26 +34,70 @@ Shared types and utilities for all Traceway JavaScript SDKs. Zero runtime depend
8
34
  npm install @tracewayapp/core
9
35
  ```
10
36
 
11
- ## Types
37
+ ## Quick Start
38
+
39
+ ```ts
40
+ import {
41
+ parseConnectionString,
42
+ generateUUID,
43
+ nowISO,
44
+ EventBuffer,
45
+ type LogEvent,
46
+ type ReportRequest,
47
+ } from "@tracewayapp/core";
48
+
49
+ const { token, apiUrl } = parseConnectionString(
50
+ "your-token@https://traceway.example.com/api/report",
51
+ );
52
+
53
+ // Rolling buffer that drops entries older than 10s OR beyond 200 entries:
54
+ const logs = new EventBuffer<LogEvent>({ windowMs: 10_000, maxSize: 200 });
55
+ logs.add({
56
+ type: "log",
57
+ timestamp: nowISO(),
58
+ level: "info",
59
+ message: "user reached checkout",
60
+ });
61
+
62
+ const recent: LogEvent[] = logs.snapshot();
63
+ ```
64
+
65
+ ## Wire Types
66
+
67
+ ### Reporting
12
68
 
13
69
  | Type | Description |
14
70
  |------|-------------|
15
- | `ExceptionStackTrace` | Exception/message record matching the Traceway protocol |
71
+ | `ExceptionStackTrace` | Single exception/message record |
16
72
  | `MetricRecord` | Metric data point (`name`, `value`, `recordedAt`, optional `tags`) |
17
73
  | `Span` | Sub-operation span within a trace |
18
74
  | `Trace` | Endpoint or task trace |
19
- | `CollectionFrame` | Batch of stack traces, metrics, and traces |
75
+ | `CollectionFrame` | Batch of stack traces, metrics, traces, and session recordings |
20
76
  | `ReportRequest` | Top-level request payload sent to `/api/report` |
21
- | `TracewayOptions` | Configuration options for SDK initialization |
77
+
78
+ ### Timeline (logs, actions, recordings)
79
+
80
+ The browser, RN, and framework SDKs ship a rolling timeline of logs and action breadcrumbs alongside each captured exception. The wire types are defined here so dashboards and custom SDKs can share them.
81
+
82
+ | Type | Description |
83
+ |------|-------------|
84
+ | `TracewayEventBase` | Discriminator (`type`) + `timestamp` shared by all timeline events |
85
+ | `LogEvent` | Console log: `level` (`debug` / `info` / `warn` / `error`) + `message` |
86
+ | `NetworkEvent` | `fetch` / `XHR` request: `method`, `url`, `durationMs`, optional `statusCode`, byte counts, `error` |
87
+ | `NavigationEvent` | History API or manual navigation transition: `action` (`push` / `replace` / `pop`), `from`, `to` |
88
+ | `CustomEvent` | User-defined breadcrumb from `recordAction(category, name, data?)` |
89
+ | `TracewayEvent` | Discriminated union of the four event types above |
90
+ | `SessionRecordingPayload` | Replay frames + buffered `logs` + `actions` + `startedAt` / `endedAt` anchors, attached to an exception via `exceptionId` |
22
91
 
23
92
  ## Utilities
24
93
 
25
94
  | Function | Description |
26
95
  |----------|-------------|
96
+ | `parseConnectionString(str)` | Split `{token}@{apiUrl}` into `{ token, apiUrl }`; throws on malformed input |
27
97
  | `generateUUID()` | Generate a UUID v4 string |
28
- | `parseConnectionString(str)` | Split `{token}@{apiUrl}` into `{ token, apiUrl }` |
29
98
  | `nowISO()` | Current time as ISO 8601 string |
30
99
  | `msToNanoseconds(ms)` | Convert milliseconds to nanoseconds (integer) |
100
+ | `EventBuffer<T>` | Bounded rolling buffer (`windowMs`, `maxSize`) used by the higher-level SDKs to keep the most recent logs and actions |
31
101
 
32
102
  ## Constants
33
103
 
@@ -36,3 +106,24 @@ npm install @tracewayapp/core
36
106
  | `METRIC_MEM_USED` | `"mem.used"` |
37
107
  | `METRIC_MEM_TOTAL` | `"mem.total"` |
38
108
  | `METRIC_CPU_USED_PCNT` | `"cpu.used_pcnt"` |
109
+
110
+ ## Platform Support
111
+
112
+ | Environment | Status |
113
+ |---|---|
114
+ | Browser (any modern engine) | Yes |
115
+ | Node.js ≥ 18 | Yes |
116
+ | React Native (Hermes / JSC) | Yes |
117
+ | Cloudflare Workers / Deno / Bun | Yes |
118
+
119
+ ## Links
120
+
121
+ - [Traceway Website](https://tracewayapp.com)
122
+ - [Traceway GitHub](https://github.com/tracewayapp/traceway)
123
+ - [Documentation](https://docs.tracewayapp.com)
124
+ - [Browser SDK](https://www.npmjs.com/package/@tracewayapp/frontend)
125
+ - [React Native SDK](https://www.npmjs.com/package/@tracewayapp/react-native)
126
+
127
+ ## License
128
+
129
+ MIT
package/dist/index.d.mts CHANGED
@@ -41,7 +41,21 @@ interface CollectionFrame {
41
41
  }
42
42
  interface SessionRecordingPayload {
43
43
  exceptionId: string;
44
+ /** Replay frames (rrweb events on the web, MP4 chunks on native). */
44
45
  events: unknown[];
46
+ /**
47
+ * ISO 8601 timestamp of the first frame / first event in this recording.
48
+ * Combined with [endedAt] this lets the backend align logs and actions
49
+ * (which carry their own absolute timestamps) onto the video timeline:
50
+ * `offsetIntoVideoMs = event.timestamp - recording.startedAt`.
51
+ */
52
+ startedAt?: string;
53
+ /** ISO 8601 timestamp of the last frame / last event in this recording. */
54
+ endedAt?: string;
55
+ /** Last ~10s of console output. Capped at 200 entries by the SDK. */
56
+ logs?: LogEvent[];
57
+ /** Last ~10s of network/navigation/custom actions. Capped at 200 entries by the SDK. */
58
+ actions?: TracewayEvent[];
45
59
  }
46
60
  interface ReportRequest {
47
61
  collectionFrames: CollectionFrame[];
@@ -59,6 +73,39 @@ interface TracewayOptions {
59
73
  sampleRate?: number;
60
74
  errorSampleRate?: number;
61
75
  }
76
+ interface TracewayEventBase {
77
+ type: "log" | "network" | "navigation" | "custom";
78
+ timestamp: string;
79
+ }
80
+ interface LogEvent extends TracewayEventBase {
81
+ type: "log";
82
+ level: "debug" | "info" | "warn" | "error";
83
+ message: string;
84
+ }
85
+ interface NetworkEvent extends TracewayEventBase {
86
+ type: "network";
87
+ method: string;
88
+ url: string;
89
+ durationMs: number;
90
+ statusCode?: number;
91
+ requestBytes?: number;
92
+ responseBytes?: number;
93
+ error?: string;
94
+ }
95
+ interface NavigationEvent extends TracewayEventBase {
96
+ type: "navigation";
97
+ /** "push" | "replace" | "pop" — pop covers back/forward. */
98
+ action: string;
99
+ from?: string;
100
+ to?: string;
101
+ }
102
+ interface CustomEvent extends TracewayEventBase {
103
+ type: "custom";
104
+ category: string;
105
+ name: string;
106
+ data?: Record<string, unknown>;
107
+ }
108
+ type TracewayEvent = LogEvent | NetworkEvent | NavigationEvent | CustomEvent;
62
109
 
63
110
  declare function generateUUID(): string;
64
111
  interface ParsedConnectionString {
@@ -73,4 +120,30 @@ declare const METRIC_MEM_USED = "mem.used";
73
120
  declare const METRIC_MEM_TOTAL = "mem.total";
74
121
  declare const METRIC_CPU_USED_PCNT = "cpu.used_pcnt";
75
122
 
76
- export { type CollectionFrame, type ExceptionStackTrace, METRIC_CPU_USED_PCNT, METRIC_MEM_TOTAL, METRIC_MEM_USED, type MetricRecord, type ParsedConnectionString, type ReportRequest, type SessionRecordingPayload, type Span, type Trace, type TracewayOptions, generateUUID, msToNanoseconds, nowISO, parseConnectionString };
123
+ interface EventBufferOptions {
124
+ /** Window kept relative to `now`. Older entries are pruned. Default 10_000ms. */
125
+ windowMs?: number;
126
+ /** Hard cap on entries kept regardless of window. Default 200. */
127
+ maxSize?: number;
128
+ }
129
+ /**
130
+ * Time-windowed FIFO buffer with a hard size cap.
131
+ *
132
+ * Drops entries older than `windowMs` and keeps at most `maxSize` entries.
133
+ * Pruning runs on every `add` and `snapshot` so the buffer is self-maintaining
134
+ * without a background timer. Mirrors the Flutter SDK's EventBuffer.
135
+ */
136
+ declare class EventBuffer<T extends TracewayEventBase> {
137
+ readonly windowMs: number;
138
+ readonly maxSize: number;
139
+ private q;
140
+ constructor(options?: EventBufferOptions);
141
+ add(event: T): void;
142
+ /** Returns events ordered oldest -> newest. */
143
+ snapshot(): T[];
144
+ clear(): void;
145
+ get length(): number;
146
+ private prune;
147
+ }
148
+
149
+ export { type CollectionFrame, type CustomEvent, EventBuffer, type EventBufferOptions, type ExceptionStackTrace, type LogEvent, METRIC_CPU_USED_PCNT, METRIC_MEM_TOTAL, METRIC_MEM_USED, type MetricRecord, type NavigationEvent, type NetworkEvent, type ParsedConnectionString, type ReportRequest, type SessionRecordingPayload, type Span, type Trace, type TracewayEvent, type TracewayEventBase, type TracewayOptions, generateUUID, msToNanoseconds, nowISO, parseConnectionString };
package/dist/index.d.ts CHANGED
@@ -41,7 +41,21 @@ interface CollectionFrame {
41
41
  }
42
42
  interface SessionRecordingPayload {
43
43
  exceptionId: string;
44
+ /** Replay frames (rrweb events on the web, MP4 chunks on native). */
44
45
  events: unknown[];
46
+ /**
47
+ * ISO 8601 timestamp of the first frame / first event in this recording.
48
+ * Combined with [endedAt] this lets the backend align logs and actions
49
+ * (which carry their own absolute timestamps) onto the video timeline:
50
+ * `offsetIntoVideoMs = event.timestamp - recording.startedAt`.
51
+ */
52
+ startedAt?: string;
53
+ /** ISO 8601 timestamp of the last frame / last event in this recording. */
54
+ endedAt?: string;
55
+ /** Last ~10s of console output. Capped at 200 entries by the SDK. */
56
+ logs?: LogEvent[];
57
+ /** Last ~10s of network/navigation/custom actions. Capped at 200 entries by the SDK. */
58
+ actions?: TracewayEvent[];
45
59
  }
46
60
  interface ReportRequest {
47
61
  collectionFrames: CollectionFrame[];
@@ -59,6 +73,39 @@ interface TracewayOptions {
59
73
  sampleRate?: number;
60
74
  errorSampleRate?: number;
61
75
  }
76
+ interface TracewayEventBase {
77
+ type: "log" | "network" | "navigation" | "custom";
78
+ timestamp: string;
79
+ }
80
+ interface LogEvent extends TracewayEventBase {
81
+ type: "log";
82
+ level: "debug" | "info" | "warn" | "error";
83
+ message: string;
84
+ }
85
+ interface NetworkEvent extends TracewayEventBase {
86
+ type: "network";
87
+ method: string;
88
+ url: string;
89
+ durationMs: number;
90
+ statusCode?: number;
91
+ requestBytes?: number;
92
+ responseBytes?: number;
93
+ error?: string;
94
+ }
95
+ interface NavigationEvent extends TracewayEventBase {
96
+ type: "navigation";
97
+ /** "push" | "replace" | "pop" — pop covers back/forward. */
98
+ action: string;
99
+ from?: string;
100
+ to?: string;
101
+ }
102
+ interface CustomEvent extends TracewayEventBase {
103
+ type: "custom";
104
+ category: string;
105
+ name: string;
106
+ data?: Record<string, unknown>;
107
+ }
108
+ type TracewayEvent = LogEvent | NetworkEvent | NavigationEvent | CustomEvent;
62
109
 
63
110
  declare function generateUUID(): string;
64
111
  interface ParsedConnectionString {
@@ -73,4 +120,30 @@ declare const METRIC_MEM_USED = "mem.used";
73
120
  declare const METRIC_MEM_TOTAL = "mem.total";
74
121
  declare const METRIC_CPU_USED_PCNT = "cpu.used_pcnt";
75
122
 
76
- export { type CollectionFrame, type ExceptionStackTrace, METRIC_CPU_USED_PCNT, METRIC_MEM_TOTAL, METRIC_MEM_USED, type MetricRecord, type ParsedConnectionString, type ReportRequest, type SessionRecordingPayload, type Span, type Trace, type TracewayOptions, generateUUID, msToNanoseconds, nowISO, parseConnectionString };
123
+ interface EventBufferOptions {
124
+ /** Window kept relative to `now`. Older entries are pruned. Default 10_000ms. */
125
+ windowMs?: number;
126
+ /** Hard cap on entries kept regardless of window. Default 200. */
127
+ maxSize?: number;
128
+ }
129
+ /**
130
+ * Time-windowed FIFO buffer with a hard size cap.
131
+ *
132
+ * Drops entries older than `windowMs` and keeps at most `maxSize` entries.
133
+ * Pruning runs on every `add` and `snapshot` so the buffer is self-maintaining
134
+ * without a background timer. Mirrors the Flutter SDK's EventBuffer.
135
+ */
136
+ declare class EventBuffer<T extends TracewayEventBase> {
137
+ readonly windowMs: number;
138
+ readonly maxSize: number;
139
+ private q;
140
+ constructor(options?: EventBufferOptions);
141
+ add(event: T): void;
142
+ /** Returns events ordered oldest -> newest. */
143
+ snapshot(): T[];
144
+ clear(): void;
145
+ get length(): number;
146
+ private prune;
147
+ }
148
+
149
+ export { type CollectionFrame, type CustomEvent, EventBuffer, type EventBufferOptions, type ExceptionStackTrace, type LogEvent, METRIC_CPU_USED_PCNT, METRIC_MEM_TOTAL, METRIC_MEM_USED, type MetricRecord, type NavigationEvent, type NetworkEvent, type ParsedConnectionString, type ReportRequest, type SessionRecordingPayload, type Span, type Trace, type TracewayEvent, type TracewayEventBase, type TracewayOptions, generateUUID, msToNanoseconds, nowISO, parseConnectionString };
package/dist/index.js CHANGED
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
+ EventBuffer: () => EventBuffer,
23
24
  METRIC_CPU_USED_PCNT: () => METRIC_CPU_USED_PCNT,
24
25
  METRIC_MEM_TOTAL: () => METRIC_MEM_TOTAL,
25
26
  METRIC_MEM_USED: () => METRIC_MEM_USED,
@@ -68,8 +69,44 @@ function msToNanoseconds(ms) {
68
69
  var METRIC_MEM_USED = "mem.used";
69
70
  var METRIC_MEM_TOTAL = "mem.total";
70
71
  var METRIC_CPU_USED_PCNT = "cpu.used_pcnt";
72
+
73
+ // src/event-buffer.ts
74
+ var EventBuffer = class {
75
+ windowMs;
76
+ maxSize;
77
+ q = [];
78
+ constructor(options = {}) {
79
+ this.windowMs = options.windowMs ?? 1e4;
80
+ this.maxSize = options.maxSize ?? 200;
81
+ }
82
+ add(event) {
83
+ this.q.push(event);
84
+ this.prune();
85
+ }
86
+ /** Returns events ordered oldest -> newest. */
87
+ snapshot() {
88
+ this.prune();
89
+ return this.q.slice();
90
+ }
91
+ clear() {
92
+ this.q.length = 0;
93
+ }
94
+ get length() {
95
+ return this.q.length;
96
+ }
97
+ prune() {
98
+ const cutoff = Date.now() - this.windowMs;
99
+ while (this.q.length > 0 && Date.parse(this.q[0].timestamp) < cutoff) {
100
+ this.q.shift();
101
+ }
102
+ while (this.q.length > this.maxSize) {
103
+ this.q.shift();
104
+ }
105
+ }
106
+ };
71
107
  // Annotate the CommonJS export names for ESM import in node:
72
108
  0 && (module.exports = {
109
+ EventBuffer,
73
110
  METRIC_CPU_USED_PCNT,
74
111
  METRIC_MEM_TOTAL,
75
112
  METRIC_MEM_USED,
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/utils.ts","../src/constants.ts"],"sourcesContent":["export type {\n ExceptionStackTrace,\n MetricRecord,\n Span,\n Trace,\n CollectionFrame,\n SessionRecordingPayload,\n ReportRequest,\n TracewayOptions,\n} from \"./types.js\";\n\nexport {\n generateUUID,\n parseConnectionString,\n nowISO,\n msToNanoseconds,\n} from \"./utils.js\";\nexport type { ParsedConnectionString } from \"./utils.js\";\n\nexport {\n METRIC_MEM_USED,\n METRIC_MEM_TOTAL,\n METRIC_CPU_USED_PCNT,\n} from \"./constants.js\";\n","export function generateUUID(): string {\n if (\n typeof crypto !== \"undefined\" &&\n typeof crypto.randomUUID === \"function\"\n ) {\n return crypto.randomUUID();\n }\n return \"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx\".replace(/[xy]/g, (c) => {\n const r = (Math.random() * 16) | 0;\n const v = c === \"x\" ? r : (r & 0x3) | 0x8;\n return v.toString(16);\n });\n}\n\nexport interface ParsedConnectionString {\n token: string;\n apiUrl: string;\n}\n\nexport function parseConnectionString(\n connectionString: string,\n): ParsedConnectionString {\n const atIndex = connectionString.indexOf(\"@\");\n if (atIndex === -1) {\n throw new Error(\n \"Invalid connection string: must be in format {token}@{apiUrl}\",\n );\n }\n const token = connectionString.slice(0, atIndex);\n const apiUrl = connectionString.slice(atIndex + 1);\n if (!token || !apiUrl) {\n throw new Error(\n \"Invalid connection string: token and apiUrl must not be empty\",\n );\n }\n return { token, apiUrl };\n}\n\nexport function nowISO(): string {\n return new Date().toISOString();\n}\n\nexport function msToNanoseconds(ms: number): number {\n return Math.round(ms * 1_000_000);\n}\n","export const METRIC_MEM_USED = \"mem.used\";\nexport const METRIC_MEM_TOTAL = \"mem.total\";\nexport const METRIC_CPU_USED_PCNT = \"cpu.used_pcnt\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,SAAS,eAAuB;AACrC,MACE,OAAO,WAAW,eAClB,OAAO,OAAO,eAAe,YAC7B;AACA,WAAO,OAAO,WAAW;AAAA,EAC3B;AACA,SAAO,uCAAuC,QAAQ,SAAS,CAAC,MAAM;AACpE,UAAM,IAAK,KAAK,OAAO,IAAI,KAAM;AACjC,UAAM,IAAI,MAAM,MAAM,IAAK,IAAI,IAAO;AACtC,WAAO,EAAE,SAAS,EAAE;AAAA,EACtB,CAAC;AACH;AAOO,SAAS,sBACd,kBACwB;AACxB,QAAM,UAAU,iBAAiB,QAAQ,GAAG;AAC5C,MAAI,YAAY,IAAI;AAClB,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACA,QAAM,QAAQ,iBAAiB,MAAM,GAAG,OAAO;AAC/C,QAAM,SAAS,iBAAiB,MAAM,UAAU,CAAC;AACjD,MAAI,CAAC,SAAS,CAAC,QAAQ;AACrB,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACA,SAAO,EAAE,OAAO,OAAO;AACzB;AAEO,SAAS,SAAiB;AAC/B,UAAO,oBAAI,KAAK,GAAE,YAAY;AAChC;AAEO,SAAS,gBAAgB,IAAoB;AAClD,SAAO,KAAK,MAAM,KAAK,GAAS;AAClC;;;AC5CO,IAAM,kBAAkB;AACxB,IAAM,mBAAmB;AACzB,IAAM,uBAAuB;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts","../src/utils.ts","../src/constants.ts","../src/event-buffer.ts"],"sourcesContent":["export type {\n ExceptionStackTrace,\n MetricRecord,\n Span,\n Trace,\n CollectionFrame,\n SessionRecordingPayload,\n ReportRequest,\n TracewayOptions,\n TracewayEvent,\n TracewayEventBase,\n LogEvent,\n NetworkEvent,\n NavigationEvent,\n CustomEvent,\n} from \"./types.js\";\n\nexport {\n generateUUID,\n parseConnectionString,\n nowISO,\n msToNanoseconds,\n} from \"./utils.js\";\nexport type { ParsedConnectionString } from \"./utils.js\";\n\nexport {\n METRIC_MEM_USED,\n METRIC_MEM_TOTAL,\n METRIC_CPU_USED_PCNT,\n} from \"./constants.js\";\n\nexport { EventBuffer } from \"./event-buffer.js\";\nexport type { EventBufferOptions } from \"./event-buffer.js\";\n","export function generateUUID(): string {\n if (\n typeof crypto !== \"undefined\" &&\n typeof crypto.randomUUID === \"function\"\n ) {\n return crypto.randomUUID();\n }\n return \"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx\".replace(/[xy]/g, (c) => {\n const r = (Math.random() * 16) | 0;\n const v = c === \"x\" ? r : (r & 0x3) | 0x8;\n return v.toString(16);\n });\n}\n\nexport interface ParsedConnectionString {\n token: string;\n apiUrl: string;\n}\n\nexport function parseConnectionString(\n connectionString: string,\n): ParsedConnectionString {\n const atIndex = connectionString.indexOf(\"@\");\n if (atIndex === -1) {\n throw new Error(\n \"Invalid connection string: must be in format {token}@{apiUrl}\",\n );\n }\n const token = connectionString.slice(0, atIndex);\n const apiUrl = connectionString.slice(atIndex + 1);\n if (!token || !apiUrl) {\n throw new Error(\n \"Invalid connection string: token and apiUrl must not be empty\",\n );\n }\n return { token, apiUrl };\n}\n\nexport function nowISO(): string {\n return new Date().toISOString();\n}\n\nexport function msToNanoseconds(ms: number): number {\n return Math.round(ms * 1_000_000);\n}\n","export const METRIC_MEM_USED = \"mem.used\";\nexport const METRIC_MEM_TOTAL = \"mem.total\";\nexport const METRIC_CPU_USED_PCNT = \"cpu.used_pcnt\";\n","import type { TracewayEventBase } from \"./types.js\";\n\nexport interface EventBufferOptions {\n /** Window kept relative to `now`. Older entries are pruned. Default 10_000ms. */\n windowMs?: number;\n /** Hard cap on entries kept regardless of window. Default 200. */\n maxSize?: number;\n}\n\n/**\n * Time-windowed FIFO buffer with a hard size cap.\n *\n * Drops entries older than `windowMs` and keeps at most `maxSize` entries.\n * Pruning runs on every `add` and `snapshot` so the buffer is self-maintaining\n * without a background timer. Mirrors the Flutter SDK's EventBuffer.\n */\nexport class EventBuffer<T extends TracewayEventBase> {\n readonly windowMs: number;\n readonly maxSize: number;\n private q: T[] = [];\n\n constructor(options: EventBufferOptions = {}) {\n this.windowMs = options.windowMs ?? 10_000;\n this.maxSize = options.maxSize ?? 200;\n }\n\n add(event: T): void {\n this.q.push(event);\n this.prune();\n }\n\n /** Returns events ordered oldest -> newest. */\n snapshot(): T[] {\n this.prune();\n return this.q.slice();\n }\n\n clear(): void {\n this.q.length = 0;\n }\n\n get length(): number {\n return this.q.length;\n }\n\n private prune(): void {\n const cutoff = Date.now() - this.windowMs;\n while (this.q.length > 0 && Date.parse(this.q[0]!.timestamp) < cutoff) {\n this.q.shift();\n }\n while (this.q.length > this.maxSize) {\n this.q.shift();\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,SAAS,eAAuB;AACrC,MACE,OAAO,WAAW,eAClB,OAAO,OAAO,eAAe,YAC7B;AACA,WAAO,OAAO,WAAW;AAAA,EAC3B;AACA,SAAO,uCAAuC,QAAQ,SAAS,CAAC,MAAM;AACpE,UAAM,IAAK,KAAK,OAAO,IAAI,KAAM;AACjC,UAAM,IAAI,MAAM,MAAM,IAAK,IAAI,IAAO;AACtC,WAAO,EAAE,SAAS,EAAE;AAAA,EACtB,CAAC;AACH;AAOO,SAAS,sBACd,kBACwB;AACxB,QAAM,UAAU,iBAAiB,QAAQ,GAAG;AAC5C,MAAI,YAAY,IAAI;AAClB,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACA,QAAM,QAAQ,iBAAiB,MAAM,GAAG,OAAO;AAC/C,QAAM,SAAS,iBAAiB,MAAM,UAAU,CAAC;AACjD,MAAI,CAAC,SAAS,CAAC,QAAQ;AACrB,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACA,SAAO,EAAE,OAAO,OAAO;AACzB;AAEO,SAAS,SAAiB;AAC/B,UAAO,oBAAI,KAAK,GAAE,YAAY;AAChC;AAEO,SAAS,gBAAgB,IAAoB;AAClD,SAAO,KAAK,MAAM,KAAK,GAAS;AAClC;;;AC5CO,IAAM,kBAAkB;AACxB,IAAM,mBAAmB;AACzB,IAAM,uBAAuB;;;ACc7B,IAAM,cAAN,MAA+C;AAAA,EAC3C;AAAA,EACA;AAAA,EACD,IAAS,CAAC;AAAA,EAElB,YAAY,UAA8B,CAAC,GAAG;AAC5C,SAAK,WAAW,QAAQ,YAAY;AACpC,SAAK,UAAU,QAAQ,WAAW;AAAA,EACpC;AAAA,EAEA,IAAI,OAAgB;AAClB,SAAK,EAAE,KAAK,KAAK;AACjB,SAAK,MAAM;AAAA,EACb;AAAA;AAAA,EAGA,WAAgB;AACd,SAAK,MAAM;AACX,WAAO,KAAK,EAAE,MAAM;AAAA,EACtB;AAAA,EAEA,QAAc;AACZ,SAAK,EAAE,SAAS;AAAA,EAClB;AAAA,EAEA,IAAI,SAAiB;AACnB,WAAO,KAAK,EAAE;AAAA,EAChB;AAAA,EAEQ,QAAc;AACpB,UAAM,SAAS,KAAK,IAAI,IAAI,KAAK;AACjC,WAAO,KAAK,EAAE,SAAS,KAAK,KAAK,MAAM,KAAK,EAAE,CAAC,EAAG,SAAS,IAAI,QAAQ;AACrE,WAAK,EAAE,MAAM;AAAA,IACf;AACA,WAAO,KAAK,EAAE,SAAS,KAAK,SAAS;AACnC,WAAK,EAAE,MAAM;AAAA,IACf;AAAA,EACF;AACF;","names":[]}
package/dist/index.mjs CHANGED
@@ -36,7 +36,43 @@ function msToNanoseconds(ms) {
36
36
  var METRIC_MEM_USED = "mem.used";
37
37
  var METRIC_MEM_TOTAL = "mem.total";
38
38
  var METRIC_CPU_USED_PCNT = "cpu.used_pcnt";
39
+
40
+ // src/event-buffer.ts
41
+ var EventBuffer = class {
42
+ windowMs;
43
+ maxSize;
44
+ q = [];
45
+ constructor(options = {}) {
46
+ this.windowMs = options.windowMs ?? 1e4;
47
+ this.maxSize = options.maxSize ?? 200;
48
+ }
49
+ add(event) {
50
+ this.q.push(event);
51
+ this.prune();
52
+ }
53
+ /** Returns events ordered oldest -> newest. */
54
+ snapshot() {
55
+ this.prune();
56
+ return this.q.slice();
57
+ }
58
+ clear() {
59
+ this.q.length = 0;
60
+ }
61
+ get length() {
62
+ return this.q.length;
63
+ }
64
+ prune() {
65
+ const cutoff = Date.now() - this.windowMs;
66
+ while (this.q.length > 0 && Date.parse(this.q[0].timestamp) < cutoff) {
67
+ this.q.shift();
68
+ }
69
+ while (this.q.length > this.maxSize) {
70
+ this.q.shift();
71
+ }
72
+ }
73
+ };
39
74
  export {
75
+ EventBuffer,
40
76
  METRIC_CPU_USED_PCNT,
41
77
  METRIC_MEM_TOTAL,
42
78
  METRIC_MEM_USED,
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/utils.ts","../src/constants.ts"],"sourcesContent":["export function generateUUID(): string {\n if (\n typeof crypto !== \"undefined\" &&\n typeof crypto.randomUUID === \"function\"\n ) {\n return crypto.randomUUID();\n }\n return \"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx\".replace(/[xy]/g, (c) => {\n const r = (Math.random() * 16) | 0;\n const v = c === \"x\" ? r : (r & 0x3) | 0x8;\n return v.toString(16);\n });\n}\n\nexport interface ParsedConnectionString {\n token: string;\n apiUrl: string;\n}\n\nexport function parseConnectionString(\n connectionString: string,\n): ParsedConnectionString {\n const atIndex = connectionString.indexOf(\"@\");\n if (atIndex === -1) {\n throw new Error(\n \"Invalid connection string: must be in format {token}@{apiUrl}\",\n );\n }\n const token = connectionString.slice(0, atIndex);\n const apiUrl = connectionString.slice(atIndex + 1);\n if (!token || !apiUrl) {\n throw new Error(\n \"Invalid connection string: token and apiUrl must not be empty\",\n );\n }\n return { token, apiUrl };\n}\n\nexport function nowISO(): string {\n return new Date().toISOString();\n}\n\nexport function msToNanoseconds(ms: number): number {\n return Math.round(ms * 1_000_000);\n}\n","export const METRIC_MEM_USED = \"mem.used\";\nexport const METRIC_MEM_TOTAL = \"mem.total\";\nexport const METRIC_CPU_USED_PCNT = \"cpu.used_pcnt\";\n"],"mappings":";AAAO,SAAS,eAAuB;AACrC,MACE,OAAO,WAAW,eAClB,OAAO,OAAO,eAAe,YAC7B;AACA,WAAO,OAAO,WAAW;AAAA,EAC3B;AACA,SAAO,uCAAuC,QAAQ,SAAS,CAAC,MAAM;AACpE,UAAM,IAAK,KAAK,OAAO,IAAI,KAAM;AACjC,UAAM,IAAI,MAAM,MAAM,IAAK,IAAI,IAAO;AACtC,WAAO,EAAE,SAAS,EAAE;AAAA,EACtB,CAAC;AACH;AAOO,SAAS,sBACd,kBACwB;AACxB,QAAM,UAAU,iBAAiB,QAAQ,GAAG;AAC5C,MAAI,YAAY,IAAI;AAClB,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACA,QAAM,QAAQ,iBAAiB,MAAM,GAAG,OAAO;AAC/C,QAAM,SAAS,iBAAiB,MAAM,UAAU,CAAC;AACjD,MAAI,CAAC,SAAS,CAAC,QAAQ;AACrB,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACA,SAAO,EAAE,OAAO,OAAO;AACzB;AAEO,SAAS,SAAiB;AAC/B,UAAO,oBAAI,KAAK,GAAE,YAAY;AAChC;AAEO,SAAS,gBAAgB,IAAoB;AAClD,SAAO,KAAK,MAAM,KAAK,GAAS;AAClC;;;AC5CO,IAAM,kBAAkB;AACxB,IAAM,mBAAmB;AACzB,IAAM,uBAAuB;","names":[]}
1
+ {"version":3,"sources":["../src/utils.ts","../src/constants.ts","../src/event-buffer.ts"],"sourcesContent":["export function generateUUID(): string {\n if (\n typeof crypto !== \"undefined\" &&\n typeof crypto.randomUUID === \"function\"\n ) {\n return crypto.randomUUID();\n }\n return \"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx\".replace(/[xy]/g, (c) => {\n const r = (Math.random() * 16) | 0;\n const v = c === \"x\" ? r : (r & 0x3) | 0x8;\n return v.toString(16);\n });\n}\n\nexport interface ParsedConnectionString {\n token: string;\n apiUrl: string;\n}\n\nexport function parseConnectionString(\n connectionString: string,\n): ParsedConnectionString {\n const atIndex = connectionString.indexOf(\"@\");\n if (atIndex === -1) {\n throw new Error(\n \"Invalid connection string: must be in format {token}@{apiUrl}\",\n );\n }\n const token = connectionString.slice(0, atIndex);\n const apiUrl = connectionString.slice(atIndex + 1);\n if (!token || !apiUrl) {\n throw new Error(\n \"Invalid connection string: token and apiUrl must not be empty\",\n );\n }\n return { token, apiUrl };\n}\n\nexport function nowISO(): string {\n return new Date().toISOString();\n}\n\nexport function msToNanoseconds(ms: number): number {\n return Math.round(ms * 1_000_000);\n}\n","export const METRIC_MEM_USED = \"mem.used\";\nexport const METRIC_MEM_TOTAL = \"mem.total\";\nexport const METRIC_CPU_USED_PCNT = \"cpu.used_pcnt\";\n","import type { TracewayEventBase } from \"./types.js\";\n\nexport interface EventBufferOptions {\n /** Window kept relative to `now`. Older entries are pruned. Default 10_000ms. */\n windowMs?: number;\n /** Hard cap on entries kept regardless of window. Default 200. */\n maxSize?: number;\n}\n\n/**\n * Time-windowed FIFO buffer with a hard size cap.\n *\n * Drops entries older than `windowMs` and keeps at most `maxSize` entries.\n * Pruning runs on every `add` and `snapshot` so the buffer is self-maintaining\n * without a background timer. Mirrors the Flutter SDK's EventBuffer.\n */\nexport class EventBuffer<T extends TracewayEventBase> {\n readonly windowMs: number;\n readonly maxSize: number;\n private q: T[] = [];\n\n constructor(options: EventBufferOptions = {}) {\n this.windowMs = options.windowMs ?? 10_000;\n this.maxSize = options.maxSize ?? 200;\n }\n\n add(event: T): void {\n this.q.push(event);\n this.prune();\n }\n\n /** Returns events ordered oldest -> newest. */\n snapshot(): T[] {\n this.prune();\n return this.q.slice();\n }\n\n clear(): void {\n this.q.length = 0;\n }\n\n get length(): number {\n return this.q.length;\n }\n\n private prune(): void {\n const cutoff = Date.now() - this.windowMs;\n while (this.q.length > 0 && Date.parse(this.q[0]!.timestamp) < cutoff) {\n this.q.shift();\n }\n while (this.q.length > this.maxSize) {\n this.q.shift();\n }\n }\n}\n"],"mappings":";AAAO,SAAS,eAAuB;AACrC,MACE,OAAO,WAAW,eAClB,OAAO,OAAO,eAAe,YAC7B;AACA,WAAO,OAAO,WAAW;AAAA,EAC3B;AACA,SAAO,uCAAuC,QAAQ,SAAS,CAAC,MAAM;AACpE,UAAM,IAAK,KAAK,OAAO,IAAI,KAAM;AACjC,UAAM,IAAI,MAAM,MAAM,IAAK,IAAI,IAAO;AACtC,WAAO,EAAE,SAAS,EAAE;AAAA,EACtB,CAAC;AACH;AAOO,SAAS,sBACd,kBACwB;AACxB,QAAM,UAAU,iBAAiB,QAAQ,GAAG;AAC5C,MAAI,YAAY,IAAI;AAClB,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACA,QAAM,QAAQ,iBAAiB,MAAM,GAAG,OAAO;AAC/C,QAAM,SAAS,iBAAiB,MAAM,UAAU,CAAC;AACjD,MAAI,CAAC,SAAS,CAAC,QAAQ;AACrB,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACA,SAAO,EAAE,OAAO,OAAO;AACzB;AAEO,SAAS,SAAiB;AAC/B,UAAO,oBAAI,KAAK,GAAE,YAAY;AAChC;AAEO,SAAS,gBAAgB,IAAoB;AAClD,SAAO,KAAK,MAAM,KAAK,GAAS;AAClC;;;AC5CO,IAAM,kBAAkB;AACxB,IAAM,mBAAmB;AACzB,IAAM,uBAAuB;;;ACc7B,IAAM,cAAN,MAA+C;AAAA,EAC3C;AAAA,EACA;AAAA,EACD,IAAS,CAAC;AAAA,EAElB,YAAY,UAA8B,CAAC,GAAG;AAC5C,SAAK,WAAW,QAAQ,YAAY;AACpC,SAAK,UAAU,QAAQ,WAAW;AAAA,EACpC;AAAA,EAEA,IAAI,OAAgB;AAClB,SAAK,EAAE,KAAK,KAAK;AACjB,SAAK,MAAM;AAAA,EACb;AAAA;AAAA,EAGA,WAAgB;AACd,SAAK,MAAM;AACX,WAAO,KAAK,EAAE,MAAM;AAAA,EACtB;AAAA,EAEA,QAAc;AACZ,SAAK,EAAE,SAAS;AAAA,EAClB;AAAA,EAEA,IAAI,SAAiB;AACnB,WAAO,KAAK,EAAE;AAAA,EAChB;AAAA,EAEQ,QAAc;AACpB,UAAM,SAAS,KAAK,IAAI,IAAI,KAAK;AACjC,WAAO,KAAK,EAAE,SAAS,KAAK,KAAK,MAAM,KAAK,EAAE,CAAC,EAAG,SAAS,IAAI,QAAQ;AACrE,WAAK,EAAE,MAAM;AAAA,IACf;AACA,WAAO,KAAK,EAAE,SAAS,KAAK,SAAS;AACnC,WAAK,EAAE,MAAM;AAAA,IACf;AAAA,EACF;AACF;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tracewayapp/core",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "Shared types and utilities for Traceway SDKs",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",