@tracewayapp/core 1.0.3 → 1.0.5

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
@@ -10,16 +10,32 @@ npm install @tracewayapp/core
10
10
 
11
11
  ## Types
12
12
 
13
+ ### Reporting
14
+
13
15
  | Type | Description |
14
16
  |------|-------------|
15
17
  | `ExceptionStackTrace` | Exception/message record matching the Traceway protocol |
16
18
  | `MetricRecord` | Metric data point (`name`, `value`, `recordedAt`, optional `tags`) |
17
19
  | `Span` | Sub-operation span within a trace |
18
20
  | `Trace` | Endpoint or task trace |
19
- | `CollectionFrame` | Batch of stack traces, metrics, and traces |
21
+ | `CollectionFrame` | Batch of stack traces, metrics, traces, and session recordings |
20
22
  | `ReportRequest` | Top-level request payload sent to `/api/report` |
21
23
  | `TracewayOptions` | Configuration options for SDK initialization |
22
24
 
25
+ ### Logs, Actions, and Session Recordings
26
+
27
+ The frontend SDK ships a rolling timeline of logs and action breadcrumbs alongside each captured exception, plus an rrweb-based session replay. The wire types are defined here so other packages and dashboards can share them.
28
+
29
+ | Type | Description |
30
+ |------|-------------|
31
+ | `TracewayEventBase` | Discriminator (`type`) + `timestamp` shared by all timeline events |
32
+ | `LogEvent` | Console log entry: `level` (`debug` / `info` / `warn` / `error`) + `message` |
33
+ | `NetworkEvent` | `fetch` / `XHR` request: `method`, `url`, `durationMs`, optional `statusCode`, byte counts, `error` |
34
+ | `NavigationEvent` | History API transition: `action` (`push` / `replace` / `pop`), `from`, `to` |
35
+ | `CustomEvent` | User-defined breadcrumb from `recordAction(category, name, data?)` |
36
+ | `TracewayEvent` | Discriminated union of the four event types above |
37
+ | `SessionRecordingPayload` | Replay frames + buffered `logs` + `actions` + `startedAt` / `endedAt` anchors, attached to an exception via `exceptionId` |
38
+
23
39
  ## Utilities
24
40
 
25
41
  | Function | Description |
@@ -28,6 +44,7 @@ npm install @tracewayapp/core
28
44
  | `parseConnectionString(str)` | Split `{token}@{apiUrl}` into `{ token, apiUrl }` |
29
45
  | `nowISO()` | Current time as ISO 8601 string |
30
46
  | `msToNanoseconds(ms)` | Convert milliseconds to nanoseconds (integer) |
47
+ | `EventBuffer<T>` | Bounded rolling buffer (`windowMs`, `maxSize`) used by the frontend SDK to keep the most recent logs and actions |
31
48
 
32
49
  ## Constants
33
50
 
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.3",
3
+ "version": "1.0.5",
4
4
  "description": "Shared types and utilities for Traceway SDKs",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",