-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
65 lines (56 loc) · 1.44 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'use strict';
var res = require('http').ServerResponse.prototype;
//
// Prevent double overrides of this module.
//
if (res._hasTrailersPatch) return;
/**
* The original setHeader method.
*
* @type {Function}
* @api private
*/
var setHeader = res.setHeader;
/**
* The trailing headers that should be written at the end of the request.
*
* @type {Object}
* @private
*/
res.trailers = {};
/**
* Patch the `setHeader` method of the Outgoing message so it becomes aware of
* a flushed header. It will store the headers in the trailer object so they are
* flushed when the response is ended.
*
* @param {String} name Header name.
* @param {String} value Header value.
* @api public
*/
res.setHeader = function patchedSetHeader(name, value) {
if (this._header) {
this.trailers[name] = value;
} else {
setHeader.call(this, name, value);
}
};
var end = res.end;
/**
* Patch the `end` method so it will write out the trailing headers before the
* connections is closed.
*
* @param {Mixed} data Optional data to write.
* @param {String} encoding The encoding of the message.
* @param {Function} callback The callback function.
* @api public
*/
res.end = function patchedEnd(data, encoding, callback) {
if (Object.keys(this.trailers).length) {
this.addTrailers(this.trailers);
}
return end.call(this, data, encoding, callback);
};
//
// Make sure that the trailers patch is only loaded once.
//
res._hasTrailersPatch = true;