options.js 5.9 KB

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