-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathindex.ts
402 lines (361 loc) · 11.4 KB
/
index.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
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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { loadProxyConfiguration } from '@angular/build/private';
import {
BuilderContext,
BuilderOutput,
createBuilder,
targetFromTargetString,
} from '@angular-devkit/architect';
import { json, logging, tags } from '@angular-devkit/core';
import type {
BrowserSyncInstance,
Options as BrowserSyncOptions,
HttpsOptions,
MiddlewareHandler,
ProxyOptions,
} from 'browser-sync';
import { join, resolve as pathResolve } from 'node:path';
import * as url from 'node:url';
import {
EMPTY,
Observable,
catchError,
combineLatest,
concatMap,
debounce,
debounceTime,
delay,
finalize,
from,
ignoreElements,
map,
of,
startWith,
switchMap,
tap,
zip,
} from 'rxjs';
import { Schema } from './schema';
import { getAvailablePort, spawnAsObservable, waitUntilServerIsListening } from './utils';
/** Log messages to ignore and not rely to the logger */
const IGNORED_STDOUT_MESSAGES = [
'server listening on',
'Angular is running in development mode. Call enableProdMode() to enable production mode.',
];
export type SSRDevServerBuilderOptions = Schema;
export type SSRDevServerBuilderOutput = BuilderOutput & {
baseUrl?: string;
port?: string;
};
export function execute(
options: SSRDevServerBuilderOptions,
context: BuilderContext,
): Observable<SSRDevServerBuilderOutput> {
let browserSync: typeof import('browser-sync');
try {
browserSync = require('browser-sync');
} catch {
return of({
success: false,
error:
// eslint-disable-next-line max-len
'Required dependency `browser-sync` is not installed, most likely you need to run `npm install browser-sync --save-dev` in your project.',
});
}
const bsInstance = browserSync.create();
const browserTarget = targetFromTargetString(options.browserTarget);
const serverTarget = targetFromTargetString(options.serverTarget);
const getBaseUrl = (bs: BrowserSyncInstance) =>
`${bs.getOption('scheme')}://${bs.getOption('host')}:${bs.getOption('port')}`;
const browserTargetRun = context.scheduleTarget(browserTarget, {
watch: options.watch,
progress: options.progress,
verbose: options.verbose,
// Disable bundle budgets are these are not meant to be used with a dev-server as this will add extra JavaScript for live-reloading.
budgets: [],
} as json.JsonObject);
const serverTargetRun = context.scheduleTarget(serverTarget, {
watch: options.watch,
progress: options.progress,
verbose: options.verbose,
} as json.JsonObject);
context.logger.error(tags.stripIndents`
****************************************************************************************
This is a simple server for use in testing or debugging Angular applications locally.
It hasn't been reviewed for security issues.
DON'T USE IT FOR PRODUCTION!
****************************************************************************************
`);
return zip(browserTargetRun, serverTargetRun, getAvailablePort()).pipe(
switchMap(([br, sr, nodeServerPort]) => {
return combineLatest([br.output, sr.output]).pipe(
// This is needed so that if both server and browser emit close to each other
// we only emit once. This typically happens on the first build.
debounceTime(120),
switchMap(([b, s]) => {
if (!s.success || !b.success) {
return of([b, s]);
}
return startNodeServer(s, nodeServerPort, context.logger, !!options.inspect).pipe(
map(() => [b, s]),
catchError((err) => {
context.logger.error(`A server error has occurred.\n${mapErrorToMessage(err)}`);
return EMPTY;
}),
);
}),
map(
([b, s]) =>
[
{
success: b.success && s.success,
error: b.error || s.error,
},
nodeServerPort,
] as [SSRDevServerBuilderOutput, number],
),
tap(([builderOutput]) => {
if (builderOutput.success) {
context.logger.info('\nCompiled successfully.');
}
}),
debounce(([builderOutput]) =>
builderOutput.success && !options.inspect
? waitUntilServerIsListening(nodeServerPort)
: EMPTY,
),
finalize(() => {
void br.stop();
void sr.stop();
}),
);
}),
concatMap(([builderOutput, nodeServerPort]) => {
if (!builderOutput.success) {
return of(builderOutput);
}
if (bsInstance.active) {
bsInstance.reload();
return of(builderOutput);
} else {
return from(initBrowserSync(bsInstance, nodeServerPort, options, context)).pipe(
tap((bs) => {
const baseUrl = getBaseUrl(bs);
context.logger.info(tags.oneLine`
**
Angular Universal Live Development Server is listening on ${baseUrl},
open your browser on ${baseUrl}
**
`);
}),
map(() => builderOutput),
);
}
}),
map(
(builderOutput) =>
({
success: builderOutput.success,
error: builderOutput.error,
baseUrl: getBaseUrl(bsInstance),
port: bsInstance.getOption('port'),
}) as SSRDevServerBuilderOutput,
),
finalize(() => {
if (bsInstance) {
bsInstance.exit();
bsInstance.cleanup();
}
}),
catchError((error) =>
of({
success: false,
error: mapErrorToMessage(error),
}),
),
);
}
// Logs output to the terminal.
// Removes any trailing new lines from the output.
export function log(
{ stderr, stdout }: { stderr: string | undefined; stdout: string | undefined },
logger: logging.LoggerApi,
) {
if (stderr) {
// Strip the webpack scheme (webpack://) from error log.
logger.error(stderr.replace(/\n?$/, '').replace(/webpack:\/\//g, '.'));
}
if (stdout && !IGNORED_STDOUT_MESSAGES.some((x) => stdout.includes(x))) {
logger.info(stdout.replace(/\n?$/, ''));
}
}
function startNodeServer(
serverOutput: BuilderOutput,
port: number,
logger: logging.LoggerApi,
inspectMode = false,
): Observable<void> {
const outputPath = serverOutput.outputPath as string;
const path = join(outputPath, 'main.js');
const env = { ...process.env, PORT: '' + port };
const args = ['--enable-source-maps', `"${path}"`];
if (inspectMode) {
args.unshift('--inspect-brk');
}
return of(null).pipe(
delay(0), // Avoid EADDRINUSE error since it will cause the kill event to be finish.
switchMap(() => spawnAsObservable('node', args, { env, shell: true })),
tap((res) => log({ stderr: res.stderr, stdout: res.stdout }, logger)),
ignoreElements(),
// Emit a signal after the process has been started
startWith(undefined),
);
}
async function initBrowserSync(
browserSyncInstance: BrowserSyncInstance,
nodeServerPort: number,
options: SSRDevServerBuilderOptions,
context: BuilderContext,
): Promise<BrowserSyncInstance> {
if (browserSyncInstance.active) {
return browserSyncInstance;
}
const { port: browserSyncPort, open, host, publicHost, proxyConfig } = options;
const bsPort = browserSyncPort || (await getAvailablePort());
const bsOptions: BrowserSyncOptions = {
proxy: {
target: `localhost:${nodeServerPort}`,
proxyOptions: {
xfwd: true,
},
proxyRes: [
(proxyRes) => {
if ('headers' in proxyRes) {
proxyRes.headers['cache-control'] = undefined;
}
},
],
// proxyOptions is not in the typings
} as ProxyOptions & { proxyOptions: { xfwd: boolean } },
host,
port: bsPort,
ui: false,
server: false,
notify: false,
ghostMode: false,
logLevel: options.verbose ? 'debug' : 'silent',
open,
https: getSslConfig(context.workspaceRoot, options),
};
const publicHostNormalized =
publicHost && publicHost.endsWith('/')
? publicHost.substring(0, publicHost.length - 1)
: publicHost;
if (publicHostNormalized) {
const { protocol, hostname, port, pathname } = url.parse(publicHostNormalized);
const defaultSocketIoPath = '/browser-sync/socket.io';
const defaultNamespace = '/browser-sync';
const hasPathname = !!(pathname && pathname !== '/');
const namespace = hasPathname ? pathname + defaultNamespace : defaultNamespace;
const path = hasPathname ? pathname + defaultSocketIoPath : defaultSocketIoPath;
bsOptions.socket = {
namespace,
path,
domain: url.format({
protocol,
hostname,
port,
}),
};
// When having a pathname we also need to create a reverse proxy because socket.io
// will be listening on: 'http://localhost:4200/ssr/browser-sync/socket.io'
// However users will typically have a reverse proxy that will redirect all matching requests
// ex: http://testinghost.com/ssr -> http://localhost:4200 which will result in a 404.
if (hasPathname) {
const { createProxyMiddleware } = await import('http-proxy-middleware');
// Remove leading slash
bsOptions.scriptPath = (p) => p.substring(1);
bsOptions.middleware = [
createProxyMiddleware({
pathFilter: defaultSocketIoPath,
target: url.format({
protocol: 'http',
hostname: host,
port: bsPort,
pathname: path,
}),
ws: true,
logger: {
info: () => {},
warn: () => {},
error: () => {},
},
}),
];
}
}
if (proxyConfig) {
if (!bsOptions.middleware) {
bsOptions.middleware = [];
} else if (!Array.isArray(bsOptions.middleware)) {
bsOptions.middleware = [bsOptions.middleware];
}
bsOptions.middleware = [
...bsOptions.middleware,
...(await getProxyConfig(context.workspaceRoot, proxyConfig)),
];
}
return new Promise((resolve, reject) => {
browserSyncInstance.init(bsOptions, (error, bs) => {
if (error) {
reject(error);
} else {
resolve(bs);
}
});
});
}
function mapErrorToMessage(error: unknown): string {
if (error instanceof Error) {
return error.message;
}
if (typeof error === 'string') {
return error;
}
return '';
}
function getSslConfig(
root: string,
options: SSRDevServerBuilderOptions,
): HttpsOptions | undefined | boolean {
const { ssl, sslCert, sslKey } = options;
if (ssl && sslCert && sslKey) {
return {
key: pathResolve(root, sslKey),
cert: pathResolve(root, sslCert),
};
}
return ssl;
}
async function getProxyConfig(root: string, proxyConfig: string): Promise<MiddlewareHandler[]> {
const proxy = await loadProxyConfiguration(root, proxyConfig);
if (!proxy) {
return [];
}
const { createProxyMiddleware } = await import('http-proxy-middleware');
return Object.entries(proxy).map(([key, context]) => {
const filterRegExp = new RegExp(key);
return createProxyMiddleware({
pathFilter: (pathname) => filterRegExp.test(pathname),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
...(context as any),
});
});
}
export default createBuilder<SSRDevServerBuilderOptions, BuilderOutput>(execute);