From a35f4686fc1e719b7cfda0262dd32b86e5eebdb7 Mon Sep 17 00:00:00 2001 From: Shivam Raj Date: Wed, 19 Feb 2025 01:26:13 +0530 Subject: [PATCH 1/2] renamed clientId to userAgentHeader in connect args --- examples/repl | 2 +- lib/DBSQLClient.ts | 2 +- lib/contracts/IDBSQLClient.ts | 2 +- lib/utils/buildUserAgentString.ts | 4 ++-- tests/unit/utils/utils.test.ts | 22 +++++++++++----------- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/examples/repl b/examples/repl index d214549c..effdccf1 100755 --- a/examples/repl +++ b/examples/repl @@ -15,7 +15,7 @@ async function initClient({ host, endpointId, token }) { host, path: `/sql/2.0/warehouses/${endpointId}`, token, - clientId: 'REPL', + userAgentHeader: 'REPL', }); } diff --git a/lib/DBSQLClient.ts b/lib/DBSQLClient.ts index d3d9427c..1e5a6d2a 100644 --- a/lib/DBSQLClient.ts +++ b/lib/DBSQLClient.ts @@ -111,7 +111,7 @@ export default class DBSQLClient extends EventEmitter implements IDBSQLClient, I socketTimeout: options.socketTimeout, proxy: options.proxy, headers: { - 'User-Agent': buildUserAgentString(options.clientId), + 'User-Agent': buildUserAgentString(options.userAgentHeader), }, }; } diff --git a/lib/contracts/IDBSQLClient.ts b/lib/contracts/IDBSQLClient.ts index 0c0cee89..cd5116ff 100644 --- a/lib/contracts/IDBSQLClient.ts +++ b/lib/contracts/IDBSQLClient.ts @@ -30,7 +30,7 @@ export type ConnectionOptions = { host: string; port?: number; path: string; - clientId?: string; + userAgentHeader?: string; socketTimeout?: number; proxy?: ProxyOptions; } & AuthOptions; diff --git a/lib/utils/buildUserAgentString.ts b/lib/utils/buildUserAgentString.ts index ce26fcfd..bc78735a 100644 --- a/lib/utils/buildUserAgentString.ts +++ b/lib/utils/buildUserAgentString.ts @@ -11,7 +11,7 @@ function getOperatingSystemVersion(): string { return `${os.type()} ${os.release()}`; } -export default function buildUserAgentString(clientId?: string): string { - const extra = [clientId, getNodeVersion(), getOperatingSystemVersion()].filter(Boolean); +export default function buildUserAgentString(userAgentHeader?: string): string { + const extra = [userAgentHeader, getNodeVersion(), getOperatingSystemVersion()].filter(Boolean); return `${productName}/${packageVersion} (${extra.join('; ')})`; } diff --git a/tests/unit/utils/utils.test.ts b/tests/unit/utils/utils.test.ts index f94e88c4..4868b180 100644 --- a/tests/unit/utils/utils.test.ts +++ b/tests/unit/utils/utils.test.ts @@ -20,13 +20,13 @@ describe('buildUserAgentString', () => { // // UserAgent ::= '/' '(' ')' // ProductName ::= 'NodejsDatabricksSqlConnector' - // ::= [ ';' ] 'Node.js' ';' + // ::= [ ';' ] 'Node.js' ';' // // Examples: - // - with provided: NodejsDatabricksSqlConnector/0.1.8-beta.1 (Client ID; Node.js 16.13.1; Darwin 21.5.0) - // - without provided: NodejsDatabricksSqlConnector/0.1.8-beta.1 (Node.js 16.13.1; Darwin 21.5.0) + // - with provided: NodejsDatabricksSqlConnector/0.1.8-beta.1 (Client ID; Node.js 16.13.1; Darwin 21.5.0) + // - without provided: NodejsDatabricksSqlConnector/0.1.8-beta.1 (Node.js 16.13.1; Darwin 21.5.0) - function checkUserAgentString(ua: string, clientId?: string) { + function checkUserAgentString(ua: string, userAgentHeader?: string) { // Prefix: 'NodejsDatabricksSqlConnector/' // Version: three period-separated digits and optional suffix const re = @@ -38,18 +38,18 @@ describe('buildUserAgentString', () => { expect(comment.split(';').length).to.be.gte(2); // at least Node and OS version should be there - if (clientId) { - expect(comment.trim()).to.satisfy((s: string) => s.startsWith(`${clientId};`)); + if (userAgentHeader) { + expect(comment.trim()).to.satisfy((s: string) => s.startsWith(`${userAgentHeader};`)); } } - it('matches pattern with clientId', () => { - const clientId = 'Some Client ID'; - const ua = buildUserAgentString(clientId); - checkUserAgentString(ua, clientId); + it('matches pattern with userAgentHeader', () => { + const userAgentHeader = 'Some Client ID'; + const ua = buildUserAgentString(userAgentHeader); + checkUserAgentString(ua, userAgentHeader); }); - it('matches pattern without clientId', () => { + it('matches pattern without userAgentHeader', () => { const ua = buildUserAgentString(); checkUserAgentString(ua); }); From 85a0e6d096fdd35f4750eb3e140fb8063fe7ab08 Mon Sep 17 00:00:00 2001 From: Shivam Raj Date: Wed, 19 Feb 2025 22:35:37 +0530 Subject: [PATCH 2/2] addressed comments --- examples/repl | 2 +- lib/DBSQLClient.ts | 2 +- lib/contracts/IDBSQLClient.ts | 2 +- lib/utils/buildUserAgentString.ts | 4 ++-- tests/unit/utils/utils.test.ts | 24 +++++++++++++----------- 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/examples/repl b/examples/repl index effdccf1..c546511b 100755 --- a/examples/repl +++ b/examples/repl @@ -15,7 +15,7 @@ async function initClient({ host, endpointId, token }) { host, path: `/sql/2.0/warehouses/${endpointId}`, token, - userAgentHeader: 'REPL', + userAgentEntry: 'REPL', }); } diff --git a/lib/DBSQLClient.ts b/lib/DBSQLClient.ts index 1e5a6d2a..ead39d9e 100644 --- a/lib/DBSQLClient.ts +++ b/lib/DBSQLClient.ts @@ -111,7 +111,7 @@ export default class DBSQLClient extends EventEmitter implements IDBSQLClient, I socketTimeout: options.socketTimeout, proxy: options.proxy, headers: { - 'User-Agent': buildUserAgentString(options.userAgentHeader), + 'User-Agent': buildUserAgentString(options.userAgentEntry), }, }; } diff --git a/lib/contracts/IDBSQLClient.ts b/lib/contracts/IDBSQLClient.ts index cd5116ff..73fedde1 100644 --- a/lib/contracts/IDBSQLClient.ts +++ b/lib/contracts/IDBSQLClient.ts @@ -30,7 +30,7 @@ export type ConnectionOptions = { host: string; port?: number; path: string; - userAgentHeader?: string; + userAgentEntry?: string; socketTimeout?: number; proxy?: ProxyOptions; } & AuthOptions; diff --git a/lib/utils/buildUserAgentString.ts b/lib/utils/buildUserAgentString.ts index bc78735a..1cbbf177 100644 --- a/lib/utils/buildUserAgentString.ts +++ b/lib/utils/buildUserAgentString.ts @@ -11,7 +11,7 @@ function getOperatingSystemVersion(): string { return `${os.type()} ${os.release()}`; } -export default function buildUserAgentString(userAgentHeader?: string): string { - const extra = [userAgentHeader, getNodeVersion(), getOperatingSystemVersion()].filter(Boolean); +export default function buildUserAgentString(userAgentEntry?: string): string { + const extra = [userAgentEntry, getNodeVersion(), getOperatingSystemVersion()].filter(Boolean); return `${productName}/${packageVersion} (${extra.join('; ')})`; } diff --git a/tests/unit/utils/utils.test.ts b/tests/unit/utils/utils.test.ts index fe42170d..6f0369b2 100644 --- a/tests/unit/utils/utils.test.ts +++ b/tests/unit/utils/utils.test.ts @@ -19,14 +19,16 @@ describe('buildUserAgentString', () => { // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent // // UserAgent ::= '/' '(' ')' + // where: // ProductName ::= 'NodejsDatabricksSqlConnector' - // ::= [ ';' ] 'Node.js' ';' + // ProductVersion ::= three period-separated digits + // ::= [ ';' ] 'Node.js' ';' // // Examples: - // - with provided: NodejsDatabricksSqlConnector/0.1.8-beta.1 (Client ID; Node.js 16.13.1; Darwin 21.5.0) - // - without provided: NodejsDatabricksSqlConnector/0.1.8-beta.1 (Node.js 16.13.1; Darwin 21.5.0) + // - with provided: NodejsDatabricksSqlConnector/0.1.8-beta.1 (; Node.js 16.13.1; Darwin 21.5.0) + // - without provided: NodejsDatabricksSqlConnector/0.1.8-beta.1 (Node.js 16.13.1; Darwin 21.5.0) - function checkUserAgentString(ua: string, userAgentHeader?: string) { + function checkUserAgentString(ua: string, userAgentEntry?: string) { // Prefix: 'NodejsDatabricksSqlConnector/' // Version: three period-separated digits and optional suffix const re = @@ -39,18 +41,18 @@ describe('buildUserAgentString', () => { const parts = comment.split(';').map((s) => s.trim()); expect(parts.length).to.be.gte(2); // at least Node and OS version should be there - if (userAgentHeader) { - expect(comment.trim()).to.satisfy((s: string) => s.startsWith(`${userAgentHeader};`)); + if (userAgentEntry) { + expect(comment.trim()).to.satisfy((s: string) => s.startsWith(`${userAgentEntry};`)); } } - it('matches pattern with userAgentHeader', () => { - const userAgentHeader = 'Some Client ID'; - const ua = buildUserAgentString(userAgentHeader); - checkUserAgentString(ua, userAgentHeader); + it('matches pattern with userAgentEntry', () => { + const userAgentEntry = 'Some user agent'; + const ua = buildUserAgentString(userAgentEntry); + checkUserAgentString(ua, userAgentEntry); }); - it('matches pattern without userAgentHeader', () => { + it('matches pattern without userAgentEntry', () => { const ua = buildUserAgentString(); checkUserAgentString(ua); });