Skip to content

Commit eaa2ba1

Browse files
author
soletan
committed
revising type detection of response messages
1 parent 6e0922a commit eaa2ba1

File tree

1 file changed

+24
-63
lines changed

1 file changed

+24
-63
lines changed

lib/connection.js

Lines changed: 24 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@ var Connection = function(config) {
2222

2323
util.inherits(Connection, EventEmitter);
2424

25+
Connection.mapMsgNames = {
26+
0x52: 'authenticationOk',
27+
0x53: 'parameterStatus',
28+
0x4b: 'backendKeyData',
29+
0x43: 'commandComplete',
30+
0x5a: 'readyForQuery',
31+
0x54: 'rowDescription',
32+
0x44: 'dataRow',
33+
0x45: 'error',
34+
0x4e: 'notice',
35+
0x31: 'parseComplete',
36+
0x32: 'bindComplete',
37+
0x41: 'notification',
38+
0x6e: 'noData',
39+
0x49: 'emptyQuery',
40+
0x73: 'portalSuspended'
41+
};
42+
2543
var p = Connection.prototype;
2644

2745
p.connect = function(port, host) {
@@ -306,72 +324,15 @@ p.parseMessage = function() {
306324
length: length
307325
};
308326

309-
switch(id)
327+
if ( id in Connection.mapMsgNames )
310328
{
329+
msg.name = Connection.mapMsgNames[id];
330+
var fncName = "parse" + String.fromCharCode( id );
311331

312-
case 0x52: //R
313-
msg.name = 'authenticationOk';
314-
return this.parseR(msg);
315-
316-
case 0x53: //S
317-
msg.name = 'parameterStatus';
318-
return this.parseS(msg);
319-
320-
case 0x4b: //K
321-
msg.name = 'backendKeyData';
322-
return this.parseK(msg);
323-
324-
case 0x43: //C
325-
msg.name = 'commandComplete';
326-
return this.parseC(msg);
327-
328-
case 0x5a: //Z
329-
msg.name = 'readyForQuery';
330-
return this.parseZ(msg);
331-
332-
case 0x54: //T
333-
msg.name = 'rowDescription';
334-
return this.parseT(msg);
335-
336-
case 0x44: //D
337-
msg.name = 'dataRow';
338-
return this.parseD(msg);
339-
340-
case 0x45: //E
341-
msg.name = 'error';
342-
return this.parseE(msg);
343-
344-
case 0x4e: //N
345-
msg.name = 'notice';
346-
return this.parseN(msg);
347-
348-
case 0x31: //1
349-
msg.name = 'parseComplete';
350-
return msg;
351-
352-
case 0x32: //2
353-
msg.name = 'bindComplete';
354-
return msg;
355-
356-
case 0x41: //A
357-
msg.name = 'notification';
358-
return this.parseA(msg);
359-
360-
case 0x6e: //n
361-
msg.name = 'noData';
362-
return msg;
363-
364-
case 0x49: //I
365-
msg.name = 'emptyQuery';
366-
return msg;
367-
368-
case 0x73: //s
369-
msg.name = 'portalSuspended';
370-
return msg;
371-
372-
default:
373-
throw new Error("Unrecognized message code " + id);
332+
return fncName in this ? this[fncName]( msg );
374333
}
334+
else
335+
throw new Error("Unrecognized message code " + id);
375336
};
376337

377338
p.parseR = function(msg) {

0 commit comments

Comments
 (0)