@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.
@@ -20,7 +20,7 @@ async function getChequebookAddress(requestOptions) {
20
20
  });
21
21
  const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
22
22
  return {
23
- chequebookAddress: cafe_utility_1.Types.asString(body.chequebookAddress, { name: 'chequebookAddress' }),
23
+ chequebookAddress: new typed_bytes_1.EthAddress(cafe_utility_1.Types.asString(body.chequebookAddress, { name: 'chequebookAddress' })),
24
24
  };
25
25
  }
26
26
  exports.getChequebookAddress = getChequebookAddress;
@@ -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;
@@ -14,23 +14,28 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.capacityBreakpoints = exports.RedundancyStrategy = exports.RedundancyLevel = exports.FEED_INDEX_HEX_LENGTH = exports.TAGS_LIMIT_MAX = exports.TAGS_LIMIT_MIN = exports.STAMPS_DEPTH_MAX = exports.STAMPS_DEPTH_MIN = exports.PSS_TARGET_HEX_LENGTH_MAX = exports.CHUNK_SIZE = exports.BRANCHES = exports.SECTION_SIZE = void 0;
17
+ exports.capacityBreakpoints = exports.RedundancyStrategy = exports.RedundancyLevel = exports.TAGS_LIMIT_MAX = exports.TAGS_LIMIT_MIN = exports.STAMPS_DEPTH_MAX = exports.STAMPS_DEPTH_MIN = exports.PSS_TARGET_HEX_LENGTH_MAX = exports.CHUNK_SIZE = exports.BRANCHES = exports.SECTION_SIZE = void 0;
18
18
  __exportStar(require("./debug"), exports);
19
19
  exports.SECTION_SIZE = 32;
20
20
  exports.BRANCHES = 128;
21
21
  exports.CHUNK_SIZE = exports.SECTION_SIZE * exports.BRANCHES;
22
22
  exports.PSS_TARGET_HEX_LENGTH_MAX = 4;
23
23
  /**
24
- * Minimal depth that can be used for creation of postage batch
24
+ * Minimum postage batch depth.
25
25
  */
26
26
  exports.STAMPS_DEPTH_MIN = 17;
27
27
  /**
28
- * Maximal depth that can be used for creation of postage batch
28
+ * Maximum postage batch depth.
29
29
  */
30
30
  exports.STAMPS_DEPTH_MAX = 255;
31
+ /**
32
+ * Minimum tags API page size.
33
+ */
31
34
  exports.TAGS_LIMIT_MIN = 1;
35
+ /**
36
+ * Maximum tags API page size.
37
+ */
32
38
  exports.TAGS_LIMIT_MAX = 1000;
33
- exports.FEED_INDEX_HEX_LENGTH = 16;
34
39
  /**
35
40
  * Add redundancy to the data being uploaded so that downloaders can download it with better UX.
36
41
  * 0 value is default and does not add any redundancy to the file.
@@ -1,11 +1,19 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.parseSizeToBytes = exports.Bytes = void 0;
4
7
  const cafe_utility_1 = require("cafe-utility");
8
+ const debug_1 = __importDefault(require("debug"));
9
+ const debug = (0, debug_1.default)('bee-js:bytes');
5
10
  const DECODER = new TextDecoder();
6
11
  const ENCODER = new TextEncoder();
7
12
  class Bytes {
8
13
  constructor(bytes, byteLength) {
14
+ if (!bytes) {
15
+ throw Error(`Bytes#constructor: constructor parameter is falsy: ${bytes}`);
16
+ }
9
17
  if (bytes instanceof Bytes) {
10
18
  this.bytes = bytes.bytes;
11
19
  }
@@ -15,9 +23,21 @@ class Bytes {
15
23
  else if (bytes instanceof ArrayBuffer) {
16
24
  this.bytes = new Uint8Array(bytes);
17
25
  }
18
- else {
26
+ else if (bytes instanceof Uint8Array) {
19
27
  this.bytes = bytes;
20
28
  }
29
+ else {
30
+ const unknownInput = bytes;
31
+ const toHex = cafe_utility_1.Objects.getDeep(unknownInput, 'toHex');
32
+ if (cafe_utility_1.Types.isFunction(toHex)) {
33
+ const hex = toHex.call(unknownInput);
34
+ this.bytes = cafe_utility_1.Binary.hexToUint8Array(cafe_utility_1.Types.asHexString(hex, { name: 'Bytes#constructor(bytes)' }));
35
+ }
36
+ else {
37
+ debug('bytes', bytes);
38
+ throw new Error(`Bytes#constructor: unsupported type: ${typeof bytes}`);
39
+ }
40
+ }
21
41
  this.length = this.bytes.length;
22
42
  if (byteLength) {
23
43
  if (Array.isArray(byteLength)) {
@@ -6,7 +6,7 @@ class Duration {
6
6
  constructor(seconds) {
7
7
  this.seconds = Math.ceil(seconds);
8
8
  if (seconds < 0) {
9
- throw Error('Duration cannot be negative');
9
+ this.seconds = 0;
10
10
  }
11
11
  }
12
12
  static fromMilliseconds(milliseconds) {
@@ -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
  }
@@ -124,6 +124,7 @@ function preparePssMessageHandler(value) {
124
124
  return {
125
125
  onMessage: cafe_utility_1.Types.asFunction(object.onMessage, { name: 'onMessage' }),
126
126
  onError: cafe_utility_1.Types.asFunction(object.onError, { name: 'onError' }),
127
+ onClose: cafe_utility_1.Types.asFunction(object.onClose, { name: 'onClose' }),
127
128
  };
128
129
  }
129
130
  exports.preparePssMessageHandler = preparePssMessageHandler;
@@ -132,6 +133,7 @@ function prepareGsocMessageHandler(value) {
132
133
  return {
133
134
  onMessage: cafe_utility_1.Types.asFunction(object.onMessage, { name: 'onMessage' }),
134
135
  onError: cafe_utility_1.Types.asFunction(object.onError, { name: 'onError' }),
136
+ onClose: cafe_utility_1.Types.asFunction(object.onClose, { name: 'onClose' }),
135
137
  };
136
138
  }
137
139
  exports.prepareGsocMessageHandler = prepareGsocMessageHandler;