Title: Integrate AsyncLocalStorage with EventEmitter · Issue #33723 · nodejs/node · GitHub
Open Graph Title: Integrate AsyncLocalStorage with EventEmitter · Issue #33723 · nodejs/node
X Title: Integrate AsyncLocalStorage with EventEmitter · Issue #33723 · nodejs/node
Description: Is your feature request related to a problem? Please describe. I'm going to port this library to AsyncLocalStorage. As a part of this migration I need to make ALS play nicely with events emitted by request and response objects. Current v...
Open Graph Description: Is your feature request related to a problem? Please describe. I'm going to port this library to AsyncLocalStorage. As a part of this migration I need to make ALS play nicely with events emitted by...
X Description: Is your feature request related to a problem? Please describe. I'm going to port this library to AsyncLocalStorage. As a part of this migration I need to make ALS play nicely with events emitte...
Opengraph URL: https://github.com/nodejs/node/issues/33723
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Integrate AsyncLocalStorage with EventEmitter","articleBody":"\u003c!--\r\nThank you for suggesting an idea to make Node.js better.\r\n\r\nPlease fill in as much of the template below as you're able.\r\n-->\r\n\r\n**Is your feature request related to a problem? Please describe.**\r\n\r\nI'm going to port [this library](https://github.com/puzpuzpuz/cls-rtracer) to `AsyncLocalStorage`. As a part of this migration I need to make ALS play nicely with events emitted by request and response objects. Current version of the library uses [`ns.bindEmitter()`](https://github.com/Jeff-Lewis/cls-hooked#namespacebindemitteremitter) method available in `cls-hooked` (it's also present in `continuation-local-storage`).\r\n\r\n**Describe the solution you'd like**\r\n\r\nIt's enough to add `.bindEmitter(emitter, store)` method into ALS:\r\n\r\n```js\r\nconst store = { foo: 'bar' };\r\nals.bindEmitter(req, store);\r\n\r\nreq.on('close', () => {\r\n console.log(als.getStore()); // prints \"{ foo: 'bar' }\"\r\n});\r\n```\r\n\r\n**Describe alternatives you've considered**\r\n\r\nOf course, this integration can be implemented as a user-land module. But I believe that this will be a common case among ALS users, so it's probably worth to consider integrating ALS with `EventEmitter` in the core.\r\n\r\nAs more of such changes can bloat ALS API, I'd like to hear opinions from @nodejs/async_hooks before starting to work on the implementation.\r\n\r\n**Update**. The consensus is to add something like `AsyncResource#bind()` method that would allow to do the following:\r\n\r\n```js\r\nals.run({ foo: 'bar' }, () => {\r\n AsyncResource res = new AsyncResource('foobar');\r\n req.on('close', res.bind(() => {\r\n console.log(als.getStore()); // prints \"{ foo: 'bar' }\"\r\n }));\r\n});\r\n```","author":{"url":"https://github.com/puzpuzpuz","@type":"Person","name":"puzpuzpuz"},"datePublished":"2020-06-04T09:21:21Z","url":"https://github.com/nodejs/node/issues/33723"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:b6882140-10cd-a70c-69a7-25fb84e3cff8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 85E0:E7908:733968:A260F0:6B047E68 |
| html-safe-nonce | 32482ed0f3aa001dfd9f88b7f6d777c75908d2b69ea39a634ed88bcbf9fc0139 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NUUwOkU3OTA4OjczMzk2ODpBMjYwRjA6NkIwNDdFNjgiLCJ2aXNpdG9yX2lkIjoiODMzNDI5OTM0NzIyMzM0Njc5MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 288fe2e5891f84622c75ab45d1a80998412dd5182ac1c6418b17c79cbca2f030 |
| hovercard-subject-tag | issue:630657628 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/nodejs/node/33723/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f4b445c0ce3d93defa08c2b2f64b424180325da427253386be9973786c4cc0ad/nodejs/node/issues/33723 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f4b445c0ce3d93defa08c2b2f64b424180325da427253386be9973786c4cc0ad/nodejs/node/issues/33723 |
| og:image:alt | Is your feature request related to a problem? Please describe. I'm going to port this library to AsyncLocalStorage. As a part of this migration I need to make ALS play nicely with events emitted by... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | puzpuzpuz |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0366ddfb372682a32e92a5d86155cb08fa83e5bbe79953a968789f2f7e24c511 |
| turbo-cache-control | no-preview |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ed3f2e01f6b7672780c586c8af0e72726374d7b6 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width