This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathoffline-datastore.js
107 lines (85 loc) · 2.81 KB
/
offline-datastore.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
'use strict'
const { Key } = require('interface-datastore')
const { Record } = require('libp2p-record')
const { encodeBase32 } = require('./utils')
const errcode = require('err-code')
const debug = require('debug')
const log = debug('jsipfs:ipns:offline-datastore')
log.error = debug('jsipfs:ipns:offline-datastore:error')
// Offline datastore aims to mimic the same encoding as routing when storing records
// to the local datastore
class OfflineDatastore {
constructor (repo) {
this._repo = repo
}
/**
* Put a value to the local datastore indexed by the received key properly encoded.
* @param {Buffer} key identifier of the value.
* @param {Buffer} value value to be stored.
* @param {function(Error)} callback
* @returns {void}
*/
put (key, value, callback) {
if (!Buffer.isBuffer(key)) {
const errMsg = `Offline datastore key must be a buffer`
log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_INVALID_KEY'))
}
if (!Buffer.isBuffer(value)) {
const errMsg = `Offline datastore value must be a buffer`
log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_INVALID_VALUE'))
}
let routingKey
try {
routingKey = this._routingKey(key)
} catch (err) {
const errMsg = `Not possible to generate the routing key`
log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_GENERATING_ROUTING_KEY'))
}
// Marshal to libp2p record as the DHT does
const record = new Record(key, value)
this._repo.datastore.put(routingKey, record.serialize(), callback)
}
/**
* Get a value from the local datastore indexed by the received key properly encoded.
* @param {Buffer} key identifier of the value to be obtained.
* @param {function(Error, Buffer)} callback
* @returns {void}
*/
get (key, callback) {
if (!Buffer.isBuffer(key)) {
const errMsg = `Offline datastore key must be a buffer`
log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_INVALID_KEY'))
}
let routingKey
try {
routingKey = this._routingKey(key)
} catch (err) {
const errMsg = `Not possible to generate the routing key`
log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_GENERATING_ROUTING_KEY'))
}
this._repo.datastore.get(routingKey, (err, res) => {
if (err) {
return callback(err)
}
// Unmarshal libp2p record as the DHT does
let record
try {
record = Record.deserialize(res)
} catch (err) {
log.error(err)
return callback(err)
}
callback(null, record.value)
})
}
// encode key properly - base32(/ipns/{cid})
_routingKey (key) {
return new Key('/' + encodeBase32(key), false)
}
}
exports = module.exports = OfflineDatastore