@dr.pogodin/react-utils 1.25.5 → 1.26.0

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