Cache NodeId and Location (#35)

* Cache NodeId and Location
* Fix frontend dependency vuln
This commit is contained in:
Maciej Hirsz
2018-08-24 16:26:05 +02:00
committed by GitHub
parent ea98fe515a
commit afef6d7d8f
7 changed files with 2856 additions and 2792 deletions
+7 -5
View File
@@ -7,7 +7,7 @@ export interface Location {
city: Types.City;
}
const cache = new Map<string, Location>();
const cache = new Map<string, Maybe<Location>>();
export async function locate(ip: string): Promise<Maybe<Location>> {
if (ip === '127.0.0.1') {
@@ -18,17 +18,19 @@ export async function locate(ip: string): Promise<Maybe<Location>> {
});
}
const cached = cache.get(ip);
if (cached) {
return Promise.resolve(cached);
if (cache.has(ip)) {
return Promise.resolve(cache.get(ip));
}
const cached = cache.get(ip);
return new Promise<Maybe<Location>>((resolve, _) => {
iplocation(ip, (err, result) => {
if (err) {
console.error(`Couldn't locate ${ip}`);
cache.set(ip, null);
return resolve(null);
}