Skip to content
This repository has been archived by the owner on Aug 9, 2021. It is now read-only.

Commit

Permalink
Merge pull request #575 from 3box/hotfix/v1.10.9
Browse files Browse the repository at this point in the history
fix: multi tab ipfs instances for pubsub
  • Loading branch information
zachferland authored Sep 10, 2019
2 parents b83247c + e81235e commit 2cf854a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
5 changes: 4 additions & 1 deletion RELEASE-NOTES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Release Notes

## v1.10.7 - 2019-08-22
## v1.10.9 - 2019-09-10
* fix: allow multiple tab connections (support)

## v1.10.8 - 2019-08-22
* fix: ensureConnected consume db adddress, reconnect

## v1.10.7 - 2019-08-17
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "3box",
"version": "1.10.8",
"version": "1.10.9",
"description": "Interact with user data",
"main": "lib/3box.js",
"directories": {
Expand Down Expand Up @@ -52,7 +52,7 @@
"events": "^3.0.0",
"graphql-request": "^1.8.2",
"https-did-resolver": "^0.1.0",
"ipfs": "^0.36.3",
"ipfs": "^0.36.4",
"ipfs-did-document": "^1.2.3",
"ipfs-mini": "^1.1.5",
"ipfs-postmsg-proxy": "^3.1.1",
Expand Down
37 changes: 35 additions & 2 deletions src/3box.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const utils = require('./utils/index')
const idUtils = require('./utils/id')
const config = require('./config.js')
const API = require('./api')
const IPFSRepo = require('ipfs-repo')
const LevelStore = require('datastore-level')

const ACCOUNT_TYPES = {
ethereum: 'ethereum',
Expand Down Expand Up @@ -723,6 +725,26 @@ class Box {
}
}

function initIPFSRepo () {
let repoOpts = {}
let ipfsRootPath

// if in browser, create unique root storage, and ipfs id on each instance
if (window && window.indexedDB) {
const sessionID = utils.randInt(10000)
ipfsRootPath = 'ipfs/root/' + sessionID
const levelInstance = new LevelStore(ipfsRootPath)
repoOpts = { storageBackends: { root: () => levelInstance } }
}

const repo = new IPFSRepo('ipfs', repoOpts)

return {
repo,
rootPath: ipfsRootPath
}
}

async function initIPFS (ipfs, iframeStore, ipfsOptions) {
// if (!ipfs && !ipfsProxy) throw new Error('No IPFS object configured and no default available for environment')
if (!!ipfs && iframeStore) console.log('Warning: iframeStore true, orbit db cache in iframe, but the given ipfs object is being used, and may not be running in same iframe.')
Expand All @@ -731,13 +753,24 @@ async function initIPFS (ipfs, iframeStore, ipfsOptions) {
} else {
// await iframeLoadedPromise
// return ipfsProxy
let ipfsRepo
if (!ipfsOptions) {
ipfsRepo = initIPFSRepo()
ipfsOptions = Object.assign(IPFS_OPTIONS, { repo: ipfsRepo.repo })
}
return new Promise((resolve, reject) => {
ipfs = new IPFS(ipfsOptions || IPFS_OPTIONS)
ipfs = new IPFS(ipfsOptions)
ipfs.on('error', error => {
console.error(error)
reject(error)
})
ipfs.on('ready', () => resolve(ipfs))
ipfs.on('ready', () => {
resolve(ipfs)
if (ipfsRepo && window && window.indexedDB) {
// deletes once db is closed again
window.indexedDB.deleteDatabase(ipfsRepo.rootPath)
}
})
})
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,8 @@ module.exports = {
const digest = Buffer.from(sha256.digest(str))
return Multihash.encode(digest, 'sha2-256').toString('hex')
},

randInt: max => Math.floor(Math.random() * max),

sha256
}

0 comments on commit 2cf854a

Please sign in to comment.