@ethersphere/bee-js 1.2.1 → 3.0.0

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 (181) hide show
  1. package/CHANGELOG.md +135 -2
  2. package/README.md +3 -1
  3. package/dist/index.browser.min.js +1 -1
  4. package/dist/index.browser.min.js.LICENSE.txt +51 -0
  5. package/dist/index.browser.min.js.map +1 -1
  6. package/dist/index.min.js +1 -1
  7. package/dist/index.min.js.LICENSE.txt +42 -0
  8. package/dist/index.min.js.map +1 -1
  9. package/dist/src/bee-debug.d.ts +75 -38
  10. package/dist/src/bee.d.ts +88 -82
  11. package/dist/src/chunk/soc.d.ts +6 -6
  12. package/dist/src/feed/index.d.ts +7 -7
  13. package/dist/src/feed/json.d.ts +2 -2
  14. package/dist/src/modules/bytes.d.ts +8 -11
  15. package/dist/src/modules/bzz.d.ts +9 -14
  16. package/dist/src/modules/chunk.d.ts +7 -11
  17. package/dist/src/modules/debug/balance.d.ts +9 -9
  18. package/dist/src/modules/debug/chequebook.d.ts +17 -18
  19. package/dist/src/modules/debug/chunk.d.ts +5 -5
  20. package/dist/src/modules/debug/connectivity.d.ts +7 -7
  21. package/dist/src/modules/debug/settlements.d.ts +5 -5
  22. package/dist/src/modules/debug/stamps.d.ts +7 -5
  23. package/dist/src/modules/debug/states.d.ts +5 -5
  24. package/dist/src/modules/debug/status.d.ts +5 -4
  25. package/dist/src/modules/debug/tag.d.ts +3 -3
  26. package/dist/src/modules/debug/transactions.d.ts +9 -9
  27. package/dist/src/modules/feed.d.ts +5 -5
  28. package/dist/src/modules/pinning.d.ts +9 -9
  29. package/dist/src/modules/pss.d.ts +4 -3
  30. package/dist/src/modules/soc.d.ts +3 -3
  31. package/dist/src/modules/status.d.ts +3 -2
  32. package/dist/src/modules/stewardship.d.ts +4 -4
  33. package/dist/src/modules/tag.d.ts +6 -6
  34. package/dist/src/types/debug.d.ts +4 -2
  35. package/dist/src/types/index.d.ts +71 -19
  36. package/dist/src/utils/bytes.d.ts +1 -1
  37. package/dist/src/utils/data.browser.d.ts +3 -1
  38. package/dist/src/utils/data.d.ts +7 -4
  39. package/dist/src/utils/expose.d.ts +6 -9
  40. package/dist/src/utils/headers.d.ts +3 -3
  41. package/dist/src/utils/http.d.ts +25 -0
  42. package/dist/src/utils/merge.d.ts +8 -0
  43. package/dist/src/utils/pss.d.ts +9 -0
  44. package/dist/src/utils/stream.d.ts +37 -0
  45. package/dist/src/utils/type.d.ts +19 -9
  46. package/package.json +15 -8
  47. package/.babelrc.js +0 -33
  48. package/.depcheckrc.json +0 -27
  49. package/.editorconfig +0 -6
  50. package/.eslintignore +0 -1
  51. package/.eslintrc.js +0 -108
  52. package/.gitattributes +0 -1
  53. package/.github/config.yaml +0 -23
  54. package/.github/workflows/check.yaml +0 -57
  55. package/.github/workflows/publish_npmjs.yaml +0 -20
  56. package/.github/workflows/release_github.yaml +0 -20
  57. package/.github/workflows/tests.yaml +0 -128
  58. package/.huskyrc +0 -5
  59. package/.prettierrc +0 -13
  60. package/.vscode/launch.json +0 -49
  61. package/.vscode/settings.json +0 -39
  62. package/CODEOWNERS +0 -1
  63. package/CODE_OF_CONDUCT.md +0 -76
  64. package/commitlint.config.js +0 -3
  65. package/dist/src/modules/stamps.d.ts +0 -4
  66. package/dist/src/utils/hooks.d.ts +0 -31
  67. package/dist/src/utils/safe-axios.d.ts +0 -8
  68. package/jest-puppeteer.config.js +0 -5
  69. package/jest.config.ts +0 -170
  70. package/src/bee-debug.ts +0 -440
  71. package/src/bee.ts +0 -941
  72. package/src/chunk/bmt.ts +0 -55
  73. package/src/chunk/cac.ts +0 -79
  74. package/src/chunk/serialize.ts +0 -16
  75. package/src/chunk/signer.ts +0 -141
  76. package/src/chunk/soc.ts +0 -190
  77. package/src/chunk/span.ts +0 -33
  78. package/src/feed/index.ts +0 -221
  79. package/src/feed/json.ts +0 -32
  80. package/src/feed/topic.ts +0 -23
  81. package/src/feed/type.ts +0 -12
  82. package/src/index.ts +0 -24
  83. package/src/modules/bytes.ts +0 -75
  84. package/src/modules/bzz.ts +0 -184
  85. package/src/modules/chunk.ts +0 -75
  86. package/src/modules/debug/balance.ts +0 -63
  87. package/src/modules/debug/chequebook.ts +0 -174
  88. package/src/modules/debug/chunk.ts +0 -39
  89. package/src/modules/debug/connectivity.ts +0 -61
  90. package/src/modules/debug/settlements.ts +0 -33
  91. package/src/modules/debug/stamps.ts +0 -69
  92. package/src/modules/debug/states.ts +0 -35
  93. package/src/modules/debug/status.ts +0 -34
  94. package/src/modules/debug/tag.ts +0 -19
  95. package/src/modules/debug/transactions.ts +0 -79
  96. package/src/modules/feed.ts +0 -92
  97. package/src/modules/pinning.ts +0 -74
  98. package/src/modules/pss.ts +0 -48
  99. package/src/modules/soc.ts +0 -41
  100. package/src/modules/stamps.ts +0 -59
  101. package/src/modules/status.ts +0 -13
  102. package/src/modules/stewardship.ts +0 -21
  103. package/src/modules/tag.ts +0 -83
  104. package/src/types/axios/index.d.ts +0 -18
  105. package/src/types/debug.ts +0 -204
  106. package/src/types/index.ts +0 -497
  107. package/src/types/tar-js.d.ts +0 -1
  108. package/src/types/webpack-bundle-analyzer/index.d.ts +0 -13
  109. package/src/utils/bytes.ts +0 -144
  110. package/src/utils/collection.ts +0 -151
  111. package/src/utils/data.browser.ts +0 -24
  112. package/src/utils/data.ts +0 -35
  113. package/src/utils/error.ts +0 -23
  114. package/src/utils/eth.ts +0 -229
  115. package/src/utils/expose.ts +0 -10
  116. package/src/utils/file.ts +0 -40
  117. package/src/utils/hash.ts +0 -16
  118. package/src/utils/headers.ts +0 -63
  119. package/src/utils/hex.ts +0 -186
  120. package/src/utils/hooks.ts +0 -89
  121. package/src/utils/safe-axios.ts +0 -98
  122. package/src/utils/tar.ts +0 -30
  123. package/src/utils/type.ts +0 -302
  124. package/src/utils/uint64.ts +0 -31
  125. package/src/utils/url.ts +0 -52
  126. package/test/bee.sh +0 -187
  127. package/test/data/1.txt +0 -1
  128. package/test/data/2.txt +0 -0
  129. package/test/data/empty +0 -0
  130. package/test/data/sub/3.txt +0 -1
  131. package/test/data/sub//303/251 +0 -0
  132. package/test/data/sub//360/237/230/216 +0 -0
  133. package/test/integration/bee-class.browser.spec.ts +0 -215
  134. package/test/integration/bee-class.spec.ts +0 -595
  135. package/test/integration/bee-debug-class.spec.ts +0 -85
  136. package/test/integration/chunk/bmt.spec.ts +0 -33
  137. package/test/integration/chunk/cac.spec.ts +0 -27
  138. package/test/integration/chunk/soc.spec.ts +0 -38
  139. package/test/integration/feed/index.spec.ts +0 -84
  140. package/test/integration/modules/bytes.spec.ts +0 -23
  141. package/test/integration/modules/bzz.spec.ts +0 -270
  142. package/test/integration/modules/chunk.spec.ts +0 -29
  143. package/test/integration/modules/debug/balance.spec.ts +0 -70
  144. package/test/integration/modules/debug/chequebook.spec.ts +0 -64
  145. package/test/integration/modules/debug/connectivity.spec.ts +0 -69
  146. package/test/integration/modules/debug/settlements.spec.ts +0 -34
  147. package/test/integration/modules/debug/states.spec.ts +0 -34
  148. package/test/integration/modules/debug/status.spec.ts +0 -65
  149. package/test/integration/modules/debug/tag.spec.ts +0 -27
  150. package/test/integration/modules/feed.spec.ts +0 -61
  151. package/test/integration/modules/pinning.spec.ts +0 -155
  152. package/test/integration/modules/pss.spec.ts +0 -92
  153. package/test/integration/modules/status.spec.ts +0 -10
  154. package/test/integration/modules/stewardship.spec.ts +0 -32
  155. package/test/integration/modules/tag.spec.ts +0 -51
  156. package/test/test-type-sequencer.js +0 -36
  157. package/test/testpage/testpage.html +0 -20
  158. package/test/unit/assertions.ts +0 -376
  159. package/test/unit/bee-class.spec.ts +0 -685
  160. package/test/unit/bee-debug-class.spec.ts +0 -365
  161. package/test/unit/chunk/bmt.spec.ts +0 -16
  162. package/test/unit/chunk/cac.spec.ts +0 -29
  163. package/test/unit/chunk/serialize.spec.ts +0 -24
  164. package/test/unit/chunk/signer.spec.ts +0 -140
  165. package/test/unit/chunk/soc.spec.ts +0 -25
  166. package/test/unit/chunk/span.spec.ts +0 -38
  167. package/test/unit/feed/json.spec.ts +0 -73
  168. package/test/unit/nock.ts +0 -134
  169. package/test/unit/utils/bytes.spec.ts +0 -32
  170. package/test/unit/utils/collection.spec.ts +0 -22
  171. package/test/unit/utils/eth.spec.ts +0 -224
  172. package/test/unit/utils/hex.spec.ts +0 -115
  173. package/test/unit/utils/hooks.spec.ts +0 -179
  174. package/test/unit/utils/safe-axios.spec.ts +0 -20
  175. package/test/unit/utils/type.spec.ts +0 -32
  176. package/test/unit/utils/uint64.spec.ts +0 -102
  177. package/test/utils.ts +0 -292
  178. package/tsconfig.json +0 -18
  179. package/tsconfig.test.json +0 -11
  180. package/typedoc.json +0 -7
  181. package/webpack.config.ts +0 -141
package/src/utils/hex.ts DELETED
@@ -1,186 +0,0 @@
1
- import { Bytes, makeBytes } from './bytes'
2
- import { BrandedType, FlavoredType } from '../types'
3
-
4
- /**
5
- * Nominal type to represent hex strings WITHOUT '0x' prefix.
6
- * For example for 32 bytes hex representation you have to use 64 length.
7
- * TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
8
- */
9
- export type HexString<Length extends number = number> = FlavoredType<
10
- string & {
11
- readonly length: Length
12
- },
13
- 'HexString'
14
- >
15
-
16
- /**
17
- * Type for HexString with prefix.
18
- * The main hex type used internally should be non-prefixed HexString
19
- * and therefore this type should be used as least as possible.
20
- * Because of that it does not contain the Length property as the variables
21
- * should be validated and converted to HexString ASAP.
22
- */
23
- export type PrefixedHexString = BrandedType<string, 'PrefixedHexString'>
24
-
25
- /**
26
- * Creates unprefixed hex string from wide range of data.
27
- *
28
- * TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
29
- *
30
- * @param input
31
- * @param len of the resulting HexString WITHOUT prefix!
32
- */
33
- export function makeHexString<L extends number>(input: string | number | Uint8Array | unknown, len?: L): HexString<L> {
34
- if (typeof input === 'number') {
35
- return intToHex<L>(input, len)
36
- }
37
-
38
- if (input instanceof Uint8Array) {
39
- return bytesToHex<L>(input, len)
40
- }
41
-
42
- if (typeof input === 'string') {
43
- if (isPrefixedHexString(input)) {
44
- const hex = input.slice(2) as HexString<L>
45
-
46
- if (len && hex.length !== len) {
47
- throw new TypeError(`Length mismatch for valid hex string. Expecting length ${len}: ${hex}`)
48
- }
49
-
50
- return hex
51
- } else {
52
- // We use assertHexString() as there might be more reasons why a string is not valid hex string
53
- // and usage of isHexString() would not give enough information to the user on what is going
54
- // wrong.
55
- assertHexString<L>(input, len)
56
-
57
- return input
58
- }
59
- }
60
-
61
- throw new TypeError('Not HexString compatible type!')
62
- }
63
-
64
- /**
65
- * Converts a hex string to Uint8Array
66
- *
67
- * @param hex string input without 0x prefix!
68
- */
69
- export function hexToBytes<Length extends number, LengthHex extends number = number>(
70
- hex: HexString<LengthHex>,
71
- ): Bytes<Length> {
72
- assertHexString(hex)
73
-
74
- const bytes = makeBytes(hex.length / 2)
75
- for (let i = 0; i < bytes.length; i++) {
76
- const hexByte = hex.substr(i * 2, 2)
77
- bytes[i] = parseInt(hexByte, 16)
78
- }
79
-
80
- return bytes as Bytes<Length>
81
- }
82
-
83
- /**
84
- * Converts array of number or Uint8Array to HexString without prefix.
85
- *
86
- * @param bytes The input array
87
- * @param len The length of the non prefixed HexString
88
- */
89
- export function bytesToHex<Length extends number = number>(bytes: Uint8Array, len?: Length): HexString<Length> {
90
- const hexByte = (n: number) => n.toString(16).padStart(2, '0')
91
- const hex = Array.from(bytes, hexByte).join('') as HexString<Length>
92
-
93
- // TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
94
- if (len && hex.length !== len) {
95
- throw new TypeError(`Resulting HexString does not have expected length ${len}: ${hex}`)
96
- }
97
-
98
- return hex
99
- }
100
-
101
- /**
102
- * Converts integer number to hex string.
103
- *
104
- * Optionally provides '0x' prefix or padding
105
- *
106
- * @param int The positive integer to be converted
107
- * @param len The length of the non prefixed HexString
108
- */
109
- export function intToHex<Length extends number = number>(int: number, len?: Length): HexString<Length> {
110
- if (!Number.isInteger(int)) throw new TypeError('the value provided is not integer')
111
-
112
- if (int > Number.MAX_SAFE_INTEGER) throw new TypeError('the value provided exceeds safe integer')
113
-
114
- if (int < 0) throw new TypeError('the value provided is a negative integer')
115
- const hex = int.toString(16) as HexString<Length>
116
-
117
- // TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
118
- if (len && hex.length !== len) {
119
- throw new TypeError(`Resulting HexString does not have expected length ${len}: ${hex}`)
120
- }
121
-
122
- return hex
123
- }
124
-
125
- /**
126
- * Type guard for HexStrings.
127
- * Requires no 0x prefix!
128
- *
129
- * TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
130
- *
131
- * @param s string input
132
- * @param len expected length of the HexString
133
- */
134
- export function isHexString<Length extends number = number>(s: unknown, len?: number): s is HexString<Length> {
135
- return typeof s === 'string' && /^[0-9a-f]+$/i.test(s) && (!len || s.length === len)
136
- }
137
-
138
- /**
139
- * Type guard for PrefixedHexStrings.
140
- * Does enforce presence of 0x prefix!
141
- *
142
- * @param s string input
143
- */
144
- export function isPrefixedHexString(s: unknown): s is PrefixedHexString {
145
- return typeof s === 'string' && /^0x[0-9a-f]+$/i.test(s)
146
- }
147
-
148
- /**
149
- * Verifies if the provided input is a HexString.
150
- *
151
- * TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
152
- *
153
- * @param s string input
154
- * @param len expected length of the HexString
155
- * @param name optional name for the asserted value
156
- * @returns HexString or throws error
157
- */
158
- export function assertHexString<Length extends number = number>(
159
- s: unknown,
160
- len?: number,
161
- name = 'value',
162
- ): asserts s is HexString<Length> {
163
- if (!isHexString(s, len)) {
164
- if (isPrefixedHexString(s)) {
165
- throw new TypeError(`${name} not valid non prefixed hex string (has 0x prefix): ${s}`)
166
- }
167
-
168
- // Don't display length error if no length specified in order not to confuse user
169
- const lengthMsg = len ? ` of length ${len}` : ''
170
- throw new TypeError(`${name} not valid hex string${lengthMsg}: ${s}`)
171
- }
172
- }
173
-
174
- /**
175
- * Verifies if the provided input is a PrefixedHexString.
176
- *
177
- * @param s string input
178
- * @param len expected length of the HexString
179
- * @param name optional name for the asserted value
180
- * @returns HexString or throws error
181
- */
182
- export function assertPrefixedHexString(s: string, name = 'value'): asserts s is PrefixedHexString {
183
- if (!isPrefixedHexString(s)) {
184
- throw new TypeError(`${name} not valid prefixed hex string: ${s}`)
185
- }
186
- }
@@ -1,89 +0,0 @@
1
- import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'
2
- import { BeeRequest, BeeResponse, HttpMethod } from '../types'
3
-
4
- type HookCallback<V> = (value: V) => V | Promise<V>
5
-
6
- function wrapRequest(request: AxiosRequestConfig): BeeRequest {
7
- let headers = request.headers
8
-
9
- // For request interceptor, axios returns also default headers in the object that needs to be joined based on HTTP method
10
- // and the default headers removed
11
- if (headers.common && headers.get && headers.delete && headers.post && headers.patch) {
12
- // Filters out the default headers properties
13
- const { common: _1, get: _2, delete: _3, post: _4, patch: _5, head: _6, put: _7, ...rest } = headers
14
-
15
- headers = Object.assign({}, request.headers.common ?? {}, request.headers[request.method as HttpMethod] ?? {}, rest)
16
- }
17
-
18
- return {
19
- url: `${request.url}`,
20
- method: request.method as HttpMethod,
21
- params: request.params,
22
- headers,
23
- data: request.data,
24
- }
25
- }
26
-
27
- function wrapRequestClosure(
28
- cb: HookCallback<BeeRequest>,
29
- ): (request: AxiosRequestConfig) => Promise<AxiosRequestConfig> {
30
- return async (request: AxiosRequestConfig) => {
31
- await cb(wrapRequest(request))
32
-
33
- return request
34
- }
35
- }
36
-
37
- function wrapResponseClosure(cb: HookCallback<BeeResponse>): (response: AxiosResponse) => Promise<AxiosResponse> {
38
- return async (response: AxiosResponse) => {
39
- await cb({
40
- headers: response.headers,
41
- data: response.data,
42
- status: response.status,
43
- statusText: response.statusText,
44
- request: wrapRequest(response.config),
45
- })
46
-
47
- return response
48
- }
49
- }
50
-
51
- /**
52
- * Function that registers listener callback for all outgoing HTTP requests that bee-js makes.
53
- *
54
- * **Be Aware! This listener listens to all Bee/BeeDebug class instances without differentiation!**
55
- *
56
- * @param cb
57
- * @returns ID of the listener that can be used to stop the callback to receive new requests
58
- */
59
- export function onRequest(cb: HookCallback<BeeRequest>): number {
60
- return axios.interceptors.request.use(wrapRequestClosure(cb))
61
- }
62
-
63
- /**
64
- * Function that stops forwarding new requests to associated listener callback
65
- * @param id ID that onRequest returned
66
- */
67
- export function clearOnRequest(id: number): void {
68
- axios.interceptors.request.eject(id)
69
- }
70
-
71
- /**
72
- * Function that registers listener callback for all incoming HTTP responses that bee-js made.
73
- *
74
- * **Be Aware! This listener listens to all Bee/BeeDebug class instances without differentiation!**
75
- *
76
- * @param cb
77
- * @returns ID of the listener that can be used to stop the callback to receive new responses
78
- */
79
- export function onResponse(cb: HookCallback<BeeResponse>): number {
80
- return axios.interceptors.response.use(wrapResponseClosure(cb))
81
- }
82
-
83
- /**
84
- * Function that stops forwarding new responses to associated listener callback
85
- * @param id ID that onResponse returned
86
- */
87
- export function clearOnResponse(id: number): void {
88
- axios.interceptors.response.eject(id)
89
- }
@@ -1,98 +0,0 @@
1
- import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'
2
- import { BeeError, BeeRequestError, BeeResponseError } from './error'
3
-
4
- import utils from 'axios/lib/utils'
5
- import normalizeHeaderName from 'axios/lib/helpers/normalizeHeaderName'
6
- axios.defaults.adapter = require('axios/lib/adapters/http') // https://stackoverflow.com/a/57320262
7
-
8
- /**
9
- * Utility function that sets passed headers to ALL axios calls without distinction of Bee URLs.
10
- *
11
- * @param headers
12
- */
13
- export function setDefaultHeaders(headers: Record<string, string>): void {
14
- axios.defaults.headers.common = headers
15
- }
16
-
17
- /**
18
- * Utility function from axios's implementation
19
- * https://github.com/axios/axios/blob/d99d5faac29899eba68ce671e6b3cbc9832e9ad8/lib/defaults.js
20
- *
21
- * @param headers
22
- * @param value
23
- */
24
- function setContentTypeIfUnset(headers: Record<string, unknown>, value: string) {
25
- if (!utils.isUndefined(headers) && utils.isUndefined(headers['Content-Type'])) {
26
- headers['Content-Type'] = value
27
- }
28
- }
29
-
30
- /**
31
- * Taken over from axios's implementation
32
- * https://github.com/axios/axios/blob/d99d5faac29899eba68ce671e6b3cbc9832e9ad8/lib/defaults.js
33
- *
34
- * Unfortunately we can't use the default implementation as fallback one and override only the JSON serialization as the JSON.stringify
35
- * is a last thing to do as all the previous cases (buffer, file etc.) are objects as well
36
- * so they have to be ruled out first before trying JSON.stringify.
37
- *
38
- * @param data
39
- * @param headers
40
- */
41
- function transformRequest(data: unknown, headers: Record<string, unknown>): string | unknown {
42
- normalizeHeaderName(headers, 'Accept')
43
- normalizeHeaderName(headers, 'Content-Type')
44
-
45
- if (
46
- utils.isFormData(data) ||
47
- utils.isArrayBuffer(data) ||
48
- utils.isBuffer(data) ||
49
- utils.isStream(data) ||
50
- utils.isFile(data) ||
51
- utils.isBlob(data)
52
- ) {
53
- return data
54
- }
55
-
56
- if (utils.isArrayBufferView(data)) {
57
- return data.buffer
58
- }
59
-
60
- if (utils.isURLSearchParams(data)) {
61
- setContentTypeIfUnset(headers, 'application/x-www-form-urlencoded;charset=utf-8')
62
-
63
- return data.toString()
64
- }
65
-
66
- if (utils.isObject(data) || (headers && headers['Content-Type'] === 'application/json')) {
67
- setContentTypeIfUnset(headers, 'application/json')
68
-
69
- return JSON.stringify(data)
70
- }
71
-
72
- return data
73
- }
74
-
75
- export async function safeAxios<T>(config: AxiosRequestConfig): Promise<AxiosResponse<T>> {
76
- try {
77
- const response = await axios({
78
- maxContentLength: Infinity,
79
- maxBodyLength: Infinity,
80
- transformRequest,
81
- ...config,
82
- })
83
-
84
- return response
85
- } catch (e) {
86
- if (e.response) {
87
- if (e.response.data?.message) {
88
- throw new BeeResponseError(e.response.status, `${e.response.statusText}: ${e.response.data.message}`)
89
- } else {
90
- throw new BeeResponseError(e.response.status, e.response.statusText)
91
- }
92
- } else if (e.request) {
93
- throw new BeeRequestError(e.message)
94
- } else {
95
- throw new BeeError(e.message)
96
- }
97
- }
98
- }
package/src/utils/tar.ts DELETED
@@ -1,30 +0,0 @@
1
- import { Collection } from '../types'
2
- import Tar from 'tar-js'
3
-
4
- // this is a workaround type so that we are able to pass in Uint8Arrays
5
- // as string to `tar.append`
6
- interface StringLike {
7
- readonly length: number
8
- charCodeAt: (index: number) => number
9
- }
10
-
11
- // converts a string to utf8 Uint8Array and returns it as a string-like
12
- // object that `tar.append` accepts as path
13
- function fixUnicodePath(path: string): StringLike {
14
- const codes = new TextEncoder().encode(path)
15
-
16
- return {
17
- length: codes.length,
18
- charCodeAt: index => codes[index],
19
- }
20
- }
21
-
22
- export function makeTar(data: Collection<Uint8Array>): Uint8Array {
23
- const tar = new Tar()
24
- for (const entry of data) {
25
- const path = fixUnicodePath(entry.path)
26
- tar.append(path, entry.data)
27
- }
28
-
29
- return tar.out
30
- }
package/src/utils/type.ts DELETED
@@ -1,302 +0,0 @@
1
- import { Readable } from 'stream'
2
- import {
3
- Address,
4
- AddressPrefix,
5
- ADDRESS_HEX_LENGTH,
6
- BatchId,
7
- BATCH_ID_HEX_LENGTH,
8
- CollectionUploadOptions,
9
- ENCRYPTED_REFERENCE_HEX_LENGTH,
10
- FileUploadOptions,
11
- NumberString,
12
- PssMessageHandler,
13
- PUBKEY_HEX_LENGTH,
14
- PublicKey,
15
- Reference,
16
- REFERENCE_HEX_LENGTH,
17
- Tag,
18
- UploadOptions,
19
- AllTagsOptions,
20
- TAGS_LIMIT_MIN,
21
- TAGS_LIMIT_MAX,
22
- TransactionHash,
23
- } from '../types'
24
- import { BeeArgumentError } from './error'
25
- import { isFile } from './file'
26
- import { assertHexString, assertPrefixedHexString } from './hex'
27
-
28
- export function isReadable(entry: unknown): entry is Readable {
29
- return (
30
- typeof entry === 'object' &&
31
- entry !== null &&
32
- typeof (entry as Readable).pipe === 'function' &&
33
- (entry as Readable).readable &&
34
- typeof (entry as Readable)._read === 'function'
35
- )
36
- }
37
-
38
- export function isUint8Array(obj: unknown): obj is Uint8Array {
39
- return obj instanceof Uint8Array
40
- }
41
-
42
- export function isInteger(value: unknown): value is number | NumberString {
43
- return (
44
- (typeof value === 'string' && /^-?(0|[1-9][0-9]*)$/g.test(value)) ||
45
- (typeof value === 'number' &&
46
- value > Number.MIN_SAFE_INTEGER &&
47
- value < Number.MAX_SAFE_INTEGER &&
48
- Number.isInteger(value))
49
- )
50
- }
51
-
52
- export function isObject(value: unknown): value is Record<string, unknown> {
53
- return value !== null && typeof value === 'object'
54
- }
55
-
56
- export function isStrictlyObject(value: unknown): value is Record<string, unknown> {
57
- return isObject(value) && !Array.isArray(value)
58
- }
59
-
60
- export function assertBoolean(value: unknown): asserts value is boolean {
61
- if (value !== true && value !== false) throw new TypeError('value is not boolean')
62
- }
63
-
64
- export function assertInteger(value: unknown): asserts value is number | NumberString {
65
- if (!isInteger(value)) throw new TypeError('value is not integer')
66
- }
67
-
68
- export function assertNonNegativeInteger(value: unknown, name = 'Value'): asserts value is number | NumberString {
69
- assertInteger(value)
70
-
71
- if (Number(value) < 0) throw new BeeArgumentError(`${name} has to be bigger or equal to zero`, value)
72
- }
73
-
74
- export function assertReference(value: unknown): asserts value is Reference {
75
- try {
76
- assertHexString(value, REFERENCE_HEX_LENGTH)
77
- } catch (e) {
78
- assertHexString(value, ENCRYPTED_REFERENCE_HEX_LENGTH)
79
- }
80
- }
81
-
82
- export function assertAddress(value: unknown): asserts value is Address {
83
- assertHexString(value, ADDRESS_HEX_LENGTH, 'Address')
84
- }
85
-
86
- export function assertBatchId(value: unknown): asserts value is BatchId {
87
- assertHexString(value, BATCH_ID_HEX_LENGTH, 'BatchId')
88
- }
89
-
90
- export function assertUploadOptions(value: unknown, name = 'UploadOptions'): asserts value is UploadOptions {
91
- if (!isStrictlyObject(value)) {
92
- throw new TypeError(`${name} has to be an object!`)
93
- }
94
-
95
- const options = value as UploadOptions
96
-
97
- if (options.pin && typeof options.pin !== 'boolean') {
98
- throw new TypeError(`options.pin property in ${name} has to be boolean or undefined!`)
99
- }
100
-
101
- if (options.encrypt && typeof options.encrypt !== 'boolean') {
102
- throw new TypeError(`options.encrypt property in ${name} has to be boolean or undefined!`)
103
- }
104
-
105
- if (options.tag) {
106
- if (typeof options.tag !== 'number') {
107
- throw new TypeError(`options.tag property in ${name} has to be number or undefined!`)
108
- }
109
-
110
- assertNonNegativeInteger(options.tag, 'options.tag')
111
- }
112
-
113
- if (options.axiosOptions && !isStrictlyObject(options.axiosOptions)) {
114
- throw new TypeError(`options.axiosOptions property in ${name} has to be object or undefined!`)
115
- }
116
- }
117
-
118
- export function assertFileUploadOptions(value: unknown): asserts value is FileUploadOptions {
119
- assertUploadOptions(value, 'FileUploadOptions')
120
-
121
- const options = value as FileUploadOptions
122
-
123
- if (options.size) {
124
- if (typeof options.size !== 'number') {
125
- throw new TypeError('tag property in FileUploadOptions has to be number or undefined!')
126
- }
127
-
128
- assertNonNegativeInteger(options.size, 'options.size')
129
- }
130
-
131
- if (options.contentType && typeof options.contentType !== 'string') {
132
- throw new TypeError('contentType property in FileUploadOptions has to be string or undefined!')
133
- }
134
- }
135
-
136
- export function assertCollectionUploadOptions(value: unknown): asserts value is CollectionUploadOptions {
137
- assertUploadOptions(value, 'CollectionUploadOptions')
138
-
139
- const options = value as CollectionUploadOptions
140
-
141
- if (options.indexDocument && typeof options.indexDocument !== 'string') {
142
- throw new TypeError('indexDocument property in CollectionUploadOptions has to be string or undefined!')
143
- }
144
-
145
- if (options.errorDocument && typeof options.errorDocument !== 'string') {
146
- throw new TypeError('errorDocument property in CollectionUploadOptions has to be string or undefined!')
147
- }
148
- }
149
-
150
- export function isTag(value: unknown): value is Tag {
151
- if (!isStrictlyObject(value)) {
152
- return false
153
- }
154
-
155
- const tag = value as Record<string, unknown>
156
-
157
- const numberProperties = ['total', 'processed', 'synced', 'uid']
158
- const correctNumberProperties = numberProperties.every(numberProperty => typeof tag[numberProperty] === 'number')
159
-
160
- if (!correctNumberProperties || !tag.startedAt || typeof tag.startedAt !== 'string') {
161
- return false
162
- }
163
-
164
- return true
165
- }
166
-
167
- export function assertTag(value: unknown): asserts value is Tag {
168
- if (!isStrictlyObject(value)) {
169
- throw new TypeError('Tag is not an object!')
170
- }
171
-
172
- const tag = value as Record<string, unknown>
173
-
174
- const numberProperties = ['total', 'processed', 'synced', 'uid']
175
- for (const numberProperty of numberProperties) {
176
- if (!tag[numberProperty]) {
177
- throw new TypeError(`Tag's property '${numberProperty}' has to be specified!`)
178
- }
179
-
180
- if (typeof tag[numberProperty] !== 'number') {
181
- throw new TypeError(`Tag's property '${numberProperty}' has to be number!`)
182
- }
183
- }
184
-
185
- if (!tag.startedAt) {
186
- throw new TypeError("Tag's property 'startedAt' has to be specified!")
187
- }
188
-
189
- if (typeof tag.startedAt !== 'string') {
190
- throw new TypeError("Tag's property 'startedAt' has to be string!")
191
- }
192
- }
193
-
194
- export function assertAddressPrefix(value: unknown): asserts value is AddressPrefix {
195
- assertHexString(value, undefined, 'AddressPrefix')
196
-
197
- if (value.length > ADDRESS_HEX_LENGTH) {
198
- throw new BeeArgumentError(
199
- `AddressPrefix must have length of ${ADDRESS_HEX_LENGTH} at most! Got string with ${value.length}`,
200
- value,
201
- )
202
- }
203
- }
204
-
205
- export function assertPssMessageHandler(value: unknown): asserts value is PssMessageHandler {
206
- if (!isStrictlyObject(value)) {
207
- throw new TypeError('PssMessageHandler has to be object!')
208
- }
209
-
210
- const handler = value as unknown as PssMessageHandler
211
-
212
- if (typeof handler.onMessage !== 'function') {
213
- throw new TypeError('onMessage property of PssMessageHandler has to be function!')
214
- }
215
-
216
- if (typeof handler.onError !== 'function') {
217
- throw new TypeError('onError property of PssMessageHandler has to be function!')
218
- }
219
- }
220
-
221
- export function assertPublicKey(value: unknown): asserts value is PublicKey {
222
- assertHexString(value, PUBKEY_HEX_LENGTH, 'PublicKey')
223
- }
224
-
225
- /**
226
- * Check whether the given parameter is valid data to upload
227
- * @param value
228
- * @throws TypeError if not valid
229
- */
230
- export function assertData(value: unknown): asserts value is string | Uint8Array {
231
- if (typeof value !== 'string' && !(value instanceof Uint8Array)) {
232
- throw new TypeError('Data must be either string or Uint8Array!')
233
- }
234
- }
235
-
236
- /**
237
- * Check whether the given parameter is a correct file representation to file upload.
238
- * @param value
239
- * @throws TypeError if not valid
240
- */
241
- export function assertFileData(value: unknown): asserts value is string | Uint8Array | Readable | File {
242
- if (typeof value !== 'string' && !(value instanceof Uint8Array) && !isFile(value) && !isReadable(value)) {
243
- throw new TypeError('Data must be either string, Readable, Uint8Array or File!')
244
- }
245
- }
246
-
247
- /**
248
- * Checks whether optional options for AllTags query are valid
249
- * @param options
250
- */
251
- export function assertAllTagsOptions(options: unknown): asserts options is AllTagsOptions {
252
- if (options !== undefined && !isStrictlyObject(options)) {
253
- throw new TypeError('options has to be an object or undefined!')
254
- }
255
-
256
- if (options?.limit !== undefined) {
257
- if (typeof options.limit !== 'number') {
258
- throw new TypeError('options.limit has to be a number or undefined!')
259
- }
260
-
261
- if (options.limit < TAGS_LIMIT_MIN) {
262
- throw new BeeArgumentError(`options.limit has to be at least ${TAGS_LIMIT_MIN}`, options.limit)
263
- }
264
-
265
- if (options.limit > TAGS_LIMIT_MAX) {
266
- throw new BeeArgumentError(`options.limit has to be at most ${TAGS_LIMIT_MAX}`, options.limit)
267
- }
268
- }
269
-
270
- if (options?.offset !== undefined) {
271
- assertNonNegativeInteger(options.offset, 'options.offset')
272
- }
273
- }
274
-
275
- /**
276
- * Utility functions that return Tag UID
277
- * @param tagUid
278
- */
279
- export function makeTagUid(tagUid: number | Tag): number {
280
- if (isTag(tagUid)) {
281
- return tagUid.uid
282
- } else if (typeof tagUid === 'number') {
283
- assertNonNegativeInteger(tagUid, 'UID')
284
-
285
- return tagUid
286
- }
287
-
288
- throw new TypeError('tagUid has to be either Tag or a number (UID)!')
289
- }
290
-
291
- export function assertTransactionHash(transactionHash: unknown): asserts transactionHash is TransactionHash {
292
- if (typeof transactionHash !== 'string') {
293
- throw new TypeError('TransactionHash has to be a string!')
294
- }
295
-
296
- assertPrefixedHexString(transactionHash, 'TransactionHash')
297
-
298
- // Hash is 64 long + '0x' prefix = 66
299
- if (transactionHash.length !== 66) {
300
- throw new TypeError('TransactionHash has to be prefixed hex string with total length 66 (prefix including)')
301
- }
302
- }