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)})} //unused currently: function delay(ms, val) { return new Promise(resolve => {setTimeout(() => { resolve(val); },ms)})}
let defaultoptions = { 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 //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 //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) //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 //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) { constructor(options, verbose) {
super(options, verbose); super(options, verbose);
this.options = options; // Dictionary of options { ipfs: {...}, "yarrays", yarray: {...} } this.options = options; // Dictionary of options
this.gun = undefined; this.gun = undefined;
this.name = "GUN"; // For console log etc this.name = "GUN"; // For console log etc
this.supportURLs = ['gun']; 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. 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 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 console.log("GUN options %o", combinedoptions); // Log even if !verbose
let t = new TransportGUN(combinedoptions, verbose); // Note doesnt start IPFS or OrbitDB 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 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 utils = require('./utils'); // Utility functions
const defaultoptions = { const defaultoptions = {
ipfs: { repo: '/tmp/dweb_ipfsv2700', //TODO-IPFS think through where, esp for browser
repo: '/tmp/dweb_ipfsv2700', //TODO-IPFS think through where, esp for browser //init: false,
//init: false, //start: false,
//start: false, //TODO-IPFS-Q how is this decentralized - can it run offline? Does it depend on star-signal.cloud.ipfs.team
//TODO-IPFS-Q how is this decentralized - can it run offline? Does it depend on star-signal.cloud.ipfs.team config: {
config: { // Addresses: { Swarm: [ '/dns4/star-signal.cloud.ipfs.team/wss/p2p-webrtc-star']}, // For Y - same as defaults
// 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: [ ] }, // 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
//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/...")
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
//init: true, // Comment out for Y EXPERIMENTAL: {
EXPERIMENTAL: { pubsub: true
pubsub: true
}
} }
}; };
@ -53,13 +51,13 @@ class TransportIPFS extends Transport {
Fields: Fields:
ipfs: object returned when starting IPFS ipfs: object returned when starting IPFS
yarray: object returned when starting yarray TODO - this is not complete
*/ */
constructor(options, verbose) { constructor(options, verbose) {
super(options, verbose); super(options, verbose);
this.ipfs = undefined; // Undefined till start IPFS 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.name = "IPFS"; // For console log etc
this.supportURLs = ['ipfs']; this.supportURLs = ['ipfs'];
this.supportFunctions = ['fetch', 'store']; // Does not support reverse, createReadStream fails on files uploaded with urlstore TODO reenable when Kyle fixes urlstore 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; const self = this;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.ipfs = new IPFS(this.options.ipfs); this.ipfs = new IPFS(this.options);
this.ipfs.on('ready', () => { this.ipfs.on('ready', () => {
//this._makepromises(); //this._makepromises();
resolve(); 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. 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); if (verbose) console.log("IPFS loading options %o", combinedoptions);
const t = new TransportIPFS(combinedoptions, verbose); // Note doesnt start IPFS const t = new TransportIPFS(combinedoptions, verbose); // Note doesnt start IPFS
Transports.addtransport(t); Transports.addtransport(t);
@ -268,7 +266,7 @@ class TransportIPFS extends Transport {
} catch (err) { // TimeoutError or could be some other error from IPFS etc } 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); console.log("Caught misc error in TransportIPFS.p_rawfetch trying IPFS", err.message);
try { try {
let ipfsurl = TransportIPFS.ipfsGatewayFrom(url) let ipfsurl = TransportIPFS.ipfsGatewayFrom(url);
return await utils.p_timeout( return await utils.p_timeout(
httptools.p_GET(ipfsurl), // Returns a buffer httptools.p_GET(ipfsurl), // Returns a buffer
timeoutMS, "Timed out IPFS fetch of "+ipfsurl) timeoutMS, "Timed out IPFS fetch of "+ipfsurl)
@ -339,7 +337,7 @@ class TransportIPFS extends Transport {
verbose = true; verbose = true;
if (verbose) console.log("p_f_createReadStream",url); if (verbose) console.log("p_f_createReadStream",url);
const mh = TransportIPFS.multihashFrom(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. let throughstream; //Holds pointer to stream between calls.
const self = this; const self = this;
function crs(opts) { // This is a synchronous function 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 const Transports = require('./Transports'); // Manage all Transports that are loaded
let defaultoptions = { let defaultoptions = {
webtorrent: {}
}; };
class TransportWEBTORRENT extends Transport { class TransportWEBTORRENT extends Transport {
@ -42,7 +41,7 @@ class TransportWEBTORRENT extends Transport {
*/ */
let self = this; let self = this;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.webtorrent = new WebTorrent(this.options.webtorrent); this.webtorrent = new WebTorrent(this.options);
this.webtorrent.once("ready", () => { this.webtorrent.once("ready", () => {
console.log("WEBTORRENT READY"); console.log("WEBTORRENT READY");
resolve(); 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. 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); console.log("WebTorrent options %o", combinedoptions);
let t = new TransportWEBTORRENT(combinedoptions, verbose); let t = new TransportWEBTORRENT(combinedoptions, verbose);
Transports.addtransport(t); Transports.addtransport(t);
@ -103,7 +102,7 @@ class TransportWEBTORRENT extends Transport {
throw new errors.CodingError("TransportWEBTORRENT.p_rawfetch: requires url"); 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('/'); const index = urlstring.indexOf('/');
if (index === -1) { if (index === -1) {
@ -216,7 +215,7 @@ class TransportWEBTORRENT extends Transport {
} catch(err) { } catch(err) {
console.log(`p_fileFrom failed on ${url} ${err.message}`); console.log(`p_fileFrom failed on ${url} ${err.message}`);
throw(err); throw(err);
}; }
} }
@ -248,7 +247,7 @@ class TransportWEBTORRENT extends Transport {
} catch(err) { } catch(err) {
console.log(`p_f_createReadStream failed on ${url} ${err.message}`); console.log(`p_f_createReadStream failed on ${url} ${err.message}`);
throw(err); throw(err);
}; }
} }
createReadStream(file, opts, verbose) { createReadStream(file, opts, verbose) {
@ -268,9 +267,9 @@ class TransportWEBTORRENT extends Transport {
fileStream.pipe(through); fileStream.pipe(through);
return through; return through;
} catch(err) { } catch(err) {
console.log("TransportWEBTORRENT caught error", err) console.log("TransportWEBTORRENT caught error", err);
if (typeof through.destroy === 'function') if (typeof through.destroy === 'function')
through.destroy(err) through.destroy(err);
else through.emit('error', 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 const utils = require('./utils'); // Utility functions
let defaultoptions = { let defaultoptions = {
yarray: { // Based on how IIIF uses them in bootstrap.js in ipfs-iiif-db repo
db: { db: {
name: 'indexeddb', // leveldb in node name: 'indexeddb', // leveldb in node
}, },
@ -33,21 +32,18 @@ let defaultoptions = {
name: 'ipfs', name: 'ipfs',
//ipfs: ipfs, // Need to link IPFS here once created //ipfs: ipfs, // Need to link IPFS here once created
}, },
}
}; };
class TransportYJS extends Transport { class TransportYJS extends Transport {
/* /*
YJS specific transport - over IPFS, but could probably use other YJS transports YJS specific transport - over IPFS, but could probably use other YJS transports
Fields: Fields: TODO document this
ipfs: object returned when starting IPFS
yarray: object returned when starting yarray
*/ */
constructor(options, verbose) { constructor(options, verbose) {
super(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.name = "YJS"; // For console log etc
this.supportURLs = ['yjs']; this.supportURLs = ['yjs'];
this.supportFunctions = ['fetch', 'add', 'list', 'listmonitor', 'newlisturls', this.supportFunctions = ['fetch', 'add', 'list', 'listmonitor', 'newlisturls',
@ -70,7 +66,7 @@ class TransportYJS extends Transport {
if (verbose) console.log("Found Y for", url); if (verbose) console.log("Found Y for", url);
return this.yarrays[url]; return this.yarrays[url];
} else { } 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); if (verbose) console.log("Creating Y for", url); //"options=",options);
return this.yarrays[url] = await Y(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. 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 if (verbose) console.log("YJS options %o", combinedoptions); // Log even if !verbose
let t = new TransportYJS(combinedoptions, verbose); // Note doesnt start IPFS or Y let t = new TransportYJS(combinedoptions, verbose); // Note doesnt start IPFS or Y
Transports.addtransport(t); Transports.addtransport(t);
@ -119,7 +115,7 @@ class TransportYJS extends Transport {
try { try {
this.status = Transport.STATUS_STARTING; // Should display, but probably not refreshed in most case this.status = Transport.STATUS_STARTING; // Should display, but probably not refreshed in most case
if (cb) cb(this); 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 = {}; this.yarrays = {};
await this.p_status(verbose); await this.p_status(verbose);
} catch(err) { } 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. 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. 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); return super.p_status(verbose);
} }
@ -240,7 +236,7 @@ class TransportYJS extends Transport {
async p_newdatabase(pubkey, {verbose=false}={}) { async p_newdatabase(pubkey, {verbose=false}={}) {
//if (pubkey instanceof Dweb.PublicPrivate) //if (pubkey instanceof Dweb.PublicPrivate)
if (pubkey.hasOwnProperty("keypair")) 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 // 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) // specifies the type of public key (NACL VERIFY being the only kind we expect currently)
let u = `yjs:/yjs/${encodeURIComponent(pubkey)}`; let u = `yjs:/yjs/${encodeURIComponent(pubkey)}`;