mirror of
https://github.com/fluencelabs/dweb-transports
synced 2025-04-25 06:42:17 +00:00
Push script loading into Transports
This commit is contained in:
parent
8fc8e8761f
commit
0229bd92aa
@ -392,4 +392,7 @@ class TransportGUN extends Transport {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Transports._transportclasses["GUN"] = TransportGUN;
|
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;
|
exports = module.exports = TransportGUN;
|
||||||
|
@ -55,8 +55,6 @@ class TransportHTTP extends Transport {
|
|||||||
this.status = Transport.STATUS_LOADED;
|
this.status = Transport.STATUS_LOADED;
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO-SPLIT define load()
|
|
||||||
|
|
||||||
static setup0(options) {
|
static setup0(options) {
|
||||||
let combinedoptions = Transport.mergeoptions(defaulthttpoptions, options.http);
|
let combinedoptions = Transport.mergeoptions(defaulthttpoptions, options.http);
|
||||||
try {
|
try {
|
||||||
@ -393,5 +391,6 @@ class TransportHTTP extends Transport {
|
|||||||
|
|
||||||
}
|
}
|
||||||
Transports._transportclasses["HTTP"] = TransportHTTP;
|
Transports._transportclasses["HTTP"] = TransportHTTP;
|
||||||
|
TransportHTTP.requires = []; // Nothing to load
|
||||||
exports = module.exports = TransportHTTP;
|
exports = module.exports = TransportHTTP;
|
||||||
|
|
||||||
|
@ -141,8 +141,6 @@ class TransportIPFS extends Transport {
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//TODO-SPLIT define load()
|
|
||||||
|
|
||||||
static setup0(options) {
|
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.
|
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;
|
Transports._transportclasses["IPFS"] = TransportIPFS;
|
||||||
// noinspection JSUndefinedPropertyAssignment
|
// noinspection JSUndefinedPropertyAssignment
|
||||||
exports = module.exports = TransportIPFS;
|
exports = module.exports = TransportIPFS;
|
||||||
|
@ -398,5 +398,6 @@ class TransportWEBTORRENT extends Transport {
|
|||||||
|
|
||||||
}
|
}
|
||||||
Transports._transportclasses["WEBTORRENT"] = TransportWEBTORRENT;
|
Transports._transportclasses["WEBTORRENT"] = TransportWEBTORRENT;
|
||||||
|
TransportWEBTORRENT.requires = ['https://cdn.jsdelivr.net/npm/webtorrent@latest/webtorrent.min.js'];
|
||||||
|
|
||||||
exports = module.exports = TransportWEBTORRENT;
|
exports = module.exports = TransportWEBTORRENT;
|
||||||
|
@ -269,4 +269,5 @@ class TransportWOLK extends Transport {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Transports._transportclasses["WOLK"] = TransportWOLK;
|
Transports._transportclasses["WOLK"] = TransportWOLK;
|
||||||
|
TransportWOLK.requires = []; //TODO-SPLIT correct this
|
||||||
exports = module.exports = TransportWOLK;
|
exports = module.exports = TransportWOLK;
|
||||||
|
@ -368,4 +368,5 @@ class TransportYJS extends Transport {
|
|||||||
}
|
}
|
||||||
TransportYJS.Y = Y; // Allow node tests to find it
|
TransportYJS.Y = Y; // Allow node tests to find it
|
||||||
Transports._transportclasses["YJS"] = TransportYJS;
|
Transports._transportclasses["YJS"] = TransportYJS;
|
||||||
|
TransportYJS.requires = []; //TODO-SPLIT correct this
|
||||||
exports = module.exports = TransportYJS;
|
exports = module.exports = TransportYJS;
|
||||||
|
@ -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) {
|
static connect(options, cb) {
|
||||||
const prom = this.p_connect(options);
|
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
|
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) {
|
static async p_connect(options) {
|
||||||
/*
|
/*
|
||||||
This is a standardish starting process, feel free to subclass or replace !
|
This is a standardish starting process, feel free to subclass or replace !
|
||||||
@ -724,15 +750,8 @@ class Transports {
|
|||||||
*/
|
*/
|
||||||
try {
|
try {
|
||||||
options = options || {};
|
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
|
this._optionspaused = (options.paused || []).map(n => n.toUpperCase()); // Array of transports paused - defaults to none, upper cased
|
||||||
if (!(tabbrevs && tabbrevs.length)) { tabbrevs = options.defaulttransports || [] }
|
let transports = this.setup0(this._tabbrevs(options), options); // synchronous
|
||||||
// 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
|
|
||||||
["statuscb", "mirror"].forEach(k => { if (options[k]) this[k] = options[k];} )
|
["statuscb", "mirror"].forEach(k => { if (options[k]) this[k] = options[k];} )
|
||||||
//TODO move this to function and then call this from consumer
|
//TODO move this to function and then call this from consumer
|
||||||
if (!!options.statuselement) {
|
if (!!options.statuselement) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user