@upcoming/bee-js 9.9.1 → 11.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/README.md +2 -2
  2. package/dist/cjs/bee.js +881 -370
  3. package/dist/cjs/chunk/bmt.js +1 -2
  4. package/dist/cjs/chunk/cac.js +27 -32
  5. package/dist/cjs/chunk/soc.js +39 -31
  6. package/dist/cjs/feed/identifier.js +1 -2
  7. package/dist/cjs/feed/index.js +29 -19
  8. package/dist/cjs/feed/retrievable.js +1 -2
  9. package/dist/cjs/index.js +17 -7
  10. package/dist/cjs/manifest/manifest.js +19 -2
  11. package/dist/cjs/modules/bytes.js +4 -5
  12. package/dist/cjs/modules/bzz.js +4 -5
  13. package/dist/cjs/modules/chunk.js +2 -3
  14. package/dist/cjs/modules/debug/balance.js +4 -5
  15. package/dist/cjs/modules/debug/chequebook.js +9 -10
  16. package/dist/cjs/modules/debug/connectivity.js +7 -8
  17. package/dist/cjs/modules/debug/settlements.js +2 -3
  18. package/dist/cjs/modules/debug/stake.js +6 -7
  19. package/dist/cjs/modules/debug/stamps.js +24 -60
  20. package/dist/cjs/modules/debug/states.js +6 -6
  21. package/dist/cjs/modules/debug/status.js +9 -9
  22. package/dist/cjs/modules/debug/transactions.js +4 -5
  23. package/dist/cjs/modules/envelope.js +1 -2
  24. package/dist/cjs/modules/feed.js +3 -4
  25. package/dist/cjs/modules/grantee.js +3 -4
  26. package/dist/cjs/modules/gsoc.js +2 -3
  27. package/dist/cjs/modules/pinning.js +4 -5
  28. package/dist/cjs/modules/pss.js +2 -3
  29. package/dist/cjs/modules/rchash.js +1 -2
  30. package/dist/cjs/modules/soc.js +1 -2
  31. package/dist/cjs/modules/status.js +2 -3
  32. package/dist/cjs/modules/stewardship.js +2 -3
  33. package/dist/cjs/modules/tag.js +5 -6
  34. package/dist/cjs/types/debug.js +3 -3
  35. package/dist/cjs/types/index.js +11 -6
  36. package/dist/cjs/utils/bytes.js +23 -3
  37. package/dist/cjs/utils/chunk-size.js +1 -2
  38. package/dist/cjs/utils/chunk-stream.browser.js +30 -6
  39. package/dist/cjs/utils/chunk-stream.js +3 -4
  40. package/dist/cjs/utils/cid.js +3 -3
  41. package/dist/cjs/utils/collection.browser.js +2 -3
  42. package/dist/cjs/utils/collection.js +5 -6
  43. package/dist/cjs/utils/collection.node.js +2 -3
  44. package/dist/cjs/utils/data.browser.js +1 -2
  45. package/dist/cjs/utils/data.js +1 -2
  46. package/dist/cjs/utils/duration.js +27 -1
  47. package/dist/cjs/utils/expose.js +4 -1
  48. package/dist/cjs/utils/file.js +2 -3
  49. package/dist/cjs/utils/headers.js +2 -3
  50. package/dist/cjs/utils/http.js +25 -5
  51. package/dist/cjs/utils/pss.js +1 -2
  52. package/dist/cjs/utils/redundancy.js +3 -4
  53. package/dist/cjs/utils/size.js +25 -0
  54. package/dist/cjs/utils/stamps.js +60 -11
  55. package/dist/cjs/utils/tar-uploader.browser.js +1 -2
  56. package/dist/cjs/utils/tar-uploader.js +1 -2
  57. package/dist/cjs/utils/tar-writer.browser.js +1 -2
  58. package/dist/cjs/utils/tar-writer.js +1 -2
  59. package/dist/cjs/utils/type.js +37 -25
  60. package/dist/cjs/utils/url.js +3 -4
  61. package/dist/cjs/utils/workaround.js +7 -5
  62. package/dist/index.browser.min.js +1 -1
  63. package/dist/index.browser.min.js.map +1 -1
  64. package/dist/mjs/bee.js +864 -366
  65. package/dist/mjs/chunk/cac.js +21 -30
  66. package/dist/mjs/chunk/soc.js +16 -17
  67. package/dist/mjs/feed/index.js +7 -6
  68. package/dist/mjs/manifest/manifest.js +19 -2
  69. package/dist/mjs/modules/debug/chequebook.js +2 -2
  70. package/dist/mjs/modules/debug/stamps.js +38 -93
  71. package/dist/mjs/modules/debug/states.js +3 -0
  72. package/dist/mjs/modules/debug/status.js +1 -1
  73. package/dist/mjs/types/index.js +8 -3
  74. package/dist/mjs/utils/bytes.js +19 -2
  75. package/dist/mjs/utils/chunk-stream.browser.js +29 -5
  76. package/dist/mjs/utils/duration.js +27 -1
  77. package/dist/mjs/utils/expose.js +1 -1
  78. package/dist/mjs/utils/http.js +25 -3
  79. package/dist/mjs/utils/size.js +25 -0
  80. package/dist/mjs/utils/stamps.js +48 -0
  81. package/dist/mjs/utils/type.js +8 -1
  82. package/dist/mjs/utils/workaround.js +5 -2
  83. package/dist/types/bee.d.ts +756 -252
  84. package/dist/types/chunk/cac.d.ts +27 -13
  85. package/dist/types/chunk/soc.d.ts +43 -11
  86. package/dist/types/index.d.ts +3 -0
  87. package/dist/types/modules/bzz.d.ts +0 -1
  88. package/dist/types/modules/debug/stamps.d.ts +2 -2
  89. package/dist/types/modules/debug/status.d.ts +1 -1
  90. package/dist/types/modules/gsoc.d.ts +0 -1
  91. package/dist/types/modules/pss.d.ts +0 -1
  92. package/dist/types/types/debug.d.ts +2 -1
  93. package/dist/types/types/index.d.ts +53 -6
  94. package/dist/types/utils/constants.d.ts +3 -3
  95. package/dist/types/utils/duration.d.ts +24 -0
  96. package/dist/types/utils/error.d.ts +2 -2
  97. package/dist/types/utils/expose.d.ts +1 -1
  98. package/dist/types/utils/size.d.ts +23 -0
  99. package/dist/types/utils/stamps.d.ts +15 -1
  100. package/dist/types/utils/tar.browser.d.ts +1 -1
  101. package/dist/types/utils/tar.d.ts +0 -1
  102. package/dist/types/utils/type.d.ts +0 -1
  103. package/package.json +10 -13
@@ -10,6 +10,7 @@ const MAX_FAILED_ATTEMPTS = 100000;
10
10
  const DELAY_FAST = 200;
11
11
  const DELAY_SLOW = 1000;
12
12
  const DELAY_THRESHOLD = Dates.minutes(1) / DELAY_FAST;
13
+ const ABORT_ERROR_MESSAGE = 'Request aborted';
13
14
  export const DEFAULT_HTTP_CONFIG = {
14
15
  headers: {
15
16
  accept: 'application/json, text/plain, */*'
@@ -17,6 +18,11 @@ export const DEFAULT_HTTP_CONFIG = {
17
18
  maxBodyLength: Infinity,
18
19
  maxContentLength: Infinity
19
20
  };
21
+ function throwIfAborted(signal, config, responseData, responseStatus) {
22
+ if (signal?.aborted) {
23
+ throw new BeeResponseError(config.method || 'get', config.url || '<unknown>', ABORT_ERROR_MESSAGE, responseData, responseStatus, 'ERR_CANCELED');
24
+ }
25
+ }
20
26
  /**
21
27
  * Main function to make HTTP requests.
22
28
  * @param options User defined settings
@@ -24,9 +30,11 @@ export const DEFAULT_HTTP_CONFIG = {
24
30
  */
25
31
  export async function http(options, config) {
26
32
  const requestConfig = Objects.deepMerge3(DEFAULT_HTTP_CONFIG, config, options);
27
- if (requestConfig.data && typeof Buffer !== 'undefined' && Buffer.isBuffer(requestConfig.data)) {
28
- requestConfig.data = requestConfig.data.buffer.slice(requestConfig.data.byteOffset, requestConfig.data.byteOffset + requestConfig.data.byteLength);
33
+ if (options.signal) {
34
+ requestConfig.signal = options.signal;
35
+ throwIfAborted(options.signal, config);
29
36
  }
37
+ maybeReplaceBodyBuffers(requestConfig);
30
38
  if (requestConfig.params) {
31
39
  const keys = Object.keys(requestConfig.params);
32
40
  for (const key of keys) {
@@ -38,6 +46,7 @@ export async function http(options, config) {
38
46
  }
39
47
  let failedAttempts = 0;
40
48
  while (failedAttempts < MAX_FAILED_ATTEMPTS) {
49
+ throwIfAborted(options.signal, config);
41
50
  try {
42
51
  debug(`${requestConfig.method || 'get'} ${Strings.joinUrl([requestConfig.baseURL, requestConfig.url])}`, {
43
52
  headers: {
@@ -50,11 +59,16 @@ export async function http(options, config) {
50
59
  return response;
51
60
  } catch (e) {
52
61
  if (e instanceof AxiosError) {
62
+ if (e.code === 'ERR_CANCELED') {
63
+ throwIfAborted({
64
+ aborted: true
65
+ }, config, e.response?.data, e.response?.status);
66
+ }
53
67
  if (e.code === 'ECONNABORTED' && options.endlesslyRetry) {
54
68
  failedAttempts++;
55
69
  await System.sleepMillis(failedAttempts < DELAY_THRESHOLD ? DELAY_FAST : DELAY_SLOW);
56
70
  } else {
57
- throw new BeeResponseError(config.method || 'get', config.url || '<unknown>', e.message, e.response?.data, e.response?.status, e.code);
71
+ throw new BeeResponseError(config.method || 'get', config.url || '<unknown>', e.message, e.response?.data, e.response?.status, e.response?.statusText);
58
72
  }
59
73
  } else {
60
74
  throw e;
@@ -74,4 +88,12 @@ function maybeRunOnRequestHook(options, requestConfig) {
74
88
  params: requestConfig.params
75
89
  });
76
90
  }
91
+ }
92
+ function maybeReplaceBodyBuffers(config) {
93
+ if (config.data && config.data instanceof Uint8Array) {
94
+ config.data = config.data.buffer.slice(config.data.byteOffset, config.data.byteOffset + config.data.byteLength);
95
+ }
96
+ if (config.data && typeof Buffer !== 'undefined' && Buffer.isBuffer(config.data)) {
97
+ config.data = config.data.buffer.slice(config.data.byteOffset, config.data.byteOffset + config.data.byteLength);
98
+ }
77
99
  }
@@ -25,6 +25,31 @@ export class Size {
25
25
  static fromGigabytes(gigabytes) {
26
26
  return new Size(gigabytes * 1000 * 1000 * 1000);
27
27
  }
28
+ /**
29
+ * Parses a size string and returns a `Size` instance.
30
+ *
31
+ * Case insensitive. E.g. both `"28MB"` and `"1gb"` are valid.
32
+ *
33
+ * Whitespaces are ignored. E.g. both `"512 kb"` and `"2megabytes"` are valid.
34
+ *
35
+ * Decimal numbers are supported. E.g. `"1.5gb"` is valid.
36
+ *
37
+ * Uses 1000 as the base for conversions. E.g. 1kb = 1000 bytes.
38
+ * This is consistent with the effective stamp utilization table.
39
+ *
40
+ * Supported units:
41
+ * - b, byte, bytes
42
+ * - kb, kilobyte, kilobytes
43
+ * - mb, megabyte, megabytes
44
+ * - gb, gigabyte, gigabytes
45
+ * - tb, terabyte, terabytes
46
+ *
47
+ * @param size - A string representing a size
48
+ * @returns a `Size` instance
49
+ */
50
+ static parseFromString(size) {
51
+ return Size.fromBytes(Numbers.makeStorage(size, 1000));
52
+ }
28
53
  toBytes() {
29
54
  return this.bytes;
30
55
  }
@@ -2,8 +2,11 @@ import { Binary } from 'cafe-utility';
2
2
  import { capacityBreakpoints } from "../types/index.js";
3
3
  import { Bytes, parseSizeToBytes } from "./bytes.js";
4
4
  import { Duration } from "./duration.js";
5
+ import { Size } from "./size.js";
5
6
  import { BZZ } from "./tokens.js";
6
7
  import { asNumberString } from "./type.js";
8
+ import { BatchId } from "./typed-bytes.js";
9
+ import { normalizeBatchTTL } from "./workaround.js";
7
10
  const MAX_UTILIZATION = 0.9;
8
11
  /**
9
12
  * Utility function that calculates usage of postage batch based on its utilization, depth and bucket depth.
@@ -137,4 +140,49 @@ export function marshalStamp(signature, batchId, timestamp, index) {
137
140
  throw Error('invalid index length');
138
141
  }
139
142
  return new Bytes(Binary.concatBytes(batchId, index, timestamp, signature));
143
+ }
144
+ export function mapPostageBatch(raw, encryption, erasureCodeLevel) {
145
+ const usage = getStampUsage(raw.utilization, raw.depth, raw.bucketDepth);
146
+ const batchTTL = normalizeBatchTTL(raw.batchTTL);
147
+ const duration = Duration.fromSeconds(batchTTL);
148
+ const effectiveBytes = getStampEffectiveBytes(raw.depth, encryption, erasureCodeLevel);
149
+ return {
150
+ batchID: new BatchId(raw.batchID),
151
+ utilization: raw.utilization,
152
+ usable: raw.usable,
153
+ label: raw.label,
154
+ depth: raw.depth,
155
+ amount: asNumberString(raw.amount),
156
+ bucketDepth: raw.bucketDepth,
157
+ blockNumber: raw.blockNumber,
158
+ immutableFlag: raw.immutableFlag,
159
+ usage,
160
+ usageText: `${Math.round(usage * 100)}%`,
161
+ size: Size.fromBytes(effectiveBytes),
162
+ remainingSize: Size.fromBytes(Math.ceil(effectiveBytes * (1 - usage))),
163
+ theoreticalSize: Size.fromBytes(getStampTheoreticalBytes(raw.depth)),
164
+ duration,
165
+ calculateSize(encryption, redundancyLevel) {
166
+ const effectiveBytes = getStampEffectiveBytes(raw.depth, encryption, redundancyLevel);
167
+ return Size.fromBytes(effectiveBytes);
168
+ },
169
+ calculateRemainingSize(encryption, redundancyLevel) {
170
+ const effectiveBytes = getStampEffectiveBytes(raw.depth, encryption, redundancyLevel);
171
+ return Size.fromBytes(Math.ceil(effectiveBytes * (1 - this.usage)));
172
+ }
173
+ };
174
+ }
175
+ export function unmapPostageBatch(batch) {
176
+ return {
177
+ batchID: batch.batchID.toHex(),
178
+ utilization: batch.utilization,
179
+ usable: batch.usable,
180
+ label: batch.label,
181
+ depth: batch.depth,
182
+ amount: batch.amount,
183
+ bucketDepth: batch.bucketDepth,
184
+ blockNumber: batch.blockNumber,
185
+ immutableFlag: batch.immutableFlag,
186
+ batchTTL: batch.duration.toSeconds()
187
+ };
140
188
  }
@@ -34,7 +34,8 @@ export function prepareBeeRequestOptions(value) {
34
34
  httpsAgent: object.httpsAgent,
35
35
  endlesslyRetry: Types.asOptional(x => Types.asBoolean(x, {
36
36
  name: 'endlesslyRetry'
37
- }), object.endlesslyRetry)
37
+ }), object.endlesslyRetry),
38
+ signal: object.signal
38
39
  };
39
40
  }
40
41
  export function prepareDownloadOptions(value) {
@@ -158,6 +159,9 @@ export function preparePssMessageHandler(value) {
158
159
  }),
159
160
  onError: Types.asFunction(object.onError, {
160
161
  name: 'onError'
162
+ }),
163
+ onClose: Types.asFunction(object.onClose, {
164
+ name: 'onClose'
161
165
  })
162
166
  };
163
167
  }
@@ -171,6 +175,9 @@ export function prepareGsocMessageHandler(value) {
171
175
  }),
172
176
  onError: Types.asFunction(object.onError, {
173
177
  name: 'onError'
178
+ }),
179
+ onClose: Types.asFunction(object.onClose, {
180
+ name: 'onClose'
174
181
  })
175
182
  };
176
183
  }
@@ -6,8 +6,11 @@ export function normalizeBatchTTL(batchTTL) {
6
6
  if (batchTTL < 1) {
7
7
  return 1;
8
8
  }
9
- if (batchTTL > 315569260) {
10
- return 315569260;
9
+ // Cap `batchTTL` (represents seconds) to 100 years.
10
+ // We can assume `storagePrice` is invalid (e.g. 1).
11
+ // This is needed to prevent Date objects breaking.
12
+ if (batchTTL > 3155695200) {
13
+ return 3155695200;
11
14
  }
12
15
  return batchTTL;
13
16
  }