From 76d286c0a963d438a397d0c4bcfabc7f402458cc Mon Sep 17 00:00:00 2001 From: Ugaitz Urien Date: Mon, 6 Nov 2023 09:56:02 +0100 Subject: [PATCH] Starting with to rewrite in ESM --- .gitignore | 3 +++ hook.js | 23 ++++++++++++++++--- package.json | 3 ++- test/runtest | 1 + test/source/static-import.js | 13 +++++++++++ .../fill-subscription-global-source-data.js | 9 ++++++++ test/source/utils/index.js | 3 +++ 7 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 test/source/static-import.js create mode 100644 test/source/utils/fill-subscription-global-source-data.js create mode 100644 test/source/utils/index.js diff --git a/.gitignore b/.gitignore index 13aa6d8..ccc7888 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ package-lock.json yarn.lock node_modules coverage + +# IDE files +*.iml diff --git a/hook.js b/hook.js index 3639fbf..5941195 100644 --- a/hook.js +++ b/hook.js @@ -2,6 +2,9 @@ // // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc. +const dc = require('diagnostics_channel') +const sourcePreloadChannel = dc.channel('iitm:source:preload') + const specifiers = new Map() const isWin = process.platform === "win32" @@ -13,7 +16,6 @@ const NODE_MAJOR = Number(NODE_VERSION[0]) const NODE_MINOR = Number(NODE_VERSION[1]) let entrypoint - if (NODE_MAJOR >= 20) { getExports = require('./lib/get-exports.js') } else { @@ -136,7 +138,6 @@ register(${JSON.stringify(realUrl)}, namespace, set, ${JSON.stringify(specifiers ` } } - return parentGetSource(url, context, parentGetSource) } @@ -151,7 +152,23 @@ register(${JSON.stringify(realUrl)}, namespace, set, ${JSON.stringify(specifiers } } - return parentLoad(url, context, parentLoad) + const parentLoadResult = await parentLoad(url, context, parentLoad) + + if (parentLoadResult.source && sourcePreloadChannel.hasSubscribers) { + const source = parentLoadResult.source + const dataToPublish = { + source: parentLoadResult.source, + url + } + + sourcePreloadChannel.publish(dataToPublish) + + if (source !== dataToPublish.source) { + parentLoadResult.source = dataToPublish.source + } + } + + return parentLoadResult } if (NODE_MAJOR >= 17 || (NODE_MAJOR === 16 && NODE_MINOR >= 12)) { diff --git a/package.json b/package.json index 9af84f6..5e44d5f 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "Intercept imports in Node.js", "main": "index.js", "scripts": { - "test": "c8 --check-coverage --lines 85 imhotap --runner 'node test/runtest' --files test/{hook,low-level,other,get-esm-exports}/*", + "test": "c8 --check-coverage --lines 85 imhotap --runner 'node test/runtest' --files test/{hook,low-level,other,get-esm-exports,source}/*", + "test:source": "c8 --check-coverage --lines 85 imhotap --runner 'node test/runtest' --files test/source/*", "test:ts": "c8 imhotap --runner 'node test/runtest' --files test/typescript/*.test.mts", "coverage": "c8 --reporter html imhotap --runner 'node test/runtest' --files test/{hook,low-level,other,get-esm-exports}/* && echo '\nNow open coverage/index.html\n'" }, diff --git a/test/runtest b/test/runtest index 555ca75..2d3c8ef 100755 --- a/test/runtest +++ b/test/runtest @@ -13,6 +13,7 @@ process.env.NODE_NO_WARNINGS = 1 const filename = process.argv[2] const args = [ '--unhandled-rejections=strict', + // '--require','./test/source/utils/fill-subscription-global-source-data.js', ...process.argv.slice(2) ] diff --git a/test/source/static-import.js b/test/source/static-import.js new file mode 100644 index 0000000..5529829 --- /dev/null +++ b/test/source/static-import.js @@ -0,0 +1,13 @@ +require('./utils/fill-subscription-global-source-data.js') +const { strictEqual } = require('assert') +;(async function () { + const sm1 = await import('../fixtures/something.js') + const sm2 = await import('../fixtures/something.mjs') + + setTimeout(() => { + console.log('global.hello', global.hello) + }, 200) + console.log(`global.subscriptionExecuted: ${global.subscriptionExecuted}`) + strictEqual(sm1.default(), 42) + strictEqual(sm2.default(), 42) +})() diff --git a/test/source/utils/fill-subscription-global-source-data.js b/test/source/utils/fill-subscription-global-source-data.js new file mode 100644 index 0000000..e36f0e2 --- /dev/null +++ b/test/source/utils/fill-subscription-global-source-data.js @@ -0,0 +1,9 @@ +global.subscriptionSourceData = [] +const dc = require('diagnostics_channel') + +dc.subscribe('iitm:source:preload', function (message) { + console.log('hello iitm:source:preload url:', message.url) + global.subscriptionExecuted = true +}) + +console.log('fill-subscription-global-source-data subscribed') diff --git a/test/source/utils/index.js b/test/source/utils/index.js new file mode 100644 index 0000000..0401823 --- /dev/null +++ b/test/source/utils/index.js @@ -0,0 +1,3 @@ +// TODO Decide what is the good place for this utils directory +// are utils for source test, but I don't want to execute this +// index.js file in the testsuite