1
1
'use strict' ;
2
2
3
- var util = require ( 'util' ) ;
3
+ var util = require ( 'util' ) ;
4
4
5
5
function JavascriptReplyParser ( ) {
6
6
this . name = exports . name ;
7
- this . _buffer = new Buffer ( 0 ) ;
8
- this . _offset = 0 ;
7
+ this . _buffer = new Buffer ( 0 ) ;
8
+ this . _offset = 0 ;
9
+ this . _buffers = [ ] ;
9
10
}
10
11
11
12
function IncompleteReadBuffer ( message ) {
@@ -66,7 +67,7 @@ JavascriptReplyParser.prototype._parseResult = function (type) {
66
67
return null ;
67
68
}
68
69
69
- if ( packetHeader > this . _bytesRemaining ( ) ) {
70
+ if ( packetHeader > this . _buffer . length - this . _offset ) {
70
71
this . _offset = offset - 1 ;
71
72
throw new IncompleteReadBuffer ( 'Wait for more data.' ) ;
72
73
}
@@ -93,14 +94,37 @@ JavascriptReplyParser.prototype._parseResult = function (type) {
93
94
} ;
94
95
95
96
JavascriptReplyParser . prototype . execute = function ( buffer ) {
96
- this . append ( buffer ) ;
97
+ var i = buffer . length - 1 ;
98
+
99
+ while ( buffer [ i ] !== 0x0a ) {
100
+ i -- ;
101
+ if ( i < 1 ) {
102
+ this . _buffers . push ( buffer ) ;
103
+ return ;
104
+ }
105
+ }
106
+
107
+ if ( this . _buffers . length !== 0 ) {
108
+ this . _buffers . unshift ( this . _offset === 0 ? this . _buffer : this . _buffer . slice ( this . _offset ) ) ;
109
+ this . _buffers . push ( buffer ) ;
110
+ this . _buffer = Buffer . concat ( this . _buffers ) ;
111
+ this . _buffers = [ ] ;
112
+ } else if ( this . _offset >= this . _buffer . length ) {
113
+ this . _buffer = buffer ;
114
+ } else {
115
+ this . _buffer = Buffer . concat ( [ this . _buffer . slice ( this . _offset ) , buffer ] ) ;
116
+ }
117
+ this . _offset = 0 ;
118
+ this . run ( ) ;
119
+ } ;
97
120
98
- var type , offset ;
121
+ JavascriptReplyParser . prototype . run = function ( buffer ) {
122
+ var type , offset = this . _offset ;
99
123
100
124
while ( true ) {
101
125
offset = this . _offset ;
102
126
// at least 4 bytes: :1\r\n
103
- if ( this . _bytesRemaining ( ) < 4 ) {
127
+ if ( this . _buffer . length - this . _offset < 4 ) {
104
128
break ;
105
129
}
106
130
@@ -109,7 +133,7 @@ JavascriptReplyParser.prototype.execute = function (buffer) {
109
133
110
134
if ( type === 43 || type === 58 || type === 36 ) { // Strings + // Integers : // Bulk strings $
111
135
this . send_reply ( this . _parseResult ( type ) ) ;
112
- } else if ( type === 45 ) { // Errors -
136
+ } else if ( type === 45 ) { // Errors -
113
137
this . send_error ( this . _parseResult ( type ) ) ;
114
138
} else if ( type === 42 ) { // Arrays *
115
139
// set a rewind point. if a failure occurs,
@@ -130,24 +154,11 @@ JavascriptReplyParser.prototype.execute = function (buffer) {
130
154
}
131
155
} ;
132
156
133
- JavascriptReplyParser . prototype . append = function ( newBuffer ) {
134
-
135
- // out of data
136
- if ( this . _offset >= this . _buffer . length ) {
137
- this . _buffer = newBuffer ;
138
- this . _offset = 0 ;
139
- return ;
140
- }
141
-
142
- this . _buffer = Buffer . concat ( [ this . _buffer . slice ( this . _offset ) , newBuffer ] ) ;
143
- this . _offset = 0 ;
144
- } ;
145
-
146
157
JavascriptReplyParser . prototype . parseHeader = function ( ) {
147
- var end = this . _packetEndOffset ( ) + 1 ,
148
- value = this . _buffer . toString ( 'ascii' , this . _offset , end - 1 ) | 0 ;
158
+ var end = this . _packetEndOffset ( ) ,
159
+ value = this . _buffer . toString ( 'ascii' , this . _offset , end ) | 0 ;
149
160
150
- this . _offset = end + 1 ;
161
+ this . _offset = end + 2 ;
151
162
152
163
return value ;
153
164
} ;
@@ -167,9 +178,5 @@ JavascriptReplyParser.prototype._packetEndOffset = function () {
167
178
return offset ;
168
179
} ;
169
180
170
- JavascriptReplyParser . prototype . _bytesRemaining = function ( ) {
171
- return ( this . _buffer . length - this . _offset ) < 0 ? 0 : ( this . _buffer . length - this . _offset ) ;
172
- } ;
173
-
174
181
exports . Parser = JavascriptReplyParser ;
175
182
exports . name = 'javascript' ;
0 commit comments