From 5246d9e56a0fd508d16e296c9941a61773f66c7f Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Tue, 24 Feb 2026 18:29:03 +0000 Subject: [PATCH 1/2] refactor(index): use `path.dirname` over regex to resolve bin path --- src/index.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/index.js b/src/index.js index 773ea38..ea6fffc 100644 --- a/src/index.js +++ b/src/index.js @@ -2,7 +2,7 @@ const { spawn, spawnSync } = require("node:child_process"); const { open } = require("node:fs/promises"); -const { normalize, resolve: pathResolve } = require("node:path"); +const { dirname, normalize, resolve: pathResolve } = require("node:path"); const { platform } = require("node:process"); const freeze = require("ice-barrage"); const { gt, lt } = require("semver"); @@ -19,7 +19,6 @@ const RTF_MAGIC_BUFFER = Buffer.from(RTF_MAGIC_NUMBER); const RTF_MAGIC_NUMBER_LENGTH = RTF_MAGIC_NUMBER.length; // Cache immutable regex as they are expensive to create and garbage collect -const UNRTF_PATH_REG = /(.+)unrtf/u; // UnRTF version output is inconsistent between versions but always starts with the semantic version number const UNRTF_VERSION_REG = /^(\d{1,2}\.\d{1,2}\.\d{1,2})/u; @@ -218,15 +217,15 @@ class UnRTF { /* istanbul ignore next: requires specific OS */ const which = spawnSync(platform === "win32" ? "where" : "which", [ "unrtf", - ]).stdout.toString(); - const unrtfPath = UNRTF_PATH_REG.exec(which)?.[1]; - - if (unrtfPath) { - this.#unrtfPath = unrtfPath; + ]) + .stdout.toString() + .trim(); + if (which) { + this.#unrtfPath = dirname(which); } /* istanbul ignore next: requires specific OS */ - if (platform === "win32" && !unrtfPath) { + if (platform === "win32" && !this.#unrtfPath) { try { // @ts-ignore: Optional dependency // eslint-disable-next-line n/global-require -- Conditional require From 5b727e23afce8db22c750d9f2362aabb8b3de778 Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Tue, 24 Feb 2026 18:34:13 +0000 Subject: [PATCH 2/2] test(index): use dirname in tests --- test/index.test.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/index.test.js b/test/index.test.js index 4396675..b04324c 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -6,7 +6,7 @@ const { execFile, spawnSync } = require("node:child_process"); const { unlink } = require("node:fs/promises"); -const { join, normalize, sep } = require("node:path"); +const { dirname, join, normalize, sep } = require("node:path"); const { platform } = require("node:process"); const { promisify } = require("node:util"); const { @@ -42,10 +42,10 @@ const originalProcess = jest.requireActual("node:process"); * @throws {Error} If the OS is not supported or the binaries are not found. */ function getTestBinaryPath() { - const which = spawnSync(platform === "win32" ? "where" : "which", [ - "unrtf", - ]).stdout.toString(); - let unrtfPath = /(.+)unrtf/u.exec(which)?.[1]; + const which = spawnSync(platform === "win32" ? "where" : "which", ["unrtf"]) + .stdout.toString() + .trim(); + let unrtfPath = dirname(which); if (platform === "win32" && !unrtfPath) { // @ts-ignore: Optional dependency