debugging-aid
Version:
Experimental tools for debugging Node.js apps without pausing
87 lines (73 loc) • 1.83 kB
JavaScript
const inspector = require('inspector')
const session = new inspector.Session()
const iliketoleakyleaky = {
myspoon: 'my spoon is too big'
}
let collectable = {
ishallbe: 'collected!'
}
const {
Worker,
isMainThread,
parentPort,
workerData
} = require('worker_threads')
if (isMainThread) {
mainThread()
} else {
worker()
}
function mainThread () {
const worker = new Worker(__filename)
worker.on('message', console.log)
worker.on('error', console.error)
session.connect()
let started = false
session.on('HeapProfiler.addHeapSnapshotChunk', (m) => {
if (!started) {
worker.postMessage({ command: 'start' })
started = true
}
worker.postMessage({ command: 'chunk', chunk: m.params.chunk })
})
session.post('HeapProfiler.takeHeapSnapshot', null, (err, r) => {
worker.postMessage({ command: 'end' })
started = false
console.log('HeapProfiler.takeHeapSnapshot done:', err, r)
collectable = { newnewnew: 'stuffstuff' }
session.post('HeapProfiler.takeHeapSnapshot', null, (err, r) => {
worker.postMessage({ command: 'end' })
started = false
console.log('HeapProfiler.takeHeapSnapshot done:', err, r)
session.disconnect()
})
})
}
function worker () {
let previous, current, currentString
const logic = () => {
console.log(current)
// reformat to extract meaningful data
// diff
// find largest items
// extract and save
}
const commands = {
start (message) {
previous = current
current = null
currentString = ''
},
chunk (message) {
currentString += message.chunk
},
end (message) {
current = JSON.parse(currentString)
logic()
}
}
parentPort.on('message', (message) => {
commands[message.command](message)
})
}