-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
51 lines (45 loc) · 2.08 KB
/
server.js
File metadata and controls
51 lines (45 loc) · 2.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
// -- Libs -----------------------------------------------------------------------------
const fs = require('fs');
const path = require('path');
const express = require('express');
const useragent = require('useragent');
// -- Config ----------------------------------------------------------------------------
const template = fs.readFileSync(path.join(__dirname, 'public/template.html'), 'utf8');
const log = process.stdout.write.bind(process.stdout);
const port = process.env.PORT || 3000;
const app = express();
// -- Helpers ---------------------------------------------------------------------------
function isCompat(userAgent) {
const { family, major } = useragent.parse(userAgent);
const majorVersion = parseInt(major, 10);
return family === 'IE'
|| (family === 'Chrome' && majorVersion < 48)
|| (family === 'Firefox' && majorVersion < 52)
|| (family === 'Safari' && majorVersion < 10);
}
function staticPath(...args) {
return path.join(__dirname, 'public', ...args);
}
function renderTemplate(isCompat, useCustomElementRegistry, useNativeShadow) {
// Poor's man templating engine
return template
.replace('{{useCustomElementRegistry}}', useCustomElementRegistry ? 'enabled' : 'disabled')
.replace('{{useNativeShadow}}', useNativeShadow ? 'enabled' : 'disabled')
.replace('{{js_bundle}}', isCompat ? 'compat' : 'main');
}
// -- Middlewares -----------------------------------------------------------------------
app.use('/static', express.static(staticPath()));
app.get('/', (req, res) => {
const isCompatMode = isCompat(req.headers['user-agent']);
const { useCustomElementRegistry, useNativeShadow } = req.query;
res.send(renderTemplate(isCompatMode, useCustomElementRegistry, useNativeShadow));
});
// -- Server Start -----------------------------------------------------------------------
module.exports.start = () => {
return new Promise((resolve) => {
const server = app.listen(port, () => {
log(`Server ready - http://localhost:${port}\n`);
resolve(server);
});
});
};