Skip to content

Commit 8bbf410

Browse files
uint0kravets-levko
andauthored
Support http agent for non https connections (#57)
* fix: Support http agent for non https connections Signed-off-by: Chen Zhou <i@uint0.dev> * Update lib/connection/connections/HttpConnection.ts Co-authored-by: Levko Kravets <levko.ne@gmail.com> Signed-off-by: Chen Zhou <i@uint0.dev> Co-authored-by: Levko Kravets <levko.ne@gmail.com>
1 parent fe3de50 commit 8bbf410

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

lib/connection/connections/HttpConnection.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import thrift from 'thrift';
22
import https from 'https';
33

4-
import { IncomingMessage } from 'http';
4+
import http, { IncomingMessage } from 'http';
55
import IThriftConnection from '../contracts/IThriftConnection';
66
import IConnectionProvider from '../contracts/IConnectionProvider';
77
import IConnectionOptions, { Options } from '../contracts/IConnectionOptions';
@@ -21,12 +21,13 @@ export default class HttpConnection implements IConnectionProvider, IThriftConne
2121
private connection: any;
2222

2323
connect(options: IConnectionOptions, authProvider: IAuthentication): Promise<IThriftConnection> {
24+
const Agent = options.options?.https ? https.Agent : http.Agent;
2425
const httpTransport = new HttpTransport({
2526
transport: thrift.TBufferedTransport,
2627
protocol: thrift.TBinaryProtocol,
2728
...options.options,
2829
nodeOptions: {
29-
agent: new https.Agent({
30+
agent: new Agent({
3031
keepAlive: true,
3132
maxSockets: 5,
3233
keepAliveMsecs: 10000,

tests/unit/connection/connections/HttpConnection.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const http = require('http');
12
const { expect } = require('chai');
23
const HttpConnection = require('../../../../').connections.HttpConnection;
34

@@ -161,4 +162,29 @@ describe('HttpConnection.connect', () => {
161162
expect(Object.keys(connection.thrift.options.nodeOptions)).to.be.deep.eq(['agent']);
162163
});
163164
});
165+
166+
it('should use a http agent if https is not enabled', () => {
167+
const connection = new HttpConnection();
168+
const authenticator = authProviderMock();
169+
const resultConnection = {
170+
responseCallback() {},
171+
};
172+
connection.thrift = thriftMock(resultConnection);
173+
174+
return connection
175+
.connect(
176+
{
177+
host: 'localhost',
178+
port: 10001,
179+
options: {
180+
https: false,
181+
path: '/hive',
182+
},
183+
},
184+
authenticator,
185+
)
186+
.then(() => {
187+
expect(connection.thrift.options.nodeOptions.agent).to.be.instanceOf(http.Agent);
188+
});
189+
});
164190
});

0 commit comments

Comments
 (0)