Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Added response.destroy method #1675

Merged
merged 4 commits into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 1 addition & 11 deletions lib/request_overrider.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,7 @@ function RequestOverrider(req, options, interceptors, remove) {
req.socket = req.connection = new Socket({ proto: options.proto })
response.socket = response.client = response.connection = req.socket

// https://nodejs.org/api/http.html#http_message_destroy_error
response.destroy = function(err) {
debug('res.destroy')
response.emit('close', err)
req.socket.destroy()
if (err instanceof Error) {
emitError(err)
}
}

propagate(['timeout'], req.socket, req)
propagate(['timeout', 'error'], req.socket, req)

// Emit a fake socket event on the next tick to mimic what would happen on a real request.
// Some clients listen for a 'socket' event to be emitted before calling end(),
Expand Down
6 changes: 5 additions & 1 deletion lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ module.exports = class Socket extends EventEmitter {
).toString('base64')
}

destroy() {
destroy(err) {
this.destroyed = true
this.readable = this.writable = false
if (err) {
this.emit('error', err)
}
return this
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is return this needed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the documented behavior.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see it now. Thanks.

}
}