zcatalyst-cli 1.10.1 → 1.12.1

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 (149) hide show
  1. package/docs/.DS_Store +0 -0
  2. package/docs/client-utils.toml +5 -0
  3. package/docs/command_needs/auth.toml +1 -1
  4. package/docs/optional-import.toml +10 -0
  5. package/docs/plugin-loader.toml +4 -0
  6. package/docs/serve/server/index.toml +4 -0
  7. package/docs/serve/server/lib/web_client/server.toml +4 -0
  8. package/lib/apig-utils.js +2 -1
  9. package/lib/authentication/login.js +10 -12
  10. package/lib/client-utils.js +8 -6
  11. package/lib/command_needs/auth.js +2 -2
  12. package/lib/commands/client/delete.js +2 -1
  13. package/lib/commands/ds/status.js +5 -5
  14. package/lib/commands/event/generate/index.js +4 -13
  15. package/lib/commands/functions/delete.js +2 -2
  16. package/lib/commands/functions/shell.js +5 -2
  17. package/lib/commands/iac/import.js +37 -10
  18. package/lib/commands/init.js +1 -1
  19. package/lib/commands/pull.js +1 -1
  20. package/lib/commands/serve.js +2 -1
  21. package/lib/endpoints/lib/sdk.js +2 -2
  22. package/lib/error.js +16 -13
  23. package/lib/errorOut.js +2 -2
  24. package/lib/fn-utils/lib/common.js +4 -4
  25. package/lib/fn-utils/lib/java.js +6 -4
  26. package/lib/fn-utils/lib/node.js +2 -2
  27. package/lib/init/dependencies/npm-install.js +10 -10
  28. package/lib/init/features/client/index.js +47 -0
  29. package/lib/init/features/client/initializers/angular.js +104 -0
  30. package/lib/init/features/client/initializers/basic.js +50 -0
  31. package/lib/init/features/client/initializers/lyte.js +56 -0
  32. package/lib/init/features/client/initializers/react.js +78 -0
  33. package/lib/init/features/functions/index.js +14 -5
  34. package/lib/init/features/functions/languages/node.js +3 -1
  35. package/lib/init/features/index.js +10 -3
  36. package/lib/init/features/project.js +1 -1
  37. package/lib/init/util/client.js +89 -4
  38. package/lib/internal/command.js +4 -1
  39. package/lib/internal/config.js +2 -1
  40. package/lib/optional-import.js +27 -26
  41. package/lib/plugin-loader.js +27 -10
  42. package/lib/port-resolver.js +4 -2
  43. package/lib/prompt/index.js +1 -1
  44. package/lib/pull/features/functions/index.js +1 -1
  45. package/lib/serve/index.js +6 -1
  46. package/lib/serve/server/index.js +52 -8
  47. package/lib/serve/server/lib/java/JavaaioServer.java +3 -2
  48. package/lib/serve/server/lib/master.js +22 -24
  49. package/lib/serve/server/lib/web_client/index.js +30 -0
  50. package/lib/serve/server/lib/web_client/server.js +174 -0
  51. package/lib/shell/dependencies/invoker/bio/java/JavabioInvoker.java +7 -1
  52. package/lib/shell/dependencies/invoker/bio/node.js +5 -2
  53. package/lib/shell/index.js +10 -6
  54. package/lib/util_modules/char.js +1 -1
  55. package/lib/util_modules/config/lib/apig.js +2 -1
  56. package/lib/util_modules/config/lib/client.js +18 -21
  57. package/lib/util_modules/constants/index.js +3 -1
  58. package/lib/util_modules/constants/lib/placeholders.js +1 -0
  59. package/lib/util_modules/constants/lib/plugin.js +28 -0
  60. package/lib/util_modules/constants/lib/template.js +11 -1
  61. package/lib/util_modules/constants/lib/urls.js +29 -13
  62. package/lib/util_modules/{contextHelp.js → context-help.js} +0 -0
  63. package/lib/util_modules/env.js +13 -12
  64. package/lib/util_modules/fs/index.js +1 -9
  65. package/lib/util_modules/fs/lib/async.js +3 -3
  66. package/lib/util_modules/fs/utils.js +8 -0
  67. package/lib/util_modules/global-space.js +2 -0
  68. package/lib/util_modules/option.js +5 -1
  69. package/lib/util_modules/server.js +53 -0
  70. package/lib/util_modules/shell.js +10 -6
  71. package/lib/winston.js +1 -1
  72. package/package.json +8 -5
  73. package/templates/.DS_Store +0 -0
  74. package/templates/init/.DS_Store +0 -0
  75. package/templates/init/client/.DS_Store +0 -0
  76. package/templates/init/client/{client-package.json → basic/client-package.json} +0 -0
  77. package/templates/init/client/{index.html → basic/index.html} +0 -0
  78. package/templates/init/client/{main.css → basic/main.css} +0 -0
  79. package/templates/init/client/{main.js → basic/main.js} +0 -0
  80. package/templates/init/client/lyte/build/build.js +301 -0
  81. package/templates/init/client/lyte/build/scripts/cliDownloadScript.js +54 -0
  82. package/templates/init/client/lyte/client-package.json +5 -0
  83. package/templates/init/client/lyte/components/javascript/welcome-comp.js +13 -0
  84. package/templates/init/client/lyte/components/styles/welcome-comp.css +0 -0
  85. package/templates/init/client/lyte/components/templates/welcome-comp.html +8 -0
  86. package/templates/init/client/lyte/data-store/adapters/.gitkeep +0 -0
  87. package/templates/init/client/lyte/data-store/models/.gitkeep +0 -0
  88. package/templates/init/client/lyte/data-store/serializers/.gitkeep +0 -0
  89. package/templates/init/client/lyte/index.html +17 -0
  90. package/templates/init/client/lyte/package.json +12 -0
  91. package/templates/init/client/lyte/router.js +14 -0
  92. package/templates/init/client/lyte/routes/index.js +54 -0
  93. package/templates/init/client/react/.DS_Store +0 -0
  94. package/templates/init/client/react/react_js/package.json +11 -0
  95. package/templates/init/client/react/react_js/template/README.md +70 -0
  96. package/templates/init/client/react/react_js/template/client-package.json +5 -0
  97. package/templates/init/client/react/react_js/template/gitignore +23 -0
  98. package/templates/init/client/react/react_js/template/public/favicon.ico +0 -0
  99. package/templates/init/client/react/react_js/template/public/index.html +43 -0
  100. package/templates/init/client/react/react_js/template/public/logo192.png +0 -0
  101. package/templates/init/client/react/react_js/template/public/logo512.png +0 -0
  102. package/templates/init/client/react/react_js/template/public/manifest.json +25 -0
  103. package/templates/init/client/react/react_js/template/public/robots.txt +3 -0
  104. package/templates/init/client/react/react_js/template/src/App.css +38 -0
  105. package/templates/init/client/react/react_js/template/src/App.js +25 -0
  106. package/templates/init/client/react/react_js/template/src/App.test.js +8 -0
  107. package/templates/init/client/react/react_js/template/src/index.css +13 -0
  108. package/templates/init/client/react/react_js/template/src/index.js +17 -0
  109. package/templates/init/client/react/react_js/template/src/logo.svg +1 -0
  110. package/templates/init/client/react/react_js/template/src/reportWebVitals.js +13 -0
  111. package/templates/init/client/react/react_js/template/src/setupTests.js +5 -0
  112. package/templates/init/client/react/react_js/template.json +13 -0
  113. package/templates/init/client/react/react_ts/package.json +11 -0
  114. package/templates/init/client/react/react_ts/template/README.md +46 -0
  115. package/templates/init/client/react/react_ts/template/client-package.json +5 -0
  116. package/templates/init/client/react/react_ts/template/gitignore +23 -0
  117. package/templates/init/client/react/react_ts/template/public/favicon.ico +0 -0
  118. package/templates/init/client/react/react_ts/template/public/index.html +43 -0
  119. package/templates/init/client/react/react_ts/template/public/logo192.png +0 -0
  120. package/templates/init/client/react/react_ts/template/public/logo512.png +0 -0
  121. package/templates/init/client/react/react_ts/template/public/manifest.json +25 -0
  122. package/templates/init/client/react/react_ts/template/public/robots.txt +3 -0
  123. package/templates/init/client/react/react_ts/template/src/App.css +38 -0
  124. package/templates/init/client/react/react_ts/template/src/App.test.tsx +9 -0
  125. package/templates/init/client/react/react_ts/template/src/App.tsx +26 -0
  126. package/templates/init/client/react/react_ts/template/src/index.css +13 -0
  127. package/templates/init/client/react/react_ts/template/src/index.tsx +17 -0
  128. package/templates/init/client/react/react_ts/template/src/logo.svg +1 -0
  129. package/templates/init/client/react/react_ts/template/src/reportWebVitals.ts +15 -0
  130. package/templates/init/client/react/react_ts/template/src/setupTests.ts +5 -0
  131. package/templates/init/client/react/react_ts/template.json +18 -0
  132. package/templates/init/functions/java/integ/cliq/com/handlers/BotHandler.java +116 -113
  133. package/templates/init/functions/java/integ/cliq/com/handlers/CommandHandler.java +20 -27
  134. package/templates/init/functions/java/integ/cliq/com/handlers/FunctionHandler.java +143 -112
  135. package/templates/init/functions/java/integ/cliq/com/handlers/InstallationHandler.java +4 -7
  136. package/templates/init/functions/java/integ/cliq/com/handlers/InstallationValidator.java +4 -7
  137. package/templates/init/functions/java/integ/cliq/com/handlers/MessageActionHandler.java +10 -12
  138. package/templates/init/functions/java/integ/cliq/com/handlers/WidgetHandler.java +100 -66
  139. package/templates/init/functions/java/integ/cliq/sample.java +5 -7
  140. package/templates/init/functions/node/integ/cliq/.DS_Store +0 -0
  141. package/templates/init/functions/node/integ/cliq/handlers/bot-handler.js +23 -8
  142. package/templates/init/functions/node/integ/cliq/handlers/command-handler.js +3 -4
  143. package/templates/init/functions/node/integ/cliq/handlers/function-handler.js +46 -0
  144. package/templates/init/functions/node/integ/cliq/handlers/widget-handler.js +31 -0
  145. package/templates/init/functions/node/integ/cliq/package.json +2 -1
  146. package/templates/init/functions/node/integ/cliq/sample.js +3 -1
  147. package/templates/web-socket.txt +21 -0
  148. package/lib/init/features/client.js +0 -50
  149. package/lib/serve/server/lib/client.js +0 -30
@@ -31,34 +31,35 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
31
31
  return (mod && mod.__esModule) ? mod : { "default": mod };
32
32
  };
33
33
  Object.defineProperty(exports, "__esModule", { value: true });
34
- const path_1 = __importDefault(require("path"));
35
34
  const app_module_path_1 = __importDefault(require("app-module-path"));
36
- const logger_1 = require("./util_modules/logger");
37
- const project_1 = require("./util_modules/project");
38
- const global_dirs_1 = __importDefault(require("global-dirs"));
39
- exports.default = (name) => __awaiter(void 0, void 0, void 0, function* () {
40
- try {
41
- if (name.startsWith('./')) {
42
- const projectModulePath = project_1.resolveProjectPath(name);
43
- app_module_path_1.default.addPath(projectModulePath);
44
- return Promise.resolve().then(() => __importStar(require(projectModulePath)));
45
- }
46
- else {
47
- for (const manager of ['npm', 'yarn']) {
48
- try {
49
- const packageName = path_1.default.join(global_dirs_1.default[manager].packages, name);
50
- app_module_path_1.default.addPath(packageName);
51
- return Promise.resolve().then(() => __importStar(require(packageName)));
52
- }
53
- catch (e) {
54
- logger_1.debug(e);
55
- }
56
- }
57
- return;
35
+ const project_js_1 = require("./util_modules/project.js");
36
+ const path_1 = require("path");
37
+ const async_js_1 = require("./util_modules/fs/lib/async.js");
38
+ const error_js_1 = __importDefault(require("./error.js"));
39
+ const ansi_colors_1 = require("ansi-colors");
40
+ exports.default = (name, source) => __awaiter(void 0, void 0, void 0, function* () {
41
+ if (name === path_1.basename(name)) {
42
+ const projectModulePath = project_js_1.resolveProjectPath(path_1.join(source, 'node_modules', name));
43
+ const projectModulePathExists = yield async_js_1.dirExists(projectModulePath);
44
+ if (!projectModulePathExists) {
45
+ throw new error_js_1.default('Module path does not exists', {
46
+ errorId: 'OPT-IMP-1',
47
+ arg: [ansi_colors_1.bold(name), ansi_colors_1.italic.red(projectModulePath), ansi_colors_1.bold(source)],
48
+ exit: 1
49
+ });
58
50
  }
51
+ app_module_path_1.default.addPath(projectModulePath);
52
+ return Promise.resolve().then(() => __importStar(require(projectModulePath)));
59
53
  }
60
- catch (err) {
61
- logger_1.debug(err);
62
- return;
54
+ const projectModulePath = project_js_1.resolveProjectPath(name);
55
+ const projectModulePathExists = yield async_js_1.dirExists(projectModulePath);
56
+ if (!projectModulePathExists) {
57
+ throw new error_js_1.default('Module path does not exists', {
58
+ errorId: 'OPT-IMP-2',
59
+ arg: [ansi_colors_1.bold(name), ansi_colors_1.italic.red(projectModulePath), ansi_colors_1.bold(source)],
60
+ exit: 1
61
+ });
63
62
  }
63
+ app_module_path_1.default.addPath(projectModulePath);
64
+ return Promise.resolve().then(() => __importStar(require(projectModulePath)));
64
65
  });
@@ -31,17 +31,30 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
31
31
  return (mod && mod.__esModule) ? mod : { "default": mod };
32
32
  };
33
33
  Object.defineProperty(exports, "__esModule", { value: true });
34
- const optional_import_1 = __importDefault(require("./optional-import"));
35
- const runtime_store_1 = __importDefault(require("./runtime-store"));
36
- const config = __importStar(require("./util_modules/config"));
34
+ const ansi_colors_1 = require("ansi-colors");
35
+ const error_js_1 = __importDefault(require("./error.js"));
36
+ const optional_import_js_1 = __importDefault(require("./optional-import.js"));
37
+ const runtime_store_js_1 = __importDefault(require("./runtime-store.js"));
38
+ const ClientConfig = __importStar(require("./util_modules/config/lib/client.js"));
39
+ const FunctionsConfig = __importStar(require("./util_modules/config/lib/functions.js"));
40
+ const config = {
41
+ client: {
42
+ plugin: ClientConfig.plugin,
43
+ source: ClientConfig.source
44
+ },
45
+ functions: {
46
+ plugin: FunctionsConfig.plugin,
47
+ source: FunctionsConfig.source
48
+ }
49
+ };
37
50
  const loadedPlugins = {};
38
51
  function pluginLoader(feature, name, source) {
39
52
  return __awaiter(this, void 0, void 0, function* () {
40
- const configModule = config[(feature + 'Config')];
41
- if (configModule === undefined) {
53
+ const pluginConfig = config[feature];
54
+ if (pluginConfig === undefined) {
42
55
  return;
43
56
  }
44
- const plugins = configModule.plugin(name, source);
57
+ const plugins = pluginConfig.plugin(name, source);
45
58
  let specificPlugin;
46
59
  if (typeof plugins === 'string') {
47
60
  specificPlugin = plugins;
@@ -57,18 +70,22 @@ function pluginLoader(feature, name, source) {
57
70
  pluginMod = loadedPlugins[specificPlugin];
58
71
  }
59
72
  else {
60
- pluginMod = (yield optional_import_1.default(specificPlugin));
73
+ pluginMod = yield optional_import_js_1.default(specificPlugin, pluginConfig.source());
61
74
  }
62
75
  if (pluginMod === undefined) {
63
- return;
76
+ throw new error_js_1.default(`Configured plugin could not be found for [${feature}:${name}] - ${specificPlugin}`, {
77
+ exit: 1,
78
+ errorId: 'PLG-LDR-1',
79
+ arg: [ansi_colors_1.italic.red(specificPlugin), ansi_colors_1.bold(name), ansi_colors_1.bold(feature)]
80
+ });
64
81
  }
65
82
  loadedPlugins[specificPlugin] = pluginMod;
66
83
  if (typeof pluginMod === 'function') {
67
- runtime_store_1.default.set(`context.${feature}.plugins.${name}${source ? '.' + source : ''}`, specificPlugin);
84
+ runtime_store_js_1.default.set(`context.${feature}.plugins.${name}${source ? '.' + source : ''}`, specificPlugin);
68
85
  return pluginMod;
69
86
  }
70
87
  else if (typeof pluginMod[name] === 'function') {
71
- runtime_store_1.default.set(`context.${feature}.plugins.${name}${source ? '.' + source : ''}`, specificPlugin);
88
+ runtime_store_js_1.default.set(`context.${feature}.plugins.${name}${source ? '.' + source : ''}`, specificPlugin);
72
89
  return pluginMod[name];
73
90
  }
74
91
  return;
@@ -70,7 +70,8 @@ class PortResolver {
70
70
  if (searchSpan === 0) {
71
71
  const err = error_1.default.getErrorInstance(e, {
72
72
  message: `Provided port '${port}' is not available`,
73
- skipHelp: false
73
+ skipHelp: false,
74
+ fileName: __filename
74
75
  });
75
76
  err.errorId = 'PORT-RESOLVER-1';
76
77
  err.arg = [
@@ -84,7 +85,8 @@ class PortResolver {
84
85
  }
85
86
  const cerr = error_1.default.getErrorInstance(e, {
86
87
  message: `No ports from '${port}' to ${port + searchSpan} is free to use as a ${type} port.`,
87
- skipHelp: false
88
+ skipHelp: false,
89
+ fileName: __filename
88
90
  });
89
91
  cerr.errorId = 'PORT-RESOLVER-2';
90
92
  cerr.arg = [ansi_colors_1.bold(port + ''), ansi_colors_1.bold(port + searchSpan + ''), ansi_colors_1.bold('--' + type)];
@@ -63,7 +63,7 @@ exports.default = {
63
63
  throw new error_1.default('No such type registered', { exit: 2 });
64
64
  }
65
65
  }),
66
- seperator: (line) => {
66
+ separator: (line) => {
67
67
  return new inquirer_1.default.Separator(line);
68
68
  },
69
69
  choice,
@@ -57,7 +57,7 @@ exports.default = () => __awaiter(void 0, void 0, void 0, function* () {
57
57
  return accumulator;
58
58
  }, {});
59
59
  const choicesArr = js_1.JS.transform(choicesObj, (result, value, key) => {
60
- result.push(prompt_1.default.seperator('----' + key + '----'));
60
+ result.push(prompt_1.default.separator('----' + key + '----'));
61
61
  result.push(...value);
62
62
  return result;
63
63
  }, []);
@@ -54,7 +54,8 @@ exports.default = () => __awaiter(void 0, void 0, void 0, function* () {
54
54
  runtime_store_1.default.set('context.port.http.master', masterPort);
55
55
  yield Promise.all(serveTargets.map((target) => features[target]()));
56
56
  const rawTargets = runtime_store_1.default.get('context.functions.targets', []);
57
- if (option_1.getOptionValue('watch', false)) {
57
+ const watchOpt = option_1.getOptionValue('watch', false);
58
+ if (watchOpt) {
58
59
  yield Promise.all(rawTargets.map((target) => __awaiter(void 0, void 0, void 0, function* () { return (target.valid ? fn_watcher_1.default(target) : undefined); })));
59
60
  }
60
61
  const funtionTargets = rawTargets.filter((target) => {
@@ -73,6 +74,10 @@ exports.default = () => __awaiter(void 0, void 0, void 0, function* () {
73
74
  if (serveTargets.includes('client')) {
74
75
  const client = runtime_store_1.default.get('context.client', false);
75
76
  if (client && client.valid) {
77
+ client.opts = {
78
+ open: option_1.getOptionValue('open', false),
79
+ watch: watchOpt
80
+ };
76
81
  advancedServer.add('client', client);
77
82
  }
78
83
  else {
@@ -1,4 +1,23 @@
1
1
  'use strict';
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
2
21
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
22
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
23
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -35,6 +54,7 @@ const js_1 = require("../../util_modules/js");
35
54
  const logger_1 = require("../../util_modules/logger");
36
55
  const shell_1 = require("../../util_modules/shell");
37
56
  const master_1 = __importDefault(require("./lib/master"));
57
+ const ansi_colors_1 = require("ansi-colors");
38
58
  class Server {
39
59
  constructor() {
40
60
  this.targetDetailsArr = [];
@@ -85,8 +105,8 @@ class Server {
85
105
  opts.push(details.httpPort + '');
86
106
  const targetSource = (_c = details.target) === null || _c === void 0 ? void 0 : _c.source;
87
107
  const configType = details.type === 'server' ? 'functions' : details.type;
88
- const servePlugin = yield plugin_loader_1.default(configType, 'start', targetSource);
89
- if (servePlugin !== undefined) {
108
+ const servePlugin = yield plugin_loader_1.default(configType, 'start', targetSource).catch((err) => logger_1.debug(err));
109
+ if (typeof servePlugin === 'function') {
90
110
  const event = servePlugin(details, masterPort);
91
111
  logger_1.labeled(label, `Plugin : "${runtime_store_1.default.get(`context.${configType}.plugins.start${targetSource ? '.' + targetSource : ''}`)}" is used for ${details.type} serve`).MESSAGE();
92
112
  return event;
@@ -111,7 +131,7 @@ class Server {
111
131
  const label = `${details.type}[${(_a = details.target) === null || _a === void 0 ? void 0 : _a.name}]`;
112
132
  const _d = details.target, { ['watcher']: _ } = _d, serialisableTarget = __rest(_d, ['watcher']);
113
133
  const opts = [
114
- path_1.join(__dirname, 'lib', details.type === 'client' ? 'client.js' : 'node.js'),
134
+ path_1.join(__dirname, 'lib', 'node.js'),
115
135
  details.httpPort + '',
116
136
  JSON.stringify(serialisableTarget)
117
137
  ];
@@ -121,12 +141,28 @@ class Server {
121
141
  }
122
142
  const targetSource = (_b = details.target) === null || _b === void 0 ? void 0 : _b.source;
123
143
  const configType = details.type === 'server' ? 'functions' : details.type;
124
- const servePlugin = yield plugin_loader_1.default(configType, 'start', targetSource);
125
- if (servePlugin !== undefined) {
126
- const event = servePlugin(details, masterPort);
144
+ const servePlugin = yield plugin_loader_1.default(configType, 'start', targetSource).catch((err) => logger_1.debug(err));
145
+ if (typeof servePlugin === 'function') {
146
+ const event = servePlugin(details, masterPort).catch((e) => {
147
+ var _a, _b;
148
+ const err = error_1.default.getErrorInstance(e, {
149
+ fileName: __filename,
150
+ skipHelp: false
151
+ });
152
+ err.errorId = 'SERVE-IDX-1';
153
+ err.arg = [
154
+ configType,
155
+ ansi_colors_1.bold(((_b = (_a = details.target) === null || _a === void 0 ? void 0 : _a.plugins) === null || _b === void 0 ? void 0 : _b.start)[targetSource] || 'start'),
156
+ ansi_colors_1.bold('Error: ') + ansi_colors_1.italic.red(err.message)
157
+ ];
158
+ throw err;
159
+ });
127
160
  logger_1.labeled(label, `Plugin : "${runtime_store_1.default.get(`context.${configType}.plugins.start${targetSource ? '.' + targetSource : ''}`)}" is used for ${details.type} serve`).MESSAGE();
128
161
  return event;
129
162
  }
163
+ if (details.type === 'client') {
164
+ return yield (yield Promise.resolve().then(() => __importStar(require('./lib/web_client/index.js')))).default(details, masterPort);
165
+ }
130
166
  const child = shell_1.spawn('node', opts, {
131
167
  cwd: targetSource,
132
168
  stdio: 'pipe',
@@ -136,7 +172,7 @@ class Server {
136
172
  X_ZOHO_CATALYST_CONSOLE_URL: constants_1.ORIGIN.admin
137
173
  }
138
174
  }).RAW();
139
- logger_1.labeled(label, `URL : http://localhost:${masterPort}/${details.type === 'client' ? 'app/' : 'server/' + ((_c = details.target) === null || _c === void 0 ? void 0 : _c.name) + '/'}`).MESSAGE();
175
+ logger_1.labeled(label, `URL : http://localhost:${masterPort}/server/${(_c = details.target) === null || _c === void 0 ? void 0 : _c.name}/`).MESSAGE();
140
176
  logger_1.info();
141
177
  return child;
142
178
  });
@@ -196,7 +232,15 @@ class Server {
196
232
  this.masterServer = server;
197
233
  return new Promise((res) => {
198
234
  server.on('listening', () => {
199
- this.targetDetailsArr.forEach((targetDetails) => { var _a; return (_a = targetDetails.process) === null || _a === void 0 ? void 0 : _a.emit('start'); });
235
+ this.targetDetailsArr.forEach((targetDetails) => {
236
+ var _a;
237
+ if (targetDetails.process &&
238
+ 'send' in targetDetails.process) {
239
+ targetDetails.process.send('start');
240
+ return;
241
+ }
242
+ (_a = targetDetails.process) === null || _a === void 0 ? void 0 : _a.emit('start');
243
+ });
200
244
  res();
201
245
  });
202
246
  });
@@ -67,11 +67,12 @@ public class JavaaioServer extends DefaultCoreServer {
67
67
  LogManager manager = LogManager.getLogManager();
68
68
  manager.reset();
69
69
  Logger rootLogger = manager.getLogger("");
70
- LogHandler handler = new JavaaioServer().new LogHandler();
70
+ JavaaioServer aioServer = new JavaaioServer();
71
+ LogHandler handler = aioServer.new LogHandler();
71
72
  rootLogger.addHandler(handler);
72
73
 
73
74
  port = Integer.parseInt(args[0]);
74
- new JavaaioServer().start();
75
+ aioServer.start();
75
76
  }
76
77
  catch (Exception e) {
77
78
  System.out.println(getStackTraceAsString(e));
@@ -13,7 +13,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const express_1 = __importDefault(require("express"));
16
- const http_proxy_1 = require("http-proxy");
17
16
  const error_1 = __importDefault(require("../../../error"));
18
17
  const apig_matcher_1 = __importDefault(require("../../../express_middlewares/apig-matcher"));
19
18
  const auth_checker_1 = __importDefault(require("../../../express_middlewares/auth-checker"));
@@ -30,6 +29,8 @@ const js_1 = require("../../../util_modules/js");
30
29
  const logger_2 = require("../../../util_modules/logger");
31
30
  const project_2 = require("../../../util_modules/project");
32
31
  const option_1 = require("../../../util_modules/option");
32
+ const server_js_1 = require("../../../util_modules/server.js");
33
+ const http_proxy_1 = require("http-proxy");
33
34
  const removeSecure = (str) => str.replace(/;\s?Secure/i, '').replace(/;\s?SameSite=None/i, '');
34
35
  const redirectByAuth = (req, res, redirectUrl) => {
35
36
  if (req.headers['x-zc-user-cred-token'] === undefined) {
@@ -94,7 +95,6 @@ function spinUpMaster(listenPort, otherServerDetails) {
94
95
  const rules = runtime_store_1.default.get('context.apig.local.config', -1);
95
96
  const customProxyUrl = option_1.getOptionValue('proxy');
96
97
  const app = express_1.default();
97
- const connections = {};
98
98
  let systemRoutes;
99
99
  app.use(logger_1.default);
100
100
  app.use(cookie_parser_1.default);
@@ -122,26 +122,30 @@ function spinUpMaster(listenPort, otherServerDetails) {
122
122
  app.use('/', apig_matcher_1.default, auth_checker_1.default, url_rewriter_1.default);
123
123
  }
124
124
  const proxy = http_proxy_1.createProxyServer()
125
- .on('error', (err, req) => {
125
+ .on('error', (err, req, res) => {
126
+ res.writeHead(503, { 'Content-Type': 'json' });
127
+ res.write(JSON.stringify({ error: 'unable to proxy request' }));
128
+ res.end();
126
129
  console.log('unable to proxy request ', req.url);
127
130
  console.error(err);
128
131
  })
129
- .on('proxyRes', proxyResponseHandler(systemRoutes));
132
+ .on('proxyRes', proxyResponseHandler(systemRoutes))
133
+ .on('close', () => logger_2.debug('proxy closed'));
134
+ const proxyDestroyer = new server_js_1.ConnectionDestroyer(proxy);
130
135
  otherServerDetails.forEach((serverDetail) => {
131
136
  var _a, _b, _c, _d, _e, _f;
132
137
  switch (serverDetail.type) {
133
138
  case 'client':
134
139
  const appTarget = `http://127.0.0.1:${serverDetail.httpPort}/app/`;
135
140
  app.use('/app', (req, res) => {
136
- var _a;
137
- if (req.url.startsWith('/local-redirect') &&
138
- ((_a = serverDetail.target) === null || _a === void 0 ? void 0 : _a.login_redirect)) {
139
- const isUrl = serverDetail.target.login_redirect.startsWith('/');
141
+ const target = serverDetail.target;
142
+ if (req.url.startsWith('/local-redirect') && target.login_redirect) {
143
+ const isUrl = target.login_redirect.startsWith('/');
140
144
  if (isUrl) {
141
- res.redirect(serverDetail.target.login_redirect);
145
+ res.redirect(target.login_redirect);
142
146
  }
143
147
  else {
144
- res.redirect('/app/' + serverDetail.target.login_redirect);
148
+ res.redirect('/app/' + target.login_redirect);
145
149
  }
146
150
  }
147
151
  else {
@@ -192,7 +196,8 @@ function spinUpMaster(listenPort, otherServerDetails) {
192
196
  ws: true
193
197
  });
194
198
  });
195
- app.use('/baas/v1/project/:projectId/function/' + ((_f = serverDetail.target) === null || _f === void 0 ? void 0 : _f.id), (req, res) => {
199
+ app.use('/baas/v1/project/:projectId/function/' +
200
+ ((_f = serverDetail.target) === null || _f === void 0 ? void 0 : _f.id), (req, res) => {
196
201
  var _a;
197
202
  proxy.web(req, res, {
198
203
  target: `http://127.0.0.1:${serverDetail.httpPort}/server/${(_a = serverDetail.target) === null || _a === void 0 ? void 0 : _a.id}`,
@@ -211,24 +216,17 @@ function spinUpMaster(listenPort, otherServerDetails) {
211
216
  app.use(error_handler_1.default);
212
217
  const expressServer = app.listen(listenPort).on('error', (err) => {
213
218
  if (proxy) {
214
- proxy.close();
219
+ proxyDestroyer.destroy();
215
220
  }
216
221
  console.error(err);
217
222
  });
223
+ const expressDestroyer = new server_js_1.ConnectionDestroyer(expressServer);
218
224
  expressServer.maxConnections = 100;
219
- process.once('SIGINT', () => Object.keys(connections).forEach((sokcetId) => { var _a; return (_a = connections[sokcetId]) === null || _a === void 0 ? void 0 : _a.destroy(); }));
220
- expressServer.on('connection', (socket) => {
221
- const offset = Math.floor(Math.random() * 10);
222
- const id = Date.now() + offset + '';
223
- connections[id] = socket;
224
- socket.on('close', () => {
225
- delete connections[id];
226
- });
227
- });
228
- expressServer.on('close', () => {
225
+ process.on('SIGINT', () => expressDestroyer.destroy(false));
226
+ expressServer.on('close', () => __awaiter(this, void 0, void 0, function* () {
229
227
  logger_2.info('Shutting down server');
230
- proxy.close();
231
- });
228
+ yield proxyDestroyer.destroy().catch((err) => logger_2.debug(err));
229
+ }));
232
230
  expressServer.on('upgrade', (...upgradeParam) => {
233
231
  app.emit('upgrade', ...upgradeParam);
234
232
  });
@@ -0,0 +1,30 @@
1
+ 'use strict';
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const open_1 = __importDefault(require("open"));
16
+ const logger_1 = require("../../../../util_modules/logger");
17
+ const server_js_1 = __importDefault(require("./server.js"));
18
+ exports.default = (details, masterPort) => __awaiter(void 0, void 0, void 0, function* () {
19
+ var _a;
20
+ const target = details.target;
21
+ const clientListner = yield server_js_1.default(details.httpPort, target.source, target.homepage, ((_a = target.opts) === null || _a === void 0 ? void 0 : _a.watch) || false);
22
+ clientListner.addListener('start', () => {
23
+ var _a, _b;
24
+ const appUrl = 'http://localhost:' + masterPort;
25
+ const label = `${details.type}[${(_a = details.target) === null || _a === void 0 ? void 0 : _a.name}]`;
26
+ logger_1.labeled(label, `URL : ${appUrl}/app/`).MESSAGE();
27
+ ((_b = details.target.opts) === null || _b === void 0 ? void 0 : _b.open) && open_1.default(appUrl).catch();
28
+ });
29
+ return clientListner;
30
+ });
@@ -0,0 +1,174 @@
1
+ 'use strict';
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const express_1 = __importDefault(require("express"));
16
+ const events_1 = require("events");
17
+ const chokidar_1 = require("chokidar");
18
+ const logger_js_1 = require("../../../../util_modules/logger.js");
19
+ const index_js_1 = require("../../../../util_modules/constants/index.js");
20
+ const error_js_1 = __importDefault(require("../../../../error.js"));
21
+ const index_js_2 = require("../../../../util_modules/fs/index.js");
22
+ const path_1 = require("path");
23
+ const ws_1 = require("ws");
24
+ const port_resolver_js_1 = __importDefault(require("../../../../port-resolver.js"));
25
+ const ansi_colors_1 = require("ansi-colors");
26
+ const server_js_1 = require("../../../../util_modules/server.js");
27
+ function prepareScriptFile(dir, port) {
28
+ return __awaiter(this, void 0, void 0, function* () {
29
+ const scriptFile = yield index_js_2.ASYNC.readFile(index_js_1.TEMPLATE.client.basic.socket);
30
+ if (!scriptFile) {
31
+ throw new error_js_1.default('Unable to read the script file', { exit: 2 });
32
+ }
33
+ const scriptReady = scriptFile.replace(new RegExp(index_js_1.PLACEHOLDER.client.port, 'g'), port + '');
34
+ yield index_js_2.ASYNC.ensureDir(dir);
35
+ const scriptFilePath = path_1.join(dir, 'reload-script.js');
36
+ yield index_js_2.ASYNC.writeFile(scriptFilePath, scriptReady);
37
+ return scriptFilePath;
38
+ });
39
+ }
40
+ function stringInjector(source, anchorIdx, marker, inject) {
41
+ const headLastIdx = anchorIdx + marker.length;
42
+ const scriptPrefix = source.slice(0, headLastIdx);
43
+ const scriptSuffix = source.slice(headLastIdx);
44
+ return scriptPrefix + inject + scriptSuffix;
45
+ }
46
+ function injectScript(scriptLocale, indexHtml) {
47
+ return __awaiter(this, void 0, void 0, function* () {
48
+ const headIdx = indexHtml.search(/<head>/gi);
49
+ const scriptTag = `\n<!-- Catalyst web client reload script -->\n<script type="text/javascript" src="${scriptLocale}"></script>\n`;
50
+ if (headIdx !== -1) {
51
+ return stringInjector(indexHtml, headIdx, '<head>', scriptTag);
52
+ }
53
+ const htmlIdx = indexHtml.search(/<html>/gi);
54
+ const headTag = `\n<head>${scriptTag}</head>\n`;
55
+ return stringInjector(indexHtml, htmlIdx, '<html>', headTag);
56
+ });
57
+ }
58
+ function reloadApp(event, path, liveSockets) {
59
+ logger_js_1.info(ansi_colors_1.cyan(`${event} detected in ${path} of the Web-Client.`));
60
+ liveSockets.forEach((socket) => socket.send('reload'));
61
+ }
62
+ function webClientServer(httpPort, source, homepage, enableWatch) {
63
+ return __awaiter(this, void 0, void 0, function* () {
64
+ const eventListener = new events_1.EventEmitter();
65
+ const catalystTempDir = path_1.join(source, '.catalyst');
66
+ const socketServerPort = yield port_resolver_js_1.default.getFreePort(httpPort, 20, true);
67
+ const reloadScriptSrc = enableWatch
68
+ ? path_1.relative(source, yield prepareScriptFile(catalystTempDir, socketServerPort))
69
+ : undefined;
70
+ if (reloadScriptSrc) {
71
+ const clientWatcher = chokidar_1.watch(source, {
72
+ ignoreInitial: true,
73
+ ignored: ['node_modules/**/*']
74
+ });
75
+ const liveSockets = [];
76
+ let watcherReady = false;
77
+ clientWatcher.on('error', (err) => {
78
+ watcherReady = false;
79
+ eventListener.emit('error', new error_js_1.default('client watcher error', {
80
+ original: err,
81
+ exit: 1,
82
+ skipHelp: true
83
+ }));
84
+ });
85
+ clientWatcher.on('ready', () => {
86
+ watcherReady = true;
87
+ const socketServer = new ws_1.WebSocketServer({
88
+ port: socketServerPort,
89
+ path: '/client-reload'
90
+ })
91
+ .on('connection', (clientSocket) => {
92
+ if (!watcherReady) {
93
+ clientSocket.send('watcher not ready');
94
+ clientSocket.close();
95
+ return;
96
+ }
97
+ clientSocket.on('close', () => {
98
+ const socketIdx = liveSockets.findIndex((socket) => socket === clientSocket);
99
+ liveSockets.splice(socketIdx, 1);
100
+ });
101
+ liveSockets.push(clientSocket);
102
+ clientSocket.send('connected');
103
+ })
104
+ .on('error', (err) => logger_js_1.debug(err));
105
+ setTimeout(() => {
106
+ clientWatcher.on('change', (path) => {
107
+ reloadApp('Change(s) is', path_1.relative(source, path), liveSockets);
108
+ });
109
+ clientWatcher.on('add', (path) => {
110
+ reloadApp('Addition of file(s) is', path_1.relative(source, path), liveSockets);
111
+ });
112
+ clientWatcher.on('unlink', (path) => {
113
+ reloadApp('Deletion of file(s) is', path_1.relative(source, path), liveSockets);
114
+ });
115
+ }, 1000);
116
+ eventListener.addListener('close', () => socketServer === null || socketServer === void 0 ? void 0 : socketServer.close());
117
+ });
118
+ eventListener.addListener('close', () => {
119
+ if (clientWatcher) {
120
+ clientWatcher.close();
121
+ }
122
+ liveSockets.forEach((socket) => socket.close());
123
+ index_js_2.SYNC.deleteDir(catalystTempDir);
124
+ });
125
+ }
126
+ const app = express_1.default();
127
+ app.use('/app', (req, res) => __awaiter(this, void 0, void 0, function* () {
128
+ const requestedFile = req.url === '/' ? homepage : decodeURIComponent(req.path);
129
+ if (enableWatch &&
130
+ requestedFile === homepage &&
131
+ (requestedFile.includes('.html') || requestedFile.includes('.htm'))) {
132
+ const homepageFile = yield index_js_2.ASYNC.readFile(path_1.join(source, homepage));
133
+ if (!homepageFile) {
134
+ eventListener.emit('error', new error_js_1.default('Unable to read the homepage file', {
135
+ exit: 1,
136
+ errorId: 'WEB-CLIENT-SERVER-1',
137
+ arg: [ansi_colors_1.red.italic(homepage)]
138
+ }));
139
+ return;
140
+ }
141
+ const injectedHomePage = reloadScriptSrc
142
+ ? yield injectScript(encodeURIComponent(reloadScriptSrc), homepageFile)
143
+ : undefined;
144
+ res.contentType('text/html');
145
+ res.status(200);
146
+ res.send(injectedHomePage || homepageFile);
147
+ return;
148
+ }
149
+ res.sendFile(path_1.join(source, requestedFile), (err) => {
150
+ if (err) {
151
+ logger_js_1.error('Unable to serve the requested file: ' + path_1.join(source, requestedFile));
152
+ logger_js_1.debug(err.stack || err.message);
153
+ res.status(404);
154
+ res.send('Resource not found');
155
+ }
156
+ });
157
+ }));
158
+ const server = app.listen(httpPort).on('error', (err) => {
159
+ eventListener.emit('error', err);
160
+ });
161
+ const expressDestroyer = new server_js_1.ConnectionDestroyer(server);
162
+ eventListener.on('error', (err) => {
163
+ logger_js_1.error('Error when serving the web-client: ' + err.message);
164
+ logger_js_1.info('Please exit the command to get more info');
165
+ eventListener.emit('close');
166
+ });
167
+ eventListener.on('close', () => __awaiter(this, void 0, void 0, function* () {
168
+ yield expressDestroyer.destroy();
169
+ logger_js_1.debug('client server closed');
170
+ }));
171
+ return new Promise((res) => server.on('listening', () => res(eventListener)));
172
+ });
173
+ }
174
+ exports.default = webClientServer;
@@ -94,10 +94,16 @@ public class JavabioInvoker {
94
94
  String responseMeta = Paths.get(invokerDir, "user_meta.json").toString();
95
95
 
96
96
  BufferedWriter responseWriter = new BufferedWriter(new FileWriter(responseFilePath));
97
- responseWriter.write(responseJson.toString());
97
+ String responseStr = responseJson.toString();
98
+ responseWriter.write(responseStr);
98
99
  responseWriter.close();
99
100
 
100
101
  BufferedWriter metaWriter = new BufferedWriter(new FileWriter(responseMeta));
102
+
103
+ JSONObject metaResp = ((JSONObject) metaJson.get("response"));
104
+ metaResp.put("Content-Type", "application/json");
105
+ metaResp.put("Content-Length", responseStr.length());
106
+
101
107
  metaWriter.write(metaJson.toString());
102
108
  metaWriter.close();
103
109
  }