Last active
August 29, 2015 14:00
-
-
Save networkimprov/11132726 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 lib = require('./buglib.js'); | |
lib.on('connect', function() { | |
console.log('library ready'); | |
throw new Error('test throw'); | |
console.log('done'); | |
}); | |
lib.init(null); |
This file contains hidden or 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 lib2 = require('./buglib2.js'); | |
var sEvents = {}; | |
module.exports.on = function(event, callback) { | |
if (typeof event !== 'string' || typeof callback !== 'function') | |
throw new Error('arguments are: String event, Function callback'); | |
sEvents[event] = callback; | |
}; | |
module.exports.init = function(params) { | |
lib2.init(params, function(op, data) { | |
if (sEvents[op]) | |
return sEvents[op](data); | |
if (op === 'error') | |
throw data; | |
}); | |
}; |
This file contains hidden or 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 cp = require('child_process'); | |
var fs = require('fs'); | |
var net = require('net'); | |
var sSocketPath = '/tmp/bug.sock'; | |
module.exports.init = function(params, callback) { | |
try { | |
fs.unlinkSync(sSocketPath); | |
} catch (err) { | |
if (err.code !== 'ENOENT') throw err; | |
} | |
var aSrvr = net.createServer(handleConnect); | |
aSrvr.listen(sSocketPath, fLoad); | |
function fLoad() { | |
var aC = cp.spawn('bash', ['-c', 'echo stuff | nc -U '+sSocketPath], {stdio:'inherit'}); | |
aC.on('exit', function(code, signal) { | |
//aSrvr.close(); | |
}); | |
} | |
function handleConnect(socket) { | |
var aTimer = setTimeout(function() { | |
console.log('library connected but idle');//. disconnect | |
aTimer = null; | |
}, 2000); | |
socket.on('data', function(data) { | |
fMsg(JSON.parse('{"lib":"'+data.lib+'"}')); | |
}); | |
function fMsg(jso) { | |
return callback('connect', jso); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
and the exception gets caught!