StandaloneConnector.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const net_1 = require("net");
  4. const tls_1 = require("tls");
  5. const utils_1 = require("../utils");
  6. const AbstractConnector_1 = require("./AbstractConnector");
  7. function isIIpcConnectionOptions(value) {
  8. return value.path;
  9. }
  10. exports.isIIpcConnectionOptions = isIIpcConnectionOptions;
  11. class StandaloneConnector extends AbstractConnector_1.default {
  12. constructor(options) {
  13. super(options.disconnectTimeout);
  14. this.options = options;
  15. }
  16. connect(_) {
  17. const { options } = this;
  18. this.connecting = true;
  19. let connectionOptions;
  20. if (isIIpcConnectionOptions(options)) {
  21. connectionOptions = {
  22. path: options.path,
  23. };
  24. }
  25. else {
  26. connectionOptions = {};
  27. if (options.port != null) {
  28. connectionOptions.port = options.port;
  29. }
  30. if (options.host != null) {
  31. connectionOptions.host = options.host;
  32. }
  33. if (options.family != null) {
  34. connectionOptions.family = options.family;
  35. }
  36. }
  37. if (options.tls) {
  38. Object.assign(connectionOptions, options.tls);
  39. }
  40. // TODO:
  41. // We use native Promise here since other Promise
  42. // implementation may use different schedulers that
  43. // cause issue when the stream is resolved in the
  44. // next tick.
  45. // Should use the provided promise in the next major
  46. // version and do not connect before resolved.
  47. return new Promise((resolve, reject) => {
  48. process.nextTick(() => {
  49. if (!this.connecting) {
  50. reject(new Error(utils_1.CONNECTION_CLOSED_ERROR_MSG));
  51. return;
  52. }
  53. try {
  54. if (options.tls) {
  55. this.stream = tls_1.connect(connectionOptions);
  56. }
  57. else {
  58. this.stream = net_1.createConnection(connectionOptions);
  59. }
  60. }
  61. catch (err) {
  62. reject(err);
  63. return;
  64. }
  65. this.stream.once("error", (err) => {
  66. this.firstError = err;
  67. });
  68. resolve(this.stream);
  69. });
  70. });
  71. }
  72. }
  73. exports.default = StandaloneConnector;