Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@keymetrics/interdb

Package Overview
Dependencies
Maintainers
4
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@keymetrics/interdb - npm Package Compare versions

Comparing version 0.0.5 to 0.0.6

constants.js

24

lib/db.js

@@ -23,3 +23,3 @@ 'use strict'

this.db = {
lastUpdate: Date.now(),
lastUpdate: 0,
data: {}

@@ -30,2 +30,6 @@ }

broadcastDB () {
this.interdb.clients.broadcast('interdb:command:updateDb', this.db)
}
// Data

@@ -43,3 +47,3 @@

})
this.interdb.clients.broadcast('command:updateDb', this.db)
this.broadcastDB()
}

@@ -53,3 +57,3 @@

})
this.interdb.clients.broadcast('command:updateDb', this.db)
this.broadcastDB()
}

@@ -67,12 +71,20 @@

push (key, value, cb) {
if (this.db.data[key] && !Array.isArray(this.db.data[key])) return cb(new Error(`${key} is not an array`))
if (this.db.data[key] && !Array.isArray(this.db.data[key])) {
return cb(new Error(`${key} is not an array`))
}
if (this.db.data[key]) this.db.data[key].push(value)
else this.db.data[key] = [value]
if (!this.db.data[key]) this.db.data[key] = []
this.db.lastUpdate = Date.now()
this.db.data[key].push(value)
fs.writeFile(this.path, JSON.stringify(this.db), err => {
if (typeof cb === 'function') return cb(err)
})
this.broadcastDB()
}
getDatabase () {
return this.db
}
// Timestamp

@@ -79,0 +91,0 @@

'use strict'
const Database = require('./db.js')
const Clients = require('./clients.js')
const Synapsis = require('synapsis')
const EventEmitter = require('events').EventEmitter
const cst = require('../constants.js')
class InterDB {
class InterDB extends EventEmitter {
constructor (conf) {
super()
this.conf = conf
this.clients = new Synapsis({
namespace: this.conf.namespace,
password: this.conf.password,
identity: this.conf.identity
})
}
start () {
this.db = new Database(this)
this.clients = new Clients(this)
this.clients.start()
this.clients.on('ready', () => {
// Allow deactivation of db while in WIP
if (this.conf.db === false) return
this.db = new Database(this)
this.clients.once('peer:connected', (identity, socket) => {
this.syncDatabase(socket, () => {
this.emit('ready')
})
})
setTimeout(() => {
this.emit('ready')
}, 2000)
})
/**
* Routes
*/
// When a peer ask last update of db, send the last update to him
this.clients.router(cst.LASTUPDATE_DATABASE, (reply) => {
reply(null, this.db.getLastUpdate())
})
// When a peer ask to sync db, send the local db to him
this.clients.router(cst.SYNC_DATABASE, (reply) => {
reply(null, this.db.getDatabase())
})
// When a peer ask to update with new db, update local db
this.clients.router(cst.UPDATE_DATABASE, (data) => {
this.db.updateAll(data, () => {
this.emit('interdb:db:refreshed')
})
})
}

@@ -22,5 +64,23 @@

reload () {
this.stop()
this.start()
syncDatabase (socket, cb) {
let lastUpdates = []
const peersNumber = this.clients.getPeers().length
this.clients.broadcast(cst.LASTUPDATE_DATABASE, (err, res, identity) => {
if (err) return console.log(err)
lastUpdates.push({ id: identity.id, lastUpdate: res })
if (lastUpdates.length === peersNumber) {
lastUpdates.sort((a, b) => {
return (a.lastUpdate - b.lastUpdate)
})
this.clients.send(lastUpdates[0].id, cst.SYNC_DATABASE, (err, res, identity) => {
if (err) return console.log(err)
this.db.updateAll(res, () => {
cb()
})
})
}
})
}

@@ -27,0 +87,0 @@ }

{
"name": "@keymetrics/interdb",
"version": "0.0.5",
"version": "0.0.6",
"description": "Shared and discoverable database services",
"main": "index.js",
"scripts": {
"test": "./node_modules/.bin/mocha test"
"test": "./node_modules/.bin/mocha test/*.mocha.js --exit --timeout 3000"
},

@@ -29,8 +29,9 @@ "repository": {

"devDependencies": {
"mocha": "^5.0.0",
"mocha": "^4.0.0",
"standard": "^10.0.3"
},
"dependencies": {
"synapsis": "^0.4.0"
"debug": "*",
"synapsis": "^0.6.0"
}
}

@@ -11,12 +11,18 @@ # InterDB

const con = new interdb({
namespace: 'Salon',
password: 'hardcoded-password',
path: './db'
namespace: 'business',
password: 'long-password',
path: './path.db',
identity : {
// Info that will be shared to other dashboards
}
})
// Global
con.start() // connect client
con.stop() // disconnect client
con.reload() // reload discovery
con.start() // join network
con.stop() // exit network
// Bus
con.clients.*
// Refer to Synapsis documentation (https://github.com/Unitech/synapsis)
// DB

@@ -30,12 +36,3 @@ con.db.put('key', 'value', cb) // put new data in existing key or create it

// Bus
con.bus.broadcast('event', {}) // broadcast to all connected clients
con.bus.send('hostname/ip', 'event', {}) // send to specific client
con.bus.on('event' (hostname, data) => {}) // use event listener to handle recv
// Clients
con.clients.getAll() // get all connected clients
con.clients.add({ hostname, publicKey })
con.clients.on('connected', (hostname) => {}) // handle clients connection
con.clients.on('disconnected', (hostname) => {}) // handle clients disconnection
```

@@ -42,0 +39,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc