@sypnia/telemetry 0.0.0-stage → 0.2.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.
@@ -0,0 +1,221 @@
1
+ // ../shared/src/route.ts
2
+ var UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
3
+ var HEX_RE = /^[0-9a-f]{16,}$/i;
4
+ var TOKEN_RE = /^(?=.*[0-9])[A-Za-z0-9_-]{20,}$/;
5
+ var SCHEME_RE = /^[a-z][a-z0-9+.-]*:\/\//i;
6
+ function isIdSegment(segment) {
7
+ if (!segment) return false;
8
+ return /^[0-9]+$/.test(segment) || UUID_RE.test(segment) || HEX_RE.test(segment) || TOKEN_RE.test(segment) || segment.includes("%");
9
+ }
10
+ function pathnameOf(input) {
11
+ let path = input;
12
+ if (SCHEME_RE.test(path)) {
13
+ try {
14
+ path = new URL(path).pathname;
15
+ } catch {
16
+ }
17
+ }
18
+ const cut = path.search(/[?#]/);
19
+ if (cut >= 0) path = path.slice(0, cut);
20
+ if (path.length > 1) path = path.replace(/\/+$/, "");
21
+ return path || "/";
22
+ }
23
+ function routeOf(input) {
24
+ const path = pathnameOf(input);
25
+ if (path === "/") return path;
26
+ return path.split("/").map((segment) => isIdSegment(segment) ? ":id" : segment).join("/");
27
+ }
28
+ function matchesRoute(path, patterns) {
29
+ if (!patterns || patterns.length === 0) return false;
30
+ return patterns.some((pattern) => {
31
+ if (typeof pattern === "string") {
32
+ const prefix = pattern.length > 1 ? pattern.replace(/\/+$/, "") : pattern;
33
+ return path === prefix || path.startsWith(prefix === "/" ? "/" : `${prefix}/`);
34
+ }
35
+ return testPattern(pattern, path);
36
+ });
37
+ }
38
+ function matchesUrl(url, patterns) {
39
+ if (!patterns || patterns.length === 0) return false;
40
+ return patterns.some((pattern) => typeof pattern === "string" ? url.startsWith(pattern) : testPattern(pattern, url));
41
+ }
42
+ function matchesText(text, patterns) {
43
+ if (!patterns || patterns.length === 0) return false;
44
+ return patterns.some((pattern) => typeof pattern === "string" ? text.includes(pattern) : testPattern(pattern, text));
45
+ }
46
+ function testPattern(pattern, text) {
47
+ pattern.lastIndex = 0;
48
+ return pattern.test(text);
49
+ }
50
+
51
+ // ../shared/src/redact.ts
52
+ var EMAIL_RE = /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g;
53
+ var URL_RE = /[a-z][a-z0-9+.-]*:\/\/[^\s)'"]+/giu;
54
+ var UUID_RE2 = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi;
55
+ var QUOTED_RE = /"[^"\n]{0,200}"|'[^'\n]{0,200}'/gu;
56
+ var WORD_RE = /[A-Za-z0-9_-]{20,}/g;
57
+ var DIGITS_RE = /[0-9]+/g;
58
+ var MESSAGE_CHARS = 500;
59
+ function cutChars(text, max) {
60
+ if (text.length <= max) return text;
61
+ let out = "";
62
+ let count = 0;
63
+ for (const char of text) {
64
+ if (count === max) break;
65
+ out += char;
66
+ count++;
67
+ }
68
+ return out;
69
+ }
70
+ function scrubEmails(text) {
71
+ return text.replace(EMAIL_RE, "[email]");
72
+ }
73
+ function redactMessage(message) {
74
+ return cutChars(message, MESSAGE_CHARS).replace(EMAIL_RE, "[email]").replace(URL_RE, "<url>").replace(UUID_RE2, "<uuid>").replace(QUOTED_RE, '"\u2026"').replace(WORD_RE, (word) => /[0-9]/.test(word) ? "<token>" : word).replace(DIGITS_RE, "#");
75
+ }
76
+
77
+ // ../shared/src/wire.ts
78
+ var TRACEPARENT_HEADER = "traceparent";
79
+ var SESSION_HEADER = "x-sypnia-session";
80
+ var DEFAULT_ENDPOINT = "https://telemetry.ingest.sypnia.com";
81
+ function baseUrl(endpoint) {
82
+ return (endpoint || DEFAULT_ENDPOINT).replace(/\/+$/, "");
83
+ }
84
+
85
+ // ../shared/src/fingerprint.ts
86
+ var PRIME = 16777619;
87
+ var SEED_A = 2166136261;
88
+ var SEED_B = 84696351;
89
+ var MESSAGE_UNITS = 300;
90
+ var STACK_LINES = 30;
91
+ var URL_RE2 = /https?:\/\/[^\s)'"]+/gi;
92
+ var CHUNK_PATH_RE = /\/_next\/static\/chunks\/[^\s)'"]+/gi;
93
+ var UUID_RE3 = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi;
94
+ var QUOTED_RE2 = /(["'`])(?:\\.|(?!\1).){0,200}\1/g;
95
+ var HEX_RE2 = /\b[0-9a-f]{12,}\b/gi;
96
+ var NUMBER_RE = /[0-9]+(?:[.,][0-9]+)?/g;
97
+ var WHITESPACE = "\\t\\n\\v\\f\\r \\u0085\\u00a0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000";
98
+ var WHITESPACE_RUN_RE = new RegExp(`[${WHITESPACE}]+`, "g");
99
+ var EDGE_WHITESPACE_RE = new RegExp(`^[${WHITESPACE}]+|[${WHITESPACE}]+$`, "g");
100
+ var FRAME_V8_RE = /^\s*at\s+(?:(.*?)\s+\()?(.+?):([0-9]+):([0-9]+)\)?\s*$/;
101
+ var FRAME_GECKO_RE = /^\s*(.*?)@(.+?):([0-9]+):([0-9]+)\s*$/;
102
+ var SCHEME_RE2 = /^[a-z][a-z0-9+.-]*:\/\//i;
103
+ var BUILD_HASH_RE = /[-.][0-9a-f]{8,}(\.(?:m?js|tsx?))$/i;
104
+ var TOP_CHUNK_RE = /^\/_next\/static\/chunks\/([0-9a-z_-]+)\.js$/i;
105
+ var BEFORE_BUILD_FOLDER_RE = /^.*?(\.next\/)/;
106
+ var NOISY_FRAMES = [
107
+ "node_modules",
108
+ "/_next/static/chunks/framework",
109
+ "/_next/static/chunks/main",
110
+ "/_next/static/chunks/webpack",
111
+ "/_next/static/chunks/polyfills",
112
+ "/_next/static/chunks/turbopack-",
113
+ "react-dom",
114
+ "scheduler",
115
+ "node:internal",
116
+ "<anonymous>"
117
+ ];
118
+ function fnv1a(text, seed) {
119
+ let hash = seed >>> 0;
120
+ for (let i = 0; i < text.length; i++) {
121
+ hash ^= text.charCodeAt(i);
122
+ hash = Math.imul(hash, PRIME) >>> 0;
123
+ }
124
+ return hash.toString(16).padStart(8, "0");
125
+ }
126
+ function hash64(text) {
127
+ return fnv1a(text, SEED_A) + fnv1a(text, SEED_B);
128
+ }
129
+ function firstUnits(text, max) {
130
+ if (text.length <= max) return text;
131
+ const last = text.charCodeAt(max - 1);
132
+ const splitsPair = last >= 55296 && last <= 56319;
133
+ return text.slice(0, splitsPair ? max - 1 : max);
134
+ }
135
+ function normalizeMessage(message) {
136
+ if (!message) return "";
137
+ const text = message.replace(URL_RE2, "<url>").replace(CHUNK_PATH_RE, "<chunk>").replace(UUID_RE3, "<uuid>").replace(QUOTED_RE2, "$1\u2026$1").replace(HEX_RE2, "<hex>").replace(NUMBER_RE, "#").replace(WHITESPACE_RUN_RE, " ").replace(EDGE_WHITESPACE_RE, "");
138
+ return firstUnits(text, MESSAGE_UNITS);
139
+ }
140
+ function normalizeFrameFile(file) {
141
+ let out = file;
142
+ if (SCHEME_RE2.test(out)) {
143
+ try {
144
+ out = new URL(out).pathname;
145
+ } catch {
146
+ }
147
+ }
148
+ out = out.replace(/[?#].*$/, "").replace(BUILD_HASH_RE, "$1");
149
+ const chunk = TOP_CHUNK_RE.exec(out)?.[1];
150
+ if (chunk !== void 0) {
151
+ const runtime = chunk.startsWith("turbopack-");
152
+ const id = runtime ? chunk.slice("turbopack-".length) : chunk;
153
+ if (id.length >= 8 && /[0-9]/.test(id) && /[a-z_]/i.test(id)) {
154
+ return `/_next/static/chunks/${runtime ? "turbopack-" : ""}*.js`;
155
+ }
156
+ }
157
+ return out.replace(BEFORE_BUILD_FOLDER_RE, "$1");
158
+ }
159
+ function topFrame(stack) {
160
+ if (!stack) return null;
161
+ let first = null;
162
+ for (const line of stack.split("\n").slice(0, STACK_LINES)) {
163
+ const match = FRAME_V8_RE.exec(line) ?? FRAME_GECKO_RE.exec(line);
164
+ if (!match) continue;
165
+ const frame = {
166
+ fn: (match[1] ?? "").trim() || "<anonymous>",
167
+ file: normalizeFrameFile(match[2] ?? "")
168
+ };
169
+ if (!NOISY_FRAMES.some((marker) => frame.file.includes(marker))) return frame;
170
+ first ??= frame;
171
+ }
172
+ return first;
173
+ }
174
+ function groupingName(name) {
175
+ return (name || "Error").replace(EDGE_WHITESPACE_RE, "");
176
+ }
177
+ function errorFingerprint(input) {
178
+ if (input.digest && /^[0-9]{6,}$/.test(input.digest)) return hash64(`digest:${input.digest}`);
179
+ const frame = topFrame(input.stack);
180
+ return hash64(
181
+ `${groupingName(input.name)}|${normalizeMessage(input.message)}|${frame ? `${frame.fn}@${frame.file}` : ""}`
182
+ );
183
+ }
184
+
185
+ // ../shared/src/ids.ts
186
+ var TRACEPARENT_RE = /^00-([0-9a-f]{32})-([0-9a-f]{16})-[0-9a-f]{2}$/;
187
+ var ZERO_TRACE = "0".repeat(32);
188
+ var ZERO_SPAN = "0".repeat(16);
189
+ function randomHex(chars) {
190
+ const bytes = new Uint8Array(Math.ceil(chars / 2));
191
+ try {
192
+ globalThis.crypto.getRandomValues(bytes);
193
+ } catch {
194
+ for (let i = 0; i < bytes.length; i++) bytes[i] = Math.floor(Math.random() * 256);
195
+ }
196
+ let out = "";
197
+ for (const byte of bytes) out += byte.toString(16).padStart(2, "0");
198
+ return out.slice(0, chars);
199
+ }
200
+ function newTraceId() {
201
+ const id = randomHex(32);
202
+ return id === ZERO_TRACE ? newTraceId() : id;
203
+ }
204
+ function newSpanId() {
205
+ const id = randomHex(16);
206
+ return id === ZERO_SPAN ? newSpanId() : id;
207
+ }
208
+ function formatTraceparent(traceId, spanId) {
209
+ return `00-${traceId}-${spanId}-01`;
210
+ }
211
+ function parseTraceparent(value) {
212
+ if (!value) return null;
213
+ const match = TRACEPARENT_RE.exec(value.trim().toLowerCase());
214
+ if (!match || match[1] === ZERO_TRACE || match[2] === ZERO_SPAN) return null;
215
+ return { traceId: match[1] ?? "", spanId: match[2] ?? "" };
216
+ }
217
+ function isHexId(value) {
218
+ return typeof value === "string" && /^[0-9a-f]{32}$/.test(value);
219
+ }
220
+
221
+ export { SESSION_HEADER, TRACEPARENT_HEADER, baseUrl, cutChars, errorFingerprint, formatTraceparent, hash64, isHexId, matchesRoute, matchesText, matchesUrl, newSpanId, newTraceId, parseTraceparent, pathnameOf, randomHex, redactMessage, routeOf, scrubEmails };
@@ -0,0 +1,51 @@
1
+ import { pathnameOf, routeOf } from './chunk-U6USLZJ2.js';
2
+
3
+ // ../next/src/route-names.ts
4
+ var STATIC = 0;
5
+ var DYNAMIC = 1;
6
+ var CATCH_ALL = 2;
7
+ var OPTIONAL_CATCH_ALL = 3;
8
+ function kindOf(segment) {
9
+ if (/^\[\[\.\.\.[^\]]+\]\]$/.test(segment)) return OPTIONAL_CATCH_ALL;
10
+ if (/^\[\.\.\.[^\]]+\]$/.test(segment)) return CATCH_ALL;
11
+ if (/^\[[^\]]+\]$/.test(segment)) return DYNAMIC;
12
+ return STATIC;
13
+ }
14
+ function escape(text) {
15
+ return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
16
+ }
17
+ function compile(route) {
18
+ const segments = route.split("/").filter(Boolean);
19
+ const kinds = segments.map(kindOf);
20
+ let source = "";
21
+ segments.forEach((segment, index) => {
22
+ const kind = kinds[index];
23
+ if (kind === OPTIONAL_CATCH_ALL) source += "(?:/.+)?";
24
+ else if (kind === CATCH_ALL) source += "/.+";
25
+ else if (kind === DYNAMIC) source += "/[^/]+";
26
+ else source += `/${escape(segment)}`;
27
+ });
28
+ return { route, kinds, pattern: new RegExp(`^${source || "/"}$`) };
29
+ }
30
+ function compareNames(a, b) {
31
+ const length = Math.max(a.kinds.length, b.kinds.length);
32
+ for (let i = 0; i < length; i++) {
33
+ const left = a.kinds[i];
34
+ const right = b.kinds[i];
35
+ if (left === void 0) return -1;
36
+ if (right === void 0) return 1;
37
+ if (left !== right) return left - right;
38
+ }
39
+ return 0;
40
+ }
41
+ function routeNamer(routes, basePath = "") {
42
+ const compiled = routes.map(compile).sort(compareNames);
43
+ return (pathname) => {
44
+ let path = pathnameOf(pathname);
45
+ if (basePath && (path === basePath || path.startsWith(`${basePath}/`))) path = path.slice(basePath.length) || "/";
46
+ const found = compiled.find((name) => name.pattern.test(path === "/" ? "/" : path));
47
+ return found ? found.route : routeOf(path);
48
+ };
49
+ }
50
+
51
+ export { routeNamer };