1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.givenHttpServerConfig = void 0;
|
8 | const tslib_1 = require("tslib");
|
9 | const assert_1 = tslib_1.__importDefault(require("assert"));
|
10 | const fs_1 = require("fs");
|
11 | const path_1 = tslib_1.__importDefault(require("path"));
|
12 | const FIXTURES = path_1.default.resolve(__dirname, '../fixtures');
|
13 | const DUMMY_TLS_CONFIG = {
|
14 | key: (0, fs_1.readFileSync)(path_1.default.join(FIXTURES, 'key.pem')),
|
15 | cert: (0, fs_1.readFileSync)(path_1.default.join(FIXTURES, 'cert.pem')),
|
16 | };
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | function assertHostPort(config) {
|
22 | (0, assert_1.default)(config.host != null, 'host is not set');
|
23 | (0, assert_1.default)(config.port != null, 'port is not set');
|
24 | }
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | function givenHttpServerConfig(customConfig) {
|
35 | const defaults = { host: '127.0.0.1', port: 0 };
|
36 | if (isHttpsConfig(customConfig)) {
|
37 | const config = { ...customConfig };
|
38 | if (config.host == null)
|
39 | config.host = defaults.host;
|
40 | if (config.port == null)
|
41 | config.port = defaults.port;
|
42 | setupTlsConfig(config);
|
43 | assertHostPort(config);
|
44 | return config;
|
45 | }
|
46 | assertHttpConfig(customConfig);
|
47 | const config = { ...customConfig };
|
48 | if (config.host == null)
|
49 | config.host = defaults.host;
|
50 | if (config.port == null)
|
51 | config.port = defaults.port;
|
52 | assertHostPort(config);
|
53 | return config;
|
54 | }
|
55 | exports.givenHttpServerConfig = givenHttpServerConfig;
|
56 | function setupTlsConfig(config) {
|
57 | if ('key' in config && 'cert' in config)
|
58 | return;
|
59 | if ('pfx' in config)
|
60 | return;
|
61 | Object.assign(config, DUMMY_TLS_CONFIG);
|
62 | }
|
63 |
|
64 |
|
65 |
|
66 | function isHttpsConfig(config) {
|
67 | return (config === null || config === void 0 ? void 0 : config.protocol) === 'https';
|
68 | }
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | function assertHttpConfig(config) {
|
74 | (0, assert_1.default)((config === null || config === void 0 ? void 0 : config.protocol) == null || (config === null || config === void 0 ? void 0 : config.protocol) === 'http');
|
75 | }
|
76 |
|
\ | No newline at end of file |