Skip to content
This repository has been archived by the owner on Sep 18, 2020. It is now read-only.

Commit

Permalink
Fix duplicated apps DB and data when syncing
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Ray committed Aug 23, 2019
1 parent 534c6fa commit 8030338
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/lib/masq.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import DetectBrowser from 'detect-browser'
import { isUsernameAlreadyTaken, capitalize, dispatchMasqError } from './utils'

const { encrypt, decrypt, importKey, exportKey, genAESKey, genEncryptedMasterKeyAndNonce, decryptMasterKeyAndNonce, genRandomBufferAsStr, updateMasterKeyAndNonce } = common.crypto
const { dbReady, createPromisifiedHyperDB, put, get, list, del, watch } = common.utils
const { dbReady, dbExists, createPromisifiedHyperDB, put, get, list, del, watch } = common.utils
const { MasqError, checkObject } = common.errors

const HUB_URLS = process.env.REACT_APP_SIGNALHUB_URLS.split(',')
Expand Down Expand Up @@ -141,15 +141,18 @@ class Masq {
this._startReplicate(this.profileDB)

const apps = await this.getApps()
apps.forEach(app => {
apps.forEach(async (app) => {
const dbName = `app-${profileId}-${app.id}`

// app is not replicated yet
if (!(await dbExists(dbName))) return

const db = openOrCreateDB(dbName)
this.appsDBs[dbName] = db
db.on('ready', () => {
const discoveryKey = db.discoveryKey.toString('hex')
this.appsDBs[discoveryKey] = db
this._startReplicate(db)
})
await dbReady(db)
const discoveryKey = db.discoveryKey.toString('hex')
this.appsDBs[discoveryKey] = db
this._startReplicate(db)
})

const profile = await this.getAndDecrypt('/profile')
Expand Down Expand Up @@ -358,11 +361,12 @@ class Masq {
removeApp (app) {
checkObject(app, requiredParametersApp)
const dbName = `app-${this.profileId}-${app.id}`
const discoveryKey = this.appsDBs[dbName].discoveryKey.toString('hex')
// missing key
const sw = this.swarms[discoveryKey]
sw.close()
window.indexedDB.deleteDatabase(dbName)
if (this.appsDBs[dbName]) {
const discoveryKey = this.appsDBs[dbName].discoveryKey.toString('hex')
const sw = this.swarms[discoveryKey]
sw.close()
window.indexedDB.deleteDatabase(dbName)
}
return this._deleteResource('apps', app)
}

Expand Down Expand Up @@ -644,7 +648,8 @@ class Masq {
for (let app of otherDevice.apps) {
if (!this.appsDBs[app.discoveryKey]) {
// Add app
await this._createDBAndSyncApp(app.id + '-copy', app.key)
const id = process.env.NODE_ENV === 'test' ? app.id + '-copy' : app.id
await this._createDBAndSyncApp(id, app.key)
continue
}

Expand Down

0 comments on commit 8030338

Please sign in to comment.