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 @@ -138,4 +139,4 @@ On this path, diagnostics for ES modules are posted from the loader thread and
therefore arrive asynchronously, some time after the module was transformed;
diagnostics for CommonJS modules come from the `_compile` patch and are emitted
synchronously as before. The port does not keep the process alive, so
diagnostics still in flight when the process exits are dropped.
diagnostics still in flight when the process exits are dropped.
8 changes: 7 additions & 1 deletion hook-sync.mjs
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
export { initializeSync as initialize, loadSync as load, resolveSync as resolve, setDiagnosticsHook, createDiagnosticsPort } from './hook.mjs'
export {
initializeSync as initialize,
loadSync as load,
resolveSync as resolve,
setDiagnosticsHook,
createDiagnosticsPort
} from './hook.mjs'
21 changes: 12 additions & 9 deletions hook.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
'use strict'
import createDebug from 'debug'
import { create } from '@apm-js-collab/code-transformer'
import parse from 'module-details-from-path'

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

const debug = createDebug('@apm-js-collab/tracing-hooks:esm-hook')
let transformers = null
let packages = null
Expand Down Expand Up @@ -34,10 +37,10 @@ export function createDiagnosticsPort() {
return port2
}

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 @@ -152,4 +155,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,5 +1,5 @@
'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 { pathToFileURL } = require('node:url')
Expand All @@ -8,7 +8,7 @@ const { emitDiagnostics } = require('./lib/diagnostics')
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