forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssr-ssl-plugin.ts
More file actions
46 lines (40 loc) · 1.29 KB
/
ssr-ssl-plugin.ts
File metadata and controls
46 lines (40 loc) · 1.29 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
/**
* @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 { rootCertificates } from 'node:tls';
import type { Plugin } from 'vite';
export function createAngularServerSideSSLPlugin(): Plugin {
return {
name: 'angular-ssr-ssl-plugin',
apply: 'serve',
async configureServer({ config, httpServer }) {
const {
ssr,
server: { https },
} = config;
if (!ssr || !https?.cert) {
return;
}
// TODO(alanagius): Replace `undici` with `tls.setDefaultCACertificates` once we only support Node.js 22.18.0+ and 24.5.0+.
// See: https://nodejs.org/api/tls.html#tlssetdefaultcacertificatescerts
const { getGlobalDispatcher, setGlobalDispatcher, Agent } = await import('undici');
const originalDispatcher = getGlobalDispatcher();
const { cert } = https;
const certificates = Array.isArray(cert) ? cert : [cert];
setGlobalDispatcher(
new Agent({
connect: {
ca: [...rootCertificates, ...certificates],
},
}),
);
httpServer?.on('close', () => {
setGlobalDispatcher(originalDispatcher);
});
},
};
}