From e37a2b10e1525dddf6e9b5564750f3cbeffd8ca1 Mon Sep 17 00:00:00 2001 From: Scott Lyons Date: Wed, 20 Nov 2024 19:46:17 -0800 Subject: [PATCH 1/2] Adding custom user-agent to spice.js --- src/client.ts | 8 ++++---- src/interfaces.ts | 1 + src/user-agent.ts | 2 +- test/user-agent.test.ts | 2 +- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/client.ts b/src/client.ts index 478422f..efa08ac 100644 --- a/src/client.ts +++ b/src/client.ts @@ -60,8 +60,9 @@ class SpiceClient { this._apiKey = params; this._httpUrl = 'https://data.spiceai.io'; this._flightUrl = 'flight.spiceai.io:443'; + this._userAgent = getUserAgent(); } else { - const { apiKey, httpUrl, flightUrl, flightTlsEnabled } = params; + const { apiKey, httpUrl, flightUrl, flightTlsEnabled, userAgent } = params; this._apiKey = apiKey; this._httpUrl = httpUrl || 'http://127.0.0.1:8090'; @@ -72,9 +73,8 @@ class SpiceClient { : this._flightUrl.includes('127.0.0.1') ? false : true; + this._userAgent = userAgent || getUserAgent(); } - - this._userAgent = getUserAgent(); } private createClient(meta: any): any { @@ -105,7 +105,7 @@ class SpiceClient { const meta = new grpc.Metadata(); const client: FlightClient = this.createClient(meta); meta.set('authorization', 'Bearer ' + this._apiKey); - meta.set('x-spice-user-agent', this._userAgent); + meta.set('User-Agent', this._userAgent); let queryBuff = Buffer.from(queryText, 'utf8'); diff --git a/src/interfaces.ts b/src/interfaces.ts index c6ac2cf..a787435 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -34,6 +34,7 @@ export interface SpiceClientConfig { httpUrl?: string; flightUrl?: string; flightTlsEnabled?: boolean; + userAgent?: string; } export interface RefreshOverrides { diff --git a/src/user-agent.ts b/src/user-agent.ts index abcbce9..23ca6e2 100644 --- a/src/user-agent.ts +++ b/src/user-agent.ts @@ -5,5 +5,5 @@ export function getUserAgent(): string { const osType = os.type(); const osRelease = os.release(); const osArch = os.machine(); - return `spice.js ${VERSION} (${osType}/${osRelease} ${osArch})`; + return `spice.js/${VERSION} (${osType}/${osRelease} ${osArch})`; } \ No newline at end of file diff --git a/test/user-agent.test.ts b/test/user-agent.test.ts index 0b6a01a..b728d32 100644 --- a/test/user-agent.test.ts +++ b/test/user-agent.test.ts @@ -1,7 +1,7 @@ import { getUserAgent } from "../src/user-agent"; describe("user-agent", () => { - const matching_regex = /spice.js \d+\.\d+\.\d+ \((Linux|Windows|Darwin)\/[\d\w\.\-\_]+ (x86_64|aarch64|i386|arm64)\)/; + const matching_regex = /spice.js\/\d+\.\d+\.\d+ \((Linux|Windows|Darwin)\/[\d\w\.\-\_]+ (x86_64|aarch64|i386|arm64)\)/; it("should match the user-agent regex", async () => { const ua = getUserAgent(); From 228cd0d2b6821bcb8d57a564abd1439efb9b17bf Mon Sep 17 00:00:00 2001 From: Scott Lyons Date: Thu, 21 Nov 2024 07:20:40 -0800 Subject: [PATCH 2/2] Fixing User-Agent header --- src/client.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client.ts b/src/client.ts index efa08ac..c9a9362 100644 --- a/src/client.ts +++ b/src/client.ts @@ -231,7 +231,7 @@ class SpiceClient { const headers = [ ['Content-Type', 'application/json'], ['Accept-Encoding', 'br, gzip, deflate'], - ['X-Spice-User-Agent', this._userAgent] + ['User-Agent', this._userAgent] ]; if (this._apiKey) {