client.js 18 KB

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