{"action":"created","issue":{"url":"https://api.github.com/repos/ArtskydJ/sox-stream/issues/4","repository_url":"https://api.github.com/repos/ArtskydJ/sox-stream","labels_url":"https://api.github.com/repos/ArtskydJ/sox-stream/issues/4/labels{/name}","comments_url":"https://api.github.com/repos/ArtskydJ/sox-stream/issues/4/comments","events_url":"https://api.github.com/repos/ArtskydJ/sox-stream/issues/4/events","html_url":"https://github.com/ArtskydJ/sox-stream/issues/4","id":153267472,"number":4,"title":"Failure causes infinite loop","user":{"login":"MGlauser","id":1903435,"avatar_url":"https://avatars.githubusercontent.com/u/1903435?v=3","gravatar_id":"","url":"https://api.github.com/users/MGlauser","html_url":"https://github.com/MGlauser","followers_url":"https://api.github.com/users/MGlauser/followers","following_url":"https://api.github.com/users/MGlauser/following{/other_user}","gists_url":"https://api.github.com/users/MGlauser/gists{/gist_id}","starred_url":"https://api.github.com/users/MGlauser/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MGlauser/subscriptions","organizations_url":"https://api.github.com/users/MGlauser/orgs","repos_url":"https://api.github.com/users/MGlauser/repos","events_url":"https://api.github.com/users/MGlauser/events{/privacy}","received_events_url":"https://api.github.com/users/MGlauser/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/ArtskydJ/sox-stream/labels/bug","name":"bug","color":"fc2929"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2016-05-05T16:19:04Z","updated_at":"2016-05-05T18:05:15Z","closed_at":null,"body":"This line is causing an infinite loop.\r\nThe problem comes when a failure condition is created for example when being sent an mp3 file and telling sox it’s a wav file.  (Aside from that being a bad thing, the module must handle it gracefully.)\r\n\r\nSox throws an error as it should.  \r\nThe tmpFile does not exist so cleanup throws an error and you’re in an infinite loop.\r\n\r\n\r\n\tfunction emitErr(err) {\r\n\t\ttmpFile.cleanup( duplex.emit.bind(duplex, 'error', err) )\r\n\t}\r\nThe suggested way to deal with it is in this style, but forgive me for not knowing how to take the above line and implement it like the below line.\r\n\r\nfunction emitErr(err) {\r\n    if (err.code !== 'ENOENT') { // Avoid infinite loops\r\n        tmpFile.cleanup(duplex.emit.bind(duplex, 'error', err));\r\n    }\r\n}\r\n"},"comment":{"url":"https://api.github.com/repos/ArtskydJ/sox-stream/issues/comments/217228540","html_url":"https://github.com/ArtskydJ/sox-stream/issues/4#issuecomment-217228540","issue_url":"https://api.github.com/repos/ArtskydJ/sox-stream/issues/4","id":217228540,"user":{"login":"MGlauser","id":1903435,"avatar_url":"https://avatars.githubusercontent.com/u/1903435?v=3","gravatar_id":"","url":"https://api.github.com/users/MGlauser","html_url":"https://github.com/MGlauser","followers_url":"https://api.github.com/users/MGlauser/followers","following_url":"https://api.github.com/users/MGlauser/following{/other_user}","gists_url":"https://api.github.com/users/MGlauser/gists{/gist_id}","starred_url":"https://api.github.com/users/MGlauser/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MGlauser/subscriptions","organizations_url":"https://api.github.com/users/MGlauser/orgs","repos_url":"https://api.github.com/users/MGlauser/repos","events_url":"https://api.github.com/users/MGlauser/events{/privacy}","received_events_url":"https://api.github.com/users/MGlauser/received_events","type":"User","site_admin":false},"created_at":"2016-05-05T18:05:14Z","updated_at":"2016-05-05T18:05:14Z","body":"Thank you for this!\n\n\n> On May 5, 2016, at 11:02 AM, Joseph Dykstra <notifications@github.com> wrote:\n> \n> If you pipe the output of sox-stream to your response, then if an error occurs during the transcoding process, then you will have already responded with part of the file.\n> \n> As far as I know, the headers are sent at the beginning of the response. So you probably can't start sending the file until it has successfully transcoded the file. Thus, you would have to buffer the file.\n> \n> The two ways to buffer a file that I can think of are: memory, disk\n> \n> Memory version, concat-stream or 'simple-concat'\n> \n> var concat = require('simple-concat')\n> \n> // ...\n> stream.pipe(convert).pipe(concat(function (err, buf) {\n>     if (err) {\n>         res.writeHead(500)\n>         res.end()\n>     } else {\n>         res.writeHead(200)\n>         res.end(buf)\n>     }\n> }))\n> Depending on your architecture, you might want to cache to disk anyway:\n> \n> stream.pipe(fs.createWriteStream('herp_derp'))\n>     .on('end', function () { // Or is it 'close'?\n>         res.writeHead(200)\n>         fs.createReadStream('herp_derp').pipe(res)\n>     }).on('error', function (err) {\n>         res.writeHead(500)\n>         res.end()\n>     })\n> If you use disk caching, maybe sox.js <https://github.com/ArtskydJ/sox.js> will work for you?\n> \n> —\n> You are receiving this because you authored the thread.\n> Reply to this email directly or view it on GitHub <https://github.com/ArtskydJ/sox-stream/issues/4#issuecomment-217227901>\n\n"}}