pg 8.0.2 → 8.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,215 +0,0 @@
1
- 'use strict'
2
- /**
3
- * Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
4
- * All rights reserved.
5
- *
6
- * This source code is licensed under the MIT license found in the
7
- * README.md file in the root directory of this source tree.
8
- */
9
-
10
- var net = require('net')
11
- var EventEmitter = require('events').EventEmitter
12
- var util = require('util')
13
-
14
- // eslint-disable-next-line
15
- const { parse, serialize } = require('../../pg-protocol/dist')
16
-
17
- // TODO(bmc) support binary mode here
18
- // var BINARY_MODE = 1
19
- console.log('***using faster connection***')
20
- var Connection = function (config) {
21
- EventEmitter.call(this)
22
- config = config || {}
23
- this.stream = config.stream || new net.Socket()
24
- this.stream.setNoDelay(true)
25
- this._keepAlive = config.keepAlive
26
- this._keepAliveInitialDelayMillis = config.keepAliveInitialDelayMillis
27
- this.lastBuffer = false
28
- this.parsedStatements = {}
29
- this.ssl = config.ssl || false
30
- this._ending = false
31
- this._emitMessage = false
32
- var self = this
33
- this.on('newListener', function (eventName) {
34
- if (eventName === 'message') {
35
- self._emitMessage = true
36
- }
37
- })
38
- }
39
-
40
- util.inherits(Connection, EventEmitter)
41
-
42
- Connection.prototype.connect = function (port, host) {
43
- var self = this
44
-
45
- if (this.stream.readyState === 'closed') {
46
- this.stream.connect(port, host)
47
- } else if (this.stream.readyState === 'open') {
48
- this.emit('connect')
49
- }
50
-
51
- this.stream.on('connect', function () {
52
- if (self._keepAlive) {
53
- self.stream.setKeepAlive(true, self._keepAliveInitialDelayMillis)
54
- }
55
- self.emit('connect')
56
- })
57
-
58
- const reportStreamError = function (error) {
59
- // errors about disconnections should be ignored during disconnect
60
- if (self._ending && (error.code === 'ECONNRESET' || error.code === 'EPIPE')) {
61
- return
62
- }
63
- self.emit('error', error)
64
- }
65
- this.stream.on('error', reportStreamError)
66
-
67
- this.stream.on('close', function () {
68
- self.emit('end')
69
- })
70
-
71
- if (!this.ssl) {
72
- return this.attachListeners(this.stream)
73
- }
74
-
75
- this.stream.once('data', function (buffer) {
76
- var responseCode = buffer.toString('utf8')
77
- switch (responseCode) {
78
- case 'S': // Server supports SSL connections, continue with a secure connection
79
- break
80
- case 'N': // Server does not support SSL connections
81
- self.stream.end()
82
- return self.emit('error', new Error('The server does not support SSL connections'))
83
- default: // Any other response byte, including 'E' (ErrorResponse) indicating a server error
84
- self.stream.end()
85
- return self.emit('error', new Error('There was an error establishing an SSL connection'))
86
- }
87
- var tls = require('tls')
88
- const options = Object.assign({
89
- socket: self.stream
90
- }, self.ssl)
91
- if (net.isIP(host) === 0) {
92
- options.servername = host
93
- }
94
- self.stream = tls.connect(options)
95
- self.attachListeners(self.stream)
96
- self.stream.on('error', reportStreamError)
97
-
98
- self.emit('sslconnect')
99
- })
100
- }
101
-
102
- Connection.prototype.attachListeners = function (stream) {
103
- stream.on('end', () => {
104
- this.emit('end')
105
- })
106
- parse(stream, (msg) => {
107
- var eventName = msg.name === 'error' ? 'errorMessage' : msg.name
108
- if (this._emitMessage) {
109
- this.emit('message', msg)
110
- }
111
- this.emit(eventName, msg)
112
- })
113
- }
114
-
115
- Connection.prototype.requestSsl = function () {
116
- this.stream.write(serialize.requestSsl())
117
- }
118
-
119
- Connection.prototype.startup = function (config) {
120
- this.stream.write(serialize.startup(config))
121
- }
122
-
123
- Connection.prototype.cancel = function (processID, secretKey) {
124
- this._send(serialize.cancel(processID, secretKey))
125
- }
126
-
127
- Connection.prototype.password = function (password) {
128
- this._send(serialize.password(password))
129
- }
130
-
131
- Connection.prototype.sendSASLInitialResponseMessage = function (mechanism, initialResponse) {
132
- this._send(serialize.sendSASLInitialResponseMessage(mechanism, initialResponse))
133
- }
134
-
135
- Connection.prototype.sendSCRAMClientFinalMessage = function (additionalData) {
136
- this._send(serialize.sendSCRAMClientFinalMessage(additionalData))
137
- }
138
-
139
- Connection.prototype._send = function (buffer) {
140
- if (!this.stream.writable) {
141
- return false
142
- }
143
- return this.stream.write(buffer)
144
- }
145
-
146
- Connection.prototype.query = function (text) {
147
- this._send(serialize.query(text))
148
- }
149
-
150
- // send parse message
151
- Connection.prototype.parse = function (query) {
152
- this._send(serialize.parse(query))
153
- }
154
-
155
- // send bind message
156
- // "more" === true to buffer the message until flush() is called
157
- Connection.prototype.bind = function (config) {
158
- this._send(serialize.bind(config))
159
- }
160
-
161
- // send execute message
162
- // "more" === true to buffer the message until flush() is called
163
- Connection.prototype.execute = function (config) {
164
- this._send(serialize.execute(config))
165
- }
166
-
167
- const flushBuffer = serialize.flush()
168
- Connection.prototype.flush = function () {
169
- if (this.stream.writable) {
170
- this.stream.write(flushBuffer)
171
- }
172
- }
173
-
174
- const syncBuffer = serialize.sync()
175
- Connection.prototype.sync = function () {
176
- this._ending = true
177
- this._send(syncBuffer)
178
- this._send(flushBuffer)
179
- }
180
-
181
- const endBuffer = serialize.end()
182
-
183
- Connection.prototype.end = function () {
184
- // 0x58 = 'X'
185
- this._ending = true
186
- if (!this.stream.writable) {
187
- this.stream.end()
188
- return
189
- }
190
- return this.stream.write(endBuffer, () => {
191
- this.stream.end()
192
- })
193
- }
194
-
195
- Connection.prototype.close = function (msg) {
196
- this._send(serialize.close(msg))
197
- }
198
-
199
- Connection.prototype.describe = function (msg) {
200
- this._send(serialize.describe(msg))
201
- }
202
-
203
- Connection.prototype.sendCopyFromChunk = function (chunk) {
204
- this._send(serialize.copyData(chunk))
205
- }
206
-
207
- Connection.prototype.endCopyFrom = function () {
208
- this._send(serialize.copyDone())
209
- }
210
-
211
- Connection.prototype.sendCopyFail = function (msg) {
212
- this._send(serialize.copyFail(msg))
213
- }
214
-
215
- module.exports = Connection