-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathroutes.js
81 lines (65 loc) · 1.9 KB
/
routes.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
'use strict';
const fs = require('fs');
const htmlMap = {
'&': '&',
'<': '<',
'>': '>',
'"': '"'
};
// TODO: once proper imports are used, deduplicate from editor/js/utilts.js
function escapeHtml(str) {
if (!str) {
return '';
}
return str.replace(/[&<>"]/g, (tag) => htmlMap[tag] || tag);
}
module.exports = (XIBLE, EXPRESS_APP) => {
EXPRESS_APP.get('/api/validateFlowPermissions', async (req, res) => {
const permissionsResult = await XIBLE.Flow.validatePermissions();
res.json(permissionsResult);
});
EXPRESS_APP.get('/api/serverDate', (req, res) => {
res.json(Date.now());
});
EXPRESS_APP.get('/api/activeFlowStoreType', (req, res) => {
res.json(XIBLE.activeFlowStoreType);
});
// send out any existing statuses
EXPRESS_APP.get('/api/persistentWebSocketMessages', (req, res) => {
res.json(XIBLE.persistentWebSocketMessages);
});
// non existing views
EXPRESS_APP.get('/views/*.js', (req, res) => {
res.status(404).end();
});
// expose xibleWrapper to editor
EXPRESS_APP.get('/js/xibleWrapper.js', (req, res) => {
res.sendFile(require.resolve('xible-wrapper/dist/index.js'));
});
// expose chart.js to editor
EXPRESS_APP.get('/js/Chart.min.js', (req, res) => {
res.sendFile(require.resolve('chart.js/dist/Chart.min.js'));
});
EXPRESS_APP.get('*', (req, res, next) => {
// API hosting.
if (
/^\/api\//.test(req.path)
) {
next();
return;
}
// inject the correct base URL
fs.readFile(`${__dirname}/editor/index.htm`, (err, data) => {
if (err) {
res.status(500).end();
}
const basePath = process.env.BASE_HREF || '/';
const transformedBaseHrefData = data.toString().replace(
/<base href="[^"]*">/,
`<base href="${escapeHtml(basePath)}">`
);
res.contentType('html');
res.send(transformedBaseHrefData);
});
});
};