index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 'use strict'
  2. //Parse method copied from https://github.com/brianc/node-postgres
  3. //Copyright (c) 2010-2014 Brian Carlson (brian.m.carlson@gmail.com)
  4. //MIT License
  5. //parses a connection string
  6. function parse(str) {
  7. //unix socket
  8. if (str.charAt(0) === '/') {
  9. const config = str.split(' ')
  10. return { host: config[0], database: config[1] }
  11. }
  12. // Check for empty host in URL
  13. const config = {}
  14. let result
  15. let dummyHost = false
  16. if (/ |%[^a-f0-9]|%[a-f0-9][^a-f0-9]/i.test(str)) {
  17. // Ensure spaces are encoded as %20
  18. str = encodeURI(str).replace(/\%25(\d\d)/g, '%$1')
  19. }
  20. try {
  21. result = new URL(str, 'postgres://base')
  22. } catch (e) {
  23. // The URL is invalid so try again with a dummy host
  24. result = new URL(str.replace('@/', '@___DUMMY___/'), 'postgres://base')
  25. dummyHost = true
  26. }
  27. // We'd like to use Object.fromEntries() here but Node.js 10 does not support it
  28. for (const entry of result.searchParams.entries()) {
  29. config[entry[0]] = entry[1]
  30. }
  31. config.user = config.user || decodeURIComponent(result.username)
  32. config.password = config.password || decodeURIComponent(result.password)
  33. config.port = result.port
  34. if (result.protocol == 'socket:') {
  35. config.host = decodeURI(result.pathname)
  36. config.database = result.searchParams.get('db')
  37. config.client_encoding = result.searchParams.get('encoding')
  38. return config
  39. }
  40. const hostname = dummyHost ? '' : result.hostname
  41. if (!config.host) {
  42. // Only set the host if there is no equivalent query param.
  43. config.host = decodeURIComponent(hostname)
  44. } else if (hostname) {
  45. result.pathname = hostname + result.pathname
  46. }
  47. const pathname = result.pathname.slice(1) || null
  48. config.database = pathname ? decodeURI(pathname) : null
  49. if (config.ssl === 'true' || config.ssl === '1') {
  50. config.ssl = true
  51. }
  52. if (config.ssl === '0') {
  53. config.ssl = false
  54. }
  55. if (config.sslcert || config.sslkey || config.sslrootcert || config.sslmode) {
  56. config.ssl = {}
  57. }
  58. // Only try to load fs if we expect to read from the disk
  59. const fs = config.sslcert || config.sslkey || config.sslrootcert ? require('fs') : null
  60. if (config.sslcert) {
  61. config.ssl.cert = fs.readFileSync(config.sslcert).toString()
  62. }
  63. if (config.sslkey) {
  64. config.ssl.key = fs.readFileSync(config.sslkey).toString()
  65. }
  66. if (config.sslrootcert) {
  67. config.ssl.ca = fs.readFileSync(config.sslrootcert).toString()
  68. }
  69. switch (config.sslmode) {
  70. case 'disable': {
  71. config.ssl = false
  72. break
  73. }
  74. case 'prefer':
  75. case 'require':
  76. case 'verify-ca':
  77. case 'verify-full': {
  78. break
  79. }
  80. case 'no-verify': {
  81. config.ssl.rejectUnauthorized = false
  82. break
  83. }
  84. }
  85. return config
  86. }
  87. module.exports = parse
  88. parse.parse = parse