Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/repl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async function initClient({ host, endpointId, token }) {
host,
path: `/sql/2.0/warehouses/${endpointId}`,
token,
clientId: 'REPL',
userAgentEntry: 'REPL',
});
}

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(),
'User-Agent': buildUserAgentString(options.userAgentEntry),
},
};
}
Expand Down
2 changes: 1 addition & 1 deletion lib/contracts/IDBSQLClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export type ConnectionOptions = {
host: string;
port?: number;
path: string;
clientId?: string;
userAgentEntry?: string;
socketTimeout?: number;
proxy?: ProxyOptions;
} & AuthOptions;
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(): string {
const extra = [getNodeVersion(), getOperatingSystemVersion()].filter(Boolean);
export default function buildUserAgentString(userAgentEntry?: string): string {
const extra = [userAgentEntry, getNodeVersion(), getOperatingSystemVersion()].filter(Boolean);
return `${productName}/${packageVersion} (${extra.join('; ')})`;
}
31 changes: 19 additions & 12 deletions tests/unit/utils/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ 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> ')'
//
// 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>"
// ProductName ::= 'NodejsDatabricksSqlConnector'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit : include the where in comment

// ProductVersion ::= three period-separated digits
// <Comment> ::= [ <userAgentEntry> ';' ] 'Node.js' <NodeJsVersion> ';' <OSPlatform> <OSVersion>
//
// Example:
// - NodejsDatabricksSqlConnector/0.1.8-beta.1 (Node.js 16.13.1; Darwin 21.5.0)
// Examples:
// - with <userAgentEntry> provided: NodejsDatabricksSqlConnector/0.1.8-beta.1 (<userAgentEntry>; Node.js 16.13.1; Darwin 21.5.0)
// - without <userAgentEntry> provided: NodejsDatabricksSqlConnector/0.1.8-beta.1 (Node.js 16.13.1; Darwin 21.5.0)

function checkUserAgentString(ua: string) {
function checkUserAgentString(ua: string, userAgentEntry?: string) {
// Prefix: 'NodejsDatabricksSqlConnector/'
// Version: three period-separated digits and optional suffix
const re =
Expand All @@ -39,13 +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

// 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+/);
if (userAgentEntry) {
expect(comment.trim()).to.satisfy((s: string) => s.startsWith(`${userAgentEntry};`));
}
}

it('matches pattern', () => {
it('matches pattern with userAgentEntry', () => {
const userAgentEntry = 'Some user agent';
const ua = buildUserAgentString(userAgentEntry);
checkUserAgentString(ua, userAgentEntry);
});

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