-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
83 lines (66 loc) · 2.08 KB
/
index.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
#!/usr/bin/env node
var path = require('path'),
http = require('http'),
streams = require('./shared/streams.js'),
through = require('through'),
st = require('st'),
terrariumStream = require('terrarium-stream').Node,
stringify = require('json-stringify-safe'),
fs = require('fs'),
shoe = require('shoe');
// from https://github.com/joyent/node/blob/master/lib/repl.js
module.filename = path.resolve('rpl');
module.paths = require('module')._nodeModulePaths(module.filename);
function writeHead(res, contentType) {
res.writeHead(200, {
'Content-Type': contentType,
'Cache-Control': 'no-cache'
});
}
function RPL(filename) {
this.filename = filename;
if (this.filename) {
if (!fs.existsSync(this.filename)) {
console.log('Creating new file %s', this.filename);
} else {
this.defaultValue = fs.readFileSync(this.filename, 'utf8');
}
}
this.server = http.createServer(st({
path: __dirname + '/static',
url: '/',
cache: false,
index: 'index.html',
dot: true
}));
}
RPL.prototype.close = function() {
this.server.close.apply(this.server, arguments);
};
RPL.prototype.listen = function() {
this.server.listen.apply(this.server, arguments);
var onstream = function(stream) {
// if you've started this up with a file argument,
// send that file to the browser
if (this.defaultValue) {
stream.write(JSON.stringify({ defaultValue: this.defaultValue }));
}
stream.pipe(streams.fromJSON())
.pipe(terrariumStream())
.pipe(streams.toJSON())
.pipe(stream);
}.bind(this);
// shoe manages our connection to the browser and lets
// us send messages back and forth with streams. under the hood
// it's all websockets on modern browsers.
var sock = shoe(onstream);
sock.install(this.server, '/eval');
this.sock = sock;
};
// horrible errors within the vm can bubble up in unexpected ways.
// we keep that from crashing the process by basically ignoring them
// here.
process.on('uncaughtException', function (err) {
console.log('Caught exception: ' + err);
});
module.exports = RPL;