|
2 | 2 |
|
3 | 3 | const common = require('../common');
|
4 | 4 | const http = require('http');
|
| 5 | +const assert = require('assert'); |
5 | 6 | const fs = require('fs');
|
6 | 7 |
|
7 |
| -const server = http.createServer(function(req, res) { |
8 |
| - // this checks if the request gets dumped |
| 8 | +let resEnd = null; |
| 9 | + |
| 10 | +const server = http.createServer(common.mustCall(function(req, res) { |
| 11 | + // This checks if the request gets dumped |
| 12 | + // resume will be triggered by res.end(). |
9 | 13 | req.on('resume', common.mustCall(function() {
|
10 |
| - console.log('resume called'); |
| 14 | + // There is no 'data' event handler anymore |
| 15 | + // it gets automatically removed when dumping the request. |
| 16 | + assert.strictEqual(req.listenerCount('data'), 0); |
11 | 17 |
|
12 | 18 | req.on('data', common.mustCallAtLeast(function(d) {
|
| 19 | + // Leaving the console.log explicitly, so that |
| 20 | + // we can know how many chunks we have received. |
13 | 21 | console.log('data', d);
|
14 | 22 | }, 1));
|
15 | 23 | }));
|
16 | 24 |
|
17 |
| - // end is not called as we are just exhausting |
18 |
| - // the in-memory buffer |
19 |
| - req.on('end', common.mustNotCall); |
20 |
| - |
21 |
| - // this 'data' handler will be removed when dumped |
22 |
| - req.on('data', common.mustNotCall); |
| 25 | + // We explicitly pause the stream |
| 26 | + // so that the following on('data') does not cause |
| 27 | + // a resume. |
| 28 | + req.pause(); |
| 29 | + req.on('data', function() {}); |
23 | 30 |
|
24 |
| - // start sending the response |
| 31 | + // Start sending the response. |
25 | 32 | res.flushHeaders();
|
26 | 33 |
|
27 |
| - setTimeout(function() { |
28 |
| - res.end('hello world'); |
29 |
| - }, common.platformTimeout(100)); |
30 |
| -}); |
| 34 | + resEnd = function() { |
| 35 | + setImmediate(function() { |
| 36 | + res.end('hello world'); |
| 37 | + }); |
| 38 | + }; |
| 39 | +})); |
31 | 40 |
|
32 |
| -server.listen(0, function() { |
| 41 | +server.listen(0, common.mustCall(function() { |
33 | 42 | const req = http.request({
|
34 | 43 | method: 'POST',
|
35 | 44 | port: server.address().port
|
36 | 45 | });
|
37 | 46 |
|
38 | 47 | // Send the http request without waiting
|
39 |
| - // for the body |
| 48 | + // for the body. |
40 | 49 | req.flushHeaders();
|
41 | 50 |
|
42 | 51 | req.on('response', common.mustCall(function(res) {
|
43 |
| - // pipe the body as soon as we get the headers of the |
44 |
| - // response back |
45 |
| - fs.createReadStream(__filename).pipe(req); |
| 52 | + // Pipe the body as soon as we get the headers of the |
| 53 | + // response back. |
| 54 | + const readFileStream = fs.createReadStream(__filename); |
| 55 | + readFileStream.on('end', resEnd); |
| 56 | + |
| 57 | + readFileStream.pipe(req); |
46 | 58 |
|
47 | 59 | res.resume();
|
48 | 60 |
|
49 |
| - // wait for the response |
| 61 | + // Wait for the response. |
50 | 62 | res.on('end', function() {
|
51 | 63 | server.close();
|
52 | 64 | });
|
53 | 65 | }));
|
54 |
| -}); |
| 66 | + |
| 67 | + req.on('error', function() { |
| 68 | + // An error can happen if there is some data still |
| 69 | + // being sent, as the other side is calling .destroy() |
| 70 | + // this is safe to ignore. |
| 71 | + }); |
| 72 | +})); |
0 commit comments