This repository was archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathwash.js
executable file
·306 lines (266 loc) · 8.92 KB
/
wash.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env nodejs
if (!process.env.NODE_PATH) {
console.log('Please set NODE_PATH to an absolute /path/to/axiom/tmp/cjs/lib');
process.exit(-1);
}
global.Promise = require('es6-promise').Promise;
require('source-map-support').install();
var Completer = require('axiom/core/completer').default;
var AxiomError = require('axiom/core/error').default;
var Path = require('axiom/fs/path').default;
var FileSystemManager = require('axiom/fs/base/file_system_manager').default;
var StdioSource = require('axiom/fs/stdio_source').default;
var JsFileSystem = require('axiom/fs/js/file_system').default;
var NodeFileSystem = require('axiom/fs/node/file_system').default;
var NodeWebSocketStreams = require('axiom/fs/stream/node_web_socket_streams').default;
var Transport = require('axiom/fs/stream/transport').default;
var Channel = require('axiom/fs/stream/channel').default;
var SkeletonFileSystem = require('axiom/fs/stream/skeleton_file_system').default;
var TTYState = require('axiom/fs/tty_state').default;
var washExecutables = require('wash/exe_modules').dir;
if ('setRawMode' in process.stdin) {
// Stdin seems to be missing setRawMode under grunt.
process.stdin.setRawMode(true);
}
var WebSocketServer = require('ws').Server;
var WebSocketFs = function(cx, options) {
this.cx_ = cx;
this.options_ = options;
};
WebSocketFs.prototype.println = function(msg) {
this.cx_.stdout.write(msg + '\n');
};
WebSocketFs.prototype.run = function() {
return this.createServer_().then(function(wss) {
return new Promise(function(resolve, reject) {
this.cx_.onClose.listenOnce(function(reason, value) {
console.log('Shutting down WebSocket server');
console.log(' reason:', reason);
console.log(' value:', value);
wss.close();
resolve();
}.bind(this));
wss.on('connection', function (ws) {
try {
this.openFileSystem_(ws);
this.println('WebSocket file system connection accepted');
}
catch(error) {
this.println('WebSocket file system connection error: '
+ error.message);
console.log(error);
ws.close();
}
}.bind(this));
wss.on('error', function (error) {
this.println('WebSocket server error', error);
}.bind(this));
var msg = 'WebSocket server for file system "' +
this.options_.fileSystem + '" running on port ' + this.options_.port;
if (this.options_.ssl) {
msg += ' using SSL connections';
}
this.println(msg);
this.println('Waiting for connections, press Ctrl-C to terminate.');
}.bind(this));
}.bind(this));
};
WebSocketFs.prototype.createServer_ = function() {
return Promise.resolve().then(function() {
var cfg = this.options_;
if (cfg.ssl) {
var keyPath = Path.abs(this.cx_.getPwd(), cfg.key || 'key.pem');
var certPath = Path.abs(this.cx_.getPwd(), cfg.cert || 'cert.pem');
var p1 = this.cx_.fileSystemManager.readFile(keyPath);
var p2 = this.cx_.fileSystemManager.readFile(certPath);
return Promise.all([p1, p2]).then(function(values) {
return {
key: values[0].data,
cert: values[1].data
};
}.bind(this));
}
return null;
}.bind(this)).then(function(result) {
var httpServ = result ? require('https') : require('http');
// dummy request processing
var processRequest = function( req, res ) {
res.writeHead(200);
res.end("All glory to WebSockets!\n");
};
// Create the http(s) server.
var server = null;
if (result) {
server = httpServ.createServer({
key: result.key,
cert: result.cert
}, processRequest).listen(this.options_.port);
} else {
server = httpServ.createServer(processRequest).listen(this.options_.port);
}
// Passing the reference to web server so WS knows port/SSL capabilities.
var wss = new WebSocketServer({ server: server });
return Promise.resolve(wss);
}.bind(this));
};
WebSocketFs.prototype.openFileSystem_ = function(webSocket) {
var fileSystems = this.cx_.fileSystemManager.getFileSystems().filter(
function(fs) {
return fs.name === this.options_.fileSystem;
}.bind(this));
if (fileSystems.length !== 1)
throw new AxiomError.NotFound('file system', this.options_.fileSystem);
var fileSystem = fileSystems[0];
var streams = new NodeWebSocketStreams(webSocket);
var transport = new Transport(
'NodeWebSocketTransport',
streams.readableStream,
streams.writableStream);
var channel = new Channel('socketfs', 'socketfs', transport);
var skeleton = new SkeletonFileSystem('nodefs', fileSystem, channel);
skeleton.onClose.addListener(function(reason, value) {
this.println('WebSocket file system closed');
this.println(' reason: ' + reason);
this.println(' value: ' + value);
}.bind(this));
streams.resume();
};
/*
* A custom executable to expose the local node fs over stream transport.
*/
var socketfs = function(cx) {
cx.ready();
var port = cx.getArg('port');
var fileSystem = cx.getArg('filesystem');
var ssl = cx.getArg('ssl', false);
var key = cx.getArg('key');
var cert = cx.getArg('cert');
if (!port || !fileSystem || cx.getArg('help')) {
cx.stdout.write([
'usage: socketfs <options>',
'Run a WebSocket server to expose a local file system as a stream.',
'',
'Options:',
'',
' -h, --help',
' Print this help message and exit.',
' -p, --port <port>',
' The WebSocket port to listen to.',
' -f, --filesystem <file-system-name>',
' The name of the filesystem to expose on WebSocket connections.',
' -s --ssl',
' Enable wss.',
' -c --cert <path>',
' Path to ssl cert file (default: cert.pem).',
' -k --key <path>',
' Path to ssl key file (default: key.pem).',
'',
].join('\r\n') + '\r\n');
cx.closeOk();
return;
}
var options = {
port: port,
fileSystem: fileSystem,
ssl: ssl,
key: key,
cert: cert
};
var server = new WebSocketFs(cx, options);
server.run().then(
function() {
cx.closeOk();
}
).catch(
function(error) {
cx.closeError(error);
}
);
};
socketfs.signature = {
'help|h': '?',
'port|p': '*',
'filesystem|f': '$',
'ssl|s': '?',
'cert|c': '$',
'key|k': '$',
'_': '@'
};
function onResize(stdioSource) {
var tty = new TTYState();
tty.isatty = process.stdout.isTTY;
tty.rows = process.stdout.rows;
tty.columns = process.stdout.columns;
stdioSource.signal.write({name: 'tty-change', value: tty});
}
function startWash(fsm) {
// TODO(rpaquay)
var stdioSource = new StdioSource();
var stdio = stdioSource.stdio;
return fsm.createExecuteContext(new Path('jsfs:exe/wash'), stdio, {}).then(
function(cx) {
stdioSource.stdout.onData.addListener(function(value) {
process.stdout.write(value);
});
stdioSource.stderr.onData.addListener(function(value) {
process.stderr.write(value);
});
cx.onReady.addListener(function() {
// Resume all streams (except stdin as we want to buffer input until a
// consumer is ready to process it).
stdioSource.stdout.resume();
stdioSource.stderr.resume();
stdioSource.stdio.signal.resume();
}.bind(this));
cx.onClose.addListener(function(reason, value) {
console.log('wash closed: ' + reason + ', ' + value);
});
process.stdin.on('data', function(buffer) {
// Ctrl-C
if (buffer == '\x03') {
stdioSource.signal.write({name: 'interrupt'});
return;
}
stdioSource.stdin.write(buffer.toString());
});
onResize(stdioSource);
process.stdout.on('resize', onResize.bind(null, stdioSource));
var home = new Path('nodefs:').combine(process.env.HOME);
cx.setEnv('$HOME', home.spec);
cx.setEnv('$HISTFILE', home.combine('.wash_history').spec);
if (process.env.PWD) {
cx.setEnv('$PWD', new Path('nodefs:').combine(process.env.PWD).spec);
}
cx.setEnv('@PATH', [new Path('jsfs:exe').spec]);
return cx.execute();
});
}
function main() {
var jsfs = new JsFileSystem();
var fsm = jsfs.fileSystemManager;
return jsfs.rootDirectory.mkdir('exe').then(function(jsdir) {
jsdir.install(washExecutables);
var cmds = {
'socketfs': socketfs
};
jsdir.install(cmds);
mountNodefs(fsm);
return startWash(fsm);
});
}
function mountNodefs(fsm) {
var fs = require('fs');
NodeFileSystem.mount(fsm, 'nodefs', fs);
}
module.exports = { main: main };
if (/wash.js$/.test(process.argv[1])) {
// Keep node from exiting due to lack of events.
var aliveInterval = setInterval(function() {}, 60 * 1000);
main().then(function(value) {
console.log('exit:', value);
process.exit();
}).catch(function(err) {
console.log('Uncaught exception:', err, err.stack);
process.exit();
});
}