mirror of
https://github.com/fluencelabs/dweb-transports
synced 2025-04-25 06:42:17 +00:00
v0.1.36 make httptools accessible at Transports.httptools
This commit is contained in:
parent
cd0f51c6d0
commit
f59b32f242
@ -127,3 +127,5 @@ See [Dweb document index](./DOCUMENTINDEX.md) for a list of the repos that make
|
||||
### Release Notes
|
||||
|
||||
* 0.1.33: Bug fixes; support for gatewayUrls (for dweb-mirror)
|
||||
* 0.1.35: package update (note wont work with latest versions of yjs or uglify)
|
||||
* 0.1.36: Made httptools accessable at Transports.httptools so it doesnt have to be separately 'require'd
|
||||
|
@ -3,6 +3,7 @@ const errors = require('./Errors');
|
||||
const utils = require('./utils');
|
||||
//process.env.DEBUG = "dweb-transports"; //TODO-DEBUG set at top level
|
||||
const debugtransports = require('debug')('dweb-transports');
|
||||
const httptools = require('./httptools');
|
||||
|
||||
class Transports {
|
||||
/*
|
||||
@ -778,5 +779,5 @@ class Transports {
|
||||
Transports._transports = []; // Array of transport instances connected
|
||||
Transports.namingcb = undefined; // Will be defined by the naming component (turns URLs for names into URLs for transport)
|
||||
Transports._transportclasses = {}; // Pointers to classes whose code is loaded.
|
||||
|
||||
Transports.httptools = httptools; // Static http tools
|
||||
exports = module.exports = Transports;
|
||||
|
20904
dist/dweb-transports-bundle.js
vendored
20904
dist/dweb-transports-bundle.js
vendored
File diff suppressed because one or more lines are too long
21
httptools.js
21
httptools.js
@ -1,6 +1,6 @@
|
||||
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 debught = require('debug')('dweb-transports:httptools');
|
||||
const debug = require('debug')('dweb-transports:httptools');
|
||||
|
||||
//var fetch,Headers,Request;
|
||||
//if (typeof(Window) === "undefined") {
|
||||
@ -39,14 +39,14 @@ async function loopfetch(req, ms, count, what) {
|
||||
return await fetch(req);
|
||||
} catch(err) {
|
||||
lasterr = err;
|
||||
debught("Delaying %s by %d ms because %s", what, ms, err.message);
|
||||
debug("Delaying %s by %d ms because %s", what, ms, err.message);
|
||||
await new Promise(resolve => {setTimeout(() => { resolve(); },ms)})
|
||||
ms = ms*(1+Math.random()); // Spread out delays incase all requesting same time
|
||||
}
|
||||
}
|
||||
console.warn("loopfetch of",what,"failed");
|
||||
if (loopguard !== ((typeof window != "undefined") && window.loopguard)) {
|
||||
debught("Looping exited because of page change %s", what);
|
||||
debug("Looping exited because of page change %s", what);
|
||||
throw new Error("Looping exited because of page change "+ what)
|
||||
} else {
|
||||
throw(lasterr);
|
||||
@ -65,12 +65,12 @@ httptools.p_httpfetch = async function(httpurl, init, {wantstream=false}={}) { /
|
||||
*/
|
||||
try {
|
||||
// THis was get("range") but that works when init.headers is a Headers, but not when its an object
|
||||
debught("p_httpfetch: %s %o", httpurl, init.headers.range || "" );
|
||||
debug("p_httpfetch: %s %o", httpurl, init.headers.range || "" );
|
||||
//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);
|
||||
let response = await loopfetch(req, 500, 12, "fetching "+httpurl);
|
||||
let response = await loopfetch(req, 500, (init.method === "GET") ? ( init.count || 12) : 1, "fetching "+httpurl);
|
||||
// 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) {
|
||||
@ -89,7 +89,7 @@ httptools.p_httpfetch = async function(httpurl, init, {wantstream=false}={}) { /
|
||||
throw new errors.TransportError(`Transport Error ${response.status}: ${response.statusText}`);
|
||||
} catch (err) {
|
||||
// Error here is particularly unhelpful - if rejected during the COrs process it throws a TypeError
|
||||
debught("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) {
|
||||
throw err;
|
||||
} else {
|
||||
@ -119,7 +119,8 @@ httptools.p_GET = function(httpurl, opts={}, cb) { //TODO-API rearranged and add
|
||||
keepalive: true // Keep alive - mostly we'll be going back to same places a lot
|
||||
};
|
||||
const prom = httptools.p_httpfetch(httpurl, init, {wantstream: opts.wantstream}); // This s a real http url
|
||||
if (cb) { prom.then((res)=>cb(null,res)).catch((err) => cb(err)); } else { return prom; } // Unpromisify pattern v3
|
||||
//if (cb) { prom.then((res)=>cb(null,res)).catch((err) => cb(err)); } else { return prom; } // Unpromisify pattern v3
|
||||
if (cb) { prom.catch((err) => cb(err)).then((res)=>cb(null,res)).catch((err) => debug("Uncaught error %O",err)); } else { return prom; } // Unpromisify pattern v4
|
||||
}
|
||||
httptools.p_POST = function(httpurl, opts={}, cb) { //TODO-API rearranged and addded cb
|
||||
/* Locate and return a block, based on its url
|
||||
@ -136,16 +137,16 @@ httptools.p_POST = function(httpurl, opts={}, cb) { //TODO-API rearranged and ad
|
||||
method: 'POST',
|
||||
headers: {}, //headers,
|
||||
//body: new Buffer(data),
|
||||
body: data,
|
||||
body: opts.data,
|
||||
mode: 'cors',
|
||||
cache: 'default',
|
||||
redirect: 'follow', // Chrome defaults to manual
|
||||
keepalive: true // Keep alive - mostly we'll be going back to same places a lot
|
||||
keepalive: false // Keep alive - mostly we'll be going back to same places a lot
|
||||
};
|
||||
if (opts.contenttype) init.headers["Content-Type"] = opts.contenttype;
|
||||
const prom = httptools.p_httpfetch(httpurl, init);
|
||||
if (cb) { prom.then((res)=>cb(null,res)).catch((err) => cb(err)); } else { return prom; } // Unpromisify pattern v3
|
||||
}
|
||||
|
||||
|
||||
|
||||
exports = module.exports = httptools;
|
@ -51,5 +51,5 @@
|
||||
"test": "cd src; node ./test.js",
|
||||
"help": "echo 'test (test it)'; echo 'build (creates dweb-transports-bundle)'"
|
||||
},
|
||||
"version": "0.1.35"
|
||||
"version": "0.1.36"
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user