@upcoming/bee-js 9.9.0 → 10.1.2

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.
@@ -19,9 +19,9 @@ export async function getChequebookAddress(requestOptions) {
19
19
  name: 'response.data'
20
20
  });
21
21
  return {
22
- chequebookAddress: Types.asString(body.chequebookAddress, {
22
+ chequebookAddress: new EthAddress(Types.asString(body.chequebookAddress, {
23
23
  name: 'chequebookAddress'
24
- })
24
+ }))
25
25
  };
26
26
  }
27
27
  /**
@@ -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) {
@@ -4,16 +4,21 @@ export const BRANCHES = 128;
4
4
  export const CHUNK_SIZE = SECTION_SIZE * BRANCHES;
5
5
  export const PSS_TARGET_HEX_LENGTH_MAX = 4;
6
6
  /**
7
- * Minimal depth that can be used for creation of postage batch
7
+ * Minimum postage batch depth.
8
8
  */
9
9
  export const STAMPS_DEPTH_MIN = 17;
10
10
  /**
11
- * Maximal depth that can be used for creation of postage batch
11
+ * Maximum postage batch depth.
12
12
  */
13
13
  export const STAMPS_DEPTH_MAX = 255;
14
+ /**
15
+ * Minimum tags API page size.
16
+ */
14
17
  export const TAGS_LIMIT_MIN = 1;
18
+ /**
19
+ * Maximum tags API page size.
20
+ */
15
21
  export const TAGS_LIMIT_MAX = 1000;
16
- export const FEED_INDEX_HEX_LENGTH = 16;
17
22
  /**
18
23
  * Add redundancy to the data being uploaded so that downloaders can download it with better UX.
19
24
  * 0 value is default and does not add any redundancy to the file.
@@ -1,8 +1,13 @@
1
- import { Binary, Types } from 'cafe-utility';
1
+ import { Binary, Objects, Types } from 'cafe-utility';
2
+ import _debug from 'debug';
3
+ const debug = _debug('bee-js:bytes');
2
4
  const DECODER = new TextDecoder();
3
5
  const ENCODER = new TextEncoder();
4
6
  export class Bytes {
5
7
  constructor(bytes, byteLength) {
8
+ if (!bytes) {
9
+ throw Error(`Bytes#constructor: constructor parameter is falsy: ${bytes}`);
10
+ }
6
11
  if (bytes instanceof Bytes) {
7
12
  this.bytes = bytes.bytes;
8
13
  } else if (typeof bytes === 'string') {
@@ -11,8 +16,20 @@ export class Bytes {
11
16
  }));
12
17
  } else if (bytes instanceof ArrayBuffer) {
13
18
  this.bytes = new Uint8Array(bytes);
14
- } else {
19
+ } else if (bytes instanceof Uint8Array) {
15
20
  this.bytes = bytes;
21
+ } else {
22
+ const unknownInput = bytes;
23
+ const toHex = Objects.getDeep(unknownInput, 'toHex');
24
+ if (Types.isFunction(toHex)) {
25
+ const hex = toHex.call(unknownInput);
26
+ this.bytes = Binary.hexToUint8Array(Types.asHexString(hex, {
27
+ name: 'Bytes#constructor(bytes)'
28
+ }));
29
+ } else {
30
+ debug('bytes', bytes);
31
+ throw new Error(`Bytes#constructor: unsupported type: ${typeof bytes}`);
32
+ }
16
33
  }
17
34
  this.length = this.bytes.length;
18
35
  if (byteLength) {
@@ -3,7 +3,7 @@ export class Duration {
3
3
  constructor(seconds) {
4
4
  this.seconds = Math.ceil(seconds);
5
5
  if (seconds < 0) {
6
- throw Error('Duration cannot be negative');
6
+ this.seconds = 0;
7
7
  }
8
8
  }
9
9
  static fromMilliseconds(milliseconds) {
@@ -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
  }
@@ -158,6 +158,9 @@ export function preparePssMessageHandler(value) {
158
158
  }),
159
159
  onError: Types.asFunction(object.onError, {
160
160
  name: 'onError'
161
+ }),
162
+ onClose: Types.asFunction(object.onClose, {
163
+ name: 'onClose'
161
164
  })
162
165
  };
163
166
  }
@@ -171,6 +174,9 @@ export function prepareGsocMessageHandler(value) {
171
174
  }),
172
175
  onError: Types.asFunction(object.onError, {
173
176
  name: 'onError'
177
+ }),
178
+ onClose: Types.asFunction(object.onClose, {
179
+ name: 'onClose'
174
180
  })
175
181
  };
176
182
  }