{"size": 1646, "ext": "js", "max_stars_repo_path": "apptests/src/test/resources/argo/node_modules/argo-server/example/node_modules/webkit-devtools-agent/webkit-devtools-agent.js", "max_stars_repo_name": "t-beckmann/trireme", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "apptests/src/test/resources/argo/node_modules/argo-server/example/node_modules/webkit-devtools-agent/webkit-devtools-agent.js", "max_issues_repo_name": "t-beckmann/trireme", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "apptests/src/test/resources/argo/node_modules/argo-server/example/node_modules/webkit-devtools-agent/webkit-devtools-agent.js", "max_forks_repo_name": "t-beckmann/trireme", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.3230769231, "max_line_length": 72, "alphanum_fraction": 0.5121506683}
var WebSocketServer = require('ws').Server;
var agents = require('./lib');
var wss;

var port = process.env.DEBUG_PORT || 1337;
var host = process.env.DEBUG_HOST || '127.0.0.1';

function start() {
    wss = new WebSocketServer({port: port, host: host});

    console.log('webkit-devtools-agent started on %s:%s', host, port);

    wss.on('connection', function(socket) {
        socket.on('message', function(message) {
            try {
                message = JSON.parse(message);
            } catch(e) {
                console.log(e);
                return;
            }

            var id = message.id;
            var command = message.method.split('.');
            var domain = agents[command[0]];
            var method = command[1];
            var params = message.params;

            if (!domain || !domain[method]) {
                console.error('%s is not implemented', message.method);
                return;
            }

            domain[method](params, function(result) {
                socket.send(JSON.stringify({ id: id, result: result }));
            }, function(event) {
                socket.send(JSON.stringify(event));
            });
        });
    });
}

function stop() {
    if (wss) {
        wss.close();
        console.log('webkit-devtools-agent stopped');
    }
}

if (!module.parent) {
    start();
} else {
    process.on('SIGUSR2', function() {
        if (wss) {
            stop();
        } else {
            start();
        }
    });
}

process.on('uncaughtException', function (err) {
    console.error('webkit-devtools-agent: uncaughtException: ');
    console.error(err.stack);
});
