-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: 💡 allow more control of logger config * feat: 🎸 add timing middleware to calculate response time * chore: 🤖 changeset * chore: 🤖 lint
- Loading branch information
1 parent
5153f87
commit f284f52
Showing
4 changed files
with
114 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@sebspark/logging": minor | ||
--- | ||
|
||
Added timing middleware and improved logger configuration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import type { Response } from 'express' | ||
import type { Request } from 'express' | ||
import type { ParamsDictionary } from 'express-serve-static-core' | ||
import type { NextFunction } from 'express-serve-static-core' | ||
import type { ParsedQs } from 'qs' | ||
import { expect, test, vi } from 'vitest' | ||
import { | ||
asyncLocalStorage, | ||
getElapsedTime, | ||
getStartTime, | ||
runWithTiming, | ||
timingMiddleware, | ||
} from './timing' | ||
|
||
test('getStartTime returns the start time', () => { | ||
asyncLocalStorage.run({ startTime: 12345 }, () => { | ||
expect(getStartTime()).toBe(12345) | ||
}) | ||
}) | ||
|
||
test('getElapsedTime returns the elapsed time', () => { | ||
asyncLocalStorage.run({ startTime: Date.now() - 1000 }, () => { | ||
expect(getElapsedTime()).toBeGreaterThanOrEqual(1000) | ||
}) | ||
}) | ||
|
||
test('getElapsedTime returns 0 if no start time', () => { | ||
expect(getElapsedTime()).toBe(0) | ||
}) | ||
|
||
test('timingMiddleware sets X-Start-Time header', async () => { | ||
const req = {} as Request< | ||
ParamsDictionary, | ||
unknown, | ||
unknown, | ||
ParsedQs, | ||
Record<string, unknown> | ||
> | ||
const res = { | ||
setHeader: vi.fn(), | ||
} as unknown as Response<unknown, Record<string, unknown>> | ||
const next = vi.fn() as unknown as NextFunction | ||
|
||
await runWithTiming(async () => { | ||
timingMiddleware(req, res, next) | ||
}) | ||
|
||
expect(res.setHeader).toHaveBeenCalledWith('X-Start-Time', expect.any(String)) | ||
expect(next).toHaveBeenCalled() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { AsyncLocalStorage } from 'node:async_hooks' | ||
import type { NextFunction, Request, Response } from 'express' | ||
|
||
export const asyncLocalStorage = new AsyncLocalStorage<{ startTime: number }>() | ||
|
||
export const runWithTiming = async (callback: () => Promise<void>) => { | ||
return new Promise<void>((res) => { | ||
const startTime = Date.now() | ||
asyncLocalStorage.run({ startTime }, async () => { | ||
await callback() | ||
res() | ||
}) | ||
}) | ||
} | ||
|
||
export const getStartTime = () => { | ||
return asyncLocalStorage.getStore()?.startTime | ||
} | ||
|
||
export const getElapsedTime = () => { | ||
const startTime = getStartTime() | ||
if (!startTime) { | ||
return 0 | ||
} | ||
|
||
return Date.now() - startTime | ||
} | ||
|
||
export const timingMiddleware = ( | ||
_req: Request, | ||
res: Response, | ||
next: NextFunction | ||
) => { | ||
runWithTiming(async () => { | ||
const startTime = getStartTime() | ||
if (startTime) { | ||
res.setHeader('X-Start-Time', startTime.toString()) | ||
} | ||
next() | ||
}) | ||
} |