index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. if (result.protocol == 'socket:') {
  34. config.host = decodeURI(result.pathname)
  35. config.database = result.searchParams.get('db')
  36. config.client_encoding = result.searchParams.get('encoding')
  37. return config
  38. }
  39. const hostname = dummyHost ? '' : result.hostname
  40. if (!config.host) {
  41. // Only set the host if there is no equivalent query param.
  42. config.host = decodeURIComponent(hostname)
  43. } else if (hostname && /^%2f/i.test(hostname)) {
  44. // Only prepend the hostname to the pathname if it is not a URL encoded Unix socket host.
  45. result.pathname = hostname + result.pathname
  46. }
  47. if (!config.port) {
  48. // Only set the port if there is no equivalent query param.
  49. config.port = result.port
  50. }
  51. const pathname = result.pathname.slice(1) || null
  52. config.database = pathname ? decodeURI(pathname) : null
  53. if (config.ssl === 'true' || config.ssl === '1') {
  54. config.ssl = true
  55. }
  56. if (config.ssl === '0') {
  57. config.ssl = false
  58. }
  59. if (config.sslcert || config.sslkey || config.sslrootcert || config.sslmode) {
  60. config.ssl = {}
  61. }
  62. // Only try to load fs if we expect to read from the disk
  63. const fs = config.sslcert || config.sslkey || config.sslrootcert ? require('fs') : null
  64. if (config.sslcert) {
  65. config.ssl.cert = fs.readFileSync(config.sslcert).toString()
  66. }
  67. if (config.sslkey) {
  68. config.ssl.key = fs.readFileSync(config.sslkey).toString()
  69. }
  70. if (config.sslrootcert) {
  71. config.ssl.ca = fs.readFileSync(config.sslrootcert).toString()
  72. }
  73. switch (config.sslmode) {
  74. case 'disable': {
  75. config.ssl = false
  76. break
  77. }
  78. case 'prefer':
  79. case 'require':
  80. case 'verify-ca':
  81. case 'verify-full': {
  82. break
  83. }
  84. case 'no-verify': {
  85. config.ssl.rejectUnauthorized = false
  86. break
  87. }
  88. }
  89. return config
  90. }
  91. module.exports = parse
  92. parse.parse = parse