Skip to content

Commit 0a2bdb4

Browse files
Refactoring and cleanup in connection-related code (#149)
* Cleanup DBSQLClient code; remove redundant and no longer needed NoSaslAuthentication Signed-off-by: Levko Kravets <levko.ne@gmail.com> * Make HttpTransport and related types more type-safe, remove redundant code Signed-off-by: Levko Kravets <levko.ne@gmail.com> * Remove some leftover code Signed-off-by: Levko Kravets <levko.ne@gmail.com> * Update tests Signed-off-by: Levko Kravets <levko.ne@gmail.com> * Pass User-Agent through HttpConnection instead of auth provider Signed-off-by: Levko Kravets <levko.ne@gmail.com> --------- Signed-off-by: Levko Kravets <levko.ne@gmail.com>
1 parent 9b85557 commit 0a2bdb4

File tree

13 files changed

+176
-122
lines changed

13 files changed

+176
-122
lines changed

lib/DBSQLClient.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import IDBSQLSession from './contracts/IDBSQLSession';
1111
import IThriftConnection from './connection/contracts/IThriftConnection';
1212
import IConnectionProvider from './connection/contracts/IConnectionProvider';
1313
import IAuthentication from './connection/contracts/IAuthentication';
14-
import NoSaslAuthentication from './connection/auth/NoSaslAuthentication';
1514
import HttpConnection from './connection/connections/HttpConnection';
1615
import IConnectionOptions from './connection/contracts/IConnectionOptions';
1716
import Status from './dto/Status';
@@ -48,16 +47,13 @@ export default class DBSQLClient extends EventEmitter implements IDBSQLClient {
4847

4948
private connectionProvider: IConnectionProvider;
5049

51-
private authProvider: IAuthentication;
52-
5350
private readonly logger: IDBSQLLogger;
5451

5552
private readonly thrift = thrift;
5653

5754
constructor(options?: ClientOptions) {
5855
super();
5956
this.connectionProvider = new HttpConnection();
60-
this.authProvider = new NoSaslAuthentication();
6157
this.logger = options?.logger || new DBSQLLogger();
6258
this.client = null;
6359
this.connection = null;
@@ -73,6 +69,9 @@ export default class DBSQLClient extends EventEmitter implements IDBSQLClient {
7369
path: prependSlash(path),
7470
https: true,
7571
...otherOptions,
72+
headers: {
73+
'User-Agent': buildUserAgentString(options.clientId),
74+
},
7675
},
7776
};
7877
}
@@ -87,17 +86,14 @@ export default class DBSQLClient extends EventEmitter implements IDBSQLClient {
8786
* const session = client.connect({host, path, token});
8887
*/
8988
public async connect(options: ConnectionOptions, authProvider?: IAuthentication): Promise<IDBSQLClient> {
90-
this.authProvider =
89+
authProvider =
9190
authProvider ||
9291
new PlainHttpAuthentication({
9392
username: 'token',
9493
password: options.token,
95-
headers: {
96-
'User-Agent': buildUserAgentString(options.clientId),
97-
},
9894
});
9995

100-
this.connection = await this.connectionProvider.connect(this.getConnectionOptions(options), this.authProvider);
96+
this.connection = await this.connectionProvider.connect(this.getConnectionOptions(options), authProvider);
10197

10298
this.client = this.thrift.createClient(TCLIService, this.connection.getConnection());
10399

lib/connection/auth/NoSaslAuthentication.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,30 @@
1+
import { HttpHeaders } from 'thrift';
12
import IAuthentication from '../contracts/IAuthentication';
2-
import ITransport from '../contracts/ITransport';
3-
import { AuthOptions } from '../types/AuthOptions';
3+
import HttpTransport from '../transports/HttpTransport';
44

5-
type HttpAuthOptions = AuthOptions & {
6-
headers?: object;
7-
};
5+
interface PlainHttpAuthenticationOptions {
6+
username?: string;
7+
password?: string;
8+
headers?: HttpHeaders;
9+
}
810

911
export default class PlainHttpAuthentication implements IAuthentication {
10-
private username: string;
12+
private readonly username: string;
1113

12-
private password: string;
14+
private readonly password: string;
1315

14-
private headers: object;
16+
private readonly headers: HttpHeaders;
1517

16-
constructor(options: HttpAuthOptions) {
18+
constructor(options: PlainHttpAuthenticationOptions) {
1719
this.username = options?.username || 'anonymous';
18-
this.password = options?.password !== undefined ? options?.password : 'anonymous';
20+
this.password = options?.password ?? 'anonymous';
1921
this.headers = options?.headers || {};
2022
}
2123

22-
authenticate(transport: ITransport): Promise<ITransport> {
23-
transport.setOptions('headers', {
24+
public async authenticate(transport: HttpTransport): Promise<void> {
25+
transport.updateHeaders({
2426
...this.headers,
25-
Authorization: this.getToken(this.username, this.password),
27+
Authorization: `Bearer ${this.password}`,
2628
});
27-
28-
return Promise.resolve(transport);
29-
}
30-
31-
private getToken(username: string, password: string): string {
32-
return `Bearer ${password}`;
3329
}
3430
}

lib/connection/auth/helpers/SaslPackageFactory.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import ITransport from './ITransport';
1+
import HttpTransport from '../transports/HttpTransport';
22

33
export default interface IAuthentication {
4-
authenticate(connection: ITransport): Promise<ITransport>;
4+
authenticate(transport: HttpTransport): Promise<void>;
55
}

lib/connection/contracts/IConnectionOptions.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { HttpHeaders } from 'thrift';
2+
13
export type Options = {
24
socketTimeout?: number;
35
username?: string;
@@ -9,7 +11,7 @@ export type Options = {
911
retry_max_delay?: number;
1012
connect_timeout?: number;
1113
timeout?: number;
12-
headers?: object;
14+
headers?: HttpHeaders;
1315
path?: string;
1416
ca?: Buffer | string;
1517
cert?: Buffer | string;

lib/connection/contracts/ITransport.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,56 @@
1-
import ITransport from '../contracts/ITransport';
1+
import { ConnectOptions, HttpHeaders } from 'thrift';
22

3-
export default class HttpTransport implements ITransport {
4-
private httpOptions: object;
3+
export default class HttpTransport {
4+
private options: ConnectOptions;
55

6-
constructor(httpOptions: object = {}) {
7-
this.httpOptions = httpOptions;
6+
constructor(options: ConnectOptions = {}) {
7+
this.options = { ...options };
88
}
99

10-
getTransport(): any {
11-
return this.httpOptions;
10+
public getOptions(): ConnectOptions {
11+
return this.options;
1212
}
1313

14-
setOptions(option: string, value: any) {
15-
this.httpOptions = {
16-
...this.httpOptions,
14+
public setOptions(options: ConnectOptions) {
15+
this.options = { ...options };
16+
}
17+
18+
public updateOptions(options: Partial<ConnectOptions>) {
19+
this.options = {
20+
...this.options,
21+
...options,
22+
};
23+
}
24+
25+
public getOption<K extends keyof ConnectOptions>(option: K): ConnectOptions[K] {
26+
return this.options[option];
27+
}
28+
29+
public setOption<K extends keyof ConnectOptions>(option: K, value: ConnectOptions[K]) {
30+
this.options = {
31+
...this.options,
1732
[option]: value,
1833
};
1934
}
2035

21-
getOptions(): object {
22-
return this.httpOptions;
36+
public getHeaders(): HttpHeaders {
37+
return this.options.headers ?? {};
38+
}
39+
40+
public setHeaders(headers: HttpHeaders) {
41+
this.options = {
42+
...this.options,
43+
headers: { ...headers },
44+
};
45+
}
46+
47+
public updateHeaders(headers: Partial<ConnectOptions['headers']>) {
48+
this.options = {
49+
...this.options,
50+
headers: {
51+
...this.options.headers,
52+
...headers,
53+
},
54+
};
2355
}
2456
}

lib/connection/types/AuthOptions.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

lib/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ import TCLIService_types from '../thrift/TCLIService_types';
44
import DBSQLClient from './DBSQLClient';
55
import DBSQLSession from './DBSQLSession';
66
import DBSQLLogger from './DBSQLLogger';
7-
import NoSaslAuthentication from './connection/auth/NoSaslAuthentication';
87
import PlainHttpAuthentication from './connection/auth/PlainHttpAuthentication';
98
import HttpConnection from './connection/connections/HttpConnection';
109
import { formatProgress } from './utils';
1110
import { LogLevel } from './contracts/IDBSQLLogger';
1211

1312
export const auth = {
14-
NoSaslAuthentication,
1513
PlainHttpAuthentication,
1614
};
1715

tests/unit/connection/auth/NoSaslAuthentication.test.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

tests/unit/connection/auth/PlainHttpAuthentication.test.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,15 @@ describe('PlainHttpAuthentication', () => {
2929
expect(auth.password).to.be.eq('');
3030
});
3131

32-
it('auth token must be set to header', () => {
32+
it('auth token must be set to header', async () => {
3333
const auth = new PlainHttpAuthentication();
3434
const transportMock = {
35-
setOptions(name, value) {
36-
expect(name).to.be.eq('headers');
37-
expect(value.Authorization).to.be.eq('Bearer anonymous');
35+
updateHeaders(headers) {
36+
expect(headers).to.deep.equal({
37+
Authorization: 'Bearer anonymous',
38+
});
3839
},
3940
};
40-
return auth.authenticate(transportMock).then((transport) => {
41-
expect(transport).to.be.eq(transportMock);
42-
});
41+
await auth.authenticate(transportMock); // it just should not fail
4342
});
4443
});

0 commit comments

Comments
 (0)