Skip to content

Commit 31623fb

Browse files
paulmelnikowgr2m
authored andcommitted
fix: req.end(cb) compatibility with Node 12 (#1551)
1 parent 9a494da commit 31623fb

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

.travis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ jobs:
3434
# Show prettier errors, even if lint fails.
3535
script: run-s --silent --continue-on-error lint prettier:check
3636
- stage: test
37-
node_js: 10
37+
node_js: 12
38+
- node_js: 10
39+
# Avoid running lint and prettier again.
40+
script: npm run --silent unit
3841
- node_js: 8
3942
# Avoid running lint and prettier again.
4043
script: npm run --silent unit

lib/recorder.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,13 @@ function record(rec_options) {
395395
// Starting in Node 8, `res.end()` does not call `res.write()` directly.
396396
// TODO: This is `req.end()`; is that a typo? ^^
397397
const oldEnd = req.end
398-
req.end = function(data, encoding) {
398+
req.end = function(data, encoding, callback) {
399+
// TODO Shuffle the arguments for parity with the real `req.end()`.
400+
// https://github.com/nock/nock/issues/1549
401+
if (_.isFunction(data) && arguments.length === 1) {
402+
callback = data
403+
data = null
404+
}
399405
if (data) {
400406
debug(thisRecordingId, 'new', proto, 'body chunk')
401407
if (!Buffer.isBuffer(data)) {

lib/request_overrider.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,16 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
124124
return false
125125
}
126126

127-
req.end = function(buffer, encoding, callback) {
127+
req.end = function(data, encoding, callback) {
128128
debug('req.end')
129-
if (_.isFunction(buffer) && arguments.length === 1) {
130-
callback = buffer
131-
buffer = null
129+
// TODO Shuffle the arguments for parity with the real `req.end()`.
130+
// https://github.com/nock/nock/issues/1549
131+
if (_.isFunction(data) && arguments.length === 1) {
132+
callback = data
133+
data = null
132134
}
133135
if (!req.aborted && !ended) {
134-
req.write(buffer, encoding, function() {
136+
req.write(data, encoding, function() {
135137
if (typeof callback === 'function') {
136138
callback()
137139
}

0 commit comments

Comments
 (0)