@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.
- package/CHANGELOG.md +2607 -0
- package/CONTRIBUTING.md +124 -0
- package/GNU-AGPL-3.0.txt +665 -0
- package/LICENSE +1 -0
- package/README.md +255 -2
- package/SECURITY.md +41 -0
- package/bin/pm2 +3 -0
- package/bin/pm2-dev +3 -0
- package/bin/pm2-docker +3 -0
- package/bin/pm2-runtime +3 -0
- package/bin/pm2.ps1 +3 -0
- package/constants.js +113 -0
- package/index.js +12 -0
- package/lib/API/Configuration.js +212 -0
- package/lib/API/Containerizer.js +335 -0
- package/lib/API/Dashboard.js +726 -0
- package/lib/API/Deploy.js +117 -0
- package/lib/API/Extra.js +798 -0
- package/lib/API/ExtraMgmt/Docker.js +30 -0
- package/lib/API/Log.js +388 -0
- package/lib/API/LogManagement.js +351 -0
- package/lib/API/Modules/LOCAL.js +122 -0
- package/lib/API/Modules/Modularizer.js +148 -0
- package/lib/API/Modules/NPM.js +443 -0
- package/lib/API/Modules/TAR.js +368 -0
- package/lib/API/Modules/flagExt.js +46 -0
- package/lib/API/Modules/index.js +120 -0
- package/lib/API/Monit.js +247 -0
- package/lib/API/Serve.js +497 -0
- package/lib/API/Startup.js +630 -0
- package/lib/API/UX/helpers.js +213 -0
- package/lib/API/UX/index.js +9 -0
- package/lib/API/UX/pm2-describe.js +197 -0
- package/lib/API/UX/pm2-ls-minimal.js +31 -0
- package/lib/API/UX/pm2-ls.js +581 -0
- package/lib/API/Version.js +383 -0
- package/lib/API/interpreter.json +12 -0
- package/lib/API/pm2-plus/PM2IO.js +381 -0
- package/lib/API/pm2-plus/auth-strategies/CliAuth.js +306 -0
- package/lib/API/pm2-plus/auth-strategies/WebAuth.js +195 -0
- package/lib/API/pm2-plus/helpers.js +97 -0
- package/lib/API/pm2-plus/link.js +126 -0
- package/lib/API/pm2-plus/pres/motd +16 -0
- package/lib/API/pm2-plus/pres/motd.update +26 -0
- package/lib/API/pm2-plus/pres/welcome +28 -0
- package/lib/API/pm2-plus/process-selector.js +52 -0
- package/lib/API/schema.json +375 -0
- package/lib/API.js +1980 -0
- package/lib/Client.js +793 -0
- package/lib/Common.js +949 -0
- package/lib/Configuration.js +309 -0
- package/lib/Daemon.js +468 -0
- package/lib/Event.js +37 -0
- package/lib/God/ActionMethods.js +955 -0
- package/lib/God/ClusterMode.js +192 -0
- package/lib/God/ForkMode.js +205 -0
- package/lib/God/Methods.js +292 -0
- package/lib/God/Reload.js +300 -0
- package/lib/God/StdioLogger.js +239 -0
- package/lib/God.js +653 -0
- package/lib/HttpInterface.js +75 -0
- package/lib/OtelManager.js +74 -0
- package/lib/ProcessContainer.js +321 -0
- package/lib/ProcessContainerBun.js +355 -0
- package/lib/ProcessContainerFork.js +42 -0
- package/lib/ProcessContainerForkBun.js +31 -0
- package/lib/ProcessUtils.js +74 -0
- package/lib/TreeKill.js +111 -0
- package/lib/Utility.js +304 -0
- package/lib/VersionCheck.js +46 -0
- package/lib/Watcher.js +117 -0
- package/lib/Worker.js +200 -0
- package/lib/binaries/CLI.js +1092 -0
- package/lib/binaries/DevCLI.js +183 -0
- package/lib/binaries/Runtime.js +101 -0
- package/lib/binaries/Runtime4Docker.js +192 -0
- package/lib/completion.js +229 -0
- package/lib/completion.sh +40 -0
- package/lib/motd +36 -0
- package/lib/templates/Dockerfiles/Dockerfile-nodejs.tpl +8 -0
- package/lib/templates/ecosystem-es.tpl +24 -0
- package/lib/templates/ecosystem-simple-es.tpl +8 -0
- package/lib/templates/ecosystem-simple.tpl +6 -0
- package/lib/templates/ecosystem.tpl +22 -0
- package/lib/templates/init-scripts/launchd.tpl +35 -0
- package/lib/templates/init-scripts/openrc.tpl +52 -0
- package/lib/templates/init-scripts/pm2-init-amazon.sh +86 -0
- package/lib/templates/init-scripts/rcd-openbsd.tpl +41 -0
- package/lib/templates/init-scripts/rcd.tpl +44 -0
- package/lib/templates/init-scripts/smf.tpl +43 -0
- package/lib/templates/init-scripts/systemd-online.tpl +22 -0
- package/lib/templates/init-scripts/systemd.tpl +22 -0
- package/lib/templates/init-scripts/upstart.tpl +103 -0
- package/lib/templates/logrotate.d/pm2 +10 -0
- package/lib/templates/sample-apps/http-server/README.md +14 -0
- package/lib/templates/sample-apps/http-server/api.js +9 -0
- package/lib/templates/sample-apps/http-server/ecosystem.config.js +14 -0
- package/lib/templates/sample-apps/http-server/package.json +11 -0
- package/lib/templates/sample-apps/pm2-plus-metrics-actions/README.md +45 -0
- package/lib/templates/sample-apps/pm2-plus-metrics-actions/custom-metrics.js +66 -0
- package/lib/templates/sample-apps/pm2-plus-metrics-actions/ecosystem.config.js +12 -0
- package/lib/templates/sample-apps/pm2-plus-metrics-actions/package.json +11 -0
- package/lib/templates/sample-apps/python-app/README.md +4 -0
- package/lib/templates/sample-apps/python-app/echo.py +7 -0
- package/lib/templates/sample-apps/python-app/ecosystem.config.js +12 -0
- package/lib/templates/sample-apps/python-app/package.json +11 -0
- package/lib/tools/Config.js +251 -0
- package/lib/tools/SysMetrics.js +375 -0
- package/lib/tools/copydirSync.js +101 -0
- package/lib/tools/deleteFolderRecursive.js +19 -0
- package/lib/tools/find-package-json.js +74 -0
- package/lib/tools/fmt.js +72 -0
- package/lib/tools/interpreter.js +88 -0
- package/lib/tools/isbinaryfile.js +94 -0
- package/lib/tools/json5.js +752 -0
- package/lib/tools/multimeter/bar.js +111 -0
- package/lib/tools/multimeter/charm/encode.js +18 -0
- package/lib/tools/multimeter/charm/index.js +305 -0
- package/lib/tools/multimeter/index.js +76 -0
- package/lib/tools/open.js +56 -0
- package/lib/tools/passwd.js +58 -0
- package/lib/tools/pm2-config-keys.js +165 -0
- package/lib/tools/prompt.js +76 -0
- package/lib/tools/serviceStop.js +149 -0
- package/lib/tools/sexec.js +55 -0
- package/lib/tools/treeify.js +113 -0
- package/lib/tools/typestrip.js +55 -0
- package/lib/tools/which.js +120 -0
- package/lib/tools/xdg-open +861 -0
- package/modules/blessed/LICENSE +20 -0
- package/modules/blessed/README.md +34 -0
- package/modules/blessed/index.js +1 -0
- package/modules/blessed/lib/alias.js +526 -0
- package/modules/blessed/lib/blessed.js +32 -0
- package/modules/blessed/lib/colors.js +530 -0
- package/modules/blessed/lib/events.js +189 -0
- package/modules/blessed/lib/gpmclient.js +221 -0
- package/modules/blessed/lib/helpers.js +165 -0
- package/modules/blessed/lib/keys.js +339 -0
- package/modules/blessed/lib/program.js +4297 -0
- package/modules/blessed/lib/tput.js +3022 -0
- package/modules/blessed/lib/unicode.js +885 -0
- package/modules/blessed/lib/widget.js +32 -0
- package/modules/blessed/lib/widgets/box.js +34 -0
- package/modules/blessed/lib/widgets/element.js +2570 -0
- package/modules/blessed/lib/widgets/list.js +599 -0
- package/modules/blessed/lib/widgets/log.js +83 -0
- package/modules/blessed/lib/widgets/node.js +282 -0
- package/modules/blessed/lib/widgets/screen.js +2298 -0
- package/modules/blessed/lib/widgets/scrollablebox.js +389 -0
- package/modules/blessed/lib/widgets/scrollabletext.js +35 -0
- package/modules/blessed/lib/widgets/text.js +35 -0
- package/modules/blessed/usr/linux +0 -0
- package/modules/blessed/usr/windows-ansi +0 -0
- package/modules/blessed/usr/xterm +0 -0
- package/modules/blessed/usr/xterm-256color +0 -0
- package/modules/blessed/usr/xterm.termcap +243 -0
- package/modules/chokidar/LICENSE +21 -0
- package/modules/chokidar/VENDORED.md +76 -0
- package/modules/chokidar/index.js +979 -0
- package/modules/chokidar/lib/brace-expand.js +180 -0
- package/modules/chokidar/lib/constants.js +66 -0
- package/modules/chokidar/lib/fsevents-handler.js +526 -0
- package/modules/chokidar/lib/nodefs-handler.js +654 -0
- package/modules/fclone.js +49 -0
- package/modules/pm2-axon/index.js +2 -0
- package/modules/pm2-axon/lib/configurable/History.md +5 -0
- package/modules/pm2-axon/lib/configurable/Makefile +7 -0
- package/modules/pm2-axon/lib/configurable/Readme.md +57 -0
- package/modules/pm2-axon/lib/configurable/index.js +2 -0
- package/modules/pm2-axon/lib/configurable/lib/configurable.js +47 -0
- package/modules/pm2-axon/lib/configurable/package.json +68 -0
- package/modules/pm2-axon/lib/index.js +44 -0
- package/modules/pm2-axon/lib/plugins/queue.js +71 -0
- package/modules/pm2-axon/lib/plugins/round-robin.js +55 -0
- package/modules/pm2-axon/lib/sockets/pub-emitter.js +26 -0
- package/modules/pm2-axon/lib/sockets/pub.js +77 -0
- package/modules/pm2-axon/lib/sockets/pull.js +37 -0
- package/modules/pm2-axon/lib/sockets/push.js +32 -0
- package/modules/pm2-axon/lib/sockets/rep.js +73 -0
- package/modules/pm2-axon/lib/sockets/req.js +101 -0
- package/modules/pm2-axon/lib/sockets/sock.js +430 -0
- package/modules/pm2-axon/lib/sockets/sub-emitter.js +92 -0
- package/modules/pm2-axon/lib/sockets/sub.js +147 -0
- package/modules/pm2-axon/lib/utils.js +19 -0
- package/modules/pm2-axon-rpc/index.js +3 -0
- package/modules/pm2-axon-rpc/lib/client.js +63 -0
- package/modules/pm2-axon-rpc/lib/server.js +139 -0
- package/modules/pm2-io-agent/LICENSE +661 -0
- package/modules/pm2-io-agent/README.md +62 -0
- package/modules/pm2-io-agent/config.js +30 -0
- package/modules/pm2-io-agent/constants.js +118 -0
- package/modules/pm2-io-agent/index.js +2 -0
- package/modules/pm2-io-agent/src/InteractorClient.js +513 -0
- package/modules/pm2-io-agent/src/InteractorDaemon.js +455 -0
- package/modules/pm2-io-agent/src/PM2Client.js +112 -0
- package/modules/pm2-io-agent/src/PM2Interface.js +180 -0
- package/modules/pm2-io-agent/src/TransporterInterface.js +173 -0
- package/modules/pm2-io-agent/src/Utility.js +392 -0
- package/modules/pm2-io-agent/src/WatchDog.js +70 -0
- package/modules/pm2-io-agent/src/push/DataRetriever.js +77 -0
- package/modules/pm2-io-agent/src/push/PushInteractor.js +206 -0
- package/modules/pm2-io-agent/src/push/TransactionAggregator.js +670 -0
- package/modules/pm2-io-agent/src/reverse/ReverseInteractor.js +128 -0
- package/modules/pm2-io-agent/src/transporters/Transporter.js +129 -0
- package/modules/pm2-io-agent/src/transporters/WebsocketTransport.js +202 -0
- package/modules/pm2-io-agent/src/utils/BinaryHeap.js +135 -0
- package/modules/pm2-io-agent/src/utils/EDS.js +111 -0
- package/modules/pm2-io-agent/src/utils/probes/Histogram.js +204 -0
- package/modules/pm2-io-agent/src/utils/units.js +10 -0
- package/modules/pm2-io-bpm/configuration.js +141 -0
- package/modules/pm2-io-bpm/constants.js +10 -0
- package/modules/pm2-io-bpm/featureManager.js +110 -0
- package/modules/pm2-io-bpm/features/dependencies.js +48 -0
- package/modules/pm2-io-bpm/features/events.js +50 -0
- package/modules/pm2-io-bpm/features/metrics.js +102 -0
- package/modules/pm2-io-bpm/features/notify.js +238 -0
- package/modules/pm2-io-bpm/features/profiling.js +70 -0
- package/modules/pm2-io-bpm/features/tracing.js +174 -0
- package/modules/pm2-io-bpm/index.js +11 -0
- package/modules/pm2-io-bpm/metrics/eventLoopMetrics.js +136 -0
- package/modules/pm2-io-bpm/metrics/httpMetrics.js +175 -0
- package/modules/pm2-io-bpm/metrics/network.js +132 -0
- package/modules/pm2-io-bpm/metrics/runtime.js +161 -0
- package/modules/pm2-io-bpm/metrics/v8.js +104 -0
- package/modules/pm2-io-bpm/otel/constants.js +7 -0
- package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/platform/index.js +17 -0
- package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/platform/node/index.js +17 -0
- package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/platform/node/util.js +51 -0
- package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/transform.js +137 -0
- package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/types.js +24 -0
- package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/utils.js +23 -0
- package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/zipkin.js +149 -0
- package/modules/pm2-io-bpm/pmx.js +380 -0
- package/modules/pm2-io-bpm/profilers/addonProfiler.js +193 -0
- package/modules/pm2-io-bpm/profilers/inspectorProfiler.js +302 -0
- package/modules/pm2-io-bpm/serviceManager.js +21 -0
- package/modules/pm2-io-bpm/services/actions.js +159 -0
- package/modules/pm2-io-bpm/services/inspector.js +43 -0
- package/modules/pm2-io-bpm/services/metrics.js +186 -0
- package/modules/pm2-io-bpm/services/runtimeStats.js +54 -0
- package/modules/pm2-io-bpm/services/transport.js +11 -0
- package/modules/pm2-io-bpm/transports/IPCTransport.js +115 -0
- package/modules/pm2-io-bpm/utils/BinaryHeap.js +134 -0
- package/modules/pm2-io-bpm/utils/EDS.js +113 -0
- package/modules/pm2-io-bpm/utils/EWMA.js +32 -0
- package/modules/pm2-io-bpm/utils/autocast.js +61 -0
- package/modules/pm2-io-bpm/utils/metrics/counter.js +33 -0
- package/modules/pm2-io-bpm/utils/metrics/gauge.js +23 -0
- package/modules/pm2-io-bpm/utils/metrics/histogram.js +192 -0
- package/modules/pm2-io-bpm/utils/metrics/meter.js +46 -0
- package/modules/pm2-io-bpm/utils/miscellaneous.js +19 -0
- package/modules/pm2-io-bpm/utils/module.js +67 -0
- package/modules/pm2-io-bpm/utils/shimmer.js +19 -0
- package/modules/pm2-io-bpm/utils/stackParser.js +160 -0
- package/modules/pm2-io-bpm/utils/transactionAggregator.js +429 -0
- package/modules/pm2-io-bpm/utils/units.js +16 -0
- package/modules/vizion/LICENSE +13 -0
- package/modules/vizion/README.md +133 -0
- package/modules/vizion/index.js +1 -0
- package/modules/vizion/lib/git/git.js +247 -0
- package/modules/vizion/lib/vizion.js +52 -0
- package/package.json +170 -4
- package/paths.js +121 -0
- package/pm2 +11 -0
- package/types/index.d.ts +732 -0
- package/types/tsconfig.json +14 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
|
|
2
|
+
# pm2 custom metrics boilerplate
|
|
3
|
+
|
|
4
|
+
In this boilerplate you will discover a working example of custom metrics feature.
|
|
5
|
+
|
|
6
|
+
Metrics covered are:
|
|
7
|
+
- io.metric
|
|
8
|
+
- io.counter
|
|
9
|
+
- io.meter
|
|
10
|
+
- io.histogram
|
|
11
|
+
|
|
12
|
+
## What is Custom Metrics?
|
|
13
|
+
|
|
14
|
+
Custom metrics is a powerfull way to get more visibility from a running application. It will allow you to monitor in realtime the current value of variables, know the number of actions being processed, measure latency and much more.
|
|
15
|
+
|
|
16
|
+
Once you have plugged in some custom metrics you will be able to monitor their value in realtime with
|
|
17
|
+
|
|
18
|
+
`pm2 monit`
|
|
19
|
+
|
|
20
|
+
Or
|
|
21
|
+
|
|
22
|
+
`pm2 describe`
|
|
23
|
+
|
|
24
|
+
Or on the PM2+ Web interface
|
|
25
|
+
|
|
26
|
+
`pm2 open`
|
|
27
|
+
|
|
28
|
+
## Example
|
|
29
|
+
|
|
30
|
+
```javascript
|
|
31
|
+
const io = require('@pm2/io')
|
|
32
|
+
|
|
33
|
+
const currentReq = io.counter({
|
|
34
|
+
name: 'CM: Current Processing',
|
|
35
|
+
type: 'counter'
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
setInterval(() => {
|
|
39
|
+
currentReq.inc()
|
|
40
|
+
}, 1000)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Documentation
|
|
44
|
+
|
|
45
|
+
https://doc.pm2.io/en/plus/guide/custom-metrics/
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
|
|
2
|
+
const io = require('@pm2/io')
|
|
3
|
+
|
|
4
|
+
// Straight Metric
|
|
5
|
+
var user_count = 10
|
|
6
|
+
|
|
7
|
+
const users = io.metric({
|
|
8
|
+
name: 'CM: Realtime user',
|
|
9
|
+
value: () => {
|
|
10
|
+
return user_count
|
|
11
|
+
}
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
// or users.set(user_count)
|
|
15
|
+
|
|
16
|
+
// Counter (.inc() .dec())
|
|
17
|
+
const currentReq = io.counter({
|
|
18
|
+
name: 'CM: Current Processing',
|
|
19
|
+
type: 'counter'
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
setInterval(() => {
|
|
23
|
+
currentReq.inc()
|
|
24
|
+
}, 1000)
|
|
25
|
+
|
|
26
|
+
// Meter
|
|
27
|
+
const reqsec = io.meter({
|
|
28
|
+
name: 'CM: req/sec'
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
setInterval(() => {
|
|
32
|
+
reqsec.mark()
|
|
33
|
+
}, 100)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
// Histogram
|
|
37
|
+
const latency = io.histogram({
|
|
38
|
+
name: 'CM: latency'
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
var latencyValue = 0;
|
|
42
|
+
|
|
43
|
+
setInterval(() => {
|
|
44
|
+
latencyValue = Math.round(Math.random() * 100);
|
|
45
|
+
latency.update(latencyValue);
|
|
46
|
+
}, 100)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
////////////////////
|
|
50
|
+
// Custom Actions //
|
|
51
|
+
////////////////////
|
|
52
|
+
|
|
53
|
+
io.action('add user', (done) => {
|
|
54
|
+
user_count++
|
|
55
|
+
done({success:true})
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
io.action('remove user', (done) => {
|
|
59
|
+
user_count++
|
|
60
|
+
done({success:true})
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
io.action('with params', (arg, done) => {
|
|
64
|
+
console.log(arg)
|
|
65
|
+
done({success:arg})
|
|
66
|
+
})
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pm2-plus-custom-metrics",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Example that shows how to use pm2+ custom metrics",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"author": "",
|
|
10
|
+
"license": "ISC"
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "python-app",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Example on how to manage a Python application with PM2",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"author": "",
|
|
10
|
+
"license": "ISC"
|
|
11
|
+
}
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2013-present the PM2 project authors. All rights reserved.
|
|
3
|
+
* Use of this source code is governed by a license that
|
|
4
|
+
* can be found in the LICENSE file.
|
|
5
|
+
*/
|
|
6
|
+
var util = require('util');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Validator of configured file / commander options.
|
|
10
|
+
*/
|
|
11
|
+
var Config = module.exports = {
|
|
12
|
+
_errMsgs: {
|
|
13
|
+
'require': '"%s" is required',
|
|
14
|
+
'type' : 'Expect "%s" to be a typeof %s, but now is %s',
|
|
15
|
+
'regex' : 'Verify "%s" with regex failed, %s',
|
|
16
|
+
'max' : 'The maximum of "%s" is %s, but now is %s',
|
|
17
|
+
'min' : 'The minimum of "%s" is %s, but now is %s'
|
|
18
|
+
},
|
|
19
|
+
/**
|
|
20
|
+
* Schema definition.
|
|
21
|
+
* @returns {exports|*}
|
|
22
|
+
*/
|
|
23
|
+
get schema(){
|
|
24
|
+
// Cache.
|
|
25
|
+
if (this._schema) {
|
|
26
|
+
return this._schema;
|
|
27
|
+
}
|
|
28
|
+
// Render aliases.
|
|
29
|
+
this._schema = require('../API/schema');
|
|
30
|
+
for (var k in this._schema) {
|
|
31
|
+
if (k.indexOf('\\') > 0) {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
var aliases = [
|
|
35
|
+
k.split('_').map(function(n, i){
|
|
36
|
+
if (i != 0 && n && n.length > 1) {
|
|
37
|
+
return n[0].toUpperCase() + n.slice(1);
|
|
38
|
+
}
|
|
39
|
+
return n;
|
|
40
|
+
}).join('')
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
if (this._schema[k].alias && Array.isArray(this._schema[k].alias)) {
|
|
44
|
+
// If multiple aliases, merge
|
|
45
|
+
this._schema[k].alias.forEach(function(alias) {
|
|
46
|
+
aliases.splice(0, 0, alias);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
else if (this._schema[k].alias)
|
|
50
|
+
aliases.splice(0, 0, this._schema[k].alias);
|
|
51
|
+
|
|
52
|
+
this._schema[k].alias = aliases;
|
|
53
|
+
}
|
|
54
|
+
return this._schema;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Filter / Alias options
|
|
60
|
+
*/
|
|
61
|
+
Config.filterOptions = function(cmd) {
|
|
62
|
+
var conf = {};
|
|
63
|
+
var schema = this.schema;
|
|
64
|
+
|
|
65
|
+
for (var key in schema) {
|
|
66
|
+
var aliases = schema[key].alias;
|
|
67
|
+
aliases && aliases.forEach(function(alias){
|
|
68
|
+
if (typeof(cmd[alias]) !== 'undefined') {
|
|
69
|
+
conf[key] || (conf[key] = cmd[alias]);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return conf;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Verify JSON configurations.
|
|
79
|
+
* @param {Object} json
|
|
80
|
+
* @returns {{errors: Array, config: {}}}
|
|
81
|
+
*/
|
|
82
|
+
Config.validateJSON = function(json){
|
|
83
|
+
// clone config
|
|
84
|
+
var conf = Object.assign({}, json),
|
|
85
|
+
res = {};
|
|
86
|
+
this._errors = [];
|
|
87
|
+
|
|
88
|
+
var regexKeys = {}, defines = this.schema;
|
|
89
|
+
|
|
90
|
+
for (var sk in defines) {
|
|
91
|
+
// Pick up RegExp keys.
|
|
92
|
+
if (sk.indexOf('\\') >= 0) {
|
|
93
|
+
regexKeys[sk] = false;
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
var aliases = defines[sk].alias;
|
|
98
|
+
|
|
99
|
+
aliases && aliases.forEach(function(alias){
|
|
100
|
+
conf[sk] || (conf[sk] = json[alias]);
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
var val = conf[sk];
|
|
104
|
+
delete conf[sk];
|
|
105
|
+
|
|
106
|
+
// Validate key-value pairs.
|
|
107
|
+
if (val === undefined ||
|
|
108
|
+
val === null ||
|
|
109
|
+
((val = this._valid(sk, val)) === null)) {
|
|
110
|
+
|
|
111
|
+
// If value is not defined
|
|
112
|
+
// Set default value (via schema.json)
|
|
113
|
+
if (typeof(defines[sk].default) !== 'undefined')
|
|
114
|
+
res[sk] = defines[sk].default;
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
//console.log(sk, val, val === null, val === undefined);
|
|
118
|
+
res[sk] = val;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Validate RegExp values.
|
|
122
|
+
var hasRegexKey = false;
|
|
123
|
+
for (var k in regexKeys) {
|
|
124
|
+
hasRegexKey = true;
|
|
125
|
+
regexKeys[k] = new RegExp(k);
|
|
126
|
+
}
|
|
127
|
+
if (hasRegexKey) {
|
|
128
|
+
for (var k in conf) {
|
|
129
|
+
for (var rk in regexKeys) {
|
|
130
|
+
if (regexKeys[rk].test(k))
|
|
131
|
+
if (this._valid(k, conf[k], defines[rk])) {
|
|
132
|
+
res[k] = conf[k];
|
|
133
|
+
delete conf[k];
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return {errors: this._errors, config: res};
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Validate key-value pairs by specific schema
|
|
144
|
+
* @param {String} key
|
|
145
|
+
* @param {Mixed} value
|
|
146
|
+
* @param {Object} sch
|
|
147
|
+
* @returns {*}
|
|
148
|
+
* @private
|
|
149
|
+
*/
|
|
150
|
+
Config._valid = function(key, value, sch){
|
|
151
|
+
var sch = sch || this.schema[key],
|
|
152
|
+
scht = typeof sch.type == 'string' ? [sch.type] : sch.type;
|
|
153
|
+
|
|
154
|
+
// Required value.
|
|
155
|
+
var undef = typeof value == 'undefined';
|
|
156
|
+
if(this._error(sch.require && undef, 'require', key)){
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// If undefined, make a break.
|
|
161
|
+
if (undef) {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Wrap schema types.
|
|
166
|
+
scht = scht.map(function(t){
|
|
167
|
+
return '[object ' + t[0].toUpperCase() + t.slice(1) + ']'
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// Typeof value.
|
|
171
|
+
var type = Object.prototype.toString.call(value), nt = '[object Number]';
|
|
172
|
+
|
|
173
|
+
// Auto parse Number
|
|
174
|
+
if (type != '[object Boolean]' && scht.indexOf(nt) >= 0 && !isNaN(value)) {
|
|
175
|
+
value = parseFloat(value);
|
|
176
|
+
type = nt;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Verify types.
|
|
180
|
+
if (this._error(!~scht.indexOf(type), 'type', key, scht.join(' / '), type)) {
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Verify RegExp if exists.
|
|
185
|
+
if (this._error(type == '[object String]' && sch.regex && !(new RegExp(sch.regex)).test(value),
|
|
186
|
+
'regex', key, sch.desc || ('should match ' + sch.regex))) {
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Verify maximum / minimum of Number value.
|
|
191
|
+
if (type == '[object Number]') {
|
|
192
|
+
if (this._error(typeof sch.max != 'undefined' && value > sch.max, 'max', key, sch.max, value)) {
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
if (this._error(typeof sch.min != 'undefined' && value < sch.min, 'min', key, sch.min, value)) {
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// If first type is Array, but current is String, try to split them.
|
|
201
|
+
if(scht.length > 1 && type != scht[0] && type == '[object String]'){
|
|
202
|
+
if(scht[0] == '[object Array]') {
|
|
203
|
+
value = (value.match(/[^\s"'=]+="[^"]*"|[^\s"'=]+='[^']*'|"([^"]*)"|'([^']*)'|\S+/g) || [])
|
|
204
|
+
.map(function(v) {
|
|
205
|
+
if (v && v[0] === '"' && v[v.length - 1] === '"') return v.slice(1, -1);
|
|
206
|
+
if (v && v[0] === "'" && v[v.length - 1] === "'") return v.slice(1, -1);
|
|
207
|
+
return v;
|
|
208
|
+
})
|
|
209
|
+
.filter(function(v) {
|
|
210
|
+
return v && v.trim();
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Custom types: sbyte && stime.
|
|
216
|
+
if(sch.ext_type && type == '[object String]' && value.length >= 2) {
|
|
217
|
+
var seed = {
|
|
218
|
+
'sbyte': {
|
|
219
|
+
'G': 1024 * 1024 * 1024,
|
|
220
|
+
'M': 1024 * 1024,
|
|
221
|
+
'K': 1024
|
|
222
|
+
},
|
|
223
|
+
'stime': {
|
|
224
|
+
'h': 60 * 60 * 1000,
|
|
225
|
+
'm': 60 * 1000,
|
|
226
|
+
's': 1000
|
|
227
|
+
}
|
|
228
|
+
}[sch.ext_type];
|
|
229
|
+
|
|
230
|
+
if(seed){
|
|
231
|
+
value = parseFloat(value.slice(0, -1)) * (seed[value.slice(-1)]);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return value;
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Wrap errors.
|
|
239
|
+
* @param {Boolean} possible A value indicates whether it is an error or not.
|
|
240
|
+
* @param {String} type
|
|
241
|
+
* @returns {*}
|
|
242
|
+
* @private
|
|
243
|
+
*/
|
|
244
|
+
Config._error = function(possible, type){
|
|
245
|
+
if (possible) {
|
|
246
|
+
var args = Array.prototype.slice.call(arguments);
|
|
247
|
+
args.splice(0, 2, this._errMsgs[type]);
|
|
248
|
+
this._errors && this._errors.push(util.format.apply(null, args));
|
|
249
|
+
}
|
|
250
|
+
return possible;
|
|
251
|
+
}
|