Skip to content

Commit

Permalink
fix recorder when calling res.end(data)
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Kuba committed Jun 4, 2017
1 parent f6369d0 commit f984668
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
18 changes: 18 additions & 0 deletions lib/recorder.js
Expand Up @@ -8,6 +8,7 @@ var debug = require('debug')('nock.recorder');
var _ = require('lodash');
var Stream = require('stream');
var URL = require('url');
var semver = require('semver')

var SEPARATOR = '\n<<<<<<-- cut here -->>>>>>\n';
var recordingInProgress = false;
Expand Down Expand Up @@ -340,6 +341,23 @@ function record(rec_options) {
}
};

// in Node 8, res.end() does not call res.write() directly
if (semver.satisfies(process.version, '>=8')) {
var oldEnd = req.end;
req.end = function(data, encoding, callback) {
if ('undefined' !== typeof(data)) {
if (data) {
debug(thisRecordingId, 'new', proto, 'body chunk');
if (! Buffer.isBuffer(data)) {
data = new Buffer(data, encoding);
}
bodyChunks.push(data);
}
}
oldEnd.apply(req, arguments);
};
}

return req;
});
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -174,7 +174,8 @@
"lodash": "~4.17.2",
"mkdirp": "^0.5.0",
"propagate": "0.4.0",
"qs": "^6.0.2"
"qs": "^6.0.2",
"semver": "^5.3.0"
},
"devDependencies": {
"async": "^2.1.1",
Expand Down

0 comments on commit f984668

Please sign in to comment.