forked from JacksonTian/ping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
portal.js
executable file
·113 lines (97 loc) · 3.5 KB
/
portal.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
var url = require("url");
var path = require("path");
var fs = require("fs");
var vm = require("vm");
var PortalView = require("./portalview").PortalView;
var Portal = function () {
};
Portal.extension = ".portal";
Portal.templateSettings = {
evaluate : /<%([\s\S]+?)%>/g,
interpolate : /<%=([\s\S]+?)%>/g
};
Portal.preprocess = function (str, sandbox, settings) {
var c = settings || Portal.templateSettings;
try {
var temp = str.replace(c.interpolate, function (match, code) {
var script = "view.viewData." + code;
return vm.runInNewContext(script, sandbox);
//return sandbox.view.viewData[code] ? sandbox.view.viewData[code] : "The key " + code + " is undefined.";
}).replace(c.evaluate, function (match, code) {
console.log(code);
var result = vm.runInNewContext(code, sandbox);
//console.log(result);
return result;
});
} catch (ex) {
return ex.stack;
}
return temp;
};
Portal.postprocess = function (str, sandbox, settings) {
var c = settings || Portal.templateSettings;
var temp = str.replace(c.interpolate, function (match, code) {
return sandbox[code] ? sandbox[code] : "The key " + code + " is undefined.";
});
return temp;
};
Portal.prototype.route = function (requestUrl) {
var portal = requestUrl.split("/")[1];
var pathname = portal + Portal.extension;
var realPath = path.join("portals", path.normalize(pathname.replace(/\.\./g, "")));
return {
"portal": portal,
"path": realPath
};
};
Portal.prototype.dispatch = function (request, response) {
if (request.url == "/favicon.ico") {
response.writeHead(404);
response.end();
return;
}
var routeInfo = this.route(request.url);
var portalView = new PortalView();
portalView.all("before", "file", function (viewData, file) {
console.log("Portal ready.");
portalView.viewData = viewData;
var sandbox = {
"view": portalView
};
var preprocessed = Portal.preprocess(file, sandbox);
portalView.after("partial_end", portalView.partialSeq.length, function () {
response.writeHead(200);
response.write(Portal.postprocess(preprocessed, portalView.partialViews));
console.log("Postprocess done.");
portalView.fire("postprocess_done");
});
portalView.getPartials();
portalView.getPipes();
console.log("Preprocess done.");
});
portalView.on("postprocess_done", function () {
portalView.processAjax(response);
portalView.processPipes(response);
});
try {
var model = new require("./models/" + routeInfo.portal);
portalView.model = model;
model.before(function (viewData) {
portalView.fire("before", viewData);
});
} catch (ex) {
response.writeHead(500);
response.write(ex.stack);
response.end("\n");
}
fs.readFile(routeInfo.path, function (err, file) {
if (err) {
response.writeHead(404, {'Content-Type': 'text/plain'});
response.write("This request URL " + pathname + " was not found on this server.\n");
response.end();
} else {
portalView.fire("file", file.toString("utf-8"));
}
});
};
exports.Portal = Portal;