-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrender.js
More file actions
231 lines (212 loc) · 6.08 KB
/
Copy pathrender.js
File metadata and controls
231 lines (212 loc) · 6.08 KB
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
'use strict';
var fs = require('fs'),
path = require('path'),
mustache = require('mustache'),
yaml = require('js-yaml');
var cache = require('./cache'),
quotes = require('./quotes'),
database = require('./database');
/**
* Render data as JSON text
*/
function formatJSON(data, callback) {
try {
data = JSON.stringify(data, undefined, '\t');
} catch (e) {
return callback(e);
}
callback(null, data);
}
formatJSON.contentType = 'application/json;charset=utf-8';
/**
* Render data as YAML text
*/
function formatYAML(data, callback) {
try {
data = yaml.safeDump(data, {
skipInvalid: true
});
} catch (e) {
return callback(e);
}
callback(null, data);
}
formatYAML.contentType = 'text/yaml;charset=utf-8';
/**
* Render data using mustache.js
*/
function formatHTML(data, template, callback) {
if (typeof (template) === 'function') {
callback = template;
template = 'index';
}
if (!cache.templates[template]) {
return callback('Template `' + template + '` Not Loaded!');
}
try {
callback(null, mustache.render(cache.templates[template], data,
cache.templates));
} catch (e) {
callback(e);
}
}
formatHTML.contentType = 'text/html;charset=utf-8';
exports.formatHTML = formatHTML;
/**
* render the 404 NOT FOUND template
*/
function render404Error(response) {
formatHTML(null, 'error404', function (err, data) {
var mime = 'text/html';
if (err) {
data = '404 Not Found';
mime = 'text/plain';
}
return respond(data, 404, {
'Content-Type': mime
}, response);
});
}
exports.render404Error = render404Error;
/**
* render the 500 INTERNAL SERVER ERROR template
*/
function render500Error(err, response) {
formatHTML(null, 'error500', function (err2, data) {
var mime = 'text/html';
if (err2) {
data = err;
mime = 'text/plain';
}
return respond(data, 500, {
'Content-Type': mime
}, response);
});
}
exports.render500Error = render500Error;
/**
* write out the data to the response using the specified HTTP status code and
* contentType.
*/
function respond(data, code, headers, response) {
headers = headers || {};
if (headers) {
response.writeHead(code, headers);
} else {
response.writeHead(code);
}
response.write(data, /utf-8/.test(headers['Content-Type'] || '') ? 'utf8' : 'binary');
response.end();
}
/**
* Serve files from `/static`
*
* This should not end up getting called from production as the host web server
* should handle this. as such efficiency is not a concern
*/
exports.serveStatic = function serveStatic(uri, _, response) {
var filename = path.join(process.cwd(), uri);
fs.exists(filename, function (exists) {
if (!exists) {
return render404Error(response);
}
//TODO: this really shouldn't be the sync version.
if (fs.statSync(filename).isDirectory()) {
filename += '/index.html';
}
fs.readFile(filename, 'binary', function (err, file) {
if (err) {
return render500Error(err, response);
}
respond(file, 200, undefined, response);
});
});
};
/**
* Render the index with the current data
*/
exports.renderIndex = function renderIndex(uri, request, response) {
var formatter = formatHTML,
accept = request.headers.accept;
// Determine the correct data formatter (HTML/JSON/YAML)
uri = uri.toLowerCase();
if (uri === '/index.json' || accept === 'application/json') {
formatter = formatJSON;
} else if (uri === '/index.yml' || accept === 'application/yaml') {
formatter = formatYAML;
}
// Protection against rendering data without having any checks loaded
// (shouldn't happen anyway, but just in case)
if (!cache.summary) {
return render500Error('E_NO_DATA', response);
}
// Site is served under multiple hosts so put that in the data to render
cache.summary.host = request.headers.host;
//cache.summary.discodefinition = quotes.getQuote;
// Run the data through the formatter and respond with the results
formatter(cache.summary, function (err2, data2) {
if (err2) {
return render500Error(err2, response);
}
respond(data2, 200, {
'Content-Type': formatter.contentType
}, response);
});
};
/**
* Render all loaded and minified scripts to client
*
* NOTA BENE: Order of scripts is not guaranteed and may change from
* load to load
*/
exports.renderScripts = function renderScripts(_, __, response) {
if (!cache.scripts) {
return render500Error('E_NO_DATA', response);
}
var text = [];
text.push(['/*', cache.scripts.map(function (d) {
return d.name;
}), '*/'].join(' '));
text = text.concat(cache.scripts.map(function (d) {
return d.data;
}));
respond(text.join('\n'), 200, {
'Content-Type': 'application/javascript'
}, response);
};
/**
* Render all loaded and minified stylesheets to the client
*/
exports.renderStyles = function renderStyles(_, __, response) {
if (!cache.styles) {
return render500Error('E_NO_DATA', response);
}
var text = [];
text.push(['/*', cache.styles.map(function (d) {
return d.name;
}), '*/'].join(' '));
text = text.concat(cache.styles.map(function (d) {
return d.data;
}));
respond(text.join('\n'), 200, {
'Content-Type': 'text/css'
}, response);
};
/**
* PROBABLY BROKEN.
*
* Render faked sample data to the client.
*
* TODO: Fix or replace this.
*/
exports.renderSample = function renderSample(time, response) {
var data = database.summarizeData(database.getSampleData(time, 200), {});
formatHTML(data, function (err2, data2) {
if (err2) {
return render500Error(err2, response);
}
respond(data2, 200, {
'Content-Type': 'text/html'
}, response);
});
};