Created
August 8, 2018 01:07
-
-
Save austinfrey/ffc773f2bbab187767599b6e9a3b1146 to your computer and use it in GitHub Desktop.
hyperdrive dataplex client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const hyperdrive = require('hyperdrive') | |
const ram = require('random-access-memory') | |
const websocket = require('websocket-stream') | |
const dataplex = require('dataplex') | |
const {pipe, through} = require('mississippi') | |
const EventEmitter = require('events') | |
const bus = new EventEmitter() | |
module.exports = socketClient | |
function socketClient(url) { | |
const socket = websocket(url) | |
const plex = dataplex() | |
pipe(socket, plex, socket) | |
return function (key, opts) { | |
if (!opts) opts = {} | |
if (typeof key === 'object') { | |
opts = key | |
key = null | |
} | |
const drive = (!key) | |
? hyperdrive(ram) | |
: hyperdrive(ram, key) | |
drive.ready(() => { | |
const key = drive.key.toString('hex') | |
const stream = plex.open(`/${key}`) | |
console.log('[ CLIENT ]', key) | |
drive.readdir('/', (err, files) => console.log(err || files)) // This does not run | |
pipe(stream, logger(), drive.replicate(opts), stream) | |
}) | |
return { socket, drive, bus } | |
} | |
} | |
function logger() { | |
return through(function (chunk, enc, next) { | |
console.log('DATA', chunk) | |
this.push(chunk) | |
next() | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment