1 | var utils,
|
2 | slice = [].slice;
|
3 |
|
4 | utils = require("./utils");
|
5 |
|
6 | utils.extend(global, utils);
|
7 |
|
8 | module.exports = function(sock) {
|
9 | if (sock == null) {
|
10 | sock = config.sock;
|
11 | }
|
12 | return {
|
13 | chromix: function() {
|
14 | var arg, callbacks, client, extra_arguments, i, len, path, request;
|
15 | path = arguments[0], request = arguments[1], extra_arguments = 3 <= arguments.length ? slice.call(arguments, 2) : [];
|
16 | extend(request, {
|
17 | path: path
|
18 | });
|
19 | if (request.args == null) {
|
20 | request.args = [];
|
21 | }
|
22 | callbacks = [];
|
23 | for (i = 0, len = extra_arguments.length; i < len; i++) {
|
24 | arg = extra_arguments[i];
|
25 | (typeof arg === "function" ? callbacks : request.args).push(arg);
|
26 | }
|
27 | client = require("net").connect(sock, function() {
|
28 | return client.write(JSON.stringify(request));
|
29 | });
|
30 | return client.on("data", function(data) {
|
31 | var callback, j, len1, response;
|
32 | response = JSON.parse(data.toString("utf8"));
|
33 | if (response.error) {
|
34 | console.error("error: " + response.error);
|
35 | process.exit(1);
|
36 | }
|
37 | for (j = 0, len1 = callbacks.length; j < len1; j++) {
|
38 | callback = callbacks[j];
|
39 | callback.apply(null, response.response);
|
40 | }
|
41 | return client.destroy();
|
42 | });
|
43 | }
|
44 | };
|
45 | };
|