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

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 (129) hide show
  1. package/docs/command_needs/rc.toml +8 -8
  2. package/docs/commands/event/generate/job.toml +7 -0
  3. package/docs/endpoints/lib/job-scheduling.toml +3 -0
  4. package/docs/internal/command.toml +5 -0
  5. package/lib/appsail-utils.js +4 -10
  6. package/lib/authentication/index.js +1 -0
  7. package/lib/bin/catalyst.js +1 -1
  8. package/lib/command_needs/rc.js +8 -8
  9. package/lib/commands/appsail/add.js +2 -1
  10. package/lib/commands/client/setup.js +2 -1
  11. package/lib/commands/codelib/install.js +5 -2
  12. package/lib/commands/event/generate/index.js +2 -1
  13. package/lib/commands/event/generate/integ.js +2 -1
  14. package/lib/commands/event/generate/job.js +82 -0
  15. package/lib/commands/functions/add.js +2 -1
  16. package/lib/commands/functions/delete.js +2 -4
  17. package/lib/commands/functions/setup.js +2 -1
  18. package/lib/commands/functions/shell.js +1 -0
  19. package/lib/commands/index.js +3 -1
  20. package/lib/commands/init.js +10 -7
  21. package/lib/commands/login.js +1 -0
  22. package/lib/commands/logout.js +1 -0
  23. package/lib/commands/pull.js +1 -0
  24. package/lib/commands/serve.js +1 -1
  25. package/lib/commands/whoami.js +1 -0
  26. package/lib/deploy/features/appsail/index.js +42 -25
  27. package/lib/deploy/features/appsail/utils.js +4 -5
  28. package/lib/endpoints/index.js +10 -3
  29. package/lib/endpoints/lib/appsail.js +7 -1
  30. package/lib/endpoints/lib/job-scheduling.js +61 -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 +36 -26
  38. package/lib/init/features/client/index.js +2 -1
  39. package/lib/init/features/functions/index.js +4 -0
  40. package/lib/init/features/functions/languages/python.js +21 -5
  41. package/lib/init/features/project.js +5 -15
  42. package/lib/internal/api.js +25 -6
  43. package/lib/internal/command.js +30 -6
  44. package/lib/migration/index.js +4 -2
  45. package/lib/optional-import.js +3 -2
  46. package/lib/prompt/types/file-path.js +1 -1
  47. package/lib/prompt/types/tree.js +3 -3
  48. package/lib/serve/features/appsail.js +2 -3
  49. package/lib/serve/index.js +1 -2
  50. package/lib/serve/server/index.js +8 -9
  51. package/lib/serve/server/lib/appsail/index.js +29 -35
  52. package/lib/serve/server/lib/java/aio_server/lib/catalyst-cli-java-runtime-1.0.0.jar +0 -0
  53. package/lib/serve/server/lib/java/aio_server/lib/catalyst-function-sdk-1.0.0.jar +0 -0
  54. package/lib/serve/server/lib/java/aio_server/lib/catalyst-java-runtime-1.0.0.jar +0 -0
  55. package/lib/serve/server/lib/java/index.js +2 -2
  56. package/lib/serve/server/lib/master/appsail.js +53 -0
  57. package/lib/serve/server/lib/master/functions.js +34 -0
  58. package/lib/serve/server/lib/master/index.js +155 -0
  59. package/lib/{express_middlewares/unknownReqProxy.js → serve/server/lib/master/unknown-req-proxy.js} +4 -7
  60. package/lib/serve/server/lib/master/utils.js +130 -0
  61. package/lib/serve/server/lib/master/web-client.js +39 -0
  62. package/lib/serve/server/lib/node/index.js +3 -3
  63. package/lib/serve/server/lib/python/index.js +3 -3
  64. package/lib/shell/dependencies/invoker/bio/java/lib/catalyst-function-sdk-1.0.0.jar +0 -0
  65. package/lib/shell/dependencies/invoker/cron/java/JavacronInvoker.java +1 -1
  66. package/lib/shell/dependencies/invoker/cron/java/lib/catalyst-function-sdk-1.0.0.jar +0 -0
  67. package/lib/shell/dependencies/invoker/event/java/JavaeventInvoker.java +2 -1
  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 +1 -0
  70. package/lib/shell/dependencies/invoker/integ/java/JavaintegInvoker.java +14 -0
  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 +13 -0
  73. package/lib/shell/dependencies/invoker/job/java/JavajobInvoker.java +268 -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 +121 -13
  78. package/lib/shell/index.js +7 -1
  79. package/lib/shell/prepare/languages/index.js +4 -4
  80. package/lib/shell/prepare/languages/java.js +1 -2
  81. package/lib/shell/prepare/languages/python.js +10 -16
  82. package/lib/throbber/index.js +6 -1
  83. package/lib/userConfig.js +7 -1
  84. package/lib/util_modules/config/index.js +1 -1
  85. package/lib/util_modules/config/lib/{appSail.js → appsail.js} +60 -10
  86. package/lib/util_modules/config/lib/client.js +6 -8
  87. package/lib/util_modules/config/lib/functions.js +6 -8
  88. package/lib/util_modules/constants/lib/default.js +9 -9
  89. package/lib/util_modules/constants/lib/fn-type.js +2 -1
  90. package/lib/util_modules/constants/lib/needed-scopes.js +53 -44
  91. package/lib/util_modules/constants/lib/placeholders.js +6 -0
  92. package/lib/util_modules/constants/lib/ref-mapping.js +2 -1
  93. package/lib/util_modules/constants/lib/remote-mapping.js +2 -1
  94. package/lib/util_modules/constants/lib/scopes.js +5 -1
  95. package/lib/util_modules/constants/lib/template.js +9 -4
  96. package/lib/util_modules/constants/lib/urls.js +8 -0
  97. package/lib/util_modules/fs/lib/async.js +14 -4
  98. package/lib/util_modules/fs/lib/sync.js +12 -1
  99. package/lib/util_modules/server.js +2 -4
  100. package/lib/winston.js +17 -11
  101. package/package.json +1 -1
  102. package/templates/event/job.json +30 -0
  103. package/templates/init/functions/java/job/.classpath +6 -0
  104. package/templates/init/functions/java/job/.project +17 -0
  105. package/templates/init/functions/java/job/catalyst-config.json +11 -0
  106. package/templates/init/functions/java/job/sample.java +34 -0
  107. package/templates/init/functions/node/aio/package.json +1 -1
  108. package/templates/init/functions/node/aio/sample.js +7 -0
  109. package/templates/init/functions/node/bio/package.json +1 -1
  110. package/templates/init/functions/node/bio/sample.js +5 -0
  111. package/templates/init/functions/node/bio/types/basicio.d.ts +57 -0
  112. package/templates/init/functions/node/cron/package.json +1 -1
  113. package/templates/init/functions/node/cron/sample.js +5 -0
  114. package/templates/init/functions/node/cron/types/cron.d.ts +64 -0
  115. package/templates/init/functions/node/event/package.json +1 -1
  116. package/templates/init/functions/node/event/sample.js +11 -8
  117. package/templates/init/functions/node/event/types/event.d.ts +82 -0
  118. package/templates/init/functions/node/integ/cliq/package.json +1 -1
  119. package/templates/init/functions/node/integ/convokraft/package.json +1 -1
  120. package/templates/init/functions/node/{stream → job}/catalyst-config.json +3 -2
  121. package/templates/init/functions/node/{stream → job}/package.json +1 -1
  122. package/templates/init/functions/node/job/sample.js +27 -0
  123. package/templates/init/functions/node/job/types/job.d.ts +62 -0
  124. package/templates/init/functions/python/job/catalyst-config.json +11 -0
  125. package/templates/init/functions/python/job/requirements.txt +1 -0
  126. package/templates/init/functions/python/job/sample.py +22 -0
  127. package/templates/init.txt +13 -0
  128. package/lib/serve/server/lib/master.js +0 -326
  129. package/templates/init/functions/node/stream/sample.js +0 -15
@@ -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 = jobDetails.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
+ }
@@ -0,0 +1,93 @@
1
+ 'use strict';
2
+
3
+ import * as path from 'path';
4
+ import * as fs from 'fs';
5
+ import { pathToFileURL } from 'url';
6
+
7
+ const args = process.argv.slice(2);
8
+ const target = JSON.parse(args[0]);
9
+ const userData = JSON.parse(args[1]);
10
+ const projectJson = JSON.parse(args[2]);
11
+ const authJson = JSON.parse(args[3]);
12
+ const buildDir = JSON.parse(args[4]);
13
+
14
+ const requestFile = path.join(buildDir, '.catalyst', 'user_req_body');
15
+ const responseFile = path.join(buildDir, '.catalyst', 'user_res_body');
16
+ const metaFile = path.join(buildDir, '.catalyst', 'user_meta.json');
17
+
18
+ const writeToFile = (resp, status) => {
19
+ fs.writeFileSync(responseFile, resp);
20
+ fs.writeFileSync(metaFile, JSON.stringify({ response: { statusCode: status } }));
21
+ };
22
+
23
+ let body = {};
24
+
25
+ if (fs.existsSync(requestFile)) {
26
+ const content = fs.readFileSync(requestFile);
27
+ try {
28
+ body = JSON.parse(content.toString());
29
+ } catch (err) {
30
+ body = {};
31
+ }
32
+ }
33
+
34
+ /**
35
+ * execution timeout 15 minutes
36
+ */
37
+ const timeout = 15 * 60 * 1000;
38
+ const endTime = timeout + Date.now();
39
+
40
+ // exit on timeout
41
+ process.env.DEBUG === 'false' &&
42
+ setTimeout(() => {
43
+ writeToFile('TIMEOUT', 408);
44
+ process.exit(0);
45
+ }, timeout);
46
+
47
+ const context = {
48
+ catalystHeaders: Object.assign(projectJson, authJson),
49
+ getMaxExecutionTimeMs: () => timeout,
50
+ getRemainingExecutionTimeMs: () => endTime - Date.now(),
51
+ closeWithSuccess: () => {
52
+ writeToFile('SUCCESS', 200);
53
+ process.exit(0);
54
+ },
55
+ closeWithFailure: () => {
56
+ writeToFile('FAILURE', 530);
57
+ process.exit(0);
58
+ }
59
+ };
60
+
61
+ const jobReq = {
62
+ getJobDetails: () => ('job_details' in userData ? userData.job_details : {}),
63
+ getJobMetaDetails: () => jobReq.getJobDetails()?.job_meta_details || {},
64
+ getJobpoolDetails: () => jobReq.getJobMetaDetails()?.jobpool_details || {},
65
+ getProjectDetails: () => jobReq.getJobpoolDetails()?.project_details || {},
66
+ getJobCapacityAttributes: () => jobReq.getJobDetails()?.capacity || {},
67
+ getAllJobParams: () => jobReq.getJobMetaDetails()?.params || {},
68
+ getJobParam: (key) => (jobReq.getAllJobParams() || {})[key]
69
+ };
70
+
71
+ import(pathToFileURL(target.index))
72
+ .then((module) => {
73
+ try {
74
+ if (!('default' in module)) {
75
+ throw new Error('Could not find any default export');
76
+ }
77
+ if (typeof module.default !== 'function') {
78
+ throw new Error('The default export is not a function');
79
+ }
80
+ module.default(jobReq, context);
81
+ } catch (e) {
82
+ // eslint-disable-next-line no-console
83
+ console.error(e);
84
+ writeToFile('CODE_EXCEPTION', 532);
85
+ process.exit(0);
86
+ }
87
+ })
88
+ .catch((e) => {
89
+ // eslint-disable-next-line no-console
90
+ console.error(e);
91
+ writeToFile('INTERNAL_SERVER_ERROR', 500);
92
+ process.exit(0);
93
+ });
@@ -1,4 +1,27 @@
1
1
  'use strict';
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
26
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
27
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -13,7 +36,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
36
  };
14
37
  Object.defineProperty(exports, "__esModule", { value: true });
15
38
  const path_1 = require("path");
16
- const http_1 = __importDefault(require("http"));
39
+ const http_1 = __importStar(require("http"));
17
40
  const error_1 = __importDefault(require("../../error"));
18
41
  const fn_utils_1 = require("../../fn-utils");
19
42
  const credential_1 = __importDefault(require("../../internal/credential"));
@@ -30,6 +53,7 @@ const shell_1 = require("../../util_modules/shell");
30
53
  const http_functions_1 = require("../dependencies/http-functions");
31
54
  const events_1 = __importDefault(require("events"));
32
55
  const ensure_java_userconfig_1 = require("../../fn-utils/lib/ensure-java-userconfig");
56
+ const server_1 = require("../../util_modules/server");
33
57
  class LocalFunction {
34
58
  constructor(repl, target) {
35
59
  var _a, _b, _c;
@@ -73,15 +97,44 @@ class LocalFunction {
73
97
  return new error_1.default('Invalid input');
74
98
  }
75
99
  return (() => __awaiter(this, void 0, void 0, function* () {
76
- var _a, _b, _c, _d, _e, _f;
100
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
77
101
  const projectRoot = runtime_store_1.default.get('project.root');
78
102
  const accessToken = yield credential_1.default.getAccessToken();
79
103
  const slaveOptions = [];
80
104
  const debugPort = runtime_store_1.default.get('context.port.debug.' + constants_1.FN_TYPE.basic, null);
81
105
  yield fs_1.ASYNC.ensureFile(this.responseFile, true);
82
106
  yield fs_1.ASYNC.ensureFile(this.metaFile, true);
107
+ if (this.target.type === constants_1.FN_TYPE.job) {
108
+ const jobDetails = data;
109
+ const jobMetaDetails = {
110
+ headers: Object.assign(Object.assign({}, (_a = jobDetails.job_meta_details) === null || _a === void 0 ? void 0 : _a.headers), {
111
+ 'x-zc-projectid': (0, project_1.getProjectId)(),
112
+ 'x-zc-project-domain': (0, project_1.getDomainPrefix)() + '.' + constants_1.ORIGIN.app.replace('https://', ''),
113
+ 'x-zc-project-key': (0, project_1.getDomainKey)(),
114
+ 'x-zc-environment': (0, project_1.getEnvName)(),
115
+ 'x-zc-user-cred-type': 'token',
116
+ 'x-zc-user-cred-token': accessToken,
117
+ 'x-zc-admin-cred-type': 'token',
118
+ 'x-zc-admin-cred-token': accessToken,
119
+ 'x-zc-user-type': 'admin'
120
+ }),
121
+ params: ((_b = jobDetails.job_meta_details) === null || _b === void 0 ? void 0 : _b.params) || {},
122
+ jobpool_details: ((_c = jobDetails.job_meta_details) === null || _c === void 0 ? void 0 : _c.jobpool_details) || {
123
+ type: 'Function',
124
+ project_details: {
125
+ project_name: (0, project_1.getProjectName)(),
126
+ id: (0, project_1.getProjectId)()
127
+ }
128
+ }
129
+ };
130
+ jobDetails.job_meta_details = jobMetaDetails;
131
+ jobDetails.capacity = jobDetails.capacity || {
132
+ memory: '256'
133
+ };
134
+ data = jobDetails;
135
+ }
83
136
  const slaveFnTarget = { index: this.target.index, name: this.target.name };
84
- if ((_a = this.target.stack) === null || _a === void 0 ? void 0 : _a.startsWith(runtime_1.default.language.node.value)) {
137
+ if ((_d = this.target.stack) === null || _d === void 0 ? void 0 : _d.startsWith(runtime_1.default.language.node.value)) {
85
138
  if (debugPort !== null) {
86
139
  slaveOptions.push('--inspect-brk=' + debugPort);
87
140
  }
@@ -105,10 +158,10 @@ class LocalFunction {
105
158
  this.slave = (0, shell_1.spawn)('node', slaveOptions, {
106
159
  cwd: (0, path_1.join)(projectRoot, constants_1.FOLDERNAME.build, constants_1.FOLDERNAME.functions, this.target.name),
107
160
  stdio: 'pipe',
108
- 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_1.getProjectTimezone)(Intl.DateTimeFormat().resolvedOptions().timeZone) }, this.target.env_var)
161
+ 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: (_e = this.target) === null || _e === void 0 ? void 0 : _e.id, CATALYST_PROJECT_TIMEZONE: (0, project_1.getProjectTimezone)(Intl.DateTimeFormat().resolvedOptions().timeZone) }, this.target.env_var)
109
162
  }).RAW();
110
163
  }
111
- else if ((_b = this.target.stack) === null || _b === void 0 ? void 0 : _b.startsWith(runtime_1.default.language.java.value)) {
164
+ else if ((_f = this.target.stack) === null || _f === void 0 ? void 0 : _f.startsWith(runtime_1.default.language.java.value)) {
112
165
  const javaInvokerDir = (0, path_1.parse)(this.javaInvoker).dir;
113
166
  slaveOptions.push('-cp');
114
167
  slaveOptions.push(javaInvokerDir +
@@ -143,11 +196,11 @@ class LocalFunction {
143
196
  this.slave = (0, shell_1.spawn)(spawnCommand, slaveOptions, {
144
197
  cwd: (0, path_1.join)(projectRoot, constants_1.FOLDERNAME.build, constants_1.FOLDERNAME.functions, this.target.name),
145
198
  stdio: 'pipe',
146
- 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_1.getProjectTimezone)(Intl.DateTimeFormat().resolvedOptions().timeZone) }, this.target.env_var)
199
+ 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: (_g = this.target) === null || _g === void 0 ? void 0 : _g.id, CATALYST_PROJECT_TIMEZONE: (0, project_1.getProjectTimezone)(Intl.DateTimeFormat().resolvedOptions().timeZone) }, this.target.env_var)
147
200
  }).RAW();
148
201
  }
149
- else if ((_c = this.target.stack) === null || _c === void 0 ? void 0 : _c.startsWith(runtime_1.default.language.python.value)) {
150
- const stackVersion = (_d = this.target.stack) === null || _d === void 0 ? void 0 : _d.replace('python_', '');
202
+ else if ((_h = this.target.stack) === null || _h === void 0 ? void 0 : _h.startsWith(runtime_1.default.language.python.value)) {
203
+ const stackVersion = (_j = this.target.stack) === null || _j === void 0 ? void 0 : _j.replace('python_', '');
151
204
  const runtimesDir = constants_1.ENVPATH.runtimes.data;
152
205
  const httpPort = parseInt(runtime_store_1.default.get('context.port.http.' + constants_1.REMOTE_REF.functions.type[constants_1.FN_TYPE.basic]), 10);
153
206
  const runTimePort = yield port_resolver_1.default.getFreePort(httpPort, 20, false);
@@ -158,17 +211,72 @@ class LocalFunction {
158
211
  this.slave = (0, shell_1.spawn)(userConfig_1.default.get(`python${stackVersion}.bin`), slaveOptions, {
159
212
  cwd: (0, path_1.join)(projectRoot, constants_1.FOLDERNAME.build, constants_1.FOLDERNAME.functions, this.target.name),
160
213
  stdio: 'pipe',
161
- 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: (0, path_1.join)(projectRoot, constants_1.FOLDERNAME.build, constants_1.FOLDERNAME.functions, this.target.name), X_ZOHO_CATALYST_SERVER_LISTEN_PORT: runTimePort.toString(), CATALYST_PROJECT_TIMEZONE: (0, project_1.getProjectTimezone)(Intl.DateTimeFormat().resolvedOptions().timeZone) }, this.target.env_var)
214
+ 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: (_k = this.target) === null || _k === void 0 ? void 0 : _k.id, X_ZOHO_CATALYST_CODE_LOCATION: (0, path_1.join)(projectRoot, constants_1.FOLDERNAME.build, constants_1.FOLDERNAME.functions, this.target.name), X_ZOHO_CATALYST_SERVER_LISTEN_PORT: runTimePort.toString(), X_ZOHO_DATA_URL: `http://localhost:${runTimePort}/data`, X_ZOHO_CALLBACK_URL: `http://localhost:${runTimePort}/callback`, CATALYST_PROJECT_TIMEZONE: (0, project_1.getProjectTimezone)(Intl.DateTimeFormat().resolvedOptions().timeZone), X_ZOHO_ADMIN_CRED_TOKEN: 'dummy', X_ZOHO_PROJECT_SECRET_KEY: 'dummy', X_ZOHO_CATALYST_ORG: (0, project_1.getEnvId)(), X_ZOHO_CATALYST_ENVIRONMENT: 'Local', X_ZOHO_JOBMETA_JOBID: 'dummy', CATALYST_FUNCTION_TYPE: constants_1.REMOTE_REF.functions.type[this.target.type] }, this.target.env_var)
162
215
  }).RAW();
163
216
  this.slave.once('spawn', () => __awaiter(this, void 0, void 0, function* () {
217
+ var _o;
164
218
  const jsonData = data;
165
219
  jsonData.timestamp = Date.now();
166
- const reqJson = JSON.stringify(jsonData);
167
- yield (0, http_functions_1.checkIfRuntimeServerRunning)(runTimePort.toString());
168
220
  const writeResponse = (response, status) => {
169
221
  fs_1.SYNC.writeFile(this.responseFile, response);
170
222
  fs_1.SYNC.writeFile(this.metaFile, JSON.stringify({ response: { statusCode: status } }));
171
223
  };
224
+ if (this.target.type === constants_1.FN_TYPE.job) {
225
+ const jobDetailsServer = new http_1.Server((serverReq, serverRes) => __awaiter(this, void 0, void 0, function* () {
226
+ var _p, _q, _r;
227
+ if ((_p = serverReq.url) === null || _p === void 0 ? void 0 : _p.includes('data')) {
228
+ serverRes.writeHead(200);
229
+ serverRes.write(JSON.stringify({
230
+ data: jsonData
231
+ }));
232
+ serverRes.end();
233
+ }
234
+ else if ((_q = serverReq.url) === null || _q === void 0 ? void 0 : _q.includes('callback')) {
235
+ const reqData = yield new Promise((resolve) => {
236
+ const data = [];
237
+ serverReq.on('data', (chunk) => data.push(chunk));
238
+ serverReq.on('end', () => resolve(JSON.parse(Buffer.concat(data).toString())));
239
+ });
240
+ switch (reqData.job_status) {
241
+ case 200: {
242
+ writeResponse('SUCCESS', 200);
243
+ break;
244
+ }
245
+ case 532: {
246
+ writeResponse('CODE_EXCEPTION', 532);
247
+ break;
248
+ }
249
+ case 530: {
250
+ writeResponse('FAILURE', 530);
251
+ break;
252
+ }
253
+ case 500: {
254
+ writeResponse('INTERNAL_SERVER_ERROR', 500);
255
+ break;
256
+ }
257
+ }
258
+ serverRes.writeHead(200);
259
+ serverRes.end();
260
+ return;
261
+ }
262
+ else {
263
+ (0, logger_1.debug)(`Invalid request: (${serverReq.method}) ${serverReq.url}`);
264
+ writeResponse('INTERNAL_SERVER_ERROR', 500);
265
+ (_r = this.slave) === null || _r === void 0 ? void 0 : _r.kill('SIGINT');
266
+ return;
267
+ }
268
+ }));
269
+ const jobConnDestroyer = new server_1.ConnectionDestroyer(jobDetailsServer);
270
+ jobDetailsServer.listen(runTimePort, () => {
271
+ (0, logger_1.debug)('server listening on port :' + runTimePort);
272
+ });
273
+ (_o = this.slave) === null || _o === void 0 ? void 0 : _o.once('exit', () => {
274
+ jobConnDestroyer.destroy();
275
+ });
276
+ return;
277
+ }
278
+ const reqJson = JSON.stringify(jsonData);
279
+ yield (0, http_functions_1.checkIfRuntimeServerRunning)(runTimePort.toString());
172
280
  const req = http_1.default
173
281
  .request(`http://127.0.0.1:${runTimePort}`, {
174
282
  headers: {
@@ -239,11 +347,11 @@ class LocalFunction {
239
347
  exit: 2
240
348
  });
241
349
  }
242
- (_e = this.slave.stdout) === null || _e === void 0 ? void 0 : _e.on('data', (message) => {
350
+ (_l = this.slave.stdout) === null || _l === void 0 ? void 0 : _l.on('data', (message) => {
243
351
  (0, shell_1.clearLine)(process.stdout);
244
352
  (0, logger_1.info)(message.toString());
245
353
  });
246
- (_f = this.slave.stderr) === null || _f === void 0 ? void 0 : _f.on('data', (message) => {
354
+ (_m = this.slave.stderr) === null || _m === void 0 ? void 0 : _m.on('data', (message) => {
247
355
  const errorStr = js_1.JS.trim(message.toString());
248
356
  (0, shell_1.clearLine)(process.stdout);
249
357
  (0, logger_1.info)(errorStr);
@@ -50,7 +50,13 @@ exports.default = () => __awaiter(void 0, void 0, void 0, function* () {
50
50
  const tunnelServer = new tunnel_server_1.default(replServer);
51
51
  const projectRoot = runtime_store_1.default.get('project.root');
52
52
  try {
53
- yield (0, prepare_1.default)([constants_1.FN_TYPE.basic, constants_1.FN_TYPE.cron, constants_1.FN_TYPE.event, constants_1.FN_TYPE.integration]);
53
+ yield (0, prepare_1.default)([
54
+ constants_1.FN_TYPE.basic,
55
+ constants_1.FN_TYPE.cron,
56
+ constants_1.FN_TYPE.event,
57
+ constants_1.FN_TYPE.integration,
58
+ constants_1.FN_TYPE.job
59
+ ]);
54
60
  const targets = runtime_store_1.default
55
61
  .get('context.functions.targets', [])
56
62
  .filter((target) => {
@@ -47,17 +47,17 @@ function java(targets) {
47
47
  return (yield Promise.resolve().then(() => __importStar(require('./java')))).default(targets);
48
48
  });
49
49
  }
50
- function python(targets, fnWatchEvent) {
50
+ function python(targets) {
51
51
  return __awaiter(this, void 0, void 0, function* () {
52
- return (yield Promise.resolve().then(() => __importStar(require('./python')))).default(targets, fnWatchEvent);
52
+ return (yield Promise.resolve().then(() => __importStar(require('./python')))).default(targets);
53
53
  });
54
54
  }
55
- function prepareFunctions(targets, fnWatchEvent) {
55
+ function prepareFunctions(targets) {
56
56
  return __awaiter(this, void 0, void 0, function* () {
57
57
  return Promise.all([
58
58
  node(targets.filter((target) => { var _a; return ((_a = target.stack) === null || _a === void 0 ? void 0 : _a.startsWith(runtime_1.default.language.node.value)) && target.valid; })),
59
59
  java(targets.filter((target) => { var _a; return ((_a = target.stack) === null || _a === void 0 ? void 0 : _a.startsWith(runtime_1.default.language.java.value)) && target.valid; })),
60
- python(targets.filter((target) => { var _a; return ((_a = target.stack) === null || _a === void 0 ? void 0 : _a.startsWith(runtime_1.default.language.python.value)) && target.valid; }), fnWatchEvent)
60
+ python(targets.filter((target) => { var _a; return ((_a = target.stack) === null || _a === void 0 ? void 0 : _a.startsWith(runtime_1.default.language.python.value)) && target.valid; }))
61
61
  ]);
62
62
  });
63
63
  }
@@ -18,7 +18,6 @@ const java_1 = require("../../../fn-utils/lib/java");
18
18
  const runtime_store_1 = __importDefault(require("../../../runtime-store"));
19
19
  const constants_1 = require("../../../util_modules/constants");
20
20
  const fs_1 = require("../../../util_modules/fs");
21
- const fs_2 = require("fs");
22
21
  const throbber_1 = __importDefault(require("../../../throbber"));
23
22
  const error_1 = __importDefault(require("../../../error"));
24
23
  exports.default = (targets) => __awaiter(void 0, void 0, void 0, function* () {
@@ -56,7 +55,7 @@ exports.default = (targets) => __awaiter(void 0, void 0, void 0, function* () {
56
55
  const serverFiles = yield fs_1.ASYNC.walk((0, path_1.dirname)(javaServer));
57
56
  yield Promise.all(serverFiles.map((file) => __awaiter(void 0, void 0, void 0, function* () {
58
57
  try {
59
- return fs_1.ASYNC.copyFile(file.path, (0, path_1.join)(targetBuildPath, (0, path_1.relative)((0, path_1.dirname)(javaServer), file.path.replace('lib' + path_1.sep, ''))), fs_2.constants.COPYFILE_EXCL);
58
+ return fs_1.ASYNC.copyFile(file.path, (0, path_1.join)(targetBuildPath, (0, path_1.relative)((0, path_1.dirname)(javaServer), file.path.replace('lib' + path_1.sep, ''))), false);
60
59
  }
61
60
  catch (e) {
62
61
  const err = error_1.default.getErrorInstance(e).original;