diff --git a/dist/orbitdb.min.js b/dist/orbitdb.min.js index 8090431..5468912 100644 --- a/dist/orbitdb.min.js +++ b/dist/orbitdb.min.js @@ -51,25 +51,30 @@ var OrbitDB = var _promise2 = _interopRequireDefault(_promise); - var _classCallCheck2 = __webpack_require__(56); + var _assign = __webpack_require__(69); + + var _assign2 = _interopRequireDefault(_assign); + + var _classCallCheck2 = __webpack_require__(74); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); - var _createClass2 = __webpack_require__(57); + var _createClass2 = __webpack_require__(75); var _createClass3 = _interopRequireDefault(_createClass2); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - var EventEmitter = __webpack_require__(60).EventEmitter; - var logger = __webpack_require__(61).create("orbit-db.Client"); - var PubSub = __webpack_require__(65); - var CounterStore = __webpack_require__(118); - var KeyValueStore = __webpack_require__(250); - var EventStore = __webpack_require__(252); + var EventEmitter = __webpack_require__(79).EventEmitter; + var logger = __webpack_require__(80).create("orbit-db.Client"); + var EventStore = __webpack_require__(87); + var FeedStore = __webpack_require__(103); + var KeyValueStore = __webpack_require__(105); + var CounterStore = __webpack_require__(107); + var PubSub = __webpack_require__(111); var OrbitDB = function () { - function OrbitDB(ipfs, options) { + function OrbitDB(ipfs) { (0, _classCallCheck3.default)(this, OrbitDB); this._ipfs = ipfs; @@ -81,12 +86,14 @@ var OrbitDB = } (0, _createClass3.default)(OrbitDB, [{ - key: 'eventlog', - value: function eventlog(dbname, options) { + key: 'feed', + value: function feed(dbname, options) { var _this = this; - if (!options) options = { subscribe: true }; - var store = new EventStore(this._ipfs, dbname, options); + if (!options) options = {}; + if (options.subscribe === undefined) (0, _assign2.default)(options, { subscribe: true }); + + var store = new FeedStore(this._ipfs, this.user.username, dbname, options); return this._subscribe(store, dbname, options.subscribe).then(function () { return _this.stores[dbname] = store; }).then(function () { @@ -94,12 +101,14 @@ var OrbitDB = }); } }, { - key: 'kvstore', - value: function kvstore(dbname, options) { + key: 'eventlog', + value: function eventlog(dbname, options) { var _this2 = this; - if (!options) options = { subscribe: true }; - var store = new KeyValueStore(this._ipfs, dbname, options); + if (!options) options = {}; + if (options.subscribe === undefined) (0, _assign2.default)(options, { subscribe: true }); + + var store = new EventStore(this._ipfs, this.user.username, dbname, options); return this._subscribe(store, dbname, options.subscribe).then(function () { return _this2.stores[dbname] = store; }).then(function () { @@ -107,18 +116,35 @@ var OrbitDB = }); } }, { - key: 'counter', - value: function counter(dbname, options) { + key: 'kvstore', + value: function kvstore(dbname, options) { var _this3 = this; - if (!options) options = { subscribe: true }; - var store = new CounterStore(this._ipfs, dbname, options); + if (!options) options = {}; + if (options.subscribe === undefined) (0, _assign2.default)(options, { subscribe: true }); + + var store = new KeyValueStore(this._ipfs, this.user.username, dbname, options); return this._subscribe(store, dbname, options.subscribe).then(function () { return _this3.stores[dbname] = store; }).then(function () { return store; }); } + }, { + key: 'counter', + value: function counter(dbname, options) { + var _this4 = this; + + if (!options) options = {}; + if (options.subscribe === undefined) (0, _assign2.default)(options, { subscribe: true }); + + var store = new CounterStore(this._ipfs, this.user.username, dbname, options); + return this._subscribe(store, dbname, options.subscribe).then(function () { + return _this4.stores[dbname] = store; + }).then(function () { + return store; + }); + } }, { key: 'disconnect', value: function disconnect() { @@ -130,17 +156,17 @@ var OrbitDB = }, { key: '_subscribe', value: function _subscribe(store, dbname, subscribe, callback) { - var _this4 = this; + var _this5 = this; if (subscribe === undefined) subscribe = true; return store.use(this.user.username).then(function (events) { - events.on('readable', _this4._onSync.bind(_this4)); - events.on('data', _this4._onWrite.bind(_this4)); - events.on('load', _this4._onLoad.bind(_this4)); - events.on('close', _this4._onClose.bind(_this4)); + events.on('readable', _this5._onSync.bind(_this5)); + events.on('data', _this5._onWrite.bind(_this5)); + events.on('load', _this5._onLoad.bind(_this5)); + events.on('close', _this5._onClose.bind(_this5)); - if (subscribe) _this4._pubsub.subscribe(dbname, '', _this4._onMessage.bind(_this4)); + if (subscribe) _this5._pubsub.subscribe(dbname, '', _this5._onMessage.bind(_this5)); return; }); @@ -156,6 +182,7 @@ var OrbitDB = }, { key: '_onWrite', value: function _onWrite(dbname, hash) { + if (!hash) throw new Error("Hash can't be null!"); this._pubsub.publish(dbname, hash); this.events.emit('data', dbname, hash); } @@ -178,30 +205,52 @@ var OrbitDB = } }, { key: '_connect', - value: function _connect(host, port, username, password, allowOffline) { - var _this5 = this; + value: function _connect(hash, username, password, allowOffline) { + var _this6 = this; - return new _promise2.default(function (resolve, reject) { - if (allowOffline === undefined) allowOffline = false; + if (allowOffline === undefined) allowOffline = false; - _this5._pubsub = new PubSub(_this5._ipfs); - _this5._pubsub.connect(host, port, username, password).then(function () { - logger.debug('Connected to Pubsub at \'' + host + ':' + port + '\''); - _this5.user = { username: username, id: username }; // TODO: user id from ipfs hash - _this5.network = { host: host, port: port, name: 'TODO: network name' }; - resolve(); - }).catch(function (e) { - logger.warn("Couldn't connect to Pubsub:", e.message); - if (!allowOffline) { - logger.debug("'allowOffline' set to false, terminating"); - _this5._pubsub.disconnect(); - reject(e); - return; - } - _this5.user = { username: username, id: username }; // TODO: user id from ipfs hash - _this5.network = { host: host, port: port, name: 'TODO: network name' }; - resolve(); + var readNetworkInfo = function readNetworkInfo(hash) { + return new _promise2.default(function (resolve, reject) { + _this6._ipfs.cat(hash).then(function (res) { + var buf = ''; + res.on('error', function (err) { + return reject(err); + }).on('data', function (data) { + return buf += data; + }).on('end', function () { + return resolve(buf); + }); + }); }); + }; + + var host = void 0, + port = void 0, + name = void 0; + return readNetworkInfo(hash).then(function (network) { + return JSON.parse(network); + }).then(function (network) { + _this6.network = network; + name = network.name; + host = network.publishers[0].split(":")[0]; + port = network.publishers[0].split(":")[1]; + }).then(function () { + _this6._pubsub = new PubSub(); + return _this6._pubsub.connect(host, port, username, password); + }).then(function () { + logger.debug('Connected to Pubsub at \'' + host + ':' + port + '\''); + _this6.user = { username: username, id: username }; // TODO: user id from ipfs hash + return; + }).catch(function (e) { + logger.warn("Couldn't connect to Pubsub: " + e.message); + if (!allowOffline) { + logger.debug("'allowOffline' set to false, terminating"); + _this6._pubsub.disconnect(); + throw e; + } + _this6.user = { username: username, id: username }; // TODO: user id from ipfs hash + return; }); } }]); @@ -215,24 +264,18 @@ var OrbitDB = (0, _createClass3.default)(OrbitClientFactory, null, [{ key: 'connect', - value: function connect(host, port, username, password, ipfs, options) { - var createClient = function createClient(ipfs) { - return new _promise2.default(function (resolve, reject) { - var client = new OrbitDB(ipfs, options); - client._connect(host, port, username, password, options.allowOffline).then(function () { - return resolve(client); - }).catch(reject); - }); - }; - - options = options ? options : {}; + value: function connect(network, username, password, ipfs, options) { + if (!options) options = { allowOffline: false }; if (!ipfs) { logger.error("IPFS instance not provided"); throw new Error("IPFS instance not provided"); } - return createClient(ipfs); + var client = new OrbitDB(ipfs); + return client._connect(network, username, password, options.allowOffline).then(function () { + return client; + }); } }]); return OrbitClientFactory; @@ -252,8 +295,8 @@ var OrbitDB = __webpack_require__(3); __webpack_require__(4); - __webpack_require__(28); - __webpack_require__(35); + __webpack_require__(48); + __webpack_require__(52); module.exports = __webpack_require__(12).Promise; /***/ }, @@ -334,14 +377,14 @@ var OrbitDB = 'use strict'; var LIBRARY = __webpack_require__(9) , $export = __webpack_require__(10) - , redefine = __webpack_require__(15) - , hide = __webpack_require__(16) - , has = __webpack_require__(21) - , Iterators = __webpack_require__(22) - , $iterCreate = __webpack_require__(23) - , setToStringTag = __webpack_require__(24) - , getProto = __webpack_require__(17).getProto - , ITERATOR = __webpack_require__(25)('iterator') + , redefine = __webpack_require__(25) + , hide = __webpack_require__(15) + , has = __webpack_require__(26) + , Iterators = __webpack_require__(27) + , $iterCreate = __webpack_require__(28) + , setToStringTag = __webpack_require__(44) + , getPrototypeOf = __webpack_require__(46) + , ITERATOR = __webpack_require__(45)('iterator') , BUGGY = !([].keys && 'next' in [].keys()) // Safari has buggy iterators w/o `next` , FF_ITERATOR = '@@iterator' , KEYS = 'keys' @@ -364,20 +407,24 @@ var OrbitDB = , proto = Base.prototype , $native = proto[ITERATOR] || proto[FF_ITERATOR] || DEFAULT && proto[DEFAULT] , $default = $native || getMethod(DEFAULT) - , methods, key; + , $entries = DEFAULT ? !DEF_VALUES ? $default : getMethod('entries') : undefined + , $anyNative = NAME == 'Array' ? proto.entries || $native : $native + , methods, key, IteratorPrototype; // Fix native - if($native){ - var IteratorPrototype = getProto($default.call(new Base)); - // Set @@toStringTag to native iterators - setToStringTag(IteratorPrototype, TAG, true); - // FF fix - if(!LIBRARY && has(proto, FF_ITERATOR))hide(IteratorPrototype, ITERATOR, returnThis); - // fix Array#{values, @@iterator}.name in V8 / FF - if(DEF_VALUES && $native.name !== VALUES){ - VALUES_BUG = true; - $default = function values(){ return $native.call(this); }; + if($anyNative){ + IteratorPrototype = getPrototypeOf($anyNative.call(new Base)); + if(IteratorPrototype !== Object.prototype){ + // Set @@toStringTag to native iterators + setToStringTag(IteratorPrototype, TAG, true); + // fix for some old engines + if(!LIBRARY && !has(IteratorPrototype, ITERATOR))hide(IteratorPrototype, ITERATOR, returnThis); } } + // fix Array#{values, @@iterator}.name in V8 / FF + if(DEF_VALUES && $native && $native.name !== VALUES){ + VALUES_BUG = true; + $default = function values(){ return $native.call(this); }; + } // Define iterator if((!LIBRARY || FORCED) && (BUGGY || VALUES_BUG || !proto[ITERATOR])){ hide(proto, ITERATOR, $default); @@ -387,9 +434,9 @@ var OrbitDB = Iterators[TAG] = returnThis; if(DEFAULT){ methods = { - values: DEF_VALUES ? $default : getMethod(VALUES), - keys: IS_SET ? $default : getMethod(KEYS), - entries: !DEF_VALUES ? $default : getMethod('entries') + values: DEF_VALUES ? $default : getMethod(VALUES), + keys: IS_SET ? $default : getMethod(KEYS), + entries: $entries }; if(FORCED)for(key in methods){ if(!(key in proto))redefine(proto, key, methods[key]); @@ -411,6 +458,7 @@ var OrbitDB = var global = __webpack_require__(11) , core = __webpack_require__(12) , ctx = __webpack_require__(13) + , hide = __webpack_require__(15) , PROTOTYPE = 'prototype'; var $export = function(type, name, source){ @@ -421,12 +469,13 @@ var OrbitDB = , IS_BIND = type & $export.B , IS_WRAP = type & $export.W , exports = IS_GLOBAL ? core : core[name] || (core[name] = {}) + , expProto = exports[PROTOTYPE] , target = IS_GLOBAL ? global : IS_STATIC ? global[name] : (global[name] || {})[PROTOTYPE] , key, own, out; if(IS_GLOBAL)source = name; for(key in source){ // contains in native - own = !IS_FORCED && target && key in target; + own = !IS_FORCED && target && target[key] !== undefined; if(own && key in exports)continue; // export native or passed out = own ? target[key] : source[key]; @@ -436,23 +485,36 @@ var OrbitDB = : IS_BIND && own ? ctx(out, global) // wrap global constructors for prevent change them in library : IS_WRAP && target[key] == out ? (function(C){ - var F = function(param){ - return this instanceof C ? new C(param) : C(param); + var F = function(a, b, c){ + if(this instanceof C){ + switch(arguments.length){ + case 0: return new C; + case 1: return new C(a); + case 2: return new C(a, b); + } return new C(a, b, c); + } return C.apply(this, arguments); }; F[PROTOTYPE] = C[PROTOTYPE]; return F; // make static versions for prototype methods })(out) : IS_PROTO && typeof out == 'function' ? ctx(Function.call, out) : out; - if(IS_PROTO)(exports[PROTOTYPE] || (exports[PROTOTYPE] = {}))[key] = out; + // export proto methods to core.%CONSTRUCTOR%.methods.%NAME% + if(IS_PROTO){ + (exports.virtual || (exports.virtual = {}))[key] = out; + // export proto methods to core.%CONSTRUCTOR%.prototype.%NAME% + if(type & $export.R && expProto && !expProto[key])hide(expProto, key, out); + } } }; // type bitmap - $export.F = 1; // forced - $export.G = 2; // global - $export.S = 4; // static - $export.P = 8; // proto - $export.B = 16; // bind - $export.W = 32; // wrap + $export.F = 1; // forced + $export.G = 2; // global + $export.S = 4; // static + $export.P = 8; // proto + $export.B = 16; // bind + $export.W = 32; // wrap + $export.U = 64; // safe + $export.R = 128; // real proto method for `library` module.exports = $export; /***/ }, @@ -468,7 +530,7 @@ var OrbitDB = /* 12 */ /***/ function(module, exports) { - var core = module.exports = {version: '1.2.6'}; + var core = module.exports = {version: '2.3.0'}; if(typeof __e == 'number')__e = core; // eslint-disable-line no-undef /***/ }, @@ -509,41 +571,114 @@ var OrbitDB = /* 15 */ /***/ function(module, exports, __webpack_require__) { - module.exports = __webpack_require__(16); - -/***/ }, -/* 16 */ -/***/ function(module, exports, __webpack_require__) { - - var $ = __webpack_require__(17) - , createDesc = __webpack_require__(18); - module.exports = __webpack_require__(19) ? function(object, key, value){ - return $.setDesc(object, key, createDesc(1, value)); + var dP = __webpack_require__(16) + , createDesc = __webpack_require__(24); + module.exports = __webpack_require__(20) ? function(object, key, value){ + return dP.f(object, key, createDesc(1, value)); } : function(object, key, value){ object[key] = value; return object; }; /***/ }, -/* 17 */ -/***/ function(module, exports) { +/* 16 */ +/***/ function(module, exports, __webpack_require__) { - var $Object = Object; - module.exports = { - create: $Object.create, - getProto: $Object.getPrototypeOf, - isEnum: {}.propertyIsEnumerable, - getDesc: $Object.getOwnPropertyDescriptor, - setDesc: $Object.defineProperty, - setDescs: $Object.defineProperties, - getKeys: $Object.keys, - getNames: $Object.getOwnPropertyNames, - getSymbols: $Object.getOwnPropertySymbols, - each: [].forEach + var anObject = __webpack_require__(17) + , IE8_DOM_DEFINE = __webpack_require__(19) + , toPrimitive = __webpack_require__(23) + , dP = Object.defineProperty; + + exports.f = __webpack_require__(20) ? Object.defineProperty : function defineProperty(O, P, Attributes){ + anObject(O); + P = toPrimitive(P, true); + anObject(Attributes); + if(IE8_DOM_DEFINE)try { + return dP(O, P, Attributes); + } catch(e){ /* empty */ } + if('get' in Attributes || 'set' in Attributes)throw TypeError('Accessors not supported!'); + if('value' in Attributes)O[P] = Attributes.value; + return O; + }; + +/***/ }, +/* 17 */ +/***/ function(module, exports, __webpack_require__) { + + var isObject = __webpack_require__(18); + module.exports = function(it){ + if(!isObject(it))throw TypeError(it + ' is not an object!'); + return it; }; /***/ }, /* 18 */ +/***/ function(module, exports) { + + module.exports = function(it){ + return typeof it === 'object' ? it !== null : typeof it === 'function'; + }; + +/***/ }, +/* 19 */ +/***/ function(module, exports, __webpack_require__) { + + module.exports = !__webpack_require__(20) && !__webpack_require__(21)(function(){ + return Object.defineProperty(__webpack_require__(22)('div'), 'a', {get: function(){ return 7; }}).a != 7; + }); + +/***/ }, +/* 20 */ +/***/ function(module, exports, __webpack_require__) { + + // Thank's IE8 for his funny defineProperty + module.exports = !__webpack_require__(21)(function(){ + return Object.defineProperty({}, 'a', {get: function(){ return 7; }}).a != 7; + }); + +/***/ }, +/* 21 */ +/***/ function(module, exports) { + + module.exports = function(exec){ + try { + return !!exec(); + } catch(e){ + return true; + } + }; + +/***/ }, +/* 22 */ +/***/ function(module, exports, __webpack_require__) { + + var isObject = __webpack_require__(18) + , document = __webpack_require__(11).document + // in old IE typeof document.createElement is 'object' + , is = isObject(document) && isObject(document.createElement); + module.exports = function(it){ + return is ? document.createElement(it) : {}; + }; + +/***/ }, +/* 23 */ +/***/ function(module, exports, __webpack_require__) { + + // 7.1.1 ToPrimitive(input [, PreferredType]) + var isObject = __webpack_require__(18); + // instead of the ES6 spec version, we didn't implement @@toPrimitive case + // and the second argument - flag - preferred type is a string + module.exports = function(it, S){ + if(!isObject(it))return it; + var fn, val; + if(S && typeof (fn = it.toString) == 'function' && !isObject(val = fn.call(it)))return val; + if(typeof (fn = it.valueOf) == 'function' && !isObject(val = fn.call(it)))return val; + if(!S && typeof (fn = it.toString) == 'function' && !isObject(val = fn.call(it)))return val; + throw TypeError("Can't convert object to primitive value"); + }; + +/***/ }, +/* 24 */ /***/ function(module, exports) { module.exports = function(bitmap, value){ @@ -556,28 +691,13 @@ var OrbitDB = }; /***/ }, -/* 19 */ +/* 25 */ /***/ function(module, exports, __webpack_require__) { - // Thank's IE8 for his funny defineProperty - module.exports = !__webpack_require__(20)(function(){ - return Object.defineProperty({}, 'a', {get: function(){ return 7; }}).a != 7; - }); + module.exports = __webpack_require__(15); /***/ }, -/* 20 */ -/***/ function(module, exports) { - - module.exports = function(exec){ - try { - return !!exec(); - } catch(e){ - return true; - } - }; - -/***/ }, -/* 21 */ +/* 26 */ /***/ function(module, exports) { var hasOwnProperty = {}.hasOwnProperty; @@ -586,55 +706,218 @@ var OrbitDB = }; /***/ }, -/* 22 */ +/* 27 */ /***/ function(module, exports) { module.exports = {}; /***/ }, -/* 23 */ +/* 28 */ /***/ function(module, exports, __webpack_require__) { 'use strict'; - var $ = __webpack_require__(17) - , descriptor = __webpack_require__(18) - , setToStringTag = __webpack_require__(24) + var create = __webpack_require__(29) + , descriptor = __webpack_require__(24) + , setToStringTag = __webpack_require__(44) , IteratorPrototype = {}; // 25.1.2.1.1 %IteratorPrototype%[@@iterator]() - __webpack_require__(16)(IteratorPrototype, __webpack_require__(25)('iterator'), function(){ return this; }); + __webpack_require__(15)(IteratorPrototype, __webpack_require__(45)('iterator'), function(){ return this; }); module.exports = function(Constructor, NAME, next){ - Constructor.prototype = $.create(IteratorPrototype, {next: descriptor(1, next)}); + Constructor.prototype = create(IteratorPrototype, {next: descriptor(1, next)}); setToStringTag(Constructor, NAME + ' Iterator'); }; /***/ }, -/* 24 */ +/* 29 */ /***/ function(module, exports, __webpack_require__) { - var def = __webpack_require__(17).setDesc - , has = __webpack_require__(21) - , TAG = __webpack_require__(25)('toStringTag'); - - module.exports = function(it, tag, stat){ - if(it && !has(it = stat ? it : it.prototype, TAG))def(it, TAG, {configurable: true, value: tag}); + // 19.1.2.2 / 15.2.3.5 Object.create(O [, Properties]) + var anObject = __webpack_require__(17) + , dPs = __webpack_require__(30) + , enumBugKeys = __webpack_require__(42) + , IE_PROTO = __webpack_require__(39)('IE_PROTO') + , Empty = function(){ /* empty */ } + , PROTOTYPE = 'prototype'; + + // Create object with fake `null` prototype: use iframe Object with cleared prototype + var createDict = function(){ + // Thrash, waste and sodomy: IE GC bug + var iframe = __webpack_require__(22)('iframe') + , i = enumBugKeys.length + , gt = '>' + , iframeDocument; + iframe.style.display = 'none'; + __webpack_require__(43).appendChild(iframe); + iframe.src = 'javascript:'; // eslint-disable-line no-script-url + // createDict = iframe.contentWindow.Object; + // html.removeChild(iframe); + iframeDocument = iframe.contentWindow.document; + iframeDocument.open(); + iframeDocument.write('