@ethersphere/bee-js 10.3.0 → 10.4.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.
@@ -2,13 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.diluteBatch = exports.topUpBatch = exports.createPostageBatch = exports.getPostageBatchBuckets = exports.getPostageBatch = exports.getAllPostageBatches = exports.getGlobalPostageBatches = void 0;
4
4
  const cafe_utility_1 = require("cafe-utility");
5
- const duration_1 = require("../../utils/duration");
6
5
  const http_1 = require("../../utils/http");
7
- const size_1 = require("../../utils/size");
8
6
  const stamps_1 = require("../../utils/stamps");
9
7
  const type_1 = require("../../utils/type");
10
8
  const typed_bytes_1 = require("../../utils/typed-bytes");
11
- const workaround_1 = require("../../utils/workaround");
12
9
  const STAMPS_ENDPOINT = 'stamps';
13
10
  const BATCHES_ENDPOINT = 'batches';
14
11
  async function getGlobalPostageBatches(requestOptions) {
@@ -39,40 +36,7 @@ async function getAllPostageBatches(requestOptions) {
39
36
  });
40
37
  const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
41
38
  const stamps = cafe_utility_1.Types.asArray(body.stamps, { name: 'stamps' }).map(x => cafe_utility_1.Types.asObject(x, { name: 'stamp' }));
42
- return stamps.map(x => {
43
- const utilization = cafe_utility_1.Types.asNumber(x.utilization, { name: 'utilization' });
44
- const depth = cafe_utility_1.Types.asNumber(x.depth, { name: 'depth' });
45
- const bucketDepth = cafe_utility_1.Types.asNumber(x.bucketDepth, { name: 'bucketDepth' });
46
- const usage = (0, stamps_1.getStampUsage)(utilization, depth, bucketDepth);
47
- const batchTTL = (0, workaround_1.normalizeBatchTTL)(cafe_utility_1.Types.asNumber(x.batchTTL, { name: 'batchTTL' }));
48
- const duration = duration_1.Duration.fromSeconds(batchTTL);
49
- const effectiveBytes = (0, stamps_1.getStampEffectiveBytes)(depth);
50
- return {
51
- batchID: new typed_bytes_1.BatchId(cafe_utility_1.Types.asString(x.batchID, { name: 'batchID' })),
52
- utilization,
53
- usable: cafe_utility_1.Types.asBoolean(x.usable, { name: 'usable' }),
54
- label: cafe_utility_1.Types.asEmptiableString(x.label, { name: 'label' }),
55
- depth,
56
- amount: (0, type_1.asNumberString)(x.amount, { name: 'amount' }),
57
- bucketDepth,
58
- blockNumber: cafe_utility_1.Types.asNumber(x.blockNumber, { name: 'blockNumber' }),
59
- immutableFlag: cafe_utility_1.Types.asBoolean(x.immutableFlag, { name: 'immutableFlag' }),
60
- usage,
61
- usageText: `${Math.round(usage * 100)}%`,
62
- size: size_1.Size.fromBytes(effectiveBytes),
63
- remainingSize: size_1.Size.fromBytes(Math.ceil(effectiveBytes * (1 - usage))),
64
- theoreticalSize: size_1.Size.fromBytes((0, stamps_1.getStampTheoreticalBytes)(depth)),
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
- },
74
- };
75
- });
39
+ return stamps.map(x => (0, stamps_1.mapPostageBatch)(validateRawPostageBatch(x)));
76
40
  }
77
41
  exports.getAllPostageBatches = getAllPostageBatches;
78
42
  async function getPostageBatch(requestOptions, postageBatchId, encryption, erasureCodeLevel) {
@@ -82,38 +46,7 @@ async function getPostageBatch(requestOptions, postageBatchId, encryption, erasu
82
46
  responseType: 'json',
83
47
  });
84
48
  const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
85
- const utilization = cafe_utility_1.Types.asNumber(body.utilization, { name: 'utilization' });
86
- const depth = cafe_utility_1.Types.asNumber(body.depth, { name: 'depth' });
87
- const bucketDepth = cafe_utility_1.Types.asNumber(body.bucketDepth, { name: 'bucketDepth' });
88
- const usage = (0, stamps_1.getStampUsage)(utilization, depth, bucketDepth);
89
- const batchTTL = (0, workaround_1.normalizeBatchTTL)(cafe_utility_1.Types.asNumber(body.batchTTL, { name: 'batchTTL' }));
90
- const duration = duration_1.Duration.fromSeconds(batchTTL);
91
- const effectiveBytes = (0, stamps_1.getStampEffectiveBytes)(depth, encryption, erasureCodeLevel);
92
- return {
93
- batchID: new typed_bytes_1.BatchId(cafe_utility_1.Types.asString(body.batchID, { name: 'batchID' })),
94
- utilization,
95
- usable: cafe_utility_1.Types.asBoolean(body.usable, { name: 'usable' }),
96
- label: cafe_utility_1.Types.asEmptiableString(body.label, { name: 'label' }),
97
- depth,
98
- amount: (0, type_1.asNumberString)(body.amount, { name: 'amount' }),
99
- bucketDepth,
100
- blockNumber: cafe_utility_1.Types.asNumber(body.blockNumber, { name: 'blockNumber' }),
101
- immutableFlag: cafe_utility_1.Types.asBoolean(body.immutableFlag, { name: 'immutableFlag' }),
102
- usage,
103
- usageText: `${Math.round(usage * 100)}%`,
104
- size: size_1.Size.fromBytes(effectiveBytes),
105
- remainingSize: size_1.Size.fromBytes(Math.ceil(effectiveBytes * (1 - usage))),
106
- theoreticalSize: size_1.Size.fromBytes((0, stamps_1.getStampTheoreticalBytes)(depth)),
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
- },
116
- };
49
+ return (0, stamps_1.mapPostageBatch)(validateRawPostageBatch(body), encryption, erasureCodeLevel);
117
50
  }
118
51
  exports.getPostageBatch = getPostageBatch;
119
52
  async function getPostageBatchBuckets(requestOptions, postageBatchId) {
@@ -175,3 +108,17 @@ async function diluteBatch(requestOptions, id, depth) {
175
108
  return new typed_bytes_1.BatchId(cafe_utility_1.Types.asString(body.batchID, { name: 'batchID' }));
176
109
  }
177
110
  exports.diluteBatch = diluteBatch;
111
+ function validateRawPostageBatch(raw) {
112
+ return {
113
+ amount: (0, type_1.asNumberString)(raw.amount, { name: 'amount' }),
114
+ batchID: cafe_utility_1.Types.asString(raw.batchID, { name: 'batchID' }),
115
+ batchTTL: cafe_utility_1.Types.asNumber(raw.batchTTL, { name: 'batchTTL' }),
116
+ bucketDepth: cafe_utility_1.Types.asNumber(raw.bucketDepth, { name: 'bucketDepth' }),
117
+ blockNumber: cafe_utility_1.Types.asNumber(raw.blockNumber, { name: 'blockNumber' }),
118
+ depth: cafe_utility_1.Types.asNumber(raw.depth, { name: 'depth' }),
119
+ immutableFlag: cafe_utility_1.Types.asBoolean(raw.immutableFlag, { name: 'immutableFlag' }),
120
+ label: cafe_utility_1.Types.asEmptiableString(raw.label, { name: 'label' }),
121
+ usable: cafe_utility_1.Types.asBoolean(raw.usable, { name: 'usable' }),
122
+ utilization: cafe_utility_1.Types.asNumber(raw.utilization, { name: 'utilization' }),
123
+ };
124
+ }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getStampUsage = exports.getStampTheoreticalBytes = exports.getStampEffectiveBytesBreakpoints = exports.getStampEffectiveBytes = exports.getStampDuration = exports.getStampCost = exports.getDepthForSize = exports.getAmountForDuration = exports.getRedundancyStats = exports.getRedundancyStat = exports.approximateOverheadForRedundancyLevel = exports.makeMaxTarget = exports.getFolderSize = exports.makeCollectionFromFileList = exports.getCollectionSize = void 0;
3
+ exports.unmapPostageBatch = exports.mapPostageBatch = exports.getStampUsage = exports.getStampTheoreticalBytes = exports.getStampEffectiveBytesBreakpoints = exports.getStampEffectiveBytes = exports.getStampDuration = exports.getStampCost = exports.getDepthForSize = exports.getAmountForDuration = exports.getRedundancyStats = exports.getRedundancyStat = exports.approximateOverheadForRedundancyLevel = exports.makeMaxTarget = exports.getFolderSize = exports.makeCollectionFromFileList = exports.getCollectionSize = void 0;
4
4
  var collection_1 = require("./collection");
5
5
  Object.defineProperty(exports, "getCollectionSize", { enumerable: true, get: function () { return collection_1.getCollectionSize; } });
6
6
  Object.defineProperty(exports, "makeCollectionFromFileList", { enumerable: true, get: function () { return collection_1.makeCollectionFromFileList; } });
@@ -21,3 +21,5 @@ Object.defineProperty(exports, "getStampEffectiveBytes", { enumerable: true, get
21
21
  Object.defineProperty(exports, "getStampEffectiveBytesBreakpoints", { enumerable: true, get: function () { return stamps_1.getStampEffectiveBytesBreakpoints; } });
22
22
  Object.defineProperty(exports, "getStampTheoreticalBytes", { enumerable: true, get: function () { return stamps_1.getStampTheoreticalBytes; } });
23
23
  Object.defineProperty(exports, "getStampUsage", { enumerable: true, get: function () { return stamps_1.getStampUsage; } });
24
+ Object.defineProperty(exports, "mapPostageBatch", { enumerable: true, get: function () { return stamps_1.mapPostageBatch; } });
25
+ Object.defineProperty(exports, "unmapPostageBatch", { enumerable: true, get: function () { return stamps_1.unmapPostageBatch; } });
@@ -1,12 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.marshalStamp = exports.convertEnvelopeToMarshaledStamp = exports.getDepthForSize = exports.getAmountForDuration = exports.getStampDuration = exports.getStampCost = exports.getStampEffectiveBytesBreakpoints = exports.getStampEffectiveBytes = exports.getStampTheoreticalBytes = exports.getStampUsage = void 0;
3
+ exports.unmapPostageBatch = exports.mapPostageBatch = exports.marshalStamp = exports.convertEnvelopeToMarshaledStamp = exports.getDepthForSize = exports.getAmountForDuration = exports.getStampDuration = exports.getStampCost = exports.getStampEffectiveBytesBreakpoints = exports.getStampEffectiveBytes = exports.getStampTheoreticalBytes = exports.getStampUsage = void 0;
4
4
  const cafe_utility_1 = require("cafe-utility");
5
5
  const types_1 = require("../types");
6
6
  const bytes_1 = require("./bytes");
7
7
  const duration_1 = require("./duration");
8
+ const size_1 = require("./size");
8
9
  const tokens_1 = require("./tokens");
9
10
  const type_1 = require("./type");
11
+ const typed_bytes_1 = require("./typed-bytes");
12
+ const workaround_1 = require("./workaround");
10
13
  const MAX_UTILIZATION = 0.9;
11
14
  /**
12
15
  * Utility function that calculates usage of postage batch based on its utilization, depth and bucket depth.
@@ -172,3 +175,50 @@ function marshalStamp(signature, batchId, timestamp, index) {
172
175
  return new bytes_1.Bytes(cafe_utility_1.Binary.concatBytes(batchId, index, timestamp, signature));
173
176
  }
174
177
  exports.marshalStamp = marshalStamp;
178
+ function mapPostageBatch(raw, encryption, erasureCodeLevel) {
179
+ const usage = getStampUsage(raw.utilization, raw.depth, raw.bucketDepth);
180
+ const batchTTL = (0, workaround_1.normalizeBatchTTL)(raw.batchTTL);
181
+ const duration = duration_1.Duration.fromSeconds(batchTTL);
182
+ const effectiveBytes = getStampEffectiveBytes(raw.depth, encryption, erasureCodeLevel);
183
+ return {
184
+ batchID: new typed_bytes_1.BatchId(raw.batchID),
185
+ utilization: raw.utilization,
186
+ usable: raw.usable,
187
+ label: raw.label,
188
+ depth: raw.depth,
189
+ amount: (0, type_1.asNumberString)(raw.amount),
190
+ bucketDepth: raw.bucketDepth,
191
+ blockNumber: raw.blockNumber,
192
+ immutableFlag: raw.immutableFlag,
193
+ usage,
194
+ usageText: `${Math.round(usage * 100)}%`,
195
+ size: size_1.Size.fromBytes(effectiveBytes),
196
+ remainingSize: size_1.Size.fromBytes(Math.ceil(effectiveBytes * (1 - usage))),
197
+ theoreticalSize: size_1.Size.fromBytes(getStampTheoreticalBytes(raw.depth)),
198
+ duration,
199
+ calculateSize(encryption, redundancyLevel) {
200
+ const effectiveBytes = getStampEffectiveBytes(raw.depth, encryption, redundancyLevel);
201
+ return size_1.Size.fromBytes(effectiveBytes);
202
+ },
203
+ calculateRemainingSize(encryption, redundancyLevel) {
204
+ const effectiveBytes = getStampEffectiveBytes(raw.depth, encryption, redundancyLevel);
205
+ return size_1.Size.fromBytes(Math.ceil(effectiveBytes * (1 - this.usage)));
206
+ },
207
+ };
208
+ }
209
+ exports.mapPostageBatch = mapPostageBatch;
210
+ function unmapPostageBatch(batch) {
211
+ return {
212
+ batchID: batch.batchID.toHex(),
213
+ utilization: batch.utilization,
214
+ usable: batch.usable,
215
+ label: batch.label,
216
+ depth: batch.depth,
217
+ amount: batch.amount,
218
+ bucketDepth: batch.bucketDepth,
219
+ blockNumber: batch.blockNumber,
220
+ immutableFlag: batch.immutableFlag,
221
+ batchTTL: batch.duration.toSeconds(),
222
+ };
223
+ }
224
+ exports.unmapPostageBatch = unmapPostageBatch;
@@ -9,8 +9,11 @@ function normalizeBatchTTL(batchTTL) {
9
9
  if (batchTTL < 1) {
10
10
  return 1;
11
11
  }
12
- if (batchTTL > 315569260) {
13
- return 315569260;
12
+ // Cap `batchTTL` (represents seconds) to 100 years.
13
+ // We can assume `storagePrice` is invalid (e.g. 1).
14
+ // This is needed to prevent Date objects breaking.
15
+ if (batchTTL > 3155695200) {
16
+ return 3155695200;
14
17
  }
15
18
  return batchTTL;
16
19
  }