Skip to content

Commit

Permalink
Starting with to rewrite in ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
uurien committed Nov 6, 2023
1 parent 5845e21 commit 76d286c
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 4 deletions.
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')
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

0 comments on commit 76d286c

Please sign in to comment.