@@ -5,17 +5,23 @@ var CopyFromStream = function () {
5
5
this . _buffer = new Buffer ( 0 ) ;
6
6
this . _connection = false ;
7
7
this . _finished = false ;
8
+ this . _finishedSent = false ;
9
+ this . _closed = false ;
8
10
this . _error = false ;
11
+ this . _dataBuffered = false ;
9
12
this . __defineGetter__ ( "writable" , this . _writable . bind ( this ) ) ;
10
13
} ;
11
14
util . inherits ( CopyFromStream , Stream ) ;
12
15
CopyFromStream . prototype . _writable = function ( ) {
13
- return ! this . _finished && ! this . _error ;
16
+ return ! ( this . _finished || this . _error ) ;
14
17
}
15
18
CopyFromStream . prototype . startStreamingToConnection = function ( connection ) {
19
+ if ( this . _error ) {
20
+ return ;
21
+ }
16
22
this . _connection = connection ;
17
- this . _handleChunk ( ) ;
18
- this . _endIfConnectionReady ( ) ;
23
+ this . _sendIfConnectionReady ( ) ;
24
+ this . _endIfNeedAndPossible ( ) ;
19
25
} ;
20
26
CopyFromStream . prototype . _handleChunk = function ( string , encoding ) {
21
27
var dataChunk ,
@@ -30,52 +36,66 @@ CopyFromStream.prototype._handleChunk = function (string, encoding) {
30
36
//Buffer.concat is better, but it's missing
31
37
//in node v0.6.x
32
38
tmpBuffer = new Buffer ( this . _buffer . length + dataChunk . length ) ;
33
- tmpBuffer . copy ( this . _buffer ) ;
34
- tmpBuffer . copy ( dataChunk , this . _buffer . length ) ;
39
+ this . _buffer . copy ( tmpBuffer ) ;
40
+ dataChunk . copy ( tmpBuffer , this . _buffer . length ) ;
35
41
this . _buffer = tmpBuffer ;
36
42
} else {
37
43
this . _buffer = dataChunk ;
38
44
}
39
45
}
46
+
40
47
return this . _sendIfConnectionReady ( ) ;
41
48
} ;
42
49
CopyFromStream . prototype . _sendIfConnectionReady = function ( ) {
43
50
var dataSent = false ;
44
- if ( this . _connection && this . _buffer . length ) {
51
+ if ( this . _connection ) {
45
52
dataSent = this . _connection . sendCopyFromChunk ( this . _buffer ) ;
46
53
this . _buffer = new Buffer ( 0 ) ;
54
+ if ( this . _dataBuffered ) {
55
+ this . emit ( 'drain' ) ;
56
+ }
57
+ this . _dataBuffered = false ;
58
+ } else {
59
+ this . _dataBuffered = true ;
47
60
}
48
61
return dataSent ;
49
62
} ;
50
- CopyFromStream . prototype . _endIfConnectionReady = function ( ) {
51
- if ( this . _connection && this . _finished ) {
52
- //TODO change function name
63
+ CopyFromStream . prototype . _endIfNeedAndPossible = function ( ) {
64
+ if ( this . _connection && this . _finished && ! this . _finishedSent ) {
65
+ this . _finishedSent = true ;
53
66
this . _connection . endCopyFrom ( ) ;
54
67
}
55
68
}
56
69
CopyFromStream . prototype . write = function ( string , encoding ) {
57
- if ( ! this . _writable ) {
58
- //TODO possibly throw exception?
70
+ if ( this . _error || this . _finished ) {
59
71
return false ;
60
72
}
61
73
return this . _handleChunk . apply ( this , arguments ) ;
62
74
} ;
63
75
CopyFromStream . prototype . end = function ( string , encondig ) {
64
- if ( ! this . _writable ) {
65
- //TODO possibly throw exception?
76
+ if ( this . _error || this . _finished ) {
66
77
return false ;
67
78
}
68
79
this . _finished = true ;
69
80
if ( string !== undefined ) {
70
81
this . _handleChunk . apply ( this , arguments ) ;
71
82
} ;
72
- this . _endIfConnectionReady ( ) ;
83
+ this . _endIfNeedAndPossible ( ) ;
73
84
} ;
74
85
CopyFromStream . prototype . error = function ( error ) {
86
+ if ( this . _error || this . _closed ) {
87
+ return false ;
88
+ }
75
89
this . _error = true ;
76
90
this . emit ( 'error' , error ) ;
77
91
} ;
78
92
CopyFromStream . prototype . close = function ( ) {
93
+ if ( this . _error || this . _closed ) {
94
+ return false ;
95
+ }
96
+ if ( ! this . _finishedSent ) {
97
+ throw new Error ( "seems to be error in code that uses CopyFromStream" ) ;
98
+ }
79
99
this . emit ( "close" ) ;
80
100
} ;
81
101
var CopyToStream = function ( ) {
0 commit comments