Skip to content

Commit 538bf7a

Browse files
authored
renamed clientId to userAgentHeader in connect args (#281)
Signed-off-by: Shivam Raj <[email protected]>
1 parent 771fb3b commit 538bf7a

File tree

5 files changed

+24
-17
lines changed

5 files changed

+24
-17
lines changed

examples/repl

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ async function initClient({ host, endpointId, token }) {
1515
host,
1616
path: `/sql/2.0/warehouses/${endpointId}`,
1717
token,
18-
clientId: 'REPL',
18+
userAgentEntry: 'REPL',
1919
});
2020
}
2121

lib/DBSQLClient.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export default class DBSQLClient extends EventEmitter implements IDBSQLClient, I
111111
socketTimeout: options.socketTimeout,
112112
proxy: options.proxy,
113113
headers: {
114-
'User-Agent': buildUserAgentString(),
114+
'User-Agent': buildUserAgentString(options.userAgentEntry),
115115
},
116116
};
117117
}

lib/contracts/IDBSQLClient.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export type ConnectionOptions = {
3030
host: string;
3131
port?: number;
3232
path: string;
33-
clientId?: string;
33+
userAgentEntry?: string;
3434
socketTimeout?: number;
3535
proxy?: ProxyOptions;
3636
} & AuthOptions;

lib/utils/buildUserAgentString.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function getOperatingSystemVersion(): string {
1111
return `${os.type()} ${os.release()}`;
1212
}
1313

14-
export default function buildUserAgentString(): string {
15-
const extra = [getNodeVersion(), getOperatingSystemVersion()].filter(Boolean);
14+
export default function buildUserAgentString(userAgentEntry?: string): string {
15+
const extra = [userAgentEntry, getNodeVersion(), getOperatingSystemVersion()].filter(Boolean);
1616
return `${productName}/${packageVersion} (${extra.join('; ')})`;
1717
}

tests/unit/utils/utils.test.ts

+19-12
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@ const progressUpdateResponseStub: TProgressUpdateResp = {
1717
describe('buildUserAgentString', () => {
1818
// It should follow https://www.rfc-editor.org/rfc/rfc7231#section-5.5.3 and
1919
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent
20-
// UserAgent ::= <ProductName> '/' <ProductVersion> '(' <Comment> ')'
20+
//
21+
// UserAgent ::= <ProductName> '/' <ProductVersion> '(' <Comment> ')'
2122
// where:
22-
// <ProductName> is "NodejsDatabricksSqlConnector"
23-
// <ProductVersion> is three period-separated digits (optionally with a suffix)
24-
// <Comment> is "Node.js <NodeJsVersion>; <OSPlatform> <OSVersion>"
23+
// ProductName ::= 'NodejsDatabricksSqlConnector'
24+
// ProductVersion ::= three period-separated digits
25+
// <Comment> ::= [ <userAgentEntry> ';' ] 'Node.js' <NodeJsVersion> ';' <OSPlatform> <OSVersion>
2526
//
26-
// Example:
27-
// - NodejsDatabricksSqlConnector/0.1.8-beta.1 (Node.js 16.13.1; Darwin 21.5.0)
27+
// Examples:
28+
// - with <userAgentEntry> provided: NodejsDatabricksSqlConnector/0.1.8-beta.1 (<userAgentEntry>; Node.js 16.13.1; Darwin 21.5.0)
29+
// - without <userAgentEntry> provided: NodejsDatabricksSqlConnector/0.1.8-beta.1 (Node.js 16.13.1; Darwin 21.5.0)
2830

29-
function checkUserAgentString(ua: string) {
31+
function checkUserAgentString(ua: string, userAgentEntry?: string) {
3032
// Prefix: 'NodejsDatabricksSqlConnector/'
3133
// Version: three period-separated digits and optional suffix
3234
const re =
@@ -39,13 +41,18 @@ describe('buildUserAgentString', () => {
3941
const parts = comment.split(';').map((s) => s.trim());
4042
expect(parts.length).to.be.gte(2); // at least Node and OS version should be there
4143

42-
// First part should start with "Node.js" followed by a version number.
43-
expect(parts[0]).to.match(/^Node\.js\s+\d+\.\d+\.\d+/);
44-
// Second part should represent the OS platform (a word) and OS version.
45-
expect(parts[1]).to.match(/^\w+/);
44+
if (userAgentEntry) {
45+
expect(comment.trim()).to.satisfy((s: string) => s.startsWith(`${userAgentEntry};`));
46+
}
4647
}
4748

48-
it('matches pattern', () => {
49+
it('matches pattern with userAgentEntry', () => {
50+
const userAgentEntry = 'Some user agent';
51+
const ua = buildUserAgentString(userAgentEntry);
52+
checkUserAgentString(ua, userAgentEntry);
53+
});
54+
55+
it('matches pattern without userAgentEntry', () => {
4956
const ua = buildUserAgentString();
5057
checkUserAgentString(ua);
5158
});

0 commit comments

Comments
 (0)