refactor createReadStream for error andlig

This commit is contained in:
Mitra Ardron 2018-10-11 15:47:46 +11:00
parent 00ffa39f9e
commit bd6b2bb2d1

View File

@ -190,25 +190,23 @@ class TransportHTTP extends Transport {
:param file: Webtorrent "file" as returned by webtorrentfindfile
:param opts: { start: byte to start from; end: optional end byte }
:resolves to stream: The readable stream.
:returns stream: The readable stream - it is returned immediately, though won't be sending data until the http completes
*/
debughttp("createreadstream %s %o", Url.parse(url).href, opts);
let through;
try {
through = new stream.PassThrough();
const p_filestream = httptools.p_GET(this._url(url, servercommands.rawfetch), Object.assign({wantstream: true}, opts));
p_filestream.then(s => s.pipe(through));
return through; // Returns through synchronously, before the pipe is setup
} catch(err) {
httptools.p_GET(this._url(url, servercommands.rawfetch), Object.assign({wantstream: true}, opts))
.then(s => s.pipe(through))
.catch(err => {
console.warn(this.name, "createReadStream caught error", err.message);
if (typeof through.destroy === 'function')
through.destroy(err);
else through.emit('error', err)
if (typeof through.destroy === 'function') {
through.destroy(err); // Will emit error & close and free up resources
} else {
through.emit('error', err);
}
});
return through; // Returns "through" synchronously, before the pipe is setup
}
async p_createReadStream(url, opts) {