Created
May 19, 2014 21:37
-
-
Save heapwolf/9569a5d8490142b5193b to your computer and use it in GitHub Desktop.
cheap hook
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
var level = require('level') | |
var net = require('net') | |
var mts = require('monotonic-timestamp') | |
var multilevel = require('multilevel') | |
var db = level('./db', { valueEncoding: 'json' }) | |
var cl = level('./cl', { valueEncoding: 'json' }) | |
var put = db.put | |
db.put = function(key, value, options, cb) { | |
if (typeof options == 'function') { | |
cb = options; | |
options = {}; | |
} | |
var change_value = { | |
type: 'put', | |
key: key, | |
verified: false | |
} | |
var change_key = mts(); | |
cl.put(change_key, change_value, function(err) { | |
if (err) return // emit to server | |
put.call(db, key, value, options, function(err) { | |
if (err) return cb(err) | |
change_value.verified = true; | |
cl.put(change_key, change_value, function(err) { | |
if (err) return // emit to server | |
cb(null) | |
}) | |
}) | |
}) | |
} | |
var server = net.createServer(function (con) { | |
con.pipe(multilevel.server(db)).pipe(con); | |
}).listen(3000); | |
var mc = multilevel.client(); | |
var con = net.connect(3000); | |
con.pipe(mc.createRpcStream()).pipe(con); | |
mc.put('foo', 100, function(err) { | |
console.log(err || 'OK') | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment