Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor with client as re-usable module.
- Loading branch information
1 parent
a72e8b3
commit 2b99a0d
Showing
5 changed files
with
37 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
# Usage: | ||
# see examples in ./client.coffee. | ||
|
||
utils = require "./utils" | ||
utils.extend global, utils | ||
|
||
module.exports = (sock = config.sock) -> | ||
|
||
# This sends a single request to chrome, unpacks the response, and calls any callbacks with the response as | ||
# argument(s). | ||
chromix: | ||
(path, request, extra_arguments...) -> | ||
extend request, {path} | ||
request.args ?= [] | ||
callbacks = [] | ||
|
||
# Extra arguments which are functions are callbacks (usually just one); all other arguments are added to the | ||
# list of arguments. | ||
for arg in extra_arguments | ||
(if typeof(arg) == "function" then callbacks else request.args).push arg | ||
|
||
client = require("net").connect sock, -> | ||
client.write JSON.stringify request | ||
|
||
client.on "data", (data) -> | ||
response = JSON.parse data.toString "utf8" | ||
if response.error | ||
console.error "error: #{response.error}" | ||
process.exit 1 | ||
callback response.response... for callback in callbacks | ||
client.destroy() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters