Unverified Commit 92dd7847 authored by Aral Balkan's avatar Aral Balkan
Browse files

Update to use hyperdb

parent 1f44f318
Pipeline #753 canceled with stages
//
// Hypha basic WebRTC node
// Hypha basic WebRTC node using hyperdb
//
// Hypercore
const { Buffer } = require('buffer')
const ram = require('random-access-memory')
const hypercore = require('hypercore')
const hyperdb = require('hyperdb')
// WebRTC swarm / replication
var signalhub = require('signalhub')
......@@ -38,10 +39,10 @@ const hyphalinkTextField = document.getElementById('hyphalink')
const generatedTextField = document.getElementById('generated')
const connectButton = document.getElementById('connect')
const indeterminateProgressIndicator = document.getElementById('indeterminateProgressIndicator')
const hypercoreContentsTextArea = document.getElementById('hypercoreContents')
const dbContentsTextArea = document.getElementById('dbContents')
const errorsTextArea = document.getElementById('errors')
const signals = ['ready', 'data', 'error', 'append', 'download', 'upload', 'sync', 'close']
const signals = ['ready', 'change', 'error', 'append', 'download', 'upload', 'sync', 'close']
function setSignalVisible(signal, state) {
const offState = document.querySelector(`#${signal}Signal > .off`)
......@@ -76,7 +77,7 @@ function blinkSignal(signal) {
function resetForm() {
hyphalinkTextField.value = ''
resetSignals()
hypercoreContentsTextArea.value = ''
dbContentsTextArea.value = ''
errorsTextArea.value = ''
}
......@@ -119,30 +120,30 @@ document.addEventListener('DOMContentLoaded', () => {
const discoveryKeyInHex = discoveryKeyBuffer.toString('hex')
// Create new in-browser hypercore.
const localCore = hypercore((filename) => ram(filename), hyphalinkBuffer, {
const db = hyperdb((filename) => ram(filename), hyphalinkBuffer, {
createIfMissing: false,
overwrite: false,
valueEncoding: 'json',
onwrite: (index, data, peer, next) => {
console.log(`Feed: [onWrite] index = ${index}, peer = ${peer}, data:`)
console.log(data)
next()
}
valueEncoding: 'json'
})
const localReadStream = localCore.createReadStream({live: true})
localReadStream.on('data', (data) => {
// Watch for changes on the /table directory
const watcher = db.watch('/table', () => {
console.log('Database updated!')
db.get('/table', (error, values) => {
console.log(values)
blinkSignal('data')
console.log('Feed [read stream, on data]' , data)
blinkSignal('change')
console.log('db [change: get]', values)
// New data is available on the feed. Display it on the page.
for (let [key, value] of Object.entries(data)) {
hypercoreContentsTextArea.value += `${key}: ${value}\n`
}
// New data is available on the db. Display it on the page.
const obj = values[0].value
for (let [key, value] of Object.entries(obj)) {
dbContentsTextArea.value += `${key}: ${value}\n`
}
})
})
localCore.on('ready', () => {
db.on('ready', () => {
console.log('Local core ready.')
......@@ -166,7 +167,7 @@ document.addEventListener('DOMContentLoaded', () => {
console.log(`WebSwarm [peer for ${hyphalinkInHex} (discovery key: ${discoveryKeyInHex})] About to replicate.`)
// Create the local replication stream.
const localReplicationStream = localCore.replicate({
const localReplicationStream = db.replicate({
// TODO: why is Jim’s shopping list example setting encrypt to false?
// The encryption of __what__ does this affect?
// (I haven’t even tested this yet with it set to true to limit the variables.)
......
......@@ -4,11 +4,11 @@
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<meta http-equiv='X-UA-Compatible' content='ie=edge'>
<title>Hypha Spike: WebRTC 1</title>
<title>Hypha Spike: WebRTC Hyperdb</title>
<link rel='stylesheet' href='style.css'>
</head>
<body>
<h1>Hypha Spike: WebRTC 1</h1>
<h1>Hypha Spike: WebRTC Hyperdb</h1>
<form id='connectionForm'>
......@@ -34,10 +34,10 @@
</div>
<div>
<label>Hypercore signals:</label>
<label>Hyperdb signals:</label>
<div>
<span id='readySignal' class='signal'><span class='off'></span><span class='on'></span> Ready</span>
<span id='dataSignal' class='signal'><span class='off'></span><span class='on'></span> Data</span>
<span id='changeSignal' class='signal'><span class='off'></span><span class='on'></span> Change</span>
<span id='errorSignal' class='signal'><span class='off'></span><span class='on'></span> Error</span>
<span id='appendSignal' class='signal'><span class='off'></span><span class='on'></span> Append</span>
<span id='downloadSignal' class='signal'><span class='off'></span><span class='on'></span></span>
......@@ -48,8 +48,8 @@
</div>
<div>
<label for='hypercoreContents'>Hypercore contents</label>
<textarea id='hypercoreContents' readonly></textarea>
<label for='dbContents'>Hyperdb contents</label>
<textarea id='dbContents' readonly></textarea>
</div>
<div>
......@@ -60,14 +60,14 @@
<h3>Instructions</h3>
<ul>
<li>Run the latest <a href='https://ar.al/2019/01/14/hypha-spike-dat-1/'>Hypha DAT-1 Spike</a> (with WebRTC support) in a separate browser and copy the hyphalink.</li>
<li>Run the latest <a href='https://ar.al/2019/01/14/hypha-spike-multiwriter-1/'>Hypha Multiwriter-1 Spike</a> (with WebRTC support) in a separate browser and copy the hyphalink.</li>
<li>Run a local instance of <a href='https://github.com/mafintosh/signalhub'>signalhub</a>: <pre>signalhub listen -p 445 --key localhost-key.pem --cert localhost.pem</pre></li>
<li>Run a local instance of <a href='https://github.com/mafintosh/signalhub'>signalhub</a> from the _/server_ directory: <pre>signalhub listen -p 445 --key localhost-key.pem --cert localhost.pem</pre></li>
<li>Paste the hyphalink into the form above and press the <em>Connect</em> button.</li>
</ul>
<p>The hypercore from the DAT-1 Spike should replicate to your local hypercore – browser to browser ­– via WebRTC.</p>
<p>The hyperdb from the Multiwriter-1 Spike should replicate to your local hyperdb – browser to browser ­– via WebRTC.</p>
</form>
......
......@@ -974,6 +974,11 @@
"delayed-stream": "~1.0.0"
}
},
"compare": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/compare/-/compare-2.0.0.tgz",
"integrity": "sha512-FXeLLVm09Uh7Updmmx2NCCRG2nMq+mdY3DR9PqhVeOrie3IFU+occFQoqziFkHlTUDw8mDgmdblIZ+J9tsSAUA=="
},
"component-emitter": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/component-emitter/-/component-emitter-1.2.1.tgz",
......@@ -2545,6 +2550,36 @@
}
}
},
"hyperdb": {
"version": "3.5.0",
"resolved": "https://registry.npmjs.org/hyperdb/-/hyperdb-3.5.0.tgz",
"integrity": "sha512-NfubW5251YCMcU2iTgobcAHS53qRzwAS+jDOANu0B9W72xtlOFKOlICkvqsMmMiF0SUAVy/Yfud8DUuykeoIJQ==",
"requires": {
"array-lru": "^1.1.1",
"bulk-write-stream": "^1.1.3",
"codecs": "^1.2.1",
"compare": "^2.0.0",
"hypercore": "^6.13.0",
"hypercore-protocol": "^6.6.4",
"inherits": "^2.0.3",
"mutexify": "^1.2.0",
"nanoiterator": "^1.1.0",
"protocol-buffers-encodings": "^1.1.0",
"random-access-file": "^2.0.1",
"sodium-universal": "^2.0.0",
"thunky": "^1.0.2",
"unordered-array-remove": "^1.0.2",
"unordered-set": "^2.0.0",
"varint": "^5.0.0"
},
"dependencies": {
"varint": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/varint/-/varint-5.0.0.tgz",
"integrity": "sha1-2Ca4n3SQcy+rwMDtaT7Uddyynr8="
}
}
},
"ieee754": {
"version": "1.1.12",
"resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.12.tgz",
......@@ -3211,6 +3246,11 @@
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
},
"mutexify": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/mutexify/-/mutexify-1.2.0.tgz",
"integrity": "sha512-oprzxd2zhfrJqEuB98qc1dRMMonClBQ57UPDjnbcrah4orEMTq1jq3+AcdFe5ePzdbJXI7zmdhfftIdMnhYFoQ=="
},
"nan": {
"version": "2.12.1",
"resolved": "https://registry.npmjs.org/nan/-/nan-2.12.1.tgz",
......@@ -3222,6 +3262,15 @@
"resolved": "https://registry.npmjs.org/nanoassert/-/nanoassert-1.1.0.tgz",
"integrity": "sha1-TzFS4JVA/eKMdvRLGbvNHVpCR40="
},
"nanoiterator": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/nanoiterator/-/nanoiterator-1.2.0.tgz",
"integrity": "sha512-Ybp8yZQDvjgqjrER+jlvZ2m/qzhK7gakNTKFDs84OLPrzsHcSbsFczJlNkDmyBlYldxFEO3JT3gwBWCN9nj8aQ==",
"requires": {
"inherits": "^2.0.3",
"readable-stream": "^2.3.3"
}
},
"nanomatch": {
"version": "1.2.13",
"resolved": "https://registry.npmjs.org/nanomatch/-/nanomatch-1.2.13.tgz",
......
{
"name": "dat-1",
"version": "1.0.0",
"description": "Hypha Spike: DAT 1",
"description": "Hypha Spike: WebRTC Hyperdb",
"main": "server/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
......@@ -9,7 +9,7 @@
},
"repository": {
"type": "git",
"url": "git@source.ind.ie:hypha/spikes/dat-1.git"
"url": "git@source.ind.ie:hypha/spikes/webrtc-hyperdb.git"
},
"keywords": [
"hypha",
......@@ -24,6 +24,7 @@
"budo": "github:aral/budo",
"buffer": "^5.2.1",
"hypercore": "^6.22.4",
"hyperdb": "^3.5.0",
"pump": "^3.0.0",
"random-access-memory": "^3.1.1",
"signalhub": "^4.9.0",
......
......@@ -22,7 +22,7 @@ const server = budo('client/index.js', {
server.on('connect', (event) => {
// Display connection info.
const horizontalRule = new Array(60).fill('').join('')
console.log('\nHypha spike: WebRTC 1')
console.log('\nHypha spike: WebRTC Hyperdb')
console.log(horizontalRule)
console.log(`Serving: ${event.uri}`)
console.log(`Working directory: ${event.dir}`)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment