@@ -43,6 +43,9 @@ function InputStream() {
4343util . inherits ( InputStream , stream . Readable ) ;
4444
4545InputStream . prototype . _data = function ( chunk ) {
46+ if ( this . closed || this . destroyed )
47+ return ;
48+
4649 if ( this . _canPush ) {
4750 this . _canPush = this . push ( chunk ) ;
4851 } else {
@@ -55,6 +58,12 @@ InputStream.prototype._read = function (size) {
5558 while ( this . _buffer . length && ( this . _canPush = this . push ( this . _buffer . shift ( ) ) ) ) ;
5659} ;
5760
61+ InputStream . prototype . _destroy = function ( err , callback ) {
62+ this . _buffer = [ ] ;
63+ this . _canPush = false ;
64+ callback ( err ) ;
65+ }
66+
5867/**
5968 * function OutputStream(conn, type)
6069 * Writable stream interface for FastCGI output streams
@@ -68,66 +77,42 @@ function OutputStream(conn, id, recordType) {
6877 this . _conn = conn ;
6978 this . _id = id ;
7079
71- this . _open = true ;
72-
73- this . on ( 'finish' , function ( ) {
74- this . _conn . stream . writeRecord (
75- this . _id ,
76- new this . recordType ( ) ) ;
77- } ) ;
80+ this . _finalized = false ;
7881}
7982util . inherits ( OutputStream , stream . Writable ) ;
8083
81- OutputStream . prototype . _close = function ( hadError ) {
82- if ( this . _open ) {
83- this . _open = false ;
84- this . emit ( 'close' , hadError ? true : false ) ;
85- }
86- } ;
87-
8884OutputStream . prototype . _write = function ( chunk , encoding , callback ) {
89- var chunks = [ ] ;
90-
91- if ( ! Buffer . isBuffer ( chunk ) ) {
92- chunk = new Buffer ( chunk , encoding ) ;
93- }
94-
95- if ( chunk . length <= 0 ) {
96- callback . call ( this ) ;
97- return ;
98- }
99-
100- var start = 0 , end = 65535 ;
101- while ( end < chunk . length ) {
102- this . _conn . stream . writeRecord (
103- this . _id , new this . recordType ( chunk . slice ( start , end ) ) ) ;
104-
105- start = end ;
106- end += 65535 ;
85+ var start = 0 ;
86+ var self = this ;
87+
88+ function writeSubChunk ( err ) {
89+ if ( err || start >= chunk . length ) {
90+ callback ( err ) ;
91+ return ;
92+ }
93+
94+ self . _conn . stream . writeRecord (
95+ self . _id ,
96+ new self . recordType ( chunk . subarray ( start , Math . min ( start += 65535 , chunk . length ) ) ) ,
97+ writeSubChunk ) ;
10798 }
10899
109- this . _conn . stream . writeRecord (
110- this . _id ,
111- new this . recordType ( chunk . slice ( start ) ) ,
112- callback . bind ( this ) ) ;
100+ writeSubChunk ( ) ;
113101} ;
114102
115- OutputStream . prototype . write = function ( ) {
116- if ( ! this . _open ) {
117- this . emit ( 'error' , new Error ( "Output stream is not open" ) ) ;
118- return ;
119- }
120-
121- return stream . Writable . prototype . write . apply ( this , arguments ) ;
103+ OutputStream . prototype . _final = function ( callback ) {
104+ this . _finalized = true ;
105+ this . _conn . stream . writeRecord ( this . _id , new this . recordType ( ) , callback ) ;
122106}
123107
124- OutputStream . prototype . end = function ( ) {
125- if ( ! this . _open ) {
126- this . emit ( 'error' , new Error ( "Output stream is not open" ) ) ;
127- return ;
108+ OutputStream . prototype . _destroy = function ( err , callback ) {
109+ if ( ! this . _finalized ) {
110+ this . _conn . stream . writeRecord ( this . _id , new this . recordType ( ) , function ( ) {
111+ callback ( err ) ;
112+ } ) ;
113+ } else {
114+ callback ( err ) ;
128115 }
129-
130- return stream . Writable . prototype . end . apply ( this , arguments ) ;
131116}
132117
133118/**
@@ -138,28 +123,25 @@ OutputStream.prototype.end = function () {
138123function IOStream ( conn , id , recordType ) {
139124 stream . Duplex . call ( this ) ;
140125
141- this . recordType = recordType || fcgi . records . StdOut ;
142-
143126 this . _buffer = [ ] ;
144127 this . _canPush = false ;
145128
129+ this . recordType = recordType || fcgi . records . StdOut ;
130+
146131 this . _conn = conn ;
147132 this . _id = id ;
148133
149- this . _open = true ;
150-
151- this . on ( 'finish' , function ( ) {
152- this . _conn . stream . writeRecord (
153- this . _id ,
154- new this . recordType ( ) ) ;
155- } ) ;
134+ this . _finalized = false ;
156135}
157136util . inherits ( IOStream , stream . Duplex ) ;
158137
159138IOStream . prototype . _data = InputStream . prototype . _data ;
160139IOStream . prototype . _read = InputStream . prototype . _read ;
161140
162- IOStream . prototype . _close = OutputStream . prototype . _close ;
163141IOStream . prototype . _write = OutputStream . prototype . _write ;
164- IOStream . prototype . write = OutputStream . prototype . write ;
165- IOStream . prototype . end = OutputStream . prototype . end ;
142+ IOStream . prototype . _final = OutputStream . prototype . _final ;
143+
144+ IOStream . prototype . _destroy = function ( err , callback ) {
145+ InputStream . prototype . _destroy . call ( this , err ,
146+ OutputStream . prototype . _destroy . bind ( this , err , callback ) ) ;
147+ }
0 commit comments