Push script loading into Transports

This commit is contained in:
Mitra Ardron 2019-09-13 15:03:34 +10:00
parent 8fc8e8761f
commit 0229bd92aa
7 changed files with 37 additions and 12 deletions

View File

@ -392,4 +392,7 @@ class TransportGUN extends Transport {
}
}
Transports._transportclasses["GUN"] = TransportGUN;
TransportGUN.requires = ['gun/gun.js', 'gun/lib/path.js', 'gun/nts', 'gun/lib/wire', 'gun/lib/multicast', 'gun/lib/radix.js',
'gun/lib/radisk.js', 'gun/lib/store.js', 'gun/lib/rindexed.js']
.map(s => 'https://cdn.jsdelivr.net/npm/' + s);
exports = module.exports = TransportGUN;

View File

@ -55,8 +55,6 @@ class TransportHTTP extends Transport {
this.status = Transport.STATUS_LOADED;
}
//TODO-SPLIT define load()
static setup0(options) {
let combinedoptions = Transport.mergeoptions(defaulthttpoptions, options.http);
try {
@ -393,5 +391,6 @@ class TransportHTTP extends Transport {
}
Transports._transportclasses["HTTP"] = TransportHTTP;
TransportHTTP.requires = []; // Nothing to load
exports = module.exports = TransportHTTP;

View File

@ -141,8 +141,6 @@ class TransportIPFS extends Transport {
}
*/
//TODO-SPLIT define load()
static setup0(options) {
/*
First part of setup, create obj, add to Transports but dont attempt to connect, typically called instead of p_setup if want to parallelize connections.
@ -507,6 +505,9 @@ class TransportIPFS extends Transport {
}
}
TransportIPFS.requires=['https://unpkg.com/ipfs/dist/index.min.js']; // 2.3Mb
Transports._transportclasses["IPFS"] = TransportIPFS;
// noinspection JSUndefinedPropertyAssignment
exports = module.exports = TransportIPFS;

View File

@ -398,5 +398,6 @@ class TransportWEBTORRENT extends Transport {
}
Transports._transportclasses["WEBTORRENT"] = TransportWEBTORRENT;
TransportWEBTORRENT.requires = ['https://cdn.jsdelivr.net/npm/webtorrent@latest/webtorrent.min.js'];
exports = module.exports = TransportWEBTORRENT;

View File

@ -269,4 +269,5 @@ class TransportWOLK extends Transport {
}
}
Transports._transportclasses["WOLK"] = TransportWOLK;
TransportWOLK.requires = []; //TODO-SPLIT correct this
exports = module.exports = TransportWOLK;

View File

@ -368,4 +368,5 @@ class TransportYJS extends Transport {
}
TransportYJS.Y = Y; // Allow node tests to find it
Transports._transportclasses["YJS"] = TransportYJS;
TransportYJS.requires = []; //TODO-SPLIT correct this
exports = module.exports = TransportYJS;

View File

@ -712,10 +712,36 @@ class Transports {
}
}
static _tabbrevs(options) {
// options = {transports, defaulttransports, ... }
// returns [ABBREVIATION] e.g. ["IPFS","HTTP"]
let tabbrevs = options.transports; // Array of transport abbreviations
if (!(tabbrevs && tabbrevs.length)) { tabbrevs = options.defaulttransports || [] }
// WOLK is currently working (I think)
// GUN is turned off by default because it fills up localstorage on browser and stops working, https://github.com/internetarchive/dweb-archive/issues/106
//if (! tabbrevs.length) { tabbrevs = ["HTTP", "YJS", "IPFS", "WEBTORRENT", "GUN", "WOLK"]; } // SEE-OTHER-ADDTRANSPORT
if (! tabbrevs.length) { tabbrevs = ["HTTP", "IPFS", "WEBTORRENT", "WOLK"]; } // SEE-OTHER-ADDTRANSPORT
tabbrevs = tabbrevs.map(n => n.toUpperCase());
return tabbrevs;
}
/**
* Load required javascript into an html page
* This is tricky - order is significant, (see dweb-archive/archive.html for a hopefully working example)
*/
static loadIntoHtmlPage(options) {
this._tabbrevs(options).forEach(t => {
this._transportclasses[t].requires.map(s => {
debug("Loading %s %s", t, s);
document.write('<script src="' + s + '"><\/script>');
});
})
}
static connect(options, cb) {
const prom = this.p_connect(options);
if (cb) { prom.catch((err) => cb(err)).then((res)=>cb(null,res)); } else { return prom; } // This should be a standard unpromisify pattern
}
static async p_connect(options) {
/*
This is a standardish starting process, feel free to subclass or replace !
@ -724,15 +750,8 @@ class Transports {
*/
try {
options = options || {};
let tabbrevs = options.transports; // Array of transport abbreviations
this._optionspaused = (options.paused || []).map(n => n.toUpperCase()); // Array of transports paused - defaults to none, upper cased
if (!(tabbrevs && tabbrevs.length)) { tabbrevs = options.defaulttransports || [] }
// WOLK is currently failing, and what is worse returning a data structure with a 404 instead of just failing
// GUN is turned off by default because it fills up localstorage on browser and stops working, https://github.com/internetarchive/dweb-archive/issues/106
//if (! tabbrevs.length) { tabbrevs = ["HTTP", "YJS", "IPFS", "WEBTORRENT", "GUN", "WOLK"]; } // SEE-OTHER-ADDTRANSPORT
if (! tabbrevs.length) { tabbrevs = ["HTTP", "IPFS", "WEBTORRENT", "WOLK"]; } // SEE-OTHER-ADDTRANSPORT
tabbrevs = tabbrevs.map(n => n.toUpperCase());
let transports = this.setup0(tabbrevs, options); // synchronous
let transports = this.setup0(this._tabbrevs(options), options); // synchronous
["statuscb", "mirror"].forEach(k => { if (options[k]) this[k] = options[k];} )
//TODO move this to function and then call this from consumer
if (!!options.statuselement) {