-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathTcpConnection.ts
58 lines (47 loc) · 1.82 KB
/
TcpConnection.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const thrift = require('thrift');
const ThriftConnection = thrift.Connection;
import IConnectionOptions from "../contracts/IConnectionOptions";
import TlsTransport from "../transports/TlsTransport";
import TcpTransport from "../transports/TcpTransport";
import IAuthentication from "../contracts/IAuthentication";
import IThriftConnection from "../contracts/IThriftConnection";
import ITransport from "../contracts/ITransport";
import IConnectionProvider from "../contracts/IConnectionProvider";
export default class TcpConnection implements IConnectionProvider, IThriftConnection {
private connection: any;
connect(options: IConnectionOptions, authProvider: IAuthentication): Promise<IThriftConnection> {
const transport = options.options?.ssl
? new TlsTransport(options.host, options.port, { ...(options?.options || {}) })
: new TcpTransport(options.host, options.port);
return authProvider.authenticate(transport).then(transport => {
this.connection = this.createConnection(transport, options);
return this;
});
}
getConnection() {
return this.connection;
}
isConnected(): boolean {
if (!this.connection) {
return false;
} else {
return this.connection.connected;
}
}
private createConnection(transport: ITransport, options: IConnectionOptions): any {
const stream = transport.getTransport();
const instance = new ThriftConnection(
stream,
{
transport: thrift.TFramedTransport,
protocol: thrift.TBinaryProtocol,
...(options?.options || {}),
...transport.getOptions()
}
);
instance.host = options.host;
instance.port = options.port;
transport.emit('connect');
return instance;
}
}