From bef5221ed891db12a0b760f12fc9654e2f4e241b Mon Sep 17 00:00:00 2001 From: Thodoris Greasidis Date: Tue, 25 Feb 2025 18:25:06 +0200 Subject: [PATCH] Use the CLI version in the X-Balena-Client header Change-type: patch See: https://balena.fibery.io/Work/Project/Extend-the-X-Balena-Client-header-to-include-the-UI-CLI-version-as-well-1174 --- src/utils/lazy.ts | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/utils/lazy.ts b/src/utils/lazy.ts index fbcc1ef9..b50d7dd8 100644 --- a/src/utils/lazy.ts +++ b/src/utils/lazy.ts @@ -21,6 +21,7 @@ import type { Chalk } from 'chalk'; import type * as visuals from 'resin-cli-visuals'; import type * as CliForm from 'resin-cli-form'; import type { ux } from '@oclif/core'; +import { version } from '../../package.json'; // Equivalent of _.once but avoiding the need to import lodash for lazy deps const once = (fn: () => T) => { @@ -43,9 +44,22 @@ export const onceAsync = (fn: () => Promise) => { }; }; -export const getBalenaSdk = once(() => - (require('balena-sdk') as typeof BalenaSdk).fromSharedOptions(), -); +const cliXBalenaClientHeaderInterceptor: BalenaSdk.Interceptor = { + request($request) { + // We intentionally overwrite the sdk version string from the header + // to conserve bandwidth. + $request.headers['X-Balena-Client'] = `balena-cli/${version}`; + return $request; + }, +}; + +export const getBalenaSdk = once(() => { + const sdk = (require('balena-sdk') as typeof BalenaSdk).fromSharedOptions(); + if (!sdk.interceptors.includes(cliXBalenaClientHeaderInterceptor)) { + sdk.interceptors.push(cliXBalenaClientHeaderInterceptor); + } + return sdk; +}); export const getVisuals = once( () => require('resin-cli-visuals') as typeof visuals,