-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.js
97 lines (89 loc) · 2.28 KB
/
helpers.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
// helpers
var fs = require('fs');
module.exports = {
//
// save port numbers
//
save_ports: function(path, port, next_port) {
fs.writeFile(path + "/port", port, function(err) {
if(err) { console.log(err); }
console.log("saved %s in %s/port", port, path);
});
fs.writeFile(path + "/next_port", next_port, function(err) {
if(err) { console.log(err); }
console.log("saved %s in %s/next_port", next_port, path);
});
},
//
// Return a javascript document
//
js: function(res, content, dynamic) {
headers = {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/javascript'
};
// if (dynamic) {
headers['Cache-Control'] = 'no-cache, max-age=0';
// } else {
// headers['Cache-Control'] = 'max-age=1';
// }
res.writeHead(200, headers );
res.end(dynamic ? JSON.stringify(content): content);
},
//
// Return a html document
//
html: function(res, string) {
res.writeHead(200, {
'Cache-Control': 'no-cache, max-age=0',
'Content-Type': 'text/html'
});
res.end(string);
},
//
// Redirect the browser
//
redirect: function(res, url) {
res.writeHead(302, {
'Location': url,
'Content-Type': 'text/html',
});
res.end('Moved');
},
//
// Get the next value, calculated from the previous one
//
next_value: (value) => Math.round(value + 2 * Math.random() - 1.0, 3),
//
// Read a file, or if missing, return a default value
//
read_or_default: function(path, fallback) {
var fs = require('fs');
if (fs.existsSync(path)) {
return fs.readFileSync(path, 'utf8');
} else return fallback;
},
//
// Create a HTML document with headlines
// The friendly background color is supposedly liked by both men and women
//
friendly_page: function(subject, tagline, content) {
style = 'background:#7fdba3; zoom: 2; margin:3em; font-family:roboto,arial;';
return '<body style="' + style + '"><h3>' +
subject + '</h3><h4>' + tagline + '</h4>' + content + '</body>';
},
//
// Adds versioning and status needed by a dotted client/server system
//
versioned_json: function(content, current, deployed, target) {
content.dotted = {
major: current.major,
tag: current.tag,
deployed_tag: deployed.tag,
migrate: current.major < target.major,
port: current.port,
next_port: target.port,
};
return content;
},
}