-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (63 loc) · 1.84 KB
/
index.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
65
66
67
68
69
70
(function(global, factory){
const e = "object" === typeof(exports)
const m = "undefined" !== typeof(module)
const c = e && m
if(c) module.exports = factory()
else global.kvif = factory()
})(this, (function(){
const kvif = {}
kvif.check_hash_default = function(db, cb){ cb(null) }
kvif.get = function(db, key, cb){
const {
get,
check_hash,
} = db
const ch = check_hash || kvif.check_hash_default
ch(db, function(e){
switch(e){
default: return cb(e)
case null: return get(key, cb)
case undefined: return get(key, cb)
}
})
}
kvif.content2hash = function(content=Buffer.allocUnsafe(0), algorithm="sha512", encoding="hex"){
const hash = require("crypto").createHash(algorithm || "sha512")
hash.update(content || Buffer.allocUnsafe(0))
return hash.digest(encoding || "hex")
}
kvif.factory = {
new_db_from_map_like: map_like => {
return {
get: (key, cb) => cb(null, map_like.get(key))
}
},
new_checked_db_from_map_like: (map_like, new_content_name, new_hash_name, content2hash) => {
const d_ncn = name => name + ".content"
const d_nhn = name => name + ".sha512"
const ncn = new_content_name || d_ncn
const nhn = new_hash_name || d_nhn
const c2h = content2hash || kvif.content2hash
return {
set: (key, value, cb) => {
const kc = ncn(key)
const kh = nhn(key)
map_like.set(kc, value)
map_like.set(kh, c2h(value))
cb(null)
},
get: (key, cb) => {
const kc = ncn(key)
const kh = nhn(key)
const c = map_like.get(kc)
const h = map_like.get(kh)
switch(h === c2h(c)){
case false: return cb(new Error("data corruption."), undefined)
case true: return cb(null , c)
}
},
}
},
}
return kvif
}))