index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict'
  2. var url = require('url')
  3. var fs = require('fs')
  4. //Parse method copied from https://github.com/brianc/node-postgres
  5. //Copyright (c) 2010-2014 Brian Carlson (brian.m.carlson@gmail.com)
  6. //MIT License
  7. //parses a connection string
  8. function parse(str) {
  9. //unix socket
  10. if (str.charAt(0) === '/') {
  11. var config = str.split(' ')
  12. return { host: config[0], database: config[1] }
  13. }
  14. // url parse expects spaces encoded as %20
  15. var result = url.parse(
  16. / |%[^a-f0-9]|%[a-f0-9][^a-f0-9]/i.test(str) ? encodeURI(str).replace(/\%25(\d\d)/g, '%$1') : str,
  17. true
  18. )
  19. var config = result.query
  20. for (var k in config) {
  21. if (Array.isArray(config[k])) {
  22. config[k] = config[k][config[k].length - 1]
  23. }
  24. }
  25. var auth = (result.auth || ':').split(':')
  26. config.user = auth[0]
  27. config.password = auth.splice(1).join(':')
  28. config.port = result.port
  29. if (result.protocol == 'socket:') {
  30. config.host = decodeURI(result.pathname)
  31. config.database = result.query.db
  32. config.client_encoding = result.query.encoding
  33. return config
  34. }
  35. if (!config.host) {
  36. // Only set the host if there is no equivalent query param.
  37. config.host = result.hostname
  38. }
  39. // If the host is missing it might be a URL-encoded path to a socket.
  40. var pathname = result.pathname
  41. if (!config.host && pathname && /^%2f/i.test(pathname)) {
  42. var pathnameSplit = pathname.split('/')
  43. config.host = decodeURIComponent(pathnameSplit[0])
  44. pathname = pathnameSplit.splice(1).join('/')
  45. }
  46. // result.pathname is not always guaranteed to have a '/' prefix (e.g. relative urls)
  47. // only strip the slash if it is present.
  48. if (pathname && pathname.charAt(0) === '/') {
  49. pathname = pathname.slice(1) || null
  50. }
  51. config.database = pathname && decodeURI(pathname)
  52. if (config.ssl === 'true' || config.ssl === '1') {
  53. config.ssl = true
  54. }
  55. if (config.ssl === '0') {
  56. config.ssl = false
  57. }
  58. if (config.sslcert || config.sslkey || config.sslrootcert || config.sslmode) {
  59. config.ssl = {}
  60. }
  61. if (config.sslcert) {
  62. config.ssl.cert = fs.readFileSync(config.sslcert).toString()
  63. }
  64. if (config.sslkey) {
  65. config.ssl.key = fs.readFileSync(config.sslkey).toString()
  66. }
  67. if (config.sslrootcert) {
  68. config.ssl.ca = fs.readFileSync(config.sslrootcert).toString()
  69. }
  70. switch (config.sslmode) {
  71. case 'disable': {
  72. config.ssl = false
  73. break
  74. }
  75. case 'prefer':
  76. case 'require':
  77. case 'verify-ca':
  78. case 'verify-full': {
  79. break
  80. }
  81. case 'no-verify': {
  82. config.ssl.rejectUnauthorized = false
  83. break
  84. }
  85. }
  86. return config
  87. }
  88. module.exports = parse
  89. parse.parse = parse