Einkaufslisten anzeigen, Datenbankeinbindung
This commit is contained in:
468
express-server/node_modules/pg/lib/client.js
generated
vendored
Normal file
468
express-server/node_modules/pg/lib/client.js
generated
vendored
Normal file
@ -0,0 +1,468 @@
|
||||
'use strict'
|
||||
/**
|
||||
* Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* README.md file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
var EventEmitter = require('events').EventEmitter
|
||||
var util = require('util')
|
||||
var utils = require('./utils')
|
||||
var pgPass = require('pgpass')
|
||||
var TypeOverrides = require('./type-overrides')
|
||||
|
||||
var ConnectionParameters = require('./connection-parameters')
|
||||
var Query = require('./query')
|
||||
var defaults = require('./defaults')
|
||||
var Connection = require('./connection')
|
||||
|
||||
var Client = function (config) {
|
||||
EventEmitter.call(this)
|
||||
|
||||
this.connectionParameters = new ConnectionParameters(config)
|
||||
this.user = this.connectionParameters.user
|
||||
this.database = this.connectionParameters.database
|
||||
this.port = this.connectionParameters.port
|
||||
this.host = this.connectionParameters.host
|
||||
this.password = this.connectionParameters.password
|
||||
this.replication = this.connectionParameters.replication
|
||||
|
||||
var c = config || {}
|
||||
|
||||
this._Promise = c.Promise || global.Promise
|
||||
this._types = new TypeOverrides(c.types)
|
||||
this._ending = false
|
||||
this._connecting = false
|
||||
this._connected = false
|
||||
this._connectionError = false
|
||||
this._queryable = true
|
||||
|
||||
this.connection = c.connection || new Connection({
|
||||
stream: c.stream,
|
||||
ssl: this.connectionParameters.ssl,
|
||||
keepAlive: c.keepAlive || false,
|
||||
encoding: this.connectionParameters.client_encoding || 'utf8'
|
||||
})
|
||||
this.queryQueue = []
|
||||
this.binary = c.binary || defaults.binary
|
||||
this.processID = null
|
||||
this.secretKey = null
|
||||
this.ssl = this.connectionParameters.ssl || false
|
||||
}
|
||||
|
||||
util.inherits(Client, EventEmitter)
|
||||
|
||||
Client.prototype._errorAllQueries = function (err) {
|
||||
const enqueueError = (query) => {
|
||||
process.nextTick(() => {
|
||||
query.handleError(err, this.connection)
|
||||
})
|
||||
}
|
||||
|
||||
if (this.activeQuery) {
|
||||
enqueueError(this.activeQuery)
|
||||
this.activeQuery = null
|
||||
}
|
||||
|
||||
this.queryQueue.forEach(enqueueError)
|
||||
this.queryQueue.length = 0
|
||||
}
|
||||
|
||||
Client.prototype._connect = function (callback) {
|
||||
var self = this
|
||||
var con = this.connection
|
||||
if (this._connecting || this._connected) {
|
||||
const err = new Error('Client has already been connected. You cannot reuse a client.')
|
||||
process.nextTick(() => {
|
||||
callback(err)
|
||||
})
|
||||
return
|
||||
}
|
||||
this._connecting = true
|
||||
|
||||
if (this.host && this.host.indexOf('/') === 0) {
|
||||
con.connect(this.host + '/.s.PGSQL.' + this.port)
|
||||
} else {
|
||||
con.connect(this.port, this.host)
|
||||
}
|
||||
|
||||
// once connection is established send startup message
|
||||
con.on('connect', function () {
|
||||
if (self.ssl) {
|
||||
con.requestSsl()
|
||||
} else {
|
||||
con.startup(self.getStartupConf())
|
||||
}
|
||||
})
|
||||
|
||||
con.on('sslconnect', function () {
|
||||
con.startup(self.getStartupConf())
|
||||
})
|
||||
|
||||
function checkPgPass (cb) {
|
||||
return function (msg) {
|
||||
if (self.password !== null) {
|
||||
cb(msg)
|
||||
} else {
|
||||
pgPass(self.connectionParameters, function (pass) {
|
||||
if (undefined !== pass) {
|
||||
self.connectionParameters.password = self.password = pass
|
||||
}
|
||||
cb(msg)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// password request handling
|
||||
con.on('authenticationCleartextPassword', checkPgPass(function () {
|
||||
con.password(self.password)
|
||||
}))
|
||||
|
||||
// password request handling
|
||||
con.on('authenticationMD5Password', checkPgPass(function (msg) {
|
||||
con.password(utils.postgresMd5PasswordHash(self.user, self.password, msg.salt))
|
||||
}))
|
||||
|
||||
con.once('backendKeyData', function (msg) {
|
||||
self.processID = msg.processID
|
||||
self.secretKey = msg.secretKey
|
||||
})
|
||||
|
||||
const connectingErrorHandler = (err) => {
|
||||
if (this._connectionError) {
|
||||
return
|
||||
}
|
||||
this._connectionError = true
|
||||
if (callback) {
|
||||
return callback(err)
|
||||
}
|
||||
this.emit('error', err)
|
||||
}
|
||||
|
||||
const connectedErrorHandler = (err) => {
|
||||
this._queryable = false
|
||||
this._errorAllQueries(err)
|
||||
this.emit('error', err)
|
||||
}
|
||||
|
||||
const connectedErrorMessageHandler = (msg) => {
|
||||
const activeQuery = this.activeQuery
|
||||
|
||||
if (!activeQuery) {
|
||||
connectedErrorHandler(msg)
|
||||
return
|
||||
}
|
||||
|
||||
this.activeQuery = null
|
||||
activeQuery.handleError(msg, con)
|
||||
}
|
||||
|
||||
con.on('error', connectingErrorHandler)
|
||||
con.on('errorMessage', connectingErrorHandler)
|
||||
|
||||
// hook up query handling events to connection
|
||||
// after the connection initially becomes ready for queries
|
||||
con.once('readyForQuery', function () {
|
||||
self._connecting = false
|
||||
self._connected = true
|
||||
self._attachListeners(con)
|
||||
con.removeListener('error', connectingErrorHandler)
|
||||
con.removeListener('errorMessage', connectingErrorHandler)
|
||||
con.on('error', connectedErrorHandler)
|
||||
con.on('errorMessage', connectedErrorMessageHandler)
|
||||
|
||||
// process possible callback argument to Client#connect
|
||||
if (callback) {
|
||||
callback(null, self)
|
||||
// remove callback for proper error handling
|
||||
// after the connect event
|
||||
callback = null
|
||||
}
|
||||
self.emit('connect')
|
||||
})
|
||||
|
||||
con.on('readyForQuery', function () {
|
||||
var activeQuery = self.activeQuery
|
||||
self.activeQuery = null
|
||||
self.readyForQuery = true
|
||||
if (activeQuery) {
|
||||
activeQuery.handleReadyForQuery(con)
|
||||
}
|
||||
self._pulseQueryQueue()
|
||||
})
|
||||
|
||||
con.once('end', () => {
|
||||
const error = this._ending
|
||||
? new Error('Connection terminated')
|
||||
: new Error('Connection terminated unexpectedly')
|
||||
|
||||
this._errorAllQueries(error)
|
||||
|
||||
if (!this._ending) {
|
||||
// if the connection is ended without us calling .end()
|
||||
// on this client then we have an unexpected disconnection
|
||||
// treat this as an error unless we've already emitted an error
|
||||
// during connection.
|
||||
if (this._connecting && !this._connectionError) {
|
||||
if (callback) {
|
||||
callback(error)
|
||||
} else {
|
||||
connectedErrorHandler(error)
|
||||
}
|
||||
} else if (!this._connectionError) {
|
||||
connectedErrorHandler(error)
|
||||
}
|
||||
}
|
||||
|
||||
process.nextTick(() => {
|
||||
this.emit('end')
|
||||
})
|
||||
})
|
||||
|
||||
con.on('notice', function (msg) {
|
||||
self.emit('notice', msg)
|
||||
})
|
||||
}
|
||||
|
||||
Client.prototype.connect = function (callback) {
|
||||
if (callback) {
|
||||
this._connect(callback)
|
||||
return
|
||||
}
|
||||
|
||||
return new this._Promise((resolve, reject) => {
|
||||
this._connect((error) => {
|
||||
if (error) {
|
||||
reject(error)
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Client.prototype._attachListeners = function (con) {
|
||||
const self = this
|
||||
// delegate rowDescription to active query
|
||||
con.on('rowDescription', function (msg) {
|
||||
self.activeQuery.handleRowDescription(msg)
|
||||
})
|
||||
|
||||
// delegate dataRow to active query
|
||||
con.on('dataRow', function (msg) {
|
||||
self.activeQuery.handleDataRow(msg)
|
||||
})
|
||||
|
||||
// delegate portalSuspended to active query
|
||||
con.on('portalSuspended', function (msg) {
|
||||
self.activeQuery.handlePortalSuspended(con)
|
||||
})
|
||||
|
||||
// deletagate emptyQuery to active query
|
||||
con.on('emptyQuery', function (msg) {
|
||||
self.activeQuery.handleEmptyQuery(con)
|
||||
})
|
||||
|
||||
// delegate commandComplete to active query
|
||||
con.on('commandComplete', function (msg) {
|
||||
self.activeQuery.handleCommandComplete(msg, con)
|
||||
})
|
||||
|
||||
// if a prepared statement has a name and properly parses
|
||||
// we track that its already been executed so we don't parse
|
||||
// it again on the same client
|
||||
con.on('parseComplete', function (msg) {
|
||||
if (self.activeQuery.name) {
|
||||
con.parsedStatements[self.activeQuery.name] = true
|
||||
}
|
||||
})
|
||||
|
||||
con.on('copyInResponse', function (msg) {
|
||||
self.activeQuery.handleCopyInResponse(self.connection)
|
||||
})
|
||||
|
||||
con.on('copyData', function (msg) {
|
||||
self.activeQuery.handleCopyData(msg, self.connection)
|
||||
})
|
||||
|
||||
con.on('notification', function (msg) {
|
||||
self.emit('notification', msg)
|
||||
})
|
||||
}
|
||||
|
||||
Client.prototype.getStartupConf = function () {
|
||||
var params = this.connectionParameters
|
||||
|
||||
var data = {
|
||||
user: params.user,
|
||||
database: params.database
|
||||
}
|
||||
|
||||
var appName = params.application_name || params.fallback_application_name
|
||||
if (appName) {
|
||||
data.application_name = appName
|
||||
}
|
||||
if (params.replication) {
|
||||
data.replication = '' + params.replication
|
||||
}
|
||||
if (params.statement_timeout) {
|
||||
data.statement_timeout = String(parseInt(params.statement_timeout, 10))
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
Client.prototype.cancel = function (client, query) {
|
||||
if (client.activeQuery === query) {
|
||||
var con = this.connection
|
||||
|
||||
if (this.host && this.host.indexOf('/') === 0) {
|
||||
con.connect(this.host + '/.s.PGSQL.' + this.port)
|
||||
} else {
|
||||
con.connect(this.port, this.host)
|
||||
}
|
||||
|
||||
// once connection is established send cancel message
|
||||
con.on('connect', function () {
|
||||
con.cancel(client.processID, client.secretKey)
|
||||
})
|
||||
} else if (client.queryQueue.indexOf(query) !== -1) {
|
||||
client.queryQueue.splice(client.queryQueue.indexOf(query), 1)
|
||||
}
|
||||
}
|
||||
|
||||
Client.prototype.setTypeParser = function (oid, format, parseFn) {
|
||||
return this._types.setTypeParser(oid, format, parseFn)
|
||||
}
|
||||
|
||||
Client.prototype.getTypeParser = function (oid, format) {
|
||||
return this._types.getTypeParser(oid, format)
|
||||
}
|
||||
|
||||
// Ported from PostgreSQL 9.2.4 source code in src/interfaces/libpq/fe-exec.c
|
||||
Client.prototype.escapeIdentifier = function (str) {
|
||||
return '"' + str.replace(/"/g, '""') + '"'
|
||||
}
|
||||
|
||||
// Ported from PostgreSQL 9.2.4 source code in src/interfaces/libpq/fe-exec.c
|
||||
Client.prototype.escapeLiteral = function (str) {
|
||||
var hasBackslash = false
|
||||
var escaped = '\''
|
||||
|
||||
for (var i = 0; i < str.length; i++) {
|
||||
var c = str[i]
|
||||
if (c === '\'') {
|
||||
escaped += c + c
|
||||
} else if (c === '\\') {
|
||||
escaped += c + c
|
||||
hasBackslash = true
|
||||
} else {
|
||||
escaped += c
|
||||
}
|
||||
}
|
||||
|
||||
escaped += '\''
|
||||
|
||||
if (hasBackslash === true) {
|
||||
escaped = ' E' + escaped
|
||||
}
|
||||
|
||||
return escaped
|
||||
}
|
||||
|
||||
Client.prototype._pulseQueryQueue = function () {
|
||||
if (this.readyForQuery === true) {
|
||||
this.activeQuery = this.queryQueue.shift()
|
||||
if (this.activeQuery) {
|
||||
this.readyForQuery = false
|
||||
this.hasExecuted = true
|
||||
|
||||
const queryError = this.activeQuery.submit(this.connection)
|
||||
if (queryError) {
|
||||
process.nextTick(() => {
|
||||
this.activeQuery.handleError(queryError, this.connection)
|
||||
this.readyForQuery = true
|
||||
this._pulseQueryQueue()
|
||||
})
|
||||
}
|
||||
} else if (this.hasExecuted) {
|
||||
this.activeQuery = null
|
||||
this.emit('drain')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Client.prototype.query = function (config, values, callback) {
|
||||
// can take in strings, config object or query object
|
||||
var query
|
||||
var result
|
||||
|
||||
if (config === null || config === undefined) {
|
||||
throw new TypeError('Client was passed a null or undefined query')
|
||||
} else if (typeof config.submit === 'function') {
|
||||
result = query = config
|
||||
if (typeof values === 'function') {
|
||||
query.callback = query.callback || values
|
||||
}
|
||||
} else {
|
||||
query = new Query(config, values, callback)
|
||||
if (!query.callback) {
|
||||
result = new this._Promise((resolve, reject) => {
|
||||
query.callback = (err, res) => err ? reject(err) : resolve(res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if (this.binary && !query.binary) {
|
||||
query.binary = true
|
||||
}
|
||||
if (query._result) {
|
||||
query._result._getTypeParser = this._types.getTypeParser.bind(this._types)
|
||||
}
|
||||
|
||||
if (!this._queryable) {
|
||||
process.nextTick(() => {
|
||||
query.handleError(new Error('Client has encountered a connection error and is not queryable'), this.connection)
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
||||
if (this._ending) {
|
||||
process.nextTick(() => {
|
||||
query.handleError(new Error('Client was closed and is not queryable'), this.connection)
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
||||
this.queryQueue.push(query)
|
||||
this._pulseQueryQueue()
|
||||
return result
|
||||
}
|
||||
|
||||
Client.prototype.end = function (cb) {
|
||||
this._ending = true
|
||||
|
||||
if (this.activeQuery) {
|
||||
// if we have an active query we need to force a disconnect
|
||||
// on the socket - otherwise a hung query could block end forever
|
||||
this.connection.stream.destroy()
|
||||
} else {
|
||||
this.connection.end()
|
||||
}
|
||||
|
||||
if (cb) {
|
||||
this.connection.once('end', cb)
|
||||
} else {
|
||||
return new this._Promise((resolve) => {
|
||||
this.connection.once('end', resolve)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// expose a Query constructor
|
||||
Client.Query = Query
|
||||
|
||||
module.exports = Client
|
119
express-server/node_modules/pg/lib/connection-parameters.js
generated
vendored
Normal file
119
express-server/node_modules/pg/lib/connection-parameters.js
generated
vendored
Normal file
@ -0,0 +1,119 @@
|
||||
'use strict'
|
||||
/**
|
||||
* Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* README.md file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
var dns = require('dns')
|
||||
|
||||
var defaults = require('./defaults')
|
||||
|
||||
var parse = require('pg-connection-string').parse // parses a connection string
|
||||
|
||||
var val = function (key, config, envVar) {
|
||||
if (envVar === undefined) {
|
||||
envVar = process.env[ 'PG' + key.toUpperCase() ]
|
||||
} else if (envVar === false) {
|
||||
// do nothing ... use false
|
||||
} else {
|
||||
envVar = process.env[ envVar ]
|
||||
}
|
||||
|
||||
return config[key] ||
|
||||
envVar ||
|
||||
defaults[key]
|
||||
}
|
||||
|
||||
var useSsl = function () {
|
||||
switch (process.env.PGSSLMODE) {
|
||||
case 'disable':
|
||||
return false
|
||||
case 'prefer':
|
||||
case 'require':
|
||||
case 'verify-ca':
|
||||
case 'verify-full':
|
||||
return true
|
||||
}
|
||||
return defaults.ssl
|
||||
}
|
||||
|
||||
var ConnectionParameters = function (config) {
|
||||
// if a string is passed, it is a raw connection string so we parse it into a config
|
||||
config = typeof config === 'string' ? parse(config) : config || {}
|
||||
|
||||
// if the config has a connectionString defined, parse IT into the config we use
|
||||
// this will override other default values with what is stored in connectionString
|
||||
if (config.connectionString) {
|
||||
config = Object.assign({}, config, parse(config.connectionString))
|
||||
}
|
||||
|
||||
this.user = val('user', config)
|
||||
this.database = val('database', config)
|
||||
this.port = parseInt(val('port', config), 10)
|
||||
this.host = val('host', config)
|
||||
this.password = val('password', config)
|
||||
this.binary = val('binary', config)
|
||||
this.ssl = typeof config.ssl === 'undefined' ? useSsl() : config.ssl
|
||||
this.client_encoding = val('client_encoding', config)
|
||||
this.replication = val('replication', config)
|
||||
// a domain socket begins with '/'
|
||||
this.isDomainSocket = (!(this.host || '').indexOf('/'))
|
||||
|
||||
this.application_name = val('application_name', config, 'PGAPPNAME')
|
||||
this.fallback_application_name = val('fallback_application_name', config, false)
|
||||
this.statement_timeout = val('statement_timeout', config, false)
|
||||
}
|
||||
|
||||
// Convert arg to a string, surround in single quotes, and escape single quotes and backslashes
|
||||
var quoteParamValue = function (value) {
|
||||
return "'" + ('' + value).replace(/\\/g, '\\\\').replace(/'/g, "\\'") + "'"
|
||||
}
|
||||
|
||||
var add = function (params, config, paramName) {
|
||||
var value = config[paramName]
|
||||
if (value) {
|
||||
params.push(paramName + '=' + quoteParamValue(value))
|
||||
}
|
||||
}
|
||||
|
||||
ConnectionParameters.prototype.getLibpqConnectionString = function (cb) {
|
||||
var params = []
|
||||
add(params, this, 'user')
|
||||
add(params, this, 'password')
|
||||
add(params, this, 'port')
|
||||
add(params, this, 'application_name')
|
||||
add(params, this, 'fallback_application_name')
|
||||
|
||||
var ssl = typeof this.ssl === 'object' ? this.ssl : { sslmode: this.ssl }
|
||||
add(params, ssl, 'sslmode')
|
||||
add(params, ssl, 'sslca')
|
||||
add(params, ssl, 'sslkey')
|
||||
add(params, ssl, 'sslcert')
|
||||
add(params, ssl, 'sslrootcert')
|
||||
|
||||
if (this.database) {
|
||||
params.push('dbname=' + quoteParamValue(this.database))
|
||||
}
|
||||
if (this.replication) {
|
||||
params.push('replication=' + quoteParamValue(this.replication))
|
||||
}
|
||||
if (this.host) {
|
||||
params.push('host=' + quoteParamValue(this.host))
|
||||
}
|
||||
if (this.isDomainSocket) {
|
||||
return cb(null, params.join(' '))
|
||||
}
|
||||
if (this.client_encoding) {
|
||||
params.push('client_encoding=' + quoteParamValue(this.client_encoding))
|
||||
}
|
||||
dns.lookup(this.host, function (err, address) {
|
||||
if (err) return cb(err, null)
|
||||
params.push('hostaddr=' + quoteParamValue(address))
|
||||
return cb(null, params.join(' '))
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = ConnectionParameters
|
655
express-server/node_modules/pg/lib/connection.js
generated
vendored
Normal file
655
express-server/node_modules/pg/lib/connection.js
generated
vendored
Normal file
@ -0,0 +1,655 @@
|
||||
'use strict'
|
||||
/**
|
||||
* Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* README.md file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
var net = require('net')
|
||||
var EventEmitter = require('events').EventEmitter
|
||||
var util = require('util')
|
||||
|
||||
var Writer = require('buffer-writer')
|
||||
var Reader = require('packet-reader')
|
||||
|
||||
var TEXT_MODE = 0
|
||||
var BINARY_MODE = 1
|
||||
var Connection = function (config) {
|
||||
EventEmitter.call(this)
|
||||
config = config || {}
|
||||
this.stream = config.stream || new net.Socket()
|
||||
this._keepAlive = config.keepAlive
|
||||
this.lastBuffer = false
|
||||
this.lastOffset = 0
|
||||
this.buffer = null
|
||||
this.offset = null
|
||||
this.encoding = config.encoding || 'utf8'
|
||||
this.parsedStatements = {}
|
||||
this.writer = new Writer()
|
||||
this.ssl = config.ssl || false
|
||||
this._ending = false
|
||||
this._mode = TEXT_MODE
|
||||
this._emitMessage = false
|
||||
this._reader = new Reader({
|
||||
headerSize: 1,
|
||||
lengthPadding: -4
|
||||
})
|
||||
var self = this
|
||||
this.on('newListener', function (eventName) {
|
||||
if (eventName === 'message') {
|
||||
self._emitMessage = true
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
util.inherits(Connection, EventEmitter)
|
||||
|
||||
Connection.prototype.connect = function (port, host) {
|
||||
if (this.stream.readyState === 'closed') {
|
||||
this.stream.connect(port, host)
|
||||
} else if (this.stream.readyState === 'open') {
|
||||
this.emit('connect')
|
||||
}
|
||||
|
||||
var self = this
|
||||
|
||||
this.stream.on('connect', function () {
|
||||
if (self._keepAlive) {
|
||||
self.stream.setKeepAlive(true)
|
||||
}
|
||||
self.emit('connect')
|
||||
})
|
||||
|
||||
const reportStreamError = function (error) {
|
||||
// don't raise ECONNRESET errors - they can & should be ignored
|
||||
// during disconnect
|
||||
if (self._ending && error.code === 'ECONNRESET') {
|
||||
return
|
||||
}
|
||||
self.emit('error', error)
|
||||
}
|
||||
this.stream.on('error', reportStreamError)
|
||||
|
||||
this.stream.on('close', function () {
|
||||
self.emit('end')
|
||||
})
|
||||
|
||||
if (!this.ssl) {
|
||||
return this.attachListeners(this.stream)
|
||||
}
|
||||
|
||||
this.stream.once('data', function (buffer) {
|
||||
var responseCode = buffer.toString('utf8')
|
||||
switch (responseCode) {
|
||||
case 'N': // Server does not support SSL connections
|
||||
return self.emit('error', new Error('The server does not support SSL connections'))
|
||||
case 'S': // Server supports SSL connections, continue with a secure connection
|
||||
break
|
||||
default: // Any other response byte, including 'E' (ErrorResponse) indicating a server error
|
||||
return self.emit('error', new Error('There was an error establishing an SSL connection'))
|
||||
}
|
||||
var tls = require('tls')
|
||||
self.stream = tls.connect({
|
||||
socket: self.stream,
|
||||
servername: host,
|
||||
checkServerIdentity: self.ssl.checkServerIdentity || tls.checkServerIdentity,
|
||||
rejectUnauthorized: self.ssl.rejectUnauthorized,
|
||||
ca: self.ssl.ca,
|
||||
pfx: self.ssl.pfx,
|
||||
key: self.ssl.key,
|
||||
passphrase: self.ssl.passphrase,
|
||||
cert: self.ssl.cert,
|
||||
NPNProtocols: self.ssl.NPNProtocols
|
||||
})
|
||||
self.attachListeners(self.stream)
|
||||
self.stream.on('error', reportStreamError)
|
||||
|
||||
self.emit('sslconnect')
|
||||
})
|
||||
}
|
||||
|
||||
Connection.prototype.attachListeners = function (stream) {
|
||||
var self = this
|
||||
stream.on('data', function (buff) {
|
||||
self._reader.addChunk(buff)
|
||||
var packet = self._reader.read()
|
||||
while (packet) {
|
||||
var msg = self.parseMessage(packet)
|
||||
var eventName = msg.name === 'error' ? 'errorMessage' : msg.name
|
||||
if (self._emitMessage) {
|
||||
self.emit('message', msg)
|
||||
}
|
||||
self.emit(eventName, msg)
|
||||
packet = self._reader.read()
|
||||
}
|
||||
})
|
||||
stream.on('end', function () {
|
||||
self.emit('end')
|
||||
})
|
||||
}
|
||||
|
||||
Connection.prototype.requestSsl = function () {
|
||||
var bodyBuffer = this.writer
|
||||
.addInt16(0x04D2)
|
||||
.addInt16(0x162F).flush()
|
||||
|
||||
var length = bodyBuffer.length + 4
|
||||
|
||||
var buffer = new Writer()
|
||||
.addInt32(length)
|
||||
.add(bodyBuffer)
|
||||
.join()
|
||||
this.stream.write(buffer)
|
||||
}
|
||||
|
||||
Connection.prototype.startup = function (config) {
|
||||
var writer = this.writer
|
||||
.addInt16(3)
|
||||
.addInt16(0)
|
||||
|
||||
Object.keys(config).forEach(function (key) {
|
||||
var val = config[key]
|
||||
writer.addCString(key).addCString(val)
|
||||
})
|
||||
|
||||
writer.addCString('client_encoding').addCString("'utf-8'")
|
||||
|
||||
var bodyBuffer = writer.addCString('').flush()
|
||||
// this message is sent without a code
|
||||
|
||||
var length = bodyBuffer.length + 4
|
||||
|
||||
var buffer = new Writer()
|
||||
.addInt32(length)
|
||||
.add(bodyBuffer)
|
||||
.join()
|
||||
this.stream.write(buffer)
|
||||
}
|
||||
|
||||
Connection.prototype.cancel = function (processID, secretKey) {
|
||||
var bodyBuffer = this.writer
|
||||
.addInt16(1234)
|
||||
.addInt16(5678)
|
||||
.addInt32(processID)
|
||||
.addInt32(secretKey)
|
||||
.flush()
|
||||
|
||||
var length = bodyBuffer.length + 4
|
||||
|
||||
var buffer = new Writer()
|
||||
.addInt32(length)
|
||||
.add(bodyBuffer)
|
||||
.join()
|
||||
this.stream.write(buffer)
|
||||
}
|
||||
|
||||
Connection.prototype.password = function (password) {
|
||||
// 0x70 = 'p'
|
||||
this._send(0x70, this.writer.addCString(password))
|
||||
}
|
||||
|
||||
Connection.prototype._send = function (code, more) {
|
||||
if (!this.stream.writable) {
|
||||
return false
|
||||
}
|
||||
if (more === true) {
|
||||
this.writer.addHeader(code)
|
||||
} else {
|
||||
return this.stream.write(this.writer.flush(code))
|
||||
}
|
||||
}
|
||||
|
||||
Connection.prototype.query = function (text) {
|
||||
// 0x51 = Q
|
||||
this.stream.write(this.writer.addCString(text).flush(0x51))
|
||||
}
|
||||
|
||||
// send parse message
|
||||
// "more" === true to buffer the message until flush() is called
|
||||
Connection.prototype.parse = function (query, more) {
|
||||
// expect something like this:
|
||||
// { name: 'queryName',
|
||||
// text: 'select * from blah',
|
||||
// types: ['int8', 'bool'] }
|
||||
|
||||
// normalize missing query names to allow for null
|
||||
query.name = query.name || ''
|
||||
if (query.name.length > 63) {
|
||||
console.error('Warning! Postgres only supports 63 characters for query names.')
|
||||
console.error('You supplied', query.name, '(', query.name.length, ')')
|
||||
console.error('This can cause conflicts and silent errors executing queries')
|
||||
}
|
||||
// normalize null type array
|
||||
query.types = query.types || []
|
||||
var len = query.types.length
|
||||
var buffer = this.writer
|
||||
.addCString(query.name) // name of query
|
||||
.addCString(query.text) // actual query text
|
||||
.addInt16(len)
|
||||
for (var i = 0; i < len; i++) {
|
||||
buffer.addInt32(query.types[i])
|
||||
}
|
||||
|
||||
var code = 0x50
|
||||
this._send(code, more)
|
||||
}
|
||||
|
||||
// send bind message
|
||||
// "more" === true to buffer the message until flush() is called
|
||||
Connection.prototype.bind = function (config, more) {
|
||||
// normalize config
|
||||
config = config || {}
|
||||
config.portal = config.portal || ''
|
||||
config.statement = config.statement || ''
|
||||
config.binary = config.binary || false
|
||||
var values = config.values || []
|
||||
var len = values.length
|
||||
var useBinary = false
|
||||
for (var j = 0; j < len; j++) { useBinary |= values[j] instanceof Buffer }
|
||||
var buffer = this.writer
|
||||
.addCString(config.portal)
|
||||
.addCString(config.statement)
|
||||
if (!useBinary) { buffer.addInt16(0) } else {
|
||||
buffer.addInt16(len)
|
||||
for (j = 0; j < len; j++) { buffer.addInt16(values[j] instanceof Buffer) }
|
||||
}
|
||||
buffer.addInt16(len)
|
||||
for (var i = 0; i < len; i++) {
|
||||
var val = values[i]
|
||||
if (val === null || typeof val === 'undefined') {
|
||||
buffer.addInt32(-1)
|
||||
} else if (val instanceof Buffer) {
|
||||
buffer.addInt32(val.length)
|
||||
buffer.add(val)
|
||||
} else {
|
||||
buffer.addInt32(Buffer.byteLength(val))
|
||||
buffer.addString(val)
|
||||
}
|
||||
}
|
||||
|
||||
if (config.binary) {
|
||||
buffer.addInt16(1) // format codes to use binary
|
||||
buffer.addInt16(1)
|
||||
} else {
|
||||
buffer.addInt16(0) // format codes to use text
|
||||
}
|
||||
// 0x42 = 'B'
|
||||
this._send(0x42, more)
|
||||
}
|
||||
|
||||
// send execute message
|
||||
// "more" === true to buffer the message until flush() is called
|
||||
Connection.prototype.execute = function (config, more) {
|
||||
config = config || {}
|
||||
config.portal = config.portal || ''
|
||||
config.rows = config.rows || ''
|
||||
this.writer
|
||||
.addCString(config.portal)
|
||||
.addInt32(config.rows)
|
||||
|
||||
// 0x45 = 'E'
|
||||
this._send(0x45, more)
|
||||
}
|
||||
|
||||
var emptyBuffer = Buffer.alloc(0)
|
||||
|
||||
Connection.prototype.flush = function () {
|
||||
// 0x48 = 'H'
|
||||
this.writer.add(emptyBuffer)
|
||||
this._send(0x48)
|
||||
}
|
||||
|
||||
Connection.prototype.sync = function () {
|
||||
// clear out any pending data in the writer
|
||||
this.writer.flush(0)
|
||||
|
||||
this.writer.add(emptyBuffer)
|
||||
this._ending = true
|
||||
this._send(0x53)
|
||||
}
|
||||
|
||||
const END_BUFFER = Buffer.from([0x58, 0x00, 0x00, 0x00, 0x04])
|
||||
|
||||
Connection.prototype.end = function () {
|
||||
// 0x58 = 'X'
|
||||
this.writer.add(emptyBuffer)
|
||||
this._ending = true
|
||||
return this.stream.write(END_BUFFER, () => {
|
||||
this.stream.end()
|
||||
})
|
||||
}
|
||||
|
||||
Connection.prototype.close = function (msg, more) {
|
||||
this.writer.addCString(msg.type + (msg.name || ''))
|
||||
this._send(0x43, more)
|
||||
}
|
||||
|
||||
Connection.prototype.describe = function (msg, more) {
|
||||
this.writer.addCString(msg.type + (msg.name || ''))
|
||||
this._send(0x44, more)
|
||||
}
|
||||
|
||||
Connection.prototype.sendCopyFromChunk = function (chunk) {
|
||||
this.stream.write(this.writer.add(chunk).flush(0x64))
|
||||
}
|
||||
|
||||
Connection.prototype.endCopyFrom = function () {
|
||||
this.stream.write(this.writer.add(emptyBuffer).flush(0x63))
|
||||
}
|
||||
|
||||
Connection.prototype.sendCopyFail = function (msg) {
|
||||
// this.stream.write(this.writer.add(emptyBuffer).flush(0x66));
|
||||
this.writer.addCString(msg)
|
||||
this._send(0x66)
|
||||
}
|
||||
|
||||
var Message = function (name, length) {
|
||||
this.name = name
|
||||
this.length = length
|
||||
}
|
||||
|
||||
Connection.prototype.parseMessage = function (buffer) {
|
||||
this.offset = 0
|
||||
var length = buffer.length + 4
|
||||
switch (this._reader.header) {
|
||||
case 0x52: // R
|
||||
return this.parseR(buffer, length)
|
||||
|
||||
case 0x53: // S
|
||||
return this.parseS(buffer, length)
|
||||
|
||||
case 0x4b: // K
|
||||
return this.parseK(buffer, length)
|
||||
|
||||
case 0x43: // C
|
||||
return this.parseC(buffer, length)
|
||||
|
||||
case 0x5a: // Z
|
||||
return this.parseZ(buffer, length)
|
||||
|
||||
case 0x54: // T
|
||||
return this.parseT(buffer, length)
|
||||
|
||||
case 0x44: // D
|
||||
return this.parseD(buffer, length)
|
||||
|
||||
case 0x45: // E
|
||||
return this.parseE(buffer, length)
|
||||
|
||||
case 0x4e: // N
|
||||
return this.parseN(buffer, length)
|
||||
|
||||
case 0x31: // 1
|
||||
return new Message('parseComplete', length)
|
||||
|
||||
case 0x32: // 2
|
||||
return new Message('bindComplete', length)
|
||||
|
||||
case 0x33: // 3
|
||||
return new Message('closeComplete', length)
|
||||
|
||||
case 0x41: // A
|
||||
return this.parseA(buffer, length)
|
||||
|
||||
case 0x6e: // n
|
||||
return new Message('noData', length)
|
||||
|
||||
case 0x49: // I
|
||||
return new Message('emptyQuery', length)
|
||||
|
||||
case 0x73: // s
|
||||
return new Message('portalSuspended', length)
|
||||
|
||||
case 0x47: // G
|
||||
return this.parseG(buffer, length)
|
||||
|
||||
case 0x48: // H
|
||||
return this.parseH(buffer, length)
|
||||
|
||||
case 0x57: // W
|
||||
return new Message('replicationStart', length)
|
||||
|
||||
case 0x63: // c
|
||||
return new Message('copyDone', length)
|
||||
|
||||
case 0x64: // d
|
||||
return this.parsed(buffer, length)
|
||||
}
|
||||
}
|
||||
|
||||
Connection.prototype.parseR = function (buffer, length) {
|
||||
var code = 0
|
||||
var msg = new Message('authenticationOk', length)
|
||||
if (msg.length === 8) {
|
||||
code = this.parseInt32(buffer)
|
||||
if (code === 3) {
|
||||
msg.name = 'authenticationCleartextPassword'
|
||||
}
|
||||
return msg
|
||||
}
|
||||
if (msg.length === 12) {
|
||||
code = this.parseInt32(buffer)
|
||||
if (code === 5) { // md5 required
|
||||
msg.name = 'authenticationMD5Password'
|
||||
msg.salt = Buffer.alloc(4)
|
||||
buffer.copy(msg.salt, 0, this.offset, this.offset + 4)
|
||||
this.offset += 4
|
||||
return msg
|
||||
}
|
||||
}
|
||||
throw new Error('Unknown authenticationOk message type' + util.inspect(msg))
|
||||
}
|
||||
|
||||
Connection.prototype.parseS = function (buffer, length) {
|
||||
var msg = new Message('parameterStatus', length)
|
||||
msg.parameterName = this.parseCString(buffer)
|
||||
msg.parameterValue = this.parseCString(buffer)
|
||||
return msg
|
||||
}
|
||||
|
||||
Connection.prototype.parseK = function (buffer, length) {
|
||||
var msg = new Message('backendKeyData', length)
|
||||
msg.processID = this.parseInt32(buffer)
|
||||
msg.secretKey = this.parseInt32(buffer)
|
||||
return msg
|
||||
}
|
||||
|
||||
Connection.prototype.parseC = function (buffer, length) {
|
||||
var msg = new Message('commandComplete', length)
|
||||
msg.text = this.parseCString(buffer)
|
||||
return msg
|
||||
}
|
||||
|
||||
Connection.prototype.parseZ = function (buffer, length) {
|
||||
var msg = new Message('readyForQuery', length)
|
||||
msg.name = 'readyForQuery'
|
||||
msg.status = this.readString(buffer, 1)
|
||||
return msg
|
||||
}
|
||||
|
||||
var ROW_DESCRIPTION = 'rowDescription'
|
||||
Connection.prototype.parseT = function (buffer, length) {
|
||||
var msg = new Message(ROW_DESCRIPTION, length)
|
||||
msg.fieldCount = this.parseInt16(buffer)
|
||||
var fields = []
|
||||
for (var i = 0; i < msg.fieldCount; i++) {
|
||||
fields.push(this.parseField(buffer))
|
||||
}
|
||||
msg.fields = fields
|
||||
return msg
|
||||
}
|
||||
|
||||
var Field = function () {
|
||||
this.name = null
|
||||
this.tableID = null
|
||||
this.columnID = null
|
||||
this.dataTypeID = null
|
||||
this.dataTypeSize = null
|
||||
this.dataTypeModifier = null
|
||||
this.format = null
|
||||
}
|
||||
|
||||
var FORMAT_TEXT = 'text'
|
||||
var FORMAT_BINARY = 'binary'
|
||||
Connection.prototype.parseField = function (buffer) {
|
||||
var field = new Field()
|
||||
field.name = this.parseCString(buffer)
|
||||
field.tableID = this.parseInt32(buffer)
|
||||
field.columnID = this.parseInt16(buffer)
|
||||
field.dataTypeID = this.parseInt32(buffer)
|
||||
field.dataTypeSize = this.parseInt16(buffer)
|
||||
field.dataTypeModifier = this.parseInt32(buffer)
|
||||
if (this.parseInt16(buffer) === TEXT_MODE) {
|
||||
this._mode = TEXT_MODE
|
||||
field.format = FORMAT_TEXT
|
||||
} else {
|
||||
this._mode = BINARY_MODE
|
||||
field.format = FORMAT_BINARY
|
||||
}
|
||||
return field
|
||||
}
|
||||
|
||||
var DATA_ROW = 'dataRow'
|
||||
var DataRowMessage = function (length, fieldCount) {
|
||||
this.name = DATA_ROW
|
||||
this.length = length
|
||||
this.fieldCount = fieldCount
|
||||
this.fields = []
|
||||
}
|
||||
|
||||
// extremely hot-path code
|
||||
Connection.prototype.parseD = function (buffer, length) {
|
||||
var fieldCount = this.parseInt16(buffer)
|
||||
var msg = new DataRowMessage(length, fieldCount)
|
||||
for (var i = 0; i < fieldCount; i++) {
|
||||
msg.fields.push(this._readValue(buffer))
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
// extremely hot-path code
|
||||
Connection.prototype._readValue = function (buffer) {
|
||||
var length = this.parseInt32(buffer)
|
||||
if (length === -1) return null
|
||||
if (this._mode === TEXT_MODE) {
|
||||
return this.readString(buffer, length)
|
||||
}
|
||||
return this.readBytes(buffer, length)
|
||||
}
|
||||
|
||||
// parses error
|
||||
Connection.prototype.parseE = function (buffer, length) {
|
||||
var fields = {}
|
||||
var msg, item
|
||||
var input = new Message('error', length)
|
||||
var fieldType = this.readString(buffer, 1)
|
||||
while (fieldType !== '\0') {
|
||||
fields[fieldType] = this.parseCString(buffer)
|
||||
fieldType = this.readString(buffer, 1)
|
||||
}
|
||||
if (input.name === 'error') {
|
||||
// the msg is an Error instance
|
||||
msg = new Error(fields.M)
|
||||
for (item in input) {
|
||||
// copy input properties to the error
|
||||
if (input.hasOwnProperty(item)) {
|
||||
msg[item] = input[item]
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// the msg is an object literal
|
||||
msg = input
|
||||
msg.message = fields.M
|
||||
}
|
||||
msg.severity = fields.S
|
||||
msg.code = fields.C
|
||||
msg.detail = fields.D
|
||||
msg.hint = fields.H
|
||||
msg.position = fields.P
|
||||
msg.internalPosition = fields.p
|
||||
msg.internalQuery = fields.q
|
||||
msg.where = fields.W
|
||||
msg.schema = fields.s
|
||||
msg.table = fields.t
|
||||
msg.column = fields.c
|
||||
msg.dataType = fields.d
|
||||
msg.constraint = fields.n
|
||||
msg.file = fields.F
|
||||
msg.line = fields.L
|
||||
msg.routine = fields.R
|
||||
return msg
|
||||
}
|
||||
|
||||
// same thing, different name
|
||||
Connection.prototype.parseN = function (buffer, length) {
|
||||
var msg = this.parseE(buffer, length)
|
||||
msg.name = 'notice'
|
||||
return msg
|
||||
}
|
||||
|
||||
Connection.prototype.parseA = function (buffer, length) {
|
||||
var msg = new Message('notification', length)
|
||||
msg.processId = this.parseInt32(buffer)
|
||||
msg.channel = this.parseCString(buffer)
|
||||
msg.payload = this.parseCString(buffer)
|
||||
return msg
|
||||
}
|
||||
|
||||
Connection.prototype.parseG = function (buffer, length) {
|
||||
var msg = new Message('copyInResponse', length)
|
||||
return this.parseGH(buffer, msg)
|
||||
}
|
||||
|
||||
Connection.prototype.parseH = function (buffer, length) {
|
||||
var msg = new Message('copyOutResponse', length)
|
||||
return this.parseGH(buffer, msg)
|
||||
}
|
||||
|
||||
Connection.prototype.parseGH = function (buffer, msg) {
|
||||
var isBinary = buffer[this.offset] !== 0
|
||||
this.offset++
|
||||
msg.binary = isBinary
|
||||
var columnCount = this.parseInt16(buffer)
|
||||
msg.columnTypes = []
|
||||
for (var i = 0; i < columnCount; i++) {
|
||||
msg.columnTypes.push(this.parseInt16(buffer))
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
Connection.prototype.parsed = function (buffer, length) {
|
||||
var msg = new Message('copyData', length)
|
||||
msg.chunk = this.readBytes(buffer, msg.length - 4)
|
||||
return msg
|
||||
}
|
||||
|
||||
Connection.prototype.parseInt32 = function (buffer) {
|
||||
var value = buffer.readInt32BE(this.offset)
|
||||
this.offset += 4
|
||||
return value
|
||||
}
|
||||
|
||||
Connection.prototype.parseInt16 = function (buffer) {
|
||||
var value = buffer.readInt16BE(this.offset)
|
||||
this.offset += 2
|
||||
return value
|
||||
}
|
||||
|
||||
Connection.prototype.readString = function (buffer, length) {
|
||||
return buffer.toString(this.encoding, this.offset, (this.offset += length))
|
||||
}
|
||||
|
||||
Connection.prototype.readBytes = function (buffer, length) {
|
||||
return buffer.slice(this.offset, (this.offset += length))
|
||||
}
|
||||
|
||||
Connection.prototype.parseCString = function (buffer) {
|
||||
var start = this.offset
|
||||
var end = buffer.indexOf(0, start)
|
||||
this.offset = end + 1
|
||||
return buffer.toString(this.encoding, start, end)
|
||||
}
|
||||
// end parsing methods
|
||||
module.exports = Connection
|
70
express-server/node_modules/pg/lib/defaults.js
generated
vendored
Normal file
70
express-server/node_modules/pg/lib/defaults.js
generated
vendored
Normal file
@ -0,0 +1,70 @@
|
||||
'use strict'
|
||||
/**
|
||||
* Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* README.md file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
module.exports = {
|
||||
// database host. defaults to localhost
|
||||
host: 'localhost',
|
||||
|
||||
// database user's name
|
||||
user: process.platform === 'win32' ? process.env.USERNAME : process.env.USER,
|
||||
|
||||
// name of database to connect
|
||||
database: process.platform === 'win32' ? process.env.USERNAME : process.env.USER,
|
||||
|
||||
// database user's password
|
||||
password: null,
|
||||
|
||||
// a Postgres connection string to be used instead of setting individual connection items
|
||||
// NOTE: Setting this value will cause it to override any other value (such as database or user) defined
|
||||
// in the defaults object.
|
||||
connectionString: undefined,
|
||||
|
||||
// database port
|
||||
port: 5432,
|
||||
|
||||
// number of rows to return at a time from a prepared statement's
|
||||
// portal. 0 will return all rows at once
|
||||
rows: 0,
|
||||
|
||||
// binary result mode
|
||||
binary: false,
|
||||
|
||||
// Connection pool options - see https://github.com/brianc/node-pg-pool
|
||||
|
||||
// number of connections to use in connection pool
|
||||
// 0 will disable connection pooling
|
||||
max: 10,
|
||||
|
||||
// max milliseconds a client can go unused before it is removed
|
||||
// from the pool and destroyed
|
||||
idleTimeoutMillis: 30000,
|
||||
|
||||
client_encoding: '',
|
||||
|
||||
ssl: false,
|
||||
|
||||
application_name: undefined,
|
||||
fallback_application_name: undefined,
|
||||
|
||||
parseInputDatesAsUTC: false,
|
||||
|
||||
// max milliseconds any query using this connection will execute for before timing out in error. false=unlimited
|
||||
statement_timeout: false
|
||||
}
|
||||
|
||||
var pgTypes = require('pg-types')
|
||||
// save default parsers
|
||||
var parseBigInteger = pgTypes.getTypeParser(20, 'text')
|
||||
var parseBigIntegerArray = pgTypes.getTypeParser(1016, 'text')
|
||||
|
||||
// parse int8 so you can get your count values as actual numbers
|
||||
module.exports.__defineSetter__('parseInt8', function (val) {
|
||||
pgTypes.setTypeParser(20, 'text', val ? pgTypes.getTypeParser(23, 'text') : parseBigInteger)
|
||||
pgTypes.setTypeParser(1016, 'text', val ? pgTypes.getTypeParser(1007, 'text') : parseBigIntegerArray)
|
||||
})
|
57
express-server/node_modules/pg/lib/index.js
generated
vendored
Normal file
57
express-server/node_modules/pg/lib/index.js
generated
vendored
Normal file
@ -0,0 +1,57 @@
|
||||
'use strict'
|
||||
/**
|
||||
* Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* README.md file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
var util = require('util')
|
||||
var Client = require('./client')
|
||||
var defaults = require('./defaults')
|
||||
var Connection = require('./connection')
|
||||
var Pool = require('pg-pool')
|
||||
|
||||
const poolFactory = (Client) => {
|
||||
var BoundPool = function (options) {
|
||||
var config = Object.assign({ Client: Client }, options)
|
||||
return new Pool(config)
|
||||
}
|
||||
|
||||
util.inherits(BoundPool, Pool)
|
||||
|
||||
return BoundPool
|
||||
}
|
||||
|
||||
var PG = function (clientConstructor) {
|
||||
this.defaults = defaults
|
||||
this.Client = clientConstructor
|
||||
this.Query = this.Client.Query
|
||||
this.Pool = poolFactory(this.Client)
|
||||
this._pools = []
|
||||
this.Connection = Connection
|
||||
this.types = require('pg-types')
|
||||
}
|
||||
|
||||
if (typeof process.env.NODE_PG_FORCE_NATIVE !== 'undefined') {
|
||||
module.exports = new PG(require('./native'))
|
||||
} else {
|
||||
module.exports = new PG(Client)
|
||||
|
||||
// lazy require native module...the native module may not have installed
|
||||
module.exports.__defineGetter__('native', function () {
|
||||
delete module.exports.native
|
||||
var native = null
|
||||
try {
|
||||
native = new PG(require('./native'))
|
||||
} catch (err) {
|
||||
if (err.code !== 'MODULE_NOT_FOUND') {
|
||||
throw err
|
||||
}
|
||||
console.error(err.message)
|
||||
}
|
||||
module.exports.native = native
|
||||
return native
|
||||
})
|
||||
}
|
256
express-server/node_modules/pg/lib/native/client.js
generated
vendored
Normal file
256
express-server/node_modules/pg/lib/native/client.js
generated
vendored
Normal file
@ -0,0 +1,256 @@
|
||||
'use strict'
|
||||
/**
|
||||
* Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* README.md file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
var Native = require('pg-native')
|
||||
var TypeOverrides = require('../type-overrides')
|
||||
var semver = require('semver')
|
||||
var pkg = require('../../package.json')
|
||||
var assert = require('assert')
|
||||
var EventEmitter = require('events').EventEmitter
|
||||
var util = require('util')
|
||||
var ConnectionParameters = require('../connection-parameters')
|
||||
|
||||
var msg = 'Version >= ' + pkg.minNativeVersion + ' of pg-native required.'
|
||||
assert(semver.gte(Native.version, pkg.minNativeVersion), msg)
|
||||
|
||||
var NativeQuery = require('./query')
|
||||
|
||||
var Client = module.exports = function (config) {
|
||||
EventEmitter.call(this)
|
||||
config = config || {}
|
||||
|
||||
this._Promise = config.Promise || global.Promise
|
||||
this._types = new TypeOverrides(config.types)
|
||||
|
||||
this.native = new Native({
|
||||
types: this._types
|
||||
})
|
||||
|
||||
this._queryQueue = []
|
||||
this._ending = false
|
||||
this._connecting = false
|
||||
this._connected = false
|
||||
this._queryable = true
|
||||
|
||||
// keep these on the object for legacy reasons
|
||||
// for the time being. TODO: deprecate all this jazz
|
||||
var cp = this.connectionParameters = new ConnectionParameters(config)
|
||||
this.user = cp.user
|
||||
this.password = cp.password
|
||||
this.database = cp.database
|
||||
this.host = cp.host
|
||||
this.port = cp.port
|
||||
|
||||
// a hash to hold named queries
|
||||
this.namedQueries = {}
|
||||
}
|
||||
|
||||
Client.Query = NativeQuery
|
||||
|
||||
util.inherits(Client, EventEmitter)
|
||||
|
||||
Client.prototype._errorAllQueries = function (err) {
|
||||
const enqueueError = (query) => {
|
||||
process.nextTick(() => {
|
||||
query.native = this.native
|
||||
query.handleError(err)
|
||||
})
|
||||
}
|
||||
|
||||
if (this._hasActiveQuery()) {
|
||||
enqueueError(this._activeQuery)
|
||||
this._activeQuery = null
|
||||
}
|
||||
|
||||
this._queryQueue.forEach(enqueueError)
|
||||
this._queryQueue.length = 0
|
||||
}
|
||||
|
||||
// connect to the backend
|
||||
// pass an optional callback to be called once connected
|
||||
// or with an error if there was a connection error
|
||||
Client.prototype._connect = function (cb) {
|
||||
var self = this
|
||||
|
||||
if (this._connecting) {
|
||||
process.nextTick(() => cb(new Error('Client has already been connected. You cannot reuse a client.')))
|
||||
return
|
||||
}
|
||||
|
||||
this._connecting = true
|
||||
|
||||
this.connectionParameters.getLibpqConnectionString(function (err, conString) {
|
||||
if (err) return cb(err)
|
||||
self.native.connect(conString, function (err) {
|
||||
if (err) return cb(err)
|
||||
|
||||
// set internal states to connected
|
||||
self._connected = true
|
||||
|
||||
// handle connection errors from the native layer
|
||||
self.native.on('error', function (err) {
|
||||
self._queryable = false
|
||||
self._errorAllQueries(err)
|
||||
self.emit('error', err)
|
||||
})
|
||||
|
||||
self.native.on('notification', function (msg) {
|
||||
self.emit('notification', {
|
||||
channel: msg.relname,
|
||||
payload: msg.extra
|
||||
})
|
||||
})
|
||||
|
||||
// signal we are connected now
|
||||
self.emit('connect')
|
||||
self._pulseQueryQueue(true)
|
||||
|
||||
cb()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Client.prototype.connect = function (callback) {
|
||||
if (callback) {
|
||||
this._connect(callback)
|
||||
return
|
||||
}
|
||||
|
||||
return new this._Promise((resolve, reject) => {
|
||||
this._connect((error) => {
|
||||
if (error) {
|
||||
reject(error)
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// send a query to the server
|
||||
// this method is highly overloaded to take
|
||||
// 1) string query, optional array of parameters, optional function callback
|
||||
// 2) object query with {
|
||||
// string query
|
||||
// optional array values,
|
||||
// optional function callback instead of as a separate parameter
|
||||
// optional string name to name & cache the query plan
|
||||
// optional string rowMode = 'array' for an array of results
|
||||
// }
|
||||
Client.prototype.query = function (config, values, callback) {
|
||||
var query
|
||||
var result
|
||||
|
||||
if (typeof config.submit === 'function') {
|
||||
result = query = config
|
||||
// accept query(new Query(...), (err, res) => { }) style
|
||||
if (typeof values === 'function') {
|
||||
config.callback = values
|
||||
}
|
||||
} else {
|
||||
query = new NativeQuery(config, values, callback)
|
||||
if (!query.callback) {
|
||||
let resolveOut, rejectOut
|
||||
result = new this._Promise((resolve, reject) => {
|
||||
resolveOut = resolve
|
||||
rejectOut = reject
|
||||
})
|
||||
query.callback = (err, res) => err ? rejectOut(err) : resolveOut(res)
|
||||
}
|
||||
}
|
||||
|
||||
if (!this._queryable) {
|
||||
query.native = this.native
|
||||
process.nextTick(() => {
|
||||
query.handleError(new Error('Client has encountered a connection error and is not queryable'))
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
||||
if (this._ending) {
|
||||
query.native = this.native
|
||||
process.nextTick(() => {
|
||||
query.handleError(new Error('Client was closed and is not queryable'))
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
||||
this._queryQueue.push(query)
|
||||
this._pulseQueryQueue()
|
||||
return result
|
||||
}
|
||||
|
||||
// disconnect from the backend server
|
||||
Client.prototype.end = function (cb) {
|
||||
var self = this
|
||||
|
||||
this._ending = true
|
||||
|
||||
if (!this._connected) {
|
||||
this.once('connect', this.end.bind(this, cb))
|
||||
}
|
||||
var result
|
||||
if (!cb) {
|
||||
result = new this._Promise(function (resolve, reject) {
|
||||
cb = (err) => err ? reject(err) : resolve()
|
||||
})
|
||||
}
|
||||
this.native.end(function () {
|
||||
self._errorAllQueries(new Error('Connection terminated'))
|
||||
|
||||
process.nextTick(() => {
|
||||
self.emit('end')
|
||||
if (cb) cb()
|
||||
})
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
||||
Client.prototype._hasActiveQuery = function () {
|
||||
return this._activeQuery && this._activeQuery.state !== 'error' && this._activeQuery.state !== 'end'
|
||||
}
|
||||
|
||||
Client.prototype._pulseQueryQueue = function (initialConnection) {
|
||||
if (!this._connected) {
|
||||
return
|
||||
}
|
||||
if (this._hasActiveQuery()) {
|
||||
return
|
||||
}
|
||||
var query = this._queryQueue.shift()
|
||||
if (!query) {
|
||||
if (!initialConnection) {
|
||||
this.emit('drain')
|
||||
}
|
||||
return
|
||||
}
|
||||
this._activeQuery = query
|
||||
query.submit(this)
|
||||
var self = this
|
||||
query.once('_done', function () {
|
||||
self._pulseQueryQueue()
|
||||
})
|
||||
}
|
||||
|
||||
// attempt to cancel an in-progress query
|
||||
Client.prototype.cancel = function (query) {
|
||||
if (this._activeQuery === query) {
|
||||
this.native.cancel(function () {})
|
||||
} else if (this._queryQueue.indexOf(query) !== -1) {
|
||||
this._queryQueue.splice(this._queryQueue.indexOf(query), 1)
|
||||
}
|
||||
}
|
||||
|
||||
Client.prototype.setTypeParser = function (oid, format, parseFn) {
|
||||
return this._types.setTypeParser(oid, format, parseFn)
|
||||
}
|
||||
|
||||
Client.prototype.getTypeParser = function (oid, format) {
|
||||
return this._types.getTypeParser(oid, format)
|
||||
}
|
2
express-server/node_modules/pg/lib/native/index.js
generated
vendored
Normal file
2
express-server/node_modules/pg/lib/native/index.js
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
'use strict'
|
||||
module.exports = require('./client')
|
160
express-server/node_modules/pg/lib/native/query.js
generated
vendored
Normal file
160
express-server/node_modules/pg/lib/native/query.js
generated
vendored
Normal file
@ -0,0 +1,160 @@
|
||||
'use strict'
|
||||
/**
|
||||
* Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* README.md file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
var EventEmitter = require('events').EventEmitter
|
||||
var util = require('util')
|
||||
var utils = require('../utils')
|
||||
|
||||
var NativeQuery = module.exports = function (config, values, callback) {
|
||||
EventEmitter.call(this)
|
||||
config = utils.normalizeQueryConfig(config, values, callback)
|
||||
this.text = config.text
|
||||
this.values = config.values
|
||||
this.name = config.name
|
||||
this.callback = config.callback
|
||||
this.state = 'new'
|
||||
this._arrayMode = config.rowMode === 'array'
|
||||
|
||||
// if the 'row' event is listened for
|
||||
// then emit them as they come in
|
||||
// without setting singleRowMode to true
|
||||
// this has almost no meaning because libpq
|
||||
// reads all rows into memory befor returning any
|
||||
this._emitRowEvents = false
|
||||
this.on('newListener', function (event) {
|
||||
if (event === 'row') this._emitRowEvents = true
|
||||
}.bind(this))
|
||||
}
|
||||
|
||||
util.inherits(NativeQuery, EventEmitter)
|
||||
|
||||
var errorFieldMap = {
|
||||
'sqlState': 'code',
|
||||
'statementPosition': 'position',
|
||||
'messagePrimary': 'message',
|
||||
'context': 'where',
|
||||
'schemaName': 'schema',
|
||||
'tableName': 'table',
|
||||
'columnName': 'column',
|
||||
'dataTypeName': 'dataType',
|
||||
'constraintName': 'constraint',
|
||||
'sourceFile': 'file',
|
||||
'sourceLine': 'line',
|
||||
'sourceFunction': 'routine'
|
||||
}
|
||||
|
||||
NativeQuery.prototype.handleError = function (err) {
|
||||
// copy pq error fields into the error object
|
||||
var fields = this.native.pq.resultErrorFields()
|
||||
if (fields) {
|
||||
for (var key in fields) {
|
||||
var normalizedFieldName = errorFieldMap[key] || key
|
||||
err[normalizedFieldName] = fields[key]
|
||||
}
|
||||
}
|
||||
if (this.callback) {
|
||||
this.callback(err)
|
||||
} else {
|
||||
this.emit('error', err)
|
||||
}
|
||||
this.state = 'error'
|
||||
}
|
||||
|
||||
NativeQuery.prototype.then = function (onSuccess, onFailure) {
|
||||
return this._getPromise().then(onSuccess, onFailure)
|
||||
}
|
||||
|
||||
NativeQuery.prototype.catch = function (callback) {
|
||||
return this._getPromise().catch(callback)
|
||||
}
|
||||
|
||||
NativeQuery.prototype._getPromise = function () {
|
||||
if (this._promise) return this._promise
|
||||
this._promise = new Promise(function (resolve, reject) {
|
||||
this._once('end', resolve)
|
||||
this._once('error', reject)
|
||||
}.bind(this))
|
||||
return this._promise
|
||||
}
|
||||
|
||||
NativeQuery.prototype.submit = function (client) {
|
||||
this.state = 'running'
|
||||
var self = this
|
||||
this.native = client.native
|
||||
client.native.arrayMode = this._arrayMode
|
||||
|
||||
var after = function (err, rows, results) {
|
||||
client.native.arrayMode = false
|
||||
setImmediate(function () {
|
||||
self.emit('_done')
|
||||
})
|
||||
|
||||
// handle possible query error
|
||||
if (err) {
|
||||
return self.handleError(err)
|
||||
}
|
||||
|
||||
// emit row events for each row in the result
|
||||
if (self._emitRowEvents) {
|
||||
if (results.length > 1) {
|
||||
rows.forEach((rowOfRows, i) => {
|
||||
rowOfRows.forEach(row => {
|
||||
self.emit('row', row, results[i])
|
||||
})
|
||||
})
|
||||
} else {
|
||||
rows.forEach(function (row) {
|
||||
self.emit('row', row, results)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// handle successful result
|
||||
self.state = 'end'
|
||||
self.emit('end', results)
|
||||
if (self.callback) {
|
||||
self.callback(null, results)
|
||||
}
|
||||
}
|
||||
|
||||
if (process.domain) {
|
||||
after = process.domain.bind(after)
|
||||
}
|
||||
|
||||
// named query
|
||||
if (this.name) {
|
||||
if (this.name.length > 63) {
|
||||
console.error('Warning! Postgres only supports 63 characters for query names.')
|
||||
console.error('You supplied', this.name, '(', this.name.length, ')')
|
||||
console.error('This can cause conflicts and silent errors executing queries')
|
||||
}
|
||||
var values = (this.values || []).map(utils.prepareValue)
|
||||
|
||||
// check if the client has already executed this named query
|
||||
// if so...just execute it again - skip the planning phase
|
||||
if (client.namedQueries[this.name]) {
|
||||
return client.native.execute(this.name, values, after)
|
||||
}
|
||||
// plan the named query the first time, then execute it
|
||||
return client.native.prepare(this.name, this.text, values.length, function (err) {
|
||||
if (err) return after(err)
|
||||
client.namedQueries[self.name] = true
|
||||
return self.native.execute(self.name, values, after)
|
||||
})
|
||||
} else if (this.values) {
|
||||
if (!Array.isArray(this.values)) {
|
||||
const err = new Error('Query values must be an array')
|
||||
return after(err)
|
||||
}
|
||||
var vals = this.values.map(utils.prepareValue)
|
||||
client.native.query(this.text, vals, after)
|
||||
} else {
|
||||
client.native.query(this.text, after)
|
||||
}
|
||||
}
|
219
express-server/node_modules/pg/lib/query.js
generated
vendored
Normal file
219
express-server/node_modules/pg/lib/query.js
generated
vendored
Normal file
@ -0,0 +1,219 @@
|
||||
'use strict'
|
||||
/**
|
||||
* Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* README.md file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
var EventEmitter = require('events').EventEmitter
|
||||
var util = require('util')
|
||||
|
||||
var Result = require('./result')
|
||||
var utils = require('./utils')
|
||||
|
||||
var Query = function (config, values, callback) {
|
||||
// use of "new" optional
|
||||
if (!(this instanceof Query)) { return new Query(config, values, callback) }
|
||||
|
||||
config = utils.normalizeQueryConfig(config, values, callback)
|
||||
|
||||
this.text = config.text
|
||||
this.values = config.values
|
||||
this.rows = config.rows
|
||||
this.types = config.types
|
||||
this.name = config.name
|
||||
this.binary = config.binary
|
||||
// use unique portal name each time
|
||||
this.portal = config.portal || ''
|
||||
this.callback = config.callback
|
||||
this._rowMode = config.rowMode
|
||||
if (process.domain && config.callback) {
|
||||
this.callback = process.domain.bind(config.callback)
|
||||
}
|
||||
this._result = new Result(this._rowMode, this.types)
|
||||
|
||||
// potential for multiple results
|
||||
this._results = this._result
|
||||
this.isPreparedStatement = false
|
||||
this._canceledDueToError = false
|
||||
this._promise = null
|
||||
EventEmitter.call(this)
|
||||
}
|
||||
|
||||
util.inherits(Query, EventEmitter)
|
||||
|
||||
Query.prototype.requiresPreparation = function () {
|
||||
// named queries must always be prepared
|
||||
if (this.name) { return true }
|
||||
// always prepare if there are max number of rows expected per
|
||||
// portal execution
|
||||
if (this.rows) { return true }
|
||||
// don't prepare empty text queries
|
||||
if (!this.text) { return false }
|
||||
// prepare if there are values
|
||||
if (!this.values) { return false }
|
||||
return this.values.length > 0
|
||||
}
|
||||
|
||||
Query.prototype._checkForMultirow = function () {
|
||||
// if we already have a result with a command property
|
||||
// then we've already executed one query in a multi-statement simple query
|
||||
// turn our results into an array of results
|
||||
if (this._result.command) {
|
||||
if (!Array.isArray(this._results)) {
|
||||
this._results = [this._result]
|
||||
}
|
||||
this._result = new Result(this._rowMode, this.types)
|
||||
this._results.push(this._result)
|
||||
}
|
||||
}
|
||||
|
||||
// associates row metadata from the supplied
|
||||
// message with this query object
|
||||
// metadata used when parsing row results
|
||||
Query.prototype.handleRowDescription = function (msg) {
|
||||
this._checkForMultirow()
|
||||
this._result.addFields(msg.fields)
|
||||
this._accumulateRows = this.callback || !this.listeners('row').length
|
||||
}
|
||||
|
||||
Query.prototype.handleDataRow = function (msg) {
|
||||
var row
|
||||
|
||||
if (this._canceledDueToError) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
row = this._result.parseRow(msg.fields)
|
||||
} catch (err) {
|
||||
this._canceledDueToError = err
|
||||
return
|
||||
}
|
||||
|
||||
this.emit('row', row, this._result)
|
||||
if (this._accumulateRows) {
|
||||
this._result.addRow(row)
|
||||
}
|
||||
}
|
||||
|
||||
Query.prototype.handleCommandComplete = function (msg, con) {
|
||||
this._checkForMultirow()
|
||||
this._result.addCommandComplete(msg)
|
||||
// need to sync after each command complete of a prepared statement
|
||||
if (this.isPreparedStatement) {
|
||||
con.sync()
|
||||
}
|
||||
}
|
||||
|
||||
// if a named prepared statement is created with empty query text
|
||||
// the backend will send an emptyQuery message but *not* a command complete message
|
||||
// execution on the connection will hang until the backend receives a sync message
|
||||
Query.prototype.handleEmptyQuery = function (con) {
|
||||
if (this.isPreparedStatement) {
|
||||
con.sync()
|
||||
}
|
||||
}
|
||||
|
||||
Query.prototype.handleReadyForQuery = function (con) {
|
||||
if (this._canceledDueToError) {
|
||||
return this.handleError(this._canceledDueToError, con)
|
||||
}
|
||||
if (this.callback) {
|
||||
this.callback(null, this._results)
|
||||
}
|
||||
this.emit('end', this._results)
|
||||
}
|
||||
|
||||
Query.prototype.handleError = function (err, connection) {
|
||||
// need to sync after error during a prepared statement
|
||||
if (this.isPreparedStatement) {
|
||||
connection.sync()
|
||||
}
|
||||
if (this._canceledDueToError) {
|
||||
err = this._canceledDueToError
|
||||
this._canceledDueToError = false
|
||||
}
|
||||
// if callback supplied do not emit error event as uncaught error
|
||||
// events will bubble up to node process
|
||||
if (this.callback) {
|
||||
return this.callback(err)
|
||||
}
|
||||
this.emit('error', err)
|
||||
}
|
||||
|
||||
Query.prototype.submit = function (connection) {
|
||||
if (typeof this.text !== 'string' && typeof this.name !== 'string') {
|
||||
return new Error('A query must have either text or a name. Supplying neither is unsupported.')
|
||||
}
|
||||
if (this.values && !Array.isArray(this.values)) {
|
||||
return new Error('Query values must be an array')
|
||||
}
|
||||
if (this.requiresPreparation()) {
|
||||
this.prepare(connection)
|
||||
} else {
|
||||
connection.query(this.text)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
Query.prototype.hasBeenParsed = function (connection) {
|
||||
return this.name && connection.parsedStatements[this.name]
|
||||
}
|
||||
|
||||
Query.prototype.handlePortalSuspended = function (connection) {
|
||||
this._getRows(connection, this.rows)
|
||||
}
|
||||
|
||||
Query.prototype._getRows = function (connection, rows) {
|
||||
connection.execute({
|
||||
portal: this.portal,
|
||||
rows: rows
|
||||
}, true)
|
||||
connection.flush()
|
||||
}
|
||||
|
||||
Query.prototype.prepare = function (connection) {
|
||||
var self = this
|
||||
// prepared statements need sync to be called after each command
|
||||
// complete or when an error is encountered
|
||||
this.isPreparedStatement = true
|
||||
// TODO refactor this poor encapsulation
|
||||
if (!this.hasBeenParsed(connection)) {
|
||||
connection.parse({
|
||||
text: self.text,
|
||||
name: self.name,
|
||||
types: self.types
|
||||
}, true)
|
||||
}
|
||||
|
||||
if (self.values) {
|
||||
self.values = self.values.map(utils.prepareValue)
|
||||
}
|
||||
|
||||
// http://developer.postgresql.org/pgdocs/postgres/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY
|
||||
connection.bind({
|
||||
portal: self.portal,
|
||||
statement: self.name,
|
||||
values: self.values,
|
||||
binary: self.binary
|
||||
}, true)
|
||||
|
||||
connection.describe({
|
||||
type: 'P',
|
||||
name: self.portal || ''
|
||||
}, true)
|
||||
|
||||
this._getRows(connection, this.rows)
|
||||
}
|
||||
|
||||
Query.prototype.handleCopyInResponse = function (connection) {
|
||||
connection.sendCopyFail('No source stream defined')
|
||||
}
|
||||
|
||||
Query.prototype.handleCopyData = function (msg, connection) {
|
||||
// noop
|
||||
}
|
||||
module.exports = Query
|
104
express-server/node_modules/pg/lib/result.js
generated
vendored
Normal file
104
express-server/node_modules/pg/lib/result.js
generated
vendored
Normal file
@ -0,0 +1,104 @@
|
||||
'use strict'
|
||||
/**
|
||||
* Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* README.md file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
var types = require('pg-types')
|
||||
|
||||
// result object returned from query
|
||||
// in the 'end' event and also
|
||||
// passed as second argument to provided callback
|
||||
var Result = function (rowMode) {
|
||||
this.command = null
|
||||
this.rowCount = null
|
||||
this.oid = null
|
||||
this.rows = []
|
||||
this.fields = []
|
||||
this._parsers = []
|
||||
this.RowCtor = null
|
||||
this.rowAsArray = rowMode === 'array'
|
||||
if (this.rowAsArray) {
|
||||
this.parseRow = this._parseRowAsArray
|
||||
}
|
||||
}
|
||||
|
||||
var matchRegexp = /^([A-Za-z]+)(?: (\d+))?(?: (\d+))?/
|
||||
|
||||
// adds a command complete message
|
||||
Result.prototype.addCommandComplete = function (msg) {
|
||||
var match
|
||||
if (msg.text) {
|
||||
// pure javascript
|
||||
match = matchRegexp.exec(msg.text)
|
||||
} else {
|
||||
// native bindings
|
||||
match = matchRegexp.exec(msg.command)
|
||||
}
|
||||
if (match) {
|
||||
this.command = match[1]
|
||||
if (match[3]) {
|
||||
// COMMMAND OID ROWS
|
||||
this.oid = parseInt(match[2], 10)
|
||||
this.rowCount = parseInt(match[3], 10)
|
||||
} else if (match[2]) {
|
||||
// COMMAND ROWS
|
||||
this.rowCount = parseInt(match[2], 10)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Result.prototype._parseRowAsArray = function (rowData) {
|
||||
var row = []
|
||||
for (var i = 0, len = rowData.length; i < len; i++) {
|
||||
var rawValue = rowData[i]
|
||||
if (rawValue !== null) {
|
||||
row.push(this._parsers[i](rawValue))
|
||||
} else {
|
||||
row.push(null)
|
||||
}
|
||||
}
|
||||
return row
|
||||
}
|
||||
|
||||
Result.prototype.parseRow = function (rowData) {
|
||||
var row = {}
|
||||
for (var i = 0, len = rowData.length; i < len; i++) {
|
||||
var rawValue = rowData[i]
|
||||
var field = this.fields[i].name
|
||||
if (rawValue !== null) {
|
||||
row[field] = this._parsers[i](rawValue)
|
||||
} else {
|
||||
row[field] = null
|
||||
}
|
||||
}
|
||||
return row
|
||||
}
|
||||
|
||||
Result.prototype.addRow = function (row) {
|
||||
this.rows.push(row)
|
||||
}
|
||||
|
||||
Result.prototype.addFields = function (fieldDescriptions) {
|
||||
// clears field definitions
|
||||
// multiple query statements in 1 action can result in multiple sets
|
||||
// of rowDescriptions...eg: 'select NOW(); select 1::int;'
|
||||
// you need to reset the fields
|
||||
if (this.fields.length) {
|
||||
this.fields = []
|
||||
this._parsers = []
|
||||
}
|
||||
for (var i = 0; i < fieldDescriptions.length; i++) {
|
||||
var desc = fieldDescriptions[i]
|
||||
this.fields.push(desc)
|
||||
var parser = this._getTypeParser(desc.dataTypeID, desc.format || 'text')
|
||||
this._parsers.push(parser)
|
||||
}
|
||||
}
|
||||
|
||||
Result.prototype._getTypeParser = types.getTypeParser
|
||||
|
||||
module.exports = Result
|
39
express-server/node_modules/pg/lib/type-overrides.js
generated
vendored
Normal file
39
express-server/node_modules/pg/lib/type-overrides.js
generated
vendored
Normal file
@ -0,0 +1,39 @@
|
||||
'use strict'
|
||||
/**
|
||||
* Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* README.md file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
var types = require('pg-types')
|
||||
|
||||
function TypeOverrides (userTypes) {
|
||||
this._types = userTypes || types
|
||||
this.text = {}
|
||||
this.binary = {}
|
||||
}
|
||||
|
||||
TypeOverrides.prototype.getOverrides = function (format) {
|
||||
switch (format) {
|
||||
case 'text': return this.text
|
||||
case 'binary': return this.binary
|
||||
default: return {}
|
||||
}
|
||||
}
|
||||
|
||||
TypeOverrides.prototype.setTypeParser = function (oid, format, parseFn) {
|
||||
if (typeof format === 'function') {
|
||||
parseFn = format
|
||||
format = 'text'
|
||||
}
|
||||
this.getOverrides(format)[oid] = parseFn
|
||||
}
|
||||
|
||||
TypeOverrides.prototype.getTypeParser = function (oid, format) {
|
||||
format = format || 'text'
|
||||
return this.getOverrides(format)[oid] || this._types.getTypeParser(oid, format)
|
||||
}
|
||||
|
||||
module.exports = TypeOverrides
|
164
express-server/node_modules/pg/lib/utils.js
generated
vendored
Normal file
164
express-server/node_modules/pg/lib/utils.js
generated
vendored
Normal file
@ -0,0 +1,164 @@
|
||||
'use strict'
|
||||
/**
|
||||
* Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* README.md file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
const crypto = require('crypto')
|
||||
|
||||
const defaults = require('./defaults')
|
||||
|
||||
function escapeElement (elementRepresentation) {
|
||||
var escaped = elementRepresentation
|
||||
.replace(/\\/g, '\\\\')
|
||||
.replace(/"/g, '\\"')
|
||||
|
||||
return '"' + escaped + '"'
|
||||
}
|
||||
|
||||
// convert a JS array to a postgres array literal
|
||||
// uses comma separator so won't work for types like box that use
|
||||
// a different array separator.
|
||||
function arrayString (val) {
|
||||
var result = '{'
|
||||
for (var i = 0; i < val.length; i++) {
|
||||
if (i > 0) {
|
||||
result = result + ','
|
||||
}
|
||||
if (val[i] === null || typeof val[i] === 'undefined') {
|
||||
result = result + 'NULL'
|
||||
} else if (Array.isArray(val[i])) {
|
||||
result = result + arrayString(val[i])
|
||||
} else if (val[i] instanceof Buffer) {
|
||||
result += '\\\\x' + val[i].toString('hex')
|
||||
} else {
|
||||
result += escapeElement(prepareValue(val[i]))
|
||||
}
|
||||
}
|
||||
result = result + '}'
|
||||
return result
|
||||
}
|
||||
|
||||
// converts values from javascript types
|
||||
// to their 'raw' counterparts for use as a postgres parameter
|
||||
// note: you can override this function to provide your own conversion mechanism
|
||||
// for complex types, etc...
|
||||
var prepareValue = function (val, seen) {
|
||||
if (val instanceof Buffer) {
|
||||
return val
|
||||
}
|
||||
if (ArrayBuffer.isView(val)) {
|
||||
var buf = Buffer.from(val.buffer, val.byteOffset, val.byteLength)
|
||||
if (buf.length === val.byteLength) {
|
||||
return buf
|
||||
}
|
||||
return buf.slice(val.byteOffset, val.byteOffset + val.byteLength) // Node.js v4 does not support those Buffer.from params
|
||||
}
|
||||
if (val instanceof Date) {
|
||||
if (defaults.parseInputDatesAsUTC) {
|
||||
return dateToStringUTC(val)
|
||||
} else {
|
||||
return dateToString(val)
|
||||
}
|
||||
}
|
||||
if (Array.isArray(val)) {
|
||||
return arrayString(val)
|
||||
}
|
||||
if (val === null || typeof val === 'undefined') {
|
||||
return null
|
||||
}
|
||||
if (typeof val === 'object') {
|
||||
return prepareObject(val, seen)
|
||||
}
|
||||
return val.toString()
|
||||
}
|
||||
|
||||
function prepareObject (val, seen) {
|
||||
if (val && typeof val.toPostgres === 'function') {
|
||||
seen = seen || []
|
||||
if (seen.indexOf(val) !== -1) {
|
||||
throw new Error('circular reference detected while preparing "' + val + '" for query')
|
||||
}
|
||||
seen.push(val)
|
||||
|
||||
return prepareValue(val.toPostgres(prepareValue), seen)
|
||||
}
|
||||
return JSON.stringify(val)
|
||||
}
|
||||
|
||||
function pad (number, digits) {
|
||||
number = '' + number
|
||||
while (number.length < digits) { number = '0' + number }
|
||||
return number
|
||||
}
|
||||
|
||||
function dateToString (date) {
|
||||
var offset = -date.getTimezoneOffset()
|
||||
var ret = pad(date.getFullYear(), 4) + '-' +
|
||||
pad(date.getMonth() + 1, 2) + '-' +
|
||||
pad(date.getDate(), 2) + 'T' +
|
||||
pad(date.getHours(), 2) + ':' +
|
||||
pad(date.getMinutes(), 2) + ':' +
|
||||
pad(date.getSeconds(), 2) + '.' +
|
||||
pad(date.getMilliseconds(), 3)
|
||||
|
||||
if (offset < 0) {
|
||||
ret += '-'
|
||||
offset *= -1
|
||||
} else { ret += '+' }
|
||||
|
||||
return ret + pad(Math.floor(offset / 60), 2) + ':' + pad(offset % 60, 2)
|
||||
}
|
||||
|
||||
function dateToStringUTC (date) {
|
||||
var ret = pad(date.getUTCFullYear(), 4) + '-' +
|
||||
pad(date.getUTCMonth() + 1, 2) + '-' +
|
||||
pad(date.getUTCDate(), 2) + 'T' +
|
||||
pad(date.getUTCHours(), 2) + ':' +
|
||||
pad(date.getUTCMinutes(), 2) + ':' +
|
||||
pad(date.getUTCSeconds(), 2) + '.' +
|
||||
pad(date.getUTCMilliseconds(), 3)
|
||||
|
||||
return ret + '+00:00'
|
||||
}
|
||||
|
||||
function normalizeQueryConfig (config, values, callback) {
|
||||
// can take in strings or config objects
|
||||
config = (typeof (config) === 'string') ? { text: config } : config
|
||||
if (values) {
|
||||
if (typeof values === 'function') {
|
||||
config.callback = values
|
||||
} else {
|
||||
config.values = values
|
||||
}
|
||||
}
|
||||
if (callback) {
|
||||
config.callback = callback
|
||||
}
|
||||
return config
|
||||
}
|
||||
|
||||
const md5 = function (string) {
|
||||
return crypto.createHash('md5').update(string, 'utf-8').digest('hex')
|
||||
}
|
||||
|
||||
// See AuthenticationMD5Password at https://www.postgresql.org/docs/current/static/protocol-flow.html
|
||||
const postgresMd5PasswordHash = function (user, password, salt) {
|
||||
var inner = md5(password + user)
|
||||
var outer = md5(Buffer.concat([Buffer.from(inner), salt]))
|
||||
return 'md5' + outer
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
prepareValue: function prepareValueWrapper (value) {
|
||||
// this ensures that extra arguments do not get passed into prepareValue
|
||||
// by accident, eg: from calling values.map(utils.prepareValue)
|
||||
return prepareValue(value)
|
||||
},
|
||||
normalizeQueryConfig,
|
||||
postgresMd5PasswordHash,
|
||||
md5
|
||||
}
|
Reference in New Issue
Block a user