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

@ -134,11 +134,11 @@ class TransportHTTP extends Transport {
} }
p_newlisturls(cl) { p_newlisturls(cl) {
let u = cl._publicurls.map(urlstr => Url.parse(urlstr)) let u = cl._publicurls.map(urlstr => Url.parse(urlstr))
.find(parsedurl => .find(parsedurl =>
((parsedurl.protocol === "https:" && ["gateway.dweb.me", "dweb.me"].includes(parsedurl.host) ((parsedurl.protocol === "https:" && ["gateway.dweb.me", "dweb.me"].includes(parsedurl.host)
&& (parsedurl.pathname.includes('/content/rawfetch') || parsedurl.pathname.includes('/contenthash/'))) && (parsedurl.pathname.includes('/content/rawfetch') || parsedurl.pathname.includes('/contenthash/')))
|| (parsedurl.protocol === "contenthash:") && (parsedurl.pathname.split('/')[1] === "contenthash"))); || (parsedurl.protocol === "contenthash:") && (parsedurl.pathname.split('/')[1] === "contenthash")));
if (!u) { if (!u) {
// noinspection JSUnresolvedVariable // noinspection JSUnresolvedVariable
u = `contenthash:/contenthash/${ cl.keypair.verifyexportmultihashsha256_58() }`; // Pretty random, but means same test will generate same list and server is expecting base58 of a hash u = `contenthash:/contenthash/${ cl.keypair.verifyexportmultihashsha256_58() }`; // Pretty random, but means same test will generate same list and server is expecting base58 of a hash
@ -190,25 +190,23 @@ class TransportHTTP extends Transport {
:param file: Webtorrent "file" as returned by webtorrentfindfile :param file: Webtorrent "file" as returned by webtorrentfindfile
:param opts: { start: byte to start from; end: optional end byte } :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); debughttp("createreadstream %s %o", Url.parse(url).href, opts);
let through; let through;
try { through = new stream.PassThrough();
through = new stream.PassThrough(); httptools.p_GET(this._url(url, servercommands.rawfetch), Object.assign({wantstream: true}, opts))
const p_filestream = httptools.p_GET(this._url(url, servercommands.rawfetch), Object.assign({wantstream: true}, opts)); .then(s => s.pipe(through))
p_filestream.then(s => s.pipe(through)); .catch(err => {
return through; // Returns through synchronously, before the pipe is setup console.warn(this.name, "createReadStream caught error", err.message);
} catch(err) { if (typeof through.destroy === 'function') {
console.warn(this.name,"createReadStream caught error", err.message); through.destroy(err); // Will emit error & close and free up resources
if (typeof through.destroy === 'function') } else {
through.destroy(err); through.emit('error', err);
else through.emit('error', err) }
} });
return through; // Returns "through" synchronously, before the pipe is setup
} }
async p_createReadStream(url, opts) { async p_createReadStream(url, opts) {