Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
belgrade live coding image server
var wsock = require('websocket-stream');
var ws = wsock('ws://' + location.host);
var split = require('split2');
var through = require('through2');
var h = require('virtual-dom/h');
var state = { images: [] };
var createImage = require('./image.js');
var main = require('main-loop');
var loop = main(state, render, require('virtual-dom'));
document.body.insertBefore(loop.target, document.body.childNodes[0])
function render (state) {
return h('div#activity', state.images.map(function (key) {
return createImage(key);
}));
}
ws.pipe(split()).pipe(through(function (buf, enc, next) {
console.log('key=', buf.toString());
state.images.unshift(buf.toString());
loop.update(state);
next();
}));
var h = require('virtual-dom/h');
module.exports = function (key) {
return h('div.image', [
h('img', {
src: '/images/' + key
})
]);
};
<html>
<body>
<h1>IMAGES</h1>
<div id="content"></div>
<script src="bundle.js"></script>
</body>
</html>
var http = require('http');
var level = require('level');
var db = level('/tmp/whatever.db', {
valueEncoding: 'json'
});
var qs = require('querystring');
var blob = require('content-addressable-blob-store');
var store = blob({ path: '/tmp/blobs' });
var through = require('through2');
var createImage = require('./image.js');
var wsock = require('websocket-stream');
var ecstatic = require('ecstatic')(__dirname + '/public');
var fs = require('fs');
var vstr = require('virtual-dom-stringify');
var hyperstream = require('hyperstream');
var server = http.createServer(function (req, res) {
if (req.method === 'POST') {
req.pipe(store.createWriteStream(function (err, w) {
if (err) return res.end(err);
putImage(w.key);
}));
}
else if (req.url === '/') {
var r = db.createReadStream({
gt: 'activity!',
lt: 'activity!~',
reverse: true
});
fs.createReadStream(__dirname + '/public/index.html')
.pipe(hyperstream({
'#content': r.pipe(through.obj(write))
}))
.pipe(res)
;
function write (row, enc, next) {
var tree = createImage(row.value)
this.push(vstr(tree));
next();
}
}
else if (req.url.split('/')[1] === 'images') {
var key = req.url.split('/')[2];
res.setHeader('content-type', 'image/svg+xml');
var r = store.createReadStream(key);
r.on('error', function (err) { res.end(err + '\n') });
r.pipe(res);
}
else ecstatic(req, res);
function putImage (key) {
var rows = [
{
type: 'put',
key: 'image!' + key,
value: 0
},
{
type: 'put',
key: 'activity!' + new Date().toISOString(),
value: key
}
];
db.batch(rows, function (err) {
if (err) return res.end(err);
updates.emit('image', key);
res.end('ok\n');
});
}
});
server.listen(5000);
var EventEmitter = require('events').EventEmitter;
var updates = new EventEmitter;
wsock.createServer({ server: server }, function (stream) {
updates.on('image', onimage);
function onimage (key) {
stream.write(key + '\n');
}
stream.on('end', function () {
updates.removeListener('image', onimage);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.