@@ -16,10 +16,12 @@ var Reader = require('packet-reader')
16
16
17
17
var TEXT_MODE = 0
18
18
var BINARY_MODE = 1
19
+
19
20
var Connection = function ( config ) {
20
21
EventEmitter . call ( this )
21
22
config = config || { }
22
23
this . stream = config . stream || new net . Socket ( )
24
+ this . stream . setNoDelay ( true )
23
25
this . _keepAlive = config . keepAlive
24
26
this . _keepAliveInitialDelayMillis = config . keepAliveInitialDelayMillis
25
27
this . lastBuffer = false
@@ -87,7 +89,8 @@ Connection.prototype.connect = function (port, host) {
87
89
return self . emit ( 'error' , new Error ( 'The server does not support SSL connections' ) )
88
90
case 'S' : // Server supports SSL connections, continue with a secure connection
89
91
break
90
- default : // Any other response byte, including 'E' (ErrorResponse) indicating a server error
92
+ default :
93
+ // Any other response byte, including 'E' (ErrorResponse) indicating a server error
91
94
return self . emit ( 'error' , new Error ( 'There was an error establishing an SSL connection' ) )
92
95
}
93
96
var tls = require ( 'tls' )
@@ -136,8 +139,9 @@ Connection.prototype.attachListeners = function (stream) {
136
139
137
140
Connection . prototype . requestSsl = function ( ) {
138
141
var bodyBuffer = this . writer
139
- . addInt16 ( 0x04D2 )
140
- . addInt16 ( 0x162F ) . flush ( )
142
+ . addInt16 ( 0x04d2 )
143
+ . addInt16 ( 0x162f )
144
+ . flush ( )
141
145
142
146
var length = bodyBuffer . length + 4
143
147
@@ -149,9 +153,7 @@ Connection.prototype.requestSsl = function () {
149
153
}
150
154
151
155
Connection . prototype . startup = function ( config ) {
152
- var writer = this . writer
153
- . addInt16 ( 3 )
154
- . addInt16 ( 0 )
156
+ var writer = this . writer . addInt16 ( 3 ) . addInt16 ( 0 )
155
157
156
158
Object . keys ( config ) . forEach ( function ( key ) {
157
159
var val = config [ key ]
@@ -206,8 +208,7 @@ Connection.prototype.sendSASLInitialResponseMessage = function (mechanism, initi
206
208
207
209
Connection . prototype . sendSCRAMClientFinalMessage = function ( additionalData ) {
208
210
// 0x70 = 'p'
209
- this . writer
210
- . addString ( additionalData )
211
+ this . writer . addString ( additionalData )
211
212
212
213
this . _send ( 0x70 )
213
214
}
@@ -216,11 +217,7 @@ Connection.prototype._send = function (code, more) {
216
217
if ( ! this . stream . writable ) {
217
218
return false
218
219
}
219
- if ( more === true ) {
220
- this . writer . addHeader ( code )
221
- } else {
222
- return this . stream . write ( this . writer . flush ( code ) )
223
- }
220
+ return this . stream . write ( this . writer . flush ( code ) )
224
221
}
225
222
226
223
Connection . prototype . query = function ( text ) {
@@ -229,8 +226,7 @@ Connection.prototype.query = function (text) {
229
226
}
230
227
231
228
// send parse message
232
- // "more" === true to buffer the message until flush() is called
233
- Connection . prototype . parse = function ( query , more ) {
229
+ Connection . prototype . parse = function ( query ) {
234
230
// expect something like this:
235
231
// { name: 'queryName',
236
232
// text: 'select * from blah',
@@ -257,12 +253,13 @@ Connection.prototype.parse = function (query, more) {
257
253
}
258
254
259
255
var code = 0x50
260
- this . _send ( code , more )
256
+ this . _send ( code )
257
+ this . flush ( )
261
258
}
262
259
263
260
// send bind message
264
261
// "more" === true to buffer the message until flush() is called
265
- Connection . prototype . bind = function ( config , more ) {
262
+ Connection . prototype . bind = function ( config ) {
266
263
// normalize config
267
264
config = config || { }
268
265
config . portal = config . portal || ''
@@ -271,13 +268,17 @@ Connection.prototype.bind = function (config, more) {
271
268
var values = config . values || [ ]
272
269
var len = values . length
273
270
var useBinary = false
274
- for ( var j = 0 ; j < len ; j ++ ) { useBinary |= values [ j ] instanceof Buffer }
275
- var buffer = this . writer
276
- . addCString ( config . portal )
277
- . addCString ( config . statement )
278
- if ( ! useBinary ) { buffer . addInt16 ( 0 ) } else {
271
+ for ( var j = 0 ; j < len ; j ++ ) {
272
+ useBinary |= values [ j ] instanceof Buffer
273
+ }
274
+ var buffer = this . writer . addCString ( config . portal ) . addCString ( config . statement )
275
+ if ( ! useBinary ) {
276
+ buffer . addInt16 ( 0 )
277
+ } else {
279
278
buffer . addInt16 ( len )
280
- for ( j = 0 ; j < len ; j ++ ) { buffer . addInt16 ( values [ j ] instanceof Buffer ) }
279
+ for ( j = 0 ; j < len ; j ++ ) {
280
+ buffer . addInt16 ( values [ j ] instanceof Buffer )
281
+ }
281
282
}
282
283
buffer . addInt16 ( len )
283
284
for ( var i = 0 ; i < len ; i ++ ) {
@@ -300,59 +301,63 @@ Connection.prototype.bind = function (config, more) {
300
301
buffer . addInt16 ( 0 ) // format codes to use text
301
302
}
302
303
// 0x42 = 'B'
303
- this . _send ( 0x42 , more )
304
+ this . _send ( 0x42 )
305
+ this . flush ( )
304
306
}
305
307
306
308
// send execute message
307
309
// "more" === true to buffer the message until flush() is called
308
- Connection . prototype . execute = function ( config , more ) {
310
+ Connection . prototype . execute = function ( config ) {
309
311
config = config || { }
310
312
config . portal = config . portal || ''
311
313
config . rows = config . rows || ''
312
- this . writer
313
- . addCString ( config . portal )
314
- . addInt32 ( config . rows )
314
+ this . writer . addCString ( config . portal ) . addInt32 ( config . rows )
315
315
316
316
// 0x45 = 'E'
317
- this . _send ( 0x45 , more )
317
+ this . _send ( 0x45 )
318
+ this . flush ( )
318
319
}
319
320
320
321
var emptyBuffer = Buffer . alloc ( 0 )
321
322
323
+ const flushBuffer = Buffer . from ( [ 0x48 , 0x00 , 0x00 , 0x00 , 0x04 ] )
322
324
Connection . prototype . flush = function ( ) {
323
- // 0x48 = 'H'
324
- this . writer . add ( emptyBuffer )
325
- this . _send ( 0x48 )
325
+ if ( this . stream . writable ) {
326
+ this . stream . write ( flushBuffer )
327
+ }
326
328
}
327
329
330
+ const syncBuffer = Buffer . from ( [ 0x53 , 0x00 , 0x00 , 0x00 , 0x04 ] )
328
331
Connection . prototype . sync = function ( ) {
329
- // clear out any pending data in the writer
330
- this . writer . flush ( 0 )
331
-
332
- this . writer . add ( emptyBuffer )
333
332
this . _ending = true
334
- this . _send ( 0x53 )
333
+ // clear out any pending data in the writer
334
+ this . writer . clear ( )
335
+ if ( this . stream . writable ) {
336
+ this . stream . write ( syncBuffer )
337
+ this . stream . write ( flushBuffer )
338
+ }
335
339
}
336
340
337
341
const END_BUFFER = Buffer . from ( [ 0x58 , 0x00 , 0x00 , 0x00 , 0x04 ] )
338
342
339
343
Connection . prototype . end = function ( ) {
340
344
// 0x58 = 'X'
341
- this . writer . add ( emptyBuffer )
345
+ this . writer . clear ( )
342
346
this . _ending = true
343
347
return this . stream . write ( END_BUFFER , ( ) => {
344
348
this . stream . end ( )
345
349
} )
346
350
}
347
351
348
- Connection . prototype . close = function ( msg , more ) {
352
+ Connection . prototype . close = function ( msg ) {
349
353
this . writer . addCString ( msg . type + ( msg . name || '' ) )
350
- this . _send ( 0x43 , more )
354
+ this . _send ( 0x43 )
351
355
}
352
356
353
- Connection . prototype . describe = function ( msg , more ) {
357
+ Connection . prototype . describe = function ( msg ) {
354
358
this . writer . addCString ( msg . type + ( msg . name || '' ) )
355
- this . _send ( 0x44 , more )
359
+ this . _send ( 0x44 )
360
+ this . flush ( )
356
361
}
357
362
358
363
Connection . prototype . sendCopyFromChunk = function ( chunk ) {
@@ -376,8 +381,9 @@ var Message = function (name, length) {
376
381
377
382
Connection . prototype . parseMessage = function ( buffer ) {
378
383
this . offset = 0
379
- var length = buffer . length + 4
380
- switch ( this . _reader . header ) {
384
+ const length = buffer . length + 4 ;
385
+ const code = this . _reader . header ;
386
+ switch ( code ) {
381
387
case 0x52 : // R
382
388
return this . parseR ( buffer , length )
383
389
@@ -441,6 +447,7 @@ Connection.prototype.parseMessage = function (buffer) {
441
447
case 0x64 : // d
442
448
return this . parsed ( buffer , length )
443
449
}
450
+ console . log ( 'could not parse' , packet )
444
451
}
445
452
446
453
Connection . prototype . parseR = function ( buffer , length ) {
0 commit comments