-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
455 lines (423 loc) · 13.3 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//© 2019-20 by blubbll
("use strict");
///////////////////////////////////////////////////////////////////////////
//DEPLOY
///////////////////////////////////////////////////////////////////////////
(async () => {
const script = "!.glitch-deploy.js";
if (process.env.PROJECT_DOMAIN) {
const deployfile = ":deploying:";
require("download")(
"https://raw.githubusercontent.com/blubbll/glitch-deploy/master/glitch-deploy.js",
__dirname,
{
filename: script
}
).then(() => {
deployProcess();
});
const deployProcess = async () => {
const deploy = require(`./${script}`);
const deployCheck = async () => {
//console.log("🐢Checking if we can deploy...");
if (fs.existsSync(`${__dirname}/${deployfile}`)) {
console.log("🐢💥Deploying triggered via file.");
fs.unlinkSync(deployfile);
await deploy({
ftp: {
password: process.env.DEPLOY_PASS,
user: process.env.DEPLOY_USER,
host: process.env.DEPLOY_HOST
},
clear: 0,
verbose: 1,
env: 1
});
request(
`https://evennode-reboot.glitch.me/reboot/${process.env.DEPLOY_TOKEN}/${process.env.PROJECT_DOMAIN}`,
(error, response, body) => {
console.log(error || body);
}
);
require("child_process").exec("refresh");
} else setTimeout(deployCheck, 9999); //10s
};
setTimeout(deployCheck, 999); //1s
};
} else require(`./${script}`)({ env: true }); //apply env on deployed server
})();
// init project
const express = require("express"),
app = express(),
fs = require("fs"),
bodyParser = require("body-parser"),
cors = require("cors"),
request = require("request"),
sass = require("node-sass"),
es6tr = require("es6-transpiler"),
regionParser = require("accept-language-parser"),
Terser = require("terser"),
crush = require("html-crush").crush,
fetch = require("node-fetch");
//configure bodyparser
app.use(cors(), [bodyParser.urlencoded({ extended: true }), bodyParser.json()]);
{
//BUILD
if (process.env.PROJECT_NAME) {
//dist-folder (output)
const dist = "/!dist";
//create folders if not existing
!fs.existsSync(`${__dirname}/${dist}`) &&
fs.mkdirSync(`${__dirname}/${dist}`);
!fs.existsSync(`${__dirname}/${dist}/html`) &&
fs.mkdirSync(`${__dirname}/${dist}/html`);
{
//ui-words
const json = JSON.parse(
fs.readFileSync(`${__dirname}/build/ui-words.json`, "utf8")
).ui;
let html = `<ui-words>`;
for (const group in json) {
html += `<cat name="${group}">`;
for (const key in json[group]) {
html += `<key name="${key}">`;
const val = json[group][key];
html += `${val}</key>`;
}
html += `</cat>`;
}
html += `</ui-words>`;
fs.writeFileSync(`${__dirname}/${dist}/html/ui-words.html`, html, "utf8");
}
const atto = "/*!💜I love you, Monad.*/";
const transpile = (file, direct) => {
const result = es6tr.run({ filename: file });
const outFile = `${file.replace("/build", dist)}`;
if (result.src) {
const outResult = result.src.replace(/\0/gi, "").replace(/\\r\n/gi, "");
if (!direct)
[
fs.writeFileSync(outFile, `${atto}\r\n${outResult}`),
console.log(`Transpiled ${file} to ${outFile}!`)
];
else {
//console.log(`Transpiled ${file}!`);
return outResult;
}
} else console.warn(`Error at transpiling of file ${file}:`, result);
};
//HTML
let building = false;
const COMPILE_HTML = () => {
if (building) return;
else building = true;
const htmls = [
`${__dirname}/build/index.html`,
`${__dirname}/build/cookie.html`,
`${__dirname}/build/cookie-what.html`,
`${__dirname}/build/outdated-browser.html`,
`${__dirname}/build/html/app.html`,
`${__dirname}/build/html/404.html`,
`${__dirname}/build/html/error.html`,
`${__dirname}/build/html/channel.html`,
`${__dirname}/build/html/player.html`,
`${__dirname}/build/html/history-list.html`,
`${__dirname}/build/html/result-list.html`,
`${__dirname}/build/html/result-item.html`,
`${__dirname}/build/html/start.html`
];
const templateFile = `${__dirname}/${dist}/html/templates.html`;
fs.writeFileSync(templateFile, "<templates>"); //clear bundlefile
for (const html of htmls) {
let input = fs.readFileSync(html, "utf8");
if (html.endsWith("/build/index.html")) {
input = input
.replace(
//size indicators
/{{indicators}}/gi,
fs.readFileSync(`${__dirname}/build/components/indicators.html`)
)
.replace(
//some sgv filters
/{{filters}}/gi,
fs.readFileSync(`${__dirname}/build/components/filters.html`)
)
.replace(
//loading animation instructions
/{{loader}}/gi,
fs.readFileSync(`${__dirname}/build/components/loader.html`)
);
}
if (
html
.split("/build/")
.pop()
.startsWith("html/")
) {
//bundle output html
fs.appendFileSync(
templateFile,
`<template name="${html.split("html/")[1].split(".")[0]}">${
crush(input, {
removeLineBreaks: true,
removeIndentations: true,
lineLengthLimit: Number.POSITIVE_INFINITY
}).result
}</template>`
);
} else
fs.writeFileSync(
html.replace("/build", dist),
crush(input, {
removeLineBreaks: true,
removeIndentations: true,
lineLengthLimit: Number.POSITIVE_INFINITY
}).result,
"utf8"
);
//console.log(`Minified ${html}!`);
}
fs.appendFileSync(templateFile, "</templates>");
console.log(`Minified html`);
setTimeout(() => {
building = false;
}, 999);
};
//BUNDLE JS
const COMPILE_JS = () => {
if (building) return;
else building = true;
const bundleFile = `${__dirname}/${dist}/bundle.js`;
let bundle = "";
//maybe glob would be better in the future
const scripts = [
`${__dirname}/build/js/tools.js`,
`${__dirname}/build/js/routing.js`,
`${__dirname}/build/js/_PLAY.js`,
`${__dirname}/build/js/_SEARCH.js`,
`${__dirname}/build/js/_HISTORY.js`,
`${__dirname}/build/js/bg.js`,
`${__dirname}/build/js/client.js`
];
for (const script of scripts) {
bundle += transpile(script, true);
}
//write bundle
const minified = Terser.minify(bundle);
minified.error
? console.warn(minified.error)
: fs.writeFileSync(bundleFile, `${atto}\r\n${minified.code}`, "utf8");
console.log(`Bundled ${scripts} into ${bundleFile}!`);
building = false;
};
//SASS
const COMPILE_CSS = () => {
if (building) return;
else building = true;
const bundleFile = `${__dirname}/${dist}/bundle.css`;
let bundle = "";
const styles = [
`${__dirname}/build/css/lib/neumorphism-light.css`,
`${__dirname}/build/css/style.sass.css`
];
for (const style of styles) {
bundle += sass
.renderSync({
data: fs.readFileSync(style, "utf8") || "/**/",
outputStyle: "compressed"
})
.css.toString("utf8");
}
//write bundle
fs.writeFileSync(bundleFile, `${atto}\r\n${bundle}`, "utf8");
console.log(`Bundled ${styles} into ${bundleFile}!`);
building = false;
};
//first compile
{
COMPILE_CSS();
COMPILE_JS();
COMPILE_HTML();
//watch changes
fs.watch(`${__dirname}/build/css/`, COMPILE_CSS);
fs.watch(`${__dirname}/build/js/`, COMPILE_JS);
fs.watch(`${__dirname}/build/html/`, COMPILE_HTML);
}
}
}
//API
const API = "/api";
//calculate language based on accept-language header
const getLanguage = header => {
return (header
? regionParser.parse(header)[0]
? regionParser.parse(header)[0].code
: header
: "EN"
).toLowerCase();
};
//get da ip
app.set("trust proxy", true);
//CORZ
app.use("*", (req, res, next) => {
if (
req.originalUrl === "/app.html" ||
req.originalUrl.startsWith("/api/") ||
req.originalUrl.startsWith("/html/")
) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "*");
res.setHeader("Access-Control-Allow-Headers", "*");
}
next();
});
//simple geo ip
app.get(`${API}/geoip`, (req, res) => {
const get = () =>
request(`http://api.petabyet.com/geoip/${req.ip}`)
.on("response", _res => {
if (res.statusCode === 200) {
_res.pipe(res);
} else setTimeout(get, 999);
})
.on("error", () => setTimeout(get, 999));
get();
});
//SEARCH
app.get(`${API}/:region/search/:q/:page`, (req, res) => {
const get = () =>
request({
uri: encodeURI(
`https://${process.env.IV_HOST}/api/v1/search/?region=${req.params.region}&q=${req.params.q}&page=${req.params.page}`
),
method: "GET",
timeout: 3000,
followRedirect: true,
maxRedirects: 10
})
.on("response", _res => {
if (res.statusCode === 200) {
_res.pipe(res);
} else setTimeout(get, 999);
})
.on("error", () => setTimeout(get, 999));
get();
});
//VIDEO
app.get(`${API}/:region/video/:vid`, (req, res) => {
const get = () =>
request({
uri: encodeURI(
`https://${process.env.IV_HOST}/api/v1/videos/${req.params.vid}?region=${req.params.region}`
),
method: "GET",
timeout: 3000,
followRedirect: true,
maxRedirects: 10
})
.on("response", _res => {
if (res.statusCode === 200) {
_res.pipe(res);
} else setTimeout(get, 999);
})
.on("error", () => setTimeout(get, 999));
get();
});
//VIDEO
app.get(`${API}/:region/videoDEMO/:vid`, (req, res) => {
const get = () =>
request({
uri: encodeURI(
`https://${process.env.IV_HOST_DEMO}/api/v1/videos/${req.params.vid}?region=${req.params.region}`
),
method: "GET",
timeout: 3000,
followRedirect: true,
maxRedirects: 10
})
.on("response", _res => {
if (res.statusCode === 200) {
_res.pipe(res);
} else setTimeout(get, 999);
})
.on("error", () => setTimeout(get, 999));
get();
});
//COMPLETE
app.get(`${API}/:region/complete/:q`, (req, res) => {
//const language = getLanguage(req.headers["accept-language"]);
const empty = "No results found...";
const url = `https://suggestqueries.google.com/complete/search?client=youtube&cp=1&ds=yt&q=${req.params.q}&hl=${req.params.region}&format=5&alt=json&callback=?`;
request(
{
uri: url,
method: "GET",
timeout: 3000,
followRedirect: true,
maxRedirects: 10,
encoding: "latin1"
},
async (error, response, body) => {
//console.warn(response);
if (body) {
let suggs = [];
//console.log(body)
const any = !body.includes('",[],{"k"');
if (any) {
const raw = body
.split(`window.google.ac.h(["${req.params.q}",[[`)[1]
.split("]]]")[0] //trim end
.split(","); //into arr
raw.length--; //remove last char
Array.prototype.forEach.call(raw, (val, key) => {
if (val !== "0]" && val.length) {
//reached da end
if (val.slice(1) === "]]" || val.startsWith("{")) return;
if (!val.slice(1).endsWith("]]") && val.slice(1).length > 1)
suggs.push(
val
.slice(key === 0 ? 1 : 2, -1)
.replace(/\\u([0-9a-fA-F]{4})/g, (m, cc) =>
String.fromCharCode("0x" + cc)
)
);
}
});
}
suggs.length
? res.json({ code: 200, data: suggs })
: res.json({ code: 404, msg: empty });
}
}
);
});
//forwarding if not defined
app.get("*", (req, res, next) => {
if (req.url === "/" || !fs.existsSync(`${__dirname}/!dist${req.url}`))
//index
res.send(
fs
.readFileSync(`${__dirname}/!dist/index.html`, "utf8")
.replace(/{{local}}/gi, `${req.protocol}://${req.hostname}/`)
.replace(/{{from}}/gi, `${req.url}`)
);
//resource
else next();
});
// static
app.use(express.static(`${__dirname}/!dist`));
// listen for requests :)
const listener = app.listen(process.env.PORT, () => {
console.log(`Your app is listening on port ${listener.address().port}`);
});
const restartHours = 6;
setTimeout(() => {
process.on("exit", () => {
require("child_process").spawn(process.argv.shift(), process.argv, {
cwd: process.cwd(),
detached: true,
stdio: "inherit"
});
});
process.exit();
}, 1000 * 60 * 60 * restartHours); //restart every 6 hours