mirror of
https://github.com/fluencelabs/dweb-transports
synced 2025-04-25 06:42:17 +00:00
Expand p_httpfetch to handle callbacks
This commit is contained in:
parent
ca303fdb06
commit
bd6ea94eb4
@ -119,7 +119,7 @@ class TransportHTTP extends Transport {
|
|||||||
*/
|
*/
|
||||||
//PY: res = self._sendGetPost(True, "rawstore", headers={"Content-Type": "application/octet-stream"}, urlargs=[], data=data)
|
//PY: res = self._sendGetPost(True, "rawstore", headers={"Content-Type": "application/octet-stream"}, urlargs=[], data=data)
|
||||||
console.assert(data, "TransportHttp.p_rawstore: requires data");
|
console.assert(data, "TransportHttp.p_rawstore: requires data");
|
||||||
const res = await httptools.p_POST(this._cmdurl(servercommands.rawstore), "application/octet-stream", data); // resolves to URL
|
const res = await httptools.p_POST(this._cmdurl(servercommands.rawstore), {data, contenttype: "application/octet-stream"}); // resolves to URL
|
||||||
let parsedurl = Url.parse(res);
|
let parsedurl = Url.parse(res);
|
||||||
let pathparts = parsedurl.pathname.split('/');
|
let pathparts = parsedurl.pathname.split('/');
|
||||||
return `contenthash:/contenthash/${pathparts.slice(-1)}`
|
return `contenthash:/contenthash/${pathparts.slice(-1)}`
|
||||||
@ -129,8 +129,8 @@ class TransportHTTP extends Transport {
|
|||||||
p_rawadd(url, sig) {
|
p_rawadd(url, sig) {
|
||||||
// Logged by Transports
|
// Logged by Transports
|
||||||
if (!url || !sig) throw new errors.CodingError("TransportHTTP.p_rawadd: invalid parms", url, sig);
|
if (!url || !sig) throw new errors.CodingError("TransportHTTP.p_rawadd: invalid parms", url, sig);
|
||||||
let value = stringify(sig.preflight(Object.assign({},sig)))+"\n";
|
const data = stringify(sig.preflight(Object.assign({},sig)))+"\n";
|
||||||
return httptools.p_POST(this._url(url, servercommands.rawadd), "application/json", value); // Returns immediately
|
return httptools.p_POST(this._url(url, servercommands.rawadd), {data, contenttype: "application/json"}); // Returns immediately
|
||||||
}
|
}
|
||||||
|
|
||||||
p_newlisturls(cl) {
|
p_newlisturls(cl) {
|
||||||
@ -257,11 +257,11 @@ class TransportHTTP extends Transport {
|
|||||||
// Logged by Transports
|
// Logged by Transports
|
||||||
//debughttp("p_set %o %o %o", url, keyvalues, value);
|
//debughttp("p_set %o %o %o", url, keyvalues, value);
|
||||||
if (typeof keyvalues === "string") {
|
if (typeof keyvalues === "string") {
|
||||||
let kv = stringify([{key: keyvalues, value: value}]);
|
let data = stringify([{key: keyvalues, value: value}]);
|
||||||
await httptools.p_POST(this._url(url, servercommands.set), "application/json", kv); // Returns immediately
|
await httptools.p_POST(this._url(url, servercommands.set), {data, contenttype: "application/json"}); // Returns immediately
|
||||||
} else {
|
} else {
|
||||||
let kv = stringify(Object.keys(keyvalues).map((k) => ({"key": k, "value": keyvalues[k]})));
|
let data = stringify(Object.keys(keyvalues).map((k) => ({"key": k, "value": keyvalues[k]})));
|
||||||
await httptools.p_POST(this._url(url, servercommands.set), "application/json", kv); // Returns immediately
|
await httptools.p_POST(this._url(url, servercommands.set), {data, contenttype: "application/json"}); // Returns immediately
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
22
httptools.js
22
httptools.js
@ -98,16 +98,16 @@ httptools.p_httpfetch = async function(httpurl, init, {wantstream=false}={}) { /
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
httptools.p_GET = function(httpurl, opts={}, cb) { //TODO-API rearranged and addded cb
|
||||||
httptools.p_GET = function(httpurl, opts={}) {
|
|
||||||
/* Locate and return a block, based on its url
|
/* Locate and return a block, based on its url
|
||||||
Throws TransportError if fails
|
Throws TransportError if fails
|
||||||
opts {
|
opts {
|
||||||
start, end, // Range of bytes wanted - inclusive i.e. 0,1023 is 1024 bytes
|
start, end, // Range of bytes wanted - inclusive i.e. 0,1023 is 1024 bytes
|
||||||
wantstream, // Return a stream rather than data
|
wantstream, // Return a stream rather than data
|
||||||
}
|
}
|
||||||
resolves to: URL that can be used to fetch the resource, of form contenthash:/contenthash/Q123
|
returns result via promise or cb(err, result)
|
||||||
*/
|
*/
|
||||||
|
if (typeof opts === "function") { cb = opts; opts = {}; }
|
||||||
let headers = new Headers();
|
let headers = new Headers();
|
||||||
if (opts.start || opts.end) headers.append("range", `bytes=${opts.start || 0}-${(opts.end<Infinity) ? opts.end : ""}`);
|
if (opts.start || opts.end) headers.append("range", `bytes=${opts.start || 0}-${(opts.end<Infinity) ? opts.end : ""}`);
|
||||||
let init = { //https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch
|
let init = { //https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch
|
||||||
@ -118,13 +118,18 @@ httptools.p_GET = function(httpurl, opts={}) {
|
|||||||
redirect: 'follow', // Chrome defaults to manual
|
redirect: 'follow', // Chrome defaults to manual
|
||||||
keepalive: true // Keep alive - mostly we'll be going back to same places a lot
|
keepalive: true // Keep alive - mostly we'll be going back to same places a lot
|
||||||
};
|
};
|
||||||
return httptools.p_httpfetch(httpurl, init, {wantstream: opts.wantstream}); // This s a real http url
|
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
|
||||||
}
|
}
|
||||||
httptools.p_POST = function(httpurl, type, data) {
|
httptools.p_POST = function(httpurl, opts={}, cb) { //TODO-API rearranged and addded cb
|
||||||
// Locate and return a block, based on its url
|
/* Locate and return a block, based on its url
|
||||||
|
opts = { data, contenttype }
|
||||||
|
returns result via promise or cb(err, result)
|
||||||
|
*/
|
||||||
// Throws TransportError if fails
|
// Throws TransportError if fails
|
||||||
//let headers = new window.Headers();
|
//let headers = new window.Headers();
|
||||||
//headers.set('content-type',type); Doesn't work, it ignores it
|
//headers.set('content-type',type); Doesn't work, it ignores it
|
||||||
|
if (typeof opts === "function") { cb = opts; opts = {}; }
|
||||||
let init = {
|
let init = {
|
||||||
//https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch
|
//https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch
|
||||||
//https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name for headers tat cant be set
|
//https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name for headers tat cant be set
|
||||||
@ -137,7 +142,10 @@ httptools.p_POST = function(httpurl, type, data) {
|
|||||||
redirect: 'follow', // Chrome defaults to manual
|
redirect: 'follow', // Chrome defaults to manual
|
||||||
keepalive: true // Keep alive - mostly we'll be going back to same places a lot
|
keepalive: true // Keep alive - mostly we'll be going back to same places a lot
|
||||||
};
|
};
|
||||||
return httptools.p_httpfetch(httpurl, init);
|
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;
|
exports = module.exports = httptools;
|
Loading…
x
Reference in New Issue
Block a user