### Install als-context-watch Source: https://github.com/agentscope-java/als-context-watch/blob/main/README.md Install the package via npm. Requires Node >= 20.6 and has no runtime dependencies. ```sh npm install als-context-watch ``` -------------------------------- ### Run tests and build with Node Source: https://github.com/agentscope-java/als-context-watch/blob/main/README.md Run the test suite and example directly with Node's test runner, then build and test the distribution as CI does. Requires Node 24+ for type stripping when running tests directly. ```sh node --test "test/*.test.ts" # full suite, needs node 24+ for type stripping node examples/overhead-demo.ts npm run build && npm run test:dist # what CI runs against node 20 and 22 ``` -------------------------------- ### Watch AsyncLocalStorage context with watchContext Source: https://github.com/agentscope-java/als-context-watch/blob/main/README.md Sets up context watching for an AsyncLocalStorage instance. Patches EventEmitter methods globally to capture stack traces when listeners are attached with an active context, and calls onLost if a listener fires without the context. Stop the watcher with watcher.stop() when done. ```js import { AsyncLocalStorage } from 'node:async_hooks'; import { watchContext } from 'als-context-watch'; const als = new AsyncLocalStorage(); const watcher = watchContext(als, { onLost: ({ boundary, stack, expectedStore }) => { log.warn(`ALS context lost at ${boundary}`, { expectedStore, attachedAt: stack }); }, }); // ... run your app ... watcher.stop(); // unpatch, e.g. at the end of a test or when leaving dev mode ``` -------------------------------- ### Wrap non-EventEmitter callbacks with watcher.wrap() Source: https://github.com/agentscope-java/als-context-watch/blob/main/README.md Wraps a callback that is not registered via EventEmitter, such as one pushed onto a plain array. The wrap() method snapshots the store at call time and reports through onLost if the wrapped function runs with an empty store. It is a no-op when there is no context to watch. ```js const watcher = watchContext(als, { onLost }); jobQueue.push(watcher.wrap(() => sendWebhook(payload), 'webhook retry callback')); ``` -------------------------------- ### AsyncLocalStorage context loss with EventEmitter Source: https://github.com/agentscope-java/als-context-watch/blob/main/README.md Shows the problem: an AsyncLocalStorage context is entered once for the whole request, but a later EventEmitter callback runs outside that context, so the requestId is missing from the log line. The synchronous emit works, but the later emit from a different call stack does not carry the context. ```javascript const als = new AsyncLocalStorage(); app.use((req, res, next) => als.run({ requestId: req.id }, next)); app.get('/import', (req, res) => { jobBus.on('progress', (pct) => { log.info(`import ${pct}%`); // no requestId — which import? whose? }); jobBus.emit('progress', 10); // happens to work: same tick, same context startImportJob(req.body); // emits 'progress' again, later, from elsewhere }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.