Skip to content

Commit

Permalink
fix(metrics): fix issue where package download counters were not full…
Browse files Browse the repository at this point in the history
…y accurate

During real world testing, the count of package downloads almost always came in lower then it should
have. It seems that sometimes the npm client kills the connection prior to Express thinking the full
response has been sent. The solution seems to be to rely on the `close` event instead of the
`finish` event on the response object.

fix #7
  • Loading branch information
Ed Clement committed Dec 23, 2021
1 parent 001e8f8 commit 97eb944
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export default class VerdaccioMiddlewarePlugin implements IPluginMiddleware<Metr

// We won't know the final status code until the response is sent to the client. Because of this we don't collect
// the metrics for this request until the response 'finish' event is emitted.
res.once('finish', () => {
res.once('close', () => {
const { statusCode } = res;
const metricLabels: MetricsLabels = { username, userAgentName, statusCode };
if (packageGroup) {
Expand Down
2 changes: 1 addition & 1 deletion tests/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export function generateMockResponse() {
status: jest.fn(),
send: jest.fn(),
once: jest.fn().mockImplementation(function (eventName, handler: any) {
if (eventName === 'finish') {
if (eventName === 'close') {
mockResponse.statusCode = 200;
mockResponse.finish.mockImplementation(handler);
}
Expand Down

0 comments on commit 97eb944

Please sign in to comment.