@dr.pogodin/react-utils 1.47.0-alpha.5 → 1.47.0-alpha.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (211) hide show
  1. package/build/development/client/getInj.js +11 -0
  2. package/build/development/client/getInj.js.map +1 -1
  3. package/build/development/client/index.js +1 -1
  4. package/build/development/client/index.js.map +1 -1
  5. package/build/development/index.js +1 -11
  6. package/build/development/index.js.map +1 -1
  7. package/build/development/server/index.js +3 -6
  8. package/build/development/server/index.js.map +1 -1
  9. package/build/development/server/renderer.js +20 -0
  10. package/build/development/server/renderer.js.map +1 -1
  11. package/build/development/server/server.js +6 -0
  12. package/build/development/server/server.js.map +1 -1
  13. package/build/development/server/utils/index.js +1 -1
  14. package/build/development/server/utils/index.js.map +1 -1
  15. package/build/development/shared/components/Button/index.js +1 -1
  16. package/build/development/shared/components/Button/index.js.map +1 -1
  17. package/build/development/shared/components/Checkbox/index.js +1 -1
  18. package/build/development/shared/components/Checkbox/index.js.map +1 -1
  19. package/build/development/shared/components/Input/index.js +1 -1
  20. package/build/development/shared/components/Input/index.js.map +1 -1
  21. package/build/development/shared/components/Modal/index.js +1 -1
  22. package/build/development/shared/components/Modal/index.js.map +1 -1
  23. package/build/development/shared/components/PageLayout/index.js +1 -1
  24. package/build/development/shared/components/PageLayout/index.js.map +1 -1
  25. package/build/development/shared/components/TextArea/index.js +1 -1
  26. package/build/development/shared/components/TextArea/index.js.map +1 -1
  27. package/build/development/shared/components/Throbber/index.js +1 -1
  28. package/build/development/shared/components/Throbber/index.js.map +1 -1
  29. package/build/development/shared/components/WithTooltip/index.js +1 -1
  30. package/build/development/shared/components/WithTooltip/index.js.map +1 -1
  31. package/build/development/shared/components/YouTubeVideo/index.js +1 -1
  32. package/build/development/shared/components/YouTubeVideo/index.js.map +1 -1
  33. package/build/development/shared/components/selectors/CustomDropdown/Options/index.js +6 -0
  34. package/build/development/shared/components/selectors/CustomDropdown/Options/index.js.map +1 -1
  35. package/build/development/shared/components/selectors/CustomDropdown/index.js +5 -1
  36. package/build/development/shared/components/selectors/CustomDropdown/index.js.map +1 -1
  37. package/build/development/shared/components/selectors/NativeDropdown/index.js +7 -1
  38. package/build/development/shared/components/selectors/NativeDropdown/index.js.map +1 -1
  39. package/build/development/shared/components/selectors/Switch/index.js +3 -1
  40. package/build/development/shared/components/selectors/Switch/index.js.map +1 -1
  41. package/build/development/shared/utils/jest/E2eSsrEnv.js +4 -0
  42. package/build/development/shared/utils/jest/E2eSsrEnv.js.map +1 -1
  43. package/build/development/shared/utils/splitComponent.js +18 -0
  44. package/build/development/shared/utils/splitComponent.js.map +1 -1
  45. package/build/development/shared/utils/webpack.js +4 -0
  46. package/build/development/shared/utils/webpack.js.map +1 -1
  47. package/build/production/client/getInj.js +1 -1
  48. package/build/production/client/getInj.js.map +1 -1
  49. package/build/production/client/index.js +1 -1
  50. package/build/production/client/index.js.map +1 -1
  51. package/build/production/index.js +2 -6
  52. package/build/production/index.js.map +1 -1
  53. package/build/production/server/index.js +3 -3
  54. package/build/production/server/index.js.map +1 -1
  55. package/build/production/server/renderer.js +1 -1
  56. package/build/production/server/renderer.js.map +1 -1
  57. package/build/production/server/server.js +1 -1
  58. package/build/production/server/server.js.map +1 -1
  59. package/build/production/server/utils/index.js +1 -1
  60. package/build/production/server/utils/index.js.map +1 -1
  61. package/build/production/shared/components/Button/index.js +1 -1
  62. package/build/production/shared/components/Button/index.js.map +1 -1
  63. package/build/production/shared/components/Checkbox/index.js +1 -1
  64. package/build/production/shared/components/Checkbox/index.js.map +1 -1
  65. package/build/production/shared/components/Input/index.js +1 -1
  66. package/build/production/shared/components/Input/index.js.map +1 -1
  67. package/build/production/shared/components/Modal/index.js +1 -1
  68. package/build/production/shared/components/Modal/index.js.map +1 -1
  69. package/build/production/shared/components/PageLayout/index.js +1 -1
  70. package/build/production/shared/components/PageLayout/index.js.map +1 -1
  71. package/build/production/shared/components/TextArea/index.js +1 -1
  72. package/build/production/shared/components/TextArea/index.js.map +1 -1
  73. package/build/production/shared/components/Throbber/index.js +1 -1
  74. package/build/production/shared/components/Throbber/index.js.map +1 -1
  75. package/build/production/shared/components/WithTooltip/index.js +1 -1
  76. package/build/production/shared/components/WithTooltip/index.js.map +1 -1
  77. package/build/production/shared/components/YouTubeVideo/index.js +1 -1
  78. package/build/production/shared/components/YouTubeVideo/index.js.map +1 -1
  79. package/build/production/shared/components/selectors/CustomDropdown/Options/index.js +1 -1
  80. package/build/production/shared/components/selectors/CustomDropdown/Options/index.js.map +1 -1
  81. package/build/production/shared/components/selectors/CustomDropdown/index.js +2 -2
  82. package/build/production/shared/components/selectors/CustomDropdown/index.js.map +1 -1
  83. package/build/production/shared/components/selectors/NativeDropdown/index.js +2 -2
  84. package/build/production/shared/components/selectors/NativeDropdown/index.js.map +1 -1
  85. package/build/production/shared/components/selectors/Switch/index.js +1 -1
  86. package/build/production/shared/components/selectors/Switch/index.js.map +1 -1
  87. package/build/production/shared/utils/jest/E2eSsrEnv.js +1 -1
  88. package/build/production/shared/utils/jest/E2eSsrEnv.js.map +1 -1
  89. package/build/production/shared/utils/splitComponent.js +1 -1
  90. package/build/production/shared/utils/splitComponent.js.map +1 -1
  91. package/build/production/shared/utils/webpack.js +1 -1
  92. package/build/production/shared/utils/webpack.js.map +1 -1
  93. package/build/types-code/client/index.d.ts +1 -1
  94. package/build/types-code/index.d.ts +0 -5
  95. package/build/types-code/server/index.d.ts +4 -9
  96. package/build/types-code/server/utils/index.d.ts +1 -1
  97. package/build/web/client/getInj.js +7 -0
  98. package/build/web/client/getInj.js.map +1 -1
  99. package/build/web/client/index.js +1 -1
  100. package/build/web/client/index.js.map +1 -1
  101. package/build/web/index.js +1 -11
  102. package/build/web/index.js.map +1 -1
  103. package/build/web/server/index.js +3 -6
  104. package/build/web/server/index.js.map +1 -1
  105. package/build/web/server/renderer.js +10 -0
  106. package/build/web/server/renderer.js.map +1 -1
  107. package/build/web/server/server.js +3 -0
  108. package/build/web/server/server.js.map +1 -1
  109. package/build/web/server/utils/index.js +1 -1
  110. package/build/web/server/utils/index.js.map +1 -1
  111. package/build/web/shared/components/Button/index.js +1 -1
  112. package/build/web/shared/components/Button/index.js.map +1 -1
  113. package/build/web/shared/components/Checkbox/index.js +1 -1
  114. package/build/web/shared/components/Checkbox/index.js.map +1 -1
  115. package/build/web/shared/components/Input/index.js +1 -1
  116. package/build/web/shared/components/Input/index.js.map +1 -1
  117. package/build/web/shared/components/Modal/index.js +1 -1
  118. package/build/web/shared/components/Modal/index.js.map +1 -1
  119. package/build/web/shared/components/PageLayout/index.js +1 -1
  120. package/build/web/shared/components/PageLayout/index.js.map +1 -1
  121. package/build/web/shared/components/TextArea/index.js +1 -1
  122. package/build/web/shared/components/TextArea/index.js.map +1 -1
  123. package/build/web/shared/components/Throbber/index.js +1 -1
  124. package/build/web/shared/components/Throbber/index.js.map +1 -1
  125. package/build/web/shared/components/WithTooltip/index.js +1 -1
  126. package/build/web/shared/components/WithTooltip/index.js.map +1 -1
  127. package/build/web/shared/components/YouTubeVideo/index.js +1 -1
  128. package/build/web/shared/components/YouTubeVideo/index.js.map +1 -1
  129. package/build/web/shared/components/selectors/CustomDropdown/Options/index.js +3 -0
  130. package/build/web/shared/components/selectors/CustomDropdown/Options/index.js.map +1 -1
  131. package/build/web/shared/components/selectors/CustomDropdown/index.js +3 -1
  132. package/build/web/shared/components/selectors/CustomDropdown/index.js.map +1 -1
  133. package/build/web/shared/components/selectors/NativeDropdown/index.js +4 -1
  134. package/build/web/shared/components/selectors/NativeDropdown/index.js.map +1 -1
  135. package/build/web/shared/components/selectors/Switch/index.js +2 -1
  136. package/build/web/shared/components/selectors/Switch/index.js.map +1 -1
  137. package/build/web/shared/utils/jest/E2eSsrEnv.js +2 -0
  138. package/build/web/shared/utils/jest/E2eSsrEnv.js.map +1 -1
  139. package/build/web/shared/utils/splitComponent.js +9 -0
  140. package/build/web/shared/utils/splitComponent.js.map +1 -1
  141. package/build/web/shared/utils/webpack.js +2 -0
  142. package/build/web/shared/utils/webpack.js.map +1 -1
  143. package/config/babel/node-ssr.js +7 -1
  144. package/config/babel/webpack.js +3 -1
  145. package/config/webpack/app-base.js +4 -7
  146. package/config/webpack/app-development.js +2 -3
  147. package/package.json +20 -9
  148. package/src/client/getInj.ts +0 -71
  149. package/src/client/index.tsx +0 -45
  150. package/src/client/init.ts +0 -52
  151. package/src/index.ts +0 -75
  152. package/src/server/Cache.ts +0 -61
  153. package/src/server/index.ts +0 -243
  154. package/src/server/renderer.tsx +0 -635
  155. package/src/server/server.ts +0 -330
  156. package/src/server/utils/errors.ts +0 -122
  157. package/src/server/utils/index.ts +0 -3
  158. package/src/shared/components/Button/index.tsx +0 -130
  159. package/src/shared/components/Button/style.scss +0 -54
  160. package/src/shared/components/Checkbox/index.tsx +0 -53
  161. package/src/shared/components/Checkbox/theme.scss +0 -63
  162. package/src/shared/components/GenericLink/index.tsx +0 -141
  163. package/src/shared/components/GenericLink/style.scss +0 -3
  164. package/src/shared/components/Input/index.tsx +0 -96
  165. package/src/shared/components/Input/theme.scss +0 -52
  166. package/src/shared/components/Link.tsx +0 -25
  167. package/src/shared/components/Modal/base-theme.scss +0 -38
  168. package/src/shared/components/Modal/index.tsx +0 -177
  169. package/src/shared/components/Modal/styles.scss +0 -3
  170. package/src/shared/components/NavLink.tsx +0 -18
  171. package/src/shared/components/PageLayout/base-theme.scss +0 -30
  172. package/src/shared/components/PageLayout/index.tsx +0 -51
  173. package/src/shared/components/TextArea/index.tsx +0 -139
  174. package/src/shared/components/TextArea/style.scss +0 -68
  175. package/src/shared/components/Throbber/index.tsx +0 -28
  176. package/src/shared/components/Throbber/theme.scss +0 -26
  177. package/src/shared/components/WithTooltip/Tooltip.tsx +0 -274
  178. package/src/shared/components/WithTooltip/default-theme.scss +0 -36
  179. package/src/shared/components/WithTooltip/index.tsx +0 -186
  180. package/src/shared/components/YouTubeVideo/base.scss +0 -15
  181. package/src/shared/components/YouTubeVideo/index.tsx +0 -73
  182. package/src/shared/components/YouTubeVideo/throbber.scss +0 -11
  183. package/src/shared/components/index.ts +0 -20
  184. package/src/shared/components/selectors/CustomDropdown/Options/index.tsx +0 -124
  185. package/src/shared/components/selectors/CustomDropdown/Options/style.scss +0 -6
  186. package/src/shared/components/selectors/CustomDropdown/index.tsx +0 -146
  187. package/src/shared/components/selectors/CustomDropdown/theme.scss +0 -118
  188. package/src/shared/components/selectors/NativeDropdown/index.tsx +0 -90
  189. package/src/shared/components/selectors/NativeDropdown/theme.scss +0 -81
  190. package/src/shared/components/selectors/Switch/index.tsx +0 -74
  191. package/src/shared/components/selectors/Switch/theme.scss +0 -39
  192. package/src/shared/components/selectors/common.ts +0 -57
  193. package/src/shared/components/selectors/index.ts +0 -8
  194. package/src/shared/utils/config.ts +0 -59
  195. package/src/shared/utils/globalState.ts +0 -44
  196. package/src/shared/utils/index.ts +0 -45
  197. package/src/shared/utils/isomorphy/buildInfo.ts +0 -50
  198. package/src/shared/utils/isomorphy/environment-check.ts +0 -18
  199. package/src/shared/utils/isomorphy/index.ts +0 -36
  200. package/src/shared/utils/jest/E2eSsrEnv.ts +0 -304
  201. package/src/shared/utils/jest/global.ts +0 -17
  202. package/src/shared/utils/jest/index.ts +0 -215
  203. package/src/shared/utils/splitComponent.tsx +0 -284
  204. package/src/shared/utils/time.ts +0 -101
  205. package/src/shared/utils/webpack.ts +0 -80
  206. package/src/styles/_global/reset.css +0 -52
  207. package/src/styles/_mixins/fonts.scss +0 -40
  208. package/src/styles/_mixins/media.scss +0 -150
  209. package/src/styles/_mixins/typography.scss +0 -83
  210. package/src/styles/global.scss +0 -11
  211. package/src/styles/mixins.scss +0 -6
@@ -1,61 +0,0 @@
1
- type CachedItemT<DatumT> = {
2
- data: DatumT;
3
- size: number;
4
- timestamp: number;
5
- };
6
-
7
- /**
8
- * Implements the static cache.
9
- */
10
- export default class Cache<DatumT> {
11
- private items: Record<string, CachedItemT<DatumT>> = {};
12
-
13
- private size = 0;
14
-
15
- constructor(private maxSize: number) { }
16
-
17
- /**
18
- * Cache lookup.
19
- * @param key Item key to look for.
20
- * @param [maxage=Number.MAX_VALUE] Optional. The maximum age of
21
- * cached item to serve. Default to infinite.
22
- * @returns Cached item, or null if the item is absent in cache,
23
- * or stale.
24
- */
25
- get({
26
- key,
27
- maxage = Number.MAX_VALUE,
28
- }: {
29
- key: string;
30
- maxage?: number;
31
- }): DatumT | null {
32
- const item = this.items[key];
33
- return item && Date.now() - item.timestamp < maxage ? item.data : null;
34
- }
35
-
36
- /**
37
- * Adds item to cache.
38
- * @param data Item to add.
39
- * @param key Key to store the item at.
40
- * @param size Byte size of the item.
41
- */
42
- add(data: DatumT, key: string, size: number): void {
43
- const cached = this.items[key];
44
- if (cached) this.size -= cached.size;
45
-
46
- this.items[key] = { data, size, timestamp: Date.now() };
47
- this.size += size;
48
-
49
- if (this.size > this.maxSize) {
50
- const entries = Object.entries(this.items);
51
- entries.sort((a, b) => a[1].timestamp - b[1].timestamp);
52
-
53
- for (const entry of entries) {
54
- const [itemKey, item] = entry;
55
- delete this.items[itemKey];
56
- this.size -= item.size;
57
- if (this.size < this.maxSize / 2) break;
58
- }
59
- }
60
- }
61
- }
@@ -1,243 +0,0 @@
1
- // eslint-disable-next-line import/no-unassigned-import
2
- import 'source-map-support/register.js';
3
-
4
- import http from 'node:http';
5
- import https from 'node:https';
6
-
7
- import { cloneDeep, defaults } from 'lodash-es';
8
-
9
- // Polyfill required by ReactJS.
10
- // TODO: Double-check, if it is still required by React v19?
11
- // eslint-disable-next-line import/no-unassigned-import
12
- import 'raf/polyfill.js';
13
-
14
- import type { Configuration } from 'webpack';
15
-
16
- import serverFactory, {
17
- type OptionsT as ServerOptionsT,
18
- type ServerT,
19
- getDefaultCspSettings,
20
- } from './server';
21
-
22
- import { SCRIPT_LOCATIONS, newDefaultLogger } from './renderer';
23
-
24
- import { errors } from './utils';
25
-
26
- export type {
27
- BeforeRenderResT,
28
- BeforeRenderT,
29
- ConfigT,
30
- ServerSsrContext,
31
- } from './renderer';
32
-
33
- export { errors, getDefaultCspSettings, type ServerT };
34
-
35
- /**
36
- * Normalizes a port into a number, string, or false.
37
- * TODO: Drop this function?
38
- * @param value Port name or number.
39
- * @return Port number (Number), name (String).
40
- */
41
- function normalizePort(value: number | string) {
42
- const port = typeof value === 'string' ? parseInt(value) : value;
43
- if (Number.isFinite(port)) return port; /* port number */
44
- return value; /* named pipe */
45
- }
46
-
47
- type OptionsT = ServerOptionsT & {
48
- https?: {
49
- cert: string;
50
- key: string;
51
- };
52
-
53
- // TODO: Should we limit it to number | string, and throw if it is different value?
54
- port?: false | number | string;
55
- };
56
-
57
- /**
58
- * Creates and launches web-server for ReactJS application. Allows zero
59
- * or detailed configuration, supports server-side rendering,
60
- * and development tools, including Hot Module Reloading (HMR).
61
- *
62
- * NOTE: Many of options defined below are passed down to the server and
63
- * renderer factories, and their declared default values are set in those
64
- * factories, rather than here.
65
- *
66
- * @param {object} webpackConfig Webpack configuration used to build
67
- * the frontend bundle. In production mode the server will read out of it
68
- * `context`, `publicPath`, and a few other parameters, necessary to locate
69
- * and serve the app bundle. In development mode the server will use entire
70
- * provided config to build the app bundle in memory, and further watch and
71
- * update it via HMR.
72
- * @param {object} [options] Additional parameters.
73
- * @param {Component} [options.Application] The root ReactJS component of
74
- * the app to use for the server-side rendering. When not provided
75
- * the server-side rendering is disabled.
76
- * @param {function} [options.beforeExpressJsError] Asynchronous callback
77
- * (`(server) => Promise<boolean>`) to be executed just before the default error
78
- * handler is added to ExpressJS server. If the callback is provided and its
79
- * result resolves to a truthy value, `react-utils` won't attach the default
80
- * error handler.
81
- * @param {function} [options.beforeExpressJsSetup] Asynchronous callback
82
- * (`(server) => Promise) to be executed right after ExpressJS server creation,
83
- * before any configuration is performed.
84
- * @param {BeforeRenderHook} [options.beforeRender] The hook to run just before
85
- * the server-side rendering. For each incoming request, it will be executed
86
- * just before the HTML markup is generated at the server. It allows to load
87
- * and provide the data necessary for server-side rendering, and also to inject
88
- * additional configuration and scripts into the generated HTML code.
89
- * @param {boolean} [options.noCsp] Set `true` to disable
90
- * Content-Security-Policy (CSP) headers altogether.
91
- * @param {function} [options.cspSettingsHook] A hook allowing
92
- * to customize [CSP](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP)
93
- * settings for [helmet](https://github.com/helmetjs/helmet)'s
94
- * `contentSecurityPolicy` middleware on per-request basis.
95
- *
96
- * If provided it should be a with signature: \
97
- * `(defaultSettings: object, req: object)` &rArr; `object` \
98
- * which gets the default settings (also used without the hook),
99
- * and the incoming request object. The hook response will be passed
100
- * as options to the helmet `contentSecurityPolicy` middleware.
101
- *
102
- * Currently, the default settings is the following object in production
103
- * environment:
104
- * ```js
105
- * {
106
- * directives: {
107
- * defaultSrc: ["'self'"],
108
- * baseUri: ["'self'"],
109
- * blockAllMixedContent: [],
110
- * fontSrc: ["'self'", 'https:', 'data:'],
111
- * frameAncestors: ["'self'"],
112
- * frameSrc: ["'self'", 'https://*.youtube.com'],
113
- * imgSrc: ["'self'", 'data:'],
114
- * objectSrc: ["'none'"],
115
- * scriptSrc: ["'self'", "'unsafe-eval'", `'nonce-UNIQUE_NONCE_VALUE'`],
116
- * scriptSrcAttr: ["'none'"],
117
- * styleSrc: ["'self'", 'https:', "'unsafe-inline'"],
118
- * upgradeInsecureRequests: [] // Removed in dev mode.
119
- * }
120
- * }
121
- * ```
122
- * It matches the default value used by Helmet with a few updates:
123
- * - YouTube host is whitelisted in the `frameSrc` directive to ensure
124
- * the {@link YouTubeVideo} component works.
125
- * - An unique per-request nonce is added to `scriptSrc` directive to
126
- * whitelist auxiliary scripts injected by react-utils. The actual nonce
127
- * value can be fetched by host code via `.nonce` field of `req` argument
128
- * of `.beforeRender` hook.
129
- * - `upgradeInsecureRequests` directive is removed in development mode,
130
- * to simplify local testing with http requests.
131
- * @param {string} [options.defaultLoggerLogLevel='info'] Log level for
132
- * the default logger, which is created if no `logger` option provided.
133
- * @param {boolean} [options.devMode] Pass in `true` to start the server in
134
- * development mode.
135
- * @param {string} [options.favicon] Path to the favicon to use by the server.
136
- * By default no favicon is used.
137
- * @param {object} [options.https] If provided, HTTPS server will be started,
138
- * instead of HTTP otherwise. The object should provide SSL certificate and key
139
- * via two string fields: `cert`, and `key`.
140
- * @param {string} [options.https.cert] SSL Certificate.
141
- * @param {string} [options.https.key] SSL key.
142
- * @param {boolean} [options.httpsRedirect=true] Pass in `true` to enable
143
- * automatic redirection of all incoming HTTP requests to HTTPS.
144
- *
145
- * To smoothly use it at `localhost` you need to run the server in HTTPS mode,
146
- * and also properly create and install a self-signed SSL sertificate on your
147
- * system. This article is helpful:
148
- * [How to get HTTPS working on your local development environment in 5 minutes](https://medium.freecodecamp.org/how-to-get-https-working-on-your-local-development-environment-in-5-minutes-7af615770eec)
149
- * @param {Logger} [options.logger] The logger to use at server side.
150
- * By default [`winston`](https://www.npmjs.com/package/winston) logger
151
- * with console transport is used. The logger you provide, or the default
152
- * `winston` logger otherwise, will be attached to the created ExpressJS server
153
- * object.
154
- * @param {function} [options.onExpressJsSetup] An async callback
155
- * (`(server) => Promise`) to be triggered when most of the server
156
- * configuration is completed, just before the server-side renderer,
157
- * and the default error handler are attached. You can use it to mount
158
- * custom API routes. The server-side logger can be accessed as `server.logger`.
159
- * @param {number|string} [options.port=3000] The port to start the server on.
160
- * @param {number} [options.staticCacheSize=1.e7] The maximum
161
- * static cache size in bytes. Defaults to ~10 MB.
162
- * @param {function} [options.staticCacheController] When given, it activates,
163
- * and controls the static caching of generated HTML markup. When this function
164
- * is provided, on each incoming request it is triggered with the request
165
- * passed in as the argument. To attempt to serve the response from the cache
166
- * it should return the object with the following fields:
167
- * - `key: string` &ndash; the cache key for the response;
168
- * - `maxage?: number` &ndash; the maximum age of cached result in ms.
169
- * If undefined - infinite age is assumed.
170
- * @param {number} [options.maxSsrRounds=10] Maximum number of SSR rounds.
171
- * @param {number} [options.ssrTimeout=1000] SSR timeout in milliseconds,
172
- * defaults to 1 second.
173
- * @return Resolves to an object with created Express and HTTP servers.
174
- */
175
- export default async function launchServer(
176
- webpackConfig: Configuration,
177
- options: OptionsT = {},
178
- ): Promise<{
179
- expressServer: ServerT;
180
- httpServer: http.Server;
181
- }> {
182
- /* Options normalization. */
183
- const ops = cloneDeep(options);
184
-
185
- // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
186
- ops.port = normalizePort(ops.port || process.env.PORT || 3000);
187
- defaults(ops, { httpsRedirect: true });
188
-
189
- // TODO: Need a separate type for normalized options, which guarantees
190
- // the logger is set!
191
- ops.logger ??= newDefaultLogger({
192
- defaultLogLevel: ops.defaultLoggerLogLevel,
193
- });
194
-
195
- /* Creates servers, resolves and sets the port. */
196
- const expressServer = await serverFactory(webpackConfig, ops);
197
-
198
- let httpServer: http.Server;
199
- if (ops.https) {
200
- httpServer = https.createServer({
201
- cert: ops.https.cert,
202
- key: ops.https.key,
203
- }, expressServer as unknown as () => void);
204
- } else httpServer = http.createServer(expressServer as unknown as () => void);
205
-
206
- /* Sets error handler for HTTP(S) server. */
207
- httpServer.on('error', (error: Error) => {
208
- if ((error as { syscall?: string }).syscall !== 'listen') throw error;
209
- const bind = typeof ops.port === 'string' ? `Pipe ${ops.port}` : `Port ${ops.port}`;
210
-
211
- /* Human-readable message for some specific listen errors. */
212
- switch ((error as { code?: string }).code) {
213
- case 'EACCES':
214
- ops.logger!.error(`${bind} requires elevated privileges`);
215
- return process.exit(1);
216
- case 'EADDRINUSE':
217
- ops.logger!.error(`${bind} is already in use`);
218
- return process.exit(1);
219
- case undefined:
220
- default:
221
- throw error;
222
- }
223
- });
224
-
225
- /* Listening event handler for HTTP(S) server. */
226
- httpServer.on('listening', () => {
227
- const addr = httpServer.address()!;
228
- const bind = typeof addr === 'string' ? `pipe ${addr}` : `port ${addr.port}`;
229
- ops.logger!.info(`Server listening on ${bind} in ${
230
- process.env.NODE_ENV} mode`);
231
- });
232
-
233
- httpServer.listen(ops.port);
234
-
235
- return {
236
- expressServer,
237
- httpServer,
238
- };
239
- }
240
-
241
- launchServer.SCRIPT_LOCATIONS = SCRIPT_LOCATIONS;
242
- launchServer.getDefaultCspSettings = getDefaultCspSettings;
243
- launchServer.errors = errors;