33const common = require ( '../common' ) ;
44const http = require ( 'http' ) ;
55const assert = require ( 'assert' ) ;
6- const fs = require ( 'fs ' ) ;
6+ const { Writable } = require ( 'stream ' ) ;
77const Countdown = require ( '../common/countdown' ) ;
88
9- function cleanup ( fname ) {
10- try {
11- if ( fs . statSync ( fname ) ) fs . unlinkSync ( fname ) ;
12- } catch ( err ) { }
13- }
14-
159const N = 2 ;
16- const fname = '/dev/null' ;
1710let abortRequest = true ;
1811
1912const server = http . Server ( common . mustCall ( ( req , res ) => {
2013const headers = { 'Content-Type' : 'text/plain' } ;
2114headers [ 'Content-Length' ] = 50 ;
2215const socket = res . socket ;
2316res . 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' ) ;
2818if ( abortRequest ) {
29- setTimeout ( ( ) => socket . destroy ( ) , 600 ) ;
19+ process . nextTick ( ( ) => socket . destroy ( ) ) ;
3020} else {
31- setTimeout ( ( ) => res . end ( 'eeeeeeeeee' ) , 1000 ) ;
21+ process . nextTick ( ( ) => res . end ( 'eeeeeeeeee' ) ) ;
3222}
3323} , N ) ) ;
3424
3525server . listen ( 0 , common . mustCall ( ( ) => {
36- cleanup ( fname ) ;
3726download ( ) ;
3827} ) ) ;
3928
@@ -53,13 +42,17 @@ function download(){
5342assert . strictEqual ( res . statusCode , 200 ) ;
5443assert . strictEqual ( res . headers . connection , 'close' ) ;
5544let 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 ) ;
5851const _handle = res . socket . _handle ;
5952_handle . _close = res . socket . _handle . close ;
6053_handle . close = function ( callback ) {
6154_handle . _close ( ) ;
62- // set readable to true event though request is complete
55+ // set readable to true even though request is complete
6356if ( res . complete ) res . readable = true ;
6457callback ( ) ;
6558} ;
@@ -70,9 +63,8 @@ function download(){
7063aborted = true ;
7164} ) ;
7265res . on ( 'error' , common . mustNotCall ( ) ) ;
73- fstream . on ( 'finish' , ( ) => {
66+ writable . on ( 'finish' , ( ) => {
7467assert . strictEqual ( aborted , abortRequest ) ;
75- cleanup ( fname ) ;
7668finishCountdown . dec ( ) ;
7769if ( finishCountdown . remaining === 0 ) return ;
7870abortRequest = false ; // next one should be a good response
0 commit comments