{"action":"opened","number":24,"pull_request":{"url":"https://api.github.com/repos/jstty/hyper.io/pulls/24","id":212239826,"node_id":"MDExOlB1bGxSZXF1ZXN0MjEyMjM5ODI2","html_url":"https://github.com/jstty/hyper.io/pull/24","diff_url":"https://github.com/jstty/hyper.io/pull/24.diff","patch_url":"https://github.com/jstty/hyper.io/pull/24.patch","issue_url":"https://api.github.com/repos/jstty/hyper.io/issues/24","number":24,"state":"open","locked":false,"title":"(fix) Bypass hyper.io req --> res pipeline when early response","user":{"login":"noodle","id":4826095,"node_id":"MDQ6VXNlcjQ4MjYwOTU=","avatar_url":"https://avatars0.githubusercontent.com/u/4826095?v=4","gravatar_id":"","url":"https://api.github.com/users/noodle","html_url":"https://github.com/noodle","followers_url":"https://api.github.com/users/noodle/followers","following_url":"https://api.github.com/users/noodle/following{/other_user}","gists_url":"https://api.github.com/users/noodle/gists{/gist_id}","starred_url":"https://api.github.com/users/noodle/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/noodle/subscriptions","organizations_url":"https://api.github.com/users/noodle/orgs","repos_url":"https://api.github.com/users/noodle/repos","events_url":"https://api.github.com/users/noodle/events{/privacy}","received_events_url":"https://api.github.com/users/noodle/received_events","type":"User","site_admin":false},"body":"Hi Joe, hope things are well.  I'm working on something at the moment where I need to redirect to a login screen in a `$preRoute` handler.  e.g.\r\n\r\n```js\r\nasync $preRoute($session, $rawResponse) {\r\n    if (!$session || !$session.user) {\r\n        $rawResponse.redirect('/login');\r\n    }\r\n}\r\n```\r\n\r\nThe problem is I get this warning (non-breaking) in the server logs: \r\n```\r\nPotentially unhandled rejection [3] Error: Can't render headers after they are sent to the client\r\n    at ServerResponse.writeHead (_http_server.js:216:15)\r\n    at ServerResponse.writeHead (/Users/michhart/Desktop/projects/customer-journey/server/node_modules/on-headers/index.js:55:19)\r\n    at ServerResponse.writeHead (/Users/michhart/Desktop/projects/customer-journey/server/node_modules/on-headers/index.js:55:19)\r\n    at ServerResponse.writeHead (/Users/michhart/Desktop/projects/customer-journey/server/node_modules/on-headers/index.js:55:19)\r\n    at /Users/michhart/Desktop/projects/customer-journey/server/node_modules/hyper.io/lib/http.framework.express.js:358:13\r\n    at tryCatchReject (/Users/michhart/Desktop/projects/customer-journey/server/node_modules/when/lib/makePromise.js:845:30)\r\n    at runContinuation1 (/Users/michhart/Desktop/projects/customer-journey/server/node_modules/when/lib/makePromise.js:804:4)\r\n    at Fulfilled.when (/Users/michhart/Desktop/projects/customer-journey/server/node_modules/when/lib/makePromise.js:592:4)\r\n    at Pending.run (/Users/michhart/Desktop/projects/customer-journey/server/node_modules/when/lib/makePromise.js:483:13)\r\n    at Scheduler._drain (/Users/michhart/Desktop/projects/customer-journey/server/node_modules/when/lib/Scheduler.js:62:19)\r\n```\r\nThis happens because the handler at the end of the $preRoute --> route --> $postRoute promise chain will always call `res.writeHead`, which can only be called once.  So that rules out redirecting using `$rawResponse` directly.\r\n\r\nAnother solution I tried was using $error instead of $rawResponse.  e.g.\r\n\r\n```js\r\nasync $preRoute($session, $error) {\r\n    if (!$session || !$session.user) {\r\n        $error(null, 302, { Location: '/login' });\r\n        // return Promise.reject({});   <-- no effect\r\n    }\r\n}\r\n```\r\n\r\nHowever this doesn't work because the route --> $postRoute chain will still be invoked, since 302 isn't a 4xx or 5xx status code.  Thus, I get 302 responses with the content from the route, and without the Location header set (so no redirect).\r\n\r\n**Solution 1**: The first solution is included in this PR.  It uses `$rawResponse.headersSent` to check if a response has already been sent.  If true, the extra handler pipeline logic will be bypassed.  This will make Attempt 1 above work.\r\n\r\n**Solution 2**: Consider status codes >= 300 as error codes instead of only >= 400.  This will make Attempt 2 above work.\r\n\r\nI prefer Solution 1 but its disadvantage is it couples *apiviewRoutes.js* to express.js, which I know is something you wanted to avoid.  What are your thoughts?","created_at":"2018-08-30T23:42:05Z","updated_at":"2018-08-30T23:42:05Z","closed_at":null,"merged_at":null,"merge_commit_sha":null,"assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/jstty/hyper.io/pulls/24/commits","review_comments_url":"https://api.github.com/repos/jstty/hyper.io/pulls/24/comments","review_comment_url":"https://api.github.com/repos/jstty/hyper.io/pulls/comments{/number}","comments_url":"https://api.github.com/repos/jstty/hyper.io/issues/24/comments","statuses_url":"https://api.github.com/repos/jstty/hyper.io/statuses/f6e265340c7f9c2ca91d6d2ac5bc58f3c4769006","head":{"label":"noodle:preroute-fix","ref":"preroute-fix","sha":"f6e265340c7f9c2ca91d6d2ac5bc58f3c4769006","user":{"login":"noodle","id":4826095,"node_id":"MDQ6VXNlcjQ4MjYwOTU=","avatar_url":"https://avatars0.githubusercontent.com/u/4826095?v=4","gravatar_id":"","url":"https://api.github.com/users/noodle","html_url":"https://github.com/noodle","followers_url":"https://api.github.com/users/noodle/followers","following_url":"https://api.github.com/users/noodle/following{/other_user}","gists_url":"https://api.github.com/users/noodle/gists{/gist_id}","starred_url":"https://api.github.com/users/noodle/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/noodle/subscriptions","organizations_url":"https://api.github.com/users/noodle/orgs","repos_url":"https://api.github.com/users/noodle/repos","events_url":"https://api.github.com/users/noodle/events{/privacy}","received_events_url":"https://api.github.com/users/noodle/received_events","type":"User","site_admin":false},"repo":{"id":120026201,"node_id":"MDEwOlJlcG9zaXRvcnkxMjAwMjYyMDE=","name":"hyper.io","full_name":"noodle/hyper.io","owner":{"login":"noodle","id":4826095,"node_id":"MDQ6VXNlcjQ4MjYwOTU=","avatar_url":"https://avatars0.githubusercontent.com/u/4826095?v=4","gravatar_id":"","url":"https://api.github.com/users/noodle","html_url":"https://github.com/noodle","followers_url":"https://api.github.com/users/noodle/followers","following_url":"https://api.github.com/users/noodle/following{/other_user}","gists_url":"https://api.github.com/users/noodle/gists{/gist_id}","starred_url":"https://api.github.com/users/noodle/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/noodle/subscriptions","organizations_url":"https://api.github.com/users/noodle/orgs","repos_url":"https://api.github.com/users/noodle/repos","events_url":"https://api.github.com/users/noodle/events{/privacy}","received_events_url":"https://api.github.com/users/noodle/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/noodle/hyper.io","description":"Microservice Framework for modern webapps","fork":true,"url":"https://api.github.com/repos/noodle/hyper.io","forks_url":"https://api.github.com/repos/noodle/hyper.io/forks","keys_url":"https://api.github.com/repos/noodle/hyper.io/keys{/key_id}","collaborators_url":"https://api.github.com/repos/noodle/hyper.io/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/noodle/hyper.io/teams","hooks_url":"https://api.github.com/repos/noodle/hyper.io/hooks","issue_events_url":"https://api.github.com/repos/noodle/hyper.io/issues/events{/number}","events_url":"https://api.github.com/repos/noodle/hyper.io/events","assignees_url":"https://api.github.com/repos/noodle/hyper.io/assignees{/user}","branches_url":"https://api.github.com/repos/noodle/hyper.io/branches{/branch}","tags_url":"https://api.github.com/repos/noodle/hyper.io/tags","blobs_url":"https://api.github.com/repos/noodle/hyper.io/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/noodle/hyper.io/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/noodle/hyper.io/git/refs{/sha}","trees_url":"https://api.github.com/repos/noodle/hyper.io/git/trees{/sha}","statuses_url":"https://api.github.com/repos/noodle/hyper.io/statuses/{sha}","languages_url":"https://api.github.com/repos/noodle/hyper.io/languages","stargazers_url":"https://api.github.com/repos/noodle/hyper.io/stargazers","contributors_url":"https://api.github.com/repos/noodle/hyper.io/contributors","subscribers_url":"https://api.github.com/repos/noodle/hyper.io/subscribers","subscription_url":"https://api.github.com/repos/noodle/hyper.io/subscription","commits_url":"https://api.github.com/repos/noodle/hyper.io/commits{/sha}","git_commits_url":"https://api.github.com/repos/noodle/hyper.io/git/commits{/sha}","comments_url":"https://api.github.com/repos/noodle/hyper.io/comments{/number}","issue_comment_url":"https://api.github.com/repos/noodle/hyper.io/issues/comments{/number}","contents_url":"https://api.github.com/repos/noodle/hyper.io/contents/{+path}","compare_url":"https://api.github.com/repos/noodle/hyper.io/compare/{base}...{head}","merges_url":"https://api.github.com/repos/noodle/hyper.io/merges","archive_url":"https://api.github.com/repos/noodle/hyper.io/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/noodle/hyper.io/downloads","issues_url":"https://api.github.com/repos/noodle/hyper.io/issues{/number}","pulls_url":"https://api.github.com/repos/noodle/hyper.io/pulls{/number}","milestones_url":"https://api.github.com/repos/noodle/hyper.io/milestones{/number}","notifications_url":"https://api.github.com/repos/noodle/hyper.io/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/noodle/hyper.io/labels{/name}","releases_url":"https://api.github.com/repos/noodle/hyper.io/releases{/id}","deployments_url":"https://api.github.com/repos/noodle/hyper.io/deployments","created_at":"2018-02-02T20:09:40Z","updated_at":"2018-02-02T20:09:42Z","pushed_at":"2018-08-30T23:41:12Z","git_url":"git://github.com/noodle/hyper.io.git","ssh_url":"git@github.com:noodle/hyper.io.git","clone_url":"https://github.com/noodle/hyper.io.git","svn_url":"https://github.com/noodle/hyper.io","homepage":"","size":530,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"other","name":"Other","spdx_id":null,"url":null,"node_id":"MDc6TGljZW5zZTA="},"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"jstty:master","ref":"master","sha":"ef7d831232a36051d013041c73cc8e4d1a813537","user":{"login":"jstty","id":3078120,"node_id":"MDQ6VXNlcjMwNzgxMjA=","avatar_url":"https://avatars1.githubusercontent.com/u/3078120?v=4","gravatar_id":"","url":"https://api.github.com/users/jstty","html_url":"https://github.com/jstty","followers_url":"https://api.github.com/users/jstty/followers","following_url":"https://api.github.com/users/jstty/following{/other_user}","gists_url":"https://api.github.com/users/jstty/gists{/gist_id}","starred_url":"https://api.github.com/users/jstty/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jstty/subscriptions","organizations_url":"https://api.github.com/users/jstty/orgs","repos_url":"https://api.github.com/users/jstty/repos","events_url":"https://api.github.com/users/jstty/events{/privacy}","received_events_url":"https://api.github.com/users/jstty/received_events","type":"User","site_admin":false},"repo":{"id":21785603,"node_id":"MDEwOlJlcG9zaXRvcnkyMTc4NTYwMw==","name":"hyper.io","full_name":"jstty/hyper.io","owner":{"login":"jstty","id":3078120,"node_id":"MDQ6VXNlcjMwNzgxMjA=","avatar_url":"https://avatars1.githubusercontent.com/u/3078120?v=4","gravatar_id":"","url":"https://api.github.com/users/jstty","html_url":"https://github.com/jstty","followers_url":"https://api.github.com/users/jstty/followers","following_url":"https://api.github.com/users/jstty/following{/other_user}","gists_url":"https://api.github.com/users/jstty/gists{/gist_id}","starred_url":"https://api.github.com/users/jstty/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jstty/subscriptions","organizations_url":"https://api.github.com/users/jstty/orgs","repos_url":"https://api.github.com/users/jstty/repos","events_url":"https://api.github.com/users/jstty/events{/privacy}","received_events_url":"https://api.github.com/users/jstty/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/jstty/hyper.io","description":"Microservice Framework for modern webapps","fork":false,"url":"https://api.github.com/repos/jstty/hyper.io","forks_url":"https://api.github.com/repos/jstty/hyper.io/forks","keys_url":"https://api.github.com/repos/jstty/hyper.io/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jstty/hyper.io/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jstty/hyper.io/teams","hooks_url":"https://api.github.com/repos/jstty/hyper.io/hooks","issue_events_url":"https://api.github.com/repos/jstty/hyper.io/issues/events{/number}","events_url":"https://api.github.com/repos/jstty/hyper.io/events","assignees_url":"https://api.github.com/repos/jstty/hyper.io/assignees{/user}","branches_url":"https://api.github.com/repos/jstty/hyper.io/branches{/branch}","tags_url":"https://api.github.com/repos/jstty/hyper.io/tags","blobs_url":"https://api.github.com/repos/jstty/hyper.io/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jstty/hyper.io/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jstty/hyper.io/git/refs{/sha}","trees_url":"https://api.github.com/repos/jstty/hyper.io/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jstty/hyper.io/statuses/{sha}","languages_url":"https://api.github.com/repos/jstty/hyper.io/languages","stargazers_url":"https://api.github.com/repos/jstty/hyper.io/stargazers","contributors_url":"https://api.github.com/repos/jstty/hyper.io/contributors","subscribers_url":"https://api.github.com/repos/jstty/hyper.io/subscribers","subscription_url":"https://api.github.com/repos/jstty/hyper.io/subscription","commits_url":"https://api.github.com/repos/jstty/hyper.io/commits{/sha}","git_commits_url":"https://api.github.com/repos/jstty/hyper.io/git/commits{/sha}","comments_url":"https://api.github.com/repos/jstty/hyper.io/comments{/number}","issue_comment_url":"https://api.github.com/repos/jstty/hyper.io/issues/comments{/number}","contents_url":"https://api.github.com/repos/jstty/hyper.io/contents/{+path}","compare_url":"https://api.github.com/repos/jstty/hyper.io/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jstty/hyper.io/merges","archive_url":"https://api.github.com/repos/jstty/hyper.io/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jstty/hyper.io/downloads","issues_url":"https://api.github.com/repos/jstty/hyper.io/issues{/number}","pulls_url":"https://api.github.com/repos/jstty/hyper.io/pulls{/number}","milestones_url":"https://api.github.com/repos/jstty/hyper.io/milestones{/number}","notifications_url":"https://api.github.com/repos/jstty/hyper.io/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jstty/hyper.io/labels{/name}","releases_url":"https://api.github.com/repos/jstty/hyper.io/releases{/id}","deployments_url":"https://api.github.com/repos/jstty/hyper.io/deployments","created_at":"2014-07-13T07:53:08Z","updated_at":"2018-04-22T12:41:06Z","pushed_at":"2018-02-05T17:43:58Z","git_url":"git://github.com/jstty/hyper.io.git","ssh_url":"git@github.com:jstty/hyper.io.git","clone_url":"https://github.com/jstty/hyper.io.git","svn_url":"https://github.com/jstty/hyper.io","homepage":"","size":523,"stargazers_count":25,"watchers_count":25,"language":"JavaScript","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4,"mirror_url":null,"archived":false,"open_issues_count":20,"license":{"key":"other","name":"Other","spdx_id":null,"url":null,"node_id":"MDc6TGljZW5zZTA="},"forks":4,"open_issues":20,"watchers":25,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/jstty/hyper.io/pulls/24"},"html":{"href":"https://github.com/jstty/hyper.io/pull/24"},"issue":{"href":"https://api.github.com/repos/jstty/hyper.io/issues/24"},"comments":{"href":"https://api.github.com/repos/jstty/hyper.io/issues/24/comments"},"review_comments":{"href":"https://api.github.com/repos/jstty/hyper.io/pulls/24/comments"},"review_comment":{"href":"https://api.github.com/repos/jstty/hyper.io/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/jstty/hyper.io/pulls/24/commits"},"statuses":{"href":"https://api.github.com/repos/jstty/hyper.io/statuses/f6e265340c7f9c2ca91d6d2ac5bc58f3c4769006"}},"author_association":"NONE","merged":false,"mergeable":null,"rebaseable":null,"mergeable_state":"unknown","merged_by":null,"comments":0,"review_comments":0,"maintainer_can_modify":true,"commits":1,"additions":10,"deletions":0,"changed_files":2}}
{"actor":{"display_login":"noodle"}}