@ethersphere/bee-js 9.1.0 → 9.1.1

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,6 +20,22 @@ class Fork {
20
20
  }
21
21
  static split(a, b) {
22
22
  const commonPart = cafe_utility_1.Binary.commonPrefix(a.prefix, b.prefix);
23
+ if (commonPart.length === a.prefix.length) {
24
+ const remainingB = b.prefix.slice(commonPart.length);
25
+ b.node.path = b.prefix.slice(commonPart.length);
26
+ b.prefix = b.prefix.slice(commonPart.length);
27
+ b.node.parent = a.node;
28
+ a.node.forks.set(remainingB[0], b);
29
+ return a;
30
+ }
31
+ if (commonPart.length === b.prefix.length) {
32
+ const remainingA = a.prefix.slice(commonPart.length);
33
+ a.node.path = a.prefix.slice(commonPart.length);
34
+ a.prefix = a.prefix.slice(commonPart.length);
35
+ a.node.parent = b.node;
36
+ b.node.forks.set(remainingA[0], a);
37
+ return b;
38
+ }
23
39
  const node = new MantarayNode({ path: commonPart });
24
40
  const newAFork = new Fork(a.prefix.slice(commonPart.length), a.node);
25
41
  const newBFork = new Fork(b.prefix.slice(commonPart.length), b.node);
@@ -58,13 +74,13 @@ class Fork {
58
74
  const prefixLength = cafe_utility_1.Binary.uint8ToNumber(reader.read(1));
59
75
  const prefix = reader.read(prefixLength);
60
76
  reader.read(30 - prefixLength);
61
- const targetAddress = reader.read(32);
77
+ const selfAddress = reader.read(32);
62
78
  let metadata = undefined;
63
79
  if (isType(type, TYPE_WITH_METADATA)) {
64
80
  const metadataLength = cafe_utility_1.Binary.uint16ToNumber(reader.read(2), 'BE');
65
81
  metadata = JSON.parse(DECODER.decode(reader.read(metadataLength)));
66
82
  }
67
- return new Fork(prefix, new MantarayNode({ targetAddress, metadata, path: prefix }));
83
+ return new Fork(prefix, new MantarayNode({ selfAddress, metadata, path: prefix }));
68
84
  }
69
85
  }
70
86
  exports.Fork = Fork;
@@ -175,15 +191,16 @@ class MantarayNode {
175
191
  * Do not forget calling `loadRecursively` on the returned node to load the entire tree.
176
192
  */
177
193
  static async unmarshal(bee, reference, options, requestOptions) {
194
+ reference = new typed_bytes_1.Reference(reference);
178
195
  const data = (await bee.downloadData(reference, options, requestOptions)).toUint8Array();
179
- return this.unmarshalFromData(data);
196
+ return this.unmarshalFromData(data, reference.toUint8Array());
180
197
  }
181
198
  /**
182
199
  * Unmarshals a MantarayNode from the given data.
183
200
  *
184
201
  * Do not forget calling `loadRecursively` on the returned node to load the entire tree.
185
202
  */
186
- static unmarshalFromData(data) {
203
+ static unmarshalFromData(data, selfAddress) {
187
204
  const obfuscationKey = data.subarray(0, 32);
188
205
  const decrypted = cafe_utility_1.Binary.xorCypher(data.subarray(32), obfuscationKey);
189
206
  const reader = new cafe_utility_1.Uint8ArrayReader(decrypted);
@@ -193,7 +210,7 @@ class MantarayNode {
193
210
  }
194
211
  const targetAddressLength = cafe_utility_1.Binary.uint8ToNumber(reader.read(1));
195
212
  const targetAddress = targetAddressLength === 0 ? __1.NULL_ADDRESS : reader.read(targetAddressLength);
196
- const node = new MantarayNode({ targetAddress, obfuscationKey });
213
+ const node = new MantarayNode({ selfAddress, targetAddress, obfuscationKey });
197
214
  const forkBitmap = reader.read(32);
198
215
  for (let i = 0; i < 256; i++) {
199
216
  if (cafe_utility_1.Binary.getBit(forkBitmap, i, 'LE')) {
@@ -290,7 +307,10 @@ class MantarayNode {
290
307
  */
291
308
  async loadRecursively(bee, options, requestOptions) {
292
309
  for (const fork of this.forks.values()) {
293
- const node = await MantarayNode.unmarshal(bee, fork.node.targetAddress, options, requestOptions);
310
+ if (!fork.node.selfAddress) {
311
+ throw Error('MantarayNode#loadRecursively fork.node.selfAddress is not set');
312
+ }
313
+ const node = await MantarayNode.unmarshal(bee, fork.node.selfAddress, options, requestOptions);
294
314
  fork.node.targetAddress = node.targetAddress;
295
315
  fork.node.forks = node.forks;
296
316
  fork.node.path = fork.prefix;