@milaboratories/pl-client 2.11.7 → 2.11.9
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/dist/core/auth.cjs +24 -0
- package/dist/core/auth.cjs.map +1 -0
- package/dist/core/auth.d.ts +0 -1
- package/dist/core/auth.js +21 -0
- package/dist/core/auth.js.map +1 -0
- package/dist/core/cache.d.ts +0 -1
- package/dist/core/client.cjs +288 -0
- package/dist/core/client.cjs.map +1 -0
- package/dist/core/client.d.ts +0 -1
- package/dist/core/client.js +267 -0
- package/dist/core/client.js.map +1 -0
- package/dist/core/config.cjs +111 -0
- package/dist/core/config.cjs.map +1 -0
- package/dist/core/config.d.ts +2 -1
- package/dist/core/config.js +95 -0
- package/dist/core/config.js.map +1 -0
- package/dist/core/default_client.cjs +143 -0
- package/dist/core/default_client.cjs.map +1 -0
- package/dist/core/default_client.d.ts +0 -1
- package/dist/core/default_client.js +120 -0
- package/dist/core/default_client.js.map +1 -0
- package/dist/core/driver.cjs +14 -0
- package/dist/core/driver.cjs.map +1 -0
- package/dist/core/driver.d.ts +0 -1
- package/dist/core/driver.js +12 -0
- package/dist/core/driver.js.map +1 -0
- package/dist/core/error_resource.cjs +9 -0
- package/dist/core/error_resource.cjs.map +1 -0
- package/dist/core/error_resource.d.ts +0 -1
- package/dist/core/error_resource.js +7 -0
- package/dist/core/error_resource.js.map +1 -0
- package/dist/core/errors.cjs +106 -0
- package/dist/core/errors.cjs.map +1 -0
- package/dist/core/errors.d.ts +0 -1
- package/dist/core/errors.js +93 -0
- package/dist/core/errors.js.map +1 -0
- package/dist/core/final.cjs +98 -0
- package/dist/core/final.cjs.map +1 -0
- package/dist/core/final.d.ts +0 -1
- package/dist/core/final.js +96 -0
- package/dist/core/final.js.map +1 -0
- package/dist/core/grpc.d.ts +0 -1
- package/dist/core/http.d.ts +1 -1
- package/dist/core/ll_client.cjs +266 -0
- package/dist/core/ll_client.cjs.map +1 -0
- package/dist/core/ll_client.d.ts +0 -1
- package/dist/core/ll_client.js +264 -0
- package/dist/core/ll_client.js.map +1 -0
- package/dist/core/ll_transaction.cjs +236 -0
- package/dist/core/ll_transaction.cjs.map +1 -0
- package/dist/core/ll_transaction.d.ts +0 -1
- package/dist/core/ll_transaction.js +233 -0
- package/dist/core/ll_transaction.js.map +1 -0
- package/dist/core/stat.cjs +74 -0
- package/dist/core/stat.cjs.map +1 -0
- package/dist/core/stat.d.ts +0 -1
- package/dist/core/stat.js +70 -0
- package/dist/core/stat.js.map +1 -0
- package/dist/core/transaction.cjs +626 -0
- package/dist/core/transaction.cjs.map +1 -0
- package/dist/core/transaction.d.ts +0 -1
- package/dist/core/transaction.js +613 -0
- package/dist/core/transaction.js.map +1 -0
- package/dist/core/type_conversion.cjs +106 -0
- package/dist/core/type_conversion.cjs.map +1 -0
- package/dist/core/type_conversion.d.ts +0 -1
- package/dist/core/type_conversion.js +102 -0
- package/dist/core/type_conversion.js.map +1 -0
- package/dist/core/types.cjs +159 -0
- package/dist/core/types.cjs.map +1 -0
- package/dist/core/types.d.ts +0 -1
- package/dist/core/types.js +134 -0
- package/dist/core/types.js.map +1 -0
- package/dist/core/unauth_client.cjs +43 -0
- package/dist/core/unauth_client.cjs.map +1 -0
- package/dist/core/unauth_client.d.ts +0 -1
- package/dist/core/unauth_client.js +41 -0
- package/dist/core/unauth_client.js.map +1 -0
- package/dist/helpers/pl.cjs +124 -0
- package/dist/helpers/pl.cjs.map +1 -0
- package/dist/helpers/pl.d.ts +0 -1
- package/dist/helpers/pl.js +94 -0
- package/dist/helpers/pl.js.map +1 -0
- package/dist/helpers/poll.cjs +148 -0
- package/dist/helpers/poll.cjs.map +1 -0
- package/dist/helpers/poll.d.ts +0 -1
- package/dist/helpers/poll.js +123 -0
- package/dist/helpers/poll.js.map +1 -0
- package/dist/helpers/rich_resource_types.d.ts +1 -1
- package/dist/helpers/smart_accessors.d.ts +1 -1
- package/dist/helpers/state_helpers.d.ts +0 -1
- package/dist/helpers/tx_helpers.cjs +25 -0
- package/dist/helpers/tx_helpers.cjs.map +1 -0
- package/dist/helpers/tx_helpers.d.ts +0 -1
- package/dist/helpers/tx_helpers.js +23 -0
- package/dist/helpers/tx_helpers.js.map +1 -0
- package/dist/index.cjs +99 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +0 -1
- package/dist/index.js +16 -1
- package/dist/index.js.map +1 -1
- package/dist/proto/github.com/googleapis/googleapis/google/rpc/status.cjs +71 -0
- package/dist/proto/github.com/googleapis/googleapis/google/rpc/status.cjs.map +1 -0
- package/dist/proto/github.com/googleapis/googleapis/google/rpc/status.d.ts +0 -1
- package/dist/proto/github.com/googleapis/googleapis/google/rpc/status.js +69 -0
- package/dist/proto/github.com/googleapis/googleapis/google/rpc/status.js.map +1 -0
- package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/downloadapi/protocol.client.d.ts +3 -5
- package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/downloadapi/protocol.d.ts +0 -1
- package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/lsapi/protocol.client.d.ts +3 -5
- package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/lsapi/protocol.d.ts +0 -1
- package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/progressapi/protocol.client.d.ts +3 -5
- package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/progressapi/protocol.d.ts +0 -1
- package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/streamingapi/protocol.client.d.ts +3 -5
- package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/streamingapi/protocol.d.ts +0 -1
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api.cjs +12238 -0
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api.cjs.map +1 -0
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api.client.cjs +223 -0
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api.client.cjs.map +1 -0
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api.client.d.ts +3 -5
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api.client.js +221 -0
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api.client.js.map +1 -0
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api.d.ts +0 -1
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api.js +12086 -0
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api.js.map +1 -0
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api_types.cjs +1127 -0
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api_types.cjs.map +1 -0
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api_types.d.ts +0 -1
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api_types.js +1113 -0
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api_types.js.map +1 -0
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/base_types.cjs +152 -0
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/base_types.cjs.map +1 -0
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/base_types.d.ts +0 -1
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/base_types.js +149 -0
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/base_types.js.map +1 -0
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/import.d.ts +0 -1
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/resource_types.cjs +500 -0
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/resource_types.cjs.map +1 -0
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/resource_types.d.ts +0 -1
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/resource_types.js +496 -0
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/resource_types.js.map +1 -0
- package/dist/proto/google/api/http.d.ts +0 -1
- package/dist/proto/google/protobuf/any.cjs +151 -0
- package/dist/proto/google/protobuf/any.cjs.map +1 -0
- package/dist/proto/google/protobuf/any.d.ts +0 -1
- package/dist/proto/google/protobuf/any.js +149 -0
- package/dist/proto/google/protobuf/any.js.map +1 -0
- package/dist/proto/google/protobuf/descriptor.d.ts +0 -1
- package/dist/proto/google/protobuf/duration.cjs +105 -0
- package/dist/proto/google/protobuf/duration.cjs.map +1 -0
- package/dist/proto/google/protobuf/duration.d.ts +0 -1
- package/dist/proto/google/protobuf/duration.js +103 -0
- package/dist/proto/google/protobuf/duration.js.map +1 -0
- package/dist/proto/google/protobuf/empty.d.ts +0 -1
- package/dist/proto/google/protobuf/struct.d.ts +0 -1
- package/dist/proto/google/protobuf/timestamp.cjs +133 -0
- package/dist/proto/google/protobuf/timestamp.cjs.map +1 -0
- package/dist/proto/google/protobuf/timestamp.d.ts +0 -1
- package/dist/proto/google/protobuf/timestamp.js +131 -0
- package/dist/proto/google/protobuf/timestamp.js.map +1 -0
- package/dist/proto/google/protobuf/wrappers.d.ts +0 -1
- package/dist/test/test_config.cjs +149 -0
- package/dist/test/test_config.cjs.map +1 -0
- package/dist/test/test_config.d.ts +0 -1
- package/dist/test/test_config.js +123 -0
- package/dist/test/test_config.js.map +1 -0
- package/dist/util/branding.d.ts +0 -1
- package/dist/util/pl.cjs +8 -0
- package/dist/util/pl.cjs.map +1 -0
- package/dist/util/pl.d.ts +0 -1
- package/dist/util/pl.js +6 -0
- package/dist/util/pl.js.map +1 -0
- package/dist/util/util.cjs +17 -0
- package/dist/util/util.cjs.map +1 -0
- package/dist/util/util.d.ts +0 -1
- package/dist/util/util.js +15 -0
- package/dist/util/util.js.map +1 -0
- package/package.json +14 -11
- package/src/core/client.test.ts +1 -0
- package/src/core/config.test.ts +1 -0
- package/src/core/config.ts +10 -0
- package/src/core/error.test.ts +1 -0
- package/src/core/ll_client.test.ts +1 -0
- package/src/core/ll_transaction.test.ts +1 -0
- package/src/core/transaction.test.ts +1 -0
- package/src/core/types.test.ts +1 -0
- package/src/core/unauth_client.test.ts +1 -0
- package/src/helpers/rich_resource_types.test.ts +2 -0
- package/src/test/test_config.test.ts +1 -0
- package/src/util/util.test.ts +1 -0
- package/dist/core/auth.d.ts.map +0 -1
- package/dist/core/cache.d.ts.map +0 -1
- package/dist/core/client.d.ts.map +0 -1
- package/dist/core/config.d.ts.map +0 -1
- package/dist/core/default_client.d.ts.map +0 -1
- package/dist/core/driver.d.ts.map +0 -1
- package/dist/core/error_resource.d.ts.map +0 -1
- package/dist/core/errors.d.ts.map +0 -1
- package/dist/core/final.d.ts.map +0 -1
- package/dist/core/grpc.d.ts.map +0 -1
- package/dist/core/http.d.ts.map +0 -1
- package/dist/core/ll_client.d.ts.map +0 -1
- package/dist/core/ll_transaction.d.ts.map +0 -1
- package/dist/core/stat.d.ts.map +0 -1
- package/dist/core/transaction.d.ts.map +0 -1
- package/dist/core/type_conversion.d.ts.map +0 -1
- package/dist/core/types.d.ts.map +0 -1
- package/dist/core/unauth_client.d.ts.map +0 -1
- package/dist/helpers/pl.d.ts.map +0 -1
- package/dist/helpers/poll.d.ts.map +0 -1
- package/dist/helpers/rich_resource_types.d.ts.map +0 -1
- package/dist/helpers/smart_accessors.d.ts.map +0 -1
- package/dist/helpers/state_helpers.d.ts.map +0 -1
- package/dist/helpers/tx_helpers.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.mjs +0 -14530
- package/dist/index.mjs.map +0 -1
- package/dist/proto/github.com/googleapis/googleapis/google/rpc/status.d.ts.map +0 -1
- package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/downloadapi/protocol.client.d.ts.map +0 -1
- package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/downloadapi/protocol.d.ts.map +0 -1
- package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/lsapi/protocol.client.d.ts.map +0 -1
- package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/lsapi/protocol.d.ts.map +0 -1
- package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/progressapi/protocol.client.d.ts.map +0 -1
- package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/progressapi/protocol.d.ts.map +0 -1
- package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/streamingapi/protocol.client.d.ts.map +0 -1
- package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/streamingapi/protocol.d.ts.map +0 -1
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api.client.d.ts.map +0 -1
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api.d.ts.map +0 -1
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api_types.d.ts.map +0 -1
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/base_types.d.ts.map +0 -1
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/import.d.ts.map +0 -1
- package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/resource_types.d.ts.map +0 -1
- package/dist/proto/google/api/http.d.ts.map +0 -1
- package/dist/proto/google/protobuf/any.d.ts.map +0 -1
- package/dist/proto/google/protobuf/descriptor.d.ts.map +0 -1
- package/dist/proto/google/protobuf/duration.d.ts.map +0 -1
- package/dist/proto/google/protobuf/empty.d.ts.map +0 -1
- package/dist/proto/google/protobuf/struct.d.ts.map +0 -1
- package/dist/proto/google/protobuf/timestamp.d.ts.map +0 -1
- package/dist/proto/google/protobuf/wrappers.d.ts.map +0 -1
- package/dist/test/test_config.d.ts.map +0 -1
- package/dist/util/branding.d.ts.map +0 -1
- package/dist/util/pl.d.ts.map +0 -1
- package/dist/util/util.d.ts.map +0 -1
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { LLPlClient } from './ll_client.js';
|
|
2
|
+
import { toGlobalResourceId, PlTransaction, TxCommitConflict } from './transaction.js';
|
|
3
|
+
import { createHash } from 'node:crypto';
|
|
4
|
+
import { NullResourceId, isNullResourceId, ensureResourceIdNotNull } from './types.js';
|
|
5
|
+
import { ClientRoot } from '../helpers/pl.js';
|
|
6
|
+
import { assertNever, createRetryState, nextRetryStateOrError } from '@milaboratories/ts-helpers';
|
|
7
|
+
import { MaintenanceAPI_Ping_Response_Compression } from '../proto/github.com/milaboratory/pl/plapi/plapiproto/api.js';
|
|
8
|
+
import * as tp from 'node:timers/promises';
|
|
9
|
+
import { LRUCache } from 'lru-cache';
|
|
10
|
+
import { DefaultFinalResourceDataPredicate } from './final.js';
|
|
11
|
+
import { initialTxStat, addStat } from './stat.js';
|
|
12
|
+
|
|
13
|
+
const defaultTxOps = {
|
|
14
|
+
sync: false,
|
|
15
|
+
};
|
|
16
|
+
const AnonymousClientRoot = 'AnonymousRoot';
|
|
17
|
+
function alternativeRootFieldName(alternativeRoot) {
|
|
18
|
+
return `alternative_root_${alternativeRoot}`;
|
|
19
|
+
}
|
|
20
|
+
/** Client to access core PL API. */
|
|
21
|
+
class PlClient {
|
|
22
|
+
drivers = new Map();
|
|
23
|
+
/** Artificial delay introduced after write transactions completion, to
|
|
24
|
+
* somewhat throttle the load on pl. Delay introduced after sync, if requested. */
|
|
25
|
+
txDelay;
|
|
26
|
+
/** Last resort measure to solve complicated race conditions in pl. */
|
|
27
|
+
forceSync;
|
|
28
|
+
/** Last resort measure to solve complicated race conditions in pl. */
|
|
29
|
+
defaultRetryOptions;
|
|
30
|
+
buildLLPlClient;
|
|
31
|
+
_ll;
|
|
32
|
+
/** Stores client root (this abstraction is intended for future implementation of the security model) */
|
|
33
|
+
_clientRoot = NullResourceId;
|
|
34
|
+
_serverInfo = undefined;
|
|
35
|
+
_txCommittedStat = initialTxStat();
|
|
36
|
+
_txConflictStat = initialTxStat();
|
|
37
|
+
_txErrorStat = initialTxStat();
|
|
38
|
+
//
|
|
39
|
+
// Caching
|
|
40
|
+
//
|
|
41
|
+
/** This function determines whether resource data can be cached */
|
|
42
|
+
finalPredicate;
|
|
43
|
+
/** Resource data cache, to minimize redundant data rereading from remote db */
|
|
44
|
+
resourceDataCache;
|
|
45
|
+
constructor(configOrAddress, auth, ops = {}) {
|
|
46
|
+
// Will reinitialize client after getting available feature from server.
|
|
47
|
+
this.buildLLPlClient = (shouldUseGzip) => {
|
|
48
|
+
return new LLPlClient(configOrAddress, { auth, ...ops, shouldUseGzip });
|
|
49
|
+
};
|
|
50
|
+
this._ll = this.buildLLPlClient(false);
|
|
51
|
+
const conf = this._ll.conf;
|
|
52
|
+
this.txDelay = conf.txDelay;
|
|
53
|
+
this.forceSync = conf.forceSync;
|
|
54
|
+
this.finalPredicate = ops.finalPredicate ?? DefaultFinalResourceDataPredicate;
|
|
55
|
+
this.resourceDataCache = new LRUCache({
|
|
56
|
+
maxSize: conf.maxCacheBytes,
|
|
57
|
+
sizeCalculation: (v) => (v.basicData.data?.length ?? 0) + 64,
|
|
58
|
+
});
|
|
59
|
+
switch (conf.retryBackoffAlgorithm) {
|
|
60
|
+
case 'exponential':
|
|
61
|
+
this.defaultRetryOptions = {
|
|
62
|
+
type: 'exponentialBackoff',
|
|
63
|
+
initialDelay: conf.retryInitialDelay,
|
|
64
|
+
maxAttempts: conf.retryMaxAttempts,
|
|
65
|
+
backoffMultiplier: conf.retryExponentialBackoffMultiplier,
|
|
66
|
+
jitter: conf.retryJitter,
|
|
67
|
+
};
|
|
68
|
+
break;
|
|
69
|
+
case 'linear':
|
|
70
|
+
this.defaultRetryOptions = {
|
|
71
|
+
type: 'linearBackoff',
|
|
72
|
+
initialDelay: conf.retryInitialDelay,
|
|
73
|
+
maxAttempts: conf.retryMaxAttempts,
|
|
74
|
+
backoffStep: conf.retryLinearBackoffStep,
|
|
75
|
+
jitter: conf.retryJitter,
|
|
76
|
+
};
|
|
77
|
+
break;
|
|
78
|
+
default:
|
|
79
|
+
assertNever(conf.retryBackoffAlgorithm);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
get txCommittedStat() {
|
|
83
|
+
return { ...this._txCommittedStat };
|
|
84
|
+
}
|
|
85
|
+
get txConflictStat() {
|
|
86
|
+
return { ...this._txConflictStat };
|
|
87
|
+
}
|
|
88
|
+
get txErrorStat() {
|
|
89
|
+
return { ...this._txErrorStat };
|
|
90
|
+
}
|
|
91
|
+
get txTotalStat() {
|
|
92
|
+
return addStat(addStat(this._txCommittedStat, this._txConflictStat), this._txErrorStat);
|
|
93
|
+
}
|
|
94
|
+
get allTxStat() {
|
|
95
|
+
return {
|
|
96
|
+
committed: this.txCommittedStat,
|
|
97
|
+
conflict: this.txConflictStat,
|
|
98
|
+
error: this.txErrorStat,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
async ping() {
|
|
102
|
+
return (await this._ll.grpcPl.get().ping({})).response;
|
|
103
|
+
}
|
|
104
|
+
get conf() {
|
|
105
|
+
return this._ll.conf;
|
|
106
|
+
}
|
|
107
|
+
get httpDispatcher() {
|
|
108
|
+
return this._ll.httpDispatcher;
|
|
109
|
+
}
|
|
110
|
+
get grpcTransport() {
|
|
111
|
+
return this._ll.grpcTransport;
|
|
112
|
+
}
|
|
113
|
+
get initialized() {
|
|
114
|
+
return !isNullResourceId(this._clientRoot);
|
|
115
|
+
}
|
|
116
|
+
checkInitialized() {
|
|
117
|
+
if (!this.initialized)
|
|
118
|
+
throw new Error('Client not initialized');
|
|
119
|
+
}
|
|
120
|
+
get clientRoot() {
|
|
121
|
+
this.checkInitialized();
|
|
122
|
+
return ensureResourceIdNotNull(this._clientRoot);
|
|
123
|
+
}
|
|
124
|
+
get serverInfo() {
|
|
125
|
+
this.checkInitialized();
|
|
126
|
+
return this._serverInfo;
|
|
127
|
+
}
|
|
128
|
+
/** Currently implements custom logic to emulate future behaviour with single root. */
|
|
129
|
+
async init() {
|
|
130
|
+
if (this.initialized)
|
|
131
|
+
throw new Error('Already initialized');
|
|
132
|
+
// calculating reproducible root name from the username
|
|
133
|
+
const user = this._ll.authUser;
|
|
134
|
+
const mainRootName = user === null ? AnonymousClientRoot : createHash('sha256').update(user).digest('hex');
|
|
135
|
+
this._serverInfo = await this.ping();
|
|
136
|
+
if (this._serverInfo.compression === MaintenanceAPI_Ping_Response_Compression.GZIP) {
|
|
137
|
+
await this._ll.close();
|
|
138
|
+
this._ll = this.buildLLPlClient(true);
|
|
139
|
+
}
|
|
140
|
+
this._clientRoot = await this._withTx('initialization', true, NullResourceId, async (tx) => {
|
|
141
|
+
let mainRoot;
|
|
142
|
+
if (await tx.checkResourceNameExists(mainRootName))
|
|
143
|
+
mainRoot = await tx.getResourceByName(mainRootName);
|
|
144
|
+
else {
|
|
145
|
+
mainRoot = tx.createRoot(ClientRoot);
|
|
146
|
+
tx.setResourceName(mainRootName, mainRoot);
|
|
147
|
+
}
|
|
148
|
+
if (this.conf.alternativeRoot === undefined) {
|
|
149
|
+
await tx.commit();
|
|
150
|
+
return await toGlobalResourceId(mainRoot);
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
const aFId = {
|
|
154
|
+
resourceId: mainRoot,
|
|
155
|
+
fieldName: alternativeRootFieldName(this.conf.alternativeRoot),
|
|
156
|
+
};
|
|
157
|
+
const altRoot = tx.createEphemeral(ClientRoot);
|
|
158
|
+
tx.lock(altRoot);
|
|
159
|
+
tx.createField(aFId, 'Dynamic');
|
|
160
|
+
tx.setField(aFId, altRoot);
|
|
161
|
+
await tx.commit();
|
|
162
|
+
return await altRoot.globalId;
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
/** Returns true if field existed */
|
|
167
|
+
async deleteAlternativeRoot(alternativeRootName) {
|
|
168
|
+
this.checkInitialized();
|
|
169
|
+
if (this._ll.conf.alternativeRoot !== undefined)
|
|
170
|
+
throw new Error('Initialized with alternative root.');
|
|
171
|
+
return await this.withWriteTx('delete-alternative-root', async (tx) => {
|
|
172
|
+
const fId = {
|
|
173
|
+
resourceId: tx.clientRoot,
|
|
174
|
+
fieldName: alternativeRootFieldName(alternativeRootName),
|
|
175
|
+
};
|
|
176
|
+
const exists = tx.fieldExists(fId);
|
|
177
|
+
tx.removeField(fId);
|
|
178
|
+
await tx.commit();
|
|
179
|
+
return await exists;
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
async _withTx(name, writable, clientRoot, body, ops) {
|
|
183
|
+
// for exponential / linear backoff
|
|
184
|
+
let retryState = createRetryState(ops?.retryOptions ?? this.defaultRetryOptions);
|
|
185
|
+
while (true) {
|
|
186
|
+
// opening low-level tx
|
|
187
|
+
const llTx = this._ll.createTx(writable, ops);
|
|
188
|
+
// wrapping it into high-level tx (this also asynchronously sends initialization message)
|
|
189
|
+
const tx = new PlTransaction(llTx, name, writable, clientRoot, this.finalPredicate, this.resourceDataCache);
|
|
190
|
+
let ok = false;
|
|
191
|
+
let result = undefined;
|
|
192
|
+
let txId;
|
|
193
|
+
try {
|
|
194
|
+
// executing transaction body
|
|
195
|
+
result = await body(tx);
|
|
196
|
+
// collecting stat
|
|
197
|
+
this._txCommittedStat = addStat(this._txCommittedStat, tx.stat);
|
|
198
|
+
ok = true;
|
|
199
|
+
}
|
|
200
|
+
catch (e) {
|
|
201
|
+
// the only recoverable
|
|
202
|
+
if (e instanceof TxCommitConflict) {
|
|
203
|
+
// ignoring
|
|
204
|
+
// collecting stat
|
|
205
|
+
this._txConflictStat = addStat(this._txConflictStat, tx.stat);
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
// collecting stat
|
|
209
|
+
this._txErrorStat = addStat(this._txErrorStat, tx.stat);
|
|
210
|
+
throw e;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
finally {
|
|
214
|
+
// close underlying grpc stream, if not yet done
|
|
215
|
+
// even though we can skip two lines below for read-only transactions,
|
|
216
|
+
// we don't do it to simplify reasoning about what is going on in
|
|
217
|
+
// concurrent code, especially in significant latency situations
|
|
218
|
+
await tx.complete();
|
|
219
|
+
await tx.await();
|
|
220
|
+
txId = await tx.getGlobalTxId();
|
|
221
|
+
}
|
|
222
|
+
if (ok) {
|
|
223
|
+
// syncing on transaction if requested
|
|
224
|
+
if (ops?.sync === undefined ? this.forceSync : ops?.sync)
|
|
225
|
+
await this._ll.grpcPl.get().txSync({ txId });
|
|
226
|
+
// introducing artificial delay, if requested
|
|
227
|
+
if (writable && this.txDelay > 0)
|
|
228
|
+
await tp.setTimeout(this.txDelay, undefined, { signal: ops?.abortSignal });
|
|
229
|
+
return result;
|
|
230
|
+
}
|
|
231
|
+
// we only get here after TxCommitConflict error,
|
|
232
|
+
// all other errors terminate this loop instantly
|
|
233
|
+
await tp.setTimeout(retryState.nextDelay, undefined, { signal: ops?.abortSignal });
|
|
234
|
+
retryState = nextRetryStateOrError(retryState);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
async withTx(name, writable, body, ops = {}) {
|
|
238
|
+
this.checkInitialized();
|
|
239
|
+
return await this._withTx(name, writable, this.clientRoot, body, { ...ops, ...defaultTxOps });
|
|
240
|
+
}
|
|
241
|
+
async withWriteTx(name, body, ops = {}) {
|
|
242
|
+
return await this.withTx(name, true, body, { ...ops, ...defaultTxOps });
|
|
243
|
+
}
|
|
244
|
+
async withReadTx(name, body, ops = {}) {
|
|
245
|
+
return await this.withTx(name, false, body, { ...ops, ...defaultTxOps });
|
|
246
|
+
}
|
|
247
|
+
getDriver(definition) {
|
|
248
|
+
const attached = this.drivers.get(definition.name);
|
|
249
|
+
if (attached !== undefined)
|
|
250
|
+
return attached;
|
|
251
|
+
const driver = definition.init(this, this._ll, this.httpDispatcher);
|
|
252
|
+
this.drivers.set(definition.name, driver);
|
|
253
|
+
return driver;
|
|
254
|
+
}
|
|
255
|
+
/** Closes underlying transport */
|
|
256
|
+
async close() {
|
|
257
|
+
await this._ll.close();
|
|
258
|
+
}
|
|
259
|
+
static async init(configOrAddress, auth, ops = {}) {
|
|
260
|
+
const pl = new PlClient(configOrAddress, auth, ops);
|
|
261
|
+
await pl.init();
|
|
262
|
+
return pl;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export { PlClient };
|
|
267
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sources":["../../src/core/client.ts"],"sourcesContent":["import type { AuthOps, PlClientConfig, PlConnectionStatusListener } from './config';\nimport type { PlCallOps } from './ll_client';\nimport { LLPlClient } from './ll_client';\nimport type { AnyResourceRef } from './transaction';\nimport { PlTransaction, toGlobalResourceId, TxCommitConflict } from './transaction';\nimport { createHash } from 'node:crypto';\nimport type { OptionalResourceId, ResourceId } from './types';\nimport { ensureResourceIdNotNull, isNullResourceId, NullResourceId } from './types';\nimport { ClientRoot } from '../helpers/pl';\nimport type { RetryOptions } from '@milaboratories/ts-helpers';\nimport { assertNever, createRetryState, nextRetryStateOrError } from '@milaboratories/ts-helpers';\nimport type { PlDriver, PlDriverDefinition } from './driver';\nimport type { MaintenanceAPI_Ping_Response } from '../proto/github.com/milaboratory/pl/plapi/plapiproto/api';\nimport { MaintenanceAPI_Ping_Response_Compression } from '../proto/github.com/milaboratory/pl/plapi/plapiproto/api';\nimport * as tp from 'node:timers/promises';\nimport type { Dispatcher } from 'undici';\nimport { LRUCache } from 'lru-cache';\nimport type { ResourceDataCacheRecord } from './cache';\nimport type { FinalResourceDataPredicate } from './final';\nimport { DefaultFinalResourceDataPredicate } from './final';\nimport type { AllTxStat, TxStat } from './stat';\nimport { addStat, initialTxStat } from './stat';\nimport type { GrpcTransport } from '@protobuf-ts/grpc-transport';\n\nexport type TxOps = PlCallOps & {\n sync?: boolean;\n retryOptions?: RetryOptions;\n};\n\nconst defaultTxOps = {\n sync: false,\n};\n\nconst AnonymousClientRoot = 'AnonymousRoot';\n\nfunction alternativeRootFieldName(alternativeRoot: string): string {\n return `alternative_root_${alternativeRoot}`;\n}\n\n/** Client to access core PL API. */\nexport class PlClient {\n private readonly drivers = new Map<string, PlDriver>();\n\n /** Artificial delay introduced after write transactions completion, to\n * somewhat throttle the load on pl. Delay introduced after sync, if requested. */\n private readonly txDelay: number;\n\n /** Last resort measure to solve complicated race conditions in pl. */\n private readonly forceSync: boolean;\n\n /** Last resort measure to solve complicated race conditions in pl. */\n private readonly defaultRetryOptions: RetryOptions;\n\n private readonly buildLLPlClient: (shouldUseGzip: boolean) => LLPlClient;\n private _ll: LLPlClient;\n /** Stores client root (this abstraction is intended for future implementation of the security model) */\n private _clientRoot: OptionalResourceId = NullResourceId;\n\n private _serverInfo: MaintenanceAPI_Ping_Response | undefined = undefined;\n\n private _txCommittedStat: TxStat = initialTxStat();\n private _txConflictStat: TxStat = initialTxStat();\n private _txErrorStat: TxStat = initialTxStat();\n\n //\n // Caching\n //\n\n /** This function determines whether resource data can be cached */\n public readonly finalPredicate: FinalResourceDataPredicate;\n\n /** Resource data cache, to minimize redundant data rereading from remote db */\n private readonly resourceDataCache: LRUCache<ResourceId, ResourceDataCacheRecord>;\n\n private constructor(\n configOrAddress: PlClientConfig | string,\n auth: AuthOps,\n ops: {\n statusListener?: PlConnectionStatusListener;\n finalPredicate?: FinalResourceDataPredicate;\n } = {},\n ) {\n // Will reinitialize client after getting available feature from server.\n this.buildLLPlClient = (shouldUseGzip: boolean) => {\n return new LLPlClient(configOrAddress, { auth, ...ops, shouldUseGzip });\n };\n this._ll = this.buildLLPlClient(false);\n const conf = this._ll.conf;\n this.txDelay = conf.txDelay;\n this.forceSync = conf.forceSync;\n this.finalPredicate = ops.finalPredicate ?? DefaultFinalResourceDataPredicate;\n this.resourceDataCache = new LRUCache({\n maxSize: conf.maxCacheBytes,\n sizeCalculation: (v) => (v.basicData.data?.length ?? 0) + 64,\n });\n switch (conf.retryBackoffAlgorithm) {\n case 'exponential':\n this.defaultRetryOptions = {\n type: 'exponentialBackoff',\n initialDelay: conf.retryInitialDelay,\n maxAttempts: conf.retryMaxAttempts,\n backoffMultiplier: conf.retryExponentialBackoffMultiplier,\n jitter: conf.retryJitter,\n };\n break;\n case 'linear':\n this.defaultRetryOptions = {\n type: 'linearBackoff',\n initialDelay: conf.retryInitialDelay,\n maxAttempts: conf.retryMaxAttempts,\n backoffStep: conf.retryLinearBackoffStep,\n jitter: conf.retryJitter,\n };\n break;\n default:\n assertNever(conf.retryBackoffAlgorithm);\n }\n }\n\n public get txCommittedStat(): TxStat {\n return { ...this._txCommittedStat };\n }\n\n public get txConflictStat(): TxStat {\n return { ...this._txConflictStat };\n }\n\n public get txErrorStat(): TxStat {\n return { ...this._txErrorStat };\n }\n\n public get txTotalStat(): TxStat {\n return addStat(addStat(this._txCommittedStat, this._txConflictStat), this._txErrorStat);\n }\n\n public get allTxStat(): AllTxStat {\n return {\n committed: this.txCommittedStat,\n conflict: this.txConflictStat,\n error: this.txErrorStat,\n };\n }\n\n public async ping(): Promise<MaintenanceAPI_Ping_Response> {\n return (await this._ll.grpcPl.get().ping({})).response;\n }\n\n public get conf(): PlClientConfig {\n return this._ll.conf;\n }\n\n public get httpDispatcher(): Dispatcher {\n return this._ll.httpDispatcher;\n }\n\n public get grpcTransport(): GrpcTransport {\n return this._ll.grpcTransport;\n }\n\n private get initialized() {\n return !isNullResourceId(this._clientRoot);\n }\n\n private checkInitialized() {\n if (!this.initialized) throw new Error('Client not initialized');\n }\n\n public get clientRoot(): ResourceId {\n this.checkInitialized();\n return ensureResourceIdNotNull(this._clientRoot);\n }\n\n public get serverInfo(): MaintenanceAPI_Ping_Response {\n this.checkInitialized();\n return this._serverInfo!;\n }\n\n /** Currently implements custom logic to emulate future behaviour with single root. */\n public async init() {\n if (this.initialized) throw new Error('Already initialized');\n\n // calculating reproducible root name from the username\n const user = this._ll.authUser;\n const mainRootName\n = user === null ? AnonymousClientRoot : createHash('sha256').update(user).digest('hex');\n\n this._serverInfo = await this.ping();\n if (this._serverInfo.compression === MaintenanceAPI_Ping_Response_Compression.GZIP) {\n await this._ll.close();\n this._ll = this.buildLLPlClient(true);\n }\n\n this._clientRoot = await this._withTx('initialization', true, NullResourceId, async (tx) => {\n let mainRoot: AnyResourceRef;\n\n if (await tx.checkResourceNameExists(mainRootName))\n mainRoot = await tx.getResourceByName(mainRootName);\n else {\n mainRoot = tx.createRoot(ClientRoot);\n tx.setResourceName(mainRootName, mainRoot);\n }\n\n if (this.conf.alternativeRoot === undefined) {\n await tx.commit();\n return await toGlobalResourceId(mainRoot);\n } else {\n const aFId = {\n resourceId: mainRoot,\n fieldName: alternativeRootFieldName(this.conf.alternativeRoot),\n };\n\n const altRoot = tx.createEphemeral(ClientRoot);\n tx.lock(altRoot);\n tx.createField(aFId, 'Dynamic');\n tx.setField(aFId, altRoot);\n await tx.commit();\n\n return await altRoot.globalId;\n }\n });\n }\n\n /** Returns true if field existed */\n public async deleteAlternativeRoot(alternativeRootName: string): Promise<boolean> {\n this.checkInitialized();\n if (this._ll.conf.alternativeRoot !== undefined)\n throw new Error('Initialized with alternative root.');\n return await this.withWriteTx('delete-alternative-root', async (tx) => {\n const fId = {\n resourceId: tx.clientRoot,\n fieldName: alternativeRootFieldName(alternativeRootName),\n };\n const exists = tx.fieldExists(fId);\n tx.removeField(fId);\n await tx.commit();\n return await exists;\n });\n }\n\n private async _withTx<T>(\n name: string,\n writable: boolean,\n clientRoot: OptionalResourceId,\n body: (tx: PlTransaction) => Promise<T>,\n ops?: TxOps,\n ): Promise<T> {\n // for exponential / linear backoff\n let retryState = createRetryState(ops?.retryOptions ?? this.defaultRetryOptions);\n\n while (true) {\n // opening low-level tx\n const llTx = this._ll.createTx(writable, ops);\n // wrapping it into high-level tx (this also asynchronously sends initialization message)\n const tx = new PlTransaction(\n llTx,\n name,\n writable,\n clientRoot,\n this.finalPredicate,\n this.resourceDataCache,\n );\n\n let ok = false;\n let result: T | undefined = undefined;\n let txId;\n\n try {\n // executing transaction body\n result = await body(tx);\n // collecting stat\n this._txCommittedStat = addStat(this._txCommittedStat, tx.stat);\n ok = true;\n } catch (e: unknown) {\n // the only recoverable\n if (e instanceof TxCommitConflict) {\n // ignoring\n // collecting stat\n this._txConflictStat = addStat(this._txConflictStat, tx.stat);\n } else {\n // collecting stat\n this._txErrorStat = addStat(this._txErrorStat, tx.stat);\n throw e;\n }\n } finally {\n // close underlying grpc stream, if not yet done\n\n // even though we can skip two lines below for read-only transactions,\n // we don't do it to simplify reasoning about what is going on in\n // concurrent code, especially in significant latency situations\n await tx.complete();\n await tx.await();\n\n txId = await tx.getGlobalTxId();\n }\n\n if (ok) {\n // syncing on transaction if requested\n if (ops?.sync === undefined ? this.forceSync : ops?.sync)\n await this._ll.grpcPl.get().txSync({ txId });\n\n // introducing artificial delay, if requested\n if (writable && this.txDelay > 0)\n await tp.setTimeout(this.txDelay, undefined, { signal: ops?.abortSignal });\n\n return result!;\n }\n\n // we only get here after TxCommitConflict error,\n // all other errors terminate this loop instantly\n\n await tp.setTimeout(retryState.nextDelay, undefined, { signal: ops?.abortSignal });\n retryState = nextRetryStateOrError(retryState);\n }\n }\n\n private async withTx<T>(\n name: string,\n writable: boolean,\n body: (tx: PlTransaction) => Promise<T>,\n ops: Partial<TxOps> = {},\n ): Promise<T> {\n this.checkInitialized();\n return await this._withTx(name, writable, this.clientRoot, body, { ...ops, ...defaultTxOps });\n }\n\n public async withWriteTx<T>(\n name: string,\n body: (tx: PlTransaction) => Promise<T>,\n ops: Partial<TxOps> = {},\n ): Promise<T> {\n return await this.withTx(name, true, body, { ...ops, ...defaultTxOps });\n }\n\n public async withReadTx<T>(\n name: string,\n body: (tx: PlTransaction) => Promise<T>,\n ops: Partial<TxOps> = {},\n ): Promise<T> {\n return await this.withTx(name, false, body, { ...ops, ...defaultTxOps });\n }\n\n public getDriver<Drv extends PlDriver>(definition: PlDriverDefinition<Drv>): Drv {\n const attached = this.drivers.get(definition.name);\n if (attached !== undefined) return attached as Drv;\n const driver = definition.init(this, this._ll, this.httpDispatcher);\n this.drivers.set(definition.name, driver);\n return driver;\n }\n\n /** Closes underlying transport */\n public async close() {\n await this._ll.close();\n }\n\n public static async init(\n configOrAddress: PlClientConfig | string,\n auth: AuthOps,\n ops: {\n statusListener?: PlConnectionStatusListener;\n } = {},\n ) {\n const pl = new PlClient(configOrAddress, auth, ops);\n await pl.init();\n return pl;\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;AA6B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zC,QAAA,OAAO,MAAM;IACf;;AAGO,IAAA,MAAM,KAAK,GAAA;AAChB,QAAA,MAAM,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE;IACxB;IAEO,aAAa,IAAI,CACtB,eAAwC,EACxC,IAAa,EACb,GAAA,GAEI,EAAE,EAAA;QAEN,MAAM,EAAE,GAAG,IAAI,QAAQ,CAAC,eAAe,EAAE,IAAI,EAAE,GAAG,CAAC;AACnD,QAAA,MAAM,EAAE,CAAC,IAAI,EAAE;AACf,QAAA,OAAO,EAAE;IACX;AACD;;;;"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const DEFAULT_REQUEST_TIMEOUT = 5_000;
|
|
4
|
+
const DEFAULT_RO_TX_TIMEOUT = 300_000;
|
|
5
|
+
const DEFAULT_RW_TX_TIMEOUT = 60_000;
|
|
6
|
+
const DEFAULT_TOKEN_TTL_SECONDS = 31 * 24 * 60 * 60;
|
|
7
|
+
const DEFAULT_AUTH_MAX_REFRESH = 12 * 24 * 60 * 60;
|
|
8
|
+
const DEFAULT_MAX_CACHE_BYTES = 128_000_000; // 128 Mb
|
|
9
|
+
const DEFAULT_RETRY_BACKOFF_ALGORITHM = 'exponential';
|
|
10
|
+
const DEFAULT_RETRY_MAX_ATTEMPTS = 16; // 1st attempt + 15 retries
|
|
11
|
+
const DEFAULT_RETRY_INITIAL_DELAY = 14; // 14 ms * <jitter> of sleep after first failure
|
|
12
|
+
const DEFAULT_RETRY_EXPONENTIAL_BACKOFF_MULTIPLIER = 1.5; // + 50% on each round
|
|
13
|
+
const DEFAULT_RETRY_LINEAR_BACKOFF_STEP = 50; // + 50 ms
|
|
14
|
+
const DEFAULT_RETRY_JITTER = 0.3; // 30%
|
|
15
|
+
const DefaultRetryOptions = {
|
|
16
|
+
type: 'exponentialBackoff',
|
|
17
|
+
maxAttempts: DEFAULT_RETRY_MAX_ATTEMPTS,
|
|
18
|
+
initialDelay: DEFAULT_RETRY_INITIAL_DELAY,
|
|
19
|
+
backoffMultiplier: DEFAULT_RETRY_EXPONENTIAL_BACKOFF_MULTIPLIER,
|
|
20
|
+
jitter: DEFAULT_RETRY_JITTER,
|
|
21
|
+
};
|
|
22
|
+
function parseInt(s) {
|
|
23
|
+
if (!s)
|
|
24
|
+
return undefined;
|
|
25
|
+
const num = Number(s);
|
|
26
|
+
if (Number.isNaN(num))
|
|
27
|
+
throw new Error(`Can't parse number: ${s}`);
|
|
28
|
+
return num;
|
|
29
|
+
}
|
|
30
|
+
/** Parses pl url and creates a config object that can be passed to
|
|
31
|
+
* {@link PlClient} of {@link UnauthenticatedPlClient}. */
|
|
32
|
+
function plAddressToConfig(address, overrides = {}) {
|
|
33
|
+
if (address.indexOf('://') === -1)
|
|
34
|
+
// non-url address
|
|
35
|
+
return {
|
|
36
|
+
hostAndPort: address,
|
|
37
|
+
ssl: false,
|
|
38
|
+
defaultRequestTimeout: DEFAULT_REQUEST_TIMEOUT,
|
|
39
|
+
defaultROTransactionTimeout: DEFAULT_RO_TX_TIMEOUT,
|
|
40
|
+
defaultRWTransactionTimeout: DEFAULT_RW_TX_TIMEOUT,
|
|
41
|
+
authTTLSeconds: DEFAULT_TOKEN_TTL_SECONDS,
|
|
42
|
+
authMaxRefreshSeconds: DEFAULT_AUTH_MAX_REFRESH,
|
|
43
|
+
txDelay: 0,
|
|
44
|
+
forceSync: false,
|
|
45
|
+
maxCacheBytes: DEFAULT_MAX_CACHE_BYTES,
|
|
46
|
+
retryBackoffAlgorithm: DEFAULT_RETRY_BACKOFF_ALGORITHM,
|
|
47
|
+
retryMaxAttempts: DEFAULT_RETRY_MAX_ATTEMPTS,
|
|
48
|
+
retryInitialDelay: DEFAULT_RETRY_INITIAL_DELAY,
|
|
49
|
+
retryExponentialBackoffMultiplier: DEFAULT_RETRY_EXPONENTIAL_BACKOFF_MULTIPLIER,
|
|
50
|
+
retryLinearBackoffStep: DEFAULT_RETRY_LINEAR_BACKOFF_STEP,
|
|
51
|
+
retryJitter: DEFAULT_RETRY_JITTER,
|
|
52
|
+
...overrides,
|
|
53
|
+
};
|
|
54
|
+
const url = new URL(address);
|
|
55
|
+
if (url.protocol !== 'https:'
|
|
56
|
+
&& url.protocol !== 'http:'
|
|
57
|
+
&& url.protocol !== 'grpc:'
|
|
58
|
+
&& url.protocol !== 'tls:')
|
|
59
|
+
throw new Error(`Unexpected URL schema: ${url.protocol}`);
|
|
60
|
+
if (url.pathname !== '/' && url.pathname !== '')
|
|
61
|
+
throw new Error(`Unexpected URL path: ${url.pathname}`);
|
|
62
|
+
return {
|
|
63
|
+
hostAndPort: url.host, // this also includes port
|
|
64
|
+
alternativeRoot: url.searchParams.get('alternative-root') ?? undefined,
|
|
65
|
+
ssl: url.protocol === 'https:' || url.protocol === 'tls:',
|
|
66
|
+
defaultRequestTimeout: parseInt(url.searchParams.get('request-timeout')) ?? DEFAULT_REQUEST_TIMEOUT,
|
|
67
|
+
defaultROTransactionTimeout: parseInt(url.searchParams.get('ro-tx-timeout'))
|
|
68
|
+
?? parseInt(url.searchParams.get('tx-timeout'))
|
|
69
|
+
?? DEFAULT_RO_TX_TIMEOUT,
|
|
70
|
+
defaultRWTransactionTimeout: parseInt(url.searchParams.get('rw-tx-timeout'))
|
|
71
|
+
?? parseInt(url.searchParams.get('tx-timeout'))
|
|
72
|
+
?? DEFAULT_RW_TX_TIMEOUT,
|
|
73
|
+
authTTLSeconds: DEFAULT_TOKEN_TTL_SECONDS,
|
|
74
|
+
authMaxRefreshSeconds: DEFAULT_AUTH_MAX_REFRESH,
|
|
75
|
+
grpcProxy: url.searchParams.get('grpc-proxy') ?? undefined,
|
|
76
|
+
httpProxy: url.searchParams.get('http-proxy') ?? undefined,
|
|
77
|
+
user: url.username === '' ? undefined : url.username,
|
|
78
|
+
password: url.password === '' ? undefined : url.password,
|
|
79
|
+
txDelay: parseInt(url.searchParams.get('tx-delay')) ?? 0,
|
|
80
|
+
forceSync: Boolean(url.searchParams.get('force-sync')),
|
|
81
|
+
maxCacheBytes: parseInt(url.searchParams.get('max-cache-bytes')) ?? DEFAULT_MAX_CACHE_BYTES,
|
|
82
|
+
retryBackoffAlgorithm: (url.searchParams.get('retry-backoff-algorithm')
|
|
83
|
+
?? DEFAULT_RETRY_BACKOFF_ALGORITHM),
|
|
84
|
+
retryMaxAttempts: parseInt(url.searchParams.get('retry-max-attempts')) ?? DEFAULT_RETRY_MAX_ATTEMPTS,
|
|
85
|
+
retryInitialDelay: parseInt(url.searchParams.get('retry-initial-delay')) ?? DEFAULT_RETRY_INITIAL_DELAY,
|
|
86
|
+
retryExponentialBackoffMultiplier: parseInt(url.searchParams.get('retry-exp-backoff-multiplier'))
|
|
87
|
+
?? DEFAULT_RETRY_EXPONENTIAL_BACKOFF_MULTIPLIER,
|
|
88
|
+
retryLinearBackoffStep: parseInt(url.searchParams.get('retry-linear-backoff-step'))
|
|
89
|
+
?? DEFAULT_RETRY_LINEAR_BACKOFF_STEP,
|
|
90
|
+
retryJitter: parseInt(url.searchParams.get('retry-backoff-jitter')) ?? DEFAULT_RETRY_JITTER,
|
|
91
|
+
...overrides,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
const AnonymousAuthInformation = {};
|
|
95
|
+
|
|
96
|
+
exports.AnonymousAuthInformation = AnonymousAuthInformation;
|
|
97
|
+
exports.DEFAULT_AUTH_MAX_REFRESH = DEFAULT_AUTH_MAX_REFRESH;
|
|
98
|
+
exports.DEFAULT_MAX_CACHE_BYTES = DEFAULT_MAX_CACHE_BYTES;
|
|
99
|
+
exports.DEFAULT_REQUEST_TIMEOUT = DEFAULT_REQUEST_TIMEOUT;
|
|
100
|
+
exports.DEFAULT_RETRY_BACKOFF_ALGORITHM = DEFAULT_RETRY_BACKOFF_ALGORITHM;
|
|
101
|
+
exports.DEFAULT_RETRY_EXPONENTIAL_BACKOFF_MULTIPLIER = DEFAULT_RETRY_EXPONENTIAL_BACKOFF_MULTIPLIER;
|
|
102
|
+
exports.DEFAULT_RETRY_INITIAL_DELAY = DEFAULT_RETRY_INITIAL_DELAY;
|
|
103
|
+
exports.DEFAULT_RETRY_JITTER = DEFAULT_RETRY_JITTER;
|
|
104
|
+
exports.DEFAULT_RETRY_LINEAR_BACKOFF_STEP = DEFAULT_RETRY_LINEAR_BACKOFF_STEP;
|
|
105
|
+
exports.DEFAULT_RETRY_MAX_ATTEMPTS = DEFAULT_RETRY_MAX_ATTEMPTS;
|
|
106
|
+
exports.DEFAULT_RO_TX_TIMEOUT = DEFAULT_RO_TX_TIMEOUT;
|
|
107
|
+
exports.DEFAULT_RW_TX_TIMEOUT = DEFAULT_RW_TX_TIMEOUT;
|
|
108
|
+
exports.DEFAULT_TOKEN_TTL_SECONDS = DEFAULT_TOKEN_TTL_SECONDS;
|
|
109
|
+
exports.DefaultRetryOptions = DefaultRetryOptions;
|
|
110
|
+
exports.plAddressToConfig = plAddressToConfig;
|
|
111
|
+
//# sourceMappingURL=config.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.cjs","sources":["../../src/core/config.ts"],"sourcesContent":["import type { ExponentialBackoffRetryOptions } from '@milaboratories/ts-helpers';\n\n/** Base configuration structure for PL client */\nexport interface PlClientConfig {\n /** Port and host of remote pl server */\n hostAndPort: string;\n\n /** If set, client will expose a nested object under a field with name `alternative_root_${alternativeRoot}` as a\n * client root. */\n alternativeRoot?: string;\n\n /** If true, client will establish tls connection to the server, using default\n * CA of node instance. */\n // Not implementing custom ssl validation logic for now.\n // Implementing it in a correct way is really nontrivial thing,\n // real use-cases should be considered.\n ssl: boolean;\n\n /** Default timeout in milliseconds for unary calls, like ping and login. */\n defaultRequestTimeout: number;\n\n /** Default timeout in milliseconds for read-write transaction, should be\n * adjusted for long round-trip or low bandwidth connections. */\n defaultRWTransactionTimeout: number;\n /** Default timeout in milliseconds for read-only transaction, should be\n * adjusted for long round-trip or low bandwidth connections. */\n defaultROTransactionTimeout: number;\n\n /** Controls what TTL will be requested from the server, when new JWT token\n * is requested. */\n authTTLSeconds: number;\n /** If token is older than this time, it will be refreshed regardless of its\n * expiration time. */\n authMaxRefreshSeconds: number;\n\n /** Proxy server URL to use for pl connection. */\n grpcProxy?: string;\n /** Proxy server URL to use for http connections of pl drivers, like file\n * downloading. */\n httpProxy?: string;\n\n /** Username extracted from pl URL. Ignored by {@link PlClient}, picked up by {@link defaultPlClient}. */\n user?: string;\n /** Password extracted from pl URL. Ignored by {@link PlClient}, picked up by {@link defaultPlClient}. */\n password?: string;\n\n /** Artificial delay introduced after write transactions completion, to\n * somewhat throttle the load on pl. Delay introduced after sync, if requested. */\n txDelay: number;\n\n /** Last resort measure to solve complicated race conditions in pl. */\n forceSync: boolean;\n\n /** Maximal number of bytes of resource state to cache */\n maxCacheBytes: number;\n\n //\n // Retry\n //\n\n /**\n * What type of backoff strategy to use in transaction retries\n * (pl uses optimistic transaction model with regular retries in write transactions)\n * */\n retryBackoffAlgorithm: 'exponential' | 'linear';\n\n /** Maximal number of attempts in */\n retryMaxAttempts: number;\n\n /** Delay after first failed attempt, in ms. */\n retryInitialDelay: number;\n\n /** Each time delay will be multiplied by this number (1.5 means plus on 50% each attempt) */\n retryExponentialBackoffMultiplier: number;\n\n /** [used only for ] This value will be added to the delay from the previous step, in ms */\n retryLinearBackoffStep: number;\n\n /** Value from 0 to 1, determine level of randomness to introduce to the backoff delays sequence. (0 meaning no randomness) */\n retryJitter: number;\n}\n\nexport const DEFAULT_REQUEST_TIMEOUT = 5_000;\nexport const DEFAULT_RO_TX_TIMEOUT = 300_000;\nexport const DEFAULT_RW_TX_TIMEOUT = 60_000;\nexport const DEFAULT_TOKEN_TTL_SECONDS = 31 * 24 * 60 * 60;\nexport const DEFAULT_AUTH_MAX_REFRESH = 12 * 24 * 60 * 60;\n\nexport const DEFAULT_MAX_CACHE_BYTES = 128_000_000; // 128 Mb\n\nexport const DEFAULT_RETRY_BACKOFF_ALGORITHM = 'exponential';\nexport const DEFAULT_RETRY_MAX_ATTEMPTS = 16; // 1st attempt + 15 retries\nexport const DEFAULT_RETRY_INITIAL_DELAY = 14; // 14 ms * <jitter> of sleep after first failure\nexport const DEFAULT_RETRY_EXPONENTIAL_BACKOFF_MULTIPLIER = 1.5; // + 50% on each round\nexport const DEFAULT_RETRY_LINEAR_BACKOFF_STEP = 50; // + 50 ms\nexport const DEFAULT_RETRY_JITTER = 0.3; // 30%\n\nexport const DefaultRetryOptions: ExponentialBackoffRetryOptions = {\n type: 'exponentialBackoff',\n maxAttempts: DEFAULT_RETRY_MAX_ATTEMPTS,\n initialDelay: DEFAULT_RETRY_INITIAL_DELAY,\n backoffMultiplier: DEFAULT_RETRY_EXPONENTIAL_BACKOFF_MULTIPLIER,\n jitter: DEFAULT_RETRY_JITTER,\n};\n\ntype PlConfigOverrides = Partial<\n Pick<\n PlClientConfig,\n | 'ssl'\n | 'defaultRequestTimeout'\n | 'defaultROTransactionTimeout'\n | 'defaultRWTransactionTimeout'\n | 'httpProxy'\n | 'grpcProxy'\n >\n>;\n\nfunction parseInt(s: string | null | undefined): number | undefined {\n if (!s) return undefined;\n const num = Number(s);\n if (Number.isNaN(num)) throw new Error(`Can't parse number: ${s}`);\n return num;\n}\n\n/** Parses pl url and creates a config object that can be passed to\n * {@link PlClient} of {@link UnauthenticatedPlClient}. */\nexport function plAddressToConfig(\n address: string,\n overrides: PlConfigOverrides = {},\n): PlClientConfig {\n if (address.indexOf('://') === -1)\n // non-url address\n return {\n hostAndPort: address,\n ssl: false,\n defaultRequestTimeout: DEFAULT_REQUEST_TIMEOUT,\n defaultROTransactionTimeout: DEFAULT_RO_TX_TIMEOUT,\n defaultRWTransactionTimeout: DEFAULT_RW_TX_TIMEOUT,\n authTTLSeconds: DEFAULT_TOKEN_TTL_SECONDS,\n authMaxRefreshSeconds: DEFAULT_AUTH_MAX_REFRESH,\n txDelay: 0,\n forceSync: false,\n\n maxCacheBytes: DEFAULT_MAX_CACHE_BYTES,\n\n retryBackoffAlgorithm: DEFAULT_RETRY_BACKOFF_ALGORITHM,\n retryMaxAttempts: DEFAULT_RETRY_MAX_ATTEMPTS,\n retryInitialDelay: DEFAULT_RETRY_INITIAL_DELAY,\n retryExponentialBackoffMultiplier: DEFAULT_RETRY_EXPONENTIAL_BACKOFF_MULTIPLIER,\n retryLinearBackoffStep: DEFAULT_RETRY_LINEAR_BACKOFF_STEP,\n retryJitter: DEFAULT_RETRY_JITTER,\n\n ...overrides,\n };\n\n const url = new URL(address);\n\n if (\n url.protocol !== 'https:'\n && url.protocol !== 'http:'\n && url.protocol !== 'grpc:'\n && url.protocol !== 'tls:'\n )\n throw new Error(`Unexpected URL schema: ${url.protocol}`);\n\n if (url.pathname !== '/' && url.pathname !== '')\n throw new Error(`Unexpected URL path: ${url.pathname}`);\n\n return {\n hostAndPort: url.host, // this also includes port\n alternativeRoot: url.searchParams.get('alternative-root') ?? undefined,\n ssl: url.protocol === 'https:' || url.protocol === 'tls:',\n defaultRequestTimeout:\n parseInt(url.searchParams.get('request-timeout')) ?? DEFAULT_REQUEST_TIMEOUT,\n defaultROTransactionTimeout:\n parseInt(url.searchParams.get('ro-tx-timeout'))\n ?? parseInt(url.searchParams.get('tx-timeout'))\n ?? DEFAULT_RO_TX_TIMEOUT,\n defaultRWTransactionTimeout:\n parseInt(url.searchParams.get('rw-tx-timeout'))\n ?? parseInt(url.searchParams.get('tx-timeout'))\n ?? DEFAULT_RW_TX_TIMEOUT,\n authTTLSeconds: DEFAULT_TOKEN_TTL_SECONDS,\n authMaxRefreshSeconds: DEFAULT_AUTH_MAX_REFRESH,\n grpcProxy: url.searchParams.get('grpc-proxy') ?? undefined,\n httpProxy: url.searchParams.get('http-proxy') ?? undefined,\n user: url.username === '' ? undefined : url.username,\n password: url.password === '' ? undefined : url.password,\n txDelay: parseInt(url.searchParams.get('tx-delay')) ?? 0,\n forceSync: Boolean(url.searchParams.get('force-sync')),\n\n maxCacheBytes: parseInt(url.searchParams.get('max-cache-bytes')) ?? DEFAULT_MAX_CACHE_BYTES,\n\n retryBackoffAlgorithm: (url.searchParams.get('retry-backoff-algorithm')\n ?? DEFAULT_RETRY_BACKOFF_ALGORITHM) as any,\n retryMaxAttempts:\n parseInt(url.searchParams.get('retry-max-attempts')) ?? DEFAULT_RETRY_MAX_ATTEMPTS,\n retryInitialDelay:\n parseInt(url.searchParams.get('retry-initial-delay')) ?? DEFAULT_RETRY_INITIAL_DELAY,\n retryExponentialBackoffMultiplier:\n parseInt(url.searchParams.get('retry-exp-backoff-multiplier'))\n ?? DEFAULT_RETRY_EXPONENTIAL_BACKOFF_MULTIPLIER,\n retryLinearBackoffStep:\n parseInt(url.searchParams.get('retry-linear-backoff-step'))\n ?? DEFAULT_RETRY_LINEAR_BACKOFF_STEP,\n retryJitter: parseInt(url.searchParams.get('retry-backoff-jitter')) ?? DEFAULT_RETRY_JITTER,\n\n ...overrides,\n };\n}\n\n/**\n * Authorization data / JWT Token.\n * Absent JWT Token tells the client to connect as anonymous user.\n * */\nexport interface AuthInformation {\n /** Absent token means anonymous access */\n jwtToken?: string;\n}\n\nexport const AnonymousAuthInformation: AuthInformation = {};\n\n/** Authorization related settings to pass to {@link PlClient}. */\nexport interface AuthOps {\n /** Initial authorization information */\n authInformation: AuthInformation;\n /** Will be executed after successful authorization information refresh */\n readonly onUpdate?: (newInfo: AuthInformation) => void;\n /** Will be executed if auth-related error happens during normal client operation */\n readonly onAuthError?: () => void;\n /** Will be executed if error encountered during token update */\n readonly onUpdateError?: (error: unknown) => void;\n}\n\n/** Connection status. */\nexport type PlConnectionStatus = 'OK' | 'Disconnected' | 'Unauthenticated';\n\n/** Listener that will be called each time connection status changes. */\nexport type PlConnectionStatusListener = (status: PlConnectionStatus) => void;\n"],"names":[],"mappings":";;AAkFO,MAAM,uBAAuB,GAAG;AAChC,MAAM,qBAAqB,GAAG;AAC9B,MAAM,qBAAqB,GAAG;AAC9B,MAAM,yBAAyB,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG;AACjD,MAAM,wBAAwB,GAAG,EAAE,GAAG,EA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eAC1D,4CAA4C;QACjD,sBAAsB,EACpB,QAAQ,CAAC,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,2BAA2B,CAAC;eACvD,iCAAiC;AACtC,QAAA,WAAW,EAAE,QAAQ,CAAC,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC,IAAI,oBAAoB;AAE3F,QAAA,GAAG,SAAS;KACb;AACH;AAWO,MAAM,wBAAwB,GAAoB;;;;;;;;;;;;;;;;;;"}
|
package/dist/core/config.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ExponentialBackoffRetryOptions } from '@milaboratories/ts-helpers';
|
|
1
2
|
/** Base configuration structure for PL client */
|
|
2
3
|
export interface PlClientConfig {
|
|
3
4
|
/** Port and host of remote pl server */
|
|
@@ -66,6 +67,7 @@ export declare const DEFAULT_RETRY_INITIAL_DELAY = 14;
|
|
|
66
67
|
export declare const DEFAULT_RETRY_EXPONENTIAL_BACKOFF_MULTIPLIER = 1.5;
|
|
67
68
|
export declare const DEFAULT_RETRY_LINEAR_BACKOFF_STEP = 50;
|
|
68
69
|
export declare const DEFAULT_RETRY_JITTER = 0.3;
|
|
70
|
+
export declare const DefaultRetryOptions: ExponentialBackoffRetryOptions;
|
|
69
71
|
type PlConfigOverrides = Partial<Pick<PlClientConfig, 'ssl' | 'defaultRequestTimeout' | 'defaultROTransactionTimeout' | 'defaultRWTransactionTimeout' | 'httpProxy' | 'grpcProxy'>>;
|
|
70
72
|
/** Parses pl url and creates a config object that can be passed to
|
|
71
73
|
* {@link PlClient} of {@link UnauthenticatedPlClient}. */
|
|
@@ -95,4 +97,3 @@ export type PlConnectionStatus = 'OK' | 'Disconnected' | 'Unauthenticated';
|
|
|
95
97
|
/** Listener that will be called each time connection status changes. */
|
|
96
98
|
export type PlConnectionStatusListener = (status: PlConnectionStatus) => void;
|
|
97
99
|
export {};
|
|
98
|
-
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
const DEFAULT_REQUEST_TIMEOUT = 5_000;
|
|
2
|
+
const DEFAULT_RO_TX_TIMEOUT = 300_000;
|
|
3
|
+
const DEFAULT_RW_TX_TIMEOUT = 60_000;
|
|
4
|
+
const DEFAULT_TOKEN_TTL_SECONDS = 31 * 24 * 60 * 60;
|
|
5
|
+
const DEFAULT_AUTH_MAX_REFRESH = 12 * 24 * 60 * 60;
|
|
6
|
+
const DEFAULT_MAX_CACHE_BYTES = 128_000_000; // 128 Mb
|
|
7
|
+
const DEFAULT_RETRY_BACKOFF_ALGORITHM = 'exponential';
|
|
8
|
+
const DEFAULT_RETRY_MAX_ATTEMPTS = 16; // 1st attempt + 15 retries
|
|
9
|
+
const DEFAULT_RETRY_INITIAL_DELAY = 14; // 14 ms * <jitter> of sleep after first failure
|
|
10
|
+
const DEFAULT_RETRY_EXPONENTIAL_BACKOFF_MULTIPLIER = 1.5; // + 50% on each round
|
|
11
|
+
const DEFAULT_RETRY_LINEAR_BACKOFF_STEP = 50; // + 50 ms
|
|
12
|
+
const DEFAULT_RETRY_JITTER = 0.3; // 30%
|
|
13
|
+
const DefaultRetryOptions = {
|
|
14
|
+
type: 'exponentialBackoff',
|
|
15
|
+
maxAttempts: DEFAULT_RETRY_MAX_ATTEMPTS,
|
|
16
|
+
initialDelay: DEFAULT_RETRY_INITIAL_DELAY,
|
|
17
|
+
backoffMultiplier: DEFAULT_RETRY_EXPONENTIAL_BACKOFF_MULTIPLIER,
|
|
18
|
+
jitter: DEFAULT_RETRY_JITTER,
|
|
19
|
+
};
|
|
20
|
+
function parseInt(s) {
|
|
21
|
+
if (!s)
|
|
22
|
+
return undefined;
|
|
23
|
+
const num = Number(s);
|
|
24
|
+
if (Number.isNaN(num))
|
|
25
|
+
throw new Error(`Can't parse number: ${s}`);
|
|
26
|
+
return num;
|
|
27
|
+
}
|
|
28
|
+
/** Parses pl url and creates a config object that can be passed to
|
|
29
|
+
* {@link PlClient} of {@link UnauthenticatedPlClient}. */
|
|
30
|
+
function plAddressToConfig(address, overrides = {}) {
|
|
31
|
+
if (address.indexOf('://') === -1)
|
|
32
|
+
// non-url address
|
|
33
|
+
return {
|
|
34
|
+
hostAndPort: address,
|
|
35
|
+
ssl: false,
|
|
36
|
+
defaultRequestTimeout: DEFAULT_REQUEST_TIMEOUT,
|
|
37
|
+
defaultROTransactionTimeout: DEFAULT_RO_TX_TIMEOUT,
|
|
38
|
+
defaultRWTransactionTimeout: DEFAULT_RW_TX_TIMEOUT,
|
|
39
|
+
authTTLSeconds: DEFAULT_TOKEN_TTL_SECONDS,
|
|
40
|
+
authMaxRefreshSeconds: DEFAULT_AUTH_MAX_REFRESH,
|
|
41
|
+
txDelay: 0,
|
|
42
|
+
forceSync: false,
|
|
43
|
+
maxCacheBytes: DEFAULT_MAX_CACHE_BYTES,
|
|
44
|
+
retryBackoffAlgorithm: DEFAULT_RETRY_BACKOFF_ALGORITHM,
|
|
45
|
+
retryMaxAttempts: DEFAULT_RETRY_MAX_ATTEMPTS,
|
|
46
|
+
retryInitialDelay: DEFAULT_RETRY_INITIAL_DELAY,
|
|
47
|
+
retryExponentialBackoffMultiplier: DEFAULT_RETRY_EXPONENTIAL_BACKOFF_MULTIPLIER,
|
|
48
|
+
retryLinearBackoffStep: DEFAULT_RETRY_LINEAR_BACKOFF_STEP,
|
|
49
|
+
retryJitter: DEFAULT_RETRY_JITTER,
|
|
50
|
+
...overrides,
|
|
51
|
+
};
|
|
52
|
+
const url = new URL(address);
|
|
53
|
+
if (url.protocol !== 'https:'
|
|
54
|
+
&& url.protocol !== 'http:'
|
|
55
|
+
&& url.protocol !== 'grpc:'
|
|
56
|
+
&& url.protocol !== 'tls:')
|
|
57
|
+
throw new Error(`Unexpected URL schema: ${url.protocol}`);
|
|
58
|
+
if (url.pathname !== '/' && url.pathname !== '')
|
|
59
|
+
throw new Error(`Unexpected URL path: ${url.pathname}`);
|
|
60
|
+
return {
|
|
61
|
+
hostAndPort: url.host, // this also includes port
|
|
62
|
+
alternativeRoot: url.searchParams.get('alternative-root') ?? undefined,
|
|
63
|
+
ssl: url.protocol === 'https:' || url.protocol === 'tls:',
|
|
64
|
+
defaultRequestTimeout: parseInt(url.searchParams.get('request-timeout')) ?? DEFAULT_REQUEST_TIMEOUT,
|
|
65
|
+
defaultROTransactionTimeout: parseInt(url.searchParams.get('ro-tx-timeout'))
|
|
66
|
+
?? parseInt(url.searchParams.get('tx-timeout'))
|
|
67
|
+
?? DEFAULT_RO_TX_TIMEOUT,
|
|
68
|
+
defaultRWTransactionTimeout: parseInt(url.searchParams.get('rw-tx-timeout'))
|
|
69
|
+
?? parseInt(url.searchParams.get('tx-timeout'))
|
|
70
|
+
?? DEFAULT_RW_TX_TIMEOUT,
|
|
71
|
+
authTTLSeconds: DEFAULT_TOKEN_TTL_SECONDS,
|
|
72
|
+
authMaxRefreshSeconds: DEFAULT_AUTH_MAX_REFRESH,
|
|
73
|
+
grpcProxy: url.searchParams.get('grpc-proxy') ?? undefined,
|
|
74
|
+
httpProxy: url.searchParams.get('http-proxy') ?? undefined,
|
|
75
|
+
user: url.username === '' ? undefined : url.username,
|
|
76
|
+
password: url.password === '' ? undefined : url.password,
|
|
77
|
+
txDelay: parseInt(url.searchParams.get('tx-delay')) ?? 0,
|
|
78
|
+
forceSync: Boolean(url.searchParams.get('force-sync')),
|
|
79
|
+
maxCacheBytes: parseInt(url.searchParams.get('max-cache-bytes')) ?? DEFAULT_MAX_CACHE_BYTES,
|
|
80
|
+
retryBackoffAlgorithm: (url.searchParams.get('retry-backoff-algorithm')
|
|
81
|
+
?? DEFAULT_RETRY_BACKOFF_ALGORITHM),
|
|
82
|
+
retryMaxAttempts: parseInt(url.searchParams.get('retry-max-attempts')) ?? DEFAULT_RETRY_MAX_ATTEMPTS,
|
|
83
|
+
retryInitialDelay: parseInt(url.searchParams.get('retry-initial-delay')) ?? DEFAULT_RETRY_INITIAL_DELAY,
|
|
84
|
+
retryExponentialBackoffMultiplier: parseInt(url.searchParams.get('retry-exp-backoff-multiplier'))
|
|
85
|
+
?? DEFAULT_RETRY_EXPONENTIAL_BACKOFF_MULTIPLIER,
|
|
86
|
+
retryLinearBackoffStep: parseInt(url.searchParams.get('retry-linear-backoff-step'))
|
|
87
|
+
?? DEFAULT_RETRY_LINEAR_BACKOFF_STEP,
|
|
88
|
+
retryJitter: parseInt(url.searchParams.get('retry-backoff-jitter')) ?? DEFAULT_RETRY_JITTER,
|
|
89
|
+
...overrides,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
const AnonymousAuthInformation = {};
|
|
93
|
+
|
|
94
|
+
export { AnonymousAuthInformation, DEFAULT_AUTH_MAX_REFRESH, DEFAULT_MAX_CACHE_BYTES, DEFAULT_REQUEST_TIMEOUT, DEFAULT_RETRY_BACKOFF_ALGORITHM, DEFAULT_RETRY_EXPONENTIAL_BACKOFF_MULTIPLIER, DEFAULT_RETRY_INITIAL_DELAY, DEFAULT_RETRY_JITTER, DEFAULT_RETRY_LINEAR_BACKOFF_STEP, DEFAULT_RETRY_MAX_ATTEMPTS, DEFAULT_RO_TX_TIMEOUT, DEFAULT_RW_TX_TIMEOUT, DEFAULT_TOKEN_TTL_SECONDS, DefaultRetryOptions, plAddressToConfig };
|
|
95
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sources":["../../src/core/config.ts"],"sourcesContent":["import type { ExponentialBackoffRetryOptions } from '@milaboratories/ts-helpers';\n\n/** Base configuration structure for PL client */\nexport interface PlClientConfig {\n /** Port and host of remote pl server */\n hostAndPort: string;\n\n /** If set, client will expose a nested object under a field with name `alternative_root_${alternativeRoot}` as a\n * client root. */\n alternativeRoot?: string;\n\n /** If true, client will establish tls connection to the server, using default\n * CA of node instance. */\n // Not implementing custom ssl validation logic for now.\n // Implementing it in a correct way is really nontrivial thing,\n // real use-cases should be considered.\n ssl: boolean;\n\n /** Default timeout in milliseconds for unary calls, like ping and login. */\n defaultRequestTimeout: number;\n\n /** Default timeout in milliseconds for read-write transaction, should be\n * adjusted for long round-trip or low bandwidth connections. */\n defaultRWTransactionTimeout: number;\n /** Default timeout in milliseconds for read-only transaction, should be\n * adjusted for long round-trip or low bandwidth connections. */\n defaultROTransactionTimeout: number;\n\n /** Controls what TTL will be requested from the server, when new JWT token\n * is requested. */\n authTTLSeconds: number;\n /** If token is older than this time, it will be refreshed regardless of its\n * expiration time. */\n authMaxRefreshSeconds: number;\n\n /** Proxy server URL to use for pl connection. */\n grpcProxy?: string;\n /** Proxy server URL to use for http connections of pl drivers, like file\n * downloading. */\n httpProxy?: string;\n\n /** Username extracted from pl URL. Ignored by {@link PlClient}, picked up by {@link defaultPlClient}. */\n user?: string;\n /** Password extracted from pl URL. Ignored by {@link PlClient}, picked up by {@link defaultPlClient}. */\n password?: string;\n\n /** Artificial delay introduced after write transactions completion, to\n * somewhat throttle the load on pl. Delay introduced after sync, if requested. */\n txDelay: number;\n\n /** Last resort measure to solve complicated race conditions in pl. */\n forceSync: boolean;\n\n /** Maximal number of bytes of resource state to cache */\n maxCacheBytes: number;\n\n //\n // Retry\n //\n\n /**\n * What type of backoff strategy to use in transaction retries\n * (pl uses optimistic transaction model with regular retries in write transactions)\n * */\n retryBackoffAlgorithm: 'exponential' | 'linear';\n\n /** Maximal number of attempts in */\n retryMaxAttempts: number;\n\n /** Delay after first failed attempt, in ms. */\n retryInitialDelay: number;\n\n /** Each time delay will be multiplied by this number (1.5 means plus on 50% each attempt) */\n retryExponentialBackoffMultiplier: number;\n\n /** [used only for ] This value will be added to the delay from the previous step, in ms */\n retryLinearBackoffStep: number;\n\n /** Value from 0 to 1, determine level of randomness to introduce to the backoff delays sequence. (0 meaning no randomness) */\n retryJitter: number;\n}\n\nexport const DEFAULT_REQUEST_TIMEOUT = 5_000;\nexport const DEFAULT_RO_TX_TIMEOUT = 300_000;\nexport const DEFAULT_RW_TX_TIMEOUT = 60_000;\nexport const DEFAULT_TOKEN_TTL_SECONDS = 31 * 24 * 60 * 60;\nexport const DEFAULT_AUTH_MAX_REFRESH = 12 * 24 * 60 * 60;\n\nexport const DEFAULT_MAX_CACHE_BYTES = 128_000_000; // 128 Mb\n\nexport const DEFAULT_RETRY_BACKOFF_ALGORITHM = 'exponential';\nexport const DEFAULT_RETRY_MAX_ATTEMPTS = 16; // 1st attempt + 15 retries\nexport const DEFAULT_RETRY_INITIAL_DELAY = 14; // 14 ms * <jitter> of sleep after first failure\nexport const DEFAULT_RETRY_EXPONENTIAL_BACKOFF_MULTIPLIER = 1.5; // + 50% on each round\nexport const DEFAULT_RETRY_LINEAR_BACKOFF_STEP = 50; // + 50 ms\nexport const DEFAULT_RETRY_JITTER = 0.3; // 30%\n\nexport const DefaultRetryOptions: ExponentialBackoffRetryOptions = {\n type: 'exponentialBackoff',\n maxAttempts: DEFAULT_RETRY_MAX_ATTEMPTS,\n initialDelay: DEFAULT_RETRY_INITIAL_DELAY,\n backoffMultiplier: DEFAULT_RETRY_EXPONENTIAL_BACKOFF_MULTIPLIER,\n jitter: DEFAULT_RETRY_JITTER,\n};\n\ntype PlConfigOverrides = Partial<\n Pick<\n PlClientConfig,\n | 'ssl'\n | 'defaultRequestTimeout'\n | 'defaultROTransactionTimeout'\n | 'defaultRWTransactionTimeout'\n | 'httpProxy'\n | 'grpcProxy'\n >\n>;\n\nfunction parseInt(s: string | null | undefined): number | undefined {\n if (!s) return undefined;\n const num = Number(s);\n if (Number.isNaN(num)) throw new Error(`Can't parse number: ${s}`);\n return num;\n}\n\n/** Parses pl url and creates a config object that can be passed to\n * {@link PlClient} of {@link UnauthenticatedPlClient}. */\nexport function plAddressToConfig(\n address: string,\n overrides: PlConfigOverrides = {},\n): PlClientConfig {\n if (address.indexOf('://') === -1)\n // non-url address\n return {\n hostAndPort: address,\n ssl: false,\n defaultRequestTimeout: DEFAULT_REQUEST_TIMEOUT,\n defaultROTransactionTimeout: DEFAULT_RO_TX_TIMEOUT,\n defaultRWTransactionTimeout: DEFAULT_RW_TX_TIMEOUT,\n authTTLSeconds: DEFAULT_TOKEN_TTL_SECONDS,\n authMaxRefreshSeconds: DEFAULT_AUTH_MAX_REFRESH,\n txDelay: 0,\n forceSync: false,\n\n maxCacheBytes: DEFAULT_MAX_CACHE_BYTES,\n\n retryBackoffAlgorithm: DEFAULT_RETRY_BACKOFF_ALGORITHM,\n retryMaxAttempts: DEFAULT_RETRY_MAX_ATTEMPTS,\n retryInitialDelay: DEFAULT_RETRY_INITIAL_DELAY,\n retryExponentialBackoffMultiplier: DEFAULT_RETRY_EXPONENTIAL_BACKOFF_MULTIPLIER,\n retryLinearBackoffStep: DEFAULT_RETRY_LINEAR_BACKOFF_STEP,\n retryJitter: DEFAULT_RETRY_JITTER,\n\n ...overrides,\n };\n\n const url = new URL(address);\n\n if (\n url.protocol !== 'https:'\n && url.protocol !== 'http:'\n && url.protocol !== 'grpc:'\n && url.protocol !== 'tls:'\n )\n throw new Error(`Unexpected URL schema: ${url.protocol}`);\n\n if (url.pathname !== '/' && url.pathname !== '')\n throw new Error(`Unexpected URL path: ${url.pathname}`);\n\n return {\n hostAndPort: url.host, // this also includes port\n alternativeRoot: url.searchParams.get('alternative-root') ?? undefined,\n ssl: url.protocol === 'https:' || url.protocol === 'tls:',\n defaultRequestTimeout:\n parseInt(url.searchParams.get('request-timeout')) ?? DEFAULT_REQUEST_TIMEOUT,\n defaultROTransactionTimeout:\n parseInt(url.searchParams.get('ro-tx-timeout'))\n ?? parseInt(url.searchParams.get('tx-timeout'))\n ?? DEFAULT_RO_TX_TIMEOUT,\n defaultRWTransactionTimeout:\n parseInt(url.searchParams.get('rw-tx-timeout'))\n ?? parseInt(url.searchParams.get('tx-timeout'))\n ?? DEFAULT_RW_TX_TIMEOUT,\n authTTLSeconds: DEFAULT_TOKEN_TTL_SECONDS,\n authMaxRefreshSeconds: DEFAULT_AUTH_MAX_REFRESH,\n grpcProxy: url.searchParams.get('grpc-proxy') ?? undefined,\n httpProxy: url.searchParams.get('http-proxy') ?? undefined,\n user: url.username === '' ? undefined : url.username,\n password: url.password === '' ? undefined : url.password,\n txDelay: parseInt(url.searchParams.get('tx-delay')) ?? 0,\n forceSync: Boolean(url.searchParams.get('force-sync')),\n\n maxCacheBytes: parseInt(url.searchParams.get('max-cache-bytes')) ?? DEFAULT_MAX_CACHE_BYTES,\n\n retryBackoffAlgorithm: (url.searchParams.get('retry-backoff-algorithm')\n ?? DEFAULT_RETRY_BACKOFF_ALGORITHM) as any,\n retryMaxAttempts:\n parseInt(url.searchParams.get('retry-max-attempts')) ?? DEFAULT_RETRY_MAX_ATTEMPTS,\n retryInitialDelay:\n parseInt(url.searchParams.get('retry-initial-delay')) ?? DEFAULT_RETRY_INITIAL_DELAY,\n retryExponentialBackoffMultiplier:\n parseInt(url.searchParams.get('retry-exp-backoff-multiplier'))\n ?? DEFAULT_RETRY_EXPONENTIAL_BACKOFF_MULTIPLIER,\n retryLinearBackoffStep:\n parseInt(url.searchParams.get('retry-linear-backoff-step'))\n ?? DEFAULT_RETRY_LINEAR_BACKOFF_STEP,\n retryJitter: parseInt(url.searchParams.get('retry-backoff-jitter')) ?? DEFAULT_RETRY_JITTER,\n\n ...overrides,\n };\n}\n\n/**\n * Authorization data / JWT Token.\n * Absent JWT Token tells the client to connect as anonymous user.\n * */\nexport interface AuthInformation {\n /** Absent token means anonymous access */\n jwtToken?: string;\n}\n\nexport const AnonymousAuthInformation: AuthInformation = {};\n\n/** Authorization related settings to pass to {@link PlClient}. */\nexport interface AuthOps {\n /** Initial authorization information */\n authInformation: AuthInformation;\n /** Will be executed after successful authorization information refresh */\n readonly onUpdate?: (newInfo: AuthInformation) => void;\n /** Will be executed if auth-related error happens during normal client operation */\n readonly onAuthError?: () => void;\n /** Will be executed if error encountered during token update */\n readonly onUpdateError?: (error: unknown) => void;\n}\n\n/** Connection status. */\nexport type PlConnectionStatus = 'OK' | 'Disconnected' | 'Unauthenticated';\n\n/** Listener that will be called each time connection status changes. */\nexport type PlConnectionStatusListener = (status: PlConnectionStatus) => void;\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