-
-
Notifications
You must be signed in to change notification settings - Fork 259
/
route.js
189 lines (148 loc) · 4.52 KB
/
route.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
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
'use strict';
const DIR_SERVER = './';
const DIR_COMMON = '../common/';
const fs = require('fs');
const {
promisify,
} = require('util');
const flop = require('flop');
const ponse = require('ponse');
const rendy = require('rendy');
const format = require('format-io');
const squad = require('squad');
const apart = require('apart');
const currify = require('currify');
const tryToCatch = require('try-to-catch');
const config = require(DIR_SERVER + 'config');
const root = require(DIR_SERVER + 'root');
const prefixer = require(DIR_SERVER + 'prefixer');
const CloudFunc = require(DIR_COMMON + 'cloudfunc');
const prefix = squad(prefixer, apart(config, 'prefix'));
const sendIndex = (params, data) => {
const ponseParams = {
...params,
name: 'index.html'
};
ponse.send(data, ponseParams);
};
const {FS} = CloudFunc;
const Columns = require('./columns');
const Template = require('./template');
const read = promisify(flop.read);
const realpath = promisify(fs.realpath);
/**
* routing of server queries
*/
module.exports = currify((options, request, response, next) => {
const name = ponse.getPathName(request);
const isFS = RegExp('^/$|^' + FS).test(name);
if (!isFS)
return next();
route(options, request, response)
.catch(next);
});
async function route(options, request, response) {
const name = ponse.getPathName(request);
const gzip = true;
const p = {
request,
response,
gzip,
name,
};
config('prefix', prefixer(request.baseUrl));
const rootName = name.replace(CloudFunc.FS, '') || '/';
const fullPath = root(rootName);
const [error, dir] = await tryToCatch(read, fullPath);
const {html} = options;
if (!error)
return sendIndex(p, buildIndex(html, {
...dir,
path: format.addSlashToEnd(rootName),
}));
if (error.code !== 'ENOTDIR')
return ponse.sendError(error, p);
const [realPathError, pathReal] = await tryToCatch(realpath, fullPath);
ponse.sendFile({
...p,
name: realPathError ? name : pathReal,
gzip: false
});
}
/**
* additional processing of index file
*/
function indexProcessing(options) {
const oneFilePanel = config('oneFilePanel');
const noKeysPanel = !config('keysPanel');
const noContact = !config('contact');
const noConfig = !config('configDialog');
const noConsole = !config('console');
const noTerminal = !config('terminal');
const panel = options.panel;
let data = options.data;
if (noKeysPanel)
data = hideKeysPanel(data);
if (oneFilePanel)
data = data
.replace('icon-move', 'icon-move none')
.replace('icon-copy', 'icon-copy none');
if (noContact)
data = data
.replace('icon-contact', 'icon-contact none');
if (noConfig)
data = data
.replace('icon-config', 'icon-config none');
if (noConsole)
data = data
.replace('icon-console', 'icon-console none');
if (noTerminal)
data = data
.replace('icon-terminal', 'icon-terminal none');
const left = rendy(Template.panel, {
side : 'left',
content : panel,
className : !oneFilePanel ? '' : 'panel-single'
});
let right = '';
if (!oneFilePanel)
right = rendy(Template.panel, {
side : 'right',
content : panel,
className : ''
});
const name = config('name');
data = rendy(data, {
title: CloudFunc.getTitle({
name,
}),
fm: left + right,
prefix: prefix(),
config: JSON.stringify(config('*')),
columns: Columns[config('columns')],
});
return data;
}
function buildIndex(html, json) {
const panel = CloudFunc.buildFromJSON({
data: json,
prefix: prefix(),
template: Template,
});
return indexProcessing({
panel,
data: html,
});
}
module.exports._hideKeysPanel = hideKeysPanel;
function hideKeysPanel(html) {
const keysPanel = '<div id="js-keyspanel" class="{{ className }}"';
const keysPanelRegExp = '<div id="?js-keyspanel"? class="?{{ className }}"?';
const from = rendy(keysPanelRegExp, {
className: 'keyspanel'
});
const to = rendy(keysPanel, {
className: 'keyspanel hidden'
});
return html.replace(RegExp(from), to);
}