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 ) => {
2013 const headers = { 'Content-Type' : 'text/plain' } ;
2114 headers [ 'Content-Length' ] = 50 ;
2215 const socket = res . socket ;
2316 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' ) ;
2818 if ( 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 ) ;
3726 download ( ) ;
3827} ) ) ;
3928
@@ -53,13 +42,17 @@ function download() {
5342 assert . strictEqual ( res . statusCode , 200 ) ;
5443 assert . strictEqual ( res . headers . connection , 'close' ) ;
5544 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 ) ;
5851 const _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
6356 if ( res . complete ) res . readable = true ;
6457 callback ( ) ;
6558 } ;
@@ -70,9 +63,8 @@ function download() {
7063 aborted = true ;
7164 } ) ;
7265 res . on ( 'error' , common . mustNotCall ( ) ) ;
73- fstream . on ( 'finish' , ( ) => {
66+ writable . on ( 'finish' , ( ) => {
7467 assert . strictEqual ( aborted , abortRequest ) ;
75- cleanup ( fname ) ;
7668 finishCountdown . dec ( ) ;
7769 if ( finishCountdown . remaining === 0 ) return ;
7870 abortRequest = false ; // next one should be a good response
0 commit comments