|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { executionAsyncId, createHook, AsyncResource } = require('node:async_hooks') |
| 4 | +const { EventEmitter } = require('node:events') |
| 5 | + |
| 6 | +class CustomResource extends AsyncResource { |
| 7 | + constructor(type, traceId) { |
| 8 | + super(type) |
| 9 | + |
| 10 | + this.traceId = traceId |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +class AsyncWatcher extends EventEmitter { |
| 15 | + setupInitHook() { |
| 16 | + // init is called during object construction. The resource may not have |
| 17 | + // completed construction when this callback runs, therefore all fields of the |
| 18 | + // resource referenced by "asyncId" may not have been populated. |
| 19 | + this.init = (asyncId, type, triggerAsyncId, resource) => { |
| 20 | + this.emit('INIT', { |
| 21 | + asyncId, |
| 22 | + type, |
| 23 | + triggerAsyncId, |
| 24 | + executionAsyncId: executionAsyncId(), |
| 25 | + resource, |
| 26 | + }) |
| 27 | + } |
| 28 | + return this |
| 29 | + } |
| 30 | + |
| 31 | + setupDestroyHook() { |
| 32 | + // Destroy is called when an AsyncWrap instance is destroyed. |
| 33 | + this.destroy = (asyncId) => { |
| 34 | + this.emit('DESTROY', { |
| 35 | + asyncId, |
| 36 | + executionAsyncId: executionAsyncId(), |
| 37 | + }) |
| 38 | + } |
| 39 | + return this |
| 40 | + } |
| 41 | + |
| 42 | + start() { |
| 43 | + createHook({ |
| 44 | + init: this.init.bind(this), |
| 45 | + destroy: this.destroy.bind(this), |
| 46 | + }).enable() |
| 47 | + |
| 48 | + return this |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +class AsyncHookContainer { |
| 53 | + constructor(types) { |
| 54 | + const checkedTypes = types |
| 55 | + |
| 56 | + const idMap = new Map() |
| 57 | + const resourceMap = new Map() |
| 58 | + const watcher = new AsyncWatcher() |
| 59 | + const check = (t) => { |
| 60 | + try { |
| 61 | + return checkedTypes.includes(t) |
| 62 | + } catch (err) { |
| 63 | + return false |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + watcher |
| 68 | + .setupInitHook() |
| 69 | + .setupDestroyHook() |
| 70 | + .start() |
| 71 | + .on('INIT', ({ asyncId, type, resource, triggerAsyncId }) => { |
| 72 | + idMap.set(asyncId, triggerAsyncId) |
| 73 | + |
| 74 | + if (check(type)) { |
| 75 | + resourceMap.set(asyncId, resource) |
| 76 | + } |
| 77 | + }) |
| 78 | + .on('DESTROY', ({ asyncId }) => { |
| 79 | + idMap.delete(asyncId) |
| 80 | + resourceMap.delete(asyncId) |
| 81 | + }) |
| 82 | + |
| 83 | + this.types = checkedTypes |
| 84 | + this.idMap = idMap |
| 85 | + this.resourceMap = resourceMap |
| 86 | + this.watcher = watcher |
| 87 | + } |
| 88 | + |
| 89 | + getStore(asyncId) { |
| 90 | + let resource = this.resourceMap.get(asyncId) |
| 91 | + |
| 92 | + if (resource != null) { |
| 93 | + return resource |
| 94 | + } |
| 95 | + |
| 96 | + let id = this.idMap.get(asyncId) |
| 97 | + let sentinel = 0 |
| 98 | + |
| 99 | + while (id != null && sentinel < 100) { |
| 100 | + resource = this.resourceMap.get(id) |
| 101 | + |
| 102 | + if (resource != null) { |
| 103 | + return resource |
| 104 | + } |
| 105 | + |
| 106 | + id = this.idMap.get(id) |
| 107 | + sentinel += 1 |
| 108 | + } |
| 109 | + |
| 110 | + return undefined |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +module.exports = { |
| 115 | + AsyncWatcher, |
| 116 | + AsyncHookContainer, |
| 117 | + CustomResource, |
| 118 | +} |
0 commit comments