From dd20563cd6686f90af5065737217a07c8e3ace3b Mon Sep 17 00:00:00 2001 From: jackwotherspoon Date: Fri, 3 Jan 2025 19:22:11 +0000 Subject: [PATCH] feat: add support for custom user agent --- src/connector.ts | 2 ++ src/sqladmin-fetcher.ts | 3 +++ test/connector.ts | 20 ++++++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/src/connector.ts b/src/connector.ts index 4c306618..c152ebd5 100644 --- a/src/connector.ts +++ b/src/connector.ts @@ -148,6 +148,7 @@ interface ConnectorOptions { * Defaults to `googleapis.com`. */ universeDomain?: string; + userAgent?: string; } // The Connector class is the main public API to interact @@ -164,6 +165,7 @@ export class Connector { loginAuth: opts.auth, sqlAdminAPIEndpoint: opts.sqlAdminAPIEndpoint, universeDomain: opts.universeDomain, + userAgent: opts.userAgent, }); this.localProxies = new Set(); this.sockets = new Set(); diff --git a/src/sqladmin-fetcher.ts b/src/sqladmin-fetcher.ts index 152e011e..2a436e17 100644 --- a/src/sqladmin-fetcher.ts +++ b/src/sqladmin-fetcher.ts @@ -82,6 +82,7 @@ export interface SQLAdminFetcherOptions { loginAuth?: GoogleAuth | AuthClient; sqlAdminAPIEndpoint?: string; universeDomain?: string; + userAgent?: string; } export class SQLAdminFetcher { @@ -92,6 +93,7 @@ export class SQLAdminFetcher { loginAuth, sqlAdminAPIEndpoint, universeDomain, + userAgent, }: SQLAdminFetcherOptions = {}) { let auth: GoogleAuth; @@ -111,6 +113,7 @@ export class SQLAdminFetcher { { product: 'cloud-sql-nodejs-connector', version: 'LIBRARY_SEMVER_VERSION', + comment: userAgent, }, ], universeDomain: universeDomain, diff --git a/test/connector.ts b/test/connector.ts index 9ce184ea..f7b8fa1f 100644 --- a/test/connector.ts +++ b/test/connector.ts @@ -545,3 +545,23 @@ t.test('Connector, custom universeDomain', async t => { t.same(actualUniverseDomain, expectedUniverseDomain); }); + +t.test('Connector, custom userAgent', async t => { + const expectedUserAgent = 'custom-agent'; + let actualUserAgent: string | undefined; + // mocks sql admin fetcher to check that the custom + // userAgent is correctly passed into it + const {Connector} = t.mockRequire('../src/connector', { + '../src/sqladmin-fetcher': { + SQLAdminFetcher: class { + constructor({userAgent}: SQLAdminFetcherOptions) { + actualUserAgent = userAgent; + } + }, + }, + }); + + new Connector({userAgent: expectedUserAgent}); + + t.same(actualUserAgent, expectedUserAgent); +});