@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,430 @@
1
+
2
+ /**
3
+ * Module dependencies.
4
+ */
5
+
6
+ var Emitter = require('events').EventEmitter;
7
+ var Configurable = require('../configurable');
8
+ var debug = require('debug')('axon:sock');
9
+ var Message = require('amp-message');
10
+ var Parser = require('amp').Stream;
11
+ var net = require('net');
12
+ var fs = require('fs');
13
+
14
+ /**
15
+ * Parse address string into components.
16
+ * Handles both URL-style ("tcp://host:port") and unix socket paths ("/tmp/sock").
17
+ */
18
+ function parseAddr(str) {
19
+ try {
20
+ var u = new URL(str);
21
+ if (u.protocol === 'unix:') {
22
+ return { pathname: u.pathname, hostname: null, port: null };
23
+ }
24
+ return { hostname: u.hostname, port: u.port, pathname: null };
25
+ } catch(e) {
26
+ return { pathname: str, hostname: null, port: null };
27
+ }
28
+ }
29
+
30
+ /**
31
+ * Errors to ignore.
32
+ */
33
+
34
+ var ignore = [
35
+ 'ECONNREFUSED',
36
+ 'ECONNRESET',
37
+ 'ETIMEDOUT',
38
+ 'EHOSTUNREACH',
39
+ 'ENETUNREACH',
40
+ 'ENETDOWN',
41
+ 'EPIPE',
42
+ 'ENOENT'
43
+ ];
44
+
45
+ /**
46
+ * Expose `Socket`.
47
+ */
48
+
49
+ module.exports = Socket;
50
+
51
+ /**
52
+ * Initialize a new `Socket`.
53
+ *
54
+ * A "Socket" encapsulates the ability of being
55
+ * the "client" or the "server" depending on
56
+ * whether `connect()` or `bind()` was called.
57
+ *
58
+ * @api private
59
+ */
60
+
61
+ function Socket() {
62
+ var self = this;
63
+ this.opts = {};
64
+ this.server = null;
65
+ this.socks = [];
66
+ this.settings = {};
67
+ this.set('hwm', Infinity);
68
+ this.set('identity', String(process.pid));
69
+ this.set('retry timeout', 100);
70
+ this.set('retry max timeout', 5000);
71
+ }
72
+
73
+ /**
74
+ * Inherit from `Emitter.prototype`.
75
+ */
76
+
77
+ Socket.prototype.__proto__ = Emitter.prototype;
78
+
79
+ /**
80
+ * Make it configurable `.set()` etc.
81
+ */
82
+
83
+ Configurable(Socket.prototype);
84
+
85
+ /**
86
+ * Use the given `plugin`.
87
+ *
88
+ * @param {Function} plugin
89
+ * @api private
90
+ */
91
+
92
+ Socket.prototype.use = function(plugin){
93
+ plugin(this);
94
+ return this;
95
+ };
96
+
97
+ /**
98
+ * Creates a new `Message` and write the `args`.
99
+ *
100
+ * @param {Array} args
101
+ * @return {Buffer}
102
+ * @api private
103
+ */
104
+
105
+ Socket.prototype.pack = function(args){
106
+ var msg = new Message(args);
107
+ return msg.toBuffer();
108
+ };
109
+
110
+ /**
111
+ * Close all open underlying sockets.
112
+ *
113
+ * @api private
114
+ */
115
+
116
+ Socket.prototype.closeSockets = function(){
117
+ debug('closing %d connections', this.socks.length);
118
+ this.socks.forEach(function(sock){
119
+ sock.destroy();
120
+ });
121
+ };
122
+
123
+ /**
124
+ * Close the socket.
125
+ *
126
+ * Delegates to the server or clients
127
+ * based on the socket `type`.
128
+ *
129
+ * @param {Function} [fn]
130
+ * @api public
131
+ */
132
+
133
+ Socket.prototype.close = function(fn){
134
+ debug('closing');
135
+ this.closing = true;
136
+ this.closeSockets();
137
+ if (this.server) this.closeServer(fn);
138
+ };
139
+
140
+ /**
141
+ * Close the server.
142
+ *
143
+ * @param {Function} [fn]
144
+ * @api public
145
+ */
146
+
147
+ Socket.prototype.closeServer = function(fn){
148
+ debug('closing server');
149
+ this.server.on('close', this.emit.bind(this, 'close'));
150
+ this.server.close();
151
+ fn && fn();
152
+ };
153
+
154
+ /**
155
+ * Return the server address.
156
+ *
157
+ * @return {Object}
158
+ * @api public
159
+ */
160
+
161
+ Socket.prototype.address = function(){
162
+ if (!this.server) return;
163
+ var addr = this.server.address();
164
+ addr.string = 'tcp://' + addr.address + ':' + addr.port;
165
+ return addr;
166
+ };
167
+
168
+ /**
169
+ * Remove `sock`.
170
+ *
171
+ * @param {Socket} sock
172
+ * @api private
173
+ */
174
+
175
+ Socket.prototype.removeSocket = function(sock){
176
+ var i = this.socks.indexOf(sock);
177
+ if (!~i) return;
178
+ debug('remove socket %d', i);
179
+ this.socks.splice(i, 1);
180
+ };
181
+
182
+ /**
183
+ * Add `sock`.
184
+ *
185
+ * @param {Socket} sock
186
+ * @api private
187
+ */
188
+
189
+ Socket.prototype.addSocket = function(sock){
190
+ var parser = new Parser;
191
+ var i = this.socks.push(sock) - 1;
192
+ debug('add socket %d', i);
193
+ sock.pipe(parser);
194
+ parser.on('data', this.onmessage(sock));
195
+ };
196
+
197
+ /**
198
+ * Handle `sock` errors.
199
+ *
200
+ * Emits:
201
+ *
202
+ * - `error` (err) when the error is not ignored
203
+ * - `ignored error` (err) when the error is ignored
204
+ * - `socket error` (err) regardless of ignoring
205
+ *
206
+ * @param {Socket} sock
207
+ * @api private
208
+ */
209
+
210
+ Socket.prototype.handleErrors = function(sock){
211
+ var self = this;
212
+ sock.on('error', function(err){
213
+ debug('error %s', err.code || err.message);
214
+ self.emit('socket error', err);
215
+ self.removeSocket(sock);
216
+ if (!~ignore.indexOf(err.code)) return self.emit('error', err);
217
+ debug('ignored %s', err.code);
218
+ self.emit('ignored error', err);
219
+ });
220
+ };
221
+
222
+ /**
223
+ * Handles framed messages emitted from the parser, by
224
+ * default it will go ahead and emit the "message" events on
225
+ * the socket. However, if the "higher level" socket needs
226
+ * to hook into the messages before they are emitted, it
227
+ * should override this method and take care of everything
228
+ * it self, including emitted the "message" event.
229
+ *
230
+ * @param {net.Socket} sock
231
+ * @return {Function} closure(msg, mulitpart)
232
+ * @api private
233
+ */
234
+
235
+ Socket.prototype.onmessage = function(sock){
236
+ var self = this;
237
+ return function(buf){
238
+ var msg = new Message(buf);
239
+ self.emit.apply(self, ['message'].concat(msg.args), sock);
240
+ };
241
+ };
242
+
243
+ /**
244
+ * Connect to `port` at `host` and invoke `fn()`.
245
+ *
246
+ * Defaults `host` to localhost.
247
+ *
248
+ * TODO: needs big cleanup
249
+ *
250
+ * @param {Number|String} port
251
+ * @param {String} host
252
+ * @param {Function} fn
253
+ * @return {Socket}
254
+ * @api public
255
+ */
256
+
257
+ Socket.prototype.connect = function(port, host, fn){
258
+ var self = this;
259
+ if ('server' == this.type) throw new Error('cannot connect() after bind()');
260
+ if ('function' == typeof host) fn = host, host = undefined;
261
+
262
+ if ('string' == typeof port) {
263
+ port = parseAddr(port);
264
+
265
+ if (port.pathname) {
266
+ fn = host;
267
+ host = null;
268
+ fn = undefined;
269
+ port = port.pathname;
270
+ } else {
271
+ host = port.hostname || '0.0.0.0';
272
+ port = parseInt(port.port, 10);
273
+ }
274
+ } else {
275
+ host = host || '0.0.0.0';
276
+ }
277
+
278
+ var max = self.get('retry max timeout');
279
+ var sock = new net.Socket;
280
+ sock.setNoDelay();
281
+ this.type = 'client';
282
+ port = port;
283
+
284
+ this.handleErrors(sock);
285
+
286
+ sock.on('close', function(){
287
+ self.connected = false;
288
+ self.removeSocket(sock);
289
+ if (self.closing) return self.emit('close');
290
+ var retry = self.retry || self.get('retry timeout');
291
+ if (retry === 0) return self.emit('close');
292
+ setTimeout(function(){
293
+ debug('attempting reconnect');
294
+ self.emit('reconnect attempt');
295
+ sock.destroy();
296
+ self.connect(port, host);
297
+ self.retry = Math.round(Math.min(max, retry * 1.5));
298
+ }, retry);
299
+ });
300
+
301
+ sock.on('connect', function(){
302
+ debug('connect');
303
+ self.connected = true;
304
+ self.addSocket(sock);
305
+ self.retry = self.get('retry timeout');
306
+ self.emit('connect');
307
+ fn && fn();
308
+ });
309
+
310
+ debug('connect attempt %s:%s', host, port);
311
+ sock.connect(port, host);
312
+ return this;
313
+ };
314
+
315
+ /**
316
+ * Handle connection.
317
+ *
318
+ * @param {Socket} sock
319
+ * @api private
320
+ */
321
+
322
+ Socket.prototype.onconnect = function(sock){
323
+ var self = this;
324
+ var addr = null;
325
+
326
+ if (sock.remoteAddress && sock.remotePort)
327
+ addr = sock.remoteAddress + ':' + sock.remotePort;
328
+ else if (sock.server && sock.server._pipeName)
329
+ addr = sock.server._pipeName;
330
+
331
+ debug('accept %s', addr);
332
+ this.addSocket(sock);
333
+ this.handleErrors(sock);
334
+ this.emit('connect', sock);
335
+ sock.on('close', function(){
336
+ debug('disconnect %s', addr);
337
+ self.emit('disconnect', sock);
338
+ self.removeSocket(sock);
339
+ });
340
+ };
341
+
342
+ /**
343
+ * Bind to `port` at `host` and invoke `fn()`.
344
+ *
345
+ * Defaults `host` to INADDR_ANY.
346
+ *
347
+ * Emits:
348
+ *
349
+ * - `connection` when a client connects
350
+ * - `disconnect` when a client disconnects
351
+ * - `bind` when bound and listening
352
+ *
353
+ * @param {Number|String} port
354
+ * @param {Function} fn
355
+ * @return {Socket}
356
+ * @api public
357
+ */
358
+
359
+ Socket.prototype.bind = function(port, host, fn){
360
+ var self = this;
361
+ if ('client' == this.type) throw new Error('cannot bind() after connect()');
362
+ if ('function' == typeof host) fn = host, host = undefined;
363
+
364
+ var unixSocket = false;
365
+
366
+ if ('string' == typeof port) {
367
+ port = parseAddr(port);
368
+
369
+ if (port.pathname) {
370
+ fn = host;
371
+ host = null;
372
+ port = port.pathname;
373
+ unixSocket = true;
374
+ } else {
375
+ host = port.hostname || '0.0.0.0';
376
+ port = parseInt(port.port, 10);
377
+ }
378
+ } else {
379
+ host = host || '0.0.0.0';
380
+ }
381
+
382
+ this.type = 'server';
383
+
384
+ this.server = net.createServer(this.onconnect.bind(this));
385
+
386
+ debug('bind %s:%s', host, port);
387
+ this.server.on('listening', this.emit.bind(this, 'bind'));
388
+
389
+ if (unixSocket) {
390
+ // TODO: move out
391
+ this.server.on('error', function(e) {
392
+ debug('Got error while trying to bind', e.stack || e);
393
+ if (e.code == 'EADDRINUSE') {
394
+ // Unix file socket and error EADDRINUSE is the case if
395
+ // the file socket exists. We check if other processes
396
+ // listen on file socket, otherwise it is a stale socket
397
+ // that we could reopen
398
+ // We try to connect to socket via plain network socket
399
+ var clientSocket = new net.Socket();
400
+
401
+ clientSocket.on('error', function(e2) {
402
+ debug('Got sub-error', e2);
403
+ if (e2.code == 'ECONNREFUSED' || e2.code == 'ENOENT') {
404
+ // No other server listening, so we can delete stale
405
+ // socket file and reopen server socket
406
+ try {
407
+ fs.unlinkSync(port);
408
+ } catch(e) {}
409
+ self.server.listen(port, host, fn);
410
+ }
411
+ });
412
+
413
+ clientSocket.connect({path: port}, function() {
414
+ // Connection is possible, so other server is listening
415
+ // on this file socket
416
+ if (fn) return fn(new Error('Process already listening on socket ' + port));
417
+ });
418
+ }
419
+ else {
420
+ try {
421
+ fs.unlinkSync(port);
422
+ } catch(e) {}
423
+ self.server.listen(port, host, fn);
424
+ }
425
+ });
426
+ }
427
+
428
+ this.server.listen(port, host, fn);
429
+ return this;
430
+ };
@@ -0,0 +1,92 @@
1
+
2
+ /**
3
+ * Module dependencies.
4
+ */
5
+
6
+ var Message = require('amp-message');
7
+ var SubSocket = require('./sub');
8
+
9
+ /**
10
+ * Expose `SubEmitterSocket`.
11
+ */
12
+
13
+ module.exports = SubEmitterSocket;
14
+
15
+ /**
16
+ * Initialzie a new `SubEmitterSocket`.
17
+ *
18
+ * @api private
19
+ */
20
+
21
+ function SubEmitterSocket() {
22
+ this.sock = new SubSocket;
23
+ this.sock.onmessage = this.onmessage.bind(this);
24
+ this.bind = this.sock.bind.bind(this.sock);
25
+ this.connect = this.sock.connect.bind(this.sock);
26
+ this.close = this.sock.close.bind(this.sock);
27
+ this.listeners = [];
28
+ }
29
+
30
+ /**
31
+ * Message handler.
32
+ *
33
+ * @param {net.Socket} sock
34
+ * @return {Function} closure(msg, mulitpart)
35
+ * @api private
36
+ */
37
+
38
+ SubEmitterSocket.prototype.onmessage = function(){
39
+ var listeners = this.listeners;
40
+ var self = this;
41
+
42
+ return function(buf){
43
+ var msg = new Message(buf);
44
+ var topic = msg.shift();
45
+
46
+ for (var i = 0; i < listeners.length; ++i) {
47
+ var listener = listeners[i];
48
+
49
+ var m = listener.re.exec(topic);
50
+ if (!m) continue;
51
+
52
+ listener.fn.apply(this, m.slice(1).concat(msg.args));
53
+ }
54
+ }
55
+ };
56
+
57
+ /**
58
+ * Subscribe to `event` and invoke the given callback `fn`.
59
+ *
60
+ * @param {String} event
61
+ * @param {Function} fn
62
+ * @return {SubEmitterSocket} self
63
+ * @api public
64
+ */
65
+
66
+ SubEmitterSocket.prototype.on = function(event, fn){
67
+ var re = this.sock.subscribe(event);
68
+ this.listeners.push({
69
+ event: event,
70
+ re: re,
71
+ fn: fn
72
+ });
73
+ return this;
74
+ };
75
+
76
+ /**
77
+ * Unsubscribe with the given `event`.
78
+ *
79
+ * @param {String} event
80
+ * @return {SubEmitterSocket} self
81
+ * @api public
82
+ */
83
+
84
+ SubEmitterSocket.prototype.off = function(event){
85
+ for (var i = 0; i < this.listeners.length; ++i) {
86
+ if (this.listeners[i].event === event) {
87
+ this.sock.unsubscribe(this.listeners[i].re);
88
+ this.listeners.splice(i--, 1);
89
+ }
90
+ }
91
+ return this;
92
+ };
@@ -0,0 +1,147 @@
1
+
2
+ /**
3
+ * Module dependencies.
4
+ */
5
+
6
+ var debug = require('debug')('axon:sub');
7
+ var Message = require('amp-message');
8
+ var Socket = require('./sock');
9
+
10
+ /**
11
+ * Expose `SubSocket`.
12
+ */
13
+
14
+ module.exports = SubSocket;
15
+
16
+ /**
17
+ * Initialize a new `SubSocket`.
18
+ *
19
+ * @api private
20
+ */
21
+
22
+ function SubSocket() {
23
+ Socket.call(this);
24
+ this.subscriptions = [];
25
+ }
26
+
27
+ /**
28
+ * Inherits from `Socket.prototype`.
29
+ */
30
+
31
+ SubSocket.prototype.__proto__ = Socket.prototype;
32
+
33
+ /**
34
+ * Check if this socket has subscriptions.
35
+ *
36
+ * @return {Boolean}
37
+ * @api public
38
+ */
39
+
40
+ SubSocket.prototype.hasSubscriptions = function(){
41
+ return !! this.subscriptions.length;
42
+ };
43
+
44
+ /**
45
+ * Check if any subscriptions match `topic`.
46
+ *
47
+ * @param {String} topic
48
+ * @return {Boolean}
49
+ * @api public
50
+ */
51
+
52
+ SubSocket.prototype.matches = function(topic){
53
+ for (var i = 0; i < this.subscriptions.length; ++i) {
54
+ if (this.subscriptions[i].test(topic)) {
55
+ return true;
56
+ }
57
+ }
58
+ return false;
59
+ };
60
+
61
+ /**
62
+ * Message handler.
63
+ *
64
+ * @param {net.Socket} sock
65
+ * @return {Function} closure(msg, mulitpart)
66
+ * @api private
67
+ */
68
+
69
+ SubSocket.prototype.onmessage = function(sock){
70
+ var subs = this.hasSubscriptions();
71
+ var self = this;
72
+
73
+ return function(buf){
74
+ var msg = new Message(buf);
75
+
76
+ if (subs) {
77
+ var topic = msg.args[0];
78
+ if (!self.matches(topic)) return debug('not subscribed to "%s"', topic);
79
+ }
80
+
81
+ self.emit.apply(self, ['message'].concat(msg.args).concat(sock));
82
+ };
83
+ };
84
+
85
+ /**
86
+ * Subscribe with the given `re`.
87
+ *
88
+ * @param {RegExp|String} re
89
+ * @return {RegExp}
90
+ * @api public
91
+ */
92
+
93
+ SubSocket.prototype.subscribe = function(re){
94
+ debug('subscribe to "%s"', re);
95
+ this.subscriptions.push(re = toRegExp(re));
96
+ return re;
97
+ };
98
+
99
+ /**
100
+ * Unsubscribe with the given `re`.
101
+ *
102
+ * @param {RegExp|String} re
103
+ * @api public
104
+ */
105
+
106
+ SubSocket.prototype.unsubscribe = function(re){
107
+ debug('unsubscribe from "%s"', re);
108
+ re = toRegExp(re);
109
+ for (var i = 0; i < this.subscriptions.length; ++i) {
110
+ if (this.subscriptions[i].toString() === re.toString()) {
111
+ this.subscriptions.splice(i--, 1);
112
+ }
113
+ }
114
+ };
115
+
116
+ /**
117
+ * Clear current subscriptions.
118
+ *
119
+ * @api public
120
+ */
121
+
122
+ SubSocket.prototype.clearSubscriptions = function(){
123
+ this.subscriptions = [];
124
+ };
125
+
126
+ /**
127
+ * Subscribers should not send messages.
128
+ */
129
+
130
+ SubSocket.prototype.send = function(){
131
+ throw new Error('subscribers cannot send messages');
132
+ };
133
+
134
+ /**
135
+ * Convert `str` to a `RegExp`.
136
+ *
137
+ * @param {String} str
138
+ * @return {RegExp}
139
+ * @api private
140
+ */
141
+
142
+ function toRegExp(str) {
143
+ if (str instanceof RegExp) return str;
144
+ str = str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
145
+ str = str.replace(/\\\*/g, '(.+)');
146
+ return new RegExp('^' + str + '$');
147
+ }
@@ -0,0 +1,19 @@
1
+
2
+ /**
3
+ * Slice helper.
4
+ *
5
+ * @api private
6
+ * @param {Arguments} args
7
+ * @return {Array}
8
+ */
9
+
10
+ exports.slice = function(args){
11
+ var len = args.length;
12
+ var ret = new Array(len);
13
+
14
+ for (var i = 0; i < len; i++) {
15
+ ret[i] = args[i];
16
+ }
17
+
18
+ return ret;
19
+ };
@@ -0,0 +1,3 @@
1
+
2
+ exports.Server = require('./lib/server');
3
+ exports.Client = require('./lib/client');