Address PR comment

This commit is contained in:
Belma Gutlic 2020-01-13 18:25:28 +01:00
parent 52195afe18
commit 6ee5c70af9
3 changed files with 2 additions and 27 deletions

View File

@ -55,7 +55,6 @@
"typescript": "^3.6.4" "typescript": "^3.6.4"
}, },
"dependencies": { "dependencies": {
"async-mutex": "^0.1.4",
"bcrypto": "^4.2.3", "bcrypto": "^4.2.3",
"bn.js": "^5.0.0", "bn.js": "^5.0.0",
"buffer": "^5.4.3", "buffer": "^5.4.3",

View File

@ -1,4 +1,3 @@
import {Mutex} from 'async-mutex';
import {PeerId} from "./@types/libp2p"; import {PeerId} from "./@types/libp2p";
import {bytes, bytes32} from "./@types/basic"; import {bytes, bytes32} from "./@types/basic";
@ -6,32 +5,14 @@ import {bytes, bytes32} from "./@types/basic";
* Storage for static keys of previously connected peers. * Storage for static keys of previously connected peers.
*/ */
class Keycache { class Keycache {
private mutex: Mutex;
private storage = new Map<bytes, bytes32>(); private storage = new Map<bytes, bytes32>();
constructor() {
this.mutex = new Mutex();
}
public async store(peerId: PeerId, key: bytes32): Promise<void> { public async store(peerId: PeerId, key: bytes32): Promise<void> {
const release = await this.mutex.acquire(); this.storage.set(peerId.id, key);
try {
this.storage.set(peerId.id, key);
} finally {
release();
}
} }
public async load(peerId: PeerId): Promise<bytes32|null> { public async load(peerId: PeerId): Promise<bytes32|null> {
const release = await this.mutex.acquire(); return this.storage.get(peerId.id) || null;
let key;
try {
key = this.storage.get(peerId.id) || null;
} finally {
release();
}
return key;
} }
public resetStorage(): void { public resetStorage(): void {

View File

@ -1086,11 +1086,6 @@ async-each@^1.0.1:
resolved "https://registry.yarnpkg.com/async-each/-/async-each-1.0.3.tgz#b727dbf87d7651602f06f4d4ac387f47d91b0cbf" resolved "https://registry.yarnpkg.com/async-each/-/async-each-1.0.3.tgz#b727dbf87d7651602f06f4d4ac387f47d91b0cbf"
integrity sha512-z/WhQ5FPySLdvREByI2vZiTWwCnF0moMJ1hK9YQwDTHKh6I7/uSckMetoRGb5UBZPC1z0jlw+n/XCgjeH7y1AQ== integrity sha512-z/WhQ5FPySLdvREByI2vZiTWwCnF0moMJ1hK9YQwDTHKh6I7/uSckMetoRGb5UBZPC1z0jlw+n/XCgjeH7y1AQ==
async-mutex@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/async-mutex/-/async-mutex-0.1.4.tgz#a47d1eebf584f7dcdd760e3642dc2c58613bef5c"
integrity sha512-zVWTmAnxxHaeB2B1te84oecI8zTDJ/8G49aVBblRX6be0oq6pAybNcUSxwfgVOmOjSCvN4aYZAqwtyNI8e1YGw==
atob@^2.1.1: atob@^2.1.1:
version "2.1.2" version "2.1.2"
resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.2.tgz#6d9517eb9e030d2436666651e86bd9f6f13533c9" resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.2.tgz#6d9517eb9e030d2436666651e86bd9f6f13533c9"