From eec42a64301cfda25268b58902d263fdc3127949 Mon Sep 17 00:00:00 2001 From: Mitra Ardron Date: Tue, 13 Aug 2019 13:13:54 +1000 Subject: [PATCH] httptools - make it easier to change for DAC import version --- httptools.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/httptools.js b/httptools.js index 16ad4ce..e72b54f 100644 --- a/httptools.js +++ b/httptools.js @@ -1,8 +1,14 @@ +/** + * There is an original fo this file at dweb-transports.httptools + * and a duplicate at dweb-archivecontroller which is only there while DwebTransports is being made more usable + */ const nodefetch = require('node-fetch'); // Note, were using node-fetch-npm which had a warning in webpack see https://github.com/bitinn/node-fetch/issues/421 and is intended for clients const errors = require('./Errors'); // Standard Dweb Errors +const TransportError = errors.TransportError; const debug = require('debug')('dweb-transports:httptools'); const queue = require('async/queue'); + //var fetch,Headers,Request; //if (typeof(Window) === "undefined") { if (typeof(fetch) === "undefined") { @@ -121,8 +127,14 @@ httptools.p_httpfetch = async function(httpurl, init, {wantstream=false, retries //console.log('CTX=',init["headers"].get('Content-Type')) // Using window.fetch, because it doesn't appear to be in scope otherwise in the browser. let req = new Request(httpurl, init); - //let response = await fetch(req); + + // EITHER Use queuedFetch if have async/queue let response = await queuedFetch(req, 500, retries, httpurl); + // OR Use loopfetch if dont have async/queue and hitting browser Insufficient resources + //let response = await loopfetch(req, 500, retries, httpurl); + // OR use fetch for simplicity + //let response = await fetch(req); + // fetch throws (on Chrome, untested on Firefox or Node) TypeError: Failed to fetch) // Note response.body gets a stream and response.blob gets a blob and response.arrayBuffer gets a buffer. if (response.ok) { @@ -138,14 +150,14 @@ httptools.p_httpfetch = async function(httpurl, init, {wantstream=false, retries } } // noinspection ExceptionCaughtLocallyJS - throw new errors.TransportError(`Transport Error ${httpurl} ${response.status}: ${response.statusText}`); + throw new TransportError(`Transport Error ${httpurl} ${response.status}: ${response.statusText}`); } catch (err) { // Error here is particularly unhelpful - if rejected during the COrs process it throws a TypeError debug("p_httpfetch failed: %s", err.message); // note TypeErrors are generated by CORS or the Chrome anti DDOS 'feature' should catch them here and comment - if (err instanceof errors.TransportError) { + if (err instanceof TransportError) { throw err; } else { - throw new errors.TransportError(`Transport error thrown by ${httpurl}: ${err.message}`); + throw new TransportError(`Transport error thrown by ${httpurl}: ${err.message}`); } } }