@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
package/lib/API/Serve.js
ADDED
|
@@ -0,0 +1,497 @@
|
|
|
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
|
+
'use strict';
|
|
7
|
+
|
|
8
|
+
var fs = require('fs');
|
|
9
|
+
var http = require('http');
|
|
10
|
+
var path = require('path');
|
|
11
|
+
var debug = require('debug')('pm2:serve');
|
|
12
|
+
|
|
13
|
+
// var probe = require('@pm2/io');
|
|
14
|
+
// var errorMeter = probe.meter({
|
|
15
|
+
// name : '404/sec',
|
|
16
|
+
// samples : 1,
|
|
17
|
+
// timeframe : 60
|
|
18
|
+
// })
|
|
19
|
+
/**
|
|
20
|
+
* list of supported content types.
|
|
21
|
+
*/
|
|
22
|
+
var contentTypes = {
|
|
23
|
+
'3gp': 'video/3gpp',
|
|
24
|
+
'a': 'application/octet-stream',
|
|
25
|
+
'ai': 'application/postscript',
|
|
26
|
+
'aif': 'audio/x-aiff',
|
|
27
|
+
'aiff': 'audio/x-aiff',
|
|
28
|
+
'asc': 'application/pgp-signature',
|
|
29
|
+
'asf': 'video/x-ms-asf',
|
|
30
|
+
'asm': 'text/x-asm',
|
|
31
|
+
'asx': 'video/x-ms-asf',
|
|
32
|
+
'atom': 'application/atom+xml',
|
|
33
|
+
'au': 'audio/basic',
|
|
34
|
+
'avi': 'video/x-msvideo',
|
|
35
|
+
'bat': 'application/x-msdownload',
|
|
36
|
+
'bin': 'application/octet-stream',
|
|
37
|
+
'bmp': 'image/bmp',
|
|
38
|
+
'bz2': 'application/x-bzip2',
|
|
39
|
+
'c': 'text/x-c',
|
|
40
|
+
'cab': 'application/vnd.ms-cab-compressed',
|
|
41
|
+
'cc': 'text/x-c',
|
|
42
|
+
'chm': 'application/vnd.ms-htmlhelp',
|
|
43
|
+
'class': 'application/octet-stream',
|
|
44
|
+
'com': 'application/x-msdownload',
|
|
45
|
+
'conf': 'text/plain',
|
|
46
|
+
'cpp': 'text/x-c',
|
|
47
|
+
'crt': 'application/x-x509-ca-cert',
|
|
48
|
+
'css': 'text/css',
|
|
49
|
+
'csv': 'text/csv',
|
|
50
|
+
'cxx': 'text/x-c',
|
|
51
|
+
'deb': 'application/x-debian-package',
|
|
52
|
+
'der': 'application/x-x509-ca-cert',
|
|
53
|
+
'diff': 'text/x-diff',
|
|
54
|
+
'djv': 'image/vnd.djvu',
|
|
55
|
+
'djvu': 'image/vnd.djvu',
|
|
56
|
+
'dll': 'application/x-msdownload',
|
|
57
|
+
'dmg': 'application/octet-stream',
|
|
58
|
+
'doc': 'application/msword',
|
|
59
|
+
'dot': 'application/msword',
|
|
60
|
+
'dtd': 'application/xml-dtd',
|
|
61
|
+
'dvi': 'application/x-dvi',
|
|
62
|
+
'ear': 'application/java-archive',
|
|
63
|
+
'eml': 'message/rfc822',
|
|
64
|
+
'eps': 'application/postscript',
|
|
65
|
+
'exe': 'application/x-msdownload',
|
|
66
|
+
'f': 'text/x-fortran',
|
|
67
|
+
'f77': 'text/x-fortran',
|
|
68
|
+
'f90': 'text/x-fortran',
|
|
69
|
+
'flv': 'video/x-flv',
|
|
70
|
+
'for': 'text/x-fortran',
|
|
71
|
+
'gem': 'application/octet-stream',
|
|
72
|
+
'gemspec': 'text/x-script.ruby',
|
|
73
|
+
'gif': 'image/gif',
|
|
74
|
+
'gz': 'application/x-gzip',
|
|
75
|
+
'h': 'text/x-c',
|
|
76
|
+
'hh': 'text/x-c',
|
|
77
|
+
'htm': 'text/html',
|
|
78
|
+
'html': 'text/html',
|
|
79
|
+
'ico': 'image/vnd.microsoft.icon',
|
|
80
|
+
'ics': 'text/calendar',
|
|
81
|
+
'ifb': 'text/calendar',
|
|
82
|
+
'iso': 'application/octet-stream',
|
|
83
|
+
'jar': 'application/java-archive',
|
|
84
|
+
'java': 'text/x-java-source',
|
|
85
|
+
'jnlp': 'application/x-java-jnlp-file',
|
|
86
|
+
'jpeg': 'image/jpeg',
|
|
87
|
+
'jpg': 'image/jpeg',
|
|
88
|
+
'js': 'application/javascript',
|
|
89
|
+
'json': 'application/json',
|
|
90
|
+
'log': 'text/plain',
|
|
91
|
+
'm3u': 'audio/x-mpegurl',
|
|
92
|
+
'm4v': 'video/mp4',
|
|
93
|
+
'man': 'text/troff',
|
|
94
|
+
'mathml': 'application/mathml+xml',
|
|
95
|
+
'mbox': 'application/mbox',
|
|
96
|
+
'mdoc': 'text/troff',
|
|
97
|
+
'me': 'text/troff',
|
|
98
|
+
'mid': 'audio/midi',
|
|
99
|
+
'midi': 'audio/midi',
|
|
100
|
+
'mime': 'message/rfc822',
|
|
101
|
+
'mml': 'application/mathml+xml',
|
|
102
|
+
'mng': 'video/x-mng',
|
|
103
|
+
'mov': 'video/quicktime',
|
|
104
|
+
'mp3': 'audio/mpeg',
|
|
105
|
+
'mp4': 'video/mp4',
|
|
106
|
+
'mp4v': 'video/mp4',
|
|
107
|
+
'mpeg': 'video/mpeg',
|
|
108
|
+
'mpg': 'video/mpeg',
|
|
109
|
+
'ms': 'text/troff',
|
|
110
|
+
'msi': 'application/x-msdownload',
|
|
111
|
+
'odp': 'application/vnd.oasis.opendocument.presentation',
|
|
112
|
+
'ods': 'application/vnd.oasis.opendocument.spreadsheet',
|
|
113
|
+
'odt': 'application/vnd.oasis.opendocument.text',
|
|
114
|
+
'ogg': 'application/ogg',
|
|
115
|
+
'p': 'text/x-pascal',
|
|
116
|
+
'pas': 'text/x-pascal',
|
|
117
|
+
'pbm': 'image/x-portable-bitmap',
|
|
118
|
+
'pdf': 'application/pdf',
|
|
119
|
+
'pem': 'application/x-x509-ca-cert',
|
|
120
|
+
'pgm': 'image/x-portable-graymap',
|
|
121
|
+
'pgp': 'application/pgp-encrypted',
|
|
122
|
+
'pkg': 'application/octet-stream',
|
|
123
|
+
'pl': 'text/x-script.perl',
|
|
124
|
+
'pm': 'text/x-script.perl-module',
|
|
125
|
+
'png': 'image/png',
|
|
126
|
+
'pnm': 'image/x-portable-anymap',
|
|
127
|
+
'ppm': 'image/x-portable-pixmap',
|
|
128
|
+
'pps': 'application/vnd.ms-powerpoint',
|
|
129
|
+
'ppt': 'application/vnd.ms-powerpoint',
|
|
130
|
+
'ps': 'application/postscript',
|
|
131
|
+
'psd': 'image/vnd.adobe.photoshop',
|
|
132
|
+
'py': 'text/x-script.python',
|
|
133
|
+
'qt': 'video/quicktime',
|
|
134
|
+
'ra': 'audio/x-pn-realaudio',
|
|
135
|
+
'rake': 'text/x-script.ruby',
|
|
136
|
+
'ram': 'audio/x-pn-realaudio',
|
|
137
|
+
'rar': 'application/x-rar-compressed',
|
|
138
|
+
'rb': 'text/x-script.ruby',
|
|
139
|
+
'rdf': 'application/rdf+xml',
|
|
140
|
+
'roff': 'text/troff',
|
|
141
|
+
'rpm': 'application/x-redhat-package-manager',
|
|
142
|
+
'rss': 'application/rss+xml',
|
|
143
|
+
'rtf': 'application/rtf',
|
|
144
|
+
'ru': 'text/x-script.ruby',
|
|
145
|
+
's': 'text/x-asm',
|
|
146
|
+
'sgm': 'text/sgml',
|
|
147
|
+
'sgml': 'text/sgml',
|
|
148
|
+
'sh': 'application/x-sh',
|
|
149
|
+
'sig': 'application/pgp-signature',
|
|
150
|
+
'snd': 'audio/basic',
|
|
151
|
+
'so': 'application/octet-stream',
|
|
152
|
+
'svg': 'image/svg+xml',
|
|
153
|
+
'svgz': 'image/svg+xml',
|
|
154
|
+
'swf': 'application/x-shockwave-flash',
|
|
155
|
+
't': 'text/troff',
|
|
156
|
+
'tar': 'application/x-tar',
|
|
157
|
+
'tbz': 'application/x-bzip-compressed-tar',
|
|
158
|
+
'tcl': 'application/x-tcl',
|
|
159
|
+
'tex': 'application/x-tex',
|
|
160
|
+
'texi': 'application/x-texinfo',
|
|
161
|
+
'texinfo': 'application/x-texinfo',
|
|
162
|
+
'text': 'text/plain',
|
|
163
|
+
'tif': 'image/tiff',
|
|
164
|
+
'tiff': 'image/tiff',
|
|
165
|
+
'torrent': 'application/x-bittorrent',
|
|
166
|
+
'tr': 'text/troff',
|
|
167
|
+
'txt': 'text/plain',
|
|
168
|
+
'vcf': 'text/x-vcard',
|
|
169
|
+
'vcs': 'text/x-vcalendar',
|
|
170
|
+
'vrml': 'model/vrml',
|
|
171
|
+
'war': 'application/java-archive',
|
|
172
|
+
'wav': 'audio/x-wav',
|
|
173
|
+
'webp': 'image/webp',
|
|
174
|
+
'wma': 'audio/x-ms-wma',
|
|
175
|
+
'wmv': 'video/x-ms-wmv',
|
|
176
|
+
'wmx': 'video/x-ms-wmx',
|
|
177
|
+
'wrl': 'model/vrml',
|
|
178
|
+
'wsdl': 'application/wsdl+xml',
|
|
179
|
+
'xbm': 'image/x-xbitmap',
|
|
180
|
+
'xhtml': 'application/xhtml+xml',
|
|
181
|
+
'xls': 'application/vnd.ms-excel',
|
|
182
|
+
'xml': 'application/xml',
|
|
183
|
+
'xpm': 'image/x-xpixmap',
|
|
184
|
+
'xsl': 'application/xml',
|
|
185
|
+
'xslt': 'application/xslt+xml',
|
|
186
|
+
'yaml': 'text/yaml',
|
|
187
|
+
'yml': 'text/yaml',
|
|
188
|
+
'zip': 'application/zip',
|
|
189
|
+
'woff': 'application/font-woff',
|
|
190
|
+
'woff2': 'application/font-woff',
|
|
191
|
+
'otf': 'application/font-sfnt',
|
|
192
|
+
'otc': 'application/font-sfnt',
|
|
193
|
+
'ttf': 'application/font-sfnt'
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
var options = {
|
|
197
|
+
port: process.env.PM2_SERVE_PORT || process.argv[3] || 8080,
|
|
198
|
+
host: process.env.PM2_SERVE_HOST || process.argv[4] || '0.0.0.0',
|
|
199
|
+
path: path.resolve(process.env.PM2_SERVE_PATH || process.argv[2] || '.'),
|
|
200
|
+
spa: process.env.PM2_SERVE_SPA === 'true',
|
|
201
|
+
ftp: process.env.PM2_SERVE_FTP === 'true',
|
|
202
|
+
homepage: process.env.PM2_SERVE_HOMEPAGE || '/index.html',
|
|
203
|
+
basic_auth: process.env.PM2_SERVE_BASIC_AUTH === 'true' ? {
|
|
204
|
+
username: process.env.PM2_SERVE_BASIC_AUTH_USERNAME,
|
|
205
|
+
password: process.env.PM2_SERVE_BASIC_AUTH_PASSWORD
|
|
206
|
+
} : null,
|
|
207
|
+
monitor: process.env.PM2_SERVE_MONITOR
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
if (typeof options.port === 'string') {
|
|
211
|
+
options.port = parseInt(options.port) || 8080
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (typeof options.monitor === 'string' && options.monitor !== '') {
|
|
215
|
+
try {
|
|
216
|
+
let fileContent = fs.readFileSync(path.join(process.env.PM2_HOME, 'agent.json5')).toString()
|
|
217
|
+
// Handle old configuration with json5
|
|
218
|
+
fileContent = fileContent.replace(/\s(\w+):/g, '"$1":')
|
|
219
|
+
// parse
|
|
220
|
+
let conf = JSON.parse(fileContent)
|
|
221
|
+
options.monitorBucket = conf.public_key
|
|
222
|
+
} catch (e) {
|
|
223
|
+
console.log('Interaction file does not exist')
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// start an HTTP server
|
|
228
|
+
http.createServer(function (request, response) {
|
|
229
|
+
if (options.basic_auth) {
|
|
230
|
+
if (!request.headers.authorization || request.headers.authorization.indexOf('Basic ') === -1) {
|
|
231
|
+
return sendBasicAuthResponse(response)
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
var user = parseBasicAuth(request.headers.authorization)
|
|
235
|
+
if (user.username !== options.basic_auth.username || user.password !== options.basic_auth.password) {
|
|
236
|
+
return sendBasicAuthResponse(response)
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
guard(request, response, serveFile)(request.url, request, response);
|
|
241
|
+
|
|
242
|
+
}).listen(options.port, options.host, function (err) {
|
|
243
|
+
if (err) {
|
|
244
|
+
console.error(err);
|
|
245
|
+
process.exit(1);
|
|
246
|
+
}
|
|
247
|
+
console.log('Exposing %s directory on %s:%d', options.path, options.host, options.port);
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
// Wrap a request handler or fs callback: an exception thrown there (sync or
|
|
251
|
+
// from an async fs callback) answers 500 instead of killing the server
|
|
252
|
+
function guard(request, response, fn) {
|
|
253
|
+
return function () {
|
|
254
|
+
try {
|
|
255
|
+
return fn.apply(this, arguments);
|
|
256
|
+
} catch (e) {
|
|
257
|
+
console.error('[%s] Error while handling %s : %s', new Date(), request.url, e.message || e);
|
|
258
|
+
if (!response.headersSent) response.writeHead(500, { 'Content-Type': 'text/html' });
|
|
259
|
+
response.end('500 Internal Server Error');
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function serveFile(uri, request, response) {
|
|
265
|
+
var file;
|
|
266
|
+
try {
|
|
267
|
+
file = decodeURIComponent(new URL(uri || request.url, 'http://localhost').pathname);
|
|
268
|
+
} catch (e) {
|
|
269
|
+
// malformed percent-encoding (e.g. GET /%, /%zz) throws URIError
|
|
270
|
+
file = null;
|
|
271
|
+
}
|
|
272
|
+
// a null byte (GET /%00, or /%2500 decoded again by the spa fallback)
|
|
273
|
+
// makes every fs call throw ERR_INVALID_ARG_VALUE
|
|
274
|
+
if (file === null || file.indexOf('\0') !== -1) {
|
|
275
|
+
response.writeHead(400, { 'Content-Type': 'text/html' });
|
|
276
|
+
return response.end('400 Bad Request');
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
var rootPath = path.resolve(options.path);
|
|
280
|
+
var filePath = path.resolve(rootPath + file);
|
|
281
|
+
|
|
282
|
+
// since we call filesystem directly so we need to verify that the
|
|
283
|
+
// url doesn't go outside the serve path
|
|
284
|
+
var relative = path.relative(rootPath, filePath);
|
|
285
|
+
if (relative !== '' && (relative.startsWith('..') || path.isAbsolute(relative))) {
|
|
286
|
+
response.writeHead(403, { 'Content-Type': 'text/html' });
|
|
287
|
+
return response.end('403 Forbidden');
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
fs.stat(filePath, guard(request, response, function (err, stats) {
|
|
291
|
+
if (!err && stats.isDirectory()) {
|
|
292
|
+
if (options.ftp) {
|
|
293
|
+
// In ftp mode, try index.html first, fall back to directory listing
|
|
294
|
+
var indexPath = path.join(filePath, 'index.html');
|
|
295
|
+
fs.access(indexPath, fs.constants.R_OK, guard(request, response, function (err) {
|
|
296
|
+
if (!err) {
|
|
297
|
+
return serveStaticFile(indexPath, file + '/index.html', request, response);
|
|
298
|
+
}
|
|
299
|
+
return serveDirectoryListing(filePath, file, response);
|
|
300
|
+
}));
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
// Not ftp mode: serve homepage
|
|
304
|
+
if (file === '/' || file === '') {
|
|
305
|
+
file = options.homepage;
|
|
306
|
+
request.wantHomepage = true;
|
|
307
|
+
filePath = path.resolve(options.path + file);
|
|
308
|
+
}
|
|
309
|
+
} else if (file === '/' || file === '') {
|
|
310
|
+
file = options.homepage;
|
|
311
|
+
request.wantHomepage = true;
|
|
312
|
+
filePath = path.resolve(options.path + file);
|
|
313
|
+
}
|
|
314
|
+
serveStaticFile(filePath, file, request, response);
|
|
315
|
+
}));
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function formatFileSize(bytes) {
|
|
319
|
+
if (bytes < 1024) return bytes + 'B';
|
|
320
|
+
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + 'K';
|
|
321
|
+
if (bytes < 1024 * 1024 * 1024) return (bytes / (1024 * 1024)).toFixed(1) + 'M';
|
|
322
|
+
return (bytes / (1024 * 1024 * 1024)).toFixed(1) + 'G';
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function serveDirectoryListing(dirPath, urlPath, response) {
|
|
326
|
+
fs.readdir(dirPath, function (err, entries) {
|
|
327
|
+
if (err) {
|
|
328
|
+
response.writeHead(500);
|
|
329
|
+
return response.end('Error reading directory');
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// Ensure urlPath ends with /
|
|
333
|
+
if (urlPath && urlPath[urlPath.length - 1] !== '/') urlPath += '/';
|
|
334
|
+
if (!urlPath) urlPath = '/';
|
|
335
|
+
|
|
336
|
+
var pending = entries.length;
|
|
337
|
+
var items = [];
|
|
338
|
+
|
|
339
|
+
if (pending === 0) {
|
|
340
|
+
return renderDirectoryListing(dirPath, urlPath, items, response);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
entries.forEach(function (entry) {
|
|
344
|
+
// Skip dotfiles
|
|
345
|
+
if (entry[0] === '.') {
|
|
346
|
+
if (--pending === 0) renderDirectoryListing(dirPath, urlPath, items, response);
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
fs.stat(path.join(dirPath, entry), function (err, stats) {
|
|
350
|
+
if (!err) {
|
|
351
|
+
items.push({
|
|
352
|
+
name: entry,
|
|
353
|
+
isDir: stats.isDirectory(),
|
|
354
|
+
size: stats.size,
|
|
355
|
+
mtime: stats.mtime
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
if (--pending === 0) renderDirectoryListing(dirPath, urlPath, items, response);
|
|
359
|
+
});
|
|
360
|
+
});
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function renderDirectoryListing(dirPath, urlPath, items, response) {
|
|
365
|
+
// Directories first, then files, alphabetical within each group
|
|
366
|
+
items.sort(function (a, b) {
|
|
367
|
+
if (a.isDir !== b.isDir) return a.isDir ? -1 : 1;
|
|
368
|
+
return a.name.localeCompare(b.name);
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
var escHtml = function(s) { return s.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"'); };
|
|
372
|
+
|
|
373
|
+
var rows = '';
|
|
374
|
+
|
|
375
|
+
// Parent directory link (unless at root)
|
|
376
|
+
if (urlPath !== '/') {
|
|
377
|
+
rows += '<tr><td class="name"><a href="../">../</a></td><td class="size">-</td><td class="mtime">-</td></tr>\n';
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
items.forEach(function (item) {
|
|
381
|
+
var displayName = item.isDir ? item.name + '/' : item.name;
|
|
382
|
+
var href = encodeURIComponent(item.name) + (item.isDir ? '/' : '');
|
|
383
|
+
var size = item.isDir ? '-' : formatFileSize(item.size);
|
|
384
|
+
var mtime = item.mtime.toISOString().replace('T', ' ').slice(0, 19);
|
|
385
|
+
rows += '<tr>'
|
|
386
|
+
+ '<td class="name"><a href="' + escHtml(href) + '">' + escHtml(displayName) + '</a></td>'
|
|
387
|
+
+ '<td class="size">' + size + '</td>'
|
|
388
|
+
+ '<td class="mtime">' + mtime + '</td>'
|
|
389
|
+
+ '</tr>\n';
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
var html = '<!DOCTYPE html>\n<html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width">'
|
|
393
|
+
+ '<title>Index of ' + escHtml(urlPath) + '</title>'
|
|
394
|
+
+ '<style>'
|
|
395
|
+
+ 'body{font-family:monospace;margin:20px;background:#1e1e1e;color:#ccc}'
|
|
396
|
+
+ 'h1{font-size:1.2em;border-bottom:1px solid #444;padding-bottom:8px}'
|
|
397
|
+
+ 'table{border-collapse:collapse;width:100%}'
|
|
398
|
+
+ 'th,td{text-align:left;padding:4px 12px}'
|
|
399
|
+
+ 'th{border-bottom:2px solid #444;font-size:0.9em;color:#888}'
|
|
400
|
+
+ 'tr:hover{background:#2a2a2a}'
|
|
401
|
+
+ '.size,.mtime{white-space:nowrap}'
|
|
402
|
+
+ '.size{text-align:right}'
|
|
403
|
+
+ 'a{color:#58a6ff;text-decoration:none}'
|
|
404
|
+
+ 'a:hover{text-decoration:underline}'
|
|
405
|
+
+ '</style></head><body>'
|
|
406
|
+
+ '<h1>Index of ' + escHtml(urlPath) + '</h1>'
|
|
407
|
+
+ '<table><thead><tr><th>Name</th><th class="size">Size</th><th>Last Modified</th></tr></thead>'
|
|
408
|
+
+ '<tbody>' + rows + '</tbody></table>'
|
|
409
|
+
+ '</body></html>';
|
|
410
|
+
|
|
411
|
+
response.writeHead(200, {
|
|
412
|
+
'Content-Type': 'text/html',
|
|
413
|
+
'Access-Control-Allow-Origin': '*',
|
|
414
|
+
'Access-Control-Allow-Methods': 'GET'
|
|
415
|
+
});
|
|
416
|
+
response.end(html, 'utf-8');
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
function serveStaticFile(filePath, file, request, response) {
|
|
420
|
+
var contentType = contentTypes[filePath.split('.').pop().toLowerCase()] || 'text/plain';
|
|
421
|
+
|
|
422
|
+
fs.readFile(filePath, guard(request, response, function (error, content) {
|
|
423
|
+
if (error) {
|
|
424
|
+
if ((!options.spa || file === options.homepage)) {
|
|
425
|
+
console.error('[%s] Error while serving %s with content-type %s : %s',
|
|
426
|
+
new Date(), filePath, contentType, error.message || error);
|
|
427
|
+
}
|
|
428
|
+
//errorMeter.mark();
|
|
429
|
+
if (error.code === 'ENOENT') {
|
|
430
|
+
if (options.spa && !request.wantHomepage) {
|
|
431
|
+
request.wantHomepage = true;
|
|
432
|
+
return serveFile(`/${path.basename(file)}`, request, response);
|
|
433
|
+
} else if (options.spa && file !== options.homepage) {
|
|
434
|
+
return serveFile(options.homepage, request, response);
|
|
435
|
+
}
|
|
436
|
+
fs.readFile(options.path + '/404.html', function (err, content) {
|
|
437
|
+
content = err ? '404 Not Found' : content;
|
|
438
|
+
response.writeHead(404, { 'Content-Type': 'text/html' });
|
|
439
|
+
return response.end(content, 'utf-8');
|
|
440
|
+
});
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
response.writeHead(500);
|
|
444
|
+
return response.end('Sorry, check with the site admin for error: ' + error.code + ' ..\n');
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// Add CORS headers to allow browsers to fetch data directly
|
|
448
|
+
response.writeHead(200, {
|
|
449
|
+
'Content-Type': contentType,
|
|
450
|
+
'Access-Control-Allow-Origin': '*',
|
|
451
|
+
'Access-Control-Allow-Methods': 'GET'
|
|
452
|
+
});
|
|
453
|
+
if (options.monitorBucket && contentType === 'text/html') {
|
|
454
|
+
content = content.toString().replace('</body>', `
|
|
455
|
+
<script>
|
|
456
|
+
;(function (b,e,n,o,i,t) {
|
|
457
|
+
b[o]=b[o]||function(f){(b[o].c=b[o].c||[]).push(f)};
|
|
458
|
+
t=e.createElement(i);e=e.getElementsByTagName(i)[0];
|
|
459
|
+
t.async=1;t.src=n;e.parentNode.insertBefore(t,e);
|
|
460
|
+
}(window,document,'https://apm.pm2.io/pm2-io-apm-browser.v1.js','pm2Ready','script'))
|
|
461
|
+
|
|
462
|
+
pm2Ready(function(apm) {
|
|
463
|
+
apm.setBucket('${options.monitorBucket}')
|
|
464
|
+
apm.setApplication('${options.monitor}')
|
|
465
|
+
apm.reportTimings()
|
|
466
|
+
apm.reportIssues()
|
|
467
|
+
})
|
|
468
|
+
</script>
|
|
469
|
+
</body>
|
|
470
|
+
`);
|
|
471
|
+
}
|
|
472
|
+
response.end(content, 'utf-8');
|
|
473
|
+
debug('[%s] Serving %s with content-type %s', Date.now(), filePath, contentType);
|
|
474
|
+
}));
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
function parseBasicAuth(auth) {
|
|
478
|
+
// auth is like `Basic Y2hhcmxlczoxMjM0NQ==`
|
|
479
|
+
var tmp = auth.split(' ');
|
|
480
|
+
|
|
481
|
+
var buf = Buffer.from(tmp[1], 'base64');
|
|
482
|
+
var plain = buf.toString();
|
|
483
|
+
|
|
484
|
+
var creds = plain.split(':');
|
|
485
|
+
return {
|
|
486
|
+
username: creds[0],
|
|
487
|
+
password: creds[1]
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
function sendBasicAuthResponse(response) {
|
|
492
|
+
response.writeHead(401, {
|
|
493
|
+
'Content-Type': 'text/html',
|
|
494
|
+
'WWW-Authenticate': 'Basic realm="Authentication service"'
|
|
495
|
+
});
|
|
496
|
+
return response.end('401 Unauthorized');
|
|
497
|
+
}
|