-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
141 lines (117 loc) · 3.96 KB
/
index.js
File metadata and controls
141 lines (117 loc) · 3.96 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// var hyperdb = require('hyperdb')
var codecs = require('codecs')
var sodium = require('sodium-universal')
var util = require('util')
var bulk = require('bulk-write-stream')
var stream = require('readable-stream')
var events = require('events')
var Transform = stream.Transform
module.exports = SubHyperDB
function SubHyperDB (db, prefix, opts) {
if (!opts && prefix && typeof prefix === 'object') return SubHyperDB(db, null, prefix)
if (!(this instanceof SubHyperDB)) return new SubHyperDB(db, prefix, opts)
var self = this
opts = opts || {}
if (prefix && prefix.slice('-1') !== '/') prefix = prefix + '/'
if (!prefix) prefix = this._createPrefix()
this.db = db
this.prefix = prefix
this._valueEncoding = codecs(opts.valueEncoding || 'binary')
this.ready = db.ready
db.on('ready', function () {
self.localContent = db.localContent
self.contentFeeds = db.contentFeeds
self.key = db.key
self.emit('ready')
})
}
util.inherits(SubHyperDB, events.EventEmitter)
SubHyperDB.prototype.replicate = function (opts) {
return this.db.replicate(opts)
}
SubHyperDB.prototype.put = function (key, value, cb) {
return this.db.put(this._encodeKey(key), this._encodeValue(value), decodeAndReturn(this, cb))
}
SubHyperDB.prototype.get = function (key, opts, cb) {
if (typeof opts === 'function') return this.get(key, null, opts)
this.db.get(this._encodeKey(key), opts, decodeAndReturn(this, cb))
}
SubHyperDB.prototype.del = function (key, cb) {
this.db.del(this._encodeKey(key), decodeAndReturn(this, cb))
}
SubHyperDB.prototype.batch = function (batch, cb) {
batch.map(this._encodeNode.bind(this))
this.db.batch(batch, decodeAndReturn(this, cb))
}
SubHyperDB.prototype.list = function (prefix, opts, cb) {
this.db.list(this._encodeKey(prefix), opts, decodeAndReturn(this, cb))
}
SubHyperDB.prototype.createReadStream = function (prefix, opts) {
if (!prefix) prefix = ''
var decoder = decodeAndReturn(this)
var transform = new Transform({objectMode: true})
transform._transform = function (nodes, enc, next) {
this.push(decoder(null, nodes))
next()
}
return this.db.createReadStream(this._encodeKey(prefix), opts).pipe(transform)
}
SubHyperDB.prototype.createWriteStream = function (cb) {
var self = this
return bulk.obj(write)
function write (batch, cb) {
var flattened = []
for (var i = 0; i < batch.length; i++) {
var content = batch[i]
if (Array.isArray(content)) {
for (var j = 0; j < content.length; j++) {
flattened.push(content[j])
}
} else {
flattened.push(content)
}
}
self.batch(flattened, cb)
}
}
SubHyperDB.prototype._createPrefix = function () {
var buf = Buffer.alloc(16)
sodium.randombytes_buf(buf)
var prefix = buf.toString('hex')
return '~' + prefix + '/'
}
SubHyperDB.prototype._encodeNode = function (node) {
if (node.key !== null) node.key = this._encodeKey(node.key)
if (node.value !== null) node.value = this._encodeValue(node.value)
return node
}
SubHyperDB.prototype._decodeNode = function (node) {
if (node.key !== null) node.key = this._decodeKey(node.key)
if (node.value !== null) node.value = this._decodeValue(node.value)
return node
}
SubHyperDB.prototype._encodeValue = function (value) {
return this._valueEncoding.encode(value)
}
SubHyperDB.prototype._decodeValue = function (value) {
return this._valueEncoding.decode(value)
}
SubHyperDB.prototype._encodeKey = function (key) {
if (key && key.slice(1) === '/') key = key.substring(1)
return this.prefix + key
}
SubHyperDB.prototype._decodeKey = function (key) {
return key.slice(this.prefix.length)
}
function decodeAndReturn (subdb, cb) {
if (!cb) cb = noop
return function (err, nodes) {
if (err || !nodes) return cb(err, nodes)
if (!Array.isArray(nodes)) return cb(err, subdb._decodeNode(nodes))
nodes = nodes.map(subdb._decodeNode.bind(subdb))
nodes = nodes.length === 1 ? nodes[0] : nodes
cb(err, nodes)
return nodes
}
}
function noop () {}