@ohos-ports/pm2 0.0.0-stage → 7.1.2-beta.0

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 (267) hide show
  1. package/CHANGELOG.md +2607 -0
  2. package/CONTRIBUTING.md +124 -0
  3. package/GNU-AGPL-3.0.txt +665 -0
  4. package/LICENSE +1 -0
  5. package/README.md +255 -2
  6. package/SECURITY.md +41 -0
  7. package/bin/pm2 +3 -0
  8. package/bin/pm2-dev +3 -0
  9. package/bin/pm2-docker +3 -0
  10. package/bin/pm2-runtime +3 -0
  11. package/bin/pm2.ps1 +3 -0
  12. package/constants.js +113 -0
  13. package/index.js +12 -0
  14. package/lib/API/Configuration.js +212 -0
  15. package/lib/API/Containerizer.js +335 -0
  16. package/lib/API/Dashboard.js +726 -0
  17. package/lib/API/Deploy.js +117 -0
  18. package/lib/API/Extra.js +798 -0
  19. package/lib/API/ExtraMgmt/Docker.js +30 -0
  20. package/lib/API/Log.js +388 -0
  21. package/lib/API/LogManagement.js +351 -0
  22. package/lib/API/Modules/LOCAL.js +122 -0
  23. package/lib/API/Modules/Modularizer.js +148 -0
  24. package/lib/API/Modules/NPM.js +443 -0
  25. package/lib/API/Modules/TAR.js +368 -0
  26. package/lib/API/Modules/flagExt.js +46 -0
  27. package/lib/API/Modules/index.js +120 -0
  28. package/lib/API/Monit.js +247 -0
  29. package/lib/API/Serve.js +497 -0
  30. package/lib/API/Startup.js +630 -0
  31. package/lib/API/UX/helpers.js +213 -0
  32. package/lib/API/UX/index.js +9 -0
  33. package/lib/API/UX/pm2-describe.js +197 -0
  34. package/lib/API/UX/pm2-ls-minimal.js +31 -0
  35. package/lib/API/UX/pm2-ls.js +581 -0
  36. package/lib/API/Version.js +383 -0
  37. package/lib/API/interpreter.json +12 -0
  38. package/lib/API/pm2-plus/PM2IO.js +381 -0
  39. package/lib/API/pm2-plus/auth-strategies/CliAuth.js +306 -0
  40. package/lib/API/pm2-plus/auth-strategies/WebAuth.js +195 -0
  41. package/lib/API/pm2-plus/helpers.js +97 -0
  42. package/lib/API/pm2-plus/link.js +126 -0
  43. package/lib/API/pm2-plus/pres/motd +16 -0
  44. package/lib/API/pm2-plus/pres/motd.update +26 -0
  45. package/lib/API/pm2-plus/pres/welcome +28 -0
  46. package/lib/API/pm2-plus/process-selector.js +52 -0
  47. package/lib/API/schema.json +375 -0
  48. package/lib/API.js +1980 -0
  49. package/lib/Client.js +793 -0
  50. package/lib/Common.js +949 -0
  51. package/lib/Configuration.js +309 -0
  52. package/lib/Daemon.js +468 -0
  53. package/lib/Event.js +37 -0
  54. package/lib/God/ActionMethods.js +955 -0
  55. package/lib/God/ClusterMode.js +192 -0
  56. package/lib/God/ForkMode.js +205 -0
  57. package/lib/God/Methods.js +292 -0
  58. package/lib/God/Reload.js +300 -0
  59. package/lib/God/StdioLogger.js +239 -0
  60. package/lib/God.js +653 -0
  61. package/lib/HttpInterface.js +75 -0
  62. package/lib/OtelManager.js +74 -0
  63. package/lib/ProcessContainer.js +321 -0
  64. package/lib/ProcessContainerBun.js +355 -0
  65. package/lib/ProcessContainerFork.js +42 -0
  66. package/lib/ProcessContainerForkBun.js +31 -0
  67. package/lib/ProcessUtils.js +74 -0
  68. package/lib/TreeKill.js +111 -0
  69. package/lib/Utility.js +304 -0
  70. package/lib/VersionCheck.js +46 -0
  71. package/lib/Watcher.js +117 -0
  72. package/lib/Worker.js +200 -0
  73. package/lib/binaries/CLI.js +1092 -0
  74. package/lib/binaries/DevCLI.js +183 -0
  75. package/lib/binaries/Runtime.js +101 -0
  76. package/lib/binaries/Runtime4Docker.js +192 -0
  77. package/lib/completion.js +229 -0
  78. package/lib/completion.sh +40 -0
  79. package/lib/motd +36 -0
  80. package/lib/templates/Dockerfiles/Dockerfile-nodejs.tpl +8 -0
  81. package/lib/templates/ecosystem-es.tpl +24 -0
  82. package/lib/templates/ecosystem-simple-es.tpl +8 -0
  83. package/lib/templates/ecosystem-simple.tpl +6 -0
  84. package/lib/templates/ecosystem.tpl +22 -0
  85. package/lib/templates/init-scripts/launchd.tpl +35 -0
  86. package/lib/templates/init-scripts/openrc.tpl +52 -0
  87. package/lib/templates/init-scripts/pm2-init-amazon.sh +86 -0
  88. package/lib/templates/init-scripts/rcd-openbsd.tpl +41 -0
  89. package/lib/templates/init-scripts/rcd.tpl +44 -0
  90. package/lib/templates/init-scripts/smf.tpl +43 -0
  91. package/lib/templates/init-scripts/systemd-online.tpl +22 -0
  92. package/lib/templates/init-scripts/systemd.tpl +22 -0
  93. package/lib/templates/init-scripts/upstart.tpl +103 -0
  94. package/lib/templates/logrotate.d/pm2 +10 -0
  95. package/lib/templates/sample-apps/http-server/README.md +14 -0
  96. package/lib/templates/sample-apps/http-server/api.js +9 -0
  97. package/lib/templates/sample-apps/http-server/ecosystem.config.js +14 -0
  98. package/lib/templates/sample-apps/http-server/package.json +11 -0
  99. package/lib/templates/sample-apps/pm2-plus-metrics-actions/README.md +45 -0
  100. package/lib/templates/sample-apps/pm2-plus-metrics-actions/custom-metrics.js +66 -0
  101. package/lib/templates/sample-apps/pm2-plus-metrics-actions/ecosystem.config.js +12 -0
  102. package/lib/templates/sample-apps/pm2-plus-metrics-actions/package.json +11 -0
  103. package/lib/templates/sample-apps/python-app/README.md +4 -0
  104. package/lib/templates/sample-apps/python-app/echo.py +7 -0
  105. package/lib/templates/sample-apps/python-app/ecosystem.config.js +12 -0
  106. package/lib/templates/sample-apps/python-app/package.json +11 -0
  107. package/lib/tools/Config.js +251 -0
  108. package/lib/tools/SysMetrics.js +375 -0
  109. package/lib/tools/copydirSync.js +101 -0
  110. package/lib/tools/deleteFolderRecursive.js +19 -0
  111. package/lib/tools/find-package-json.js +74 -0
  112. package/lib/tools/fmt.js +72 -0
  113. package/lib/tools/interpreter.js +88 -0
  114. package/lib/tools/isbinaryfile.js +94 -0
  115. package/lib/tools/json5.js +752 -0
  116. package/lib/tools/multimeter/bar.js +111 -0
  117. package/lib/tools/multimeter/charm/encode.js +18 -0
  118. package/lib/tools/multimeter/charm/index.js +305 -0
  119. package/lib/tools/multimeter/index.js +76 -0
  120. package/lib/tools/open.js +56 -0
  121. package/lib/tools/passwd.js +58 -0
  122. package/lib/tools/pm2-config-keys.js +165 -0
  123. package/lib/tools/prompt.js +76 -0
  124. package/lib/tools/serviceStop.js +149 -0
  125. package/lib/tools/sexec.js +55 -0
  126. package/lib/tools/treeify.js +113 -0
  127. package/lib/tools/typestrip.js +55 -0
  128. package/lib/tools/which.js +120 -0
  129. package/lib/tools/xdg-open +861 -0
  130. package/modules/blessed/LICENSE +20 -0
  131. package/modules/blessed/README.md +34 -0
  132. package/modules/blessed/index.js +1 -0
  133. package/modules/blessed/lib/alias.js +526 -0
  134. package/modules/blessed/lib/blessed.js +32 -0
  135. package/modules/blessed/lib/colors.js +530 -0
  136. package/modules/blessed/lib/events.js +189 -0
  137. package/modules/blessed/lib/gpmclient.js +221 -0
  138. package/modules/blessed/lib/helpers.js +165 -0
  139. package/modules/blessed/lib/keys.js +339 -0
  140. package/modules/blessed/lib/program.js +4297 -0
  141. package/modules/blessed/lib/tput.js +3022 -0
  142. package/modules/blessed/lib/unicode.js +885 -0
  143. package/modules/blessed/lib/widget.js +32 -0
  144. package/modules/blessed/lib/widgets/box.js +34 -0
  145. package/modules/blessed/lib/widgets/element.js +2570 -0
  146. package/modules/blessed/lib/widgets/list.js +599 -0
  147. package/modules/blessed/lib/widgets/log.js +83 -0
  148. package/modules/blessed/lib/widgets/node.js +282 -0
  149. package/modules/blessed/lib/widgets/screen.js +2298 -0
  150. package/modules/blessed/lib/widgets/scrollablebox.js +389 -0
  151. package/modules/blessed/lib/widgets/scrollabletext.js +35 -0
  152. package/modules/blessed/lib/widgets/text.js +35 -0
  153. package/modules/blessed/usr/linux +0 -0
  154. package/modules/blessed/usr/windows-ansi +0 -0
  155. package/modules/blessed/usr/xterm +0 -0
  156. package/modules/blessed/usr/xterm-256color +0 -0
  157. package/modules/blessed/usr/xterm.termcap +243 -0
  158. package/modules/chokidar/LICENSE +21 -0
  159. package/modules/chokidar/VENDORED.md +76 -0
  160. package/modules/chokidar/index.js +979 -0
  161. package/modules/chokidar/lib/brace-expand.js +180 -0
  162. package/modules/chokidar/lib/constants.js +66 -0
  163. package/modules/chokidar/lib/fsevents-handler.js +526 -0
  164. package/modules/chokidar/lib/nodefs-handler.js +654 -0
  165. package/modules/fclone.js +49 -0
  166. package/modules/pm2-axon/index.js +2 -0
  167. package/modules/pm2-axon/lib/configurable/History.md +5 -0
  168. package/modules/pm2-axon/lib/configurable/Makefile +7 -0
  169. package/modules/pm2-axon/lib/configurable/Readme.md +57 -0
  170. package/modules/pm2-axon/lib/configurable/index.js +2 -0
  171. package/modules/pm2-axon/lib/configurable/lib/configurable.js +47 -0
  172. package/modules/pm2-axon/lib/configurable/package.json +68 -0
  173. package/modules/pm2-axon/lib/index.js +44 -0
  174. package/modules/pm2-axon/lib/plugins/queue.js +71 -0
  175. package/modules/pm2-axon/lib/plugins/round-robin.js +55 -0
  176. package/modules/pm2-axon/lib/sockets/pub-emitter.js +26 -0
  177. package/modules/pm2-axon/lib/sockets/pub.js +77 -0
  178. package/modules/pm2-axon/lib/sockets/pull.js +37 -0
  179. package/modules/pm2-axon/lib/sockets/push.js +32 -0
  180. package/modules/pm2-axon/lib/sockets/rep.js +73 -0
  181. package/modules/pm2-axon/lib/sockets/req.js +101 -0
  182. package/modules/pm2-axon/lib/sockets/sock.js +430 -0
  183. package/modules/pm2-axon/lib/sockets/sub-emitter.js +92 -0
  184. package/modules/pm2-axon/lib/sockets/sub.js +147 -0
  185. package/modules/pm2-axon/lib/utils.js +19 -0
  186. package/modules/pm2-axon-rpc/index.js +3 -0
  187. package/modules/pm2-axon-rpc/lib/client.js +63 -0
  188. package/modules/pm2-axon-rpc/lib/server.js +139 -0
  189. package/modules/pm2-io-agent/LICENSE +661 -0
  190. package/modules/pm2-io-agent/README.md +62 -0
  191. package/modules/pm2-io-agent/config.js +30 -0
  192. package/modules/pm2-io-agent/constants.js +118 -0
  193. package/modules/pm2-io-agent/index.js +2 -0
  194. package/modules/pm2-io-agent/src/InteractorClient.js +513 -0
  195. package/modules/pm2-io-agent/src/InteractorDaemon.js +455 -0
  196. package/modules/pm2-io-agent/src/PM2Client.js +112 -0
  197. package/modules/pm2-io-agent/src/PM2Interface.js +180 -0
  198. package/modules/pm2-io-agent/src/TransporterInterface.js +173 -0
  199. package/modules/pm2-io-agent/src/Utility.js +392 -0
  200. package/modules/pm2-io-agent/src/WatchDog.js +70 -0
  201. package/modules/pm2-io-agent/src/push/DataRetriever.js +77 -0
  202. package/modules/pm2-io-agent/src/push/PushInteractor.js +206 -0
  203. package/modules/pm2-io-agent/src/push/TransactionAggregator.js +670 -0
  204. package/modules/pm2-io-agent/src/reverse/ReverseInteractor.js +128 -0
  205. package/modules/pm2-io-agent/src/transporters/Transporter.js +129 -0
  206. package/modules/pm2-io-agent/src/transporters/WebsocketTransport.js +202 -0
  207. package/modules/pm2-io-agent/src/utils/BinaryHeap.js +135 -0
  208. package/modules/pm2-io-agent/src/utils/EDS.js +111 -0
  209. package/modules/pm2-io-agent/src/utils/probes/Histogram.js +204 -0
  210. package/modules/pm2-io-agent/src/utils/units.js +10 -0
  211. package/modules/pm2-io-bpm/configuration.js +141 -0
  212. package/modules/pm2-io-bpm/constants.js +10 -0
  213. package/modules/pm2-io-bpm/featureManager.js +110 -0
  214. package/modules/pm2-io-bpm/features/dependencies.js +48 -0
  215. package/modules/pm2-io-bpm/features/events.js +50 -0
  216. package/modules/pm2-io-bpm/features/metrics.js +102 -0
  217. package/modules/pm2-io-bpm/features/notify.js +238 -0
  218. package/modules/pm2-io-bpm/features/profiling.js +70 -0
  219. package/modules/pm2-io-bpm/features/tracing.js +174 -0
  220. package/modules/pm2-io-bpm/index.js +11 -0
  221. package/modules/pm2-io-bpm/metrics/eventLoopMetrics.js +136 -0
  222. package/modules/pm2-io-bpm/metrics/httpMetrics.js +175 -0
  223. package/modules/pm2-io-bpm/metrics/network.js +132 -0
  224. package/modules/pm2-io-bpm/metrics/runtime.js +161 -0
  225. package/modules/pm2-io-bpm/metrics/v8.js +104 -0
  226. package/modules/pm2-io-bpm/otel/constants.js +7 -0
  227. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/platform/index.js +17 -0
  228. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/platform/node/index.js +17 -0
  229. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/platform/node/util.js +51 -0
  230. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/transform.js +137 -0
  231. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/types.js +24 -0
  232. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/utils.js +23 -0
  233. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/zipkin.js +149 -0
  234. package/modules/pm2-io-bpm/pmx.js +380 -0
  235. package/modules/pm2-io-bpm/profilers/addonProfiler.js +193 -0
  236. package/modules/pm2-io-bpm/profilers/inspectorProfiler.js +302 -0
  237. package/modules/pm2-io-bpm/serviceManager.js +21 -0
  238. package/modules/pm2-io-bpm/services/actions.js +159 -0
  239. package/modules/pm2-io-bpm/services/inspector.js +43 -0
  240. package/modules/pm2-io-bpm/services/metrics.js +186 -0
  241. package/modules/pm2-io-bpm/services/runtimeStats.js +54 -0
  242. package/modules/pm2-io-bpm/services/transport.js +11 -0
  243. package/modules/pm2-io-bpm/transports/IPCTransport.js +115 -0
  244. package/modules/pm2-io-bpm/utils/BinaryHeap.js +134 -0
  245. package/modules/pm2-io-bpm/utils/EDS.js +113 -0
  246. package/modules/pm2-io-bpm/utils/EWMA.js +32 -0
  247. package/modules/pm2-io-bpm/utils/autocast.js +61 -0
  248. package/modules/pm2-io-bpm/utils/metrics/counter.js +33 -0
  249. package/modules/pm2-io-bpm/utils/metrics/gauge.js +23 -0
  250. package/modules/pm2-io-bpm/utils/metrics/histogram.js +192 -0
  251. package/modules/pm2-io-bpm/utils/metrics/meter.js +46 -0
  252. package/modules/pm2-io-bpm/utils/miscellaneous.js +19 -0
  253. package/modules/pm2-io-bpm/utils/module.js +67 -0
  254. package/modules/pm2-io-bpm/utils/shimmer.js +19 -0
  255. package/modules/pm2-io-bpm/utils/stackParser.js +160 -0
  256. package/modules/pm2-io-bpm/utils/transactionAggregator.js +429 -0
  257. package/modules/pm2-io-bpm/utils/units.js +16 -0
  258. package/modules/vizion/LICENSE +13 -0
  259. package/modules/vizion/README.md +133 -0
  260. package/modules/vizion/index.js +1 -0
  261. package/modules/vizion/lib/git/git.js +247 -0
  262. package/modules/vizion/lib/vizion.js +52 -0
  263. package/package.json +170 -4
  264. package/paths.js +121 -0
  265. package/pm2 +11 -0
  266. package/types/index.d.ts +732 -0
  267. package/types/tsconfig.json +14 -0
@@ -0,0 +1,392 @@
1
+ 'use strict'
2
+
3
+ const os = require('os')
4
+ const crypto = require('crypto')
5
+ const dayjs = require('dayjs')
6
+ const fclone = require('../../fclone')
7
+
8
+ const interfaceType = {
9
+ v4: {
10
+ default: '127.0.0.1',
11
+ family: 'IPv4'
12
+ },
13
+ v6: {
14
+ default: '::1',
15
+ family: 'IPv6'
16
+ }
17
+ }
18
+
19
+ /**
20
+ * Search for public network adress
21
+ * @param {String} type the type of network interface, can be either 'v4' or 'v6'
22
+ */
23
+ const retrieveAddress = (type) => {
24
+ let interfce = interfaceType[type]
25
+ let ret = interfce.default
26
+ let interfaces = os.networkInterfaces()
27
+
28
+ Object.keys(interfaces).forEach(function (el) {
29
+ interfaces[el].forEach(function (el2) {
30
+ if (!el2.internal && el2.family === interfce.family) {
31
+ ret = el2.address
32
+ }
33
+ })
34
+ })
35
+ return ret
36
+ }
37
+
38
+ /**
39
+ * Simple cache implementation
40
+ *
41
+ * @param {Object} opts cache options
42
+ * @param {Function} opts.miss function called when a key isn't found in the cache
43
+ */
44
+ class Cache {
45
+ constructor (opts) {
46
+ this._cache = {}
47
+ this._miss = opts.miss
48
+ this._ttl_time = opts.ttl
49
+ this._ttl = {}
50
+
51
+ if (opts.ttl) {
52
+ this._worker = setInterval(this.worker.bind(this), 1000)
53
+ }
54
+ }
55
+
56
+ worker () {
57
+ let keys = Object.keys(this._ttl)
58
+ for (let i = 0; i < keys.length; i++) {
59
+ let key = keys[i]
60
+ let value = this._ttl[key]
61
+ if (dayjs().isAfter(value)) {
62
+ delete this._cache[key]
63
+ delete this._ttl[key]
64
+ }
65
+ }
66
+ }
67
+
68
+ /**
69
+ * Get a value from the cache
70
+ *
71
+ * @param {String} key
72
+ */
73
+ get (key) {
74
+ if (!key) return null
75
+ let value = this._cache[key]
76
+ if (value) return value
77
+
78
+ value = this._miss(key)
79
+
80
+ if (value) {
81
+ this.set(key, value)
82
+ }
83
+ return value
84
+ }
85
+
86
+ /**
87
+ * Set a value in the cache
88
+ *
89
+ * @param {String} key
90
+ * @param {Mixed} value
91
+ */
92
+ set (key, value) {
93
+ if (!key || !value) return false
94
+ this._cache[key] = value
95
+ if (this._ttl_time) {
96
+ this._ttl[key] = dayjs().add(this._ttl_time, 'seconds')
97
+ }
98
+ return true
99
+ }
100
+
101
+ reset () {
102
+ this._cache = null
103
+ this._cache = {}
104
+ this._ttl = null
105
+ this._ttl = {}
106
+ }
107
+ }
108
+
109
+ /**
110
+ * StackTraceParser is used to parse callsite from stacktrace
111
+ * and get from FS the context of the error (if available)
112
+ *
113
+ * @param {Cache} cache cache implementation used to query file from FS and get context
114
+ */
115
+ class StackTraceParser {
116
+ constructor (opts) {
117
+ this._cache = opts.cache
118
+ this._context_size = opts.context
119
+ }
120
+
121
+ isAbsolute (path) {
122
+ if (process.platform === 'win32') {
123
+ // https://github.com/nodejs/node/blob/b3fcc245fb25539909ef1d5eaa01dbf92e168633/lib/path.js#L56
124
+ let splitDeviceRe = /^([a-zA-Z]:|[\\/]{2}[^\\/]+[\\/]+[^\\/]+)?([\\/])?([\s\S]*?)$/
125
+ let result = splitDeviceRe.exec(path)
126
+ let device = result[1] || ''
127
+ let isUnc = Boolean(device && device.charAt(1) !== ':')
128
+ // UNC paths are always absolute
129
+ return Boolean(result[2] || isUnc)
130
+ } else {
131
+ return path.charAt(0) === '/'
132
+ }
133
+ }
134
+
135
+ parse (stack) {
136
+ if (!stack || stack.length === 0) return false
137
+
138
+ for (var i = 0, len = stack.length; i < len; i++) {
139
+ var callsite = stack[i]
140
+
141
+ // avoid null values
142
+ if (typeof callsite !== 'object') continue
143
+ if (!callsite.file_name || !callsite.line_number) continue
144
+
145
+ var type = this.isAbsolute(callsite.file_name) || callsite.file_name[0] === '.' ? 'user' : 'core'
146
+
147
+ // only use the callsite if its inside user space
148
+ if (!callsite || type === 'core' || callsite.file_name.indexOf('node_modules') > -1 ||
149
+ callsite.file_name.indexOf('vxx') > -1) {
150
+ continue
151
+ }
152
+
153
+ // get the whole context (all lines) and cache them if necessary
154
+ var context = this._cache.get(callsite.file_name)
155
+ var source = []
156
+ if (context && context.length > 0) {
157
+ // get line before the call
158
+ var preLine = callsite.line_number - this._context_size - 1
159
+ var pre = context.slice(preLine > 0 ? preLine : 0, callsite.line_number - 1)
160
+ if (pre && pre.length > 0) {
161
+ pre.forEach(function (line) {
162
+ source.push(line.replace(/\t/g, ' '))
163
+ })
164
+ }
165
+ // get the line where the call has been made
166
+ if (context[callsite.line_number - 1]) {
167
+ source.push(context[callsite.line_number - 1].replace(/\t/g, ' ').replace(' ', '>>'))
168
+ }
169
+ // and get the line after the call
170
+ var postLine = callsite.line_number + this._context_size
171
+ var post = context.slice(callsite.line_number, postLine)
172
+ if (post && post.length > 0) {
173
+ post.forEach(function (line) {
174
+ source.push(line.replace(/\t/g, ' '))
175
+ })
176
+ }
177
+ }
178
+ return {
179
+ context: source.length > 0 ? source.join('\n') : 'cannot retrieve source context',
180
+ callsite: [ callsite.file_name, callsite.line_number ].join(':')
181
+ }
182
+ }
183
+ return false
184
+ }
185
+
186
+ attachContext (error) {
187
+ if (!error) return error
188
+
189
+ // if pmx attached callsites we can parse them to retrieve the context
190
+ if (typeof (error.stackframes) === 'object') {
191
+ let result = this.parse(error.stackframes)
192
+ // no need to send it since there is already the stacktrace
193
+ delete error.stackframes
194
+ delete error.__error_callsites
195
+
196
+ if (result) {
197
+ error.callsite = result.callsite
198
+ error.context = result.context
199
+ }
200
+ }
201
+ // if the stack is here we can parse it directly from the stack string
202
+ // only if the context has been retrieved from elsewhere
203
+ if (typeof error.stack === 'string' && !error.callsite) {
204
+ let siteRegex = /(\/[^\\\n]*)/g
205
+ let tmp
206
+ let stack = []
207
+
208
+ // find matching groups
209
+ while ((tmp = siteRegex.exec(error.stack))) {
210
+ stack.push(tmp[1])
211
+ }
212
+
213
+ // parse each callsite to match the format used by the stackParser
214
+ stack = stack.map((callsite) => {
215
+ // remove the trailing ) if present
216
+ if (callsite[callsite.length - 1] === ')') {
217
+ callsite = callsite.substr(0, callsite.length - 1)
218
+ }
219
+ let location = callsite.split(':')
220
+
221
+ return location.length < 3 ? callsite : {
222
+ file_name: location[0],
223
+ line_number: parseInt(location[1])
224
+ }
225
+ })
226
+
227
+ let finalCallsite = this.parse(stack)
228
+ if (finalCallsite) {
229
+ error.callsite = finalCallsite.callsite
230
+ error.context = finalCallsite.context
231
+ }
232
+ }
233
+ return error
234
+ }
235
+ }
236
+
237
+ // EWMA = ExponentiallyWeightedMovingAverage from
238
+ // https://github.com/felixge/node-measured/blob/master/lib/util/ExponentiallyMovingWeightedAverage.js
239
+ // Copyright Felix Geisendörfer <felix@debuggable.com> under MIT license
240
+ class EWMA {
241
+ constructor () {
242
+ this._timePeriod = 60000
243
+ this._tickInterval = 5000
244
+ this._alpha = 1 - Math.exp(-this._tickInterval / this._timePeriod)
245
+ this._count = 0
246
+ this._rate = 0
247
+ this._interval = setInterval(_ => {
248
+ this.tick()
249
+ }, this._tickInterval)
250
+ this._interval.unref()
251
+ }
252
+
253
+ update (n) {
254
+ this._count += n || 1
255
+ }
256
+
257
+ tick () {
258
+ let instantRate = this._count / this._tickInterval
259
+ this._count = 0
260
+ this._rate += (this._alpha * (instantRate - this._rate))
261
+ }
262
+
263
+ rate (timeUnit) {
264
+ return (this._rate || 0) * timeUnit
265
+ }
266
+ }
267
+
268
+ class Cipher {
269
+ static get CIPHER_ALGORITHM () {
270
+ return 'aes256'
271
+ }
272
+
273
+ /**
274
+ * Decipher data using 256 bits key (AES)
275
+ * @param {Hex} data input data
276
+ * @param {String} key 256 bits key
277
+ * @return {Object} deciphered data parsed as json object
278
+ */
279
+ static decipherMessage (msg, key) {
280
+ try {
281
+ let decipher = crypto.createDecipher(Cipher.CIPHER_ALGORITHM, key)
282
+ let decipheredMessage = decipher.update(msg, 'hex', 'utf8')
283
+ decipheredMessage += decipher.final('utf8')
284
+ return JSON.parse(decipheredMessage)
285
+ } catch (err) {
286
+ console.error(err)
287
+ return null
288
+ }
289
+ }
290
+
291
+ /**
292
+ * Cipher data using 256 bits key (AES)
293
+ * @param {String} data input data
294
+ * @param {String} key 256 bits key
295
+ * @return {Hex} ciphered data
296
+ */
297
+ static cipherMessage (data, key) {
298
+ try {
299
+ // stringify if not already done (fail safe)
300
+ if (typeof data !== 'string') {
301
+ data = JSON.stringify(data)
302
+ }
303
+
304
+ let cipher = crypto.createCipher(Cipher.CIPHER_ALGORITHM, key)
305
+ let cipheredData = cipher.update(data, 'utf8', 'hex')
306
+ cipheredData += cipher.final('hex')
307
+ return cipheredData
308
+ } catch (err) {
309
+ console.error(err)
310
+ }
311
+ }
312
+ }
313
+
314
+ /**
315
+ * HTTP wrapper
316
+ */
317
+ class HTTPClient {
318
+ /**
319
+ * Return native module (HTTP/HTTPS)
320
+ * @param {String} url
321
+ */
322
+ getModule (url) {
323
+ return url.match(/https:\/\//) ? require('https') : require('http')
324
+ }
325
+ /**
326
+ * Send an HTTP request and return data or error if status > 200
327
+ * @param {Object} opts
328
+ * @param {String} opts.url
329
+ * @param {String} opts.method
330
+ * @param {Object} [opts.data]
331
+ * @param {Object} [opts.headers]
332
+ * @param {Function} cb invoked with <err, body>
333
+ */
334
+ open (opts, cb) {
335
+ const http = this.getModule(opts.url)
336
+ const parsedUrl = new URL(opts.url)
337
+ let data = null
338
+ const options = {
339
+ hostname: parsedUrl.hostname,
340
+ path: parsedUrl.pathname + parsedUrl.search,
341
+ port: parsedUrl.port,
342
+ method: opts.method,
343
+ headers: opts.headers
344
+ }
345
+
346
+ if (opts.data) {
347
+ data = JSON.stringify(opts.data)
348
+ options.headers = Object.assign({
349
+ 'Content-Type': 'application/json',
350
+ 'Content-Length': data.length
351
+ }, opts.headers)
352
+ }
353
+
354
+ const req = http.request(options, (res) => {
355
+ let body = ''
356
+
357
+ res.setEncoding('utf8')
358
+ res.on('data', (chunk) => {
359
+ body += chunk.toString()
360
+ })
361
+ res.on('end', () => {
362
+ try {
363
+ let jsonData = JSON.parse(body)
364
+ return cb(null, jsonData)
365
+ } catch (err) {
366
+ return cb(err)
367
+ }
368
+ })
369
+ })
370
+ req.on('error', cb)
371
+
372
+ if (data) {
373
+ req.write(data)
374
+ }
375
+ req.end()
376
+ }
377
+ }
378
+
379
+ module.exports = {
380
+ EWMA: EWMA,
381
+ Cache: Cache,
382
+ StackTraceParser: StackTraceParser,
383
+ serialize: fclone,
384
+ network: {
385
+ getIP: retrieveAddress,
386
+ v4: retrieveAddress('v4'),
387
+ v6: retrieveAddress('v6')
388
+ },
389
+ HTTPClient: HTTPClient,
390
+ Cipher: Cipher,
391
+ clone: fclone
392
+ }
@@ -0,0 +1,70 @@
1
+ 'use strict'
2
+
3
+ const debug = require('debug')('interactor:watchdog')
4
+ const child = require('child_process')
5
+ const path = require('path')
6
+ const RECONNECT_TENTATIVES_BEFORE_RESURRECT = 6
7
+
8
+ process.env.PM2_AGENT_ONLINE = true
9
+
10
+ module.exports = class WatchDog {
11
+ static start (p) {
12
+ this.pm2_binary_path = p.pm2_binary_path
13
+ this.ipm2 = p.conf.ipm2
14
+ this.relaunching = false
15
+ this.autoDumpTime = 5 * 60 * 1000
16
+
17
+ /**
18
+ * Handle PM2 connection state changes
19
+ */
20
+ this.ipm2.on('ready', _ => {
21
+ debug('Connected to PM2')
22
+ this.relaunching = false
23
+ this.autoDump()
24
+ })
25
+
26
+ debug('Launching')
27
+
28
+ this.reconnect_tentatives = 0
29
+
30
+ this.ipm2.on('reconnecting', _ => {
31
+ debug('PM2 is disconnected - Relaunching PM2')
32
+
33
+ if (this.dump_interval) {
34
+ clearInterval(this.dump_interval)
35
+ }
36
+
37
+ if (this.reconnect_tentatives++ >= RECONNECT_TENTATIVES_BEFORE_RESURRECT &&
38
+ this.relaunching === false) {
39
+ this.relaunching = true
40
+ this.resurrect()
41
+ }
42
+ })
43
+ }
44
+
45
+ static stop() {
46
+ clearInterval(this.dump_interval)
47
+ }
48
+
49
+ static resurrect () {
50
+ debug(`Trying to launch PM2: ${path.resolve(__dirname, '../../../../bin/pm2')}`)
51
+ child.exec(`node ${this.pm2_binary_path} resurrect`, (err, sto, ste) => {
52
+ if (err) console.error(err)
53
+ console.log(sto, ste)
54
+ this.reconnect_tentatives = 0
55
+ setTimeout(_ => {
56
+ this.relaunching = false
57
+ }, 10 * 1000)
58
+ })
59
+ }
60
+
61
+ static autoDump () {
62
+ this.dump_interval = setInterval(_ => {
63
+ if (this.relaunching === true) return
64
+
65
+ this.ipm2.pm2Interface.dump(function (err) {
66
+ return err ? debug('Error when dumping', err) : debug('PM2 process list dumped')
67
+ })
68
+ }, this.autoDumpTime)
69
+ }
70
+ }
@@ -0,0 +1,77 @@
1
+ 'use strict'
2
+
3
+ const os = require('os')
4
+ const pkg = require('../../../../package.json')
5
+ const constants = require('../../constants')
6
+ const path = require('path')
7
+ const hostname = os.hostname()
8
+ const platform = os.platform()
9
+ const username = process.env.SUDO_USER || process.env.C9_USER || process.env.LOGNAME ||
10
+ process.env.USER || process.env.LNAME || process.env.USERNAME
11
+
12
+ module.exports = class DataRetriever {
13
+ /**
14
+ * Normalize each process metdata
15
+ * @param {Object} processes process list extracted from pm2 daemon
16
+ * @param {Object} conf interactor configuration
17
+ */
18
+ static status (processes, conf) {
19
+ processes = processes || []
20
+ const formattedProcs = processes
21
+ .filter(proc => !proc.pm2_env.name.match(/_old_/))
22
+ .map((proc) => {
23
+
24
+ // proc.pm2_env.axm_actions = proc.pm2_env.axm_actions.concat(conf.PM2_REMOTE_METHOD_ALLOWED.map(method => {
25
+ // return {action_name: method, action_type: 'internal'}
26
+ // }))
27
+
28
+ return {
29
+ pid: proc.pid,
30
+ name: proc.pm2_env.name,
31
+ interpreter: proc.pm2_env.exec_interpreter,
32
+ args: proc.pm2_env.node_args,
33
+ path: proc.pm2_env.pm_exec_path,
34
+ restart_time: proc.pm2_env.restart_time,
35
+ created_at: proc.pm2_env.created_at,
36
+ exec_mode: proc.pm2_env.exec_mode,
37
+ pm_uptime: proc.pm2_env.pm_uptime,
38
+ status: proc.pm2_env.status,
39
+ pm_id: proc.pm2_env.pm_id,
40
+ unique_id: proc.pm2_env.unique_id,
41
+
42
+ cpu: Math.floor(proc.monit.cpu) || 0,
43
+ memory: Math.floor(proc.monit.memory) || 0,
44
+
45
+ versioning: proc.pm2_env.versioning || null,
46
+
47
+ node_env: proc.pm2_env.NODE_ENV || null,
48
+
49
+ axm_actions: proc.pm2_env.axm_actions || [],
50
+ axm_monitor: proc.pm2_env.axm_monitor || {},
51
+ //axm_options: proc.pm2_env.axm_options || {},
52
+ axm_options: {},
53
+ axm_dynamic: proc.pm2_env.dynamic || {}
54
+ }
55
+ })
56
+
57
+ return {
58
+ process: formattedProcs,
59
+ server: {
60
+ username: username,
61
+ hostname: hostname,
62
+ uptime: os.uptime(),
63
+ platform: platform,
64
+ pm2_version: conf.PM2_VERSION,
65
+ pm2_agent_version: pkg.version,
66
+ node_version: process.version,
67
+ unique_id: constants.UNIQUE_SERVER_ID,
68
+ loadavg: os.loadavg(),
69
+ total_mem: os.totalmem(),
70
+ //free_mem: os.freemem(),
71
+ // cpu: cpuMeta,
72
+ type: os.type(),
73
+ //interaction: conf.REVERSE_INTERACT,
74
+ }
75
+ }
76
+ }
77
+ }