@browser-network/database
Advanced tools
Comparing version
@@ -38,4 +38,4 @@ import { LocalDB, WrappedState } from './LocalDB'; | ||
switchAddress: t.SwitchAddress; | ||
allowList: t.IDString[]; | ||
private _denyList; | ||
private _allowList; | ||
private _onChangeHandlers; | ||
@@ -82,3 +82,5 @@ constructor({ secret, appId, network }: DbProps); | ||
* accept updates from them, which means we will no longer forward their updates as well. Also | ||
* removes their state from our storage. It's up to the developer to keep track of these (probably | ||
* removes their state from our storage. | ||
* | ||
* It's up to the developer to keep track of these (probably | ||
* within the state object that they store in this db), and repopulate this list on startup. | ||
@@ -93,6 +95,22 @@ * Calling deny with an address that's already blocked is a noop and O(1) time so don't worry about | ||
*/ | ||
undeny: (address: t.PublicKey) => void; | ||
/** | ||
* @description Add a user to our allow list. Once a single user is on this list, _only users on the | ||
* allow list will be recorded in the database_. All other users will automatically be ignored. | ||
* | ||
* It's up to the developer to keep track of these (probably | ||
* within the state object that they store in this db), and repopulate this list on startup. | ||
* Calling allow with an address that's already on the list is a noop and O(1) time so don't worry about | ||
* spamming this call. | ||
*/ | ||
allow: (address: t.PublicKey) => void; | ||
/** | ||
* @description Remove a user from the allow list. Calling this will remove the user's state from | ||
* our storage, and that user's state will no longer be forwarded either. | ||
*/ | ||
unallow: (address: t.PublicKey) => void; | ||
private onMessage; | ||
private onStateOffering; | ||
private onStateUpdate; | ||
private isForbidden; | ||
private broadcastStateUpdate; | ||
@@ -99,0 +117,0 @@ private broadcastStateUpdateByStateId; |
@@ -118,4 +118,4 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
var _this = this; | ||
this.allowList = []; | ||
this._denyList = {}; | ||
this._allowList = {}; | ||
this._onChangeHandlers = []; | ||
@@ -142,3 +142,5 @@ /** | ||
* accept updates from them, which means we will no longer forward their updates as well. Also | ||
* removes their state from our storage. It's up to the developer to keep track of these (probably | ||
* removes their state from our storage. | ||
* | ||
* It's up to the developer to keep track of these (probably | ||
* within the state object that they store in this db), and repopulate this list on startup. | ||
@@ -159,5 +161,28 @@ * Calling deny with an address that's already blocked is a noop and O(1) time so don't worry about | ||
*/ | ||
this.allow = function (address) { | ||
this.undeny = function (address) { | ||
delete _this._denyList[address]; | ||
}; | ||
/** | ||
* @description Add a user to our allow list. Once a single user is on this list, _only users on the | ||
* allow list will be recorded in the database_. All other users will automatically be ignored. | ||
* | ||
* It's up to the developer to keep track of these (probably | ||
* within the state object that they store in this db), and repopulate this list on startup. | ||
* Calling allow with an address that's already on the list is a noop and O(1) time so don't worry about | ||
* spamming this call. | ||
*/ | ||
this.allow = function (address) { | ||
if (_this._allowList[address]) { | ||
return; | ||
} | ||
_this._allowList[address] = true; | ||
}; | ||
/** | ||
* @description Remove a user from the allow list. Calling this will remove the user's state from | ||
* our storage, and that user's state will no longer be forwarded either. | ||
*/ | ||
this.unallow = function (address) { | ||
delete _this._allowList[address]; | ||
_this.localDB.remove(address); | ||
}; | ||
this.onMessage = function (message) { | ||
@@ -279,2 +304,5 @@ switch (message.type) { | ||
var localState = this.localDB.get(remoteId); | ||
if (this.isForbidden(remoteId)) { | ||
continue; | ||
} | ||
if (!localState) { | ||
@@ -292,10 +320,8 @@ // we don't even have this state yet | ||
return __awaiter(this, void 0, void 0, function () { | ||
var isForbidden; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
isForbidden = this._denyList[update.publicKey] || | ||
(this.allowList.length > 0 && !this.allowList.includes(update.publicKey)); | ||
if (isForbidden) | ||
if (this.isForbidden(update.publicKey)) { | ||
return [2 /*return*/, debug(5, 'state update from pubKey not allowed:', update.publicKey)]; | ||
} | ||
debug(5, 'received update from another node:', update); | ||
@@ -313,2 +339,10 @@ return [4 /*yield*/, this.verify(update)]; | ||
}; | ||
// We won't accept state if: | ||
// * The sender is on our deny list, or | ||
// * We have an allow list going and the sender is not on it | ||
Db.prototype.isForbidden = function (address) { | ||
var isForbidden = this._denyList[address] || | ||
(Object.keys(this._allowList).length > 0 && !this._allowList[address]); | ||
return isForbidden; | ||
}; | ||
// Broadcast an update for a specific state id | ||
@@ -315,0 +349,0 @@ Db.prototype.broadcastStateUpdate = function (data) { |
{ | ||
"name": "@browser-network/database", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "A type of distributed database built on top of the distributed browser-network", | ||
@@ -5,0 +5,0 @@ "main": "./dist/src/index.js", |
@@ -165,2 +165,18 @@ # Browser-Network Database | ||
db.clear() | ||
// Block an address. This will prevent the db from storing or transfering state from that address. | ||
// It'll also remove what state of that address is currently saved. | ||
db.deny(address) | ||
// Unblock an address after having blocked it. Once this is called, the state for that person, if it's | ||
// still present on the network, will come streaming back in. | ||
db.undeny(address) | ||
// Add the address to the allow list. Once a single user is on here, no other users will be accepted | ||
// into the database. | ||
db.allow(address) | ||
// Remove a user from the allow list. If there are any addresses left in the allow list, this user's state | ||
// will no longer be accepted into the database. Either way, this state will be removed from the db. | ||
db.unallow(address) | ||
``` | ||
@@ -170,6 +186,2 @@ | ||
Db also has allow/deny lists. The lists are just an exposed array of | ||
`clientId`s, so add to it like `db.allowList.push('<clientId>')`. Or swap in | ||
`denyList`. | ||
* If a user is on the `denyList`, we will never accept any messages from them. | ||
@@ -202,3 +214,3 @@ * If there're any ids in the `allowList`, and a user is _not_ in there, then | ||
If need be, later some logic can be put in to remove al the state that's set to | ||
If need be, later some logic can be put in to remove all the state that's set to | ||
null or undefined or {} or [] or ''. | ||
@@ -205,0 +217,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
1292023
0.37%24250
0.36%228
5.56%