@ethersphere/bee-js 5.0.0 → 6.0.0-pre.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 (192) hide show
  1. package/README.md +9 -1
  2. package/dist/index.browser.js +33 -0
  3. package/dist/index.browser.js.map +7 -0
  4. package/dist/src/bee-debug.js +624 -0
  5. package/dist/{mjs → src}/bee.js +243 -340
  6. package/dist/{mjs → src}/chunk/bmt.js +0 -0
  7. package/dist/{mjs → src}/chunk/cac.js +0 -0
  8. package/dist/{mjs → src}/chunk/serialize.js +0 -0
  9. package/dist/{mjs → src}/chunk/signer.js +13 -47
  10. package/dist/{mjs → src}/chunk/soc.js +31 -71
  11. package/dist/{mjs → src}/chunk/span.js +1 -1
  12. package/dist/{mjs → src}/feed/identifier.js +0 -0
  13. package/dist/src/feed/index.js +92 -0
  14. package/dist/src/feed/json.js +27 -0
  15. package/dist/src/feed/retrievable.js +67 -0
  16. package/dist/{mjs → src}/feed/topic.js +0 -0
  17. package/dist/{mjs → src}/feed/type.js +0 -0
  18. package/dist/{mjs → src}/index.js +1 -2
  19. package/dist/src/modules/bytes.js +59 -0
  20. package/dist/src/modules/bzz.js +122 -0
  21. package/dist/src/modules/chunk.js +45 -0
  22. package/dist/src/modules/debug/balance.js +57 -0
  23. package/dist/src/modules/debug/chequebook.js +150 -0
  24. package/dist/src/modules/debug/chunk.js +35 -0
  25. package/dist/src/modules/debug/connectivity.js +45 -0
  26. package/dist/src/modules/debug/settlements.js +29 -0
  27. package/dist/src/modules/debug/stamps.js +64 -0
  28. package/dist/src/modules/debug/states.js +46 -0
  29. package/dist/src/modules/debug/status.js +134 -0
  30. package/dist/src/modules/debug/tag.js +16 -0
  31. package/dist/src/modules/debug/transactions.js +69 -0
  32. package/dist/src/modules/feed.js +67 -0
  33. package/dist/src/modules/pinning.js +60 -0
  34. package/dist/src/modules/pss.js +40 -0
  35. package/dist/src/modules/soc.js +31 -0
  36. package/dist/src/modules/status.js +12 -0
  37. package/dist/src/modules/stewardship.js +24 -0
  38. package/dist/src/modules/tag.js +77 -0
  39. package/dist/{mjs → src}/types/debug.js +0 -0
  40. package/dist/{mjs → src}/types/index.js +0 -0
  41. package/dist/{mjs → src}/utils/bytes.js +3 -2
  42. package/dist/src/utils/collection-browser.js +19 -0
  43. package/dist/src/utils/collection-node.js +74 -0
  44. package/dist/src/utils/collection.js +64 -0
  45. package/dist/src/utils/data.browser.js +72 -0
  46. package/dist/src/utils/data.js +42 -0
  47. package/dist/{mjs → src}/utils/error.js +0 -0
  48. package/dist/{mjs → src}/utils/eth.js +32 -68
  49. package/dist/{mjs → src}/utils/expose.js +1 -1
  50. package/dist/src/utils/file.js +36 -0
  51. package/dist/{mjs → src}/utils/hash.js +0 -0
  52. package/dist/{mjs → src}/utils/headers.js +4 -4
  53. package/dist/{mjs → src}/utils/hex.js +0 -0
  54. package/dist/src/utils/http.js +162 -0
  55. package/dist/{mjs → src}/utils/merge.js +2 -2
  56. package/dist/{mjs → src}/utils/pss.js +0 -0
  57. package/dist/{mjs → src}/utils/reference.js +0 -0
  58. package/dist/src/utils/sleep.js +8 -0
  59. package/dist/{mjs → src}/utils/stamps.js +0 -0
  60. package/dist/{mjs → src}/utils/stream.js +3 -3
  61. package/dist/{mjs → src}/utils/tar.js +0 -0
  62. package/dist/{mjs → src}/utils/type.js +12 -13
  63. package/dist/{mjs → src}/utils/uint64.js +0 -0
  64. package/dist/{mjs → src}/utils/url.js +0 -0
  65. package/dist/types/chunk/bmt.d.ts +1 -1
  66. package/dist/types/chunk/soc.d.ts +1 -1
  67. package/dist/types/chunk/span.d.ts +1 -1
  68. package/dist/types/feed/index.d.ts +1 -1
  69. package/dist/types/index.d.ts +0 -13
  70. package/dist/types/modules/bzz.d.ts +1 -1
  71. package/dist/types/types/index.d.ts +8 -8
  72. package/dist/types/utils/{collection.browser.d.ts → collection-browser.d.ts} +0 -0
  73. package/dist/types/utils/{collection.node.d.ts → collection-node.d.ts} +0 -0
  74. package/dist/types/utils/error.d.ts +1 -1
  75. package/dist/types/utils/eth.d.ts +3 -3
  76. package/dist/types/utils/expose.d.ts +7 -4
  77. package/dist/types/utils/hash.d.ts +1 -1
  78. package/dist/types/utils/hex.d.ts +2 -2
  79. package/dist/types/utils/http.d.ts +3 -3
  80. package/dist/types/utils/stream.d.ts +1 -1
  81. package/dist/types/utils/uint64.d.ts +1 -1
  82. package/package.json +44 -59
  83. package/dist/cjs/bee-debug.js +0 -642
  84. package/dist/cjs/bee.js +0 -983
  85. package/dist/cjs/chunk/bmt.js +0 -55
  86. package/dist/cjs/chunk/cac.js +0 -56
  87. package/dist/cjs/chunk/serialize.js +0 -19
  88. package/dist/cjs/chunk/signer.js +0 -137
  89. package/dist/cjs/chunk/soc.js +0 -176
  90. package/dist/cjs/chunk/span.js +0 -29
  91. package/dist/cjs/feed/identifier.js +0 -35
  92. package/dist/cjs/feed/index.js +0 -132
  93. package/dist/cjs/feed/json.js +0 -41
  94. package/dist/cjs/feed/retrievable.js +0 -72
  95. package/dist/cjs/feed/topic.js +0 -25
  96. package/dist/cjs/feed/type.js +0 -15
  97. package/dist/cjs/index.js +0 -39
  98. package/dist/cjs/modules/bytes.js +0 -74
  99. package/dist/cjs/modules/bzz.js +0 -131
  100. package/dist/cjs/modules/chunk.js +0 -58
  101. package/dist/cjs/modules/debug/balance.js +0 -77
  102. package/dist/cjs/modules/debug/chequebook.js +0 -167
  103. package/dist/cjs/modules/debug/chunk.js +0 -51
  104. package/dist/cjs/modules/debug/connectivity.js +0 -75
  105. package/dist/cjs/modules/debug/settlements.js +0 -45
  106. package/dist/cjs/modules/debug/stamps.js +0 -89
  107. package/dist/cjs/modules/debug/states.js +0 -64
  108. package/dist/cjs/modules/debug/status.js +0 -153
  109. package/dist/cjs/modules/debug/tag.js +0 -30
  110. package/dist/cjs/modules/debug/transactions.js +0 -81
  111. package/dist/cjs/modules/feed.js +0 -76
  112. package/dist/cjs/modules/pinning.js +0 -80
  113. package/dist/cjs/modules/pss.js +0 -55
  114. package/dist/cjs/modules/soc.js +0 -40
  115. package/dist/cjs/modules/status.js +0 -26
  116. package/dist/cjs/modules/stewardship.js +0 -41
  117. package/dist/cjs/modules/tag.js +0 -96
  118. package/dist/cjs/package.json +0 -8
  119. package/dist/cjs/types/debug.js +0 -10
  120. package/dist/cjs/types/index.js +0 -51
  121. package/dist/cjs/types/ky-options.js +0 -8
  122. package/dist/cjs/types/ky-universal/common.js +0 -8
  123. package/dist/cjs/types/ky-universal/hooks.js +0 -8
  124. package/dist/cjs/types/ky-universal/retry.js +0 -8
  125. package/dist/cjs/utils/bytes.js +0 -121
  126. package/dist/cjs/utils/collection.browser.js +0 -36
  127. package/dist/cjs/utils/collection.js +0 -70
  128. package/dist/cjs/utils/collection.node.js +0 -115
  129. package/dist/cjs/utils/data.browser.js +0 -74
  130. package/dist/cjs/utils/data.js +0 -58
  131. package/dist/cjs/utils/error.js +0 -50
  132. package/dist/cjs/utils/eth.js +0 -211
  133. package/dist/cjs/utils/expose.js +0 -44
  134. package/dist/cjs/utils/file.js +0 -49
  135. package/dist/cjs/utils/hash.js +0 -21
  136. package/dist/cjs/utils/headers.js +0 -59
  137. package/dist/cjs/utils/hex.js +0 -150
  138. package/dist/cjs/utils/http.js +0 -172
  139. package/dist/cjs/utils/merge.js +0 -34
  140. package/dist/cjs/utils/pss.js +0 -18
  141. package/dist/cjs/utils/reference.js +0 -36
  142. package/dist/cjs/utils/sleep.js +0 -23
  143. package/dist/cjs/utils/stamps.js +0 -17
  144. package/dist/cjs/utils/stream.js +0 -146
  145. package/dist/cjs/utils/tar.js +0 -25
  146. package/dist/cjs/utils/type.js +0 -426
  147. package/dist/cjs/utils/uint64.js +0 -29
  148. package/dist/cjs/utils/url.js +0 -56
  149. package/dist/index.browser.min.js +0 -3
  150. package/dist/index.browser.min.js.LICENSE.txt +0 -60
  151. package/dist/index.browser.min.js.map +0 -1
  152. package/dist/mjs/bee-debug.js +0 -744
  153. package/dist/mjs/feed/index.js +0 -134
  154. package/dist/mjs/feed/json.js +0 -63
  155. package/dist/mjs/feed/retrievable.js +0 -105
  156. package/dist/mjs/modules/bytes.js +0 -96
  157. package/dist/mjs/modules/bzz.js +0 -160
  158. package/dist/mjs/modules/chunk.js +0 -80
  159. package/dist/mjs/modules/debug/balance.js +0 -97
  160. package/dist/mjs/modules/debug/chequebook.js +0 -198
  161. package/dist/mjs/modules/debug/chunk.js +0 -71
  162. package/dist/mjs/modules/debug/connectivity.js +0 -89
  163. package/dist/mjs/modules/debug/settlements.js +0 -65
  164. package/dist/mjs/modules/debug/stamps.js +0 -108
  165. package/dist/mjs/modules/debug/states.js +0 -84
  166. package/dist/mjs/modules/debug/status.js +0 -182
  167. package/dist/mjs/modules/debug/tag.js +0 -50
  168. package/dist/mjs/modules/debug/transactions.js +0 -103
  169. package/dist/mjs/modules/feed.js +0 -101
  170. package/dist/mjs/modules/pinning.js +0 -100
  171. package/dist/mjs/modules/pss.js +0 -74
  172. package/dist/mjs/modules/soc.js +0 -64
  173. package/dist/mjs/modules/status.js +0 -46
  174. package/dist/mjs/modules/stewardship.js +0 -60
  175. package/dist/mjs/modules/tag.js +0 -119
  176. package/dist/mjs/package.json +0 -8
  177. package/dist/mjs/types/ky-options.js +0 -7
  178. package/dist/mjs/types/ky-universal/common.js +0 -7
  179. package/dist/mjs/types/ky-universal/hooks.js +0 -7
  180. package/dist/mjs/types/ky-universal/retry.js +0 -7
  181. package/dist/mjs/utils/collection.browser.js +0 -56
  182. package/dist/mjs/utils/collection.js +0 -98
  183. package/dist/mjs/utils/collection.node.js +0 -169
  184. package/dist/mjs/utils/data.browser.js +0 -108
  185. package/dist/mjs/utils/data.js +0 -78
  186. package/dist/mjs/utils/file.js +0 -70
  187. package/dist/mjs/utils/http.js +0 -208
  188. package/dist/mjs/utils/sleep.js +0 -43
  189. package/dist/types/types/ky-options.d.ts +0 -221
  190. package/dist/types/types/ky-universal/common.d.ts +0 -13
  191. package/dist/types/types/ky-universal/hooks.d.ts +0 -92
  192. package/dist/types/types/ky-universal/retry.d.ts +0 -38
@@ -0,0 +1,67 @@
1
+ import { filterHeaders, http } from "../utils/http.js";
2
+ import { extractUploadHeaders } from "../utils/headers.js";
3
+ import { BeeError } from "../utils/error.js";
4
+ const feedEndpoint = 'feeds';
5
+ /**
6
+ * Create an initial feed root manifest
7
+ *
8
+ * @param ky Ky instance
9
+ * @param owner Owner's ethereum address in hex
10
+ * @param topic Topic in hex
11
+ * @param postageBatchId Postage BatchId to be used to create the Feed Manifest
12
+ * @param options Additional options, like type (default: 'sequence')
13
+ */
14
+
15
+ export async function createFeedManifest(ky, owner, topic, postageBatchId, options) {
16
+ const response = await http(ky, {
17
+ method: 'post',
18
+ responseType: 'json',
19
+ path: `${feedEndpoint}/${owner}/${topic}`,
20
+ searchParams: filterHeaders(options),
21
+ headers: extractUploadHeaders(postageBatchId)
22
+ });
23
+ return response.parsedData.reference;
24
+ }
25
+
26
+ function readFeedUpdateHeaders(headers) {
27
+ const feedIndex = headers.get('swarm-feed-index');
28
+ const feedIndexNext = headers.get('swarm-feed-index-next');
29
+
30
+ if (!feedIndex) {
31
+ throw new BeeError('Response did not contain expected swarm-feed-index!');
32
+ }
33
+
34
+ if (!feedIndexNext) {
35
+ throw new BeeError('Response did not contain expected swarm-feed-index-next!');
36
+ }
37
+
38
+ return {
39
+ feedIndex,
40
+ feedIndexNext
41
+ };
42
+ }
43
+ /**
44
+ * Find and retrieve feed update
45
+ *
46
+ * The feed consists of updates. This endpoint looks up an
47
+ * update that matches the provided parameters and returns
48
+ * the reference it contains along with its index and the
49
+ * index of the subsequent update.
50
+ *
51
+ * @param ky Ky instance
52
+ * @param owner Owner's ethereum address in hex
53
+ * @param topic Topic in hex
54
+ * @param options Additional options, like index, at, type
55
+ */
56
+
57
+
58
+ export async function fetchLatestFeedUpdate(ky, owner, topic, options) {
59
+ const response = await http(ky, {
60
+ responseType: 'json',
61
+ path: `${feedEndpoint}/${owner}/${topic}`,
62
+ searchParams: filterHeaders(options)
63
+ });
64
+ return { ...response.parsedData,
65
+ ...readFeedUpdateHeaders(response.headers)
66
+ };
67
+ }
@@ -0,0 +1,60 @@
1
+ import { http } from "../utils/http.js";
2
+ const PINNING_ENDPOINT = 'pins';
3
+ /**
4
+ * Pin data with given reference
5
+ *
6
+ * @param ky Ky instance for given Bee class instance
7
+ * @param reference Bee data reference
8
+ */
9
+
10
+ export async function pin(ky, reference) {
11
+ await http(ky, {
12
+ method: 'post',
13
+ responseType: 'json',
14
+ path: `${PINNING_ENDPOINT}/${reference}`
15
+ });
16
+ }
17
+ /**
18
+ * Unpin data with given reference
19
+ *
20
+ * @param ky Ky instance for given Bee class instance
21
+ * @param reference Bee data reference
22
+ */
23
+
24
+ export async function unpin(ky, reference) {
25
+ await http(ky, {
26
+ method: 'delete',
27
+ responseType: 'json',
28
+ path: `${PINNING_ENDPOINT}/${reference}`
29
+ });
30
+ }
31
+ /**
32
+ * Get pin status for specific address.
33
+ *
34
+ * @param ky Ky instance
35
+ * @param reference
36
+ * @throws Error if given address is not pinned
37
+ */
38
+
39
+ export async function getPin(ky, reference) {
40
+ const response = await http(ky, {
41
+ method: 'get',
42
+ responseType: 'json',
43
+ path: `${PINNING_ENDPOINT}/${reference}`
44
+ });
45
+ return response.parsedData;
46
+ }
47
+ /**
48
+ * Get list of all pins
49
+ *
50
+ * @param ky Ky instance
51
+ */
52
+
53
+ export async function getAllPins(ky) {
54
+ const response = await http(ky, {
55
+ method: 'get',
56
+ responseType: 'json',
57
+ path: `${PINNING_ENDPOINT}`
58
+ });
59
+ return response.parsedData.references;
60
+ }
@@ -0,0 +1,40 @@
1
+ import WebSocket from 'isomorphic-ws';
2
+ import { prepareData } from "../utils/data.js";
3
+ import { http } from "../utils/http.js";
4
+ import { extractUploadHeaders } from "../utils/headers.js";
5
+ const endpoint = 'pss';
6
+ /**
7
+ * Send to recipient or target with Postal Service for Swarm
8
+ *
9
+ * @param ky Ky instance for given Bee class instance
10
+ * @param topic Topic name
11
+ * @param target Target message address prefix
12
+ * @param data
13
+ * @param postageBatchId Postage BatchId that will be assigned to sent message
14
+ * @param recipient Recipient public key
15
+ *
16
+ */
17
+
18
+ export async function send(ky, topic, target, data, postageBatchId, recipient) {
19
+ await http(ky, {
20
+ method: 'post',
21
+ path: `${endpoint}/send/${topic}/${target}`,
22
+ body: await prepareData(data),
23
+ responseType: 'json',
24
+ searchParams: {
25
+ recipient
26
+ },
27
+ headers: extractUploadHeaders(postageBatchId)
28
+ });
29
+ }
30
+ /**
31
+ * Subscribe for messages on the given topic
32
+ *
33
+ * @param url Bee node URL
34
+ * @param topic Topic name
35
+ */
36
+
37
+ export function subscribe(url, topic) {
38
+ const wsUrl = url.replace(/^http/i, 'ws');
39
+ return new WebSocket(`${wsUrl}/${endpoint}/subscribe/${topic}`);
40
+ }
@@ -0,0 +1,31 @@
1
+ import { extractUploadHeaders } from "../utils/headers.js";
2
+ import { http } from "../utils/http.js";
3
+ const socEndpoint = 'soc';
4
+ /**
5
+ * Upload single owner chunk (SOC) to a Bee node
6
+ *
7
+ * @param ky Ky instance
8
+ * @param owner Owner's ethereum address in hex
9
+ * @param identifier Arbitrary identifier in hex
10
+ * @param signature Signature in hex
11
+ * @param data Content addressed chunk data to be uploaded
12
+ * @param postageBatchId Postage BatchId that will be assigned to uploaded data
13
+ * @param options Additional options like tag, encryption, pinning
14
+ */
15
+
16
+ export async function upload(ky, owner, identifier, signature, data, postageBatchId, options) {
17
+ const response = await http(ky, {
18
+ method: 'post',
19
+ path: `${socEndpoint}/${owner}/${identifier}`,
20
+ body: data,
21
+ headers: {
22
+ 'content-type': 'application/octet-stream',
23
+ ...extractUploadHeaders(postageBatchId, options)
24
+ },
25
+ responseType: 'json',
26
+ searchParams: {
27
+ sig: signature
28
+ }
29
+ });
30
+ return response.parsedData.reference;
31
+ }
@@ -0,0 +1,12 @@
1
+ import { http } from "../utils/http.js";
2
+ /**
3
+ * Ping the base bee URL. If connection was not successful throw error
4
+ *
5
+ * @param ky Ky instance for given Bee class instance
6
+ */
7
+
8
+ export async function checkConnection(ky) {
9
+ await http(ky, {
10
+ path: ''
11
+ });
12
+ }
@@ -0,0 +1,24 @@
1
+ import { http } from "../utils/http.js";
2
+ const stewardshipEndpoint = 'stewardship';
3
+ /**
4
+ * Reupload locally pinned data
5
+ * @param ky Ky instance
6
+ * @param reference
7
+ * @param options
8
+ * @throws BeeResponseError if not locally pinned or invalid data
9
+ */
10
+
11
+ export async function reupload(ky, reference) {
12
+ await http(ky, {
13
+ method: 'put',
14
+ path: `${stewardshipEndpoint}/${reference}`
15
+ });
16
+ }
17
+ export async function isRetrievable(ky, reference) {
18
+ const response = await http(ky, {
19
+ method: 'get',
20
+ responseType: 'json',
21
+ path: `${stewardshipEndpoint}/${reference}`
22
+ });
23
+ return response.parsedData.isRetrievable;
24
+ }
@@ -0,0 +1,77 @@
1
+ import { http } from "../utils/http.js";
2
+ const endpoint = 'tags';
3
+ /**
4
+ * Create new tag on the Bee node
5
+ *
6
+ * @param url Bee tag URL
7
+ */
8
+
9
+ export async function createTag(ky) {
10
+ const response = await http(ky, {
11
+ method: 'post',
12
+ path: endpoint,
13
+ responseType: 'json'
14
+ });
15
+ return response.parsedData;
16
+ }
17
+ /**
18
+ * Retrieve tag information from Bee node
19
+ *
20
+ * @param url Bee tag URL
21
+ * @param uid UID of tag to be retrieved
22
+ */
23
+
24
+ export async function retrieveTag(ky, uid) {
25
+ const response = await http(ky, {
26
+ path: `${endpoint}/${uid}`,
27
+ responseType: 'json'
28
+ });
29
+ return response.parsedData;
30
+ }
31
+ /**
32
+ * Get limited listing of all tags.
33
+ *
34
+ * @param url
35
+ * @param offset
36
+ * @param limit
37
+ */
38
+
39
+ export async function getAllTags(ky, offset, limit) {
40
+ const response = await http(ky, {
41
+ path: `${endpoint}`,
42
+ searchParams: {
43
+ offset,
44
+ limit
45
+ },
46
+ responseType: 'json'
47
+ });
48
+ return response.parsedData.tags;
49
+ }
50
+ /**
51
+ * Removes tag from the Bee node.
52
+ * @param url
53
+ * @param uid
54
+ */
55
+
56
+ export async function deleteTag(ky, uid) {
57
+ await http(ky, {
58
+ method: 'delete',
59
+ path: `${endpoint}/${uid}`
60
+ });
61
+ }
62
+ /**
63
+ * Updates tag
64
+ * @param url
65
+ * @param uid
66
+ * @param reference
67
+ */
68
+
69
+ export async function updateTag(ky, uid, reference) {
70
+ await http(ky, {
71
+ method: 'patch',
72
+ path: `${endpoint}/${uid}`,
73
+ json: {
74
+ reference
75
+ }
76
+ });
77
+ }
File without changes
File without changes
@@ -108,9 +108,10 @@ export function makeBytes(length) {
108
108
  return new Uint8Array(length);
109
109
  }
110
110
  export function wrapBytesWithHelpers(data) {
111
- return Object.assign(data, {
111
+ return {
112
+ bytes: () => data,
112
113
  text: () => new TextDecoder('utf-8').decode(data),
113
114
  json: () => JSON.parse(new TextDecoder('utf-8').decode(data)),
114
115
  hex: () => bytesToHex(data)
115
- });
116
+ };
116
117
  }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Creates array in the format of Collection with data loaded from directory on filesystem.
3
+ * The function loads all the data into memory!
4
+ *
5
+ * @param dir path to the directory
6
+ */
7
+ export async function makeCollectionFromFS(dir) {
8
+ throw new Error('Creating Collection from File System is not supported in browsers!');
9
+ }
10
+ /**
11
+ * Calculate folder size recursively
12
+ *
13
+ * @param dir the path to the folder to check
14
+ * @returns size in bytes
15
+ */
16
+
17
+ export async function getFolderSize(dir) {
18
+ throw new Error('Creating Collection from File System is not supported in browsers!');
19
+ }
@@ -0,0 +1,74 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ /**
4
+ * Creates array in the format of Collection with data loaded from directory on filesystem.
5
+ * The function loads all the data into memory!
6
+ *
7
+ * @param dir path to the directory
8
+ */
9
+
10
+ export async function makeCollectionFromFS(dir) {
11
+ if (typeof dir !== 'string') {
12
+ throw new TypeError('dir has to be string!');
13
+ }
14
+
15
+ if (dir === '') {
16
+ throw new TypeError('dir must not be empty string!');
17
+ }
18
+
19
+ return buildCollectionRelative(dir, '');
20
+ }
21
+
22
+ async function buildCollectionRelative(dir, relativePath) {
23
+ // Handles case when the dir is not existing or it is a file ==> throws an error
24
+ const dirname = path.join(dir, relativePath);
25
+ const entries = await fs.promises.opendir(dirname);
26
+ let collection = [];
27
+
28
+ for await (const entry of entries) {
29
+ const fullPath = path.join(dir, relativePath, entry.name);
30
+ const entryPath = path.join(relativePath, entry.name);
31
+
32
+ if (entry.isFile()) {
33
+ collection.push({
34
+ path: entryPath,
35
+ data: new Uint8Array(await fs.promises.readFile(fullPath))
36
+ });
37
+ } else if (entry.isDirectory()) {
38
+ collection = [...(await buildCollectionRelative(dir, entryPath)), ...collection];
39
+ }
40
+ }
41
+
42
+ return collection;
43
+ }
44
+ /**
45
+ * Calculate folder size recursively
46
+ *
47
+ * @param dir the path to the folder to check
48
+ * @returns size in bytes
49
+ */
50
+
51
+
52
+ export async function getFolderSize(dir) {
53
+ if (typeof dir !== 'string') {
54
+ throw new TypeError('dir has to be string!');
55
+ }
56
+
57
+ if (dir === '') {
58
+ throw new TypeError('dir must not be empty string!');
59
+ }
60
+
61
+ const entries = await fs.promises.opendir(dir);
62
+ let size = 0;
63
+
64
+ for await (const entry of entries) {
65
+ if (entry.isFile()) {
66
+ const stats = await fs.promises.stat(path.join(dir, entry.name));
67
+ size += stats.size;
68
+ } else if (entry.isDirectory()) {
69
+ size += await getFolderSize(path.join(dir, entry.name));
70
+ }
71
+ }
72
+
73
+ return size;
74
+ }
@@ -0,0 +1,64 @@
1
+ import { BeeArgumentError } from "./error.js";
2
+ import { fileArrayBuffer } from "./file.js";
3
+ import { isUint8Array } from "./type.js";
4
+ export function isCollection(data) {
5
+ if (!Array.isArray(data)) {
6
+ return false;
7
+ }
8
+
9
+ return data.every(entry => typeof entry === 'object' && entry.data && entry.path && isUint8Array(entry.data));
10
+ }
11
+ export function assertCollection(data) {
12
+ if (!isCollection(data)) {
13
+ throw new BeeArgumentError('invalid collection', data);
14
+ }
15
+ }
16
+
17
+ function makeFilePath(file) {
18
+ if (file.webkitRelativePath && file.webkitRelativePath !== '') {
19
+ return file.webkitRelativePath.replace(/.*?\//i, '');
20
+ }
21
+
22
+ if (file.name) {
23
+ return file.name;
24
+ }
25
+
26
+ throw new TypeError('file is not valid File object');
27
+ }
28
+
29
+ export async function makeCollectionFromFileList(fileList) {
30
+ const collection = [];
31
+
32
+ for (let i = 0; i < fileList.length; i++) {
33
+ const file = fileList[i];
34
+
35
+ if (file) {
36
+ collection.push({
37
+ path: makeFilePath(file),
38
+ data: new Uint8Array(await fileArrayBuffer(file))
39
+ });
40
+ }
41
+ }
42
+
43
+ return collection;
44
+ }
45
+ /**
46
+ * Calculate cumulative size of files
47
+ *
48
+ * @param fileList list of files to check
49
+ * @returns size in bytes
50
+ */
51
+
52
+ export function getCollectionSize(fileList) {
53
+ let sum = 0;
54
+
55
+ for (let i = 0; i < fileList.length; i++) {
56
+ const file = fileList[i];
57
+
58
+ if (file) {
59
+ sum += file.size;
60
+ }
61
+ }
62
+
63
+ return sum;
64
+ }
@@ -0,0 +1,72 @@
1
+ import { isNodeReadable, isReadableStream } from "./stream.js";
2
+ /**
3
+ * Validates input and converts to Uint8Array
4
+ *
5
+ * @param data any string, ArrayBuffer or Uint8Array
6
+ */
7
+
8
+ export async function prepareData(data) {
9
+ if (typeof data === 'string') return new Blob([data], {
10
+ type: 'text/plain'
11
+ });
12
+
13
+ if (data instanceof Uint8Array || data instanceof ArrayBuffer) {
14
+ return new Blob([data], {
15
+ type: 'application/octet-stream'
16
+ });
17
+ }
18
+
19
+ if (data instanceof Blob) {
20
+ return data;
21
+ } // Currently it is not possible to stream requests from browsers
22
+ // there are already first experiments on this field (Chromium)
23
+ // but till it is fully implemented across browsers-land we have to
24
+ // buffer the data before sending the requests.
25
+
26
+
27
+ if (isNodeReadable(data)) {
28
+ return new Promise(resolve => {
29
+ const buffers = [];
30
+ data.on('data', d => {
31
+ buffers.push(d);
32
+ });
33
+ data.on('end', () => {
34
+ resolve(new Blob(buffers, {
35
+ type: 'application/octet-stream'
36
+ }));
37
+ });
38
+ });
39
+ }
40
+
41
+ if (isReadableStream(data)) {
42
+ return new Promise(async resolve => {
43
+ const reader = data.getReader();
44
+ const buffers = [];
45
+ let done, value;
46
+
47
+ do {
48
+ ;
49
+ ({
50
+ done,
51
+ value
52
+ } = await reader.read());
53
+
54
+ if (!done) {
55
+ buffers.push(value);
56
+ }
57
+ } while (!done);
58
+
59
+ resolve(new Blob(buffers, {
60
+ type: 'application/octet-stream'
61
+ }));
62
+ });
63
+ }
64
+
65
+ throw new TypeError('unknown data type');
66
+ }
67
+ export async function prepareWebsocketData(data) {
68
+ if (typeof data === 'string') return new TextEncoder().encode(data);
69
+ if (data instanceof ArrayBuffer) return new Uint8Array(data);
70
+ if (data instanceof Blob) return new Uint8Array(await new Response(data).arrayBuffer());
71
+ throw new TypeError('unknown websocket data type');
72
+ }
@@ -0,0 +1,42 @@
1
+ import BlobPolyfill from 'fetch-blob';
2
+ import { isNodeReadable, isReadableStream, readableWebToNode } from "./stream.js";
3
+ /**
4
+ * Prepare data for valid input for node-fetch.
5
+ *
6
+ * node-fetch is not using WHATWG ReadableStream but NodeJS Readable so we need to convert in case of ReadableStream,
7
+ * but the typings are set to use ReadableStream so hence why type conversion here.
8
+ *
9
+ * @param data any string, ArrayBuffer, Uint8Array or Readable
10
+ */
11
+
12
+ export async function prepareData(data) {
13
+ if (typeof data === 'string') return new BlobPolyfill([data], {
14
+ type: 'text/plain'
15
+ });
16
+
17
+ if (data instanceof Uint8Array || data instanceof ArrayBuffer) {
18
+ return new BlobPolyfill([data], {
19
+ type: 'application/octet-stream'
20
+ });
21
+ }
22
+
23
+ if (data instanceof BlobPolyfill || isNodeReadable(data)) return data;
24
+
25
+ if (isReadableStream(data)) {
26
+ return readableWebToNode(data);
27
+ }
28
+
29
+ throw new TypeError('unknown data type');
30
+ }
31
+
32
+ function isBufferArray(buffer) {
33
+ return Array.isArray(buffer) && buffer.length > 0 && buffer.every(data => data instanceof Buffer);
34
+ }
35
+
36
+ export async function prepareWebsocketData(data) {
37
+ if (typeof data === 'string') return new TextEncoder().encode(data);
38
+ if (data instanceof Buffer) return new Uint8Array(data);
39
+ if (data instanceof ArrayBuffer) return new Uint8Array(data);
40
+ if (isBufferArray(data)) return new Uint8Array(Buffer.concat(data));
41
+ throw new TypeError('unknown websocket data type');
42
+ }
File without changes