Skip to content

Commit ad4aa5b

Browse files
committed
Emit a response event when fn is done
1 parent 66b7370 commit ad4aa5b

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/index.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,23 +103,26 @@ export function createHandler (fn: App, options: Options = {}) {
103103
}
104104

105105
fn(req, finalhandler(req))
106-
.then((response): void | Promise<void> => {
106+
.then((res): void | Promise<void> => {
107107
if (returned) {
108108
return
109109
}
110110

111-
response.started = true
111+
// Mark the response as started.
112+
res.started = true
113+
req.events.emit('response', res)
112114

113-
return response.buffer().then((body) => {
114-
const isBase64Encoded = options.isBinary ? options.isBinary(response) : false
115+
return res.buffer().then((body) => {
116+
const isBase64Encoded = options.isBinary ? options.isBinary(res) : false
115117

116-
response.finished = true
117-
response.bytesTransferred = body ? body.length : 0
118+
// Mark the response as finished when buffering is done.
119+
res.finished = true
120+
res.bytesTransferred = body ? body.length : 0
118121

119122
return done(null, {
120-
statusCode: response.status,
123+
statusCode: res.status,
121124
body: body ? (isBase64Encoded ? body.toString('base64') : body.toString('utf8')) : undefined,
122-
headers: response.headers.object(),
125+
headers: res.headers.object(),
123126
isBase64Encoded
124127
})
125128
})

0 commit comments

Comments
 (0)