Skip to content

Instantly share code, notes, and snippets.

@kjin
Last active July 26, 2019 23:00
Show Gist options
  • Save kjin/61230fb50dcb6b6474ba83ad1df6a09d to your computer and use it in GitHub Desktop.
Save kjin/61230fb50dcb6b6474ba83ad1df6a09d to your computer and use it in GitHub Desktop.
// Plugin for superagent@5
module.exports = [
{
file: '',
patch: (superagent, tracer) => {
const UnpatchedRequest = superagent.Request;
superagent.Request = Object.assign(function Request(...args) {
const result = UnpatchedRequest.apply(this, args);
// Request#end sends the actual request.
// It's called within a custom thenable's `then` implementation which,
// when `await`'d, loses context.
// This is a Node bug and is tracked here:
// https://github.com/nodejs/node/issues/22360
this.end = tracer.wrap(this.end);
return result;
}, UnpatchedRequest);
superagent.Request.prototype = UnpatchedRequest.prototype;
}
}
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment