async.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. var Immutable = require("immutable");
  2. var url = require("url");
  3. module.exports = {
  4. /**
  5. * The UI uses it's own server/port
  6. * @param ui
  7. * @param done
  8. */
  9. findAFreePort: function (ui, done) {
  10. var port = ui.options.get("port");
  11. var listenHost = ui.options.get("listen", "localhost");
  12. ui.bs.utils.portscanner.findAPortNotInUse(port, port + 100, {
  13. host: listenHost,
  14. timeout: 1000
  15. }, function (err, port) {
  16. if (err) {
  17. return done(err);
  18. }
  19. done(null, {
  20. options: {
  21. port: port
  22. }
  23. });
  24. });
  25. },
  26. /**
  27. * Default hooks do things like creating/joining JS files &
  28. * building angular config
  29. * @param ui
  30. * @param done
  31. */
  32. initDefaultHooks: function (ui, done) {
  33. var out = ui.pluginManager.hook("page", ui);
  34. done(null, {
  35. instance: {
  36. clientJs: ui.pluginManager.hook("client:js", ui),
  37. templates: ui.pluginManager.hook("templates", ui.getInitialTemplates(), ui),
  38. pagesConfig: out.pagesConfig,
  39. pages: out.pagesObj,
  40. pageMarkup: out.pageMarkup
  41. }
  42. });
  43. },
  44. setBsOptions: function (ui, done) {
  45. done(null, {
  46. options: {
  47. bs: Immutable.Map({
  48. mode: ui.bs.options.get("mode"),
  49. port: ui.bs.options.get("port")
  50. })
  51. }
  52. });
  53. },
  54. /**
  55. * @param ui
  56. * @param done
  57. */
  58. setUrlOptions: function (ui, done) {
  59. var port = ui.options.get("port");
  60. var bsUrls = ui.bs.getOptionIn(["urls"]).toJS();
  61. var listenHost = ui.bs.options.get("listen", "localhost");
  62. var urls = {
  63. ui: "http://" + listenHost + ":" + port
  64. };
  65. if (bsUrls.external) {
  66. var externalListen = listenHost || url.parse(bsUrls.external).hostname;
  67. urls["ui-external"] = ["http://", externalListen, ":", port].join("");
  68. }
  69. done(null, {
  70. options: {
  71. urls: Immutable.fromJS(urls)
  72. }
  73. });
  74. },
  75. /**
  76. * Simple static file server with some middlewares for custom
  77. * scripts/routes.
  78. * @param ui
  79. * @param done
  80. */
  81. startServer: function (ui, done) {
  82. var bs = ui.bs;
  83. var port = ui.options.get("port");
  84. var listenHost = ui.options.get("listen");
  85. ui.logger.debug("Using port %s", port);
  86. var server = require("./server")(ui, {
  87. middleware: {
  88. socket: bs.getMiddleware("socket-js"),
  89. connector: bs.getSocketConnector(bs.options.get("port"), {
  90. path: bs.options.getIn(["socket", "path"]),
  91. namespace: ui.config.getIn(["socket", "namespace"])
  92. })
  93. }
  94. });
  95. require('server-destroy')(server.server);
  96. bs.registerCleanupTask(function () {
  97. if (server.server) {
  98. server.server.destroy();
  99. }
  100. if (ui.servers) {
  101. Object.keys(ui.servers).forEach(function (key) {
  102. if (ui.servers[key].server) {
  103. ui.servers[key].server.destroy();
  104. }
  105. });
  106. }
  107. });
  108. done(null, {
  109. instance: {
  110. server: server.server.listen(port, listenHost),
  111. app: server.app
  112. }
  113. });
  114. },
  115. /**
  116. * Allow an API for adding/removing elements to clients
  117. * @param ui
  118. * @param done
  119. */
  120. addElementEvents: function (ui, done) {
  121. var elems = ui.pluginManager.hook("elements");
  122. var bs = ui.bs;
  123. if (!Object.keys(elems).length) {
  124. return done();
  125. }
  126. ui.setOption("clientFiles", Immutable.fromJS(elems));
  127. done(null, {
  128. instance: {
  129. enableElement: require("./client-elements").enable(ui.clients, ui, bs),
  130. disableElement: require("./client-elements").disable(ui.clients, ui, bs),
  131. addElement: require("./client-elements").addElement
  132. }
  133. });
  134. },
  135. /**
  136. * Run default plugins
  137. * @param ui
  138. * @param done
  139. */
  140. registerPlugins: function (ui, done) {
  141. Object.keys(ui.defaultPlugins).forEach(function (key) {
  142. ui.pluginManager.get(key)(ui, ui.bs);
  143. });
  144. done();
  145. },
  146. /**
  147. * The most important event is the initial connection where
  148. * the options are received from the socket
  149. * @param ui
  150. * @param done
  151. */
  152. addOptionsEvent: function (ui, done) {
  153. var bs = ui.bs;
  154. ui.clients.on("connection", function (client) {
  155. client.emit("ui:connection", ui.options.toJS());
  156. ui.options.get("clientFiles").map(function (item) {
  157. if (item.get("active")) {
  158. ui.addElement(client, item.toJS());
  159. }
  160. });
  161. });
  162. ui.socket.on("connection", function (client) {
  163. client.emit("connection", bs.getOptions().toJS());
  164. client.emit("ui:connection", ui.options.toJS());
  165. client.on("ui:get:options", function () {
  166. client.emit("ui:receive:options", {
  167. bs: bs.getOptions().toJS(),
  168. ui: ui.options.toJS()
  169. });
  170. });
  171. // proxy client events
  172. client.on("ui:client:proxy", function (evt) {
  173. ui.clients.emit(evt.event, evt.data);
  174. });
  175. client.on("ui", function (data) {
  176. ui.delegateEvent(data);
  177. });
  178. });
  179. done();
  180. }
  181. };