Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Starting with rewrite in ESM #35

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ package-lock.json
yarn.lock
node_modules
coverage

# IDE files
*.iml
23 changes: 20 additions & 3 deletions hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
//
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.

const dc = require('diagnostics_channel')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use dc-polyfill here? Should help in case anyone uses IITM with older versions of Node.js.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that we can, yes. I'll change it, but for now this is just a PoC :-D

const sourcePreloadChannel = dc.channel('iitm:source:preload')

const specifiers = new Map()
const isWin = process.platform === "win32"

Expand All @@ -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 {
Expand Down Expand Up @@ -136,7 +138,6 @@ register(${JSON.stringify(realUrl)}, namespace, set, ${JSON.stringify(specifiers
`
}
}

return parentGetSource(url, context, parentGetSource)
}

Expand All @@ -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)) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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'"
},
Expand Down
1 change: 1 addition & 0 deletions test/runtest
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]

Expand Down
13 changes: 13 additions & 0 deletions test/source/static-import.js
Original file line number Diff line number Diff line change
@@ -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)
})()
9 changes: 9 additions & 0 deletions test/source/utils/fill-subscription-global-source-data.js
Original file line number Diff line number Diff line change
@@ -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')
3 changes: 3 additions & 0 deletions test/source/utils/index.js
Original file line number Diff line number Diff line change
@@ -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
Loading