-
Notifications
You must be signed in to change notification settings - Fork 317
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Previously, non-programmatic tracer initialization was done via a combination of several command-line options, depending on whether ESM support is required, and what version of Node.js. This has now been consolidated to a single `initialize.mjs` file, which can be used either with the `--loader` option, or the `--import` option. In either case, it will initialize both the tracer and the ESM loader hook. For versions of Node.js prior to 20.6.0, the `--loader` option must be used. For versions after that, either may be used, but `--import` is preferred, since the `--loader` option will eventually be deprecated, and emits a warning as such already. All previous behavior, and related files, are preserved, so this is a semver-minor change. For now, docs are not included, since the current README does not even address non-programmatic initialization. Future work (for future PRs): * Have `.init()` also call `register()` for the loader hook if it hasn't been registered, so that CLI options become unnecessary for ESM support in Node.js * Allow programmatic config when using non-programmatic initialization.
- Loading branch information
Showing
7 changed files
with
175 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
!index.js | ||
!esbuild.js | ||
!init.js | ||
!initialize.mjs | ||
!loader-hook.mjs | ||
!register.js | ||
!package.json | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* This file serves one of two purposes, depending on how it's used. | ||
* | ||
* If used with --import, it will import init.js and register the loader hook. | ||
* If used with --loader, it will act as the loader hook, except that it will | ||
* also import init.js inside the source code of the entrypoint file. | ||
* | ||
* The result is that no matter how this file is used, so long as it's with | ||
* one of the two flags, the tracer will always be initialized, and the loader | ||
* hook will always be active for ESM support. | ||
*/ | ||
|
||
import { isMainThread } from 'worker_threads' | ||
import { register } from 'node:module'; | ||
|
||
import { fileURLToPath } from 'node:url' | ||
import { | ||
load as origLoad, | ||
resolve as origResolve, | ||
getFormat as origGetFormat, | ||
getSource as origGetSource | ||
} from 'import-in-the-middle/hook.mjs' | ||
|
||
let hasInsertedInit = false | ||
function insertInit (result) { | ||
if (!hasInsertedInit) { | ||
hasInsertedInit = true | ||
result.source = ` | ||
import '${fileURLToPath(new URL('./init.js', import.meta.url))}'; | ||
${result.source}` | ||
} | ||
return result | ||
} | ||
|
||
export async function load (...args) { | ||
return insertInit(await origLoad(...args)) | ||
} | ||
|
||
export const resolve = origResolve | ||
|
||
export const getFormat = origGetFormat | ||
|
||
export async function getSource (...args) { | ||
return insertInit(await origGetSource(...args)) | ||
} | ||
|
||
if (isMainThread) { | ||
await import('./init.js') | ||
register('./loader-hook.mjs', import.meta.url) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const http = require('http') | ||
const dc = require('dc-polyfill') | ||
|
||
let gotEvent = false | ||
dc.subscribe('apm:http:client:request:start', (event) => { | ||
gotEvent = true | ||
}) | ||
|
||
const server = http.createServer((req, res) => { | ||
res.end('Hello World') | ||
}).listen(0, () => { | ||
http.get(`http://localhost:${server.address().port}`, (res) => { | ||
res.on('data', () => {}) | ||
res.on('end', () => { | ||
server.close() | ||
// eslint-disable-next-line no-console | ||
console.log(gotEvent) | ||
process.exit() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import http from 'http' | ||
import dc from 'dc-polyfill' | ||
|
||
let gotEvent = false | ||
dc.subscribe('apm:http:client:request:start', (event) => { | ||
gotEvent = true | ||
}) | ||
|
||
const server = http.createServer((req, res) => { | ||
res.end('Hello World') | ||
}).listen(0, () => { | ||
http.get(`http://localhost:${server.address().port}`, (res) => { | ||
res.on('data', () => {}) | ||
res.on('end', () => { | ||
server.close() | ||
// eslint-disable-next-line no-console | ||
console.log(gotEvent) | ||
process.exit() | ||
}) | ||
}) | ||
}) |
File renamed without changes.