-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
61 lines (50 loc) · 1.09 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
const pify = require('pify');
const hyperdb = require('hyperdb')
const hypercore = require('hypercore')
const ram = require('random-access-memory')
let db;
function init(options) {
db = hyperdb([
hypercore(ram, {valueEncoding: 'json'})
])
}
async function get(key, options) {
const nodes = await pify(db.get.bind(db))(key)
if (nodes.length > 1) {
throw new Error('Cannot deal with forks yet 😱')
}
if (nodes.length === 0) {
return { views: [] }
}
return nodes[0].value
}
function put(key, value) {
return pify(db.put.bind(db))(key, value)
}
function getAll(options) {
}
async function has(key) {
const nodes = await pify(db.get.bind(db))(key)
return nodes.length === 0
}
async function keys() {
}
function close() {
return pify(db.close.bind(db))()
}
module.exports = {
options: [
{
name: 'p2p-storage',
description: 'The name of the adapter used as storage by the p2p adapter.',
defaultValue: process.env.P2P_STORAGE || 'memory'
}
],
init: init,
get: get,
put: put,
has: has,
keys: keys,
getAll: getAll,
close: close,
};