zcatalyst-cli 1.7.1 → 1.9.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 (179) hide show
  1. package/README.md +31 -10
  2. package/docs/apig-utils.toml +69 -0
  3. package/docs/authentication/index.toml +15 -0
  4. package/docs/authentication/login.toml +14 -0
  5. package/docs/client-utils.toml +24 -0
  6. package/docs/command_needs/auth.toml +4 -0
  7. package/docs/command_needs/rc.toml +24 -0
  8. package/docs/commands/apig/disable.toml +4 -0
  9. package/docs/commands/apig/enable.toml +4 -0
  10. package/docs/commands/apig/status.toml +4 -0
  11. package/docs/commands/client/delete.toml +34 -0
  12. package/docs/commands/ds/export.toml +4 -0
  13. package/docs/commands/ds/import.toml +15 -0
  14. package/docs/commands/ds/status.toml +9 -0
  15. package/docs/commands/event/generate/index.toml +29 -0
  16. package/docs/commands/event/generate/integ.toml +4 -0
  17. package/docs/commands/functions/config.toml +11 -0
  18. package/docs/commands/functions/delete.toml +29 -0
  19. package/docs/commands/project/use.toml +9 -0
  20. package/docs/dc.toml +4 -0
  21. package/docs/deploy/features/apig.toml +4 -0
  22. package/docs/deploy/features/functions/index.toml +4 -0
  23. package/docs/event_generate/cache.toml +4 -0
  24. package/docs/event_generate/custom.toml +4 -0
  25. package/docs/event_generate/datastore.toml +4 -0
  26. package/docs/event_generate/filestore.toml +4 -0
  27. package/docs/event_generate/integration/cliq.toml +9 -0
  28. package/docs/execute-script.toml +19 -0
  29. package/docs/fn-utils/lib/common.toml +25 -0
  30. package/docs/fn-utils/lib/java.toml +34 -0
  31. package/docs/fn-utils/lib/node.toml +4 -0
  32. package/docs/init/index.toml +4 -0
  33. package/docs/internal/command.toml +4 -0
  34. package/docs/internal/config.toml +9 -0
  35. package/docs/internal/credential.toml +14 -0
  36. package/docs/option-filter.toml +45 -0
  37. package/docs/port-resolver.toml +9 -0
  38. package/docs/pull/index.toml +4 -0
  39. package/docs/serve/index.toml +4 -0
  40. package/docs/shell/index.toml +4 -0
  41. package/docs/util_modules/project.toml +9 -0
  42. package/lib/apig-utils.js +133 -43
  43. package/lib/archiver.js +4 -2
  44. package/lib/authentication/index.js +44 -9
  45. package/lib/authentication/login.js +51 -8
  46. package/lib/bin/catalyst.js +14 -7
  47. package/lib/client-utils.js +47 -14
  48. package/lib/client.js +1 -1
  49. package/lib/command_needs/auth.js +5 -3
  50. package/lib/command_needs/rc.js +35 -13
  51. package/lib/commands/apig/disable.js +8 -3
  52. package/lib/commands/apig/enable.js +8 -3
  53. package/lib/commands/apig/status.js +29 -9
  54. package/lib/commands/client/delete.js +65 -26
  55. package/lib/commands/client/setup.js +2 -2
  56. package/lib/commands/deploy.js +2 -2
  57. package/lib/commands/ds/export.js +16 -8
  58. package/lib/commands/ds/import.js +20 -14
  59. package/lib/commands/ds/status.js +22 -11
  60. package/lib/commands/event/generate/index.js +62 -12
  61. package/lib/commands/event/generate/integ.js +7 -3
  62. package/lib/commands/functions/add.js +2 -2
  63. package/lib/commands/functions/config.js +11 -3
  64. package/lib/commands/functions/delete.js +36 -13
  65. package/lib/commands/functions/setup.js +2 -2
  66. package/lib/commands/functions/shell.js +2 -2
  67. package/lib/commands/help.js +1 -1
  68. package/lib/commands/index.js +2 -3
  69. package/lib/commands/init.js +4 -4
  70. package/lib/commands/login.js +2 -2
  71. package/lib/commands/logout.js +2 -2
  72. package/lib/commands/project/list.js +2 -2
  73. package/lib/commands/project/reset.js +1 -1
  74. package/lib/commands/project/use.js +18 -2
  75. package/lib/commands/pull.js +5 -5
  76. package/lib/commands/run.js +2 -2
  77. package/lib/commands/serve.js +7 -6
  78. package/lib/commands/token/generate.js +2 -2
  79. package/lib/commands/token/list.js +2 -2
  80. package/lib/commands/token/revoke.js +2 -2
  81. package/lib/commands/whoami.js +1 -1
  82. package/lib/dc.js +11 -1
  83. package/lib/deploy/features/apig.js +33 -33
  84. package/lib/deploy/features/client.js +12 -19
  85. package/lib/deploy/features/functions/index.js +14 -4
  86. package/lib/deploy/features/index.js +1 -1
  87. package/lib/deploy/index.js +2 -9
  88. package/lib/endpoints/index.js +5 -2
  89. package/lib/endpoints/lib/apig.js +15 -5
  90. package/lib/endpoints/lib/applogic.js +12 -4
  91. package/lib/endpoints/lib/cache.js +9 -3
  92. package/lib/endpoints/lib/catalyst-details.js +37 -0
  93. package/lib/endpoints/lib/client.js +15 -5
  94. package/lib/endpoints/lib/datastore.js +6 -2
  95. package/lib/endpoints/lib/ds-bulk.js +15 -5
  96. package/lib/endpoints/lib/env.js +3 -1
  97. package/lib/endpoints/lib/event-bus.js +3 -1
  98. package/lib/endpoints/lib/filestore.js +6 -2
  99. package/lib/endpoints/lib/functions.js +12 -4
  100. package/lib/endpoints/lib/project.js +9 -3
  101. package/lib/endpoints/lib/queue.js +9 -3
  102. package/lib/endpoints/lib/sdk.js +3 -1
  103. package/lib/endpoints/lib/zcql.js +3 -1
  104. package/lib/error.js +18 -0
  105. package/lib/errorOut.js +10 -7
  106. package/lib/event_generate/cache.js +9 -1
  107. package/lib/event_generate/custom.js +6 -1
  108. package/lib/event_generate/datastore.js +12 -2
  109. package/lib/event_generate/filestore.js +9 -1
  110. package/lib/event_generate/integration/cliq.js +10 -2
  111. package/lib/execute-script.js +35 -6
  112. package/lib/express_middlewares/unknownReqProxy.js +18 -7
  113. package/lib/fn-utils/index.js +1 -1
  114. package/lib/fn-utils/lib/common.js +95 -17
  115. package/lib/fn-utils/lib/integ.js +4 -3
  116. package/lib/fn-utils/lib/java.js +49 -13
  117. package/lib/fn-utils/lib/node.js +15 -6
  118. package/lib/fn-watcher.js +67 -0
  119. package/lib/index.js +13 -12
  120. package/lib/init/dependencies/package-json.js +3 -1
  121. package/lib/init/features/functions/index.js +41 -29
  122. package/lib/init/features/functions/languages/java.js +5 -4
  123. package/lib/init/features/functions/languages/node.js +6 -5
  124. package/lib/init/features/index.js +8 -4
  125. package/lib/init/index.js +10 -3
  126. package/lib/internal/api.js +9 -9
  127. package/lib/internal/command.js +19 -14
  128. package/lib/internal/config.js +15 -15
  129. package/lib/internal/credential.js +33 -7
  130. package/lib/internal/rc.js +18 -10
  131. package/lib/migration/global/1.4.0.js +4 -2
  132. package/lib/migration/global/1.6.2.js +4 -2
  133. package/lib/migration/index.js +1 -1
  134. package/lib/option-filter.js +79 -22
  135. package/lib/optional-import.js +1 -1
  136. package/lib/plugin-loader.js +1 -1
  137. package/lib/port-resolver.js +23 -3
  138. package/lib/prompt.js +1 -1
  139. package/lib/pull/features/functions/index.js +7 -7
  140. package/lib/pull/features/index.js +1 -1
  141. package/lib/pull/index.js +10 -3
  142. package/lib/repl-server.js +9 -5
  143. package/lib/serve/features/index.js +11 -2
  144. package/lib/serve/index.js +19 -9
  145. package/lib/serve/server/index.js +137 -60
  146. package/lib/serve/server/lib/client.js +1 -1
  147. package/lib/serve/server/lib/master.js +16 -3
  148. package/lib/serve/server/lib/node.js +1 -1
  149. package/lib/shell/dependencies/http-functions.js +120 -90
  150. package/lib/shell/dependencies/invoker/integ/node.js +1 -1
  151. package/lib/shell/dependencies/local-function.js +86 -119
  152. package/lib/shell/index.js +10 -10
  153. package/lib/shell/prepare/index.js +1 -4
  154. package/lib/shell/prepare/languages/index.js +15 -4
  155. package/lib/track.js +4 -1
  156. package/lib/util_modules/config/index.js +1 -1
  157. package/lib/util_modules/config/lib/apig.js +3 -3
  158. package/lib/util_modules/config/lib/client.js +3 -3
  159. package/lib/util_modules/config/lib/functions.js +5 -5
  160. package/lib/util_modules/constants/index.js +22 -20
  161. package/lib/util_modules/constants/lib/regex.js +3 -2
  162. package/lib/util_modules/constants/lib/runtime.js +9 -0
  163. package/lib/util_modules/contextHelp.js +63 -123
  164. package/lib/util_modules/fs/index.js +1 -1
  165. package/lib/util_modules/fs/lib/async.js +19 -2
  166. package/lib/util_modules/fs/lib/sync.js +13 -1
  167. package/lib/util_modules/logger.js +12 -20
  168. package/lib/util_modules/option.js +1 -13
  169. package/lib/util_modules/project.js +75 -26
  170. package/lib/util_modules/shell.js +8 -2
  171. package/package.json +45 -45
  172. package/scripts/postInstall.js +31 -0
  173. package/lib/cjson.js +0 -20
  174. package/lib/log-error.js +0 -18
  175. package/lib/util_modules/big-json.js +0 -16
  176. package/lib/util_modules/constants/lib/stack.js +0 -6
  177. package/templates/init/functions/.DS_Store +0 -0
  178. package/templates/init/functions/node/.DS_Store +0 -0
  179. package/templates/init/functions/node/integ/.DS_Store +0 -0
@@ -8,6 +8,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __rest = (this && this.__rest) || function (s, e) {
12
+ var t = {};
13
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
14
+ t[p] = s[p];
15
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
16
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
17
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
18
+ t[p[i]] = s[p[i]];
19
+ }
20
+ return t;
21
+ };
11
22
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
23
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
24
  };
@@ -18,6 +29,7 @@ const fn_utils_1 = require("../../fn-utils");
18
29
  const plugin_loader_1 = __importDefault(require("../../plugin-loader"));
19
30
  const runtime_store_1 = __importDefault(require("../../runtime-store"));
20
31
  const constants_1 = require("../../util_modules/constants");
32
+ const runtime_1 = __importDefault(require("../../util_modules/constants/lib/runtime"));
21
33
  const fs_1 = require("../../util_modules/fs");
22
34
  const js_1 = require("../../util_modules/js");
23
35
  const logger_1 = require("../../util_modules/logger");
@@ -27,26 +39,26 @@ class Server {
27
39
  constructor() {
28
40
  this.targetDetailsArr = [];
29
41
  }
30
- _spinUpMaster(masterPort) {
31
- return __awaiter(this, void 0, void 0, function* () {
32
- const httpPort = parseInt(runtime_store_1.default.get('context.port.http.' + constants_1.REMOTE_REF.functions.type[constants_1.FN_TYPE.basic]), 10);
33
- const debugPort = parseInt(runtime_store_1.default.get('context.port.debug.' + constants_1.REMOTE_REF.functions.type[constants_1.FN_TYPE.basic], '-1'), 10);
34
- if (!isNaN(httpPort)) {
35
- const fnTargets = runtime_store_1.default.get('context.functions.targets');
36
- fnTargets
37
- .filter((t) => t.url !== undefined && t.type === constants_1.FN_TYPE.basic)
38
- .map((t) => js_1.JS.omit(t, ['zip_stream', 'watcher', 'localFn']))
39
- .forEach((t) => {
40
- this.targetDetailsArr.push({
41
- type: 'functions',
42
- httpPort,
43
- debugPort,
44
- target: t
45
- });
42
+ _addBasicFnDetails() {
43
+ const fnTargets = runtime_store_1.default.get('context.functions.targets', []);
44
+ if (fnTargets.length === 0) {
45
+ logger_1.debug('No basic functions to add');
46
+ }
47
+ const httpPort = parseInt(runtime_store_1.default.get('context.port.http.' + constants_1.REMOTE_REF.functions.type[constants_1.FN_TYPE.basic]), 10);
48
+ const debugPort = parseInt(runtime_store_1.default.get('context.port.debug.' + constants_1.REMOTE_REF.functions.type[constants_1.FN_TYPE.basic], '-1'), 10);
49
+ if (!isNaN(httpPort)) {
50
+ fnTargets
51
+ .filter((t) => t.url !== undefined && t.type === constants_1.FN_TYPE.basic)
52
+ .map((t) => js_1.JS.omit(t, ['zip_stream', 'localFn']))
53
+ .forEach((t) => {
54
+ this.targetDetailsArr.push({
55
+ type: 'functions',
56
+ httpPort,
57
+ debugPort,
58
+ target: t
46
59
  });
47
- }
48
- return master_1.default(masterPort, this.targetDetailsArr);
49
- });
60
+ });
61
+ }
50
62
  }
51
63
  _spinUpJavaServer(details, masterPort) {
52
64
  var _a, _b, _c, _d;
@@ -97,10 +109,11 @@ class Server {
97
109
  var _a, _b, _c;
98
110
  return __awaiter(this, void 0, void 0, function* () {
99
111
  const label = `${details.type}[${(_a = details.target) === null || _a === void 0 ? void 0 : _a.name}]`;
112
+ const _d = details.target, { ['watcher']: _ } = _d, serialisableTarget = __rest(_d, ['watcher']);
100
113
  const opts = [
101
114
  path_1.join(__dirname, 'lib', details.type === 'client' ? 'client.js' : 'node.js'),
102
115
  details.httpPort + '',
103
- JSON.stringify(details.target)
116
+ JSON.stringify(serialisableTarget)
104
117
  ];
105
118
  if (details.debugPort !== -1) {
106
119
  logger_1.labeled(label, 'you can attach your debugger at port : ' + details.debugPort).MESSAGE();
@@ -141,43 +154,49 @@ class Server {
141
154
  debugPort
142
155
  });
143
156
  }
157
+ startServer(details, masterPort) {
158
+ var _a;
159
+ return __awaiter(this, void 0, void 0, function* () {
160
+ if (details.target === undefined) {
161
+ return;
162
+ }
163
+ let currentProcess;
164
+ if ((_a = details.target.stack) === null || _a === void 0 ? void 0 : _a.startsWith(runtime_1.default.language.java)) {
165
+ currentProcess = yield this._spinUpJavaServer(details, masterPort);
166
+ }
167
+ else {
168
+ currentProcess = yield this._spinUpProcess(details, masterPort);
169
+ }
170
+ details.process = currentProcess;
171
+ const stdout = currentProcess.stdout;
172
+ if (stdout && typeof stdout.pipe === 'function') {
173
+ stdout.pipe(process.stdout);
174
+ }
175
+ const stderr = currentProcess.stderr;
176
+ if (stderr && typeof stderr.pipe === 'function') {
177
+ stderr.pipe(process.stderr);
178
+ }
179
+ return new Promise((res) => setTimeout(res, 100));
180
+ });
181
+ }
144
182
  start() {
145
183
  return __awaiter(this, void 0, void 0, function* () {
146
184
  const masterPort = runtime_store_1.default.get(`context.port.http.master`, -1);
147
185
  if (masterPort === -1) {
148
186
  throw new error_1.default('master port cannot be undefined', { exit: 2 });
149
187
  }
150
- yield Promise.all(this.targetDetailsArr.map((details) => __awaiter(this, void 0, void 0, function* () {
151
- if (details.target === undefined) {
152
- return;
153
- }
154
- let currentProcess;
155
- switch (details.target.stack) {
156
- case constants_1.STACK.java8:
157
- currentProcess = yield this._spinUpJavaServer(details, masterPort);
158
- break;
159
- default:
160
- currentProcess = yield this._spinUpProcess(details, masterPort);
161
- break;
162
- }
163
- Server.process = [...Server.process, currentProcess];
164
- const stdout = currentProcess.stdout;
165
- if (stdout && typeof stdout.pipe === 'function') {
166
- stdout.pipe(process.stdout);
167
- }
168
- const stderr = currentProcess.stderr;
169
- if (stderr && typeof stderr.pipe === 'function') {
170
- stderr.pipe(process.stderr);
171
- }
172
- })));
173
- const masterServer = yield this._spinUpMaster(masterPort);
188
+ yield Promise.all(this.targetDetailsArr.map((details) => __awaiter(this, void 0, void 0, function* () { return this.startServer(details, masterPort); })));
189
+ this._addBasicFnDetails();
174
190
  if (this.targetDetailsArr.length === 0) {
175
- throw new error_1.default('master server started before other server', { exit: 2 });
191
+ throw new error_1.default('Trying to start master server before other server', {
192
+ exit: 2
193
+ });
176
194
  }
177
- Server.process.push(masterServer);
195
+ const server = yield master_1.default(masterPort, this.targetDetailsArr);
196
+ this.masterServer = server;
178
197
  return new Promise((res) => {
179
- masterServer.on('listening', () => {
180
- Server.process.forEach((process) => process.emit('start'));
198
+ server.on('listening', () => {
199
+ this.targetDetailsArr.forEach((targetDetails) => { var _a; return (_a = targetDetails.process) === null || _a === void 0 ? void 0 : _a.emit('start'); });
181
200
  res();
182
201
  });
183
202
  });
@@ -185,30 +204,88 @@ class Server {
185
204
  }
186
205
  wait() {
187
206
  return __awaiter(this, void 0, void 0, function* () {
188
- if (Server.process.length === 0) {
189
- logger_1.debug('The server is not started yet.');
207
+ if (!this.masterServer) {
208
+ logger_1.debug('Master server is not started yet.');
190
209
  throw new error_1.default('Server cannot be put to wait state.', { exit: 2 });
191
210
  }
211
+ this.targetDetailsArr.forEach((targetdetails) => {
212
+ var _a, _b;
213
+ const target = targetdetails.target;
214
+ if (targetdetails.type === 'functions' || !target || !targetdetails.process) {
215
+ return;
216
+ }
217
+ (_a = target.watcher) === null || _a === void 0 ? void 0 : _a.on('preparing', () => __awaiter(this, void 0, void 0, function* () {
218
+ yield this.kill(targetdetails.process);
219
+ }));
220
+ (_b = target.watcher) === null || _b === void 0 ? void 0 : _b.on('compiled', () => __awaiter(this, void 0, void 0, function* () {
221
+ yield this.restart(target);
222
+ logger_1.labeled(`server[${target.name}]`, 'ready!').MESSAGE();
223
+ setTimeout(() => {
224
+ var _a;
225
+ (_a = target.watcher) === null || _a === void 0 ? void 0 : _a.emit('next');
226
+ }, 1000);
227
+ }));
228
+ }, this);
192
229
  return new Promise((res) => {
193
230
  ['SIGINT', 'SIGTERM'].forEach((sig) => process.on(sig, res));
194
231
  });
195
232
  });
196
233
  }
197
- stop() {
198
- Server.process.forEach((serverProcess) => {
199
- if (serverProcess.pid === undefined) {
200
- try {
201
- serverProcess.close();
234
+ kill(serverProcess) {
235
+ return __awaiter(this, void 0, void 0, function* () {
236
+ return new Promise((resolve) => {
237
+ if (serverProcess.pid === undefined) {
238
+ try {
239
+ serverProcess.close(() => resolve());
240
+ }
241
+ catch (e) {
242
+ serverProcess.emit('close');
243
+ }
202
244
  }
203
- catch (e) {
204
- serverProcess.emit('close');
245
+ if (serverProcess.exitCode === null) {
246
+ serverProcess.kill('SIGINT');
205
247
  }
248
+ resolve();
249
+ });
250
+ });
251
+ }
252
+ restart(target) {
253
+ return __awaiter(this, void 0, void 0, function* () {
254
+ const targetIdx = this.targetDetailsArr.findIndex((targetDetails) => { var _a; return target.source === ((_a = targetDetails.target) === null || _a === void 0 ? void 0 : _a.source); });
255
+ if (targetIdx === -1) {
256
+ throw new error_1.default('Unknown target given for restart.', { exit: 2 });
206
257
  }
207
- else {
208
- serverProcess.kill('SIGINT');
258
+ if (this.targetDetailsArr[targetIdx].type === 'functions') {
259
+ return;
260
+ }
261
+ const targetProcess = this.targetDetailsArr[targetIdx].process;
262
+ if (!targetProcess) {
263
+ throw new error_1.default('Process is not defined for this target.', { exit: 2 });
264
+ }
265
+ yield this.kill(targetProcess);
266
+ yield this.startServer(this.targetDetailsArr[targetIdx], runtime_store_1.default.get(`context.port.http.master`, -1));
267
+ });
268
+ }
269
+ stop() {
270
+ return __awaiter(this, void 0, void 0, function* () {
271
+ yield Promise.all(this.targetDetailsArr.map((targetdetails) => __awaiter(this, void 0, void 0, function* () {
272
+ var _a;
273
+ const target = targetdetails.target;
274
+ if (!target) {
275
+ return;
276
+ }
277
+ yield ((_a = target.watcher) === null || _a === void 0 ? void 0 : _a.close());
278
+ if (targetdetails.process) {
279
+ return this.kill(targetdetails.process);
280
+ }
281
+ })));
282
+ if (this.masterServer) {
283
+ const masterServer = this.masterServer;
284
+ return new Promise((res, rej) => {
285
+ masterServer.close((err) => (err ? rej(err) : res()));
286
+ });
209
287
  }
210
288
  });
211
289
  }
212
290
  }
213
- Server.process = [];
214
291
  exports.default = Server;
@@ -14,7 +14,7 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
14
14
  var __importStar = (this && this.__importStar) || function (mod) {
15
15
  if (mod && mod.__esModule) return mod;
16
16
  var result = {};
17
- if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
18
  __setModuleDefault(result, mod);
19
19
  return result;
20
20
  };
@@ -29,6 +29,7 @@ const constants_1 = require("../../../util_modules/constants");
29
29
  const js_1 = require("../../../util_modules/js");
30
30
  const logger_2 = require("../../../util_modules/logger");
31
31
  const project_2 = require("../../../util_modules/project");
32
+ const option_1 = require("../../../util_modules/option");
32
33
  const removeSecure = (str) => str.replace(/;\s?Secure/i, '').replace(/;\s?SameSite=None/i, '');
33
34
  const redirectByAuth = (req, res, redirectUrl) => {
34
35
  if (req.headers['x-zc-user-cred-token'] === undefined) {
@@ -90,9 +91,10 @@ function spinUpMaster(listenPort, otherServerDetails) {
90
91
  key: project_2.getDomainKey(),
91
92
  env_name: project_2.getEnvName()
92
93
  };
93
- const bindPort = parseInt(runtime_store_1.default.get('context.port.bind', '-1'), 10);
94
94
  const rules = runtime_store_1.default.get('context.apig.local.config', -1);
95
+ const customProxyUrl = option_1.getOptionValue('proxy');
95
96
  const app = express_1.default();
97
+ const connections = {};
96
98
  let systemRoutes;
97
99
  app.use(logger_1.default);
98
100
  app.use(cookie_parser_1.default);
@@ -132,7 +134,8 @@ function spinUpMaster(listenPort, otherServerDetails) {
132
134
  const appTarget = `http://127.0.0.1:${serverDetail.httpPort}/app/`;
133
135
  app.use('/app', (req, res) => {
134
136
  var _a;
135
- if (req.url.startsWith('/local-redirect') && ((_a = serverDetail.target) === null || _a === void 0 ? void 0 : _a.login_redirect)) {
137
+ if (req.url.startsWith('/local-redirect') &&
138
+ ((_a = serverDetail.target) === null || _a === void 0 ? void 0 : _a.login_redirect)) {
136
139
  const isUrl = serverDetail.target.login_redirect.startsWith('/');
137
140
  if (isUrl) {
138
141
  res.redirect(serverDetail.target.login_redirect);
@@ -204,7 +207,7 @@ function spinUpMaster(listenPort, otherServerDetails) {
204
207
  });
205
208
  }
206
209
  });
207
- app.use('/', unknownReqProxy_1.default(proxy, { listenPort, bindPort, systemRules: systemRoutes }));
210
+ app.use('/', unknownReqProxy_1.default(proxy, customProxyUrl));
208
211
  app.use(error_handler_1.default);
209
212
  const expressServer = app.listen(listenPort).on('error', (err) => {
210
213
  if (proxy) {
@@ -212,6 +215,16 @@ function spinUpMaster(listenPort, otherServerDetails) {
212
215
  }
213
216
  console.error(err);
214
217
  });
218
+ expressServer.maxConnections = 100;
219
+ process.once('SIGINT', () => Object.keys(connections).forEach((sokcetId) => { var _a; return (_a = connections[sokcetId]) === null || _a === void 0 ? void 0 : _a.destroy(); }));
220
+ expressServer.on('connection', (socket) => {
221
+ const offset = Math.floor(Math.random() * 10);
222
+ const id = Date.now() + offset + '';
223
+ connections[id] = socket;
224
+ socket.on('close', () => {
225
+ delete connections[id];
226
+ });
227
+ });
215
228
  expressServer.on('close', () => {
216
229
  logger_2.info('Shutting down server');
217
230
  proxy.close();
@@ -14,7 +14,7 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
14
14
  var __importStar = (this && this.__importStar) || function (mod) {
15
15
  if (mod && mod.__esModule) return mod;
16
16
  var result = {};
17
- if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
18
  __setModuleDefault(result, mod);
19
19
  return result;
20
20
  };
@@ -24,11 +24,15 @@ const project_1 = __importDefault(require("../../express_middlewares/project"));
24
24
  const fn_utils_1 = require("../../fn-utils");
25
25
  const runtime_store_1 = __importDefault(require("../../runtime-store"));
26
26
  const constants_1 = require("../../util_modules/constants");
27
+ const runtime_1 = __importDefault(require("../../util_modules/constants/lib/runtime"));
27
28
  const fs_1 = require("../../util_modules/fs");
28
29
  const js_1 = require("../../util_modules/js");
29
30
  const logger_2 = require("../../util_modules/logger");
30
31
  const project_2 = require("../../util_modules/project");
31
32
  const shell_1 = require("../../util_modules/shell");
33
+ const logUrl = (name, pthName, httpPort, masterPort) => {
34
+ logger_2.labeled(`functions(${name})`, 'URL => http://localhost:' + (masterPort === -1 ? httpPort : masterPort) + pthName).MESSAGE();
35
+ };
32
36
  class HttpFunctions {
33
37
  constructor(repl) {
34
38
  this.repl = repl;
@@ -43,6 +47,7 @@ class HttpFunctions {
43
47
  this.responseFile = path_1.join(runtime_store_1.default.get('cwd'), '.build', '.catalyst', 'user_res_body');
44
48
  this.metaFile = path_1.join(runtime_store_1.default.get('cwd'), '.build', '.catalyst', 'user_meta.json');
45
49
  this.runningSlaves = [];
50
+ this.targets = [];
46
51
  this.q = new better_queue_1.default((opts, cb) => {
47
52
  var _a, _b;
48
53
  fs_1.SYNC.ensureFile(this.responseFile, true);
@@ -66,7 +71,7 @@ class HttpFunctions {
66
71
  }
67
72
  }).RAW();
68
73
  (_a = slave.stdout) === null || _a === void 0 ? void 0 : _a.on('data', (data) => {
69
- logger_2.info(data);
74
+ logger_2.info(Buffer.isBuffer(data) ? data.toString() : data);
70
75
  });
71
76
  (_b = slave.stderr) === null || _b === void 0 ? void 0 : _b.on('data', (data) => {
72
77
  const errorStr = data.toString().trim();
@@ -105,6 +110,12 @@ class HttpFunctions {
105
110
  return __awaiter(this, void 0, void 0, function* () {
106
111
  const debugPort = parseInt(runtime_store_1.default.get('context.port.debug.' + constants_1.REMOTE_REF.functions.type[constants_1.FN_TYPE.basic], '-1'), 10);
107
112
  const httpPort = parseInt(runtime_store_1.default.get('context.port.http.' + constants_1.REMOTE_REF.functions.type[constants_1.FN_TYPE.basic]), 10);
113
+ if (this.repl !== undefined) {
114
+ this.app.use((_req, _res, next) => {
115
+ shell_1.clearLine(process.stdout);
116
+ next();
117
+ });
118
+ }
108
119
  this.app.use(logger_1.default);
109
120
  this.app.use(cookie_parser_1.default);
110
121
  this.app.use(authenticator_1.default);
@@ -116,83 +127,86 @@ class HttpFunctions {
116
127
  env_name: project_2.getEnvName()
117
128
  }));
118
129
  const reqHandler = (fnTarget) => (request, response, next) => {
130
+ var _a, _b;
119
131
  const urlParts = new url_1.URL(request.originalUrl, request.protocol + '://' + request.get('host')).searchParams;
120
132
  const query = JSON.stringify(Object.fromEntries(urlParts.entries()));
121
133
  const slaveOptions = [];
122
134
  let slave = null;
123
135
  if (fnTarget.type === undefined) {
124
- throw new error_1.default('Function target type is not defined', { exit: 2 });
136
+ throw new error_1.default('Function target type is not defined', {
137
+ exit: 2
138
+ });
125
139
  }
126
- switch (fnTarget.stack) {
127
- case constants_1.STACK.node10:
128
- if (debugPort !== -1) {
129
- slaveOptions.push('--inspect-brk=' + debugPort);
130
- }
131
- slaveOptions.push(this.nodeInvoker[fnTarget.type]);
132
- slaveOptions.push(JSON.stringify(fnTarget));
133
- slaveOptions.push(query);
134
- slaveOptions.push(JSON.stringify({
135
- 'x-zc-projectid': request.headers['x-zc-projectid'],
136
- 'x-zc-project-domain': request.headers['x-zc-project-domain'],
137
- 'x-zc-project-key': request.headers['x-zc-project-key'],
138
- 'x-zc-environment': request.headers['x-zc-environment']
139
- }));
140
- slaveOptions.push(JSON.stringify({
141
- 'x-zc-user-cred-type': request.headers['x-zc-user-cred-type'],
142
- 'x-zc-user-cred-token': request.headers['x-zc-user-cred-token'],
143
- 'x-zc-admin-cred-type': request.headers['x-zc-admin-cred-type'],
144
- 'x-zc-admin-cred-token': request.headers['x-zc-admin-cred-token'],
145
- 'x-zc-cookie': request.headers['x-zc-cookie']
146
- }));
147
- slaveOptions.push(JSON.stringify(path_1.join(runtime_store_1.default.get('cwd'), '.build')));
148
- slave = this.q.push({
149
- command: 'node',
150
- options: slaveOptions,
151
- pth: fnTarget.source,
152
- req: request,
153
- id: fnTarget.name
154
- });
155
- break;
156
- case constants_1.STACK.java8:
157
- const javaInvokerDir = path_1.parse(this.javaInvoker[fnTarget.type]).dir;
158
- slaveOptions.push('-cp');
159
- slaveOptions.push(javaInvokerDir +
160
- fn_utils_1.fnUtils.java.classPathSep +
161
- path_1.join(javaInvokerDir, 'lib', '*'));
162
- if (debugPort !== -1) {
163
- slaveOptions.push('-Xdebug');
164
- slaveOptions.push('-Xrunjdwp:transport=dt_socket,address=' +
165
- debugPort +
166
- ',server=y,suspend=y');
167
- }
168
- slaveOptions.push(path_1.basename(this.javaInvoker[fnTarget.type]));
169
- slaveOptions.push(javaInvokerDir);
170
- slaveOptions.push(JSON.stringify(fnTarget));
171
- slaveOptions.push(query);
172
- slaveOptions.push(JSON.stringify({
173
- 'x-zc-projectid': request.headers['x-zc-projectid'],
174
- 'x-zc-project-domain': request.headers['x-zc-project-domain'],
175
- 'x-zc-project-key': request.headers['x-zc-project-key'],
176
- 'x-zc-environment': request.headers['x-zc-environment']
177
- }));
178
- slaveOptions.push(JSON.stringify({
179
- 'x-zc-user-cred-type': request.headers['x-zc-user-cred-type'],
180
- 'x-zc-user-cred-token': request.headers['x-zc-user-cred-token'],
181
- 'x-zc-admin-cred-type': request.headers['x-zc-admin-cred-type'],
182
- 'x-zc-admin-cred-token': request.headers['x-zc-admin-cred-token'],
183
- 'x-zc-cookie': request.headers['x-zc-cookie']
184
- }));
185
- slave = this.q.push({
186
- command: 'java',
187
- options: slaveOptions,
188
- pth: fnTarget.source,
189
- req: request,
190
- id: fnTarget.name
191
- });
192
- break;
140
+ if ((_a = fnTarget.stack) === null || _a === void 0 ? void 0 : _a.startsWith(runtime_1.default.language.nodejs)) {
141
+ if (debugPort !== -1) {
142
+ slaveOptions.push('--inspect-brk=' + debugPort);
143
+ }
144
+ slaveOptions.push(this.nodeInvoker[fnTarget.type]);
145
+ slaveOptions.push(JSON.stringify(fnTarget));
146
+ slaveOptions.push(query);
147
+ slaveOptions.push(JSON.stringify({
148
+ 'x-zc-projectid': request.headers['x-zc-projectid'],
149
+ 'x-zc-project-domain': request.headers['x-zc-project-domain'],
150
+ 'x-zc-project-key': request.headers['x-zc-project-key'],
151
+ 'x-zc-environment': request.headers['x-zc-environment']
152
+ }));
153
+ slaveOptions.push(JSON.stringify({
154
+ 'x-zc-user-cred-type': request.headers['x-zc-user-cred-type'],
155
+ 'x-zc-user-cred-token': request.headers['x-zc-user-cred-token'],
156
+ 'x-zc-admin-cred-type': request.headers['x-zc-admin-cred-type'],
157
+ 'x-zc-admin-cred-token': request.headers['x-zc-admin-cred-token'],
158
+ 'x-zc-cookie': request.headers['x-zc-cookie']
159
+ }));
160
+ slaveOptions.push(JSON.stringify(path_1.join(runtime_store_1.default.get('cwd'), '.build')));
161
+ slave = this.q.push({
162
+ command: 'node',
163
+ options: slaveOptions,
164
+ pth: fnTarget.source,
165
+ req: request,
166
+ id: fnTarget.name
167
+ });
168
+ }
169
+ else if ((_b = fnTarget.stack) === null || _b === void 0 ? void 0 : _b.startsWith(runtime_1.default.language.java)) {
170
+ const javaInvokerDir = path_1.parse(this.javaInvoker[fnTarget.type]).dir;
171
+ slaveOptions.push('-cp');
172
+ slaveOptions.push(javaInvokerDir +
173
+ fn_utils_1.fnUtils.java.classPathSep +
174
+ path_1.join(javaInvokerDir, 'lib', '*'));
175
+ if (debugPort !== -1) {
176
+ slaveOptions.push('-Xdebug');
177
+ slaveOptions.push('-Xrunjdwp:transport=dt_socket,address=' +
178
+ debugPort +
179
+ ',server=y,suspend=y');
180
+ }
181
+ slaveOptions.push(path_1.basename(this.javaInvoker[fnTarget.type]));
182
+ slaveOptions.push(javaInvokerDir);
183
+ slaveOptions.push(JSON.stringify(fnTarget));
184
+ slaveOptions.push(query);
185
+ slaveOptions.push(JSON.stringify({
186
+ 'x-zc-projectid': request.headers['x-zc-projectid'],
187
+ 'x-zc-project-domain': request.headers['x-zc-project-domain'],
188
+ 'x-zc-project-key': request.headers['x-zc-project-key'],
189
+ 'x-zc-environment': request.headers['x-zc-environment']
190
+ }));
191
+ slaveOptions.push(JSON.stringify({
192
+ 'x-zc-user-cred-type': request.headers['x-zc-user-cred-type'],
193
+ 'x-zc-user-cred-token': request.headers['x-zc-user-cred-token'],
194
+ 'x-zc-admin-cred-type': request.headers['x-zc-admin-cred-type'],
195
+ 'x-zc-admin-cred-token': request.headers['x-zc-admin-cred-token'],
196
+ 'x-zc-cookie': request.headers['x-zc-cookie']
197
+ }));
198
+ slave = this.q.push({
199
+ command: 'java',
200
+ options: slaveOptions,
201
+ pth: fnTarget.source,
202
+ req: request,
203
+ id: fnTarget.name
204
+ });
193
205
  }
194
206
  if (slave === null) {
195
- throw new error_1.default('Slave is null event after pushing opts', { exit: 2 });
207
+ throw new error_1.default('Slave is null event after pushing opts', {
208
+ exit: 2
209
+ });
196
210
  }
197
211
  slave
198
212
  .on('finish', (result) => {
@@ -224,17 +238,19 @@ class HttpFunctions {
224
238
  const fnTargets = runtime_store_1.default.get('context.functions.targets');
225
239
  fnTargets
226
240
  .filter((t) => t.url !== undefined && t.type === constants_1.FN_TYPE.basic)
227
- .map((t) => js_1.JS.omit(t, ['zip_stream', 'watcher', 'localFn']))
241
+ .map((t) => {
242
+ this.targets.push(t);
243
+ return js_1.JS.omit(t, ['zip_stream', 'watcher', 'localFn']);
244
+ })
228
245
  .forEach((t) => {
229
- if (t.stack === constants_1.STACK.java8) {
246
+ var _a;
247
+ if ((_a = t.stack) === null || _a === void 0 ? void 0 : _a.startsWith(runtime_1.default.language.java)) {
230
248
  fn_utils_1.fnUtils.java.ensureJavaInvoker(this.javaInvoker[t.type], path_1.normalize(path_1.join(__dirname, './invoker', t.type, 'java', 'Java' + t.type + 'Invoker.java')));
231
249
  }
232
250
  const pthName = new url_1.URL(t.url).pathname;
233
251
  if (pthName !== null) {
234
252
  this.app.use(pthName, reqHandler(t));
235
- logger_2.labeled(`functions(${t.name})`, 'URL => http://localhost:' +
236
- (masterPort === -1 ? httpPort : masterPort) +
237
- pthName).MESSAGE();
253
+ logUrl(t.name, pthName, httpPort, masterPort);
238
254
  }
239
255
  if (t.url_with_id !== undefined) {
240
256
  const pathWithId = new url_1.URL(t.url_with_id).pathname;
@@ -245,6 +261,26 @@ class HttpFunctions {
245
261
  });
246
262
  return new Promise((res) => {
247
263
  const server = this.app.listen(httpPort, () => {
264
+ this.targets.forEach((target) => {
265
+ var _a, _b;
266
+ (_a = target.watcher) === null || _a === void 0 ? void 0 : _a.on('preparing', () => {
267
+ if (this.repl && !this.repl.paused) {
268
+ this.repl.pause();
269
+ }
270
+ });
271
+ (_b = target.watcher) === null || _b === void 0 ? void 0 : _b.on('compiled', () => {
272
+ logger_2.labeled(`functions(${target.name})`, 'ready!').MESSAGE();
273
+ logUrl(target.name, new url_1.URL(target.url).pathname, httpPort, masterPort);
274
+ if (this.repl && this.repl.paused) {
275
+ this.repl.resume();
276
+ this.repl.showPrompt();
277
+ }
278
+ setTimeout(() => {
279
+ var _a;
280
+ (_a = target.watcher) === null || _a === void 0 ? void 0 : _a.emit('next');
281
+ }, 1000);
282
+ });
283
+ });
248
284
  if (this.repl !== undefined) {
249
285
  this.repl.showPrompt();
250
286
  }
@@ -266,28 +302,22 @@ class HttpFunctions {
266
302
  }
267
303
  stop() {
268
304
  return __awaiter(this, void 0, void 0, function* () {
269
- return new Promise((resolve) => {
305
+ return new Promise((res) => {
270
306
  this.runningSlaves.forEach((slave) => {
271
307
  slave.kill('SIGINT');
272
308
  });
273
- this.q.destroy(() => {
309
+ this.q.destroy(() => __awaiter(this, void 0, void 0, function* () {
310
+ yield Promise.all(this.targets.map((target) => __awaiter(this, void 0, void 0, function* () { var _a; return (_a = target.watcher) === null || _a === void 0 ? void 0 : _a.close(); })));
274
311
  if (HttpFunctions.server !== null) {
275
- if (HttpFunctions.server.pid === undefined) {
276
- try {
277
- HttpFunctions.server.close(() => resolve());
278
- }
279
- catch (e) {
280
- HttpFunctions.server.emit('close');
281
- }
282
- }
283
- else {
284
- HttpFunctions.server.kill('SIGINT');
285
- }
312
+ HttpFunctions.server.close((err) => {
313
+ logger_2.debug(err);
314
+ res();
315
+ });
286
316
  }
287
317
  else {
288
- resolve();
318
+ res();
289
319
  }
290
- });
320
+ }));
291
321
  });
292
322
  });
293
323
  }
@@ -14,7 +14,7 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
14
14
  var __importStar = (this && this.__importStar) || function (mod) {
15
15
  if (mod && mod.__esModule) return mod;
16
16
  var result = {};
17
- if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
18
  __setModuleDefault(result, mod);
19
19
  return result;
20
20
  };