{"id":17749467,"url":"https://github.com/oresoftware/domain-haven","last_synced_at":"2025-05-13T01:32:38.615Z","repository":{"id":57214867,"uuid":"126378026","full_name":"ORESoftware/domain-haven","owner":"ORESoftware","description":"Express middleware - pin runtime errors to a request/response pair using domains. Helps avoid needlessly shutting down servers.","archived":false,"fork":false,"pushed_at":"2024-03-29T11:33:47.000Z","size":125,"stargazers_count":5,"open_issues_count":4,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-20T01:16:25.926Z","etag":null,"topics":["express-middleware","expressjs","node-core","nodecore","nodejs"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ORESoftware.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-03-22T18:22:08.000Z","updated_at":"2023-08-16T00:41:39.000Z","dependencies_parsed_at":"2022-08-26T13:41:24.119Z","dependency_job_id":"249dd618-db15-4118-87c4-65a330d258c2","html_url":"https://github.com/ORESoftware/domain-haven","commit_stats":{"total_commits":42,"total_committers":2,"mean_commits":21.0,"dds":"0.16666666666666663","last_synced_commit":"f1d50567c01cd62f03743f3edc6aafa99a64e2c2"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ORESoftware%2Fdomain-haven","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ORESoftware%2Fdomain-haven/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ORESoftware%2Fdomain-haven/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ORESoftware%2Fdomain-haven/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ORESoftware","download_url":"https://codeload.github.com/ORESoftware/domain-haven/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253854093,"owners_count":21974223,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["express-middleware","expressjs","node-core","nodecore","nodejs"],"created_at":"2024-10-26T11:23:26.116Z","updated_at":"2025-05-13T01:32:38.588Z","avatar_url":"https://github.com/ORESoftware.png","language":"TypeScript","readme":"\n\n# Domain-Haven\n\n### Caveat\n\nWorks on Node.js version before 12. Currently not working on Node.js version 12.\n\n## About\n\nThis is an awesome module for using Node.js domains to capture runtime errors and \u003cbr\u003e\npin errors / exceptions / unhandled-rejections to a particular request/response.\n\n```js\n\nimport * as express from 'express';\nimport haven from 'domain-haven';\n\nconst app = express();\napp.use(haven());\n\nexport {app};\n\n```\n\n### In Production\n\n```bash\n### use these env vars to switch off stack-traces from leaking sensitive data to client(s):\n\nexport NODE_ENV=prod\nexport NODE_ENV=production\nexport DOMAIN_HAVEN_PROD=true\n\n```\n\nalternatively, use this option to not reveal stack-traces programmatically:\n\n\n```typescript\n\nimport * as haven from 'domain-haven';\n\nhaven.middleware({\n  opts: {\n    revealStackTraces: false\n  }\n});\n\n// where the above is equivalent to:\n\nimport haven from 'domain-haven';\n\nhaven({\n  opts: {\n    revealStackTraces: false\n  }\n});\n\n\n\n```\n### In Production, continued:\n\nIn some rare cases, we may wish to enable stack traces shown to the client for a particular request.\nYou can override behavior for a particular request by doing this:\n\n\n```typescript\n\napp.use((req,res,next) =\u003e {\n  if(req.query.secret === 'magic-spell'){  // you control this, serverside\n    req.havenOpts = {revealStackTraces: true, overrideProd: true};\n  }\n  next()\n});\n\n```\n\n# Behavior\n\nBy default, if an `uncaughtException` or `unhandledRejection` occurs and a request/response pair can be \n\u003cbr\u003e\npinned to the event, a JSON error response will be sent.  If the event cannot be pinned to\n\u003cbr\u003e\nto a particular request/response, Haven will shutdown the process.\n\n_______________________________________________\n\nOn the other hand, if the developer wants full control of what response to send, etc, use:\n\n```js\napp.use(haven({opts:{auto:false}}));\n\n\n```\n\n\n\n# Performance\nTBD\n\n\n# Usage\nTBD\n\n\n### If you just want to capture errors that don't make it to the global scope, use:\n\n```js\napp.use(haven({handleGlobalErrors: false}));\n```\n\n### To just show error messages, but not the full stack trace, use:\n\n```js\napp.use(haven({showStackTracesInResponse: false}));\n```\n\n\n\n## How it works:\n\nWhat we do is use something similar to this beautiful piece of middleware:\n\n```js \nconst havenMiddleware = function (req, res, next) {\n    \n    const d = domain.create(); // create a new domain for this request\n    \n    res.once('finish', () =\u003e {\n      d.exit();\n    });\n    \n    d.once('error',  (e) =\u003e {\n        res.status(500).json({error: e});\n    });\n    \n    d.run(next);  // we invoke the next middleware\n    \n};\n\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foresoftware%2Fdomain-haven","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foresoftware%2Fdomain-haven","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foresoftware%2Fdomain-haven/lists"}