stream.js 753 B

12345678910111213141516171819202122232425262728
  1. /**
  2. * Get a socket stream compatible with the current runtime environment.
  3. * @returns {Duplex}
  4. */
  5. module.exports.getStream = function getStream(ssl) {
  6. const net = require('net')
  7. if (typeof net.Socket === 'function') {
  8. return new net.Socket()
  9. } else {
  10. const { CloudflareSocket } = require('pg-cloudflare')
  11. return new CloudflareSocket(ssl)
  12. }
  13. }
  14. /**
  15. * Get a TLS secured socket, compatible with the current environment,
  16. * using the socket and other settings given in `options`.
  17. * @returns {Duplex}
  18. */
  19. module.exports.getSecureStream = function getSecureStream(options) {
  20. var tls = require('tls')
  21. if (tls.connect) {
  22. return tls.connect(options)
  23. } else {
  24. options.socket.startTls(options)
  25. return options.socket
  26. }
  27. }