barejs 0.2.5 → 0.2.7
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 +31 -13
- package/dist/edge.js +37 -42
- package/dist/index.js +173 -166
- package/dist/node.js +173 -166
- package/dist/types/client/index.d.ts +58 -15
- package/dist/types/core/bare.d.ts +69 -26
- package/dist/types/core/router.d.ts +24 -2
- package/dist/types/core/ws-match.d.ts +34 -2
- package/dist/types/edge.d.ts +1 -0
- package/dist/types/index.d.ts +7 -5
- package/dist/types/security/validators.d.ts +14 -1
- package/dist/types/utils/file.d.ts +23 -0
- package/dist/types/utils/tracing.d.ts +25 -0
- package/llms.txt +10 -4
- package/package.json +3 -3
- package/src/client/index.ts +129 -24
- package/src/core/bare.ts +411 -44
- package/src/core/radix.ts +54 -31
- package/src/core/router.ts +36 -3
- package/src/core/ws-match.ts +17 -2
- package/src/edge.ts +2 -0
- package/src/index.ts +7 -3
- package/src/runtime/bun.ts +27 -7
- package/src/runtime/node.ts +16 -1
- package/src/runtime/uws.ts +50 -10
- package/src/security/validators.ts +71 -6
- package/src/utils/file.ts +111 -0
- package/src/utils/openapi.ts +46 -9
- package/src/utils/tracing.ts +86 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type { Context, Middleware, Next } from '../core/context';
|
|
2
|
+
|
|
3
|
+
export interface TracingSpan {
|
|
4
|
+
traceId: string;
|
|
5
|
+
spanId: string;
|
|
6
|
+
parentSpanId?: string;
|
|
7
|
+
traceparent: string;
|
|
8
|
+
method: string;
|
|
9
|
+
path: string;
|
|
10
|
+
status: number;
|
|
11
|
+
durationMs: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface TracingOptions {
|
|
15
|
+
/** Metric name emitted in `Server-Timing` header (default: `'app'`). Set `false` to omit. */
|
|
16
|
+
serverTiming?: string | false;
|
|
17
|
+
/** Echo W3C `traceparent` response header (default: `true`) */
|
|
18
|
+
exposeTraceparent?: boolean;
|
|
19
|
+
/** Optional span callback for forwarding to OTLP / APM / structured logger */
|
|
20
|
+
onSpan?: (span: TracingSpan) => void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const TRACEPARENT_RE = /^([0-9a-f]{2})-([0-9a-f]{32})-([0-9a-f]{16})-([0-9a-f]{2})$/;
|
|
24
|
+
const HEX = Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, '0'));
|
|
25
|
+
|
|
26
|
+
const randomHex = (byteLen: number): string => {
|
|
27
|
+
const bytes = new Uint8Array(byteLen);
|
|
28
|
+
crypto.getRandomValues(bytes);
|
|
29
|
+
let out = '';
|
|
30
|
+
for (let i = 0; i < byteLen; i++) out += HEX[bytes[i]!]!;
|
|
31
|
+
return out;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Zero-dependency W3C `traceparent` + `Server-Timing` middleware.
|
|
36
|
+
* Tree-shakeable and Edge-safe (uses Web Crypto `crypto.getRandomValues` + `performance.now()`).
|
|
37
|
+
*/
|
|
38
|
+
export const tracing = (options: TracingOptions = {}): Middleware => {
|
|
39
|
+
const metricName = options.serverTiming === false ? null : (options.serverTiming || 'app').replace(/[^a-zA-Z0-9_.-]/g, '');
|
|
40
|
+
const exposeTp = options.exposeTraceparent ?? true;
|
|
41
|
+
const onSpan = options.onSpan;
|
|
42
|
+
|
|
43
|
+
return async (ctx: Context, next: Next) => {
|
|
44
|
+
const t0 = performance.now();
|
|
45
|
+
const incoming = ctx.req.headers.get('traceparent')?.trim().toLowerCase();
|
|
46
|
+
const m = incoming ? TRACEPARENT_RE.exec(incoming) : null;
|
|
47
|
+
|
|
48
|
+
const traceId = m && m[2] !== '00000000000000000000000000000000' ? m[2]! : randomHex(16);
|
|
49
|
+
const parentSpanId = m && m[3] !== '0000000000000000' ? m[3]! : undefined;
|
|
50
|
+
const flags = m ? m[4]! : '01';
|
|
51
|
+
const spanId = randomHex(8);
|
|
52
|
+
const traceparent = `00-${traceId}-${spanId}-${flags}`;
|
|
53
|
+
|
|
54
|
+
ctx.set('traceId', traceId);
|
|
55
|
+
ctx.set('spanId', spanId);
|
|
56
|
+
if (parentSpanId) ctx.set('parentSpanId', parentSpanId);
|
|
57
|
+
ctx.set('traceparent', traceparent);
|
|
58
|
+
if (exposeTp) ctx.setHeader('traceparent', traceparent);
|
|
59
|
+
|
|
60
|
+
const res = await next();
|
|
61
|
+
const durationMs = performance.now() - t0;
|
|
62
|
+
|
|
63
|
+
if (metricName) {
|
|
64
|
+
const timingVal = `${metricName};dur=${durationMs.toFixed(2)}`;
|
|
65
|
+
if (res instanceof Response) {
|
|
66
|
+
try { res.headers.append('Server-Timing', timingVal); } catch {}
|
|
67
|
+
} else {
|
|
68
|
+
ctx.setHeader('Server-Timing', timingVal);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (onSpan) {
|
|
73
|
+
const url = ctx.req.url;
|
|
74
|
+
const slash = url.indexOf('/', 8);
|
|
75
|
+
const q = slash === -1 ? -1 : url.indexOf('?', slash);
|
|
76
|
+
const path = slash === -1 ? '/' : (q === -1 ? url.slice(slash) : url.slice(slash, q));
|
|
77
|
+
const status = res instanceof Response ? res.status : (ctx._status || 200);
|
|
78
|
+
onSpan({ traceId, spanId, parentSpanId, traceparent, method: ctx.req.method, path, status, durationMs });
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return res;
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export const serverTiming = (metricName = 'app'): Middleware =>
|
|
86
|
+
tracing({ serverTiming: metricName, exposeTraceparent: false });
|