-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (88 loc) · 2.4 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
const pull = require('pull-stream')
// @ts-ignore
const deferred = require('pull-defer')
const getStats = require('./stats')
const IndexPlugin = require('./plugin')
/**
* @typedef {import('./types/helpers').SSB} SSB
*/
/**
* @typedef {import('./types/helpers').SSBConfig} SSBConfig
*/
module.exports = {
name: 'storageUsed',
version: '1.0.0',
manifest: {
getBytesStored: 'async',
stats: 'async',
stream: 'source',
},
permissions: {
master: {
allow: ['getBytesUsed', 'stream'],
},
},
/**
* @param {Required<SSB>} ssb
* @param {SSBConfig} config
*/
init(ssb, config) {
ssb.db.registerIndex(IndexPlugin)
/**
* Determine how many bytes a feed is using on the log, and (on average)
* how many bytes it is using for indexes, and add them up.
* @param {number} logBytes
* @param {*} info
* @returns {number}
*/
function sumLogAndIndexes(logBytes, info) {
const totalLogBytes = info.logUsedBytes
const proportion = logBytes / totalLogBytes
const indexesBytes = proportion * (info.indexes + info.jitIndexes)
return logBytes + indexesBytes
}
/**
* Get the storage capacity used for a specfic feed id
* @param {string} feedId
* @param {import('./types/helpers').CB<*>} cb
*/
function getBytesStored(feedId, cb) {
/** @type {IndexPlugin} */
const indexPlugin = ssb.db.getIndex('storageUsed')
stats((err, info) => {
if (err) return cb(err)
ssb.db.onDrain('storageUsed', () => {
const logBytes = indexPlugin.bytesStored.get(feedId)
if (logBytes == null) return cb(null, 0)
cb(null, sumLogAndIndexes(logBytes, info))
})
})
}
/**
* @param {import('./types/helpers').CB<*>} cb
*/
function stats(cb) {
getStats(ssb, config.path, cb)
}
function stream() {
/** @type {IndexPlugin} */
const indexPlugin = ssb.db.getIndex('storageUsed')
const source = deferred.source()
stats((err, info) => {
source.resolve(
err
? pull.error(err)
: pull(
indexPlugin.stream(),
pull.map(([feedId, logBytes]) => [
feedId,
sumLogAndIndexes(logBytes, info),
])
)
)
})
return source
}
return { getBytesStored, stats, stream }
},
}