This repository was archived by the owner on Oct 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_bus_client.coffee
More file actions
98 lines (76 loc) · 2.83 KB
/
command_bus_client.coffee
File metadata and controls
98 lines (76 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
http = require "http"
multipart = require "multipart"
crypto = require "crypto"
JSON_MEDIA_TYPE_REGEXP = /^application\/(.+\+)?json$/i
class CommandBusClient
constructor: ({@port, @logger}) ->
throw new Error "Missing port" unless @port?
throw new Error "Missing logger" unless @logger?
createNewUid: (callback) ->
logger = @logger
logger.log "CommandBusClient", "creating UID from localhost:#{@port}..."
request = @_makeRequest path: "/uids"
stream = request.stream
request.on "error", (err) ->
callback err
request.on "response", (response) =>
@_processResponse response, (err, data) ->
return callback err if err?
callback null, data.uid
stream.end()
executeCommand: (command, callback) ->
logger = @logger
commandName = command.getName()
payload = command.getPayload()
logger.log "CommandBusClient", "sending command \"#{commandName}\" to localhost:#{@port}..."
request = @_makeRequest path: "/commands"
stream = request.stream
request.on "error", callback
request.on "response", (response) =>
@_processResponse response, callback
stream.write "Content-Disposition": "form-data; name=\"name\"", commandName
for property, data of payload
headers =
"Content-Disposition": "form-data; name=\"payload.#{property}\""
"Content-Type": "application/json"
if not data? or (not Buffer.isBuffer(data) and not data.pipe?)
data = null if data is undefined
stream.write headers, JSON.stringify data
else
headers["Content-Type"] = "application/octet-stream"
stream.write headers, data
stream.end()
_makeRequestStream: ->
boundaryPrefix = "DjumpBoundary-#{crypto.pseudoRandomBytes(16).toString 'base64'}"
stream = multipart.createMultipartStream prefix: boundaryPrefix
stream
_makeRequest: ({path})->
stream = @_makeRequestStream()
options =
port: @port
host: "localhost"
path: path
method: "POST"
headers: "Content-Type": "multipart/mixed; boundary=#{stream.boundary}"
request = http.request options
stream.pipe request
request.stream = stream
request
_processResponse: (response, callback) ->
data = ""
logger = @logger
response.on "data", (chunk) ->
data += chunk
response.on "end", ->
succeeded = response.statusCode >= 200 and response.statusCode < 300
contentType = response.headers["content-type"]
if contentType and contentType.match JSON_MEDIA_TYPE_REGEXP
data = JSON.parse data
if succeeded
callback null, data
else
try data = JSON.parse data
error = data.error
logger.error "CommandBusClient", "command failed remotely: #{error.stack || error.message || error}"
callback error
module.exports = CommandBusClient