Skip to content

Commit

Permalink
fix: req.end(cb) compatibility with Node 12 (#1551)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmelnikow authored and gr2m committed May 9, 2019
1 parent 9a494da commit 31623fb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Expand Up @@ -34,7 +34,10 @@ jobs:
# Show prettier errors, even if lint fails.
script: run-s --silent --continue-on-error lint prettier:check
- stage: test
node_js: 10
node_js: 12
- node_js: 10
# Avoid running lint and prettier again.
script: npm run --silent unit
- node_js: 8
# Avoid running lint and prettier again.
script: npm run --silent unit
Expand Down
8 changes: 7 additions & 1 deletion lib/recorder.js
Expand Up @@ -395,7 +395,13 @@ function record(rec_options) {
// Starting in Node 8, `res.end()` does not call `res.write()` directly.
// TODO: This is `req.end()`; is that a typo? ^^
const oldEnd = req.end
req.end = function(data, encoding) {
req.end = function(data, encoding, callback) {
// TODO Shuffle the arguments for parity with the real `req.end()`.
// https://github.com/nock/nock/issues/1549
if (_.isFunction(data) && arguments.length === 1) {
callback = data
data = null
}
if (data) {
debug(thisRecordingId, 'new', proto, 'body chunk')
if (!Buffer.isBuffer(data)) {
Expand Down
12 changes: 7 additions & 5 deletions lib/request_overrider.js
Expand Up @@ -124,14 +124,16 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
return false
}

req.end = function(buffer, encoding, callback) {
req.end = function(data, encoding, callback) {
debug('req.end')
if (_.isFunction(buffer) && arguments.length === 1) {
callback = buffer
buffer = null
// TODO Shuffle the arguments for parity with the real `req.end()`.
// https://github.com/nock/nock/issues/1549
if (_.isFunction(data) && arguments.length === 1) {
callback = data
data = null
}
if (!req.aborted && !ended) {
req.write(buffer, encoding, function() {
req.write(data, encoding, function() {
if (typeof callback === 'function') {
callback()
}
Expand Down

0 comments on commit 31623fb

Please sign in to comment.