From cc436227f1ec18068090e7183fdf413f8000ef19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20K=C3=B6ssler?= Date: Wed, 2 Oct 2024 14:46:15 +0200 Subject: [PATCH] Add isCJS helper, update import-in-the-middle --- library/agent/hooks/wrapRequire.ts | 3 ++- library/helpers/isCJS.ts | 9 +++++++++ library/package-lock.json | 7 +++---- 3 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 library/helpers/isCJS.ts diff --git a/library/agent/hooks/wrapRequire.ts b/library/agent/hooks/wrapRequire.ts index f4de251c4..15a379f8b 100644 --- a/library/agent/hooks/wrapRequire.ts +++ b/library/agent/hooks/wrapRequire.ts @@ -34,6 +34,7 @@ export function wrapRequire() { `Could not find the _resolveFilename function in node:module using Node.js version ${process.version}` ); } + // Prevent wrapping the require function multiple times isRequireWrapped = true; // Save the original require function @@ -261,5 +262,5 @@ function patchPackage(this: mod, id: string, originalExports: unknown) { * Returns the unwrapped require function. */ export function getOrignalRequire() { - return originalRequire || mod.prototype.require; + return originalRequire || mod.prototype?.require; } diff --git a/library/helpers/isCJS.ts b/library/helpers/isCJS.ts new file mode 100644 index 000000000..c71ba20f0 --- /dev/null +++ b/library/helpers/isCJS.ts @@ -0,0 +1,9 @@ +import * as mod from "module"; + +/** + * Check if executed in CommonJS environment. + * This will always be true if the library is normally used because it is a CommonJS library. + */ +export function isCJS(): boolean { + return mod.prototype !== undefined && mod.prototype.require !== undefined; +} diff --git a/library/package-lock.json b/library/package-lock.json index e4f701775..f28d2eb8a 100644 --- a/library/package-lock.json +++ b/library/package-lock.json @@ -7555,10 +7555,9 @@ } }, "node_modules/import-in-the-middle": { - "version": "1.11.1", - "resolved": "https://registry.npmjs.org/import-in-the-middle/-/import-in-the-middle-1.11.1.tgz", - "integrity": "sha512-lGdg70ECFGv/OHQXL/IPhcxkFPeQ7YA4zborlA54XHVr58oM50QNxItRiayHMqj1MspC5Y9zaHf+QHod/gq7Ug==", - "license": "Apache-2.0", + "version": "1.11.2", + "resolved": "https://registry.npmjs.org/import-in-the-middle/-/import-in-the-middle-1.11.2.tgz", + "integrity": "sha512-gK6Rr6EykBcc6cVWRSBR5TWf8nn6hZMYSRYqCcHa0l0d1fPK7JSYo6+Mlmck76jIX9aL/IZ71c06U2VpFwl1zA==", "dependencies": { "acorn": "^8.8.2", "acorn-import-attributes": "^1.9.5",