@compas/server
Advanced tools
Comparing version 0.0.107 to 0.0.108
@@ -1307,2 +1307,12 @@ // Original types from @types/koa & @types/koa-compose | ||
headers?: HeaderOptions; | ||
/** | ||
* Options passed to the log middleware | ||
*/ | ||
logOptions?: { | ||
/** | ||
* Disable event creation on ctx.event | ||
*/ | ||
disableRootEvent?: boolean; | ||
}; | ||
} | ||
@@ -1309,0 +1319,0 @@ |
{ | ||
"name": "@compas/server", | ||
"version": "0.0.107", | ||
"version": "0.0.108", | ||
"description": "Koa server and common middleware", | ||
@@ -18,7 +18,7 @@ "main": "./index.js", | ||
"dependencies": { | ||
"@compas/insight": "0.0.107", | ||
"@compas/stdlib": "0.0.107", | ||
"@compas/insight": "0.0.108", | ||
"@compas/stdlib": "0.0.108", | ||
"formidable": "2.0.0-canary.20200504.1", | ||
"keygrip": "1.1.0", | ||
"koa": "2.13.0", | ||
"koa": "2.13.1", | ||
"koa-body": "4.2.0", | ||
@@ -48,3 +48,3 @@ "koa-session": "6.1.0" | ||
}, | ||
"gitHead": "3788c6771ecb63aa83528f821c0572b22b524d3d" | ||
"gitHead": "83367f8c39df9f42ff1669b8be6d19fc14c0c5e6" | ||
} |
@@ -22,3 +22,3 @@ import { isProduction } from "@compas/stdlib"; | ||
app.use(logMiddleware()); | ||
app.use(logMiddleware(opts.logOptions ?? {})); | ||
app.use(errorHandler(opts.errorOptions || {})); | ||
@@ -25,0 +25,0 @@ app.use(notFoundHandler()); |
@@ -7,4 +7,37 @@ import { Transform } from "stream"; | ||
* Log basic request and response information | ||
* @param {GetAppOptions.logOptions} options | ||
*/ | ||
export function logMiddleware() { | ||
export function logMiddleware(options) { | ||
/** | ||
* Real log function | ||
* | ||
* @param ctx | ||
* @param {bigint} startTime | ||
* @param {number} length | ||
*/ | ||
function logInfo(ctx, startTime, length) { | ||
const duration = Math.round( | ||
Number(process.hrtime.bigint() - startTime) / 1000000, | ||
); | ||
ctx.log.info({ | ||
request: { | ||
method: ctx.method, | ||
path: ctx.path, | ||
length: Number(ctx.get("Content-Length") || "0"), | ||
}, | ||
response: { | ||
duration, | ||
length, | ||
status: ctx.status, | ||
}, | ||
}); | ||
// Skip eventStop if we don't have events enabled. | ||
// Skip eventStop for CORS requests, this gives a bit cleaner logs. | ||
if (options.disableRootEvent !== true && ctx.method !== "OPTIONS") { | ||
eventStop(ctx.event); | ||
} | ||
} | ||
return async (ctx, next) => { | ||
@@ -25,5 +58,8 @@ const startTime = process.hrtime.bigint(); | ||
}); | ||
ctx.event = newEvent(ctx.log); | ||
eventStart(ctx.event, `${ctx.method}.${ctx.path}`); | ||
if (options.disableRootEvent !== true) { | ||
ctx.event = newEvent(ctx.log); | ||
eventStart(ctx.event, `${ctx.method}.${ctx.path}`); | ||
} | ||
await next(); | ||
@@ -63,32 +99,2 @@ | ||
/** | ||
* Real log function | ||
* | ||
* @param ctx | ||
* @param {bigint} startTime | ||
* @param {number} length | ||
*/ | ||
function logInfo(ctx, startTime, length) { | ||
const duration = Math.round( | ||
Number(process.hrtime.bigint() - startTime) / 1000000, | ||
); | ||
ctx.log.info({ | ||
request: { | ||
method: ctx.method, | ||
path: ctx.path, | ||
length: Number(ctx.get("Content-Length") || "0"), | ||
}, | ||
response: { | ||
duration, | ||
length, | ||
status: ctx.status, | ||
}, | ||
}); | ||
if (ctx.method !== "OPTIONS") { | ||
eventStop(ctx.event); | ||
} | ||
} | ||
/** | ||
* Wait for the ctx.body stream to finish before resolving | ||
@@ -95,0 +101,0 @@ * |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
58460
1839
+ Added@compas/insight@0.0.108(transitive)
+ Added@compas/stdlib@0.0.108(transitive)
+ Added@types/node@14.14.20(transitive)
+ Addedkoa@2.13.1(transitive)
- Removed@compas/insight@0.0.107(transitive)
- Removed@compas/stdlib@0.0.107(transitive)
- Removed@types/node@14.14.19(transitive)
- Removedkoa@2.13.0(transitive)
Updated@compas/insight@0.0.108
Updated@compas/stdlib@0.0.108
Updatedkoa@2.13.1