From 7016011ae09a0a67fc8eee39493b28a3c64467fe Mon Sep 17 00:00:00 2001 From: kwasniew Date: Wed, 15 Jan 2025 16:27:00 +0100 Subject: [PATCH 1/4] fix: embed version at compile time --- .gitignore | 1 + package.json | 1 + rollup.config.mjs | 8 ++++++++ src/index.ts | 5 +---- src/metrics.ts | 4 +--- yarn.lock | 14 +++++++++++--- 6 files changed, 23 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index c274848..fba9326 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ node_modules/ .vscode/ coverage/ .idea +.DS_Store diff --git a/package.json b/package.json index 2202bd3..3f37f1d 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "@babel/runtime": "^7.23.1", "@rollup/plugin-commonjs": "^25.0.5", "@rollup/plugin-node-resolve": "^15.2.3", + "@rollup/plugin-replace": "^6.0.2", "@rollup/plugin-terser": "^0.4.4", "@rollup/plugin-typescript": "^11.1.5", "@types/jest": "^29.5.5", diff --git a/rollup.config.mjs b/rollup.config.mjs index 6b176b4..9fb2001 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -3,6 +3,10 @@ import nodeResolve from '@rollup/plugin-node-resolve'; import commonjs from '@rollup/plugin-commonjs'; import terser from '@rollup/plugin-terser'; import nodePolyfills from 'rollup-plugin-node-polyfills'; +import replace from '@rollup/plugin-replace'; +import fs from 'fs'; + +const version = JSON.parse(fs.readFileSync('./package.json', 'UTF-8')).version; export default { input: './src/index.ts', @@ -25,6 +29,10 @@ export default { } ], plugins: [ + replace({ + '__VERSION__': version, + preventAssignment: true + }), typescript({ compilerOptions: { lib: ['es5', 'es6', 'dom'], diff --git a/src/index.ts b/src/index.ts index 7e82219..88137fd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,9 +11,6 @@ import { urlWithContextAsQuery, } from './util'; -// eslint-disable-next-line @typescript-eslint/no-var-requires -const packageJSON = require('../package.json'); - const DEFINED_FIELDS = [ 'userId', 'sessionId', @@ -476,7 +473,7 @@ export class UnleashClient extends TinyEmitter { const headers = { [this.headerName]: this.clientKey, Accept: 'application/json', - 'x-unleash-sdk': `unleash-js@${packageJSON.version}`, + 'x-unleash-sdk': `unleash-js@$__VERSION__`, 'x-unleash-connection-id': this.connectionId, 'x-unleash-appname': this.context.appName, }; diff --git a/src/metrics.ts b/src/metrics.ts index 29e2c9f..9e7f14e 100644 --- a/src/metrics.ts +++ b/src/metrics.ts @@ -1,8 +1,6 @@ // Simplified version of: https://github.com/Unleash/unleash-client-node/blob/main/src/metrics.ts import { notNullOrUndefined } from './util'; -// eslint-disable-next-line @typescript-eslint/no-var-requires -const packageJSON = require('../package.json'); export interface MetricsOptions { onError: OnError; @@ -127,7 +125,7 @@ export default class Metrics { [this.headerName]: this.clientKey, Accept: 'application/json', 'Content-Type': 'application/json', - 'x-unleash-sdk': `unleash-js@${packageJSON.version}`, + 'x-unleash-sdk': `unleash-js@$__VERSION__`, 'x-unleash-connection-id': this.connectionId, 'x-unleash-appname': this.appName, }; diff --git a/yarn.lock b/yarn.lock index cf404e4..7dce0f5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -642,6 +642,14 @@ is-module "^1.0.0" resolve "^1.22.1" +"@rollup/plugin-replace@^6.0.2": + version "6.0.2" + resolved "https://registry.yarnpkg.com/@rollup/plugin-replace/-/plugin-replace-6.0.2.tgz#2f565d312d681e4570ff376c55c5c08eb6f1908d" + integrity sha512-7QaYCf8bqF04dOy7w/eHmJeNExxTYwvKAmlSAH/EaWWUzbT0h5sbF6bktFoX/0F/0qwng5/dWFMyf3gzaM8DsQ== + dependencies: + "@rollup/pluginutils" "^5.0.1" + magic-string "^0.30.3" + "@rollup/plugin-terser@^0.4.4": version "0.4.4" resolved "https://registry.yarnpkg.com/@rollup/plugin-terser/-/plugin-terser-0.4.4.tgz#15dffdb3f73f121aa4fbb37e7ca6be9aeea91962" @@ -2977,9 +2985,9 @@ rollup-pluginutils@^2.8.1: estree-walker "^0.6.1" rollup@^3.29.4: - version "3.29.4" - resolved "https://registry.yarnpkg.com/rollup/-/rollup-3.29.4.tgz#4d70c0f9834146df8705bfb69a9a19c9e1109981" - integrity sha512-oWzmBZwvYrU0iJHtDmhsm662rC15FRXmcjCk1xD771dFDx5jJ02ufAQQTn0etB2emNk4J9EZg/yWKpsn9BWGRw== + version "3.29.5" + resolved "https://registry.yarnpkg.com/rollup/-/rollup-3.29.5.tgz#8a2e477a758b520fb78daf04bca4c522c1da8a54" + integrity sha512-GVsDdsbJzzy4S/v3dqWPJ7EfvZJfCHiDqe80IyrF59LYuP+e6U1LJoUqeuqRbwAWoMNoXivMNeNAOf5E22VA1w== optionalDependencies: fsevents "~2.3.2" From 131227b6558d521b7697f13eca2d29cda290b3dd Mon Sep 17 00:00:00 2001 From: kwasniew Date: Wed, 15 Jan 2025 16:32:53 +0100 Subject: [PATCH 2/4] fix: embed version at compile time --- src/index.test.ts | 4 +--- src/index.ts | 3 ++- src/metrics.ts | 3 ++- src/version.ts | 1 + 4 files changed, 6 insertions(+), 5 deletions(-) create mode 100644 src/version.ts diff --git a/src/index.test.ts b/src/index.test.ts index 8c4ef3c..17ef8a9 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -1,6 +1,4 @@ import { FetchMock } from 'jest-fetch-mock'; -// eslint-disable-next-line @typescript-eslint/no-var-requires -const packageJSON = require('../package.json'); import 'jest-localstorage-mock'; import * as data from './test/testdata.json'; import IStorageProvider from './storage-provider'; @@ -1383,7 +1381,7 @@ test('Should add `x-unleash` headers', async () => { /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; const expectedHeaders = { - 'x-unleash-sdk': `unleash-js@${packageJSON.version}`, + 'x-unleash-sdk': `unleash-js@__VERSION__`, 'x-unleash-connection-id': expect.stringMatching(uuidFormat), 'x-unleash-appname': appName, }; diff --git a/src/index.ts b/src/index.ts index 88137fd..82fe1b8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,6 +10,7 @@ import { notNullOrUndefined, urlWithContextAsQuery, } from './util'; +import {sdkVersion} from "./version"; const DEFINED_FIELDS = [ 'userId', @@ -473,7 +474,7 @@ export class UnleashClient extends TinyEmitter { const headers = { [this.headerName]: this.clientKey, Accept: 'application/json', - 'x-unleash-sdk': `unleash-js@$__VERSION__`, + 'x-unleash-sdk': sdkVersion, 'x-unleash-connection-id': this.connectionId, 'x-unleash-appname': this.context.appName, }; diff --git a/src/metrics.ts b/src/metrics.ts index 9e7f14e..00ea706 100644 --- a/src/metrics.ts +++ b/src/metrics.ts @@ -1,6 +1,7 @@ // Simplified version of: https://github.com/Unleash/unleash-client-node/blob/main/src/metrics.ts import { notNullOrUndefined } from './util'; +import {sdkVersion} from "./version"; export interface MetricsOptions { onError: OnError; @@ -125,7 +126,7 @@ export default class Metrics { [this.headerName]: this.clientKey, Accept: 'application/json', 'Content-Type': 'application/json', - 'x-unleash-sdk': `unleash-js@$__VERSION__`, + 'x-unleash-sdk': sdkVersion, 'x-unleash-connection-id': this.connectionId, 'x-unleash-appname': this.appName, }; diff --git a/src/version.ts b/src/version.ts new file mode 100644 index 0000000..896c627 --- /dev/null +++ b/src/version.ts @@ -0,0 +1 @@ +export const sdkVersion = `unleash-js@__VERSION__`; From fc4fbfe7772e5ca47f853518e69e8cd9102ef490 Mon Sep 17 00:00:00 2001 From: kwasniew Date: Wed, 15 Jan 2025 16:34:56 +0100 Subject: [PATCH 3/4] fix: embed version at compile time --- src/index.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/index.test.ts b/src/index.test.ts index 17ef8a9..fb3677d 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -1381,7 +1381,8 @@ test('Should add `x-unleash` headers', async () => { /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; const expectedHeaders = { - 'x-unleash-sdk': `unleash-js@__VERSION__`, + // will be replaced at build time with the actual version + 'x-unleash-sdk': 'unleash-js@__VERSION__', 'x-unleash-connection-id': expect.stringMatching(uuidFormat), 'x-unleash-appname': appName, }; From 51d4706886109dddfd0eb1c653b38a3f22ad73b2 Mon Sep 17 00:00:00 2001 From: kwasniew Date: Wed, 15 Jan 2025 16:36:25 +0100 Subject: [PATCH 4/4] fix: embed version at compile time --- src/index.ts | 2 +- src/metrics.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 82fe1b8..748f00e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,7 +10,7 @@ import { notNullOrUndefined, urlWithContextAsQuery, } from './util'; -import {sdkVersion} from "./version"; +import { sdkVersion } from './version'; const DEFINED_FIELDS = [ 'userId', diff --git a/src/metrics.ts b/src/metrics.ts index 00ea706..c769163 100644 --- a/src/metrics.ts +++ b/src/metrics.ts @@ -1,7 +1,7 @@ // Simplified version of: https://github.com/Unleash/unleash-client-node/blob/main/src/metrics.ts import { notNullOrUndefined } from './util'; -import {sdkVersion} from "./version"; +import { sdkVersion } from './version'; export interface MetricsOptions { onError: OnError;