mirror of
https://github.com/fluencelabs/dweb-transports
synced 2025-03-15 10:30:48 +00:00
Tweeks to work with ServiceWorker (TransportsProxy in main repo)
This commit is contained in:
parent
eda896fe67
commit
93228d71fd
10
API.md
10
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)
|
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) {
|
#####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
|
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
|
url: Array of urls, or string representing url or representing array of urls
|
||||||
|
@ -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 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');
|
const Url = require('url');
|
||||||
|
|
||||||
var fetch,Headers,Request;
|
//var fetch,Headers,Request;
|
||||||
if (typeof(Window) === "undefined") {
|
//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
|
//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
|
//XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; // Note this doesnt work if set to a var or const, needed by whatwg-fetch
|
||||||
console.log("Node loaded");
|
console.log("Node loaded");
|
||||||
fetch = nodefetch;
|
fetch = nodefetch;
|
||||||
Headers = fetch.Headers; // A class
|
Headers = fetch.Headers; // A class
|
||||||
Request = fetch.Request; // A class
|
Request = fetch.Request; // A class
|
||||||
} else {
|
} /* else {
|
||||||
// If on a browser, need to find fetch,Headers,Request in window
|
// If on a browser, need to find fetch,Headers,Request in window
|
||||||
console.log("Loading browser version of fetch,Headers,Request");
|
console.log("Loading browser version of fetch,Headers,Request");
|
||||||
fetch = window.fetch;
|
fetch = window.fetch;
|
||||||
Headers = window.Headers;
|
Headers = window.Headers;
|
||||||
Request = window.Request;
|
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
|
//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 = {
|
defaulthttpoptions = {
|
||||||
|
@ -18,14 +18,14 @@ class Transports {
|
|||||||
*/
|
*/
|
||||||
return this._transports.filter((t) => (!t.status));
|
return this._transports.filter((t) => (!t.status));
|
||||||
}
|
}
|
||||||
static connectedNames() {
|
static async p_connectedNames() {
|
||||||
/*
|
/*
|
||||||
Return an array of the names of connected transports
|
Return an array of the names of connected transports
|
||||||
*/
|
*/
|
||||||
return this._connected().map(t => t.name);
|
return this._connected().map(t => t.name);
|
||||||
}
|
}
|
||||||
static connectedNamesParm() {
|
static async p_connectedNamesParm() { // Doesnt strictly need to be async, but for consistency with Proxy it has to be.
|
||||||
return this.connectedNames().map(n => "transport="+n).join('&')
|
return (await this.p_connectedNames()).map(n => "transport="+n).join('&')
|
||||||
}
|
}
|
||||||
static validFor(urls, func, options) {
|
static validFor(urls, func, options) {
|
||||||
/*
|
/*
|
||||||
@ -488,18 +488,19 @@ class Transports {
|
|||||||
/*
|
/*
|
||||||
This is a standardish starting process, feel free to subclass or replace !
|
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.
|
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 ---");
|
if (verbose) console.group("p_connect ---");
|
||||||
try {
|
try {
|
||||||
options = options || {};
|
options = options || {};
|
||||||
let setupoptions = {};
|
let setupoptions = {};
|
||||||
let tabbrevs = options.transports; // Array of transport abbreviations
|
let tabbrevs = options.transports; // Array of transport abbreviations
|
||||||
if (!tabbrevs.length) { tabbrevs = options.defaulttransports || [] }
|
if (!(tabbrevs && tabbrevs.length)) { tabbrevs = options.defaulttransports || [] }
|
||||||
if (!tabbrevs.length) { tabbrevs = ["HTTP", "YJS", "IPFS", "WEBTORRENT"]; }
|
if (! tabbrevs.length) { tabbrevs = ["HTTP", "YJS", "IPFS", "WEBTORRENT"]; }
|
||||||
tabbrevs = tabbrevs.map(n => n.toUpperCase());
|
tabbrevs = tabbrevs.map(n => n.toUpperCase());
|
||||||
let transports = this.setup0(tabbrevs, options, verbose);
|
let transports = this.setup0(tabbrevs, options, verbose);
|
||||||
if (!!options.statuselement) {
|
if (!!options.statuselement) {
|
||||||
|
//TODO-SW need to return status through messages
|
||||||
while (statuselement.lastChild) {statuselement.removeChild(statuselement.lastChild); } // Remove any exist status
|
while (statuselement.lastChild) {statuselement.removeChild(statuselement.lastChild); } // Remove any exist status
|
||||||
statuselement.appendChild(
|
statuselement.appendChild(
|
||||||
utils.createElement("UL", {}, transports.map(t => {
|
utils.createElement("UL", {}, transports.map(t => {
|
||||||
@ -520,7 +521,7 @@ class Transports {
|
|||||||
if (verbose) console.groupEnd("p_connect ---");
|
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
|
/* 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
|
url: Array of urls, or string representing url or representing array of urls
|
||||||
return: Array of strings representing url
|
return: Array of strings representing url
|
||||||
@ -537,6 +538,14 @@ class Transports {
|
|||||||
return url;
|
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 <VIDEO> tag.
|
||||||
|
*/
|
||||||
|
return Transports.http()._url(urls.find(u => (u.startsWith("contenthash") || u.startsWith("http") )), "content/rawfetch");
|
||||||
|
}
|
||||||
|
|
||||||
static async test(verbose) {
|
static async test(verbose) {
|
||||||
if (verbose) {console.log("Transports.test")}
|
if (verbose) {console.log("Transports.test")}
|
||||||
try {
|
try {
|
||||||
|
@ -28,9 +28,9 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function fetchit() {
|
async function fetchit() {
|
||||||
let el = document.getElementById("retrievalarea");
|
let el = document.getElementById("retrievalarea");
|
||||||
let urls = DwebTransports.urlsFrom(el.value);
|
let urls = await DwebTransports.p_urlsFrom(el.value);
|
||||||
fetchanddisplay(urls); //asynchronous
|
fetchanddisplay(urls); //asynchronous
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,9 @@
|
|||||||
"email": "mitra@archive.org",
|
"email": "mitra@archive.org",
|
||||||
"url": "http://www.mitra.biz"
|
"url": "http://www.mitra.biz"
|
||||||
},
|
},
|
||||||
|
"browser": {
|
||||||
|
"node-fetch": false
|
||||||
|
},
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/mitra42/dweb-transports/issues"
|
"url": "https://github.com/mitra42/dweb-transports/issues"
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user