2022-12-20 01:12:56 +00:00

67 lines
1.4 KiB
JavaScript

import multihashing from 'multihashing-async'
import mh from 'multihashes'
const defaultHashAlg = 'sha2-256'
// 'use strict'
// const ImmutableDB from './immutabledb-interface')
const createMultihash = (data, hashAlg) => {
return new Promise((resolve, reject) => {
multihashing(data, hashAlg || defaultHashAlg, (err, multihash) => {
if (err) { return reject(err) }
resolve(mh.toB58String(multihash))
})
})
}
// const LRU from 'lru')
// const ImmutableDB from './immutabledb-interface')
// const createMultihash from './create-multihash')
/* Memory store using an LRU cache */
export default class MemStore {
constructor () {
this._store = {}// new LRU(1000)
}
async put (value) {
const data = value// new Buffer(JSON.stringify(value))
const hash = await createMultihash(data)
// console.log(this._store)
// this._store.set(hash, data)
if (!this._store) this._store = {}
// console.log(this._store)
// console.log(hash, data)
this._store[hash] = data
// return hash
return {
toJSON: () => {
return {
data: value,
multihash: hash
}
}
}
}
async get (key) {
// if (data) {
// const value = JSON.parse(data)
// return value
// }
// return data
return {
toJSON: () => {
return {
data: this._store[key],
multihash: key
}
}
}
}
}