jimpex 7.0.1 → 8.0.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 (388) hide show
  1. package/CHANGELOG.md +87 -0
  2. package/LICENSE +1 -1
  3. package/README.md +446 -434
  4. package/dist/app/index.d.ts +13 -0
  5. package/dist/app/index.js +19 -0
  6. package/dist/app/index.js.map +1 -0
  7. package/dist/app/jimpex.d.ts +13 -0
  8. package/dist/app/jimpex.js +440 -0
  9. package/dist/app/jimpex.js.map +1 -0
  10. package/dist/controllers/common/config.d.ts +78 -0
  11. package/dist/controllers/common/config.js +89 -0
  12. package/dist/controllers/common/config.js.map +1 -0
  13. package/dist/controllers/common/health.d.ts +82 -0
  14. package/dist/controllers/common/health.js +97 -0
  15. package/dist/controllers/common/health.js.map +1 -0
  16. package/dist/controllers/common/index.d.ts +21 -0
  17. package/dist/controllers/common/index.js +21 -0
  18. package/dist/controllers/common/index.js.map +1 -0
  19. package/dist/controllers/common/statics.d.ts +215 -0
  20. package/dist/controllers/common/statics.js +202 -0
  21. package/dist/controllers/common/statics.js.map +1 -0
  22. package/dist/controllers/index.d.ts +24 -0
  23. package/dist/controllers/index.js +20 -0
  24. package/dist/controllers/index.js.map +1 -0
  25. package/dist/controllers/utils/gateway.d.ts +724 -0
  26. package/dist/controllers/utils/gateway.js +425 -0
  27. package/dist/controllers/utils/gateway.js.map +1 -0
  28. package/dist/controllers/utils/index.d.ts +16 -0
  29. package/dist/controllers/utils/index.js +19 -0
  30. package/dist/controllers/utils/index.js.map +1 -0
  31. package/dist/esm/app/index.js +2 -0
  32. package/dist/esm/app/index.js.map +1 -0
  33. package/dist/esm/app/jimpex.js +415 -0
  34. package/dist/esm/app/jimpex.js.map +1 -0
  35. package/dist/esm/chunk-T2T6Q22Z.js +11 -0
  36. package/dist/esm/chunk-T2T6Q22Z.js.map +1 -0
  37. package/dist/esm/controllers/common/config.js +62 -0
  38. package/dist/esm/controllers/common/config.js.map +1 -0
  39. package/dist/esm/controllers/common/health.js +70 -0
  40. package/dist/esm/controllers/common/health.js.map +1 -0
  41. package/dist/esm/controllers/common/index.js +4 -0
  42. package/dist/esm/controllers/common/index.js.map +1 -0
  43. package/dist/esm/controllers/common/statics.js +173 -0
  44. package/dist/esm/controllers/common/statics.js.map +1 -0
  45. package/dist/esm/controllers/index.js +3 -0
  46. package/dist/esm/controllers/index.js.map +1 -0
  47. package/dist/esm/controllers/utils/gateway.js +404 -0
  48. package/dist/esm/controllers/utils/gateway.js.map +1 -0
  49. package/dist/esm/controllers/utils/index.js +2 -0
  50. package/dist/esm/controllers/utils/index.js.map +1 -0
  51. package/dist/esm/index.js +7 -0
  52. package/dist/esm/index.js.map +1 -0
  53. package/dist/esm/middlewares/common/errorHandler.js +92 -0
  54. package/dist/esm/middlewares/common/errorHandler.js.map +1 -0
  55. package/dist/esm/middlewares/common/forceHTTPS.js +41 -0
  56. package/dist/esm/middlewares/common/forceHTTPS.js.map +1 -0
  57. package/dist/esm/middlewares/common/hsts.js +53 -0
  58. package/dist/esm/middlewares/common/hsts.js.map +1 -0
  59. package/dist/esm/middlewares/common/index.js +4 -0
  60. package/dist/esm/middlewares/common/index.js.map +1 -0
  61. package/dist/esm/middlewares/html/fastHTML.js +104 -0
  62. package/dist/esm/middlewares/html/fastHTML.js.map +1 -0
  63. package/dist/esm/middlewares/html/index.js +3 -0
  64. package/dist/esm/middlewares/html/index.js.map +1 -0
  65. package/dist/esm/middlewares/html/showHTML.js +68 -0
  66. package/dist/esm/middlewares/html/showHTML.js.map +1 -0
  67. package/dist/esm/middlewares/index.js +4 -0
  68. package/dist/esm/middlewares/index.js.map +1 -0
  69. package/dist/esm/middlewares/utils/index.js +2 -0
  70. package/dist/esm/middlewares/utils/index.js.map +1 -0
  71. package/dist/esm/middlewares/utils/versionValidator.js +101 -0
  72. package/dist/esm/middlewares/utils/versionValidator.js.map +1 -0
  73. package/dist/esm/services/common/appError.js +52 -0
  74. package/dist/esm/services/common/appError.js.map +1 -0
  75. package/dist/esm/services/common/httpError.js +19 -0
  76. package/dist/esm/services/common/httpError.js.map +1 -0
  77. package/dist/esm/services/common/index.js +17 -0
  78. package/dist/esm/services/common/index.js.map +1 -0
  79. package/dist/esm/services/common/sendFile.js +27 -0
  80. package/dist/esm/services/common/sendFile.js.map +1 -0
  81. package/dist/esm/services/frontend/frontendFs.js +38 -0
  82. package/dist/esm/services/frontend/frontendFs.js.map +1 -0
  83. package/dist/esm/services/frontend/index.js +11 -0
  84. package/dist/esm/services/frontend/index.js.map +1 -0
  85. package/dist/esm/services/html/htmlGenerator.js +144 -0
  86. package/dist/esm/services/html/htmlGenerator.js.map +1 -0
  87. package/dist/esm/services/html/index.js +11 -0
  88. package/dist/esm/services/html/index.js.map +1 -0
  89. package/dist/esm/services/http/apiClient.js +71 -0
  90. package/dist/esm/services/http/apiClient.js.map +1 -0
  91. package/dist/esm/services/http/http.js +125 -0
  92. package/dist/esm/services/http/http.js.map +1 -0
  93. package/dist/esm/services/http/index.js +17 -0
  94. package/dist/esm/services/http/index.js.map +1 -0
  95. package/dist/esm/services/http/responsesBuilder.js +105 -0
  96. package/dist/esm/services/http/responsesBuilder.js.map +1 -0
  97. package/dist/esm/services/index.js +6 -0
  98. package/dist/esm/services/index.js.map +1 -0
  99. package/dist/esm/services/utils/ensureBearerToken.js +78 -0
  100. package/dist/esm/services/utils/ensureBearerToken.js.map +1 -0
  101. package/dist/esm/services/utils/index.js +11 -0
  102. package/dist/esm/services/utils/index.js.map +1 -0
  103. package/dist/esm/types/events.js +1 -0
  104. package/dist/esm/types/events.js.map +1 -0
  105. package/dist/esm/types/express.js +1 -0
  106. package/dist/esm/types/express.js.map +1 -0
  107. package/dist/esm/types/http.js +1 -0
  108. package/dist/esm/types/http.js.map +1 -0
  109. package/dist/esm/types/index.js +7 -0
  110. package/dist/esm/types/index.js.map +1 -0
  111. package/dist/esm/types/options.js +1 -0
  112. package/dist/esm/types/options.js.map +1 -0
  113. package/dist/esm/types/utils.js +1 -0
  114. package/dist/esm/types/utils.js.map +1 -0
  115. package/dist/esm/types/wootils.js +1 -0
  116. package/dist/esm/types/wootils.js.map +1 -0
  117. package/dist/esm/utils/fns/index.js +5 -0
  118. package/dist/esm/utils/fns/index.js.map +1 -0
  119. package/dist/esm/utils/fns/others.js +6 -0
  120. package/dist/esm/utils/fns/others.js.map +1 -0
  121. package/dist/esm/utils/fns/routes.js +25 -0
  122. package/dist/esm/utils/fns/routes.js.map +1 -0
  123. package/dist/esm/utils/fns/statuses.js +6 -0
  124. package/dist/esm/utils/fns/statuses.js.map +1 -0
  125. package/dist/esm/utils/fns/text.js +6 -0
  126. package/dist/esm/utils/fns/text.js.map +1 -0
  127. package/dist/esm/utils/index.js +3 -0
  128. package/dist/esm/utils/index.js.map +1 -0
  129. package/dist/esm/utils/wrappers.js +41 -0
  130. package/dist/esm/utils/wrappers.js.map +1 -0
  131. package/dist/index.d.ts +46 -0
  132. package/dist/index.js +24 -0
  133. package/dist/index.js.map +1 -0
  134. package/dist/jimpex-7eaee271.d.ts +1278 -0
  135. package/dist/middlewares/common/errorHandler.d.ts +131 -0
  136. package/dist/middlewares/common/errorHandler.js +119 -0
  137. package/dist/middlewares/common/errorHandler.js.map +1 -0
  138. package/dist/middlewares/common/forceHTTPS.d.ts +68 -0
  139. package/dist/middlewares/common/forceHTTPS.js +68 -0
  140. package/dist/middlewares/common/forceHTTPS.js.map +1 -0
  141. package/dist/middlewares/common/hsts.d.ts +109 -0
  142. package/dist/middlewares/common/hsts.js +80 -0
  143. package/dist/middlewares/common/hsts.js.map +1 -0
  144. package/dist/middlewares/common/index.d.ts +21 -0
  145. package/dist/middlewares/common/index.js +21 -0
  146. package/dist/middlewares/common/index.js.map +1 -0
  147. package/dist/middlewares/html/fastHTML.d.ts +180 -0
  148. package/dist/middlewares/html/fastHTML.js +131 -0
  149. package/dist/middlewares/html/fastHTML.js.map +1 -0
  150. package/dist/middlewares/html/index.d.ts +21 -0
  151. package/dist/middlewares/html/index.js +20 -0
  152. package/dist/middlewares/html/index.js.map +1 -0
  153. package/dist/middlewares/html/showHTML.d.ts +127 -0
  154. package/dist/middlewares/html/showHTML.js +95 -0
  155. package/dist/middlewares/html/showHTML.js.map +1 -0
  156. package/dist/middlewares/index.d.ts +30 -0
  157. package/dist/middlewares/index.js +21 -0
  158. package/dist/middlewares/index.js.map +1 -0
  159. package/dist/middlewares/utils/index.d.ts +19 -0
  160. package/dist/middlewares/utils/index.js +19 -0
  161. package/dist/middlewares/utils/index.js.map +1 -0
  162. package/dist/middlewares/utils/versionValidator.d.ts +214 -0
  163. package/dist/middlewares/utils/versionValidator.js +128 -0
  164. package/dist/middlewares/utils/versionValidator.js.map +1 -0
  165. package/dist/services/common/appError.d.ts +138 -0
  166. package/dist/services/common/appError.js +80 -0
  167. package/dist/services/common/appError.js.map +1 -0
  168. package/dist/services/common/httpError.d.ts +79 -0
  169. package/dist/services/common/httpError.js +44 -0
  170. package/dist/services/common/httpError.js.map +1 -0
  171. package/dist/services/common/index.d.ts +47 -0
  172. package/dist/services/common/index.js +41 -0
  173. package/dist/services/common/index.js.map +1 -0
  174. package/dist/services/common/sendFile.d.ts +102 -0
  175. package/dist/services/common/sendFile.js +51 -0
  176. package/dist/services/common/sendFile.js.map +1 -0
  177. package/dist/services/frontend/frontendFs.d.ts +96 -0
  178. package/dist/services/frontend/frontendFs.js +71 -0
  179. package/dist/services/frontend/frontendFs.js.map +1 -0
  180. package/dist/services/frontend/index.d.ts +40 -0
  181. package/dist/services/frontend/index.js +35 -0
  182. package/dist/services/frontend/index.js.map +1 -0
  183. package/dist/services/html/htmlGenerator.d.ts +237 -0
  184. package/dist/services/html/htmlGenerator.js +171 -0
  185. package/dist/services/html/htmlGenerator.js.map +1 -0
  186. package/dist/services/html/index.d.ts +43 -0
  187. package/dist/services/html/index.js +35 -0
  188. package/dist/services/html/index.js.map +1 -0
  189. package/dist/services/http/apiClient.d.ts +169 -0
  190. package/dist/services/http/apiClient.js +96 -0
  191. package/dist/services/http/apiClient.js.map +1 -0
  192. package/dist/services/http/http.d.ts +175 -0
  193. package/dist/services/http/http.js +158 -0
  194. package/dist/services/http/http.js.map +1 -0
  195. package/dist/services/http/index.d.ts +50 -0
  196. package/dist/services/http/index.js +41 -0
  197. package/dist/services/http/index.js.map +1 -0
  198. package/dist/services/http/responsesBuilder.d.ts +178 -0
  199. package/dist/services/http/responsesBuilder.js +132 -0
  200. package/dist/services/http/responsesBuilder.js.map +1 -0
  201. package/dist/services/index.d.ts +33 -0
  202. package/dist/services/index.js +23 -0
  203. package/dist/services/index.js.map +1 -0
  204. package/dist/services/utils/ensureBearerToken.d.ts +157 -0
  205. package/dist/services/utils/ensureBearerToken.js +105 -0
  206. package/dist/services/utils/ensureBearerToken.js.map +1 -0
  207. package/dist/services/utils/index.d.ts +43 -0
  208. package/dist/services/utils/index.js +35 -0
  209. package/dist/services/utils/index.js.map +1 -0
  210. package/dist/types/events.d.ts +13 -0
  211. package/dist/types/events.js +17 -0
  212. package/dist/types/events.js.map +1 -0
  213. package/dist/types/express.d.ts +10 -0
  214. package/dist/types/express.js +17 -0
  215. package/dist/types/express.js.map +1 -0
  216. package/dist/types/http.d.ts +79 -0
  217. package/dist/types/http.js +17 -0
  218. package/dist/types/http.js.map +1 -0
  219. package/dist/types/index.d.ts +14 -0
  220. package/dist/types/index.js +24 -0
  221. package/dist/types/index.js.map +1 -0
  222. package/dist/types/options.d.ts +13 -0
  223. package/dist/types/options.js +17 -0
  224. package/dist/types/options.js.map +1 -0
  225. package/dist/types/utils.d.ts +48 -0
  226. package/dist/types/utils.js +17 -0
  227. package/dist/types/utils.js.map +1 -0
  228. package/dist/types/wootils.d.ts +4 -0
  229. package/dist/types/wootils.js +17 -0
  230. package/dist/types/wootils.js.map +1 -0
  231. package/dist/utils/fns/index.d.ts +5 -0
  232. package/dist/utils/fns/index.js +22 -0
  233. package/dist/utils/fns/index.js.map +1 -0
  234. package/dist/utils/fns/others.d.ts +17 -0
  235. package/dist/utils/fns/others.js +29 -0
  236. package/dist/utils/fns/others.js.map +1 -0
  237. package/dist/utils/fns/routes.d.ts +39 -0
  238. package/dist/utils/fns/routes.js +51 -0
  239. package/dist/utils/fns/routes.js.map +1 -0
  240. package/dist/utils/fns/statuses.d.ts +45 -0
  241. package/dist/utils/fns/statuses.js +35 -0
  242. package/dist/utils/fns/statuses.js.map +1 -0
  243. package/dist/utils/fns/text.d.ts +9 -0
  244. package/dist/utils/fns/text.js +29 -0
  245. package/dist/utils/fns/text.js.map +1 -0
  246. package/dist/utils/index.d.ts +18 -0
  247. package/dist/utils/index.js +20 -0
  248. package/dist/utils/index.js.map +1 -0
  249. package/dist/utils/wrappers.d.ts +13 -0
  250. package/dist/utils/wrappers.js +68 -0
  251. package/dist/utils/wrappers.js.map +1 -0
  252. package/examples/basic/404.html +12 -0
  253. package/examples/basic/README.md +30 -0
  254. package/examples/basic/app.ts +18 -0
  255. package/examples/basic/controller.ts +45 -0
  256. package/examples/basic/index.ts +19 -0
  257. package/examples/basic/middleware.ts +13 -0
  258. package/examples/basic/service.ts +11 -0
  259. package/package.json +59 -38
  260. package/src/app/index.ts +1 -0
  261. package/src/app/jimpex.ts +743 -0
  262. package/src/controllers/common/config.ts +115 -0
  263. package/src/controllers/common/health.ts +128 -0
  264. package/src/controllers/common/index.ts +3 -0
  265. package/src/controllers/common/statics.ts +380 -0
  266. package/src/controllers/index.ts +2 -0
  267. package/src/controllers/utils/gateway.ts +1186 -0
  268. package/src/controllers/utils/index.ts +1 -0
  269. package/src/index.ts +6 -0
  270. package/src/middlewares/common/errorHandler.ts +203 -0
  271. package/src/middlewares/common/forceHTTPS.ts +83 -0
  272. package/src/middlewares/common/hsts.ts +135 -0
  273. package/src/middlewares/common/index.ts +3 -0
  274. package/src/middlewares/html/fastHTML.ts +255 -0
  275. package/src/middlewares/html/index.ts +2 -0
  276. package/src/middlewares/html/showHTML.ts +165 -0
  277. package/src/middlewares/index.ts +3 -0
  278. package/src/middlewares/utils/index.ts +1 -0
  279. package/src/middlewares/utils/versionValidator.ts +289 -0
  280. package/src/services/common/appError.ts +158 -0
  281. package/src/services/common/httpError.ts +74 -0
  282. package/src/services/common/index.ts +29 -0
  283. package/src/services/common/sendFile.ts +106 -0
  284. package/src/services/frontend/frontendFs.ts +101 -0
  285. package/src/services/frontend/index.ts +21 -0
  286. package/src/services/html/htmlGenerator.ts +356 -0
  287. package/src/services/html/index.ts +21 -0
  288. package/src/services/http/apiClient.ts +221 -0
  289. package/src/services/http/http.ts +286 -0
  290. package/src/services/http/index.ts +29 -0
  291. package/src/services/http/responsesBuilder.ts +265 -0
  292. package/src/services/index.ts +5 -0
  293. package/src/services/utils/ensureBearerToken.ts +202 -0
  294. package/src/services/utils/index.ts +21 -0
  295. package/src/types/events.ts +303 -0
  296. package/src/types/express.ts +21 -0
  297. package/src/types/http.ts +77 -0
  298. package/src/types/index.ts +6 -0
  299. package/src/types/options.ts +248 -0
  300. package/src/types/utils.ts +52 -0
  301. package/src/types/wootils.ts +4 -0
  302. package/src/utils/fns/index.ts +4 -0
  303. package/src/utils/fns/others.ts +15 -0
  304. package/src/utils/fns/routes.ts +64 -0
  305. package/src/utils/fns/statuses.ts +44 -0
  306. package/src/utils/fns/text.ts +8 -0
  307. package/src/utils/index.ts +2 -0
  308. package/src/utils/wrappers.ts +537 -0
  309. package/tsconfig.json +5 -8
  310. package/tsup.config.ts +10 -0
  311. package/src/app/index.js +0 -692
  312. package/src/constants/eventNames.js +0 -48
  313. package/src/constants/index.js +0 -7
  314. package/src/controllers/common/configuration.js +0 -116
  315. package/src/controllers/common/health.js +0 -79
  316. package/src/controllers/common/index.js +0 -7
  317. package/src/controllers/common/statics.js +0 -336
  318. package/src/controllers/index.js +0 -9
  319. package/src/controllers/utils/gateway.js +0 -1039
  320. package/src/controllers/utils/index.js +0 -3
  321. package/src/index.js +0 -30
  322. package/src/middlewares/common/errorHandler.js +0 -185
  323. package/src/middlewares/common/forceHTTPS.js +0 -80
  324. package/src/middlewares/common/hsts.js +0 -122
  325. package/src/middlewares/common/index.js +0 -7
  326. package/src/middlewares/html/fastHTML.js +0 -298
  327. package/src/middlewares/html/index.js +0 -5
  328. package/src/middlewares/html/showHTML.js +0 -167
  329. package/src/middlewares/index.js +0 -11
  330. package/src/middlewares/utils/index.js +0 -3
  331. package/src/middlewares/utils/versionValidator.js +0 -261
  332. package/src/services/common/appError.js +0 -136
  333. package/src/services/common/httpError.js +0 -60
  334. package/src/services/common/index.js +0 -25
  335. package/src/services/common/sendFile.js +0 -68
  336. package/src/services/frontend/frontendFs.js +0 -85
  337. package/src/services/frontend/index.js +0 -17
  338. package/src/services/html/htmlGenerator.js +0 -391
  339. package/src/services/html/index.js +0 -17
  340. package/src/services/http/apiClient.js +0 -148
  341. package/src/services/http/http.js +0 -256
  342. package/src/services/http/index.js +0 -25
  343. package/src/services/http/responsesBuilder.js +0 -193
  344. package/src/services/index.js +0 -15
  345. package/src/services/utils/ensureBearerToken.js +0 -147
  346. package/src/services/utils/index.js +0 -19
  347. package/src/types.js +0 -377
  348. package/src/utils/functions.js +0 -78
  349. package/src/utils/wrappers.js +0 -131
  350. package/types/app/index.d.ts +0 -417
  351. package/types/constants/eventNames.d.ts +0 -93
  352. package/types/constants/index.d.ts +0 -2
  353. package/types/controllers/common/configuration.d.ts +0 -71
  354. package/types/controllers/common/health.d.ts +0 -55
  355. package/types/controllers/common/index.d.ts +0 -4
  356. package/types/controllers/common/statics.d.ts +0 -271
  357. package/types/controllers/index.d.ts +0 -3
  358. package/types/controllers/utils/gateway.d.ts +0 -947
  359. package/types/controllers/utils/index.d.ts +0 -2
  360. package/types/index.d.ts +0 -15
  361. package/types/middlewares/common/errorHandler.d.ts +0 -143
  362. package/types/middlewares/common/forceHTTPS.d.ts +0 -64
  363. package/types/middlewares/common/hsts.d.ts +0 -111
  364. package/types/middlewares/common/index.d.ts +0 -4
  365. package/types/middlewares/html/fastHTML.d.ts +0 -238
  366. package/types/middlewares/html/index.d.ts +0 -3
  367. package/types/middlewares/html/showHTML.d.ts +0 -128
  368. package/types/middlewares/index.d.ts +0 -4
  369. package/types/middlewares/utils/index.d.ts +0 -2
  370. package/types/middlewares/utils/versionValidator.d.ts +0 -247
  371. package/types/services/common/appError.d.ts +0 -89
  372. package/types/services/common/httpError.d.ts +0 -37
  373. package/types/services/common/index.d.ts +0 -18
  374. package/types/services/common/sendFile.d.ts +0 -56
  375. package/types/services/frontend/frontendFs.d.ts +0 -72
  376. package/types/services/frontend/index.d.ts +0 -3
  377. package/types/services/html/htmlGenerator.d.ts +0 -298
  378. package/types/services/html/index.d.ts +0 -3
  379. package/types/services/http/apiClient.d.ts +0 -141
  380. package/types/services/http/http.d.ts +0 -159
  381. package/types/services/http/index.d.ts +0 -18
  382. package/types/services/http/responsesBuilder.d.ts +0 -140
  383. package/types/services/index.d.ts +0 -6
  384. package/types/services/utils/ensureBearerToken.d.ts +0 -137
  385. package/types/services/utils/index.d.ts +0 -16
  386. package/types/types.d.ts +0 -280
  387. package/types/utils/functions.d.ts +0 -55
  388. package/types/utils/wrappers.d.ts +0 -127
package/tsup.config.ts ADDED
@@ -0,0 +1,10 @@
1
+ import { defineConfig } from 'tsup';
2
+
3
+ export default defineConfig({
4
+ sourcemap: true,
5
+ clean: true,
6
+ format: ['esm', 'cjs'],
7
+ legacyOutput: true,
8
+ dts: true,
9
+ bundle: false,
10
+ });
package/src/app/index.js DELETED
@@ -1,692 +0,0 @@
1
- const path = require('path');
2
- const https = require('https');
3
- const Jimple = require('jimple');
4
- const ObjectUtils = require('wootils/shared/objectUtils');
5
- const express = require('express');
6
- const bodyParser = require('body-parser');
7
- const compression = require('compression');
8
- const fs = require('fs-extra');
9
- const multer = require('multer');
10
- const statuses = require('statuses');
11
- const spdy = require('spdy');
12
-
13
- /**
14
- * @typedef {import('../types').Express} Express
15
- * @typedef {import('../types').SpdyServer} SpdyServer
16
- * @typedef {import('../types').Provider} Provider
17
- * @typedef {import('../types').Controller} Controller
18
- * @typedef {import('../types').ControllerProvider} ControllerProvider
19
- * @typedef {import('../types').MiddlewareLike} MiddlewareLike
20
- * @typedef {import('../types').Server} Server
21
- * @typedef {import('../types').JimpexStartCallback} JimpexStartCallback
22
- * @typedef {import('../types').JimpexOptions} JimpexOptions
23
- */
24
-
25
- /**
26
- * @module core
27
- */
28
-
29
- const {
30
- appConfiguration,
31
- appLogger,
32
- environmentUtils,
33
- packageInfo,
34
- pathUtils,
35
- rootRequire,
36
- } = require('wootils/node/providers');
37
- const { EventsHub, proxyContainer } = require('wootils/shared');
38
-
39
- const { eventNames } = require('../constants');
40
- const commonServices = require('../services/common');
41
- const httpServices = require('../services/http');
42
- const utilsServices = require('../services/utils');
43
- /**
44
- * Jimpex is a mix of Jimple, a Javascript port of Pimple dependency injection container,
45
- * and Express, one of the most popular web frameworks for Node.
46
- *
47
- * @augments Jimple
48
- * @parent module:core
49
- * @todo Implement `helmet`.
50
- */
51
- class Jimpex extends Jimple {
52
- /**
53
- * @param {Partial<JimpexOptions>} [options={}]
54
- * Preferences to customize the application.
55
- * @param {?Object} [configuration=null]
56
- * The default configuration for the `appConfiguration` service.
57
- */
58
- constructor(options = {}, configuration = null) {
59
- super();
60
- /**
61
- * The application options.
62
- *
63
- * @type {JimpexOptions}
64
- * @access protected
65
- * @ignore
66
- */
67
- this._options = ObjectUtils.merge(
68
- {
69
- version: '0.0.0',
70
- filesizeLimit: '15MB',
71
- boot: true,
72
- proxy: false,
73
- configuration: {
74
- default: configuration,
75
- name: 'app',
76
- path: 'config/',
77
- hasFolder: true,
78
- environmentVariable: 'CONFIG',
79
- loadFromEnvironment: true,
80
- loadVersionFromConfiguration: true,
81
- filenameFormat: '[app-name].[configuration-name].config.js',
82
- },
83
- statics: {
84
- enabled: true,
85
- onHome: false,
86
- route: 'statics',
87
- folder: '',
88
- },
89
- express: {
90
- trustProxy: true,
91
- disableXPoweredBy: true,
92
- compression: true,
93
- bodyParser: true,
94
- multer: true,
95
- },
96
- defaultServices: {
97
- common: true,
98
- http: true,
99
- utils: true,
100
- },
101
- },
102
- options,
103
- this._initOptions(),
104
- );
105
- /**
106
- * If the `proxy` option was set to `true`, this property will have a reference for a
107
- * proxy of the container, in which resources can be registered and obtained using dot
108
- * notation.
109
- *
110
- * @type {?Proxy<Jimpex>}
111
- * @access protected
112
- * @ignore
113
- */
114
- this._proxy = this._options.proxy ? proxyContainer(this) : null;
115
- /**
116
- * The Express application Jimpex uses under the hood.
117
- *
118
- * @type {Express}
119
- * @access protected
120
- * @ignore
121
- */
122
- this._express = express();
123
- /**
124
- * When Jimpex is used with HTTP2 enabled, this property will be used to store the
125
- * "patched"
126
- * version of Express that uses Spdy.
127
- *
128
- * @type {?SpdyServer}
129
- * @access protected
130
- * @ignore
131
- */
132
- this._spdy = null;
133
- /**
134
- * When the application starts, this will be the instance of the server.
135
- *
136
- * @type {?Server}
137
- * @access protected
138
- * @ignore
139
- */
140
- this._instance = null;
141
- /**
142
- * A list of functions that return controllers and middlewares. When the application
143
- * starts,
144
- * the queue will be processed and those controllers and middlewares will be added to
145
- * the server instance The reason they are not added directly like with a regular
146
- * Express implementation is that services on Jimple use lazy loading, and adding
147
- * middlewares and controllers as they come could cause errors if they depend on
148
- * services that are not yet registered.
149
- *
150
- * @type {Function[]}
151
- * @access protected
152
- * @ignore
153
- */
154
- this._mountQueue = [];
155
- /**
156
- * A list with all the top routes controlled by the application. Every time a
157
- * controller is mounted, its route will be added here.
158
- *
159
- * @type {string[]}
160
- * @access protected
161
- * @ignore
162
- */
163
- this._controlledRoutes = [];
164
-
165
- this._setupCoreServices();
166
- this._setupExpress();
167
- this._setupDefaultServices();
168
- this._setupConfiguration();
169
-
170
- this._init();
171
- if (this._options.boot) {
172
- this.boot();
173
- }
174
- }
175
- /**
176
- * This is where the app would register all its specific services, middlewares and controllers.
177
- */
178
- boot() {}
179
- /**
180
- * Disables the server TLS validation.
181
- */
182
- disableTLSValidation() {
183
- // eslint-disable-next-line no-process-env
184
- process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
185
- this.get('appLogger').warning('TLS validation has been disabled');
186
- }
187
- /**
188
- * This is an alias of `start`. The idea is for it to be used on serverless platforms,
189
- * where you don't get to start your app, you just have export it.
190
- *
191
- * @param {?number} [port=null] In case the configuration doesn't already
192
- * have it,
193
- * this is the port where the application
194
- * will use to run. If this parameter is
195
- * used, the method will overwrite the `port`
196
- * setting on the configuration service.
197
- * @param {?JimpexStartCallback} [fn=null] A callback function to be called when the
198
- * server starts.
199
- * @returns {Server} The server instance.
200
- */
201
- listen(port = null, fn = null) {
202
- if (port) {
203
- const config = this.get('appConfiguration');
204
- config.set('port', port);
205
- }
206
-
207
- return this.start(fn);
208
- }
209
- /**
210
- * Mounts a controller on a specific route.
211
- *
212
- * @param {string} route
213
- * The route for the controller.
214
- * @param {Controller | ControllerProvider} controller
215
- * The route controller.
216
- */
217
- mount(route, controller) {
218
- const ref = this.ref();
219
- const useController =
220
- typeof controller.register === 'function'
221
- ? controller.register(ref, route)
222
- : controller;
223
- this._mountQueue.push((server) => {
224
- let result;
225
- const routes = this._reduceWithEvent(
226
- 'controllerWillBeMounted',
227
- useController.connect(ref, route),
228
- route,
229
- useController,
230
- );
231
- if (Array.isArray(routes)) {
232
- // If the returned value is a list of routes, mount each single route.
233
- result = routes.forEach((routeRouter) => server.use(route, routeRouter));
234
- } else {
235
- // But if the returned value is not a list, it may be a router, so mount it directly.
236
- result = server.use(route, routes);
237
- }
238
-
239
- this._controlledRoutes.push(route);
240
- this._emitEvent('routeAdded', route);
241
- return result;
242
- });
243
- }
244
- /**
245
- * Gets "safe" reference for the container that validates if the "proxy mode" is
246
- * enabled, in order to provide the proxy or the actual instance.
247
- *
248
- * @returns {Jimpex}
249
- */
250
- ref() {
251
- return this._proxy || this;
252
- }
253
- /**
254
- * Registers a provider to extend the application.
255
- *
256
- * @param {Provider} provider The provider to register.
257
- */
258
- register(provider) {
259
- provider.register(this.ref());
260
- }
261
- /**
262
- * Starts the app server.
263
- *
264
- * @param {?JimpexStartCallback} [fn=null] A callback function to be called when the
265
- * server starts.
266
- * @returns {Object} The server instance.
267
- */
268
- start(fn = null) {
269
- const config = this.get('appConfiguration');
270
- const port = config.get('port');
271
- this._emitEvent('beforeStart');
272
- this._server = this._getServer();
273
- this._instance = this._server.listen(port, () => {
274
- this._emitEvent('start');
275
- this._mountResources();
276
- this.get('appLogger').success(`Starting on port ${port}`);
277
- this._emitEvent('afterStart');
278
- if (fn) {
279
- fn(config);
280
- }
281
- this._emitEvent('afterStartCallback');
282
- });
283
-
284
- return this._instance;
285
- }
286
- /**
287
- * Stops the server instance.
288
- */
289
- stop() {
290
- if (this._instance) {
291
- this._emitEvent('beforeStop');
292
- this._instance.close();
293
- this._instance = null;
294
- this._emitEvent('afterStop');
295
- }
296
- }
297
- /**
298
- * Tries to access a resource on the container, but if is not present, it won't throw an
299
- * error,
300
- * it will just return `null`.
301
- *
302
- * @param {string} name The name of the resource.
303
- * @returns {*}
304
- * @throws {Error} If there's an error other than the one generated when the resource
305
- * doesn't exist.
306
- */
307
- try(name) {
308
- return this.has(name) ? this.get(name) : null;
309
- }
310
- /**
311
- * Adds a middleware.
312
- *
313
- * @param {MiddlewareLike} middleware The middleware to use.
314
- */
315
- use(middleware) {
316
- const ref = this.ref();
317
- const useMiddleware =
318
- typeof middleware.register === 'function' ? middleware.register(ref) : middleware;
319
- this._mountQueue.push((server) => {
320
- if (typeof useMiddleware.connect === 'function') {
321
- // If the middleware is from Jimpex, connect it and then use it.
322
- const middlewareHandler = useMiddleware.connect(ref);
323
- if (middlewareHandler) {
324
- server.use(
325
- this._reduceWithEvent('middlewareWillBeUsed', middlewareHandler, middleware),
326
- );
327
- }
328
- } else {
329
- // But if the middleware is a regular middleware, just use it directly.
330
- server.use(this._reduceWithEvent('middlewareWillBeUsed', useMiddleware, null));
331
- }
332
- });
333
- }
334
- /**
335
- * The Express app Jimpex uses under the hood.
336
- *
337
- * @type {Express}
338
- */
339
- get express() {
340
- return this._express;
341
- }
342
- /**
343
- * The server instance that gets created when the app is started.
344
- *
345
- * @returns {?Server}
346
- */
347
- get instance() {
348
- return this._instance;
349
- }
350
- /**
351
- * The app options.
352
- *
353
- * @type {JimpexOptions}
354
- */
355
- get options() {
356
- return ObjectUtils.copy(this._options);
357
- }
358
- /**
359
- * A list of all the top routes controlled by the app.
360
- *
361
- * @type {string[]}
362
- */
363
- get routes() {
364
- return ObjectUtils.copy(this._controlledRoutes);
365
- }
366
- /**
367
- * Helper method to add static folders to the app.
368
- *
369
- * @param {string} route The route for the static folder.
370
- * @param {string} [folder=''] The path to the folder. If not defined, it will use
371
- * the value from `route`.
372
- * @param {boolean} [onHome=false] If `true`, the path to the folder will be relative
373
- * to where the app is being executed
374
- * (`process.cwd()`), otherwise, it will be relative to
375
- * where the executable file is located.
376
- * @access protected
377
- * @ignore
378
- */
379
- _addStaticsFolder(route, folder = '', onHome = false) {
380
- const joinFrom = onHome ? 'home' : 'app';
381
- const staticRoute = route.replace(/^\/+/, '');
382
- const staticFolder = this.get('pathUtils').joinFrom(joinFrom, folder || staticRoute);
383
- this._express.use(`/${staticRoute}`, express.static(staticFolder));
384
- }
385
- /**
386
- * Emits an app event with a reference to this class instance.
387
- *
388
- * @param {string} name The name of the event on {@link JimpexEvents}.
389
- * @param {...*} args Extra parameters for the listeners.
390
- * @access protected
391
- * @ignore
392
- */
393
- _emitEvent(name, ...args) {
394
- this.get('events').emit(eventNames[name], ...[...args, this]);
395
- }
396
- /**
397
- * Validates the configuration and chooses the server the application needs to use: If
398
- * HTTP2 is enabled, it will use Spdy; if HTTP is enabled but HTTP is not, it will use
399
- * the native HTTPS server; otherwise, it will be just the Express instance.
400
- *
401
- * @returns {Server}
402
- * @throws {Error} If HTTP2 is enabled but HTTPS is not.
403
- * @throws {Error} If HTTPS is enabled but there's no `https.credentials` object.
404
- * @throws {Error} If HTTPS is enabled and no creadentials are found.
405
- * @access protected
406
- * @ignore
407
- */
408
- _getServer() {
409
- let [http2Config, httpsConfig] = this.get('appConfiguration').get(
410
- ['http2', 'https'],
411
- true,
412
- );
413
-
414
- // Just in case any of those settings are `null` - which overwrites destructuring.
415
- http2Config = http2Config || {};
416
- httpsConfig = httpsConfig || {};
417
-
418
- let result;
419
- if (!http2Config.enabled && !httpsConfig.enabled) {
420
- result = this._express;
421
- } else {
422
- if (http2Config.enabled && !httpsConfig.enabled) {
423
- throw new Error('HTTP2 requires for HTTPS to be enabled');
424
- } else if (!httpsConfig.credentials) {
425
- throw new Error('The `credentials` object on the HTTPS settings is missing');
426
- }
427
-
428
- const credentials = this._loadCredentials(
429
- httpsConfig.credentials.onHome,
430
- httpsConfig.credentials,
431
- );
432
-
433
- if (!Object.keys(credentials).length) {
434
- throw new Error('No credentials were found for HTTPS');
435
- }
436
-
437
- if (http2Config.enabled) {
438
- if (http2Config.spdy) {
439
- credentials.spdy = http2Config.spdy;
440
- }
441
-
442
- result = spdy.createServer(credentials, this._express);
443
- } else {
444
- result = https.createServer(credentials, this._express);
445
- }
446
- }
447
-
448
- return result;
449
- }
450
- /**
451
- * This method is like a "lifecycle method", it gets executed on the constructor right
452
- * before the "boot step". The idea is for the method to be a helper when application is
453
- * defined by subclassing {@link Jimpex}: the application could register all important
454
- * services here and the routes on boot, then, if the implementation needs to access or
455
- * overwrite a something, it can send `boot: false`, access/register what it needs and
456
- * then call `boot()`. That would be impossible for an application without overwriting
457
- * the constructor and the boot functionality.
458
- *
459
- * @access protected
460
- */
461
- _init() {}
462
- /**
463
- * It generates overwrites for the class options when they are created. This method is a
464
- * helper for when the application is defined by subclassing {@link Jimpex}: It's highly
465
- * probable that if the application needs to change the default options, it would want
466
- * to do it right from the class, instead of having to do it on every implementation. A
467
- * way to do it would be overwriting the constructor and calling `super` with the custom
468
- * overwrites; this method exists so that won't be necessary: when creating the
469
- * `options`, the constructor will merge the result of this method on top of the default
470
- * ones.
471
- *
472
- * @returns {Partial<JimpexOptions>}
473
- * @access protected
474
- */
475
- _initOptions() {
476
- return {};
477
- }
478
- /**
479
- * Loads the contents of a dictionary of files that need to be used for HTTPS
480
- * credentials.
481
- *
482
- * @param {boolean} onHome If this is `true`, the path of the
483
- * files will be relative to the
484
- * project root directory;
485
- * otherwise, it will be relative to
486
- * the directory where the application
487
- * executable is located.
488
- * @param {Object.<string, string>} credentialsInfo The dictionary where the keys are
489
- * the type of credentials (`ca`,
490
- * `cert` and/or `key`) and the values
491
- * the paths to the files.
492
- * @returns {Object.<string, string>}
493
- * @access protected
494
- * @ignore
495
- */
496
- _loadCredentials(onHome, credentialsInfo) {
497
- const location = onHome === false ? 'app' : 'home';
498
- const usePathUtils = this.get('pathUtils');
499
- return ['ca', 'cert', 'key']
500
- .filter((key) => typeof credentialsInfo[key] === 'string')
501
- .reduce(
502
- (acc, key) => ({
503
- ...acc,
504
- [key]: fs.readFileSync(usePathUtils.joinFrom(location, credentialsInfo[key])),
505
- }),
506
- {},
507
- );
508
- }
509
- /**
510
- * Processes and mount all the resources on the `mountQueue`.
511
- *
512
- * @access protected
513
- * @ignore
514
- */
515
- _mountResources() {
516
- this._mountQueue.forEach((mountFn) => mountFn(this._express));
517
- this._mountQueue.length = 0;
518
- }
519
- /**
520
- * Sends a target object to a list of reducer events so they can modify or replace it.
521
- * This method also sends a reference to this class instance as the last parameter of
522
- * the event.
523
- *
524
- * @param {string} name The name of the event on {@link JimpexEvents}.
525
- * @param {*} target The targe object to reduce.
526
- * @param {...*} args Extra parameters for the listeners.
527
- * @returns {*} An object of the same type as the `target`.
528
- * @access protected
529
- * @ignore
530
- */
531
- _reduceWithEvent(name, target, ...args) {
532
- return this.get('events').reduce(eventNames[name], target, ...[...args, this]);
533
- }
534
- /**
535
- * Creates the configuration service.
536
- *
537
- * @access protected
538
- * @ignore
539
- */
540
- _setupConfiguration() {
541
- const { version, configuration: options } = this._options;
542
- const { name, environmentVariable, loadVersionFromConfiguration } = options;
543
- let configsPath = options.path;
544
- if (options.hasFolder) {
545
- configsPath += `${options.name}/`;
546
- }
547
- const filenameFormat = options.filenameFormat
548
- .replace(/\[app-name\]/gi, name)
549
- .replace(/\[configuration-name\]/gi, '[name]');
550
-
551
- let defaultConfig = {};
552
- if (options.default) {
553
- defaultConfig = options.default;
554
- } else {
555
- const defaultConfigPath = path.join(configsPath, `${options.name}.config.js`);
556
- defaultConfig = this.get('rootRequire')(defaultConfigPath);
557
- }
558
-
559
- if (!loadVersionFromConfiguration) {
560
- defaultConfig = { version, ...defaultConfig };
561
- }
562
-
563
- this.register(
564
- appConfiguration({
565
- appName: name,
566
- defaultConfiguration: defaultConfig,
567
- options: {
568
- environmentVariable,
569
- path: configsPath,
570
- filenameFormat,
571
- },
572
- }),
573
- );
574
-
575
- if (options.loadFromEnvironment) {
576
- this.get('appConfiguration').loadFromEnvironment();
577
- }
578
-
579
- if (loadVersionFromConfiguration) {
580
- this._options.version = this.get('appConfiguration').get('version');
581
- }
582
- }
583
- /**
584
- * Registers the _'core services'_.
585
- *
586
- * @access protected
587
- * @ignore
588
- */
589
- _setupCoreServices() {
590
- // The logger service.
591
- this.register(appLogger);
592
- // The service that reads the environment variables.
593
- this.register(environmentUtils);
594
- // The app `package.json` information.
595
- this.register(packageInfo);
596
- // The service to build paths relative to the project root directory.
597
- this.register(pathUtils);
598
- // The service to make `require`s relatives to the project root directory.
599
- this.register(rootRequire);
600
- }
601
- /**
602
- * Based on the constructor received options, register or not the default services.
603
- *
604
- * @access protected
605
- * @ignore
606
- */
607
- _setupDefaultServices() {
608
- const { defaultServices } = this._options;
609
-
610
- if (defaultServices.common) {
611
- this.register(commonServices);
612
- }
613
-
614
- if (defaultServices.http) {
615
- this.register(httpServices);
616
- }
617
-
618
- if (defaultServices.utils) {
619
- this.register(utilsServices);
620
- }
621
-
622
- this.set('events', () => new EventsHub());
623
- /**
624
- * This package is heavily used when implementing a Jimpex app, so it makes sense to register
625
- * it on the container, so the implementations won't need to manually install it.
626
- */
627
- this.set('statuses', () => statuses);
628
- }
629
- /**
630
- * Creates and configure the Express instance.
631
- *
632
- * @access protected
633
- * @ignore
634
- */
635
- _setupExpress() {
636
- const { statics, filesizeLimit, express: expressOptions } = this._options;
637
- if (expressOptions.trustProxy) {
638
- this._express.enable('trust proxy');
639
- }
640
-
641
- if (expressOptions.disableXPoweredBy) {
642
- this._express.disable('x-powered-by');
643
- }
644
-
645
- if (expressOptions.compression) {
646
- this._express.use(compression());
647
- }
648
-
649
- if (statics.enabled) {
650
- this._addStaticsFolder(statics.route, statics.folder, statics.onHome);
651
- }
652
-
653
- if (expressOptions.bodyParser) {
654
- this._express.use(
655
- bodyParser.json({
656
- limit: filesizeLimit,
657
- }),
658
- );
659
- this._express.use(
660
- bodyParser.urlencoded({
661
- extended: true,
662
- limit: filesizeLimit,
663
- }),
664
- );
665
- }
666
-
667
- if (expressOptions.multer) {
668
- this._express.use(multer().any());
669
- }
670
-
671
- this.set(
672
- 'router',
673
- this.factory(() => express.Router()),
674
- );
675
- }
676
- }
677
- /**
678
- * Creates a new instance of {@link Jimpex}.
679
- *
680
- * @param {Partial<JimpexOptions>} [options={}]
681
- * Preferences to customize the application.
682
- * @param {?Object} [configuration=null]
683
- * The default configuration for the `appConfiguration` service.
684
- * @returns {Jimpex}
685
- */
686
- const jimpex = (options = {}, configuration = null) => {
687
- const app = new Jimpex(options, configuration || {});
688
- return app.ref();
689
- };
690
-
691
- module.exports.Jimpex = Jimpex;
692
- module.exports.jimpex = jimpex;