@@ -95,20 +95,18 @@ export class Parser {
9595 this . mergeBuffer ( buffer )
9696 const bufferFullLength = this . bufferLength
9797 let offset = this . bufferOffset
98- while ( offset + HEADER_LENGTH <= bufferFullLength ) {
99- // code is 1 byte long - it identifies the message type
100- const code = this . buffer [ offset ]
98+ const bufferWhileLength = bufferFullLength - HEADER_LENGTH
99+ while ( offset <= bufferWhileLength ) {
101100 // length is 1 Uint32BE - it is the length of the message EXCLUDING the code
102101 const length = this . buffer . readUInt32BE ( offset + CODE_LENGTH )
103102 const fullMessageLength = CODE_LENGTH + length
104- if ( fullMessageLength + offset <= bufferFullLength ) {
105- const message = this . handlePacket ( offset + HEADER_LENGTH , code , length , this . buffer )
106- callback ( message )
107- offset += fullMessageLength
108- } else {
109- break
110- }
103+ if ( fullMessageLength + offset > bufferFullLength ) break
104+
105+ const message = this . handlePacket ( offset + HEADER_LENGTH , this . buffer [ offset ] , length )
106+ callback ( message )
107+ offset += fullMessageLength
111108 }
109+
112110 if ( offset === bufferFullLength ) {
113111 // No more use for the buffer
114112 this . bufferLength = 0
@@ -145,7 +143,7 @@ export class Parser {
145143 this . bufferLength += buffer . length
146144 }
147145
148- private handlePacket ( offset : number , code : number , length : number , bytes : Buffer ) : BackendMessage {
146+ private handlePacket ( offset : number , code : number , length : number ) : BackendMessage {
149147 switch ( code ) {
150148 case MessageCodes . BindComplete :
151149 return bindComplete
@@ -164,33 +162,33 @@ export class Parser {
164162 case MessageCodes . EmptyQuery :
165163 return emptyQuery
166164 case MessageCodes . DataRow :
167- return this . parseDataRowMessage ( offset , length , bytes )
165+ return this . parseDataRowMessage ( offset , length , this . buffer )
168166 case MessageCodes . CommandComplete :
169- return this . parseCommandCompleteMessage ( offset , length , bytes )
167+ return this . parseCommandCompleteMessage ( offset , length , this . buffer )
170168 case MessageCodes . ReadyForQuery :
171- return this . parseReadyForQueryMessage ( offset , length , bytes )
169+ return this . parseReadyForQueryMessage ( offset , length , this . buffer )
172170 case MessageCodes . NotificationResponse :
173- return this . parseNotificationMessage ( offset , length , bytes )
171+ return this . parseNotificationMessage ( offset , length , this . buffer )
174172 case MessageCodes . AuthenticationResponse :
175- return this . parseAuthenticationResponse ( offset , length , bytes )
173+ return this . parseAuthenticationResponse ( offset , length , this . buffer )
176174 case MessageCodes . ParameterStatus :
177- return this . parseParameterStatusMessage ( offset , length , bytes )
175+ return this . parseParameterStatusMessage ( offset , length , this . buffer )
178176 case MessageCodes . BackendKeyData :
179- return this . parseBackendKeyData ( offset , length , bytes )
177+ return this . parseBackendKeyData ( offset , length , this . buffer )
180178 case MessageCodes . ErrorMessage :
181- return this . parseErrorMessage ( offset , length , bytes , 'error' )
179+ return this . parseErrorMessage ( offset , length , this . buffer , 'error' )
182180 case MessageCodes . NoticeMessage :
183- return this . parseErrorMessage ( offset , length , bytes , 'notice' )
181+ return this . parseErrorMessage ( offset , length , this . buffer , 'notice' )
184182 case MessageCodes . RowDescriptionMessage :
185- return this . parseRowDescriptionMessage ( offset , length , bytes )
183+ return this . parseRowDescriptionMessage ( offset , length , this . buffer )
186184 case MessageCodes . ParameterDescriptionMessage :
187- return this . parseParameterDescriptionMessage ( offset , length , bytes )
185+ return this . parseParameterDescriptionMessage ( offset , length , this . buffer )
188186 case MessageCodes . CopyIn :
189- return this . parseCopyInMessage ( offset , length , bytes )
187+ return this . parseCopyInMessage ( offset , length , this . buffer )
190188 case MessageCodes . CopyOut :
191- return this . parseCopyOutMessage ( offset , length , bytes )
189+ return this . parseCopyOutMessage ( offset , length , this . buffer )
192190 case MessageCodes . CopyData :
193- return this . parseCopyData ( offset , length , bytes )
191+ return this . parseCopyData ( offset , length , this . buffer )
194192 default :
195193 return new DatabaseError ( 'received invalid response: ' + code . toString ( 16 ) , length , 'error' )
196194 }
0 commit comments