/*
This file is part of susyweb.js.
susyweb.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
susyweb.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MSRCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with susyweb.js. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file index.js
* @author Fabian Vogelsteller <fabian@ethereum.org>
* @date 2017
*/
"use strict";
var _ = require('underscore');
var swarm = require("swarm-js");
var Bzz = function Bzz(provider) {
this.givenProvider = Bzz.givenProvider;
// if new SusyWeb(provider), use the given provider
Eif (provider && provider._requestManager) {
provider = provider.currentProvider;
}
// only allow file picker when in browser
Iif(typeof document !== 'undefined') {
this.pick = swarm.pick;
}
this.setProvider(provider);
};
// set default sophon provider
/* jshint ignore:start */
Bzz.givenProvider = null;
Iif(typeof sophonProvider !== 'undefined' && sophonProvider.bzz) {
Bzz.givenProvider = sophonProvider;
}
/* jshint ignore:end */
Bzz.prototype.setProvider = function(provider) {
// is sophon provider
Iif(_.isObject(provider) && _.isString(provider.bzz)) {
provider = provider.bzz;
// is no string, set default
} else if(!_.isString(provider)) {
provider = 'http://swarm-gateways.net'; // default to gateway
}
Eif(_.isString(provider)) {
this.currentProvider = provider;
} else {
return false;
}
// add functions
this.download = swarm.at(provider).download;
this.upload = swarm.at(provider).upload;
this.isAvailable = swarm.at(provider).isAvailable;
return true;
};
module.exports = Bzz;
|