Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
Add josh example server/client
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua T Corbin committed Mar 26, 2015
1 parent e5f4a7a commit 8c8eb31
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 0 deletions.
49 changes: 49 additions & 0 deletions node/examples/josh_test_client.js
@@ -0,0 +1,49 @@
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

var argv = require('minimist')(process.argv.slice(2));
var RNGStream = require('./test/lib/rng_stream');

var tchan = require('./index');
var chan = tchan();
var req = chan.request({
host: '127.0.0.1:4040',
timeout: 1000
});
req.on('response', onResponse);
req.arg1.end(argv._[0]);
req.arg2.end(argv._[1]);

if (argv.rand) {
RNGStream({limit: argv.rand}).pipe(req.arg3);
} else {
process.stdin.pipe(req.arg3);
}

function onResponse(res) {
res.arg3.pipe(process.stdout);
res.arg3.on('end', function() {
chan.quit();
});
}

function onError(err) {
console.error(err);
}
111 changes: 111 additions & 0 deletions node/examples/josh_test_server.js
@@ -0,0 +1,111 @@
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

var util = require('util');
var tchan = require('./index');
var endhand = require('./endpoint-handler');
var Logger = require('logtron');

var chan = tchan({
handler: endhand(),
logger: Logger({
meta: {
team: 'wat',
project: 'why'
},
backends: Logger.defaultBackends({
console: true
})
})
});

echo.canStream = true;
chan.handler.register('echo', echo);

exec.canStream = true;
chan.handler.register('exec', exec);

repl.canStream = true;
chan.handler.register('repl', repl);

grepn.canStream = true;
chan.handler.register('grepn', grepn);

chan.listen(4040, '127.0.0.1');

var spawn = require('child_pty').spawn;
// var spawn = require('child_process').spawn;

function exec(req, res) {
req.arg2.onValueReady(function(err, cmd) {
if (err) return res.sendError('ProtocolError');
cmd = String(cmd);
// TODO shlex
var parts = cmd.split(/ +/);
console.log('exec %j', parts);
var kid = spawn(parts.shift(), parts, {
// stdio: ['pipe', 'pipe', 'pipe']
});
req.arg3.pipe(kid.stdin);
kid.stdout.pipe(res.arg3);
// kid.stderr.pipe(res.arg3);
});
}

function grepn(req, res) {
var split2 = require('split2');
var through2 = require('through2');
req.arg2.onValueReady(function(err, needle) {
if (err) return res.sendError('ProtocolError');

var pat = new RegExp(needle);
console.log('GREPN', pat);

var n = 0;
req.arg3
.pipe(split2())
.pipe(through2(function(line, enc, done) {
++n;
if (pat.test(line)) this.push(util.format('%d:%s\n', n, line));
done();
}))
.pipe(res.arg3);
});
}

function repl(req, res) {
var repl = require('repl');
console.log('repl', req.id);
res.setOk(true);
req.arg2.end();
repl.start({
prompt: '> ',
input: req.arg3,
output: res.arg3,
terminal: true
});
}

function echo(req, res) {
res.setOk(true);
console.log('echo', req.id);
req.arg2.pipe(res.arg2);
req.arg3.pipe(res.arg3);
}
4 changes: 4 additions & 0 deletions node/package.json
Expand Up @@ -29,16 +29,20 @@
"devDependencies": {
"ansi-color": "^0.2.1",
"async": "^0.9.0",
"child_pty": "^0.5.1",
"debug-logtron": "^2.0.0",
"istanbul": "^0.3.5",
"jshint": "^2.5.6",
"ldjson-stream": "^1.2.1",
"logtron": "^8.1.0",
"metrics": "^0.1.8",
"minimist": "^1.1.0",
"once": "^1.3.1",
"opn": "^1.0.1",
"pre-commit": "0.0.9",
"split2": "^0.2.1",
"tape": "^3.0.3",
"through2": "^0.6.3",
"time-mock": "^0.1.2",
"uber-licence": "^1.1.0",
"xtend": "^4.0.0"
Expand Down

0 comments on commit 8c8eb31

Please sign in to comment.