@ethersphere/bee-js 10.0.1 → 10.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.
package/dist/cjs/bee.js CHANGED
@@ -1743,13 +1743,15 @@ class Bee {
1743
1743
  *
1744
1744
  * @param postageBatchId Batch ID
1745
1745
  * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
1746
+ * @param encryption Assume that uploads with this postage batch are encrypted, which skews the capacity.
1747
+ * @param erasureCodeLevel Assume that uploads with this postage batch are erasure coded, which skews the capacity.
1746
1748
  *
1747
1749
  * @see [Bee docs - Keep your data alive / Postage stamps](https://docs.ethswarm.org/docs/develop/access-the-swarm/introduction/#keep-your-data-alive)
1748
1750
  * @see [Bee Debug API reference - `GET /stamps/${id}`](https://docs.ethswarm.org/api/#tag/Postage-Stamps/paths/~1stamps~1%7Bbatch_id%7D/get)
1749
1751
  */
1750
- async getPostageBatch(postageBatchId, requestOptions) {
1752
+ async getPostageBatch(postageBatchId, requestOptions, encryption, erasureCodeLevel) {
1751
1753
  postageBatchId = new typed_bytes_1.BatchId(postageBatchId);
1752
- return stamps.getPostageBatch(this.getRequestOptionsForCall(requestOptions), postageBatchId);
1754
+ return stamps.getPostageBatch(this.getRequestOptionsForCall(requestOptions), postageBatchId, encryption, erasureCodeLevel);
1753
1755
  }
1754
1756
  /**
1755
1757
  * Return detailed information related to buckets for specific postage batch.
@@ -46,6 +46,7 @@ async function getAllPostageBatches(requestOptions) {
46
46
  const usage = (0, stamps_1.getStampUsage)(utilization, depth, bucketDepth);
47
47
  const batchTTL = (0, workaround_1.normalizeBatchTTL)(cafe_utility_1.Types.asNumber(x.batchTTL, { name: 'batchTTL' }));
48
48
  const duration = duration_1.Duration.fromSeconds(batchTTL);
49
+ const effectiveBytes = (0, stamps_1.getStampEffectiveBytes)(depth);
49
50
  return {
50
51
  batchID: new typed_bytes_1.BatchId(cafe_utility_1.Types.asString(x.batchID, { name: 'batchID' })),
51
52
  utilization,
@@ -58,15 +59,23 @@ async function getAllPostageBatches(requestOptions) {
58
59
  immutableFlag: cafe_utility_1.Types.asBoolean(x.immutableFlag, { name: 'immutableFlag' }),
59
60
  usage,
60
61
  usageText: `${Math.round(usage * 100)}%`,
61
- size: size_1.Size.fromBytes((0, stamps_1.getStampEffectiveBytes)(depth)),
62
- remainingSize: size_1.Size.fromBytes(Math.ceil((0, stamps_1.getStampEffectiveBytes)(depth) * (1 - usage))),
62
+ size: size_1.Size.fromBytes(effectiveBytes),
63
+ remainingSize: size_1.Size.fromBytes(Math.ceil(effectiveBytes * (1 - usage))),
63
64
  theoreticalSize: size_1.Size.fromBytes((0, stamps_1.getStampTheoreticalBytes)(depth)),
64
65
  duration,
66
+ calculateSize(encryption, redundancyLevel) {
67
+ const effectiveBytes = (0, stamps_1.getStampEffectiveBytes)(this.depth, encryption, redundancyLevel);
68
+ return size_1.Size.fromBytes(effectiveBytes);
69
+ },
70
+ calculateRemainingSize(encryption, redundancyLevel) {
71
+ const effectiveBytes = (0, stamps_1.getStampEffectiveBytes)(this.depth, encryption, redundancyLevel);
72
+ return size_1.Size.fromBytes(Math.ceil(effectiveBytes * (1 - this.usage)));
73
+ },
65
74
  };
66
75
  });
67
76
  }
68
77
  exports.getAllPostageBatches = getAllPostageBatches;
69
- async function getPostageBatch(requestOptions, postageBatchId) {
78
+ async function getPostageBatch(requestOptions, postageBatchId, encryption, erasureCodeLevel) {
70
79
  const response = await (0, http_1.http)(requestOptions, {
71
80
  method: 'get',
72
81
  url: `${STAMPS_ENDPOINT}/${postageBatchId}`,
@@ -79,6 +88,7 @@ async function getPostageBatch(requestOptions, postageBatchId) {
79
88
  const usage = (0, stamps_1.getStampUsage)(utilization, depth, bucketDepth);
80
89
  const batchTTL = (0, workaround_1.normalizeBatchTTL)(cafe_utility_1.Types.asNumber(body.batchTTL, { name: 'batchTTL' }));
81
90
  const duration = duration_1.Duration.fromSeconds(batchTTL);
91
+ const effectiveBytes = (0, stamps_1.getStampEffectiveBytes)(depth, encryption, erasureCodeLevel);
82
92
  return {
83
93
  batchID: new typed_bytes_1.BatchId(cafe_utility_1.Types.asString(body.batchID, { name: 'batchID' })),
84
94
  utilization,
@@ -91,10 +101,18 @@ async function getPostageBatch(requestOptions, postageBatchId) {
91
101
  immutableFlag: cafe_utility_1.Types.asBoolean(body.immutableFlag, { name: 'immutableFlag' }),
92
102
  usage,
93
103
  usageText: `${Math.round(usage * 100)}%`,
94
- size: size_1.Size.fromBytes((0, stamps_1.getStampEffectiveBytes)(depth)),
95
- remainingSize: size_1.Size.fromBytes(Math.ceil((0, stamps_1.getStampEffectiveBytes)(depth) * (1 - usage))),
104
+ size: size_1.Size.fromBytes(effectiveBytes),
105
+ remainingSize: size_1.Size.fromBytes(Math.ceil(effectiveBytes * (1 - usage))),
96
106
  theoreticalSize: size_1.Size.fromBytes((0, stamps_1.getStampTheoreticalBytes)(depth)),
97
107
  duration,
108
+ calculateSize(encryption, redundancyLevel) {
109
+ const effectiveBytes = (0, stamps_1.getStampEffectiveBytes)(depth, encryption, redundancyLevel);
110
+ return size_1.Size.fromBytes(effectiveBytes);
111
+ },
112
+ calculateRemainingSize(encryption, redundancyLevel) {
113
+ const effectiveBytes = (0, stamps_1.getStampEffectiveBytes)(depth, encryption, redundancyLevel);
114
+ return size_1.Size.fromBytes(Math.ceil(effectiveBytes * (1 - usage)));
115
+ },
98
116
  };
99
117
  }
100
118
  exports.getPostageBatch = getPostageBatch;
@@ -30,6 +30,32 @@ class Duration {
30
30
  static fromEndDate(endDate, startDate) {
31
31
  return new Duration((endDate.getTime() - (startDate ?? new Date()).getTime()) / 1000);
32
32
  }
33
+ /**
34
+ * Parses a duration string and returns a `Duration` instance.
35
+ *
36
+ * Case insensitive. E.g. both `"28h"` and `"1D"` are valid.
37
+ *
38
+ * Whitespaces are ignored. E.g. both `"5 d"` and `"2weeks"` are valid.
39
+ *
40
+ * Decimal numbers are supported. E.g. `"1.5h"` is valid.
41
+ *
42
+ * Supported units:
43
+ *
44
+ * - ms, milli, millis, millisecond, milliseconds
45
+ * - s, sec, second, seconds
46
+ * - m, min, minute, minutes
47
+ * - h, hour, hours
48
+ * - d, day, days
49
+ * - w, week, weeks
50
+ * - month, months
51
+ * - y, year, years
52
+ *
53
+ * @param duration - A string representing a duration
54
+ * @returns a `Duration` instance
55
+ */
56
+ static parseFromString(duration) {
57
+ return Duration.fromSeconds(cafe_utility_1.Dates.make(duration) / 1000);
58
+ }
33
59
  toSeconds() {
34
60
  return this.seconds;
35
61
  }
@@ -28,6 +28,31 @@ class Size {
28
28
  static fromGigabytes(gigabytes) {
29
29
  return new Size(gigabytes * 1000 * 1000 * 1000);
30
30
  }
31
+ /**
32
+ * Parses a size string and returns a `Size` instance.
33
+ *
34
+ * Case insensitive. E.g. both `"28MB"` and `"1gb"` are valid.
35
+ *
36
+ * Whitespaces are ignored. E.g. both `"512 kb"` and `"2megabytes"` are valid.
37
+ *
38
+ * Decimal numbers are supported. E.g. `"1.5gb"` is valid.
39
+ *
40
+ * Uses 1000 as the base for conversions. E.g. 1kb = 1000 bytes.
41
+ * This is consistent with the effective stamp utilization table.
42
+ *
43
+ * Supported units:
44
+ * - b, byte, bytes
45
+ * - kb, kilobyte, kilobytes
46
+ * - mb, megabyte, megabytes
47
+ * - gb, gigabyte, gigabytes
48
+ * - tb, terabyte, terabytes
49
+ *
50
+ * @param size - A string representing a size
51
+ * @returns a `Size` instance
52
+ */
53
+ static parseFromString(size) {
54
+ return Size.fromBytes(cafe_utility_1.Numbers.makeStorage(size, 1000));
55
+ }
31
56
  toBytes() {
32
57
  return this.bytes;
33
58
  }