@ethersphere/bee-js 12.2.2 → 12.3.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 (138) hide show
  1. package/dist/cjs/bee.js +56 -23
  2. package/dist/cjs/chunk/cac.js +4 -2
  3. package/dist/cjs/feed/index.js +3 -1
  4. package/dist/cjs/modules/bytes.js +6 -4
  5. package/dist/cjs/modules/bzz.js +5 -4
  6. package/dist/cjs/modules/chunk.js +3 -2
  7. package/dist/cjs/modules/debug/balance.js +5 -29
  8. package/dist/cjs/modules/debug/chequebook.js +9 -58
  9. package/dist/cjs/modules/debug/connectivity.js +5 -82
  10. package/dist/cjs/modules/debug/settlements.js +3 -22
  11. package/dist/cjs/modules/debug/stake.js +8 -33
  12. package/dist/cjs/modules/debug/stamps.js +27 -53
  13. package/dist/cjs/modules/debug/states.js +6 -31
  14. package/dist/cjs/modules/debug/status.js +7 -39
  15. package/dist/cjs/modules/debug/transactions.js +5 -26
  16. package/dist/cjs/modules/envelope.js +6 -6
  17. package/dist/cjs/modules/feed.js +3 -4
  18. package/dist/cjs/modules/grantee.js +8 -10
  19. package/dist/cjs/modules/pinning.js +5 -11
  20. package/dist/cjs/modules/rchash.js +2 -3
  21. package/dist/cjs/modules/soc.js +3 -2
  22. package/dist/cjs/modules/status.js +3 -3
  23. package/dist/cjs/modules/stewardship.js +2 -3
  24. package/dist/cjs/modules/tag.js +4 -35
  25. package/dist/cjs/stamper/stamper.js +2 -1
  26. package/dist/cjs/types/schema/balance.js +14 -0
  27. package/dist/cjs/types/schema/chequebook.js +49 -0
  28. package/dist/cjs/types/schema/connectivity.js +46 -0
  29. package/dist/cjs/types/schema/envelope.js +11 -0
  30. package/dist/cjs/types/schema/grantee.js +10 -0
  31. package/dist/cjs/types/schema/pinning.js +7 -0
  32. package/dist/cjs/types/schema/rchash.js +7 -0
  33. package/dist/cjs/types/schema/settlements.js +17 -0
  34. package/dist/cjs/types/schema/stake.js +33 -0
  35. package/dist/cjs/types/schema/stamps.js +48 -0
  36. package/dist/cjs/types/schema/states.js +34 -0
  37. package/dist/cjs/types/schema/status.js +39 -0
  38. package/dist/cjs/types/schema/stewardship.js +7 -0
  39. package/dist/cjs/types/schema/tag.js +20 -0
  40. package/dist/cjs/types/schema/transactions.js +24 -0
  41. package/dist/cjs/types/schema/upload.js +8 -0
  42. package/dist/cjs/utils/bad-ports.js +92 -0
  43. package/dist/cjs/utils/bytes.js +5 -3
  44. package/dist/cjs/utils/error.js +1 -0
  45. package/dist/cjs/utils/headers.js +19 -13
  46. package/dist/cjs/utils/hex-schema.js +8 -0
  47. package/dist/cjs/utils/http.js +92 -52
  48. package/dist/cjs/utils/resource-locator.js +4 -3
  49. package/dist/cjs/utils/schema.js +88 -0
  50. package/dist/cjs/utils/stamps.js +2 -1
  51. package/dist/cjs/utils/type.js +12 -130
  52. package/dist/cjs/utils/url.js +24 -0
  53. package/dist/index.browser.min.js +1 -1
  54. package/dist/index.browser.min.js.map +1 -1
  55. package/dist/mjs/bee.js +59 -30
  56. package/dist/mjs/chunk/cac.js +5 -3
  57. package/dist/mjs/feed/index.js +4 -2
  58. package/dist/mjs/modules/bytes.js +8 -8
  59. package/dist/mjs/modules/bzz.js +6 -9
  60. package/dist/mjs/modules/chunk.js +4 -7
  61. package/dist/mjs/modules/debug/balance.js +5 -61
  62. package/dist/mjs/modules/debug/chequebook.js +9 -114
  63. package/dist/mjs/modules/debug/connectivity.js +5 -145
  64. package/dist/mjs/modules/debug/settlements.js +3 -46
  65. package/dist/mjs/modules/debug/stake.js +8 -85
  66. package/dist/mjs/modules/debug/stamps.js +24 -126
  67. package/dist/mjs/modules/debug/states.js +6 -71
  68. package/dist/mjs/modules/debug/status.js +7 -93
  69. package/dist/mjs/modules/debug/transactions.js +5 -60
  70. package/dist/mjs/modules/envelope.js +6 -20
  71. package/dist/mjs/modules/feed.js +6 -7
  72. package/dist/mjs/modules/grantee.js +8 -26
  73. package/dist/mjs/modules/pinning.js +7 -21
  74. package/dist/mjs/modules/rchash.js +2 -7
  75. package/dist/mjs/modules/soc.js +4 -5
  76. package/dist/mjs/modules/status.js +4 -4
  77. package/dist/mjs/modules/stewardship.js +2 -7
  78. package/dist/mjs/modules/tag.js +4 -93
  79. package/dist/mjs/stamper/stamper.js +2 -1
  80. package/dist/mjs/types/schema/balance.js +13 -0
  81. package/dist/mjs/types/schema/chequebook.js +53 -0
  82. package/dist/mjs/types/schema/connectivity.js +41 -0
  83. package/dist/mjs/types/schema/envelope.js +8 -0
  84. package/dist/mjs/types/schema/grantee.js +7 -0
  85. package/dist/mjs/types/schema/pinning.js +4 -0
  86. package/dist/mjs/types/schema/rchash.js +4 -0
  87. package/dist/mjs/types/schema/settlements.js +22 -0
  88. package/dist/mjs/types/schema/stake.js +40 -0
  89. package/dist/mjs/types/schema/stamps.js +47 -0
  90. package/dist/mjs/types/schema/states.js +34 -0
  91. package/dist/mjs/types/schema/status.js +36 -0
  92. package/dist/mjs/types/schema/stewardship.js +4 -0
  93. package/dist/mjs/types/schema/tag.js +14 -0
  94. package/dist/mjs/types/schema/transactions.js +25 -0
  95. package/dist/mjs/types/schema/upload.js +5 -0
  96. package/dist/mjs/utils/bad-ports.js +6 -0
  97. package/dist/mjs/utils/bytes.js +6 -8
  98. package/dist/mjs/utils/error.js +5 -0
  99. package/dist/mjs/utils/headers.js +19 -13
  100. package/dist/mjs/utils/hex-schema.js +2 -0
  101. package/dist/mjs/utils/http.js +97 -59
  102. package/dist/mjs/utils/resource-locator.js +4 -3
  103. package/dist/mjs/utils/schema.js +81 -0
  104. package/dist/mjs/utils/stamps.js +2 -1
  105. package/dist/mjs/utils/type.js +13 -233
  106. package/dist/mjs/utils/url.js +23 -0
  107. package/dist/types/bee.d.ts +23 -0
  108. package/dist/types/modules/debug/connectivity.d.ts +2 -2
  109. package/dist/types/modules/debug/stamps.d.ts +2 -0
  110. package/dist/types/modules/debug/status.d.ts +2 -2
  111. package/dist/types/types/debug.d.ts +1 -0
  112. package/dist/types/types/schema/balance.d.ts +12 -0
  113. package/dist/types/types/schema/chequebook.d.ts +104 -0
  114. package/dist/types/types/schema/connectivity.d.ts +49 -0
  115. package/dist/types/types/schema/envelope.d.ts +7 -0
  116. package/dist/types/types/schema/grantee.d.ts +7 -0
  117. package/dist/types/types/schema/pinning.d.ts +4 -0
  118. package/dist/types/types/schema/rchash.d.ts +4 -0
  119. package/dist/types/types/schema/settlements.d.ts +16 -0
  120. package/dist/types/types/schema/stake.d.ts +29 -0
  121. package/dist/types/types/schema/stamps.d.ts +77 -0
  122. package/dist/types/types/schema/states.d.ts +26 -0
  123. package/dist/types/types/schema/status.d.ts +38 -0
  124. package/dist/types/types/schema/stewardship.d.ts +4 -0
  125. package/dist/types/types/schema/tag.d.ts +23 -0
  126. package/dist/types/types/schema/transactions.d.ts +29 -0
  127. package/dist/types/types/schema/upload.d.ts +5 -0
  128. package/dist/types/utils/bad-ports.d.ts +6 -0
  129. package/dist/types/utils/error.d.ts +5 -0
  130. package/dist/types/utils/hex-schema.d.ts +2 -0
  131. package/dist/types/utils/http.d.ts +18 -3
  132. package/dist/types/utils/schema.d.ts +90 -0
  133. package/dist/types/utils/stamps.d.ts +1 -0
  134. package/dist/types/utils/tar-uploader.browser.d.ts +1 -1
  135. package/dist/types/utils/tar-uploader.d.ts +1 -1
  136. package/dist/types/utils/type.d.ts +1 -16
  137. package/dist/types/utils/url.d.ts +9 -0
  138. package/package.json +11 -11
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IsRetrievableResponse = void 0;
4
+ const zod_1 = require("zod");
5
+ exports.IsRetrievableResponse = zod_1.z.object({
6
+ isRetrievable: zod_1.z.boolean(),
7
+ });
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GetAllTagsResponse = exports.TagSchema = void 0;
4
+ const zod_1 = require("zod");
5
+ exports.TagSchema = zod_1.z.object({
6
+ address: zod_1.z
7
+ .string()
8
+ .nullish()
9
+ .transform(v => v ?? ''),
10
+ seen: zod_1.z.number(),
11
+ sent: zod_1.z.number(),
12
+ split: zod_1.z.number(),
13
+ startedAt: zod_1.z.string(),
14
+ stored: zod_1.z.number(),
15
+ synced: zod_1.z.number(),
16
+ uid: zod_1.z.number(),
17
+ });
18
+ exports.GetAllTagsResponse = zod_1.z.object({
19
+ tags: zod_1.z.array(exports.TagSchema),
20
+ });
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TransactionHashResponse = exports.GetTransactionResponse = exports.GetAllTransactionsResponse = void 0;
4
+ const zod_1 = require("zod");
5
+ const type_1 = require("../../utils/type");
6
+ const typed_bytes_1 = require("../../utils/typed-bytes");
7
+ const TransactionInfoSchema = zod_1.z.object({
8
+ transactionHash: zod_1.z.string().transform(s => new typed_bytes_1.TransactionId(s)),
9
+ to: zod_1.z.string(),
10
+ nonce: zod_1.z.number(),
11
+ gasPrice: zod_1.z.string().transform(s => (0, type_1.asNumberString)(s, { name: 'gasPrice' })),
12
+ gasLimit: zod_1.z.number(),
13
+ data: zod_1.z.string(),
14
+ created: zod_1.z.string(),
15
+ description: zod_1.z.string(),
16
+ value: zod_1.z.string().transform(s => (0, type_1.asNumberString)(s, { name: 'value' })),
17
+ });
18
+ exports.GetAllTransactionsResponse = zod_1.z.object({
19
+ pendingTransactions: zod_1.z.array(TransactionInfoSchema),
20
+ });
21
+ exports.GetTransactionResponse = TransactionInfoSchema;
22
+ exports.TransactionHashResponse = zod_1.z.object({
23
+ transactionHash: zod_1.z.string().transform(s => new typed_bytes_1.TransactionId(s)),
24
+ });
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UploadResultBody = void 0;
4
+ const zod_1 = require("zod");
5
+ const typed_bytes_1 = require("../../utils/typed-bytes");
6
+ exports.UploadResultBody = zod_1.z.object({
7
+ reference: zod_1.z.string().transform(s => new typed_bytes_1.Reference(s)),
8
+ });
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BAD_PORTS = void 0;
4
+ /**
5
+ * Ports blocked by the WHATWG Fetch spec for HTTP/HTTPS requests.
6
+ *
7
+ * See: https://fetch.spec.whatwg.org/#port-blocking
8
+ */
9
+ exports.BAD_PORTS = [
10
+ '1',
11
+ '7',
12
+ '9',
13
+ '11',
14
+ '13',
15
+ '15',
16
+ '17',
17
+ '19',
18
+ '20',
19
+ '21',
20
+ '22',
21
+ '23',
22
+ '25',
23
+ '37',
24
+ '42',
25
+ '43',
26
+ '53',
27
+ '69',
28
+ '77',
29
+ '79',
30
+ '87',
31
+ '95',
32
+ '101',
33
+ '102',
34
+ '103',
35
+ '104',
36
+ '109',
37
+ '110',
38
+ '111',
39
+ '113',
40
+ '115',
41
+ '117',
42
+ '119',
43
+ '123',
44
+ '135',
45
+ '137',
46
+ '139',
47
+ '143',
48
+ '161',
49
+ '179',
50
+ '389',
51
+ '427',
52
+ '465',
53
+ '512',
54
+ '513',
55
+ '514',
56
+ '515',
57
+ '526',
58
+ '530',
59
+ '531',
60
+ '532',
61
+ '540',
62
+ '548',
63
+ '554',
64
+ '556',
65
+ '563',
66
+ '587',
67
+ '601',
68
+ '636',
69
+ '989',
70
+ '990',
71
+ '993',
72
+ '995',
73
+ '1719',
74
+ '1720',
75
+ '1723',
76
+ '2049',
77
+ '3659',
78
+ '4045',
79
+ '4190',
80
+ '5060',
81
+ '5061',
82
+ '6000',
83
+ '6566',
84
+ '6665',
85
+ '6666',
86
+ '6667',
87
+ '6668',
88
+ '6669',
89
+ '6679',
90
+ '6697',
91
+ '10080',
92
+ ];
@@ -7,6 +7,8 @@ exports.Bytes = void 0;
7
7
  exports.parseSizeToBytes = parseSizeToBytes;
8
8
  const cafe_utility_1 = require("cafe-utility");
9
9
  const debug_1 = __importDefault(require("debug"));
10
+ const zod_1 = require("zod");
11
+ const hex_schema_1 = require("./hex-schema");
10
12
  const debug = (0, debug_1.default)('bee-js:bytes');
11
13
  const DECODER = new TextDecoder();
12
14
  const ENCODER = new TextEncoder();
@@ -19,7 +21,7 @@ class Bytes {
19
21
  this.bytes = bytes.bytes;
20
22
  }
21
23
  else if (typeof bytes === 'string') {
22
- this.bytes = cafe_utility_1.Binary.hexToUint8Array(cafe_utility_1.Types.asHexString(bytes, { name: 'Bytes#constructor(bytes)' }));
24
+ this.bytes = cafe_utility_1.Binary.hexToUint8Array(hex_schema_1.HexStringSchema.parse(bytes));
23
25
  }
24
26
  else if (bytes instanceof ArrayBuffer) {
25
27
  this.bytes = new Uint8Array(bytes);
@@ -30,9 +32,9 @@ class Bytes {
30
32
  else {
31
33
  const unknownInput = bytes;
32
34
  const toHex = cafe_utility_1.Objects.getDeep(unknownInput, 'toHex');
33
- if (cafe_utility_1.Types.isFunction(toHex)) {
35
+ if (zod_1.z.function().safeParse(toHex).success) {
34
36
  const hex = toHex.call(unknownInput);
35
- this.bytes = cafe_utility_1.Binary.hexToUint8Array(cafe_utility_1.Types.asHexString(hex, { name: 'Bytes#constructor(bytes)' }));
37
+ this.bytes = cafe_utility_1.Binary.hexToUint8Array(hex_schema_1.HexStringSchema.parse(hex));
36
38
  }
37
39
  else {
38
40
  debug('bytes', bytes);
@@ -22,6 +22,7 @@ class BeeResponseError extends BeeError {
22
22
  this.responseBody = responseBody;
23
23
  this.status = status;
24
24
  this.statusText = statusText;
25
+ this.response = { status, data: responseBody, statusText };
25
26
  }
26
27
  }
27
28
  exports.BeeResponseError = BeeResponseError;
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.readFileHeaders = readFileHeaders;
4
4
  exports.prepareRequestHeaders = prepareRequestHeaders;
5
- const cafe_utility_1 = require("cafe-utility");
5
+ const zod_1 = require("zod");
6
6
  const error_1 = require("./error");
7
7
  const stamps_1 = require("./stamps");
8
8
  const typed_bytes_1 = require("./typed-bytes");
@@ -46,7 +46,7 @@ function prepareRequestHeaders(stamp, nullableOptions) {
46
46
  if (!nullableOptions) {
47
47
  return headers;
48
48
  }
49
- const options = cafe_utility_1.Types.asObject(nullableOptions);
49
+ const options = nullableOptions;
50
50
  if (options.size) {
51
51
  headers['content-length'] = String(options.size);
52
52
  }
@@ -56,26 +56,31 @@ function prepareRequestHeaders(stamp, nullableOptions) {
56
56
  if (options.redundancyLevel) {
57
57
  headers['swarm-redundancy-level'] = String(options.redundancyLevel);
58
58
  }
59
- if (cafe_utility_1.Types.isBoolean(options.act)) {
60
- headers['swarm-act'] = String(options.act);
59
+ const act = zod_1.z.boolean().safeParse(options.act);
60
+ if (act.success) {
61
+ headers['swarm-act'] = String(act.data);
61
62
  }
62
- if (cafe_utility_1.Types.isBoolean(options.pin)) {
63
- headers['swarm-pin'] = String(options.pin);
63
+ const pin = zod_1.z.boolean().safeParse(options.pin);
64
+ if (pin.success) {
65
+ headers['swarm-pin'] = String(pin.data);
64
66
  }
65
- if (cafe_utility_1.Types.isBoolean(options.encrypt)) {
66
- headers['swarm-encrypt'] = options.encrypt.toString();
67
+ const encrypt = zod_1.z.boolean().safeParse(options.encrypt);
68
+ if (encrypt.success) {
69
+ headers['swarm-encrypt'] = String(encrypt.data);
67
70
  }
68
71
  if (options.tag) {
69
72
  headers['swarm-tag'] = String(options.tag);
70
73
  }
71
- if (cafe_utility_1.Types.isBoolean(options.deferred)) {
72
- headers['swarm-deferred-upload'] = options.deferred.toString();
74
+ const deferred = zod_1.z.boolean().safeParse(options.deferred);
75
+ if (deferred.success) {
76
+ headers['swarm-deferred-upload'] = String(deferred.data);
73
77
  }
74
78
  if (options.redundancyStrategy) {
75
79
  headers['swarm-redundancy-strategy'] = String(options.redundancyStrategy);
76
80
  }
77
- if (cafe_utility_1.Types.isBoolean(options.fallback)) {
78
- headers['swarm-redundancy-fallback-mode'] = options.fallback.toString();
81
+ const fallback = zod_1.z.boolean().safeParse(options.fallback);
82
+ if (fallback.success) {
83
+ headers['swarm-redundancy-fallback-mode'] = String(fallback.data);
79
84
  }
80
85
  if (options.timeoutMs) {
81
86
  headers['swarm-chunk-retrieval-timeout'] = String(options.timeoutMs);
@@ -107,7 +112,8 @@ function prepareRequestHeaders(stamp, nullableOptions) {
107
112
  return headers;
108
113
  }
109
114
  function isEnvelopeWithBatchId(value) {
110
- if (!cafe_utility_1.Types.isObject(value)) {
115
+ const result = zod_1.z.object({}).safeParse(value);
116
+ if (!result.success) {
111
117
  return false;
112
118
  }
113
119
  const envelope = value;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HexStringSchema = void 0;
4
+ const zod_1 = require("zod");
5
+ exports.HexStringSchema = zod_1.z
6
+ .string()
7
+ .regex(/^(?:0x)?[0-9a-fA-F]*$/i, 'expected hex string')
8
+ .refine(s => s.replace(/^0x/i, '').length % 2 === 0, 'expected even-length hex string');
@@ -5,29 +5,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.DEFAULT_HTTP_CONFIG = void 0;
7
7
  exports.http = http;
8
- const axios_1 = __importDefault(require("axios"));
8
+ exports.toBeeResponse = toBeeResponse;
9
9
  const cafe_utility_1 = require("cafe-utility");
10
10
  const debug_1 = __importDefault(require("debug"));
11
11
  const index_1 = require("../index");
12
12
  const debug = (0, debug_1.default)('bee-js:http');
13
- const { AxiosError } = axios_1.default;
14
13
  const MAX_FAILED_ATTEMPTS = 100000;
15
14
  const DELAY_FAST = 200;
16
15
  const DELAY_SLOW = 1000;
17
- const DELAY_THRESHOLD = cafe_utility_1.Dates.minutes(1) / DELAY_FAST;
18
- const ABORT_ERROR_MESSAGE = 'Request aborted';
16
+ const FAST_RETRY_COUNT = 300;
19
17
  exports.DEFAULT_HTTP_CONFIG = {
20
18
  headers: {
21
19
  accept: 'application/json, text/plain, */*',
22
20
  },
23
- maxBodyLength: Infinity,
24
- maxContentLength: Infinity,
25
21
  };
26
- function throwIfAborted(signal, config, responseData, responseStatus) {
27
- if (signal?.aborted) {
28
- throw new index_1.BeeResponseError(config.method || 'get', config.url || '<unknown>', ABORT_ERROR_MESSAGE, responseData, responseStatus, 'ERR_CANCELED');
29
- }
30
- }
31
22
  /**
32
23
  * Main function to make HTTP requests.
33
24
  * @param options User defined settings
@@ -37,65 +28,114 @@ async function http(options, config) {
37
28
  const requestConfig = cafe_utility_1.Objects.deepMerge3(exports.DEFAULT_HTTP_CONFIG, config, options);
38
29
  if (options.signal) {
39
30
  requestConfig.signal = options.signal;
40
- throwIfAborted(options.signal, config);
41
31
  }
42
- maybeReplaceBodyBuffers(requestConfig);
32
+ attachBody(requestConfig);
43
33
  if (requestConfig.params) {
44
- const keys = Object.keys(requestConfig.params);
45
- for (const key of keys) {
46
- const value = requestConfig.params[key];
47
- if (value === undefined) {
48
- delete requestConfig.params[key];
49
- }
34
+ for (const k of Object.keys(requestConfig.params)) {
35
+ if (requestConfig.params[k] === undefined)
36
+ delete requestConfig.params[k];
50
37
  }
51
38
  }
39
+ const url = buildUrl(requestConfig);
40
+ const method = (requestConfig.method || 'GET').toUpperCase();
41
+ requestConfig.method = method;
42
+ options.onRequest?.({
43
+ method,
44
+ url: cafe_utility_1.Strings.joinUrl([requestConfig.baseURL ?? '', requestConfig.url ?? '']),
45
+ headers: { ...requestConfig.headers },
46
+ params: requestConfig.params,
47
+ });
52
48
  let failedAttempts = 0;
53
49
  while (failedAttempts < MAX_FAILED_ATTEMPTS) {
54
- throwIfAborted(options.signal, config);
55
50
  try {
56
- debug(`${requestConfig.method || 'get'} ${cafe_utility_1.Strings.joinUrl([
57
- requestConfig.baseURL,
58
- requestConfig.url,
59
- ])}`, { headers: { ...requestConfig.headers }, params: requestConfig.params });
60
- maybeRunOnRequestHook(options, requestConfig);
61
- const response = await (0, axios_1.default)(requestConfig);
62
- return response;
51
+ debug(`${method} ${url}`, { headers: requestConfig.headers, params: requestConfig.params });
52
+ const res = await fetch(url, requestConfig);
53
+ if (!res.ok)
54
+ await throwHttpError(method, url, res, requestConfig.responseType);
55
+ return toBeeResponse(res, requestConfig.responseType);
63
56
  }
64
57
  catch (e) {
65
- if (e instanceof AxiosError) {
66
- if (e.code === 'ERR_CANCELED') {
67
- throwIfAborted({ aborted: true }, config, e.response?.data, e.response?.status);
68
- }
69
- if (e.code === 'ECONNABORTED' && options.endlesslyRetry) {
70
- failedAttempts++;
71
- await cafe_utility_1.System.sleepMillis(failedAttempts < DELAY_THRESHOLD ? DELAY_FAST : DELAY_SLOW);
72
- }
73
- else {
74
- throw new index_1.BeeResponseError(config.method || 'get', config.url || '<unknown>', e.message, e.response?.data, e.response?.status, e.response?.statusText);
75
- }
58
+ if (e instanceof index_1.BeeResponseError)
59
+ throw e;
60
+ const err = e;
61
+ if (err.name === 'TimeoutError' && options.endlesslyRetry) {
62
+ failedAttempts++;
63
+ await cafe_utility_1.System.sleepMillis(failedAttempts < FAST_RETRY_COUNT ? DELAY_FAST : DELAY_SLOW);
76
64
  }
77
65
  else {
78
- throw e;
66
+ throw toBeeError(err, method, url);
79
67
  }
80
68
  }
81
69
  }
82
70
  throw Error('Max number of failed attempts reached');
83
71
  }
84
- function maybeRunOnRequestHook(options, requestConfig) {
85
- if (options.onRequest) {
86
- options.onRequest({
87
- method: requestConfig.method || 'GET',
88
- url: cafe_utility_1.Strings.joinUrl([requestConfig.baseURL, requestConfig.url]),
89
- headers: { ...requestConfig.headers },
90
- params: requestConfig.params,
91
- });
72
+ function attachBody(config) {
73
+ if (config.data === undefined)
74
+ return;
75
+ const data = config.data;
76
+ const isJsonShape = (data !== null && typeof data === 'object' && data.constructor === Object) || Array.isArray(data);
77
+ if (isJsonShape) {
78
+ config.body = JSON.stringify(data);
79
+ config.headers = { 'content-type': 'application/json', ...config.headers };
80
+ }
81
+ else {
82
+ config.body = data;
83
+ config.duplex = 'half';
84
+ const ct = data instanceof Blob && data.type ? data.type : 'application/octet-stream';
85
+ config.headers = { 'content-type': ct, ...config.headers };
92
86
  }
93
87
  }
94
- function maybeReplaceBodyBuffers(config) {
95
- if (config.data && config.data instanceof Uint8Array) {
96
- config.data = config.data.buffer.slice(config.data.byteOffset, config.data.byteOffset + config.data.byteLength);
88
+ async function throwHttpError(method, url, res, responseType) {
89
+ const errBody = await toBeeResponse(res, responseType).catch(() => ({ data: undefined }));
90
+ const bodyMsg = typeof errBody.data === 'string' ? errBody.data : JSON.stringify(errBody.data);
91
+ const message = bodyMsg && bodyMsg !== 'undefined' ? `${res.statusText}: ${bodyMsg}` : res.statusText;
92
+ throw new index_1.BeeResponseError(method, url, message, errBody.data, res.status, res.statusText);
93
+ }
94
+ function toBeeError(err, method, url) {
95
+ if (err.name === 'AbortError') {
96
+ return new index_1.BeeResponseError(method, url, 'Request aborted', undefined, undefined, 'ERR_CANCELED');
97
97
  }
98
- if (config.data && typeof Buffer !== 'undefined' && Buffer.isBuffer(config.data)) {
99
- config.data = config.data.buffer.slice(config.data.byteOffset, config.data.byteOffset + config.data.byteLength);
98
+ const cause = err.cause;
99
+ const message = cause?.message ? `${err.message}: ${cause.message}` : err.message;
100
+ return new index_1.BeeResponseError(method, url, message);
101
+ }
102
+ async function toBeeResponse(res, responseType = 'json') {
103
+ let data;
104
+ switch (responseType) {
105
+ case 'arraybuffer':
106
+ data = await res.arrayBuffer();
107
+ break;
108
+ case 'text':
109
+ data = await res.text();
110
+ break;
111
+ case 'blob':
112
+ data = await res.blob();
113
+ break;
114
+ case 'stream':
115
+ data = res.body;
116
+ break;
117
+ case 'json':
118
+ default: {
119
+ const text = await res.text();
120
+ data = text ? JSON.parse(text) : null;
121
+ }
122
+ }
123
+ return {
124
+ data: data,
125
+ status: res.status,
126
+ statusText: res.statusText,
127
+ headers: Object.fromEntries(res.headers.entries()),
128
+ raw: res,
129
+ };
130
+ }
131
+ function buildUrl(config) {
132
+ let url = cafe_utility_1.Strings.joinUrl([config.baseURL ?? '', config.url ?? '']);
133
+ if (config.params) {
134
+ const qs = new URLSearchParams(Object.entries(config.params)
135
+ .filter(([, v]) => v !== undefined)
136
+ .map(([k, v]) => [k, String(v)])).toString();
137
+ if (qs)
138
+ url += `?${qs}`;
100
139
  }
140
+ return url;
101
141
  }
@@ -1,15 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ResourceLocator = void 0;
4
- const cafe_utility_1 = require("cafe-utility");
4
+ const zod_1 = require("zod");
5
5
  const typed_bytes_1 = require("./typed-bytes");
6
6
  class ResourceLocator {
7
7
  constructor(raw) {
8
8
  this.raw = raw;
9
9
  }
10
10
  toString() {
11
- if (cafe_utility_1.Types.isString(this.raw) && this.raw.includes('.eth')) {
12
- return this.raw;
11
+ const asString = zod_1.z.string().safeParse(this.raw);
12
+ if (asString.success && asString.data.includes('.eth')) {
13
+ return asString.data;
13
14
  }
14
15
  return new typed_bytes_1.Reference(this.raw).toHex();
15
16
  }
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AllTagsOptionsSchema = exports.TransactionOptionsSchema = exports.PostageBatchOptionsSchema = exports.GsocMessageHandlerSchema = exports.PssMessageHandlerSchema = exports.TagUidSchema = exports.TagInputSchema = exports.CollectionUploadOptionsSchema = exports.FileUploadOptionsSchema = exports.RedundantUploadOptionsSchema = exports.UploadOptionsSchema = exports.DownloadOptionsSchema = exports.BeeRequestOptionsSchema = exports.HexStringSchema = void 0;
4
+ const zod_1 = require("zod");
5
+ const types_1 = require("../types");
6
+ const typed_bytes_1 = require("./typed-bytes");
7
+ var hex_schema_1 = require("./hex-schema");
8
+ Object.defineProperty(exports, "HexStringSchema", { enumerable: true, get: function () { return hex_schema_1.HexStringSchema; } });
9
+ const fnField = zod_1.z.any().refine(v => zod_1.z.function().safeParse(v).success, 'expected a function');
10
+ const integerStringField = zod_1.z.any().transform((v) => {
11
+ if (zod_1.z.bigint().safeParse(v).success)
12
+ v = String(v);
13
+ return zod_1.z
14
+ .string()
15
+ .regex(/^-?\d+$/, 'expected integer string')
16
+ .parse(v);
17
+ });
18
+ const nonNegativeIntegerStringField = zod_1.z.any().transform((v) => {
19
+ if (zod_1.z.bigint().safeParse(v).success)
20
+ v = String(v);
21
+ return zod_1.z.string().regex(/^\d+$/, 'expected non-negative integer string').parse(v);
22
+ });
23
+ exports.BeeRequestOptionsSchema = zod_1.z.object({
24
+ baseURL: zod_1.z.string().optional(),
25
+ timeout: zod_1.z.number().int().min(0).optional(),
26
+ headers: zod_1.z.record(zod_1.z.string(), zod_1.z.string()).optional(),
27
+ onRequest: fnField.optional(),
28
+ httpAgent: zod_1.z.unknown().optional(),
29
+ httpsAgent: zod_1.z.unknown().optional(),
30
+ endlesslyRetry: zod_1.z.boolean().optional(),
31
+ signal: zod_1.z.any().optional(),
32
+ });
33
+ exports.DownloadOptionsSchema = zod_1.z.object({
34
+ redundancyStrategy: zod_1.z.number().int().optional(),
35
+ fallback: zod_1.z.boolean().optional(),
36
+ timeoutMs: zod_1.z.number().int().min(0).optional(),
37
+ actPublisher: zod_1.z.optional(zod_1.z.any().transform((v) => new typed_bytes_1.PublicKey(v))),
38
+ actHistoryAddress: zod_1.z.optional(zod_1.z.any().transform((v) => new typed_bytes_1.Reference(v))),
39
+ actTimestamp: zod_1.z.coerce.number().optional(),
40
+ });
41
+ exports.UploadOptionsSchema = zod_1.z.object({
42
+ act: zod_1.z.boolean().optional(),
43
+ actHistoryAddress: zod_1.z.optional(zod_1.z.any().transform((v) => new typed_bytes_1.Reference(v))),
44
+ deferred: zod_1.z.boolean().optional(),
45
+ encrypt: zod_1.z.boolean().optional(),
46
+ pin: zod_1.z.boolean().optional(),
47
+ tag: zod_1.z.number().int().min(0).optional(),
48
+ });
49
+ exports.RedundantUploadOptionsSchema = exports.UploadOptionsSchema.extend({
50
+ redundancyLevel: zod_1.z.number().int().min(0).optional(),
51
+ });
52
+ exports.FileUploadOptionsSchema = exports.UploadOptionsSchema.extend({
53
+ size: zod_1.z.number().int().min(0).optional(),
54
+ contentType: zod_1.z.string().optional(),
55
+ redundancyLevel: zod_1.z.number().int().min(0).optional(),
56
+ });
57
+ exports.CollectionUploadOptionsSchema = exports.UploadOptionsSchema.extend({
58
+ errorDocument: zod_1.z.string().optional(),
59
+ indexDocument: zod_1.z.string().optional(),
60
+ redundancyLevel: zod_1.z.number().int().min(0).optional(),
61
+ });
62
+ exports.TagInputSchema = zod_1.z.object({ uid: zod_1.z.number().int() });
63
+ exports.TagUidSchema = zod_1.z.coerce.number().min(0);
64
+ exports.PssMessageHandlerSchema = zod_1.z.object({
65
+ onMessage: fnField,
66
+ onError: fnField,
67
+ onClose: fnField,
68
+ });
69
+ exports.GsocMessageHandlerSchema = zod_1.z.object({
70
+ onMessage: fnField,
71
+ onError: fnField,
72
+ onClose: fnField,
73
+ });
74
+ exports.PostageBatchOptionsSchema = zod_1.z.object({
75
+ gasPrice: integerStringField.optional(),
76
+ immutableFlag: zod_1.z.boolean().optional(),
77
+ label: zod_1.z.string().optional(),
78
+ waitForUsable: zod_1.z.boolean().optional(),
79
+ waitForUsableTimeout: zod_1.z.number().int().min(0).optional(),
80
+ });
81
+ exports.TransactionOptionsSchema = zod_1.z.object({
82
+ gasLimit: nonNegativeIntegerStringField.optional(),
83
+ gasPrice: nonNegativeIntegerStringField.optional(),
84
+ });
85
+ exports.AllTagsOptionsSchema = zod_1.z.object({
86
+ limit: zod_1.z.number().int().min(types_1.TAGS_LIMIT_MIN).max(types_1.TAGS_LIMIT_MAX).optional(),
87
+ offset: zod_1.z.number().int().min(0).optional(),
88
+ });
@@ -177,10 +177,10 @@ function marshalStamp(signature, batchId, timestamp, index) {
177
177
  return new bytes_1.Bytes(cafe_utility_1.Binary.concatBytes(batchId, index, timestamp, signature));
178
178
  }
179
179
  function mapPostageBatch(raw, encryption, erasureCodeLevel) {
180
- const usage = getStampUsage(raw.utilization, raw.depth, raw.bucketDepth);
181
180
  const batchTTL = (0, workaround_1.normalizeBatchTTL)(raw.batchTTL);
182
181
  const duration = duration_1.Duration.fromSeconds(batchTTL);
183
182
  const effectiveBytes = getStampEffectiveBytes(raw.depth, encryption, erasureCodeLevel);
183
+ const usage = raw.utilizationRatio ?? getStampUsage(raw.utilization, raw.depth, raw.bucketDepth);
184
184
  return {
185
185
  batchID: new typed_bytes_1.BatchId(raw.batchID),
186
186
  utilization: raw.utilization,
@@ -211,6 +211,7 @@ function unmapPostageBatch(batch) {
211
211
  return {
212
212
  batchID: batch.batchID.toHex(),
213
213
  utilization: batch.utilization,
214
+ utilizationRatio: batch.usage,
214
215
  usable: batch.usable,
215
216
  label: batch.label,
216
217
  depth: batch.depth,