@milaboratories/pl-client 2.16.1 → 2.16.3

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.
Files changed (58) hide show
  1. package/dist/__external/.pnpm/@rollup_plugin-typescript@12.1.4_rollup@4.52.4_tslib@2.7.0_typescript@5.6.3/__external/tslib/tslib.es6.cjs +61 -0
  2. package/dist/__external/.pnpm/@rollup_plugin-typescript@12.1.4_rollup@4.52.4_tslib@2.7.0_typescript@5.6.3/__external/tslib/tslib.es6.cjs.map +1 -0
  3. package/dist/__external/.pnpm/@rollup_plugin-typescript@12.1.4_rollup@4.52.4_tslib@2.7.0_typescript@5.6.3/__external/tslib/tslib.es6.js +58 -0
  4. package/dist/__external/.pnpm/@rollup_plugin-typescript@12.1.4_rollup@4.52.4_tslib@2.7.0_typescript@5.6.3/__external/tslib/tslib.es6.js.map +1 -0
  5. package/dist/core/PromiseTracker.cjs +39 -0
  6. package/dist/core/PromiseTracker.cjs.map +1 -0
  7. package/dist/core/PromiseTracker.d.ts +14 -0
  8. package/dist/core/PromiseTracker.d.ts.map +1 -0
  9. package/dist/core/PromiseTracker.js +37 -0
  10. package/dist/core/PromiseTracker.js.map +1 -0
  11. package/dist/core/StatefulPromise.cjs +65 -0
  12. package/dist/core/StatefulPromise.cjs.map +1 -0
  13. package/dist/core/StatefulPromise.d.ts +39 -0
  14. package/dist/core/StatefulPromise.d.ts.map +1 -0
  15. package/dist/core/StatefulPromise.js +63 -0
  16. package/dist/core/StatefulPromise.js.map +1 -0
  17. package/dist/core/ll_transaction.cjs +3 -2
  18. package/dist/core/ll_transaction.cjs.map +1 -1
  19. package/dist/core/ll_transaction.d.ts.map +1 -1
  20. package/dist/core/ll_transaction.js +3 -2
  21. package/dist/core/ll_transaction.js.map +1 -1
  22. package/dist/core/transaction.cjs +577 -515
  23. package/dist/core/transaction.cjs.map +1 -1
  24. package/dist/core/transaction.d.ts +6 -4
  25. package/dist/core/transaction.d.ts.map +1 -1
  26. package/dist/core/transaction.js +578 -516
  27. package/dist/core/transaction.js.map +1 -1
  28. package/dist/test/tcp-proxy.cjs +129 -0
  29. package/dist/test/tcp-proxy.cjs.map +1 -0
  30. package/dist/test/tcp-proxy.d.ts +17 -0
  31. package/dist/test/tcp-proxy.d.ts.map +1 -0
  32. package/dist/test/tcp-proxy.js +107 -0
  33. package/dist/test/tcp-proxy.js.map +1 -0
  34. package/dist/test/test_config.cjs +54 -7
  35. package/dist/test/test_config.cjs.map +1 -1
  36. package/dist/test/test_config.d.ts +18 -2
  37. package/dist/test/test_config.d.ts.map +1 -1
  38. package/dist/test/test_config.js +54 -7
  39. package/dist/test/test_config.js.map +1 -1
  40. package/package.json +7 -8
  41. package/src/core/PromiseTracker.ts +39 -0
  42. package/src/core/StatefulPromise.ts +92 -0
  43. package/src/core/client.test.ts +1 -1
  44. package/src/core/config.test.ts +1 -1
  45. package/src/core/connectivity.test.ts +70 -0
  46. package/src/core/error.test.ts +1 -1
  47. package/src/core/ll_client.test.ts +1 -1
  48. package/src/core/ll_transaction.test.ts +1 -1
  49. package/src/core/ll_transaction.ts +3 -2
  50. package/src/core/transaction.test.ts +6 -1
  51. package/src/core/transaction.ts +91 -48
  52. package/src/core/types.test.ts +1 -1
  53. package/src/core/unauth_client.test.ts +1 -1
  54. package/src/helpers/rich_resource_types.test.ts +1 -1
  55. package/src/test/tcp-proxy.ts +126 -0
  56. package/src/test/test_config.test.ts +1 -1
  57. package/src/test/test_config.ts +82 -7
  58. package/src/util/util.test.ts +1 -1
@@ -1,5 +1,6 @@
1
1
  'use strict';
2
2
 
3
+ var tslib_es6 = require('../__external/.pnpm/@rollup_plugin-typescript@12.1.4_rollup@4.52.4_tslib@2.7.0_typescript@5.6.3/__external/tslib/tslib.es6.cjs');
3
4
  var types = require('./types.cjs');
4
5
  var api = require('../proto/github.com/milaboratory/pl/plapi/plapiproto/api.cjs');
5
6
  var util = require('../util/util.cjs');
@@ -9,6 +10,7 @@ var errors = require('./errors.cjs');
9
10
  var stat = require('./stat.cjs');
10
11
  var error_resource = require('./error_resource.cjs');
11
12
  var pl = require('../helpers/pl.cjs');
13
+ var PromiseTracker = require('./PromiseTracker.cjs');
12
14
 
13
15
  function isField(ref) {
14
16
  return ref.hasOwnProperty('resourceId') && ref.hasOwnProperty('fieldName');
@@ -66,6 +68,15 @@ async function notFoundToUndefined(cb) {
66
68
  throw e;
67
69
  }
68
70
  }
71
+ /**
72
+ * Decorator that wraps the method's returned promise with this.track()
73
+ * This ensures that the promise will be awaited before the transaction is completed.
74
+ */
75
+ function tracked(value, _context) {
76
+ return function (...args) {
77
+ return this.track(value.apply(this, args));
78
+ };
79
+ }
69
80
  /**
70
81
  * Each platform transaction has 3 stages:
71
82
  * - initialization (txOpen message -> txInfo response)
@@ -74,542 +85,593 @@ async function notFoundToUndefined(cb) {
74
85
  *
75
86
  * This class encapsulates finalization stage and provides ready-to-communication transaction object.
76
87
  * */
77
- class PlTransaction {
78
- ll;
79
- name;
80
- writable;
81
- _clientRoot;
82
- finalPredicate;
83
- sharedResourceDataCache;
84
- enableFormattedErrors;
85
- globalTxId;
86
- localTxId = PlTransaction.nextLocalTxId();
87
- /** Used in caching */
88
- txOpenTimestamp = Date.now();
89
- localResourceIdCounter = 0;
90
- /** Store logical tx open / closed state to prevent invalid sequence of requests.
91
- * True means output stream was completed.
92
- * Contract: there must be no async operations between setting this field to true and sending complete signal to stream. */
93
- _completed = false;
94
- /** Void operation futures are placed into this pool, and corresponding method return immediately.
95
- * This is done to save number of round-trips. Next operation producing result will also await those
96
- * pending ops, to throw any pending errors. */
97
- pendingVoidOps = [];
98
- globalTxIdWasAwaited = false;
99
- _startTime = Date.now();
100
- _stat = stat.initialTxStatWithoutTime();
101
- get stat() {
102
- return {
103
- ...this._stat,
104
- timeMs: Date.now() - this._startTime,
105
- };
106
- }
107
- constructor(ll, name, writable, _clientRoot, finalPredicate, sharedResourceDataCache, enableFormattedErrors = false) {
108
- this.ll = ll;
109
- this.name = name;
110
- this.writable = writable;
111
- this._clientRoot = _clientRoot;
112
- this.finalPredicate = finalPredicate;
113
- this.sharedResourceDataCache = sharedResourceDataCache;
114
- this.enableFormattedErrors = enableFormattedErrors;
115
- // initiating transaction
116
- this.globalTxId = this.sendSingleAndParse({
117
- oneofKind: 'txOpen',
118
- txOpen: {
119
- name,
120
- enableFormattedErrors,
121
- writable: writable
122
- ? api.TxAPI_Open_Request_WritableTx.WRITABLE
123
- : api.TxAPI_Open_Request_WritableTx.NOT_WRITABLE,
124
- },
125
- }, (r) => tsHelpers.notEmpty(r.txOpen.tx?.id));
126
- // To avoid floating promise
127
- this.globalTxId.catch((err) => {
128
- if (!this.globalTxIdWasAwaited) {
129
- console.warn(err);
88
+ let PlTransaction = (() => {
89
+ let _instanceExtraInitializers = [];
90
+ let _getResourceData_decorators;
91
+ let _getResourceDataIfExists_decorators;
92
+ let _getFieldIfExists_decorators;
93
+ let _listKeyValues_decorators;
94
+ let _listKeyValuesString_decorators;
95
+ let _listKeyValuesIfResourceExists_decorators;
96
+ let _listKeyValuesStringIfResourceExists_decorators;
97
+ let _getKValue_decorators;
98
+ let _getKValueString_decorators;
99
+ let _getKValueJson_decorators;
100
+ let _getKValueIfExists_decorators;
101
+ let _getKValueStringIfExists_decorators;
102
+ let _getKValueJsonIfExists_decorators;
103
+ return class PlTransaction {
104
+ static {
105
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
106
+ _getResourceData_decorators = [tracked];
107
+ _getResourceDataIfExists_decorators = [tracked];
108
+ _getFieldIfExists_decorators = [tracked];
109
+ _listKeyValues_decorators = [tracked];
110
+ _listKeyValuesString_decorators = [tracked];
111
+ _listKeyValuesIfResourceExists_decorators = [tracked];
112
+ _listKeyValuesStringIfResourceExists_decorators = [tracked];
113
+ _getKValue_decorators = [tracked];
114
+ _getKValueString_decorators = [tracked];
115
+ _getKValueJson_decorators = [tracked];
116
+ _getKValueIfExists_decorators = [tracked];
117
+ _getKValueStringIfExists_decorators = [tracked];
118
+ _getKValueJsonIfExists_decorators = [tracked];
119
+ tslib_es6.__esDecorate(this, null, _getResourceData_decorators, { kind: "method", name: "getResourceData", static: false, private: false, access: { has: obj => "getResourceData" in obj, get: obj => obj.getResourceData }, metadata: _metadata }, null, _instanceExtraInitializers);
120
+ tslib_es6.__esDecorate(this, null, _getResourceDataIfExists_decorators, { kind: "method", name: "getResourceDataIfExists", static: false, private: false, access: { has: obj => "getResourceDataIfExists" in obj, get: obj => obj.getResourceDataIfExists }, metadata: _metadata }, null, _instanceExtraInitializers);
121
+ tslib_es6.__esDecorate(this, null, _getFieldIfExists_decorators, { kind: "method", name: "getFieldIfExists", static: false, private: false, access: { has: obj => "getFieldIfExists" in obj, get: obj => obj.getFieldIfExists }, metadata: _metadata }, null, _instanceExtraInitializers);
122
+ tslib_es6.__esDecorate(this, null, _listKeyValues_decorators, { kind: "method", name: "listKeyValues", static: false, private: false, access: { has: obj => "listKeyValues" in obj, get: obj => obj.listKeyValues }, metadata: _metadata }, null, _instanceExtraInitializers);
123
+ tslib_es6.__esDecorate(this, null, _listKeyValuesString_decorators, { kind: "method", name: "listKeyValuesString", static: false, private: false, access: { has: obj => "listKeyValuesString" in obj, get: obj => obj.listKeyValuesString }, metadata: _metadata }, null, _instanceExtraInitializers);
124
+ tslib_es6.__esDecorate(this, null, _listKeyValuesIfResourceExists_decorators, { kind: "method", name: "listKeyValuesIfResourceExists", static: false, private: false, access: { has: obj => "listKeyValuesIfResourceExists" in obj, get: obj => obj.listKeyValuesIfResourceExists }, metadata: _metadata }, null, _instanceExtraInitializers);
125
+ tslib_es6.__esDecorate(this, null, _listKeyValuesStringIfResourceExists_decorators, { kind: "method", name: "listKeyValuesStringIfResourceExists", static: false, private: false, access: { has: obj => "listKeyValuesStringIfResourceExists" in obj, get: obj => obj.listKeyValuesStringIfResourceExists }, metadata: _metadata }, null, _instanceExtraInitializers);
126
+ tslib_es6.__esDecorate(this, null, _getKValue_decorators, { kind: "method", name: "getKValue", static: false, private: false, access: { has: obj => "getKValue" in obj, get: obj => obj.getKValue }, metadata: _metadata }, null, _instanceExtraInitializers);
127
+ tslib_es6.__esDecorate(this, null, _getKValueString_decorators, { kind: "method", name: "getKValueString", static: false, private: false, access: { has: obj => "getKValueString" in obj, get: obj => obj.getKValueString }, metadata: _metadata }, null, _instanceExtraInitializers);
128
+ tslib_es6.__esDecorate(this, null, _getKValueJson_decorators, { kind: "method", name: "getKValueJson", static: false, private: false, access: { has: obj => "getKValueJson" in obj, get: obj => obj.getKValueJson }, metadata: _metadata }, null, _instanceExtraInitializers);
129
+ tslib_es6.__esDecorate(this, null, _getKValueIfExists_decorators, { kind: "method", name: "getKValueIfExists", static: false, private: false, access: { has: obj => "getKValueIfExists" in obj, get: obj => obj.getKValueIfExists }, metadata: _metadata }, null, _instanceExtraInitializers);
130
+ tslib_es6.__esDecorate(this, null, _getKValueStringIfExists_decorators, { kind: "method", name: "getKValueStringIfExists", static: false, private: false, access: { has: obj => "getKValueStringIfExists" in obj, get: obj => obj.getKValueStringIfExists }, metadata: _metadata }, null, _instanceExtraInitializers);
131
+ tslib_es6.__esDecorate(this, null, _getKValueJsonIfExists_decorators, { kind: "method", name: "getKValueJsonIfExists", static: false, private: false, access: { has: obj => "getKValueJsonIfExists" in obj, get: obj => obj.getKValueJsonIfExists }, metadata: _metadata }, null, _instanceExtraInitializers);
132
+ if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
133
+ }
134
+ ll = tslib_es6.__runInitializers(this, _instanceExtraInitializers);
135
+ name;
136
+ writable;
137
+ _clientRoot;
138
+ finalPredicate;
139
+ sharedResourceDataCache;
140
+ enableFormattedErrors;
141
+ globalTxId;
142
+ localTxId = PlTransaction.nextLocalTxId();
143
+ /** Used in caching */
144
+ txOpenTimestamp = Date.now();
145
+ localResourceIdCounter = 0;
146
+ /** Store logical tx open / closed state to prevent invalid sequence of requests.
147
+ * True means output stream was completed.
148
+ * Contract: there must be no async operations between setting this field to true and sending complete signal to stream. */
149
+ _completed = false;
150
+ globalTxIdWasAwaited = false;
151
+ pending = new PromiseTracker.PromiseTracker();
152
+ _startTime = Date.now();
153
+ _stat = stat.initialTxStatWithoutTime();
154
+ get stat() {
155
+ return {
156
+ ...this._stat,
157
+ timeMs: Date.now() - this._startTime,
158
+ };
159
+ }
160
+ constructor(ll, name, writable, _clientRoot, finalPredicate, sharedResourceDataCache, enableFormattedErrors = false) {
161
+ this.ll = ll;
162
+ this.name = name;
163
+ this.writable = writable;
164
+ this._clientRoot = _clientRoot;
165
+ this.finalPredicate = finalPredicate;
166
+ this.sharedResourceDataCache = sharedResourceDataCache;
167
+ this.enableFormattedErrors = enableFormattedErrors;
168
+ // initiating transaction
169
+ this.globalTxId = this.sendSingleAndParse({
170
+ oneofKind: 'txOpen',
171
+ txOpen: {
172
+ name,
173
+ enableFormattedErrors,
174
+ writable: writable
175
+ ? api.TxAPI_Open_Request_WritableTx.WRITABLE
176
+ : api.TxAPI_Open_Request_WritableTx.NOT_WRITABLE,
177
+ },
178
+ }, (r) => tsHelpers.notEmpty(r.txOpen.tx?.id));
179
+ void this.track(this.globalTxId);
180
+ // To avoid floating promise
181
+ this.globalTxId.catch((err) => {
182
+ if (!this.globalTxIdWasAwaited) {
183
+ console.warn(err);
184
+ }
185
+ });
186
+ // Adding stats
187
+ this._stat.txCount++;
188
+ }
189
+ /**
190
+ * Collect all pending promises for the transaction finalization.
191
+ */
192
+ track(promiseOrCallback) {
193
+ return this.pending.track(promiseOrCallback);
194
+ }
195
+ async drainAndAwaitPendingOps() {
196
+ // awaiting these pending operations first, to catch any errors
197
+ await this.pending.awaitAll();
198
+ }
199
+ sendSingleAndParse(r, parser) {
200
+ return this.pending.track(async () => {
201
+ const rawResponsePromise = this.ll.send(r, false);
202
+ void this.pending.track(rawResponsePromise);
203
+ await this.drainAndAwaitPendingOps();
204
+ // awaiting our result, and parsing the response
205
+ return parser(await rawResponsePromise);
206
+ });
207
+ }
208
+ sendMultiAndParse(r, parser) {
209
+ return this.pending.track(async () => {
210
+ const rawResponsePromise = this.ll.send(r, true);
211
+ void this.pending.track(rawResponsePromise);
212
+ await this.drainAndAwaitPendingOps();
213
+ // awaiting our result, and parsing the response
214
+ return parser(await rawResponsePromise);
215
+ });
216
+ }
217
+ async sendVoidSync(r) {
218
+ await this.ll.send(r, false);
219
+ }
220
+ /** Requests sent with this method should never produce recoverable errors */
221
+ sendVoidAsync(r) {
222
+ void this.track(this.sendVoidSync(r));
223
+ }
224
+ checkTxOpen() {
225
+ if (this._completed)
226
+ throw new Error('Transaction already closed');
227
+ }
228
+ get completed() {
229
+ return this._completed;
230
+ }
231
+ /** Commit & closes transaction. {@link TxCommitConflict} is thrown on
232
+ * commit conflicts. */
233
+ async commit() {
234
+ this.checkTxOpen();
235
+ // tx will accept no requests after this one
236
+ this._completed = true;
237
+ if (!this.writable) {
238
+ // no need to explicitly commit or reject read-only tx
239
+ const completeResult = this.track(this.ll.complete());
240
+ await this.drainAndAwaitPendingOps();
241
+ await completeResult;
242
+ await this.ll.await();
243
+ }
244
+ else {
245
+ const commitResponse = this.track(this.sendSingleAndParse({ oneofKind: 'txCommit', txCommit: {} }, (r) => r.txCommit.success));
246
+ // send closing frame right after commit to save some time on round-trips
247
+ const completeResult = this.track(this.ll.complete());
248
+ // now when we pushed all packets into the stream, we should wait for any
249
+ // pending void operations from before, to catch any errors
250
+ await this.drainAndAwaitPendingOps();
251
+ if (!(await commitResponse))
252
+ throw new TxCommitConflict();
253
+ await completeResult;
254
+ // await event-loop completion
255
+ await this.ll.await();
130
256
  }
131
- });
132
- // Adding stats
133
- this._stat.txCount++;
134
- }
135
- async drainAndAwaitPendingOps() {
136
- if (this.pendingVoidOps.length === 0)
137
- return;
138
- // drain pending operations into temp array
139
- const pending = this.pendingVoidOps;
140
- this.pendingVoidOps = [];
141
- // awaiting these pending operations first, to catch any errors
142
- await Promise.all(pending);
143
- }
144
- async sendSingleAndParse(r, parser) {
145
- // pushing operation packet to server
146
- const rawResponsePromise = this.ll.send(r, false);
147
- await this.drainAndAwaitPendingOps();
148
- // awaiting our result, and parsing the response
149
- return parser(await rawResponsePromise);
150
- }
151
- async sendMultiAndParse(r, parser) {
152
- // pushing operation packet to server
153
- const rawResponsePromise = this.ll.send(r, true);
154
- await this.drainAndAwaitPendingOps();
155
- // awaiting our result, and parsing the response
156
- return parser(await rawResponsePromise);
157
- }
158
- async sendVoidSync(r) {
159
- await this.ll.send(r, false);
160
- }
161
- /** Requests sent with this method should never produce recoverable errors */
162
- sendVoidAsync(r) {
163
- this.pendingVoidOps.push(this.sendVoidSync(r));
164
- }
165
- checkTxOpen() {
166
- if (this._completed)
167
- throw new Error('Transaction already closed');
168
- }
169
- get completed() {
170
- return this._completed;
171
- }
172
- /** Commit & closes transaction. {@link TxCommitConflict} is thrown on
173
- * commit conflicts. */
174
- async commit() {
175
- this.checkTxOpen();
176
- // tx will accept no requests after this one
177
- this._completed = true;
178
- if (!this.writable) {
179
- // no need to explicitly commit or reject read-only tx
180
- const completeResult = this.ll.complete();
181
- await this.drainAndAwaitPendingOps();
182
- await completeResult;
183
- await this.ll.await();
184
257
  }
185
- else {
186
- // @TODO, also floating promises
187
- const commitResponse = this.sendSingleAndParse({ oneofKind: 'txCommit', txCommit: {} }, (r) => r.txCommit.success);
258
+ async discard() {
259
+ this.checkTxOpen();
260
+ // tx will accept no requests after this one
261
+ this._completed = true;
262
+ const discardResponse = this.sendVoidSync({ oneofKind: 'txDiscard', txDiscard: {} });
263
+ void this.track(discardResponse);
188
264
  // send closing frame right after commit to save some time on round-trips
189
- const completeResult = this.ll.complete();
265
+ const completeResult = this.track(this.ll.complete());
190
266
  // now when we pushed all packets into the stream, we should wait for any
191
267
  // pending void operations from before, to catch any errors
192
268
  await this.drainAndAwaitPendingOps();
193
- if (!(await commitResponse))
194
- throw new TxCommitConflict();
269
+ await discardResponse;
195
270
  await completeResult;
196
- // await event-loop completion
197
271
  await this.ll.await();
198
272
  }
199
- }
200
- async discard() {
201
- this.checkTxOpen();
202
- // tx will accept no requests after this one
203
- this._completed = true;
204
- const discardResponse = this.sendVoidSync({ oneofKind: 'txDiscard', txDiscard: {} });
205
- // send closing frame right after commit to save some time on round-trips
206
- const completeResult = this.ll.complete();
207
- // now when we pushed all packets into the stream, we should wait for any
208
- // pending void operations from before, to catch any errors
209
- await this.drainAndAwaitPendingOps();
210
- await discardResponse;
211
- await completeResult;
212
- await this.ll.await();
213
- }
214
- //
215
- // Main tx methods
216
- //
217
- get clientRoot() {
218
- return types.ensureResourceIdNotNull(this._clientRoot);
219
- }
220
- //
221
- // Resources
222
- //
223
- createSingleton(name, type, errorIfExists = false) {
224
- const localId = this.nextLocalResourceId(false);
225
- const globalId = this.sendSingleAndParse({
226
- oneofKind: 'resourceCreateSingleton',
227
- resourceCreateSingleton: {
228
- type,
229
- id: localId,
230
- data: Buffer.from(name),
231
- errorIfExists,
232
- },
233
- }, (r) => r.resourceCreateSingleton.resourceId);
234
- return { globalId, localId };
235
- }
236
- async getSingleton(name, loadFields = true) {
237
- return await this.sendSingleAndParse({
238
- oneofKind: 'resourceGetSingleton',
239
- resourceGetSingleton: {
240
- data: Buffer.from(name),
241
- loadFields,
242
- },
243
- }, (r) => type_conversion.protoToResource(tsHelpers.notEmpty(r.resourceGetSingleton.resource)));
244
- }
245
- createResource(root, req, parser) {
246
- const localId = this.nextLocalResourceId(root);
247
- const globalId = this.sendSingleAndParse(req(localId), (r) => parser(r));
248
- return { globalId, localId };
249
- }
250
- createRoot(type) {
251
- this._stat.rootsCreated++;
252
- return this.createResource(true, (localId) => ({ oneofKind: 'resourceCreateRoot', resourceCreateRoot: { type, id: localId } }), (r) => r.resourceCreateRoot.resourceId);
253
- }
254
- createStruct(type, data) {
255
- this._stat.structsCreated++;
256
- this._stat.structsCreatedDataBytes += data?.length ?? 0;
257
- return this.createResource(false, (localId) => ({
258
- oneofKind: 'resourceCreateStruct',
259
- resourceCreateStruct: {
260
- type,
261
- id: localId,
262
- data: data === undefined ? undefined : typeof data === 'string' ? Buffer.from(data) : data,
263
- },
264
- }), (r) => r.resourceCreateStruct.resourceId);
265
- }
266
- createEphemeral(type, data) {
267
- this._stat.ephemeralsCreated++;
268
- this._stat.ephemeralsCreatedDataBytes += data?.length ?? 0;
269
- return this.createResource(false, (localId) => ({
270
- oneofKind: 'resourceCreateEphemeral',
271
- resourceCreateEphemeral: {
272
- type,
273
- id: localId,
274
- data: data === undefined ? undefined : typeof data === 'string' ? Buffer.from(data) : data,
275
- },
276
- }), (r) => r.resourceCreateEphemeral.resourceId);
277
- }
278
- createValue(type, data, errorIfExists = false) {
279
- this._stat.valuesCreated++;
280
- this._stat.valuesCreatedDataBytes += data?.length ?? 0;
281
- return this.createResource(false, (localId) => ({
282
- oneofKind: 'resourceCreateValue',
283
- resourceCreateValue: {
284
- type,
285
- id: localId,
286
- data: typeof data === 'string' ? Buffer.from(data) : data,
287
- errorIfExists,
288
- },
289
- }), (r) => r.resourceCreateValue.resourceId);
290
- }
291
- createJsonValue(data) {
292
- const jsonData = tsHelpers.canonicalJsonBytes(data);
293
- return this.createValue(pl.JsonObject, jsonData, false);
294
- }
295
- createJsonGzValue(data, minSizeToGzip = 16_384) {
296
- const { data: jsonData, isGzipped } = tsHelpers.canonicalJsonGzBytes(data, minSizeToGzip);
297
- return this.createValue(isGzipped ? pl.JsonGzObject : pl.JsonObject, jsonData, false);
298
- }
299
- createError(message) {
300
- return this.createValue(error_resource.ErrorResourceType, JSON.stringify({ message }));
301
- }
302
- setResourceName(name, rId) {
303
- this.sendVoidAsync({
304
- oneofKind: 'resourceNameSet',
305
- resourceNameSet: { resourceId: toResourceId(rId), name },
306
- });
307
- }
308
- deleteResourceName(name) {
309
- this.sendVoidAsync({ oneofKind: 'resourceNameDelete', resourceNameDelete: { name } });
310
- }
311
- async getResourceByName(name) {
312
- return await this.sendSingleAndParse({ oneofKind: 'resourceNameGet', resourceNameGet: { name } }, (r) => types.ensureResourceIdNotNull(r.resourceNameGet.resourceId));
313
- }
314
- async checkResourceNameExists(name) {
315
- return await this.sendSingleAndParse({ oneofKind: 'resourceNameExists', resourceNameExists: { name } }, (r) => r.resourceNameExists.exists);
316
- }
317
- removeResource(rId) {
318
- this.sendVoidAsync({ oneofKind: 'resourceRemove', resourceRemove: { id: rId } });
319
- }
320
- async resourceExists(rId) {
321
- return await this.sendSingleAndParse({ oneofKind: 'resourceExists', resourceExists: { resourceId: rId } }, (r) => r.resourceExists.exists);
322
- }
323
- async getResourceData(rId, loadFields = true, ignoreCache = false) {
324
- if (!ignoreCache && !isResourceRef(rId) && !types.isLocalResourceId(rId)) {
325
- // checking if we can return result from cache
326
- const fromCache = this.sharedResourceDataCache.get(rId);
327
- if (fromCache && fromCache.cacheTxOpenTimestamp < this.txOpenTimestamp) {
328
- if (!loadFields) {
329
- this._stat.rGetDataCacheHits++;
330
- this._stat.rGetDataCacheBytes += fromCache.basicData.data?.length ?? 0;
331
- return fromCache.basicData;
332
- }
333
- else if (fromCache.data) {
334
- this._stat.rGetDataCacheHits++;
335
- this._stat.rGetDataCacheBytes += fromCache.basicData.data?.length ?? 0;
336
- this._stat.rGetDataCacheFields += fromCache.data.fields.length;
337
- return fromCache.data;
338
- }
339
- }
273
+ //
274
+ // Main tx methods
275
+ //
276
+ get clientRoot() {
277
+ return types.ensureResourceIdNotNull(this._clientRoot);
278
+ }
279
+ //
280
+ // Resources
281
+ //
282
+ createSingleton(name, type, errorIfExists = false) {
283
+ const localId = this.nextLocalResourceId(false);
284
+ const globalId = this.sendSingleAndParse({
285
+ oneofKind: 'resourceCreateSingleton',
286
+ resourceCreateSingleton: {
287
+ type,
288
+ id: localId,
289
+ data: Buffer.from(name),
290
+ errorIfExists,
291
+ },
292
+ }, (r) => r.resourceCreateSingleton.resourceId);
293
+ void this.track(globalId);
294
+ return { globalId, localId };
295
+ }
296
+ getSingleton(name, loadFields = true) {
297
+ return this.sendSingleAndParse({
298
+ oneofKind: 'resourceGetSingleton',
299
+ resourceGetSingleton: {
300
+ data: Buffer.from(name),
301
+ loadFields,
302
+ },
303
+ }, (r) => type_conversion.protoToResource(tsHelpers.notEmpty(r.resourceGetSingleton.resource)));
304
+ }
305
+ createResource(root, req, parser) {
306
+ const localId = this.nextLocalResourceId(root);
307
+ const globalId = this.sendSingleAndParse(req(localId), (r) => parser(r));
308
+ void this.track(globalId);
309
+ return { globalId, localId };
310
+ }
311
+ createRoot(type) {
312
+ this._stat.rootsCreated++;
313
+ return this.createResource(true, (localId) => ({ oneofKind: 'resourceCreateRoot', resourceCreateRoot: { type, id: localId } }), (r) => r.resourceCreateRoot.resourceId);
314
+ }
315
+ createStruct(type, data) {
316
+ this._stat.structsCreated++;
317
+ this._stat.structsCreatedDataBytes += data?.length ?? 0;
318
+ return this.createResource(false, (localId) => ({
319
+ oneofKind: 'resourceCreateStruct',
320
+ resourceCreateStruct: {
321
+ type,
322
+ id: localId,
323
+ data: data === undefined ? undefined : typeof data === 'string' ? Buffer.from(data) : data,
324
+ },
325
+ }), (r) => r.resourceCreateStruct.resourceId);
326
+ }
327
+ createEphemeral(type, data) {
328
+ this._stat.ephemeralsCreated++;
329
+ this._stat.ephemeralsCreatedDataBytes += data?.length ?? 0;
330
+ return this.createResource(false, (localId) => ({
331
+ oneofKind: 'resourceCreateEphemeral',
332
+ resourceCreateEphemeral: {
333
+ type,
334
+ id: localId,
335
+ data: data === undefined ? undefined : typeof data === 'string' ? Buffer.from(data) : data,
336
+ },
337
+ }), (r) => r.resourceCreateEphemeral.resourceId);
338
+ }
339
+ createValue(type, data, errorIfExists = false) {
340
+ this._stat.valuesCreated++;
341
+ this._stat.valuesCreatedDataBytes += data?.length ?? 0;
342
+ return this.createResource(false, (localId) => ({
343
+ oneofKind: 'resourceCreateValue',
344
+ resourceCreateValue: {
345
+ type,
346
+ id: localId,
347
+ data: typeof data === 'string' ? Buffer.from(data) : data,
348
+ errorIfExists,
349
+ },
350
+ }), (r) => r.resourceCreateValue.resourceId);
351
+ }
352
+ createJsonValue(data) {
353
+ const jsonData = tsHelpers.canonicalJsonBytes(data);
354
+ return this.createValue(pl.JsonObject, jsonData, false);
355
+ }
356
+ createJsonGzValue(data, minSizeToGzip = 16_384) {
357
+ const { data: jsonData, isGzipped } = tsHelpers.canonicalJsonGzBytes(data, minSizeToGzip);
358
+ return this.createValue(isGzipped ? pl.JsonGzObject : pl.JsonObject, jsonData, false);
359
+ }
360
+ createError(message) {
361
+ return this.createValue(error_resource.ErrorResourceType, JSON.stringify({ message }));
362
+ }
363
+ setResourceName(name, rId) {
364
+ this.sendVoidAsync({
365
+ oneofKind: 'resourceNameSet',
366
+ resourceNameSet: { resourceId: toResourceId(rId), name },
367
+ });
368
+ }
369
+ deleteResourceName(name) {
370
+ this.sendVoidAsync({ oneofKind: 'resourceNameDelete', resourceNameDelete: { name } });
371
+ }
372
+ getResourceByName(name) {
373
+ return this.sendSingleAndParse({ oneofKind: 'resourceNameGet', resourceNameGet: { name } }, (r) => types.ensureResourceIdNotNull(r.resourceNameGet.resourceId));
374
+ }
375
+ checkResourceNameExists(name) {
376
+ return this.sendSingleAndParse({ oneofKind: 'resourceNameExists', resourceNameExists: { name } }, (r) => r.resourceNameExists.exists);
377
+ }
378
+ removeResource(rId) {
379
+ this.sendVoidAsync({ oneofKind: 'resourceRemove', resourceRemove: { id: rId } });
340
380
  }
341
- const result = await this.sendSingleAndParse({
342
- oneofKind: 'resourceGet',
343
- resourceGet: { resourceId: toResourceId(rId), loadFields: loadFields },
344
- }, (r) => type_conversion.protoToResource(tsHelpers.notEmpty(r.resourceGet.resource)));
345
- this._stat.rGetDataNetRequests++;
346
- this._stat.rGetDataNetBytes += result.data?.length ?? 0;
347
- this._stat.rGetDataNetFields += result.fields.length;
348
- // we will cache only final resource data states
349
- // caching result even if we were ignore the cache
350
- if (!isResourceRef(rId) && !types.isLocalResourceId(rId) && this.finalPredicate(result)) {
351
- tsHelpers.deepFreeze(result);
352
- const fromCache = this.sharedResourceDataCache.get(rId);
353
- if (fromCache) {
354
- if (loadFields && !fromCache.data) {
355
- fromCache.data = result;
356
- // updating timestamp becuse we updated the record
357
- fromCache.cacheTxOpenTimestamp = this.txOpenTimestamp;
381
+ resourceExists(rId) {
382
+ return this.sendSingleAndParse({ oneofKind: 'resourceExists', resourceExists: { resourceId: rId } }, (r) => r.resourceExists.exists);
383
+ }
384
+ async getResourceData(rId, loadFields = true, ignoreCache = false) {
385
+ if (!ignoreCache && !isResourceRef(rId) && !types.isLocalResourceId(rId)) {
386
+ // checking if we can return result from cache
387
+ const fromCache = this.sharedResourceDataCache.get(rId);
388
+ if (fromCache && fromCache.cacheTxOpenTimestamp < this.txOpenTimestamp) {
389
+ if (!loadFields) {
390
+ this._stat.rGetDataCacheHits++;
391
+ this._stat.rGetDataCacheBytes += fromCache.basicData.data?.length ?? 0;
392
+ return fromCache.basicData;
393
+ }
394
+ else if (fromCache.data) {
395
+ this._stat.rGetDataCacheHits++;
396
+ this._stat.rGetDataCacheBytes += fromCache.basicData.data?.length ?? 0;
397
+ this._stat.rGetDataCacheFields += fromCache.data.fields.length;
398
+ return fromCache.data;
399
+ }
358
400
  }
359
401
  }
360
- else {
361
- const basicData = types.extractBasicResourceData(result);
362
- tsHelpers.deepFreeze(basicData);
363
- if (loadFields)
364
- this.sharedResourceDataCache.set(rId, {
365
- basicData,
366
- data: result,
367
- cacheTxOpenTimestamp: this.txOpenTimestamp,
368
- });
369
- else
370
- this.sharedResourceDataCache.set(rId, {
371
- basicData,
372
- data: undefined,
373
- cacheTxOpenTimestamp: this.txOpenTimestamp,
374
- });
402
+ const result = await this.sendSingleAndParse({
403
+ oneofKind: 'resourceGet',
404
+ resourceGet: { resourceId: toResourceId(rId), loadFields: loadFields },
405
+ }, (r) => type_conversion.protoToResource(tsHelpers.notEmpty(r.resourceGet.resource)));
406
+ this._stat.rGetDataNetRequests++;
407
+ this._stat.rGetDataNetBytes += result.data?.length ?? 0;
408
+ this._stat.rGetDataNetFields += result.fields.length;
409
+ // we will cache only final resource data states
410
+ // caching result even if we were ignore the cache
411
+ if (!isResourceRef(rId) && !types.isLocalResourceId(rId) && this.finalPredicate(result)) {
412
+ tsHelpers.deepFreeze(result);
413
+ const fromCache = this.sharedResourceDataCache.get(rId);
414
+ if (fromCache) {
415
+ if (loadFields && !fromCache.data) {
416
+ fromCache.data = result;
417
+ // updating timestamp because we updated the record
418
+ fromCache.cacheTxOpenTimestamp = this.txOpenTimestamp;
419
+ }
420
+ }
421
+ else {
422
+ const basicData = types.extractBasicResourceData(result);
423
+ tsHelpers.deepFreeze(basicData);
424
+ if (loadFields)
425
+ this.sharedResourceDataCache.set(rId, {
426
+ basicData,
427
+ data: result,
428
+ cacheTxOpenTimestamp: this.txOpenTimestamp,
429
+ });
430
+ else
431
+ this.sharedResourceDataCache.set(rId, {
432
+ basicData,
433
+ data: undefined,
434
+ cacheTxOpenTimestamp: this.txOpenTimestamp,
435
+ });
436
+ }
375
437
  }
438
+ return result;
376
439
  }
377
- return result;
378
- }
379
- async getResourceDataIfExists(rId, loadFields = true) {
380
- // calling this mehtod will ignore cache, because user intention is to detect resource absence
381
- // which cache will prevent
382
- const result = await notFoundToUndefined(async () => await this.getResourceData(rId, loadFields, true));
383
- // cleaning cache record if resorce was removed from the db
384
- if (result === undefined && !isResourceRef(rId) && !types.isLocalResourceId(rId))
385
- this.sharedResourceDataCache.delete(rId);
386
- return result;
387
- }
388
- /**
389
- * Inform platform that resource will not get any new input fields.
390
- * This is required, when client creates resource without schema and wants
391
- * controller to start calculations.
392
- * Most controllers will not start calculations even when all inputs
393
- * have their values, if inputs list is not locked.
394
- */
395
- lockInputs(rId) {
396
- this._stat.inputsLocked++;
397
- this.sendVoidAsync({
398
- oneofKind: 'resourceLockInputs',
399
- resourceLockInputs: { resourceId: toResourceId(rId) },
400
- });
401
- }
402
- /**
403
- * Inform platform that resource will not get any new output fields.
404
- * This is required for resource to pass deduplication.
405
- */
406
- lockOutputs(rId) {
407
- this._stat.outputsLocked++;
408
- this.sendVoidAsync({
409
- oneofKind: 'resourceLockOutputs',
410
- resourceLockOutputs: { resourceId: toResourceId(rId) },
411
- });
412
- }
413
- lock(rID) {
414
- this.lockInputs(rID);
415
- this.lockOutputs(rID);
416
- }
417
- setResourceError(rId, ref) {
418
- this.sendVoidAsync({
419
- oneofKind: 'resourceSetError',
420
- resourceSetError: { resourceId: toResourceId(rId), errorResourceId: toResourceId(ref) },
421
- });
422
- }
423
- //
424
- // Fields
425
- //
426
- createField(fId, fieldType, value) {
427
- this._stat.fieldsCreated++;
428
- this.sendVoidAsync({
429
- oneofKind: 'fieldCreate',
430
- fieldCreate: { type: type_conversion.fieldTypeToProto(fieldType), id: toFieldId(fId) },
431
- });
432
- if (value !== undefined)
433
- this.setField(fId, value);
434
- }
435
- async fieldExists(fId) {
436
- return await this.sendSingleAndParse({
437
- oneofKind: 'fieldExists',
438
- fieldExists: { field: toFieldId(fId) },
439
- }, (r) => r.fieldExists.exists);
440
- }
441
- setField(fId, ref) {
442
- this._stat.fieldsSet++;
443
- if (isResource(ref))
440
+ async getResourceDataIfExists(rId, loadFields = true) {
441
+ // calling this method will ignore cache, because user intention is to detect resource absence
442
+ // which cache will prevent
443
+ const result = await notFoundToUndefined(async () => await this.getResourceData(rId, loadFields, true));
444
+ // cleaning cache record if resource was removed from the db
445
+ if (result === undefined && !isResourceRef(rId) && !types.isLocalResourceId(rId))
446
+ this.sharedResourceDataCache.delete(rId);
447
+ return result;
448
+ }
449
+ /**
450
+ * Inform platform that resource will not get any new input fields.
451
+ * This is required, when client creates resource without schema and wants
452
+ * controller to start calculations.
453
+ * Most controllers will not start calculations even when all inputs
454
+ * have their values, if inputs list is not locked.
455
+ */
456
+ lockInputs(rId) {
457
+ this._stat.inputsLocked++;
458
+ this.sendVoidAsync({
459
+ oneofKind: 'resourceLockInputs',
460
+ resourceLockInputs: { resourceId: toResourceId(rId) },
461
+ });
462
+ }
463
+ /**
464
+ * Inform platform that resource will not get any new output fields.
465
+ * This is required for resource to pass deduplication.
466
+ */
467
+ lockOutputs(rId) {
468
+ this._stat.outputsLocked++;
444
469
  this.sendVoidAsync({
445
- oneofKind: 'fieldSet',
446
- fieldSet: {
447
- field: toFieldId(fId),
448
- value: {
449
- resourceId: toResourceId(ref),
450
- fieldName: '', // default value, read as undefined
470
+ oneofKind: 'resourceLockOutputs',
471
+ resourceLockOutputs: { resourceId: toResourceId(rId) },
472
+ });
473
+ }
474
+ lock(rID) {
475
+ this.lockInputs(rID);
476
+ this.lockOutputs(rID);
477
+ }
478
+ setResourceError(rId, ref) {
479
+ this.sendVoidAsync({
480
+ oneofKind: 'resourceSetError',
481
+ resourceSetError: { resourceId: toResourceId(rId), errorResourceId: toResourceId(ref) },
482
+ });
483
+ }
484
+ //
485
+ // Fields
486
+ //
487
+ createField(fId, fieldType, value) {
488
+ this._stat.fieldsCreated++;
489
+ this.sendVoidAsync({
490
+ oneofKind: 'fieldCreate',
491
+ fieldCreate: { type: type_conversion.fieldTypeToProto(fieldType), id: toFieldId(fId) },
492
+ });
493
+ if (value !== undefined)
494
+ this.setField(fId, value);
495
+ }
496
+ fieldExists(fId) {
497
+ return this.sendSingleAndParse({
498
+ oneofKind: 'fieldExists',
499
+ fieldExists: { field: toFieldId(fId) },
500
+ }, (r) => r.fieldExists.exists);
501
+ }
502
+ setField(fId, ref) {
503
+ this._stat.fieldsSet++;
504
+ if (isResource(ref))
505
+ this.sendVoidAsync({
506
+ oneofKind: 'fieldSet',
507
+ fieldSet: {
508
+ field: toFieldId(fId),
509
+ value: {
510
+ resourceId: toResourceId(ref),
511
+ fieldName: '', // default value, read as undefined
512
+ },
451
513
  },
514
+ });
515
+ else
516
+ this.sendVoidAsync({
517
+ oneofKind: 'fieldSet',
518
+ fieldSet: {
519
+ field: toFieldId(fId),
520
+ value: toFieldId(ref),
521
+ },
522
+ });
523
+ }
524
+ setFieldError(fId, ref) {
525
+ this._stat.fieldsSet++;
526
+ this.sendVoidAsync({
527
+ oneofKind: 'fieldSetError',
528
+ fieldSetError: { field: toFieldId(fId), errResourceId: toResourceId(ref) },
529
+ });
530
+ }
531
+ getField(fId) {
532
+ this._stat.fieldsGet++;
533
+ return this.sendSingleAndParse({ oneofKind: 'fieldGet', fieldGet: { field: toFieldId(fId) } }, (r) => type_conversion.protoToField(tsHelpers.notEmpty(r.fieldGet.field)));
534
+ }
535
+ async getFieldIfExists(fId) {
536
+ return notFoundToUndefined(async () => await this.getField(fId));
537
+ }
538
+ resetField(fId) {
539
+ this.sendVoidAsync({ oneofKind: 'fieldReset', fieldReset: { field: toFieldId(fId) } });
540
+ }
541
+ removeField(fId) {
542
+ this.sendVoidAsync({ oneofKind: 'fieldRemove', fieldRemove: { field: toFieldId(fId) } });
543
+ }
544
+ //
545
+ // KV
546
+ //
547
+ async listKeyValues(rId) {
548
+ const result = await this.sendMultiAndParse({
549
+ oneofKind: 'resourceKeyValueList',
550
+ resourceKeyValueList: { resourceId: toResourceId(rId), startFrom: '', limit: 0 },
551
+ }, (r) => r.map((e) => e.resourceKeyValueList.record));
552
+ this._stat.kvListRequests++;
553
+ this._stat.kvListEntries += result.length;
554
+ for (const kv of result)
555
+ this._stat.kvListBytes += kv.key.length + kv.value.length;
556
+ return result;
557
+ }
558
+ async listKeyValuesString(rId) {
559
+ return (await this.listKeyValues(rId)).map(({ key, value }) => ({
560
+ key,
561
+ value: Buffer.from(value).toString(),
562
+ }));
563
+ }
564
+ async listKeyValuesIfResourceExists(rId) {
565
+ return notFoundToUndefined(async () => await this.listKeyValues(rId));
566
+ }
567
+ async listKeyValuesStringIfResourceExists(rId) {
568
+ return notFoundToUndefined(async () => await this.listKeyValuesString(rId));
569
+ }
570
+ setKValue(rId, key, value) {
571
+ this._stat.kvSetRequests++;
572
+ this._stat.kvSetBytes++;
573
+ this.sendVoidAsync({
574
+ oneofKind: 'resourceKeyValueSet',
575
+ resourceKeyValueSet: {
576
+ resourceId: toResourceId(rId),
577
+ key,
578
+ value: util.toBytes(value),
452
579
  },
453
580
  });
454
- else
581
+ }
582
+ deleteKValue(rId, key) {
455
583
  this.sendVoidAsync({
456
- oneofKind: 'fieldSet',
457
- fieldSet: {
458
- field: toFieldId(fId),
459
- value: toFieldId(ref),
584
+ oneofKind: 'resourceKeyValueDelete',
585
+ resourceKeyValueDelete: {
586
+ resourceId: toResourceId(rId),
587
+ key,
460
588
  },
461
589
  });
462
- }
463
- setFieldError(fId, ref) {
464
- this._stat.fieldsSet++;
465
- this.sendVoidAsync({
466
- oneofKind: 'fieldSetError',
467
- fieldSetError: { field: toFieldId(fId), errResourceId: toResourceId(ref) },
468
- });
469
- }
470
- async getField(fId) {
471
- this._stat.fieldsGet++;
472
- return await this.sendSingleAndParse({ oneofKind: 'fieldGet', fieldGet: { field: toFieldId(fId) } }, (r) => type_conversion.protoToField(tsHelpers.notEmpty(r.fieldGet.field)));
473
- }
474
- async getFieldIfExists(fId) {
475
- return notFoundToUndefined(async () => await this.getField(fId));
476
- }
477
- resetField(fId) {
478
- this.sendVoidAsync({ oneofKind: 'fieldReset', fieldReset: { field: toFieldId(fId) } });
479
- }
480
- removeField(fId) {
481
- this.sendVoidAsync({ oneofKind: 'fieldRemove', fieldRemove: { field: toFieldId(fId) } });
482
- }
483
- //
484
- // KV
485
- //
486
- async listKeyValues(rId) {
487
- const result = await this.sendMultiAndParse({
488
- oneofKind: 'resourceKeyValueList',
489
- resourceKeyValueList: { resourceId: toResourceId(rId), startFrom: '', limit: 0 },
490
- }, (r) => r.map((e) => e.resourceKeyValueList.record));
491
- this._stat.kvListRequests++;
492
- this._stat.kvListEntries += result.length;
493
- for (const kv of result)
494
- this._stat.kvListBytes += kv.key.length + kv.value.length;
495
- return result;
496
- }
497
- async listKeyValuesString(rId) {
498
- return (await this.listKeyValues(rId)).map(({ key, value }) => ({
499
- key,
500
- value: Buffer.from(value).toString(),
501
- }));
502
- }
503
- async listKeyValuesIfResourceExists(rId) {
504
- return notFoundToUndefined(async () => await this.listKeyValues(rId));
505
- }
506
- async listKeyValuesStringIfResourceExists(rId) {
507
- return notFoundToUndefined(async () => await this.listKeyValuesString(rId));
508
- }
509
- setKValue(rId, key, value) {
510
- this._stat.kvSetRequests++;
511
- this._stat.kvSetBytes++;
512
- this.sendVoidAsync({
513
- oneofKind: 'resourceKeyValueSet',
514
- resourceKeyValueSet: {
515
- resourceId: toResourceId(rId),
516
- key,
517
- value: util.toBytes(value),
518
- },
519
- });
520
- }
521
- deleteKValue(rId, key) {
522
- this.sendVoidAsync({
523
- oneofKind: 'resourceKeyValueDelete',
524
- resourceKeyValueDelete: {
525
- resourceId: toResourceId(rId),
526
- key,
527
- },
528
- });
529
- }
530
- async getKValue(rId, key) {
531
- const result = await this.sendSingleAndParse({
532
- oneofKind: 'resourceKeyValueGet',
533
- resourceKeyValueGet: { resourceId: toResourceId(rId), key },
534
- }, (r) => r.resourceKeyValueGet.value);
535
- this._stat.kvGetRequests++;
536
- this._stat.kvGetBytes += result.length;
537
- return result;
538
- }
539
- async getKValueString(rId, key) {
540
- return Buffer.from(await this.getKValue(rId, key)).toString();
541
- }
542
- async getKValueJson(rId, key) {
543
- return JSON.parse(await this.getKValueString(rId, key));
544
- }
545
- async getKValueIfExists(rId, key) {
546
- const result = await this.sendSingleAndParse({
547
- oneofKind: 'resourceKeyValueGetIfExists',
548
- resourceKeyValueGetIfExists: { resourceId: toResourceId(rId), key },
549
- }, (r) => r.resourceKeyValueGetIfExists.exists ? r.resourceKeyValueGetIfExists.value : undefined);
550
- this._stat.kvGetRequests++;
551
- this._stat.kvGetBytes += result?.length ?? 0;
552
- return result;
553
- }
554
- async getKValueStringIfExists(rId, key) {
555
- const data = await this.getKValueIfExists(rId, key);
556
- return data === undefined ? undefined : Buffer.from(data).toString();
557
- }
558
- async getKValueJsonIfExists(rId, key) {
559
- const str = await this.getKValueString(rId, key);
560
- if (str === undefined)
561
- return undefined;
562
- return JSON.parse(str);
563
- }
564
- //
565
- // Cache
566
- //
567
- // TODO
568
- //
569
- // High level ops
570
- //
571
- /** Resolves existing or create first level resource from */
572
- getFutureFieldValue(rId, fieldName, fieldType) {
573
- const data = Buffer.from(JSON.stringify({ fieldName, fieldType }));
574
- const getFieldResource = this.createEphemeral({ name: 'json/getField', version: '1' }, data);
575
- this.setField({ resourceId: getFieldResource, fieldName: 'resource' }, rId);
576
- return { resourceId: getFieldResource, fieldName: 'result' };
577
- }
578
- //
579
- // Technical
580
- //
581
- async getGlobalTxId() {
582
- this.globalTxIdWasAwaited = true;
583
- return await this.globalTxId;
584
- }
585
- /** Closes output event stream */
586
- async complete() {
587
- if (this._completed)
588
- return;
589
- this._completed = true;
590
- const completeResult = this.ll.complete();
591
- await this.drainAndAwaitPendingOps();
592
- await completeResult;
593
- }
594
- /** Await incoming message loop termination and throw
595
- * any leftover errors if it was unsuccessful */
596
- async await() {
597
- await this.ll.await();
598
- }
599
- //
600
- // Helpers
601
- //
602
- nextLocalResourceId(root) {
603
- return types.createLocalResourceId(root, ++this.localResourceIdCounter, this.localTxId);
604
- }
605
- static localTxIdCounter = 0;
606
- static nextLocalTxId() {
607
- PlTransaction.localTxIdCounter++;
608
- if (PlTransaction.localTxIdCounter === types.MaxTxId)
609
- PlTransaction.localTxIdCounter = 1;
610
- return PlTransaction.localTxIdCounter;
611
- }
612
- }
590
+ }
591
+ async getKValue(rId, key) {
592
+ const result = await this.sendSingleAndParse({
593
+ oneofKind: 'resourceKeyValueGet',
594
+ resourceKeyValueGet: { resourceId: toResourceId(rId), key },
595
+ }, (r) => r.resourceKeyValueGet.value);
596
+ this._stat.kvGetRequests++;
597
+ this._stat.kvGetBytes += result.length;
598
+ return result;
599
+ }
600
+ async getKValueString(rId, key) {
601
+ return Buffer.from(await this.getKValue(rId, key)).toString();
602
+ }
603
+ async getKValueJson(rId, key) {
604
+ return JSON.parse(await this.getKValueString(rId, key));
605
+ }
606
+ async getKValueIfExists(rId, key) {
607
+ const result = await this.sendSingleAndParse({
608
+ oneofKind: 'resourceKeyValueGetIfExists',
609
+ resourceKeyValueGetIfExists: { resourceId: toResourceId(rId), key },
610
+ }, (r) => r.resourceKeyValueGetIfExists.exists ? r.resourceKeyValueGetIfExists.value : undefined);
611
+ this._stat.kvGetRequests++;
612
+ this._stat.kvGetBytes += result?.length ?? 0;
613
+ return result;
614
+ }
615
+ async getKValueStringIfExists(rId, key) {
616
+ const data = await this.getKValueIfExists(rId, key);
617
+ return data === undefined ? undefined : Buffer.from(data).toString();
618
+ }
619
+ async getKValueJsonIfExists(rId, key) {
620
+ const str = await this.getKValueString(rId, key);
621
+ if (str === undefined)
622
+ return undefined;
623
+ return JSON.parse(str);
624
+ }
625
+ //
626
+ // Cache
627
+ //
628
+ // TODO
629
+ //
630
+ // High level ops
631
+ //
632
+ /** Resolves existing or create first level resource from */
633
+ getFutureFieldValue(rId, fieldName, fieldType) {
634
+ const data = Buffer.from(JSON.stringify({ fieldName, fieldType }));
635
+ const getFieldResource = this.createEphemeral({ name: 'json/getField', version: '1' }, data);
636
+ this.setField({ resourceId: getFieldResource, fieldName: 'resource' }, rId);
637
+ return { resourceId: getFieldResource, fieldName: 'result' };
638
+ }
639
+ //
640
+ // Technical
641
+ //
642
+ async getGlobalTxId() {
643
+ this.globalTxIdWasAwaited = true;
644
+ return await this.globalTxId;
645
+ }
646
+ /** Closes output event stream */
647
+ async complete() {
648
+ if (this._completed)
649
+ return;
650
+ this._completed = true;
651
+ const completeResult = this.track(this.ll.complete());
652
+ await this.drainAndAwaitPendingOps();
653
+ await completeResult;
654
+ }
655
+ /** Await incoming message loop termination and throw
656
+ * any leftover errors if it was unsuccessful */
657
+ async await() {
658
+ await this.ll.await();
659
+ }
660
+ //
661
+ // Helpers
662
+ //
663
+ nextLocalResourceId(root) {
664
+ return types.createLocalResourceId(root, ++this.localResourceIdCounter, this.localTxId);
665
+ }
666
+ static localTxIdCounter = 0;
667
+ static nextLocalTxId() {
668
+ PlTransaction.localTxIdCounter++;
669
+ if (PlTransaction.localTxIdCounter === types.MaxTxId)
670
+ PlTransaction.localTxIdCounter = 1;
671
+ return PlTransaction.localTxIdCounter;
672
+ }
673
+ };
674
+ })();
613
675
 
614
676
  exports.PlTransaction = PlTransaction;
615
677
  exports.TxCommitConflict = TxCommitConflict;