zcatalyst-cli 1.18.0-beta.0 → 1.18.0-beta.10

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 (160) hide show
  1. package/docs/command_needs/rc.toml +8 -8
  2. package/docs/commands/ds/import.toml +5 -5
  3. package/docs/commands/event/generate/job.toml +7 -0
  4. package/docs/commands/zest/generate/index.toml +4 -0
  5. package/docs/endpoints/lib/job-scheduling.toml +3 -0
  6. package/docs/internal/command.toml +5 -0
  7. package/docs/serve/server/lib/appsail/index.toml +22 -1
  8. package/lib/appsail-utils.js +4 -10
  9. package/lib/authentication/index.js +1 -0
  10. package/lib/authentication/login.js +8 -4
  11. package/lib/bin/catalyst.js +1 -1
  12. package/lib/code-deck.js +7 -7
  13. package/lib/command_needs/rc.js +8 -8
  14. package/lib/commands/appsail/add.js +3 -2
  15. package/lib/commands/client/setup.js +2 -1
  16. package/lib/commands/codelib/install.js +6 -2
  17. package/lib/commands/ds/import.js +37 -21
  18. package/lib/commands/event/generate/index.js +16 -3
  19. package/lib/commands/event/generate/integ.js +2 -1
  20. package/lib/commands/event/generate/job.js +82 -0
  21. package/lib/commands/functions/add.js +2 -1
  22. package/lib/commands/functions/delete.js +2 -4
  23. package/lib/commands/functions/setup.js +2 -1
  24. package/lib/commands/functions/shell.js +1 -0
  25. package/lib/commands/iac/export.js +5 -1
  26. package/lib/commands/iac/import.js +4 -1
  27. package/lib/commands/index.js +4 -1
  28. package/lib/commands/init.js +10 -7
  29. package/lib/commands/login.js +1 -0
  30. package/lib/commands/logout.js +1 -0
  31. package/lib/commands/pull.js +1 -0
  32. package/lib/commands/serve.js +1 -1
  33. package/lib/commands/whoami.js +1 -0
  34. package/lib/commands/zest/generate/index.js +304 -0
  35. package/lib/deploy/features/appsail/index.js +42 -25
  36. package/lib/deploy/features/appsail/utils.js +4 -5
  37. package/lib/endpoints/index.js +24 -3
  38. package/lib/endpoints/lib/appsail.js +7 -1
  39. package/lib/endpoints/lib/ds-bulk.js +1 -0
  40. package/lib/endpoints/lib/job-scheduling.js +61 -0
  41. package/lib/endpoints/lib/stratus.js +63 -0
  42. package/lib/endpoints/lib/tunnel.js +18 -0
  43. package/lib/endpoints/lib/zest.js +124 -0
  44. package/lib/event_generate/stratus.js +52 -0
  45. package/lib/express_middlewares/logger.js +2 -4
  46. package/lib/fn-utils/lib/common.js +8 -4
  47. package/lib/fn-utils/lib/java.js +1 -1
  48. package/lib/fn-utils/lib/python.js +2 -2
  49. package/lib/fn-watcher.js +1 -1
  50. package/lib/iac/status/deploy.js +1 -5
  51. package/lib/init/dependencies/python/ensure-python.js +6 -8
  52. package/lib/init/features/appsail/index.js +38 -27
  53. package/lib/init/features/client/index.js +2 -1
  54. package/lib/init/features/functions/index.js +4 -0
  55. package/lib/init/features/functions/languages/python.js +21 -5
  56. package/lib/init/features/project.js +6 -15
  57. package/lib/internal/api.js +26 -7
  58. package/lib/internal/command.js +31 -7
  59. package/lib/migration/index.js +4 -2
  60. package/lib/optional-import.js +3 -2
  61. package/lib/prompt/index.js +5 -2
  62. package/lib/prompt/types/file-path.js +1 -1
  63. package/lib/prompt/types/search-box.js +281 -0
  64. package/lib/prompt/types/tree.js +3 -3
  65. package/lib/serve/features/appsail.js +3 -4
  66. package/lib/serve/index.js +1 -2
  67. package/lib/serve/server/index.js +52 -23
  68. package/lib/serve/server/lib/appsail/index.js +89 -39
  69. package/lib/serve/server/lib/appsail/start.js +9 -1
  70. package/lib/serve/server/lib/java/aio_server/lib/catalyst-cli-java-runtime-1.0.0.jar +0 -0
  71. package/lib/serve/server/lib/java/aio_server/lib/{catalyst-function-sdk-1.0.0.jar → catalyst-function-sdk-1.1.0.jar} +0 -0
  72. package/lib/serve/server/lib/java/aio_server/lib/catalyst-java-runtime-1.0.0.jar +0 -0
  73. package/lib/serve/server/lib/java/index.js +2 -2
  74. package/lib/serve/server/lib/master/appsail.js +53 -0
  75. package/lib/serve/server/lib/master/functions.js +34 -0
  76. package/lib/serve/server/lib/master/index.js +155 -0
  77. package/lib/{express_middlewares/unknownReqProxy.js → serve/server/lib/master/unknown-req-proxy.js} +9 -9
  78. package/lib/serve/server/lib/master/utils.js +142 -0
  79. package/lib/serve/server/lib/master/web-client.js +39 -0
  80. package/lib/serve/server/lib/node/index.js +3 -3
  81. package/lib/serve/server/lib/node/server/package-lock.json +12 -0
  82. package/lib/serve/server/lib/python/index.js +3 -3
  83. package/lib/shell/dependencies/http-functions.js +1 -1
  84. package/lib/shell/dependencies/invoker/bio/java/lib/catalyst-function-sdk-1.1.0.jar +0 -0
  85. package/lib/shell/dependencies/invoker/cron/java/JavacronInvoker.java +1 -1
  86. package/lib/shell/dependencies/invoker/cron/java/lib/catalyst-function-sdk-1.1.0.jar +0 -0
  87. package/lib/shell/dependencies/invoker/event/java/JavaeventInvoker.java +2 -1
  88. package/lib/shell/dependencies/invoker/event/java/lib/catalyst-function-sdk-1.1.0.jar +0 -0
  89. package/lib/shell/dependencies/invoker/event/node.mjs +1 -0
  90. package/lib/shell/dependencies/invoker/integ/java/JavaintegInvoker.java +15 -0
  91. package/lib/shell/dependencies/invoker/integ/java/lib/catalyst-function-sdk-1.1.0.jar +0 -0
  92. package/lib/shell/dependencies/invoker/integ/node.mjs +13 -0
  93. package/lib/shell/dependencies/invoker/job/java/JavajobInvoker.java +268 -0
  94. package/lib/shell/dependencies/invoker/job/java/lib/catalyst-function-sdk-1.1.0.jar +0 -0
  95. package/lib/shell/dependencies/invoker/job/java/lib/org.json.jar +0 -0
  96. package/lib/shell/dependencies/invoker/job/node.mjs +93 -0
  97. package/lib/shell/dependencies/local-function.js +122 -13
  98. package/lib/shell/dependencies/tunnel-server.js +52 -23
  99. package/lib/shell/index.js +22 -22
  100. package/lib/shell/prepare/languages/index.js +4 -4
  101. package/lib/shell/prepare/languages/java.js +1 -2
  102. package/lib/shell/prepare/languages/python.js +10 -16
  103. package/lib/throbber/index.js +6 -1
  104. package/lib/userConfig.js +7 -1
  105. package/lib/util_modules/config/index.js +1 -1
  106. package/lib/util_modules/config/lib/{appSail.js → appsail.js} +64 -10
  107. package/lib/util_modules/config/lib/client.js +6 -8
  108. package/lib/util_modules/config/lib/functions.js +6 -8
  109. package/lib/util_modules/constants/lib/dc-type.js +8 -0
  110. package/lib/util_modules/constants/lib/default.js +9 -9
  111. package/lib/util_modules/constants/lib/event-action.js +1 -0
  112. package/lib/util_modules/constants/lib/event-source.js +1 -0
  113. package/lib/util_modules/constants/lib/fn-type.js +2 -1
  114. package/lib/util_modules/constants/lib/needed-scopes.js +53 -44
  115. package/lib/util_modules/constants/lib/placeholders.js +7 -0
  116. package/lib/util_modules/constants/lib/ref-mapping.js +2 -1
  117. package/lib/util_modules/constants/lib/remote-mapping.js +2 -1
  118. package/lib/util_modules/constants/lib/scopes.js +17 -2
  119. package/lib/util_modules/constants/lib/template.js +9 -4
  120. package/lib/util_modules/constants/lib/urls.js +16 -0
  121. package/lib/util_modules/fs/lib/async.js +14 -4
  122. package/lib/util_modules/fs/lib/sync.js +12 -1
  123. package/lib/util_modules/server.js +2 -4
  124. package/lib/winston.js +17 -11
  125. package/package.json +4 -2
  126. package/templates/event/job.json +30 -0
  127. package/templates/init/client/react/react_js/template/src/index.js +3 -3
  128. package/templates/init/client/react/react_ts/template/src/index.tsx +3 -3
  129. package/templates/init/client/react/react_ts/template.json +3 -3
  130. package/templates/init/functions/java/job/.classpath +6 -0
  131. package/templates/init/functions/java/job/.project +17 -0
  132. package/templates/init/functions/java/job/catalyst-config.json +11 -0
  133. package/templates/init/functions/java/job/sample.java +34 -0
  134. package/templates/init/functions/node/aio/package.json +1 -1
  135. package/templates/init/functions/node/aio/sample.js +7 -0
  136. package/templates/init/functions/node/bio/package.json +1 -1
  137. package/templates/init/functions/node/bio/sample.js +5 -0
  138. package/templates/init/functions/node/bio/types/basicio.d.ts +57 -0
  139. package/templates/init/functions/node/cron/package.json +1 -1
  140. package/templates/init/functions/node/cron/sample.js +5 -0
  141. package/templates/init/functions/node/cron/types/cron.d.ts +64 -0
  142. package/templates/init/functions/node/event/package.json +1 -1
  143. package/templates/init/functions/node/event/sample.js +11 -8
  144. package/templates/init/functions/node/event/types/event.d.ts +82 -0
  145. package/templates/init/functions/node/integ/cliq/package.json +1 -1
  146. package/templates/init/functions/node/integ/convokraft/package.json +1 -1
  147. package/templates/init/functions/node/{stream → job}/catalyst-config.json +3 -2
  148. package/templates/init/functions/node/{stream → job}/package.json +1 -1
  149. package/templates/init/functions/node/job/sample.js +27 -0
  150. package/templates/init/functions/node/job/types/job.d.ts +62 -0
  151. package/templates/init/functions/python/job/catalyst-config.json +11 -0
  152. package/templates/init/functions/python/job/requirements.txt +1 -0
  153. package/templates/init/functions/python/job/sample.py +22 -0
  154. package/templates/init.txt +14 -0
  155. package/lib/serve/server/lib/master.js +0 -326
  156. package/lib/shell/dependencies/invoker/bio/java/lib/catalyst-function-sdk-1.0.0.jar +0 -0
  157. package/lib/shell/dependencies/invoker/cron/java/lib/catalyst-function-sdk-1.0.0.jar +0 -0
  158. package/lib/shell/dependencies/invoker/event/java/lib/catalyst-function-sdk-1.0.0.jar +0 -0
  159. package/lib/shell/dependencies/invoker/integ/java/lib/catalyst-function-sdk-1.0.0.jar +0 -0
  160. package/templates/init/functions/node/stream/sample.js +0 -15
@@ -0,0 +1,142 @@
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 iam_cookies = [
24
+ 'ZD_CSRF_TOKEN',
25
+ '_iamadt_client',
26
+ '_iambdt_client',
27
+ '_zcsr_tmp',
28
+ 'iamcsr',
29
+ 'zalb',
30
+ 'stk',
31
+ 'JSESSIONID'
32
+ ];
33
+ const isIamCookie = (cookieStr) => iam_cookies.some((val) => cookieStr.startsWith(val));
34
+ const removeSecure = (str) => str.replace(/;\s?Secure/i, '').replace(/;\s?SameSite=None/i, '');
35
+ exports.removeSecure = removeSecure;
36
+ const redirectByAuth = (req, res, redirectUrl) => {
37
+ if (req.headers['cookie'] !== undefined) {
38
+ res.writeHead(302, { Location: redirectUrl });
39
+ res.end();
40
+ }
41
+ else {
42
+ res.writeHead(302, { Location: '/app/' });
43
+ res.end();
44
+ }
45
+ };
46
+ exports.redirectByAuth = redirectByAuth;
47
+ const createProxyServer = (port) => {
48
+ const proxy = (0, http_proxy_1.createProxyServer)({
49
+ hostRewrite: `localhost:${port}`,
50
+ protocolRewrite: 'http'
51
+ })
52
+ .on('error', (err, req, res) => {
53
+ if (res.writableEnded) {
54
+ return;
55
+ }
56
+ if (res instanceof http_1.ServerResponse) {
57
+ res.writeHead(502, { 'Content-Type': 'json' });
58
+ }
59
+ res.end(JSON.stringify({ error: 'unable to serve the request' }));
60
+ console.log();
61
+ console.log('Unable to reach the servers to serve the request: ', req.url);
62
+ console.error(err);
63
+ })
64
+ .on('econnreset', (err, req, res, targetUrl) => {
65
+ if (res.writableEnded) {
66
+ return;
67
+ }
68
+ if (res instanceof http_1.ServerResponse) {
69
+ res.writeHead(502, { 'Content-Type': 'json' });
70
+ }
71
+ res.end(JSON.stringify({ error: 'unable to serve the request. ECONNRESET.' }));
72
+ console.log();
73
+ console.log('Connection reset while trying to serve the request ', req.url);
74
+ (0, logger_1.debug)('Connection reset from target URL : ', targetUrl);
75
+ console.error(err);
76
+ })
77
+ .on('close', () => (0, logger_1.debug)('proxy closed'));
78
+ return proxy;
79
+ };
80
+ exports.createProxyServer = createProxyServer;
81
+ const proxyResponseHandler = ({ systemRoutes, signInRedirect = '/app/local-redirect' }) => (proxyRes, req, res) => {
82
+ var _a, _b;
83
+ if (!req.url) {
84
+ return;
85
+ }
86
+ proxyRes.headers['set-cookie'] = (_a = proxyRes.headers['set-cookie']) === null || _a === void 0 ? void 0 : _a.map((cookie) => {
87
+ if (isIamCookie(cookie))
88
+ return (0, exports.removeSecure)(cookie);
89
+ return cookie;
90
+ });
91
+ if (req.url.startsWith('/baas') || req.url.startsWith('/__catalyst')) {
92
+ if (req.url.includes('/signin-redirect')) {
93
+ if (systemRoutes === undefined) {
94
+ (0, exports.redirectByAuth)(req, res, signInRedirect);
95
+ return;
96
+ }
97
+ const sourceRedirectUrl = Object.keys(systemRoutes).find((sourceUrl) => {
98
+ const methodByConfig = systemRoutes ? systemRoutes[sourceUrl] : undefined;
99
+ if (methodByConfig) {
100
+ return !!Object.values(methodByConfig).find((config) => {
101
+ return config.name === 'Login Redirect';
102
+ });
103
+ }
104
+ return false;
105
+ });
106
+ if (sourceRedirectUrl === undefined) {
107
+ res.end('INVALID_URL');
108
+ return;
109
+ }
110
+ (0, exports.redirectByAuth)(req, res, sourceRedirectUrl);
111
+ }
112
+ else if (req.url.includes('/logout')) {
113
+ const set = proxyRes.headers['set-cookie'];
114
+ if (set) {
115
+ const result = set.map((cookie) => cookie.replace(new RegExp('Domain=[\\S^;]*', 'g'), 'Domain=localhost;'));
116
+ proxyRes.headers['set-cookie'] = result;
117
+ }
118
+ }
119
+ }
120
+ else if (req.url.startsWith('/accounts') && req.url.includes('/signin')) {
121
+ (_b = proxyRes.headers['set-cookie']) === null || _b === void 0 ? void 0 : _b.push('IAM_TEST_COOKIE=IAM_TEST_COOKIE; Domain=localhost; Path=/');
122
+ }
123
+ };
124
+ exports.proxyResponseHandler = proxyResponseHandler;
125
+ const appsailInitJs = () => __awaiter(void 0, void 0, void 0, function* () {
126
+ let initJsStr = yield fs_1.ASYNC.readFile(constants_1.TEMPLATE.init_js);
127
+ if (!initJsStr) {
128
+ throw new error_1.default('Unable to read the init_js file', { exit: 2 });
129
+ }
130
+ const feed = [
131
+ (0, project_1.getProjectId)(),
132
+ (0, project_1.getDomainKey)(),
133
+ constants_1.ORIGIN.iamPortal,
134
+ (0, project_1.getEnvId)(),
135
+ `-development${constants_1.ORIGIN.stratusSuffix}`
136
+ ];
137
+ Object.values(constants_1.PLACEHOLDER.init_js).forEach((holder, idx) => {
138
+ initJsStr = initJsStr === null || initJsStr === void 0 ? void 0 : initJsStr.replace(new RegExp(holder, 'g'), feed[idx]);
139
+ });
140
+ return initJsStr;
141
+ });
142
+ 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, X_ZOHO_STRATUS_RESOURCE_SUFFIX: constants_1.ORIGIN.stratusSuffix, CATALYST_PORTAL_DOMAIN: constants_1.ORIGIN.iamPortal, CATALYST_PROJECT_TIMEZONE: (0, project_1.getProjectTimezone)(Intl.DateTimeFormat().resolvedOptions().timeZone) }, details.target.env_var)
92
92
  }).RAW();
93
93
  });
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "catalyst-cli-node-runtime",
3
+ "version": "1.0.0",
4
+ "lockfileVersion": 3,
5
+ "requires": true,
6
+ "packages": {
7
+ "": {
8
+ "name": "catalyst-cli-node-runtime",
9
+ "version": "1.0.0"
10
+ }
11
+ }
12
+ }
@@ -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_STRATUS_RESOURCE_SUFFIX: constants_1.ORIGIN.stratusSuffix, 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_PORTAL_DOMAIN: constants_1.ORIGIN.iamPortal, 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) => {
@@ -117,7 +117,7 @@ class HttpFunctions {
117
117
  const slave = (0, shell_1.spawn)(opts.command, opts.options, {
118
118
  cwd: opts.pth,
119
119
  stdio: 'pipe',
120
- 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, CATALYST_PROJECT_TIMEZONE: (0, project_2.getProjectTimezone)(Intl.DateTimeFormat().resolvedOptions().timeZone) }, opts.envVars)
120
+ 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_STRATUS_RESOURCE_SUFFIX: constants_1.ORIGIN.stratusSuffix, CATALYST_PORTAL_DOMAIN: constants_1.ORIGIN.iamPortal, CATALYST_PROJECT_TIMEZONE: (0, project_2.getProjectTimezone)(Intl.DateTimeFormat().resolvedOptions().timeZone) }, opts.envVars)
121
121
  }).RAW();
122
122
  (_a = slave.stdout) === null || _a === void 0 ? void 0 : _a.on('data', (data) => {
123
123
  (0, logger_2.info)(Buffer.isBuffer(data) ? data.toString() : data);
@@ -276,7 +276,7 @@ public class JavacronInvoker {
276
276
  Method runner = cls.getMethod("handleCronExecute", CronRequest.class, Context.class);
277
277
  CRON_STATUS cronStatus = null;
278
278
 
279
- if(System.getenv("DEBUG").equals("false")) {
279
+ if(System.getenv("DEBUG") != null && System.getenv("DEBUG").equals("false")) {
280
280
  Timer executionTimer = new Timer(true);
281
281
  executionTimer.schedule(new TimerTask() {
282
282
  @Override
@@ -259,6 +259,7 @@ public class JavaeventInvoker {
259
259
  defaultEvent.setSource(String.valueOf(userData.get("source")));
260
260
  defaultEvent.setSourceEntityId(sourceId == "null" ? null : Long.valueOf(sourceId));
261
261
  defaultEvent.setTime(eventTime == "null" ? null : Long.valueOf(eventTime));
262
+ defaultEvent.setRawData(userData);
262
263
 
263
264
  EventRequest eventRequest = defaultEvent;
264
265
 
@@ -268,7 +269,7 @@ public class JavaeventInvoker {
268
269
  Method runner = cls.getMethod("handleEvent", EventRequest.class, Context.class);
269
270
  EVENT_STATUS eventStatus = null;
270
271
 
271
- if(System.getenv("DEBUG").equals("false")) {
272
+ if(System.getenv("DEBUG") != null && System.getenv("DEBUG").equals("false")) {
272
273
  Timer executionTimer = new Timer(true);
273
274
  executionTimer.schedule(new TimerTask() {
274
275
  @Override
@@ -55,6 +55,7 @@ const eventReq = {
55
55
  getSourceEntityId: () => userData.source_entity_id,
56
56
  getEventBusDetails: () => userData.event_bus_details,
57
57
  getProjectDetails: () => userData.project_details,
58
+ getRawData: () => userData,
58
59
  /**
59
60
  * @deprecated use individual get methods.
60
61
  */
@@ -14,6 +14,9 @@ import java.nio.file.Path;
14
14
  import java.nio.file.Paths;
15
15
  import java.util.HashMap;
16
16
  import java.util.Iterator;
17
+ import java.util.Timer;
18
+ import java.util.TimerTask;
19
+ import java.util.concurrent.TimeoutException;
17
20
  import java.util.logging.Handler;
18
21
  import java.util.logging.LogManager;
19
22
  import java.util.logging.LogRecord;
@@ -120,6 +123,7 @@ public class JavaintegInvoker {
120
123
  catalystConfig.put("project_key", project.get("x-zc-project-key").toString());
121
124
  catalystConfig.put("project_domain", project.get("x-zc-project-domain").toString());
122
125
  catalystConfig.put("environment", project.get("x-zc-environment").toString());
126
+
123
127
 
124
128
  ZCThreadLocal.putValue("CATALYST_CONFIG", catalystConfig.toString());
125
129
  }
@@ -231,6 +235,17 @@ public class JavaintegInvoker {
231
235
 
232
236
  Method runner = cls.getMethod("runner", ZCIntegRequest.class);
233
237
  ZCIntegResponse integResponse = null;
238
+
239
+ if(System.getenv("DEBUG") != null && System.getenv("DEBUG").equals("false")) {
240
+ Timer executionTimer = new Timer(true);
241
+ executionTimer.schedule(new TimerTask() {
242
+ @Override
243
+ public void run() {
244
+ throwAndExit(new TimeoutException("function execution timeout"), 408, invokerDir);
245
+ }
246
+ }, 900000L); // 15 mins
247
+ }
248
+
234
249
  try {
235
250
  integResponse = (ZCIntegResponse) runner.invoke(cls.getDeclaredConstructor().newInstance(), integRequest);
236
251
  } catch (Exception e) {
@@ -20,6 +20,19 @@ const writeAndExit = (resp) => {
20
20
  process.exit(0);
21
21
  };
22
22
 
23
+ /**
24
+ * execution timeout of 15 minutes
25
+ */
26
+ const timeout = 15 * 60 * 1000;
27
+ // const endTime = timeout + Date.now();
28
+
29
+ // exit on timeout
30
+ process.env.DEBUG === 'false' &&
31
+ setTimeout(() => {
32
+ writeToFile('TIMEOUT', 408);
33
+ process.exit(0);
34
+ }, timeout);
35
+
23
36
  const integrationRequest = data || {};
24
37
 
25
38
  integrationRequest.catalystHeaders = Object.assign(projectJson, authJson);
@@ -0,0 +1,268 @@
1
+ import java.io.BufferedWriter;
2
+ import java.io.File;
3
+ import java.io.FileWriter;
4
+ import java.io.FilenameFilter;
5
+ import java.io.PrintWriter;
6
+ import java.io.StringWriter;
7
+ import java.lang.reflect.Method;
8
+ import java.net.URL;
9
+ import java.net.URLClassLoader;
10
+ import java.nio.file.Paths;
11
+ import java.util.HashMap;
12
+ import java.util.Iterator;
13
+ import java.util.Timer;
14
+ import java.util.TimerTask;
15
+ import java.util.concurrent.TimeoutException;
16
+ import java.util.logging.Handler;
17
+ import java.util.logging.LogManager;
18
+ import java.util.logging.LogRecord;
19
+ import java.util.logging.Logger;
20
+ import java.util.logging.SimpleFormatter;
21
+
22
+ import org.json.JSONObject;
23
+
24
+ import com.catalyst.Context;
25
+ import com.catalyst.config.ZCThreadLocal;
26
+ import com.catalyst.job.CapacityAttributes;
27
+ import com.catalyst.job.JOB_STATUS;
28
+ import com.catalyst.impl.DefaultContext;
29
+ import com.catalyst.job.JobRequest;
30
+ import com.catalyst.job.impl.DefaultJobRequest;
31
+
32
+ public class JavajobInvoker {
33
+
34
+ private static final Integer MESSAGE_LENGTH = 1500;
35
+
36
+ public class LogHandler extends Handler {
37
+
38
+ @Override
39
+ public void publish(LogRecord record) {
40
+ try {
41
+ String exceptionMessage = "";
42
+ if (record.getThrown() != null) {
43
+ exceptionMessage = getStackTraceAsString(record.getThrown());
44
+ }
45
+ String message = new SimpleFormatter().format(record) + " " + exceptionMessage;
46
+ if (message.length() > MESSAGE_LENGTH) {
47
+ message = message.substring(0, MESSAGE_LENGTH);
48
+ }
49
+
50
+ System.out.println("[" + record.getLevel().getName() + "] : " + message);
51
+
52
+ } catch (Exception e) {
53
+ System.out.println(getStackTraceAsString(e));
54
+ }
55
+
56
+ }
57
+
58
+ @Override
59
+ public void flush() {
60
+ // TODO Auto-generated method stub
61
+
62
+ }
63
+
64
+ @Override
65
+ public void close() throws SecurityException {
66
+ // TODO Auto-generated method stub
67
+
68
+ }
69
+
70
+ }
71
+
72
+ private static String getStackTraceAsString(Throwable throwable) {
73
+ StringWriter stringWriter = new StringWriter();
74
+ throwable.printStackTrace(new PrintWriter(stringWriter));
75
+ return stringWriter.toString();
76
+ }
77
+
78
+ private static <T> HashMap<String, T> jsonToMap(String t) throws Exception {
79
+
80
+ HashMap<String, T> map = new HashMap<String, T>();
81
+ JSONObject jObject = new JSONObject(t);
82
+ Iterator<?> keys = jObject.keys();
83
+
84
+ while (keys.hasNext()) {
85
+ String key = (String) keys.next();
86
+ Object value = jObject.get(key);
87
+ map.put(key, (T) value);
88
+
89
+ }
90
+ return map;
91
+ }
92
+
93
+ private static void writeResponse(String response, Integer status, String invokerDir) throws Exception {
94
+ // write response data
95
+ String responseFilePath = Paths.get(invokerDir, "../user_res_body").toString();
96
+
97
+ BufferedWriter responseWriter = new BufferedWriter(new FileWriter(responseFilePath));
98
+ responseWriter.write(response);
99
+ responseWriter.close();
100
+
101
+ // write response meta
102
+ String metaFilePath = Paths.get(invokerDir, "../user_meta.json").toString();
103
+
104
+ BufferedWriter metaWriter = new BufferedWriter(new FileWriter(metaFilePath));
105
+ JSONObject metaJson = new JSONObject();
106
+ metaJson.put("statusCode", status);
107
+ metaWriter.write(new JSONObject().put("response", metaJson).toString());
108
+ metaWriter.close();
109
+ }
110
+
111
+ private static void throwAndExit(Exception err, int exitCode, String invokerDir) {
112
+ try {
113
+ String sStackTrace = getStackTraceAsString(err); // stack trace as a string
114
+ if (exitCode == 532) {
115
+ writeResponse("CODE_EXCEPTION", exitCode, invokerDir);
116
+ } else if (exitCode == 500) {
117
+ writeResponse("INTERNAL_SERVER_ERROR", 500, invokerDir);
118
+ } else if (exitCode == 408) {
119
+ writeResponse("TIMEOUT", exitCode, invokerDir);
120
+ }
121
+ System.out.println(sStackTrace);
122
+ } catch (Exception e) {
123
+ System.out.println(e);
124
+ }
125
+ System.exit(exitCode);
126
+ }
127
+
128
+ private static void setZCThreadLocalProject(HashMap<String, Object> project) throws Exception {
129
+
130
+ JSONObject catalystConfig = new JSONObject();
131
+ catalystConfig.put("project_id", project.get("x-zc-projectid").toString());
132
+ catalystConfig.put("project_key", project.get("x-zc-project-key").toString());
133
+ catalystConfig.put("project_domain", project.get("x-zc-project-domain").toString());
134
+ catalystConfig.put("environment", project.get("x-zc-environment").toString());
135
+
136
+ ZCThreadLocal.putValue("CATALYST_CONFIG", catalystConfig.toString());
137
+ }
138
+
139
+ private static void setZCThreadLocalAuth(HashMap<String, Object> auth) throws Exception {
140
+ JSONObject catalystAuth = new JSONObject();
141
+
142
+ String adminAuthHeaderType = auth.get("x-zc-admin-cred-type").toString();
143
+ String adminAuthToken = auth.get("x-zc-admin-cred-token").toString();
144
+
145
+ JSONObject adminAuth = new JSONObject();
146
+ adminAuth.put((adminAuthHeaderType.equals("token")) ? "access_token" : "ticket", adminAuthToken); // No I18N
147
+ catalystAuth.put("admin_cred", adminAuth);
148
+
149
+ JSONObject clientAuth = new JSONObject();
150
+ if(auth.containsKey("x-zc-user-cred-type") && auth.containsKey("x-zc-user-cred-token")) {
151
+ String userAuthHeaderType = auth.get("x-zc-user-cred-type").toString();
152
+ String userAuthToken = auth.get("x-zc-user-cred-token").toString();
153
+
154
+ clientAuth.put((userAuthHeaderType.equals("token")) ? "access_token" : "ticket", userAuthToken); // No I18N
155
+ }
156
+ if(auth.containsKey("x-zc-user-type")) { // No I18N
157
+ String userType = auth.get("x-zc-user-type").toString(); // No I18N
158
+ clientAuth.put("user_type", userType); // No I18N
159
+ }
160
+ if (auth.containsKey("x-zc-cookie")) {
161
+ String cookie = auth.get("x-zc-cookie").toString();
162
+ clientAuth.put("cookie", cookie);
163
+ }
164
+ catalystAuth.put("client_cred", clientAuth);
165
+ ZCThreadLocal.putValue("CATALYST_AUTH", catalystAuth.toString()); // No I18N
166
+
167
+ // For backward compatibility, to be remove in future.
168
+ if(auth.containsKey("x-zc-cookie")) {
169
+ String cookie = auth.get("x-zc-cookie").toString();
170
+ ZCThreadLocal.putValue("client_cookie", cookie); // No I18N
171
+ }
172
+ // to be removed in future
173
+ }
174
+
175
+ public static void main(String[] args) {
176
+ String invokerDir = args[0];
177
+ try {
178
+ LogManager manager = LogManager.getLogManager();
179
+ manager.reset();
180
+ Logger rootLogger = manager.getLogger("");
181
+ LogHandler handler = new JavajobInvoker().new LogHandler();
182
+ rootLogger.addHandler(handler);
183
+
184
+ HashMap<String, Object> target = jsonToMap(args[1]);
185
+ String fnExeName = (String) target.get("index");
186
+ String fnName = (String) target.get("name");
187
+ String fnExePath = Paths.get(invokerDir, "../../", "functions", fnName).normalize().toString();
188
+
189
+ JSONObject userData = new JSONObject(args[2]);
190
+ HashMap<String, Object> projectData = jsonToMap(args[3]);
191
+ HashMap<String, Object> authData = jsonToMap(args[4]);
192
+
193
+ File[] jarFiles = new File(fnExePath).listFiles(new FilenameFilter() {
194
+ @Override
195
+ public boolean accept(File dir, String name) {
196
+ if(name.endsWith(".jar")) {
197
+ return true;
198
+ }
199
+ return false;
200
+ }
201
+ });
202
+ int jarCount = jarFiles.length;
203
+ URL[] URLs = new URL[jarCount + 1];
204
+ URLs[0] = new File(Paths.get(fnExePath).toString()).toURI().toURL();
205
+ for (int i = 1; i <= jarCount; i++) {
206
+ URLs[i] = jarFiles[i - 1].toURI().toURL();
207
+ }
208
+ URLClassLoader child = new URLClassLoader(URLs, JavajobInvoker.class.getClassLoader());
209
+ Class<?> cls = Class.forName(fnExeName, true, child);
210
+
211
+ setZCThreadLocalProject(projectData);
212
+ setZCThreadLocalAuth(authData);
213
+
214
+ DefaultJobRequest defaultJob = new DefaultJobRequest();
215
+
216
+ JSONObject jobDetails = userData.getJSONObject("job_details");
217
+ JSONObject jobMetaDetails = jobDetails.getJSONObject("job_meta_details");
218
+ JSONObject jobpoolDetails = jobMetaDetails.getJSONObject("jobpool_details");
219
+ JSONObject projectDetails = jobpoolDetails.getJSONObject("project_details");
220
+ JSONObject capacity = userData.getJSONObject("capacity");
221
+ JSONObject params = jobMetaDetails.getJSONObject("params");
222
+
223
+ defaultJob.setJobDetails(jobDetails != null ? (JSONObject) jobDetails : new JSONObject());
224
+ defaultJob.setJobMetaDetails(jobMetaDetails != null ? (JSONObject) jobDetails : new JSONObject());
225
+ defaultJob.setJobpoolDetails(jobpoolDetails != null ? (JSONObject) jobpoolDetails : new JSONObject());
226
+ defaultJob.setProjectDetails(projectDetails);
227
+ CapacityAttributes capacityAttributes = CapacityAttributes.getInstance();
228
+ capacityAttributes.setMemory(capacity.getLong("memory"));
229
+ defaultJob.setJobCapacity(capacityAttributes);
230
+ defaultJob.setJobParams(jsonToMap(params != null ? params.toString() : new JSONObject().toString()));
231
+
232
+ JobRequest jobRequest = defaultJob;
233
+
234
+ DefaultContext defaultContext = new DefaultContext(cls.getName(), 900000L);
235
+ Context context = defaultContext;
236
+
237
+ Method runner = cls.getMethod("handleJobExecute", JobRequest.class, Context.class);
238
+ JOB_STATUS jobStatus = null;
239
+
240
+ if(System.getenv("DEBUG") != null && System.getenv("DEBUG").equals("false")) {
241
+ Timer executionTimer = new Timer(true);
242
+ executionTimer.schedule(new TimerTask() {
243
+ @Override
244
+ public void run() {
245
+ throwAndExit(new TimeoutException("function execution timeout"), 408, invokerDir);
246
+ }
247
+ }, defaultContext.getMaxExecutionTimeMs());
248
+ }
249
+
250
+ try {
251
+ jobStatus = (JOB_STATUS) runner.invoke(cls.getDeclaredConstructor().newInstance(), jobRequest, context);
252
+ } catch (Exception e) {
253
+ throwAndExit(e, 532, invokerDir);
254
+ }
255
+
256
+ if(jobStatus == null) {
257
+ writeResponse("UNINTENTIONAL_TERMINATION", 531, invokerDir);
258
+ } else {
259
+ int status = jobStatus.getStatus();
260
+ writeResponse(jobStatus.name(), status == 500 ? 530 : status, invokerDir);
261
+ }
262
+
263
+ System.exit(0);
264
+ } catch (Exception e) {
265
+ throwAndExit(e, 500, invokerDir);
266
+ }
267
+ }
268
+ }