@upcoming/bee-js 0.5.4 → 0.6.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.
package/dist/cjs/bee.js CHANGED
@@ -824,6 +824,11 @@ class Bee {
824
824
  }
825
825
  return (0, feed_1.makeFeedWriter)(this.getRequestOptionsForCall(options), topic, signer);
826
826
  }
827
+ async fetchLatestFeedUpdate(topic, owner, requestOptions) {
828
+ topic = new typed_bytes_1.Topic(topic);
829
+ owner = new typed_bytes_1.EthAddress(owner);
830
+ return (0, feed_2.fetchLatestFeedUpdate)(this.getRequestOptionsForCall(requestOptions), owner, topic);
831
+ }
827
832
  /**
828
833
  * Returns an object for reading single owner chunks
829
834
  *
@@ -1293,7 +1298,6 @@ class Bee {
1293
1298
  if (options) {
1294
1299
  options = (0, type_1.prepareTransactionOptions)(options);
1295
1300
  }
1296
- // TODO: Add test for response
1297
1301
  return stake.stake(this.getRequestOptionsForCall(requestOptions), amountString, options);
1298
1302
  }
1299
1303
  /**
@@ -102,6 +102,9 @@ class MantarayNode {
102
102
  get fullPathString() {
103
103
  return DECODER.decode(this.fullPath);
104
104
  }
105
+ /**
106
+ * Returns the metadata at the `/` path to access idiomatic properties.
107
+ */
105
108
  getRootMetadata() {
106
109
  const node = this.find('/');
107
110
  if (node && node.metadata) {
@@ -109,6 +112,34 @@ class MantarayNode {
109
112
  }
110
113
  return cafe_utility_1.Optional.empty();
111
114
  }
115
+ /**
116
+ * Returns the `swarm-index-document` and `swarm-error-document` metadata values.
117
+ */
118
+ getDocsMetadata() {
119
+ const node = this.find('/');
120
+ if (!node || !node.metadata) {
121
+ return { indexDocument: null, errorDocument: null };
122
+ }
123
+ return {
124
+ indexDocument: node.metadata['website-index-document'] ?? null,
125
+ errorDocument: node.metadata['website-error-document'] ?? null,
126
+ };
127
+ }
128
+ /**
129
+ * Attempts to resolve the manifest as a feed, returning the latest update.
130
+ */
131
+ async resolveFeed(bee, requestOptions) {
132
+ const node = this.find('/');
133
+ if (!node || !node.metadata) {
134
+ return cafe_utility_1.Optional.empty();
135
+ }
136
+ const owner = node.metadata['swarm-feed-owner'];
137
+ const topic = node.metadata['swarm-feed-topic'];
138
+ return cafe_utility_1.Optional.of(await bee.fetchLatestFeedUpdate(topic, owner, requestOptions));
139
+ }
140
+ /**
141
+ * Gets the binary representation of the node.
142
+ */
112
143
  async marshal() {
113
144
  for (const fork of this.forks.values()) {
114
145
  if (!fork.node.selfAddress) {
@@ -135,8 +166,21 @@ class MantarayNode {
135
166
  : this.targetAddress, forkBitmap, ...forks), this.obfuscationKey);
136
167
  return cafe_utility_1.Binary.concatBytes(this.obfuscationKey, data);
137
168
  }
169
+ /**
170
+ * Downloads and unmarshals a MantarayNode from the given reference.
171
+ *
172
+ * Do not forget calling `loadRecursively` on the returned node to load the entire tree.
173
+ */
138
174
  static async unmarshal(bee, reference, options, requestOptions) {
139
175
  const data = (await bee.downloadData(reference, options, requestOptions)).toUint8Array();
176
+ return this.unmarshalFromData(data);
177
+ }
178
+ /**
179
+ * Unmarshals a MantarayNode from the given data.
180
+ *
181
+ * Do not forget calling `loadRecursively` on the returned node to load the entire tree.
182
+ */
183
+ static unmarshalFromData(data) {
140
184
  const obfuscationKey = data.subarray(0, 32);
141
185
  const decrypted = cafe_utility_1.Binary.xorCypher(data.subarray(32), obfuscationKey);
142
186
  const reader = new cafe_utility_1.Uint8ArrayReader(decrypted);
@@ -157,6 +201,9 @@ class MantarayNode {
157
201
  }
158
202
  return node;
159
203
  }
204
+ /**
205
+ * Adds a fork to the node.
206
+ */
160
207
  addFork(path, reference, metadata) {
161
208
  this.selfAddress = null;
162
209
  path = path instanceof Uint8Array ? path : ENCODER.encode(path);
@@ -196,6 +243,9 @@ class MantarayNode {
196
243
  }
197
244
  }
198
245
  }
246
+ /**
247
+ * Removes a fork from the node.
248
+ */
199
249
  removeFork(path) {
200
250
  this.selfAddress = null;
201
251
  path = path instanceof Uint8Array ? path : ENCODER.encode(path);
@@ -212,12 +262,18 @@ class MantarayNode {
212
262
  parent.addFork(cafe_utility_1.Binary.concatBytes(match.path, fork.prefix), fork.node.targetAddress, fork.node.metadata);
213
263
  }
214
264
  }
265
+ /**
266
+ * Calculates the self address of the node.
267
+ */
215
268
  async calculateSelfAddress() {
216
269
  if (this.selfAddress) {
217
270
  return new typed_bytes_1.Reference(this.selfAddress);
218
271
  }
219
272
  return new typed_bytes_1.Reference((await cafe_utility_1.MerkleTree.root(await this.marshal())).hash());
220
273
  }
274
+ /**
275
+ * Saves the node and its children recursively.
276
+ */
221
277
  async saveRecursively(bee, postageBatchId, options, requestOptions) {
222
278
  for (const fork of this.forks.values()) {
223
279
  await fork.node.saveRecursively(bee, postageBatchId, options, requestOptions);
@@ -226,6 +282,9 @@ class MantarayNode {
226
282
  this.selfAddress = result.reference.toUint8Array();
227
283
  return result;
228
284
  }
285
+ /**
286
+ * Loads the node and its children recursively.
287
+ */
229
288
  async loadRecursively(bee, options, requestOptions) {
230
289
  for (const fork of this.forks.values()) {
231
290
  const node = await MantarayNode.unmarshal(bee, fork.node.targetAddress, options, requestOptions);
@@ -236,10 +295,16 @@ class MantarayNode {
236
295
  await fork.node.loadRecursively(bee, options, requestOptions);
237
296
  }
238
297
  }
298
+ /**
299
+ * Finds a node in the tree by its path.
300
+ */
239
301
  find(path) {
240
302
  const [closest, match] = this.findClosest(path);
241
303
  return match.length === path.length ? closest : null;
242
304
  }
305
+ /**
306
+ * Finds the closest node in the tree to the given path.
307
+ */
243
308
  findClosest(path, current = new Uint8Array()) {
244
309
  path = path instanceof Uint8Array ? path : ENCODER.encode(path);
245
310
  if (path.length === 0) {
@@ -251,6 +316,11 @@ class MantarayNode {
251
316
  }
252
317
  return [this, current];
253
318
  }
319
+ /**
320
+ * Returns an array of all nodes in the tree which have a target address set.
321
+ *
322
+ * Must be called after `loadRecursively`.
323
+ */
254
324
  collect(nodes = []) {
255
325
  for (const fork of this.forks.values()) {
256
326
  if (!cafe_utility_1.Binary.equals(fork.node.targetAddress, __1.NULL_ADDRESS)) {
@@ -260,6 +330,19 @@ class MantarayNode {
260
330
  }
261
331
  return nodes;
262
332
  }
333
+ /**
334
+ * Returns a path:reference map of all nodes in the tree which have a target address set.
335
+ *
336
+ * Must be called after `loadRecursively`.
337
+ */
338
+ collectAndMap() {
339
+ const nodes = this.collect();
340
+ const result = {};
341
+ for (const node of nodes) {
342
+ result[node.fullPathString] = new typed_bytes_1.Reference(node.targetAddress).toHex();
343
+ }
344
+ return result;
345
+ }
263
346
  determineType() {
264
347
  let type = 0;
265
348
  if (!cafe_utility_1.Binary.equals(this.targetAddress, __1.NULL_ADDRESS) || cafe_utility_1.Binary.equals(this.path, PATH_SEPARATOR)) {
@@ -43,7 +43,7 @@ async function getChainState(requestOptions) {
43
43
  block: cafe_utility_1.Types.asNumber(body.block, { name: 'block' }),
44
44
  chainTip: cafe_utility_1.Types.asNumber(body.chainTip, { name: 'chainTip' }),
45
45
  totalAmount: (0, type_1.asNumberString)(body.totalAmount, { name: 'totalAmount' }),
46
- currentPrice: (0, type_1.asNumberString)(body.currentPrice, { name: 'currentPrice' }),
46
+ currentPrice: cafe_utility_1.Types.asNumber(body.currentPrice, { name: 'currentPrice' }),
47
47
  };
48
48
  }
49
49
  exports.getChainState = getChainState;
@@ -52,19 +52,19 @@ class BZZ {
52
52
  return new BZZ(this.state.divide(other)[0]);
53
53
  }
54
54
  gt(other) {
55
- return this.state.value > (other instanceof BZZ ? other.state : new cafe_utility_1.FixedPointNumber(other, BZZ.DIGITS)).value;
55
+ return this.state.compare(other.state) === 1;
56
56
  }
57
57
  gte(other) {
58
- return this.state.value >= (other instanceof BZZ ? other.state : new cafe_utility_1.FixedPointNumber(other, BZZ.DIGITS)).value;
58
+ return this.state.compare(other.state) !== -1;
59
59
  }
60
60
  lt(other) {
61
- return this.state.value < (other instanceof BZZ ? other.state : new cafe_utility_1.FixedPointNumber(other, BZZ.DIGITS)).value;
61
+ return this.state.compare(other.state) === -1;
62
62
  }
63
63
  lte(other) {
64
- return this.state.value <= (other instanceof BZZ ? other.state : new cafe_utility_1.FixedPointNumber(other, BZZ.DIGITS)).value;
64
+ return this.state.compare(other.state) !== 1;
65
65
  }
66
66
  eq(other) {
67
- return this.state.value === (other instanceof BZZ ? other.state : new cafe_utility_1.FixedPointNumber(other, BZZ.DIGITS)).value;
67
+ return this.state.compare(other.state) === 0;
68
68
  }
69
69
  }
70
70
  exports.BZZ = BZZ;
@@ -119,19 +119,19 @@ class DAI {
119
119
  return new DAI(this.state.divide(other)[0]);
120
120
  }
121
121
  gt(other) {
122
- return this.state.value > (other instanceof DAI ? other.state : new cafe_utility_1.FixedPointNumber(other, DAI.DIGITS)).value;
122
+ return this.state.compare(other.state) === 1;
123
123
  }
124
124
  gte(other) {
125
- return this.state.value >= (other instanceof DAI ? other.state : new cafe_utility_1.FixedPointNumber(other, DAI.DIGITS)).value;
125
+ return this.state.compare(other.state) !== -1;
126
126
  }
127
127
  lt(other) {
128
- return this.state.value < (other instanceof DAI ? other.state : new cafe_utility_1.FixedPointNumber(other, DAI.DIGITS)).value;
128
+ return this.state.compare(other.state) === -1;
129
129
  }
130
130
  lte(other) {
131
- return this.state.value <= (other instanceof DAI ? other.state : new cafe_utility_1.FixedPointNumber(other, DAI.DIGITS)).value;
131
+ return this.state.compare(other.state) !== 1;
132
132
  }
133
133
  eq(other) {
134
- return this.state.value === (other instanceof DAI ? other.state : new cafe_utility_1.FixedPointNumber(other, DAI.DIGITS)).value;
134
+ return this.state.compare(other.state) === 0;
135
135
  }
136
136
  }
137
137
  exports.DAI = DAI;