forked from little-core-labs/dns-over-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.js
64 lines (54 loc) · 1.44 KB
/
store.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const debug = require('debug')('dns-over-http:store')
const lru = require('lru-cache')
class RecordStore {
constructor(opts) {
this.stores = {}
this.opts = opts
}
store(port, host) {
const key = hash(port, host)
if (false == key in this.stores) {
this.stores[key] = lru(this.opts)
}
return this.stores[key]
}
set(port, host, name, record, ttl) {
const store = this.store(port, host)
const key = hash(name, record.data)
store.set(key, record, ttl)
debug("store: set: port=%s host=%s name=%s ttl=%s",
port, host, name, ttl, record)
return this
}
get(port, host, name) {
const store = this.store(port, host)
return store.values().filter((record) => name == record.name)
}
has(port, host, name, record) {
const store = this.store(port, host)
const key = hash(name, record.name)
return store.has(key)
}
remove(port, host, name, record) {
const store = this.store(port, host)
const key = hash(name, record.name)
return store.del(key)
}
reset() {
for (const k in this.stores) {
if ('function' == typeof this.stores[k].reset) {
this.stores[k].reset()
delete this.stores[k]
}
}
}
}
function hash(...args) {
return Buffer.concat(args.map((x) => Buffer.from(String(x)))).toString('hex')
}
function createRecordStore(opts) {
return new RecordStore(opts)
}
module.exports = Object.assign(createRecordStore, {
RecordStore
})