mirror of
https://github.com/fluencelabs/dweb-transports
synced 2025-04-24 14:22:17 +00:00
Split - framework and IPFS partial
This commit is contained in:
parent
e4e2b5cfab
commit
93014ad3ff
@ -18,6 +18,8 @@ class 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.
|
||||
|
@ -96,6 +96,8 @@ class TransportGUN extends Transport {
|
||||
return this.gun.path(patharray); // Not sure how this could become undefined as it will return g before the path is walked, but if do a lookup on this "g" then should get undefined
|
||||
}
|
||||
|
||||
//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.
|
||||
|
@ -55,6 +55,8 @@ class TransportHTTP extends Transport {
|
||||
this.status = Transport.STATUS_LOADED;
|
||||
}
|
||||
|
||||
//TODO-SPLIT define load()
|
||||
|
||||
static setup0(options) {
|
||||
let combinedoptions = Transport.mergeoptions(defaulthttpoptions, options.http);
|
||||
try {
|
||||
|
@ -9,13 +9,15 @@ const httptools = require('./httptools'); // Expose some of the httptools so tha
|
||||
const debug = require('debug')('dweb-transports:ipfs');
|
||||
|
||||
// IPFS components
|
||||
const IPFS = require('ipfs');
|
||||
//TODO-SPLIT remove this import depend on archive.html or node to pre-load
|
||||
let IPFS; //TODO-SPLIT move this line lower when fix structure
|
||||
//IPFS = require('ipfs');
|
||||
//TODO-SPLIT remove this import depend on archive.html or node to pre-load
|
||||
const ipfsAPI = require('ipfs-http-client');
|
||||
const CID = require('cids');
|
||||
//Removed next two as not needed if use "Kludge" flagged below.
|
||||
//const dagPB = require('ipld-dag-pb');
|
||||
//const DAGNode = dagPB.DAGNode; // So can check its type
|
||||
const unixFs = require('ipfs-unixfs');
|
||||
//TODO-SPLIT looks like can get this from Ipfs.CID
|
||||
//const CID = require('cids');
|
||||
//TODO-SPLIT unclear why need unixfs - only appears to be used in test_ipfs.js
|
||||
//const unixFs = require('ipfs-unixfs');
|
||||
|
||||
// Library packages other than IPFS
|
||||
const Url = require('url');
|
||||
@ -89,10 +91,17 @@ class TransportIPFS extends Transport {
|
||||
});
|
||||
}
|
||||
IPFSAutoConnect(cb) {
|
||||
if (global.Ipfs) {
|
||||
IPFS = global.Ipfs; //Loaded by <script etc but still need a create
|
||||
} else if (window.Ipfs) {
|
||||
IPFS = window.Ipfs; //Loaded by <script etc but still need a create
|
||||
}
|
||||
//TODO-SPLIT I think next few lines are wrong, dont think I've seen global.ipfs or window.ipfs but
|
||||
//TODO-SPLIT https://github.com/ipfs/js-ipfs implies global.Ipfs but needs a "create" or "new"
|
||||
if (global.ipfs) {
|
||||
this._ipfsversion(global.ipfs, "global.ipfs", cb );
|
||||
} else if (typeof window !== "undefined" && window.ipfs) {
|
||||
this._ipfsversion(window.ipfs, "window.ipfs", cb );
|
||||
this._ipfsversion(window.ipfs, "window.ipfs", cb);
|
||||
} else {
|
||||
// noinspection ES6ConvertVarToLetConst
|
||||
var ipfs = ipfsAPI('localhost', '5001', {protocol: 'http'}); // leaving out the arguments will default to these values
|
||||
@ -139,6 +148,8 @@ 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.
|
||||
@ -212,7 +223,7 @@ class TransportIPFS extends Transport {
|
||||
/*
|
||||
Convert a CID into a standardised URL e.g. ipfs:/ipfs/abc123
|
||||
*/
|
||||
if (unknown instanceof CID)
|
||||
if (unknown instanceof IPFS.CID) //TODO-SPLIT - I think there is a way to get this from a types array
|
||||
return "ipfs:/ipfs/"+unknown.toBaseEncodedString();
|
||||
if (typeof unknown === "object" && unknown.hash) // e.g. from files.add
|
||||
return "ipfs:/ipfs/"+unknown.hash;
|
||||
@ -228,7 +239,7 @@ class TransportIPFS extends Transport {
|
||||
returns: CID
|
||||
throws: TransportError if cant convert
|
||||
*/
|
||||
if (url instanceof CID) return url;
|
||||
if (url instanceof IPFS.CID) return url;
|
||||
if (typeof(url) === "string") url = Url.parse(url);
|
||||
if (url && url["pathname"]) { // On browser "instanceof Url" isn't valid)
|
||||
const patharr = url.pathname.split('/');
|
||||
@ -236,7 +247,7 @@ class TransportIPFS extends Transport {
|
||||
throw new errors.TransportError("TransportIPFS.cidFrom bad format for url should be dweb: or ipfs:/ipfs/...: " + url.href);
|
||||
if (patharr.length > 3)
|
||||
throw new errors.TransportError("TransportIPFS.cidFrom not supporting paths in url yet, should be dweb: or ipfs:/ipfs/...: " + url.href);
|
||||
return new CID(patharr[2]);
|
||||
return new IPFS.CID(patharr[2]);
|
||||
} else {
|
||||
throw new errors.CodingError("TransportIPFS.cidFrom: Cant convert url", url);
|
||||
}
|
||||
@ -244,7 +255,7 @@ class TransportIPFS extends Transport {
|
||||
|
||||
static _stringFrom(url) {
|
||||
// Tool for ipfsFrom and ipfsGatewayFrom
|
||||
if (url instanceof CID)
|
||||
if (url instanceof IPFS.CID)
|
||||
return "/ipfs/"+url.toBaseEncodedString();
|
||||
if (typeof url === 'object' && url.path) { // It better be URL which unfortunately is hard to test
|
||||
return url.path;
|
||||
@ -275,7 +286,7 @@ class TransportIPFS extends Transport {
|
||||
}
|
||||
|
||||
static multihashFrom(url) {
|
||||
if (url instanceof CID)
|
||||
if (url instanceof IPFS.CID)
|
||||
return url.toBaseEncodedString();
|
||||
if (typeof url === 'object' && url.path)
|
||||
url = url.path; // /ipfs/Q...
|
||||
|
@ -57,6 +57,8 @@ class TransportWEBTORRENT 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.
|
||||
|
@ -41,7 +41,9 @@ class TransportWOLK extends Transport {
|
||||
return wolknode
|
||||
}
|
||||
|
||||
//stuff that happens b/f using ntwk bandwidth (config/connect/stuff)
|
||||
//TODO-SPLIT define load()
|
||||
|
||||
//stuff that happens b/f using ntwk bandwidth (config/connect/stuff)
|
||||
static setup0(options) {
|
||||
let combinedoptions = Transport.mergeoptions(defaultoptions, options.wolk);
|
||||
debug("setup options=%o", combinedoptions);
|
||||
|
@ -96,7 +96,7 @@ class TransportYJS extends Transport {
|
||||
return this.p__y(url, { share: {map: "Map"}}); // Copies options, ipfs will be set already
|
||||
}
|
||||
|
||||
|
||||
//TODO-SPLIT define load()
|
||||
|
||||
static setup0(options) {
|
||||
/*
|
||||
|
@ -651,6 +651,7 @@ class Transports {
|
||||
// "IPFS" or "IPFS,LOCAL,HTTP"
|
||||
let localoptions = {http: {urlbase: "http://localhost:4244"}}; //TODO-MIRROR "localoptions" may not be needed any more
|
||||
return tabbrevs.map((tabbrev) => {
|
||||
//TODO-SPLIT-UPNEXT remove LOCAL - not used any more
|
||||
let transportclass = this._transportclasses[ (tabbrev === "LOCAL") ? "HTTP" : tabbrev ];
|
||||
if (!transportclass) {
|
||||
debug("Connection to %s unavailable", tabbrev);
|
||||
@ -731,8 +732,9 @@ class Transports {
|
||||
//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);
|
||||
let transports = this.setup0(tabbrevs, 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) {
|
||||
let statuselement = options.statuselement;
|
||||
while (statuselement.lastChild) {statuselement.removeChild(statuselement.lastChild); } // Remove any exist status
|
||||
@ -746,6 +748,7 @@ class Transports {
|
||||
}))
|
||||
);
|
||||
}
|
||||
//TODO-SPLIT-UPNEXT invert this, use a waterfall here, and then wrap in promise for p_setup, then put load's here
|
||||
await this.p_setup1(this.refreshstatus);
|
||||
await this.p_setup2(this.refreshstatus);
|
||||
debug("Connection completed to %o", this._connected().map(t=>t.name))
|
||||
|
8884
dist/dweb-transports-bundle.js
vendored
8884
dist/dweb-transports-bundle.js
vendored
File diff suppressed because one or more lines are too long
@ -2,7 +2,6 @@ const IPFS = require('ipfs');
|
||||
|
||||
const canonicaljson = require('@stratumn/canonicaljson');
|
||||
var ipfs;
|
||||
const CID = require('cids');
|
||||
const unixFs = require('ipfs-unixfs');
|
||||
const multihashes = require('multihashes');
|
||||
let tryexpectedfailures = true; // Set to false if want to check the things we expect to fail.
|
||||
@ -31,7 +30,7 @@ function ipfsFrom(url) {
|
||||
Convert to a ipfspath i.e. /ipfs/Qm....
|
||||
Required because of strange differences in APIs between files.cat and dag.get see https://github.com/ipfs/js-ipfs/issues/1229
|
||||
*/
|
||||
if (url instanceof CID)
|
||||
if (url instanceof ipfs.CID)
|
||||
return "/ipfs/"+url.toBaseEncodedString();
|
||||
if (typeof(url) !== "string") { // It better be URL which unfortunately is hard to test
|
||||
url = url.path;
|
||||
@ -42,7 +41,7 @@ function ipfsFrom(url) {
|
||||
throw new errors.CodingError(`ipfsFrom: Cant convert url ${url} into a path starting /ipfs/`);
|
||||
}
|
||||
function multihashFrom(url) {
|
||||
if (url instanceof CID)
|
||||
if (url instanceof ipfs.CID)
|
||||
return url.toBaseEncodedString();
|
||||
if (typeof url === 'object' && url.path)
|
||||
url = url.path; // /ipfs/Q...
|
||||
|
Loading…
x
Reference in New Issue
Block a user