This repository was archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 311
/
Copy pathhttp-server.ts
171 lines (146 loc) · 5.28 KB
/
http-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
import * as path from 'path';
import { injectNotificationScript } from './injector';
import { injectLiveReloadScript } from './live-reload';
import * as express from 'express';
import * as fs from 'fs';
import * as url from 'url';
import {
ServeConfig,
LOGGER_DIR,
IONIC_LAB_URL,
IOS_PLATFORM_PATHS,
ANDROID_PLATFORM_PATHS
} from './serve-config';
import { Logger } from '../logger/logger';
import * as proxyMiddleware from 'proxy-middleware';
import { injectDiagnosticsHtml } from '../logger/logger-diagnostics';
import * as Constants from '../util/constants';
import { getBooleanPropertyValue } from '../util/helpers';
import { getProjectJson, IonicProject } from '../util/ionic-project';
import { LabAppView, ApiCordovaProject, ApiPackageJson } from './lab';
/**
* Create HTTP server
*/
export function createHttpServer(config: ServeConfig): express.Application {
const app = express();
app.set('serveConfig', config);
app.get('/', serveIndex);
app.use('/', express.static(config.wwwDir));
app.use(`/${LOGGER_DIR}`, express.static(path.join(__dirname, '..', '..', 'bin'), { maxAge: 31536000 }));
// Lab routes
app.use(IONIC_LAB_URL + '/static', express.static(path.join(__dirname, '..', '..', 'lab', 'static')));
app.get(IONIC_LAB_URL, LabAppView);
app.get(IONIC_LAB_URL + '/api/v1/cordova', ApiCordovaProject);
app.get(IONIC_LAB_URL + '/api/v1/app-config', ApiPackageJson);
app.get('/cordova.js', servePlatformResource, serveMockCordovaJS);
app.get('/cordova_plugins.js', servePlatformResource);
app.get('/plugins/*', servePlatformResource);
if (config.useProxy) {
setupProxies(app);
}
return app;
}
function setupProxies(app: express.Application) {
if (getBooleanPropertyValue(Constants.ENV_READ_CONFIG_JSON)) {
getProjectJson().then(function (projectConfig: IonicProject) {
for (const proxy of projectConfig.proxies || []) {
let opts: any = url.parse(proxy.proxyUrl);
if (proxy.proxyNoAgent) {
opts.agent = false;
}
opts.rejectUnauthorized = !(proxy.rejectUnauthorized === false);
opts.cookieRewrite = proxy.cookieRewrite;
app.use(proxy.path, proxyMiddleware(opts));
Logger.info('Proxy added:' + proxy.path + ' => ' + url.format(opts));
}
}).catch((err: Error) => {
Logger.error(`Failed to read the projects ionic.config.json file: ${err.message}`);
});
}
}
/**
* http responder for /index.html base entrypoint
*/
function serveIndex(req: express.Request, res: express.Response) {
const config: ServeConfig = req.app.get('serveConfig');
// respond with the index.html file
const indexFileName = path.join(config.wwwDir, process.env[Constants.ENV_VAR_HTML_TO_SERVE]);
fs.readFile(indexFileName, (err, indexHtml) => {
if (!indexHtml) {
Logger.error(`Failed to load index.html`);
res.send('try again later');
return;
}
if (config.useLiveReload) {
indexHtml = injectLiveReloadScript(indexHtml, req.hostname, config.liveReloadPort);
indexHtml = injectNotificationScript(config.rootDir, indexHtml, config.notifyOnConsoleLog, config.notificationPort);
}
indexHtml = injectDiagnosticsHtml(config.buildDir, indexHtml);
res.set('Content-Type', 'text/html');
res.send(indexHtml);
});
}
/**
* http responder for cordova.js file
*/
function serveMockCordovaJS(req: express.Request, res: express.Response) {
res.set('Content-Type', 'application/javascript');
res.send('// mock cordova file during development');
}
/**
* Middleware to serve platform resources
*/
async function servePlatformResource(req: express.Request, res: express.Response, next: express.NextFunction) {
const config: ServeConfig = req.app.get('serveConfig');
const userAgent = req.header('user-agent');
if (!config.isCordovaServe) {
return next();
}
let root = await getResourcePath(req.url, config, userAgent);
if (root) {
res.sendFile(req.url, { root });
} else {
next();
}
}
/**
* Determines the appropriate resource path, and checks if the specified url
*
* @returns string of the resource path or undefined if there is no match
*/
async function getResourcePath(url: string, config: ServeConfig, userAgent: string): Promise<string> {
let searchPaths: string[] = [config.wwwDir];
if (isUserAgentIOS(userAgent)) {
searchPaths = IOS_PLATFORM_PATHS.map(resourcePath => path.join(config.rootDir, resourcePath));
} else if (isUserAgentAndroid(userAgent)) {
searchPaths = ANDROID_PLATFORM_PATHS.map(resourcePath => path.join(config.rootDir, resourcePath));
}
for (let i = 0; i < searchPaths.length; i++) {
let checkPath = path.join(searchPaths[i], url);
try {
let result = await checkFile(checkPath);
return searchPaths[i];
} catch (e) { }
}
}
/**
* Checks if a file exists (responds to stat)
*/
function checkFile(filePath: string): Promise<void> {
return new Promise((resolve, reject) => {
fs.stat(filePath, (err: Error, stats: any) => {
if (err) {
return reject();
}
resolve();
});
});
}
function isUserAgentIOS(ua: string): boolean {
ua = ua.toLowerCase();
return (ua.indexOf('iphone') > -1 || ua.indexOf('ipad') > -1 || ua.indexOf('ipod') > -1);
}
function isUserAgentAndroid(ua: string): boolean {
ua = ua.toLowerCase();
return ua.indexOf('android') > -1;
}