forked from ffxiv-teamcraft/ffxiv-teamcraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
201 lines (168 loc) · 4.96 KB
/
server.ts
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
// These are important and needed before anything else
import 'zone.js/dist/zone-node';
import 'reflect-metadata';
import { ngExpressEngine } from '@nguniversal/express-engine';
import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader';
import * as express from 'express';
import * as path from 'path';
import { join } from 'path';
import { readFileSync } from 'fs';
import { REQUEST } from '@nguniversal/express-engine/tokens';
import { enableProdMode } from '@angular/core';
export const DIST_FOLDER = path.join(process.cwd(), 'dist/apps');
export const APP_NAME = 'client';
//Garland tools data skeleton
(global as any).gt = {
patch: {},
xp: [],
jobs: [],
node: {},
fishing: {},
mob: {},
location: {},
skywatcher: {},
quest: {},
venture: {},
npc: {},
action: {},
leve: {},
achievement: {},
instance: {},
fate: {},
item: {
index: []
},
bell: {
nodes: [],
fish: []
}
};
require('./ssr/output/gt-fish');
require('./ssr/output/gt-nodes');
const jsdom = require('jsdom');
const { JSDOM } = jsdom;
// index.html template
const template = readFileSync(path.join(DIST_FOLDER, APP_NAME, 'index.html')).toString();
const win = new JSDOM(template).window;
(global as any).XMLHttpRequest = win.XMLHttpRequest;
(global as any).WebSocket = win.WebSocket;
(global as any).window = win;
(global as any).window.gt = (global as any).gt;
(global as any).DOMTokenList = win.DOMTokenList;
(global as any).Node = win.Node;
(global as any).Text = win.Text;
(global as any).HTMLElement = win.HTMLElement;
(global as any).HTMLAnchorElement = win.HTMLAnchorElement;
(global as any).URLSearchParams = win.URLSearchParams;
(global as any).navigator = win.navigator;
(global as any).Event = win.Event;
(global as any).gtag = () => null;
Object.defineProperty(win.document.body.style, 'transform', {
value: () => {
return {
enumerable: true,
configurable: true
};
}
});
(global as any).document = win.document;
//Mock localStorage
const fakeStorage: Storage = {
length: 0,
clear: () => {
},
getItem: (_key: string) => null,
key: (_index: number) => null,
removeItem: (_key: string) => {
},
setItem: (_key: string, _data: string) => {
}
};
(global as any)['localStorage'] = fakeStorage;
// Faster renders in prod mode
enableProdMode();
if (global.v8debug) {
global.v8debug.Debug.setBreakOnException(); // speaks for itself
}
// Export our express server
export const app = express();
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require(`./dist/${APP_NAME}-server/main`);
app.engine('html', ngExpressEngine({
bootstrap: AppServerModuleNgFactory,
providers: [
provideModuleMap(LAZY_MODULE_MAP)
]
}));
app.set('view engine', 'html');
app.set('views', join(DIST_FOLDER, APP_NAME));
// Serve static files
app.get('*.*', express.static(join(DIST_FOLDER, APP_NAME)));
function detectIndexBot(userAgent) {
const bots = [
'bingbot',
'yandexbot',
'duckduckbot',
'googlebot',
'slurp',
'facebookexternalhit',
'linkedinbot',
'embedly',
'baiduspider',
'pinterest',
'vkShare',
'facebot',
'outbrain',
'W3C_Validator'
];
const agent = userAgent.toLowerCase();
for (const bot of bots) {
if (agent.indexOf(bot.toLowerCase()) > -1) {
console.log('index bot detected', bot, agent);
return true;
}
}
console.log('no bots found', agent);
return false;
}
function detectDeepLinkBot(userAgent) {
const deepLinkBots = [
'twitterbot',
'slackbot',
'Discordbot'
];
const agent = userAgent.toLowerCase();
for (const bot of deepLinkBots) {
if (agent.indexOf(bot.toLowerCase()) > -1) {
console.log('deep link bot detected', bot, agent);
return true;
}
}
console.log('no bots found', agent);
return false;
}
const indexAllowedPages = ['/search', '/community-rotations', '/levequests', '/about', '/support-us', '/desynth-guide', '/gc-supply', '/macro-translator', '/db/'];
// All regular routes use the Universal engine
app.get('*', (req, res) => {
const noSEO = req.headers.host.indexOf('beta.') > -1 || req.headers.host.indexOf('preview.') > -1;
const isIndexBot = detectIndexBot(req.headers['user-agent']);
const isDeepLinkBot = detectDeepLinkBot(req.headers['user-agent']);
const langFromUrl = /\/db\/(\w{2})\//.exec(req.url);
(req as any).lang = (langFromUrl && langFromUrl[1]) || req.headers['accept-language'] || 'en';
if (isDeepLinkBot || (!noSEO && isIndexBot && indexAllowedPages.some(page => req.originalUrl.indexOf(page) > -1))) {
res.render(join(DIST_FOLDER, APP_NAME, 'index.html'), {
req,
providers: [
{ provide: REQUEST, useValue: req }
]
});
} else {
res.sendFile(join(DIST_FOLDER, APP_NAME, 'index.html'));
}
});
// If we're not in the Cloud Functions environment, spin up a Node server
if (!process.env.FUNCTION_NAME) {
const PORT = process.env.PORT || 4000;
app.listen(PORT, () => {
console.log(`Node server listening on http://localhost:${PORT}`);
});
}