{"action":"created","comment":{"url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/pulls/comments/37581478","id":37581478,"diff_hunk":"@@ -23,39 +26,78 @@ class ExecSession {\n       Cmd: options.command,\n     };\n     this.attachOptions = {\n+      stream: true,\n       stdin: true,\n       stdout: true,\n       stderr: true,\n-      stream: true,\n     };\n     this.container = options.container;\n-    this.socket = options.socket;\n     this.server = options.server;\n+    this.socket = options.socket;\n+    this.closed = false;\n+\n+    //must set this before this.execute() in case of premature close\n+    this.socket.on('close', () => {\n+      //should be how to ctrl+c ctrl+d, might be better way to kill\n+      debug('client close');\n+      //for now, it kills this session\n+      this.close();\n+    });\n   }\n \n-  async execute () {\n+  async execute() {\n     //TODO: add error handling support here\n     this.exec = await this.container.exec(this.execOptions);\n     this.execStream = await this.exec.start(this.attachOptions);\n+    // this.execStream = await new Promise(async (accept, reject) => {\n+    //   var req = http.request({\n+    //     socketPath: '/var/run/docker.sock', //replace with generic later\n+    //     path: '/exec/' + this.exec.id + '/start?' + qs.stringify(this.attachOptions),\n+    //     method: 'POST',\n+    //     headers: {\n+    //       'Content-Type': 'application/json',\n+    //       'Connection': 'upgrade',\n+    //       'Upgrade': 'tcp',\n+    //     }\n+    //   }, reject);\n+    //   req.write(JSON.stringify({\n+    //     Detach: false,\n+    //     Tty: this.options.tty,\n+    //   }));\n+    //   req.end();\n+    //   req.on('upgrade', accept);\n+    //   req.on('error', reject);\n+    //   req.setTimeout(10000, reject);\n+    // });\n+    // debug(this.execStream);\n+    // this.execStream.write(new Buffer([30]));\n \n-    //handling output\n-    this.strout = through((data) => {\n-      this.sendMessage(msgcode.stdout, new Buffer(data));\n-    });\n+    var header = null;\n \n-    this.strerr = through((data) => {\n-      this.sendMessage(msgcode.stderr, new Buffer(data));\n-    });\n+    this.execStream.on('readable', () => {\n+      header = header || this.execStream.read(8);\n+      while (header !== null) {\n+        var type = header.readUInt8(0);\n+        var payload = this.execStream.read(header.readUInt32BE(4));\n+        if (payload === null) {\n+          break;\n+        }\n+        if (!this.sendMessage(type, payload)) {\n+          this.execStream.pause();\n+          this.strbuf.on('drain', this.execStream.resume);\n+        }\n \n-    this.exec.modem.demuxStream(this.execStream, this.strout, this.strerr);\n+        //try to set new header to continue reading\n+        header = this.execStream.read(8);\n+      }\n+    });\n     //This stream is created solely for the purposes of pausing, because\n     //data will only buffer up in streams using this.queue()\n-    this.strbuf = through();\n+    this.strbuf = through2();\n \n-    const MAX_OUTSTANDING_BYTES = 8 * 1024 * 1024;\n     this.outstandingBytes = 0;\n \n-    this.strbuf.pipe(through((data) => {\n+    this.strbuf.pipe(through2((data, enc, cb) => {","path":"src/server.js","position":116,"original_position":116,"commit_id":"25ba10ce9d38d8aaa6801f4ea9c9c063397d55bc","original_commit_id":"25ba10ce9d38d8aaa6801f4ea9c9c063397d55bc","user":{"login":"jonasfj","id":149732,"avatar_url":"https://avatars.githubusercontent.com/u/149732?v=3","gravatar_id":"","url":"https://api.github.com/users/jonasfj","html_url":"https://github.com/jonasfj","followers_url":"https://api.github.com/users/jonasfj/followers","following_url":"https://api.github.com/users/jonasfj/following{/other_user}","gists_url":"https://api.github.com/users/jonasfj/gists{/gist_id}","starred_url":"https://api.github.com/users/jonasfj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jonasfj/subscriptions","organizations_url":"https://api.github.com/users/jonasfj/orgs","repos_url":"https://api.github.com/users/jonasfj/repos","events_url":"https://api.github.com/users/jonasfj/events{/privacy}","received_events_url":"https://api.github.com/users/jonasfj/received_events","type":"User","site_admin":false},"body":"Don't pipe.. this is bad... if you wanted to you should use node flow control... not call pause/resume..\r\nHow about\r\n```js\r\nthis.strbuf.on('data', (data) => {\r\n  .... // call strbuf.pause/resume as needed\r\n});\r\nthis.strbuf.resume(); // or maybe not do that here... but wait for client to send `resume`...\r\n```","created_at":"2015-08-20T21:09:11Z","updated_at":"2015-08-20T21:09:11Z","html_url":"https://github.com/taskcluster/docker-exec-websocket-server/pull/4#discussion_r37581478","pull_request_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/pulls/4","_links":{"self":{"href":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/pulls/comments/37581478"},"html":{"href":"https://github.com/taskcluster/docker-exec-websocket-server/pull/4#discussion_r37581478"},"pull_request":{"href":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/pulls/4"}}},"pull_request":{"url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/pulls/4","id":42972403,"html_url":"https://github.com/taskcluster/docker-exec-websocket-server/pull/4","diff_url":"https://github.com/taskcluster/docker-exec-websocket-server/pull/4.diff","patch_url":"https://github.com/taskcluster/docker-exec-websocket-server/pull/4.patch","issue_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/issues/4","number":4,"state":"open","locked":false,"title":"Stream refactor","user":{"login":"EggyLv999","id":2454672,"avatar_url":"https://avatars.githubusercontent.com/u/2454672?v=3","gravatar_id":"","url":"https://api.github.com/users/EggyLv999","html_url":"https://github.com/EggyLv999","followers_url":"https://api.github.com/users/EggyLv999/followers","following_url":"https://api.github.com/users/EggyLv999/following{/other_user}","gists_url":"https://api.github.com/users/EggyLv999/gists{/gist_id}","starred_url":"https://api.github.com/users/EggyLv999/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/EggyLv999/subscriptions","organizations_url":"https://api.github.com/users/EggyLv999/orgs","repos_url":"https://api.github.com/users/EggyLv999/repos","events_url":"https://api.github.com/users/EggyLv999/events{/privacy}","received_events_url":"https://api.github.com/users/EggyLv999/received_events","type":"User","site_admin":false},"body":"Includes through->through2 and the other stream stuff (refactoring the demux function). \r\n\r\nSometimes resize doesn't cause docker to do anything. Only happens when run in conjunction with other tests, but I have no idea what would be causing it (the server does properly receive the resize message and pass it on to the docker container, but somehow the docker container doesn't get or ignores the message). Otherwise, tests pass.","created_at":"2015-08-20T20:09:01Z","updated_at":"2015-08-20T21:09:11Z","closed_at":null,"merged_at":null,"merge_commit_sha":"124ce0420686a1c9015007aae6d02f9d2bf72422","assignee":{"login":"jonasfj","id":149732,"avatar_url":"https://avatars.githubusercontent.com/u/149732?v=3","gravatar_id":"","url":"https://api.github.com/users/jonasfj","html_url":"https://github.com/jonasfj","followers_url":"https://api.github.com/users/jonasfj/followers","following_url":"https://api.github.com/users/jonasfj/following{/other_user}","gists_url":"https://api.github.com/users/jonasfj/gists{/gist_id}","starred_url":"https://api.github.com/users/jonasfj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jonasfj/subscriptions","organizations_url":"https://api.github.com/users/jonasfj/orgs","repos_url":"https://api.github.com/users/jonasfj/repos","events_url":"https://api.github.com/users/jonasfj/events{/privacy}","received_events_url":"https://api.github.com/users/jonasfj/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/pulls/4/commits","review_comments_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/pulls/4/comments","review_comment_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/pulls/comments{/number}","comments_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/issues/4/comments","statuses_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/statuses/25ba10ce9d38d8aaa6801f4ea9c9c063397d55bc","head":{"label":"taskcluster:stream-refactor","ref":"stream-refactor","sha":"25ba10ce9d38d8aaa6801f4ea9c9c063397d55bc","user":{"login":"taskcluster","id":6257436,"avatar_url":"https://avatars.githubusercontent.com/u/6257436?v=3","gravatar_id":"","url":"https://api.github.com/users/taskcluster","html_url":"https://github.com/taskcluster","followers_url":"https://api.github.com/users/taskcluster/followers","following_url":"https://api.github.com/users/taskcluster/following{/other_user}","gists_url":"https://api.github.com/users/taskcluster/gists{/gist_id}","starred_url":"https://api.github.com/users/taskcluster/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/taskcluster/subscriptions","organizations_url":"https://api.github.com/users/taskcluster/orgs","repos_url":"https://api.github.com/users/taskcluster/repos","events_url":"https://api.github.com/users/taskcluster/events{/privacy}","received_events_url":"https://api.github.com/users/taskcluster/received_events","type":"Organization","site_admin":false},"repo":{"id":36757291,"name":"docker-exec-websocket-server","full_name":"taskcluster/docker-exec-websocket-server","owner":{"login":"taskcluster","id":6257436,"avatar_url":"https://avatars.githubusercontent.com/u/6257436?v=3","gravatar_id":"","url":"https://api.github.com/users/taskcluster","html_url":"https://github.com/taskcluster","followers_url":"https://api.github.com/users/taskcluster/followers","following_url":"https://api.github.com/users/taskcluster/following{/other_user}","gists_url":"https://api.github.com/users/taskcluster/gists{/gist_id}","starred_url":"https://api.github.com/users/taskcluster/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/taskcluster/subscriptions","organizations_url":"https://api.github.com/users/taskcluster/orgs","repos_url":"https://api.github.com/users/taskcluster/repos","events_url":"https://api.github.com/users/taskcluster/events{/privacy}","received_events_url":"https://api.github.com/users/taskcluster/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/taskcluster/docker-exec-websocket-server","description":"  A server that serves the results of docker exec over websockets. ","fork":false,"url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server","forks_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/forks","keys_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/keys{/key_id}","collaborators_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/teams","hooks_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/hooks","issue_events_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/issues/events{/number}","events_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/events","assignees_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/assignees{/user}","branches_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/branches{/branch}","tags_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/tags","blobs_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/git/refs{/sha}","trees_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/git/trees{/sha}","statuses_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/statuses/{sha}","languages_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/languages","stargazers_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/stargazers","contributors_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/contributors","subscribers_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/subscribers","subscription_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/subscription","commits_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/commits{/sha}","git_commits_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/git/commits{/sha}","comments_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/comments{/number}","issue_comment_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/issues/comments{/number}","contents_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/contents/{+path}","compare_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/compare/{base}...{head}","merges_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/merges","archive_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/downloads","issues_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/issues{/number}","pulls_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/pulls{/number}","milestones_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/milestones{/number}","notifications_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/labels{/name}","releases_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/releases{/id}","created_at":"2015-06-02T19:40:18Z","updated_at":"2015-06-04T00:01:18Z","pushed_at":"2015-08-20T20:09:26Z","git_url":"git://github.com/taskcluster/docker-exec-websocket-server.git","ssh_url":"git@github.com:taskcluster/docker-exec-websocket-server.git","clone_url":"https://github.com/taskcluster/docker-exec-websocket-server.git","svn_url":"https://github.com/taskcluster/docker-exec-websocket-server","homepage":"","size":376,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":2,"forks":0,"open_issues":2,"watchers":0,"default_branch":"master"}},"base":{"label":"taskcluster:master","ref":"master","sha":"58ac6ffc40df01e4ed6b52ca1ee7e25190cb5896","user":{"login":"taskcluster","id":6257436,"avatar_url":"https://avatars.githubusercontent.com/u/6257436?v=3","gravatar_id":"","url":"https://api.github.com/users/taskcluster","html_url":"https://github.com/taskcluster","followers_url":"https://api.github.com/users/taskcluster/followers","following_url":"https://api.github.com/users/taskcluster/following{/other_user}","gists_url":"https://api.github.com/users/taskcluster/gists{/gist_id}","starred_url":"https://api.github.com/users/taskcluster/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/taskcluster/subscriptions","organizations_url":"https://api.github.com/users/taskcluster/orgs","repos_url":"https://api.github.com/users/taskcluster/repos","events_url":"https://api.github.com/users/taskcluster/events{/privacy}","received_events_url":"https://api.github.com/users/taskcluster/received_events","type":"Organization","site_admin":false},"repo":{"id":36757291,"name":"docker-exec-websocket-server","full_name":"taskcluster/docker-exec-websocket-server","owner":{"login":"taskcluster","id":6257436,"avatar_url":"https://avatars.githubusercontent.com/u/6257436?v=3","gravatar_id":"","url":"https://api.github.com/users/taskcluster","html_url":"https://github.com/taskcluster","followers_url":"https://api.github.com/users/taskcluster/followers","following_url":"https://api.github.com/users/taskcluster/following{/other_user}","gists_url":"https://api.github.com/users/taskcluster/gists{/gist_id}","starred_url":"https://api.github.com/users/taskcluster/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/taskcluster/subscriptions","organizations_url":"https://api.github.com/users/taskcluster/orgs","repos_url":"https://api.github.com/users/taskcluster/repos","events_url":"https://api.github.com/users/taskcluster/events{/privacy}","received_events_url":"https://api.github.com/users/taskcluster/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/taskcluster/docker-exec-websocket-server","description":"  A server that serves the results of docker exec over websockets. ","fork":false,"url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server","forks_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/forks","keys_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/keys{/key_id}","collaborators_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/teams","hooks_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/hooks","issue_events_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/issues/events{/number}","events_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/events","assignees_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/assignees{/user}","branches_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/branches{/branch}","tags_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/tags","blobs_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/git/refs{/sha}","trees_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/git/trees{/sha}","statuses_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/statuses/{sha}","languages_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/languages","stargazers_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/stargazers","contributors_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/contributors","subscribers_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/subscribers","subscription_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/subscription","commits_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/commits{/sha}","git_commits_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/git/commits{/sha}","comments_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/comments{/number}","issue_comment_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/issues/comments{/number}","contents_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/contents/{+path}","compare_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/compare/{base}...{head}","merges_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/merges","archive_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/downloads","issues_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/issues{/number}","pulls_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/pulls{/number}","milestones_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/milestones{/number}","notifications_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/labels{/name}","releases_url":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/releases{/id}","created_at":"2015-06-02T19:40:18Z","updated_at":"2015-06-04T00:01:18Z","pushed_at":"2015-08-20T20:09:26Z","git_url":"git://github.com/taskcluster/docker-exec-websocket-server.git","ssh_url":"git@github.com:taskcluster/docker-exec-websocket-server.git","clone_url":"https://github.com/taskcluster/docker-exec-websocket-server.git","svn_url":"https://github.com/taskcluster/docker-exec-websocket-server","homepage":"","size":376,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":2,"forks":0,"open_issues":2,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/pulls/4"},"html":{"href":"https://github.com/taskcluster/docker-exec-websocket-server/pull/4"},"issue":{"href":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/issues/4"},"comments":{"href":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/issues/4/comments"},"review_comments":{"href":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/pulls/4/comments"},"review_comment":{"href":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/pulls/4/commits"},"statuses":{"href":"https://api.github.com/repos/taskcluster/docker-exec-websocket-server/statuses/25ba10ce9d38d8aaa6801f4ea9c9c063397d55bc"}}}}