-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver.js
67 lines (61 loc) · 2.37 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
'use strict';
const get = require('lodash.get');
const serverVarsFactory = function () {
return Object.create({
store: {},
add: function (key, val) {
if (typeof key === 'object') {
// copy all properties into the store
Object.assign(this.store, key);
} else {
// set on the store (copy if an object)
this.store[key] =
!Array.isArray(val) && typeof val === 'object' ? Object.assign({}, val) : val;
}
},
get: function (key) {
if (!key) {
// return a copy of the entire store
return Object.create({}, this.store);
}
return get(this.store, key);
},
inject: function () {
var stringified =
'<script>window.__SERVER_VARS__ = ' +
// safely embed JSON within HTML
// see http://stackoverflow.com/a/4180424/266795
JSON.stringify(this.store)
.replace(/</g, '\\u003c')
.replace(/-->/g, '--\\>')
.replace(/\u2028/g, '\\u2028')
.replace(/\u2029/g, '\\u2029') +
';</script>';
this.store = {}; // helps w gc
return stringified;
},
// inert script is faster for large objects
injectInertScript: function () {
const stringified = `<script id="serverVars_data" type="application/json">${JSON.stringify(
this.store
)
.replace(/</g, '\\u003c')
.replace(/>/g, '\\u003E')
.replace(/\u2028/g, '\\u2028')
.replace(/\u2029/g, '\\u2029')}</script>
<script>window.__SERVER_VARS__ = JSON.parse(document.getElementById("serverVars_data").textContent);</script>`;
this.store = null; // helps w gc
return stringified;
},
});
};
const serverVars = serverVarsFactory();
serverVars.serverVarsFactory = serverVarsFactory;
serverVars.middleware = function (req, res, next) {
if (res.serverVars === undefined) {
res.serverVars = res.locals.serverVars = serverVarsFactory({});
res.serverVars.add(Object.assign({}, serverVars.store)); // get a copy of app-wide serverVars
}
next();
};
module.exports = serverVars;