@cslug/http 0.7.0 → 0.8.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.
- package/docs/README.md +1 -1
- package/package.json +3 -3
- package/src/kernel.ts +69 -38
- package/src/middleware/web.ts +1 -1
- package/src/router.ts +1 -1
package/docs/README.md
CHANGED
|
@@ -10,7 +10,7 @@ export default defineRoutes((r) => {
|
|
|
10
10
|
r.get('/circuits/:slug', [CircuitController, 'show']).name('circuits.show')
|
|
11
11
|
})
|
|
12
12
|
r.resource('operators', OperatorController).only(['index', 'show'])
|
|
13
|
-
r.group({ domain: 'api.
|
|
13
|
+
r.group({ domain: 'api.example.com', prefix: '/v1' }, (r) => { … })
|
|
14
14
|
})
|
|
15
15
|
|
|
16
16
|
// a controller action
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cslug/http",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Router, middleware, HttpContext, sessions, CSRF, error rendering on Bun.serve.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"bun": ">=1.3.14"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"@cslug/core": "0.
|
|
33
|
-
"@cslug/validation": "0.
|
|
32
|
+
"@cslug/core": "0.8.0",
|
|
33
|
+
"@cslug/validation": "0.8.0"
|
|
34
34
|
}
|
|
35
35
|
}
|
package/src/kernel.ts
CHANGED
|
@@ -5,9 +5,11 @@ import {
|
|
|
5
5
|
HEALTH_CHECKS,
|
|
6
6
|
type HealthCheck,
|
|
7
7
|
Logger,
|
|
8
|
+
Metrics,
|
|
8
9
|
NotFoundError,
|
|
9
10
|
runInContext,
|
|
10
11
|
type Token,
|
|
12
|
+
Tracer,
|
|
11
13
|
ValidationError,
|
|
12
14
|
} from '@cslug/core'
|
|
13
15
|
import { type HttpContext, HttpRequest, ResponseBuilder } from './context'
|
|
@@ -140,58 +142,87 @@ export class HttpKernel {
|
|
|
140
142
|
|
|
141
143
|
let status = 500
|
|
142
144
|
let routeName: string | undefined
|
|
145
|
+
let routePath: string | undefined
|
|
146
|
+
let traceId: string | undefined
|
|
147
|
+
const tracer = this.app.resolve(Tracer)
|
|
143
148
|
try {
|
|
144
149
|
return await runInContext(
|
|
145
150
|
{ ...(currentContext() ?? {}), app: this.app, scope, requestId, locale: defaultLocale, http: ctx },
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
151
|
+
() =>
|
|
152
|
+
// a server span per request, continuing the caller's trace (W3C `traceparent`)
|
|
153
|
+
tracer.span(
|
|
154
|
+
request.method,
|
|
155
|
+
{
|
|
156
|
+
kind: 'server',
|
|
157
|
+
parent: request.headers.get('traceparent') ?? undefined,
|
|
158
|
+
attributes: tracer.enabled
|
|
159
|
+
? { 'http.request.method': request.method, 'url.path': url.pathname, 'server.address': host }
|
|
160
|
+
: undefined,
|
|
161
|
+
},
|
|
162
|
+
async (span) => {
|
|
163
|
+
traceId = span.traceId || undefined
|
|
164
|
+
const run = async (): Promise<Response> => {
|
|
165
|
+
// HTML forms can only send GET/POST: `<input name="_method" value="PUT">` spoofs the others
|
|
166
|
+
let method = request.method
|
|
167
|
+
if (method === 'POST' && req.isForm) {
|
|
168
|
+
const spoofed = (await req.body())._method
|
|
169
|
+
if (typeof spoofed === 'string' && ['PUT', 'PATCH', 'DELETE'].includes(spoofed.toUpperCase()))
|
|
170
|
+
method = spoofed.toUpperCase()
|
|
171
|
+
}
|
|
172
|
+
const { route, params, allowed } = this.match(method, host, url.pathname)
|
|
173
|
+
if (!route) {
|
|
174
|
+
if (this.publicDir && (request.method === 'GET' || request.method === 'HEAD')) {
|
|
175
|
+
const file = serveStatic(this.publicDir, url.pathname, request)
|
|
176
|
+
if (file) return file
|
|
177
|
+
}
|
|
178
|
+
if (allowed.size) {
|
|
179
|
+
const allow = [...allowed, ...(allowed.has('GET') ? ['HEAD'] : [])].join(', ')
|
|
180
|
+
return json(
|
|
181
|
+
{ title: 'Method Not Allowed', status: 405, code: 'http.method_not_allowed', requestId },
|
|
182
|
+
{ status: 405, headers: { allow } },
|
|
183
|
+
)
|
|
184
|
+
}
|
|
185
|
+
throw new NotFoundError('route')
|
|
186
|
+
}
|
|
187
|
+
routeName = route.name
|
|
188
|
+
routePath = route.path
|
|
189
|
+
span.updateName(`${method} ${route.path}`)
|
|
190
|
+
Object.assign(ctx.params, params)
|
|
191
|
+
Object.assign(ctx, { routeName: route.name })
|
|
192
|
+
return this.pipeline(ctx, route.middleware, () => this.callAction(ctx, route))
|
|
160
193
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
)
|
|
194
|
+
let res: Response
|
|
195
|
+
try {
|
|
196
|
+
res = await this.pipeline(ctx, this.global, run)
|
|
197
|
+
} catch (error) {
|
|
198
|
+
res = await this.errors.handle(error, ctx)
|
|
199
|
+
if (res.status >= 500) span.recordError(error)
|
|
167
200
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
}
|
|
175
|
-
let res: Response
|
|
176
|
-
try {
|
|
177
|
-
res = await this.pipeline(ctx, this.global, run)
|
|
178
|
-
} catch (error) {
|
|
179
|
-
res = await this.errors.handle(error, ctx)
|
|
180
|
-
}
|
|
181
|
-
res = this.finalize(res, ctx, request.method)
|
|
182
|
-
status = res.status
|
|
183
|
-
return res
|
|
184
|
-
},
|
|
201
|
+
res = this.finalize(res, ctx, request.method)
|
|
202
|
+
status = res.status
|
|
203
|
+
span.setAttributes({ 'http.response.status_code': status, 'http.route': routePath })
|
|
204
|
+
return res
|
|
205
|
+
},
|
|
206
|
+
),
|
|
185
207
|
)
|
|
186
208
|
} finally {
|
|
187
209
|
await scope.dispose()
|
|
210
|
+
const ms = performance.now() - started
|
|
211
|
+
const metrics = this.app.resolve(Metrics)
|
|
212
|
+
if (metrics.enabled)
|
|
213
|
+
metrics.record('http.server.request.duration', ms / 1000, {
|
|
214
|
+
'http.request.method': request.method,
|
|
215
|
+
'http.route': routePath,
|
|
216
|
+
'http.response.status_code': status,
|
|
217
|
+
})
|
|
188
218
|
this.log.info('request', {
|
|
189
219
|
method: request.method,
|
|
190
220
|
path: url.pathname,
|
|
191
221
|
status,
|
|
192
|
-
ms: Math.round(
|
|
222
|
+
ms: Math.round(ms * 10) / 10,
|
|
193
223
|
route: routeName,
|
|
194
224
|
requestId,
|
|
225
|
+
...(traceId ? { traceId } : {}),
|
|
195
226
|
})
|
|
196
227
|
}
|
|
197
228
|
}
|
package/src/middleware/web.ts
CHANGED
|
@@ -16,7 +16,7 @@ export type SessionOptions = {
|
|
|
16
16
|
cookie?: string
|
|
17
17
|
lifetime?: string
|
|
18
18
|
sameSite?: 'lax' | 'strict' | 'none'
|
|
19
|
-
/** Cookie domain, e.g. `.
|
|
19
|
+
/** Cookie domain, e.g. `.example.com` to share a login across subdomains. */
|
|
20
20
|
domain?: string
|
|
21
21
|
}
|
|
22
22
|
|
package/src/router.ts
CHANGED
|
@@ -31,7 +31,7 @@ export type RouteDef = {
|
|
|
31
31
|
|
|
32
32
|
export type GroupOptions = {
|
|
33
33
|
prefix?: string
|
|
34
|
-
/** Host to match, may contain parameters: `':operator.
|
|
34
|
+
/** Host to match, may contain parameters: `':operator.example.com'`. */
|
|
35
35
|
domain?: string
|
|
36
36
|
middleware?: Middleware[]
|
|
37
37
|
/** Route-name prefix: `as: 'admin'` + `.name('show')` → `admin.show`. */
|