@@ -13,201 +13,201 @@ var util = require('util')
13
13
14
14
const { parse, serialize } = require('pg-protocol')
15
15
16
- // TODO(bmc) support binary mode at some point
17
- var Connection = function (config) {
18
- EventEmitter.call(this)
19
- config = config || {}
20
- this.stream = config.stream || new net.Socket()
21
- this._keepAlive = config.keepAlive
22
- this._keepAliveInitialDelayMillis = config.keepAliveInitialDelayMillis
23
- this.lastBuffer = false
24
- this.parsedStatements = {}
25
- this.ssl = config.ssl || false
26
- this._ending = false
27
- this._emitMessage = false
28
- var self = this
29
- this.on('newListener', function (eventName) {
30
- if (eventName === 'message') {
31
- self._emitMessage = true
32
- }
33
- })
34
- }
35
-
36
- util.inherits(Connection, EventEmitter)
37
-
38
- Connection.prototype.connect = function (port, host) {
39
- var self = this
40
-
41
- this._connecting = true
42
- this.stream.setNoDelay(true)
43
- this.stream.connect(port, host)
16
+ const flushBuffer = serialize.flush()
17
+ const syncBuffer = serialize.sync()
18
+ const endBuffer = serialize.end()
44
19
45
- this.stream.once('connect', function () {
46
- if (self._keepAlive) {
47
- self.stream.setKeepAlive(true, self._keepAliveInitialDelayMillis)
48
- }
49
- self.emit('connect')
50
- })
20
+ // TODO(bmc) support binary mode at some point
21
+ class Connection extends EventEmitter {
22
+ constructor(config) {
23
+ super()
24
+ config = config || {}
25
+ this.stream = config.stream || new net.Socket()
26
+ this._keepAlive = config.keepAlive
27
+ this._keepAliveInitialDelayMillis = config.keepAliveInitialDelayMillis
28
+ this.lastBuffer = false
29
+ this.parsedStatements = {}
30
+ this.ssl = config.ssl || false
31
+ this._ending = false
32
+ this._emitMessage = false
33
+ var self = this
34
+ this.on('newListener', function (eventName) {
35
+ if (eventName === 'message') {
36
+ self._emitMessage = true
37
+ }
38
+ })
39
+ }
51
40
52
- const reportStreamError = function (error) {
53
- // errors about disconnections should be ignored during disconnect
54
- if (self._ending && (error.code === 'ECONNRESET' || error.code === 'EPIPE')) {
55
- return
56
- }
57
- self.emit('error', error)
58
- }
59
- this.stream.on('error', reportStreamError)
60
-
61
- this.stream.on('close', function () {
62
- self.emit('end')
63
- })
64
-
65
- if (!this.ssl) {
66
- return this.attachListeners(this.stream)
67
- }
68
-
69
- this.stream.once('data', function (buffer) {
70
- var responseCode = buffer.toString('utf8')
71
- switch (responseCode) {
72
- case 'S': // Server supports SSL connections, continue with a secure connection
73
- break
74
- case 'N': // Server does not support SSL connections
75
- self.stream.end()
76
- return self.emit('error', new Error('The server does not support SSL connections'))
77
- default:
78
- // Any other response byte, including 'E' (ErrorResponse) indicating a server error
79
- self.stream.end()
80
- return self.emit('error', new Error('There was an error establishing an SSL connection'))
41
+ connect(port, host) {
42
+ var self = this
43
+
44
+ this._connecting = true
45
+ this.stream.setNoDelay(true)
46
+ this.stream.connect(port, host)
47
+
48
+ this.stream.once('connect', function () {
49
+ if (self._keepAlive) {
50
+ self.stream.setKeepAlive(true, self._keepAliveInitialDelayMillis)
51
+ }
52
+ self.emit('connect')
53
+ })
54
+
55
+ const reportStreamError = function (error) {
56
+ // errors about disconnections should be ignored during disconnect
57
+ if (self._ending && (error.code === 'ECONNRESET' || error.code === 'EPIPE')) {
58
+ return
59
+ }
60
+ self.emit('error', error)
81
61
}
82
- var tls = require('tls')
83
- const options = Object.assign(
84
- {
85
- socket: self.stream,
86
- },
87
- self.ssl
88
- )
89
- if (net.isIP(host) === 0) {
90
- options.servername = host
91
- }
92
- self.stream = tls.connect(options)
93
- self.attachListeners(self.stream)
94
- self.stream.on('error', reportStreamError)
62
+ this.stream.on('error', reportStreamError)
95
63
96
- self.emit('sslconnect')
97
- } )
98
- }
64
+ this.stream.on('close', function () {
65
+ self.emit('end' )
66
+ })
99
67
100
- Connection.prototype.attachListeners = function (stream) {
101
- stream.on('end', () => {
102
- this.emit('end')
103
- })
104
- parse(stream, (msg) => {
105
- var eventName = msg.name === 'error' ? 'errorMessage' : msg.name
106
- if (this._emitMessage) {
107
- this.emit('message', msg)
68
+ if (!this.ssl) {
69
+ return this.attachListeners(this.stream)
108
70
}
109
- this.emit(eventName, msg)
110
- })
111
- }
112
71
113
- Connection.prototype.requestSsl = function () {
114
- this.stream.write(serialize.requestSsl())
115
- }
72
+ this.stream.once('data', function (buffer) {
73
+ var responseCode = buffer.toString('utf8')
74
+ switch (responseCode) {
75
+ case 'S': // Server supports SSL connections, continue with a secure connection
76
+ break
77
+ case 'N': // Server does not support SSL connections
78
+ self.stream.end()
79
+ return self.emit('error', new Error('The server does not support SSL connections'))
80
+ default:
81
+ // Any other response byte, including 'E' (ErrorResponse) indicating a server error
82
+ self.stream.end()
83
+ return self.emit('error', new Error('There was an error establishing an SSL connection'))
84
+ }
85
+ var tls = require('tls')
86
+ const options = Object.assign(
87
+ {
88
+ socket: self.stream,
89
+ },
90
+ self.ssl
91
+ )
92
+ if (net.isIP(host) === 0) {
93
+ options.servername = host
94
+ }
95
+ self.stream = tls.connect(options)
96
+ self.attachListeners(self.stream)
97
+ self.stream.on('error', reportStreamError)
98
+
99
+ self.emit('sslconnect')
100
+ })
101
+ }
116
102
117
- Connection.prototype.startup = function (config) {
118
- this.stream.write(serialize.startup(config))
119
- }
103
+ attachListeners(stream) {
104
+ stream.on('end', () => {
105
+ this.emit('end')
106
+ })
107
+ parse(stream, (msg) => {
108
+ var eventName = msg.name === 'error' ? 'errorMessage' : msg.name
109
+ if (this._emitMessage) {
110
+ this.emit('message', msg)
111
+ }
112
+ this.emit(eventName, msg)
113
+ })
114
+ }
120
115
121
- Connection.prototype.cancel = function (processID, secretKey ) {
122
- this._send (serialize.cancel(processID, secretKey ))
123
- }
116
+ requestSsl( ) {
117
+ this.stream.write (serialize.requestSsl( ))
118
+ }
124
119
125
- Connection.prototype.password = function (password ) {
126
- this._send (serialize.password(password ))
127
- }
120
+ startup(config ) {
121
+ this.stream.write (serialize.startup(config ))
122
+ }
128
123
129
- Connection.prototype.sendSASLInitialResponseMessage = function (mechanism, initialResponse ) {
130
- this._send(serialize.sendSASLInitialResponseMessage(mechanism, initialResponse ))
131
- }
124
+ cancel(processID, secretKey ) {
125
+ this._send(serialize.cancel(processID, secretKey ))
126
+ }
132
127
133
- Connection.prototype.sendSCRAMClientFinalMessage = function (additionalData ) {
134
- this._send(serialize.sendSCRAMClientFinalMessage(additionalData ))
135
- }
128
+ password(password ) {
129
+ this._send(serialize.password(password ))
130
+ }
136
131
137
- Connection.prototype._send = function (buffer) {
138
- if (!this.stream.writable) {
139
- return false
132
+ sendSASLInitialResponseMessage(mechanism, initialResponse) {
133
+ this._send(serialize.sendSASLInitialResponseMessage(mechanism, initialResponse))
140
134
}
141
- return this.stream.write(buffer)
142
- }
143
135
144
- Connection.prototype.query = function (text ) {
145
- this._send(serialize.query(text ))
146
- }
136
+ sendSCRAMClientFinalMessage(additionalData ) {
137
+ this._send(serialize.sendSCRAMClientFinalMessage(additionalData ))
138
+ }
147
139
148
- // send parse message
149
- Connection.prototype.parse = function (query) {
150
- this._send(serialize.parse(query))
151
- }
140
+ _send(buffer) {
141
+ if (!this.stream.writable) {
142
+ return false
143
+ }
144
+ return this.stream.write(buffer)
145
+ }
152
146
153
- // send bind message
154
- // "more" === true to buffer the message until flush() is called
155
- Connection.prototype.bind = function (config) {
156
- this._send(serialize.bind(config))
157
- }
147
+ query(text) {
148
+ this._send(serialize.query(text))
149
+ }
158
150
159
- // send execute message
160
- // "more" === true to buffer the message until flush() is called
161
- Connection.prototype.execute = function (config) {
162
- this._send(serialize.execute(config))
163
- }
151
+ // send parse message
152
+ parse(query) {
153
+ this._send(serialize.parse(query))
154
+ }
164
155
165
- const flushBuffer = serialize.flush()
166
- Connection.prototype.flush = function () {
167
- if (this.stream.writable ) {
168
- this.stream.write(flushBuffer )
156
+ // send bind message
157
+ // "more" === true to buffer the message until flush () is called
158
+ bind(config ) {
159
+ this._send(serialize.bind(config) )
169
160
}
170
- }
171
161
172
- const syncBuffer = serialize.sync()
173
- Connection.prototype.sync = function () {
174
- this._ending = true
175
- this._send(flushBuffer)
176
- this._send(syncBuffer)
177
- }
162
+ // send execute message
163
+ // "more" === true to buffer the message until flush() is called
164
+ execute(config) {
165
+ this._send(serialize.execute(config))
166
+ }
178
167
179
- const endBuffer = serialize.end()
168
+ flush() {
169
+ if (this.stream.writable) {
170
+ this.stream.write(flushBuffer)
171
+ }
172
+ }
180
173
181
- Connection.prototype.end = function () {
182
- // 0x58 = 'X'
183
- this._ending = true
184
- if (!this._connecting || !this.stream.writable) {
185
- this.stream.end()
186
- return
174
+ sync() {
175
+ this._ending = true
176
+ this._send(flushBuffer)
177
+ this._send(syncBuffer)
187
178
}
188
- return this.stream.write(endBuffer, () => {
189
- this.stream.end()
190
- })
191
- }
192
179
193
- Connection.prototype.close = function (msg) {
194
- this._send(serialize.close(msg))
195
- }
180
+ end() {
181
+ // 0x58 = 'X'
182
+ this._ending = true
183
+ if (!this._connecting || !this.stream.writable) {
184
+ this.stream.end()
185
+ return
186
+ }
187
+ return this.stream.write(endBuffer, () => {
188
+ this.stream.end()
189
+ })
190
+ }
196
191
197
- Connection.prototype.describe = function (msg) {
198
- this._send(serialize.describe (msg))
199
- }
192
+ close (msg) {
193
+ this._send(serialize.close (msg))
194
+ }
200
195
201
- Connection.prototype.sendCopyFromChunk = function (chunk ) {
202
- this._send(serialize.copyData(chunk ))
203
- }
196
+ describe(msg ) {
197
+ this._send(serialize.describe(msg ))
198
+ }
204
199
205
- Connection.prototype.endCopyFrom = function ( ) {
206
- this._send(serialize.copyDone( ))
207
- }
200
+ sendCopyFromChunk(chunk ) {
201
+ this._send(serialize.copyData(chunk ))
202
+ }
208
203
209
- Connection.prototype.sendCopyFail = function (msg) {
210
- this._send(serialize.copyFail(msg))
204
+ endCopyFrom() {
205
+ this._send(serialize.copyDone())
206
+ }
207
+
208
+ sendCopyFail(msg) {
209
+ this._send(serialize.copyFail(msg))
210
+ }
211
211
}
212
212
213
213
module.exports = Connection
0 commit comments