@ethersphere/bee-js 8.3.1 → 9.0.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.
Files changed (224) hide show
  1. package/README.md +192 -50
  2. package/dist/cjs/bee-dev.js +78 -0
  3. package/dist/cjs/bee.js +341 -417
  4. package/dist/cjs/chunk/bmt.js +15 -32
  5. package/dist/cjs/chunk/cac.js +26 -36
  6. package/dist/cjs/chunk/soc.js +56 -51
  7. package/dist/cjs/feed/identifier.js +3 -28
  8. package/dist/cjs/feed/index.js +95 -44
  9. package/dist/cjs/feed/retrievable.js +11 -22
  10. package/dist/cjs/index.js +17 -1
  11. package/dist/cjs/manifest/manifest.js +369 -0
  12. package/dist/cjs/modules/bytes.js +24 -12
  13. package/dist/cjs/modules/bzz.js +24 -34
  14. package/dist/cjs/modules/chunk.js +13 -7
  15. package/dist/cjs/modules/debug/balance.js +29 -4
  16. package/dist/cjs/modules/debug/chequebook.js +60 -16
  17. package/dist/cjs/modules/debug/connectivity.js +82 -4
  18. package/dist/cjs/modules/debug/settlements.js +22 -2
  19. package/dist/cjs/modules/debug/stake.js +29 -11
  20. package/dist/cjs/modules/debug/stamps.js +88 -7
  21. package/dist/cjs/modules/debug/states.js +25 -3
  22. package/dist/cjs/modules/debug/status.js +45 -47
  23. package/dist/cjs/modules/debug/transactions.js +27 -5
  24. package/dist/cjs/modules/envelope.js +8 -6
  25. package/dist/cjs/modules/feed.js +25 -10
  26. package/dist/cjs/modules/grantee.js +18 -12
  27. package/dist/cjs/modules/gsoc.js +24 -0
  28. package/dist/cjs/modules/pinning.js +13 -2
  29. package/dist/cjs/modules/pss.js +9 -3
  30. package/dist/cjs/modules/soc.js +9 -4
  31. package/dist/cjs/modules/stewardship.js +7 -3
  32. package/dist/cjs/modules/tag.js +35 -3
  33. package/dist/cjs/package.json +1 -0
  34. package/dist/cjs/stamper/stamper.js +43 -0
  35. package/dist/cjs/types/debug.js +16 -1
  36. package/dist/cjs/types/index.js +2 -24
  37. package/dist/cjs/utils/bytes.js +67 -115
  38. package/dist/cjs/utils/chunk-size.js +17 -0
  39. package/dist/cjs/utils/chunk-stream.browser.js +85 -0
  40. package/dist/cjs/utils/chunk-stream.js +84 -0
  41. package/dist/cjs/utils/cid.js +5 -6
  42. package/dist/cjs/utils/collection.browser.js +2 -2
  43. package/dist/cjs/utils/collection.js +3 -2
  44. package/dist/cjs/utils/collection.node.js +0 -2
  45. package/dist/cjs/utils/constants.js +7 -3
  46. package/dist/cjs/utils/data.browser.js +6 -3
  47. package/dist/cjs/utils/data.js +8 -4
  48. package/dist/cjs/utils/duration.js +51 -0
  49. package/dist/cjs/utils/expose.js +11 -40
  50. package/dist/cjs/utils/headers.js +79 -52
  51. package/dist/cjs/utils/http.js +34 -10
  52. package/dist/cjs/utils/mime.js +78 -0
  53. package/dist/cjs/utils/pss.js +3 -4
  54. package/dist/cjs/utils/redundancy.js +18 -14
  55. package/dist/cjs/utils/resource-locator.js +17 -0
  56. package/dist/cjs/utils/size.js +35 -0
  57. package/dist/cjs/utils/stamps.js +67 -51
  58. package/dist/cjs/utils/tar-uploader.browser.js +2 -2
  59. package/dist/cjs/utils/tar-uploader.js +2 -2
  60. package/dist/cjs/utils/tokens.js +144 -0
  61. package/dist/cjs/utils/type.js +142 -350
  62. package/dist/cjs/utils/typed-bytes.js +179 -0
  63. package/dist/cjs/utils/upload-progress.js +2 -0
  64. package/dist/cjs/utils/url.js +0 -4
  65. package/dist/cjs/utils/workaround.js +27 -0
  66. package/dist/index.browser.min.js +1 -2
  67. package/dist/index.browser.min.js.map +1 -1
  68. package/dist/mjs/bee-dev.js +98 -0
  69. package/dist/mjs/bee.js +367 -411
  70. package/dist/mjs/chunk/bmt.js +13 -29
  71. package/dist/mjs/chunk/cac.js +26 -34
  72. package/dist/mjs/chunk/soc.js +57 -53
  73. package/dist/mjs/feed/identifier.js +3 -26
  74. package/dist/mjs/feed/index.js +98 -51
  75. package/dist/mjs/feed/retrievable.js +12 -23
  76. package/dist/mjs/index.js +10 -1
  77. package/dist/mjs/manifest/manifest.js +371 -0
  78. package/dist/mjs/modules/bytes.js +27 -15
  79. package/dist/mjs/modules/bzz.js +25 -32
  80. package/dist/mjs/modules/chunk.js +16 -8
  81. package/dist/mjs/modules/debug/balance.js +61 -4
  82. package/dist/mjs/modules/debug/chequebook.js +116 -16
  83. package/dist/mjs/modules/debug/connectivity.js +144 -3
  84. package/dist/mjs/modules/debug/settlements.js +46 -2
  85. package/dist/mjs/modules/debug/stake.js +69 -11
  86. package/dist/mjs/modules/debug/stamps.js +184 -7
  87. package/dist/mjs/modules/debug/states.js +55 -3
  88. package/dist/mjs/modules/debug/status.js +97 -45
  89. package/dist/mjs/modules/debug/transactions.js +61 -5
  90. package/dist/mjs/modules/envelope.js +11 -9
  91. package/dist/mjs/modules/feed.js +26 -10
  92. package/dist/mjs/modules/grantee.js +35 -13
  93. package/dist/mjs/modules/gsoc.js +16 -0
  94. package/dist/mjs/modules/pinning.js +23 -2
  95. package/dist/mjs/modules/pss.js +10 -4
  96. package/dist/mjs/modules/soc.js +10 -5
  97. package/dist/mjs/modules/stewardship.js +14 -4
  98. package/dist/mjs/modules/tag.js +93 -3
  99. package/dist/mjs/package.json +2 -1
  100. package/dist/mjs/stamper/stamper.js +39 -0
  101. package/dist/mjs/types/debug.js +15 -1
  102. package/dist/mjs/types/index.js +2 -24
  103. package/dist/mjs/utils/bytes.js +63 -104
  104. package/dist/mjs/utils/chunk-size.js +13 -0
  105. package/dist/mjs/utils/chunk-stream.browser.js +85 -0
  106. package/dist/mjs/utils/chunk-stream.js +87 -0
  107. package/dist/mjs/utils/cid.js +5 -6
  108. package/dist/mjs/utils/collection.browser.js +2 -2
  109. package/dist/mjs/utils/collection.js +2 -2
  110. package/dist/mjs/utils/collection.node.js +0 -2
  111. package/dist/mjs/utils/constants.js +6 -2
  112. package/dist/mjs/utils/data.browser.js +9 -3
  113. package/dist/mjs/utils/data.js +12 -4
  114. package/dist/mjs/utils/duration.js +47 -0
  115. package/dist/mjs/utils/expose.js +2 -7
  116. package/dist/mjs/utils/headers.js +73 -48
  117. package/dist/mjs/utils/http.js +33 -11
  118. package/dist/mjs/utils/mime.js +75 -0
  119. package/dist/mjs/utils/pss.js +3 -4
  120. package/dist/mjs/utils/redundancy.js +18 -8
  121. package/dist/mjs/utils/resource-locator.js +13 -0
  122. package/dist/mjs/utils/size.js +31 -0
  123. package/dist/mjs/utils/stamps.js +47 -44
  124. package/dist/mjs/utils/tar-uploader.browser.js +2 -2
  125. package/dist/mjs/utils/tar-uploader.js +2 -2
  126. package/dist/mjs/utils/tokens.js +139 -0
  127. package/dist/mjs/utils/type.js +216 -310
  128. package/dist/mjs/utils/typed-bytes.js +160 -0
  129. package/dist/mjs/utils/upload-progress.js +1 -0
  130. package/dist/mjs/utils/url.js +0 -4
  131. package/dist/mjs/utils/workaround.js +22 -0
  132. package/dist/types/bee-dev.d.ts +5 -0
  133. package/dist/types/bee.d.ts +88 -165
  134. package/dist/types/chunk/bmt.d.ts +2 -2
  135. package/dist/types/chunk/cac.d.ts +7 -24
  136. package/dist/types/chunk/soc.d.ts +15 -15
  137. package/dist/types/feed/identifier.d.ts +2 -4
  138. package/dist/types/feed/index.d.ts +14 -18
  139. package/dist/types/feed/retrievable.d.ts +3 -4
  140. package/dist/types/index.d.ts +28 -1
  141. package/dist/types/manifest/manifest.d.ts +106 -0
  142. package/dist/types/modules/bytes.d.ts +8 -5
  143. package/dist/types/modules/bzz.d.ts +8 -10
  144. package/dist/types/modules/chunk.d.ts +4 -3
  145. package/dist/types/modules/debug/balance.d.ts +3 -2
  146. package/dist/types/modules/debug/chequebook.d.ts +7 -6
  147. package/dist/types/modules/debug/connectivity.d.ts +5 -3
  148. package/dist/types/modules/debug/settlements.d.ts +2 -1
  149. package/dist/types/modules/debug/stake.d.ts +4 -2
  150. package/dist/types/modules/debug/stamps.d.ts +5 -4
  151. package/dist/types/modules/debug/status.d.ts +6 -25
  152. package/dist/types/modules/debug/transactions.d.ts +5 -4
  153. package/dist/types/modules/envelope.d.ts +3 -2
  154. package/dist/types/modules/feed.d.ts +20 -16
  155. package/dist/types/modules/grantee.d.ts +7 -6
  156. package/dist/types/modules/gsoc.d.ts +7 -0
  157. package/dist/types/modules/pinning.d.ts +2 -4
  158. package/dist/types/modules/pss.d.ts +4 -3
  159. package/dist/types/modules/soc.d.ts +3 -2
  160. package/dist/types/modules/stewardship.d.ts +4 -4
  161. package/dist/types/modules/tag.d.ts +2 -1
  162. package/dist/types/stamper/stamper.d.ts +15 -0
  163. package/dist/types/types/debug.d.ts +63 -115
  164. package/dist/types/types/index.d.ts +103 -204
  165. package/dist/types/utils/bytes.d.ts +16 -90
  166. package/dist/types/utils/chunk-size.d.ts +1 -0
  167. package/dist/types/utils/chunk-stream.browser.d.ts +6 -0
  168. package/dist/types/utils/chunk-stream.d.ts +6 -0
  169. package/dist/types/utils/cid.d.ts +3 -2
  170. package/dist/types/utils/collection.browser.d.ts +2 -2
  171. package/dist/types/utils/collection.d.ts +2 -1
  172. package/dist/types/utils/collection.node.d.ts +0 -1
  173. package/dist/types/utils/constants.d.ts +4 -1
  174. package/dist/types/utils/duration.d.ts +17 -0
  175. package/dist/types/utils/error.d.ts +2 -2
  176. package/dist/types/utils/expose.d.ts +2 -7
  177. package/dist/types/utils/headers.d.ts +3 -4
  178. package/dist/types/utils/mime.d.ts +1 -0
  179. package/dist/types/utils/pss.d.ts +2 -2
  180. package/dist/types/utils/resource-locator.d.ts +6 -0
  181. package/dist/types/utils/size.d.ts +16 -0
  182. package/dist/types/utils/stamps.d.ts +27 -33
  183. package/dist/types/utils/tar-uploader.browser.d.ts +3 -4
  184. package/dist/types/utils/tar-uploader.d.ts +3 -4
  185. package/dist/types/utils/tokens.d.ts +77 -0
  186. package/dist/types/utils/type.d.ts +20 -52
  187. package/dist/types/utils/typed-bytes.d.ts +68 -0
  188. package/dist/types/utils/upload-progress.d.ts +4 -0
  189. package/dist/types/utils/workaround.d.ts +2 -0
  190. package/package.json +11 -17
  191. package/dist/cjs/chunk/signer.js +0 -126
  192. package/dist/cjs/chunk/span.js +0 -25
  193. package/dist/cjs/feed/json.js +0 -28
  194. package/dist/cjs/feed/topic.js +0 -25
  195. package/dist/cjs/feed/type.js +0 -15
  196. package/dist/cjs/modules/debug/chunk.js +0 -21
  197. package/dist/cjs/modules/debug/tag.js +0 -19
  198. package/dist/cjs/utils/eth.js +0 -216
  199. package/dist/cjs/utils/hash.js +0 -21
  200. package/dist/cjs/utils/hex.js +0 -150
  201. package/dist/cjs/utils/reference.js +0 -36
  202. package/dist/index.browser.min.js.LICENSE.txt +0 -8
  203. package/dist/mjs/chunk/signer.js +0 -114
  204. package/dist/mjs/chunk/span.js +0 -21
  205. package/dist/mjs/feed/json.js +0 -26
  206. package/dist/mjs/feed/topic.js +0 -19
  207. package/dist/mjs/feed/type.js +0 -10
  208. package/dist/mjs/modules/debug/chunk.js +0 -17
  209. package/dist/mjs/modules/debug/tag.js +0 -15
  210. package/dist/mjs/utils/eth.js +0 -192
  211. package/dist/mjs/utils/hash.js +0 -16
  212. package/dist/mjs/utils/hex.js +0 -135
  213. package/dist/mjs/utils/reference.js +0 -29
  214. package/dist/types/chunk/signer.d.ts +0 -31
  215. package/dist/types/chunk/span.d.ts +0 -10
  216. package/dist/types/feed/json.d.ts +0 -4
  217. package/dist/types/feed/topic.d.ts +0 -3
  218. package/dist/types/feed/type.d.ts +0 -6
  219. package/dist/types/modules/debug/chunk.d.ts +0 -10
  220. package/dist/types/modules/debug/tag.d.ts +0 -8
  221. package/dist/types/utils/eth.d.ts +0 -67
  222. package/dist/types/utils/hash.d.ts +0 -9
  223. package/dist/types/utils/hex.d.ts +0 -86
  224. package/dist/types/utils/reference.d.ts +0 -2
package/dist/cjs/index.js CHANGED
@@ -26,13 +26,29 @@ var __importStar = (this && this.__importStar) || function (mod) {
26
26
  return result;
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.Bee = exports.Utils = exports.SUPPORTED_BEE_VERSION_EXACT = exports.SUPPORTED_BEE_VERSION = void 0;
29
+ exports.Stamper = exports.BeeDev = exports.Bee = exports.Size = exports.Utils = exports.Duration = exports.Bytes = exports.SUPPORTED_BEE_VERSION_EXACT = exports.SUPPORTED_BEE_VERSION = exports.MantarayNode = exports.MerkleTree = void 0;
30
30
  const bee_1 = require("./bee");
31
31
  Object.defineProperty(exports, "Bee", { enumerable: true, get: function () { return bee_1.Bee; } });
32
+ const bee_dev_1 = require("./bee-dev");
33
+ Object.defineProperty(exports, "BeeDev", { enumerable: true, get: function () { return bee_dev_1.BeeDev; } });
34
+ const stamper_1 = require("./stamper/stamper");
35
+ Object.defineProperty(exports, "Stamper", { enumerable: true, get: function () { return stamper_1.Stamper; } });
36
+ var cafe_utility_1 = require("cafe-utility");
37
+ Object.defineProperty(exports, "MerkleTree", { enumerable: true, get: function () { return cafe_utility_1.MerkleTree; } });
38
+ var manifest_1 = require("./manifest/manifest");
39
+ Object.defineProperty(exports, "MantarayNode", { enumerable: true, get: function () { return manifest_1.MantarayNode; } });
32
40
  var status_1 = require("./modules/debug/status");
33
41
  Object.defineProperty(exports, "SUPPORTED_BEE_VERSION", { enumerable: true, get: function () { return status_1.SUPPORTED_BEE_VERSION; } });
34
42
  Object.defineProperty(exports, "SUPPORTED_BEE_VERSION_EXACT", { enumerable: true, get: function () { return status_1.SUPPORTED_BEE_VERSION_EXACT; } });
35
43
  __exportStar(require("./types"), exports);
44
+ var bytes_1 = require("./utils/bytes");
45
+ Object.defineProperty(exports, "Bytes", { enumerable: true, get: function () { return bytes_1.Bytes; } });
36
46
  __exportStar(require("./utils/constants"), exports);
47
+ var duration_1 = require("./utils/duration");
48
+ Object.defineProperty(exports, "Duration", { enumerable: true, get: function () { return duration_1.Duration; } });
37
49
  __exportStar(require("./utils/error"), exports);
38
50
  exports.Utils = __importStar(require("./utils/expose"));
51
+ var size_1 = require("./utils/size");
52
+ Object.defineProperty(exports, "Size", { enumerable: true, get: function () { return size_1.Size; } });
53
+ __exportStar(require("./utils/tokens"), exports);
54
+ __exportStar(require("./utils/typed-bytes"), exports);
@@ -0,0 +1,369 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MantarayNode = exports.Fork = void 0;
4
+ const cafe_utility_1 = require("cafe-utility");
5
+ const __1 = require("..");
6
+ const typed_bytes_1 = require("../utils/typed-bytes");
7
+ const ENCODER = new TextEncoder();
8
+ const DECODER = new TextDecoder();
9
+ const TYPE_VALUE = 2;
10
+ const TYPE_EDGE = 4;
11
+ const TYPE_WITH_PATH_SEPARATOR = 8;
12
+ const TYPE_WITH_METADATA = 16;
13
+ const PATH_SEPARATOR = new Uint8Array([47]);
14
+ const VERSION_02_HASH_HEX = '5768b3b6a7db56d21d1abff40d41cebfc83448fed8d7e9b06ec0d3b073f28f7b';
15
+ const VERSION_02_HASH = cafe_utility_1.Binary.hexToUint8Array(VERSION_02_HASH_HEX);
16
+ class Fork {
17
+ constructor(prefix, node) {
18
+ this.prefix = prefix;
19
+ this.node = node;
20
+ }
21
+ static split(a, b) {
22
+ const commonPart = cafe_utility_1.Binary.commonPrefix(a.prefix, b.prefix);
23
+ const node = new MantarayNode({ path: commonPart });
24
+ const newAFork = new Fork(a.prefix.slice(commonPart.length), a.node);
25
+ const newBFork = new Fork(b.prefix.slice(commonPart.length), b.node);
26
+ a.node.path = a.prefix.slice(commonPart.length);
27
+ b.node.path = b.prefix.slice(commonPart.length);
28
+ a.prefix = a.prefix.slice(commonPart.length);
29
+ b.prefix = b.prefix.slice(commonPart.length);
30
+ node.forks.set(newAFork.prefix[0], newAFork);
31
+ node.forks.set(newBFork.prefix[0], newBFork);
32
+ newAFork.node.parent = node;
33
+ newBFork.node.parent = node;
34
+ return new Fork(commonPart, node);
35
+ }
36
+ marshal() {
37
+ if (!this.node.selfAddress) {
38
+ throw Error('Fork#marshal node.selfAddress is not set');
39
+ }
40
+ const data = [];
41
+ data.push(new Uint8Array([this.node.determineType()]));
42
+ data.push(cafe_utility_1.Binary.numberToUint8(this.prefix.length));
43
+ data.push(this.prefix);
44
+ if (this.prefix.length < 30) {
45
+ data.push(new Uint8Array(30 - this.prefix.length));
46
+ }
47
+ data.push(this.node.selfAddress);
48
+ if (this.node.metadata) {
49
+ const metadataBytes = cafe_utility_1.Binary.padEndToMultiple(new Uint8Array([0x00, 0x00, ...ENCODER.encode(JSON.stringify(this.node.metadata))]), 32, 0x0a);
50
+ const metadataLengthBytes = cafe_utility_1.Binary.numberToUint16(metadataBytes.length - 2, 'BE');
51
+ metadataBytes.set(metadataLengthBytes, 0);
52
+ data.push(metadataBytes);
53
+ }
54
+ return cafe_utility_1.Binary.concatBytes(...data);
55
+ }
56
+ static unmarshal(reader) {
57
+ const type = cafe_utility_1.Binary.uint8ToNumber(reader.read(1));
58
+ const prefixLength = cafe_utility_1.Binary.uint8ToNumber(reader.read(1));
59
+ const prefix = reader.read(prefixLength);
60
+ reader.read(30 - prefixLength);
61
+ const targetAddress = reader.read(32);
62
+ let metadata = undefined;
63
+ if (isType(type, TYPE_WITH_METADATA)) {
64
+ const metadataLength = cafe_utility_1.Binary.uint16ToNumber(reader.read(2), 'BE');
65
+ metadata = JSON.parse(DECODER.decode(reader.read(metadataLength)));
66
+ }
67
+ return new Fork(prefix, new MantarayNode({ targetAddress, metadata, path: prefix }));
68
+ }
69
+ }
70
+ exports.Fork = Fork;
71
+ class MantarayNode {
72
+ constructor(options) {
73
+ this.obfuscationKey = new Uint8Array(32);
74
+ this.selfAddress = null;
75
+ this.targetAddress = new Uint8Array(32);
76
+ this.metadata = null;
77
+ this.path = new Uint8Array(0);
78
+ this.forks = new Map();
79
+ this.parent = null;
80
+ if (options?.targetAddress) {
81
+ this.targetAddress = options.targetAddress;
82
+ }
83
+ if (options?.selfAddress) {
84
+ this.selfAddress = options.selfAddress;
85
+ }
86
+ if (options?.metadata) {
87
+ this.metadata = options.metadata;
88
+ }
89
+ if (options?.obfuscationKey) {
90
+ this.obfuscationKey = options.obfuscationKey;
91
+ }
92
+ if (options?.path) {
93
+ this.path = options.path;
94
+ }
95
+ if (options?.parent) {
96
+ this.parent = options.parent;
97
+ }
98
+ }
99
+ get fullPath() {
100
+ return cafe_utility_1.Binary.concatBytes(this.parent?.fullPath ?? new Uint8Array(0), this.path);
101
+ }
102
+ get fullPathString() {
103
+ return DECODER.decode(this.fullPath);
104
+ }
105
+ /**
106
+ * Returns the metadata at the `/` path to access idiomatic properties.
107
+ */
108
+ getRootMetadata() {
109
+ const node = this.find('/');
110
+ if (node && node.metadata) {
111
+ return cafe_utility_1.Optional.of(node.metadata);
112
+ }
113
+ return cafe_utility_1.Optional.empty();
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
+ if (!owner || !topic) {
139
+ return cafe_utility_1.Optional.empty();
140
+ }
141
+ return cafe_utility_1.Optional.of(await bee.fetchLatestFeedUpdate(topic, owner, requestOptions));
142
+ }
143
+ /**
144
+ * Gets the binary representation of the node.
145
+ */
146
+ async marshal() {
147
+ for (const fork of this.forks.values()) {
148
+ if (!fork.node.selfAddress) {
149
+ fork.node.selfAddress = (await fork.node.calculateSelfAddress()).toUint8Array();
150
+ }
151
+ }
152
+ const header = new Uint8Array(32);
153
+ header.set(VERSION_02_HASH, 0);
154
+ header.set(cafe_utility_1.Binary.equals(this.targetAddress, __1.NULL_ADDRESS) && cafe_utility_1.Binary.equals(this.path, new Uint8Array([47]))
155
+ ? cafe_utility_1.Binary.numberToUint8(0)
156
+ : cafe_utility_1.Binary.numberToUint8(this.targetAddress.length), 31);
157
+ const forkBitmap = new Uint8Array(32);
158
+ for (const fork of this.forks.keys()) {
159
+ cafe_utility_1.Binary.setBit(forkBitmap, fork, 1, 'LE');
160
+ }
161
+ const forks = [];
162
+ for (let i = 0; i < 256; i++) {
163
+ if (cafe_utility_1.Binary.getBit(forkBitmap, i, 'LE')) {
164
+ forks.push(this.forks.get(i).marshal());
165
+ }
166
+ }
167
+ const data = cafe_utility_1.Binary.xorCypher(cafe_utility_1.Binary.concatBytes(header, cafe_utility_1.Binary.equals(this.targetAddress, __1.NULL_ADDRESS) && cafe_utility_1.Binary.equals(this.path, new Uint8Array([47]))
168
+ ? new Uint8Array(0)
169
+ : this.targetAddress, forkBitmap, ...forks), this.obfuscationKey);
170
+ return cafe_utility_1.Binary.concatBytes(this.obfuscationKey, data);
171
+ }
172
+ /**
173
+ * Downloads and unmarshals a MantarayNode from the given reference.
174
+ *
175
+ * Do not forget calling `loadRecursively` on the returned node to load the entire tree.
176
+ */
177
+ static async unmarshal(bee, reference, options, requestOptions) {
178
+ const data = (await bee.downloadData(reference, options, requestOptions)).toUint8Array();
179
+ return this.unmarshalFromData(data);
180
+ }
181
+ /**
182
+ * Unmarshals a MantarayNode from the given data.
183
+ *
184
+ * Do not forget calling `loadRecursively` on the returned node to load the entire tree.
185
+ */
186
+ static unmarshalFromData(data) {
187
+ const obfuscationKey = data.subarray(0, 32);
188
+ const decrypted = cafe_utility_1.Binary.xorCypher(data.subarray(32), obfuscationKey);
189
+ const reader = new cafe_utility_1.Uint8ArrayReader(decrypted);
190
+ const versionHash = reader.read(31);
191
+ if (!cafe_utility_1.Binary.equals(versionHash, VERSION_02_HASH.slice(0, 31))) {
192
+ throw new Error('MantarayNode#unmarshal invalid version hash');
193
+ }
194
+ const targetAddressLength = cafe_utility_1.Binary.uint8ToNumber(reader.read(1));
195
+ const targetAddress = targetAddressLength === 0 ? __1.NULL_ADDRESS : reader.read(targetAddressLength);
196
+ const node = new MantarayNode({ targetAddress, obfuscationKey });
197
+ const forkBitmap = reader.read(32);
198
+ for (let i = 0; i < 256; i++) {
199
+ if (cafe_utility_1.Binary.getBit(forkBitmap, i, 'LE')) {
200
+ const newFork = Fork.unmarshal(reader);
201
+ node.forks.set(i, newFork);
202
+ newFork.node.parent = node;
203
+ }
204
+ }
205
+ return node;
206
+ }
207
+ /**
208
+ * Adds a fork to the node.
209
+ */
210
+ addFork(path, reference, metadata) {
211
+ this.selfAddress = null;
212
+ path = path instanceof Uint8Array ? path : ENCODER.encode(path);
213
+ // TODO: this should not be ignored
214
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
215
+ let tip = this;
216
+ while (path.length) {
217
+ const prefix = path.slice(0, 30);
218
+ path = path.slice(30);
219
+ const isLast = path.length === 0;
220
+ const [bestMatch, matchedPath] = tip.findClosest(prefix);
221
+ const remainingPath = prefix.slice(matchedPath.length);
222
+ if (matchedPath.length) {
223
+ tip = bestMatch;
224
+ }
225
+ if (!remainingPath.length) {
226
+ continue;
227
+ }
228
+ const newFork = new Fork(remainingPath, new MantarayNode({
229
+ targetAddress: isLast ? new typed_bytes_1.Reference(reference).toUint8Array() : undefined,
230
+ metadata: isLast ? metadata : undefined,
231
+ path: remainingPath,
232
+ }));
233
+ const existing = bestMatch.forks.get(remainingPath[0]);
234
+ if (existing) {
235
+ const fork = Fork.split(newFork, existing);
236
+ tip.forks.set(remainingPath[0], fork);
237
+ fork.node.parent = tip;
238
+ tip.selfAddress = null;
239
+ tip = newFork.node;
240
+ }
241
+ else {
242
+ tip.forks.set(remainingPath[0], newFork);
243
+ newFork.node.parent = tip;
244
+ tip.selfAddress = null;
245
+ tip = newFork.node;
246
+ }
247
+ }
248
+ }
249
+ /**
250
+ * Removes a fork from the node.
251
+ */
252
+ removeFork(path) {
253
+ this.selfAddress = null;
254
+ path = path instanceof Uint8Array ? path : ENCODER.encode(path);
255
+ if (path.length === 0) {
256
+ throw Error('MantarayNode#removeFork [path] parameter cannot be empty');
257
+ }
258
+ const match = this.find(path);
259
+ if (!match) {
260
+ throw Error('MantarayNode#removeFork fork not found');
261
+ }
262
+ const [parent, matchedPath] = this.findClosest(path.slice(0, path.length - 1));
263
+ parent.forks.delete(path.slice(matchedPath.length)[0]);
264
+ for (const fork of match.forks.values()) {
265
+ parent.addFork(cafe_utility_1.Binary.concatBytes(match.path, fork.prefix), fork.node.targetAddress, fork.node.metadata);
266
+ }
267
+ }
268
+ /**
269
+ * Calculates the self address of the node.
270
+ */
271
+ async calculateSelfAddress() {
272
+ if (this.selfAddress) {
273
+ return new typed_bytes_1.Reference(this.selfAddress);
274
+ }
275
+ return new typed_bytes_1.Reference((await cafe_utility_1.MerkleTree.root(await this.marshal())).hash());
276
+ }
277
+ /**
278
+ * Saves the node and its children recursively.
279
+ */
280
+ async saveRecursively(bee, postageBatchId, options, requestOptions) {
281
+ for (const fork of this.forks.values()) {
282
+ await fork.node.saveRecursively(bee, postageBatchId, options, requestOptions);
283
+ }
284
+ const result = await bee.uploadData(postageBatchId, await this.marshal(), options, requestOptions);
285
+ this.selfAddress = result.reference.toUint8Array();
286
+ return result;
287
+ }
288
+ /**
289
+ * Loads the node and its children recursively.
290
+ */
291
+ async loadRecursively(bee, options, requestOptions) {
292
+ for (const fork of this.forks.values()) {
293
+ const node = await MantarayNode.unmarshal(bee, fork.node.targetAddress, options, requestOptions);
294
+ fork.node.targetAddress = node.targetAddress;
295
+ fork.node.forks = node.forks;
296
+ fork.node.path = fork.prefix;
297
+ fork.node.parent = this;
298
+ await fork.node.loadRecursively(bee, options, requestOptions);
299
+ }
300
+ }
301
+ /**
302
+ * Finds a node in the tree by its path.
303
+ */
304
+ find(path) {
305
+ const [closest, match] = this.findClosest(path);
306
+ return match.length === path.length ? closest : null;
307
+ }
308
+ /**
309
+ * Finds the closest node in the tree to the given path.
310
+ */
311
+ findClosest(path, current = new Uint8Array()) {
312
+ path = path instanceof Uint8Array ? path : ENCODER.encode(path);
313
+ if (path.length === 0) {
314
+ return [this, current];
315
+ }
316
+ const fork = this.forks.get(path[0]);
317
+ if (fork && cafe_utility_1.Binary.commonPrefix(fork.prefix, path).length === fork.prefix.length) {
318
+ return fork.node.findClosest(path.slice(fork.prefix.length), cafe_utility_1.Binary.concatBytes(current, fork.prefix));
319
+ }
320
+ return [this, current];
321
+ }
322
+ /**
323
+ * Returns an array of all nodes in the tree which have a target address set.
324
+ *
325
+ * Must be called after `loadRecursively`.
326
+ */
327
+ collect(nodes = []) {
328
+ for (const fork of this.forks.values()) {
329
+ if (!cafe_utility_1.Binary.equals(fork.node.targetAddress, __1.NULL_ADDRESS)) {
330
+ nodes.push(fork.node);
331
+ }
332
+ fork.node.collect(nodes);
333
+ }
334
+ return nodes;
335
+ }
336
+ /**
337
+ * Returns a path:reference map of all nodes in the tree which have a target address set.
338
+ *
339
+ * Must be called after `loadRecursively`.
340
+ */
341
+ collectAndMap() {
342
+ const nodes = this.collect();
343
+ const result = {};
344
+ for (const node of nodes) {
345
+ result[node.fullPathString] = new typed_bytes_1.Reference(node.targetAddress).toHex();
346
+ }
347
+ return result;
348
+ }
349
+ determineType() {
350
+ let type = 0;
351
+ if (!cafe_utility_1.Binary.equals(this.targetAddress, __1.NULL_ADDRESS) || cafe_utility_1.Binary.equals(this.path, PATH_SEPARATOR)) {
352
+ type |= TYPE_VALUE;
353
+ }
354
+ if (this.forks.size > 0) {
355
+ type |= TYPE_EDGE;
356
+ }
357
+ if (cafe_utility_1.Binary.indexOf(this.path, PATH_SEPARATOR) !== -1 && !cafe_utility_1.Binary.equals(this.path, PATH_SEPARATOR)) {
358
+ type |= TYPE_WITH_PATH_SEPARATOR;
359
+ }
360
+ if (this.metadata) {
361
+ type |= TYPE_WITH_METADATA;
362
+ }
363
+ return type;
364
+ }
365
+ }
366
+ exports.MantarayNode = MantarayNode;
367
+ function isType(value, type) {
368
+ return (value & type) === type;
369
+ }
@@ -1,10 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.downloadReadable = exports.download = exports.head = exports.upload = void 0;
4
+ const cafe_utility_1 = require("cafe-utility");
4
5
  const bytes_1 = require("../utils/bytes");
5
6
  const headers_1 = require("../utils/headers");
6
7
  const http_1 = require("../utils/http");
7
8
  const type_1 = require("../utils/type");
9
+ const typed_bytes_1 = require("../utils/typed-bytes");
8
10
  const endpoint = 'bytes';
9
11
  /**
10
12
  * Upload data to a Bee node
@@ -22,13 +24,16 @@ async function upload(requestOptions, data, postageBatchId, options) {
22
24
  data,
23
25
  headers: {
24
26
  'content-type': 'application/octet-stream',
25
- ...(0, headers_1.extractRedundantUploadHeaders)(postageBatchId, options),
27
+ ...(0, headers_1.prepareRequestHeaders)(postageBatchId, options),
26
28
  },
27
29
  });
30
+ const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
28
31
  return {
29
- reference: response.data.reference,
32
+ reference: new typed_bytes_1.Reference(cafe_utility_1.Types.asHexString(body.reference)),
30
33
  tagUid: response.headers['swarm-tag'] ? (0, type_1.makeTagUid)(response.headers['swarm-tag']) : undefined,
31
- historyAddress: response.headers['swarm-act-history-address'] || '',
34
+ historyAddress: response.headers['swarm-act-history-address']
35
+ ? cafe_utility_1.Optional.of(new typed_bytes_1.Reference(response.headers['swarm-act-history-address']))
36
+ : cafe_utility_1.Optional.empty(),
32
37
  };
33
38
  }
34
39
  exports.upload = upload;
@@ -38,9 +43,10 @@ exports.upload = upload;
38
43
  * @param requestOptions Options for making requests
39
44
  * @param hash Bee content reference
40
45
  */
41
- async function head(requestOptions, hash) {
46
+ async function head(requestOptions, reference) {
47
+ reference = new typed_bytes_1.Reference(reference);
42
48
  const response = await (0, http_1.http)(requestOptions, {
43
- url: `${endpoint}/${hash}`,
49
+ url: `${endpoint}/${reference}`,
44
50
  method: 'head',
45
51
  responseType: 'json',
46
52
  });
@@ -55,13 +61,16 @@ exports.head = head;
55
61
  * @param requestOptions Options for making requests
56
62
  * @param hash Bee content reference
57
63
  */
58
- async function download(requestOptions, hash, options) {
64
+ async function download(requestOptions, resource, options) {
65
+ if (options) {
66
+ options = (0, type_1.prepareDownloadOptions)(options);
67
+ }
59
68
  const response = await (0, http_1.http)(requestOptions, {
60
69
  responseType: 'arraybuffer',
61
- url: `${endpoint}/${hash}`,
62
- headers: (0, headers_1.extractDownloadHeaders)(options),
70
+ url: `${endpoint}/${resource}`,
71
+ headers: (0, headers_1.prepareRequestHeaders)(null, options),
63
72
  });
64
- return (0, bytes_1.wrapBytesWithHelpers)(new Uint8Array(response.data));
73
+ return new bytes_1.Bytes(response.data);
65
74
  }
66
75
  exports.download = download;
67
76
  /**
@@ -70,11 +79,14 @@ exports.download = download;
70
79
  * @param requestOptions Options for making requests
71
80
  * @param hash Bee content reference
72
81
  */
73
- async function downloadReadable(requestOptions, hash, options) {
82
+ async function downloadReadable(requestOptions, resource, options) {
83
+ if (options) {
84
+ options = (0, type_1.prepareDownloadOptions)(options);
85
+ }
74
86
  const response = await (0, http_1.http)(requestOptions, {
75
87
  responseType: 'stream',
76
- url: `${endpoint}/${hash}`,
77
- headers: (0, headers_1.extractDownloadHeaders)(options),
88
+ url: `${endpoint}/${resource}`,
89
+ headers: (0, headers_1.prepareRequestHeaders)(null, options),
78
90
  });
79
91
  return response.data;
80
92
  }
@@ -1,21 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.uploadCollection = exports.extractCollectionUploadHeaders = exports.downloadFileReadable = exports.downloadFile = exports.uploadFile = void 0;
3
+ exports.uploadCollection = exports.downloadFileReadable = exports.downloadFile = exports.uploadFile = void 0;
4
+ const cafe_utility_1 = require("cafe-utility");
4
5
  const bytes_1 = require("../utils/bytes");
5
6
  const collection_1 = require("../utils/collection");
6
7
  const headers_1 = require("../utils/headers");
7
8
  const http_1 = require("../utils/http");
8
9
  const tar_uploader_1 = require("../utils/tar-uploader");
9
10
  const type_1 = require("../utils/type");
11
+ const typed_bytes_1 = require("../utils/typed-bytes");
10
12
  const bzzEndpoint = 'bzz';
11
- function extractFileUploadHeaders(postageBatchId, options) {
12
- const headers = (0, headers_1.extractRedundantUploadHeaders)(postageBatchId, options);
13
- if (options?.size)
14
- headers['content-length'] = String(options.size);
15
- if (options?.contentType)
16
- headers['content-type'] = options.contentType;
17
- return headers;
18
- }
19
13
  /**
20
14
  * Upload single file
21
15
  *
@@ -36,16 +30,17 @@ async function uploadFile(requestOptions, data, postageBatchId, name, options) {
36
30
  method: 'post',
37
31
  url: bzzEndpoint,
38
32
  data,
39
- headers: {
40
- ...extractFileUploadHeaders(postageBatchId, options),
41
- },
33
+ headers: (0, headers_1.prepareRequestHeaders)(postageBatchId, options),
42
34
  params: { name },
43
35
  responseType: 'json',
44
36
  });
37
+ const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
45
38
  return {
46
- reference: response.data.reference,
39
+ reference: new typed_bytes_1.Reference(cafe_utility_1.Types.asHexString(body.reference)),
47
40
  tagUid: response.headers['swarm-tag'] ? (0, type_1.makeTagUid)(response.headers['swarm-tag']) : undefined,
48
- historyAddress: response.headers['swarm-act-history-address'] || '',
41
+ historyAddress: response.headers['swarm-act-history-address']
42
+ ? cafe_utility_1.Optional.of(new typed_bytes_1.Reference(response.headers['swarm-act-history-address']))
43
+ : cafe_utility_1.Optional.empty(),
49
44
  };
50
45
  }
51
46
  exports.uploadFile = uploadFile;
@@ -56,16 +51,16 @@ exports.uploadFile = uploadFile;
56
51
  * @param hash Bee file or collection hash
57
52
  * @param path If hash is collection then this defines path to a single file in the collection
58
53
  */
59
- async function downloadFile(requestOptions, hash, path = '', options) {
54
+ async function downloadFile(requestOptions, resource, path = '', options) {
60
55
  const response = await (0, http_1.http)(requestOptions, {
61
56
  method: 'GET',
62
57
  responseType: 'arraybuffer',
63
- url: `${bzzEndpoint}/${hash}/${path}`,
64
- headers: (0, headers_1.extractDownloadHeaders)(options),
58
+ url: `${bzzEndpoint}/${resource}/${path}`,
59
+ headers: (0, headers_1.prepareRequestHeaders)(null, options),
65
60
  });
66
61
  const file = {
67
62
  ...(0, headers_1.readFileHeaders)(response.headers),
68
- data: (0, bytes_1.wrapBytesWithHelpers)(new Uint8Array(response.data)),
63
+ data: new bytes_1.Bytes(response.data),
69
64
  };
70
65
  return file;
71
66
  }
@@ -77,12 +72,13 @@ exports.downloadFile = downloadFile;
77
72
  * @param hash Bee file or collection hash
78
73
  * @param path If hash is collection then this defines path to a single file in the collection
79
74
  */
80
- async function downloadFileReadable(requestOptions, hash, path = '', options) {
75
+ async function downloadFileReadable(requestOptions, reference, path = '', options) {
76
+ reference = new typed_bytes_1.Reference(reference);
81
77
  const response = await (0, http_1.http)(requestOptions, {
82
78
  method: 'GET',
83
79
  responseType: 'stream',
84
- url: `${bzzEndpoint}/${hash}/${path}`,
85
- headers: (0, headers_1.extractDownloadHeaders)(options),
80
+ url: `${bzzEndpoint}/${reference}/${path}`,
81
+ headers: (0, headers_1.prepareRequestHeaders)(null, options),
86
82
  });
87
83
  const file = {
88
84
  ...(0, headers_1.readFileHeaders)(response.headers),
@@ -91,17 +87,8 @@ async function downloadFileReadable(requestOptions, hash, path = '', options) {
91
87
  return file;
92
88
  }
93
89
  exports.downloadFileReadable = downloadFileReadable;
94
- function extractCollectionUploadHeaders(postageBatchId, options) {
95
- const headers = (0, headers_1.extractRedundantUploadHeaders)(postageBatchId, options);
96
- if (options?.indexDocument) {
97
- headers['swarm-index-document'] = options.indexDocument;
98
- }
99
- if (options?.errorDocument) {
100
- headers['swarm-error-document'] = options.errorDocument;
101
- }
102
- return headers;
103
- }
104
- exports.extractCollectionUploadHeaders = extractCollectionUploadHeaders;
90
+ /*******************************************************************************************************************/
91
+ // Collections
105
92
  /**
106
93
  * Upload collection
107
94
  * @param requestOptions Options for making requests
@@ -112,10 +99,13 @@ exports.extractCollectionUploadHeaders = extractCollectionUploadHeaders;
112
99
  async function uploadCollection(requestOptions, collection, postageBatchId, options) {
113
100
  (0, collection_1.assertCollection)(collection);
114
101
  const response = await (0, tar_uploader_1.uploadTar)(requestOptions, collection, postageBatchId, options);
102
+ const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
115
103
  return {
116
- reference: response.data.reference,
104
+ reference: new typed_bytes_1.Reference(cafe_utility_1.Types.asHexString(body.reference)),
117
105
  tagUid: response.headers['swarm-tag'] ? (0, type_1.makeTagUid)(response.headers['swarm-tag']) : undefined,
118
- historyAddress: response.headers['swarm-act-history-address'] || '',
106
+ historyAddress: response.headers['swarm-act-history-address']
107
+ ? cafe_utility_1.Optional.of(new typed_bytes_1.Reference(response.headers['swarm-act-history-address']))
108
+ : cafe_utility_1.Optional.empty(),
119
109
  };
120
110
  }
121
111
  exports.uploadCollection = uploadCollection;
@@ -1,10 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.download = exports.upload = void 0;
4
- const bytes_1 = require("../utils/bytes");
4
+ const cafe_utility_1 = require("cafe-utility");
5
5
  const headers_1 = require("../utils/headers");
6
6
  const http_1 = require("../utils/http");
7
7
  const type_1 = require("../utils/type");
8
+ const typed_bytes_1 = require("../utils/typed-bytes");
8
9
  const endpoint = 'chunks';
9
10
  /**
10
11
  * Upload chunk to a Bee node
@@ -25,14 +26,17 @@ async function upload(requestOptions, data, stamp, options) {
25
26
  data,
26
27
  headers: {
27
28
  'content-type': 'application/octet-stream',
28
- ...(0, headers_1.extractUploadHeaders)(stamp, options),
29
+ ...(0, headers_1.prepareRequestHeaders)(stamp, options),
29
30
  },
30
31
  responseType: 'json',
31
32
  });
33
+ const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
32
34
  return {
33
- reference: response.data.reference,
35
+ reference: new typed_bytes_1.Reference(cafe_utility_1.Types.asString(body.reference, { name: 'reference' })),
34
36
  tagUid: response.headers['swarm-tag'] ? (0, type_1.makeTagUid)(response.headers['swarm-tag']) : undefined,
35
- historyAddress: response.headers['swarm-act-history-address'] || '',
37
+ historyAddress: response.headers['swarm-act-history-address']
38
+ ? cafe_utility_1.Optional.of(new typed_bytes_1.Reference(response.headers['swarm-act-history-address']))
39
+ : cafe_utility_1.Optional.empty(),
36
40
  };
37
41
  }
38
42
  exports.upload = upload;
@@ -43,11 +47,13 @@ exports.upload = upload;
43
47
  * @param hash Bee content reference
44
48
  *
45
49
  */
46
- async function download(requestOptions, hash) {
50
+ async function download(requestOptions, reference, options) {
51
+ reference = new typed_bytes_1.Reference(reference);
47
52
  const response = await (0, http_1.http)(requestOptions, {
48
53
  responseType: 'arraybuffer',
49
- url: `${endpoint}/${hash}`,
54
+ url: `${endpoint}/${reference}`,
55
+ headers: (0, headers_1.prepareRequestHeaders)(null, options),
50
56
  });
51
- return (0, bytes_1.wrapBytesWithHelpers)(new Uint8Array(response.data));
57
+ return new Uint8Array(response.data);
52
58
  }
53
59
  exports.download = download;