@@ -25,6 +25,7 @@ import {
25
25
MessageName ,
26
26
AuthenticationMD5Password ,
27
27
NoticeMessage ,
28
+ ParserError ,
28
29
} from './messages'
29
30
import { BufferReader } from './buffer-reader'
30
31
@@ -79,13 +80,11 @@ export class Parser {
79
80
private bufferLength : number = 0
80
81
private bufferOffset : number = 0
81
82
private reader = new BufferReader ( )
82
- private mode : Mode
83
83
84
84
constructor ( opts ?: StreamOptions ) {
85
85
if ( opts ?. mode === 'binary' ) {
86
86
throw new Error ( 'Binary mode not supported yet' )
87
87
}
88
- this . mode = opts ?. mode || 'text'
89
88
}
90
89
91
90
public parse ( buffer : Buffer , callback : MessageCallback ) {
@@ -152,53 +151,57 @@ export class Parser {
152
151
}
153
152
154
153
private handlePacket ( offset : number , code : number , length : number , bytes : Buffer ) : BackendMessage {
155
- switch ( code ) {
156
- case MessageCodes . BindComplete :
157
- return bindComplete
158
- case MessageCodes . ParseComplete :
159
- return parseComplete
160
- case MessageCodes . CloseComplete :
161
- return closeComplete
162
- case MessageCodes . NoData :
163
- return noData
164
- case MessageCodes . PortalSuspended :
165
- return portalSuspended
166
- case MessageCodes . CopyDone :
167
- return copyDone
168
- case MessageCodes . ReplicationStart :
169
- return replicationStart
170
- case MessageCodes . EmptyQuery :
171
- return emptyQuery
172
- case MessageCodes . DataRow :
173
- return this . parseDataRowMessage ( offset , length , bytes )
174
- case MessageCodes . CommandComplete :
175
- return this . parseCommandCompleteMessage ( offset , length , bytes )
176
- case MessageCodes . ReadyForQuery :
177
- return this . parseReadyForQueryMessage ( offset , length , bytes )
178
- case MessageCodes . NotificationResponse :
179
- return this . parseNotificationMessage ( offset , length , bytes )
180
- case MessageCodes . AuthenticationResponse :
181
- return this . parseAuthenticationResponse ( offset , length , bytes )
182
- case MessageCodes . ParameterStatus :
183
- return this . parseParameterStatusMessage ( offset , length , bytes )
184
- case MessageCodes . BackendKeyData :
185
- return this . parseBackendKeyData ( offset , length , bytes )
186
- case MessageCodes . ErrorMessage :
187
- return this . parseErrorMessage ( offset , length , bytes , 'error' )
188
- case MessageCodes . NoticeMessage :
189
- return this . parseErrorMessage ( offset , length , bytes , 'notice' )
190
- case MessageCodes . RowDescriptionMessage :
191
- return this . parseRowDescriptionMessage ( offset , length , bytes )
192
- case MessageCodes . ParameterDescriptionMessage :
193
- return this . parseParameterDescriptionMessage ( offset , length , bytes )
194
- case MessageCodes . CopyIn :
195
- return this . parseCopyInMessage ( offset , length , bytes )
196
- case MessageCodes . CopyOut :
197
- return this . parseCopyOutMessage ( offset , length , bytes )
198
- case MessageCodes . CopyData :
199
- return this . parseCopyData ( offset , length , bytes )
200
- default :
201
- return new DatabaseError ( 'received invalid response: ' + code . toString ( 16 ) , length , 'error' )
154
+ try {
155
+ switch ( code ) {
156
+ case MessageCodes . BindComplete :
157
+ return bindComplete
158
+ case MessageCodes . ParseComplete :
159
+ return parseComplete
160
+ case MessageCodes . CloseComplete :
161
+ return closeComplete
162
+ case MessageCodes . NoData :
163
+ return noData
164
+ case MessageCodes . PortalSuspended :
165
+ return portalSuspended
166
+ case MessageCodes . CopyDone :
167
+ return copyDone
168
+ case MessageCodes . ReplicationStart :
169
+ return replicationStart
170
+ case MessageCodes . EmptyQuery :
171
+ return emptyQuery
172
+ case MessageCodes . DataRow :
173
+ return this . parseDataRowMessage ( offset , length , bytes )
174
+ case MessageCodes . CommandComplete :
175
+ return this . parseCommandCompleteMessage ( offset , length , bytes )
176
+ case MessageCodes . ReadyForQuery :
177
+ return this . parseReadyForQueryMessage ( offset , length , bytes )
178
+ case MessageCodes . NotificationResponse :
179
+ return this . parseNotificationMessage ( offset , length , bytes )
180
+ case MessageCodes . AuthenticationResponse :
181
+ return this . parseAuthenticationResponse ( offset , length , bytes )
182
+ case MessageCodes . ParameterStatus :
183
+ return this . parseParameterStatusMessage ( offset , length , bytes )
184
+ case MessageCodes . BackendKeyData :
185
+ return this . parseBackendKeyData ( offset , length , bytes )
186
+ case MessageCodes . ErrorMessage :
187
+ return this . parseErrorMessage ( offset , length , bytes , 'error' )
188
+ case MessageCodes . NoticeMessage :
189
+ return this . parseErrorMessage ( offset , length , bytes , 'notice' )
190
+ case MessageCodes . RowDescriptionMessage :
191
+ return this . parseRowDescriptionMessage ( offset , length , bytes )
192
+ case MessageCodes . ParameterDescriptionMessage :
193
+ return this . parseParameterDescriptionMessage ( offset , length , bytes )
194
+ case MessageCodes . CopyIn :
195
+ return this . parseCopyInMessage ( offset , length , bytes )
196
+ case MessageCodes . CopyOut :
197
+ return this . parseCopyOutMessage ( offset , length , bytes )
198
+ case MessageCodes . CopyData :
199
+ return this . parseCopyData ( offset , length , bytes )
200
+ default :
201
+ return new DatabaseError ( 'received invalid response: ' + code . toString ( 16 ) , length , 'error' )
202
+ }
203
+ } catch ( error ) {
204
+ return new ParserError ( `unexpected error handling packet: ${ error } ` , length , 'error' )
202
205
}
203
206
}
204
207
0 commit comments