diff --git a/API.md b/API.md index 03022c3..66cc6a2 100644 --- a/API.md +++ b/API.md @@ -257,13 +257,13 @@ _transportclasses All classes whose code is loaded e.g. {HTTP: TransportHTTP, returns Array of transports that are connected (i.e. status=STATUS_CONNECTED) ``` -#####static _connectedNames() +#####static async p_connectedNames() ``` -returns Array of names transports that are connected (i.e. status=STATUS_CONNECTED) +resolves to: Array of names transports that are connected (i.e. status=STATUS_CONNECTED) ``` -#####static connectedNamesParm +#####static async p_connectedNamesParm ``` -returns part of URL string for transports e.g. 'transport=HTTP&transport=IPFS" +resolves to: part of URL string for transports e.g. 'transport=HTTP&transport=IPFS" ``` #####static validFor(urls, func, options) { @@ -334,7 +334,7 @@ options { } ``` -#####static urlsFrom(url) +#####static async p_urlsFrom(url) Utility to convert to urls form wanted for Transports functions, e.g. from user input ``` url: Array of urls, or string representing url or representing array of urls diff --git a/TransportHTTP.js b/TransportHTTP.js index f049fd2..d1bd3cf 100644 --- a/TransportHTTP.js +++ b/TransportHTTP.js @@ -4,21 +4,22 @@ const Transports = require('./Transports'); // Manage all Transports that are lo 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 Url = require('url'); -var fetch,Headers,Request; -if (typeof(Window) === "undefined") { +//var fetch,Headers,Request; +//if (typeof(Window) === "undefined") { +if (typeof(fetch) === "undefined") { //var fetch = require('whatwg-fetch').fetch; //Not as good as node-fetch-npm, but might be the polyfill needed for browser.safari //XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; // Note this doesnt work if set to a var or const, needed by whatwg-fetch console.log("Node loaded"); fetch = nodefetch; Headers = fetch.Headers; // A class Request = fetch.Request; // A class -} else { +} /* else { // If on a browser, need to find fetch,Headers,Request in window console.log("Loading browser version of fetch,Headers,Request"); fetch = window.fetch; Headers = window.Headers; Request = window.Request; -} +} */ //TODO-HTTP to work on Safari or mobile will require a polyfill, see https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch for comment defaulthttpoptions = { diff --git a/Transports.js b/Transports.js index 2b26f98..ad24a7d 100644 --- a/Transports.js +++ b/Transports.js @@ -18,14 +18,14 @@ class Transports { */ return this._transports.filter((t) => (!t.status)); } - static connectedNames() { + static async p_connectedNames() { /* Return an array of the names of connected transports */ return this._connected().map(t => t.name); } - static connectedNamesParm() { - return this.connectedNames().map(n => "transport="+n).join('&') + static async p_connectedNamesParm() { // Doesnt strictly need to be async, but for consistency with Proxy it has to be. + return (await this.p_connectedNames()).map(n => "transport="+n).join('&') } static validFor(urls, func, options) { /* @@ -488,18 +488,19 @@ class Transports { /* This is a standardish starting process, feel free to subclass or replace ! It will connect to a set of standard transports and is intended to work inside a browser. - options = { defaulttransports: ["IPFS"]; statuselement: el } + options = { defaulttransports: ["IPFS"], statuselement: el, http: {}, ipfs: {} } */ if (verbose) console.group("p_connect ---"); try { options = options || {}; let setupoptions = {}; let tabbrevs = options.transports; // Array of transport abbreviations - if (!tabbrevs.length) { tabbrevs = options.defaulttransports || [] } - if (!tabbrevs.length) { tabbrevs = ["HTTP", "YJS", "IPFS", "WEBTORRENT"]; } + if (!(tabbrevs && tabbrevs.length)) { tabbrevs = options.defaulttransports || [] } + if (! tabbrevs.length) { tabbrevs = ["HTTP", "YJS", "IPFS", "WEBTORRENT"]; } tabbrevs = tabbrevs.map(n => n.toUpperCase()); let transports = this.setup0(tabbrevs, options, verbose); if (!!options.statuselement) { + //TODO-SW need to return status through messages while (statuselement.lastChild) {statuselement.removeChild(statuselement.lastChild); } // Remove any exist status statuselement.appendChild( utils.createElement("UL", {}, transports.map(t => { @@ -520,7 +521,7 @@ class Transports { if (verbose) console.groupEnd("p_connect ---"); } - static urlsFrom(url) { //TODO backport to main repo - copy from htmlutils to utils + static async p_urlsFrom(url) { //TODO backport to main repo - copy from htmlutils to utils /* Utility to convert to urls form wanted for Transports functions, e.g. from user input url: Array of urls, or string representing url or representing array of urls return: Array of strings representing url @@ -537,6 +538,14 @@ class Transports { return url; } + static async p_httpfetchurls(urls) { + /* + Utility to take a array of Transport urls, convert back to a single url that can be used for a fetch, typically + this is done when cant handle a stream, so want to give the url to the