3
3
const common = require ( '../common' ) ;
4
4
const http = require ( 'http' ) ;
5
5
const assert = require ( 'assert' ) ;
6
- const fs = require ( 'fs ' ) ;
6
+ const { Writable } = require ( 'stream ' ) ;
7
7
const Countdown = require ( '../common/countdown' ) ;
8
8
9
- function cleanup ( fname ) {
10
- try {
11
- if ( fs . statSync ( fname ) ) fs . unlinkSync ( fname ) ;
12
- } catch ( err ) { }
13
- }
14
-
15
9
const N = 2 ;
16
- const fname = '/dev/null' ;
17
10
let abortRequest = true ;
18
11
19
12
const server = http . Server ( common . mustCall ( ( req , res ) => {
20
13
const headers = { 'Content-Type' : 'text/plain' } ;
21
14
headers [ 'Content-Length' ] = 50 ;
22
15
const socket = res . socket ;
23
16
res . writeHead ( 200 , headers ) ;
24
- setTimeout ( ( ) => res . write ( 'aaaaaaaaaa' ) , 100 ) ;
25
- setTimeout ( ( ) => res . write ( 'bbbbbbbbbb' ) , 200 ) ;
26
- setTimeout ( ( ) => res . write ( 'cccccccccc' ) , 300 ) ;
27
- setTimeout ( ( ) => res . write ( 'dddddddddd' ) , 400 ) ;
17
+ res . write ( 'aaaaaaaaaabbbbbbbbbbccccccccccdddddddddd' ) ;
28
18
if ( abortRequest ) {
29
- setTimeout ( ( ) => socket . destroy ( ) , 600 ) ;
19
+ process . nextTick ( ( ) => socket . destroy ( ) ) ;
30
20
} else {
31
- setTimeout ( ( ) => res . end ( 'eeeeeeeeee' ) , 1000 ) ;
21
+ process . nextTick ( ( ) => res . end ( 'eeeeeeeeee' ) ) ;
32
22
}
33
23
} , N ) ) ;
34
24
35
25
server . listen ( 0 , common . mustCall ( ( ) => {
36
- cleanup ( fname ) ;
37
26
download ( ) ;
38
27
} ) ) ;
39
28
@@ -53,13 +42,17 @@ function download() {
53
42
assert . strictEqual ( res . statusCode , 200 ) ;
54
43
assert . strictEqual ( res . headers . connection , 'close' ) ;
55
44
let aborted = false ;
56
- const fstream = fs . createWriteStream ( fname ) ;
57
- res . pipe ( fstream ) ;
45
+ const writable = new Writable ( {
46
+ write ( chunk , encoding , callback ) {
47
+ callback ( ) ;
48
+ }
49
+ } ) ;
50
+ res . pipe ( writable ) ;
58
51
const _handle = res . socket . _handle ;
59
52
_handle . _close = res . socket . _handle . close ;
60
53
_handle . close = function ( callback ) {
61
54
_handle . _close ( ) ;
62
- // set readable to true event though request is complete
55
+ // set readable to true even though request is complete
63
56
if ( res . complete ) res . readable = true ;
64
57
callback ( ) ;
65
58
} ;
@@ -70,9 +63,8 @@ function download() {
70
63
aborted = true ;
71
64
} ) ;
72
65
res . on ( 'error' , common . mustNotCall ( ) ) ;
73
- fstream . on ( 'finish' , ( ) => {
66
+ writable . on ( 'finish' , ( ) => {
74
67
assert . strictEqual ( aborted , abortRequest ) ;
75
- cleanup ( fname ) ;
76
68
finishCountdown . dec ( ) ;
77
69
if ( finishCountdown . remaining === 0 ) return ;
78
70
abortRequest = false ; // next one should be a good response
0 commit comments