@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
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Stamper = void 0;
4
+ const cafe_utility_1 = require("cafe-utility");
5
+ const typed_bytes_1 = require("../utils/typed-bytes");
6
+ class Stamper {
7
+ constructor(signer, batchId, buckets, depth) {
8
+ this.signer = new typed_bytes_1.PrivateKey(signer);
9
+ this.batchId = new typed_bytes_1.BatchId(batchId);
10
+ this.buckets = buckets;
11
+ this.depth = depth;
12
+ this.maxSlot = 2 ** (this.depth - 16);
13
+ }
14
+ static fromBlank(signer, batchId, depth) {
15
+ return new Stamper(signer, batchId, new Uint32Array(65536), depth);
16
+ }
17
+ static fromState(signer, batchId, buckets, depth) {
18
+ return new Stamper(signer, batchId, buckets, depth);
19
+ }
20
+ stamp(chunk) {
21
+ const address = chunk.hash();
22
+ const bucket = cafe_utility_1.Binary.uint16ToNumber(address, 'BE');
23
+ const height = this.buckets[bucket];
24
+ if (height >= this.maxSlot) {
25
+ throw Error('Bucket is full');
26
+ }
27
+ this.buckets[bucket]++;
28
+ const index = cafe_utility_1.Binary.concatBytes(cafe_utility_1.Binary.numberToUint32(bucket, 'BE'), cafe_utility_1.Binary.numberToUint32(height, 'BE'));
29
+ const timestamp = cafe_utility_1.Binary.numberToUint64(BigInt(Date.now()), 'BE');
30
+ const signature = this.signer.sign(cafe_utility_1.Binary.concatBytes(address, this.batchId.toUint8Array(), index, timestamp));
31
+ return {
32
+ batchId: this.batchId,
33
+ index,
34
+ issuer: this.signer.publicKey().address().toUint8Array(),
35
+ signature: signature.toUint8Array(),
36
+ timestamp,
37
+ };
38
+ }
39
+ getState() {
40
+ return this.buckets;
41
+ }
42
+ }
43
+ exports.Stamper = Stamper;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BeeModes = void 0;
3
+ exports.toBeeMode = exports.BeeModes = void 0;
4
4
  var BeeModes;
5
5
  (function (BeeModes) {
6
6
  BeeModes["FULL"] = "full";
@@ -8,3 +8,18 @@ var BeeModes;
8
8
  BeeModes["ULTRA_LIGHT"] = "ultra-light";
9
9
  BeeModes["DEV"] = "dev";
10
10
  })(BeeModes = exports.BeeModes || (exports.BeeModes = {}));
11
+ function toBeeMode(value) {
12
+ switch (value) {
13
+ case 'full':
14
+ return BeeModes.FULL;
15
+ case 'light':
16
+ return BeeModes.LIGHT;
17
+ case 'ultra-light':
18
+ return BeeModes.ULTRA_LIGHT;
19
+ case 'dev':
20
+ return BeeModes.DEV;
21
+ default:
22
+ throw new Error(`Unknown Bee mode: ${value}`);
23
+ }
24
+ }
25
+ exports.toBeeMode = toBeeMode;
@@ -14,28 +14,16 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.SIGNATURE_BYTES_LENGTH = exports.SIGNATURE_HEX_LENGTH = exports.TOPIC_HEX_LENGTH = exports.TOPIC_BYTES_LENGTH = exports.RedundancyStrategy = exports.RedundancyLevel = exports.FEED_INDEX_HEX_LENGTH = exports.TAGS_LIMIT_MAX = exports.TAGS_LIMIT_MIN = exports.STAMPS_DEPTH_MAX = exports.STAMPS_AMOUNT_MIN = exports.STAMPS_DEPTH_MIN = exports.ENCRYPTED_REFERENCE_BYTES_LENGTH = exports.REFERENCE_BYTES_LENGTH = exports.ENCRYPTED_REFERENCE_HEX_LENGTH = exports.REFERENCE_HEX_LENGTH = exports.BATCH_ID_HEX_LENGTH = exports.PUBKEY_HEX_LENGTH = exports.PSS_TARGET_HEX_LENGTH_MAX = exports.ADDRESS_HEX_LENGTH = exports.CHUNK_SIZE = exports.BRANCHES = exports.SECTION_SIZE = exports.SPAN_SIZE = void 0;
17
+ exports.RedundancyStrategy = exports.RedundancyLevel = exports.FEED_INDEX_HEX_LENGTH = exports.TAGS_LIMIT_MAX = exports.TAGS_LIMIT_MIN = exports.STAMPS_DEPTH_MAX = exports.STAMPS_DEPTH_MIN = exports.PSS_TARGET_HEX_LENGTH_MAX = exports.CHUNK_SIZE = exports.BRANCHES = exports.SECTION_SIZE = void 0;
18
18
  __exportStar(require("./debug"), exports);
19
- exports.SPAN_SIZE = 8;
20
19
  exports.SECTION_SIZE = 32;
21
20
  exports.BRANCHES = 128;
22
21
  exports.CHUNK_SIZE = exports.SECTION_SIZE * exports.BRANCHES;
23
- exports.ADDRESS_HEX_LENGTH = 64;
24
- exports.PSS_TARGET_HEX_LENGTH_MAX = 6;
25
- exports.PUBKEY_HEX_LENGTH = 66;
26
- exports.BATCH_ID_HEX_LENGTH = 64;
27
- exports.REFERENCE_HEX_LENGTH = 64;
28
- exports.ENCRYPTED_REFERENCE_HEX_LENGTH = 128;
29
- exports.REFERENCE_BYTES_LENGTH = 32;
30
- exports.ENCRYPTED_REFERENCE_BYTES_LENGTH = 64;
22
+ exports.PSS_TARGET_HEX_LENGTH_MAX = 4;
31
23
  /**
32
24
  * Minimal depth that can be used for creation of postage batch
33
25
  */
34
26
  exports.STAMPS_DEPTH_MIN = 17;
35
- /**
36
- * Minimal amount that can be used for creation of postage batch
37
- */
38
- exports.STAMPS_AMOUNT_MIN = 24000 * 24 * 60 * 12;
39
27
  /**
40
28
  * Maximal depth that can be used for creation of postage batch
41
29
  */
@@ -72,13 +60,3 @@ var RedundancyStrategy;
72
60
  RedundancyStrategy[RedundancyStrategy["PROX"] = 2] = "PROX";
73
61
  RedundancyStrategy[RedundancyStrategy["RACE"] = 3] = "RACE";
74
62
  })(RedundancyStrategy = exports.RedundancyStrategy || (exports.RedundancyStrategy = {}));
75
- /*********************************************************
76
- * Writers and Readers interfaces
77
- */
78
- exports.TOPIC_BYTES_LENGTH = 32;
79
- exports.TOPIC_HEX_LENGTH = 64;
80
- /*********************************************************
81
- * Ethereum compatible signing interfaces and definitions
82
- */
83
- exports.SIGNATURE_HEX_LENGTH = 130;
84
- exports.SIGNATURE_BYTES_LENGTH = 65;
@@ -1,121 +1,73 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.wrapBytesWithHelpers = exports.makeBytes = exports.bytesEqual = exports.flexBytesAtOffset = exports.bytesAtOffset = exports.assertFlexBytes = exports.isFlexBytes = exports.assertBytes = exports.hasBytesAtOffset = exports.isBytes = void 0;
4
- const hex_1 = require("./hex");
5
- /**
6
- * Type guard for `Bytes<T>` type
7
- *
8
- * @param b The byte array
9
- * @param length The length of the byte array
10
- */
11
- function isBytes(b, length) {
12
- return b instanceof Uint8Array && b.length === length;
13
- }
14
- exports.isBytes = isBytes;
15
- /**
16
- * Function that verifies if passed data are Bytes and if the array has "length" number of bytes under given offset.
17
- * @param data
18
- * @param offset
19
- * @param length
20
- */
21
- function hasBytesAtOffset(data, offset, length) {
22
- if (!(data instanceof Uint8Array)) {
23
- throw new TypeError('Data has to an Uint8Array!');
3
+ exports.Bytes = void 0;
4
+ const cafe_utility_1 = require("cafe-utility");
5
+ const DECODER = new TextDecoder();
6
+ const ENCODER = new TextEncoder();
7
+ class Bytes {
8
+ constructor(bytes, byteLength) {
9
+ if (bytes instanceof Bytes) {
10
+ this.bytes = bytes.bytes;
11
+ }
12
+ else if (typeof bytes === 'string') {
13
+ this.bytes = cafe_utility_1.Binary.hexToUint8Array(cafe_utility_1.Types.asHexString(bytes, { name: 'Bytes#constructor(bytes)' }));
14
+ }
15
+ else if (bytes instanceof ArrayBuffer) {
16
+ this.bytes = new Uint8Array(bytes);
17
+ }
18
+ else {
19
+ this.bytes = bytes;
20
+ }
21
+ this.length = this.bytes.length;
22
+ if (byteLength) {
23
+ if (Array.isArray(byteLength)) {
24
+ if (!byteLength.includes(this.length)) {
25
+ throw new Error(`Bytes#checkByteLength: bytes length is ${this.length} but expected ${byteLength.join(' or ')}`);
26
+ }
27
+ }
28
+ else if (this.length !== byteLength) {
29
+ throw new Error(`Bytes#checkByteLength: bytes length is ${this.length} but expected ${byteLength}`);
30
+ }
31
+ }
24
32
  }
25
- const offsetBytes = data.slice(offset, offset + length);
26
- return isBytes(offsetBytes, length);
27
- }
28
- exports.hasBytesAtOffset = hasBytesAtOffset;
29
- /**
30
- * Verifies if a byte array has a certain length
31
- *
32
- * @param b The byte array
33
- * @param length The specified length
34
- */
35
- function assertBytes(b, length) {
36
- if (!isBytes(b, length)) {
37
- throw new TypeError(`Parameter is not valid Bytes of length: ${length} !== ${b.length}`);
33
+ static keccak256(bytes) {
34
+ return new Bytes(cafe_utility_1.Binary.keccak256(new Bytes(bytes).toUint8Array()));
38
35
  }
39
- }
40
- exports.assertBytes = assertBytes;
41
- /**
42
- * Type guard for FlexBytes<Min,Max> type
43
- *
44
- * @param b The byte array
45
- * @param min Minimum size of the array
46
- * @param max Maximum size of the array
47
- */
48
- function isFlexBytes(b, min, max) {
49
- return b instanceof Uint8Array && b.length >= min && b.length <= max;
50
- }
51
- exports.isFlexBytes = isFlexBytes;
52
- /**
53
- * Verifies if a byte array has a certain length between min and max
54
- *
55
- * @param b The byte array
56
- * @param min Minimum size of the array
57
- * @param max Maximum size of the array
58
- */
59
- function assertFlexBytes(b, min, max) {
60
- if (!isFlexBytes(b, min, max)) {
61
- throw new TypeError(`Parameter is not valid FlexBytes of min: ${min}, max: ${max}, length: ${b.length}`);
36
+ static fromUtf8(utf8) {
37
+ return new Bytes(ENCODER.encode(utf8));
38
+ }
39
+ static fromSlice(bytes, start, length) {
40
+ if (length === undefined) {
41
+ return new Bytes(bytes.slice(start));
42
+ }
43
+ return new Bytes(bytes.slice(start, start + length));
44
+ }
45
+ offset(index) {
46
+ return new Uint8Array(this.bytes.slice(index));
47
+ }
48
+ toUint8Array() {
49
+ return new Uint8Array(this.bytes);
50
+ }
51
+ toHex() {
52
+ return cafe_utility_1.Binary.uint8ArrayToHex(this.bytes);
53
+ }
54
+ toBase64() {
55
+ return cafe_utility_1.Binary.uint8ArrayToBase64(this.bytes);
56
+ }
57
+ toBase32() {
58
+ return cafe_utility_1.Binary.uint8ArrayToBase32(this.bytes);
59
+ }
60
+ toString() {
61
+ return this.toHex();
62
+ }
63
+ toUtf8() {
64
+ return DECODER.decode(this.bytes);
65
+ }
66
+ toJSON() {
67
+ return JSON.parse(this.toUtf8());
68
+ }
69
+ equals(other) {
70
+ return this.toHex() === new Bytes(other).toHex();
62
71
  }
63
72
  }
64
- exports.assertFlexBytes = assertFlexBytes;
65
- /**
66
- * Return `length` bytes starting from `offset`
67
- *
68
- * @param data The original data
69
- * @param offset The offset to start from
70
- * @param length The length of data to be returned
71
- */
72
- function bytesAtOffset(data, offset, length) {
73
- const offsetBytes = data.slice(offset, offset + length);
74
- // We are returning strongly typed Bytes so we have to verify that length is really what we claim
75
- assertBytes(offsetBytes, length);
76
- return offsetBytes;
77
- }
78
- exports.bytesAtOffset = bytesAtOffset;
79
- /**
80
- * Return flex bytes starting from `offset`
81
- *
82
- * @param data The original data
83
- * @param offset The offset to start from
84
- * @param _min The minimum size of the data
85
- * @param _max The maximum size of the data
86
- */
87
- function flexBytesAtOffset(data, offset,
88
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
89
- _min,
90
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
91
- _max) {
92
- return data.slice(offset);
93
- }
94
- exports.flexBytesAtOffset = flexBytesAtOffset;
95
- /**
96
- * Returns true if two byte arrays are equal
97
- *
98
- * @param a Byte array to compare
99
- * @param b Byte array to compare
100
- */
101
- function bytesEqual(a, b) {
102
- return a.length === b.length && a.every((value, index) => value === b[index]);
103
- }
104
- exports.bytesEqual = bytesEqual;
105
- /**
106
- * Returns a new byte array filled with zeroes with the specified length
107
- *
108
- * @param length The length of data to be returned
109
- */
110
- function makeBytes(length) {
111
- return new Uint8Array(length);
112
- }
113
- exports.makeBytes = makeBytes;
114
- function wrapBytesWithHelpers(data) {
115
- return Object.assign(data, {
116
- text: () => new TextDecoder('utf-8').decode(data),
117
- json: () => JSON.parse(new TextDecoder('utf-8').decode(data)),
118
- hex: () => (0, hex_1.bytesToHex)(data),
119
- });
120
- }
121
- exports.wrapBytesWithHelpers = wrapBytesWithHelpers;
73
+ exports.Bytes = Bytes;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.totalChunks = void 0;
4
+ function totalChunks(fileSize) {
5
+ const chunkSize = 4096;
6
+ const hashesPerChunk = 128;
7
+ function chunksAtLevel(chunkCount) {
8
+ if (chunkCount <= 1) {
9
+ return chunkCount;
10
+ }
11
+ const upperLevelChunks = Math.ceil(chunkCount / hashesPerChunk);
12
+ return chunkCount + chunksAtLevel(upperLevelChunks);
13
+ }
14
+ const baseLevelChunks = Math.ceil(fileSize / chunkSize);
15
+ return chunksAtLevel(baseLevelChunks);
16
+ }
17
+ exports.totalChunks = totalChunks;
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.streamFiles = exports.streamDirectory = exports.hashDirectory = void 0;
4
+ const cafe_utility_1 = require("cafe-utility");
5
+ const __1 = require("..");
6
+ const manifest_1 = require("../manifest/manifest");
7
+ const chunk_size_1 = require("./chunk-size");
8
+ const collection_1 = require("./collection");
9
+ const mime_1 = require("./mime");
10
+ const typed_bytes_1 = require("./typed-bytes");
11
+ async function hashDirectory(_dir) {
12
+ throw new Error('Hashing directories is not supported in browsers!');
13
+ }
14
+ exports.hashDirectory = hashDirectory;
15
+ async function streamDirectory(_bee, _dir, _postageBatchId, _onUploadProgress, _options, _requestOptions) {
16
+ throw new Error('Streaming directories is not supported in browsers!');
17
+ }
18
+ exports.streamDirectory = streamDirectory;
19
+ async function streamFiles(bee, files, postageBatchId, onUploadProgress, options, requestOptions) {
20
+ const queue = new cafe_utility_1.AsyncQueue(64, 64);
21
+ let total = 0;
22
+ let processed = 0;
23
+ for (const file of files) {
24
+ total += (0, chunk_size_1.totalChunks)(file.size);
25
+ }
26
+ postageBatchId = new typed_bytes_1.BatchId(postageBatchId);
27
+ async function onChunk(chunk) {
28
+ await queue.enqueue(async () => {
29
+ await bee.uploadChunk(postageBatchId, chunk.build(), options, requestOptions);
30
+ onUploadProgress?.({ total, processed: ++processed });
31
+ });
32
+ }
33
+ const mantaray = new manifest_1.MantarayNode();
34
+ for (const file of files) {
35
+ const rootChunk = await new Promise((resolve, reject) => {
36
+ const tree = new cafe_utility_1.MerkleTree(onChunk);
37
+ let offset = 0;
38
+ const reader = new FileReader();
39
+ reader.onerror = () => {
40
+ reject(reader.error);
41
+ };
42
+ const readNextChunk = async () => {
43
+ if (offset >= file.size) {
44
+ const rootChunk = await tree.finalize();
45
+ resolve(rootChunk);
46
+ return;
47
+ }
48
+ const slice = file.slice(offset, offset + 4096);
49
+ reader.readAsArrayBuffer(slice);
50
+ };
51
+ reader.onload = async (event) => {
52
+ if (!event.target) {
53
+ reject('No event target');
54
+ return;
55
+ }
56
+ const data = event.target.result;
57
+ if (data) {
58
+ await tree.append(new Uint8Array(data));
59
+ offset += 4096;
60
+ }
61
+ readNextChunk();
62
+ };
63
+ readNextChunk();
64
+ });
65
+ await queue.drain();
66
+ const { filename, extension } = cafe_utility_1.Strings.parseFilename(file.name);
67
+ mantaray.addFork((0, collection_1.makeFilePath)(file), rootChunk.hash(), {
68
+ 'Content-Type': maybeEnrichMime(mime_1.mimes[extension.toLowerCase()] || 'application/octet-stream'),
69
+ Filename: filename,
70
+ });
71
+ if (file.name === 'index.html') {
72
+ mantaray.addFork('/', __1.NULL_ADDRESS, {
73
+ 'website-index-document': 'index.html',
74
+ });
75
+ }
76
+ }
77
+ return mantaray.saveRecursively(bee, postageBatchId, options, requestOptions);
78
+ }
79
+ exports.streamFiles = streamFiles;
80
+ function maybeEnrichMime(mime) {
81
+ if (['text/html', 'text/css'].includes(mime)) {
82
+ return `${mime}; charset=utf-8`;
83
+ }
84
+ return mime;
85
+ }
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.streamFiles = exports.streamDirectory = exports.hashDirectory = void 0;
4
+ const cafe_utility_1 = require("cafe-utility");
5
+ const fs_1 = require("fs");
6
+ const __1 = require("..");
7
+ const manifest_1 = require("../manifest/manifest");
8
+ const chunk_size_1 = require("./chunk-size");
9
+ const collection_node_1 = require("./collection.node");
10
+ const mime_1 = require("./mime");
11
+ const typed_bytes_1 = require("./typed-bytes");
12
+ async function hashDirectory(dir) {
13
+ const files = await (0, collection_node_1.makeCollectionFromFS)(dir);
14
+ const mantaray = new manifest_1.MantarayNode();
15
+ for (const file of files) {
16
+ const tree = new cafe_utility_1.MerkleTree(cafe_utility_1.MerkleTree.NOOP);
17
+ if (!file.fsPath) {
18
+ throw Error('File does not have fsPath, which should never happen in node. Please report this issue.');
19
+ }
20
+ const readStream = (0, fs_1.createReadStream)(file.fsPath);
21
+ for await (const data of readStream) {
22
+ await tree.append(data);
23
+ }
24
+ const rootChunk = await tree.finalize();
25
+ const { filename, extension } = cafe_utility_1.Strings.parseFilename(file.path);
26
+ mantaray.addFork(file.path, rootChunk.hash(), {
27
+ 'Content-Type': maybeEnrichMime(mime_1.mimes[extension.toLowerCase()] || 'application/octet-stream'),
28
+ Filename: filename,
29
+ });
30
+ }
31
+ return mantaray.calculateSelfAddress();
32
+ }
33
+ exports.hashDirectory = hashDirectory;
34
+ async function streamDirectory(bee, dir, postageBatchId, onUploadProgress, options, requestOptions) {
35
+ const queue = new cafe_utility_1.AsyncQueue(64, 64);
36
+ let total = 0;
37
+ let processed = 0;
38
+ postageBatchId = new typed_bytes_1.BatchId(postageBatchId);
39
+ const files = await (0, collection_node_1.makeCollectionFromFS)(dir);
40
+ for (const file of files) {
41
+ total += (0, chunk_size_1.totalChunks)(file.size);
42
+ }
43
+ async function onChunk(chunk) {
44
+ await queue.enqueue(async () => {
45
+ await bee.uploadChunk(postageBatchId, chunk.build(), options, requestOptions);
46
+ onUploadProgress?.({ total, processed: ++processed });
47
+ });
48
+ }
49
+ const mantaray = new manifest_1.MantarayNode();
50
+ for (const file of files) {
51
+ if (!file.fsPath) {
52
+ throw Error('File does not have fsPath, which should never happen in node. Please report this issue.');
53
+ }
54
+ const readStream = (0, fs_1.createReadStream)(file.fsPath);
55
+ const tree = new cafe_utility_1.MerkleTree(onChunk);
56
+ for await (const data of readStream) {
57
+ await tree.append(data);
58
+ }
59
+ const rootChunk = await tree.finalize();
60
+ await queue.drain();
61
+ const { filename, extension } = cafe_utility_1.Strings.parseFilename(file.path);
62
+ mantaray.addFork(file.path, rootChunk.hash(), {
63
+ 'Content-Type': maybeEnrichMime(mime_1.mimes[extension.toLowerCase()] || 'application/octet-stream'),
64
+ Filename: filename,
65
+ });
66
+ if (file.path === 'index.html') {
67
+ mantaray.addFork('/', __1.NULL_ADDRESS, {
68
+ 'website-index-document': 'index.html',
69
+ });
70
+ }
71
+ }
72
+ return mantaray.saveRecursively(bee, postageBatchId, options, requestOptions);
73
+ }
74
+ exports.streamDirectory = streamDirectory;
75
+ function maybeEnrichMime(mime) {
76
+ if (['text/html', 'text/css'].includes(mime)) {
77
+ return `${mime}; charset=utf-8`;
78
+ }
79
+ return mime;
80
+ }
81
+ async function streamFiles(_bee, _files, _postageBatchId, _onUploadProgress, _options, _requestOptions) {
82
+ throw new Error('Streaming files is not supported in Node.js');
83
+ }
84
+ exports.streamFiles = streamFiles;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.convertCidToReference = exports.convertReferenceToCid = exports.SWARM_FEED_CODEC = exports.SWARM_MANIFEST_CODEC = void 0;
4
4
  const cafe_utility_1 = require("cafe-utility");
5
+ const typed_bytes_1 = require("./typed-bytes");
5
6
  exports.SWARM_MANIFEST_CODEC = 0xfa;
6
7
  exports.SWARM_FEED_CODEC = 0xfb;
7
8
  const CODEC_TABLE = {
@@ -9,9 +10,7 @@ const CODEC_TABLE = {
9
10
  [exports.SWARM_FEED_CODEC]: 'feed',
10
11
  };
11
12
  function convertReferenceToCid(reference, type) {
12
- if (reference.length !== 64) {
13
- throw TypeError('Only 32-byte, non-encrypted references are supported');
14
- }
13
+ reference = new typed_bytes_1.Reference(reference);
15
14
  const base32 = 'b';
16
15
  const version = new Uint8Array([1]);
17
16
  const codec = new Uint8Array([type === 'feed' ? exports.SWARM_FEED_CODEC : exports.SWARM_MANIFEST_CODEC]);
@@ -19,7 +18,7 @@ function convertReferenceToCid(reference, type) {
19
18
  const sha256 = new Uint8Array([27]);
20
19
  const size = new Uint8Array([32]);
21
20
  const header = cafe_utility_1.Binary.uint8ArrayToBase32(cafe_utility_1.Binary.concatBytes(version, codec, unknown, sha256, size)).replace(/\=+$/, '');
22
- const hash = cafe_utility_1.Binary.uint8ArrayToBase32(cafe_utility_1.Binary.hexToUint8Array(reference)).replace(/\=+$/, '');
21
+ const hash = reference.toBase32().replace(/\=+$/, '');
23
22
  return `${base32}${header}${hash}`.toLowerCase();
24
23
  }
25
24
  exports.convertReferenceToCid = convertReferenceToCid;
@@ -29,10 +28,10 @@ function convertCidToReference(cid) {
29
28
  if (!CODEC_TABLE[codec]) {
30
29
  throw new Error('Unknown codec');
31
30
  }
32
- const reference = bytes.slice(-32);
31
+ const reference = new typed_bytes_1.Reference(bytes.slice(-32));
33
32
  return {
34
33
  type: CODEC_TABLE[codec],
35
- reference: cafe_utility_1.Binary.uint8ArrayToHex(reference),
34
+ reference,
36
35
  };
37
36
  }
38
37
  exports.convertCidToReference = convertCidToReference;
@@ -7,7 +7,7 @@ exports.getFolderSize = exports.makeCollectionFromFS = void 0;
7
7
  *
8
8
  * @param dir path to the directory
9
9
  */
10
- async function makeCollectionFromFS(dir) {
10
+ async function makeCollectionFromFS(_dir) {
11
11
  throw new Error('Creating Collection from File System is not supported in browsers!');
12
12
  }
13
13
  exports.makeCollectionFromFS = makeCollectionFromFS;
@@ -17,7 +17,7 @@ exports.makeCollectionFromFS = makeCollectionFromFS;
17
17
  * @param dir the path to the folder to check
18
18
  * @returns size in bytes
19
19
  */
20
- async function getFolderSize(dir) {
20
+ async function getFolderSize(_dir) {
21
21
  throw new Error('Creating Collection from File System is not supported in browsers!');
22
22
  }
23
23
  exports.getFolderSize = getFolderSize;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getCollectionSize = exports.makeCollectionFromFileList = exports.assertCollection = exports.isCollection = void 0;
3
+ exports.getCollectionSize = exports.makeCollectionFromFileList = exports.makeFilePath = exports.assertCollection = exports.isCollection = void 0;
4
4
  const error_1 = require("./error");
5
5
  function isCollection(data) {
6
6
  if (!Array.isArray(data)) {
@@ -24,7 +24,8 @@ function makeFilePath(file) {
24
24
  }
25
25
  throw new TypeError('file is not valid File object');
26
26
  }
27
- async function makeCollectionFromFileList(fileList) {
27
+ exports.makeFilePath = makeFilePath;
28
+ function makeCollectionFromFileList(fileList) {
28
29
  return Array.from(fileList).map(file => ({
29
30
  path: makeFilePath(file),
30
31
  size: file.size,
@@ -8,7 +8,6 @@ const fs_1 = __importDefault(require("fs"));
8
8
  const path_1 = __importDefault(require("path"));
9
9
  /**
10
10
  * Creates array in the format of Collection with data loaded from directory on filesystem.
11
- * The function loads all the data into memory!
12
11
  *
13
12
  * @param dir path to the directory
14
13
  */
@@ -23,7 +22,6 @@ async function makeCollectionFromFS(dir) {
23
22
  }
24
23
  exports.makeCollectionFromFS = makeCollectionFromFS;
25
24
  async function buildCollectionRelative(dir, relativePath) {
26
- // Handles case when the dir is not existing or it is a file ==> throws an error
27
25
  const dirname = path_1.default.join(dir, relativePath);
28
26
  const entries = await fs_1.default.promises.opendir(dirname);
29
27
  let collection = [];
@@ -1,6 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NULL_TOPIC = exports.NULL_STAMP = exports.SWARM_GATEWAY_URL = void 0;
3
+ exports.NULL_OWNER = exports.NULL_IDENTIFIER = exports.NULL_ADDRESS = exports.NULL_TOPIC = exports.NULL_STAMP = exports.SWARM_GATEWAY_URL = void 0;
4
+ const typed_bytes_1 = require("./typed-bytes");
4
5
  exports.SWARM_GATEWAY_URL = 'https://api.gateway.ethswarm.org';
5
- exports.NULL_STAMP = '0000000000000000000000000000000000000000000000000000000000000000';
6
- exports.NULL_TOPIC = '0000000000000000000000000000000000000000000000000000000000000000';
6
+ exports.NULL_STAMP = new typed_bytes_1.BatchId('0000000000000000000000000000000000000000000000000000000000000000');
7
+ exports.NULL_TOPIC = new typed_bytes_1.Topic('0000000000000000000000000000000000000000000000000000000000000000');
8
+ exports.NULL_ADDRESS = new Uint8Array(32);
9
+ exports.NULL_IDENTIFIER = new Uint8Array(32);
10
+ exports.NULL_OWNER = new Uint8Array(20);
@@ -2,12 +2,15 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.prepareWebsocketData = void 0;
4
4
  async function prepareWebsocketData(data) {
5
- if (typeof data === 'string')
5
+ if (typeof data === 'string') {
6
6
  return new TextEncoder().encode(data);
7
- if (data instanceof ArrayBuffer)
7
+ }
8
+ if (data instanceof ArrayBuffer) {
8
9
  return new Uint8Array(data);
9
- if (data instanceof Blob)
10
+ }
11
+ if (data instanceof Blob) {
10
12
  return new Uint8Array(await new Response(data).arrayBuffer());
13
+ }
11
14
  throw new TypeError('unknown websocket data type');
12
15
  }
13
16
  exports.prepareWebsocketData = prepareWebsocketData;
@@ -5,14 +5,18 @@ function isBufferArray(buffer) {
5
5
  return Array.isArray(buffer) && buffer.length > 0 && buffer.every(data => data instanceof Buffer);
6
6
  }
7
7
  async function prepareWebsocketData(data) {
8
- if (typeof data === 'string')
8
+ if (typeof data === 'string') {
9
9
  return new TextEncoder().encode(data);
10
- if (data instanceof Buffer)
10
+ }
11
+ if (data instanceof Buffer) {
11
12
  return new Uint8Array(data);
12
- if (data instanceof ArrayBuffer)
13
+ }
14
+ if (data instanceof ArrayBuffer) {
13
15
  return new Uint8Array(data);
14
- if (isBufferArray(data))
16
+ }
17
+ if (isBufferArray(data)) {
15
18
  return new Uint8Array(Buffer.concat(data));
19
+ }
16
20
  throw new TypeError('unknown websocket data type');
17
21
  }
18
22
  exports.prepareWebsocketData = prepareWebsocketData;