holepunch-hop
Version:
data interface to HOP
62 lines (54 loc) • 1.21 kB
JavaScript
import b4a from 'b4a'
class BoxesModule {
constructor(db, crypto) {
this.db = db
this.crypto = crypto
}
/**
* save box
* @method saveBentoBox
*/
saveBentoBox = async function (boxInfo) {
await this.db.put(boxInfo.id, boxInfo.data)
return boxInfo.data
}
/**
* get one box by id
* @method getBentoBox
*/
getBentoBox = async function (key) {
const nodeData = await this.db.get(key)
return nodeData
}
/**
* get all boxes
* @method getBentoBoxHistory
*/
getBentoBoxHistory = async function (lsID, category, key) {
const { gt, lt } = this.crypto.getRange(lsID, category)
const boxHistory = await this.db.createReadStream({
gt,
lt,
keyEncoding: 'binary',
valueEncoding: 'json'
})
let boxData = []
for await (const { key, value } of boxHistory) {
let hexKey = key.toString('hex')
boxData.push({ hexKey, value })
}
return boxData
}
/**
* delete box
* @method deleteBentoBox
*/
deleteBentoBox = async function (box) {
await this.db.del(box.id)
let deleteInfo = {}
deleteInfo.id = box.id
return deleteInfo
}
}
export default BoxesModule