forked from dktr0/extramuros
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.js
280 lines (244 loc) · 8.44 KB
/
client.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// our required dependencies
const {chunksToLinesAsync, chomp} = require('@rauschma/stringio');
var spawn = require('child_process').spawn;
var nopt = require('nopt');
// var zmq = require('zmq');
var WebSocket = require('ws');
var osc = require('osc');
var fs = require('fs');
var path = require('path');
// some global variables
var stdin = process.stdin;
var stderr = process.stderr;
var output = process.stdout;
// parse command-line options
var knownOpts = {
"server" : [String, null],
"osc-port" : [Number, null],
"ws-port" : [Number, null],
"password" : [String, null],
"help": Boolean,
"feedback": Boolean,
"tidal": Boolean,
"tidalVisuals": Boolean,
"tidalCabal": Boolean,
"tidalCustom": [path],
"ghciPath": [String, null],
"sonic-pi": Boolean,
"newlines-as-spaces" : Boolean
};
var shortHands = {
"s" : ["--server"],
"n" : ["--newlines-as-spaces"],
"o" : ["--oscSend-port"],
"w" : ["--ws-port"],
"p" : ["--password"],
"t" : ["--tidal"],
"T" : ["--tidalCustom"],
"g" : ["--ghciPath"],
"h" : ["--help"],
"f" : ["--feedback"]
};
var parsed = nopt(knownOpts,shortHands,process.argv,2);
if(parsed['help']!=null) {
stderr.write("extramuros client.js usage:\n");
stderr.write(" --help (-h) this help message\n");
stderr.write(" --server (-s) [address] address of server's downstream (default:localhost)\n");
stderr.write(" --ws-port [number] port for OSC WebSocket connection to server (default: 8000)\n");
stderr.write(" --oscSend-port [number] UDP port on which to receive OSC messages (default: none)\n");
stderr.write(" --password [word] (-p) password to authenticate messages to server\n");
stderr.write(" --feedback (-f) send feedback from stdin to server\n");
stderr.write(" --tidal (-t) launch Tidal (as installed by stack)\n");
stderr.write(" --tidalCabal launch Tidal (as installed by cabal)\n");
stderr.write(" --ghciPath path of your local ghci bin\n");
stderr.write(" --sonic-pi for Sonic Pi (each evaluation handled by sonic-pi-tool)\n");
stderr.write(" --newlines-as-spaces (-n) converts any received newlines to spaces on stdout\n");
process.exit(1);
}
if(process.argv.length<3) {
stderr.write("extramuros: use --help to display available options\n");
}
var defaultFeedbackFunction = function(x, ws) {
stderr.write(x + "\n");
try {ws.send(JSON.stringify({request: 'feedback', text: x}))}
catch(e) {stderr.write("warning: exception in WebSocket send\n")}
};
// before setting up connections, set some defaults and look for problems
var newlines = parsed['newlines-as-spaces'];
var server = parsed['server'];
if(server == null) { server = "localhost"; }
var port = parsed['ws-port'];
if(port == null) { port = 8000; }
var oscPort = parsed['osc-port'];
var feedback = parsed['feedback'];
var wsAddress = "ws://" + server + ":" + (port.toString());
var password = parsed['password'];
if(oscPort!=null && password == null) {
stderr.write("Error: Password required if an OSC port is specified\n");
process.exit(1);
}
var sonicPi = parsed['sonic-pi'];
var withTidal = parsed['tidal'];
var withTidalCabal = parsed['tidalCabal'];
var withTidalVisuals = parsed['tidalVisuals'];
var withCustomTidalBoot = parsed['tidalCustom'];
const withGhciPath = parsed['ghciPath'];
if(withTidalCabal != null || withTidalVisuals != null || withCustomTidalBoot != null) withTidal = true;
if(withCustomTidalBoot!=null) { // custom tidal boot file provided
if(withTidalVisuals==true) {
stderr.write("Error: Too many arguments provided for Tidal boot options\n");
process.exit(1);
}
else {
try{ fs.accessSync(withCustomTidalBoot, fs.F_OK); }
catch (e) {
stderr.write("Error: Tidal boot file does not exist\n");
process.exit(1);
}
}
}
var child;
var tidal;
var buf = '';
var buf2 = '';
function main (ws) {
if(withTidal != null) {
if(withTidalCabal != null) {
if (withGhciPath != null) {
tidal = spawn(withGhciPath, ['-XOverloadedStrings']);
} else {
tidal = spawn('ghci', ['-XOverloadedStrings']);
}
}
else tidal = spawn('stack',['ghci','--ghci-options','-XOverloadedStrings']);
tidal.on('close', function (code) {
stderr.write('Tidal process exited with code ' + code + "\n");
});
output = tidal.stdin;
feedbackSource = tidal.stderr;
tidal.stderr.addListener("data", function(m) {
buf += m;
if (buf.toString().endsWith("\n")) {
defaultFeedbackFunction(buf.toString(), ws);
buf = '';
}
});
tidal.stdout.addListener("data", function(m) {
buf2 += m;
if (buf2.toString().endsWith("\n")) {
defaultFeedbackFunction(buf2.toString(), ws);
buf2 = '';
}
});
const dotGhci = "BootTidal.hs";
fs.readFile(dotGhci,'utf8', function (err,data) {
if (err) { console.log(err); return; }
tidal.stdin.write(data);
console.log("Tidal/GHCI initialized");
});
child = tidal;
}
}
function sanitizeStringForTidal(x) {
var lines = x.split("\n");
var result = "";
var blockOpen = false;
for(var n in lines) {
var line = lines[n];
var startsWithSpace = false;
if(line[0] == " " || line[0] == "\t") startsWithSpace = true;
if(blockOpen == false) {
blockOpen = true;
result = result + ":{\n" + line + "\n";
}
else if(startsWithSpace == false) {
result = result + ":}\n:{\n" + line + "\n";
}
else if(startsWithSpace == true) {
result = result + line + "\n";
}
}
if(blockOpen == true) {
result = result + ":}\n";
blockOpen = false;
}
return result;
}
if(oscPort !=null) {
stderr.write("extramuros: listening for OSC on UDP port " + oscPort);
udp = new osc.UDPPort( { localAddress: "0.0.0.0",localPort: oscPort});
udp.open();
}
var connectWs = function() {
var ws = new WebSocket(wsAddress);
var udp,oscFunction,feedbackFunction;
stderr.write("extramuros: connecting to " + wsAddress + "...\n");
send = function(o) {
try {ws.send(JSON.stringify(o))}
catch(e) {stderr.write("warning: exception in WebSocket send\n")}
};
main(ws);
oscFunction = function(m) {
send({ 'request': 'oscFromClient', 'password' : password, 'address': m.address, 'args': m.args });
};
feedbackFunction = function(m) {
send({'request': 'feedback','password' : password,'text': m.toString() });
//send({'request': 'feedback','password' : password,'text': "Hallo Welt" });
};
ws.on('open',function() {
stderr.write("extramuros: connected to " + wsAddress + "\n");
if(oscPort !=null) {
if(udp!=null) udp.on("message",oscFunction);
}
if(feedback != null) {
if(child != null) {
child.stderr.addListener("data",feedbackFunction);
child.stdout.addListener("data",feedbackFunction);
}
}
});
ws.on('message',function(m) {
var n = JSON.parse(m);
if(n.type == 'eval') {
var s = n.code;
if(newlines) { // convert newline sequences to whitespace
s = s.replace(/\n|\n\r|\r\n|\r/g," ");
}
else if(withTidal) {
s = sanitizeStringForTidal(s);
}
if(sonicPi) {
sonicPiTool = spawn('sonic-pi-tool', ['eval-stdin']);
sonicPiTool.on('close', function (code) {
stderr.write('sonic-pi-tool process exited with code ' + code + "\n");
});
sonicPiTool.stdin.end(s); // or write and then EOF some other way???
}
else {
output.write(s+"\n");
stderr.write(s+"\n");
}
}
});
ws.on('close',function() {
stderr.write("extramuros: websocket connection " + wsAddress + " closed\n");
if(udp!=null)udp.close();
if(feedback != null) {
if(child != null) {
child.stderr.removeListener("data",feedbackFunction);
child.stdout.removeListener("data",feedbackFunction)
}
}
setTimeout(connectWs,5000);
});
ws.on('error',function() {
// setTimeout(connectWs,5000);
});
};
if(wsAddress != null) connectWs();
stdin.addListener("data", function (t) {
//t = t.replace(/--.*\n/g, "\n").replace(/\t/g, "").replace(/\n\]/g, "]");
//output.write(t+"\n");
});
main();
process.on('SIGINT', function() { /* sub.close(); */ sequencerWs.close(); } );