client.js 18 KB

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