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

fix: Fix req.end(cb) when recording in Node 12 #1551

Merged
merged 3 commits into from
May 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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