forked from turboext/css
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
225 lines (184 loc) · 5.38 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = 'development';
}
if (!process.env.LIVERELOAD) {
process.env.LIVERELOAD = 'true';
}
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
const express = require('express');
const app = module.exports = express();
const rp = require('request-promise');
const { URL } = require('url');
const chalk = require('chalk');
const fs = require('fs');
const path = require('path');
const compression = require('compression');
const removeCSP = require('./lib/server/remove-csp');
const inject = require('./lib/server/inject');
const styleMiddlware = require('./lib/server/style-middleware');
if (process.env.NODE_ENV !== 'development') {
app.use((req, res, next) => {
const match = req.hostname.match(/^(.+)\.turboext\.net$/);
let pullRequest;
if (match && fs.existsSync(path.join('checkout', match[1]))) {
pullRequest = match[1];
} else {
return res.redirect('https://master.turboext.net');
}
req.ctx = req.ctx || {};
req.ctx.pullRequest = pullRequest;
next();
});
}
app.use(styleMiddlware);
app.use(express.static('public'));
app.use('/hosts', express.static('hosts'));
app.use(compression());
app.use((req, res, next) => {
const url = normalize(req.query.text);
req.ctx = req.ctx || {};
req.ctx.url = url;
req.ctx.hostname = req.query.hostname || getHostname(url);
next();
});
app.get('/turbo', (req, res, next) => {
const {
url,
hostname
} = req.ctx;
const params = cleanupParams(req.query);
if (!hostname || req.query.disable) {
return getTurbo(req, url, params).then(html => res.send(html)).catch(e => next(e));
}
if (params.ajax_type) {
return getTurbo(req, url, params).then(json => {
res.set('Content-Type', 'application/json; charset=utf-8');
res.send(json);
return res.end();
}).catch(e => next(e));
}
getTurbo(req, url, params)
.then(html => inject(req, html, hostname))
.then(html => res.send(html))
.catch(e => next(e));
});
app.get('/frame', (req, res, next) => {
fs.readFile('public/frame.html', 'utf8', (e, html) => {
if (e) {
return next(e);
}
res.send(html.replace(
'/turbo?placeholder=1',
req.originalUrl.replace('/frame', '/turbo')
));
});
});
app.get('/frame-morda', (req, res, next) => {
fs.readFile('public/frame-morda.html', 'utf8', (e, html) => {
if (e) {
return next(e);
}
res.send(html.replace(
'/turbo?placeholder=1',
req.originalUrl.replace('/frame-morda', '/turbo')
));
});
});
app.use((req, res) => {
res.status(404);
res.end('Unknown route');
});
app.use((err, req, res) => {
res.status(500);
res.end(err);
});
const DEV_SERVER_PORT = 3000;
const LIVRELOAD_PORT = 35729;
const PUBLIC = process.env.PUBLIC === 'true';
const debug = ['NODE_ENV', 'LIVERELOAD', 'PUBLIC']
.filter(v => process.env[v])
.map(v => `${v}=${process.env[v]}`).join(', ');
console.log(`Env variables: ${chalk.gray(debug)}`);
app.listen(DEV_SERVER_PORT, () => {
if (PUBLIC) {
const ngrok = require('ngrok');
ngrok.connect({
addr: DEV_SERVER_PORT,
region: 'eu'
}).then(url => {
console.log(`DevServer started at ${chalk.blue.underline(`${url}`)}`);
}).catch(e => {
console.error(e);
process.exit(1);
});
} else {
let output = `DevServer started at ${chalk.blue.underline(`http://localhost:${DEV_SERVER_PORT}`)}`;
if (process.env.LIVERELOAD === 'true' && process.env.NODE_ENV !== 'production') {
const livereload = require('./lib/server/lr');
livereload().listen(LIVRELOAD_PORT, () => {
output += chalk.gray(`, live reload started at http://localhost:${LIVRELOAD_PORT}`);
console.log(output);
});
} else {
console.log(output);
}
}
});
/**
**
* @param url
* @returns {string}
*/
function getHostname(url) {
try {
return new URL(url).origin;
} catch (e) {
return '';
}
}
function normalize(str) {
try {
const url = new URL(str);
if (url.hostname === 'yandex.ru') {
return url.searchParams.get('text') || '';
} else {
return url.toString();
}
} catch (e) {
return str;
}
}
function cleanupParams(queryParams) {
if (!queryParams) {
return {};
}
const params = { ...queryParams };
delete params.text;
// служебные параметры dev-server
delete params.disable;
delete params.hostname;
return params;
}
function getTurbo(req, url, params) {
const headers = { ...req.headers };
delete headers.host;
// выключаем поддержку brotli
headers['accept-encoding'] = 'gzip, deflate';
const turboHost = process.env.TURBO_HOST || 'https://yandex.ru';
if (!url) {
return rp({
uri: `${turboHost}/turbo`,
headers,
gzip: true
}).then(removeCSP);
}
return rp({
uri: `${turboHost}/turbo`,
headers,
qs: {
text: url,
...params
},
gzip: true
}).then(removeCSP);
}