Skip to content
Open
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
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Tracing Hooks

This repository contains a ESM loader for injecting tracing channel hooks into Node.js modules. It also has a patch for Module to be used to patch CJS modules.
This repository contains a ESM loader for injecting tracing channel hooks into
Node.js modules. It also has a patch for Module to be used to patch CJS modules.

## Usage

Note: the module loading hooks API in Node.js has changed as of
v26. To support all active Node.js versions with
forward-compatibility, create a combined loader as an ESM module.
forward-compatibility, create a combined loader as an ES Module (ESM).

This can be done for any CommonJS _or_ ES Module application, but
the loader itself must use ESM.
Expand Down Expand Up @@ -112,4 +113,4 @@ setDiagnosticsHook(({ url, moduleName, error }) => {
// injection succeeded
}
})
```
```
7 changes: 6 additions & 1 deletion hook-sync.mjs
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
export { initializeSync as initialize, loadSync as load, resolveSync as resolve, setDiagnosticsHook } from './hook.mjs'
export {
initializeSync as initialize,
loadSync as load,
resolveSync as resolve,
setDiagnosticsHook
} from './hook.mjs'
18 changes: 10 additions & 8 deletions hook.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use strict'
import createDebug from 'debug'

import { readFile } from 'node:fs/promises'
import { create } from '@apm-js-collab/code-transformer'
import parse from 'module-details-from-path'
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { create as defaultCreate } from '@apm-js-collab/code-transformer'
import createDebug from 'debug'
import parse from 'module-details-from-path'
import getPackageVersion from './lib/get-package-version.js'
import { readFileSync } from 'node:fs'

const debug = createDebug('@apm-js-collab/tracing-hooks:esm-hook')
let transformers = null
let packages = null
Expand All @@ -17,10 +19,10 @@ export function setDiagnosticsHook(hook) {
diagnosticsHook = hook
}

export async function initialize(data = {}) {
return initializeSync(data)
export async function initialize(data = {}, { create = defaultCreate } = {}) {
return initializeSync(data, { create })
}
export function initializeSync(data = {}) {
export function initializeSync(data = {}, { create = defaultCreate } = {}) {
const instrumentations = data?.instrumentations || []
instrumentator = create(instrumentations)
packages = new Set(instrumentations.map(i => i.module.name))
Expand Down Expand Up @@ -101,4 +103,4 @@ export function loadResult(url, result) {
}

return result
}
}
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict'
const { create } = require('@apm-js-collab/code-transformer')
const { create: defaultCreate } = require('@apm-js-collab/code-transformer')
const Module = require('node:module')
const parse = require('module-details-from-path')
const getPackageVersion = require('./lib/get-package-version')
const debug = require('debug')('@apm-js-collab/tracing-hooks:module-patch')

class ModulePatch {
constructor({ instrumentations = [] } = {}) {
constructor({ instrumentations = [], create = defaultCreate } = {}) {
this.packages = new Set(instrumentations.map(i => i.module.name))
this.instrumentator = create(instrumentations)
this.compile = Module.prototype._compile
Expand Down
Loading
Loading