@ethersphere/bee-js 9.8.0 → 10.0.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.
@@ -67,4 +67,21 @@ export class Bytes {
67
67
  represent() {
68
68
  return this.toHex();
69
69
  }
70
+ }
71
+ export function parseSizeToBytes(sizeStr) {
72
+ const units = {
73
+ B: 1,
74
+ kB: 1000,
75
+ MB: 1000 ** 2,
76
+ GB: 1000 ** 3,
77
+ TB: 1000 ** 4,
78
+ PB: 1000 ** 5
79
+ };
80
+ const match = sizeStr.match(/^([\d.]+)\s*(B|kB|MB|GB|TB|PB)$/);
81
+ if (!match) {
82
+ throw new Error(`Invalid size format: ${sizeStr}`);
83
+ }
84
+ const value = parseFloat(match[1]);
85
+ const unit = match[2];
86
+ return Math.ceil(value * units[unit]);
70
87
  }
@@ -2,8 +2,8 @@ import { Dates } from 'cafe-utility';
2
2
  export class Duration {
3
3
  constructor(seconds) {
4
4
  this.seconds = Math.ceil(seconds);
5
- if (seconds <= 0) {
6
- throw Error('Duration must be greater than 0');
5
+ if (seconds < 0) {
6
+ this.seconds = 0;
7
7
  }
8
8
  }
9
9
  static fromMilliseconds(milliseconds) {
@@ -48,4 +48,8 @@ export class Duration {
48
48
  represent() {
49
49
  return Dates.secondsToHumanTime(this.seconds);
50
50
  }
51
- }
51
+ isZero() {
52
+ return this.seconds === 0;
53
+ }
54
+ }
55
+ Duration.ZERO = new Duration(0);
@@ -1,5 +1,6 @@
1
1
  import { Binary } from 'cafe-utility';
2
- import { Bytes } from "./bytes.js";
2
+ import { capacityBreakpoints } from "../types/index.js";
3
+ import { Bytes, parseSizeToBytes } from "./bytes.js";
3
4
  import { Duration } from "./duration.js";
4
5
  import { BZZ } from "./tokens.js";
5
6
  import { asNumberString } from "./type.js";
@@ -37,24 +38,33 @@ const effectiveSizeBreakpoints = [[17, 0.00004089], [18, 0.00609], [19, 0.10249]
37
38
  *
38
39
  * @returns {number} The effective size of the postage batch in bytes.
39
40
  */
40
- export function getStampEffectiveBytes(depth) {
41
+ export function getStampEffectiveBytes(depth, encryption, erasureCodeLevel) {
41
42
  if (depth < 17) {
42
43
  return 0;
43
44
  }
44
- const breakpoint = effectiveSizeBreakpoints.find(([d, size]) => {
45
- if (depth === d) {
46
- return size;
45
+ if (encryption !== undefined && erasureCodeLevel !== undefined) {
46
+ const encryptionKey = encryption ? 'ENCRYPTION_ON' : 'ENCRYPTION_OFF';
47
+ const breakpoints = capacityBreakpoints[encryptionKey][erasureCodeLevel];
48
+ const entry = breakpoints.find(item => item.batchDepth === depth);
49
+ if (entry?.effectiveVolume) {
50
+ return parseSizeToBytes(entry.effectiveVolume);
51
+ }
52
+ } else {
53
+ const breakpoint = effectiveSizeBreakpoints.find(([d, size]) => {
54
+ if (depth === d) {
55
+ return size;
56
+ }
57
+ });
58
+ if (breakpoint) {
59
+ return breakpoint[1] * 1000 * 1000 * 1000;
47
60
  }
48
- });
49
- if (breakpoint) {
50
- return breakpoint[1] * 1000 * 1000 * 1000;
51
61
  }
52
62
  return Math.ceil(getStampTheoreticalBytes(depth) * MAX_UTILIZATION);
53
63
  }
54
- export function getStampEffectiveBytesBreakpoints() {
64
+ export function getStampEffectiveBytesBreakpoints(encryption, erasureCodeLevel) {
55
65
  const map = new Map();
56
66
  for (let i = 17; i < 35; i++) {
57
- map.set(i, getStampEffectiveBytes(i));
67
+ map.set(i, getStampEffectiveBytes(i, encryption, erasureCodeLevel));
58
68
  }
59
69
  return map;
60
70
  }
@@ -91,10 +101,21 @@ export function getAmountForDuration(duration, pricePerBlock, blockTime) {
91
101
  * @param size The effective size of the postage batch
92
102
  * @returns
93
103
  */
94
- export function getDepthForSize(size) {
95
- for (const [depth, sizeBreakpoint] of effectiveSizeBreakpoints) {
96
- if (size.toBytes() <= sizeBreakpoint * 1000 * 1000 * 1000) {
97
- return depth;
104
+ export function getDepthForSize(size, encryption, erasureCodeLevel) {
105
+ if (encryption !== undefined && erasureCodeLevel !== undefined) {
106
+ const encryptionKey = encryption ? 'ENCRYPTION_ON' : 'ENCRYPTION_OFF';
107
+ const breakpoints = capacityBreakpoints[encryptionKey][erasureCodeLevel];
108
+ const entry = breakpoints.find(item => {
109
+ return size.toBytes() <= parseSizeToBytes(item.effectiveVolume);
110
+ });
111
+ if (entry?.effectiveVolume) {
112
+ return entry.batchDepth;
113
+ }
114
+ } else {
115
+ for (const [depth, sizeBreakpoint] of effectiveSizeBreakpoints) {
116
+ if (size.toBytes() <= sizeBreakpoint * 1000 * 1000 * 1000) {
117
+ return depth;
118
+ }
98
119
  }
99
120
  }
100
121
  return 35;
@@ -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
  }