|
1 | 1 | module.exports = (res, cb) => { |
2 | | - res._parent = { |
| 2 | + const parent = { |
3 | 3 | write: res.write, |
4 | 4 | end: res.end, |
5 | 5 | content: undefined, |
6 | 6 | ended: false |
7 | 7 | } |
8 | 8 |
|
9 | 9 | res.write = function (content) { |
10 | | - accumulate(res, content) |
11 | | - return res._parent.write.apply(res, arguments) |
| 10 | + accumulate(parent, content) |
| 11 | + return parent.write.apply(res, arguments) |
12 | 12 | } |
13 | 13 |
|
14 | 14 | res.end = function (content, encoding) { |
15 | | - if (!res._parent.ended) { |
16 | | - res._parent.ended = true |
| 15 | + if (!parent.ended) { |
| 16 | + parent.ended = true |
17 | 17 |
|
18 | | - accumulate(res, content) |
| 18 | + accumulate(parent, content) |
19 | 19 | const headers = res.getHeaders() |
20 | | - const payload = map(res.statusCode, headers, res._parent.content, encoding) |
| 20 | + const payload = map(res.statusCode, headers, parent.content, encoding) |
21 | 21 |
|
22 | 22 | setImmediate(() => { |
23 | 23 | cb(payload) |
24 | 24 | }) |
25 | 25 | } |
26 | 26 |
|
27 | | - return res._parent.end.apply(res, arguments) |
| 27 | + return parent.end.apply(res, arguments) |
28 | 28 | } |
29 | 29 | } |
30 | 30 |
|
31 | 31 | function map (status, headers, data, encoding) { |
32 | 32 | return { |
33 | | - status: status, |
34 | | - headers: headers, |
35 | | - data: data, |
| 33 | + status, |
| 34 | + headers, |
| 35 | + data, |
36 | 36 | encoding: typeof encoding === 'string' ? encoding : null |
37 | 37 | } |
38 | 38 | } |
39 | 39 |
|
40 | | -function accumulate (res, content) { |
| 40 | +function accumulate (parent, content) { |
41 | 41 | if (content) { |
42 | 42 | if (typeof content === 'string') { |
43 | | - res._parent.content = (res._parent.content || '') + content |
| 43 | + parent.content = (parent.content || '') + content |
44 | 44 | } else if (Buffer.isBuffer(content)) { |
45 | | - let oldContent = res._parent.content |
| 45 | + let oldContent = parent.content |
46 | 46 |
|
47 | 47 | if (typeof oldContent === 'string') { |
48 | 48 | oldContent = Buffer.from(oldContent) |
49 | 49 | } else if (!oldContent) { |
50 | 50 | oldContent = Buffer.alloc(0) |
51 | 51 | } |
52 | 52 |
|
53 | | - res._parent.content = Buffer.concat([oldContent, content], oldContent.length + content.length) |
| 53 | + parent.content = Buffer.concat([oldContent, content], oldContent.length + content.length) |
54 | 54 | } else { |
55 | | - res._parent.content = content |
| 55 | + parent.content = content |
56 | 56 | } |
57 | 57 | } |
58 | 58 | } |
0 commit comments