use options.id

This commit is contained in:
shamb0t 2019-11-14 15:28:25 +00:00
parent cb7b0809a3
commit 73f878d1e5
3 changed files with 26 additions and 35 deletions

32
package-lock.json generated
View File

@ -6359,15 +6359,6 @@
"integrity": "sha1-kV4tbQI8Q9UiStn20qPEFW9XEvU=",
"dev": true
},
"for-each": {
"version": "0.3.3",
"resolved": "https://registry.npmjs.org/for-each/-/for-each-0.3.3.tgz",
"integrity": "sha512-jqYfLp7mo9vIyQf8ykW2v7A+2N4QjeCeI5+Dz9XraiO1ign81wjiH7Fb9vSOWvQfNtmSa4H2RoQTrrXivdUZmw==",
"dev": true,
"requires": {
"is-callable": "^1.1.3"
}
},
"for-in": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz",
@ -14437,14 +14428,10 @@
}
},
"parse-headers": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/parse-headers/-/parse-headers-2.0.2.tgz",
"integrity": "sha512-/LypJhzFmyBIDYP9aDVgeyEb5sQfbfY5mnDq4hVhlQ69js87wXfmEI5V3xI6vvXasqebp0oCytYFLxsBVfCzSg==",
"dev": true,
"requires": {
"for-each": "^0.3.3",
"string.prototype.trim": "^1.1.2"
}
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/parse-headers/-/parse-headers-2.0.3.tgz",
"integrity": "sha512-QhhZ+DCCit2Coi2vmAKbq5RGTRcQUOE2+REgv8vdyu7MnYx2eZztegqtTx99TZ86GTIwqiy3+4nQTWZ2tgmdCA==",
"dev": true
},
"parse-json": {
"version": "4.0.0",
@ -17542,17 +17529,6 @@
"strip-ansi": "^3.0.0"
}
},
"string.prototype.trim": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/string.prototype.trim/-/string.prototype.trim-1.2.0.tgz",
"integrity": "sha512-9EIjYD/WdlvLpn987+ctkLf0FfvBefOCuiEr2henD8X+7jfwPnyvTdmW8OJhj5p+M0/96mBdynLWkxUr+rHlpg==",
"dev": true,
"requires": {
"define-properties": "^1.1.3",
"es-abstract": "^1.13.0",
"function-bind": "^1.1.1"
}
},
"string.prototype.trimleft": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/string.prototype.trimleft/-/string.prototype.trimleft-2.1.0.tgz",

View File

@ -78,7 +78,15 @@ class OrbitDB {
static async createInstance (ipfs, options = {}) {
if (!isDefined(ipfs)) { throw new Error('IPFS is a required argument. See https://github.com/orbitdb/orbit-db/blob/master/API.md#createinstance') }
const { id } = await ipfs.id()
if (options.offline === undefined) {
options.offline = false
}
if (options.offline && !options.id ) {
throw new Error('Offline mode requires passing an `id` in the options')
}
const { id } = options.offline ? ({ id: options.id }) : await ipfs.id()
if (!options.directory) { options.directory = './orbitdb' }
@ -112,10 +120,6 @@ class OrbitDB {
options.cache = new Cache(cacheStorage)
}
if (options.offline === undefined) {
options.offline = false
}
const finalOptions = Object.assign({}, options, { peerId: id })
return new OrbitDB(ipfs, options.identity, finalOptions)
}
@ -441,7 +445,7 @@ class OrbitDB {
// Make sure the type from the manifest matches the type that was given as an option
if (manifest.name !== dbAddress.path) { throw new Error(`Manifest '${manifest.name}' cannot be opened as '${dbAddress.path}'`) }
if (options.type && manifest.type !== options.type) { throw new Error(`Database '${dbAddress}' is type '${manifest.type}' but was opened as '${options.type}'`) }
// Save the database locally
await this._addManifestToCache(options.cache, dbAddress)

View File

@ -58,7 +58,7 @@ Object.keys(testAPIs).forEach(API => {
})
it('starts in offline mode', async () => {
orbitdb = await OrbitDB.createInstance(ipfs1, { offline: true, directory: dbPath1 })
orbitdb = await OrbitDB.createInstance(ipfs1, { id: 'A', offline: true, directory: dbPath1 })
assert.equal(orbitdb._pubsub, null)
await orbitdb.stop()
})
@ -74,5 +74,16 @@ Object.keys(testAPIs).forEach(API => {
assert.notEqual(orbitdb._pubsub, null)
await orbitdb.stop()
})
it('throws error if no `id` passed in offline mode', async () => {
let err
try {
orbitdb = await OrbitDB.createInstance(ipfs1, { offline: true, directory: dbPath1 })
} catch (e) {
err = e.message
}
assert.equal(err, 'Offline mode requires passing an `id` in the options')
await orbitdb.stop()
})
})
})