UNPKG

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