@ohos-ports/core 1.0.113-beta.0 → 1.16.33-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/LICENSE +13 -0
- package/README.md +3 -0
- package/lib/api/node.js +176 -0
- package/lib/api/team.js +1889 -0
- package/lib/blocklet/downloader/blocklet-downloader.js +246 -0
- package/lib/blocklet/downloader/bundle-downloader.js +316 -0
- package/lib/blocklet/downloader/constants.js +3 -0
- package/lib/blocklet/downloader/resolve-download.js +187 -0
- package/lib/blocklet/extras.js +177 -0
- package/lib/blocklet/hooks.js +89 -0
- package/lib/blocklet/manager/base.js +49 -0
- package/lib/blocklet/manager/config-synchronizer.js +128 -0
- package/lib/blocklet/manager/disk.js +5170 -0
- package/lib/blocklet/manager/engine.js +124 -0
- package/lib/blocklet/manager/helper/install-application-from-backup.js +285 -0
- package/lib/blocklet/manager/helper/install-application-from-dev.js +72 -0
- package/lib/blocklet/manager/helper/install-application-from-general.js +209 -0
- package/lib/blocklet/manager/helper/install-component-from-dev.js +128 -0
- package/lib/blocklet/manager/helper/install-component-from-upload.js +197 -0
- package/lib/blocklet/manager/helper/install-component-from-url.js +214 -0
- package/lib/blocklet/manager/helper/migrate-application-to-struct-v2.js +557 -0
- package/lib/blocklet/manager/helper/rollback-cache.js +41 -0
- package/lib/blocklet/manager/helper/upgrade-components.js +179 -0
- package/lib/blocklet/manager/pm2-events.js +50 -0
- package/lib/blocklet/migration-dist/migration.cjs +38605 -0
- package/lib/blocklet/migration-dist/run-script.cjs +29 -0
- package/lib/blocklet/migration.js +145 -0
- package/lib/blocklet/project/connect-by-studio.js +91 -0
- package/lib/blocklet/project/connect-to-store.js +64 -0
- package/lib/blocklet/project/create-pack-release.js +293 -0
- package/lib/blocklet/project/disconnect-from-store.js +13 -0
- package/lib/blocklet/project/get-is-multiple-tenant.js +8 -0
- package/lib/blocklet/project/index.js +545 -0
- package/lib/blocklet/project/logo.png +0 -0
- package/lib/blocklet/project/publish-to-store.js +108 -0
- package/lib/blocklet/project/util.js +194 -0
- package/lib/blocklet/security/access-policy.js +131 -0
- package/lib/blocklet/security/index.js +309 -0
- package/lib/blocklet/security/response-header-policy.js +178 -0
- package/lib/blocklet/security/security-rule.js +127 -0
- package/lib/blocklet/security/utils.js +35 -0
- package/lib/blocklet/security/validator.js +40 -0
- package/lib/blocklet/storage/backup/base.js +149 -0
- package/lib/blocklet/storage/backup/blocklet-extras.js +110 -0
- package/lib/blocklet/storage/backup/blocklet.js +159 -0
- package/lib/blocklet/storage/backup/blocklets.js +109 -0
- package/lib/blocklet/storage/backup/data.js +35 -0
- package/lib/blocklet/storage/backup/disk.js +131 -0
- package/lib/blocklet/storage/backup/logs.js +24 -0
- package/lib/blocklet/storage/backup/routing-rule.js +47 -0
- package/lib/blocklet/storage/backup/spaces.js +345 -0
- package/lib/blocklet/storage/restore/base.js +74 -0
- package/lib/blocklet/storage/restore/blocklet-extras.js +89 -0
- package/lib/blocklet/storage/restore/blocklet.js +56 -0
- package/lib/blocklet/storage/restore/blocklets.js +43 -0
- package/lib/blocklet/storage/restore/disk.js +104 -0
- package/lib/blocklet/storage/restore/logs.js +21 -0
- package/lib/blocklet/storage/restore/spaces.js +174 -0
- package/lib/blocklet/storage/utils/disk.js +178 -0
- package/lib/blocklet/storage/utils/hash.js +53 -0
- package/lib/blocklet/storage/utils/zip.js +43 -0
- package/lib/cert.js +221 -0
- package/lib/crons/check-new-version.js +68 -0
- package/lib/crons/monitor-disk-usage.js +46 -0
- package/lib/crons/rotate-pm2-logs/index.js +41 -0
- package/lib/crons/rotate-pm2-logs/script.js +56 -0
- package/lib/event/auto-backup-handler.js +48 -0
- package/lib/event/index.js +520 -0
- package/lib/index.js +768 -364
- package/lib/locales/ar.js +18 -0
- package/lib/locales/de.js +21 -0
- package/lib/locales/en.js +19 -0
- package/lib/locales/es.js +21 -0
- package/lib/locales/fr.js +21 -0
- package/lib/locales/hi.js +18 -0
- package/lib/locales/i18n.db +0 -0
- package/lib/locales/id.js +20 -0
- package/lib/locales/index.js +54 -0
- package/lib/locales/ja.js +19 -0
- package/lib/locales/ko.js +18 -0
- package/lib/locales/pt.js +20 -0
- package/lib/locales/ru.js +21 -0
- package/lib/locales/th.js +18 -0
- package/lib/locales/vi.js +18 -0
- package/lib/locales/zh-tw.js +17 -0
- package/lib/locales/zh.js +17 -0
- package/lib/migrations/1.0.21-update-config.js +34 -0
- package/lib/migrations/1.0.22-max-memory.js +40 -0
- package/lib/migrations/1.0.25.js +36 -0
- package/lib/migrations/1.0.32-update-config.js +30 -0
- package/lib/migrations/1.0.33-blocklets.js +33 -0
- package/lib/migrations/1.0.36-snapshot.js +10 -0
- package/lib/migrations/1.1.10-team.js +26 -0
- package/lib/migrations/1.1.9-snapshot.js +7 -0
- package/lib/migrations/1.16.11-component-status.js +22 -0
- package/lib/migrations/1.16.33-blocklet-security-rule.js +17 -0
- package/lib/migrations/1.16.6-chain-info.js +78 -0
- package/lib/migrations/1.16.8-component-title.js +66 -0
- package/lib/migrations/1.2.11-blocklet-registry.js +10 -0
- package/lib/migrations/1.2.2-blocklet-registry.js +10 -0
- package/lib/migrations/1.3.1-disk-alert-threshold.js +10 -0
- package/lib/migrations/1.4.5-access-key.js +18 -0
- package/lib/migrations/1.5.0-site.js +34 -0
- package/lib/migrations/1.5.15-site.js +184 -0
- package/lib/migrations/1.5.20-registry.js +15 -0
- package/lib/migrations/1.6.17-blocklet-children.js +48 -0
- package/lib/migrations/1.6.21-rename-ip-echo-domain.js +35 -0
- package/lib/migrations/1.6.4-security.js +59 -0
- package/lib/migrations/1.6.5-security.js +60 -0
- package/lib/migrations/1.6.9-update-node-info-and-certificate.js +39 -0
- package/lib/migrations/1.7.1-blocklet-setup.js +18 -0
- package/lib/migrations/1.7.12-blocklet-meta.js +51 -0
- package/lib/migrations/1.7.15-blocklet-bundle-source.js +42 -0
- package/lib/migrations/1.7.20-blocklet-component.js +41 -0
- package/lib/migrations/1.8.33-blocklet-mem-limit.js +20 -0
- package/lib/migrations/README.md +71 -0
- package/lib/migrations/index.js +215 -0
- package/lib/monitor/blocklet-runtime-monitor.js +204 -0
- package/lib/monitor/get-history-list.js +37 -0
- package/lib/monitor/node-monit-sender.js +99 -0
- package/lib/monitor/node-runtime-monitor.js +205 -0
- package/lib/processes/updater.js +186 -0
- package/lib/router/helper.js +1653 -0
- package/lib/router/index.js +341 -0
- package/lib/router/manager.js +806 -0
- package/lib/states/README.md +38 -0
- package/lib/states/access-key.js +124 -0
- package/lib/states/access-policy.js +29 -0
- package/lib/states/audit-log.js +734 -0
- package/lib/states/backup.js +165 -0
- package/lib/states/base.js +25 -0
- package/lib/states/blocklet-extras.js +257 -0
- package/lib/states/blocklet.js +755 -0
- package/lib/states/cache.js +23 -0
- package/lib/states/connect-account.js +8 -0
- package/lib/states/index.js +76 -0
- package/lib/states/job.js +8 -0
- package/lib/states/migration.js +34 -0
- package/lib/states/node.js +324 -0
- package/lib/states/notification.js +81 -0
- package/lib/states/passport.js +8 -0
- package/lib/states/project.js +119 -0
- package/lib/states/release.js +50 -0
- package/lib/states/response-header-policy.js +30 -0
- package/lib/states/routing-snapshot.js +146 -0
- package/lib/states/runtime-insight.js +25 -0
- package/lib/states/security-rule.js +30 -0
- package/lib/states/session.js +53 -0
- package/lib/states/site.js +102 -0
- package/lib/states/tag.js +8 -0
- package/lib/states/traffic-insight.js +25 -0
- package/lib/states/user-session.js +8 -0
- package/lib/states/user.js +717 -0
- package/lib/states/verify-code.js +152 -0
- package/lib/states/webhook.js +66 -0
- package/lib/team/manager.js +508 -0
- package/lib/util/blocklet.js +2182 -0
- package/lib/util/cache.js +63 -0
- package/lib/util/chain.js +29 -0
- package/lib/util/default-node-config.js +46 -0
- package/lib/util/docker/blocklet-custom-dockerfile.js +92 -0
- package/lib/util/docker/check-docker-has-image.js +23 -0
- package/lib/util/docker/check-docker-installed.js +32 -0
- package/lib/util/docker/check-docker-run-history.js +18 -0
- package/lib/util/docker/check-need-run-docker.js +32 -0
- package/lib/util/docker/create-docker-image.js +65 -0
- package/lib/util/docker/debian-dockerfile.js +50 -0
- package/lib/util/docker/docker-container-prune.js +75 -0
- package/lib/util/docker/docker-exec-chown.js +39 -0
- package/lib/util/docker/docker-exec.js +68 -0
- package/lib/util/docker/docker-remove-by-name.js +20 -0
- package/lib/util/docker/get-docker-runtime-info.js +39 -0
- package/lib/util/docker/parse-docker-name.js +10 -0
- package/lib/util/docker/parse-docker-options-from-pm2.js +295 -0
- package/lib/util/docker/shard-network.js +10 -0
- package/lib/util/domain-status.js +118 -0
- package/lib/util/get-accessible-external-node-ip.js +92 -0
- package/lib/util/get-dynamic-components.js +47 -0
- package/lib/util/get-meta-from-url.js +30 -0
- package/lib/util/get-migration-scripts.js +36 -0
- package/lib/util/get-safe-env.js +12 -0
- package/lib/util/index.js +522 -0
- package/lib/util/install-external-dependencies.js +67 -0
- package/lib/util/ip.js +53 -0
- package/lib/util/launcher.js +475 -0
- package/lib/util/log.js +431 -0
- package/lib/util/maintain.js +178 -0
- package/lib/util/promise-debounce.js +29 -0
- package/lib/util/promise-once.js +14 -0
- package/lib/util/promise-spawn.js +93 -0
- package/lib/util/queue.js +44 -0
- package/lib/util/ready.js +89 -0
- package/lib/util/request.js +10 -0
- package/lib/util/requirement.js +61 -0
- package/lib/util/reset-node.js +166 -0
- package/lib/util/rotator.js +194 -0
- package/lib/util/router.js +122 -0
- package/lib/util/rpc.js +78 -0
- package/lib/util/spaces.js +102 -0
- package/lib/util/store.js +184 -0
- package/lib/util/sysinfo.js +46 -0
- package/lib/util/ua.js +54 -0
- package/lib/validators/backup.js +34 -0
- package/lib/validators/blocklet-extra.js +18 -0
- package/lib/validators/node.js +73 -0
- package/lib/validators/permission.js +38 -0
- package/lib/validators/project.js +39 -0
- package/lib/validators/role.js +64 -0
- package/lib/validators/router.js +192 -0
- package/lib/validators/space-gateway.js +33 -0
- package/lib/validators/trusted-factory.js +28 -0
- package/lib/validators/trusted-passport.js +32 -0
- package/lib/validators/user.js +62 -0
- package/lib/validators/util.js +92 -0
- package/lib/validators/webhook.js +42 -0
- package/lib/webhook/index.js +216 -0
- package/lib/webhook/reduce-queue.js +12 -0
- package/lib/webhook/sender/api/index.js +67 -0
- package/lib/webhook/sender/base.js +11 -0
- package/lib/webhook/sender/index.js +44 -0
- package/lib/webhook/sender/slack/index.js +167 -0
- package/lib/webhook/sender/wallet/index.js +62 -0
- package/package.json +107 -34
- package/examples/BatchClient.js +0 -61
- package/examples/compressor/example.js +0 -7
- package/examples/jsonrpcserver/rpcmethod.js +0 -21
- package/examples/jsonrpcserver/rpcrequest.js +0 -20
- package/examples/jsonrpcserver/rpctest.js +0 -6
- package/examples/loader-pipes/test-mc.js +0 -52
- package/examples/loader-pipes/test.js +0 -51
- package/examples/mc-test.js +0 -44
- package/examples/redisdebug.js +0 -80
- package/lib/cache/CFMemcache.js +0 -57
- package/lib/cache/CacheLoader.js +0 -217
- package/lib/cache/Memcache.js +0 -406
- package/lib/class/Class.js +0 -54
- package/lib/client/MemcacheAdapter.js +0 -139
- package/lib/client/Memcached.js +0 -142
- package/lib/client/MemcachedSocket.js +0 -570
- package/lib/client/MySQLPool.js +0 -153
- package/lib/client/PGPool.js +0 -296
- package/lib/client/Redis.js +0 -787
- package/lib/client/S3.js +0 -103
- package/lib/client/S3Client.js +0 -392
- package/lib/client/pg/CursorQueryWorker.js +0 -160
- package/lib/client/rabbitmq/RbmqChannel.js +0 -61
- package/lib/client/rabbitmq/RbmqConfig.js +0 -144
- package/lib/client/rabbitmq/RbmqConnection.js +0 -259
- package/lib/client/rabbitmq/RbmqExchange.js +0 -112
- package/lib/client/rabbitmq/RbmqIncomingMessage.js +0 -53
- package/lib/client/rabbitmq/RbmqMessage.js +0 -120
- package/lib/client/rabbitmq/RbmqOutcomingMessage.js +0 -47
- package/lib/client/rabbitmq/RbmqQueue.js +0 -87
- package/lib/client/rabbitmq/index.js +0 -8
- package/lib/common/Assertions.js +0 -184
- package/lib/common/Merge.js +0 -32
- package/lib/common/Net.js +0 -56
- package/lib/common/Process.js +0 -20
- package/lib/common/Random.js +0 -53
- package/lib/common/Sort.js +0 -19
- package/lib/common/Types.js +0 -163
- package/lib/credentials/CredentialsProvider.js +0 -45
- package/lib/credentials/Manager.js +0 -101
- package/lib/crypto/Crypto.js +0 -66
- package/lib/data/BinaryData.js +0 -363
- package/lib/data/Compressor.js +0 -83
- package/lib/data/FormData.js +0 -144
- package/lib/data/Mime.js +0 -45
- package/lib/event/ErrorEvent.js +0 -71
- package/lib/event/Event.js +0 -20
- package/lib/event/EventDispatcher.js +0 -157
- package/lib/http/ConnectionPool.js +0 -82
- package/lib/http/ExpressServer.js +0 -19
- package/lib/http/Headers.js +0 -165
- package/lib/http/Loader.js +0 -584
- package/lib/http/Message.js +0 -189
- package/lib/http/Request.js +0 -848
- package/lib/http/RequestProcessor.js +0 -150
- package/lib/http/Response.js +0 -850
- package/lib/http/Server.js +0 -267
- package/lib/http/Socket.js +0 -157
- package/lib/http/Url.js +0 -2
- package/lib/jsonrpc/JsonRpcAbstract.js +0 -109
- package/lib/jsonrpc/JsonRpcBatchRequest.js +0 -54
- package/lib/jsonrpc/JsonRpcBatchResponse.js +0 -73
- package/lib/jsonrpc/JsonRpcError.js +0 -191
- package/lib/jsonrpc/JsonRpcMethod.js +0 -67
- package/lib/jsonrpc/JsonRpcRequest.js +0 -224
- package/lib/jsonrpc/JsonRpcRequestProcessor.js +0 -366
- package/lib/jsonrpc/JsonRpcResponse.js +0 -175
- package/lib/jsonrpc/JsonRpcServer.js +0 -81
- package/lib/logger/EmptyLogger.js +0 -15
- package/lib/logger/LoggerFactory.js +0 -20
- package/lib/logger/WildFireLogger.js +0 -267
- package/lib/pattern/Pool.js +0 -187
- package/lib/pattern/decorator/AbstractDecorator.js +0 -49
- package/lib/pattern/decorator/DecoratorChain.js +0 -285
- package/lib/util/DeviceDetector.js +0 -153
- package/lib/yaml/YAML.js +0 -46
- package/patterns.conf +0 -6
- package/tests/cache/CacheLoaderSpec.js +0 -7
- package/tests/class/ClassSpec.js +0 -106
- package/tests/client/MemcachedSocketSpec.js +0 -1228
- package/tests/client/MemcachedSpec.js +0 -77
- package/tests/client/MySQLPoolSpec.js +0 -231
- package/tests/client/PGPoolSpec.js +0 -479
- package/tests/client/RedisSpec.js +0 -471
- package/tests/client/S3ClientSpec.js +0 -26
- package/tests/client/S3Spec.js +0 -129
- package/tests/client/rabbitmq/RbmqConnectionSpec.js +0 -71
- package/tests/common/AssertionsSpec.js +0 -278
- package/tests/common/NetSpec.js +0 -6
- package/tests/common/RandomSpec.js +0 -35
- package/tests/common/TypesSpec.js +0 -7
- package/tests/credentials/CredentialsProviderSpec.js +0 -46
- package/tests/crypto/CryptoSpec.js +0 -33
- package/tests/data/BinaryDataSpec.js +0 -50
- package/tests/data/CompressorSpec.js +0 -7
- package/tests/data/FormDataSpec.js +0 -7
- package/tests/data/MimeSpec.js +0 -7
- package/tests/event/ErrorEventSpec.js +0 -20
- package/tests/event/EventDispatcherSpec.js +0 -319
- package/tests/event/EventSpec.js +0 -17
- package/tests/http/ConnectionPoolSpec.js +0 -7
- package/tests/http/HeadersSpec.js +0 -79
- package/tests/http/LoaderSpec.js +0 -43
- package/tests/http/MessageSpec.js +0 -251
- package/tests/http/RequestProcessorSpec.js +0 -124
- package/tests/http/RequestSpec.js +0 -651
- package/tests/http/ResponseSpec.js +0 -491
- package/tests/http/ServerSpec.js +0 -7
- package/tests/http/SocketSpec.js +0 -7
- package/tests/http/UrlSpec.js +0 -14
- package/tests/jsonrpc/JsonRpcAbstractSpec.js +0 -20
- package/tests/jsonrpc/JsonRpcBatchRequestSpec.js +0 -31
- package/tests/jsonrpc/JsonRpcBatchResponseSpec.js +0 -38
- package/tests/jsonrpc/JsonRpcErrorSpec.js +0 -58
- package/tests/jsonrpc/JsonRpcMethodSpec.js +0 -7
- package/tests/jsonrpc/JsonRpcRequestProcessorSpec.js +0 -271
- package/tests/jsonrpc/JsonRpcRequestSpec.js +0 -70
- package/tests/jsonrpc/JsonRpcResponseSpec.js +0 -63
- package/tests/jsonrpc/JsonRpcServerSpec.js +0 -42
- package/tests/pattern/PoolSpec.js +0 -180
- package/tests/pattern/decorator/AbstractDecoratorSpec.js +0 -7
- package/tests/pattern/decorator/DecoratorChainSpec.js +0 -7
- package/tests/yaml/YAMLSpec.js +0 -132
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
const { spawn } = require('child_process');
|
|
2
|
+
const pRetry = require('p-retry');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 通用的命令执行函数,支持字符串输入
|
|
6
|
+
* @param {string} commandString - 完整的命令字符串
|
|
7
|
+
* @param {object} [options={}] - 可选的 spawn 配置项
|
|
8
|
+
* @returns {Promise<string>} - 执行结果
|
|
9
|
+
*/
|
|
10
|
+
function basePromiseSpawn(commandString, options = {}, timeout = 0) {
|
|
11
|
+
return new Promise((resolve, reject) => {
|
|
12
|
+
const childProcess = spawn(commandString, {
|
|
13
|
+
...options,
|
|
14
|
+
shell: true,
|
|
15
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
16
|
+
env: {
|
|
17
|
+
...options.env,
|
|
18
|
+
PATH: process.env.PATH,
|
|
19
|
+
HTTP_PROXY: process.env.DOCKER_HTTP_PROXY || '',
|
|
20
|
+
HTTPS_PROXY: process.env.DOCKER_HTTPS_PROXY || '',
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
let stdoutData = '';
|
|
25
|
+
let stderrData = '';
|
|
26
|
+
|
|
27
|
+
let timer;
|
|
28
|
+
|
|
29
|
+
const clearTimer = () => {
|
|
30
|
+
if (timer) {
|
|
31
|
+
clearTimeout(timer);
|
|
32
|
+
timer = null;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
childProcess.stdout.on('data', (data) => {
|
|
37
|
+
stdoutData += data;
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
childProcess.stderr.on('data', (data) => {
|
|
41
|
+
stderrData += data;
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
childProcess.on('error', (err) => {
|
|
45
|
+
clearTimer();
|
|
46
|
+
childProcess.stdout.destroy();
|
|
47
|
+
childProcess.stderr.destroy();
|
|
48
|
+
reject(err);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
childProcess.on('close', (code) => {
|
|
52
|
+
clearTimer();
|
|
53
|
+
childProcess.stdout.destroy();
|
|
54
|
+
childProcess.stderr.destroy();
|
|
55
|
+
if (code === 0) {
|
|
56
|
+
resolve(stdoutData);
|
|
57
|
+
} else {
|
|
58
|
+
reject(new Error(`Command failed with code ${code}: ${stderrData}`));
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
childProcess.on('exit', () => {
|
|
63
|
+
clearTimer();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
if (timeout > 0) {
|
|
67
|
+
// 启用超时功能
|
|
68
|
+
timer = setTimeout(() => {
|
|
69
|
+
childProcess.stdout.destroy();
|
|
70
|
+
childProcess.stderr.destroy();
|
|
71
|
+
childProcess.kill('SIGKILL');
|
|
72
|
+
reject(new Error(`Command timed out after ${timeout}ms`));
|
|
73
|
+
}, timeout);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const promiseSpawn = (command, options = {}, { timeout = 0, retry = 0 } = {}) => {
|
|
79
|
+
if (command === '' || command === undefined) {
|
|
80
|
+
throw new Error('Command is empty');
|
|
81
|
+
}
|
|
82
|
+
const func = () => basePromiseSpawn(command, options, timeout);
|
|
83
|
+
if (!retry) {
|
|
84
|
+
return func();
|
|
85
|
+
}
|
|
86
|
+
return pRetry(func, {
|
|
87
|
+
retries: retry,
|
|
88
|
+
minTimeout: timeout,
|
|
89
|
+
maxTimeout: timeout,
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
module.exports = promiseSpawn;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
const createQueue = require('@abtnode/queue');
|
|
2
|
+
const QueueStore = require('@abtnode/queue/lib/store/sequelize');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param {object} params
|
|
6
|
+
* @param {string} params.name name of the queue
|
|
7
|
+
* @param {object} params.model model
|
|
8
|
+
* @param {function} params.onJob called on receives job
|
|
9
|
+
* @param {boolean} params.daemon [param=false] is the running in a daemon environment, default false
|
|
10
|
+
* @param {object} params.options options
|
|
11
|
+
* @param {any} [params.options.id] id of the job
|
|
12
|
+
* @param {number} [params.options.concurrency] [param=1] number of concurrent jobs
|
|
13
|
+
* @param {number} [params.options.maxRetries] [param=1] number of max retries, default 1
|
|
14
|
+
* @param {number} [params.options.maxTimeout] [param=86400000] max timeout, in ms, default 86400000ms(1d)
|
|
15
|
+
* @param {number} [params.options.retryDelay] [param=0] retry delay, in ms, default 0ms
|
|
16
|
+
* @param {boolean} [params.options.enableScheduledJob]
|
|
17
|
+
* @typedef {
|
|
18
|
+
* EventEmitter & {
|
|
19
|
+
* store: JobStore;
|
|
20
|
+
* push: (...args: {
|
|
21
|
+
* job: any;
|
|
22
|
+
* jobId: string;
|
|
23
|
+
* persist: boolean;
|
|
24
|
+
* delay: number;
|
|
25
|
+
* }) => EventEmitter;
|
|
26
|
+
* get: (id: string) => Promise<any>;
|
|
27
|
+
* cancel: (id: string) => Promise<...>;
|
|
28
|
+
* options: {}
|
|
29
|
+
* }
|
|
30
|
+
* } Queue
|
|
31
|
+
*/
|
|
32
|
+
module.exports = ({ name, model, onJob, daemon = false, options = {} }) => {
|
|
33
|
+
if (daemon) {
|
|
34
|
+
const queue = createQueue({
|
|
35
|
+
store: new QueueStore(model, name),
|
|
36
|
+
onJob,
|
|
37
|
+
options,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
return queue;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return { push: () => ({ on: () => {} }) };
|
|
44
|
+
};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
const logger = require('@abtnode/logger')('@abtnode/core:ready');
|
|
3
|
+
const chalk = require('chalk');
|
|
4
|
+
|
|
5
|
+
const createStateReadyHandler =
|
|
6
|
+
(routingSnapshot) =>
|
|
7
|
+
async ({ states, options }) => {
|
|
8
|
+
const snapshotHash = await routingSnapshot.init();
|
|
9
|
+
|
|
10
|
+
return states.node
|
|
11
|
+
.read()
|
|
12
|
+
.then(async (state) => {
|
|
13
|
+
if (snapshotHash && !state.routing.snapshotHash) {
|
|
14
|
+
logger.info('set snapshot hash because its empty');
|
|
15
|
+
await states.node.updateNodeRouting({ ...state.routing, snapshotHash });
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Set default sender/receiver for notification center
|
|
19
|
+
states.notification.setDefaultSender(state.did);
|
|
20
|
+
if (state.nodeOwner) {
|
|
21
|
+
states.notification.setDefaultReceiver(state.nodeOwner.did);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (process.env.NODE_ENV === 'test' || process.env.CI || process.env.TRAVIS) {
|
|
25
|
+
// Do not validate data dir sharing in test env
|
|
26
|
+
} else {
|
|
27
|
+
const count = await states.node.count();
|
|
28
|
+
if (count > 1) {
|
|
29
|
+
// eslint-disable-next-line no-underscore-dangle
|
|
30
|
+
await states.node.remove({ did: state.did });
|
|
31
|
+
console.error('\n\x1b[31m======================================================');
|
|
32
|
+
console.error(`Data dir: ${options.dataDir} is used by another Blocklet Server instance, abort!`);
|
|
33
|
+
console.error('Sharing data dir between Blocklet Server instances may break things!');
|
|
34
|
+
console.error('======================================================\x1b[0m');
|
|
35
|
+
console.log('\nIf you intend to use this dir:');
|
|
36
|
+
console.log(` 1. Stop Blocklet Server by ${chalk.cyan('blocklet server stop --force')}`);
|
|
37
|
+
console.log(` 2. Clear data dir by ${chalk.cyan(`rm -r ${options.dataDir}`)}`);
|
|
38
|
+
console.log(' 3. Reinitialize and start Blocklet Server');
|
|
39
|
+
process.exit(1);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
.catch((err) => {
|
|
44
|
+
console.error('Can not ready node state on Blocklet Server start:', err.message);
|
|
45
|
+
process.exit(1);
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const createStateReadyQueue = ({ states, options, dataDirs }) => {
|
|
50
|
+
const readyState = Object.keys(states).reduce((acc, key) => {
|
|
51
|
+
acc[key] = false;
|
|
52
|
+
return acc;
|
|
53
|
+
}, {});
|
|
54
|
+
|
|
55
|
+
const readyCallbacks = [];
|
|
56
|
+
const markReady = async (key) => {
|
|
57
|
+
logger.debug('mark ready', key);
|
|
58
|
+
readyState[key] = true;
|
|
59
|
+
if (Object.keys(readyState).every((x) => readyState[x])) {
|
|
60
|
+
if (readyCallbacks.length) {
|
|
61
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
62
|
+
for (const cb of readyCallbacks) {
|
|
63
|
+
// eslint-disable-next-line no-await-in-loop
|
|
64
|
+
await cb({ states, options, dataDirs });
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
Object.keys(states).forEach((key) => {
|
|
71
|
+
const state = states[key];
|
|
72
|
+
if (typeof state.onReady === 'function') {
|
|
73
|
+
state.onReady(() => markReady(key));
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
return (cb) => {
|
|
78
|
+
if (Object.keys(readyState).every((x) => readyState[x])) {
|
|
79
|
+
cb({ states, options, dataDirs });
|
|
80
|
+
} else {
|
|
81
|
+
readyCallbacks.push(cb);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
module.exports = {
|
|
87
|
+
createStateReadyQueue,
|
|
88
|
+
createStateReadyHandler,
|
|
89
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
const axios = require('@abtnode/util/lib/axios');
|
|
2
|
+
const { version } = require('../../package.json');
|
|
3
|
+
|
|
4
|
+
module.exports =
|
|
5
|
+
process.env.NODE_ENV === 'test'
|
|
6
|
+
? axios
|
|
7
|
+
: axios.create({
|
|
8
|
+
timeout: 10 * 1000,
|
|
9
|
+
headers: { 'User-Agent': `ABTNode/${version}`, 'x-blocklet-server-version': version },
|
|
10
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
const semver = require('semver');
|
|
2
|
+
|
|
3
|
+
let { version: actualServerVersion } = require('../../package.json');
|
|
4
|
+
|
|
5
|
+
const { version: actualNodejsVersion } = process;
|
|
6
|
+
|
|
7
|
+
const defaults = { abtnode: '>= 1.1.0', os: '*', cpu: '*', nodejs: '*' };
|
|
8
|
+
|
|
9
|
+
const isSatisfied = (requirements, throwOnUnsatisfied = true) => {
|
|
10
|
+
if (!requirements || typeof requirements !== 'object') {
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const { platform: actualOs, arch: actualCpu } = process;
|
|
15
|
+
const {
|
|
16
|
+
os: expectedOs,
|
|
17
|
+
cpu: expectedCpu,
|
|
18
|
+
abtnode,
|
|
19
|
+
server,
|
|
20
|
+
nodejs: expectedNodejsVersion,
|
|
21
|
+
} = Object.assign(defaults, requirements);
|
|
22
|
+
const expectedServerVersion = server || abtnode;
|
|
23
|
+
|
|
24
|
+
const isOsSatisfied =
|
|
25
|
+
expectedOs === '*' || expectedOs === actualOs || (Array.isArray(expectedOs) && expectedOs.includes(actualOs));
|
|
26
|
+
const isCpuSatisfied =
|
|
27
|
+
expectedCpu === '*' || expectedCpu === actualCpu || (Array.isArray(expectedCpu) && expectedCpu.includes(actualCpu));
|
|
28
|
+
|
|
29
|
+
const isServerVersionSatisfied = semver.satisfies(semver.coerce(actualServerVersion), expectedServerVersion);
|
|
30
|
+
|
|
31
|
+
const isNodejsVersionSatisfied = semver.satisfies(semver.coerce(actualNodejsVersion), expectedNodejsVersion);
|
|
32
|
+
|
|
33
|
+
if (throwOnUnsatisfied) {
|
|
34
|
+
if (isOsSatisfied === false) {
|
|
35
|
+
throw new Error(`Your blocklet is not supported on platform ${actualOs}`);
|
|
36
|
+
}
|
|
37
|
+
if (isCpuSatisfied === false) {
|
|
38
|
+
throw new Error(`Your blocklet is not supported on architecture ${actualCpu}`);
|
|
39
|
+
}
|
|
40
|
+
if (isServerVersionSatisfied === false) {
|
|
41
|
+
throw new Error(
|
|
42
|
+
`Expected server version for the blocklet is ${expectedServerVersion}, but your server version is ${actualServerVersion}, please upgrade or downgrade your server to continue.`
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
if (isNodejsVersionSatisfied === false) {
|
|
46
|
+
throw new Error(
|
|
47
|
+
`Expected Nodejs version for the blocklet is ${expectedNodejsVersion}, but your Nodejs version is ${actualNodejsVersion}, please upgrade or downgrade Nodejs and restart your server.`
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return isOsSatisfied && isCpuSatisfied && isServerVersionSatisfied;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// Just for test
|
|
56
|
+
isSatisfied._setActualServerVersion = (d) => {
|
|
57
|
+
actualServerVersion = d;
|
|
58
|
+
};
|
|
59
|
+
isSatisfied._actualNodejsVersion = actualNodejsVersion;
|
|
60
|
+
|
|
61
|
+
module.exports = isSatisfied;
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
const logger = require('@abtnode/logger')('@abtnode/core:util:reset');
|
|
2
|
+
|
|
3
|
+
const states = require('../states');
|
|
4
|
+
|
|
5
|
+
const noop = () => true;
|
|
6
|
+
|
|
7
|
+
/* istanbul ignore next */
|
|
8
|
+
const resetOwner = async ({ teamManager }) => {
|
|
9
|
+
const result = await states.node.updateNodeInfo({ initialized: false, initializedAt: null });
|
|
10
|
+
|
|
11
|
+
try {
|
|
12
|
+
const nodeInfo = await states.node.read();
|
|
13
|
+
const teamDid = nodeInfo.did;
|
|
14
|
+
const userState = await teamManager.getUserState(teamDid);
|
|
15
|
+
const passportState = await teamManager.getPassportState(teamDid);
|
|
16
|
+
const connectedAccountState = await teamManager.getConnectedAccountState(teamDid);
|
|
17
|
+
await passportState.remove({ userDid: nodeInfo.nodeOwner.did });
|
|
18
|
+
await connectedAccountState.remove({ userDid: nodeInfo.nodeOwner.did });
|
|
19
|
+
await userState.remove({ did: nodeInfo.nodeOwner.did });
|
|
20
|
+
await states.node.updateNodeInfo({ nodeOwner: null });
|
|
21
|
+
logger.info('reset node owner', result);
|
|
22
|
+
} catch (err) {
|
|
23
|
+
// Do nothing
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return true;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/* istanbul ignore next */
|
|
30
|
+
const resetBlocklets = async ({ context, blockletManager }) => {
|
|
31
|
+
const blocklets = await blockletManager.list({ includeRuntimeInfo: false }, context);
|
|
32
|
+
for (let i = 0; i < blocklets.length; i++) {
|
|
33
|
+
const blocklet = blocklets[i];
|
|
34
|
+
// eslint-disable-next-line no-await-in-loop
|
|
35
|
+
await blockletManager.delete({ did: blocklet.meta.did, keepData: false }, context);
|
|
36
|
+
}
|
|
37
|
+
logger.info('reset blocklets', blocklets.length);
|
|
38
|
+
return true;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/* istanbul ignore next */
|
|
42
|
+
const resetSites = async ({ context, routerManager, takeRoutingSnapshot }) => {
|
|
43
|
+
const sites = await states.site.getSites();
|
|
44
|
+
for (let i = 0; i < sites.length; i++) {
|
|
45
|
+
const site = sites[i];
|
|
46
|
+
try {
|
|
47
|
+
// eslint-disable-next-line no-await-in-loop
|
|
48
|
+
await routerManager.deleteRoutingSite({ id: site.id }, context);
|
|
49
|
+
} catch (err) {
|
|
50
|
+
// Do nothing
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const hash = await takeRoutingSnapshot({ message: 'reset routing sites for test', dryRun: false }, context);
|
|
55
|
+
logger.info('reset routing sites', { hash });
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/* istanbul ignore next */
|
|
59
|
+
const resetCertificates = async ({ certManager }) => {
|
|
60
|
+
const certs = await certManager.getAll();
|
|
61
|
+
for (let i = 0; i < certs.length; i++) {
|
|
62
|
+
const cert = certs[i];
|
|
63
|
+
if (!cert.domain === '*.ip.abtnet.io') {
|
|
64
|
+
try {
|
|
65
|
+
// eslint-disable-next-line no-await-in-loop
|
|
66
|
+
await certManager.remove(cert.id);
|
|
67
|
+
} catch (err) {
|
|
68
|
+
// Do nothing
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
logger.info('reset routing certificates', { length: certs.length });
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const resetUsers = async ({ teamManager }) => {
|
|
76
|
+
try {
|
|
77
|
+
const nodeInfo = await states.node.read();
|
|
78
|
+
|
|
79
|
+
const teamDid = nodeInfo.did;
|
|
80
|
+
const userState = await teamManager.getUserState(teamDid);
|
|
81
|
+
const passportState = await teamManager.getPassportState(teamDid);
|
|
82
|
+
const connectedAccountState = await teamManager.getConnectedAccountState(teamDid);
|
|
83
|
+
await passportState.remove({ userDid: { $ne: nodeInfo.nodeOwner.did } });
|
|
84
|
+
await connectedAccountState.remove({ userDid: { $ne: nodeInfo.nodeOwner.did } });
|
|
85
|
+
await userState.remove({ did: { $ne: nodeInfo.nodeOwner.did } });
|
|
86
|
+
|
|
87
|
+
logger.info('reset users');
|
|
88
|
+
} catch (error) {
|
|
89
|
+
logger.error('failed reset users', { error });
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const resetInvitations = async ({ teamManager }) => {
|
|
94
|
+
const nodeInfo = await states.node.read();
|
|
95
|
+
|
|
96
|
+
const teamDid = nodeInfo.did;
|
|
97
|
+
const sessionState = await teamManager.getSessionState(teamDid);
|
|
98
|
+
const count = sessionState.remove({ type: 'invite' });
|
|
99
|
+
|
|
100
|
+
logger.info('reset invitations', { length: count });
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const resetFns = {
|
|
104
|
+
owner: resetOwner,
|
|
105
|
+
blocklets: resetBlocklets,
|
|
106
|
+
blockletExtras: noop,
|
|
107
|
+
sites: resetSites,
|
|
108
|
+
certificates: resetCertificates,
|
|
109
|
+
webhooks: noop,
|
|
110
|
+
accessKeys: noop,
|
|
111
|
+
users: resetUsers,
|
|
112
|
+
invitations: resetInvitations,
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
module.exports = async ({
|
|
116
|
+
params,
|
|
117
|
+
context,
|
|
118
|
+
blockletManager,
|
|
119
|
+
routerManager,
|
|
120
|
+
takeRoutingSnapshot,
|
|
121
|
+
teamManager,
|
|
122
|
+
certManager,
|
|
123
|
+
}) => {
|
|
124
|
+
if (process.env.NODE_ENV !== 'e2e') {
|
|
125
|
+
throw new Error('Reset node only exists for test purpose');
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const thingsToReset = Object.assign(
|
|
129
|
+
{
|
|
130
|
+
owner: false,
|
|
131
|
+
blocklets: false,
|
|
132
|
+
blockletExtras: false,
|
|
133
|
+
sites: false,
|
|
134
|
+
certificates: false,
|
|
135
|
+
webhooks: false,
|
|
136
|
+
accessKeys: false,
|
|
137
|
+
users: false,
|
|
138
|
+
},
|
|
139
|
+
params || {}
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
logger.info('reset node', { thingsToReset, context });
|
|
143
|
+
|
|
144
|
+
const results = {};
|
|
145
|
+
const keys = Object.keys(thingsToReset);
|
|
146
|
+
for (let i = 0; i < keys.length; i++) {
|
|
147
|
+
const key = keys[i];
|
|
148
|
+
if (thingsToReset[key]) {
|
|
149
|
+
if (typeof resetFns[key] !== 'function') {
|
|
150
|
+
throw new Error(`Unknown reset function ${key}`);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// eslint-disable-next-line no-await-in-loop
|
|
154
|
+
results[key] = await resetFns[key]({
|
|
155
|
+
context,
|
|
156
|
+
blockletManager,
|
|
157
|
+
routerManager,
|
|
158
|
+
takeRoutingSnapshot,
|
|
159
|
+
teamManager,
|
|
160
|
+
certManager,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return results;
|
|
166
|
+
};
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base on: https://github.com/keymetrics/pm2-logrotate
|
|
3
|
+
*/
|
|
4
|
+
const fs = require('fs-extra');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const dayjs = require('@abtnode/util/lib/dayjs');
|
|
7
|
+
const zlib = require('zlib');
|
|
8
|
+
const log = require('@abtnode/logger');
|
|
9
|
+
|
|
10
|
+
const buildLogger =
|
|
11
|
+
(func) =>
|
|
12
|
+
(...args) => {
|
|
13
|
+
if (typeof args[0] === 'string') {
|
|
14
|
+
func(`pm2.log.rotate.${args[0]}`, ...args.slice(1));
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
func('pm2.log.rotate', ...args);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// eslint-disable-next-line no-console
|
|
22
|
+
const logInfo = buildLogger(console.info);
|
|
23
|
+
const logError = buildLogger(console.error);
|
|
24
|
+
|
|
25
|
+
module.exports = class LogRotate {
|
|
26
|
+
constructor({ compress, dateFormat, tz, retain } = {}) {
|
|
27
|
+
this.compress = compress;
|
|
28
|
+
this.dateFormat = dateFormat;
|
|
29
|
+
this.tz = tz;
|
|
30
|
+
this.retain = retain;
|
|
31
|
+
this.compressedFileFormat = '.gz';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
getLimitSize(maxSize) {
|
|
35
|
+
if (!maxSize) {
|
|
36
|
+
return 1024 * 1024 * 10; // 10M
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (typeof maxSize === 'number') {
|
|
40
|
+
return maxSize;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (typeof maxSize !== 'string') {
|
|
44
|
+
throw new Error('maxSize must be a string or number');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// eslint-disable-next-line no-param-reassign
|
|
48
|
+
maxSize = maxSize.trim();
|
|
49
|
+
|
|
50
|
+
const unit = maxSize[maxSize.length - 1].toUpperCase();
|
|
51
|
+
if (unit === 'G') {
|
|
52
|
+
return parseInt(maxSize, 10) * 1024 * 1024 * 1024;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (unit === 'M') {
|
|
56
|
+
return parseInt(maxSize, 10) * 1024 * 1024;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (unit === 'K') {
|
|
60
|
+
return parseInt(maxSize, 10) * 1024;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return parseInt(maxSize, 10);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Apply the rotation process of the log file.
|
|
68
|
+
*
|
|
69
|
+
* @param {string} file
|
|
70
|
+
*/
|
|
71
|
+
proceed(file, callback = () => {}) {
|
|
72
|
+
// set default final time
|
|
73
|
+
let finalTime = dayjs().subtract(1, 'day').format(this.dateFormat);
|
|
74
|
+
// check for a timezone
|
|
75
|
+
if (this.tz) {
|
|
76
|
+
try {
|
|
77
|
+
finalTime = dayjs().tz(this.tz).subtract(1, 'day').format(this.dateFormat);
|
|
78
|
+
} catch (err) {
|
|
79
|
+
// use default
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const filename = path.basename(file, path.extname(file));
|
|
84
|
+
const dirname = path.dirname(file);
|
|
85
|
+
const extName = this.compress ? `.log${this.compressedFileFormat}` : '.log';
|
|
86
|
+
|
|
87
|
+
let finalName = `${path.join(dirname, filename)}-${finalTime}${extName}`;
|
|
88
|
+
|
|
89
|
+
// 这种情况是为了兼容,之间的日志文件名中的日志大了一天,所以升级时旧数据可能存在重复
|
|
90
|
+
if (fs.existsSync(finalName)) {
|
|
91
|
+
finalName = `${path.join(dirname, filename)}-${finalTime}.1${extName}`;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// if compression is enabled, add gz extention and create a gzip instance
|
|
95
|
+
let GZIP;
|
|
96
|
+
if (this.compress) {
|
|
97
|
+
GZIP = zlib.createGzip({
|
|
98
|
+
level: zlib.constants.Z_BEST_COMPRESSION,
|
|
99
|
+
memLevel: zlib.constants.Z_BEST_COMPRESSION,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// create our read/write streams
|
|
104
|
+
const readStream = fs.createReadStream(file);
|
|
105
|
+
const writeStream = fs.createWriteStream(finalName, { flags: 'w+' });
|
|
106
|
+
|
|
107
|
+
// pipe all stream
|
|
108
|
+
if (this.compress) {
|
|
109
|
+
readStream.pipe(GZIP).pipe(writeStream);
|
|
110
|
+
} else {
|
|
111
|
+
readStream.pipe(writeStream);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const onError = (err) => {
|
|
115
|
+
logError(err);
|
|
116
|
+
callback(err);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
// listen for error
|
|
120
|
+
readStream.on('error', onError);
|
|
121
|
+
writeStream.on('error', onError);
|
|
122
|
+
if (this.compression) {
|
|
123
|
+
GZIP.on('error', onError);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// when the read is done, empty the file and check for retain option
|
|
127
|
+
writeStream.on('finish', () => {
|
|
128
|
+
if (GZIP) {
|
|
129
|
+
GZIP.close();
|
|
130
|
+
}
|
|
131
|
+
readStream.close();
|
|
132
|
+
writeStream.close();
|
|
133
|
+
|
|
134
|
+
fs.truncateSync(file);
|
|
135
|
+
logInfo('rotated file:', finalName);
|
|
136
|
+
|
|
137
|
+
callback(null, finalName);
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
proceedSync(file) {
|
|
142
|
+
return new Promise((resolve, reject) => {
|
|
143
|
+
this.proceed(file, (err, res) => {
|
|
144
|
+
if (err) {
|
|
145
|
+
return reject(err);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return resolve(res);
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Apply the rotation process if the `file` size exceeds the `SIZE_LIMIT`.
|
|
155
|
+
*
|
|
156
|
+
* @param {string} file
|
|
157
|
+
*/
|
|
158
|
+
async proceedFile(file) {
|
|
159
|
+
if (!fs.existsSync(file)) {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (fs.statSync(file).size > 0) {
|
|
164
|
+
await this.proceedSync(file);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
log.deleteOldLogfiles(file, this.retain);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Apply the rotation process of all log files of `app` where the file size exceeds the`SIZE_LIMIT`.
|
|
172
|
+
*
|
|
173
|
+
* @param {Object} app
|
|
174
|
+
*/
|
|
175
|
+
async proceedPm2App(app) {
|
|
176
|
+
// Check all log path
|
|
177
|
+
// Note: If same file is defined for multiple purposes, it will be processed once only.
|
|
178
|
+
if (app.pm2_env.pm_out_log_path) {
|
|
179
|
+
await this.proceedFile(app.pm2_env.pm_out_log_path);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (app.pm2_env.pm_err_log_path && app.pm2_env.pm_err_log_path !== app.pm2_env.pm_out_log_path) {
|
|
183
|
+
await this.proceedFile(app.pm2_env.pm_err_log_path);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (
|
|
187
|
+
app.pm2_env.pm_log_path &&
|
|
188
|
+
app.pm2_env.pm_log_path !== app.pm2_env.pm_out_log_path &&
|
|
189
|
+
app.pm2_env.pm_log_path !== app.pm2_env.pm_err_log_path
|
|
190
|
+
) {
|
|
191
|
+
await this.proceedFile(app.pm2_env.pm_log_path);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
};
|