index.js
765 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
'use strict';
const TTL_SECS = 30;
const coreapi = require('komodo-sdk/coreapi');
const NodeCache = require( "node-cache" );
const cache = new NodeCache({
stdTTL: TTL_SECS,
});
async function lookup(number) {
if (!number) return;
if (cache.get(number)) {
return cache.get(number);
}
const [err, lookupResult] = await coreapi({
path: '/prefixes/lookup',
method: 'GET',
qs: {
number,
}
});
if (err || !lookupResult || lookupResult.error) {
cache.del(number);
return;
}
if (lookupResult.prefix) {
cache.set(number, lookupResult.prefix);
} else {
cache.del(number);
}
return lookupResult.prefix;
}
exports.lookup = lookup;