ssb-conn-db
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -15,2 +15,3 @@ import { AddressData, Opts } from './types'; | ||
set(address: string, data: AddressData): ConnDB; | ||
update(address: string, data: AddressData): ConnDB; | ||
get(address: string): AddressData; | ||
@@ -17,0 +18,0 @@ has(address: string): boolean; |
@@ -176,2 +176,18 @@ "use strict"; | ||
}; | ||
ConnDB.prototype.update = function (address, data) { | ||
if (!msAddress.check(address)) { | ||
throw new Error('The given address is not a valid multiserver-address'); | ||
} | ||
if (!data || typeof data !== 'object') { | ||
throw new Error('The given connection data should have been an object'); | ||
} | ||
var existed = this._map.has(address); | ||
if (!existed) | ||
return this; | ||
var previous = this._map.get(address); | ||
this._map.set(address, __assign({}, previous, data)); | ||
this._notify({ type: 'update', address: address }); | ||
this._scheduleWrite(); | ||
return this; | ||
}; | ||
ConnDB.prototype.get = function (address) { | ||
@@ -178,0 +194,0 @@ return this._map.get(address); |
{ | ||
"name": "ssb-conn-db", | ||
"description": "Module that manages a local registry of connectable peers", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"homepage": "https://github.com/staltz/ssb-conn-db", | ||
@@ -6,0 +6,0 @@ "main": "lib/index.js", |
@@ -35,4 +35,5 @@ # ssb-conn-db | ||
- `opts.writeTimeout` (default `2000` milliseconds): interval to wait when batching database writes in the filesystem | ||
* `connDB.set(address, data)`: register or update a connectable peer by its `address` (string, must conform to the [multiserver address convention](https://github.com/dominictarr/multiserver-address)) with `data` (object). If updating the data, it will *merge* the previous properties with the new properties. Returns the `connDB` instance. | ||
* `connDB.replace(address, data)`: register or update a connectable peer by its `address` (string, must conform to the [multiserver address convention](https://github.com/dominictarr/multiserver-address)) with `data` (object). If updating the data, it will *replace* the previous properties with the new properties. Returns the `connDB` instance. | ||
* `connDB.set(address, data)`: insert or update a connectable peer by its `address` (string, must conform to the [multiserver address convention](https://github.com/dominictarr/multiserver-address)) with `data` (object). If updating the data, it will *merge* the previous properties with the new properties. Returns the `connDB` instance. | ||
* `connDB.update(address, data)`: update a connectable peer by its `address` (string, must conform to the [multiserver address convention](https://github.com/dominictarr/multiserver-address)) with `data` (object). If the peer is not in the database, this method performs no operations and silently returns. Returns the `connDB` instance. | ||
* `connDB.replace(address, data)`: insert or update a connectable peer by its `address` (string, must conform to the [multiserver address convention](https://github.com/dominictarr/multiserver-address)) with `data` (object). If updating the data, it will *replace* the previous properties with the new properties. Returns the `connDB` instance. | ||
* `connDB.get(address)`: returns the data for an existing peer with the given `address`, or `undefined` if the address was not registered | ||
@@ -39,0 +40,0 @@ * `connDB.has(address)`: returns `true` if the given `address` is registered in the database, `false` otherwise |
@@ -136,2 +136,20 @@ import * as fs from 'fs'; | ||
public update(address: string, data: AddressData): ConnDB { | ||
if (!msAddress.check(address)) { | ||
throw new Error('The given address is not a valid multiserver-address'); | ||
} | ||
if (!data || typeof data !== 'object') { | ||
throw new Error('The given connection data should have been an object'); | ||
} | ||
const existed = this._map.has(address); | ||
if (!existed) return this; | ||
const previous = this._map.get(address); | ||
this._map.set(address, {...previous, ...data}); | ||
this._notify({type: 'update', address} as ListenEvent); | ||
this._scheduleWrite(); | ||
return this; | ||
} | ||
public get(address: string): AddressData { | ||
@@ -138,0 +156,0 @@ return this._map.get(address); |
@@ -100,2 +100,89 @@ const tape = require('tape'); | ||
tape('CRUD: set() with undefined deletes the property', function(t) { | ||
const dirPath = path.join(__dirname, './example'); | ||
const connJSONPath = path.join(dirPath, './conn.json'); | ||
const connDataBefore = fs.readFileSync(connJSONPath, 'utf8'); | ||
const connDB = new ConnDB({path: dirPath, writeTimeout: 0}); | ||
t.ok(connDB, 'connDB instance was created'); | ||
setTimeout(() => { | ||
const exists = connDB.has('net:staltz.com:8008~noauth'); | ||
t.true(exists, 'address to be updated is in the database'); | ||
const connDB2 = connDB.set('net:staltz.com:8008~noauth', { | ||
source: undefined, | ||
}); | ||
t.strictEquals(connDB2, connDB, 'set() returns the instance'); | ||
setTimeout(() => { | ||
const connDataAfter = fs.readFileSync(connJSONPath, 'utf8'); | ||
t.notEquals(connDataAfter, connDataBefore, 'conn.json changed'); | ||
fs.writeFileSync(connJSONPath, connDataBefore); | ||
t.pass('teardown'); | ||
t.end(); | ||
}, 200); | ||
}, 200); | ||
}); | ||
tape('CRUD: update() works', function(t) { | ||
const dirPath = path.join(__dirname, './example'); | ||
const connJSONPath = path.join(dirPath, './conn.json'); | ||
const connDataBefore = fs.readFileSync(connJSONPath, 'utf8'); | ||
const connDB = new ConnDB({path: dirPath, writeTimeout: 0}); | ||
t.ok(connDB, 'connDB instance was created'); | ||
setTimeout(() => { | ||
const exists = connDB.has('net:staltz.com:8008~noauth'); | ||
t.true(exists, 'address to be updated is in the database'); | ||
const connDB2 = connDB.update('net:staltz.com:8008~noauth', { | ||
failure: 0, | ||
}); | ||
t.strictEquals(connDB2, connDB, 'update() returns the instance'); | ||
setTimeout(() => { | ||
const connDataAfter = fs.readFileSync(connJSONPath, 'utf8'); | ||
t.notEquals(connDataAfter, connDataBefore, 'conn.json changed'); | ||
fs.writeFileSync(connJSONPath, connDataBefore); | ||
t.pass('teardown'); | ||
t.end(); | ||
}, 200); | ||
}, 200); | ||
}); | ||
tape('CRUD: update() is incapable of inserting', function(t) { | ||
const dirPath = path.join(__dirname, './example'); | ||
const connJSONPath = path.join(dirPath, './conn.json'); | ||
const connDataBefore = fs.readFileSync(connJSONPath, 'utf8'); | ||
const connDB = new ConnDB({path: dirPath, writeTimeout: 0}); | ||
t.ok(connDB, 'connDB instance was created'); | ||
setTimeout(() => { | ||
const exists = connDB.has('net:scuttlebutt.nz:8008~noauth'); | ||
t.false(exists, 'address to be updated is not yet in the database'); | ||
const connDB2 = connDB.update('net:scuttlebutt.nz:8008~noauth', { | ||
source: 'stored', | ||
}); | ||
t.strictEquals(connDB2, connDB, 'update() returns the instance'); | ||
setTimeout(() => { | ||
const connDataAfter = fs.readFileSync(connJSONPath, 'utf8'); | ||
t.equals(connDataAfter, connDataBefore, 'conn.json stayed untouched'); | ||
fs.writeFileSync(connJSONPath, connDataBefore); | ||
t.pass('teardown'); | ||
t.end(); | ||
}, 200); | ||
}, 200); | ||
}); | ||
tape('CRUD: delete() works', function(t) { | ||
@@ -102,0 +189,0 @@ const dirPath = path.join(__dirname, './example'); |
40046
972
49