Refactor options

This commit is contained in:
Mitra Ardron 2018-07-13 13:17:16 -07:00
parent 10cc6d93dd
commit 06a3408966
4 changed files with 37 additions and 44 deletions

View File

@ -16,11 +16,11 @@ const utils = require('./utils'); // Utility functions
//unused currently: function delay(ms, val) { return new Promise(resolve => {setTimeout(() => { resolve(val); },ms)})}
let defaultoptions = {
//peers: [ "http://xxxx:yyyy/gun" ] // TODO-GUN get server setup and then replace this URL
peers: [ "http://dweb.me:4246/gun" ] // TODO-GUN get server setup and then replace this URL
//localstore: true #True is default
};
//To run a superpeer - cd wherever; node install gun; cd node_modules/gun; npm start - starts server by default on port 8080, or set an "env" - see http.js
//node examples/http.js 4246
//setenv GUN_ENV false; node examples/http.js 4246
//Make sure to open of the port (typically in /etc/ferm)
//TODO-GUN - figure out how to make server persistent - started by systemctl etc and incorporate in dweb-gateway/scripts/install.sh
@ -43,7 +43,7 @@ class TransportGUN extends Transport {
constructor(options, verbose) {
super(options, verbose);
this.options = options; // Dictionary of options { ipfs: {...}, "yarrays", yarray: {...} }
this.options = options; // Dictionary of options
this.gun = undefined;
this.name = "GUN"; // For console log etc
this.supportURLs = ['gun'];
@ -72,7 +72,7 @@ class TransportGUN extends Transport {
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.
options: { gun: { }, } Set of options - "gun" is used for those to pass direct to Gun
*/
let combinedoptions = Transport.mergeoptions(defaultoptions, options);
let combinedoptions = Transport.mergeoptions(defaultoptions, options.gun);
console.log("GUN options %o", combinedoptions); // Log even if !verbose
let t = new TransportGUN(combinedoptions, verbose); // Note doesnt start IPFS or OrbitDB
t.gun = new Gun(t.options.gun); // This doesnt connect, just creates db structure

View File

@ -29,21 +29,19 @@ const Transports = require('./Transports'); // Manage all Transports that are lo
const utils = require('./utils'); // Utility functions
const defaultoptions = {
ipfs: {
repo: '/tmp/dweb_ipfsv2700', //TODO-IPFS think through where, esp for browser
//init: false,
//start: false,
//TODO-IPFS-Q how is this decentralized - can it run offline? Does it depend on star-signal.cloud.ipfs.team
config: {
// Addresses: { Swarm: [ '/dns4/star-signal.cloud.ipfs.team/wss/p2p-webrtc-star']}, // For Y - same as defaults
// Addresses: { Swarm: [ ] }, // Disable WebRTC to test browser crash, note disables Y so doesnt work.
//Addresses: {Swarm: ['/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star']}, // from https://github.com/ipfs/js-ipfs#faq 2017-12-05 as alternative to webrtc works sort-of
Bootstrap: ['/dns4/dweb.me/tcp/4245/wss/ipfs/QmPNgKEjC7wkpu3aHUzKKhZmbEfiGzL5TP1L8zZoHJyXZW'], // Supposedly connects to Dweb IPFS instance, but doesnt work (nor does ".../wss/...")
},
//init: true, // Comment out for Y
EXPERIMENTAL: {
pubsub: true
}
repo: '/tmp/dweb_ipfsv2700', //TODO-IPFS think through where, esp for browser
//init: false,
//start: false,
//TODO-IPFS-Q how is this decentralized - can it run offline? Does it depend on star-signal.cloud.ipfs.team
config: {
// Addresses: { Swarm: [ '/dns4/star-signal.cloud.ipfs.team/wss/p2p-webrtc-star']}, // For Y - same as defaults
// Addresses: { Swarm: [ ] }, // Disable WebRTC to test browser crash, note disables Y so doesnt work.
//Addresses: {Swarm: ['/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star']}, // from https://github.com/ipfs/js-ipfs#faq 2017-12-05 as alternative to webrtc works sort-of
Bootstrap: ['/dns4/dweb.me/tcp/4245/wss/ipfs/QmPNgKEjC7wkpu3aHUzKKhZmbEfiGzL5TP1L8zZoHJyXZW'], // Supposedly connects to Dweb IPFS instance, but doesnt work (nor does ".../wss/...")
},
//init: true, // Comment out for Y
EXPERIMENTAL: {
pubsub: true
}
};
@ -53,13 +51,13 @@ class TransportIPFS extends Transport {
Fields:
ipfs: object returned when starting IPFS
yarray: object returned when starting yarray
TODO - this is not complete
*/
constructor(options, verbose) {
super(options, verbose);
this.ipfs = undefined; // Undefined till start IPFS
this.options = options; // Dictionary of options { ipfs: {...}, "yarrays", yarray: {...} }
this.options = options; // Dictionary of options
this.name = "IPFS"; // For console log etc
this.supportURLs = ['ipfs'];
this.supportFunctions = ['fetch', 'store']; // Does not support reverse, createReadStream fails on files uploaded with urlstore TODO reenable when Kyle fixes urlstore
@ -85,7 +83,7 @@ class TransportIPFS extends Transport {
*/
const self = this;
return new Promise((resolve, reject) => {
this.ipfs = new IPFS(this.options.ipfs);
this.ipfs = new IPFS(this.options);
this.ipfs.on('ready', () => {
//this._makepromises();
resolve();
@ -104,7 +102,7 @@ class TransportIPFS extends Transport {
/*
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.
*/
const combinedoptions = Transport.mergeoptions(defaultoptions, options);
const combinedoptions = Transport.mergeoptions(defaultoptions, options.ipfs);
if (verbose) console.log("IPFS loading options %o", combinedoptions);
const t = new TransportIPFS(combinedoptions, verbose); // Note doesnt start IPFS
Transports.addtransport(t);
@ -268,7 +266,7 @@ class TransportIPFS extends Transport {
} catch (err) { // TimeoutError or could be some other error from IPFS etc
console.log("Caught misc error in TransportIPFS.p_rawfetch trying IPFS", err.message);
try {
let ipfsurl = TransportIPFS.ipfsGatewayFrom(url)
let ipfsurl = TransportIPFS.ipfsGatewayFrom(url);
return await utils.p_timeout(
httptools.p_GET(ipfsurl), // Returns a buffer
timeoutMS, "Timed out IPFS fetch of "+ipfsurl)
@ -339,7 +337,7 @@ class TransportIPFS extends Transport {
verbose = true;
if (verbose) console.log("p_f_createReadStream",url);
const mh = TransportIPFS.multihashFrom(url);
const links = await this.ipfs.object.links(mh)
const links = await this.ipfs.object.links(mh);
let throughstream; //Holds pointer to stream between calls.
const self = this;
function crs(opts) { // This is a synchronous function

View File

@ -15,7 +15,6 @@ const Transport = require('./Transport.js'); // Base class for TransportXyz
const Transports = require('./Transports'); // Manage all Transports that are loaded
let defaultoptions = {
webtorrent: {}
};
class TransportWEBTORRENT extends Transport {
@ -42,7 +41,7 @@ class TransportWEBTORRENT extends Transport {
*/
let self = this;
return new Promise((resolve, reject) => {
this.webtorrent = new WebTorrent(this.options.webtorrent);
this.webtorrent = new WebTorrent(this.options);
this.webtorrent.once("ready", () => {
console.log("WEBTORRENT READY");
resolve();
@ -58,7 +57,7 @@ class TransportWEBTORRENT extends Transport {
/*
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.
*/
let combinedoptions = Transport.mergeoptions(defaultoptions, options);
let combinedoptions = Transport.mergeoptions(defaultoptions, options.webtorrent);
console.log("WebTorrent options %o", combinedoptions);
let t = new TransportWEBTORRENT(combinedoptions, verbose);
Transports.addtransport(t);
@ -103,7 +102,7 @@ class TransportWEBTORRENT extends Transport {
throw new errors.CodingError("TransportWEBTORRENT.p_rawfetch: requires url");
}
const urlstring = (typeof url === "string" ? url : url.href)
const urlstring = (typeof url === "string" ? url : url.href);
const index = urlstring.indexOf('/');
if (index === -1) {
@ -216,7 +215,7 @@ class TransportWEBTORRENT extends Transport {
} catch(err) {
console.log(`p_fileFrom failed on ${url} ${err.message}`);
throw(err);
};
}
}
@ -248,7 +247,7 @@ class TransportWEBTORRENT extends Transport {
} catch(err) {
console.log(`p_f_createReadStream failed on ${url} ${err.message}`);
throw(err);
};
}
}
createReadStream(file, opts, verbose) {
@ -268,9 +267,9 @@ class TransportWEBTORRENT extends Transport {
fileStream.pipe(through);
return through;
} catch(err) {
console.log("TransportWEBTORRENT caught error", err)
console.log("TransportWEBTORRENT caught error", err);
if (typeof through.destroy === 'function')
through.destroy(err)
through.destroy(err);
else through.emit('error', err)
}
}

View File

@ -25,7 +25,6 @@ const Transports = require('./Transports'); // Manage all Transports that are lo
const utils = require('./utils'); // Utility functions
let defaultoptions = {
yarray: { // Based on how IIIF uses them in bootstrap.js in ipfs-iiif-db repo
db: {
name: 'indexeddb', // leveldb in node
},
@ -33,21 +32,18 @@ let defaultoptions = {
name: 'ipfs',
//ipfs: ipfs, // Need to link IPFS here once created
},
}
};
class TransportYJS extends Transport {
/*
YJS specific transport - over IPFS, but could probably use other YJS transports
Fields:
ipfs: object returned when starting IPFS
yarray: object returned when starting yarray
Fields: TODO document this
*/
constructor(options, verbose) {
super(options, verbose);
this.options = options; // Dictionary of options { ipfs: {...}, "yarrays", yarray: {...} }
this.options = options; // Dictionary of options
this.name = "YJS"; // For console log etc
this.supportURLs = ['yjs'];
this.supportFunctions = ['fetch', 'add', 'list', 'listmonitor', 'newlisturls',
@ -70,7 +66,7 @@ class TransportYJS extends Transport {
if (verbose) console.log("Found Y for", url);
return this.yarrays[url];
} else {
let options = Transport.mergeoptions(this.options.yarray, {connector: {room: url}}, opts); // Copies options, ipfs will be set already
let options = Transport.mergeoptions(this.options, {connector: {room: url}}, opts); // Copies options, ipfs will be set already
if (verbose) console.log("Creating Y for", url); //"options=",options);
return this.yarrays[url] = await Y(options);
}
@ -103,7 +99,7 @@ class TransportYJS extends Transport {
/*
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.
*/
let combinedoptions = Transport.mergeoptions(defaultoptions, options);
let combinedoptions = Transport.mergeoptions(defaultoptions, options.yjs);
if (verbose) console.log("YJS options %o", combinedoptions); // Log even if !verbose
let t = new TransportYJS(combinedoptions, verbose); // Note doesnt start IPFS or Y
Transports.addtransport(t);
@ -119,7 +115,7 @@ class TransportYJS extends Transport {
try {
this.status = Transport.STATUS_STARTING; // Should display, but probably not refreshed in most case
if (cb) cb(this);
this.options.yarray.connector.ipfs = Transports.ipfs(verbose).ipfs; // Find an IPFS to use (IPFS's should be starting in p_setup1)
this.options.connector.ipfs = Transports.ipfs(verbose).ipfs; // Find an IPFS to use (IPFS's should be starting in p_setup1)
this.yarrays = {};
await this.p_status(verbose);
} catch(err) {
@ -135,7 +131,7 @@ class TransportYJS extends Transport {
Return a string for the status of a transport. No particular format, but keep it short as it will probably be in a small area of the screen.
For YJS, its online if IPFS is.
*/
this.status = (await this.options.yarray.connector.ipfs.isOnline()) ? Transport.STATUS_CONNECTED : Transport.STATUS_FAILED;
this.status = (await this.options.connector.ipfs.isOnline()) ? Transport.STATUS_CONNECTED : Transport.STATUS_FAILED;
return super.p_status(verbose);
}
@ -240,7 +236,7 @@ class TransportYJS extends Transport {
async p_newdatabase(pubkey, {verbose=false}={}) {
//if (pubkey instanceof Dweb.PublicPrivate)
if (pubkey.hasOwnProperty("keypair"))
pubkey = pubkey.keypair.signingexport()
pubkey = pubkey.keypair.signingexport();
// By this point pubkey should be an export of a public key of form xyz:abc where xyz
// specifies the type of public key (NACL VERIFY being the only kind we expect currently)
let u = `yjs:/yjs/${encodeURIComponent(pubkey)}`;