Skip to content

Commit

Permalink
removed clientId from userAgent string
Browse files Browse the repository at this point in the history
Signed-off-by: Shivam Raj <shivam.raj@databricks.com>
  • Loading branch information
shivam2680 committed Feb 7, 2025
1 parent b755e62 commit 962f9b0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- uses: actions/checkout@v3
- name: Set up Python 3.10 for Node 14
if: ${{ matrix.node-version == '14' }}
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Cache node modules
uses: actions/cache@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion lib/DBSQLClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
};
}
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/buildUserAgentString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(): string {
const extra = [getNodeVersion(), getOperatingSystemVersion()].filter(Boolean);
return `${productName}/${packageVersion} (${extra.join('; ')})`;
}
34 changes: 15 additions & 19 deletions tests/unit/utils/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ const progressUpdateResponseStub: TProgressUpdateResp = {
describe('buildUserAgentString', () => {
// It should follow https://www.rfc-editor.org/rfc/rfc7231#section-5.5.3 and
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent
// UserAgent ::= <ProductName> '/' <ProductVersion> '(' <Comment> ')'
// where:
// <ProductName> is "NodejsDatabricksSqlConnector"
// <ProductVersion> is three period-separated digits (optionally with a suffix)
// <Comment> is "Node.js <NodeJsVersion>; <OSPlatform> <OSVersion>"
//
// UserAgent ::= <ProductName> '/' <ProductVersion> '(' <Comment> ')'
// ProductName ::= 'NodejsDatabricksSqlConnector'
// <Comment> ::= [ <ClientId> ';' ] 'Node.js' <NodeJsVersion> ';' <OSPlatform> <OSVersion>
//
// Examples:
// - with <ClientId> provided: NodejsDatabricksSqlConnector/0.1.8-beta.1 (Client ID; Node.js 16.13.1; Darwin 21.5.0)
// - without <ClientId> provided: NodejsDatabricksSqlConnector/0.1.8-beta.1 (Node.js 16.13.1; Darwin 21.5.0)
// Example:
// - 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) {
// Prefix: 'NodejsDatabricksSqlConnector/'
// Version: three period-separated digits and optional suffix
const re =
Expand All @@ -36,20 +36,16 @@ describe('buildUserAgentString', () => {

const { comment } = match?.groups ?? {};

expect(comment.split(';').length).to.be.gte(2); // at least Node and OS version should be there
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 (clientId) {
expect(comment.trim()).to.satisfy((s: string) => s.startsWith(`${clientId};`));
}
// First part should start with "Node.js" followed by a version number.
expect(parts[0]).to.match(/^Node\.js\s+\d+\.\d+\.\d+/);
// Second part should represent the OS platform (a word) and OS version.
expect(parts[1]).to.match(/^\w+/);
}

it('matches pattern with clientId', () => {
const clientId = 'Some Client ID';
const ua = buildUserAgentString(clientId);
checkUserAgentString(ua, clientId);
});

it('matches pattern without clientId', () => {
it('matches pattern', () => {
const ua = buildUserAgentString();
checkUserAgentString(ua);
});
Expand Down

0 comments on commit 962f9b0

Please sign in to comment.