@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
package/lib/client/Redis.js
DELETED
|
@@ -1,787 +0,0 @@
|
|
|
1
|
-
var Pool = require('../pattern/Pool.js').Pool;
|
|
2
|
-
var CredentialsProvider = require('../credentials/CredentialsProvider.js').CredentialsProvider;
|
|
3
|
-
var Event = require('../event/Event').Event;
|
|
4
|
-
var EventDispatcher = require('../event/EventDispatcher').EventDispatcher;
|
|
5
|
-
|
|
6
|
-
var redis = require('redis');
|
|
7
|
-
var instances = [];
|
|
8
|
-
var emptyFn = function () {};
|
|
9
|
-
|
|
10
|
-
var Redis = function (cp) {
|
|
11
|
-
return Redis.getInstance(cp);
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
Redis.getInstance = function (cp) {
|
|
15
|
-
for (var i = 0, max = instances.length; i < max; i++) {
|
|
16
|
-
if (instances[i][0] === cp) {
|
|
17
|
-
return instances[i][1];
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
var obj = Object.create(Redis.prototype);
|
|
21
|
-
EventDispatcher.call(obj);
|
|
22
|
-
|
|
23
|
-
obj._debugEnabled = true;
|
|
24
|
-
obj._options = {
|
|
25
|
-
db: false,
|
|
26
|
-
updateTimeout: 1000,
|
|
27
|
-
defaultConnectionsCount: 1,
|
|
28
|
-
redisConfig: {
|
|
29
|
-
'hbInterval': false,
|
|
30
|
-
'hbTimeout': false,
|
|
31
|
-
'max_attempts': 0,
|
|
32
|
-
'return_buffers': true,
|
|
33
|
-
'enable_offline_queue': false
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
obj._hosts = {};
|
|
38
|
-
obj._credentials = null;
|
|
39
|
-
obj._readyClients = 0;
|
|
40
|
-
obj._isReady = false;
|
|
41
|
-
obj._onReadyCallback = false;
|
|
42
|
-
obj._sortedHosts = [];
|
|
43
|
-
|
|
44
|
-
cp.addEventListener(CredentialsProvider.Event.LOAD, function (e) {
|
|
45
|
-
obj._onCredentialsChange(null, e.data);
|
|
46
|
-
}, obj);
|
|
47
|
-
|
|
48
|
-
cp.get(function () {
|
|
49
|
-
obj._onCredentialsChange.apply(obj, arguments);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
obj._cp = cp;
|
|
53
|
-
instances.push([cp, obj]);
|
|
54
|
-
// in the old version it was running find out why?Maybe some bug with cp
|
|
55
|
-
obj._updateTimeout = setTimeout(obj._onCredentialsChange.bind(obj), obj._options.updateTimeout);
|
|
56
|
-
|
|
57
|
-
return obj;
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
Redis.prototype = Object.create(EventDispatcher.prototype);
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Enable/disable debug mode for redisClient
|
|
64
|
-
*
|
|
65
|
-
* @return {void}
|
|
66
|
-
*/
|
|
67
|
-
Redis.prototype._debug = function () {
|
|
68
|
-
if (!this._debugEnabled) {
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
console.log.apply(console, arguments);
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Gracefully kill unused redis numConnectionss using pool release and clean host object
|
|
77
|
-
*
|
|
78
|
-
* @return {void}
|
|
79
|
-
*/
|
|
80
|
-
Redis.prototype._cleanDeletedHosts = function () {
|
|
81
|
-
var hosts = Object.keys(this._hosts);
|
|
82
|
-
|
|
83
|
-
for (var i = 0, hostsL = hosts.length; i < hostsL; i++) {
|
|
84
|
-
var server = this._hosts[hosts[i]];
|
|
85
|
-
|
|
86
|
-
if (!server.toDelete) {
|
|
87
|
-
continue;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
var clientCount = server.pool.getKnownLength();
|
|
91
|
-
if (clientCount === 0) {
|
|
92
|
-
delete this._hosts[hosts[i]];
|
|
93
|
-
continue;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
for (var j = 0; j < clientCount; j++) {
|
|
97
|
-
this._deleteClient(hosts[i]);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Check if all connections are ready.
|
|
104
|
-
*
|
|
105
|
-
* @return {bool} - true if ready, otherwise false
|
|
106
|
-
*/
|
|
107
|
-
Redis.prototype.isReady = function () {
|
|
108
|
-
return this._isReady;
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Add onReady Function
|
|
113
|
-
*
|
|
114
|
-
* @param {Function} callback funct to be called when all clients are connected
|
|
115
|
-
* @return {void}
|
|
116
|
-
*/
|
|
117
|
-
Redis.prototype.onReady = function (callback) {
|
|
118
|
-
if (typeof callback !== 'function') {
|
|
119
|
-
return;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
this._onReadyCallback = callback;
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Update redis clients to config given from credentialProvider
|
|
127
|
-
*
|
|
128
|
-
* @param {Object} - Err
|
|
129
|
-
* @param {Object} - Credentials in CredentialProvider format
|
|
130
|
-
* @return {void}
|
|
131
|
-
*/
|
|
132
|
-
Redis.prototype._onCredentialsChange = function (error, credentials) {
|
|
133
|
-
var that = this;
|
|
134
|
-
|
|
135
|
-
if (error) {
|
|
136
|
-
console.error('Redis Credentials error', error);
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
if (this._updateTimeout) {
|
|
141
|
-
clearTimeout(this._updateTimeout);
|
|
142
|
-
this._updateTimeout = null;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
this._cleanDeletedHosts();
|
|
146
|
-
|
|
147
|
-
if (credentials) {
|
|
148
|
-
if (credentials.hasOwnProperty('connect_timeout')) {
|
|
149
|
-
this._options.redisConfig.connect_timeout = credentials.connect_timeout;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
if (this._options.hasOwnProperty('db')) {
|
|
153
|
-
this._options.db = credentials.db;
|
|
154
|
-
} else {
|
|
155
|
-
this._options.db = false;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
this._options.redisConfig.enable_offline_queue = credentials.enable_offline_queue || false;
|
|
159
|
-
redis.debug_mode = that._debugEnabled = credentials.debug || false;
|
|
160
|
-
this._options.redisConfig.hbInterval = credentials.hbInterval || false;
|
|
161
|
-
this._options.redisConfig.hbTimeout = credentials.hbTimeout || false;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
//parse credentials to class format
|
|
165
|
-
credentials = this._normalizeCredentials(credentials);
|
|
166
|
-
|
|
167
|
-
if (!credentials || typeof credentials !== 'object') {
|
|
168
|
-
if (typeof this._credentials !== 'object') {
|
|
169
|
-
console.error('Redis no credentials available');
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
credentials = this._credentials;
|
|
174
|
-
} else {
|
|
175
|
-
this._credentials = credentials;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
var credentialKeys = Object.keys(credentials);
|
|
179
|
-
var hostsKeys = Object.keys(this._hosts);
|
|
180
|
-
|
|
181
|
-
credentialKeys.forEach(function (i) {
|
|
182
|
-
if (hostsKeys.indexOf(i) < 0) {
|
|
183
|
-
that._hosts[i] = {
|
|
184
|
-
connections: credentials[i],
|
|
185
|
-
pool: new Pool(),
|
|
186
|
-
active: 0,
|
|
187
|
-
errors: 0,
|
|
188
|
-
toDelete: false
|
|
189
|
-
};
|
|
190
|
-
}
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
hostsKeys.forEach(function (i) {
|
|
194
|
-
if (credentialKeys.indexOf(i) < 0) {
|
|
195
|
-
that._hosts[i].toDelete = true;
|
|
196
|
-
}
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
var hosts = Object.keys(this._hosts);
|
|
200
|
-
for (var j = 0, hostsL = hosts.length; j < hostsL; j++) {
|
|
201
|
-
if (credentials[hosts[j]]) {
|
|
202
|
-
this._hosts[hosts[j]].connections = credentials[hosts[j]];
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
var connected = this._hosts[hosts[j]].pool.getKnownLength();
|
|
206
|
-
|
|
207
|
-
if (connected !== this._hosts[hosts[j]].connections) {
|
|
208
|
-
this._manageConnections(hosts[j], this._hosts[hosts[j]].connections - connected);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
//sort host for getShard method (backward compatibility)
|
|
213
|
-
this._sortedHosts = Object.keys(this._hosts).sort(function (a, b) {
|
|
214
|
-
var hostA = a.split(':');
|
|
215
|
-
var hostB = b.split(':');
|
|
216
|
-
|
|
217
|
-
if (hostA[0] !== hostB[0]) {
|
|
218
|
-
return hostA[0] > hostB[0] ? 1 : -1;
|
|
219
|
-
}
|
|
220
|
-
return hostA[1] - hostB[1];
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
};
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* Normalize params from credentialsProvider to class format
|
|
227
|
-
*
|
|
228
|
-
* @param {Object}
|
|
229
|
-
* @return {Object} - object with {ip:port => numConnections}
|
|
230
|
-
*/
|
|
231
|
-
Redis.prototype._normalizeCredentials = function (items) {
|
|
232
|
-
if (!items || !items.hosts) {
|
|
233
|
-
return false;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
var result = {};
|
|
237
|
-
|
|
238
|
-
if (typeof items.connections === 'undefined') {
|
|
239
|
-
items.connections = this._options.defaultConnectionsCount;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
items.connections = parseInt(items.connections);
|
|
243
|
-
var connections = isNaN(items.connections) ? 0 : items.connections;
|
|
244
|
-
|
|
245
|
-
for (var i = 0; i < items.hosts.length; i++) {
|
|
246
|
-
if (items.hosts[i].host && items.hosts[i].port) {
|
|
247
|
-
//if we have two ore more same entries in config we add it to existing entry
|
|
248
|
-
if (result[items.hosts[i].host + ':' + items.hosts[i].port]) {
|
|
249
|
-
result[items.hosts[i].host + ':' + items.hosts[i].port] += connections;
|
|
250
|
-
} else {
|
|
251
|
-
result[items.hosts[i].host + ':' + items.hosts[i].port] = connections;
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
return result;
|
|
257
|
-
};
|
|
258
|
-
|
|
259
|
-
/**
|
|
260
|
-
* Add/delete connections to redis
|
|
261
|
-
*
|
|
262
|
-
* @param {string} - ip:port string
|
|
263
|
-
* @param {int} - number of connections
|
|
264
|
-
* @return {void}
|
|
265
|
-
*/
|
|
266
|
-
Redis.prototype._manageConnections = function (addr, connections) {
|
|
267
|
-
if (!connections) {
|
|
268
|
-
return;
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
if (!this._validateHost(addr)) {
|
|
272
|
-
console.error('Bad hostname adres:'.addr);
|
|
273
|
-
return;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
var toRemove = false;
|
|
277
|
-
|
|
278
|
-
if (connections < 0) {
|
|
279
|
-
toRemove = true;
|
|
280
|
-
connections = (connections * -1);
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
for (var i = 0; i < connections; i++) {
|
|
284
|
-
if (!toRemove) {
|
|
285
|
-
this._createClient(addr);
|
|
286
|
-
} else {
|
|
287
|
-
this._deleteClient(addr);
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
};
|
|
291
|
-
|
|
292
|
-
/**
|
|
293
|
-
* close single redis client
|
|
294
|
-
*
|
|
295
|
-
* @param {string} - host with port
|
|
296
|
-
* @return {void}
|
|
297
|
-
*/
|
|
298
|
-
Redis.prototype._deleteClient = function (addr) {
|
|
299
|
-
if (!this._validateHost(addr)) {
|
|
300
|
-
console.error('Bad hostname adres:', addr);
|
|
301
|
-
return;
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
this._hosts[addr].pool.get(function (client) {
|
|
305
|
-
client.quit(function (err) {
|
|
306
|
-
if (err) {
|
|
307
|
-
client.connection_gone('delete_client');
|
|
308
|
-
}
|
|
309
|
-
});
|
|
310
|
-
});
|
|
311
|
-
};
|
|
312
|
-
|
|
313
|
-
/**
|
|
314
|
-
* Simple hostname:ip string validation
|
|
315
|
-
*
|
|
316
|
-
* @param {string} addr - ip:port
|
|
317
|
-
* @return {bool} v-alidation result
|
|
318
|
-
*/
|
|
319
|
-
Redis.prototype._validateHost = function (addr) {
|
|
320
|
-
if (!addr || typeof addr !== 'string' || addr.indexOf(':') === -1 || addr.split(':').length !== 2) {
|
|
321
|
-
return false;
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
return true;
|
|
325
|
-
};
|
|
326
|
-
|
|
327
|
-
/**
|
|
328
|
-
* Check if all connections are established at start and run onReady callback (only runs at client start)
|
|
329
|
-
*
|
|
330
|
-
* @return {void}
|
|
331
|
-
*/
|
|
332
|
-
Redis.prototype._readyCheck = function () {
|
|
333
|
-
var that = this;
|
|
334
|
-
var readyTmp = 0;
|
|
335
|
-
if (this._isReady) {
|
|
336
|
-
return;
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
that._readyClients++;
|
|
340
|
-
|
|
341
|
-
Object.keys(this._hosts).forEach(function (host) {
|
|
342
|
-
readyTmp += that._hosts[host].connections;
|
|
343
|
-
});
|
|
344
|
-
|
|
345
|
-
if (readyTmp === this._readyClients) {
|
|
346
|
-
this._isReady = true;
|
|
347
|
-
|
|
348
|
-
this.dispatchEvent(new Event(Redis.Event.READY));
|
|
349
|
-
|
|
350
|
-
if (typeof this._onReadyCallback === 'function') {
|
|
351
|
-
this._onReadyCallback();
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
};
|
|
355
|
-
|
|
356
|
-
/**
|
|
357
|
-
* Add redis client to pool
|
|
358
|
-
*
|
|
359
|
-
* @param {string} - ip:port
|
|
360
|
-
* @return {void}
|
|
361
|
-
*/
|
|
362
|
-
Redis.prototype._createClient = function (addr) {
|
|
363
|
-
var that = this;
|
|
364
|
-
var countActive = false;
|
|
365
|
-
|
|
366
|
-
if (!this._validateHost(addr)) {
|
|
367
|
-
console.error('Bad hostname adres:', addr);
|
|
368
|
-
return;
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
if (!that._hosts[addr]) {
|
|
372
|
-
console.error('Host not found in pools');
|
|
373
|
-
return;
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
var host = addr.split(':', 2);
|
|
377
|
-
var client = redis.createClient(host[1], host[0], that._options.redisConfig);
|
|
378
|
-
that._heartbeat(client);
|
|
379
|
-
|
|
380
|
-
client.on('connect', function () {
|
|
381
|
-
that._debug('Redis client connected to', addr);
|
|
382
|
-
});
|
|
383
|
-
|
|
384
|
-
client.on('ready', function () {
|
|
385
|
-
//increment connection active counter and add flag that connection is connected
|
|
386
|
-
//redis default flag cant be use because on end event its set to false
|
|
387
|
-
that._hosts[addr].active++;
|
|
388
|
-
countActive = true;
|
|
389
|
-
|
|
390
|
-
if (that._options.db || that._options.db === 0) {
|
|
391
|
-
client.select(that._options.db, function (error) {
|
|
392
|
-
if (error) {
|
|
393
|
-
console.error('Redis client error', error);
|
|
394
|
-
that._hosts[addr].errors++;
|
|
395
|
-
} else {
|
|
396
|
-
that._debug('Redis client', addr, 'ready');
|
|
397
|
-
that._readyCheck();
|
|
398
|
-
}
|
|
399
|
-
});
|
|
400
|
-
} else {
|
|
401
|
-
that._debug('Redis client', addr, 'ready');
|
|
402
|
-
that._readyCheck();
|
|
403
|
-
}
|
|
404
|
-
});
|
|
405
|
-
|
|
406
|
-
client.on('error', function (error) {
|
|
407
|
-
console.error('Redis client error:', addr, 'Error:', error);
|
|
408
|
-
that._hosts[addr].errors++;
|
|
409
|
-
that._hosts[addr].pool.markForDeletion(client);
|
|
410
|
-
that._hosts[addr].pool.release(client);
|
|
411
|
-
client.connection_gone('error');
|
|
412
|
-
});
|
|
413
|
-
|
|
414
|
-
client.on('end', function () {
|
|
415
|
-
//disable client reconnect by set closing var
|
|
416
|
-
client.closing = true;
|
|
417
|
-
|
|
418
|
-
//if connection was active and it disconnects remove it from active counter
|
|
419
|
-
if (countActive) {
|
|
420
|
-
that._hosts[addr].active--;
|
|
421
|
-
countActive = false;
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
if (!that._hosts[addr]) {
|
|
425
|
-
console.error('Cannot find host', addr, ' onRedisClientEnd');
|
|
426
|
-
return;
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
if (that._hbTimeout) {
|
|
430
|
-
clearTimeout(that._hbTimeout);
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
that._hosts[addr].pool.markForDeletion(client);
|
|
434
|
-
that._hosts[addr].pool.release(client);
|
|
435
|
-
client.end();
|
|
436
|
-
|
|
437
|
-
//when client ends check connections count
|
|
438
|
-
if (!that._updateTimeout) {
|
|
439
|
-
that._updateTimeout = setTimeout(that._onCredentialsChange.bind(that), that._options.updateTimeout);
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
that._debug('client :', addr, 'disconnected');
|
|
443
|
-
});
|
|
444
|
-
|
|
445
|
-
that._hosts[addr].pool.add(client);
|
|
446
|
-
};
|
|
447
|
-
|
|
448
|
-
/**
|
|
449
|
-
* Add hearbeat to redis client
|
|
450
|
-
*
|
|
451
|
-
* @param {Object} client Redis Client object
|
|
452
|
-
* @return {void}
|
|
453
|
-
*/
|
|
454
|
-
|
|
455
|
-
Redis.prototype._heartbeat = function (client) {
|
|
456
|
-
var that = this;
|
|
457
|
-
|
|
458
|
-
if (!client) {
|
|
459
|
-
return;
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
if (client._hbTimeout) {
|
|
463
|
-
clearTimeout(client._hbTimeout);
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
if (client.ready && client.options.hbInterval) {
|
|
467
|
-
//add timeout for ping command
|
|
468
|
-
if (client.options.hbTimeout) {
|
|
469
|
-
client._hbWait = setTimeout(function () {
|
|
470
|
-
//send event connection gone
|
|
471
|
-
//its run flush_and_error <- run all queued callback with error
|
|
472
|
-
//its run end event (on end we set closing to true because of disable reconnects)
|
|
473
|
-
client.quit(emptyFn);
|
|
474
|
-
client.connection_gone('ping');
|
|
475
|
-
}, client.options.hbTimeout);
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
client.ping(function () {
|
|
479
|
-
clearTimeout(client._hbWait);
|
|
480
|
-
});
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
//set timer for hb
|
|
484
|
-
//if hbtimer is not set, timer from credentail checking will be used
|
|
485
|
-
//its because of checking if hb is enabled or not
|
|
486
|
-
var timer = client.options.hbInterval || this._options.updateTimeout;
|
|
487
|
-
|
|
488
|
-
if (!timer) {
|
|
489
|
-
return;
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
client._hbTimeout = setTimeout(function () {
|
|
493
|
-
that._heartbeat(client);
|
|
494
|
-
}, timer);
|
|
495
|
-
};
|
|
496
|
-
|
|
497
|
-
/**
|
|
498
|
-
* Returns object with connection statistics
|
|
499
|
-
*
|
|
500
|
-
* @return {Object}
|
|
501
|
-
*/
|
|
502
|
-
Redis.prototype.getStats = function () {
|
|
503
|
-
var result = {};
|
|
504
|
-
var hosts = Object.keys(this._hosts);
|
|
505
|
-
for (var j = 0, hostsL = hosts.length; j < hostsL; j++) {
|
|
506
|
-
var host = this._hosts[hosts[j]];
|
|
507
|
-
|
|
508
|
-
//do not show deleted connections in stats
|
|
509
|
-
if (host.toDelete) {
|
|
510
|
-
continue;
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
//active key deprecated
|
|
514
|
-
result[hosts[j]] = {
|
|
515
|
-
known: host.pool.getKnownLength(),
|
|
516
|
-
spare: host.pool.getSpareLength(),
|
|
517
|
-
waiting: host.pool.getWaitingLength(),
|
|
518
|
-
active: host.active,
|
|
519
|
-
errors: host.errors,
|
|
520
|
-
queued: host.pool.getQueued()
|
|
521
|
-
};
|
|
522
|
-
|
|
523
|
-
// reset error counter
|
|
524
|
-
host.errors = 0;
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
return result;
|
|
528
|
-
};
|
|
529
|
-
|
|
530
|
-
/**
|
|
531
|
-
* Generate crc32 hash from string
|
|
532
|
-
*
|
|
533
|
-
* @param {String} - string to hash
|
|
534
|
-
* @return {string} hashed key
|
|
535
|
-
*/
|
|
536
|
-
Redis.prototype._hashKey = function (key) {
|
|
537
|
-
var digest = crc32(key);
|
|
538
|
-
var activeHosts = 0;
|
|
539
|
-
var hosts = Object.keys(this._hosts);
|
|
540
|
-
|
|
541
|
-
for (var i = 0, hl = hosts.length; i < hl; i++) {
|
|
542
|
-
if (!this._hosts[hosts[i]].toDelete) {
|
|
543
|
-
activeHosts++;
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
if (!activeHosts) {
|
|
548
|
-
return undefined;
|
|
549
|
-
} else {
|
|
550
|
-
return digest % activeHosts;
|
|
551
|
-
}
|
|
552
|
-
};
|
|
553
|
-
|
|
554
|
-
/**
|
|
555
|
-
* Destroy all connections and disable update interval
|
|
556
|
-
*
|
|
557
|
-
* @return {void}
|
|
558
|
-
*/
|
|
559
|
-
Redis.prototype.destroy = function () {
|
|
560
|
-
var that = this;
|
|
561
|
-
clearTimeout(this._updateTimeout);
|
|
562
|
-
|
|
563
|
-
for (var host in this._hosts) {
|
|
564
|
-
var server = that._hosts[host];
|
|
565
|
-
var clientsCount = server.pool.getKnownLength();
|
|
566
|
-
|
|
567
|
-
for (var j = 0; j < clientsCount; j++) {
|
|
568
|
-
that._deleteClient();
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
};
|
|
572
|
-
|
|
573
|
-
/**
|
|
574
|
-
* Get redis hostname assigned to key
|
|
575
|
-
*
|
|
576
|
-
* @param {string} -
|
|
577
|
-
* @return {string} - redis key
|
|
578
|
-
*/
|
|
579
|
-
Redis.prototype._getShard = function (key) {
|
|
580
|
-
var pool = this._hashKey(key);
|
|
581
|
-
var hostsL = Object.keys(this._hosts).length;
|
|
582
|
-
|
|
583
|
-
for (var i = 0; i < hostsL; i++) {
|
|
584
|
-
if (pool > hostsL) {
|
|
585
|
-
pool = 0;
|
|
586
|
-
}
|
|
587
|
-
|
|
588
|
-
if (!this._sortedHosts[pool] || !this._hosts[this._sortedHosts[pool]] || this._hosts[this._sortedHosts[pool]].toDelete) {
|
|
589
|
-
pool++;
|
|
590
|
-
continue;
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
if (this._hosts[this._sortedHosts[pool]].pool.getKnownLength() > 0) {
|
|
594
|
-
return this._sortedHosts[pool];
|
|
595
|
-
}
|
|
596
|
-
|
|
597
|
-
console.info('Dont know any active client for shard', this._sortedHosts[pool]);
|
|
598
|
-
|
|
599
|
-
pool++;
|
|
600
|
-
}
|
|
601
|
-
};
|
|
602
|
-
|
|
603
|
-
/**
|
|
604
|
-
* Returns redis client by keys
|
|
605
|
-
*
|
|
606
|
-
* @param {string} key key for shard
|
|
607
|
-
* @param {Function} callback call with err, client, host
|
|
608
|
-
* @return {void}
|
|
609
|
-
*/
|
|
610
|
-
Redis.prototype.getConnectionByKey = function (key, callback) {
|
|
611
|
-
var host = this._getShard(key);
|
|
612
|
-
|
|
613
|
-
if (!callback) {
|
|
614
|
-
console.error('Callback is required');
|
|
615
|
-
return;
|
|
616
|
-
}
|
|
617
|
-
|
|
618
|
-
if (host === undefined) {
|
|
619
|
-
if (callback) {
|
|
620
|
-
callback('All shards are down', null);
|
|
621
|
-
}
|
|
622
|
-
return;
|
|
623
|
-
}
|
|
624
|
-
|
|
625
|
-
this._hosts[host].pool.get(function (client) {
|
|
626
|
-
callback(null, client, host);
|
|
627
|
-
});
|
|
628
|
-
|
|
629
|
-
};
|
|
630
|
-
|
|
631
|
-
/**
|
|
632
|
-
* Function ovverides redis client to add shard support
|
|
633
|
-
*
|
|
634
|
-
* @param
|
|
635
|
-
* @return {void}
|
|
636
|
-
*/
|
|
637
|
-
var cmd = function (cmd) {
|
|
638
|
-
return function () {
|
|
639
|
-
var that = this;
|
|
640
|
-
var tmp = new Array(arguments.length);
|
|
641
|
-
|
|
642
|
-
for (var i = 0; i < arguments.length; i++) {
|
|
643
|
-
tmp[i] = arguments[i];
|
|
644
|
-
}
|
|
645
|
-
|
|
646
|
-
var key = tmp[0];
|
|
647
|
-
var cb = tmp.pop();
|
|
648
|
-
|
|
649
|
-
if (typeof cb !== 'function') {
|
|
650
|
-
tmp.push(cb);
|
|
651
|
-
cb = function (err) {
|
|
652
|
-
if (err) {
|
|
653
|
-
console.error('Redis/Error cmd=%s error="%s"', cmd, err);
|
|
654
|
-
}
|
|
655
|
-
};
|
|
656
|
-
}
|
|
657
|
-
|
|
658
|
-
this.getConnectionByKey(key, function (err, client, host) {
|
|
659
|
-
if (err) {
|
|
660
|
-
return cb(err, null);
|
|
661
|
-
}
|
|
662
|
-
|
|
663
|
-
if (client === Pool.ERROR.MAX_QUEUED_REACHED) {
|
|
664
|
-
cb(Pool.ERROR.MAX_QUEUED_REACHED, null);
|
|
665
|
-
return;
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
tmp.push(function (err, data) {
|
|
669
|
-
if (err) {
|
|
670
|
-
console.warn('Redis/Error cmd=%s error="%s"', cmd, err);
|
|
671
|
-
that._hosts[host].errors++;
|
|
672
|
-
}
|
|
673
|
-
|
|
674
|
-
that._hosts[host].pool.release(client);
|
|
675
|
-
|
|
676
|
-
cb(err, data);
|
|
677
|
-
});
|
|
678
|
-
client[cmd].apply(client, tmp);
|
|
679
|
-
});
|
|
680
|
-
|
|
681
|
-
};
|
|
682
|
-
};
|
|
683
|
-
CMDS = [];
|
|
684
|
-
|
|
685
|
-
var CMDS = ['get', 'set', 'setnx', 'setex', 'append', 'strlen', 'del', 'exists',
|
|
686
|
-
'setbit', 'getbit', 'setrange', 'getrange', 'substr', 'incr', 'decr', 'mget', 'rpush',
|
|
687
|
-
'lpush', 'rpushx', 'lpushx', 'linsert', 'rpop', 'lpop', 'brpop', 'brpoplpush', 'blpop',
|
|
688
|
-
'llen', 'lindex', 'lset', 'lrange', 'ltrim', 'lrem', 'rpoplpush', 'sadd', 'srem', 'smove',
|
|
689
|
-
'sismember', 'scard', 'spop', 'srandmember', 'sinter', 'sinterstore', 'sunion', 'sunionstore',
|
|
690
|
-
'sdiff', 'sdiffstore', 'smembers', 'zadd', 'zincrby', 'zrem', 'zremrangebyscore', 'zremrangebyrank',
|
|
691
|
-
'zunionstore', 'zinterstore', 'zrange', 'zrangebyscore', 'zrevrangebyscore', 'zcount', 'zrevrange',
|
|
692
|
-
'zcard', 'zscore', 'zrank', 'zrevrank', 'hset', 'hsetnx', 'hget', 'hmset', 'hmget', 'hincrby', 'hdel', 'hlen',
|
|
693
|
-
'hkeys', 'hvals', 'hgetall', 'hexists', 'incrby', 'decrby', 'getset', 'mset', 'msetnx', 'randomkey', 'select',
|
|
694
|
-
'move', 'rename', 'renamenx', 'expire', 'expireat', 'keys', 'dbsize', 'auth', 'ping', 'echo', 'save',
|
|
695
|
-
'bgsave', 'bgrewriteaof', 'shutdown', 'lastsave', 'type', 'multi', 'exec', 'discard', 'sync', 'flushdb',
|
|
696
|
-
'flushall', 'sort', 'info', 'monitor', 'ttl', 'persist', 'slaveof', 'debug', 'config', 'subscribe',
|
|
697
|
-
'unsubscribe',
|
|
698
|
-
'psubscribe', 'punsubscribe', 'publish', 'watch', 'unwatch', 'cluster', 'restore', 'migrate', 'dump', 'object',
|
|
699
|
-
'client', 'eval', 'evalsha'
|
|
700
|
-
];
|
|
701
|
-
|
|
702
|
-
var i;
|
|
703
|
-
for (i = 0; i < CMDS.length; i++) {
|
|
704
|
-
Redis.prototype[CMDS[i]] = cmd(CMDS[i]);
|
|
705
|
-
}
|
|
706
|
-
|
|
707
|
-
var crc32 = function (str, hex) {
|
|
708
|
-
var crc = ~0;
|
|
709
|
-
var i, l;
|
|
710
|
-
for (i = 0, l = str.length; i < l; i++) {
|
|
711
|
-
crc = (crc >>> 8) ^ crc32tab[(crc ^ str.charCodeAt(i)) & 0xff];
|
|
712
|
-
}
|
|
713
|
-
crc = Math.abs(crc ^ -1);
|
|
714
|
-
return hex ? crc.toString(16) : crc;
|
|
715
|
-
};
|
|
716
|
-
|
|
717
|
-
var crc32tab = [
|
|
718
|
-
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
|
|
719
|
-
0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
|
|
720
|
-
0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
|
|
721
|
-
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
|
|
722
|
-
0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
|
|
723
|
-
0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
|
|
724
|
-
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec,
|
|
725
|
-
0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
|
|
726
|
-
0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
|
|
727
|
-
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
|
|
728
|
-
0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940,
|
|
729
|
-
0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
|
|
730
|
-
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116,
|
|
731
|
-
0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
|
|
732
|
-
0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
|
|
733
|
-
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
|
|
734
|
-
0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a,
|
|
735
|
-
0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
|
|
736
|
-
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818,
|
|
737
|
-
0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
|
|
738
|
-
0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
|
|
739
|
-
0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
|
|
740
|
-
0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c,
|
|
741
|
-
0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
|
|
742
|
-
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
|
|
743
|
-
0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
|
|
744
|
-
0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
|
|
745
|
-
0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
|
|
746
|
-
0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086,
|
|
747
|
-
0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
|
|
748
|
-
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4,
|
|
749
|
-
0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
|
|
750
|
-
0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
|
|
751
|
-
0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
|
|
752
|
-
0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
|
|
753
|
-
0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
|
|
754
|
-
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe,
|
|
755
|
-
0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
|
|
756
|
-
0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
|
|
757
|
-
0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
|
|
758
|
-
0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252,
|
|
759
|
-
0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
|
|
760
|
-
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60,
|
|
761
|
-
0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
|
|
762
|
-
0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
|
|
763
|
-
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
|
|
764
|
-
0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04,
|
|
765
|
-
0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
|
|
766
|
-
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a,
|
|
767
|
-
0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
|
|
768
|
-
0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
|
|
769
|
-
0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
|
|
770
|
-
0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e,
|
|
771
|
-
0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
|
|
772
|
-
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
|
|
773
|
-
0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
|
|
774
|
-
0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
|
|
775
|
-
0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
|
|
776
|
-
0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0,
|
|
777
|
-
0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
|
|
778
|
-
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6,
|
|
779
|
-
0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
|
|
780
|
-
0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
|
|
781
|
-
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
|
|
782
|
-
];
|
|
783
|
-
|
|
784
|
-
Redis.Event = {};
|
|
785
|
-
Redis.Event.READY = 'Redis.Event.READY';
|
|
786
|
-
|
|
787
|
-
exports.Redis = Redis;
|