@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,187 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const fs = require('fs-extra');
|
|
3
|
+
const { Throttle } = require('stream-throttle');
|
|
4
|
+
|
|
5
|
+
const { BLOCKLET_BUNDLE_FOLDER } = require('@blocklet/constant');
|
|
6
|
+
|
|
7
|
+
const defaultLogger = require('@abtnode/logger')('@abtnode/core:resolve-download');
|
|
8
|
+
|
|
9
|
+
const { getBlockletMeta } = require('../../util');
|
|
10
|
+
|
|
11
|
+
const { ensureBlockletExpanded, expandTarball, getBundleDir } = require('../../util/blocklet');
|
|
12
|
+
|
|
13
|
+
const asyncFs = fs.promises;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* decompress file, format dir and move to installDir
|
|
17
|
+
* @param {string} src file
|
|
18
|
+
* @param {{
|
|
19
|
+
* removeTarFile: boolean;
|
|
20
|
+
* logger;
|
|
21
|
+
* nodeInfo: { enableDocker: boolean };
|
|
22
|
+
* }} option
|
|
23
|
+
*/
|
|
24
|
+
const resolveDownload = async (tarFile, dist, { removeTarFile = true, logger = defaultLogger, onProgress } = {}) => {
|
|
25
|
+
const downloadDir = path.join(path.dirname(tarFile), path.basename(tarFile, path.extname(tarFile)));
|
|
26
|
+
const tmp = `${downloadDir}-tmp`;
|
|
27
|
+
try {
|
|
28
|
+
if (typeof onProgress === 'function') {
|
|
29
|
+
onProgress({ name: 'blocklet.tar.gz' });
|
|
30
|
+
}
|
|
31
|
+
await expandTarball({ source: tarFile, dest: tmp, strip: 0 });
|
|
32
|
+
} catch (error) {
|
|
33
|
+
logger.error('expand blocklet tar file error', { error });
|
|
34
|
+
throw error;
|
|
35
|
+
} finally {
|
|
36
|
+
if (removeTarFile) {
|
|
37
|
+
fs.removeSync(tarFile);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
let installDir;
|
|
41
|
+
let meta;
|
|
42
|
+
try {
|
|
43
|
+
// resolve dir
|
|
44
|
+
let dir = tmp;
|
|
45
|
+
const files = await asyncFs.readdir(dir);
|
|
46
|
+
if (files.includes('package')) {
|
|
47
|
+
dir = path.join(tmp, 'package');
|
|
48
|
+
} else if (files.length === 1) {
|
|
49
|
+
const d = path.join(dir, files[0]);
|
|
50
|
+
if ((await asyncFs.stat(d)).isDirectory()) {
|
|
51
|
+
dir = d;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (fs.existsSync(path.join(dir, BLOCKLET_BUNDLE_FOLDER))) {
|
|
56
|
+
dir = path.join(dir, BLOCKLET_BUNDLE_FOLDER);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
logger.info('Move downloadDir to installDir', { downloadDir });
|
|
60
|
+
await fs.move(dir, downloadDir, { overwrite: true });
|
|
61
|
+
fs.removeSync(tmp);
|
|
62
|
+
|
|
63
|
+
meta = getBlockletMeta(downloadDir);
|
|
64
|
+
const { name, version } = meta;
|
|
65
|
+
|
|
66
|
+
if (typeof onProgress === 'function') {
|
|
67
|
+
onProgress({ name: 'blocklet.zip' });
|
|
68
|
+
}
|
|
69
|
+
await ensureBlockletExpanded(meta, downloadDir);
|
|
70
|
+
|
|
71
|
+
installDir = getBundleDir(dist, meta);
|
|
72
|
+
if (fs.existsSync(installDir)) {
|
|
73
|
+
fs.removeSync(installDir);
|
|
74
|
+
logger.info('cleanup blocklet upgrade dir', { name, version, installDir });
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
fs.mkdirSync(installDir, { recursive: true });
|
|
78
|
+
|
|
79
|
+
await fs.move(downloadDir, installDir, { overwrite: true });
|
|
80
|
+
} catch (error) {
|
|
81
|
+
fs.removeSync(downloadDir);
|
|
82
|
+
fs.removeSync(tmp);
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return { meta, installDir };
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const resolveDiffDownload = async (
|
|
90
|
+
tarFile,
|
|
91
|
+
dist,
|
|
92
|
+
{ meta: oldMeta, deleteSet, cwd = '/', logger = defaultLogger } = {}
|
|
93
|
+
) => {
|
|
94
|
+
// eslint-disable-next-line no-param-reassign
|
|
95
|
+
tarFile = path.join(cwd, tarFile);
|
|
96
|
+
// eslint-disable-next-line no-param-reassign
|
|
97
|
+
dist = path.join(cwd, dist);
|
|
98
|
+
|
|
99
|
+
logger.info('Resolve diff download', { tarFile, cwd });
|
|
100
|
+
const downloadDir = path.join(path.dirname(tarFile), path.basename(tarFile, path.extname(tarFile)));
|
|
101
|
+
const diffDir = `${downloadDir}-diff`;
|
|
102
|
+
try {
|
|
103
|
+
await expandTarball({ source: tarFile, dest: diffDir, strip: 0 });
|
|
104
|
+
fs.removeSync(tarFile);
|
|
105
|
+
} catch (error) {
|
|
106
|
+
fs.removeSync(tarFile);
|
|
107
|
+
logger.error('expand blocklet tar file error', { error });
|
|
108
|
+
throw error;
|
|
109
|
+
}
|
|
110
|
+
logger.info('Copy installDir to downloadDir', { installDir: dist, downloadDir });
|
|
111
|
+
await fs.copy(getBundleDir(dist, oldMeta), downloadDir);
|
|
112
|
+
try {
|
|
113
|
+
// delete
|
|
114
|
+
logger.info('Delete files from downloadDir', { fileNum: deleteSet.length });
|
|
115
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
116
|
+
for (const file of deleteSet) {
|
|
117
|
+
/* eslint-disable no-await-in-loop */
|
|
118
|
+
await fs.remove(path.join(downloadDir, file));
|
|
119
|
+
}
|
|
120
|
+
// walk & cover
|
|
121
|
+
logger.info('Move files from diffDir to downloadDir', { diffDir, downloadDir });
|
|
122
|
+
const walkDiff = async (dir) => {
|
|
123
|
+
const files = await asyncFs.readdir(dir);
|
|
124
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
125
|
+
for (const file of files) {
|
|
126
|
+
const p = path.join(dir, file);
|
|
127
|
+
const stat = await asyncFs.stat(p);
|
|
128
|
+
if (stat.isDirectory()) {
|
|
129
|
+
await walkDiff(p);
|
|
130
|
+
} else if (stat.isFile()) {
|
|
131
|
+
await fs.move(p, path.join(downloadDir, path.relative(diffDir, p)), { overwrite: true });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
await walkDiff(diffDir);
|
|
136
|
+
fs.removeSync(diffDir);
|
|
137
|
+
const meta = getBlockletMeta(downloadDir);
|
|
138
|
+
|
|
139
|
+
await ensureBlockletExpanded(meta, downloadDir);
|
|
140
|
+
|
|
141
|
+
// move to installDir
|
|
142
|
+
const bundleDir = getBundleDir(dist, meta);
|
|
143
|
+
logger.info('Move downloadDir to installDir', { downloadDir, bundleDir });
|
|
144
|
+
await fs.move(downloadDir, bundleDir, { overwrite: true });
|
|
145
|
+
|
|
146
|
+
return { meta, installDir: bundleDir };
|
|
147
|
+
} catch (error) {
|
|
148
|
+
fs.removeSync(downloadDir);
|
|
149
|
+
fs.removeSync(diffDir);
|
|
150
|
+
throw error;
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* const { filename, mimetype, encoding, createReadStream } = await uploader;
|
|
156
|
+
*/
|
|
157
|
+
const downloadFromUpload = async (uploader, { downloadDir, logger = defaultLogger } = {}) => {
|
|
158
|
+
const cwd = downloadDir;
|
|
159
|
+
const { filename, createReadStream } = await uploader;
|
|
160
|
+
const tarFile = path.join(cwd, `${path.basename(filename, path.extname(filename))}.tgz`);
|
|
161
|
+
await fs.ensureDir(cwd);
|
|
162
|
+
await new Promise((resolve, reject) => {
|
|
163
|
+
const readStream = createReadStream();
|
|
164
|
+
const writeStream = fs.createWriteStream(tarFile);
|
|
165
|
+
readStream
|
|
166
|
+
.pipe(new Throttle({ rate: 1024 * 1024 * 20 })) // 20MB
|
|
167
|
+
.pipe(writeStream);
|
|
168
|
+
readStream.on('error', (error) => {
|
|
169
|
+
logger.error('File upload read stream failed', { error });
|
|
170
|
+
writeStream.destroy(new Error(`File upload read stream failed, ${error.message}`));
|
|
171
|
+
});
|
|
172
|
+
writeStream.on('error', (error) => {
|
|
173
|
+
logger.error('File upload write stream failed', { error });
|
|
174
|
+
fs.removeSync(tarFile);
|
|
175
|
+
reject(error);
|
|
176
|
+
});
|
|
177
|
+
writeStream.on('finish', resolve);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
return { tarFile };
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
module.exports = {
|
|
184
|
+
resolveDownload,
|
|
185
|
+
resolveDiffDownload,
|
|
186
|
+
downloadFromUpload,
|
|
187
|
+
};
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
const uniqBy = require('lodash/uniqBy');
|
|
2
|
+
const cloneDeep = require('lodash/cloneDeep');
|
|
3
|
+
const security = require('@abtnode/util/lib/security');
|
|
4
|
+
const { BLOCKLET_CONFIGURABLE_KEY } = require('@blocklet/constant');
|
|
5
|
+
|
|
6
|
+
const removeDeletedMetaConfigs = (oldConfigs, newConfigs) =>
|
|
7
|
+
oldConfigs.filter((old) => newConfigs.some(({ key, name }) => [key, name].includes(old.key)));
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 从 blocklet.yml:environment 中导入配置:配置项字段是 name
|
|
11
|
+
* 主动配置:配置项字段是 key
|
|
12
|
+
*
|
|
13
|
+
* 自定义配置(customConfigs): 配置项的 custom 为 true
|
|
14
|
+
* 系统配置(appConfigs): 系统保留的配置项名,且 custom 为 false
|
|
15
|
+
* 组件配置(metaConfigs): 配置项的 custom 为 false, 且不是系统保留的配置项名
|
|
16
|
+
* 如果从 blocklet.yml 中导入组件配置,会删除已经删除的配置项
|
|
17
|
+
*/
|
|
18
|
+
const mergeConfigs = ({ old: oldConfigs, cur: newConfigs = [], did = '', dek = '' }) => {
|
|
19
|
+
const enableSecurity = dek && did;
|
|
20
|
+
|
|
21
|
+
const isConfigFromMeta = newConfigs.every((x) => x.key);
|
|
22
|
+
|
|
23
|
+
const customConfigs = (oldConfigs || []).filter((x) => x.custom);
|
|
24
|
+
const appConfigs = (oldConfigs || []).filter((x) => !x.custom && !!BLOCKLET_CONFIGURABLE_KEY[x.key]);
|
|
25
|
+
|
|
26
|
+
const allMetaConfigs = (oldConfigs || []).filter((x) => !x.custom && !BLOCKLET_CONFIGURABLE_KEY[x.key]);
|
|
27
|
+
|
|
28
|
+
const metaConfigs = isConfigFromMeta ? allMetaConfigs : removeDeletedMetaConfigs(allMetaConfigs, newConfigs);
|
|
29
|
+
|
|
30
|
+
// oldConfig 表示从数据库中可以用的字段
|
|
31
|
+
const oldConfig = [...metaConfigs, ...appConfigs, ...customConfigs].reduce((acc, x) => {
|
|
32
|
+
acc[x.key] = {
|
|
33
|
+
value: x.value,
|
|
34
|
+
required: x.required,
|
|
35
|
+
description: x.description || '',
|
|
36
|
+
secure: x.secure || false,
|
|
37
|
+
validation: x.validation || '',
|
|
38
|
+
custom: x.custom,
|
|
39
|
+
shared: x.shared,
|
|
40
|
+
};
|
|
41
|
+
return acc;
|
|
42
|
+
}, {});
|
|
43
|
+
|
|
44
|
+
// newConfig 为用户传的,也可以是从环境变量中去读的
|
|
45
|
+
const uniqConfigs = uniqBy(newConfigs, (x) => x.key || x.name);
|
|
46
|
+
|
|
47
|
+
// `BLOCKLET_*` and `ABT_NODE_*` vars can only be set by Blocklet Server Daemon with only a few exceptions.
|
|
48
|
+
const newConfig = cloneDeep(
|
|
49
|
+
uniqConfigs.filter((x) => {
|
|
50
|
+
const key = x.key || x.name;
|
|
51
|
+
|
|
52
|
+
if (!key) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (BLOCKLET_CONFIGURABLE_KEY[key]) {
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return !(key.toString().startsWith('ABT_NODE_') || key.toString().startsWith('BLOCKLET_'));
|
|
61
|
+
})
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
if (enableSecurity) {
|
|
65
|
+
newConfig.forEach((x) => {
|
|
66
|
+
if (x.secure || oldConfig[x.key]?.secure) {
|
|
67
|
+
x.value = security.encrypt(x.value, did, dek);
|
|
68
|
+
if (x.default) {
|
|
69
|
+
x.default = security.encrypt(x.default, did, dek);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
newConfig.forEach((config) => {
|
|
76
|
+
const { name, key, value, default: defaultVal, required, description, secure, validation, custom, shared } = config;
|
|
77
|
+
// 新增、更新或者删除
|
|
78
|
+
if (key) {
|
|
79
|
+
const origin = oldConfig[key] || {};
|
|
80
|
+
oldConfig[key] = {
|
|
81
|
+
value,
|
|
82
|
+
required: required === undefined ? !!origin.required : required,
|
|
83
|
+
description: description || origin.description || '',
|
|
84
|
+
validation: validation || origin.validation || '',
|
|
85
|
+
secure: secure === undefined ? !!origin.secure : secure,
|
|
86
|
+
custom: custom === undefined ? !!origin.custom : custom,
|
|
87
|
+
shared: shared === undefined ? origin.shared : shared,
|
|
88
|
+
};
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
// 安装 或者 重装
|
|
92
|
+
if (name) {
|
|
93
|
+
// 重装时 name 和 value不覆盖
|
|
94
|
+
if (oldConfig[name]) {
|
|
95
|
+
const oldSecure = oldConfig[name].secure;
|
|
96
|
+
const oldValue =
|
|
97
|
+
enableSecurity && oldSecure ? security.decrypt(oldConfig[name].value, did, dek) : oldConfig[name].value;
|
|
98
|
+
|
|
99
|
+
const newSecure = secure === undefined ? false : secure;
|
|
100
|
+
|
|
101
|
+
oldConfig[name] = {
|
|
102
|
+
value: enableSecurity && newSecure ? security.encrypt(oldValue, did, dek) : oldValue,
|
|
103
|
+
required: required === undefined ? false : required,
|
|
104
|
+
description: description || '',
|
|
105
|
+
validation: validation || '',
|
|
106
|
+
secure: newSecure,
|
|
107
|
+
custom: custom === undefined ? false : custom,
|
|
108
|
+
shared,
|
|
109
|
+
};
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// 新增添加
|
|
114
|
+
oldConfig[name] = {
|
|
115
|
+
value: defaultVal,
|
|
116
|
+
required: required === undefined ? false : required,
|
|
117
|
+
description: description || '',
|
|
118
|
+
validation: validation || '',
|
|
119
|
+
secure: secure === undefined ? false : secure,
|
|
120
|
+
custom: custom === undefined ? false : custom,
|
|
121
|
+
shared,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const mergedObj = Object.assign({}, oldConfig);
|
|
127
|
+
const mergedConfig = Object.keys(mergedObj)
|
|
128
|
+
.filter((x) => typeof mergedObj[x].value !== 'undefined') // removes the key that have value set to undefined
|
|
129
|
+
.map((key) => ({
|
|
130
|
+
key,
|
|
131
|
+
value: mergedObj[key].value,
|
|
132
|
+
required: mergedObj[key].required,
|
|
133
|
+
description: mergedObj[key].description,
|
|
134
|
+
secure: mergedObj[key].secure,
|
|
135
|
+
validation: mergedObj[key].validation,
|
|
136
|
+
custom: mergedObj[key].custom,
|
|
137
|
+
shared: mergedObj[key].shared,
|
|
138
|
+
}));
|
|
139
|
+
|
|
140
|
+
return mergedConfig;
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
const parseConfigs = ({ data, did, dek }) => {
|
|
144
|
+
const enableSecurity = dek && did;
|
|
145
|
+
|
|
146
|
+
if (enableSecurity && Array.isArray(data)) {
|
|
147
|
+
return cloneDeep(data).map((x) => {
|
|
148
|
+
if (x.secure) {
|
|
149
|
+
x.value = security.decrypt(x.value, did, dek);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return x;
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return data;
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const encryptConfigs = ({ data, did, dek }) => {
|
|
160
|
+
const enableSecurity = dek && did;
|
|
161
|
+
|
|
162
|
+
if (enableSecurity && Array.isArray(data)) {
|
|
163
|
+
data.forEach((x) => {
|
|
164
|
+
if (x.secure) {
|
|
165
|
+
x.value = security.encrypt(x.value, did, dek);
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return data;
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
module.exports = {
|
|
174
|
+
mergeConfigs,
|
|
175
|
+
parseConfigs,
|
|
176
|
+
encryptConfigs,
|
|
177
|
+
};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
const get = require('lodash/get');
|
|
2
|
+
const camelCase = require('lodash/camelCase');
|
|
3
|
+
const runScript = require('@abtnode/util/lib/run-script');
|
|
4
|
+
const { getSecurityNodeOptions } = require('@abtnode/util/lib/security');
|
|
5
|
+
|
|
6
|
+
// eslint-disable-next-line global-require
|
|
7
|
+
const logger = require('@abtnode/logger')(`${require('../../package.json').name}:blocklet:hooks`);
|
|
8
|
+
|
|
9
|
+
const { getSafeEnv } = require('../util');
|
|
10
|
+
const states = require('../states');
|
|
11
|
+
|
|
12
|
+
const runUserHook = async (label, hookName, args) => {
|
|
13
|
+
const {
|
|
14
|
+
appDir,
|
|
15
|
+
hooks,
|
|
16
|
+
env,
|
|
17
|
+
exitOnError = true,
|
|
18
|
+
silent = false,
|
|
19
|
+
notification,
|
|
20
|
+
did,
|
|
21
|
+
timeout,
|
|
22
|
+
output: outputFile,
|
|
23
|
+
error: errorFile,
|
|
24
|
+
} = args;
|
|
25
|
+
const hook = get(hooks, `[${hookName}]`) || get(hooks, `[${camelCase(hookName)}]`);
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
if (!hook) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
logger.info(`run hook:${hookName}:`, { label, hook });
|
|
33
|
+
|
|
34
|
+
const nodeInfo = await states.node.read();
|
|
35
|
+
await runScript(hook, [label, hookName].join(':'), {
|
|
36
|
+
cwd: appDir,
|
|
37
|
+
env: {
|
|
38
|
+
...getSafeEnv(env),
|
|
39
|
+
BLOCKLET_HOOK_NAME: hookName,
|
|
40
|
+
NODE_OPTIONS: getSecurityNodeOptions({ environmentObj: env, ...args }, nodeInfo.enableFileSystemIsolation),
|
|
41
|
+
},
|
|
42
|
+
silent,
|
|
43
|
+
timeout,
|
|
44
|
+
output: outputFile,
|
|
45
|
+
error: errorFile,
|
|
46
|
+
});
|
|
47
|
+
} catch (error) {
|
|
48
|
+
logger.error(`run ${hook} error:`, { label, error });
|
|
49
|
+
|
|
50
|
+
if (notification) {
|
|
51
|
+
notification.create({
|
|
52
|
+
title: `Run '${hook}' failed, check the description for more detailed error information`,
|
|
53
|
+
description: error.message,
|
|
54
|
+
entityType: 'blocklet',
|
|
55
|
+
entityId: did,
|
|
56
|
+
severity: 'error',
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (exitOnError) {
|
|
61
|
+
throw new Error(`Run [${label} - ${hookName}] failed: ${error.message}`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const preInstall = (label, ...args) => runUserHook(label, 'pre-install', ...args);
|
|
67
|
+
const postInstall = (label, ...args) => runUserHook(label, 'post-install', ...args);
|
|
68
|
+
const preConfig = (label, ...args) => runUserHook(label, 'pre-config', ...args);
|
|
69
|
+
const preFlight = (label, ...args) => runUserHook(get(label, 'meta.title') || label, 'pre-flight', ...args);
|
|
70
|
+
const preStart = (blocklet, options) => {
|
|
71
|
+
// check required environments
|
|
72
|
+
let environments = get(blocklet, 'meta.environments', []);
|
|
73
|
+
if (!Array.isArray(environments)) {
|
|
74
|
+
environments = [environments];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const tmp = environments.filter((e) => e.required && options.env[e.name] === undefined).map((e) => e.name);
|
|
78
|
+
if (tmp.length > 0) {
|
|
79
|
+
throw new Error(`Required environments is not set: ${tmp.join(',')}`);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return runUserHook(blocklet.meta.title, 'pre-start', options);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const postStart = (blocklet, ...args) => runUserHook(blocklet.meta.title, 'post-start', ...args);
|
|
86
|
+
const preUninstall = (label, ...args) => runUserHook(label, 'pre-uninstall', ...args);
|
|
87
|
+
const preStop = (label, ...args) => runUserHook(label, 'pre-stop', ...args);
|
|
88
|
+
|
|
89
|
+
module.exports = { preInstall, postInstall, preFlight, preStart, postStart, preUninstall, preStop, preConfig };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/* eslint-disable no-unused-vars */
|
|
2
|
+
const { EventEmitter } = require('events');
|
|
3
|
+
const logger = require('@abtnode/logger')('@abtnode/core:manager');
|
|
4
|
+
|
|
5
|
+
class BaseBlockletManager extends EventEmitter {
|
|
6
|
+
constructor() {
|
|
7
|
+
super();
|
|
8
|
+
|
|
9
|
+
this.setMaxListeners(100);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
install() {
|
|
13
|
+
throw new Error('Install must be implemented in sub class');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
start() {
|
|
17
|
+
throw new Error('Install must be implemented in sub class');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
stop() {
|
|
21
|
+
throw new Error('Stop must be implemented in sub class');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
restart() {
|
|
25
|
+
throw new Error('Restart must be implemented in sub class');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
reload() {
|
|
29
|
+
throw new Error('Reload must be implemented in sub class');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
delete() {
|
|
33
|
+
throw new Error('Delete must be implemented in sub class');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
detail() {
|
|
37
|
+
throw new Error('Detail must be implemented in sub class');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
list() {
|
|
41
|
+
throw new Error('List must be implemented in sub class');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
config() {
|
|
45
|
+
throw new Error('Config must be implemented in sub class');
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
module.exports = BaseBlockletManager;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const throttle = require('lodash/throttle');
|
|
3
|
+
const fs = require('fs-extra');
|
|
4
|
+
const logger = require('@abtnode/logger')('@abtnode/core:config-synchronizer');
|
|
5
|
+
const { encrypt } = require('@blocklet/sdk/lib/security');
|
|
6
|
+
const {
|
|
7
|
+
APP_CONFIG_FILE_PATH,
|
|
8
|
+
APP_CONFIG_DIR,
|
|
9
|
+
COMPONENT_ENV_FILE_NAME,
|
|
10
|
+
APP_CONFIG_PUBLIC_DIR,
|
|
11
|
+
} = require('@blocklet/constant');
|
|
12
|
+
|
|
13
|
+
const { findComponentByIdV2, getSharedConfigObj, getBlockletAppIdList } = require('@blocklet/meta/lib/util');
|
|
14
|
+
const { getBlockletLanguages, getBlockletPreferences } = require('@blocklet/env/lib/util');
|
|
15
|
+
const { getComponentsInternalInfo } = require('@blocklet/meta/lib/blocklet');
|
|
16
|
+
const { getComponentApiKey } = require('@abtnode/util/lib/blocklet');
|
|
17
|
+
|
|
18
|
+
const { getBlockletConfigObj } = require('../../util/blocklet');
|
|
19
|
+
|
|
20
|
+
const getValueFromEnvironments = (environments, key) => {
|
|
21
|
+
const env = (environments || []).find((x) => x.key === key);
|
|
22
|
+
return env ? env.value : undefined;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const isAppObj = (x) => typeof x === 'object' && x !== null && !!x.meta;
|
|
26
|
+
|
|
27
|
+
class ConfigSynchronizer {
|
|
28
|
+
constructor({ manager, states, wait = 5000 }) {
|
|
29
|
+
this.manager = manager;
|
|
30
|
+
this.wait = wait;
|
|
31
|
+
this.throttles = new Map();
|
|
32
|
+
this.states = states;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async syncAppConfig(did, { serverVersion: inputServerVersion } = {}) {
|
|
36
|
+
try {
|
|
37
|
+
const app = isAppObj(did) ? did : await this.manager.getBlocklet(did);
|
|
38
|
+
const serverVersion = inputServerVersion || (await this.states.node.read()).version;
|
|
39
|
+
const env = {
|
|
40
|
+
appId: app.appDid,
|
|
41
|
+
appPid: app.appPid,
|
|
42
|
+
appIds: getBlockletAppIdList(app),
|
|
43
|
+
appName: app.meta.title,
|
|
44
|
+
appDescription: app.meta.description,
|
|
45
|
+
appUrl: getValueFromEnvironments(app.environments, 'BLOCKLET_APP_URL'),
|
|
46
|
+
appStorageEndpoint: getValueFromEnvironments(app.environments, 'BLOCKLET_APP_SPACE_ENDPOINT'),
|
|
47
|
+
languages: getBlockletLanguages(getValueFromEnvironments(app.environments, 'BLOCKLET_APP_LANGUAGES')),
|
|
48
|
+
preferences: getBlockletPreferences(
|
|
49
|
+
(app.configs || []).reduce((acc, x) => {
|
|
50
|
+
acc[x.key] = x.value;
|
|
51
|
+
return acc;
|
|
52
|
+
}, {})
|
|
53
|
+
),
|
|
54
|
+
serverVersion,
|
|
55
|
+
initialized: app?.settings?.initialized,
|
|
56
|
+
};
|
|
57
|
+
const components = getComponentsInternalInfo(app);
|
|
58
|
+
|
|
59
|
+
const config = { env, components };
|
|
60
|
+
|
|
61
|
+
const dataDir = getValueFromEnvironments(app.environments, 'BLOCKLET_DATA_DIR');
|
|
62
|
+
|
|
63
|
+
await fs.outputFile(path.join(dataDir, APP_CONFIG_FILE_PATH), JSON.stringify(config));
|
|
64
|
+
} catch (error) {
|
|
65
|
+
logger.error('sync app config failed', { error });
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async syncComponentConfig(did, rootDid, { serverSk }) {
|
|
70
|
+
try {
|
|
71
|
+
const app = await this.manager.getBlocklet(rootDid);
|
|
72
|
+
const component = findComponentByIdV2(app, did);
|
|
73
|
+
|
|
74
|
+
const hasResources = !!component.meta.resource?.bundles?.length;
|
|
75
|
+
|
|
76
|
+
const env = {
|
|
77
|
+
...component.configObj,
|
|
78
|
+
...getSharedConfigObj(app, component, true),
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const componentApiKey = getComponentApiKey({
|
|
82
|
+
serverSk,
|
|
83
|
+
app,
|
|
84
|
+
component,
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const dataDir = getValueFromEnvironments(app.environments, 'BLOCKLET_DATA_DIR');
|
|
88
|
+
|
|
89
|
+
await fs.outputFile(
|
|
90
|
+
path.join(dataDir, APP_CONFIG_DIR, component.meta.did, COMPONENT_ENV_FILE_NAME),
|
|
91
|
+
encrypt(JSON.stringify(env), componentApiKey, component.meta.did)
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
if (hasResources) {
|
|
95
|
+
const publicEnv = {
|
|
96
|
+
...getBlockletConfigObj(component, { excludeSecure: true }),
|
|
97
|
+
...getSharedConfigObj(app, component, true),
|
|
98
|
+
};
|
|
99
|
+
await fs.outputFile(
|
|
100
|
+
path.join(dataDir, APP_CONFIG_PUBLIC_DIR, component.meta.did, COMPONENT_ENV_FILE_NAME),
|
|
101
|
+
JSON.stringify(publicEnv)
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
} catch (error) {
|
|
105
|
+
logger.error('sync component config failed', { error });
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async throttledSyncAppConfig(did) {
|
|
110
|
+
if (!this.throttles.has(did)) {
|
|
111
|
+
this.throttles.set(
|
|
112
|
+
did,
|
|
113
|
+
throttle(
|
|
114
|
+
async () => {
|
|
115
|
+
await this.syncAppConfig(did);
|
|
116
|
+
this.throttles.get(did).cancel();
|
|
117
|
+
this.throttles.delete(did);
|
|
118
|
+
},
|
|
119
|
+
this.wait,
|
|
120
|
+
{ leading: false }
|
|
121
|
+
)
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
await this.throttles.get(did)();
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
module.exports = ConfigSynchronizer;
|