Socket
Socket
Sign inDemoInstall

ssb-conn-db

Package Overview
Dependencies
11
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.5 to 0.0.6

3

lib/index.d.ts
import { AddressData, Opts } from './types';
declare type Updater = (prev: AddressData) => AddressData;
declare class ConnDB {

@@ -18,3 +19,3 @@ private readonly _map;

set(address: string, data: AddressData): ConnDB;
update(address: string, data: AddressData): ConnDB;
update(address: string, x: AddressData | Updater): ConnDB;
get(address: string): AddressData;

@@ -21,0 +22,0 @@ has(address: string): boolean;

@@ -193,8 +193,8 @@ "use strict";

};
ConnDB.prototype.update = function (address, data) {
ConnDB.prototype.update = function (address, x) {
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');
if (!x || (typeof x !== 'object' && typeof x !== 'function')) {
throw new Error('update() expects an object or a function');
}

@@ -205,3 +205,4 @@ var existed = this._map.has(address);

var previous = this._map.get(address);
this._map.set(address, __assign({}, previous, data));
var next = typeof x === 'function' ? x(previous) : x;
this._map.set(address, __assign({}, previous, next));
this._notify({ type: 'update', address: address });

@@ -208,0 +209,0 @@ this._scheduleWrite();

{
"name": "ssb-conn-db",
"description": "Module that manages a local registry of connectable peers",
"version": "0.0.5",
"version": "0.0.6",
"homepage": "https://github.com/staltz/ssb-conn-db",

@@ -30,2 +30,2 @@ "main": "lib/index.js",

"license": "MIT"
}
}

@@ -37,2 +37,3 @@ # ssb-conn-db

* `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.update(address, updater)`: update a connectable peer by its `address` (string, must conform to the [multiserver address convention](https://github.com/dominictarr/multiserver-address)) with `updater` (a function where input is the previous data object and output should be the new 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.

@@ -39,0 +40,0 @@ * `connDB.get(address)`: returns the data for an existing peer with the given `address`, or `undefined` if the address was not registered

@@ -15,2 +15,4 @@ import * as fs from 'fs';

type Updater = (prev: AddressData) => AddressData;
class ConnDB {

@@ -157,8 +159,8 @@ private readonly _map: Map<string, AddressData>;

public update(address: string, data: AddressData): ConnDB {
public update(address: string, x: AddressData | Updater): 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');
if (!x || (typeof x !== 'object' && typeof x !== 'function')) {
throw new Error('update() expects an object or a function');
}

@@ -170,3 +172,4 @@

const previous = this._map.get(address);
this._map.set(address, {...previous, ...data});
const next = typeof x === 'function' ? x(previous) : x;
this._map.set(address, {...previous, ...next});
this._notify({type: 'update', address} as ListenEvent);

@@ -173,0 +176,0 @@ this._scheduleWrite();

@@ -129,3 +129,3 @@ const tape = require('tape');

tape('CRUD: update() works', function(t) {
tape('CRUD: update() works with object argument', function(t) {
const dirPath = path.join(__dirname, './example');

@@ -159,2 +159,32 @@ const connJSONPath = path.join(dirPath, './conn.json');

tape('CRUD: update() works with function argument', 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', prev => ({
source: prev.source.toUpperCase(),
}));
t.strictEquals(connDB2, connDB, 'update() returns the instance');
setTimeout(() => {
const connDataAfter = fs.readFileSync(connJSONPath, 'utf8');
console.log(connDataAfter);
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) {

@@ -161,0 +191,0 @@ const dirPath = path.join(__dirname, './example');

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc