Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
testdb
localdb
1 change: 1 addition & 0 deletions fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = window.fetch
57 changes: 45 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"devDependencies": {
"coveralls": "^3.0.0",
"documentation": "^6.1.0",
"fs-extra": "^5.0.0",
"level-browserify": "^1.1.2",
"nyc": "^11.6.0",
"standard": "^11.0.1",
Expand All @@ -29,9 +30,13 @@
"dependencies": {
"borc": "git+https://github.com/dignifiedquire/borc.git#fix/nested-array",
"ipld-graph-builder": "^1.3.8",
"node-fetch": "^2.1.2",
"text-encoding": "^0.6.4",
"uint1array": "^1.0.5"
},
"browser": {
"node-fetch": "./fetch.js"
},
"standard": {
"ignore": [
"/benchmark/"
Expand Down
52 changes: 52 additions & 0 deletions remoteDatastore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const Buffer = require('safe-buffer').Buffer
const TreeDAG = require('./datastore.js')
const fetch = require('node-fetch')

module.exports = class RemoteTreeDAG extends TreeDAG {
/**
* @param dag {object} a level db instance
* @param remoteOpts
* @param remoteOpts.uri {string} the HTTP uri which has an interface: GET /:key -> value
* @param remoteOpts.encoding {string} the encoding of the reponse
* @param opts.decoder {object} a cbor decoder
*/
constructor (dag, remoteOpts, decoder) {
super(dag, decoder)
this.remoteOpts = Object.assign({
uri: null,
encoding: 'base64'
}, remoteOpts)
}

async get (link) {
try {
return await super.get(link)
} catch (e) {
if (this.remoteOpts.uri) {
await this.fetchRemote(link)
return super.get(link)
}
}
}

fetchRemote (key) {
if (!Buffer.isBuffer(key)) {
key = Buffer.from(key.buffer)
}

const route = `${this.remoteOpts.uri}/${key.toString('hex')}`
return fetch(route)
.then(res => res.text())
.then(text => {
const encoded = Buffer.from(text, this.remoteOpts.encoding)
return new Promise((resolve, reject) => {
this._dag.put(key, encoded.toString('hex'), () => {
resolve(key)
})
})
})
.catch(err => {
console.warn(`error fetching ${route}:`, err.message)
})
}
}
34 changes: 34 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const fs = require('fs-extra')
const tape = require('tape')
const crypto = require('crypto')
const level = require('level-browserify')
const RadixTree = require('../')
const RemoteDataStore = require('../remoteDatastore')
const remote = require('./remote')
const db = level('./testdb')

tape('root existance', async t => {
Expand Down Expand Up @@ -203,3 +206,34 @@ tape('random', async t => {

t.end()
})

tape('remote', async t => {
// remote
const remoteTree = new RadixTree({
db: db
})
const server = remote.listen(db)

const entries = 100
for (let i = 0; i < entries; i++) {
const key = crypto.createHash('sha256').update(i.toString()).digest().slice(0, 20)
remoteTree.set(key, Buffer.from([i]))
}
const stateRoot = await remoteTree.flush()

// local
fs.removeSync('./localdb')
const localTree = new RadixTree({
dag: new RemoteDataStore(level('./localdb'), {uri: 'http://localhost:3000'})
})
localTree.root = stateRoot

for (let i = 0; i < entries; i++) {
const key = crypto.createHash('sha256').update(i.toString()).digest().slice(0, 20)
const value = await localTree.get(key)
t.equals(value.value[0], i)
}

server.close()
t.end()
})
26 changes: 26 additions & 0 deletions tests/remote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const RadixTree = require('../')

const cbor = require('borc')
const http = require('http')

let tree

const server = http.createServer(async (req, res) => {
const key = Buffer.from(req.url.slice(1), 'hex')
const value = await tree.graph._dag.get(key)
res.end(cbor.encode(value).toString('base64'))
})

module.exports = {
listen: (db, port = 3000) => {
server.listen(port, (err) => {
if (err) { return console.error(err) }

tree = new RadixTree({db})

console.log(`server is listening on ${port}`)
})

return server
}
}