diff --git a/TransportHTTP.js b/TransportHTTP.js index d21f6b7..0694232 100644 --- a/TransportHTTP.js +++ b/TransportHTTP.js @@ -119,7 +119,7 @@ class TransportHTTP extends Transport { */ //PY: res = self._sendGetPost(True, "rawstore", headers={"Content-Type": "application/octet-stream"}, urlargs=[], data=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 pathparts = parsedurl.pathname.split('/'); return `contenthash:/contenthash/${pathparts.slice(-1)}` @@ -129,8 +129,8 @@ class TransportHTTP extends Transport { p_rawadd(url, sig) { // Logged by Transports if (!url || !sig) throw new errors.CodingError("TransportHTTP.p_rawadd: invalid parms", url, sig); - let value = stringify(sig.preflight(Object.assign({},sig)))+"\n"; - return httptools.p_POST(this._url(url, servercommands.rawadd), "application/json", value); // Returns immediately + const data = stringify(sig.preflight(Object.assign({},sig)))+"\n"; + return httptools.p_POST(this._url(url, servercommands.rawadd), {data, contenttype: "application/json"}); // Returns immediately } p_newlisturls(cl) { @@ -257,11 +257,11 @@ class TransportHTTP extends Transport { // Logged by Transports //debughttp("p_set %o %o %o", url, keyvalues, value); if (typeof keyvalues === "string") { - let kv = stringify([{key: keyvalues, value: value}]); - await httptools.p_POST(this._url(url, servercommands.set), "application/json", kv); // Returns immediately + let data = stringify([{key: keyvalues, value: value}]); + await httptools.p_POST(this._url(url, servercommands.set), {data, contenttype: "application/json"}); // Returns immediately } else { - let kv = 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 + let data = stringify(Object.keys(keyvalues).map((k) => ({"key": k, "value": keyvalues[k]}))); + await httptools.p_POST(this._url(url, servercommands.set), {data, contenttype: "application/json"}); // Returns immediately } } diff --git a/httptools.js b/httptools.js index 588b348..9d37332 100644 --- a/httptools.js +++ b/httptools.js @@ -98,16 +98,16 @@ httptools.p_httpfetch = async function(httpurl, init, {wantstream=false}={}) { / } } - -httptools.p_GET = function(httpurl, opts={}) { +httptools.p_GET = function(httpurl, opts={}, cb) { //TODO-API rearranged and addded cb /* Locate and return a block, based on its url Throws TransportError if fails opts { start, end, // Range of bytes wanted - inclusive i.e. 0,1023 is 1024 bytes 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(); if (opts.start || opts.end) headers.append("range", `bytes=${opts.start || 0}-${(opts.endcb(null,res)).catch((err) => cb(err)); } else { return prom; } // Unpromisify pattern v3 } -httptools.p_POST = function(httpurl, type, data) { - // Locate and return a block, based on its url +httptools.p_POST = function(httpurl, opts={}, cb) { //TODO-API rearranged and addded cb + /* Locate and return a block, based on its url + opts = { data, contenttype } + returns result via promise or cb(err, result) + */ // Throws TransportError if fails //let headers = new window.Headers(); //headers.set('content-type',type); Doesn't work, it ignores it + if (typeof opts === "function") { cb = opts; opts = {}; } let init = { //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 @@ -137,7 +142,10 @@ httptools.p_POST = function(httpurl, type, data) { redirect: 'follow', // Chrome defaults to manual 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; \ No newline at end of file