Skip to content

Commit 1e82d80

Browse files
committed
fix: replace synchronous package.json require with async getPackageMeta utility
1 parent 56e60fb commit 1e82d80

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

lib/redis/event_handler.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ import { AbortError } from "redis-errors";
55
import Command from "../Command";
66
import { MaxRetriesPerRequestError } from "../errors";
77
import { CommandItem, Respondable } from "../types";
8-
import { Debug, noop, CONNECTION_CLOSED_ERROR_MSG } from "../utils";
8+
import {
9+
Debug,
10+
noop,
11+
CONNECTION_CLOSED_ERROR_MSG,
12+
getPackageMeta,
13+
} from "../utils";
914
import DataHandler from "../DataHandler";
10-
// This seems to be less problematic, than import
11-
const { version } = require("../../package.json");
1215

1316
const debug = Debug("connection");
1417

@@ -268,7 +271,19 @@ export function readyHandler(self) {
268271

269272
if (!self.options?.disableClientInfo) {
270273
debug("set the client info");
271-
self.client("SETINFO", "LIB-VER", version).catch(noop);
274+
275+
let version = null;
276+
277+
getPackageMeta()
278+
.then((packageMeta) => {
279+
if (packageMeta) {
280+
version = packageMeta?.version;
281+
}
282+
})
283+
.catch(noop)
284+
.finally(() => {
285+
self.client("SETINFO", "LIB-VER", version ?? "unknown").catch(noop);
286+
});
272287

273288
self
274289
.client(

lib/utils/index.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { promises as fsPromises } from "fs";
2+
import { resolve } from "path";
13
import { parse as urllibParse } from "url";
24
import { defaults, noop } from "./lodash";
35
import { Callback } from "../types";
@@ -319,4 +321,39 @@ export function zipMap<K, V>(keys: K[], values: V[]): Map<K, V> {
319321
return map;
320322
}
321323

324+
325+
326+
/**
327+
* Memoized package metadata to avoid repeated file system reads.
328+
*
329+
* @internal
330+
*/
331+
let cachedPackageMeta: { version: string } = null;
332+
333+
/**
334+
* Retrieves cached package metadata from package.json.
335+
*
336+
* @internal
337+
* @returns {Promise<{version: string} | null>} Package metadata or null if unavailable
338+
*/
339+
export async function getPackageMeta() {
340+
if (cachedPackageMeta) {
341+
return cachedPackageMeta;
342+
}
343+
344+
try {
345+
const filePath = resolve(__dirname, "..", "..", "package.json");
346+
const data = await fsPromises.readFile(filePath, "utf8");
347+
const parsed = JSON.parse(data);
348+
349+
cachedPackageMeta = {
350+
version: parsed.version,
351+
};
352+
353+
return cachedPackageMeta;
354+
} catch (err) {
355+
return null;
356+
}
357+
}
358+
322359
export { Debug, defaults, noop };

test/functional/client_info.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ describe("clientInfo", function () {
4646
(cmd) => cmd.key === "LIB-NAME"
4747
);
4848

49-
expect(libVerCommand).to.exist; // version will change over time
49+
expect(libVerCommand).to.exist;
50+
expect(libVerCommand?.value).to.be.a("string");
51+
expect(libVerCommand?.value).to.not.equal("unknown");
5052
expect(libNameCommand).to.exist;
5153
expect(libNameCommand?.value).to.equal("ioredis");
5254
});
@@ -183,6 +185,8 @@ describe("clientInfo", function () {
183185
);
184186

185187
expect(libVerCommand).to.exist;
188+
expect(libVerCommand?.value).to.be.a("string");
189+
expect(libVerCommand?.value).to.not.equal("unknown");
186190
expect(libNameCommand).to.exist;
187191
expect(libNameCommand?.value).to.equal("ioredis");
188192
});

0 commit comments

Comments
 (0)