Skip to content

Commit 63ac925

Browse files
committed
[refactor] core proxy logic. all tests should be passing.
1 parent cdb4524 commit 63ac925

File tree

1 file changed

+16
-38
lines changed

1 file changed

+16
-38
lines changed

lib/node-http-proxy/http-proxy.js

+16-38
Original file line numberDiff line numberDiff line change
@@ -218,46 +218,24 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
218218
return;
219219
}
220220

221-
//
222-
// For each data `chunk` received from the `reverseProxy`
223-
// `response` write it to the outgoing `res`.
224-
// If the res socket has been killed already, then write()
225-
// will throw. Nevertheless, try our best to end it nicely.
226-
//
227-
var paused = false;
228-
response.on('data', function (chunk) {
229-
if (req.method !== 'HEAD' && res.writable) {
230-
try {
231-
var flushed = res.write(chunk);
232-
}
233-
catch (ex) {
234-
console.error("res.write error: %s", ex.message);
235-
236-
try { res.end() }
237-
catch (ex) { console.error("res.end error: %s", ex.message) }
238-
239-
return;
240-
}
241-
242-
if (!flushed && !paused) {
243-
paused = true;
244-
response.pause();
245-
res.once('drain', function () {
246-
paused = false;
247-
try { response.resume() }
248-
catch (er) { console.error("response.resume error: %s", er.message) }
249-
});
250-
251-
//
252-
// Force the `drain` event in 100ms if it hasn't
253-
// happened on its own.
254-
//
255-
setTimeout(function () {
256-
res.emit('drain');
257-
}, 100);
221+
222+
function ondata(chunk) {
223+
if (res.writable) {
224+
if (false === res.write(chunk) && response.pause) {
225+
response.pause();
226+
}
258227
}
259228
}
260-
});
229+
230+
response.on('data', ondata);
231+
232+
function ondrain() {
233+
if (response.readable && response.resume) {
234+
response.resume();
235+
}
236+
}
237+
238+
res.on('drain', ondrain);
261239

262240
//
263241
// When the `reverseProxy` `response` ends, end the

0 commit comments

Comments
 (0)