Skip to content
3 changes: 2 additions & 1 deletion integration-tests/appsec/iast.esm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ describe('ESM', () => {

const nodeOptionsList = [
'--import dd-trace/initialize.mjs',
'--require dd-trace/init.js --loader dd-trace/loader-hook.mjs'
'--require dd-trace/init.js --loader dd-trace/loader-hook.mjs',
'--import dd-trace/register.js --require dd-trace/init'
]

nodeOptionsList.forEach(nodeOptions => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
],
"dependencies": {
"dc-polyfill": "^0.1.10",
"import-in-the-middle": "^1.14.2"
"import-in-the-middle": "^2.0.0"
},
"optionalDependencies": {
"@datadog/libdatadog": "0.7.0",
Expand Down
14 changes: 13 additions & 1 deletion packages/datadog-esbuild/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,20 @@ const path = require('node:path')
const { NODE_MAJOR, NODE_MINOR } = require('../../../version.js')

const getExportsImporting = (url) => import(url).then(Object.keys)
let getExportsModulePromise

const loadGetExportsModule = () => {
if (!getExportsModulePromise) {
getExportsModulePromise = import('import-in-the-middle/lib/get-exports.mjs')
}
return getExportsModulePromise
}

const getExports = NODE_MAJOR >= 20 || (NODE_MAJOR === 18 && NODE_MINOR >= 19)
? require('import-in-the-middle/lib/get-exports.js')
? async (srcUrl, context, getSource) => {
const mod = await loadGetExportsModule()
return mod.getExports(srcUrl, context, getSource)
}
: getExportsImporting

function isStarExportLine (line) {
Expand Down
12 changes: 4 additions & 8 deletions packages/dd-trace/src/appsec/iast/taint-tracking/rewriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,10 @@ function enableRewriter (telemetryVerbosity) {
}

function isEsmConfigured () {
const hasLoaderArg = isFlagPresent('--loader') || isFlagPresent('--experimental-loader')
if (hasLoaderArg) return true

// Fast path for common case when enabled
if (require.cache[`${process.cwd()}/node_modules/import-in-the-middle/hook.js`]) {
return true
}
return Object.keys(require.cache).some(file => file.endsWith('import-in-the-middle/hook.js'))
return (isFlagPresent('--loader') ||
isFlagPresent('--experimental-loader') ||
isFlagPresent('dd-trace/initialize.mjs')) ||
isFlagPresent('dd-trace/register.js')
}

let enableEsmRewriter = function (telemetryVerbosity) {
Expand Down
42 changes: 32 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2642,10 +2642,10 @@ import-fresh@^3.2.1:
parent-module "^1.0.0"
resolve-from "^4.0.0"

import-in-the-middle@^1.14.2:
version "1.15.0"
resolved "https://registry.yarnpkg.com/import-in-the-middle/-/import-in-the-middle-1.15.0.tgz#9e20827a322bbadaeb5e3bac49ea8f6d4685fdd8"
integrity sha512-bpQy+CrsRmYmoPMAE/0G33iwRqwW4ouqdRg8jgbH3aKuCtOc8lxgmYXg2dMM92CRiGP660EtBcymH/eVUpCSaA==
import-in-the-middle@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/import-in-the-middle/-/import-in-the-middle-2.0.0.tgz#295948cee94d0565314824c6bd75379d13e5b1a5"
integrity sha512-yNZhyQYqXpkT0AKq3F3KLasUSK4fHvebNH5hOsKQw2dhGSALvQ4U0BqUc5suziKvydO5u5hgN2hy1RJaho8U5A==
dependencies:
acorn "^8.14.0"
acorn-import-attributes "^1.9.5"
Expand Down Expand Up @@ -4428,8 +4428,16 @@ streamsearch@^1.1.0:
resolved "https://registry.yarnpkg.com/streamsearch/-/streamsearch-1.1.0.tgz#404dd1e2247ca94af554e841a8ef0eaa238da764"
integrity sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==

"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.0.0, string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.2, string-width@^4.2.3:
name string-width-cjs
"string-width-cjs@npm:string-width@^4.2.0":
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
dependencies:
emoji-regex "^8.0.0"
is-fullwidth-code-point "^3.0.0"
strip-ansi "^6.0.1"

string-width@^4.0.0, string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.2, string-width@^4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
Expand Down Expand Up @@ -4493,8 +4501,14 @@ string_decoder@~1.1.1:
dependencies:
safe-buffer "~5.1.0"

"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
name strip-ansi-cjs
"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
dependencies:
ansi-regex "^5.0.1"

strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
Expand Down Expand Up @@ -4971,8 +4985,7 @@ workerpool@^9.2.0:
resolved "https://registry.yarnpkg.com/workerpool/-/workerpool-9.3.4.tgz#f6c92395b2141afd78e2a889e80cb338fe9fca41"
integrity sha512-TmPRQYYSAnnDiEB0P/Ytip7bFGvqnSU6I2BcuSw7Hx+JSg/DsUi5ebYfc8GYaSdpuvOcEs6dXxPurOYpe9QFwg==

"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
name wrap-ansi-cjs
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
Expand All @@ -4990,6 +5003,15 @@ wrap-ansi@^6.2.0:
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
dependencies:
ansi-styles "^4.0.0"
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"
Expand Down