postchain-client 2.1.0 → 2.1.1

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 (31) hide show
  1. package/built/cjs/index.js +86 -39
  2. package/built/cjs/index.js.map +1 -1
  3. package/built/esm/index.js +86 -39
  4. package/built/esm/index.js.map +1 -1
  5. package/built/src/blockchainClient/clientStub.js +2 -0
  6. package/built/src/blockchainClient/clientStub.js.map +1 -1
  7. package/built/src/blockchainClient/failoverStrategies.d.ts +6 -5
  8. package/built/src/blockchainClient/failoverStrategies.js +13 -8
  9. package/built/src/blockchainClient/failoverStrategies.js.map +1 -1
  10. package/built/src/blockchainClient/httpUtil.d.ts +1 -1
  11. package/built/src/blockchainClient/httpUtil.js +45 -21
  12. package/built/src/blockchainClient/httpUtil.js.map +1 -1
  13. package/built/src/blockchainClient/requestWithFailoverStrategy.d.ts +1 -1
  14. package/built/src/blockchainClient/requestWithFailoverStrategy.js +6 -6
  15. package/built/src/blockchainClient/requestWithFailoverStrategy.js.map +1 -1
  16. package/built/src/blockchainClient/types.d.ts +6 -0
  17. package/built/src/blockchainClient/utils.d.ts +6 -2
  18. package/built/src/blockchainClient/utils.js +20 -4
  19. package/built/src/blockchainClient/utils.js.map +1 -1
  20. package/built/test/integration/createClientIntegration.test.js +4 -0
  21. package/built/test/integration/createClientIntegration.test.js.map +1 -1
  22. package/built/test/unit/failoverStrategies.timeout.test.d.ts +1 -0
  23. package/built/test/unit/failoverStrategies.timeout.test.js +228 -0
  24. package/built/test/unit/failoverStrategies.timeout.test.js.map +1 -0
  25. package/built/test/unit/httpUtil.timeout.test.d.ts +1 -0
  26. package/built/test/unit/httpUtil.timeout.test.js +114 -0
  27. package/built/test/unit/httpUtil.timeout.test.js.map +1 -0
  28. package/built/umd/index.js +86 -39
  29. package/built/umd/index.js.map +1 -1
  30. package/changelog.md +19 -1
  31. package/package.json +1 -1
@@ -39051,13 +39051,22 @@
39051
39051
  step((generator = generator.apply(thisArg, _arguments || [])).next());
39052
39052
  });
39053
39053
  };
39054
- function handleRequest(method, path, endpoint, postObject) {
39054
+ function createTimeoutController(timeout) {
39055
+ if (!timeout)
39056
+ return { controller: undefined, timeoutId: undefined };
39057
+ const controller = new AbortController();
39058
+ const timeoutError = new Error(`Request timeout after ${timeout}ms`);
39059
+ timeoutError.name = 'TimeoutError';
39060
+ const timeoutId = setTimeout(() => controller.abort(timeoutError), timeout);
39061
+ return { controller, timeoutId };
39062
+ }
39063
+ function handleRequest(method, path, endpoint, timeout, postObject) {
39055
39064
  return __awaiter$8(this, void 0, void 0, function* () {
39056
39065
  if (method == Method.GET) {
39057
- return yield get(path, endpoint);
39066
+ return yield get(path, endpoint, timeout);
39058
39067
  }
39059
39068
  else {
39060
- return yield post(path, endpoint, postObject);
39069
+ return yield post(path, endpoint, timeout, postObject);
39061
39070
  }
39062
39071
  });
39063
39072
  }
@@ -39065,12 +39074,18 @@
39065
39074
  * Sends request to get data from a given API endpoint.
39066
39075
  * @param path API endpoint of Rell backend
39067
39076
  * @param endpoint
39077
+ * @param timeout
39068
39078
  */
39069
- function get(path, endpoint) {
39079
+ function get(path, endpoint, timeout) {
39070
39080
  return __awaiter$8(this, void 0, void 0, function* () {
39071
39081
  debug(`GET URL ${new URL(path, endpoint).href}`);
39072
39082
  try {
39073
- const response = yield fetch(new URL(path, endpoint).href);
39083
+ const { controller, timeoutId } = createTimeoutController(timeout);
39084
+ const response = yield fetch(new URL(path, endpoint).href, {
39085
+ signal: controller === null || controller === void 0 ? void 0 : controller.signal,
39086
+ });
39087
+ if (timeoutId)
39088
+ clearTimeout(timeoutId);
39074
39089
  const contentType = response.headers.get("Content-Type");
39075
39090
  const transactionTimestamp = response.headers.get("X-Transaction-Timestamp");
39076
39091
  let rspBody = null;
@@ -39092,18 +39107,35 @@
39092
39107
  }
39093
39108
  });
39094
39109
  }
39110
+ function constructBufferResponseBody(response) {
39111
+ return __awaiter$8(this, void 0, void 0, function* () {
39112
+ const contentType = response.headers.get("content-type");
39113
+ if (contentType === "application/octet-stream") {
39114
+ const responseBuffer = yield response.arrayBuffer();
39115
+ const buffer = require$$0$2.Buffer.from(responseBuffer);
39116
+ return decodeValue(buffer);
39117
+ }
39118
+ if (contentType === "application/json") {
39119
+ return yield response.json();
39120
+ }
39121
+ const responseText = yield response.text();
39122
+ return responseText ? responseText : response.statusText;
39123
+ });
39124
+ }
39095
39125
  /**
39096
39126
  * Sends request to post data to a given API endpoint.
39097
39127
  * @param path API endpoint of Rell backend
39098
39128
  * @param endpoint
39129
+ * @param timeout
39099
39130
  * @param requestBody request body
39100
39131
  */
39101
- function post(path, endpoint, requestBody) {
39132
+ function post(path, endpoint, timeout, requestBody) {
39102
39133
  return __awaiter$8(this, void 0, void 0, function* () {
39103
39134
  debug(`POST URL ${new URL(path, endpoint).href}`);
39104
39135
  debug(`POST body ${JSON.stringify(requestBody)}`);
39105
39136
  if (require$$0$2.Buffer.isBuffer(requestBody)) {
39106
39137
  try {
39138
+ const { controller, timeoutId } = createTimeoutController(timeout);
39107
39139
  const requestOptions = {
39108
39140
  method: "post",
39109
39141
  body: new Uint8Array(requestBody),
@@ -39111,8 +39143,11 @@
39111
39143
  Accept: "application/octet-stream",
39112
39144
  "Content-Type": "application/octet-stream",
39113
39145
  },
39146
+ signal: controller === null || controller === void 0 ? void 0 : controller.signal,
39114
39147
  };
39115
39148
  const response = yield fetch(new URL(path, endpoint).href, requestOptions);
39149
+ if (timeoutId)
39150
+ clearTimeout(timeoutId);
39116
39151
  const transactionTimestamp = response.headers.get("X-Transaction-Timestamp");
39117
39152
  return createResponseObject(null, response.status, yield constructBufferResponseBody(response), transactionTimestamp);
39118
39153
  }
@@ -39122,6 +39157,7 @@
39122
39157
  }
39123
39158
  else {
39124
39159
  try {
39160
+ const { controller, timeoutId } = createTimeoutController(timeout);
39125
39161
  const response = yield fetch(new URL(path, endpoint).href, {
39126
39162
  method: "post",
39127
39163
  body: JSON.stringify(requestBody),
@@ -39129,7 +39165,10 @@
39129
39165
  Accept: "application/json",
39130
39166
  "Content-Type": "application/json",
39131
39167
  },
39168
+ signal: controller === null || controller === void 0 ? void 0 : controller.signal,
39132
39169
  });
39170
+ if (timeoutId)
39171
+ clearTimeout(timeoutId);
39133
39172
  const transactionTimestamp = response.headers.get("X-Transaction-Timestamp");
39134
39173
  return createResponseObject(null, response.status, yield constructBufferResponseBody(response), transactionTimestamp);
39135
39174
  }
@@ -39139,21 +39178,6 @@
39139
39178
  }
39140
39179
  });
39141
39180
  }
39142
- function constructBufferResponseBody(response) {
39143
- return __awaiter$8(this, void 0, void 0, function* () {
39144
- const contentType = response.headers.get("content-type");
39145
- if (contentType === "application/octet-stream") {
39146
- const responseBuffer = yield response.arrayBuffer();
39147
- const buffer = require$$0$2.Buffer.from(responseBuffer);
39148
- return decodeValue(buffer);
39149
- }
39150
- if (contentType === "application/json") {
39151
- return yield response.json();
39152
- }
39153
- const responseText = yield response.text();
39154
- return responseText ? responseText : response.statusText;
39155
- });
39156
- }
39157
39181
  function createResponseObject(error, statusCode, rspBody, transactionTimestamp) {
39158
39182
  let finalStatusCode = statusCode;
39159
39183
  // Handle browser network errors as a specific status code for smart timeout logic
@@ -39239,24 +39263,26 @@
39239
39263
  }
39240
39264
  }
39241
39265
  function abortOnError(_a) {
39242
- return __awaiter$7(this, arguments, void 0, function* ({ method, path, config, postObject, }) {
39266
+ return __awaiter$7(this, arguments, void 0, function* ({ method, path, config, postObject, timeoutOverride, }) {
39243
39267
  return yield retryRequest({
39244
39268
  method,
39245
39269
  path,
39246
39270
  config,
39247
39271
  postObject,
39272
+ timeoutOverride,
39248
39273
  useNextNodeOnError: false,
39249
39274
  recoveryThreshold: config.recoveryThreshold, // Use user config or let nodeManager use default
39250
39275
  });
39251
39276
  });
39252
39277
  }
39253
39278
  function tryNextOnError(_a) {
39254
- return __awaiter$7(this, arguments, void 0, function* ({ method, path, config, postObject, }) {
39279
+ return __awaiter$7(this, arguments, void 0, function* ({ method, path, config, postObject, timeoutOverride, }) {
39255
39280
  return yield retryRequest({
39256
39281
  method,
39257
39282
  path,
39258
39283
  config,
39259
39284
  postObject,
39285
+ timeoutOverride,
39260
39286
  useNextNodeOnError: true,
39261
39287
  recoveryThreshold: config.recoveryThreshold,
39262
39288
  });
@@ -39269,7 +39295,7 @@
39269
39295
  return endpointPoolLength - (endpointPoolLength - 1) / 3;
39270
39296
  }
39271
39297
  function queryMajority(_a) {
39272
- return __awaiter$7(this, arguments, void 0, function* ({ method, path, config, postObject, }) {
39298
+ return __awaiter$7(this, arguments, void 0, function* ({ method, path, config, postObject, timeoutOverride, }) {
39273
39299
  var _b;
39274
39300
  // Set up constants
39275
39301
  const bftMajorityThreshold = calculateBftMajorityThreshold(config.endpointPool.length);
@@ -39280,7 +39306,8 @@
39280
39306
  const outcomes = [];
39281
39307
  const promises = availableNodes.map((node) => __awaiter$7(this, void 0, void 0, function* () {
39282
39308
  try {
39283
- const response = yield handleRequest(method, path, node.url, postObject);
39309
+ const requestTimeout = timeoutOverride !== null && timeoutOverride !== void 0 ? timeoutOverride : config.responseTimeout;
39310
+ const response = yield handleRequest(method, path, node.url, requestTimeout, postObject);
39284
39311
  const { statusCode } = response;
39285
39312
  if (statusCode && isSuccessfulStatusCode(statusCode)) {
39286
39313
  outcomes.push({ type: "SUCCESS", result: response });
@@ -39340,7 +39367,7 @@
39340
39367
  });
39341
39368
  }
39342
39369
  function singleEndpoint(_a) {
39343
- return __awaiter$7(this, arguments, void 0, function* ({ method, path, config, postObject, }) {
39370
+ return __awaiter$7(this, arguments, void 0, function* ({ method, path, config, postObject, timeoutOverride, }) {
39344
39371
  let statusCode = null;
39345
39372
  let rspBody = null;
39346
39373
  let error = null;
@@ -39350,7 +39377,8 @@
39350
39377
  throw new Error("No nodes available after recovery attempt. Check node configuration.");
39351
39378
  }
39352
39379
  for (let attempt = 0; attempt < config.attemptsPerEndpoint; attempt++) {
39353
- const response = yield handleRequest(method, path, endpoint.url, postObject);
39380
+ const requestTimeout = timeoutOverride !== null && timeoutOverride !== void 0 ? timeoutOverride : config.responseTimeout;
39381
+ const response = yield handleRequest(method, path, endpoint.url, requestTimeout, postObject);
39354
39382
  if (response) {
39355
39383
  ({ error, statusCode, rspBody, transactionTimestamp } = response);
39356
39384
  }
@@ -39367,7 +39395,7 @@
39367
39395
  });
39368
39396
  }
39369
39397
  function retryRequest(_a) {
39370
- return __awaiter$7(this, arguments, void 0, function* ({ method, path, config, postObject, useNextNodeOnError, recoveryThreshold, }) {
39398
+ return __awaiter$7(this, arguments, void 0, function* ({ method, path, config, postObject, useNextNodeOnError, recoveryThreshold, timeoutOverride, }) {
39371
39399
  var _b, _c, _d;
39372
39400
  let statusCode = null;
39373
39401
  let rspBody = null;
@@ -39377,7 +39405,8 @@
39377
39405
  const availableNodes = nodeManager.getAvailableNodes(recoveryThreshold);
39378
39406
  for (const node of availableNodes) {
39379
39407
  for (let attempt = 0; attempt < config.attemptsPerEndpoint; attempt++) {
39380
- const response = yield handleRequest(method, path, node.url, postObject);
39408
+ const requestTimeout = timeoutOverride !== null && timeoutOverride !== void 0 ? timeoutOverride : config.responseTimeout;
39409
+ const response = yield handleRequest(method, path, node.url, requestTimeout, postObject);
39381
39410
  error = (_b = response === null || response === void 0 ? void 0 : response.error) !== null && _b !== void 0 ? _b : null;
39382
39411
  statusCode = (_c = response === null || response === void 0 ? void 0 : response.statusCode) !== null && _c !== void 0 ? _c : null;
39383
39412
  rspBody = (_d = response === null || response === void 0 ? void 0 : response.rspBody) !== null && _d !== void 0 ? _d : null;
@@ -39501,19 +39530,19 @@
39501
39530
  });
39502
39531
  };
39503
39532
  function requestWithFailoverStrategy(method_1, path_1, config_1, postObject_1) {
39504
- return __awaiter$6(this, arguments, void 0, function* (method, path, config, postObject, forceSingleEndpoint = false) {
39533
+ return __awaiter$6(this, arguments, void 0, function* (method, path, config, postObject, forceSingleEndpoint = false, timeoutOverride) {
39505
39534
  switch (config.failoverStrategy) {
39506
39535
  case exports.FailoverStrategy.AbortOnError:
39507
- return yield abortOnError({ method, path, config, postObject });
39536
+ return yield abortOnError({ method, path, config, postObject, timeoutOverride });
39508
39537
  case exports.FailoverStrategy.TryNextOnError:
39509
- return yield tryNextOnError({ method, path, config, postObject });
39538
+ return yield tryNextOnError({ method, path, config, postObject, timeoutOverride });
39510
39539
  case exports.FailoverStrategy.SingleEndpoint:
39511
- return yield singleEndpoint({ method, path, config, postObject });
39540
+ return yield singleEndpoint({ method, path, config, postObject, timeoutOverride });
39512
39541
  case exports.FailoverStrategy.QueryMajority:
39513
39542
  if (forceSingleEndpoint) {
39514
- return yield singleEndpoint({ method, path, config, postObject });
39543
+ return yield singleEndpoint({ method, path, config, postObject, timeoutOverride });
39515
39544
  }
39516
- return yield queryMajority({ method, path, config, postObject });
39545
+ return yield queryMajority({ method, path, config, postObject, timeoutOverride });
39517
39546
  default:
39518
39547
  throw new Error(`Unsupported failover strategy: ${config.failoverStrategy}`);
39519
39548
  }
@@ -39554,6 +39583,8 @@
39554
39583
  endpointPool,
39555
39584
  chainId: settings.blockchainIid,
39556
39585
  merkleHashVersion: (_a = settings.merkleHashVersion) !== null && _a !== void 0 ? _a : MERKLE_HASH_VERSIONS.UNSET,
39586
+ connectTimeout: settings.connectTimeout,
39587
+ responseTimeout: settings.responseTimeout,
39557
39588
  });
39558
39589
  }
39559
39590
  throw new MissingBlockchainIdentifierError();
@@ -39569,6 +39600,8 @@
39569
39600
  endpointPool,
39570
39601
  chainId: directoryChainIid,
39571
39602
  merkleHashVersion: (_a = settings.merkleHashVersion) !== null && _a !== void 0 ? _a : MERKLE_HASH_VERSIONS.UNSET,
39603
+ connectTimeout: settings.connectTimeout,
39604
+ responseTimeout: settings.responseTimeout,
39572
39605
  });
39573
39606
  }))();
39574
39607
  return {
@@ -39585,13 +39618,16 @@
39585
39618
  attemptInterval: ((_h = settings.failOverConfig) === null || _h === void 0 ? void 0 : _h.attemptInterval) || defaultFailoverConfig.attemptInterval,
39586
39619
  unreachableDuration: ((_j = settings.failOverConfig) === null || _j === void 0 ? void 0 : _j.unreachableDuration) || defaultFailoverConfig.unreachableDuration,
39587
39620
  directoryChainRid: settings.directoryChainRid || directoryChainRid,
39621
+ connectTimeout: settings.connectTimeout,
39622
+ responseTimeout: settings.responseTimeout,
39588
39623
  };
39589
39624
  });
39590
39625
  }
39591
39626
  function getMerkleHashVersionFromDapp(config) {
39592
39627
  return __awaiter$5(this, void 0, void 0, function* () {
39628
+ var _a;
39593
39629
  try {
39594
- const response = yield requestWithFailoverStrategy(Method.GET, `/config/${config.blockchainRid}/features`, config);
39630
+ const response = yield requestWithFailoverStrategy(Method.GET, `/config/${config.blockchainRid}/features`, config, undefined, false, (_a = config.connectTimeout) !== null && _a !== void 0 ? _a : config.responseTimeout);
39595
39631
  const validatedResponse = validateMerkleHash(response.rspBody);
39596
39632
  return validatedResponse.merkle_hash_version;
39597
39633
  }
@@ -39602,7 +39638,7 @@
39602
39638
  });
39603
39639
  }
39604
39640
  function nodeDiscovery(_a) {
39605
- return __awaiter$5(this, arguments, void 0, function* ({ nodeManager, directoryEndpointPool, failOverConfig, blockchainRid, blockchainIid, }) {
39641
+ return __awaiter$5(this, arguments, void 0, function* ({ nodeManager, directoryEndpointPool, failOverConfig, blockchainRid, blockchainIid, connectTimeout, responseTimeout, }) {
39606
39642
  if (directoryEndpointPool.length === 0) {
39607
39643
  throw new DirectoryNodeUrlPoolException();
39608
39644
  }
@@ -39615,6 +39651,8 @@
39615
39651
  endpointPool: directoryEndpointPool,
39616
39652
  chainId: directoryIid,
39617
39653
  failOverConfig,
39654
+ connectTimeout,
39655
+ responseTimeout,
39618
39656
  });
39619
39657
  const blockchainRidToUse = yield (() => __awaiter$5(this, void 0, void 0, function* () {
39620
39658
  if (blockchainRid) {
@@ -39626,6 +39664,8 @@
39626
39664
  endpointPool: directoryEndpointPool,
39627
39665
  chainId: blockchainIid,
39628
39666
  failOverConfig,
39667
+ connectTimeout,
39668
+ responseTimeout,
39629
39669
  });
39630
39670
  }
39631
39671
  throw new MissingBlockchainIdentifierError();
@@ -39783,6 +39823,8 @@
39783
39823
  failOverConfig: settings.failOverConfig,
39784
39824
  blockchainRid: settings.blockchainRid,
39785
39825
  blockchainIid: settings.blockchainIid,
39826
+ connectTimeout: settings.connectTimeout,
39827
+ responseTimeout: settings.responseTimeout,
39786
39828
  });
39787
39829
  return discoveredNodes;
39788
39830
  }
@@ -40042,7 +40084,8 @@
40042
40084
  }
40043
40085
  }
40044
40086
  function getBlockchainRidFromIid(_a) {
40045
- return __awaiter$5(this, arguments, void 0, function* ({ endpointPool, chainId, failOverConfig = {}, nodeManager, }) {
40087
+ return __awaiter$5(this, arguments, void 0, function* ({ endpointPool, chainId, failOverConfig = {}, nodeManager, connectTimeout, responseTimeout, }) {
40088
+ var _b;
40046
40089
  const mergedFailOverConfig = Object.assign(Object.assign({}, defaultFailoverConfig), failOverConfig);
40047
40090
  const config = {
40048
40091
  endpointPool,
@@ -40056,8 +40099,10 @@
40056
40099
  attemptInterval: mergedFailOverConfig.attemptInterval,
40057
40100
  unreachableDuration: mergedFailOverConfig.unreachableDuration,
40058
40101
  merkleHashVersion: MERKLE_HASH_VERSIONS.UNSET,
40102
+ connectTimeout,
40103
+ responseTimeout,
40059
40104
  };
40060
- const { error, statusCode, rspBody } = yield requestWithFailoverStrategy(Method.GET, `/brid/iid_${chainId}`, config);
40105
+ const { error, statusCode, rspBody } = yield requestWithFailoverStrategy(Method.GET, `/brid/iid_${chainId}`, config, undefined, false, (_b = config.connectTimeout) !== null && _b !== void 0 ? _b : config.responseTimeout);
40061
40106
  const blockchainRid = isString(rspBody);
40062
40107
  if (!blockchainRid) {
40063
40108
  throw error;
@@ -41272,6 +41317,8 @@
41272
41317
  attemptInterval: 5000,
41273
41318
  unreachableDuration: 30000,
41274
41319
  directoryChainRid: "1111111111111111111111111111111111111111111111111111111111111111",
41320
+ connectTimeout: 1000,
41321
+ responseTimeout: 1000,
41275
41322
  },
41276
41323
  query() {
41277
41324
  return __awaiter(this, void 0, void 0, function* () {