@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/mjs/bee.js CHANGED
@@ -1735,13 +1735,15 @@ export class Bee {
1735
1735
  *
1736
1736
  * @param postageBatchId Batch ID
1737
1737
  * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
1738
+ * @param encryption Assume that uploads with this postage batch are encrypted, which skews the capacity.
1739
+ * @param erasureCodeLevel Assume that uploads with this postage batch are erasure coded, which skews the capacity.
1738
1740
  *
1739
1741
  * @see [Bee docs - Keep your data alive / Postage stamps](https://docs.ethswarm.org/docs/develop/access-the-swarm/introduction/#keep-your-data-alive)
1740
1742
  * @see [Bee Debug API reference - `GET /stamps/${id}`](https://docs.ethswarm.org/api/#tag/Postage-Stamps/paths/~1stamps~1%7Bbatch_id%7D/get)
1741
1743
  */
1742
- async getPostageBatch(postageBatchId, requestOptions) {
1744
+ async getPostageBatch(postageBatchId, requestOptions, encryption, erasureCodeLevel) {
1743
1745
  postageBatchId = new BatchId(postageBatchId);
1744
- return stamps.getPostageBatch(this.getRequestOptionsForCall(requestOptions), postageBatchId);
1746
+ return stamps.getPostageBatch(this.getRequestOptionsForCall(requestOptions), postageBatchId, encryption, erasureCodeLevel);
1745
1747
  }
1746
1748
  /**
1747
1749
  * Return detailed information related to buckets for specific postage batch.
@@ -78,6 +78,7 @@ export async function getAllPostageBatches(requestOptions) {
78
78
  name: 'batchTTL'
79
79
  }));
80
80
  const duration = Duration.fromSeconds(batchTTL);
81
+ const effectiveBytes = getStampEffectiveBytes(depth);
81
82
  return {
82
83
  batchID: new BatchId(Types.asString(x.batchID, {
83
84
  name: 'batchID'
@@ -102,14 +103,22 @@ export async function getAllPostageBatches(requestOptions) {
102
103
  }),
103
104
  usage,
104
105
  usageText: `${Math.round(usage * 100)}%`,
105
- size: Size.fromBytes(getStampEffectiveBytes(depth)),
106
- remainingSize: Size.fromBytes(Math.ceil(getStampEffectiveBytes(depth) * (1 - usage))),
106
+ size: Size.fromBytes(effectiveBytes),
107
+ remainingSize: Size.fromBytes(Math.ceil(effectiveBytes * (1 - usage))),
107
108
  theoreticalSize: Size.fromBytes(getStampTheoreticalBytes(depth)),
108
- duration
109
+ duration,
110
+ calculateSize(encryption, redundancyLevel) {
111
+ const effectiveBytes = getStampEffectiveBytes(this.depth, encryption, redundancyLevel);
112
+ return Size.fromBytes(effectiveBytes);
113
+ },
114
+ calculateRemainingSize(encryption, redundancyLevel) {
115
+ const effectiveBytes = getStampEffectiveBytes(this.depth, encryption, redundancyLevel);
116
+ return Size.fromBytes(Math.ceil(effectiveBytes * (1 - this.usage)));
117
+ }
109
118
  };
110
119
  });
111
120
  }
112
- export async function getPostageBatch(requestOptions, postageBatchId) {
121
+ export async function getPostageBatch(requestOptions, postageBatchId, encryption, erasureCodeLevel) {
113
122
  const response = await http(requestOptions, {
114
123
  method: 'get',
115
124
  url: `${STAMPS_ENDPOINT}/${postageBatchId}`,
@@ -132,6 +141,7 @@ export async function getPostageBatch(requestOptions, postageBatchId) {
132
141
  name: 'batchTTL'
133
142
  }));
134
143
  const duration = Duration.fromSeconds(batchTTL);
144
+ const effectiveBytes = getStampEffectiveBytes(depth, encryption, erasureCodeLevel);
135
145
  return {
136
146
  batchID: new BatchId(Types.asString(body.batchID, {
137
147
  name: 'batchID'
@@ -156,10 +166,18 @@ export async function getPostageBatch(requestOptions, postageBatchId) {
156
166
  }),
157
167
  usage,
158
168
  usageText: `${Math.round(usage * 100)}%`,
159
- size: Size.fromBytes(getStampEffectiveBytes(depth)),
160
- remainingSize: Size.fromBytes(Math.ceil(getStampEffectiveBytes(depth) * (1 - usage))),
169
+ size: Size.fromBytes(effectiveBytes),
170
+ remainingSize: Size.fromBytes(Math.ceil(effectiveBytes * (1 - usage))),
161
171
  theoreticalSize: Size.fromBytes(getStampTheoreticalBytes(depth)),
162
- duration
172
+ duration,
173
+ calculateSize(encryption, redundancyLevel) {
174
+ const effectiveBytes = getStampEffectiveBytes(depth, encryption, redundancyLevel);
175
+ return Size.fromBytes(effectiveBytes);
176
+ },
177
+ calculateRemainingSize(encryption, redundancyLevel) {
178
+ const effectiveBytes = getStampEffectiveBytes(depth, encryption, redundancyLevel);
179
+ return Size.fromBytes(Math.ceil(effectiveBytes * (1 - usage)));
180
+ }
163
181
  };
164
182
  }
165
183
  export async function getPostageBatchBuckets(requestOptions, postageBatchId) {
@@ -27,6 +27,32 @@ export class Duration {
27
27
  static fromEndDate(endDate, startDate) {
28
28
  return new Duration((endDate.getTime() - (startDate ?? new Date()).getTime()) / 1000);
29
29
  }
30
+ /**
31
+ * Parses a duration string and returns a `Duration` instance.
32
+ *
33
+ * Case insensitive. E.g. both `"28h"` and `"1D"` are valid.
34
+ *
35
+ * Whitespaces are ignored. E.g. both `"5 d"` and `"2weeks"` are valid.
36
+ *
37
+ * Decimal numbers are supported. E.g. `"1.5h"` is valid.
38
+ *
39
+ * Supported units:
40
+ *
41
+ * - ms, milli, millis, millisecond, milliseconds
42
+ * - s, sec, second, seconds
43
+ * - m, min, minute, minutes
44
+ * - h, hour, hours
45
+ * - d, day, days
46
+ * - w, week, weeks
47
+ * - month, months
48
+ * - y, year, years
49
+ *
50
+ * @param duration - A string representing a duration
51
+ * @returns a `Duration` instance
52
+ */
53
+ static parseFromString(duration) {
54
+ return Duration.fromSeconds(Dates.make(duration) / 1000);
55
+ }
30
56
  toSeconds() {
31
57
  return this.seconds;
32
58
  }
@@ -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
  }
@@ -1158,11 +1158,13 @@ export declare class Bee {
1158
1158
  *
1159
1159
  * @param postageBatchId Batch ID
1160
1160
  * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
1161
+ * @param encryption Assume that uploads with this postage batch are encrypted, which skews the capacity.
1162
+ * @param erasureCodeLevel Assume that uploads with this postage batch are erasure coded, which skews the capacity.
1161
1163
  *
1162
1164
  * @see [Bee docs - Keep your data alive / Postage stamps](https://docs.ethswarm.org/docs/develop/access-the-swarm/introduction/#keep-your-data-alive)
1163
1165
  * @see [Bee Debug API reference - `GET /stamps/${id}`](https://docs.ethswarm.org/api/#tag/Postage-Stamps/paths/~1stamps~1%7Bbatch_id%7D/get)
1164
1166
  */
1165
- getPostageBatch(postageBatchId: BatchId | Uint8Array | string, requestOptions?: BeeRequestOptions): Promise<PostageBatch>;
1167
+ getPostageBatch(postageBatchId: BatchId | Uint8Array | string, requestOptions?: BeeRequestOptions, encryption?: boolean, erasureCodeLevel?: RedundancyLevel): Promise<PostageBatch>;
1166
1168
  /**
1167
1169
  * Return detailed information related to buckets for specific postage batch.
1168
1170
  *
@@ -1,8 +1,8 @@
1
- import type { BeeRequestOptions, GlobalPostageBatch, NumberString, PostageBatch, PostageBatchBuckets, PostageBatchOptions } from '../../types';
1
+ import type { BeeRequestOptions, GlobalPostageBatch, NumberString, PostageBatch, PostageBatchBuckets, PostageBatchOptions, RedundancyLevel } from '../../types';
2
2
  import { BatchId } from '../../utils/typed-bytes';
3
3
  export declare function getGlobalPostageBatches(requestOptions: BeeRequestOptions): Promise<GlobalPostageBatch[]>;
4
4
  export declare function getAllPostageBatches(requestOptions: BeeRequestOptions): Promise<PostageBatch[]>;
5
- export declare function getPostageBatch(requestOptions: BeeRequestOptions, postageBatchId: BatchId): Promise<PostageBatch>;
5
+ export declare function getPostageBatch(requestOptions: BeeRequestOptions, postageBatchId: BatchId, encryption?: boolean, erasureCodeLevel?: RedundancyLevel): Promise<PostageBatch>;
6
6
  export declare function getPostageBatchBuckets(requestOptions: BeeRequestOptions, postageBatchId: BatchId): Promise<PostageBatchBuckets>;
7
7
  export declare function createPostageBatch(requestOptions: BeeRequestOptions, amount: NumberString, depth: number, options?: PostageBatchOptions): Promise<BatchId>;
8
8
  export declare function topUpBatch(requestOptions: BeeRequestOptions, id: BatchId, amount: NumberString): Promise<BatchId>;
@@ -450,6 +450,16 @@ export interface PostageBatch {
450
450
  * Theoretical size in bytes
451
451
  */
452
452
  theoreticalSize: Size;
453
+ /**
454
+ * Calculates the effective size of data that can be uploaded with this postage batch
455
+ * based on whether encryption is used and the desired redundancy level.
456
+ */
457
+ calculateSize(encryption: boolean, redundancyLevel: RedundancyLevel): Size;
458
+ /**
459
+ * Calculates the remaining size of data that can be uploaded with this postage batch
460
+ * based on whether encryption is used and the desired redundancy level.
461
+ */
462
+ calculateRemainingSize(encryption: boolean, redundancyLevel: RedundancyLevel): Size;
453
463
  }
454
464
  export interface BatchBucket {
455
465
  bucketID: number;
@@ -9,6 +9,30 @@ export declare class Duration {
9
9
  static fromWeeks(weeks: number): Duration;
10
10
  static fromYears(years: number): Duration;
11
11
  static fromEndDate(endDate: Date, startDate?: Date): Duration;
12
+ /**
13
+ * Parses a duration string and returns a `Duration` instance.
14
+ *
15
+ * Case insensitive. E.g. both `"28h"` and `"1D"` are valid.
16
+ *
17
+ * Whitespaces are ignored. E.g. both `"5 d"` and `"2weeks"` are valid.
18
+ *
19
+ * Decimal numbers are supported. E.g. `"1.5h"` is valid.
20
+ *
21
+ * Supported units:
22
+ *
23
+ * - ms, milli, millis, millisecond, milliseconds
24
+ * - s, sec, second, seconds
25
+ * - m, min, minute, minutes
26
+ * - h, hour, hours
27
+ * - d, day, days
28
+ * - w, week, weeks
29
+ * - month, months
30
+ * - y, year, years
31
+ *
32
+ * @param duration - A string representing a duration
33
+ * @returns a `Duration` instance
34
+ */
35
+ static parseFromString(duration: string): Duration;
12
36
  toSeconds(): number;
13
37
  toHours(): number;
14
38
  toDays(): number;
@@ -12,6 +12,29 @@ export declare class Size {
12
12
  static fromKilobytes(kilobytes: number): Size;
13
13
  static fromMegabytes(megabytes: number): Size;
14
14
  static fromGigabytes(gigabytes: number): Size;
15
+ /**
16
+ * Parses a size string and returns a `Size` instance.
17
+ *
18
+ * Case insensitive. E.g. both `"28MB"` and `"1gb"` are valid.
19
+ *
20
+ * Whitespaces are ignored. E.g. both `"512 kb"` and `"2megabytes"` are valid.
21
+ *
22
+ * Decimal numbers are supported. E.g. `"1.5gb"` is valid.
23
+ *
24
+ * Uses 1000 as the base for conversions. E.g. 1kb = 1000 bytes.
25
+ * This is consistent with the effective stamp utilization table.
26
+ *
27
+ * Supported units:
28
+ * - b, byte, bytes
29
+ * - kb, kilobyte, kilobytes
30
+ * - mb, megabyte, megabytes
31
+ * - gb, gigabyte, gigabytes
32
+ * - tb, terabyte, terabytes
33
+ *
34
+ * @param size - A string representing a size
35
+ * @returns a `Size` instance
36
+ */
37
+ static parseFromString(size: string): Size;
15
38
  toBytes(): number;
16
39
  toGigabytes(): number;
17
40
  toFormattedString(): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ethersphere/bee-js",
3
- "version": "10.0.1",
3
+ "version": "10.1.1",
4
4
  "description": "Javascript client for Bee",
5
5
  "keywords": [
6
6
  "bee",
@@ -62,7 +62,7 @@
62
62
  },
63
63
  "dependencies": {
64
64
  "axios": "^0.30.0",
65
- "cafe-utility": "^31.0.0",
65
+ "cafe-utility": "^32.2.0",
66
66
  "debug": "^4.4.1",
67
67
  "isomorphic-ws": "^4.0.1",
68
68
  "semver": "^7.3.5",