From 0b2a311146d45d1ed8d4b68f54ff0a6e6c9ca362 Mon Sep 17 00:00:00 2001 From: Andrew Bell <115623869+andybharness@users.noreply.github.com> Date: Mon, 8 Apr 2024 17:41:05 +0100 Subject: [PATCH 1/3] [FFM-7017] - Add Harness-SDK-Info/Harness-AccountID/Harness-EnvironmentID headers What Add meta data headers `Harness-SDK-Info`, `Harness-AccountID` and `Harness-EnvironmentID` to bring NodeJS in line with other server SDKs. Why Helps with backend load testing to identify problematic clients Testing Manual with Java SDK proxy --- package-lock.json | 4 ++-- package.json | 2 +- src/client.ts | 15 ++++++++++++++- src/streaming.ts | 5 ++++- src/version.ts | 2 +- 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 206e9f0..91b04ae 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@harnessio/ff-nodejs-server-sdk", - "version": "1.6.2", + "version": "1.7.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@harnessio/ff-nodejs-server-sdk", - "version": "1.6.2", + "version": "1.7.0", "license": "Apache-2.0", "dependencies": { "axios": "^1.6.8", diff --git a/package.json b/package.json index d196c55..568bbf6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@harnessio/ff-nodejs-server-sdk", - "version": "1.6.2", + "version": "1.7.0", "description": "Feature flags SDK for NodeJS environments", "main": "dist/cjs/index.js", "module": "dist/esm/index.mjs", diff --git a/src/client.ts b/src/client.ts index 0db6e80..04f56c0 100644 --- a/src/client.ts +++ b/src/client.ts @@ -44,6 +44,8 @@ export enum Event { CHANGED = 'changed', } +export const SDK_INFO = `NodeJS ${VERSION} Server`; + export default class Client { private evaluator: Evaluator; private repository: Repository; @@ -91,6 +93,7 @@ export default class Client { baseOptions: { headers: { 'User-Agent': `NodeJsSDK/${VERSION}`, + 'Harness-SDK-Info': SDK_INFO, }, }, }); @@ -229,6 +232,16 @@ export default class Client { ); } + if (decoded.accountID) { + this.configuration.baseOptions.headers['Harness-AccountID'] = + decoded.accountID; + } + + if (decoded.environmentIdentifier) { + this.configuration.baseOptions.headers['Harness-EnvironmentID'] = + decoded.environmentIdentifier; + } + this.environment = decoded.environment; this.cluster = decoded.clusterIdentifier || '1'; } catch (error) { @@ -329,7 +342,7 @@ export default class Client { this.repository, ); - this.streamProcessor.start(); + this.streamProcessor.start(this.configuration.baseOptions.headers); } if (this.options.enableAnalytics) { diff --git a/src/streaming.ts b/src/streaming.ts index c480659..42a86fe 100644 --- a/src/streaming.ts +++ b/src/streaming.ts @@ -14,6 +14,8 @@ import { warnStreamRetrying, } from './sdk_codes'; +import { SDK_INFO } from './client'; + type FetchFunction = ( identifier: string, environment: string, @@ -68,7 +70,7 @@ export class StreamProcessor { ); } - start(): void { + start(headers: Record): void { this.log.info('Starting new StreamProcessor'); const url = `${this.options.baseUrl}/stream?cluster=${this.cluster}`; @@ -79,6 +81,7 @@ export class StreamProcessor { Accept: 'text/event-stream', Authorization: `Bearer ${this.jwtToken}`, 'API-Key': this.apiKey, + ...headers, }, }; diff --git a/src/version.ts b/src/version.ts index e946876..f5ddf13 100644 --- a/src/version.ts +++ b/src/version.ts @@ -1 +1 @@ -export const VERSION = '1.6.2'; +export const VERSION = '1.7.0'; From 2aa05667d93ef6e6fcd02154adb5790aa7e0b8b8 Mon Sep 17 00:00:00 2001 From: Andrew Bell <115623869+andybharness@users.noreply.github.com> Date: Mon, 8 Apr 2024 17:50:04 +0100 Subject: [PATCH 2/3] fix build error --- src/streaming.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/streaming.ts b/src/streaming.ts index 42a86fe..b636f6c 100644 --- a/src/streaming.ts +++ b/src/streaming.ts @@ -14,8 +14,6 @@ import { warnStreamRetrying, } from './sdk_codes'; -import { SDK_INFO } from './client'; - type FetchFunction = ( identifier: string, environment: string, From 94a76392f3da7146a6e755f6176a08c2324d7ce8 Mon Sep 17 00:00:00 2001 From: Andrew Bell <115623869+andybharness@users.noreply.github.com> Date: Tue, 9 Apr 2024 10:10:22 +0100 Subject: [PATCH 3/3] add headers to constructor --- src/client.ts | 3 ++- src/streaming.ts | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/client.ts b/src/client.ts index 04f56c0..77ca98c 100644 --- a/src/client.ts +++ b/src/client.ts @@ -340,9 +340,10 @@ export default class Client { this.cluster, this.eventBus, this.repository, + this.configuration.baseOptions.headers, ); - this.streamProcessor.start(this.configuration.baseOptions.headers); + this.streamProcessor.start(); } if (this.options.enableAnalytics) { diff --git a/src/streaming.ts b/src/streaming.ts index b636f6c..3dc8063 100644 --- a/src/streaming.ts +++ b/src/streaming.ts @@ -33,6 +33,7 @@ export class StreamProcessor { private readonly api: ClientApi; private readonly repository: Repository; private readonly retryDelayMs: number; + private readonly headers: Record; private options: Options; private request: ClientRequest; @@ -50,6 +51,7 @@ export class StreamProcessor { cluster: string, eventBus: EventEmitter, repository: Repository, + headers: Record, ) { this.api = api; this.apiKey = apiKey; @@ -66,9 +68,10 @@ export class StreamProcessor { this.retryDelayMs = Math.floor( Math.random() * (maxDelay - minDelay) + minDelay, ); + this.headers = headers; } - start(headers: Record): void { + start(): void { this.log.info('Starting new StreamProcessor'); const url = `${this.options.baseUrl}/stream?cluster=${this.cluster}`; @@ -79,7 +82,7 @@ export class StreamProcessor { Accept: 'text/event-stream', Authorization: `Bearer ${this.jwtToken}`, 'API-Key': this.apiKey, - ...headers, + ...this.headers, }, };