mirror of
https://github.com/fluencelabs/dweb-transports
synced 2025-03-15 18:30:49 +00:00
httptools - make it easier to change for DAC import version
This commit is contained in:
parent
768893f508
commit
eec42a6430
20
httptools.js
20
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 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 errors = require('./Errors'); // Standard Dweb Errors
|
||||||
|
const TransportError = errors.TransportError;
|
||||||
const debug = require('debug')('dweb-transports:httptools');
|
const debug = require('debug')('dweb-transports:httptools');
|
||||||
const queue = require('async/queue');
|
const queue = require('async/queue');
|
||||||
|
|
||||||
|
|
||||||
//var fetch,Headers,Request;
|
//var fetch,Headers,Request;
|
||||||
//if (typeof(Window) === "undefined") {
|
//if (typeof(Window) === "undefined") {
|
||||||
if (typeof(fetch) === "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'))
|
//console.log('CTX=',init["headers"].get('Content-Type'))
|
||||||
// Using window.fetch, because it doesn't appear to be in scope otherwise in the browser.
|
// Using window.fetch, because it doesn't appear to be in scope otherwise in the browser.
|
||||||
let req = new Request(httpurl, init);
|
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);
|
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)
|
// 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.
|
// Note response.body gets a stream and response.blob gets a blob and response.arrayBuffer gets a buffer.
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
@ -138,14 +150,14 @@ httptools.p_httpfetch = async function(httpurl, init, {wantstream=false, retries
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// noinspection ExceptionCaughtLocallyJS
|
// 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) {
|
} catch (err) {
|
||||||
// Error here is particularly unhelpful - if rejected during the COrs process it throws a TypeError
|
// 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
|
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;
|
throw err;
|
||||||
} else {
|
} else {
|
||||||
throw new errors.TransportError(`Transport error thrown by ${httpurl}: ${err.message}`);
|
throw new TransportError(`Transport error thrown by ${httpurl}: ${err.message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user