UNPKG

1.03 kBtext/coffeescriptView Raw
1
2# Usage:
3# see examples in ./client.coffee.
4
5utils = require "./utils"
6utils.extend global, utils
7
8module.exports = (sock = config.sock) ->
9
10 # This sends a single request to chrome, unpacks the response, and calls any callbacks with the response as
11 # argument(s).
12 chromix:
13 (path, request, extra_arguments...) ->
14 extend request, {path}
15 request.args ?= []
16 callbacks = []
17
18 # Extra arguments which are functions are callbacks (usually just one); all other arguments are added to the
19 # list of arguments.
20 for arg in extra_arguments
21 (if typeof(arg) == "function" then callbacks else request.args).push arg
22
23 client = require("net").connect sock, ->
24 client.write JSON.stringify request
25
26 client.on "data", (data) ->
27 response = JSON.parse data.toString "utf8"
28 if response.error
29 console.error "error: #{response.error}"
30 process.exit 1
31 callback response.response... for callback in callbacks
32 client.destroy()