forked from xavi-/idea-depo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
103 lines (79 loc) · 3.38 KB
/
server.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
var sys = require("sys");
var url = require("url");
var fs = require("fs");
var bind = require("./libraries/bind-js");
var DEPO_DIR = "./depos/"
fs.mkdir(DEPO_DIR, 0666);
var srv = require("./libraries/xavlib/simple-router");
var sendChannel = (function() {
function bindAndSend(res, data, context) {
bind.to(data, context, function(data) {
res.writeHead(200, { "Conent-Length": data.length,
"Content-Type": "text/html" });
res.end(data, "utf8");
});
}
return function sendChannel(res, channelId) {
fs.readFile("./index.html", function(err, data) {
if(err) { throw err; };
var context, channel = chn.channels[channelId];
if(!channel) {
context = { text: "", "channel-name": channelId, "initial-info-id": 0 };
chn.create(channelId).onload(function(text) {
context["text"] = text;
bindAndSend(res, data, context);
});
}
else {
context = { text: channel.text, "channel-name": channelId, "initial-info-id": channel.lastInfoId };
bindAndSend(res, data, context);
}
});
};
})();
srv.urls["/operational-transforms.js"] = srv.staticFileHandler("./operational-transforms.js", "application/x-javascript");
srv.urls["/client.js"] = srv.staticFileHandler("./libraries/xavlib/channel/client.js", "application/x-javascript");
srv.urls["/json2.js"] = srv.staticFileHandler("./libraries/json2.js", "application/x-javascript");
srv.urls["/"] = srv.urls["/index.html"] = function(req, res) { sendChannel(res, "index"); };
(function() {
var regChannel = new RegExp("^/([a-zA-Z0-9_-]+)$");
srv.patterns.push({
test: function(req) { return regChannel.test(url.parse(req.url).pathname); },
handler: function(req, res) {
var channelId = regChannel.exec(url.parse(req.url).pathname)[1];
sendChannel(res, channelId);
}
});
})()
// /channel/<session-id>/send?msg=<json> => returns an info-id
// /channel/<session-id>/read?info-id=<int-id> => returns a list of json messages
var chn = require("./libraries/xavlib/channel");
var event = require("./libraries/xavlib/event");
var ot = require("./operational-transforms");
chn.onCreate(function(id, channel) {
channel.text = "";
channel.onload = event.create(channel);
fs.readFile(DEPO_DIR + id, function(err, text) {
sys.puts("Read in file: " + id);
if(err) { text = ""; }
channel.text = text.toString();
channel.onload.trigger(text);
delete channel.onload;
});
var writeInFlight = false;
channel.onReceive(function(msg, sendMoreInfo) { // TODO: handle case were messages come in while reading in file
channel.text = ot.applyOp(channel.text, msg.content);
if(!writeInFlight) {
setTimeout(function() {
fs.writeFile(DEPO_DIR + id, channel.text, function(err) {
if(err) { throw err; }
sys.puts("Wrote: " + id);
writeInFlight = false;
});
}, 1000);
writeInFlight = true;
}
});
});
srv.server.listen(8001);
chn.start(srv);