UNPKG

860 BJavaScriptView Raw
1'use strict';
2
3const net = require('net');
4const concat = require('concat-stream');
5const request = require('then-request');
6const JSON = require('./json-buffer');
7
8const server = net.createServer({allowHalfOpen: true}, c => {
9 function respond(data) {
10 c.end(JSON.stringify(data));
11 }
12
13 c.pipe(concat(function (stdin) {
14 const str = stdin.toString('utf8');
15 if (str === 'ping') {
16 c.end('pong');
17 return;
18 }
19 try {
20 const req = JSON.parse(stdin.toString());
21 request(req.method, req.url, req.options).done(function (response) {
22 respond({success: true, response: response});
23 }, function (err) {
24 respond({success: false, error: { message: err.message }});
25 });
26 } catch (ex) {
27 respond({success: false, error: { message: ex.message }});
28 }
29 }));
30});
31
32server.listen(+process.argv[2]);