@ethersphere/bee-js 3.2.0 → 3.3.2-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 (193) hide show
  1. package/dist/cjs/bee-debug.js +615 -0
  2. package/dist/cjs/bee.js +922 -0
  3. package/dist/cjs/chunk/bmt.js +55 -0
  4. package/dist/cjs/chunk/cac.js +56 -0
  5. package/dist/cjs/chunk/serialize.js +19 -0
  6. package/dist/cjs/chunk/signer.js +137 -0
  7. package/dist/cjs/chunk/soc.js +172 -0
  8. package/dist/cjs/chunk/span.js +29 -0
  9. package/dist/cjs/feed/index.js +184 -0
  10. package/dist/cjs/feed/json.js +41 -0
  11. package/dist/cjs/feed/topic.js +25 -0
  12. package/dist/cjs/feed/type.js +15 -0
  13. package/dist/cjs/index.js +35 -0
  14. package/dist/cjs/modules/bytes.js +74 -0
  15. package/dist/cjs/modules/bzz.js +131 -0
  16. package/dist/cjs/modules/chunk.js +58 -0
  17. package/dist/cjs/modules/debug/balance.js +77 -0
  18. package/dist/cjs/modules/debug/chequebook.js +167 -0
  19. package/dist/cjs/modules/debug/chunk.js +51 -0
  20. package/dist/cjs/modules/debug/connectivity.js +75 -0
  21. package/dist/cjs/modules/debug/settlements.js +45 -0
  22. package/dist/cjs/modules/debug/stamps.js +89 -0
  23. package/dist/cjs/modules/debug/states.js +47 -0
  24. package/dist/cjs/modules/debug/status.js +153 -0
  25. package/dist/cjs/modules/debug/tag.js +30 -0
  26. package/dist/cjs/modules/debug/transactions.js +81 -0
  27. package/dist/cjs/modules/feed.js +76 -0
  28. package/dist/cjs/modules/pinning.js +84 -0
  29. package/dist/cjs/modules/pss.js +55 -0
  30. package/dist/cjs/modules/soc.js +40 -0
  31. package/dist/cjs/modules/status.js +26 -0
  32. package/dist/cjs/modules/stewardship.js +41 -0
  33. package/dist/cjs/modules/tag.js +96 -0
  34. package/dist/cjs/package.json +8 -0
  35. package/dist/cjs/types/debug.js +9 -0
  36. package/dist/cjs/types/index.js +46 -0
  37. package/dist/cjs/types/ky-options.js +8 -0
  38. package/dist/cjs/types/ky-universal/common.js +8 -0
  39. package/dist/cjs/types/ky-universal/hooks.js +8 -0
  40. package/dist/cjs/types/ky-universal/retry.js +8 -0
  41. package/dist/cjs/utils/bytes.js +107 -0
  42. package/dist/cjs/utils/collection.browser.js +36 -0
  43. package/dist/cjs/utils/collection.js +70 -0
  44. package/dist/cjs/utils/collection.node.js +115 -0
  45. package/dist/cjs/utils/data.browser.js +78 -0
  46. package/dist/cjs/utils/data.js +60 -0
  47. package/dist/cjs/utils/error.js +50 -0
  48. package/dist/cjs/utils/eth.js +211 -0
  49. package/dist/cjs/utils/expose.js +44 -0
  50. package/dist/cjs/utils/file.js +49 -0
  51. package/dist/cjs/utils/hash.js +21 -0
  52. package/dist/cjs/utils/headers.js +59 -0
  53. package/dist/cjs/utils/hex.js +150 -0
  54. package/dist/cjs/utils/http.js +166 -0
  55. package/dist/cjs/utils/merge.js +34 -0
  56. package/dist/cjs/utils/pss.js +18 -0
  57. package/dist/cjs/utils/stamps.js +17 -0
  58. package/dist/cjs/utils/stream.js +146 -0
  59. package/dist/cjs/utils/tar.js +25 -0
  60. package/dist/cjs/utils/type.js +327 -0
  61. package/dist/cjs/utils/uint64.js +29 -0
  62. package/dist/cjs/utils/url.js +56 -0
  63. package/dist/index.browser.min.js +1 -1
  64. package/dist/index.browser.min.js.LICENSE.txt +12 -11
  65. package/dist/index.browser.min.js.map +1 -1
  66. package/dist/mjs/bee-debug.js +609 -0
  67. package/dist/mjs/bee.js +944 -0
  68. package/dist/mjs/chunk/bmt.js +56 -0
  69. package/dist/mjs/chunk/cac.js +52 -0
  70. package/dist/mjs/chunk/serialize.js +15 -0
  71. package/dist/mjs/chunk/signer.js +131 -0
  72. package/dist/mjs/chunk/soc.js +139 -0
  73. package/dist/mjs/chunk/span.js +28 -0
  74. package/dist/mjs/feed/index.js +145 -0
  75. package/dist/mjs/feed/json.js +27 -0
  76. package/dist/mjs/feed/topic.js +21 -0
  77. package/dist/mjs/feed/type.js +10 -0
  78. package/dist/mjs/index.js +7 -0
  79. package/dist/mjs/modules/bytes.js +59 -0
  80. package/dist/mjs/modules/bzz.js +122 -0
  81. package/dist/mjs/modules/chunk.js +45 -0
  82. package/dist/mjs/modules/debug/balance.js +57 -0
  83. package/dist/mjs/modules/debug/chequebook.js +150 -0
  84. package/dist/mjs/modules/debug/chunk.js +35 -0
  85. package/dist/mjs/modules/debug/connectivity.js +45 -0
  86. package/dist/mjs/modules/debug/settlements.js +29 -0
  87. package/dist/mjs/modules/debug/stamps.js +64 -0
  88. package/dist/mjs/modules/debug/states.js +31 -0
  89. package/dist/mjs/modules/debug/status.js +134 -0
  90. package/dist/mjs/modules/debug/tag.js +16 -0
  91. package/dist/mjs/modules/debug/transactions.js +63 -0
  92. package/dist/mjs/modules/feed.js +67 -0
  93. package/dist/mjs/modules/pinning.js +66 -0
  94. package/dist/mjs/modules/pss.js +40 -0
  95. package/dist/mjs/modules/soc.js +31 -0
  96. package/dist/mjs/modules/status.js +12 -0
  97. package/dist/mjs/modules/stewardship.js +24 -0
  98. package/dist/mjs/modules/tag.js +77 -0
  99. package/dist/mjs/package.json +8 -0
  100. package/dist/mjs/types/debug.js +7 -0
  101. package/dist/mjs/types/index.js +37 -0
  102. package/dist/mjs/types/ky-options.js +7 -0
  103. package/dist/mjs/types/ky-universal/common.js +7 -0
  104. package/dist/mjs/types/ky-universal/hooks.js +7 -0
  105. package/dist/mjs/types/ky-universal/retry.js +7 -0
  106. package/dist/mjs/utils/bytes.js +101 -0
  107. package/dist/mjs/utils/collection.browser.js +19 -0
  108. package/dist/mjs/utils/collection.js +64 -0
  109. package/dist/mjs/utils/collection.node.js +74 -0
  110. package/dist/mjs/utils/data.browser.js +73 -0
  111. package/dist/mjs/utils/data.js +43 -0
  112. package/dist/mjs/utils/error.js +56 -0
  113. package/dist/mjs/utils/eth.js +199 -0
  114. package/dist/mjs/utils/expose.js +9 -0
  115. package/dist/mjs/utils/file.js +36 -0
  116. package/dist/mjs/utils/hash.js +17 -0
  117. package/dist/mjs/utils/headers.js +58 -0
  118. package/dist/mjs/utils/hex.js +154 -0
  119. package/dist/mjs/utils/http.js +155 -0
  120. package/dist/mjs/utils/merge.js +36 -0
  121. package/dist/mjs/utils/pss.js +16 -0
  122. package/dist/mjs/utils/stamps.js +17 -0
  123. package/dist/mjs/utils/stream.js +156 -0
  124. package/dist/mjs/utils/tar.js +21 -0
  125. package/dist/mjs/utils/type.js +336 -0
  126. package/dist/mjs/utils/uint64.js +23 -0
  127. package/dist/mjs/utils/url.js +57 -0
  128. package/dist/{src → types}/bee-debug.d.ts +47 -1
  129. package/dist/{src → types}/bee.d.ts +0 -0
  130. package/dist/{src → types}/chunk/bmt.d.ts +0 -0
  131. package/dist/{src → types}/chunk/cac.d.ts +0 -0
  132. package/dist/{src → types}/chunk/serialize.d.ts +0 -0
  133. package/dist/{src → types}/chunk/signer.d.ts +0 -0
  134. package/dist/{src → types}/chunk/soc.d.ts +0 -0
  135. package/dist/{src → types}/chunk/span.d.ts +0 -0
  136. package/dist/{src → types}/feed/index.d.ts +0 -0
  137. package/dist/{src → types}/feed/json.d.ts +0 -0
  138. package/dist/{src → types}/feed/topic.d.ts +0 -0
  139. package/dist/{src → types}/feed/type.d.ts +0 -0
  140. package/dist/{src → types}/index.d.ts +0 -0
  141. package/dist/{src → types}/modules/bytes.d.ts +0 -0
  142. package/dist/{src → types}/modules/bzz.d.ts +0 -0
  143. package/dist/{src → types}/modules/chunk.d.ts +0 -0
  144. package/dist/{src → types}/modules/debug/balance.d.ts +0 -0
  145. package/dist/{src → types}/modules/debug/chequebook.d.ts +0 -0
  146. package/dist/{src → types}/modules/debug/chunk.d.ts +0 -0
  147. package/dist/{src → types}/modules/debug/connectivity.d.ts +0 -0
  148. package/dist/{src → types}/modules/debug/settlements.d.ts +0 -0
  149. package/dist/{src → types}/modules/debug/stamps.d.ts +0 -0
  150. package/dist/{src → types}/modules/debug/states.d.ts +0 -0
  151. package/dist/types/modules/debug/status.d.ts +72 -0
  152. package/dist/{src → types}/modules/debug/tag.d.ts +0 -0
  153. package/dist/{src → types}/modules/debug/transactions.d.ts +0 -0
  154. package/dist/{src → types}/modules/feed.d.ts +0 -0
  155. package/dist/{src → types}/modules/pinning.d.ts +0 -0
  156. package/dist/{src → types}/modules/pss.d.ts +0 -0
  157. package/dist/{src → types}/modules/soc.d.ts +0 -0
  158. package/dist/{src → types}/modules/status.d.ts +0 -0
  159. package/dist/{src → types}/modules/stewardship.d.ts +0 -0
  160. package/dist/{src → types}/modules/tag.d.ts +0 -0
  161. package/dist/{src → types}/types/debug.d.ts +8 -0
  162. package/dist/{src → types}/types/index.d.ts +3 -2
  163. package/dist/types/types/ky-options.d.ts +221 -0
  164. package/dist/types/types/ky-universal/common.d.ts +13 -0
  165. package/dist/types/types/ky-universal/hooks.d.ts +92 -0
  166. package/dist/types/types/ky-universal/retry.d.ts +38 -0
  167. package/dist/{src → types}/utils/bytes.d.ts +0 -0
  168. package/dist/types/utils/collection.browser.d.ts +15 -0
  169. package/dist/{src → types}/utils/collection.d.ts +0 -14
  170. package/dist/types/utils/collection.node.d.ts +15 -0
  171. package/dist/{src → types}/utils/data.browser.d.ts +0 -0
  172. package/dist/{src → types}/utils/data.d.ts +0 -0
  173. package/dist/{src → types}/utils/error.d.ts +0 -0
  174. package/dist/{src → types}/utils/eth.d.ts +0 -0
  175. package/dist/{src → types}/utils/expose.d.ts +2 -1
  176. package/dist/{src → types}/utils/file.d.ts +0 -0
  177. package/dist/{src → types}/utils/hash.d.ts +0 -0
  178. package/dist/{src → types}/utils/headers.d.ts +0 -0
  179. package/dist/{src → types}/utils/hex.d.ts +0 -0
  180. package/dist/{src → types}/utils/http.d.ts +0 -0
  181. package/dist/{src → types}/utils/merge.d.ts +0 -0
  182. package/dist/{src → types}/utils/pss.d.ts +0 -0
  183. package/dist/{src → types}/utils/stamps.d.ts +0 -0
  184. package/dist/{src → types}/utils/stream.d.ts +11 -8
  185. package/dist/{src → types}/utils/tar.d.ts +0 -0
  186. package/dist/{src → types}/utils/type.d.ts +0 -0
  187. package/dist/{src → types}/utils/uint64.d.ts +0 -0
  188. package/dist/{src → types}/utils/url.d.ts +0 -0
  189. package/package.json +55 -36
  190. package/dist/index.min.js +0 -3
  191. package/dist/index.min.js.LICENSE.txt +0 -50
  192. package/dist/index.min.js.map +0 -1
  193. package/dist/src/modules/debug/status.d.ts +0 -24
@@ -0,0 +1,101 @@
1
+ import { bytesToHex } from "./hex.js";
2
+ /**
3
+ * Type guard for `Bytes<T>` type
4
+ *
5
+ * @param b The byte array
6
+ * @param length The length of the byte array
7
+ */
8
+
9
+ export function isBytes(b, length) {
10
+ return b instanceof Uint8Array && b.length === length;
11
+ }
12
+ /**
13
+ * Verifies if a byte array has a certain length
14
+ *
15
+ * @param b The byte array
16
+ * @param length The specified length
17
+ */
18
+
19
+ export function assertBytes(b, length) {
20
+ if (!isBytes(b, length)) {
21
+ throw new TypeError(`Parameter is not valid Bytes of length: ${length} !== ${b.length}`);
22
+ }
23
+ }
24
+ /**
25
+ * Type guard for FlexBytes<Min,Max> type
26
+ *
27
+ * @param b The byte array
28
+ * @param min Minimum size of the array
29
+ * @param max Maximum size of the array
30
+ */
31
+
32
+ export function isFlexBytes(b, min, max) {
33
+ return b instanceof Uint8Array && b.length >= min && b.length <= max;
34
+ }
35
+ /**
36
+ * Verifies if a byte array has a certain length between min and max
37
+ *
38
+ * @param b The byte array
39
+ * @param min Minimum size of the array
40
+ * @param max Maximum size of the array
41
+ */
42
+
43
+ export function assertFlexBytes(b, min, max) {
44
+ if (!isFlexBytes(b, min, max)) {
45
+ throw new TypeError(`Parameter is not valid FlexBytes of min: ${min}, max: ${max}, length: ${b.length}`);
46
+ }
47
+ }
48
+ /**
49
+ * Return `length` bytes starting from `offset`
50
+ *
51
+ * @param data The original data
52
+ * @param offset The offset to start from
53
+ * @param length The length of data to be returned
54
+ */
55
+
56
+ export function bytesAtOffset(data, offset, length) {
57
+ const offsetBytes = data.slice(offset, offset + length); // We are returning strongly typed Bytes so we have to verify that length is really what we claim
58
+
59
+ assertBytes(offsetBytes, length);
60
+ return offsetBytes;
61
+ }
62
+ /**
63
+ * Return flex bytes starting from `offset`
64
+ *
65
+ * @param data The original data
66
+ * @param offset The offset to start from
67
+ * @param _min The minimum size of the data
68
+ * @param _max The maximum size of the data
69
+ */
70
+
71
+ export function flexBytesAtOffset(data, offset, // eslint-disable-next-line @typescript-eslint/no-unused-vars
72
+ _min, // eslint-disable-next-line @typescript-eslint/no-unused-vars
73
+ _max) {
74
+ return data.slice(offset);
75
+ }
76
+ /**
77
+ * Returns true if two byte arrays are equal
78
+ *
79
+ * @param a Byte array to compare
80
+ * @param b Byte array to compare
81
+ */
82
+
83
+ export function bytesEqual(a, b) {
84
+ return a.length === b.length && a.every((value, index) => value === b[index]);
85
+ }
86
+ /**
87
+ * Returns a new byte array filled with zeroes with the specified length
88
+ *
89
+ * @param length The length of data to be returned
90
+ */
91
+
92
+ export function makeBytes(length) {
93
+ return new Uint8Array(length);
94
+ }
95
+ export function wrapBytesWithHelpers(data) {
96
+ return Object.assign(data, {
97
+ text: () => new TextDecoder('utf-8').decode(data),
98
+ json: () => JSON.parse(new TextDecoder('utf-8').decode(data)),
99
+ hex: () => bytesToHex(data)
100
+ });
101
+ }
@@ -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,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,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,73 @@
1
+ import { isNodeReadable, isReadableStream } from "./stream.js";
2
+ import Blob from 'cross-blob';
3
+ /**
4
+ * Validates input and converts to Uint8Array
5
+ *
6
+ * @param data any string, ArrayBuffer or Uint8Array
7
+ */
8
+
9
+ export async function prepareData(data) {
10
+ if (typeof data === 'string') return new Blob([data], {
11
+ type: 'text/plain'
12
+ });
13
+
14
+ if (data instanceof Uint8Array || data instanceof ArrayBuffer) {
15
+ return new Blob([data], {
16
+ type: 'application/octet-stream'
17
+ });
18
+ }
19
+
20
+ if (data instanceof Blob) {
21
+ return data;
22
+ } // Currently it is not possible to stream requests from browsers
23
+ // there are already first experiments on this field (Chromium)
24
+ // but till it is fully implemented across browsers-land we have to
25
+ // buffer the data before sending the requests.
26
+
27
+
28
+ if (isNodeReadable(data)) {
29
+ return new Promise(resolve => {
30
+ const buffers = [];
31
+ data.on('data', d => {
32
+ buffers.push(d);
33
+ });
34
+ data.on('end', () => {
35
+ resolve(new Blob(buffers, {
36
+ type: 'application/octet-stream'
37
+ }));
38
+ });
39
+ });
40
+ }
41
+
42
+ if (isReadableStream(data)) {
43
+ return new Promise(async resolve => {
44
+ const reader = data.getReader();
45
+ const buffers = [];
46
+ let done, value;
47
+
48
+ do {
49
+ ;
50
+ ({
51
+ done,
52
+ value
53
+ } = await reader.read());
54
+
55
+ if (!done) {
56
+ buffers.push(value);
57
+ }
58
+ } while (!done);
59
+
60
+ resolve(new Blob(buffers, {
61
+ type: 'application/octet-stream'
62
+ }));
63
+ });
64
+ }
65
+
66
+ throw new TypeError('unknown data type');
67
+ }
68
+ export async function prepareWebsocketData(data) {
69
+ if (typeof data === 'string') return new TextEncoder().encode(data);
70
+ if (data instanceof ArrayBuffer) return new Uint8Array(data);
71
+ if (data instanceof Blob) return new Uint8Array(await new Response(data).arrayBuffer());
72
+ throw new TypeError('unknown websocket data type');
73
+ }
@@ -0,0 +1,43 @@
1
+ import Blob from 'cross-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 Blob([data], {
14
+ type: 'text/plain'
15
+ });
16
+
17
+ if (data instanceof Uint8Array || data instanceof ArrayBuffer) {
18
+ return new Blob([data], {
19
+ type: 'application/octet-stream'
20
+ });
21
+ }
22
+
23
+ if (data instanceof Blob || 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 (data instanceof Blob) return new Uint8Array(await new Response(data).arrayBuffer());
41
+ if (isBufferArray(data)) return new Uint8Array(Buffer.concat(data));
42
+ throw new TypeError('unknown websocket data type');
43
+ }
@@ -0,0 +1,56 @@
1
+ export class BeeError extends Error {
2
+ constructor(message) {
3
+ super(message);
4
+ }
5
+
6
+ }
7
+ export class BeeArgumentError extends BeeError {
8
+ value;
9
+
10
+ constructor(message, value) {
11
+ super(message);
12
+ this.value = value;
13
+ }
14
+
15
+ }
16
+ export class BeeRequestError extends BeeError {
17
+ requestOptions;
18
+ /**
19
+ * @param message
20
+ * @param requestOptions KyOptions that were used to assemble the request. THIS MIGHT NOT BE COMPLETE! If custom Ky instance was used that has set defaults then these defaults are not visible in this object!
21
+ */
22
+
23
+ constructor(message, requestOptions) {
24
+ super(message);
25
+ this.requestOptions = requestOptions;
26
+ }
27
+
28
+ }
29
+ export class BeeResponseError extends BeeError {
30
+ status;
31
+ response;
32
+ responseBody;
33
+ requestOptions;
34
+ /**
35
+ * @param status HTTP status code number
36
+ * @param response Response returned from the server
37
+ * @param responseBody Response body as string which is returned from response.text() call
38
+ * @param requestOptions KyOptions that were used to assemble the request. THIS MIGHT NOT BE COMPLETE! If custom Ky instance was used that has set defaults then these defaults are not visible in this object!
39
+ * @param message
40
+ */
41
+
42
+ constructor(status, response, responseBody, requestOptions, message) {
43
+ super(message);
44
+ this.status = status;
45
+ this.response = response;
46
+ this.responseBody = responseBody;
47
+ this.requestOptions = requestOptions;
48
+ }
49
+
50
+ }
51
+ export class BeeNotAJsonError extends BeeError {
52
+ constructor() {
53
+ super(`Received response is not valid JSON.`);
54
+ }
55
+
56
+ }
@@ -0,0 +1,199 @@
1
+ // For ESM compatibility
2
+ import pkg from 'js-sha3';
3
+ const {
4
+ keccak256,
5
+ sha3_256
6
+ } = pkg;
7
+ import { hexToBytes, intToHex, makeHexString, assertHexString } from "./hex.js";
8
+ import { assertBytes } from "./bytes.js";
9
+ const ETH_ADDR_BYTES_LENGTH = 20;
10
+ const ETH_ADDR_HEX_LENGTH = 40;
11
+ export function makeEthAddress(address) {
12
+ if (typeof address === 'string') {
13
+ const hexAddr = makeHexString(address, ETH_ADDR_HEX_LENGTH);
14
+ const ownerBytes = hexToBytes(hexAddr);
15
+ assertBytes(ownerBytes, ETH_ADDR_BYTES_LENGTH);
16
+ return ownerBytes;
17
+ } else if (address instanceof Uint8Array) {
18
+ assertBytes(address, ETH_ADDR_BYTES_LENGTH);
19
+ return address;
20
+ }
21
+
22
+ throw new TypeError('Invalid EthAddress');
23
+ }
24
+ export function makeHexEthAddress(address) {
25
+ try {
26
+ return makeHexString(address, ETH_ADDR_HEX_LENGTH);
27
+ } catch (e) {
28
+ if (e instanceof TypeError) {
29
+ e.message = `Invalid HexEthAddress: ${e.message}`;
30
+ }
31
+
32
+ throw e;
33
+ }
34
+ }
35
+ /**
36
+ * Check if this is all caps or small caps eth address (=address without checksum)
37
+ *
38
+ * @param address Ethereum address as hex string
39
+ */
40
+
41
+ function isEthAddrCaseIns(address) {
42
+ // Check it's string, all small caps or all all caps hex and 40 chars long without the `0x` prefix
43
+ return typeof address === 'string' && (/^(0x|0X)?[0-9a-f]{40}$/.test(address) || /^(0x|0X)?[0-9A-F]{40}$/.test(address));
44
+ }
45
+ /**
46
+ * Check if this is checksummed ethereum address
47
+ *
48
+ * @param address Ethereum address as hex string
49
+ */
50
+
51
+
52
+ function isValidChecksummedEthAddress(address) {
53
+ try {
54
+ // Check for valid case insensitive hex type string, 40 chars
55
+ const addr = makeHexString(address, ETH_ADDR_HEX_LENGTH); // Check the checksum
56
+
57
+ const addressHash = keccak256(addr.toLowerCase());
58
+
59
+ for (let i = 0; i < 40; i += 1) {
60
+ // the nth letter should be uppercase if the nth digit of casemap is 1
61
+ if (parseInt(addressHash[i], 16) > 7 && addr[i].toUpperCase() !== addr[i] || parseInt(addressHash[i], 16) <= 7 && addr[i].toLowerCase() !== addr[i]) {
62
+ return false;
63
+ }
64
+ }
65
+
66
+ return true;
67
+ } catch (e) {
68
+ if (e instanceof TypeError) {
69
+ return false;
70
+ }
71
+
72
+ throw e;
73
+ }
74
+ }
75
+ /**
76
+ * Check if is valid ethereum address
77
+ *
78
+ * Pretty much typed version from web3js
79
+ * https://github.com/ChainSafe/web3.js/blob/1.x/packages/web3-utils/src/utils.js
80
+ *
81
+ * @param address Ethereum address as hex string
82
+ *
83
+ * @return True if is valid eth address
84
+ */
85
+
86
+
87
+ export function isHexEthAddress(address) {
88
+ return isEthAddrCaseIns(address) || isValidChecksummedEthAddress(address);
89
+ }
90
+ /**
91
+ * Convert big-endian hex or number to little-endian.
92
+ * Note: Before conversion it is automatically padded to even length hexstring
93
+ *
94
+ * @param bigEndian Big-endian hex string or number to convert
95
+ * @param pad Length to which the string should be padded before conversion (defaul: 2)
96
+ *
97
+ * @return little-endian encoded hexstring
98
+ */
99
+
100
+ export function toLittleEndian(bigEndian, pad = 2) {
101
+ if (!(Number.isInteger(pad) && pad >= 2 && pad % 2 === 0)) {
102
+ throw new TypeError('minimal padding for conversion needs to be positive even integer');
103
+ }
104
+
105
+ let hexRep;
106
+ if (typeof bigEndian === 'string') hexRep = makeHexString(bigEndian);else if (typeof bigEndian === 'number') hexRep = intToHex(bigEndian);else throw new TypeError('incorrect input type');
107
+ hexRep = hexRep.padStart(pad, '0'); // Extend to an even length hexstring
108
+
109
+ if (hexRep.length % 2 !== 0) hexRep = hexRep.padStart(hexRep.length + 1, '0'); // Match all two pairs in the hexstring, reverse the pairs and join it again
110
+
111
+ const littleEndian = hexRep.match(/../g)?.reverse().join('');
112
+ if (littleEndian) return littleEndian;
113
+ throw new Error('failed to convert');
114
+ }
115
+ /**
116
+ * Convert little-endian hex or number to big-endian
117
+ * Note: Before conversion it is automatically padded to even length hexstring
118
+ *
119
+ * @param littleEndian Little-endian hex string or number to convert
120
+ * @param pad Length to which the string should be padded before conversion (defaul: 2)
121
+ *
122
+ * @return big-endian encoded hexstring
123
+ */
124
+
125
+ export function fromLittleEndian(littleEndian, pad = 2) {
126
+ // It's a reversible function
127
+ return toLittleEndian(littleEndian, pad);
128
+ }
129
+
130
+ function assertEthAddress(ethAddress) {
131
+ if (!isHexEthAddress(ethAddress)) throw new TypeError('invalid ETH address');
132
+ }
133
+
134
+ function assertSwarmNetworkId(networkId) {
135
+ if (Number.isInteger(networkId && networkId > 0 && networkId < Number.MAX_SAFE_INTEGER)) {
136
+ throw new TypeError('swarm network id must be positive integer');
137
+ }
138
+ }
139
+ /**
140
+ * Get swarm overlay address from public ethereum address and swarm network id
141
+ *
142
+ * @param ethAddress Public ethereum address
143
+ * @param networkId Swarm network id
144
+ *
145
+ * @return Swarm overlay address
146
+ */
147
+
148
+
149
+ export function ethToSwarmAddress(ethAddress, networkId = 1) {
150
+ assertEthAddress(ethAddress);
151
+ assertSwarmNetworkId(networkId);
152
+ const hex = `${makeHexString(ethAddress)}${toLittleEndian(networkId, 16)}`;
153
+ assertHexString(hex);
154
+ const overlayAddress = sha3_256(hexToBytes(hex));
155
+ return overlayAddress;
156
+ }
157
+ /**
158
+ * Function that takes Ethereum EIP-1193 compatible provider and create an Signer instance that
159
+ * uses `personal_sign` method to sign requested data.
160
+ *
161
+ * @param provider Injected web3 provider like window.ethereum or other compatible with EIP-1193
162
+ * @param ethAddress Optional address of the account which the data should be signed with. If not specified `eth_requestAccounts` request is used to get the account address.
163
+ */
164
+
165
+ export async function makeEthereumWalletSigner(provider, ethAddress) {
166
+ let executorFnc;
167
+
168
+ if (typeof provider !== 'object' || provider === null) {
169
+ throw new TypeError('We need JsonRPC provider object!');
170
+ }
171
+
172
+ if (provider.request) {
173
+ executorFnc = provider.request;
174
+ } else if (provider.sendAsync) {
175
+ executorFnc = provider.sendAsync;
176
+ } else {
177
+ throw new Error('Incompatible interface of given provider!');
178
+ }
179
+
180
+ if (!ethAddress) {
181
+ ethAddress = (await executorFnc({
182
+ method: 'eth_requestAccounts'
183
+ }))[0];
184
+ }
185
+
186
+ const bytesEthAddress = makeEthAddress(ethAddress);
187
+ const hexEthAddress = makeHexEthAddress(ethAddress);
188
+ return {
189
+ address: bytesEthAddress,
190
+ sign: async data => {
191
+ const result = await executorFnc({
192
+ jsonrpc: '2.0',
193
+ method: 'personal_sign',
194
+ params: ['0x' + data.hex(), '0x' + hexEthAddress]
195
+ });
196
+ return result;
197
+ }
198
+ };
199
+ }
@@ -0,0 +1,9 @@
1
+ export { getCollectionSize } from "./collection.js";
2
+ export { getFolderSize } from "./collection.node.js";
3
+ export { isBytes, assertBytes, isFlexBytes, assertFlexBytes, bytesAtOffset, flexBytesAtOffset, bytesEqual } from "./bytes.js";
4
+ export { makeHexString, hexToBytes, bytesToHex, intToHex, isHexString, assertHexString, assertPrefixedHexString } from "./hex.js";
5
+ export { makeEthAddress, makeHexEthAddress, isHexEthAddress, ethToSwarmAddress, toLittleEndian, fromLittleEndian, makeEthereumWalletSigner } from "./eth.js";
6
+ export { readableWebToNode, readableNodeToWeb, isReadableStream, isNodeReadable, normalizeToReadableStream, isReadable } from "./stream.js";
7
+ export { keccak256Hash } from "./hash.js";
8
+ export { makeMaxTarget } from "./pss.js";
9
+ export { getStampUsage } from "./stamps.js";
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Compatibility functions for working with File API objects
3
+ *
4
+ * https://developer.mozilla.org/en-US/docs/Web/API/File
5
+ */
6
+ export function isFile(file) {
7
+ // browser
8
+ if (typeof File === 'function') {
9
+ return file instanceof File;
10
+ } // node.js
11
+
12
+
13
+ const f = file;
14
+ return typeof f === 'object' && typeof f.name === 'string' && (typeof f.stream === 'function' || typeof f.arrayBuffer === 'function');
15
+ }
16
+ /**
17
+ * Compatibility helper for browsers where the `arrayBuffer function is
18
+ * missing from `File` objects.
19
+ *
20
+ * @param file A File object
21
+ */
22
+
23
+ export async function fileArrayBuffer(file) {
24
+ if (file.arrayBuffer) {
25
+ return file.arrayBuffer();
26
+ } // workaround for Safari where arrayBuffer is not supported on Files
27
+
28
+
29
+ return new Promise(resolve => {
30
+ const fr = new FileReader();
31
+
32
+ fr.onload = () => resolve(fr.result);
33
+
34
+ fr.readAsArrayBuffer(file);
35
+ });
36
+ }
@@ -0,0 +1,17 @@
1
+ // For ESM compatibility
2
+ import pkg from 'js-sha3';
3
+ const {
4
+ keccak256
5
+ } = pkg;
6
+ /**
7
+ * Helper function for calculating the keccak256 hash with
8
+ * correct types.
9
+ *
10
+ * @param messages Any number of messages (strings, byte arrays etc.)
11
+ */
12
+
13
+ export function keccak256Hash(...messages) {
14
+ const hasher = keccak256.create();
15
+ messages.forEach(bytes => hasher.update(bytes));
16
+ return Uint8Array.from(hasher.digest());
17
+ }