Skip to content

Commit

Permalink
Merge branch 'support-paused-streams'.
Browse files Browse the repository at this point in the history
Merging this in-fork, pending consideration of assaf#163.
  • Loading branch information
zebulonj committed Mar 9, 2019
2 parents 9319382 + 0f432f8 commit 33fe4a7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
9 changes: 7 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ const MATCH_HEADERS = [ /^accept/, /^authorization/, /^body/, /^content-type/, /
// fixtures - Main directory for replay fixtures.
//
// mode - The mode we're running in, see MODES.
//
// stream - The mode we're using for response streams ('flowing' or 'paused').
// See: https://nodejs.org/docs/latest-v8.x/api/stream.html#stream_two_modes
//
class Replay extends EventEmitter {

constructor(mode) {
Expand All @@ -64,6 +68,9 @@ class Replay extends EventEmitter {
this.mode = mode;
this.chain = new Chain();

// Response stream mode
this.stream = 'flowing';

// Localhost servers: pass request to localhost
this._localhosts = new Set([ 'localhost', '127.0.0.1', '::1' ]);
// Pass through requests to these servers
Expand Down Expand Up @@ -152,7 +159,6 @@ class Replay extends EventEmitter {
// Clears loaded fixtures, and updates to new dir
this.catalog.setFixturesDir(dir);
}

}


Expand Down Expand Up @@ -186,4 +192,3 @@ module.exports = replay;
// These must come last since they need module.exports to exist
require('./patch_http_request');
require('./patch_dns_lookup');

2 changes: 1 addition & 1 deletion src/patch_http_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ HTTP.request = function(options, callback) {
return new HTTP.ClientRequest(options, callback);

// Proxy request
const request = new ProxyRequest(options, Replay.chain.start);
const request = new ProxyRequest(options, Replay.chain.start, Replay.stream === 'paused');
if (callback)
request.once('response', callback);
return request;
Expand Down
8 changes: 5 additions & 3 deletions src/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ const URL = require('url');
// HTTP client request that captures the request and sends it down the processing chain.
module.exports = class ProxyRequest extends HTTP.IncomingMessage {

constructor(options = {}, proxy) {
constructor(options = {}, proxy, pauseResponse) {
super();
this.proxy = proxy;
this.pauseResponse = pauseResponse;
this.method = (options.method || 'GET').toUpperCase();
const protocol = options.protocol || (options._defaultAgent && options._defaultAgent.protocol) || 'http:';
const [host, port] = (options.host || options.hostname).split(':');
Expand Down Expand Up @@ -98,6 +99,8 @@ module.exports = class ProxyRequest extends HTTP.IncomingMessage {
}

end(data, encoding, callback) {
const request = this;

assert(!this.ended, 'Already called end');

if (typeof data === 'function')
Expand All @@ -122,7 +125,7 @@ module.exports = class ProxyRequest extends HTTP.IncomingMessage {
else if (captured) {
const response = new ProxyResponse(captured);
this.emit('response', response);
response.resume();
if (!request.pauseResponse) response.resume();
} else {
const error = new Error(`${this.method} ${URL.format(this.url)} refused: not recording and no network access`);
error.code = 'ECONNREFUSED';
Expand Down Expand Up @@ -187,4 +190,3 @@ class ProxyResponse extends Stream.Readable {
}

}

0 comments on commit 33fe4a7

Please sign in to comment.