@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,193 @@
1
+ 'use strict'
2
+
3
+ const utils = require('../utils/module')
4
+ const Configuration = require('../configuration')
5
+ const { ServiceManager } = require('../serviceManager')
6
+ const MiscUtils = require('../utils/miscellaneous')
7
+ const Debug = require('debug')
8
+
9
+ class CurrentProfile {
10
+ constructor () {
11
+ this.uuid = null
12
+ this.startTime = null
13
+ this.initiated = null
14
+ }
15
+ }
16
+
17
+ class AddonProfiler {
18
+ constructor () {
19
+ this.profiler = null
20
+ this.modules = ['v8-profiler-node8', 'v8-profiler']
21
+ this.actionService = undefined
22
+ this.transport = undefined
23
+ this.currentProfile = null
24
+ this.logger = Debug('axm:features:profiling:addon')
25
+ }
26
+
27
+ init () {
28
+ for (const moduleName of this.modules) {
29
+ let path = utils.detectModule(moduleName)
30
+ if (path === null) continue
31
+ let profiler = utils.loadModule(moduleName)
32
+ if (profiler instanceof Error) continue
33
+ this.profiler = profiler
34
+ break
35
+ }
36
+ if (this.profiler === null) {
37
+ Configuration.configureModule({
38
+ heapdump: false,
39
+ 'feature.profiler.heap_snapshot': false,
40
+ 'feature.profiler.heap_sampling': false,
41
+ 'feature.profiler.cpu_js': false
42
+ })
43
+ return this.logger('Failed to require the profiler via addon, disabling profiling ...')
44
+ }
45
+ this.logger('init')
46
+
47
+ this.actionService = ServiceManager.get('actions')
48
+ if (this.actionService === undefined) {
49
+ return this.logger('Fail to get action service')
50
+ }
51
+ this.transport = ServiceManager.get('transport')
52
+ if (this.transport === undefined) {
53
+ return this.logger('Fail to get transport service')
54
+ }
55
+
56
+ Configuration.configureModule({
57
+ heapdump: true,
58
+ 'feature.profiler.heapsnapshot': true,
59
+ 'feature.profiler.heapsampling': false,
60
+ 'feature.profiler.cpu_js': true
61
+ })
62
+ this.register()
63
+ }
64
+
65
+ register () {
66
+ if (this.actionService === undefined) {
67
+ return this.logger('Fail to get action service')
68
+ }
69
+ this.logger('register')
70
+ this.actionService.registerAction('km:heapdump', this.onHeapdump.bind(this))
71
+ this.actionService.registerAction('km:cpu:profiling:start', this.onCPUProfileStart.bind(this))
72
+ this.actionService.registerAction('km:cpu:profiling:stop', this.onCPUProfileStop.bind(this))
73
+ }
74
+
75
+ destroy () {
76
+ this.logger('Addon Profiler destroyed !')
77
+ if (this.profiler === null) return
78
+ this.profiler.deleteAllProfiles()
79
+ }
80
+
81
+ onCPUProfileStart (opts, cb) {
82
+ if (typeof cb !== 'function') {
83
+ cb = opts
84
+ opts = {}
85
+ }
86
+ if (typeof opts !== 'object' || opts === null) {
87
+ opts = {}
88
+ }
89
+
90
+ if (this.currentProfile !== null) {
91
+ return cb({
92
+ err: 'A profiling is already running',
93
+ success: false
94
+ })
95
+ }
96
+ this.currentProfile = new CurrentProfile()
97
+ this.currentProfile.uuid = MiscUtils.generateUUID()
98
+ this.currentProfile.startTime = Date.now()
99
+ this.currentProfile.initiated = typeof opts.initiated === 'string'
100
+ ? opts.initiated : 'manual'
101
+
102
+ cb({ success: true, uuid: this.currentProfile.uuid })
103
+
104
+ this.profiler.startProfiling()
105
+
106
+ if (isNaN(parseInt(opts.timeout, 10))) return
107
+ const duration = parseInt(opts.timeout, 10)
108
+ setTimeout(_ => {
109
+ this.onCPUProfileStop(_ => {
110
+ return
111
+ })
112
+ }, duration)
113
+ }
114
+
115
+ onCPUProfileStop (cb) {
116
+ if (this.currentProfile === null) {
117
+ return cb({
118
+ err: 'No profiling are already running',
119
+ success: false
120
+ })
121
+ }
122
+ if (this.transport === undefined) {
123
+ return cb({
124
+ err: 'No profiling are already running',
125
+ success: false
126
+ })
127
+ }
128
+ const profile = this.profiler.stopProfiling()
129
+ const data = JSON.stringify(profile)
130
+
131
+ cb({ success: true, uuid: this.currentProfile.uuid })
132
+
133
+ this.transport.send('profilings', {
134
+ uuid: this.currentProfile.uuid,
135
+ duration: Date.now() - this.currentProfile.startTime,
136
+ at: this.currentProfile.startTime,
137
+ data,
138
+ dump_file_size: data.length,
139
+ success: true,
140
+ initiated: this.currentProfile.initiated,
141
+ type: 'cpuprofile',
142
+ cpuprofile: true
143
+ })
144
+ this.currentProfile = null
145
+ }
146
+
147
+ onHeapdump (opts, cb) {
148
+ if (typeof cb !== 'function') {
149
+ cb = opts
150
+ opts = {}
151
+ }
152
+ if (typeof opts !== 'object' || opts === null) {
153
+ opts = {}
154
+ }
155
+
156
+ cb({ success: true })
157
+
158
+ setTimeout(() => {
159
+ const startTime = Date.now()
160
+ this.takeSnapshot()
161
+ .then((data) => {
162
+ return this.transport.send('profilings', {
163
+ data,
164
+ at: startTime,
165
+ initiated: typeof opts.initiated === 'string' ? opts.initiated : 'manual',
166
+ duration: Date.now() - startTime,
167
+ type: 'heapdump'
168
+ })
169
+ }).catch(err => {
170
+ return cb({
171
+ success: err.message,
172
+ err: err
173
+ })
174
+ })
175
+ }, 200)
176
+ }
177
+
178
+ takeSnapshot () {
179
+ return new Promise((resolve, reject) => {
180
+ const snapshot = this.profiler.takeSnapshot()
181
+ snapshot.export((err, data) => {
182
+ if (err) {
183
+ reject(err)
184
+ } else {
185
+ resolve(data)
186
+ }
187
+ snapshot.delete()
188
+ })
189
+ })
190
+ }
191
+ }
192
+
193
+ module.exports = AddonProfiler
@@ -0,0 +1,302 @@
1
+ 'use strict'
2
+
3
+ const Configuration = require('../configuration')
4
+ const { ServiceManager } = require('../serviceManager')
5
+ const MiscUtils = require('../utils/miscellaneous')
6
+ const inspector = require('inspector')
7
+ const Debug = require('debug')
8
+ class CurrentProfile {
9
+ constructor () {
10
+ this.uuid = null
11
+ this.startTime = null
12
+ this.initiated = null
13
+ }
14
+ }
15
+
16
+ class InspectorProfiler {
17
+ constructor () {
18
+ this.profiler = undefined
19
+ this.actionService = undefined
20
+ this.transport = undefined
21
+ this.currentProfile = null
22
+ this.logger = Debug('axm:features:profiling:inspector')
23
+ this.isNode11 = true // Always true on Node 18+
24
+ }
25
+
26
+ init () {
27
+ this.profiler = ServiceManager.get('inspector')
28
+ if (this.profiler === undefined) {
29
+ Configuration.configureModule({
30
+ heapdump: false,
31
+ 'feature.profiler.heap_snapshot': false,
32
+ 'feature.profiler.heap_sampling': false,
33
+ 'feature.profiler.cpu_js': false
34
+ })
35
+ return console.error('Failed to require the profiler via inspector, disabling profiling ...')
36
+ }
37
+
38
+ this.profiler.getSession().post('Profiler.enable')
39
+ this.profiler.getSession().post('HeapProfiler.enable')
40
+ this.logger('init')
41
+
42
+ this.actionService = ServiceManager.get('actions')
43
+ if (this.actionService === undefined) {
44
+ return this.logger('Fail to get action service')
45
+ }
46
+ this.transport = ServiceManager.get('transport')
47
+ if (this.transport === undefined) {
48
+ return this.logger('Fail to get transport service')
49
+ }
50
+
51
+ Configuration.configureModule({
52
+ heapdump: true,
53
+ 'feature.profiler.heapsnapshot': !this.isNode11,
54
+ 'feature.profiler.heapsampling': true,
55
+ 'feature.profiler.cpu_js': true
56
+ })
57
+ this.register()
58
+ }
59
+
60
+ register () {
61
+ if (this.actionService === undefined) {
62
+ return this.logger('Fail to get action service')
63
+ }
64
+ this.logger('register')
65
+ this.actionService.registerAction('km:heapdump', this.onHeapdump.bind(this))
66
+ this.actionService.registerAction('km:cpu:profiling:start', this.onCPUProfileStart.bind(this))
67
+ this.actionService.registerAction('km:cpu:profiling:stop', this.onCPUProfileStop.bind(this))
68
+ this.actionService.registerAction('km:heap:sampling:start', this.onHeapProfileStart.bind(this))
69
+ this.actionService.registerAction('km:heap:sampling:stop', this.onHeapProfileStop.bind(this))
70
+ }
71
+
72
+ destroy () {
73
+ this.logger('Inspector Profiler destroyed !')
74
+ if (this.profiler === undefined) return
75
+ this.profiler.getSession().post('Profiler.disable')
76
+ this.profiler.getSession().post('HeapProfiler.disable')
77
+ }
78
+
79
+ onHeapProfileStart (opts, cb) {
80
+ if (typeof cb !== 'function') {
81
+ cb = opts
82
+ opts = {}
83
+ }
84
+ if (typeof opts !== 'object' || opts === null) {
85
+ opts = {}
86
+ }
87
+
88
+ if (this.profiler === undefined) {
89
+ return cb({
90
+ err: 'Profiler not available',
91
+ success: false
92
+ })
93
+ }
94
+
95
+ if (this.currentProfile !== null) {
96
+ return cb({
97
+ err: 'A profiling is already running',
98
+ success: false
99
+ })
100
+ }
101
+ this.currentProfile = new CurrentProfile()
102
+ this.currentProfile.uuid = MiscUtils.generateUUID()
103
+ this.currentProfile.startTime = Date.now()
104
+ this.currentProfile.initiated = typeof opts.initiated === 'string'
105
+ ? opts.initiated : 'manual'
106
+
107
+ cb({ success: true, uuid: this.currentProfile.uuid })
108
+
109
+ const defaultSamplingInterval = 16384
110
+ this.profiler.getSession().post('HeapProfiler.startSampling', {
111
+ samplingInterval: typeof opts.samplingInterval === 'number'
112
+ ? opts.samplingInterval : defaultSamplingInterval
113
+ })
114
+
115
+ if (isNaN(parseInt(opts.timeout, 10))) return
116
+ const duration = parseInt(opts.timeout, 10)
117
+ setTimeout(_ => {
118
+ this.onHeapProfileStop(_ => {
119
+ return
120
+ })
121
+ }, duration)
122
+ }
123
+
124
+ onHeapProfileStop (cb) {
125
+ if (this.currentProfile === null) {
126
+ return cb({
127
+ err: 'No profiling are already running',
128
+ success: false
129
+ })
130
+ }
131
+ if (this.profiler === undefined) {
132
+ return cb({
133
+ err: 'Profiler not available',
134
+ success: false
135
+ })
136
+ }
137
+
138
+ cb({ success: true, uuid: this.currentProfile.uuid })
139
+
140
+ this.profiler.getSession().post('HeapProfiler.stopSampling', (_, res) => {
141
+ if (this.currentProfile === null) return
142
+ if (this.transport === undefined) return
143
+
144
+ const profile = res.profile
145
+ const data = JSON.stringify(profile)
146
+
147
+ this.transport.send('profilings', {
148
+ uuid: this.currentProfile.uuid,
149
+ duration: Date.now() - this.currentProfile.startTime,
150
+ at: this.currentProfile.startTime,
151
+ data,
152
+ success: true,
153
+ initiated: this.currentProfile.initiated,
154
+ type: 'heapprofile',
155
+ heapprofile: true
156
+ })
157
+ this.currentProfile = null
158
+ })
159
+ }
160
+
161
+ onCPUProfileStart (opts, cb) {
162
+ if (typeof cb !== 'function') {
163
+ cb = opts
164
+ opts = {}
165
+ }
166
+ if (typeof opts !== 'object' || opts === null) {
167
+ opts = {}
168
+ }
169
+ if (this.profiler === undefined) {
170
+ return cb({
171
+ err: 'Profiler not available',
172
+ success: false
173
+ })
174
+ }
175
+
176
+ if (this.currentProfile !== null) {
177
+ return cb({
178
+ err: 'A profiling is already running',
179
+ success: false
180
+ })
181
+ }
182
+ this.currentProfile = new CurrentProfile()
183
+ this.currentProfile.uuid = MiscUtils.generateUUID()
184
+ this.currentProfile.startTime = Date.now()
185
+ this.currentProfile.initiated = typeof opts.initiated === 'string'
186
+ ? opts.initiated : 'manual'
187
+
188
+ cb({ success: true, uuid: this.currentProfile.uuid })
189
+
190
+ if (process.hasOwnProperty('_startProfilerIdleNotifier') === true) {
191
+ process._startProfilerIdleNotifier()
192
+ }
193
+
194
+ this.profiler.getSession().post('Profiler.start')
195
+
196
+ if (isNaN(parseInt(opts.timeout, 10))) return
197
+ const duration = parseInt(opts.timeout, 10)
198
+ setTimeout(_ => {
199
+ this.onCPUProfileStop(_ => {
200
+ return
201
+ })
202
+ }, duration)
203
+ }
204
+
205
+ onCPUProfileStop (cb) {
206
+ if (this.currentProfile === null) {
207
+ return cb({
208
+ err: 'No profiling are already running',
209
+ success: false
210
+ })
211
+ }
212
+ if (this.profiler === undefined) {
213
+ return cb({
214
+ err: 'Profiler not available',
215
+ success: false
216
+ })
217
+ }
218
+
219
+ cb({ success: true, uuid: this.currentProfile.uuid })
220
+
221
+ if (process.hasOwnProperty('_stopProfilerIdleNotifier') === true) {
222
+ process._stopProfilerIdleNotifier()
223
+ }
224
+
225
+ this.profiler.getSession().post('Profiler.stop', (_, res) => {
226
+ if (this.currentProfile === null) return
227
+ if (this.transport === undefined) return
228
+
229
+ const profile = res.profile
230
+ const data = JSON.stringify(profile)
231
+
232
+ this.transport.send('profilings', {
233
+ uuid: this.currentProfile.uuid,
234
+ duration: Date.now() - this.currentProfile.startTime,
235
+ at: this.currentProfile.startTime,
236
+ data,
237
+ success: true,
238
+ initiated: this.currentProfile.initiated,
239
+ type: 'cpuprofile',
240
+ cpuprofile: true
241
+ })
242
+ this.currentProfile = null
243
+ })
244
+ }
245
+
246
+ onHeapdump (opts, cb) {
247
+ if (typeof cb !== 'function') {
248
+ cb = opts
249
+ opts = {}
250
+ }
251
+ if (typeof opts !== 'object' || opts === null) {
252
+ opts = {}
253
+ }
254
+ if (this.profiler === undefined) {
255
+ return cb({
256
+ err: 'Profiler not available',
257
+ success: false
258
+ })
259
+ }
260
+
261
+ cb({ success: true })
262
+
263
+ setTimeout(() => {
264
+ const startTime = Date.now()
265
+ this.takeSnapshot()
266
+ .then(data => {
267
+ return this.transport.send('profilings', {
268
+ data,
269
+ at: startTime,
270
+ initiated: typeof opts.initiated === 'string' ? opts.initiated : 'manual',
271
+ duration: Date.now() - startTime,
272
+ type: 'heapdump'
273
+ })
274
+ }).catch(err => {
275
+ return cb({
276
+ success: err.message,
277
+ err: err
278
+ })
279
+ })
280
+ }, 200)
281
+ }
282
+
283
+ takeSnapshot () {
284
+ return new Promise(async (resolve, reject) => {
285
+ if (this.profiler === undefined) return reject(new Error('Profiler not available'))
286
+
287
+ const chunks = []
288
+ const chunkHandler = (raw) => {
289
+ const data = raw.params
290
+ chunks.push(data.chunk)
291
+ }
292
+ this.profiler.getSession().on('HeapProfiler.addHeapSnapshotChunk', chunkHandler)
293
+ await this.profiler.getSession().post('HeapProfiler.takeHeapSnapshot', {
294
+ reportProgress: false
295
+ })
296
+ this.profiler.getSession().removeListener('HeapProfiler.addHeapSnapshotChunk', chunkHandler)
297
+ return resolve(chunks.join(''))
298
+ })
299
+ }
300
+ }
301
+
302
+ module.exports = InspectorProfiler
@@ -0,0 +1,21 @@
1
+ 'use strict'
2
+
3
+ const services = new Map()
4
+
5
+ class Service {}
6
+
7
+ class ServiceManager {
8
+ static get (serviceName) {
9
+ return services.get(serviceName)
10
+ }
11
+
12
+ static set (serviceName, service) {
13
+ return services.set(serviceName, service)
14
+ }
15
+
16
+ static reset (serviceName) {
17
+ return services.delete(serviceName)
18
+ }
19
+ }
20
+
21
+ module.exports = { Service, ServiceManager }
@@ -0,0 +1,159 @@
1
+ 'use strict'
2
+
3
+ const { ServiceManager } = require('../serviceManager')
4
+ const Debug = require('debug')
5
+
6
+ class ActionService {
7
+ constructor () {
8
+ this.timer = undefined
9
+ this.transport = undefined
10
+ this.actions = new Map()
11
+ this.logger = Debug('axm:services:actions')
12
+ }
13
+
14
+ listener (data) {
15
+ this.logger('Received new message from reverse')
16
+ if (!data) return false
17
+
18
+ const actionName = data.msg ? data.msg : data.action_name ? data.action_name : data
19
+ let action = this.actions.get(actionName)
20
+ if (typeof action !== 'object') {
21
+ return this.logger(`Received action ${actionName} but failed to find the implementation`)
22
+ }
23
+
24
+ if (!action.isScoped) {
25
+ this.logger(`Succesfully called custom action ${action.name} with arity ${action.handler.length}`)
26
+ if (action.handler.length === 2) {
27
+ let params = {}
28
+ if (typeof data === 'object') {
29
+ params = data.opts
30
+ }
31
+ return action.handler(params, action.callback)
32
+ }
33
+ return action.handler(action.callback)
34
+ }
35
+
36
+ if (data.uuid === undefined) {
37
+ return this.logger(`Received scoped action ${action.name} but without uuid`)
38
+ }
39
+
40
+ const stream = {
41
+ send: (dt) => {
42
+ this.transport.send('axm:scoped_action:stream', {
43
+ data: dt,
44
+ uuid: data.uuid,
45
+ action_name: actionName
46
+ })
47
+ },
48
+ error: (dt) => {
49
+ this.transport.send('axm:scoped_action:error', {
50
+ data: dt,
51
+ uuid: data.uuid,
52
+ action_name: actionName
53
+ })
54
+ },
55
+ end: (dt) => {
56
+ this.transport.send('axm:scoped_action:end', {
57
+ data: dt,
58
+ uuid: data.uuid,
59
+ action_name: actionName
60
+ })
61
+ }
62
+ }
63
+
64
+ this.logger(`Succesfully called scoped action ${action.name}`)
65
+ return action.handler(data.opts || {}, stream)
66
+ }
67
+
68
+ init () {
69
+ this.transport = ServiceManager.get('transport')
70
+ if (this.transport === undefined) {
71
+ return this.logger('Failed to load transport service')
72
+ }
73
+ this.actions.clear()
74
+ this.transport.on('data', this.listener.bind(this))
75
+ }
76
+
77
+ destroy () {
78
+ if (this.timer !== undefined) {
79
+ clearInterval(this.timer)
80
+ }
81
+ if (this.transport !== undefined) {
82
+ this.transport.removeListener('data', this.listener.bind(this))
83
+ }
84
+ }
85
+
86
+ registerAction (actionName, opts, handler) {
87
+ if (typeof opts === 'function') {
88
+ handler = opts
89
+ opts = undefined
90
+ }
91
+
92
+ if (typeof actionName !== 'string') {
93
+ console.error('You must define an name when registering an action')
94
+ return
95
+ }
96
+ if (typeof handler !== 'function') {
97
+ console.error('You must define an callback when registering an action')
98
+ return
99
+ }
100
+ if (this.transport === undefined) {
101
+ return this.logger('Failed to load transport service')
102
+ }
103
+
104
+ let type = 'custom'
105
+
106
+ if (actionName.indexOf('km:') === 0 || actionName.indexOf('internal:') === 0) {
107
+ type = 'internal'
108
+ }
109
+
110
+ const reply = (data) => {
111
+ this.transport.send('axm:reply', {
112
+ at: new Date().getTime(),
113
+ action_name: actionName,
114
+ return: data
115
+ })
116
+ }
117
+
118
+ const action = {
119
+ name: actionName,
120
+ callback: reply,
121
+ handler,
122
+ type,
123
+ isScoped: false,
124
+ arity: handler.length,
125
+ opts
126
+ }
127
+ this.logger(`Succesfully registered custom action ${action.name}`)
128
+ this.actions.set(actionName, action)
129
+ this.transport.addAction(action)
130
+ }
131
+
132
+ scopedAction (actionName, handler) {
133
+ if (typeof actionName !== 'string') {
134
+ console.error('You must define an name when registering an action')
135
+ return -1
136
+ }
137
+ if (typeof handler !== 'function') {
138
+ console.error('You must define an callback when registering an action')
139
+ return -1
140
+ }
141
+ if (this.transport === undefined) {
142
+ return this.logger('Failed to load transport service')
143
+ }
144
+
145
+ const action = {
146
+ name: actionName,
147
+ handler,
148
+ type: 'scoped',
149
+ isScoped: true,
150
+ arity: handler.length,
151
+ opts: null
152
+ }
153
+ this.logger(`Succesfully registered scoped action ${action.name}`)
154
+ this.actions.set(actionName, action)
155
+ this.transport.addAction(action)
156
+ }
157
+ }
158
+
159
+ module.exports = { ActionService }
@@ -0,0 +1,43 @@
1
+ 'use strict'
2
+
3
+ const inspector = require('inspector')
4
+ const Debug = require('debug')
5
+
6
+ class InspectorService {
7
+ constructor () {
8
+ this.session = null
9
+ this.logger = Debug('axm:services:inspector')
10
+ }
11
+
12
+ init () {
13
+ this.logger('Creating new inspector session')
14
+ this.session = new inspector.Session()
15
+ this.session.connect()
16
+ this.logger('Connected to inspector')
17
+ this.session.post('Profiler.enable')
18
+ this.session.post('HeapProfiler.enable')
19
+ return this.session
20
+ }
21
+
22
+ getSession () {
23
+ if (this.session === null) {
24
+ this.session = this.init()
25
+ return this.session
26
+ } else {
27
+ return this.session
28
+ }
29
+ }
30
+
31
+ destroy () {
32
+ if (this.session !== null) {
33
+ this.session.post('Profiler.disable')
34
+ this.session.post('HeapProfiler.disable')
35
+ this.session.disconnect()
36
+ this.session = null
37
+ } else {
38
+ this.logger('No open session')
39
+ }
40
+ }
41
+ }
42
+
43
+ module.exports = InspectorService