forked from RoonLabs/node-roon-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmoomsg.js
96 lines (79 loc) · 2.72 KB
/
moomsg.js
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
"use strict";
function MooMessage(moo, msg, body) {
this.moo = moo;
this.msg = msg;
this.body = body;
}
MooMessage.prototype.send_continue = function() {
var name;
var body;
var content_type;
if (arguments.length == 1) {
name = arguments[0];
} else if (arguments.length == 2) {
name = arguments[0];
body = arguments[1];
} else if (arguments.length >= 3) {
name = arguments[0];
body = arguments[1];
content_type = arguments[2];
}
var origbody = body;
if (typeof(body) == 'undefined') {
// nothing needed here
} else if (!Buffer.isBuffer(body)) {
body = Buffer.from(JSON.stringify(body), 'utf8');
content_type = content_type || "application/json";
} else {
throw new Error("missing content_type");
}
let header = 'MOO/1 CONTINUE ' + name + '\n' +
'Request-Id: ' + this.msg.request_id + '\n';
if (body) {
header += 'Content-Length: ' + body.length + '\n' +
'Content-Type: ' + content_type + '\n';
}
if (this.msg.log) this.moo.logger.log('-> CONTINUE', this.msg.request_id, name, origbody ? JSON.stringify(origbody) : "");
const m = Buffer.from(header + '\n');
if (body)
this.moo.transport.send(Buffer.concat([ m, body ], m.length + body.length));
else
this.moo.transport.send(m);
};
MooMessage.prototype.send_complete = function() {
var name;
var body;
var content_type;
if (arguments.length == 1) {
name = arguments[0];
} else if (arguments.length == 2) {
name = arguments[0];
body = arguments[1];
} else if (arguments.length >= 3) {
name = arguments[0];
body = arguments[1];
content_type = arguments[2];
}
var origbody = body;
if (typeof(body) == 'undefined') {
// nothing needed here
} else if (!Buffer.isBuffer(body)) {
body = Buffer.from(JSON.stringify(body), 'utf8');
content_type = content_type || "application/json";
} else {
throw new Error("missing content_type");
}
let header = 'MOO/1 COMPLETE ' + name + '\n' +
'Request-Id: ' + this.msg.request_id + '\n';
if (body) {
header += 'Content-Length: ' + body.length + '\n' +
'Content-Type: ' + content_type + '\n';
}
if (this.msg.log) this.moo.logger.log('-> COMPLETE', this.msg.request_id, name, origbody ? JSON.stringify(origbody) : "");
const m = Buffer.from(header + '\n');
if (body)
this.moo.transport.send(Buffer.concat([ m, body ], m.length + body.length));
else
this.moo.transport.send(m);
};
exports = module.exports = MooMessage;