client.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. 'use strict'
  2. var EventEmitter = require('events').EventEmitter
  3. var utils = require('./utils')
  4. var sasl = require('./crypto/sasl')
  5. var TypeOverrides = require('./type-overrides')
  6. var ConnectionParameters = require('./connection-parameters')
  7. var Query = require('./query')
  8. var defaults = require('./defaults')
  9. var Connection = require('./connection')
  10. const crypto = require('./crypto/utils')
  11. class Client extends EventEmitter {
  12. constructor(config) {
  13. super()
  14. this.connectionParameters = new ConnectionParameters(config)
  15. this.user = this.connectionParameters.user
  16. this.database = this.connectionParameters.database
  17. this.port = this.connectionParameters.port
  18. this.host = this.connectionParameters.host
  19. // "hiding" the password so it doesn't show up in stack traces
  20. // or if the client is console.logged
  21. Object.defineProperty(this, 'password', {
  22. configurable: true,
  23. enumerable: false,
  24. writable: true,
  25. value: this.connectionParameters.password,
  26. })
  27. this.replication = this.connectionParameters.replication
  28. var c = config || {}
  29. this._Promise = c.Promise || global.Promise
  30. this._types = new TypeOverrides(c.types)
  31. this._ending = false
  32. this._ended = false
  33. this._connecting = false
  34. this._connected = false
  35. this._connectionError = false
  36. this._queryable = true
  37. this.connection =
  38. c.connection ||
  39. new Connection({
  40. stream: c.stream,
  41. ssl: this.connectionParameters.ssl,
  42. keepAlive: c.keepAlive || false,
  43. keepAliveInitialDelayMillis: c.keepAliveInitialDelayMillis || 0,
  44. encoding: this.connectionParameters.client_encoding || 'utf8',
  45. })
  46. this.queryQueue = []
  47. this.binary = c.binary || defaults.binary
  48. this.processID = null
  49. this.secretKey = null
  50. this.ssl = this.connectionParameters.ssl || false
  51. // As with Password, make SSL->Key (the private key) non-enumerable.
  52. // It won't show up in stack traces
  53. // or if the client is console.logged
  54. if (this.ssl && this.ssl.key) {
  55. Object.defineProperty(this.ssl, 'key', {
  56. enumerable: false,
  57. })
  58. }
  59. this._connectionTimeoutMillis = c.connectionTimeoutMillis || 0
  60. }
  61. _errorAllQueries(err) {
  62. const enqueueError = (query) => {
  63. process.nextTick(() => {
  64. query.handleError(err, this.connection)
  65. })
  66. }
  67. if (this.activeQuery) {
  68. enqueueError(this.activeQuery)
  69. this.activeQuery = null
  70. }
  71. this.queryQueue.forEach(enqueueError)
  72. this.queryQueue.length = 0
  73. }
  74. _connect(callback) {
  75. var self = this
  76. var con = this.connection
  77. this._connectionCallback = callback
  78. if (this._connecting || this._connected) {
  79. const err = new Error('Client has already been connected. You cannot reuse a client.')
  80. process.nextTick(() => {
  81. callback(err)
  82. })
  83. return
  84. }
  85. this._connecting = true
  86. this.connectionTimeoutHandle
  87. if (this._connectionTimeoutMillis > 0) {
  88. this.connectionTimeoutHandle = setTimeout(() => {
  89. con._ending = true
  90. con.stream.destroy(new Error('timeout expired'))
  91. }, this._connectionTimeoutMillis)
  92. }
  93. if (this.host && this.host.indexOf('/') === 0) {
  94. con.connect(this.host + '/.s.PGSQL.' + this.port)
  95. } else {
  96. con.connect(this.port, this.host)
  97. }
  98. // once connection is established send startup message
  99. con.on('connect', function () {
  100. if (self.ssl) {
  101. con.requestSsl()
  102. } else {
  103. con.startup(self.getStartupConf())
  104. }
  105. })
  106. con.on('sslconnect', function () {
  107. con.startup(self.getStartupConf())
  108. })
  109. this._attachListeners(con)
  110. con.once('end', () => {
  111. const error = this._ending ? new Error('Connection terminated') : new Error('Connection terminated unexpectedly')
  112. clearTimeout(this.connectionTimeoutHandle)
  113. this._errorAllQueries(error)
  114. this._ended = true
  115. if (!this._ending) {
  116. // if the connection is ended without us calling .end()
  117. // on this client then we have an unexpected disconnection
  118. // treat this as an error unless we've already emitted an error
  119. // during connection.
  120. if (this._connecting && !this._connectionError) {
  121. if (this._connectionCallback) {
  122. this._connectionCallback(error)
  123. } else {
  124. this._handleErrorEvent(error)
  125. }
  126. } else if (!this._connectionError) {
  127. this._handleErrorEvent(error)
  128. }
  129. }
  130. process.nextTick(() => {
  131. this.emit('end')
  132. })
  133. })
  134. }
  135. connect(callback) {
  136. if (callback) {
  137. this._connect(callback)
  138. return
  139. }
  140. return new this._Promise((resolve, reject) => {
  141. this._connect((error) => {
  142. if (error) {
  143. reject(error)
  144. } else {
  145. resolve()
  146. }
  147. })
  148. })
  149. }
  150. _attachListeners(con) {
  151. // password request handling
  152. con.on('authenticationCleartextPassword', this._handleAuthCleartextPassword.bind(this))
  153. // password request handling
  154. con.on('authenticationMD5Password', this._handleAuthMD5Password.bind(this))
  155. // password request handling (SASL)
  156. con.on('authenticationSASL', this._handleAuthSASL.bind(this))
  157. con.on('authenticationSASLContinue', this._handleAuthSASLContinue.bind(this))
  158. con.on('authenticationSASLFinal', this._handleAuthSASLFinal.bind(this))
  159. con.on('backendKeyData', this._handleBackendKeyData.bind(this))
  160. con.on('error', this._handleErrorEvent.bind(this))
  161. con.on('errorMessage', this._handleErrorMessage.bind(this))
  162. con.on('readyForQuery', this._handleReadyForQuery.bind(this))
  163. con.on('notice', this._handleNotice.bind(this))
  164. con.on('rowDescription', this._handleRowDescription.bind(this))
  165. con.on('dataRow', this._handleDataRow.bind(this))
  166. con.on('portalSuspended', this._handlePortalSuspended.bind(this))
  167. con.on('emptyQuery', this._handleEmptyQuery.bind(this))
  168. con.on('commandComplete', this._handleCommandComplete.bind(this))
  169. con.on('parseComplete', this._handleParseComplete.bind(this))
  170. con.on('copyInResponse', this._handleCopyInResponse.bind(this))
  171. con.on('copyData', this._handleCopyData.bind(this))
  172. con.on('notification', this._handleNotification.bind(this))
  173. }
  174. // TODO(bmc): deprecate pgpass "built in" integration since this.password can be a function
  175. // it can be supplied by the user if required - this is a breaking change!
  176. _checkPgPass(cb) {
  177. const con = this.connection
  178. if (typeof this.password === 'function') {
  179. this._Promise
  180. .resolve()
  181. .then(() => this.password())
  182. .then((pass) => {
  183. if (pass !== undefined) {
  184. if (typeof pass !== 'string') {
  185. con.emit('error', new TypeError('Password must be a string'))
  186. return
  187. }
  188. this.connectionParameters.password = this.password = pass
  189. } else {
  190. this.connectionParameters.password = this.password = null
  191. }
  192. cb()
  193. })
  194. .catch((err) => {
  195. con.emit('error', err)
  196. })
  197. } else if (this.password !== null) {
  198. cb()
  199. } else {
  200. try {
  201. const pgPass = require('pgpass')
  202. pgPass(this.connectionParameters, (pass) => {
  203. if (undefined !== pass) {
  204. this.connectionParameters.password = this.password = pass
  205. }
  206. cb()
  207. })
  208. } catch (e) {
  209. this.emit('error', e)
  210. }
  211. }
  212. }
  213. _handleAuthCleartextPassword(msg) {
  214. this._checkPgPass(() => {
  215. this.connection.password(this.password)
  216. })
  217. }
  218. _handleAuthMD5Password(msg) {
  219. this._checkPgPass(async () => {
  220. try {
  221. const hashedPassword = await crypto.postgresMd5PasswordHash(this.user, this.password, msg.salt)
  222. this.connection.password(hashedPassword)
  223. } catch (e) {
  224. this.emit('error', e)
  225. }
  226. })
  227. }
  228. _handleAuthSASL(msg) {
  229. this._checkPgPass(() => {
  230. try {
  231. this.saslSession = sasl.startSession(msg.mechanisms)
  232. this.connection.sendSASLInitialResponseMessage(this.saslSession.mechanism, this.saslSession.response)
  233. } catch (err) {
  234. this.connection.emit('error', err)
  235. }
  236. })
  237. }
  238. async _handleAuthSASLContinue(msg) {
  239. try {
  240. await sasl.continueSession(this.saslSession, this.password, msg.data)
  241. this.connection.sendSCRAMClientFinalMessage(this.saslSession.response)
  242. } catch (err) {
  243. this.connection.emit('error', err)
  244. }
  245. }
  246. _handleAuthSASLFinal(msg) {
  247. try {
  248. sasl.finalizeSession(this.saslSession, msg.data)
  249. this.saslSession = null
  250. } catch (err) {
  251. this.connection.emit('error', err)
  252. }
  253. }
  254. _handleBackendKeyData(msg) {
  255. this.processID = msg.processID
  256. this.secretKey = msg.secretKey
  257. }
  258. _handleReadyForQuery(msg) {
  259. if (this._connecting) {
  260. this._connecting = false
  261. this._connected = true
  262. clearTimeout(this.connectionTimeoutHandle)
  263. // process possible callback argument to Client#connect
  264. if (this._connectionCallback) {
  265. this._connectionCallback(null, this)
  266. // remove callback for proper error handling
  267. // after the connect event
  268. this._connectionCallback = null
  269. }
  270. this.emit('connect')
  271. }
  272. const { activeQuery } = this
  273. this.activeQuery = null
  274. this.readyForQuery = true
  275. if (activeQuery) {
  276. activeQuery.handleReadyForQuery(this.connection)
  277. }
  278. this._pulseQueryQueue()
  279. }
  280. // if we receieve an error event or error message
  281. // during the connection process we handle it here
  282. _handleErrorWhileConnecting(err) {
  283. if (this._connectionError) {
  284. // TODO(bmc): this is swallowing errors - we shouldn't do this
  285. return
  286. }
  287. this._connectionError = true
  288. clearTimeout(this.connectionTimeoutHandle)
  289. if (this._connectionCallback) {
  290. return this._connectionCallback(err)
  291. }
  292. this.emit('error', err)
  293. }
  294. // if we're connected and we receive an error event from the connection
  295. // this means the socket is dead - do a hard abort of all queries and emit
  296. // the socket error on the client as well
  297. _handleErrorEvent(err) {
  298. if (this._connecting) {
  299. return this._handleErrorWhileConnecting(err)
  300. }
  301. this._queryable = false
  302. this._errorAllQueries(err)
  303. this.emit('error', err)
  304. }
  305. // handle error messages from the postgres backend
  306. _handleErrorMessage(msg) {
  307. if (this._connecting) {
  308. return this._handleErrorWhileConnecting(msg)
  309. }
  310. const activeQuery = this.activeQuery
  311. if (!activeQuery) {
  312. this._handleErrorEvent(msg)
  313. return
  314. }
  315. this.activeQuery = null
  316. activeQuery.handleError(msg, this.connection)
  317. }
  318. _handleRowDescription(msg) {
  319. // delegate rowDescription to active query
  320. this.activeQuery.handleRowDescription(msg)
  321. }
  322. _handleDataRow(msg) {
  323. // delegate dataRow to active query
  324. this.activeQuery.handleDataRow(msg)
  325. }
  326. _handlePortalSuspended(msg) {
  327. // delegate portalSuspended to active query
  328. this.activeQuery.handlePortalSuspended(this.connection)
  329. }
  330. _handleEmptyQuery(msg) {
  331. // delegate emptyQuery to active query
  332. this.activeQuery.handleEmptyQuery(this.connection)
  333. }
  334. _handleCommandComplete(msg) {
  335. // delegate commandComplete to active query
  336. this.activeQuery.handleCommandComplete(msg, this.connection)
  337. }
  338. _handleParseComplete(msg) {
  339. // if a prepared statement has a name and properly parses
  340. // we track that its already been executed so we don't parse
  341. // it again on the same client
  342. if (this.activeQuery.name) {
  343. this.connection.parsedStatements[this.activeQuery.name] = this.activeQuery.text
  344. }
  345. }
  346. _handleCopyInResponse(msg) {
  347. this.activeQuery.handleCopyInResponse(this.connection)
  348. }
  349. _handleCopyData(msg) {
  350. this.activeQuery.handleCopyData(msg, this.connection)
  351. }
  352. _handleNotification(msg) {
  353. this.emit('notification', msg)
  354. }
  355. _handleNotice(msg) {
  356. this.emit('notice', msg)
  357. }
  358. getStartupConf() {
  359. var params = this.connectionParameters
  360. var data = {
  361. user: params.user,
  362. database: params.database,
  363. }
  364. var appName = params.application_name || params.fallback_application_name
  365. if (appName) {
  366. data.application_name = appName
  367. }
  368. if (params.replication) {
  369. data.replication = '' + params.replication
  370. }
  371. if (params.statement_timeout) {
  372. data.statement_timeout = String(parseInt(params.statement_timeout, 10))
  373. }
  374. if (params.lock_timeout) {
  375. data.lock_timeout = String(parseInt(params.lock_timeout, 10))
  376. }
  377. if (params.idle_in_transaction_session_timeout) {
  378. data.idle_in_transaction_session_timeout = String(parseInt(params.idle_in_transaction_session_timeout, 10))
  379. }
  380. if (params.options) {
  381. data.options = params.options
  382. }
  383. return data
  384. }
  385. cancel(client, query) {
  386. if (client.activeQuery === query) {
  387. var con = this.connection
  388. if (this.host && this.host.indexOf('/') === 0) {
  389. con.connect(this.host + '/.s.PGSQL.' + this.port)
  390. } else {
  391. con.connect(this.port, this.host)
  392. }
  393. // once connection is established send cancel message
  394. con.on('connect', function () {
  395. con.cancel(client.processID, client.secretKey)
  396. })
  397. } else if (client.queryQueue.indexOf(query) !== -1) {
  398. client.queryQueue.splice(client.queryQueue.indexOf(query), 1)
  399. }
  400. }
  401. setTypeParser(oid, format, parseFn) {
  402. return this._types.setTypeParser(oid, format, parseFn)
  403. }
  404. getTypeParser(oid, format) {
  405. return this._types.getTypeParser(oid, format)
  406. }
  407. // escapeIdentifier and escapeLiteral moved to utility functions & exported
  408. // on PG
  409. // re-exported here for backwards compatibility
  410. escapeIdentifier(str) {
  411. return utils.escapeIdentifier(str)
  412. }
  413. escapeLiteral(str) {
  414. return utils.escapeLiteral(str)
  415. }
  416. _pulseQueryQueue() {
  417. if (this.readyForQuery === true) {
  418. this.activeQuery = this.queryQueue.shift()
  419. if (this.activeQuery) {
  420. this.readyForQuery = false
  421. this.hasExecuted = true
  422. const queryError = this.activeQuery.submit(this.connection)
  423. if (queryError) {
  424. process.nextTick(() => {
  425. this.activeQuery.handleError(queryError, this.connection)
  426. this.readyForQuery = true
  427. this._pulseQueryQueue()
  428. })
  429. }
  430. } else if (this.hasExecuted) {
  431. this.activeQuery = null
  432. this.emit('drain')
  433. }
  434. }
  435. }
  436. query(config, values, callback) {
  437. // can take in strings, config object or query object
  438. var query
  439. var result
  440. var readTimeout
  441. var readTimeoutTimer
  442. var queryCallback
  443. if (config === null || config === undefined) {
  444. throw new TypeError('Client was passed a null or undefined query')
  445. } else if (typeof config.submit === 'function') {
  446. readTimeout = config.query_timeout || this.connectionParameters.query_timeout
  447. result = query = config
  448. if (typeof values === 'function') {
  449. query.callback = query.callback || values
  450. }
  451. } else {
  452. readTimeout = this.connectionParameters.query_timeout
  453. query = new Query(config, values, callback)
  454. if (!query.callback) {
  455. result = new this._Promise((resolve, reject) => {
  456. query.callback = (err, res) => (err ? reject(err) : resolve(res))
  457. }).catch(err => {
  458. // replace the stack trace that leads to `TCP.onStreamRead` with one that leads back to the
  459. // application that created the query
  460. Error.captureStackTrace(err);
  461. throw err;
  462. })
  463. }
  464. }
  465. if (readTimeout) {
  466. queryCallback = query.callback
  467. readTimeoutTimer = setTimeout(() => {
  468. var error = new Error('Query read timeout')
  469. process.nextTick(() => {
  470. query.handleError(error, this.connection)
  471. })
  472. queryCallback(error)
  473. // we already returned an error,
  474. // just do nothing if query completes
  475. query.callback = () => {}
  476. // Remove from queue
  477. var index = this.queryQueue.indexOf(query)
  478. if (index > -1) {
  479. this.queryQueue.splice(index, 1)
  480. }
  481. this._pulseQueryQueue()
  482. }, readTimeout)
  483. query.callback = (err, res) => {
  484. clearTimeout(readTimeoutTimer)
  485. queryCallback(err, res)
  486. }
  487. }
  488. if (this.binary && !query.binary) {
  489. query.binary = true
  490. }
  491. if (query._result && !query._result._types) {
  492. query._result._types = this._types
  493. }
  494. if (!this._queryable) {
  495. process.nextTick(() => {
  496. query.handleError(new Error('Client has encountered a connection error and is not queryable'), this.connection)
  497. })
  498. return result
  499. }
  500. if (this._ending) {
  501. process.nextTick(() => {
  502. query.handleError(new Error('Client was closed and is not queryable'), this.connection)
  503. })
  504. return result
  505. }
  506. this.queryQueue.push(query)
  507. this._pulseQueryQueue()
  508. return result
  509. }
  510. ref() {
  511. this.connection.ref()
  512. }
  513. unref() {
  514. this.connection.unref()
  515. }
  516. end(cb) {
  517. this._ending = true
  518. // if we have never connected, then end is a noop, callback immediately
  519. if (!this.connection._connecting || this._ended) {
  520. if (cb) {
  521. cb()
  522. } else {
  523. return this._Promise.resolve()
  524. }
  525. }
  526. if (this.activeQuery || !this._queryable) {
  527. // if we have an active query we need to force a disconnect
  528. // on the socket - otherwise a hung query could block end forever
  529. this.connection.stream.destroy()
  530. } else {
  531. this.connection.end()
  532. }
  533. if (cb) {
  534. this.connection.once('end', cb)
  535. } else {
  536. return new this._Promise((resolve) => {
  537. this.connection.once('end', resolve)
  538. })
  539. }
  540. }
  541. }
  542. // expose a Query constructor
  543. Client.Query = Query
  544. module.exports = Client