@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,246 @@
|
|
|
1
|
+
const { EventEmitter } = require('events');
|
|
2
|
+
const fs = require('fs-extra');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const get = require('lodash/get');
|
|
5
|
+
const pick = require('lodash/pick');
|
|
6
|
+
const { toBase58 } = require('@ocap/util');
|
|
7
|
+
|
|
8
|
+
const defaultLogger = require('@abtnode/logger')('@abtnode/core:blocklet-downloader');
|
|
9
|
+
|
|
10
|
+
const { forEachComponentV2Sync, getComponentBundleId } = require('@blocklet/meta/lib/util');
|
|
11
|
+
|
|
12
|
+
const { BlockletSource, BLOCKLET_META_FILE, BLOCKLET_META_FILE_ALT, BLOCKLET_MODES } = require('@blocklet/constant');
|
|
13
|
+
|
|
14
|
+
const { getBundleDir } = require('../../util/blocklet');
|
|
15
|
+
const BundleDownloader = require('./bundle-downloader');
|
|
16
|
+
const { CACHE_KEY } = require('./constants');
|
|
17
|
+
|
|
18
|
+
const isSourceAccessible = (blocklet) =>
|
|
19
|
+
![BlockletSource.upload, BlockletSource.local, BlockletSource.custom].includes(blocklet.source);
|
|
20
|
+
|
|
21
|
+
const isMetaFileExist = (dir) => {
|
|
22
|
+
const blockletMetaFile = path.join(dir, BLOCKLET_META_FILE);
|
|
23
|
+
const blockletMetaFileAlt = path.join(dir, BLOCKLET_META_FILE_ALT);
|
|
24
|
+
|
|
25
|
+
return fs.existsSync(blockletMetaFile) || fs.existsSync(blockletMetaFileAlt);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param {blocklet} component
|
|
30
|
+
* @param {{
|
|
31
|
+
* cachedBundles: Array<{bundleId, integrity}>;
|
|
32
|
+
* }}
|
|
33
|
+
* @returns {boolean}
|
|
34
|
+
*/
|
|
35
|
+
const needDownload = (
|
|
36
|
+
component,
|
|
37
|
+
{ installDir, logger = defaultLogger, cachedBundles = [], skipCheckIntegrity } = {}
|
|
38
|
+
) => {
|
|
39
|
+
if (component.mode === BLOCKLET_MODES.DEVELOPMENT) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (component.source === BlockletSource.local) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const bundleId = getComponentBundleId(component);
|
|
48
|
+
const bundleDir = getBundleDir(installDir, component.meta);
|
|
49
|
+
|
|
50
|
+
// check bundle exist
|
|
51
|
+
|
|
52
|
+
if (!isMetaFileExist(bundleDir)) {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (!isSourceAccessible(component)) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (skipCheckIntegrity) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// check integrity
|
|
65
|
+
|
|
66
|
+
const cachedBundle = cachedBundles.find((x) => x.bundleId === bundleId);
|
|
67
|
+
|
|
68
|
+
// FIXME: 不是新安装的组件不应该 check, 不应该 block 安装成功 https://github.com/blocklet/launcher/actions/runs/4184577090/jobs/7250416272
|
|
69
|
+
if (!cachedBundle) {
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const integrity = get(component, 'meta.dist.integrity');
|
|
74
|
+
if (toBase58(integrity) === cachedBundle.integrity) {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
logger.error(`find duplicate bundle with different integrity when downloading ${component.meta.title}`, {
|
|
79
|
+
bundleId,
|
|
80
|
+
});
|
|
81
|
+
return true;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
class BlockletDownloader extends EventEmitter {
|
|
85
|
+
/**
|
|
86
|
+
* @param {{
|
|
87
|
+
* installDir: string,
|
|
88
|
+
* downloadDir: string,
|
|
89
|
+
* cache: {
|
|
90
|
+
* set: (key: string, value: any) => Promise
|
|
91
|
+
* get: (key: string) => Promise<any>
|
|
92
|
+
* },
|
|
93
|
+
* getNodeInfo: () => Promise<{ enableDocker: boolean }>
|
|
94
|
+
* }}
|
|
95
|
+
*/
|
|
96
|
+
constructor({ installDir, downloadDir, cache, logger = defaultLogger, getNodeInfo }) {
|
|
97
|
+
super();
|
|
98
|
+
|
|
99
|
+
this.installDir = installDir;
|
|
100
|
+
this.cache = cache;
|
|
101
|
+
this.logger = logger;
|
|
102
|
+
|
|
103
|
+
this.bundleDownloader = new BundleDownloader({
|
|
104
|
+
installDir,
|
|
105
|
+
downloadDir,
|
|
106
|
+
cache,
|
|
107
|
+
getNodeInfo,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* @param {{}} blocklet
|
|
113
|
+
* @param {{
|
|
114
|
+
* preDownload: ({ downloadList: Array<meta>, downloadComponentIds: Array<string> }) => any
|
|
115
|
+
* postDownload: ({ downloadList: Array<meta>, downloadComponentIds: Array<string>, isCancelled: boolean }) => any
|
|
116
|
+
* onProgress: ({ step?: string, ...extras }) => any
|
|
117
|
+
* skipCheckIntegrity: boolean
|
|
118
|
+
* }} [options={}]
|
|
119
|
+
* @return {*}
|
|
120
|
+
*/
|
|
121
|
+
async download(blocklet, options = {}) {
|
|
122
|
+
const {
|
|
123
|
+
meta: { name, title, did },
|
|
124
|
+
} = blocklet;
|
|
125
|
+
|
|
126
|
+
this.logger.info('Download Blocklet', { name, did });
|
|
127
|
+
|
|
128
|
+
const { preDownload = () => {}, postDownload = () => {}, skipCheckIntegrity, onProgress = () => {} } = options;
|
|
129
|
+
|
|
130
|
+
const { downloadComponentIds, downloadList, skipList } = await this.getDownloadList({
|
|
131
|
+
blocklet,
|
|
132
|
+
skipCheckIntegrity,
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
await preDownload({ downloadList, downloadComponentIds });
|
|
136
|
+
|
|
137
|
+
try {
|
|
138
|
+
// Fake progress events for cached bundles
|
|
139
|
+
skipList.forEach((meta) => {
|
|
140
|
+
const info = pick(meta, ['title', 'name', 'did', 'version']);
|
|
141
|
+
onProgress({ status: 'downloading', component: info });
|
|
142
|
+
setTimeout(
|
|
143
|
+
() => {
|
|
144
|
+
onProgress({ status: 'completed', component: info });
|
|
145
|
+
},
|
|
146
|
+
process.env.NODE_ENV === 'test' ? 0 : Math.random() * 800
|
|
147
|
+
);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
this.logger.info('Start Download Blocklet', {
|
|
151
|
+
name,
|
|
152
|
+
did,
|
|
153
|
+
bundles: downloadList.map((x) => get(x, 'dist.tarball')),
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
const results = await Promise.all(
|
|
157
|
+
downloadList.map((meta) => {
|
|
158
|
+
const url = meta.dist.tarball;
|
|
159
|
+
return this.bundleDownloader.download(meta, did, url, {
|
|
160
|
+
...options,
|
|
161
|
+
onProgress: (data) => {
|
|
162
|
+
onProgress({ ...data, component: pick(meta, ['title', 'name', 'did', 'version']) });
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
})
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
const isCancelled = results.some((x) => x.isCancelled);
|
|
169
|
+
|
|
170
|
+
await postDownload({ downloadList, downloadComponentIds, isCancelled });
|
|
171
|
+
|
|
172
|
+
if (isCancelled) {
|
|
173
|
+
return { isCancelled: true };
|
|
174
|
+
}
|
|
175
|
+
} catch (error) {
|
|
176
|
+
this.logger.error('Download blocklet failed', { did, name, title, error });
|
|
177
|
+
await this.bundleDownloader.cancelDownload(blocklet.meta.did);
|
|
178
|
+
throw error;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return { isCancelled: false };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
cancelDownload(did) {
|
|
185
|
+
return this.bundleDownloader.cancelDownload(did);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* @param {{
|
|
190
|
+
* blocklet;
|
|
191
|
+
* }}
|
|
192
|
+
* @returns {{
|
|
193
|
+
* downloadList: Array<import('@abtnode/client').BlockletMeta>;
|
|
194
|
+
* skipList: Array<import('@abtnode/client').BlockletMeta>;
|
|
195
|
+
* downloadComponentIds: Array<string>; // like: "z8ia1i2yq67x39vruqQTbkVcwCnqRGx8zSPsJ/z8iZwubkNdA1BfEUwc5NJpY2Jnfm7yEbyvmKS"
|
|
196
|
+
* }}
|
|
197
|
+
*/
|
|
198
|
+
async getDownloadList({ blocklet, skipCheckIntegrity }) {
|
|
199
|
+
const downloadComponentIds = [];
|
|
200
|
+
const scheduled = {};
|
|
201
|
+
const skipped = {};
|
|
202
|
+
|
|
203
|
+
const cachedBundles = (await this.cache.get(CACHE_KEY)) || [];
|
|
204
|
+
|
|
205
|
+
const handle = (component) => {
|
|
206
|
+
const bundleId = getComponentBundleId(component);
|
|
207
|
+
|
|
208
|
+
if (scheduled[bundleId]) {
|
|
209
|
+
this.logger.info(`skip download duplicate bundle ${bundleId}`);
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const needComponentDownload = needDownload(component, {
|
|
214
|
+
installDir: this.installDir,
|
|
215
|
+
cachedBundles,
|
|
216
|
+
skipCheckIntegrity,
|
|
217
|
+
});
|
|
218
|
+
if (!needComponentDownload) {
|
|
219
|
+
skipped[bundleId] = component.meta;
|
|
220
|
+
this.logger.info(`skip download existed bundle ${bundleId}`, { source: component.source });
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (!isSourceAccessible(component)) {
|
|
225
|
+
// should not throw error
|
|
226
|
+
// broken bundle should not block blocklet installing
|
|
227
|
+
// broken bundle should only block blocklet starting
|
|
228
|
+
this.logger.error(`Component bundle is broken and can not be recovered: ${bundleId}`);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
scheduled[bundleId] = component.meta;
|
|
233
|
+
downloadComponentIds.push(component.meta.did);
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
handle(blocklet);
|
|
237
|
+
forEachComponentV2Sync(blocklet, handle);
|
|
238
|
+
|
|
239
|
+
const downloadList = Object.values(scheduled);
|
|
240
|
+
const skipList = Object.values(skipped);
|
|
241
|
+
|
|
242
|
+
return { downloadList, skipList, downloadComponentIds };
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
module.exports = BlockletDownloader;
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
const { EventEmitter } = require('events');
|
|
2
|
+
const fs = require('fs-extra');
|
|
3
|
+
const { fileURLToPath } = require('url');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const pick = require('lodash/pick');
|
|
6
|
+
const cloneDeep = require('lodash/cloneDeep');
|
|
7
|
+
const { toBase58 } = require('@ocap/util');
|
|
8
|
+
|
|
9
|
+
const defaultLogger = require('@abtnode/logger')('@abtnode/core:bundle-downloader');
|
|
10
|
+
const downloadFile = require('@abtnode/util/lib/download-file');
|
|
11
|
+
const Lock = require('@abtnode/util/lib/lock');
|
|
12
|
+
|
|
13
|
+
const { getComponentBundleId } = require('@blocklet/meta/lib/util');
|
|
14
|
+
|
|
15
|
+
const { verifyIntegrity } = require('../../util/blocklet');
|
|
16
|
+
const { resolveDownload } = require('./resolve-download');
|
|
17
|
+
const { CACHE_KEY } = require('./constants');
|
|
18
|
+
|
|
19
|
+
class BundleDownloader extends EventEmitter {
|
|
20
|
+
/**
|
|
21
|
+
* @param {{
|
|
22
|
+
* cache: {
|
|
23
|
+
* set: (key: string, value: any) => Promise
|
|
24
|
+
* get: (key: string) => Promise<any>
|
|
25
|
+
* }
|
|
26
|
+
* getNodeInfo: () => Promise<{ enableDocker: boolean }>
|
|
27
|
+
* }}
|
|
28
|
+
*/
|
|
29
|
+
constructor({ installDir, downloadDir, cache, logger = defaultLogger, getNodeInfo }) {
|
|
30
|
+
super();
|
|
31
|
+
|
|
32
|
+
this.installDir = installDir;
|
|
33
|
+
this.downloadDir = downloadDir;
|
|
34
|
+
this.cache = cache;
|
|
35
|
+
this.logger = logger;
|
|
36
|
+
this.getNodeInfo = getNodeInfo;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* { did: Map({ <childDid>: <downloadFile.cancelCtrl> }) }
|
|
40
|
+
*/
|
|
41
|
+
this.downloadCtrls = {};
|
|
42
|
+
/**
|
|
43
|
+
* { [download-did-version]: Lock }
|
|
44
|
+
*/
|
|
45
|
+
this.downloadLocks = {};
|
|
46
|
+
|
|
47
|
+
this.cacheTarBallLock = new Lock();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
*
|
|
52
|
+
*
|
|
53
|
+
* @param {*} meta
|
|
54
|
+
* @param {*} rootDid
|
|
55
|
+
* @param {*} url
|
|
56
|
+
* @param {{}} [context={ nodeInfo: { enableDocker: boolean } }]
|
|
57
|
+
* @return {*}
|
|
58
|
+
* @memberof BlockletManager
|
|
59
|
+
*/
|
|
60
|
+
async download(meta, rootDid, url, context = {}) {
|
|
61
|
+
const { bundleName: name, bundleDid: did, title, version, dist = {} } = meta;
|
|
62
|
+
const { tarball, integrity } = dist;
|
|
63
|
+
|
|
64
|
+
const lockName = `download-${did}-${version}`;
|
|
65
|
+
let lock = this.downloadLocks[lockName];
|
|
66
|
+
if (!lock) {
|
|
67
|
+
lock = new Lock(lockName);
|
|
68
|
+
this.downloadLocks[lockName] = lock;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const onProgress = context.onProgress || (() => {});
|
|
72
|
+
const nodeInfo = await this.getNodeInfo();
|
|
73
|
+
|
|
74
|
+
try {
|
|
75
|
+
await lock.acquire();
|
|
76
|
+
this.logger.info('downloaded blocklet for installing', { name, version, tarball, integrity });
|
|
77
|
+
const cwd = path.join(this.downloadDir, 'download', name);
|
|
78
|
+
await fs.ensureDir(cwd);
|
|
79
|
+
this.logger.info('start download blocklet', { name, version, cwd, tarball, integrity });
|
|
80
|
+
onProgress({ status: 'downloading' });
|
|
81
|
+
let tarballPath;
|
|
82
|
+
try {
|
|
83
|
+
tarballPath = await this._downloadTarball({
|
|
84
|
+
name,
|
|
85
|
+
did,
|
|
86
|
+
version,
|
|
87
|
+
cwd,
|
|
88
|
+
tarball,
|
|
89
|
+
integrity,
|
|
90
|
+
verify: true,
|
|
91
|
+
ctrlStore: this.downloadCtrls,
|
|
92
|
+
rootDid,
|
|
93
|
+
url,
|
|
94
|
+
context,
|
|
95
|
+
onProgress,
|
|
96
|
+
});
|
|
97
|
+
} catch (error) {
|
|
98
|
+
this.logger.error('download bundle failed', { did, title, version, url, error });
|
|
99
|
+
throw error;
|
|
100
|
+
}
|
|
101
|
+
this.logger.info('downloaded blocklet tar file', { name, version, tarballPath });
|
|
102
|
+
if (tarballPath === downloadFile.CANCEL) {
|
|
103
|
+
lock.release();
|
|
104
|
+
return { isCancelled: true };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// resolve tarball and mv tarball to cache after resolved
|
|
108
|
+
await resolveDownload(tarballPath, this.installDir, {
|
|
109
|
+
removeTarFile: false,
|
|
110
|
+
nodeInfo,
|
|
111
|
+
onProgress: ({ name: _name }) => {
|
|
112
|
+
onProgress({ status: 'extracting', name: _name });
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
onProgress({ status: 'completed' });
|
|
116
|
+
await this._addCacheTarFile(tarballPath, integrity, getComponentBundleId({ meta }));
|
|
117
|
+
|
|
118
|
+
this.logger.info('resolved blocklet tar file to install dir', { name, version });
|
|
119
|
+
lock.release();
|
|
120
|
+
return { isCancelled: false };
|
|
121
|
+
} catch (error) {
|
|
122
|
+
lock.release();
|
|
123
|
+
throw error;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// eslint-disable-next-line no-unused-vars
|
|
128
|
+
cancelDownload(rootDid) {
|
|
129
|
+
if (this.downloadCtrls[rootDid]) {
|
|
130
|
+
for (const cancelCtrl of this.downloadCtrls[rootDid].values()) {
|
|
131
|
+
cancelCtrl.cancel();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
*
|
|
138
|
+
*
|
|
139
|
+
* @param {{
|
|
140
|
+
* url: string,
|
|
141
|
+
* cwd: string,
|
|
142
|
+
* tarball: string,
|
|
143
|
+
* did: string,
|
|
144
|
+
* integrity: string,
|
|
145
|
+
* verify: boolean = true,
|
|
146
|
+
* ctrlStore: {},
|
|
147
|
+
* rootDid: string,
|
|
148
|
+
* context: {} = {},
|
|
149
|
+
* }} { url, cwd, tarball, did, integrity, verify = true, ctrlStore = {}, rootDid, context = {} }
|
|
150
|
+
* @return {*}
|
|
151
|
+
* @memberof BlockletManager
|
|
152
|
+
*/
|
|
153
|
+
async _downloadTarball({
|
|
154
|
+
url,
|
|
155
|
+
cwd,
|
|
156
|
+
tarball,
|
|
157
|
+
did,
|
|
158
|
+
integrity,
|
|
159
|
+
verify = true,
|
|
160
|
+
ctrlStore = {},
|
|
161
|
+
rootDid,
|
|
162
|
+
context = {},
|
|
163
|
+
onProgress = () => {},
|
|
164
|
+
}) {
|
|
165
|
+
fs.mkdirSync(cwd, { recursive: true });
|
|
166
|
+
|
|
167
|
+
const tarballName = url.split('/').slice(-1)[0];
|
|
168
|
+
|
|
169
|
+
const tarballPath = path.join(cwd, tarballName);
|
|
170
|
+
|
|
171
|
+
const { protocol } = new URL(url);
|
|
172
|
+
|
|
173
|
+
const cachedTarFile = await this._getCachedTarFile(integrity);
|
|
174
|
+
if (cachedTarFile) {
|
|
175
|
+
this.logger.info('found cache tarFile', { did, tarballName, integrity });
|
|
176
|
+
|
|
177
|
+
await this.cacheTarBallLock.acquire();
|
|
178
|
+
try {
|
|
179
|
+
await fs.move(cachedTarFile, tarballPath, { overwrite: true });
|
|
180
|
+
} catch (error) {
|
|
181
|
+
await this.cacheTarBallLock.release();
|
|
182
|
+
this.logger.error('move cache tar file failed', { cachedTarFile, tarballPath, error });
|
|
183
|
+
throw error;
|
|
184
|
+
}
|
|
185
|
+
await this.cacheTarBallLock.release();
|
|
186
|
+
} else if (protocol.startsWith('file')) {
|
|
187
|
+
await fs.copy(decodeURIComponent(fileURLToPath(url)), tarballPath);
|
|
188
|
+
} else {
|
|
189
|
+
const cancelCtrl = new downloadFile.CancelCtrl();
|
|
190
|
+
|
|
191
|
+
if (!ctrlStore[rootDid]) {
|
|
192
|
+
ctrlStore[rootDid] = new Map();
|
|
193
|
+
}
|
|
194
|
+
ctrlStore[rootDid].set(did, cancelCtrl);
|
|
195
|
+
|
|
196
|
+
const headers = pick(context.headers ? cloneDeep(context.headers) : {}, [
|
|
197
|
+
'x-server-did',
|
|
198
|
+
'x-server-public-key',
|
|
199
|
+
'x-server-signature',
|
|
200
|
+
]);
|
|
201
|
+
const exist = (context.downloadTokenList || []).find((x) => x.did === did);
|
|
202
|
+
if (exist) {
|
|
203
|
+
headers['x-download-token'] = exist.token;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
await downloadFile(
|
|
207
|
+
url,
|
|
208
|
+
path.join(cwd, tarballName),
|
|
209
|
+
{
|
|
210
|
+
cancelCtrl,
|
|
211
|
+
onProgress: ({ total, current }) => {
|
|
212
|
+
onProgress({ status: 'downloading', total, current });
|
|
213
|
+
},
|
|
214
|
+
},
|
|
215
|
+
{
|
|
216
|
+
...context,
|
|
217
|
+
headers,
|
|
218
|
+
}
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
if (ctrlStore[rootDid]) {
|
|
222
|
+
ctrlStore[rootDid].delete(did);
|
|
223
|
+
if (!ctrlStore[rootDid].size) {
|
|
224
|
+
delete ctrlStore[rootDid];
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (cancelCtrl.isCancelled) {
|
|
229
|
+
return downloadFile.CANCEL;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (verify) {
|
|
234
|
+
try {
|
|
235
|
+
await verifyIntegrity({ file: tarballPath, integrity });
|
|
236
|
+
} catch (error) {
|
|
237
|
+
this.logger.error('verify integrity error', { error, tarball, url });
|
|
238
|
+
throw new Error(`${tarball} integrity check failed.`);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return tarballPath;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* use LRU algorithm
|
|
247
|
+
*/
|
|
248
|
+
async _addCacheTarFile(tarballPath, integrity, bundleId) {
|
|
249
|
+
// eslint-disable-next-line no-param-reassign
|
|
250
|
+
integrity = toBase58(integrity);
|
|
251
|
+
|
|
252
|
+
// move tarball to cache dir
|
|
253
|
+
const cwd = path.join(this.downloadDir, 'download-cache');
|
|
254
|
+
const cachePath = path.join(cwd, `${integrity}.tar.gz`);
|
|
255
|
+
await fs.ensureDir(cwd);
|
|
256
|
+
await fs.move(tarballPath, cachePath, { overwrite: true });
|
|
257
|
+
|
|
258
|
+
const cacheList = (await this.cache.get(CACHE_KEY)) || [];
|
|
259
|
+
const exist = cacheList.find((x) => x.bundleId === bundleId && x.integrity === integrity);
|
|
260
|
+
|
|
261
|
+
// update
|
|
262
|
+
if (exist) {
|
|
263
|
+
this.logger.info('update cache tarFile', { base58: integrity });
|
|
264
|
+
exist.accessAt = Date.now();
|
|
265
|
+
await this.cache.set(CACHE_KEY, cacheList);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// add
|
|
270
|
+
cacheList.push({ integrity, accessAt: Date.now(), bundleId });
|
|
271
|
+
if (cacheList.length > 50) {
|
|
272
|
+
// find and remove
|
|
273
|
+
let minIndex = 0;
|
|
274
|
+
let min = cacheList[0];
|
|
275
|
+
cacheList.forEach((x, i) => {
|
|
276
|
+
if (x.accessAt < min.accessAt) {
|
|
277
|
+
minIndex = i;
|
|
278
|
+
min = x;
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
cacheList.splice(minIndex, 1);
|
|
283
|
+
|
|
284
|
+
const removeFile = path.join(cwd, `${min.integrity}.tar.gz`);
|
|
285
|
+
await this.cacheTarBallLock.acquire();
|
|
286
|
+
try {
|
|
287
|
+
await fs.remove(removeFile);
|
|
288
|
+
} catch (error) {
|
|
289
|
+
this.logger.error('remove cache tar file failed', { file: removeFile, error });
|
|
290
|
+
}
|
|
291
|
+
await this.cacheTarBallLock.release();
|
|
292
|
+
|
|
293
|
+
this.logger.info('remove cache tarFile', { base58: min.integrity });
|
|
294
|
+
}
|
|
295
|
+
this.logger.info('add cache tarFile', { base58: integrity });
|
|
296
|
+
|
|
297
|
+
// update
|
|
298
|
+
await this.cache.set(CACHE_KEY, cacheList);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
_getCachedTarFile(integrity) {
|
|
302
|
+
// eslint-disable-next-line no-param-reassign
|
|
303
|
+
integrity = toBase58(integrity);
|
|
304
|
+
|
|
305
|
+
const cwd = path.join(this.downloadDir, 'download-cache');
|
|
306
|
+
const cachePath = path.join(cwd, `${integrity}.tar.gz`);
|
|
307
|
+
|
|
308
|
+
if (fs.existsSync(cachePath)) {
|
|
309
|
+
return cachePath;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
return null;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
module.exports = BundleDownloader;
|