{"name":"@apm-js-collab/tracing-hooks","version":"0.0.1","license":"Apache-2.0","_id":"@apm-js-collab/tracing-hooks@0.0.1","maintainers":[{"name":"timfish","email":"tim@timfish.uk"},{"name":"bizob2828","email":"robert.evans25@gmail.com"}],"homepage":"https://github.com/apm-js-collab/tracing-hooks#readme","bugs":{"url":"https://github.com/apm-js-collab/tracing-hooks/issues"},"dist":{"shasum":"38869afdf3224822ef4c23e102ef229ea97937e9","tarball":"http://registry-npm.ywwl.com/@apm-js-collab/tracing-hooks/download/@apm-js-collab/tracing-hooks-0.0.1.tgz","fileCount":5,"integrity":"sha512-3jXGTWknXOjo6pUJOF0gb9C5rDY31vOVsSbr5dJmqsh9EpTq4Z1AWqGgprzslq107o3g3n3xdtTObP676mn7OA==","signatures":[{"sig":"MEUCIBqE1VAKHTbznKnrt0f1554Gw2/9Ijle23HWX/jPEg5cAiEAqETO2E5OWFpMzH5qZDxEhZwpYxj6fYmmmcI9g+8tIiI=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":7755,"size":2574,"noattachment":false,"key":"/@apm-js-collab/tracing-hooks/-/@apm-js-collab/tracing-hooks-0.0.1.tgz"},"main":"index.js","type":"commonjs","engines":{"node":">=20"},"gitHead":"5acb9e58268b382ed87c3d5f9fced71d2d43073a","scripts":{"test":"c8 borp 'test/**/*.test.{js,mjs}'"},"_npmUser":{"name":"bizob2828","email":"robert.evans25@gmail.com"},"repository":{"url":"git+https://github.com/apm-js-collab/tracing-hooks.git","type":"git"},"_npmVersion":"11.5.1","description":"CJS and ESM hooks for orchestrion","directories":{},"_nodeVersion":"24.5.0","dependencies":{"debug":"^4.4.1","module-details-from-path":"^1.0.4","@apm-js-collab/code-transformer":"^0.6.0"},"_hasShrinkwrap":false,"devDependencies":{"c8":"^10.1.3","borp":"^0.20.1","@matteo.collina/snap":"^0.3.0"},"_npmOperationalInternal":{"tmp":"tmp/tracing-hooks_0.0.1_1755621190597_0.6745145552591432","host":"s3://npm-registry-packages-npm-production"},"readme":"# Tracing Hooks\n\nThis 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.\n\n## Usage\n\nNote: the module loading hooks API in Node.js has changed as of\nv26. To support all active Node.js versions with\nforward-compatibility, create a combined loader as an ESM module.\n\nThis can be done for any CommonJS _or_ ES Module application, but\nthe loader itself must use ESM.\n\n```js\n// loader.mjs\nimport Module from 'node:module'\n\n// the synchronous hooks for newer node versions\nimport { initialize, resolve, load } from '@apm-js-collab/tracing-hooks/hook-sync.mjs'\nimport ModulePatch from '@apm-js-collab/tracing-hooks'\n\n// the instrumentations we want to apply\nconst instrumentations = [\n  {\n    channelName: 'channel1',\n    module: { name: 'pkg1', verisonRange: '>=1.0.0', filePath: 'index.js' },\n    functionQuery: {\n      className: 'Class1',\n      methodName: 'method1',\n      kind: 'Async'\n    }\n  },\n  {\n    channelName: 'channel2',\n    module: { name: 'pkg2', verisonRange: '>=1.0.0', filePath: 'index.js' },\n    functionQuery: {\n      className: 'Class2,\n      methodName: 'method2',\n      kind: 'Sync'\n    }\n  }\n]\n\n// detection to decide module loader hooks to use\n// registerHooks was present but not stable until 24.13 and 25.1\nconst version = (process.versions.node ?? '0.0.0')\n  .split('.')\n  .map(n => parseInt(n, 10))\nconst stableSyncHooks = version[0] > 25 ||\n  version[0] === 25 && version[1] >= 1 ||\n  version[0] === 24 && version[1] >= 13\n\nif (typeof Module.registerHooks === 'function' && stableSyncHooks) {\n  initialize({ instrumentations })\n  Module.registerHooks({ resolve, load })\n} else if (typeof Module.register === 'function') {\n  Module.register('@apm-js-collab/tracing-hooks/hook.mjs', import.meta.url, {\n    data: { instrumentations }\n  });\n\n  // ALSO patch `Module.prototype._compile`. This is REQUIRED, not an\n  // optimisation: on this path CommonJS is instrumented entirely by\n  // the patch, never by the async `load` hook.\n  // Two reasons. When an ESM file `import`s a CJS package, Node\n  // resolves the package's INTERNAL `require()` calls through the CJS\n  // machinery, so they never reach the ESM resolve hook. And handing\n  // `source` back for a CommonJS module from the async `load` hook\n  // makes Node evaluate it on the synchronous require(esm) bridge,\n  // which throws ERR_VM_MODULE_LINK_FAILURE on Node < 24.12 whenever\n  // that module's top-level require() chain reaches an ES module\n  // (https://github.com/nodejs/node/issues/59666).\n  // Neither applies in the registerHooks case, because Node applies\n  // those hooks to all CJS and ESM modules.\n  new ModulePatch({ instrumentations }).patch();\n} else {\n  throw new Error('No available API to apply module load hooks')\n}\n```\n\nTo run your application with these instrumentations applied, pass\nit to the `--import` argument:\n\n```\nnode --import=loader.mjs ./my-app.js\n```\n\n## Debugging\n\nThe [debug module](https://www.npmjs.com/package/debug) is used to provide\ninsight into the patching process. Set `DEBUG='@apm-js-collab*'` to view these\nlogs.\n\nAdditionally, any patched files can be written out by enabling dump mode. This\nis done by setting the environment variable `TRACING_DUMP` to any value. By\ndefault, it will write out file to the system's temporary directory as the\nparent directory. The target parent directory can be configured by setting\nthe `TRACING_DUMP_DIR` environment variable to an absolute path. In either\ncase, the resolved filename of the module being patched is appended. For\nexample, if we are patching `lib/index.js` in the `foo` package, and we set\na base directory of `/tmp/dump/`, then the patched code will be written to\n`/tmp/dump/foo/lib/index.js`.\n\n### Diagnostics Hook\n\nA diagnostics hook can be set which is called every time a module is transformed \nor transformation fails.\n\n```js\nimport { setDiagnosticsHook } from '@apm-js-collab/tracing-hooks/hook-sync.mjs'\n\nsetDiagnosticsHook(({ url, moduleName, error }) => {\n  if(error) {\n    // injection failed\n  } else {\n    // injection succeeded\n  }\n})\n```\n\nWith the synchronous `registerHooks` (and the `_compile` patch) this is all that\nis needed. The older async `register` runs its hooks on a separate thread where\nthe hook set above is not visible, so pass a MessagePort created with\n`createDiagnosticsPort()` for the loader thread to post its diagnostics back:\n\n```js\nimport { setDiagnosticsHook, createDiagnosticsPort } from '@apm-js-collab/tracing-hooks/hook-sync.mjs'\n\nsetDiagnosticsHook(({ url, moduleName, error }) => { /* ... */ })\n\nconst diagnosticsPort = createDiagnosticsPort()\nModule.register('@apm-js-collab/tracing-hooks/hook.mjs', import.meta.url, {\n  data: { instrumentations, diagnosticsPort },\n  transferList: [diagnosticsPort]\n})\n```\n\nOn this path, diagnostics for ES modules are posted from the loader thread and\ntherefore arrive asynchronously, some time after the module was transformed;\ndiagnostics for CommonJS modules come from the `_compile` patch and are emitted\nsynchronously as before. The port does not keep the process alive, so\ndiagnostics still in flight when the process exits are dropped.","publish_time":1755621190792,"_cnpm_publish_time":1755621190792,"dist-tags":{"latest":"0.14.0"}}