Skip to content
Merged
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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,21 @@ case, the resolved filename of the module being patched is appended. For
example, if we are patching `lib/index.js` in the `foo` package, and we set
a base directory of `/tmp/dump/`, then the patched code will be written to
`/tmp/dump/foo/lib/index.js`.

### Diagnostics Hook

A diagnostics hook can be set which is called every time a module is transformed
or transformation fails. This hook will only work with the synchronous
`registerHooks` because the older `register` runs in a different thread.

```js
import { setDiagnosticsHook } from '@apm-js-collab/tracing-hooks/hook-sync.mjs'

setDiagnosticsHook(({ url, moduleName, error }) => {
if(error) {
// injection failed
} else {
// injection succeeded
}
})
```
2 changes: 1 addition & 1 deletion hook-sync.mjs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { initializeSync as initialize, loadSync as load, resolveSync as resolve } from './hook.mjs'
export { initializeSync as initialize, loadSync as load, resolveSync as resolve, setDiagnosticsHook } from './hook.mjs'
14 changes: 13 additions & 1 deletion hook.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ let transformers = null
let packages = null
let instrumentator = null

let diagnosticsHook;

export function setDiagnosticsHook(hook) {
diagnosticsHook = hook
}
Comment thread
timfish marked this conversation as resolved.

export async function initialize(data = {}) {
return initializeSync(data)
}
Expand Down Expand Up @@ -79,12 +85,18 @@ export function loadResult(url, result) {
const transformedCode = transformer.transform(code.toString('utf8'), 'unknown')
result.source = transformedCode?.code
result.shortCircuit = true
if (diagnosticsHook) {
diagnosticsHook({ url, moduleName: transformer.moduleName })
}
} catch(err) {
debug('Error transforming module %s: %o', url, err)
if (diagnosticsHook) {
diagnosticsHook({ url, moduleName: transformer.moduleName, error: err })
}
} finally {
transformer.free()
}
}

return result
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"lib"
],
"dependencies": {
"@apm-js-collab/code-transformer": "^0.13.0",
"@apm-js-collab/code-transformer": "^0.14.0",
"debug": "^4.4.1",
"module-details-from-path": "^1.0.4"
},
Expand Down
22 changes: 22 additions & 0 deletions test/hook-sync.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,25 @@ test('should default initialization to not crash if not defined', async (t) => {
const snapshot = await snap(result.source)
assert.deepEqual(result.source, snapshot)
})

test('should rewrite code and call diagnostics hook', async (t) => {
const { syncLoaderRewriter, snap } = t.ctx
syncLoaderRewriter.setDiagnosticsHook(({url, moduleName, error}) => {
assert.equal(url, `file://${esmPath}`)
assert.equal(moduleName, 'esm-pkg')
assert.equal(error, undefined)
})
const esmPath = path.join(import.meta.dirname, './example-deps/lib/node_modules/esm-pkg/foo.js')
function resolveFn() {
return { url: `file://${esmPath}` }
}
function nextLoad() {
const data = readFileSync(esmPath, 'utf8')
return {
format: 'module',
source: data
}
}
const url = syncLoaderRewriter.resolve('esm-pkg', {}, resolveFn)
syncLoaderRewriter.load(url.url, {}, nextLoad)
})
Loading