zcatalyst-cli 1.17.6 → 1.18.0-beta.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 (124) hide show
  1. package/docs/commands/event/generate/job.toml +7 -0
  2. package/docs/commands/functions/shell.toml +4 -0
  3. package/docs/endpoints/lib/job-scheduling.toml +3 -0
  4. package/docs/internal/command.toml +5 -0
  5. package/docs/shell/dependencies/tunnel-server.toml +4 -0
  6. package/docs/shell/index.toml +7 -0
  7. package/lib/appsail-utils.js +3 -3
  8. package/lib/authentication/index.js +1 -0
  9. package/lib/command_needs/rc.js +1 -1
  10. package/lib/commands/appsail/add.js +1 -0
  11. package/lib/commands/client/setup.js +1 -0
  12. package/lib/commands/event/generate/job.js +82 -0
  13. package/lib/commands/functions/add.js +1 -0
  14. package/lib/commands/functions/delete.js +2 -4
  15. package/lib/commands/functions/setup.js +1 -0
  16. package/lib/commands/functions/shell.js +98 -1
  17. package/lib/commands/index.js +1 -0
  18. package/lib/commands/init.js +2 -6
  19. package/lib/commands/login.js +1 -0
  20. package/lib/commands/logout.js +1 -0
  21. package/lib/commands/pull.js +1 -0
  22. package/lib/commands/serve.js +2 -8
  23. package/lib/commands/whoami.js +1 -0
  24. package/lib/deploy/features/appsail/index.js +19 -17
  25. package/lib/deploy/features/appsail/utils.js +4 -5
  26. package/lib/endpoints/index.js +15 -1
  27. package/lib/endpoints/lib/appsail.js +7 -1
  28. package/lib/endpoints/lib/job-scheduling.js +61 -0
  29. package/lib/endpoints/lib/sdk.js +2 -2
  30. package/lib/endpoints/lib/tunnel.js +66 -0
  31. package/lib/express_middlewares/logger.js +2 -4
  32. package/lib/fn-utils/lib/common.js +2 -3
  33. package/lib/fn-utils/lib/java.js +1 -1
  34. package/lib/fn-utils/lib/python.js +2 -2
  35. package/lib/fn-watcher.js +1 -1
  36. package/lib/init/dependencies/python/ensure-python.js +6 -8
  37. package/lib/init/features/appsail/index.js +14 -10
  38. package/lib/init/features/functions/index.js +4 -0
  39. package/lib/init/features/functions/languages/python.js +21 -5
  40. package/lib/internal/api.js +17 -1
  41. package/lib/internal/command.js +28 -5
  42. package/lib/port-resolver.js +1 -1
  43. package/lib/repl-server.js +9 -0
  44. package/lib/serve/features/appsail.js +2 -3
  45. package/lib/serve/index.js +1 -2
  46. package/lib/serve/server/index.js +8 -9
  47. package/lib/serve/server/lib/appsail/index.js +24 -34
  48. package/lib/serve/server/lib/java/aio_server/lib/catalyst-cli-java-runtime-1.0.0.jar +0 -0
  49. package/lib/serve/server/lib/java/aio_server/lib/catalyst-function-sdk-1.0.0.jar +0 -0
  50. package/lib/serve/server/lib/java/aio_server/lib/catalyst-java-runtime-1.0.0.jar +0 -0
  51. package/lib/serve/server/lib/java/index.js +2 -2
  52. package/lib/serve/server/lib/master/appsail.js +53 -0
  53. package/lib/serve/server/lib/master/functions.js +34 -0
  54. package/lib/serve/server/lib/master/index.js +155 -0
  55. package/lib/{express_middlewares/unknownReqProxy.js → serve/server/lib/master/unknown-req-proxy.js} +4 -7
  56. package/lib/serve/server/lib/master/utils.js +130 -0
  57. package/lib/serve/server/lib/master/web-client.js +39 -0
  58. package/lib/serve/server/lib/node/index.js +3 -3
  59. package/lib/serve/server/lib/python/index.js +6 -3
  60. package/lib/shell/dependencies/http-functions.js +21 -8
  61. package/lib/shell/dependencies/invoker/bio/java/JavabioInvoker.java +28 -32
  62. package/lib/shell/dependencies/invoker/bio/java/lib/catalyst-function-sdk-1.0.0.jar +0 -0
  63. package/lib/shell/dependencies/invoker/bio/node.mjs +2 -2
  64. package/lib/shell/dependencies/invoker/cron/java/JavacronInvoker.java +62 -13
  65. package/lib/shell/dependencies/invoker/cron/java/lib/catalyst-function-sdk-1.0.0.jar +0 -0
  66. package/lib/shell/dependencies/invoker/cron/node.mjs +28 -24
  67. package/lib/shell/dependencies/invoker/event/java/JavaeventInvoker.java +41 -6
  68. package/lib/shell/dependencies/invoker/event/java/lib/catalyst-function-sdk-1.0.0.jar +0 -0
  69. package/lib/shell/dependencies/invoker/event/node.mjs +23 -9
  70. package/lib/shell/dependencies/invoker/integ/java/JavaintegInvoker.java +3 -3
  71. package/lib/shell/dependencies/invoker/integ/java/lib/catalyst-function-sdk-1.0.0.jar +0 -0
  72. package/lib/shell/dependencies/invoker/integ/node.mjs +6 -6
  73. package/lib/shell/dependencies/invoker/job/java/JavajobInvoker.java +254 -0
  74. package/lib/shell/dependencies/invoker/job/java/lib/catalyst-function-sdk-1.0.0.jar +0 -0
  75. package/lib/shell/dependencies/invoker/job/java/lib/org.json.jar +0 -0
  76. package/lib/shell/dependencies/invoker/job/node.mjs +93 -0
  77. package/lib/shell/dependencies/local-function.js +399 -200
  78. package/lib/shell/dependencies/tunnel-server.js +173 -0
  79. package/lib/shell/index.js +62 -2
  80. package/lib/shell/prepare/languages/index.js +4 -4
  81. package/lib/shell/prepare/languages/java.js +1 -2
  82. package/lib/shell/prepare/languages/python.js +10 -16
  83. package/lib/throbber/index.js +6 -1
  84. package/lib/userConfig.js +7 -1
  85. package/lib/util_modules/config/index.js +1 -1
  86. package/lib/util_modules/config/lib/{appSail.js → appsail.js} +61 -9
  87. package/lib/util_modules/config/lib/client.js +6 -8
  88. package/lib/util_modules/config/lib/functions.js +6 -8
  89. package/lib/util_modules/constants/index.js +3 -1
  90. package/lib/util_modules/constants/lib/default.js +1 -1
  91. package/lib/util_modules/constants/lib/fn-type.js +2 -1
  92. package/lib/util_modules/constants/lib/needed-scopes.js +61 -0
  93. package/lib/util_modules/constants/lib/placeholders.js +6 -0
  94. package/lib/util_modules/constants/lib/ref-mapping.js +2 -1
  95. package/lib/util_modules/constants/lib/remote-mapping.js +2 -1
  96. package/lib/util_modules/constants/lib/scopes.js +9 -1
  97. package/lib/util_modules/constants/lib/template.js +9 -4
  98. package/lib/util_modules/constants/lib/urls.js +16 -0
  99. package/lib/util_modules/fs/lib/async.js +7 -2
  100. package/lib/util_modules/fs/lib/sync.js +12 -1
  101. package/lib/util_modules/server.js +2 -4
  102. package/lib/winston.js +17 -11
  103. package/package.json +1 -1
  104. package/templates/event/job.json +30 -0
  105. package/templates/init/functions/java/job/.classpath +6 -0
  106. package/templates/init/functions/java/job/.project +17 -0
  107. package/templates/init/functions/java/job/catalyst-config.json +11 -0
  108. package/templates/init/functions/java/job/sample.java +34 -0
  109. package/templates/init/functions/node/aio/package.json +1 -1
  110. package/templates/init/functions/node/bio/package.json +1 -1
  111. package/templates/init/functions/node/cron/package.json +1 -1
  112. package/templates/init/functions/node/event/package.json +1 -1
  113. package/templates/init/functions/node/integ/cliq/package.json +1 -1
  114. package/templates/init/functions/node/integ/convokraft/package.json +1 -1
  115. package/templates/init/functions/node/{stream → job}/catalyst-config.json +3 -2
  116. package/templates/init/functions/node/{stream → job}/package.json +1 -1
  117. package/templates/init/functions/node/job/sample.js +28 -0
  118. package/templates/init/functions/node/job/types/job.d.ts +66 -0
  119. package/templates/init/functions/python/job/catalyst-config.json +11 -0
  120. package/templates/init/functions/python/job/requirements.txt +1 -0
  121. package/templates/init/functions/python/job/sample.py +23 -0
  122. package/templates/init.txt +13 -0
  123. package/lib/serve/server/lib/master.js +0 -317
  124. package/templates/init/functions/node/stream/sample.js +0 -15
@@ -24,7 +24,6 @@ const archiver_1 = __importDefault(require("../../../../archiver"));
24
24
  const option_1 = require("../../../../util_modules/option");
25
25
  const execute_script_1 = require("../../../../execute-script");
26
26
  const master_1 = __importDefault(require("../master"));
27
- const ansi_colors_1 = require("ansi-colors");
28
27
  function executeHook(script, name, moduleSource) {
29
28
  if ((0, option_1.getOptionValue)('ignoreScripts', false)) {
30
29
  (0, logger_1.debug)(`skipping ${name} hook`);
@@ -48,46 +47,37 @@ exports.default = (serverDetails) => __awaiter(void 0, void 0, void 0, function*
48
47
  var _a, _b, _c, _d;
49
48
  let child;
50
49
  const targetSail = serverDetails.target;
51
- if (!targetSail || !targetSail.appSail) {
50
+ if (!targetSail || !targetSail.config) {
52
51
  throw new error_1.default('AppSail details not found');
53
52
  }
54
- if ((_a = targetSail.appSail.scripts) === null || _a === void 0 ? void 0 : _a.preserve) {
55
- executeHook(targetSail.appSail.scripts.preserve, `AppSail [PRESERVE] [${(_b = targetSail.appSail) === null || _b === void 0 ? void 0 : _b.name}]`, targetSail.appSail.source);
56
- }
57
- const buildPath = targetSail.appSail.buildPath
58
- ? targetSail.appSail.buildPath
59
- : (_c = targetSail.appSail) === null || _c === void 0 ? void 0 : _c.build_path;
60
- if (!(yield fs_1.ASYNC.isPathExists(buildPath))) {
61
- throw new error_1.default(`Build path does not exists [${buildPath}]`, {
62
- exit: 1,
63
- errorId: 'SERVE-APPSAIL-1',
64
- arg: [ansi_colors_1.italic.red(buildPath), (0, ansi_colors_1.bold)(targetSail.appSail.name)]
65
- });
53
+ if ((_a = targetSail.config.scripts) === null || _a === void 0 ? void 0 : _a.preserve) {
54
+ executeHook(targetSail.config.scripts.preserve, `AppSail [PRESERVE] [${targetSail.name}]`, targetSail.source);
66
55
  }
56
+ const buildPath = (_b = targetSail.config) === null || _b === void 0 ? void 0 : _b.build_path;
67
57
  const target = (yield fs_1.ASYNC.fileExists(buildPath)) ? (0, path_1.dirname)(buildPath) : buildPath;
68
58
  switch (true) {
69
- case targetSail.appSail.stack.startsWith('node'): {
59
+ case targetSail.config.stack.startsWith('node'): {
70
60
  child = startAppSail(targetSail.port.appsail, {
71
61
  type: 'nodejs',
72
62
  target,
73
- command: targetSail.appSail.command,
74
- memory: targetSail.appSail.memory || 256,
75
- env: targetSail.appSail.env_variables
63
+ command: targetSail.config.command,
64
+ memory: targetSail.config.memory || 256,
65
+ env: targetSail.config.env_variables
76
66
  });
77
67
  break;
78
68
  }
79
- case targetSail.appSail.stack.startsWith('python'): {
69
+ case targetSail.config.stack.startsWith('python'): {
80
70
  child = startAppSail(targetSail.port.appsail, {
81
71
  type: 'python',
82
72
  target,
83
- command: targetSail.appSail.command,
84
- memory: targetSail.appSail.memory || 256,
85
- env: targetSail.appSail.env_variables
73
+ command: targetSail.config.command,
74
+ memory: targetSail.config.memory || 256,
75
+ env: targetSail.config.env_variables
86
76
  });
87
77
  break;
88
78
  }
89
- case targetSail.appSail.stack.startsWith('java'): {
90
- if (targetSail.appSail.platform === 'war') {
79
+ case targetSail.config.stack.startsWith('java'): {
80
+ if (targetSail.config.platform === 'war') {
91
81
  const jettyPath = (0, path_1.join)(constants_1.ENVPATH.runtimes.data, 'jetty');
92
82
  if (!(yield fs_1.ASYNC.fileExists((0, path_1.join)(jettyPath, 'start.jar')).catch((err) => (0, logger_1.debug)(err)))) {
93
83
  try {
@@ -112,17 +102,17 @@ exports.default = (serverDetails) => __awaiter(void 0, void 0, void 0, function*
112
102
  target,
113
103
  type: 'war',
114
104
  command: jettyCommand,
115
- memory: targetSail.appSail.memory || 256,
116
- env: Object.assign({ JETTY_BASE: (0, path_1.join)(jettyPath, 'JETTY_BASE') }, targetSail.appSail.env_variables)
105
+ memory: targetSail.config.memory || 256,
106
+ env: Object.assign({ JETTY_BASE: (0, path_1.join)(jettyPath, 'JETTY_BASE') }, targetSail.config.env_variables)
117
107
  });
118
108
  break;
119
109
  }
120
110
  child = startAppSail(targetSail.port.appsail, {
121
111
  target,
122
- command: targetSail.appSail.command,
112
+ command: targetSail.config.command,
123
113
  type: 'javase',
124
- memory: targetSail.appSail.memory || 256,
125
- env: targetSail.appSail.env_variables
114
+ memory: targetSail.config.memory || 256,
115
+ env: targetSail.config.env_variables
126
116
  });
127
117
  break;
128
118
  }
@@ -131,7 +121,7 @@ exports.default = (serverDetails) => __awaiter(void 0, void 0, void 0, function*
131
121
  }
132
122
  }
133
123
  const masterServe = yield (0, master_1.default)(targetSail.port.proxy, {
134
- appSail: [serverDetails]
124
+ appSailDetails: serverDetails
135
125
  });
136
126
  child.once('exit', () => {
137
127
  masterServe.close((err) => {
@@ -140,13 +130,13 @@ exports.default = (serverDetails) => __awaiter(void 0, void 0, void 0, function*
140
130
  }
141
131
  });
142
132
  });
143
- if ((_d = targetSail.appSail.scripts) === null || _d === void 0 ? void 0 : _d.postserve) {
133
+ if ((_d = (_c = targetSail.config) === null || _c === void 0 ? void 0 : _c.scripts) === null || _d === void 0 ? void 0 : _d.postserve) {
144
134
  child.once('exit', () => {
145
- var _a, _b, _c, _d;
146
- if (!((_a = targetSail.appSail) === null || _a === void 0 ? void 0 : _a.scripts)) {
135
+ var _a, _b, _c;
136
+ if (!((_a = targetSail.config) === null || _a === void 0 ? void 0 : _a.scripts)) {
147
137
  return;
148
138
  }
149
- executeHook((_b = targetSail.appSail.scripts) === null || _b === void 0 ? void 0 : _b.postserve, `AppSail [POSTSERVE] [${(_c = targetSail.appSail) === null || _c === void 0 ? void 0 : _c.name}]`, (_d = targetSail.appSail) === null || _d === void 0 ? void 0 : _d.source);
139
+ executeHook((_c = (_b = targetSail.config) === null || _b === void 0 ? void 0 : _b.scripts) === null || _c === void 0 ? void 0 : _c.postserve, `AppSail [POSTSERVE] [${targetSail.name}]`, targetSail.source);
150
140
  });
151
141
  }
152
142
  return child;
@@ -23,7 +23,7 @@ const logger_1 = require("../../../../util_modules/logger");
23
23
  const project_1 = require("../../../../util_modules/project");
24
24
  const shell_1 = require("../../../../util_modules/shell");
25
25
  exports.default = (details, masterPort) => __awaiter(void 0, void 0, void 0, function* () {
26
- var _a, _b, _c, _d, _e;
26
+ var _a, _b, _c, _d, _e, _f;
27
27
  const projectRoot = runtime_store_1.default.get('project.root');
28
28
  const javaServer = (0, path_1.join)(projectRoot, constants_1.FOLDERNAME.build, '.catalyst', 'aioserver', 'JavaaioServer');
29
29
  const targetDir = (0, path_1.join)(projectRoot, constants_1.FOLDERNAME.build, constants_1.FOLDERNAME.functions, (_a = details.target) === null || _a === void 0 ? void 0 : _a.name);
@@ -54,6 +54,6 @@ exports.default = (details, masterPort) => __awaiter(void 0, void 0, void 0, fun
54
54
  return (0, shell_1.spawn)(spawnCommand, opts, {
55
55
  cwd: targetSource === null || targetSource === void 0 ? void 0 : targetSource.replace(projectRoot, (0, path_1.join)(projectRoot, constants_1.FOLDERNAME.build)),
56
56
  stdio: 'pipe',
57
- env: Object.assign({ X_ZOHO_CATALYST_IS_LOCAL: 'true', X_ZOHO_CATALYST_FUNCTION_LOADED: 'true', X_ZOHO_CATALYST_CODE_LOCATION: targetDir + path_1.sep, X_ZOHO_CATALYST_ACCOUNTS_URL: constants_1.ORIGIN.auth, X_ZOHO_CATALYST_CONSOLE_URL: constants_1.ORIGIN.admin, CATALYST_PROJECT_TIMEZONE: (0, project_1.getProjectTimezone)(Intl.DateTimeFormat().resolvedOptions().timeZone) }, details.target.env_var)
57
+ env: Object.assign({ X_ZOHO_CATALYST_IS_LOCAL: 'true', X_ZOHO_CATALYST_FUNCTION_LOADED: 'true', X_ZOHO_CATALYST_CODE_LOCATION: targetDir + path_1.sep, X_ZOHO_CATALYST_ACCOUNTS_URL: constants_1.ORIGIN.auth, X_ZOHO_CATALYST_CONSOLE_URL: constants_1.ORIGIN.admin, X_ZOHO_CATALYST_RESOURCE_ID: (_f = details.target) === null || _f === void 0 ? void 0 : _f.id, CATALYST_PROJECT_TIMEZONE: (0, project_1.getProjectTimezone)(Intl.DateTimeFormat().resolvedOptions().timeZone) }, details.target.env_var)
58
58
  }).RAW();
59
59
  });
@@ -0,0 +1,53 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.addAppSailRoutes = void 0;
13
+ const utils_1 = require("./utils");
14
+ function addAppSailRoutes(app, details, proxy, unknownProxy) {
15
+ var _a, _b;
16
+ app.use('/__catalyst/sdk/init.js', (req, res) => __awaiter(this, void 0, void 0, function* () {
17
+ const initJs = yield (0, utils_1.appsailInitJs)();
18
+ const _write = res.write;
19
+ res.write = () => {
20
+ res.setHeader('Content-Length', initJs.length);
21
+ return _write.call(res, initJs, 'utf-8');
22
+ };
23
+ req.url = req.originalUrl;
24
+ unknownProxy(req, res);
25
+ }));
26
+ app.use('/baas/logout', (req, res) => {
27
+ req.url = req.originalUrl;
28
+ unknownProxy(req, res);
29
+ });
30
+ ((_b = (_a = details.target) === null || _a === void 0 ? void 0 : _a.config) === null || _b === void 0 ? void 0 : _b.catalyst_auth) === true &&
31
+ app.use(['/__catalyst', '/accounts', '/oauthorize'], (req, res) => {
32
+ if (req.url.includes('/appsail/logout_redirect')) {
33
+ res.redirect(typeof req.query.service_url === 'string' ? req.query.service_url : '/');
34
+ return;
35
+ }
36
+ req.url = req.originalUrl;
37
+ unknownProxy(req, res);
38
+ });
39
+ app.use('/', (req, res) => __awaiter(this, void 0, void 0, function* () {
40
+ var _c, _d;
41
+ if (req.header('catalyst-component') === 'true') {
42
+ ((_d = (_c = details.target) === null || _c === void 0 ? void 0 : _c.config) === null || _d === void 0 ? void 0 : _d.catalyst_auth) === true
43
+ ? unknownProxy(req, res)
44
+ : res.sendStatus(400);
45
+ return;
46
+ }
47
+ proxy.web(req, res, {
48
+ target: `http://127.0.0.1:${details.target.port.appsail}`,
49
+ ws: true
50
+ });
51
+ }));
52
+ }
53
+ exports.addAppSailRoutes = addAppSailRoutes;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addFnRoutes = void 0;
4
+ const constants_1 = require("../../../../util_modules/constants");
5
+ function addFnRoutes(app, details, proxy) {
6
+ const fnTarget = details.target;
7
+ app.use(`/server/${fnTarget.name}`, (req, res) => {
8
+ proxy.web(req, res, {
9
+ target: `http://127.0.0.1:${details.httpPort}${fnTarget.type === constants_1.FN_TYPE.basic ? `/server/${fnTarget.name}/execute` : ''}`,
10
+ ws: true
11
+ });
12
+ });
13
+ app.use(`/baas/v1/project/:projectId/function/${fnTarget.name}`, (req, res) => {
14
+ proxy.web(req, res, {
15
+ target: `http://127.0.0.1:${details.httpPort}${fnTarget.type === constants_1.FN_TYPE.basic ? `/server/${fnTarget.name}/execute` : ''}`,
16
+ ws: true
17
+ });
18
+ });
19
+ if (fnTarget.id) {
20
+ app.use(`/server/${fnTarget.id}`, (req, res) => {
21
+ proxy.web(req, res, {
22
+ target: `http://127.0.0.1:${details.httpPort}${fnTarget.type === constants_1.FN_TYPE.basic ? `/server/${fnTarget.id}/execute` : ''}`,
23
+ ws: true
24
+ });
25
+ });
26
+ app.use(`/baas/v1/project/:projectId/function/${fnTarget.id}`, (req, res) => {
27
+ proxy.web(req, res, {
28
+ target: `http://127.0.0.1:${details.httpPort}${fnTarget.type === constants_1.FN_TYPE.basic ? `/server/${fnTarget.id}/execute` : ''}`,
29
+ ws: true
30
+ });
31
+ });
32
+ }
33
+ }
34
+ exports.addFnRoutes = addFnRoutes;
@@ -0,0 +1,155 @@
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 error_1 = __importDefault(require("../../../../error"));
17
+ const apig_matcher_1 = __importDefault(require("../../../../express_middlewares/apig-matcher"));
18
+ const auth_checker_1 = __importDefault(require("../../../../express_middlewares/auth-checker"));
19
+ const authenticator_1 = __importDefault(require("../../../../express_middlewares/authenticator"));
20
+ const cookie_parser_1 = __importDefault(require("../../../../express_middlewares/cookie-parser"));
21
+ const error_handler_1 = __importDefault(require("../../../../express_middlewares/error-handler"));
22
+ const logger_1 = __importDefault(require("../../../../express_middlewares/logger"));
23
+ const project_1 = __importDefault(require("../../../../express_middlewares/project"));
24
+ const unknown_req_proxy_1 = __importDefault(require("./unknown-req-proxy"));
25
+ const url_rewriter_1 = __importDefault(require("../../../../express_middlewares/url-rewriter"));
26
+ const runtime_store_1 = __importDefault(require("../../../../runtime-store"));
27
+ const constants_1 = require("../../../../util_modules/constants");
28
+ const js_1 = require("../../../../util_modules/js");
29
+ const logger_2 = require("../../../../util_modules/logger");
30
+ const project_2 = require("../../../../util_modules/project");
31
+ const option_1 = require("../../../../util_modules/option");
32
+ const server_js_1 = require("../../../../util_modules/server.js");
33
+ const utils_1 = require("./utils");
34
+ const appsail_1 = require("./appsail");
35
+ const web_client_1 = require("./web-client");
36
+ const functions_1 = require("./functions");
37
+ function spinUpMaster(listenPort, { otherServerDetails, appSailDetails }) {
38
+ var _a, _b;
39
+ return __awaiter(this, void 0, void 0, function* () {
40
+ if ((otherServerDetails && appSailDetails) || (!otherServerDetails && !appSailDetails)) {
41
+ throw new error_1.default('Either functions/client or AppSail is need to start the master server. But both cannot use the same master', { exit: 2 });
42
+ }
43
+ const projectDetails = {
44
+ id: (0, project_2.getProjectId)(),
45
+ domain_prefix: (0, project_2.getDomainPrefix)(),
46
+ domain: constants_1.ORIGIN.app.replace('https://', ''),
47
+ key: (0, project_2.getDomainKey)(),
48
+ env_name: (0, project_2.getEnvName)()
49
+ };
50
+ const rules = runtime_store_1.default.get('context.apig.local.config', -1);
51
+ const customProxyUrl = (0, option_1.getOptionValue)('proxy');
52
+ const app = (0, express_1.default)();
53
+ let systemRoutes;
54
+ const nonApigURL = ['/accounts', '/clientoauth', '/_wms', '/wmssrv'];
55
+ app.use((0, logger_1.default)(appSailDetails));
56
+ app.use(cookie_parser_1.default);
57
+ app.use((0, project_1.default)(projectDetails));
58
+ app.use(authenticator_1.default);
59
+ !appSailDetails &&
60
+ app.use(nonApigURL, (req, res) => {
61
+ req.url = req.originalUrl;
62
+ unknownProxy(req, res);
63
+ });
64
+ !appSailDetails &&
65
+ app.use('*/.catalyst%2Freload-script.js', (req, res) => {
66
+ var _a;
67
+ req.url = '/.catalyst%2Freload-script.js';
68
+ (_a = otherServerDetails === null || otherServerDetails === void 0 ? void 0 : otherServerDetails.client) === null || _a === void 0 ? void 0 : _a.forEach((clientDetails) => {
69
+ const appTarget = `http://127.0.0.1:${clientDetails.httpPort}/app/`;
70
+ proxy.web(req, res, {
71
+ target: appTarget,
72
+ changeOrigin: true
73
+ });
74
+ });
75
+ });
76
+ if (rules !== -1 && !appSailDetails) {
77
+ for (const [sourceURL, methodByConfig] of Object.entries(rules)) {
78
+ const router = app.route(sourceURL);
79
+ for (const [method, config] of Object.entries(methodByConfig)) {
80
+ if (config.system) {
81
+ if (systemRoutes === undefined) {
82
+ systemRoutes = {};
83
+ }
84
+ js_1.JS.set(systemRoutes, [sourceURL, method], config);
85
+ }
86
+ router[method]((req, res, next) => {
87
+ if (res.locals.apigRules === undefined) {
88
+ res.locals.apigRules = [];
89
+ }
90
+ res.locals.apigRules.push(Object.assign({ source_endpoint: sourceURL, params: req.params }, config));
91
+ next();
92
+ });
93
+ }
94
+ }
95
+ app.use('/', apig_matcher_1.default, auth_checker_1.default, url_rewriter_1.default);
96
+ }
97
+ const proxy = (0, utils_1.createProxyServer)(listenPort);
98
+ appSailDetails && ((_b = (_a = appSailDetails.target) === null || _a === void 0 ? void 0 : _a.config) === null || _b === void 0 ? void 0 : _b.catalyst_auth) === true
99
+ ? proxy.on('proxyRes', (0, utils_1.proxyResponseHandler)({
100
+ signInRedirect: appSailDetails.target.config.login_redirect || '/'
101
+ }))
102
+ : proxy.on('proxyRes', (0, utils_1.proxyResponseHandler)({ systemRoutes }));
103
+ const proxyDestroyer = new server_js_1.ConnectionDestroyer(proxy);
104
+ const unknownProxy = (0, unknown_req_proxy_1.default)(proxy, listenPort, customProxyUrl);
105
+ otherServerDetails &&
106
+ Object.keys(otherServerDetails).forEach((targ) => {
107
+ var _a, _b, _c;
108
+ switch (targ) {
109
+ case 'client': {
110
+ (_a = otherServerDetails.client) === null || _a === void 0 ? void 0 : _a.forEach((clientDetails) => (0, web_client_1.addWebClientRoutes)(app, clientDetails, proxy, unknownProxy));
111
+ break;
112
+ }
113
+ case 'server': {
114
+ (_b = otherServerDetails.server) === null || _b === void 0 ? void 0 : _b.forEach((serverDetail) => {
115
+ var _a;
116
+ app.use('/server/' + ((_a = serverDetail.target) === null || _a === void 0 ? void 0 : _a.name), (req, res) => {
117
+ proxy.web(req, res, {
118
+ target: `http://127.0.0.1:${serverDetail.httpPort}`
119
+ });
120
+ });
121
+ });
122
+ break;
123
+ }
124
+ case 'functions': {
125
+ (_c = otherServerDetails.functions) === null || _c === void 0 ? void 0 : _c.forEach((fnDetails) => (0, functions_1.addFnRoutes)(app, fnDetails, proxy));
126
+ break;
127
+ }
128
+ default:
129
+ throw new error_1.default('Unknown type provided to master server ' + targ, {
130
+ exit: 2
131
+ });
132
+ }
133
+ });
134
+ appSailDetails && (0, appsail_1.addAppSailRoutes)(app, appSailDetails, proxy, unknownProxy);
135
+ app.use('/', (0, unknown_req_proxy_1.default)(proxy, listenPort, customProxyUrl));
136
+ app.use(error_handler_1.default);
137
+ const expressServer = app.listen(listenPort, '127.0.0.1').on('error', (err) => {
138
+ if (proxy) {
139
+ proxyDestroyer.destroy();
140
+ }
141
+ console.error(err);
142
+ });
143
+ const expressDestroyer = new server_js_1.ConnectionDestroyer(expressServer);
144
+ expressServer.maxConnections = 100;
145
+ process.on('SIGINT', () => expressDestroyer.destroy(false));
146
+ expressServer.on('close', () => __awaiter(this, void 0, void 0, function* () {
147
+ yield proxyDestroyer.destroy().catch((err) => (0, logger_2.debug)(err));
148
+ }));
149
+ expressServer.on('upgrade', (...upgradeParam) => {
150
+ app.emit('upgrade', ...upgradeParam);
151
+ });
152
+ return expressServer;
153
+ });
154
+ }
155
+ exports.default = spinUpMaster;
@@ -3,9 +3,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const runtime_store_1 = __importDefault(require("../runtime-store"));
7
- const logger_1 = require("../util_modules/logger");
8
- exports.default = (proxyInstance, customProxyUrl) => (req, res) => {
6
+ const runtime_store_1 = __importDefault(require("../../../../runtime-store"));
7
+ const logger_1 = require("../../../../util_modules/logger");
8
+ exports.default = (proxyInstance, masterPort, customProxyUrl) => (req, res) => {
9
9
  if (req.url.length === 1 && req.url.startsWith('/')) {
10
10
  res.redirect('/app/');
11
11
  }
@@ -34,16 +34,13 @@ exports.default = (proxyInstance, customProxyUrl) => (req, res) => {
34
34
  headers['origin'] = `https://${req.headers['x-zc-project-domain']}`;
35
35
  }
36
36
  if (req.url.includes('/__catalyst/auth/')) {
37
- const masterPort = runtime_store_1.default.get('context.port.http.master', 3000);
38
- headers['catalyst-redirect-domain'] = `http://localhost:${masterPort}`;
37
+ headers['catalyst-redirect-domain'] = `http://localhost:${masterPort || 3000}`;
39
38
  }
40
39
  proxyInstance.web(req, res, {
41
40
  target: `https://${req.headers['x-zc-project-domain']}`,
42
41
  changeOrigin: true,
43
42
  ws: true,
44
43
  headers
45
- }, (err) => {
46
- (0, logger_1.debug)('Unknown proxy error: ', err);
47
44
  });
48
45
  }
49
46
  };
@@ -0,0 +1,130 @@
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
+ exports.appsailInitJs = exports.proxyResponseHandler = exports.createProxyServer = exports.redirectByAuth = exports.removeSecure = void 0;
16
+ const http_1 = require("http");
17
+ const constants_1 = require("../../../../util_modules/constants");
18
+ const fs_1 = require("../../../../util_modules/fs");
19
+ const error_1 = __importDefault(require("../../../../error"));
20
+ const project_1 = require("../../../../util_modules/project");
21
+ const http_proxy_1 = require("http-proxy");
22
+ const logger_1 = require("../../../../util_modules/logger");
23
+ const removeSecure = (str) => str.replace(/;\s?Secure/i, '').replace(/;\s?SameSite=None/i, '');
24
+ exports.removeSecure = removeSecure;
25
+ const redirectByAuth = (req, res, redirectUrl) => {
26
+ if (req.headers['x-zc-user-cred-token'] === undefined) {
27
+ res.writeHead(302, { Location: redirectUrl });
28
+ res.end();
29
+ }
30
+ else {
31
+ res.writeHead(302, { Location: '/app/' });
32
+ res.end();
33
+ }
34
+ };
35
+ exports.redirectByAuth = redirectByAuth;
36
+ const createProxyServer = (port) => {
37
+ const proxy = (0, http_proxy_1.createProxyServer)({
38
+ hostRewrite: `localhost:${port}`,
39
+ protocolRewrite: 'http'
40
+ })
41
+ .on('error', (err, req, res) => {
42
+ if (res.writableEnded) {
43
+ return;
44
+ }
45
+ if (res instanceof http_1.ServerResponse) {
46
+ res.writeHead(502, { 'Content-Type': 'json' });
47
+ }
48
+ res.end(JSON.stringify({ error: 'unable to serve the request' }));
49
+ console.log();
50
+ console.log('Unable to reach the servers to serve the request: ', req.url);
51
+ console.error(err);
52
+ })
53
+ .on('econnreset', (err, req, res, targetUrl) => {
54
+ if (res.writableEnded) {
55
+ return;
56
+ }
57
+ if (res instanceof http_1.ServerResponse) {
58
+ res.writeHead(502, { 'Content-Type': 'json' });
59
+ }
60
+ res.end(JSON.stringify({ error: 'unable to serve the request. ECONNRESET.' }));
61
+ console.log();
62
+ console.log('Connection reset while trying to serve the request ', req.url);
63
+ (0, logger_1.debug)('Connection reset from target URL : ', targetUrl);
64
+ console.error(err);
65
+ })
66
+ .on('close', () => (0, logger_1.debug)('proxy closed'));
67
+ return proxy;
68
+ };
69
+ exports.createProxyServer = createProxyServer;
70
+ const proxyResponseHandler = ({ systemRoutes, signInRedirect = '/app/local-redirect' }) => (proxyRes, req, res) => {
71
+ if (!req.url) {
72
+ return;
73
+ }
74
+ if (req.url.startsWith('/oauthorize')) {
75
+ const set = proxyRes.headers['set-cookie'];
76
+ if (set) {
77
+ const result = Array.isArray(set) ? set.map(exports.removeSecure) : (0, exports.removeSecure)(set);
78
+ proxyRes.headers['set-cookie'] = result;
79
+ }
80
+ }
81
+ else if (req.url.startsWith('/baas') || req.url.startsWith('/__catalyst')) {
82
+ if (req.url.includes('/signin-redirect')) {
83
+ if (systemRoutes === undefined) {
84
+ (0, exports.redirectByAuth)(req, res, signInRedirect);
85
+ return;
86
+ }
87
+ const sourceRedirectUrl = Object.keys(systemRoutes).find((sourceUrl) => {
88
+ const methodByConfig = systemRoutes ? systemRoutes[sourceUrl] : undefined;
89
+ if (methodByConfig) {
90
+ return !!Object.values(methodByConfig).find((config) => {
91
+ return config.name === 'Login Redirect';
92
+ });
93
+ }
94
+ return false;
95
+ });
96
+ if (sourceRedirectUrl === undefined) {
97
+ res.end('INVALID_URL');
98
+ return;
99
+ }
100
+ (0, exports.redirectByAuth)(req, res, sourceRedirectUrl);
101
+ }
102
+ else if (req.url.includes('/logout')) {
103
+ const set = proxyRes.headers['set-cookie'];
104
+ if (set) {
105
+ const result = set.map((cookie) => cookie.replace(new RegExp('Domain=[\\S^;]*', 'g'), 'Domain=localhost;'));
106
+ proxyRes.headers['set-cookie'] = result;
107
+ }
108
+ }
109
+ }
110
+ else if (req.url.startsWith('/accounts') && req.url.includes('/signin')) {
111
+ const set = proxyRes.headers['set-cookie'];
112
+ set
113
+ ? set.push('IAM_TEST_COOKIE=IAM_TEST_COOKIE; Domain=localhost; Path=/')
114
+ : ['IAM_TEST_COOKIE=IAM_TEST_COOKIE; Domain=localhost; Path=/'];
115
+ proxyRes.headers['set-cookie'] = set;
116
+ }
117
+ };
118
+ exports.proxyResponseHandler = proxyResponseHandler;
119
+ const appsailInitJs = () => __awaiter(void 0, void 0, void 0, function* () {
120
+ let initJsStr = yield fs_1.ASYNC.readFile(constants_1.TEMPLATE.init_js);
121
+ if (!initJsStr) {
122
+ throw new error_1.default('Unable to read the init_js file', { exit: 2 });
123
+ }
124
+ const feed = [(0, project_1.getProjectId)(), (0, project_1.getDomainKey)(), constants_1.ORIGIN.iamPortal, (0, project_1.getEnvId)()];
125
+ Object.values(constants_1.PLACEHOLDER.init_js).forEach((holder, idx) => {
126
+ initJsStr = initJsStr === null || initJsStr === void 0 ? void 0 : initJsStr.replace(new RegExp(holder, 'g'), feed[idx]);
127
+ });
128
+ return initJsStr;
129
+ });
130
+ exports.appsailInitJs = appsailInitJs;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addWebClientRoutes = void 0;
4
+ function addWebClientRoutes(app, details, proxy, unknownProxy) {
5
+ const appTarget = `http://127.0.0.1:${details.httpPort}/app/`;
6
+ app.use('/app', (req, res) => {
7
+ const target = details.target;
8
+ if (req.url === '/' && target.homepage.includes('/__catalyst')) {
9
+ req.url = target.homepage;
10
+ unknownProxy(req, res);
11
+ }
12
+ else if (req.url.startsWith('/local-redirect') && target.login_redirect) {
13
+ const isUrl = target.login_redirect.startsWith('/');
14
+ if (isUrl) {
15
+ res.redirect(target.login_redirect);
16
+ }
17
+ else {
18
+ res.redirect('/app/' + target.login_redirect);
19
+ }
20
+ }
21
+ else {
22
+ proxy.web(req, res, {
23
+ target: appTarget,
24
+ changeOrigin: true
25
+ });
26
+ }
27
+ });
28
+ const appTargetUrl = new URL(appTarget);
29
+ appTargetUrl.pathname = '';
30
+ appTargetUrl.protocol = 'ws';
31
+ app.addListener('upgrade', (...upgradeParam) => {
32
+ proxy.ws(upgradeParam[0], upgradeParam[1], upgradeParam[2], {
33
+ target: appTargetUrl.href,
34
+ ws: true,
35
+ changeOrigin: true
36
+ });
37
+ });
38
+ }
39
+ exports.addWebClientRoutes = addWebClientRoutes;
@@ -34,9 +34,9 @@ const logger_1 = require("../../../../util_modules/logger");
34
34
  const project_1 = require("../../../../util_modules/project");
35
35
  const shell_1 = require("../../../../util_modules/shell");
36
36
  exports.default = (details, masterPort) => __awaiter(void 0, void 0, void 0, function* () {
37
- var _a, _b, _c, _d, _e, _f;
37
+ var _a, _b, _c, _d, _e, _f, _g;
38
38
  const label = `${details.type}[${(_a = details.target) === null || _a === void 0 ? void 0 : _a.name}]`;
39
- const _g = details.target, { ['watcher']: _ } = _g, serialisableTarget = __rest(_g, ['watcher']);
39
+ const _h = details.target, { ['watcher']: _ } = _h, serialisableTarget = __rest(_h, ['watcher']);
40
40
  const projectRoot = runtime_store_1.default.get('project.root');
41
41
  serialisableTarget.index = serialisableTarget.index
42
42
  ? serialisableTarget.index.replace(projectRoot, (0, path_1.join)(projectRoot, constants_1.FOLDERNAME.build))
@@ -88,6 +88,6 @@ exports.default = (details, masterPort) => __awaiter(void 0, void 0, void 0, fun
88
88
  return (0, shell_1.spawn)('node', opts, {
89
89
  cwd: targetSource === null || targetSource === void 0 ? void 0 : targetSource.replace(projectRoot, (0, path_1.join)(projectRoot, constants_1.FOLDERNAME.build)),
90
90
  stdio: 'pipe',
91
- env: Object.assign({ NODE_PATH: targetSource + path_1.sep + 'node_modules', X_ZOHO_CATALYST_IS_LOCAL: 'true', X_ZOHO_CATALYST_FUNCTION_LOADED: 'true', X_ZOHO_CATALYST_ACCOUNTS_URL: constants_1.ORIGIN.auth, X_ZOHO_CATALYST_CONSOLE_URL: constants_1.ORIGIN.admin, CATALYST_PROJECT_TIMEZONE: (0, project_1.getProjectTimezone)(Intl.DateTimeFormat().resolvedOptions().timeZone) }, details.target.env_var)
91
+ env: Object.assign({ NODE_PATH: targetSource + path_1.sep + 'node_modules', X_ZOHO_CATALYST_IS_LOCAL: 'true', X_ZOHO_CATALYST_FUNCTION_LOADED: 'true', X_ZOHO_CATALYST_ACCOUNTS_URL: constants_1.ORIGIN.auth, X_ZOHO_CATALYST_CONSOLE_URL: constants_1.ORIGIN.admin, X_ZOHO_CATALYST_RESOURCE_ID: (_g = details.target) === null || _g === void 0 ? void 0 : _g.id, CATALYST_PROJECT_TIMEZONE: (0, project_1.getProjectTimezone)(Intl.DateTimeFormat().resolvedOptions().timeZone) }, details.target.env_var)
92
92
  }).RAW();
93
93
  });
@@ -32,17 +32,17 @@ exports.default = (details) => __awaiter(void 0, void 0, void 0, function* () {
32
32
  (0, path_1.join)(runtimesDir, constants_1.RUNTIME.language.python.value, `zcatalyst_runtime_${stackVersion === null || stackVersion === void 0 ? void 0 : stackVersion.replace('_', '')}`, 'main.py')
33
33
  ];
34
34
  return new Promise((resolve, reject) => {
35
- var _a;
35
+ var _a, _b;
36
36
  const child = (0, shell_1.spawn)(userConfig_1.default.get(`python${stackVersion}.bin`), opts, {
37
37
  cwd: targetSource === null || targetSource === void 0 ? void 0 : targetSource.replace(projectRoot, (0, path_1.join)(projectRoot, constants_1.FOLDERNAME.build)),
38
38
  stdio: 'pipe',
39
- env: Object.assign({ X_ZOHO_CATALYST_IS_LOCAL: 'true', X_ZOHO_CATALYST_FUNCTION_LOADED: 'true', X_ZOHO_CATALYST_ACCOUNTS_URL: constants_1.ORIGIN.auth, X_ZOHO_CATALYST_CONSOLE_URL: constants_1.ORIGIN.admin, X_ZOHO_CATALYST_CODE_LOCATION: targetSource === null || targetSource === void 0 ? void 0 : targetSource.replace(projectRoot, (0, path_1.join)(projectRoot, constants_1.FOLDERNAME.build)), X_ZOHO_CATALYST_SERVER_LISTEN_PORT: details.httpPort.toString(), CATALYST_PROJECT_TIMEZONE: (0, project_1.getProjectTimezone)(Intl.DateTimeFormat().resolvedOptions().timeZone) }, details.target.env_var)
39
+ env: Object.assign({ X_ZOHO_CATALYST_IS_LOCAL: 'true', X_ZOHO_CATALYST_FUNCTION_LOADED: 'true', X_ZOHO_CATALYST_ACCOUNTS_URL: constants_1.ORIGIN.auth, X_ZOHO_CATALYST_CONSOLE_URL: constants_1.ORIGIN.admin, X_ZOHO_CATALYST_RESOURCE_ID: (_a = details.target) === null || _a === void 0 ? void 0 : _a.id, X_ZOHO_CATALYST_CODE_LOCATION: targetSource === null || targetSource === void 0 ? void 0 : targetSource.replace(projectRoot, (0, path_1.join)(projectRoot, constants_1.FOLDERNAME.build)), X_ZOHO_CATALYST_SERVER_LISTEN_PORT: details.httpPort.toString(), CATALYST_PROJECT_TIMEZONE: (0, project_1.getProjectTimezone)(Intl.DateTimeFormat().resolvedOptions().timeZone) }, details.target.env_var)
40
40
  }).RAW();
41
41
  child.on('spawn', () => __awaiter(void 0, void 0, void 0, function* () {
42
42
  yield (0, http_functions_1.checkIfRuntimeServerRunning)(details.httpPort.toString());
43
43
  }));
44
44
  const errData = [];
45
- (_a = child.stderr) === null || _a === void 0 ? void 0 : _a.on('data', (chunk) => {
45
+ (_b = child.stderr) === null || _b === void 0 ? void 0 : _b.on('data', (chunk) => {
46
46
  errData.push(chunk);
47
47
  });
48
48
  child.on('error', (err) => {
@@ -52,6 +52,9 @@ exports.default = (details) => __awaiter(void 0, void 0, void 0, function* () {
52
52
  exit: 2
53
53
  }));
54
54
  });
55
+ child.on('exit', (code, signal) => {
56
+ (0, logger_1.debug)(`Python child exit: ${code}, ${signal}`);
57
+ });
55
58
  resolve(child);
56
59
  });
57
60
  });