options.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.setUiPort = exports.setMiddleware = exports.fixSnippetIncludePaths = exports.fixSnippetIgnorePaths = exports.fixRewriteRules = exports.liftExtensionsOptionFromCli = exports.setServerOpts = exports.setNamespace = exports.setStartPath = exports.setScheme = exports.setMode = exports.setOpen = exports.setProxyWs = void 0;
  4. const _ = require("./lodash.custom");
  5. const Immutable = require("immutable");
  6. const defaultConfig = require("./default-config");
  7. /**
  8. * Move top-level ws options to proxy.ws
  9. * This is to allow it to be set from the CLI
  10. * @param incoming
  11. */
  12. function setProxyWs(incoming) {
  13. if (incoming.get("ws") && incoming.get("mode") === "proxy") {
  14. return [incoming.setIn(["proxy", "ws"], true), []];
  15. }
  16. return [incoming, []];
  17. }
  18. exports.setProxyWs = setProxyWs;
  19. /**
  20. * @param incoming
  21. */
  22. function setOpen(incoming) {
  23. return [
  24. incoming.update("open", function (open) {
  25. if (incoming.get("mode") === "snippet") {
  26. if (open !== "ui" && open !== "ui-external") {
  27. return false;
  28. }
  29. }
  30. return open;
  31. }),
  32. []
  33. ];
  34. }
  35. exports.setOpen = setOpen;
  36. /**
  37. * Set the running mode
  38. * @param incoming
  39. */
  40. function setMode(incoming) {
  41. const output = incoming.set("mode", (function () {
  42. if (incoming.get("server")) {
  43. return "server";
  44. }
  45. if (incoming.get("proxy")) {
  46. return "proxy";
  47. }
  48. return "snippet";
  49. })());
  50. return [output, []];
  51. }
  52. exports.setMode = setMode;
  53. /**
  54. * @param incoming
  55. */
  56. function setScheme(incoming) {
  57. var scheme = "http";
  58. if (incoming.getIn(["server", "https"])) {
  59. scheme = "https";
  60. }
  61. if (incoming.get("https")) {
  62. scheme = "https";
  63. }
  64. if (incoming.getIn(["proxy", "url", "protocol"])) {
  65. if (incoming.getIn(["proxy", "url", "protocol"]) === "https:") {
  66. scheme = "https";
  67. }
  68. }
  69. return [incoming.set("scheme", scheme), []];
  70. }
  71. exports.setScheme = setScheme;
  72. /**
  73. * @param incoming
  74. */
  75. function setStartPath(incoming) {
  76. if (incoming.get("proxy")) {
  77. var path = incoming.getIn(["proxy", "url", "path"]);
  78. if (path !== "/") {
  79. return [incoming.set("startPath", path), []];
  80. }
  81. }
  82. return [incoming, []];
  83. }
  84. exports.setStartPath = setStartPath;
  85. /**
  86. * @param incoming
  87. */
  88. function setNamespace(incoming) {
  89. var namespace = incoming.getIn(["socket", "namespace"]);
  90. if (_.isFunction(namespace)) {
  91. return [
  92. incoming.setIn(["socket", "namespace"], namespace(defaultConfig.socket.namespace)),
  93. []
  94. ];
  95. }
  96. return [incoming, []];
  97. }
  98. exports.setNamespace = setNamespace;
  99. /**
  100. * @param incoming
  101. */
  102. function setServerOpts(incoming) {
  103. if (!incoming.get("server")) {
  104. return [incoming, []];
  105. }
  106. var indexarg = incoming.getIn(["server", "index"]) || "index.html";
  107. var optPath = ["server", "serveStaticOptions"];
  108. if (!incoming.getIn(optPath)) {
  109. return [
  110. incoming.setIn(optPath, Immutable.Map({
  111. index: indexarg
  112. })),
  113. []
  114. ];
  115. }
  116. if (!incoming.hasIn(optPath.concat(["index"]))) {
  117. return [incoming.setIn(optPath.concat(["index"]), indexarg), []];
  118. }
  119. return [incoming, []];
  120. }
  121. exports.setServerOpts = setServerOpts;
  122. function liftExtensionsOptionFromCli(incoming) {
  123. // cli extensions
  124. var optPath = ["server", "serveStaticOptions"];
  125. if (incoming.get("extensions")) {
  126. return [
  127. incoming.setIn(optPath.concat(["extensions"]), incoming.get("extensions")),
  128. []
  129. ];
  130. }
  131. return [incoming, []];
  132. }
  133. exports.liftExtensionsOptionFromCli = liftExtensionsOptionFromCli;
  134. /**
  135. * Back-compat fixes for rewriteRules being set to a boolean
  136. */
  137. function fixRewriteRules(incoming) {
  138. return [
  139. incoming.update("rewriteRules", function (rr) {
  140. return Immutable.List([])
  141. .concat(rr)
  142. .filter(Boolean);
  143. }),
  144. []
  145. ];
  146. }
  147. exports.fixRewriteRules = fixRewriteRules;
  148. function fixSnippetIgnorePaths(incoming) {
  149. var ignorePaths = incoming.getIn(["snippetOptions", "ignorePaths"]);
  150. if (ignorePaths) {
  151. if (_.isString(ignorePaths)) {
  152. ignorePaths = [ignorePaths];
  153. }
  154. ignorePaths = ignorePaths.map(ensureSlash);
  155. return [
  156. incoming.setIn(["snippetOptions", "blacklist"], Immutable.List(ignorePaths)),
  157. []
  158. ];
  159. }
  160. return [incoming, []];
  161. }
  162. exports.fixSnippetIgnorePaths = fixSnippetIgnorePaths;
  163. function fixSnippetIncludePaths(incoming) {
  164. var includePaths = incoming.getIn(["snippetOptions", "whitelist"]);
  165. if (includePaths) {
  166. includePaths = includePaths.map(ensureSlash);
  167. return [
  168. incoming.setIn(["snippetOptions", "whitelist"], Immutable.List(includePaths)),
  169. []
  170. ];
  171. }
  172. return [incoming, []];
  173. }
  174. exports.fixSnippetIncludePaths = fixSnippetIncludePaths;
  175. /**
  176. * Enforce paths to begin with a forward slash
  177. */
  178. function ensureSlash(item) {
  179. if (item[0] !== "/") {
  180. return "/" + item;
  181. }
  182. return item;
  183. }
  184. /**
  185. *
  186. */
  187. function setMiddleware(incoming) {
  188. var mw = getMiddlwares(incoming);
  189. return [incoming.set("middleware", mw), []];
  190. }
  191. exports.setMiddleware = setMiddleware;
  192. /**
  193. * top-level option, or given as part of the proxy/server option
  194. * @param item
  195. * @returns {*}
  196. */
  197. function getMiddlwares(item) {
  198. var mw = item.get("middleware");
  199. var serverMw = item.getIn(["server", "middleware"]);
  200. var proxyMw = item.getIn(["proxy", "middleware"]);
  201. var list = Immutable.List([]);
  202. if (mw) {
  203. return listMerge(list, mw);
  204. }
  205. if (serverMw) {
  206. return listMerge(list, serverMw);
  207. }
  208. if (proxyMw) {
  209. return listMerge(list, proxyMw);
  210. }
  211. return list;
  212. }
  213. /**
  214. * @param item
  215. * @returns {*}
  216. */
  217. function isList(item) {
  218. return Immutable.List.isList(item);
  219. }
  220. /**
  221. * @param list
  222. * @param item
  223. * @returns {*}
  224. */
  225. function listMerge(list, item) {
  226. if (_.isFunction(item)) {
  227. list = list.push(item);
  228. }
  229. if (isList(item) && item.size) {
  230. list = list.merge(item);
  231. }
  232. return list;
  233. }
  234. /**
  235. * @param incoming
  236. * @returns {*}
  237. */
  238. function setUiPort(incoming) {
  239. if (incoming.get("uiPort")) {
  240. return [incoming.setIn(["ui", "port"], incoming.get("uiPort")), []];
  241. }
  242. return [incoming, []];
  243. }
  244. exports.setUiPort = setUiPort;
  245. //# sourceMappingURL=options.js.map