Skip to content

Commit 07f0f48

Browse files
echopointgabrielcsapo
authored andcommitted
updates duplex lib to fix cork,uncork and change the behavior of writeHead slightly. These three methods are now chainable. (#27)
1 parent 6e540db commit 07f0f48

File tree

1 file changed

+56
-41
lines changed

1 file changed

+56
-41
lines changed

lib/http-duplex.js

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,61 @@ class HttpDuplex extends EventEmitter {
231231
get writable() {
232232
return this.res.writable;
233233
}
234+
235+
/**
236+
* Sends a response header to the client request. Must only be called one time and before calling response.end().
237+
* @method writeHead
238+
* @alias HttpDuplex.writeHead
239+
* @memberof HttpDuplex
240+
* @param {number} statusCode 3-digit HTTP status code, like 404
241+
* @param {string} [statusMessage] An optional human readable status message to send with the status code
242+
* @param {object} [headers] An object containing the response headers to send
243+
* @returns {this}
244+
* @see {@link https://nodejs.org/api/http.html#http_response_writehead_statuscode_statusmessage_headers|response.writeHead}
245+
* @example var content = 'Under Construction...';
246+
* response.writeHead(200, {
247+
* 'Content-Length': Buffer.byteLength(content),
248+
* 'Content-Type': 'text/plain'
249+
* });
250+
* response.end(content);
251+
*/
252+
writeHead(statusCode, statusMessage, headers) {
253+
this.res.writeHead(statusCode, statusMessage, headers);
254+
return this;
255+
}
256+
257+
/**
258+
* Buffers written data in memory. This data will be flushed when either the uncork or end methods are called.
259+
* @method cork
260+
* @alias HttpDuplex.cork
261+
* @memberof HttpDuplex
262+
* @returns {this}
263+
* @see uncork
264+
* @see {@link https://nodejs.org/api/stream.html#stream_writable_cork|stream.Writeable.cork}
265+
* @example
266+
* request.cork();
267+
* request.write('buffer data ');
268+
* request.write('before sending ');
269+
* request.uncork();
270+
*/
271+
cork() {
272+
this.res.connection.cork();
273+
return this;
274+
}
275+
276+
/**
277+
* Flushes all data buffered since cork() was called.
278+
* @method uncork
279+
* @alias HttpDuplex.cork
280+
* @memberof HttpDuplex
281+
* @returns {this}
282+
* @see cork
283+
* @see {@link https://nodejs.org/api/stream.html#stream_writable_uncork|stream.Writeable.uncork}
284+
*/
285+
uncork() {
286+
this.res.connection.uncork();
287+
return this;
288+
}
234289
}
235290

236291
// proxy request methods
@@ -242,7 +297,7 @@ class HttpDuplex extends EventEmitter {
242297

243298
// proxy respone methods
244299
[
245-
'cork', 'uncork', 'setDefaultEncoding', 'write', 'end', 'flush', 'writeHeader', 'writeHead', 'writeContinue',
300+
'setDefaultEncoding', 'write', 'end', 'flush', 'writeHeader', 'writeContinue',
246301
'setHeader', 'getHeader', 'removeHeader', 'addTrailers'
247302
].forEach(function (name) {
248303
HttpDuplex.prototype[name] = function () {
@@ -334,20 +389,6 @@ module.exports = HttpDuplex;
334389
* @see {@link https://nodejs.org/api/http.html#http_response_addtrailers_headers|response.addTrailers}
335390
*/
336391

337-
/**
338-
* Buffers written data in memory. This data will be flushed when either the uncork or end methods are called.
339-
* @method cork
340-
* @alias HttpDuplex.cork
341-
* @memberof HttpDuplex
342-
* @see uncork
343-
* @see {@link https://nodejs.org/api/stream.html#stream_writable_cork|stream.Writeable.cork}
344-
* @example
345-
* request.cork();
346-
* request.write('buffer data ');
347-
* request.write('before sending ');
348-
* request.uncork();
349-
*/
350-
351392
/**
352393
* Tells the server the response headers and body have been sent and that the message should be considered complete.
353394
* This MUST be called on every response.
@@ -439,15 +480,6 @@ module.exports = HttpDuplex;
439480
* request.setHeader('Set-Cookie', ['type=auth', 'language=javascript']);
440481
*/
441482

442-
/**
443-
* Flushes all data buffered since cork() was called.
444-
* @method uncork
445-
* @alias HttpDuplex.cork
446-
* @memberof HttpDuplex
447-
* @see cork
448-
* @see {@link https://nodejs.org/api/stream.html#stream_writable_uncork|stream.Writeable.uncork}
449-
*/
450-
451483
/**
452484
* Sends a chunk of the response body. This method may be called multiple times to provide successive parts of the
453485
* body.
@@ -476,23 +508,6 @@ module.exports = HttpDuplex;
476508
* {@link https://nodejs.org/api/http.html#http_event_checkcontinue|http.Server/checkContinue}
477509
*/
478510

479-
/**
480-
* Sends a response header to the client request. Must only be called one time and before calling response.end().
481-
* @method writeHead
482-
* @alias HttpDuplex.writeHead
483-
* @memberof HttpDuplex
484-
* @param {Number} statusCode 3-digit HTTP status code, like 404
485-
* @param {String} [statusMessage] An optional human readable status message to send with the status code
486-
* @param {Object} [headers] An object containing the response headers to send
487-
* @see {@link https://nodejs.org/api/http.html#http_response_writehead_statuscode_statusmessage_headers|response.writeHead}
488-
* @example var content = 'Under Construction...';
489-
* response.writeHead(200, {
490-
* 'Content-Length': Buffer.byteLength(content),
491-
* 'Content-Type': 'text/plain'
492-
* });
493-
* response.end(content);
494-
*/
495-
496511
/**
497512
* __Warning:__ This has been deprecated in node, __don't__ use it. Any apis that require this funtion should be
498513
* updated to use writeHead insted.

0 commit comments

Comments
 (0)