@zappinginc/zm2 6.0.14

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 (133) hide show
  1. package/.claude/settings.local.json +8 -0
  2. package/.gitattributes +4 -0
  3. package/.mocharc.js +14 -0
  4. package/CHANGELOG.md +2416 -0
  5. package/CLAUDE.md +84 -0
  6. package/CONTRIBUTING.md +124 -0
  7. package/GNU-AGPL-3.0.txt +665 -0
  8. package/LICENSE +1 -0
  9. package/README.md +248 -0
  10. package/bin/zm2 +3 -0
  11. package/bin/zm2-dev +3 -0
  12. package/bin/zm2-docker +3 -0
  13. package/bin/zm2-runtime +3 -0
  14. package/bin/zm2-windows +3 -0
  15. package/bin/zm2.ps1 +3 -0
  16. package/bun.lock +421 -0
  17. package/constants.js +114 -0
  18. package/index.js +13 -0
  19. package/lib/API/Configuration.js +212 -0
  20. package/lib/API/Containerizer.js +335 -0
  21. package/lib/API/Dashboard.js +459 -0
  22. package/lib/API/Deploy.js +117 -0
  23. package/lib/API/Extra.js +775 -0
  24. package/lib/API/ExtraMgmt/Docker.js +30 -0
  25. package/lib/API/Log.js +315 -0
  26. package/lib/API/LogManagement.js +371 -0
  27. package/lib/API/Modules/LOCAL.js +122 -0
  28. package/lib/API/Modules/Modularizer.js +148 -0
  29. package/lib/API/Modules/NPM.js +445 -0
  30. package/lib/API/Modules/TAR.js +362 -0
  31. package/lib/API/Modules/flagExt.js +46 -0
  32. package/lib/API/Modules/index.js +120 -0
  33. package/lib/API/Monit.js +247 -0
  34. package/lib/API/Serve.js +343 -0
  35. package/lib/API/Startup.js +629 -0
  36. package/lib/API/UX/helpers.js +213 -0
  37. package/lib/API/UX/index.js +9 -0
  38. package/lib/API/UX/pm2-describe.js +193 -0
  39. package/lib/API/UX/pm2-ls-minimal.js +31 -0
  40. package/lib/API/UX/pm2-ls.js +483 -0
  41. package/lib/API/Version.js +382 -0
  42. package/lib/API/interpreter.json +12 -0
  43. package/lib/API/pm2-plus/PM2IO.js +372 -0
  44. package/lib/API/pm2-plus/auth-strategies/CliAuth.js +288 -0
  45. package/lib/API/pm2-plus/auth-strategies/WebAuth.js +187 -0
  46. package/lib/API/pm2-plus/helpers.js +97 -0
  47. package/lib/API/pm2-plus/link.js +126 -0
  48. package/lib/API/pm2-plus/pres/motd +16 -0
  49. package/lib/API/pm2-plus/pres/motd.update +26 -0
  50. package/lib/API/pm2-plus/pres/welcome +28 -0
  51. package/lib/API/pm2-plus/process-selector.js +52 -0
  52. package/lib/API/schema.json +379 -0
  53. package/lib/API.js +1931 -0
  54. package/lib/Client.js +776 -0
  55. package/lib/Common.js +911 -0
  56. package/lib/Configuration.js +304 -0
  57. package/lib/Daemon.js +456 -0
  58. package/lib/Event.js +37 -0
  59. package/lib/God/ActionMethods.js +909 -0
  60. package/lib/God/ClusterMode.js +97 -0
  61. package/lib/God/ForkMode.js +297 -0
  62. package/lib/God/Methods.js +265 -0
  63. package/lib/God/Reload.js +240 -0
  64. package/lib/God.js +632 -0
  65. package/lib/HttpInterface.js +76 -0
  66. package/lib/ProcessContainer.js +305 -0
  67. package/lib/ProcessContainerBun.js +360 -0
  68. package/lib/ProcessContainerFork.js +42 -0
  69. package/lib/ProcessContainerForkBun.js +33 -0
  70. package/lib/ProcessUtils.js +55 -0
  71. package/lib/TreeKill.js +118 -0
  72. package/lib/Utility.js +430 -0
  73. package/lib/VersionCheck.js +46 -0
  74. package/lib/Watcher.js +117 -0
  75. package/lib/Worker.js +169 -0
  76. package/lib/binaries/CLI.js +1041 -0
  77. package/lib/binaries/DevCLI.js +183 -0
  78. package/lib/binaries/Runtime.js +101 -0
  79. package/lib/binaries/Runtime4Docker.js +192 -0
  80. package/lib/completion.js +229 -0
  81. package/lib/completion.sh +40 -0
  82. package/lib/motd +36 -0
  83. package/lib/templates/Dockerfiles/Dockerfile-java.tpl +7 -0
  84. package/lib/templates/Dockerfiles/Dockerfile-nodejs.tpl +8 -0
  85. package/lib/templates/Dockerfiles/Dockerfile-ruby.tpl +7 -0
  86. package/lib/templates/ecosystem-es.tpl +24 -0
  87. package/lib/templates/ecosystem-simple-es.tpl +8 -0
  88. package/lib/templates/ecosystem-simple.tpl +6 -0
  89. package/lib/templates/ecosystem.tpl +22 -0
  90. package/lib/templates/init-scripts/launchd.tpl +35 -0
  91. package/lib/templates/init-scripts/openrc.tpl +52 -0
  92. package/lib/templates/init-scripts/pm2-init-amazon.sh +86 -0
  93. package/lib/templates/init-scripts/rcd-openbsd.tpl +41 -0
  94. package/lib/templates/init-scripts/rcd.tpl +44 -0
  95. package/lib/templates/init-scripts/smf.tpl +43 -0
  96. package/lib/templates/init-scripts/systemd-online.tpl +22 -0
  97. package/lib/templates/init-scripts/systemd.tpl +22 -0
  98. package/lib/templates/init-scripts/upstart.tpl +103 -0
  99. package/lib/templates/logrotate.d/pm2 +10 -0
  100. package/lib/templates/sample-apps/http-server/README.md +14 -0
  101. package/lib/templates/sample-apps/http-server/api.js +9 -0
  102. package/lib/templates/sample-apps/http-server/ecosystem.config.js +14 -0
  103. package/lib/templates/sample-apps/http-server/package.json +11 -0
  104. package/lib/templates/sample-apps/pm2-plus-metrics-actions/README.md +45 -0
  105. package/lib/templates/sample-apps/pm2-plus-metrics-actions/custom-metrics.js +66 -0
  106. package/lib/templates/sample-apps/pm2-plus-metrics-actions/ecosystem.config.js +12 -0
  107. package/lib/templates/sample-apps/pm2-plus-metrics-actions/package.json +11 -0
  108. package/lib/templates/sample-apps/python-app/README.md +4 -0
  109. package/lib/templates/sample-apps/python-app/echo.py +7 -0
  110. package/lib/templates/sample-apps/python-app/ecosystem.config.js +12 -0
  111. package/lib/templates/sample-apps/python-app/package.json +11 -0
  112. package/lib/tools/Config.js +248 -0
  113. package/lib/tools/IsAbsolute.js +20 -0
  114. package/lib/tools/copydirSync.js +101 -0
  115. package/lib/tools/deleteFolderRecursive.js +19 -0
  116. package/lib/tools/find-package-json.js +74 -0
  117. package/lib/tools/fmt.js +72 -0
  118. package/lib/tools/isbinaryfile.js +94 -0
  119. package/lib/tools/json5.js +752 -0
  120. package/lib/tools/open.js +63 -0
  121. package/lib/tools/passwd.js +58 -0
  122. package/lib/tools/promise.min.js +1 -0
  123. package/lib/tools/sexec.js +55 -0
  124. package/lib/tools/treeify.js +113 -0
  125. package/lib/tools/which.js +120 -0
  126. package/lib/tools/xdg-open +861 -0
  127. package/package.json +219 -0
  128. package/paths.js +93 -0
  129. package/pm2 +11 -0
  130. package/preinstall.js +24 -0
  131. package/run.sh +9 -0
  132. package/types/index.d.ts +722 -0
  133. package/types/tsconfig.json +14 -0
@@ -0,0 +1,305 @@
1
+ /**
2
+ * Copyright 2013-2022 the PM2 project authors. All rights reserved.
3
+ * Use of this source code is governed by a license that
4
+ * can be found in the LICENSE file.
5
+ *
6
+ * This file wrap target application
7
+ * - redirect stdin, stderr to bus + log files
8
+ * - rename process
9
+ * - pid
10
+ */
11
+
12
+ var p = require('path');
13
+ var cst = require('../constants');
14
+ var Utility = require('./Utility.js');
15
+ var ProcessUtils = require('./ProcessUtils');
16
+ var Url = require('url');
17
+
18
+ // Load all env-vars from master.
19
+ var pm2_env = JSON.parse(process.env.pm2_env);
20
+ for(var k in pm2_env) {
21
+ process.env[k] = pm2_env[k];
22
+ }
23
+
24
+ // Rename process
25
+ process.title = process.env.PROCESS_TITLE || 'node ' + pm2_env.pm_exec_path;
26
+
27
+ delete process.env.pm2_env;
28
+
29
+ /**
30
+ * Main entrance to wrap the desired code
31
+ */
32
+ (function ProcessContainer() {
33
+ var fs = require('fs');
34
+
35
+ ProcessUtils.injectModules()
36
+
37
+ var stdFile = pm2_env.pm_log_path;
38
+ var outFile = pm2_env.pm_out_log_path;
39
+ var errFile = pm2_env.pm_err_log_path;
40
+ var pidFile = pm2_env.pm_pid_path;
41
+ var script = pm2_env.pm_exec_path;
42
+
43
+ var original_send = process.send;
44
+
45
+ if (typeof(process.env.source_map_support) != 'undefined' &&
46
+ process.env.source_map_support !== 'false') {
47
+ require('source-map-support').install();
48
+ }
49
+
50
+ process.send = function() {
51
+ if (process.connected)
52
+ original_send.apply(this, arguments);
53
+ };
54
+
55
+ //send node version
56
+ if (process.versions && process.versions.node) {
57
+ process.send({
58
+ 'node_version': process.versions.node
59
+ });
60
+ }
61
+
62
+ if (cst.MODIFY_REQUIRE)
63
+ require.main.filename = pm2_env.pm_exec_path;
64
+
65
+ // Resets global paths for require()
66
+ require('module')._initPaths();
67
+
68
+ try {
69
+ var pid = process.pid
70
+ if (typeof(pid) !== 'undefined')
71
+ fs.writeFileSync(pidFile, process.pid.toString());
72
+ } catch (e) {
73
+ console.error(e.stack || e);
74
+ }
75
+
76
+ // Add args to process if args specified on start
77
+ if (process.env.args != null)
78
+ process.argv = process.argv.concat(pm2_env.args);
79
+
80
+ // stdio, including: out, err and entire (both out and err if necessary).
81
+ var stds = {
82
+ out: outFile,
83
+ err: errFile
84
+ };
85
+ stdFile && (stds.std = stdFile);
86
+
87
+ // uid/gid management
88
+ if (pm2_env.uid || pm2_env.gid) {
89
+ try {
90
+ if (process.env.gid)
91
+ process.setgid(pm2_env.gid);
92
+ if (pm2_env.uid)
93
+ process.setuid(pm2_env.uid);
94
+ } catch(e) {
95
+ setTimeout(function() {
96
+ console.error('%s on call %s', e.message, e.syscall);
97
+ console.error('%s is not accessible', pm2_env.uid);
98
+ return process.exit(1);
99
+ }, 100);
100
+ }
101
+ }
102
+
103
+ exec(script, stds);
104
+ })();
105
+
106
+ /**
107
+ * Description
108
+ * @method exec
109
+ * @param {} script
110
+ * @param {} stds
111
+ * @return
112
+ */
113
+ function exec(script, stds) {
114
+ if (p.extname(script) == '.ts' || p.extname(script) == '.tsx') {
115
+ try {
116
+ require('ts-node/register');
117
+ } catch (e) {
118
+ console.error('Failed to load Typescript interpreter:', e.message || e);
119
+ }
120
+ }
121
+
122
+ process.on('message', function (msg) {
123
+ if (msg.type === 'log:reload') {
124
+ for (var k in stds){
125
+ if (typeof stds[k] == 'object' && !isNaN(stds[k].fd)){
126
+ if (stds[k].destroy) stds[k].destroy();
127
+ else if (stds[k].end) stds[k].end();
128
+ else if (stds[k].close) stds[k].close();
129
+ stds[k] = stds[k]._file;
130
+ }
131
+ }
132
+ Utility.startLogging(stds, pm2_env, function (err) {
133
+ if (err)
134
+ return console.error('Failed to reload logs:', err.stack);
135
+ console.log('Reloading log...');
136
+ });
137
+ }
138
+ });
139
+
140
+ var dayjs = null;
141
+
142
+ if (pm2_env.log_date_format)
143
+ dayjs = require('dayjs');
144
+
145
+ Utility.startLogging(stds, pm2_env, function (err) {
146
+ if (err) {
147
+ process.send({
148
+ type : 'process:exception',
149
+ data : {
150
+ message: err.message,
151
+ syscall: 'ProcessContainer.startLogging'
152
+ }
153
+ });
154
+ throw err;
155
+ return;
156
+ }
157
+
158
+ process.stderr.write = (function(write) {
159
+ return function(string, encoding, cb) {
160
+ var log_data = null;
161
+
162
+ // Disable logs if specified
163
+ if (pm2_env.disable_logs === true) {
164
+ return cb ? cb() : false;
165
+ }
166
+
167
+ if (pm2_env.log_type && pm2_env.log_type === 'json') {
168
+ log_data = JSON.stringify({
169
+ message : string.toString(),
170
+ timestamp : pm2_env.log_date_format && dayjs ?
171
+ dayjs().format(pm2_env.log_date_format) : new Date().toISOString(),
172
+ type : 'err',
173
+ process_id : pm2_env.pm_id,
174
+ app_name : pm2_env.name
175
+ }) + '\n';
176
+ }
177
+ else if (pm2_env.log_date_format && dayjs)
178
+ log_data = `${dayjs().format(pm2_env.log_date_format)}: ${string.toString()}`;
179
+ else
180
+ log_data = string.toString();
181
+
182
+ process.send({
183
+ type : 'log:err',
184
+ topic : 'log:err',
185
+ data : log_data
186
+ });
187
+
188
+ if (Utility.checkPathIsNull(pm2_env.pm_err_log_path) &&
189
+ (!pm2_env.pm_log_path || Utility.checkPathIsNull(pm2_env.pm_log_path)))
190
+ return cb ? cb() : false;
191
+
192
+ stds.std && stds.std.write && stds.std.write(log_data, encoding);
193
+ stds.err && stds.err.write && stds.err.write(log_data, encoding, cb);
194
+ };
195
+ })(process.stderr.write);
196
+
197
+ process.stdout.write = (function(write) {
198
+ return function(string, encoding, cb) {
199
+ var log_data = null;
200
+
201
+ // Disable logs if specified
202
+ if (pm2_env.disable_logs === true) {
203
+ return cb ? cb() : false;
204
+ }
205
+
206
+ if (pm2_env.log_type && pm2_env.log_type === 'json') {
207
+ log_data = JSON.stringify({
208
+ message : string.toString(),
209
+ timestamp : pm2_env.log_date_format && dayjs ?
210
+ dayjs().format(pm2_env.log_date_format) : new Date().toISOString(),
211
+ type : 'out',
212
+ process_id : pm2_env.pm_id,
213
+ app_name : pm2_env.name
214
+ }) + '\n';
215
+ }
216
+ else if (pm2_env.log_date_format && dayjs)
217
+ log_data = `${dayjs().format(pm2_env.log_date_format)}: ${string.toString()}`;
218
+ else
219
+ log_data = string.toString();
220
+
221
+ process.send({
222
+ type : 'log:out',
223
+ data : log_data
224
+ });
225
+
226
+ if (Utility.checkPathIsNull(pm2_env.pm_out_log_path) &&
227
+ (!pm2_env.pm_log_path || Utility.checkPathIsNull(pm2_env.pm_log_path)))
228
+ return cb ? cb() : null;
229
+
230
+ stds.std && stds.std.write && stds.std.write(log_data, encoding);
231
+ stds.out && stds.out.write && stds.out.write(log_data, encoding, cb);
232
+ };
233
+ })(process.stdout.write);
234
+
235
+ function getUncaughtExceptionListener(listener) {
236
+ return function uncaughtListener(err) {
237
+ var error = err && err.stack ? err.stack : err;
238
+
239
+ if (listener === 'unhandledRejection') {
240
+ error = 'You have triggered an unhandledRejection, you may have forgotten to catch a Promise rejection:\n' + error;
241
+ }
242
+
243
+ logError(['std', 'err'], error);
244
+
245
+ // Notify master that an uncaughtException has been catched
246
+ try {
247
+ if (err) {
248
+ var errObj = {};
249
+
250
+ Object.getOwnPropertyNames(err).forEach(function(key) {
251
+ errObj[key] = err[key];
252
+ });
253
+ }
254
+
255
+ process.send({
256
+ type : 'log:err',
257
+ topic : 'log:err',
258
+ data : '\n' + error + '\n'
259
+ });
260
+ process.send({
261
+ type : 'process:exception',
262
+ data : errObj !== undefined ? errObj : {message: 'No error but ' + listener + ' was caught!'}
263
+ });
264
+ } catch(e) {
265
+ logError(['std', 'err'], 'Channel is already closed can\'t broadcast error:\n' + e.stack);
266
+ }
267
+
268
+ if (!process.listeners(listener).filter(function (listener) {
269
+ return listener !== uncaughtListener;
270
+ }).length) {
271
+ if (listener == 'uncaughtException') {
272
+ process.emit('disconnect');
273
+ process.exit(cst.CODE_UNCAUGHTEXCEPTION);
274
+ }
275
+ }
276
+ }
277
+ }
278
+
279
+ process.on('uncaughtException', getUncaughtExceptionListener('uncaughtException'));
280
+ process.on('unhandledRejection', getUncaughtExceptionListener('unhandledRejection'));
281
+
282
+ // Change dir to fix process.cwd
283
+ process.chdir(pm2_env.pm_cwd || process.env.PWD || p.dirname(script));
284
+
285
+ if (ProcessUtils.isESModule(script) === true)
286
+ import(Url.pathToFileURL(process.env.pm_exec_path));
287
+ else {
288
+ if (cst.IS_BUN) {
289
+ require(script);
290
+ }
291
+ else {
292
+ require('module')._load(script, null, true);
293
+ }
294
+ }
295
+
296
+ function logError(types, error){
297
+ try {
298
+ types.forEach(function(type){
299
+ stds[type] && typeof stds[type].write == 'function' && stds[type].write(error + '\n');
300
+ });
301
+ } catch(e) { }
302
+ }
303
+ });
304
+
305
+ }
@@ -0,0 +1,360 @@
1
+ /**
2
+ * Copyright 2013-2022 the PM2 project authors. All rights reserved.
3
+ * Use of this source code is governed by a license that
4
+ * can be found in the LICENSE file.
5
+ */
6
+
7
+ var p = require('path');
8
+ var cst = require('../constants');
9
+ var Utility = require('./Utility.js');
10
+ var Url = require('url');
11
+ var util = require('util')
12
+
13
+ // Load all env-vars from master.
14
+ var pm2_env = JSON.parse(process.env.pm2_env);
15
+ for(var k in pm2_env) {
16
+ process.env[k] = pm2_env[k];
17
+ }
18
+
19
+ // Rename process
20
+ process.title = process.env.PROCESS_TITLE || 'bun ' + pm2_env.pm_exec_path;
21
+
22
+ delete process.env.pm2_env;
23
+
24
+ /**
25
+ * Main entrance to wrap the desired code
26
+ */
27
+ (function ProcessContainer() {
28
+ var fs = require('fs');
29
+
30
+ var stdFile = pm2_env.pm_log_path;
31
+ var outFile = pm2_env.pm_out_log_path;
32
+ var errFile = pm2_env.pm_err_log_path;
33
+ var pidFile = pm2_env.pm_pid_path;
34
+ var script = pm2_env.pm_exec_path;
35
+
36
+ var original_send = process.send;
37
+
38
+ if (typeof(process.env.source_map_support) != 'undefined' &&
39
+ process.env.source_map_support !== 'false') {
40
+ require('source-map-support').install();
41
+ }
42
+
43
+ process.send = function() {
44
+ if (process.connected)
45
+ original_send.apply(this, arguments);
46
+ };
47
+
48
+ //send node version
49
+ if (process.versions && process.versions.node) {
50
+ process.send({
51
+ 'node_version': process.versions.node
52
+ });
53
+ }
54
+
55
+ if (cst.MODIFY_REQUIRE)
56
+ require.main.filename = pm2_env.pm_exec_path;
57
+
58
+ // Resets global paths for require()
59
+ require('module')._initPaths();
60
+
61
+ try {
62
+ var pid = process.pid
63
+ if (typeof(pid) !== 'undefined')
64
+ fs.writeFileSync(pidFile, process.pid.toString());
65
+ } catch (e) {
66
+ console.error(e.stack || e);
67
+ }
68
+
69
+ // Add args to process if args specified on start
70
+ if (process.env.args != null)
71
+ process.argv = process.argv.concat(pm2_env.args);
72
+
73
+ // stdio, including: out, err and entire (both out and err if necessary).
74
+ var stds = {
75
+ out: outFile,
76
+ err: errFile
77
+ };
78
+ stdFile && (stds.std = stdFile);
79
+
80
+ // uid/gid management
81
+ if (pm2_env.uid || pm2_env.gid) {
82
+ try {
83
+ if (process.env.gid)
84
+ process.setgid(pm2_env.gid);
85
+ if (pm2_env.uid)
86
+ process.setuid(pm2_env.uid);
87
+ } catch(e) {
88
+ setTimeout(function() {
89
+ console.error('%s on call %s', e.message, e.syscall);
90
+ console.error('%s is not accessible', pm2_env.uid);
91
+ return process.exit(1);
92
+ }, 100);
93
+ }
94
+ }
95
+
96
+ exec(script, stds);
97
+ })();
98
+
99
+ /**
100
+ * Description
101
+ * @method exec
102
+ * @param {} script
103
+ * @param {} stds
104
+ * @return
105
+ */
106
+ function exec(script, stds) {
107
+ process.on('message', function (msg) {
108
+ if (msg.type === 'log:reload') {
109
+ for (var k in stds){
110
+ if (typeof stds[k] == 'object' && !isNaN(stds[k].fd)){
111
+ if (stds[k].destroy) stds[k].destroy();
112
+ else if (stds[k].end) stds[k].end();
113
+ else if (stds[k].close) stds[k].close();
114
+ stds[k] = stds[k]._file;
115
+ }
116
+ }
117
+ Utility.startLogging(stds, pm2_env, function (err) {
118
+ if (err)
119
+ return console.error('Failed to reload logs:', err.stack);
120
+ console.log('Reloading log...');
121
+ });
122
+ }
123
+ });
124
+
125
+ var dayjs = null;
126
+
127
+ if (pm2_env.log_date_format)
128
+ dayjs = require('dayjs');
129
+
130
+ Utility.startLogging(stds, pm2_env, function (err) {
131
+ if (err) {
132
+ process.send({
133
+ type : 'process:exception',
134
+ data : {
135
+ message: err.message,
136
+ syscall: 'ProcessContainer.startLogging'
137
+ }
138
+ });
139
+ throw err;
140
+ return;
141
+ }
142
+
143
+ const originalConsole = { ...console };
144
+
145
+ ['warn', 'error'].forEach((method) => {
146
+ console[method] = (...args) => {
147
+ let log_data = null;
148
+
149
+ const msg = util.format(...args);
150
+ //const msg = args.map(arg => (typeof arg === 'object' ? JSON.stringify(arg) : String(arg))).join(' ');
151
+
152
+ // Disable logs if specified
153
+ if (pm2_env.disable_logs === true) {
154
+ return cb ? cb() : false;
155
+ }
156
+
157
+ if (pm2_env.log_type && pm2_env.log_type === 'json') {
158
+ log_data = JSON.stringify({
159
+ message : msg,
160
+ timestamp : pm2_env.log_date_format && dayjs ?
161
+ dayjs().format(pm2_env.log_date_format) : new Date().toISOString(),
162
+ type : 'err',
163
+ process_id : pm2_env.pm_id,
164
+ app_name : pm2_env.name
165
+ }) + '\n';
166
+ }
167
+ else if (pm2_env.log_date_format && dayjs)
168
+ log_data = `${dayjs().format(pm2_env.log_date_format)}: ${msg}`;
169
+ else
170
+ log_data = msg.endsWith('\n') ? msg : msg + '\n';
171
+
172
+ // Send the log message to the master process
173
+ process.send({
174
+ type: 'log:err',
175
+ data: log_data,
176
+ });
177
+
178
+ stds.std && stds.std.write && stds.std.write(log_data);
179
+ stds.err && stds.err.write && stds.err.write(log_data);
180
+ };
181
+ });
182
+
183
+ ['log', 'info'].forEach((method) => {
184
+ console[method] = (...args) => {
185
+ let log_data = null;
186
+
187
+ const msg = util.format(...args);
188
+ //const msg = args.map(arg => (typeof arg === 'object' ? JSON.stringify(arg) : String(arg))).join(' ');
189
+
190
+ // Disable logs if specified
191
+ if (pm2_env.disable_logs === true) {
192
+ return cb ? cb() : false;
193
+ }
194
+
195
+ if (pm2_env.log_type && pm2_env.log_type === 'json') {
196
+ log_data = JSON.stringify({
197
+ message : msg,
198
+ timestamp : pm2_env.log_date_format && dayjs ?
199
+ dayjs().format(pm2_env.log_date_format) : new Date().toISOString(),
200
+ type : 'out',
201
+ process_id : pm2_env.pm_id,
202
+ app_name : pm2_env.name
203
+ }) + '\n';
204
+ }
205
+ else if (pm2_env.log_date_format && dayjs)
206
+ log_data = `${dayjs().format(pm2_env.log_date_format)}: ${msg}`;
207
+ else
208
+ log_data = msg.endsWith('\n') ? msg : msg + '\n';
209
+
210
+ // Send the log message to the master process
211
+ process.send({
212
+ type: 'log:out',
213
+ data: log_data,
214
+ });
215
+
216
+ stds.std && stds.std.write && stds.std.write(log_data);
217
+ stds.out && stds.out.write && stds.out.write(log_data);
218
+ };
219
+ });
220
+
221
+ process.stderr.write = (function(write) {
222
+ return function(string, encoding, cb) {
223
+ var log_data = null;
224
+
225
+ // Disable logs if specified
226
+ if (pm2_env.disable_logs === true) {
227
+ return cb ? cb() : false;
228
+ }
229
+
230
+ if (pm2_env.log_type && pm2_env.log_type === 'json') {
231
+ log_data = JSON.stringify({
232
+ message : string.toString(),
233
+ timestamp : pm2_env.log_date_format && dayjs ?
234
+ dayjs().format(pm2_env.log_date_format) : new Date().toISOString(),
235
+ type : 'err',
236
+ process_id : pm2_env.pm_id,
237
+ app_name : pm2_env.name
238
+ }) + '\n';
239
+ }
240
+ else if (pm2_env.log_date_format && dayjs)
241
+ log_data = `${dayjs().format(pm2_env.log_date_format)}: ${string.toString()}`;
242
+ else
243
+ log_data = string.toString();
244
+
245
+ process.send({
246
+ type : 'log:err',
247
+ topic : 'log:err',
248
+ data : log_data
249
+ });
250
+
251
+ if (Utility.checkPathIsNull(pm2_env.pm_err_log_path) &&
252
+ (!pm2_env.pm_log_path || Utility.checkPathIsNull(pm2_env.pm_log_path)))
253
+ return cb ? cb() : false;
254
+
255
+ stds.std && stds.std.write && stds.std.write(log_data, encoding);
256
+ stds.err && stds.err.write && stds.err.write(log_data, encoding, cb);
257
+ };
258
+ })(process.stderr.write);
259
+
260
+ process.stdout.write = (function(write) {
261
+ return function(string, encoding, cb) {
262
+ var log_data = null;
263
+
264
+ // Disable logs if specified
265
+ if (pm2_env.disable_logs === true) {
266
+ return cb ? cb() : false;
267
+ }
268
+
269
+ if (pm2_env.log_type && pm2_env.log_type === 'json') {
270
+ log_data = JSON.stringify({
271
+ message : string.toString(),
272
+ timestamp : pm2_env.log_date_format && dayjs ?
273
+ dayjs().format(pm2_env.log_date_format) : new Date().toISOString(),
274
+ type : 'out',
275
+ process_id : pm2_env.pm_id,
276
+ app_name : pm2_env.name
277
+ }) + '\n';
278
+ }
279
+ else if (pm2_env.log_date_format && dayjs)
280
+ log_data = `${dayjs().format(pm2_env.log_date_format)}: ${string.toString()}`;
281
+ else
282
+ log_data = string.toString();
283
+
284
+ process.send({
285
+ type : 'log:out',
286
+ data : log_data
287
+ });
288
+
289
+ if (Utility.checkPathIsNull(pm2_env.pm_out_log_path) &&
290
+ (!pm2_env.pm_log_path || Utility.checkPathIsNull(pm2_env.pm_log_path)))
291
+ return cb ? cb() : null;
292
+
293
+ stds.std && stds.std.write && stds.std.write(log_data, encoding);
294
+ stds.out && stds.out.write && stds.out.write(log_data, encoding, cb);
295
+ };
296
+ })(process.stdout.write);
297
+
298
+ function getUncaughtExceptionListener(listener) {
299
+ return function uncaughtListener(err) {
300
+ var error = err && err.stack ? err.stack : err;
301
+
302
+ if (listener === 'unhandledRejection') {
303
+ error = 'You have triggered an unhandledRejection, you may have forgotten to catch a Promise rejection:\n' + error;
304
+ }
305
+
306
+ logError(['std', 'err'], error);
307
+
308
+ // Notify master that an uncaughtException has been catched
309
+ try {
310
+ if (err) {
311
+ var errObj = {};
312
+
313
+ Object.getOwnPropertyNames(err).forEach(function(key) {
314
+ errObj[key] = err[key];
315
+ });
316
+ }
317
+
318
+ process.send({
319
+ type : 'log:err',
320
+ topic : 'log:err',
321
+ data : '\n' + error + '\n'
322
+ });
323
+
324
+ process.send({
325
+ type : 'process:exception',
326
+ data : errObj !== undefined ? errObj : {message: 'No error but ' + listener + ' was caught!'}
327
+ });
328
+ } catch(e) {
329
+ logError(['std', 'err'], 'Channel is already closed can\'t broadcast error:\n' + e.stack);
330
+ }
331
+
332
+ if (!process.listeners(listener).filter(function (listener) {
333
+ return listener !== uncaughtListener;
334
+ }).length) {
335
+ if (listener == 'uncaughtException') {
336
+ process.emit('disconnect');
337
+ process.exit(cst.CODE_UNCAUGHTEXCEPTION);
338
+ }
339
+ }
340
+ }
341
+ }
342
+
343
+ process.on('uncaughtException', getUncaughtExceptionListener('uncaughtException'));
344
+ process.on('unhandledRejection', getUncaughtExceptionListener('unhandledRejection'));
345
+
346
+ // Change dir to fix process.cwd
347
+ process.chdir(pm2_env.pm_cwd || process.env.PWD || p.dirname(script));
348
+
349
+ require(script);
350
+
351
+ function logError(types, error){
352
+ try {
353
+ types.forEach(function(type){
354
+ stds[type] && typeof stds[type].write == 'function' && stds[type].write(error + '\n');
355
+ });
356
+ } catch(e) { }
357
+ }
358
+ });
359
+
360
+ }
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Copyright 2013-2022 the PM2 project authors. All rights reserved.
3
+ * Use of this source code is governed by a license that
4
+ * can be found in the LICENSE file.
5
+ */
6
+ var url = require('url');
7
+ // Inject custom modules
8
+ var ProcessUtils = require('./ProcessUtils')
9
+ ProcessUtils.injectModules()
10
+
11
+ if (typeof(process.env.source_map_support) != "undefined" &&
12
+ process.env.source_map_support !== "false") {
13
+ require('source-map-support').install();
14
+ }
15
+
16
+ // Rename the process
17
+ process.title = process.env.PROCESS_TITLE || 'node ' + process.env.pm_exec_path;
18
+
19
+ if (process.connected &&
20
+ process.send &&
21
+ process.versions &&
22
+ process.versions.node)
23
+ process.send({
24
+ 'node_version': process.versions.node
25
+ });
26
+
27
+ // Require the real application
28
+ if (process.env.pm_exec_path) {
29
+ if (ProcessUtils.isESModule(process.env.pm_exec_path) === true) {
30
+ import(url.pathToFileURL(process.env.pm_exec_path));
31
+ }
32
+ else
33
+ require('module')._load(process.env.pm_exec_path, null, true);
34
+ }
35
+ else
36
+ throw new Error('Could not _load() the script');
37
+
38
+ // Change some values to make node think that the user's application
39
+ // was started directly such as `node app.js`
40
+ process.mainModule = process.mainModule || {};
41
+ process.mainModule.loaded = false;
42
+ require.main = process.mainModule;