-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathserver.ts
More file actions
executable file
·175 lines (153 loc) · 5.98 KB
/
server.ts
File metadata and controls
executable file
·175 lines (153 loc) · 5.98 KB
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
import chalk from "chalk";
import fs from "fs";
import http from "http";
import httpProxy from "http-proxy";
import https from "https";
import internalIp from "internal-ip";
import net from "net";
import open from "open";
import { DEFAULT_CONFIG } from "../config";
import { address, hostnameToIpAdress, isHttpUrl, isHttpsUrl, logger, logRequest, registerProcessExit, validateDevServerConfig } from "../core";
import { HAS_API, IS_API_DEV_SERVER, IS_APP_DEV_SERVER, SWA_CLI_API_URI, SWA_CLI_APP_PROTOCOL } from "../core/constants";
import { swaCLIEnv } from "../core/env";
import { validateFunctionTriggers } from "./handlers/function.handler";
import { handleUserConfig, onConnectionLost, requestMiddleware } from "./middlewares/request.middleware";
const { SWA_CLI_PORT, SWA_CLI_APP_SSL } = swaCLIEnv();
var proxyApp: any;
if (SWA_CLI_APP_SSL === "true") {
proxyApp = httpProxy.createProxyServer({
autoRewrite: true,
agent: new https.Agent({
keepAlive: true,
keepAliveMsecs: 5000,
}),
});
if (isHttpUrl(SWA_CLI_API_URI())) {
logger.warn(`Please make sure you want to hit the http proxy server.`);
}
} else if (SWA_CLI_APP_SSL === "false") {
proxyApp = httpProxy.createProxyServer({
autoRewrite: true,
agent: new http.Agent({
keepAlive: true,
keepAliveMsecs: 5000,
}),
});
if (isHttpsUrl(SWA_CLI_API_URI())) {
logger.error(`Your connection is not secure. Please start the CLI using the flag --ssl. Exiting`, true);
}
}
// TODO: handle multiple workflow files (see #32)
if (DEFAULT_CONFIG.githubActionWorkflowLocation) {
logger.log(`\nUsing workflow file:\n ${chalk.green(DEFAULT_CONFIG.githubActionWorkflowLocation)}`);
}
const httpsServerOptions: Pick<https.ServerOptions, "cert" | "key"> | null =
DEFAULT_CONFIG.ssl && DEFAULT_CONFIG.sslCert && DEFAULT_CONFIG.sslKey
? {
cert: DEFAULT_CONFIG.sslCert.startsWith("-----BEGIN") ? DEFAULT_CONFIG.sslCert : fs.readFileSync(DEFAULT_CONFIG.sslCert, "utf8"),
key: DEFAULT_CONFIG.sslKey.startsWith("-----BEGIN") ? DEFAULT_CONFIG.sslKey : fs.readFileSync(DEFAULT_CONFIG.sslKey, "utf8"),
}
: null;
function requestHandler(userConfig: SWAConfigFile | undefined) {
return async function (req: http.IncomingMessage, res: http.ServerResponse) {
await requestMiddleware(req, res, proxyApp, userConfig);
};
}
function onWsUpgrade() {
return (req: http.IncomingMessage, socket: net.Socket, head: Buffer) => {
if (IS_APP_DEV_SERVER()) {
const target = DEFAULT_CONFIG.outputLocation;
const remote = `ws://${req.headers.host}`;
logRequest(req, remote);
proxyApp.ws(
req,
socket,
head,
{
target,
secure: false,
},
onConnectionLost(req, socket, target)
);
proxyApp.once("proxyRes", (proxyRes: http.IncomingMessage) => {
logger.silly(`getting response from dev server`);
logRequest(req, remote, proxyRes.statusCode);
});
}
};
}
function onServerStart(server: https.Server | http.Server, socketConnection: net.Socket | undefined) {
return () => {
if (IS_APP_DEV_SERVER()) {
// prettier-ignore
logger.log(
`\nUsing dev server for static content:\n` +
` ${chalk.green(DEFAULT_CONFIG.outputLocation)}`
);
} else {
// prettier-ignore
logger.log(
`\nServing static content:\n` +
` ${chalk.green(DEFAULT_CONFIG.outputLocation)}`
);
}
if (DEFAULT_CONFIG.apiLocation) {
if (IS_API_DEV_SERVER()) {
// prettier-ignore
logger.log(
`\nUsing dev server for API:\n` +
` ${chalk.green(DEFAULT_CONFIG.apiLocation)}`
);
} else {
// prettier-ignore
logger.log(
`\nServing API:\n` +
` ${chalk.green(DEFAULT_CONFIG.apiLocation)}`
);
}
}
// note: this string must not change. It is used by the VS Code extension.
// see: https://github.com/Azure/static-web-apps-cli/issues/124
//--------------------------------------------------------------------------------
const serverAddress = address(DEFAULT_CONFIG.host, Number(SWA_CLI_PORT), SWA_CLI_APP_PROTOCOL);
let logMessage = `\nAzure Static Web Apps emulator started at ${chalk.green(serverAddress)}. Press CTRL+C to exit.\n\n`;
//--------------------------------------------------------------------------------
logger.log(logMessage);
if (DEFAULT_CONFIG.open) {
open(serverAddress);
}
server.on("upgrade", onWsUpgrade());
registerProcessExit(() => {
socketConnection?.end(() => logger.info("WebSocket connection closed."));
server.close(() => logger.log("Server stopped."));
proxyApp.close(() => logger.log("App proxy stopped."));
logger.info("Azure Static Web Apps emulator shutting down...");
process.exit(0);
});
};
}
// start SWA proxy server
(async () => {
let socketConnection: net.Socket | undefined;
const localIpAdress = await internalIp.v4();
// load user custom rules if running in local mode (non-dev server)
let userConfig: SWAConfigFile | undefined;
// load user configuration even when using a dev server
userConfig = await handleUserConfig(DEFAULT_CONFIG.swaConfigLocation || DEFAULT_CONFIG.appLocation);
const createServer = () => {
if (DEFAULT_CONFIG.ssl && httpsServerOptions !== null) {
return https.createServer(httpsServerOptions, requestHandler(userConfig));
}
return http.createServer(requestHandler(userConfig));
};
if (IS_APP_DEV_SERVER()) {
await validateDevServerConfig(DEFAULT_CONFIG.outputLocation, DEFAULT_CONFIG.devserverTimeout);
}
if (HAS_API) {
await validateDevServerConfig(SWA_CLI_API_URI() as string, DEFAULT_CONFIG.devserverTimeout);
await validateFunctionTriggers(SWA_CLI_API_URI() as string);
}
const server = createServer();
server.listen(Number(SWA_CLI_PORT), hostnameToIpAdress(DEFAULT_CONFIG.host), onServerStart(server, socketConnection));
server.listen(Number(SWA_CLI_PORT), localIpAdress);
})();