@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,288 +1,216 @@
1
- import { Readable } from 'stream';
2
- import { ADDRESS_HEX_LENGTH, BATCH_ID_HEX_LENGTH, ENCRYPTED_REFERENCE_HEX_LENGTH, PSS_TARGET_HEX_LENGTH_MAX, PUBKEY_HEX_LENGTH, REFERENCE_HEX_LENGTH, TAGS_LIMIT_MAX, TAGS_LIMIT_MIN } from "../types/index.js";
3
- import { convertCidToReference, convertReferenceToCid } from "./cid.js";
4
- import { BeeArgumentError, BeeError } from "./error.js";
1
+ import { Types } from 'cafe-utility';
2
+ import * as stream from 'stream';
3
+ import { TAGS_LIMIT_MAX, TAGS_LIMIT_MIN } from "../types/index.js";
5
4
  import { isFile } from "./file.js";
6
- import { assertHexString, assertPrefixedHexString, isHexString } from "./hex.js";
7
- export function isReadable(obj) {
8
- return typeof Readable !== 'undefined' && obj instanceof Readable;
5
+ import { PublicKey, Reference } from "./typed-bytes.js";
6
+ export function isReadable(value) {
7
+ return typeof stream.Readable !== 'undefined' && value instanceof stream.Readable;
9
8
  }
10
- export function isInteger(value) {
11
- return typeof value === 'string' && /^-?(0|[1-9][0-9]*)$/g.test(value) || typeof value === 'number' && value > Number.MIN_SAFE_INTEGER && value < Number.MAX_SAFE_INTEGER && Number.isInteger(value);
12
- }
13
- export function isObject(value) {
14
- return value !== null && typeof value === 'object';
15
- }
16
- export function isStrictlyObject(value) {
17
- return isObject(value) && !Array.isArray(value);
18
- }
19
- /**
20
- * Asserts if object is Error
21
- *
22
- * @param e
23
- */
24
- export function isError(e) {
25
- return e instanceof Error;
26
- }
27
- // eslint-disable-next-line @typescript-eslint/ban-types
28
- export function assertStrictlyObject(value, name = 'value') {
29
- if (!isStrictlyObject(value)) {
30
- throw new TypeError(`${name} has to be an object that is not null nor array!`);
31
- }
32
- }
33
- export function assertBoolean(value, name = 'value') {
34
- if (value !== true && value !== false) throw new TypeError(`${name} is not boolean`);
35
- }
36
- export function assertInteger(value, name = 'value') {
37
- if (!isInteger(value)) throw new TypeError(`${name} is not integer`);
38
- }
39
- export function assertNonNegativeInteger(value, name = 'Value') {
40
- assertInteger(value, name);
41
- if (Number(value) < 0) throw new BeeArgumentError(`${name} has to be bigger or equal to zero`, value);
42
- }
43
- export function assertPositiveInteger(value, name = 'Value') {
44
- assertInteger(value, name);
45
- if (Number(value) <= 0) throw new BeeArgumentError(`${name} has to be bigger then zero`, value);
46
- }
47
- export function assertReference(value) {
48
- try {
49
- assertHexString(value, REFERENCE_HEX_LENGTH);
50
- } catch (e) {
51
- assertHexString(value, ENCRYPTED_REFERENCE_HEX_LENGTH);
52
- }
53
- }
54
- export function assertReferenceOrEns(value) {
55
- if (typeof value !== 'string') {
56
- throw new TypeError('ReferenceOrEns has to be a string!');
57
- }
58
- if (isHexString(value)) {
59
- assertReference(value);
60
- return;
61
- }
62
- /**
63
- * a.asdf - VALID
64
- * test.eth - VALID
65
- * ADAM.ETH - VALID
66
- * ADAM UHLIR.ETH - INVALID
67
- * test.whatever.eth - VALID
68
- * -adg.ets - INVALID
69
- * adg-.ets - INVALID
70
- * as-a.com - VALID
71
- * ethswarm.org - VALID
72
- * http://asdf.asf - INVALID
73
- * řš+ýí.šě+ř.čě - VALID
74
- * tsg.asg?asg - INVALID
75
- * tsg.asg:1599 - INVALID
76
- * ethswarm.something- - INVALID
77
- * ethswarm.-something - INVALID
78
- * ethswarm.some-thing - VALID
79
- *
80
- * The idea of this regex is to match strings that are 1 to 63 characters long and do not start or end with dash character
81
- *
82
- * This part matches 2-63 character string that does not start or end with -
83
- * [^-.\/?:\s][^.\/?:\s]{0,61}[^-.\/?:\s] <regexp1>
84
- *
85
- * For 1 character long string we use the part after |
86
- * [^-.\/?:\s] <regexp2>
87
- *
88
- * This is terminated in a group with . character an repeated at least once
89
- * (<regexp1>|<regexp2>\.)+
90
- *
91
- * This covers everything but top level domain which is 2 to 63 characters long so we can just use the <regexp2> again
92
- * ^(<regexp1>|<regexp2>\.)+<regexp1>$
93
- */
94
- const DOMAIN_REGEX = /^(?:(?:[^-.\/?:\s][^.\/?:\s]{0,61}[^-.\/?:\s]|[^-.\/?:\s]{1,2})\.)+[^-.\/?:\s][^.\/?:\s]{0,61}[^-.\/?:\s]$/;
95
- // We are doing best-effort validation of domain here. The proper way would be to do validation using IDNA UTS64 standard
96
- // but that would give us high penalty to our dependencies as the library (idna-uts46-hx) that does this validation and translation
97
- // adds 160kB minified size which is significant. We expects that full validation will be done on Bee side.
98
- if (!DOMAIN_REGEX.test(value)) {
99
- throw new TypeError('ReferenceOrEns is not valid Reference, but also not valid ENS domain.');
9
+ export function asNumberString(value, options) {
10
+ if (typeof value === 'bigint') {
11
+ value = value.toString();
100
12
  }
13
+ return Types.asIntegerString(value, options);
101
14
  }
102
- /**
103
- * Function that mainly converts Swarm CID into hex encoded Swarm Reference
104
- *
105
- * @param value
106
- * @param expectedCidType
107
- */
108
- export function makeReferenceOrEns(value, expectedCidType) {
109
- if (typeof value !== 'string') {
110
- throw new TypeError('ReferenceCidOrEns has to be a string!');
111
- }
112
- try {
113
- const result = convertCidToReference(value);
114
- if (result.type !== expectedCidType) {
115
- throw new BeeError(`CID was expected to be of type ${expectedCidType}, but got instead ${result.type ?? 'non-Swarm CID'}`);
116
- }
117
- return result.reference;
118
- } catch (e) {
119
- if (e instanceof BeeError) throw e;
120
- }
121
- assertReferenceOrEns(value);
122
- return value;
123
- }
124
- /**
125
- * Function that adds getter which converts the reference into CID base32 encoded string.
126
- * @param result
127
- * @param cidType feed or manifest
128
- */
129
- export function addCidConversionFunction(result, cidType) {
15
+ export function prepareBeeRequestOptions(value) {
16
+ const object = Types.asObject(value, {
17
+ name: 'BeeRequestOptions'
18
+ });
130
19
  return {
131
- ...result,
132
- cid() {
133
- return convertReferenceToCid(result.reference, cidType);
134
- }
20
+ baseURL: Types.asOptional(x => Types.asString(x, {
21
+ name: 'baseURL'
22
+ }), object.baseURL),
23
+ timeout: Types.asOptional(x => Types.asInteger(x, {
24
+ name: 'timeout',
25
+ min: 0
26
+ }), object.timeout),
27
+ headers: Types.asOptional(x => Types.asStringMap(x, {
28
+ name: 'headers'
29
+ }), object.headers),
30
+ onRequest: Types.asOptional(x => Types.asFunction(x, {
31
+ name: 'onRequest'
32
+ }), object.onRequest),
33
+ httpAgent: object.httpAgent,
34
+ httpsAgent: object.httpsAgent,
35
+ endlesslyRetry: Types.asOptional(x => Types.asBoolean(x, {
36
+ name: 'endlesslyRetry'
37
+ }), object.endlesslyRetry)
135
38
  };
136
39
  }
137
- export function assertAddress(value) {
138
- assertHexString(value, ADDRESS_HEX_LENGTH, 'Address');
139
- }
140
- export function assertBatchId(value) {
141
- assertHexString(value, BATCH_ID_HEX_LENGTH, 'BatchId');
40
+ export function prepareDownloadOptions(value) {
41
+ const object = Types.asObject(value, {
42
+ name: 'DownloadOptions'
43
+ });
44
+ return {
45
+ redundancyStrategy: Types.asOptional(x => Types.asInteger(x, {
46
+ name: 'redundancyStrategy'
47
+ }), object.redundancyStrategy),
48
+ fallback: Types.asOptional(x => Types.asBoolean(x, {
49
+ name: 'fallback'
50
+ }), object.fallback),
51
+ timeoutMs: Types.asOptional(x => Types.asInteger(x, {
52
+ name: 'timeoutMs',
53
+ min: 0
54
+ }), object.timeoutMs),
55
+ actPublisher: Types.asOptional(x => new PublicKey(x), object.actPublisher),
56
+ actHistoryAddress: Types.asOptional(x => new Reference(x), object.actHistoryAddress),
57
+ actTimestamp: Types.asOptional(x => Types.asNumber(x, {
58
+ name: 'actTimestamp'
59
+ }), object.actTimestamp)
60
+ };
142
61
  }
143
- export function assertRequestOptions(value, name = 'RequestOptions') {
144
- if (value === undefined) {
145
- return;
146
- }
147
- if (!isStrictlyObject(value)) {
148
- throw new TypeError(`${name} has to be an object!`);
149
- }
150
- const options = value;
151
- if (options.timeout) {
152
- assertNonNegativeInteger(options.timeout, `${name}.timeout`);
153
- }
62
+ export function prepareUploadOptions(value, name = 'UploadOptions') {
63
+ const object = Types.asObject(value, {
64
+ name
65
+ });
66
+ return {
67
+ act: Types.asOptional(x => Types.asBoolean(x, {
68
+ name: 'act'
69
+ }), object.act),
70
+ actHistoryAddress: Types.asOptional(x => new Reference(x), object.actHistoryAddress),
71
+ deferred: Types.asOptional(x => Types.asBoolean(x, {
72
+ name: 'deferred'
73
+ }), object.deferred),
74
+ encrypt: Types.asOptional(x => Types.asBoolean(x, {
75
+ name: 'encrypt'
76
+ }), object.encrypt),
77
+ pin: Types.asOptional(x => Types.asBoolean(x, {
78
+ name: 'pin'
79
+ }), object.pin),
80
+ tag: Types.asOptional(x => Types.asInteger(x, {
81
+ name: 'tag',
82
+ min: 0
83
+ }), object.tag)
84
+ };
154
85
  }
155
- export function assertUploadOptions(value, name = 'UploadOptions') {
156
- if (!isStrictlyObject(value)) {
157
- throw new TypeError(`${name} has to be an object!`);
158
- }
159
- assertRequestOptions(value, name);
160
- const options = value;
161
- if (options.pin && typeof options.pin !== 'boolean') {
162
- throw new TypeError(`options.pin property in ${name} has to be boolean or undefined!`);
163
- }
164
- if (options.encrypt && typeof options.encrypt !== 'boolean') {
165
- throw new TypeError(`options.encrypt property in ${name} has to be boolean or undefined!`);
166
- }
167
- if (options.tag) {
168
- if (typeof options.tag !== 'number') {
169
- throw new TypeError(`options.tag property in ${name} has to be number or undefined!`);
170
- }
171
- assertNonNegativeInteger(options.tag, 'options.tag');
172
- }
86
+ export function prepareRedundantUploadOptions(value, name = 'UploadOptions') {
87
+ const uploadOptions = prepareUploadOptions(value, name);
88
+ const object = Types.asObject(value, {
89
+ name
90
+ });
91
+ return {
92
+ ...uploadOptions,
93
+ redundancyLevel: Types.asOptional(x => Types.asInteger(x, {
94
+ name: 'redundancyLevel',
95
+ min: 0
96
+ }), object.redundancyLevel)
97
+ };
173
98
  }
174
- export function assertFileUploadOptions(value) {
175
- assertUploadOptions(value, 'FileUploadOptions');
176
- const options = value;
177
- if (options.size) {
178
- if (typeof options.size !== 'number') {
179
- throw new TypeError('tag property in FileUploadOptions has to be number or undefined!');
180
- }
181
- assertNonNegativeInteger(options.size, 'options.size');
182
- }
183
- if (options.contentType && typeof options.contentType !== 'string') {
184
- throw new TypeError('contentType property in FileUploadOptions has to be string or undefined!');
185
- }
99
+ export function prepareFileUploadOptions(value) {
100
+ const uploadOptions = prepareUploadOptions(value, 'FileUploadOptions');
101
+ const object = Types.asObject(value, {
102
+ name: 'FileUploadOptions'
103
+ });
104
+ return {
105
+ ...uploadOptions,
106
+ size: Types.asOptional(x => Types.asInteger(x, {
107
+ name: 'size',
108
+ min: 0
109
+ }), object.size),
110
+ contentType: Types.asOptional(x => Types.asString(x, {
111
+ name: 'contentType'
112
+ }), object.contentType),
113
+ redundancyLevel: Types.asOptional(x => Types.asInteger(x, {
114
+ name: 'redundancyLevel',
115
+ min: 0
116
+ }), object.redundancyLevel)
117
+ };
186
118
  }
187
- export function assertCollectionUploadOptions(value) {
188
- assertUploadOptions(value, 'CollectionUploadOptions');
189
- const options = value;
190
- if (options.indexDocument && typeof options.indexDocument !== 'string') {
191
- throw new TypeError('indexDocument property in CollectionUploadOptions has to be string or undefined!');
192
- }
193
- if (options.errorDocument && typeof options.errorDocument !== 'string') {
194
- throw new TypeError('errorDocument property in CollectionUploadOptions has to be string or undefined!');
195
- }
119
+ export function prepareCollectionUploadOptions(value) {
120
+ const uploadOptions = prepareUploadOptions(value, 'CollectionUploadOptions');
121
+ const object = Types.asObject(value, {
122
+ name: 'CollectionUploadOptions'
123
+ });
124
+ return {
125
+ ...uploadOptions,
126
+ errorDocument: Types.asOptional(x => Types.asString(x, {
127
+ name: 'errorDocument'
128
+ }), object.errorDocument),
129
+ indexDocument: Types.asOptional(x => Types.asString(x, {
130
+ name: 'indexDocument'
131
+ }), object.indexDocument),
132
+ redundancyLevel: Types.asOptional(x => Types.asInteger(x, {
133
+ name: 'redundancyLevel',
134
+ min: 0
135
+ }), object.redundancyLevel)
136
+ };
196
137
  }
197
138
  export function isTag(value) {
198
- if (!isStrictlyObject(value)) {
139
+ try {
140
+ const object = Types.asObject(value, {
141
+ name: 'Tag'
142
+ });
143
+ Types.asInteger(object.uid, {
144
+ name: 'Tag.uid'
145
+ });
146
+ return true;
147
+ } catch {
199
148
  return false;
200
149
  }
201
- return Boolean(value.uid);
202
- }
203
- export function assertTag(value) {
204
- if (!isStrictlyObject(value)) {
205
- throw new TypeError('Tag is not an object!');
206
- }
207
- const tag = value;
208
- const numberProperties = ['total', 'processed', 'synced', 'uid'];
209
- for (const numberProperty of numberProperties) {
210
- if (!tag[numberProperty]) {
211
- throw new TypeError(`Tag's property '${numberProperty}' has to be specified!`);
212
- }
213
- if (typeof tag[numberProperty] !== 'number') {
214
- throw new TypeError(`Tag's property '${numberProperty}' has to be number!`);
215
- }
216
- }
217
- if (!tag.startedAt) {
218
- throw new TypeError("Tag's property 'startedAt' has to be specified!");
219
- }
220
- if (typeof tag.startedAt !== 'string') {
221
- throw new TypeError("Tag's property 'startedAt' has to be string!");
222
- }
223
150
  }
224
- export function assertAddressPrefix(value) {
225
- assertHexString(value, undefined, 'AddressPrefix');
226
- if (value.length > PSS_TARGET_HEX_LENGTH_MAX) {
227
- throw new BeeArgumentError(`AddressPrefix must have length of ${PSS_TARGET_HEX_LENGTH_MAX} at most! Got string with ${value.length}`, value);
228
- }
229
- }
230
- export function assertPssMessageHandler(value) {
231
- if (!isStrictlyObject(value)) {
232
- throw new TypeError('PssMessageHandler has to be object!');
233
- }
234
- const handler = value;
235
- if (typeof handler.onMessage !== 'function') {
236
- throw new TypeError('onMessage property of PssMessageHandler has to be function!');
237
- }
238
- if (typeof handler.onError !== 'function') {
239
- throw new TypeError('onError property of PssMessageHandler has to be function!');
240
- }
241
- }
242
- export function assertPublicKey(value) {
243
- assertHexString(value, PUBKEY_HEX_LENGTH, 'PublicKey');
151
+ export function preparePssMessageHandler(value) {
152
+ const object = Types.asObject(value, {
153
+ name: 'PssMessageHandler'
154
+ });
155
+ return {
156
+ onMessage: Types.asFunction(object.onMessage, {
157
+ name: 'onMessage'
158
+ }),
159
+ onError: Types.asFunction(object.onError, {
160
+ name: 'onError'
161
+ })
162
+ };
244
163
  }
245
- export function assertPostageBatchOptions(value) {
246
- if (value === undefined) {
247
- return;
248
- }
249
- assertStrictlyObject(value);
250
- const options = value;
251
- assertRequestOptions(options, 'PostageBatchOptions');
252
- if (options?.gasPrice) {
253
- assertNonNegativeInteger(options.gasPrice);
254
- }
255
- if (options?.immutableFlag !== undefined) {
256
- assertBoolean(options.immutableFlag);
257
- }
258
- if (options?.waitForUsable !== undefined) {
259
- assertBoolean(options.waitForUsable);
260
- }
261
- if (options?.waitForUsableTimeout !== undefined) {
262
- assertNonNegativeInteger(options.waitForUsableTimeout, 'options.waitForUsableTimeout');
263
- }
164
+ export function prepareGsocMessageHandler(value) {
165
+ const object = Types.asObject(value, {
166
+ name: 'GsocMessageHandler'
167
+ });
168
+ return {
169
+ onMessage: Types.asFunction(object.onMessage, {
170
+ name: 'onMessage'
171
+ }),
172
+ onError: Types.asFunction(object.onError, {
173
+ name: 'onError'
174
+ })
175
+ };
264
176
  }
265
- export function assertTransactionOptions(value, name = 'TransactionOptions') {
266
- if (value === undefined) {
267
- return;
268
- }
269
- assertStrictlyObject(value, name);
270
- const options = value;
271
- if (options?.gasLimit) {
272
- assertNonNegativeInteger(options.gasLimit, name);
273
- }
274
- if (options?.gasPrice) {
275
- assertNonNegativeInteger(options.gasPrice, name);
276
- }
177
+ export function preparePostageBatchOptions(value) {
178
+ const object = Types.asObject(value, {
179
+ name: 'PostageBatchOptions'
180
+ });
181
+ return {
182
+ gasPrice: Types.asOptional(x => asNumberString(x, {
183
+ name: 'gasPrice'
184
+ }), object.gasPrice),
185
+ immutableFlag: Types.asOptional(x => Types.asBoolean(x, {
186
+ name: 'immutableFlag'
187
+ }), object.immutableFlag),
188
+ label: Types.asOptional(x => Types.asString(x, {
189
+ name: 'label'
190
+ }), object.label),
191
+ waitForUsable: Types.asOptional(x => Types.asBoolean(x, {
192
+ name: 'waitForUsable'
193
+ }), object.waitForUsable),
194
+ waitForUsableTimeout: Types.asOptional(x => Types.asInteger(x, {
195
+ name: 'waitForUsableTimeout',
196
+ min: 0
197
+ }), object.waitForUsableTimeout)
198
+ };
277
199
  }
278
- export function assertCashoutOptions(value) {
279
- if (value === undefined) {
280
- return;
281
- }
282
- assertStrictlyObject(value);
283
- const options = value;
284
- assertRequestOptions(options, 'CashoutOptions');
285
- assertTransactionOptions(options, 'CashoutOptions');
200
+ export function prepareTransactionOptions(value, name = 'TransactionOptions') {
201
+ const object = Types.asObject(value, {
202
+ name
203
+ });
204
+ return {
205
+ gasLimit: Types.asOptional(x => asNumberString(x, {
206
+ name: 'gasLimit',
207
+ min: 0n
208
+ }), object.gasLimit),
209
+ gasPrice: Types.asOptional(x => asNumberString(x, {
210
+ name: 'gasPrice',
211
+ min: 0n
212
+ }), object.gasPrice)
213
+ };
286
214
  }
287
215
  /**
288
216
  * Check whether the given parameter is valid data to upload
@@ -308,26 +236,21 @@ export function assertFileData(value) {
308
236
  * Checks whether optional options for AllTags query are valid
309
237
  * @param options
310
238
  */
311
- export function assertAllTagsOptions(entry) {
312
- if (entry !== undefined && !isStrictlyObject(entry)) {
313
- throw new TypeError('options has to be an object or undefined!');
314
- }
315
- assertRequestOptions(entry, 'AllTagsOptions');
316
- const options = entry;
317
- if (options?.limit !== undefined) {
318
- if (typeof options.limit !== 'number') {
319
- throw new TypeError('AllTagsOptions.limit has to be a number or undefined!');
320
- }
321
- if (options.limit < TAGS_LIMIT_MIN) {
322
- throw new BeeArgumentError(`AllTagsOptions.limit has to be at least ${TAGS_LIMIT_MIN}`, options.limit);
323
- }
324
- if (options.limit > TAGS_LIMIT_MAX) {
325
- throw new BeeArgumentError(`AllTagsOptions.limit has to be at most ${TAGS_LIMIT_MAX}`, options.limit);
326
- }
327
- }
328
- if (options?.offset !== undefined) {
329
- assertNonNegativeInteger(options.offset, 'AllTagsOptions.offset');
330
- }
239
+ export function prepareAllTagsOptions(value) {
240
+ const object = Types.asObject(value, {
241
+ name: 'AllTagsOptions'
242
+ });
243
+ return {
244
+ limit: Types.asOptional(x => Types.asInteger(x, {
245
+ name: 'limit',
246
+ min: TAGS_LIMIT_MIN,
247
+ max: TAGS_LIMIT_MAX
248
+ }), object.limit),
249
+ offset: Types.asOptional(x => Types.asInteger(x, {
250
+ name: 'offset',
251
+ min: 0
252
+ }), object.offset)
253
+ };
331
254
  }
332
255
  /**
333
256
  * Utility functions that return Tag UID
@@ -335,32 +258,15 @@ export function assertAllTagsOptions(entry) {
335
258
  */
336
259
  export function makeTagUid(tagUid) {
337
260
  if (tagUid === undefined || tagUid === null) {
338
- throw new TypeError('TagUid was expected but got undefined or null instead!');
261
+ throw new TypeError(`Expected number | Tag | string from tagUid, got: ${tagUid}`);
339
262
  }
340
263
  if (isTag(tagUid)) {
341
264
  return tagUid.uid;
342
- } else if (typeof tagUid === 'number') {
343
- assertNonNegativeInteger(tagUid, 'UID');
344
- return tagUid;
345
- } else if (typeof tagUid === 'string') {
346
- const int = parseInt(tagUid);
347
- if (isNaN(int)) {
348
- throw new TypeError('Passed tagUid string is not valid integer!');
349
- }
350
- if (int < 0) {
351
- throw new TypeError(`TagUid was expected to be positive non-negative integer! Got ${int}`);
352
- }
353
- return int;
354
- }
355
- throw new TypeError('tagUid has to be either Tag or a number (UID)!');
356
- }
357
- export function assertTransactionHash(transactionHash) {
358
- if (typeof transactionHash !== 'string') {
359
- throw new TypeError('TransactionHash has to be a string!');
360
- }
361
- assertPrefixedHexString(transactionHash, 'TransactionHash');
362
- // Hash is 64 long + '0x' prefix = 66
363
- if (transactionHash.length !== 66) {
364
- throw new TypeError('TransactionHash has to be prefixed hex string with total length 66 (prefix including)');
265
+ } else if (typeof tagUid === 'number' || typeof tagUid === 'string') {
266
+ return Types.asNumber(tagUid, {
267
+ name: 'tagUid',
268
+ min: 0
269
+ });
365
270
  }
271
+ throw new TypeError(`Expected number | Tag | string from tagUid, got: ${tagUid}`);
366
272
  }