barejs 0.2.4 → 0.2.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 +80 -33
- package/dist/edge.js +70 -0
- package/dist/index.js +200 -189
- package/dist/node.js +200 -189
- package/dist/types/client/index.d.ts +100 -0
- package/dist/types/core/bare.d.ts +68 -25
- package/dist/types/core/context.d.ts +1 -0
- package/dist/types/core/router.d.ts +27 -2
- package/dist/types/core/ws-match.d.ts +34 -2
- package/dist/types/edge.d.ts +99 -0
- package/dist/types/index.d.ts +6 -5
- package/dist/types/runtime/types.d.ts +15 -0
- package/dist/types/security/validators.d.ts +46 -1
- package/dist/types/utils/tracing.d.ts +25 -0
- package/llms.txt +13 -6
- package/package.json +7 -3
- package/src/client/index.ts +399 -6
- package/src/core/bare.ts +366 -46
- package/src/core/context.ts +10 -3
- package/src/core/radix.ts +54 -31
- package/src/core/router.ts +37 -4
- package/src/core/ws-match.ts +17 -2
- package/src/edge.ts +494 -0
- package/src/index.ts +16 -5
- package/src/runtime/bun.ts +39 -7
- package/src/runtime/node.ts +37 -4
- package/src/runtime/types.ts +16 -0
- package/src/runtime/uws.ts +60 -12
- package/src/security/auth.ts +7 -5
- package/src/security/validators.ts +240 -38
- package/src/utils/openapi.ts +39 -9
- package/src/utils/tracing.ts +86 -0
package/src/utils/openapi.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { isStatusResponseMap } from '../security/validators';
|
|
2
|
+
|
|
1
3
|
export interface OpenAPIOptions {
|
|
2
4
|
title?: string;
|
|
3
5
|
version?: string;
|
|
@@ -119,7 +121,7 @@ export const generateOpenAPISpec = (routes: any[], options: OpenAPIOptions = {})
|
|
|
119
121
|
}
|
|
120
122
|
}
|
|
121
123
|
if (rs.response) {
|
|
122
|
-
responseSchema =
|
|
124
|
+
responseSchema = rs.response;
|
|
123
125
|
}
|
|
124
126
|
}
|
|
125
127
|
if ((h as any).__schema && !requestBody) {
|
|
@@ -151,6 +153,41 @@ export const generateOpenAPISpec = (routes: any[], options: OpenAPIOptions = {})
|
|
|
151
153
|
}
|
|
152
154
|
const allParams = [...pathParams, ...params];
|
|
153
155
|
|
|
156
|
+
const STATUS_DESCRIPTIONS: Record<string, string> = {
|
|
157
|
+
'200': 'Success',
|
|
158
|
+
'201': 'Created',
|
|
159
|
+
'202': 'Accepted',
|
|
160
|
+
'204': 'No Content',
|
|
161
|
+
'400': 'Bad Request',
|
|
162
|
+
'401': 'Unauthorized',
|
|
163
|
+
'403': 'Forbidden',
|
|
164
|
+
'404': 'Not Found',
|
|
165
|
+
'409': 'Conflict',
|
|
166
|
+
'413': 'Payload Too Large',
|
|
167
|
+
'415': 'Unsupported Media Type',
|
|
168
|
+
'422': 'Unprocessable Entity',
|
|
169
|
+
'429': 'Too Many Requests',
|
|
170
|
+
'500': 'Internal Server Error',
|
|
171
|
+
'503': 'Service Unavailable'
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
const responses: Record<string, any> = {};
|
|
175
|
+
if (responseSchema && isStatusResponseMap(responseSchema)) {
|
|
176
|
+
for (const [code, subSchema] of Object.entries(responseSchema)) {
|
|
177
|
+
responses[code] = {
|
|
178
|
+
description: STATUS_DESCRIPTIONS[code] || `Response ${code}`,
|
|
179
|
+
content: { 'application/json': { schema: extractSchema(subSchema) } }
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
} else {
|
|
183
|
+
responses['200'] = {
|
|
184
|
+
description: 'Success',
|
|
185
|
+
...(responseSchema
|
|
186
|
+
? { content: { 'application/json': { schema: extractSchema(responseSchema) } } }
|
|
187
|
+
: {})
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
154
191
|
paths[openApiPath][method] = {
|
|
155
192
|
summary: detail?.summary || `${route.method} ${openApiPath}`,
|
|
156
193
|
...(detail?.description ? { description: detail.description } : {}),
|
|
@@ -158,14 +195,7 @@ export const generateOpenAPISpec = (routes: any[], options: OpenAPIOptions = {})
|
|
|
158
195
|
...(routeSecurity.length ? { security: routeSecurity } : {}),
|
|
159
196
|
...(allParams.length ? { parameters: allParams } : {}),
|
|
160
197
|
...(requestBody ? { requestBody } : {}),
|
|
161
|
-
responses
|
|
162
|
-
'200': {
|
|
163
|
-
description: 'Success',
|
|
164
|
-
...(responseSchema
|
|
165
|
-
? { content: { 'application/json': { schema: responseSchema } } }
|
|
166
|
-
: {})
|
|
167
|
-
}
|
|
168
|
-
}
|
|
198
|
+
responses
|
|
169
199
|
};
|
|
170
200
|
}
|
|
171
201
|
|
|
@@ -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 });
|