@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
@@ -1,13 +1,17 @@
1
- import { Binary } from 'cafe-utility';
1
+ import { Types } from 'cafe-utility';
2
2
  import { BeeError } from "./error.js";
3
- /**
4
- * Read the filename from the content-disposition header
5
- * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
6
- *
7
- * @param header the content-disposition header value
8
- *
9
- * @returns the filename
10
- */
3
+ import { convertEnvelopeToMarshaledStamp } from "./stamps.js";
4
+ import { BatchId, PublicKey, Reference } from "./typed-bytes.js";
5
+ export function readFileHeaders(headers) {
6
+ const name = readContentDispositionFilename(headers['content-disposition']);
7
+ const tagUid = readTagUid(headers['swarm-tag-uid']);
8
+ const contentType = headers['content-type'] || undefined;
9
+ return {
10
+ name,
11
+ tagUid,
12
+ contentType
13
+ };
14
+ }
11
15
  function readContentDispositionFilename(header) {
12
16
  if (!header) {
13
17
  throw new BeeError('missing content-disposition header');
@@ -26,60 +30,81 @@ function readTagUid(header) {
26
30
  }
27
31
  return parseInt(header, 10);
28
32
  }
29
- export function readFileHeaders(headers) {
30
- const name = readContentDispositionFilename(headers['content-disposition']);
31
- const tagUid = readTagUid(headers['swarm-tag-uid']);
32
- const contentType = headers['content-type'] || undefined;
33
- return {
34
- name,
35
- tagUid,
36
- contentType
37
- };
38
- }
39
- export function extractUploadHeaders(stamp, options) {
40
- if (!stamp) {
41
- throw new BeeError('Stamp has to be specified!');
42
- }
33
+ export function prepareRequestHeaders(stamp, nullableOptions) {
43
34
  const headers = {};
44
- if (stamp instanceof Uint8Array) {
45
- headers['swarm-postage-stamp'] = Binary.uint8ArrayToHex(stamp);
46
- } else {
47
- headers['swarm-postage-batch-id'] = stamp;
35
+ if (isEnvelopeWithBatchId(stamp)) {
36
+ headers['swarm-postage-stamp'] = convertEnvelopeToMarshaledStamp(stamp).toHex();
37
+ } else if (stamp) {
38
+ stamp = new BatchId(stamp);
39
+ headers['swarm-postage-batch-id'] = stamp.toHex();
40
+ }
41
+ if (!nullableOptions) {
42
+ return headers;
48
43
  }
49
- if (options?.act) {
44
+ const options = Types.asObject(nullableOptions);
45
+ if (options.size) {
46
+ headers['content-length'] = String(options.size);
47
+ }
48
+ if (options.contentType) {
49
+ headers['content-type'] = String(options.contentType);
50
+ }
51
+ if (options.redundancyLevel) {
52
+ headers['swarm-redundancy-level'] = String(options.redundancyLevel);
53
+ }
54
+ if (Types.isBoolean(options.act)) {
50
55
  headers['swarm-act'] = String(options.act);
51
56
  }
52
- if (options?.pin) {
57
+ if (Types.isBoolean(options.pin)) {
53
58
  headers['swarm-pin'] = String(options.pin);
54
59
  }
55
- if (options?.encrypt) {
56
- headers['swarm-encrypt'] = String(options.encrypt);
60
+ if (Types.isBoolean(options.encrypt)) {
61
+ headers['swarm-encrypt'] = options.encrypt.toString();
57
62
  }
58
- if (options?.tag) {
63
+ if (options.tag) {
59
64
  headers['swarm-tag'] = String(options.tag);
60
65
  }
61
- if (typeof options?.deferred === 'boolean') {
66
+ if (Types.isBoolean(options.deferred)) {
62
67
  headers['swarm-deferred-upload'] = options.deferred.toString();
63
68
  }
64
- return headers;
65
- }
66
- export function extractRedundantUploadHeaders(postageBatchId, options) {
67
- const headers = extractUploadHeaders(postageBatchId, options);
68
- if (options?.redundancyLevel) {
69
- headers['swarm-redundancy-level'] = String(options.redundancyLevel);
70
- }
71
- return headers;
72
- }
73
- export function extractDownloadHeaders(options) {
74
- const headers = {};
75
- if (options?.redundancyStrategy) {
69
+ if (options.redundancyStrategy) {
76
70
  headers['swarm-redundancy-strategy'] = String(options.redundancyStrategy);
77
71
  }
78
- if (options?.fallback === false) {
79
- headers['swarm-redundancy-fallback-mode'] = 'false';
72
+ if (Types.isBoolean(options.fallback)) {
73
+ headers['swarm-redundancy-fallback-mode'] = options.fallback.toString();
80
74
  }
81
- if (options?.timeoutMs !== undefined) {
75
+ if (options.timeoutMs) {
82
76
  headers['swarm-chunk-retrieval-timeout'] = String(options.timeoutMs);
83
77
  }
78
+ if (options.indexDocument) {
79
+ headers['swarm-index-document'] = String(options.indexDocument);
80
+ }
81
+ if (options.errorDocument) {
82
+ headers['swarm-error-document'] = String(options.errorDocument);
83
+ }
84
+ if (options.actPublisher) {
85
+ headers['swarm-act-publisher'] = new PublicKey(options.actPublisher).toCompressedHex();
86
+ }
87
+ if (options.actHistoryAddress) {
88
+ headers['swarm-act-history-address'] = new Reference(options.actHistoryAddress).toHex();
89
+ }
90
+ if (options.actTimestamp) {
91
+ headers['swarm-act-timestamp'] = String(options.actTimestamp);
92
+ }
93
+ if (options.actPublisher || options.actHistoryAddress || options.actTimestamp) {
94
+ headers['swarm-act'] = 'true';
95
+ }
96
+ if (options.gasPrice) {
97
+ headers['gas-price'] = String(options.gasPrice);
98
+ }
99
+ if (options.gasLimit) {
100
+ headers['gas-limit'] = String(options.gasLimit);
101
+ }
84
102
  return headers;
103
+ }
104
+ function isEnvelopeWithBatchId(value) {
105
+ if (!Types.isObject(value)) {
106
+ return false;
107
+ }
108
+ const envelope = value;
109
+ return envelope.issuer !== undefined && envelope.index !== undefined && envelope.signature !== undefined && envelope.timestamp !== undefined && envelope.batchId !== undefined;
85
110
  }
@@ -1,9 +1,13 @@
1
1
  import axios from 'axios';
2
- import { Objects, Strings } from 'cafe-utility';
2
+ import { Dates, Objects, Strings, System } from 'cafe-utility';
3
3
  import { BeeResponseError } from "../index.js";
4
4
  const {
5
5
  AxiosError
6
6
  } = axios;
7
+ const MAX_FAILED_ATTEMPTS = 100000;
8
+ const DELAY_FAST = 200;
9
+ const DELAY_SLOW = 1000;
10
+ const DELAY_THRESHOLD = Dates.minutes(1) / DELAY_FAST;
7
11
  export const DEFAULT_HTTP_CONFIG = {
8
12
  headers: {
9
13
  accept: 'application/json, text/plain, */*'
@@ -17,18 +21,36 @@ export const DEFAULT_HTTP_CONFIG = {
17
21
  * @param config Internal settings and/or Bee settings
18
22
  */
19
23
  export async function http(options, config) {
20
- try {
21
- const requestConfig = Objects.deepMerge3(DEFAULT_HTTP_CONFIG, config, options);
22
- maybeRunOnRequestHook(options, requestConfig);
23
- const response = await axios(requestConfig);
24
- // TODO: https://github.com/axios/axios/pull/6253
25
- return response;
26
- } catch (e) {
27
- if (e instanceof AxiosError) {
28
- throw new BeeResponseError(config.method || 'get', config.url || '<unknown>', e.message, e.response?.data, e.response?.status, e.code);
24
+ const requestConfig = Objects.deepMerge3(DEFAULT_HTTP_CONFIG, config, options);
25
+ if (requestConfig.params) {
26
+ const keys = Object.keys(requestConfig.params);
27
+ for (const key of keys) {
28
+ const value = requestConfig.params[key];
29
+ if (value === undefined) {
30
+ delete requestConfig.params[key];
31
+ }
29
32
  }
30
- throw e;
31
33
  }
34
+ let failedAttempts = 0;
35
+ while (failedAttempts < MAX_FAILED_ATTEMPTS) {
36
+ try {
37
+ maybeRunOnRequestHook(options, requestConfig);
38
+ const response = await axios(requestConfig);
39
+ return response;
40
+ } catch (e) {
41
+ if (e instanceof AxiosError) {
42
+ if (e.code === 'ECONNABORTED' && options.endlesslyRetry) {
43
+ failedAttempts++;
44
+ await System.sleepMillis(failedAttempts < DELAY_THRESHOLD ? DELAY_FAST : DELAY_SLOW);
45
+ } else {
46
+ throw new BeeResponseError(config.method || 'get', config.url || '<unknown>', e.message, e.response?.data, e.response?.status, e.code);
47
+ }
48
+ } else {
49
+ throw e;
50
+ }
51
+ }
52
+ }
53
+ throw Error('Max number of failed attempts reached');
32
54
  }
33
55
  function maybeRunOnRequestHook(options, requestConfig) {
34
56
  if (options.onRequest) {
@@ -0,0 +1,75 @@
1
+ export const mimes = {
2
+ aac: 'audio/aac',
3
+ abw: 'application/x-abiword',
4
+ ai: 'application/postscript',
5
+ arc: 'application/octet-stream',
6
+ avi: 'video/x-msvideo',
7
+ azw: 'application/vnd.amazon.ebook',
8
+ bin: 'application/octet-stream',
9
+ bz: 'application/x-bzip',
10
+ bz2: 'application/x-bzip2',
11
+ csh: 'application/x-csh',
12
+ css: 'text/css',
13
+ csv: 'text/csv',
14
+ doc: 'application/msword',
15
+ dll: 'application/octet-stream',
16
+ dmg: 'application/x-apple-diskimage',
17
+ eot: 'application/vnd.ms-fontobject',
18
+ epub: 'application/epub+zip',
19
+ gif: 'image/gif',
20
+ htm: 'text/html',
21
+ html: 'text/html',
22
+ ico: 'image/x-icon',
23
+ ics: 'text/calendar',
24
+ jar: 'application/java-archive',
25
+ jpeg: 'image/jpeg',
26
+ jpg: 'image/jpeg',
27
+ js: 'application/javascript',
28
+ json: 'application/json',
29
+ mid: 'audio/midi',
30
+ midi: 'audio/midi',
31
+ mp2: 'audio/mpeg',
32
+ mp3: 'audio/mpeg',
33
+ mp4: 'video/mp4',
34
+ mpa: 'video/mpeg',
35
+ mpe: 'video/mpeg',
36
+ mpeg: 'video/mpeg',
37
+ mpkg: 'application/vnd.apple.installer+xml',
38
+ odp: 'application/vnd.oasis.opendocument.presentation',
39
+ ods: 'application/vnd.oasis.opendocument.spreadsheet',
40
+ odt: 'application/vnd.oasis.opendocument.text',
41
+ oga: 'audio/ogg',
42
+ ogv: 'video/ogg',
43
+ ogx: 'application/ogg',
44
+ otf: 'font/otf',
45
+ png: 'image/png',
46
+ pdf: 'application/pdf',
47
+ ppt: 'application/vnd.ms-powerpoint',
48
+ rar: 'application/x-rar-compressed',
49
+ rtf: 'application/rtf',
50
+ sh: 'application/x-sh',
51
+ svg: 'image/svg+xml',
52
+ swf: 'application/x-shockwave-flash',
53
+ tar: 'application/x-tar',
54
+ tif: 'image/tiff',
55
+ tiff: 'image/tiff',
56
+ ts: 'application/typescript',
57
+ ttf: 'font/ttf',
58
+ txt: 'text/plain',
59
+ vsd: 'application/vnd.visio',
60
+ wav: 'audio/x-wav',
61
+ weba: 'audio/webm',
62
+ webm: 'video/webm',
63
+ webp: 'image/webp',
64
+ woff: 'font/woff',
65
+ woff2: 'font/woff2',
66
+ xhtml: 'application/xhtml+xml',
67
+ xls: 'application/vnd.ms-excel',
68
+ xlsx: 'application/vnd.ms-excel',
69
+ xml: 'application/xml',
70
+ xul: 'application/vnd.mozilla.xul+xml',
71
+ zip: 'application/zip',
72
+ '3gp': 'video/3gpp',
73
+ '3gp2': 'video/3gpp2',
74
+ '7z': 'application/x-7z-compressed'
75
+ };
@@ -1,4 +1,5 @@
1
1
  import { PSS_TARGET_HEX_LENGTH_MAX } from "../types/index.js";
2
+ import { PeerAddress } from "./typed-bytes.js";
2
3
  /**
3
4
  * Utility function that for given strings/reference takes the most specific
4
5
  * target that Bee node will except.
@@ -7,8 +8,6 @@ import { PSS_TARGET_HEX_LENGTH_MAX } from "../types/index.js";
7
8
  * @see [Bee docs - PSS](https://docs.ethswarm.org/docs/develop/tools-and-features/pss)
8
9
  */
9
10
  export function makeMaxTarget(target) {
10
- if (typeof target !== 'string') {
11
- throw new TypeError('target has to be an string!');
12
- }
13
- return target.slice(0, PSS_TARGET_HEX_LENGTH_MAX);
11
+ target = new PeerAddress(target);
12
+ return target.toHex().slice(0, PSS_TARGET_HEX_LENGTH_MAX);
14
13
  }
@@ -7,12 +7,6 @@ const insaneTable = [[92, 87, 82, 77, 73, 68, 63, 59, 54, 50, 45, 41, 37, 33, 29
7
7
  const encInsaneTable = [[46, 43, 41, 38, 36, 34, 31, 29, 27, 25, 22, 20, 18, 16, 14, 13, 11, 9, 8, 6, 5, 4, 2, 1], [31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 7]];
8
8
  const paranoidTable = [[37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1], [90, 88, 87, 85, 84, 82, 81, 79, 77, 76, 74, 72, 71, 69, 67, 66, 64, 62, 60, 59, 57, 55, 53, 51, 49, 48, 46, 44, 41, 39, 37, 35, 32, 30, 27, 24, 20]];
9
9
  const encParanoidTable = [[18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1], [88, 85, 82, 79, 76, 72, 69, 66, 62, 59, 55, 51, 48, 44, 39, 35, 30, 24]];
10
- const tables = {
11
- [RedundancyLevel.MEDIUM]: [mediumTable, encMediumTable],
12
- [RedundancyLevel.STRONG]: [strongTable, encStrongTable],
13
- [RedundancyLevel.INSANE]: [insaneTable, encInsaneTable],
14
- [RedundancyLevel.PARANOID]: [paranoidTable, encParanoidTable]
15
- };
16
10
  /**
17
11
  * Returns an approximate multiplier for the overhead of a given redundancy level.
18
12
  * Redundancy level is a tradeoff between storage overhead and fault tolerance.
@@ -20,8 +14,10 @@ const tables = {
20
14
  * redundancy level.
21
15
  */
22
16
  export function approximateOverheadForRedundancyLevel(chunks, level, encrypted) {
23
- const tableType = level === RedundancyLevel.MEDIUM ? tables[RedundancyLevel.MEDIUM] : level === RedundancyLevel.STRONG ? tables[RedundancyLevel.STRONG] : level === RedundancyLevel.INSANE ? tables[RedundancyLevel.INSANE] : tables[RedundancyLevel.PARANOID];
24
- const table = encrypted ? tableType[1] : tableType[0];
17
+ if (level === RedundancyLevel.OFF) {
18
+ return 0;
19
+ }
20
+ const table = selectTable(level, encrypted);
25
21
  const [supportedChunks, parities] = table;
26
22
  for (let i = 0; i < supportedChunks.length; i++) {
27
23
  if (chunks >= supportedChunks[i]) {
@@ -30,6 +26,20 @@ export function approximateOverheadForRedundancyLevel(chunks, level, encrypted)
30
26
  }
31
27
  return parities[parities.length - 1] / supportedChunks[supportedChunks.length - 1];
32
28
  }
29
+ function selectTable(level, encrypted) {
30
+ switch (level) {
31
+ case RedundancyLevel.MEDIUM:
32
+ return encrypted ? encMediumTable : mediumTable;
33
+ case RedundancyLevel.STRONG:
34
+ return encrypted ? encStrongTable : strongTable;
35
+ case RedundancyLevel.INSANE:
36
+ return encrypted ? encInsaneTable : insaneTable;
37
+ case RedundancyLevel.PARANOID:
38
+ return encrypted ? encParanoidTable : paranoidTable;
39
+ default:
40
+ throw new Error(`Unknown redundancy level ${level}`);
41
+ }
42
+ }
33
43
  const medium = {
34
44
  label: 'medium',
35
45
  value: RedundancyLevel.MEDIUM,
@@ -0,0 +1,13 @@
1
+ import { Types } from 'cafe-utility';
2
+ import { Reference } from "./typed-bytes.js";
3
+ export class ResourceLocator {
4
+ constructor(raw) {
5
+ this.raw = raw;
6
+ }
7
+ toString() {
8
+ if (Types.isString(this.raw) && this.raw.includes('.eth')) {
9
+ return this.raw;
10
+ }
11
+ return new Reference(this.raw).toHex();
12
+ }
13
+ }
@@ -0,0 +1,31 @@
1
+ import { Numbers } from 'cafe-utility';
2
+ /**
3
+ * Represents a size in bytes.
4
+ *
5
+ * Uses 1000 instead of 1024 for converting between units.
6
+ * This is to stay consistent with the Swarm papers
7
+ * on theoretical and effective storage capacity.
8
+ */
9
+ export class Size {
10
+ constructor(bytes) {
11
+ this.bytes = Math.ceil(bytes);
12
+ if (bytes < 0) {
13
+ throw Error('Size must be at least 0');
14
+ }
15
+ }
16
+ static fromBytes(bytes) {
17
+ return new Size(bytes);
18
+ }
19
+ static fromGigabytes(gigabytes) {
20
+ return new Size(gigabytes * 1000 * 1000 * 1000);
21
+ }
22
+ toBytes() {
23
+ return this.bytes;
24
+ }
25
+ toGigabytes() {
26
+ return this.bytes / 1000 / 1000 / 1000;
27
+ }
28
+ toFormattedString() {
29
+ return Numbers.convertBytes(this.bytes, 1000);
30
+ }
31
+ }
@@ -1,4 +1,8 @@
1
1
  import { Binary } from 'cafe-utility';
2
+ import { Bytes } from "./bytes.js";
3
+ import { Duration } from "./duration.js";
4
+ import { BZZ } from "./tokens.js";
5
+ import { asNumberString } from "./type.js";
2
6
  /**
3
7
  * Utility function that calculates usage of postage batch based on its utilization, depth and bucket depth.
4
8
  *
@@ -10,14 +14,14 @@ export function getStampUsage(utilization, depth, bucketDepth) {
10
14
  return utilization / Math.pow(2, depth - bucketDepth);
11
15
  }
12
16
  /**
13
- * Utility function that calculates the theoritical maximum capacity of a postage batch based on its depth.
17
+ * Utility function that calculates the theoritical maximum size of a postage batch based on its depth.
14
18
  *
15
- * For smaller depths (up to 20), this may provide less accurate results.
19
+ * For smaller depths (up to 22), this may provide less accurate results.
16
20
  *
17
- * @returns {number} The maximum capacity of the postage batch in bytes.
21
+ * @returns {number} The maximum theoretical size of the postage batch in bytes.
18
22
  */
19
- export function getStampMaximumCapacityBytes(depth) {
20
- return 2 ** depth * 4096;
23
+ export function getStampTheoreticalBytes(depth) {
24
+ return 4096 * 2 ** depth;
21
25
  }
22
26
  /**
23
27
  * Based on https://docs.ethswarm.org/docs/learn/technology/contracts/postage-stamp/#effective-utilisation-table
@@ -37,79 +41,78 @@ const utilisationRateMap = {
37
41
  33: 0.9842,
38
42
  34: 0.9889
39
43
  };
44
+ const effectiveSizeBreakpoints = [[22, 4.93], [23, 17.03], [24, 44.21], [25, 102.78], [26, 225.87], [27, 480.44], [28, 1001.44], [29, 2060.27], [30, 4201.9], [31, 8519.02], [32, 17199.89], [33, 34628.46]];
40
45
  /**
41
- * Utility function that calculates the effective volume of a postage batch based on its depth.
46
+ * Utility function that calculates the effective size of a postage batch based on its depth.
42
47
  *
43
- * Below 22 depth the effective volume is 0
48
+ * Below 22 depth the effective size is 0
44
49
  * Above 34 it's always > 99%
45
50
  *
46
- * @returns {number} The effective volume of the postage batch in bytes.
51
+ * @returns {number} The effective size of the postage batch in bytes.
47
52
  */
48
53
  export function getStampEffectiveBytes(depth) {
49
54
  if (depth < 22) {
50
55
  return 0;
51
56
  }
52
57
  const utilRate = utilisationRateMap[depth] ?? 0.99;
53
- return getStampMaximumCapacityBytes(depth) * utilRate;
58
+ return Math.ceil(getStampTheoreticalBytes(depth) * utilRate);
54
59
  }
55
- /**
56
- * Utility function that calculates the cost of a postage batch based on its depth and amount.
57
- *
58
- * @returns {number} The cost of the postage batch in PLUR (10000000000000000 [1e16] PLUR = 1 BZZ)
59
- */
60
- export function getStampCostInPlur(depth, amount) {
61
- return 2 ** depth * amount;
60
+ export function getStampEffectiveBytesBreakpoints() {
61
+ const map = new Map();
62
+ for (let i = 22; i < 35; i++) {
63
+ map.set(i, getStampEffectiveBytes(i));
64
+ }
65
+ return map;
62
66
  }
63
67
  /**
64
68
  * Utility function that calculates the cost of a postage batch based on its depth and amount.
65
- *
66
- * @returns {number} The cost of the postage batch in BZZ (1 BZZ = 10000000000000000 [1e16] PLUR)
67
69
  */
68
- export function getStampCostInBzz(depth, amount) {
69
- const BZZ_UNIT = 10 ** 16;
70
- return getStampCostInPlur(depth, amount) / BZZ_UNIT;
70
+ export function getStampCost(depth, amount) {
71
+ return BZZ.fromPLUR(2n ** BigInt(depth) * BigInt(amount));
71
72
  }
72
73
  /**
73
74
  * Utility function that calculates the TTL of a postage batch based on its amount, price per block and block time.
74
75
  *
75
76
  * For more accurate results, get the price per block and block time from the Bee node or the blockchain.
76
77
  *
77
- * @returns {number} The TTL of the postage batch in seconds.
78
+ * @returns {number} The TTL of the postage batch.
78
79
  */
79
- export function getStampTtlSeconds(amount, pricePerBlock = 24000, blockTime = 5) {
80
- return amount * blockTime / pricePerBlock;
80
+ export function getStampDuration(amount, pricePerBlock, blockTime = 5) {
81
+ const amountBigInt = BigInt(asNumberString(amount));
82
+ return Duration.fromSeconds(Number(amountBigInt * BigInt(blockTime) / BigInt(pricePerBlock)));
81
83
  }
82
84
  /**
83
- * Utility function that calculates the amount of tokens required to maintain a given Time To Live (TTL) for a postage batch.
84
- *
85
- * This function estimates the required amount based on the provided TTL in days.
85
+ * Get the postage batch `amount` required for a given `duration`.
86
86
  *
87
- * @param {number} days - The Time To Live (TTL) in days.
88
- * @returns {NumberString} The estimated amount of tokens needed for the specified TTL.
87
+ * @param duration A duration object representing the duration of the storage.
88
+ * @param pricePerBlock The price per block in PLUR.
89
+ * @param blockTime The block time in seconds.
89
90
  */
90
- export function getAmountForTtl(days) {
91
- // 414720000 = (24 * 60 * 60 * 24_000) / 5
92
- return ((days <= 0 ? 1 : days) * 414720000).toString();
91
+ export function getAmountForDuration(duration, pricePerBlock, blockTime = 5) {
92
+ return BigInt(duration.toSeconds()) / BigInt(blockTime) * BigInt(pricePerBlock);
93
93
  }
94
94
  /**
95
- * Utility function that calculates the depth required for a postage batch to achieve the specified capacity in gigabytes.
95
+ * Utility function that calculates the depth required for a postage batch to achieve the specified effective size
96
96
  *
97
- * The depth is determined based on the given gigabytes, and the result is adjusted to a minimum depth of 18.
98
- *
99
- * @param {number} gigabytes - The desired capacity of the postage batch in gigabytes.
100
- * @returns {number} The calculated depth necessary to achieve the specified capacity.
97
+ * @param size The effective size of the postage batch
98
+ * @returns
101
99
  */
102
- export function getDepthForCapacity(gigabytes) {
103
- return gigabytes <= 1 ? 18 : Math.ceil(Math.log2(Math.ceil(gigabytes)) + 18);
100
+ export function getDepthForSize(size) {
101
+ for (const [depth, sizeBreakpoint] of effectiveSizeBreakpoints) {
102
+ if (size.toGigabytes() <= sizeBreakpoint) {
103
+ return depth;
104
+ }
105
+ }
106
+ return 34;
104
107
  }
105
- export function convertEnvelopeToMarshaledStamp(batchID, envelope) {
106
- return marshalStamp(envelope.signature, Binary.hexToUint8Array(batchID), envelope.timestamp, envelope.index);
108
+ export function convertEnvelopeToMarshaledStamp(envelope) {
109
+ return marshalStamp(envelope.signature, envelope.batchId.toUint8Array(), envelope.timestamp, envelope.index);
107
110
  }
108
- export function marshalStamp(signature, batchID, timestamp, index) {
111
+ export function marshalStamp(signature, batchId, timestamp, index) {
109
112
  if (signature.length !== 65) {
110
113
  throw Error('invalid signature length');
111
114
  }
112
- if (batchID.length !== 32) {
115
+ if (batchId.length !== 32) {
113
116
  throw Error('invalid batch ID length');
114
117
  }
115
118
  if (timestamp.length !== 8) {
@@ -118,5 +121,5 @@ export function marshalStamp(signature, batchID, timestamp, index) {
118
121
  if (index.length !== 8) {
119
122
  throw Error('invalid index length');
120
123
  }
121
- return Binary.concatBytes(batchID, index, timestamp, signature);
124
+ return new Bytes(Binary.concatBytes(batchId, index, timestamp, signature));
122
125
  }
@@ -1,4 +1,4 @@
1
- import { extractCollectionUploadHeaders } from "../modules/bzz.js";
1
+ import { prepareRequestHeaders } from "./headers.js";
2
2
  import { http } from "./http.js";
3
3
  import { TarStream } from "./tar.js";
4
4
  import { writeTar } from "./tar-writer.js";
@@ -15,7 +15,7 @@ export async function uploadTar(requestOptions, collection, postageBatchId, opti
15
15
  headers: {
16
16
  'content-type': 'application/x-tar',
17
17
  'swarm-collection': 'true',
18
- ...extractCollectionUploadHeaders(postageBatchId, options)
18
+ ...prepareRequestHeaders(postageBatchId, options)
19
19
  }
20
20
  });
21
21
  return response;
@@ -1,4 +1,4 @@
1
- import { extractCollectionUploadHeaders } from "../modules/bzz.js";
1
+ import { prepareRequestHeaders } from "./headers.js";
2
2
  import { http } from "./http.js";
3
3
  import { TarStream } from "./tar.js";
4
4
  import { writeTar } from "./tar-writer.js";
@@ -13,7 +13,7 @@ export async function uploadTar(requestOptions, collection, postageBatchId, opti
13
13
  headers: {
14
14
  'content-type': 'application/x-tar',
15
15
  'swarm-collection': 'true',
16
- ...extractCollectionUploadHeaders(postageBatchId, options)
16
+ ...prepareRequestHeaders(postageBatchId, options)
17
17
  }
18
18
  });
19
19
  await writeTar(collection, tarStream);