forked from dat-ecosystem-archive/dat-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
139 lines (110 loc) · 3.82 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
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
var raf = require('random-access-file')
var secretStorage = require('dat-secret-storage')
var multi = require('multi-random-access')
var messages = require('append-tree/messages')
var stat = require('hyperdrive/lib/messages').Stat
var path = require('path')
module.exports = function (dir, opts) {
if (!opts) opts = {}
var prefix = opts.prefix || '.dat/'
return {
metadata: function (name, metaOpts) {
if (typeof dir === 'function') return dir(prefix + 'metadata.' + name)
if (name === 'secret_key') return secretStorage(opts.secretDir)(path.join(dir, prefix + 'metadata.ogd'), {key: metaOpts.key, discoveryKey: metaOpts.discoveryKey})
return raf(path.join(dir, prefix + 'metadata.' + name))
},
content: function (name, contentOpts, archive) {
if (!archive) archive = contentOpts
if (name === 'data') return createStorage(archive, dir)
if (typeof dir === 'function') return dir(prefix + 'content.' + name)
return raf(path.join(dir, prefix + 'content.' + name))
}
}
}
function createStorage (archive, dir) {
if (!archive.latest) throw new Error('Currently only "latest" mode is supported.')
var latest = archive.latest
var head = null
var storage = multi({limit: 128}, locate)
// TODO: this should be split into two events, 'appending' and 'append'
archive.on('appending', onappending)
archive.on('append', onappend)
return storage
function onappend (name, opts) {
if (head) head.end = archive.content.byteLength
}
function onappending (name, opts) {
if (head) head.end = archive.content.byteLength
var v = latest ? '' : '.' + archive.metadata.length
head = {
start: archive.content.byteLength,
end: Infinity,
storage: file(name + v)
}
storage.add(head)
}
function locate (offset, cb) {
archive.ready(function (err) {
if (err) return cb(err)
find(archive.metadata, offset, function (err, node, st, index) {
if (err) return cb(err)
if (!node) return cb(new Error('Could not locate data'))
var v = latest ? '' : '.' + index
cb(null, {
start: st.byteOffset,
end: st.byteOffset + st.size,
storage: file(node.name + v)
})
})
})
}
function file (name) {
if (typeof dir === 'function') return dir(name)
return raf(name, {directory: dir, rmdir: true})
}
}
function get (metadata, btm, seq, cb) {
if (seq < btm) return cb(null, -1, null)
// TODO: this can be done a lot faster using the hypercore internal iterators, expose!
var i = seq
while (!metadata.has(i) && i > btm) i--
if (!metadata.has(i)) return cb(null, -1, null)
metadata.get(i, {valueEncoding: messages.Node}, function (err, node) {
if (err) return cb(err)
var st = node.value && stat.decode(node.value)
if (!node.value || (!st.offset && !st.blocks) || (!st.byteOffset && !st.blocks)) {
return get(metadata, btm, i - 1, cb) // TODO: check the index instead for fast lookup
}
cb(null, i, node, st)
})
}
function find (metadata, bytes, cb) {
var top = metadata.length - 1
var btm = 1
var mid = Math.floor((top + btm) / 2)
get(metadata, btm, mid, function loop (err, actual, node, st) {
if (err) return cb(err)
var oldMid = mid
if (!node) {
btm = mid
mid = Math.floor((top + btm) / 2)
} else {
var start = st.byteOffset
var end = st.byteOffset + st.size
if (start <= bytes && bytes < end) return cb(null, node, st, actual)
if (top <= btm) return cb(null, null, null, -1)
if (bytes < start) {
top = mid
mid = Math.floor((top + btm) / 2)
} else {
btm = mid
mid = Math.floor((top + btm) / 2)
}
}
if (mid === oldMid) {
if (btm < top) mid++
else return cb(null, null, null, -1)
}
get(metadata, btm, mid, loop)
})
}