-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refs #5: add - basic repl for node.js 4.2.3
- Loading branch information
Showing
2 changed files
with
77 additions
and
11 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,76 @@ | ||
var express = require('express'); | ||
var PORT = 2001; | ||
|
||
var app = express(); | ||
app.get('/', function(req, resp) { | ||
resp.send('Hello world\n'); | ||
}); | ||
|
||
app.listen(PORT); | ||
console.log('success!! port: ', PORT); | ||
var main = function() { | ||
var process = require('process'); | ||
var zmq = require('zmq'); | ||
var socket = zmq.socket('rep'); | ||
var port = 'tcp://*:2001'; | ||
|
||
// refs: https://gist.github.com/pguillory/729616 | ||
function hook_stdout(callback) { | ||
var old_stdout_write = process.stdout.write; | ||
process.stdout.write = (function(write) { | ||
return function(string, encoding, fd) { | ||
write.apply(process.stdout, arguments); | ||
callback(string, encoding, fd); | ||
}; | ||
})(process.stdout.write); | ||
return function() { | ||
process.stdout.write = old_stdout_write; | ||
} | ||
} | ||
|
||
function hook_stderr(callback) { | ||
var old_stderr_write = process.stderr.write; | ||
process.stderr.write = (function(write) { | ||
return function(string, encoding, fd) { | ||
write.apply(process.stderr, arguments); | ||
callback(string, encoding, fd); | ||
}; | ||
})(process.stderr.write); | ||
return function() { | ||
process.stderr.write = old_stderr_write; | ||
} | ||
} | ||
|
||
function execute(code) { | ||
eval(code); | ||
} | ||
|
||
process.chdir('/home/work'); | ||
|
||
socket.on('message', function(code_id, code) { | ||
var stdout = ''; | ||
var stderr = ''; | ||
var exceptions = []; // exceptions not supported yet | ||
var unhook_stdout = hook_stdout(function(string, encoding, fd) { | ||
stdout += string; | ||
}); | ||
var unhook_stderr = hook_stderr(function(string, encoding, fd) { | ||
stderr += string; | ||
}); | ||
execute(code.toString()); | ||
var result = { | ||
'stdout': stdout, | ||
'stderr': stderr, | ||
'exceptions': exceptions | ||
}; | ||
socket.send(JSON.stringify(result)); | ||
unhook_stdout(); | ||
unhook_stderr(); | ||
}); | ||
|
||
socket.on('SIGINT', function() { | ||
socket.close(); | ||
}); | ||
|
||
socket.bind(port, function(err) { | ||
if (err) { | ||
throw err; | ||
} else { | ||
console.log('Serving at ' + port + '...'); | ||
} | ||
}); | ||
} | ||
|
||
if (require.main === module) { | ||
main(); | ||
} |