@ethersphere/bee-js 3.3.0 → 3.3.1

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 (85) hide show
  1. package/dist/cjs/chunk/bmt.js +7 -2
  2. package/dist/cjs/chunk/signer.js +9 -4
  3. package/dist/cjs/modules/debug/status.js +5 -5
  4. package/dist/cjs/package.json +1 -0
  5. package/dist/cjs/types/ky-options.js +8 -0
  6. package/dist/cjs/types/ky-universal/common.js +8 -0
  7. package/dist/cjs/types/ky-universal/hooks.js +8 -0
  8. package/dist/cjs/types/ky-universal/retry.js +8 -0
  9. package/dist/cjs/utils/eth.js +8 -3
  10. package/dist/cjs/utils/hash.js +7 -2
  11. package/dist/cjs/utils/stream.js +14 -5
  12. package/dist/index.browser.min.js +3 -0
  13. package/dist/index.browser.min.js.LICENSE.txt +52 -0
  14. package/dist/index.browser.min.js.map +1 -0
  15. package/dist/mjs/bee-debug.js +608 -508
  16. package/dist/mjs/bee.js +935 -836
  17. package/dist/mjs/chunk/bmt.js +34 -24
  18. package/dist/mjs/chunk/cac.js +24 -22
  19. package/dist/mjs/chunk/serialize.js +9 -9
  20. package/dist/mjs/chunk/signer.js +92 -73
  21. package/dist/mjs/chunk/soc.js +78 -66
  22. package/dist/mjs/chunk/span.js +19 -16
  23. package/dist/mjs/feed/index.js +110 -101
  24. package/dist/mjs/feed/json.js +21 -17
  25. package/dist/mjs/feed/topic.js +18 -17
  26. package/dist/mjs/feed/type.js +5 -5
  27. package/dist/mjs/index.js +7 -7
  28. package/dist/mjs/modules/bytes.js +33 -30
  29. package/dist/mjs/modules/bzz.js +80 -77
  30. package/dist/mjs/modules/chunk.js +22 -20
  31. package/dist/mjs/modules/debug/balance.js +26 -22
  32. package/dist/mjs/modules/debug/chequebook.js +84 -65
  33. package/dist/mjs/modules/debug/chunk.js +15 -13
  34. package/dist/mjs/modules/debug/connectivity.js +34 -34
  35. package/dist/mjs/modules/debug/settlements.js +14 -12
  36. package/dist/mjs/modules/debug/stamps.js +52 -47
  37. package/dist/mjs/modules/debug/states.js +16 -14
  38. package/dist/mjs/modules/debug/status.js +55 -35
  39. package/dist/mjs/modules/debug/tag.js +8 -7
  40. package/dist/mjs/modules/debug/transactions.js +31 -25
  41. package/dist/mjs/modules/feed.js +39 -33
  42. package/dist/mjs/modules/pinning.js +34 -28
  43. package/dist/mjs/modules/pss.js +18 -14
  44. package/dist/mjs/modules/soc.js +18 -15
  45. package/dist/mjs/modules/status.js +6 -5
  46. package/dist/mjs/modules/stewardship.js +13 -12
  47. package/dist/mjs/modules/tag.js +38 -30
  48. package/dist/mjs/package.json +1 -0
  49. package/dist/mjs/types/debug.js +5 -4
  50. package/dist/mjs/types/index.js +6 -2
  51. package/dist/mjs/types/ky-options.js +7 -0
  52. package/dist/mjs/types/ky-universal/common.js +7 -0
  53. package/dist/mjs/types/ky-universal/hooks.js +7 -0
  54. package/dist/mjs/types/ky-universal/retry.js +7 -0
  55. package/dist/mjs/utils/bytes.js +32 -26
  56. package/dist/mjs/utils/collection.browser.js +4 -3
  57. package/dist/mjs/utils/collection.js +47 -35
  58. package/dist/mjs/utils/collection.node.js +53 -42
  59. package/dist/mjs/utils/data.browser.js +64 -48
  60. package/dist/mjs/utils/data.js +29 -25
  61. package/dist/mjs/utils/error.js +47 -39
  62. package/dist/mjs/utils/eth.js +119 -104
  63. package/dist/mjs/utils/expose.js +9 -9
  64. package/dist/mjs/utils/file.js +22 -19
  65. package/dist/mjs/utils/hash.js +10 -5
  66. package/dist/mjs/utils/headers.js +43 -39
  67. package/dist/mjs/utils/hex.js +78 -63
  68. package/dist/mjs/utils/http.js +119 -100
  69. package/dist/mjs/utils/merge.js +26 -20
  70. package/dist/mjs/utils/pss.js +8 -6
  71. package/dist/mjs/utils/stamps.js +7 -3
  72. package/dist/mjs/utils/stream.js +107 -82
  73. package/dist/mjs/utils/tar.js +17 -14
  74. package/dist/mjs/utils/type.js +249 -209
  75. package/dist/mjs/utils/uint64.js +16 -16
  76. package/dist/mjs/utils/url.js +32 -25
  77. package/dist/types/types/index.d.ts +1 -1
  78. package/dist/types/types/ky-options.d.ts +221 -0
  79. package/dist/types/types/ky-universal/common.d.ts +13 -0
  80. package/dist/types/types/ky-universal/hooks.d.ts +92 -0
  81. package/dist/types/types/ky-universal/retry.d.ts +38 -0
  82. package/dist/types/utils/stream.d.ts +11 -8
  83. package/package.json +9 -6
  84. package/dist/index.js +0 -28326
  85. package/dist/index.js.map +0 -1
@@ -1,12 +1,17 @@
1
- import { keccak256 } from 'js-sha3';
1
+ // For ESM compatibility
2
+ import pkg from 'js-sha3';
3
+ const {
4
+ keccak256
5
+ } = pkg;
2
6
  /**
3
7
  * Helper function for calculating the keccak256 hash with
4
8
  * correct types.
5
9
  *
6
10
  * @param messages Any number of messages (strings, byte arrays etc.)
7
11
  */
12
+
8
13
  export function keccak256Hash(...messages) {
9
- const hasher = keccak256.create();
10
- messages.forEach(bytes => hasher.update(bytes));
11
- return Uint8Array.from(hasher.digest());
12
- }
14
+ const hasher = keccak256.create();
15
+ messages.forEach(bytes => hasher.update(bytes));
16
+ return Uint8Array.from(hasher.digest());
17
+ }
@@ -1,4 +1,4 @@
1
- import { BeeError } from './error';
1
+ import { BeeError } from "./error.js";
2
2
  /**
3
3
  * Read the filename from the content-disposition header
4
4
  * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
@@ -7,48 +7,52 @@ import { BeeError } from './error';
7
7
  *
8
8
  * @returns the filename
9
9
  */
10
+
10
11
  function readContentDispositionFilename(header) {
11
- if (!header) {
12
- throw new BeeError('missing content-disposition header');
13
- }
14
- // Regex was found here
15
- // https://stackoverflow.com/questions/23054475/javascript-regex-for-extracting-filename-from-content-disposition-header
16
- const dispositionMatch = header.match(/filename\*?=['"]?(?:UTF-\d['"]*)?([^;\r\n"']*)['"]?;?/i);
17
- if (dispositionMatch && dispositionMatch.length > 0) {
18
- return dispositionMatch[1];
19
- }
20
- throw new BeeError('invalid content-disposition header');
12
+ if (!header) {
13
+ throw new BeeError('missing content-disposition header');
14
+ } // Regex was found here
15
+ // https://stackoverflow.com/questions/23054475/javascript-regex-for-extracting-filename-from-content-disposition-header
16
+
17
+
18
+ const dispositionMatch = header.match(/filename\*?=['"]?(?:UTF-\d['"]*)?([^;\r\n"']*)['"]?;?/i);
19
+
20
+ if (dispositionMatch && dispositionMatch.length > 0) {
21
+ return dispositionMatch[1];
22
+ }
23
+
24
+ throw new BeeError('invalid content-disposition header');
21
25
  }
26
+
22
27
  function readTagUid(header) {
23
- if (!header) {
24
- return undefined;
25
- }
26
- return parseInt(header, 10);
28
+ if (!header) {
29
+ return undefined;
30
+ }
31
+
32
+ return parseInt(header, 10);
27
33
  }
34
+
28
35
  export function readFileHeaders(headers) {
29
- const name = readContentDispositionFilename(headers.get('content-disposition'));
30
- const tagUid = readTagUid(headers.get('swarm-tag-uid'));
31
- const contentType = headers.get('content-type') || undefined;
32
- return {
33
- name,
34
- tagUid,
35
- contentType,
36
- };
36
+ const name = readContentDispositionFilename(headers.get('content-disposition'));
37
+ const tagUid = readTagUid(headers.get('swarm-tag-uid'));
38
+ const contentType = headers.get('content-type') || undefined;
39
+ return {
40
+ name,
41
+ tagUid,
42
+ contentType
43
+ };
37
44
  }
38
45
  export function extractUploadHeaders(postageBatchId, options) {
39
- if (!postageBatchId) {
40
- throw new BeeError('Postage BatchID has to be specified!');
41
- }
42
- const headers = {
43
- 'swarm-postage-batch-id': postageBatchId,
44
- };
45
- if (options?.pin)
46
- headers['swarm-pin'] = String(options.pin);
47
- if (options?.encrypt)
48
- headers['swarm-encrypt'] = String(options.encrypt);
49
- if (options?.tag)
50
- headers['swarm-tag'] = String(options.tag);
51
- if (typeof options?.deferred === 'boolean')
52
- headers['swarm-deferred-upload'] = options.deferred.toString();
53
- return headers;
54
- }
46
+ if (!postageBatchId) {
47
+ throw new BeeError('Postage BatchID has to be specified!');
48
+ }
49
+
50
+ const headers = {
51
+ 'swarm-postage-batch-id': postageBatchId
52
+ };
53
+ if (options?.pin) headers['swarm-pin'] = String(options.pin);
54
+ if (options?.encrypt) headers['swarm-encrypt'] = String(options.encrypt);
55
+ if (options?.tag) headers['swarm-tag'] = String(options.tag);
56
+ if (typeof options?.deferred === 'boolean') headers['swarm-deferred-upload'] = options.deferred.toString();
57
+ return headers;
58
+ }
@@ -1,4 +1,4 @@
1
- import { makeBytes } from './bytes';
1
+ import { makeBytes } from "./bytes.js";
2
2
  /**
3
3
  * Creates unprefixed hex string from wide range of data.
4
4
  *
@@ -7,44 +7,52 @@ import { makeBytes } from './bytes';
7
7
  * @param input
8
8
  * @param len of the resulting HexString WITHOUT prefix!
9
9
  */
10
+
10
11
  export function makeHexString(input, len) {
11
- if (typeof input === 'number') {
12
- return intToHex(input, len);
12
+ if (typeof input === 'number') {
13
+ return intToHex(input, len);
14
+ }
15
+
16
+ if (input instanceof Uint8Array) {
17
+ return bytesToHex(input, len);
18
+ }
19
+
20
+ if (typeof input === 'string') {
21
+ if (isPrefixedHexString(input)) {
22
+ const hex = input.slice(2);
23
+
24
+ if (len && hex.length !== len) {
25
+ throw new TypeError(`Length mismatch for valid hex string. Expecting length ${len}: ${hex}`);
26
+ }
27
+
28
+ return hex;
29
+ } else {
30
+ // We use assertHexString() as there might be more reasons why a string is not valid hex string
31
+ // and usage of isHexString() would not give enough information to the user on what is going
32
+ // wrong.
33
+ assertHexString(input, len);
34
+ return input;
13
35
  }
14
- if (input instanceof Uint8Array) {
15
- return bytesToHex(input, len);
16
- }
17
- if (typeof input === 'string') {
18
- if (isPrefixedHexString(input)) {
19
- const hex = input.slice(2);
20
- if (len && hex.length !== len) {
21
- throw new TypeError(`Length mismatch for valid hex string. Expecting length ${len}: ${hex}`);
22
- }
23
- return hex;
24
- }
25
- else {
26
- // We use assertHexString() as there might be more reasons why a string is not valid hex string
27
- // and usage of isHexString() would not give enough information to the user on what is going
28
- // wrong.
29
- assertHexString(input, len);
30
- return input;
31
- }
32
- }
33
- throw new TypeError('Not HexString compatible type!');
36
+ }
37
+
38
+ throw new TypeError('Not HexString compatible type!');
34
39
  }
35
40
  /**
36
41
  * Converts a hex string to Uint8Array
37
42
  *
38
43
  * @param hex string input without 0x prefix!
39
44
  */
45
+
40
46
  export function hexToBytes(hex) {
41
- assertHexString(hex);
42
- const bytes = makeBytes(hex.length / 2);
43
- for (let i = 0; i < bytes.length; i++) {
44
- const hexByte = hex.substr(i * 2, 2);
45
- bytes[i] = parseInt(hexByte, 16);
46
- }
47
- return bytes;
47
+ assertHexString(hex);
48
+ const bytes = makeBytes(hex.length / 2);
49
+
50
+ for (let i = 0; i < bytes.length; i++) {
51
+ const hexByte = hex.substr(i * 2, 2);
52
+ bytes[i] = parseInt(hexByte, 16);
53
+ }
54
+
55
+ return bytes;
48
56
  }
49
57
  /**
50
58
  * Converts array of number or Uint8Array to HexString without prefix.
@@ -52,14 +60,17 @@ export function hexToBytes(hex) {
52
60
  * @param bytes The input array
53
61
  * @param len The length of the non prefixed HexString
54
62
  */
63
+
55
64
  export function bytesToHex(bytes, len) {
56
- const hexByte = (n) => n.toString(16).padStart(2, '0');
57
- const hex = Array.from(bytes, hexByte).join('');
58
- // TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
59
- if (len && hex.length !== len) {
60
- throw new TypeError(`Resulting HexString does not have expected length ${len}: ${hex}`);
61
- }
62
- return hex;
65
+ const hexByte = n => n.toString(16).padStart(2, '0');
66
+
67
+ const hex = Array.from(bytes, hexByte).join(''); // TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
68
+
69
+ if (len && hex.length !== len) {
70
+ throw new TypeError(`Resulting HexString does not have expected length ${len}: ${hex}`);
71
+ }
72
+
73
+ return hex;
63
74
  }
64
75
  /**
65
76
  * Converts integer number to hex string.
@@ -69,19 +80,18 @@ export function bytesToHex(bytes, len) {
69
80
  * @param int The positive integer to be converted
70
81
  * @param len The length of the non prefixed HexString
71
82
  */
83
+
72
84
  export function intToHex(int, len) {
73
- if (!Number.isInteger(int))
74
- throw new TypeError('the value provided is not integer');
75
- if (int > Number.MAX_SAFE_INTEGER)
76
- throw new TypeError('the value provided exceeds safe integer');
77
- if (int < 0)
78
- throw new TypeError('the value provided is a negative integer');
79
- const hex = int.toString(16);
80
- // TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
81
- if (len && hex.length !== len) {
82
- throw new TypeError(`Resulting HexString does not have expected length ${len}: ${hex}`);
83
- }
84
- return hex;
85
+ if (!Number.isInteger(int)) throw new TypeError('the value provided is not integer');
86
+ if (int > Number.MAX_SAFE_INTEGER) throw new TypeError('the value provided exceeds safe integer');
87
+ if (int < 0) throw new TypeError('the value provided is a negative integer');
88
+ const hex = int.toString(16); // TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
89
+
90
+ if (len && hex.length !== len) {
91
+ throw new TypeError(`Resulting HexString does not have expected length ${len}: ${hex}`);
92
+ }
93
+
94
+ return hex;
85
95
  }
86
96
  /**
87
97
  * Type guard for HexStrings.
@@ -92,8 +102,9 @@ export function intToHex(int, len) {
92
102
  * @param s string input
93
103
  * @param len expected length of the HexString
94
104
  */
105
+
95
106
  export function isHexString(s, len) {
96
- return typeof s === 'string' && /^[0-9a-f]+$/i.test(s) && (!len || s.length === len);
107
+ return typeof s === 'string' && /^[0-9a-f]+$/i.test(s) && (!len || s.length === len);
97
108
  }
98
109
  /**
99
110
  * Type guard for PrefixedHexStrings.
@@ -101,8 +112,9 @@ export function isHexString(s, len) {
101
112
  *
102
113
  * @param s string input
103
114
  */
115
+
104
116
  export function isPrefixedHexString(s) {
105
- return typeof s === 'string' && /^0x[0-9a-f]+$/i.test(s);
117
+ return typeof s === 'string' && /^0x[0-9a-f]+$/i.test(s);
106
118
  }
107
119
  /**
108
120
  * Verifies if the provided input is a HexString.
@@ -114,15 +126,17 @@ export function isPrefixedHexString(s) {
114
126
  * @param name optional name for the asserted value
115
127
  * @returns HexString or throws error
116
128
  */
129
+
117
130
  export function assertHexString(s, len, name = 'value') {
118
- if (!isHexString(s, len)) {
119
- if (isPrefixedHexString(s)) {
120
- throw new TypeError(`${name} not valid non prefixed hex string (has 0x prefix): ${s}`);
121
- }
122
- // Don't display length error if no length specified in order not to confuse user
123
- const lengthMsg = len ? ` of length ${len}` : '';
124
- throw new TypeError(`${name} not valid hex string${lengthMsg}: ${s}`);
125
- }
131
+ if (!isHexString(s, len)) {
132
+ if (isPrefixedHexString(s)) {
133
+ throw new TypeError(`${name} not valid non prefixed hex string (has 0x prefix): ${s}`);
134
+ } // Don't display length error if no length specified in order not to confuse user
135
+
136
+
137
+ const lengthMsg = len ? ` of length ${len}` : '';
138
+ throw new TypeError(`${name} not valid hex string${lengthMsg}: ${s}`);
139
+ }
126
140
  }
127
141
  /**
128
142
  * Verifies if the provided input is a PrefixedHexString.
@@ -132,8 +146,9 @@ export function assertHexString(s, len, name = 'value') {
132
146
  * @param name optional name for the asserted value
133
147
  * @returns HexString or throws error
134
148
  */
149
+
135
150
  export function assertPrefixedHexString(s, name = 'value') {
136
- if (!isPrefixedHexString(s)) {
137
- throw new TypeError(`${name} not valid prefixed hex string: ${s}`);
138
- }
139
- }
151
+ if (!isPrefixedHexString(s)) {
152
+ throw new TypeError(`${name} not valid prefixed hex string: ${s}`);
153
+ }
154
+ }
@@ -1,47 +1,52 @@
1
- import { BeeError, BeeNotAJsonError, BeeRequestError, BeeResponseError } from './error';
1
+ import { BeeError, BeeNotAJsonError, BeeRequestError, BeeResponseError } from "./error.js";
2
2
  import kyFactory from 'ky-universal';
3
- import { normalizeToReadableStream } from './stream';
4
- import { deepMerge } from './merge';
5
- import { isObject, isStrictlyObject } from './type';
3
+ import { normalizeToReadableStream } from "./stream.js";
4
+ import { deepMerge } from "./merge.js";
5
+ import { isObject, isStrictlyObject } from "./type.js";
6
6
  const DEFAULT_KY_CONFIG = {
7
- headers: {
8
- accept: 'application/json, text/plain, */*',
9
- 'user-agent': `bee-js`,
10
- },
7
+ headers: {
8
+ accept: 'application/json, text/plain, */*',
9
+ 'user-agent': `bee-js`
10
+ }
11
11
  };
12
+
12
13
  function isHttpError(e) {
13
- return isObject(e) && typeof e.response !== 'undefined';
14
+ return isObject(e) && typeof e.response !== 'undefined';
14
15
  }
16
+
15
17
  function isHttpRequestError(e) {
16
- return isObject(e) && typeof e.request !== 'undefined';
18
+ return isObject(e) && typeof e.request !== 'undefined';
17
19
  }
20
+
18
21
  function headersToObject(header) {
19
- return [...header.entries()].reduce((obj, [key, val]) => {
20
- obj[key] = val;
21
- return obj;
22
- }, {});
22
+ return [...header.entries()].reduce((obj, [key, val]) => {
23
+ obj[key] = val;
24
+ return obj;
25
+ }, {});
23
26
  }
27
+
24
28
  function wrapRequest(request) {
25
- return {
26
- url: request.url,
27
- method: request.method.toUpperCase(),
28
- headers: headersToObject(request.headers),
29
- };
29
+ return {
30
+ url: request.url,
31
+ method: request.method.toUpperCase(),
32
+ headers: headersToObject(request.headers)
33
+ };
30
34
  }
35
+
31
36
  export function wrapRequestClosure(cb) {
32
- return async (request) => {
33
- await cb(wrapRequest(request));
34
- };
37
+ return async request => {
38
+ await cb(wrapRequest(request));
39
+ };
35
40
  }
36
41
  export function wrapResponseClosure(cb) {
37
- return async (request, options, response) => {
38
- await cb({
39
- headers: headersToObject(response.headers),
40
- status: response.status,
41
- statusText: response.statusText,
42
- request: wrapRequest(request),
43
- });
44
- };
42
+ return async (request, options, response) => {
43
+ await cb({
44
+ headers: headersToObject(response.headers),
45
+ status: response.status,
46
+ statusText: response.statusText,
47
+ request: wrapRequest(request)
48
+ });
49
+ };
45
50
  }
46
51
  /**
47
52
  * Filters out entries that has undefined value from headers object.
@@ -50,87 +55,101 @@ export function wrapResponseClosure(cb) {
50
55
  * @param obj
51
56
  */
52
57
  // eslint-disable-next-line @typescript-eslint/ban-types
58
+
53
59
  export function filterHeaders(obj) {
54
- if (obj === undefined) {
55
- return undefined;
60
+ if (obj === undefined) {
61
+ return undefined;
62
+ }
63
+
64
+ isStrictlyObject(obj);
65
+ const typedObj = obj;
66
+
67
+ for (const key in typedObj) {
68
+ if (typedObj[key] === undefined) {
69
+ delete typedObj[key];
56
70
  }
57
- isStrictlyObject(obj);
58
- const typedObj = obj;
59
- for (const key in typedObj) {
60
- if (typedObj[key] === undefined) {
61
- delete typedObj[key];
62
- }
63
- }
64
- if (Object.keys(typedObj).length === 0) {
65
- return undefined;
66
- }
67
- return typedObj;
71
+ }
72
+
73
+ if (Object.keys(typedObj).length === 0) {
74
+ return undefined;
75
+ }
76
+
77
+ return typedObj;
68
78
  }
69
79
  /**
70
80
  * Main utility function to make HTTP requests.
71
81
  * @param ky
72
82
  * @param config
73
83
  */
84
+
74
85
  export async function http(ky, config) {
75
- try {
76
- const { path, responseType, ...kyConfig } = config;
77
- const response = (await ky(path, {
78
- ...kyConfig,
79
- searchParams: filterHeaders(kyConfig.searchParams),
80
- }));
81
- switch (responseType) {
82
- case 'stream':
83
- if (!response.body) {
84
- throw new BeeError('Response was expected to get data but did not get any!');
85
- }
86
- response.data = normalizeToReadableStream(response.body);
87
- break;
88
- case 'arraybuffer':
89
- response.data = (await response.arrayBuffer());
90
- break;
91
- case 'json':
92
- try {
93
- response.data = (await response.json());
94
- }
95
- catch (e) {
96
- throw new BeeNotAJsonError();
97
- }
98
- break;
99
- default:
100
- break; // If responseType is not set, then no data are expected
101
- }
102
- return response;
103
- }
104
- catch (e) {
105
- // Passthrough thrown errors
106
- if (e instanceof BeeNotAJsonError) {
107
- throw e;
86
+ try {
87
+ const {
88
+ path,
89
+ responseType,
90
+ ...kyConfig
91
+ } = config;
92
+ const response = await ky(path, { ...kyConfig,
93
+ searchParams: filterHeaders(kyConfig.searchParams)
94
+ });
95
+
96
+ switch (responseType) {
97
+ case 'stream':
98
+ if (!response.body) {
99
+ throw new BeeError('Response was expected to get data but did not get any!');
108
100
  }
109
- if (isHttpError(e)) {
110
- let message;
111
- // We store the response body here as it can be read only once in Response's lifecycle so to make it exposed
112
- // to the user in the BeeResponseError, for further analysis.
113
- const body = await e.response.text();
114
- try {
115
- // The response can be Bee's JSON with structure `{code, message}` lets try to parse it
116
- message = JSON.parse(body).message;
117
- }
118
- catch (e) { }
119
- if (message) {
120
- throw new BeeResponseError(e.response.status, e.response, body, config, `${e.response.statusText}: ${message}`);
121
- }
122
- else {
123
- throw new BeeResponseError(e.response.status, e.response, body, config, e.response.statusText);
124
- }
125
- }
126
- else if (isHttpRequestError(e)) {
127
- throw new BeeRequestError(e.message, config);
128
- }
129
- else {
130
- throw new BeeError(e.message);
101
+
102
+ response.data = normalizeToReadableStream(response.body);
103
+ break;
104
+
105
+ case 'arraybuffer':
106
+ response.data = await response.arrayBuffer();
107
+ break;
108
+
109
+ case 'json':
110
+ try {
111
+ response.data = await response.json();
112
+ } catch (e) {
113
+ throw new BeeNotAJsonError();
131
114
  }
115
+
116
+ break;
117
+
118
+ default:
119
+ break;
120
+ // If responseType is not set, then no data are expected
121
+ }
122
+
123
+ return response;
124
+ } catch (e) {
125
+ // Passthrough thrown errors
126
+ if (e instanceof BeeNotAJsonError) {
127
+ throw e;
132
128
  }
129
+
130
+ if (isHttpError(e)) {
131
+ let message; // We store the response body here as it can be read only once in Response's lifecycle so to make it exposed
132
+ // to the user in the BeeResponseError, for further analysis.
133
+
134
+ const body = await e.response.text();
135
+
136
+ try {
137
+ // The response can be Bee's JSON with structure `{code, message}` lets try to parse it
138
+ message = JSON.parse(body).message;
139
+ } catch (e) {}
140
+
141
+ if (message) {
142
+ throw new BeeResponseError(e.response.status, e.response, body, config, `${e.response.statusText}: ${message}`);
143
+ } else {
144
+ throw new BeeResponseError(e.response.status, e.response, body, config, e.response.statusText);
145
+ }
146
+ } else if (isHttpRequestError(e)) {
147
+ throw new BeeRequestError(e.message, config);
148
+ } else {
149
+ throw new BeeError(e.message);
150
+ }
151
+ }
133
152
  }
134
153
  export function makeDefaultKy(kyConfig) {
135
- return kyFactory.create(deepMerge(DEFAULT_KY_CONFIG, kyConfig));
136
- }
154
+ return kyFactory.create(deepMerge(DEFAULT_KY_CONFIG, kyConfig));
155
+ }
@@ -1,4 +1,4 @@
1
- import { isObject } from './type';
1
+ import { isObject } from "./type.js";
2
2
  /**
3
3
  * Function that deep merges objects
4
4
  *
@@ -6,25 +6,31 @@ import { isObject } from './type';
6
6
  * @licence MIT
7
7
  * @param sources
8
8
  */
9
+
9
10
  export function deepMerge(...sources) {
10
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
- let returnValue = {};
12
- for (const source of sources) {
13
- if (Array.isArray(source)) {
14
- if (!Array.isArray(returnValue)) {
15
- returnValue = [];
16
- }
17
- returnValue = [...returnValue, ...source];
18
- }
19
- else if (isObject(source)) {
20
- // eslint-disable-next-line prefer-const
21
- for (let [key, value] of Object.entries(source)) {
22
- if (isObject(value) && key in returnValue) {
23
- value = deepMerge(returnValue[key], value);
24
- }
25
- returnValue = { ...returnValue, [key]: value };
26
- }
11
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
12
+ let returnValue = {};
13
+
14
+ for (const source of sources) {
15
+ if (Array.isArray(source)) {
16
+ if (!Array.isArray(returnValue)) {
17
+ returnValue = [];
18
+ }
19
+
20
+ returnValue = [...returnValue, ...source];
21
+ } else if (isObject(source)) {
22
+ // eslint-disable-next-line prefer-const
23
+ for (let [key, value] of Object.entries(source)) {
24
+ if (isObject(value) && key in returnValue) {
25
+ value = deepMerge(returnValue[key], value);
27
26
  }
27
+
28
+ returnValue = { ...returnValue,
29
+ [key]: value
30
+ };
31
+ }
28
32
  }
29
- return returnValue;
30
- }
33
+ }
34
+
35
+ return returnValue;
36
+ }
@@ -1,4 +1,4 @@
1
- import { PSS_TARGET_HEX_LENGTH_MAX } from '../types';
1
+ import { PSS_TARGET_HEX_LENGTH_MAX } from "../types/index.js";
2
2
  /**
3
3
  * Utility function that for given strings/reference takes the most specific
4
4
  * target that Bee node will except.
@@ -6,9 +6,11 @@ import { PSS_TARGET_HEX_LENGTH_MAX } from '../types';
6
6
  * @param target is a non-prefixed hex string Bee address
7
7
  * @see [Bee docs - PSS](https://docs.ethswarm.org/docs/dapps-on-swarm/pss)
8
8
  */
9
+
9
10
  export function makeMaxTarget(target) {
10
- if (typeof target !== 'string') {
11
- throw new TypeError('target has to be an string!');
12
- }
13
- return target.slice(0, PSS_TARGET_HEX_LENGTH_MAX);
14
- }
11
+ if (typeof target !== 'string') {
12
+ throw new TypeError('target has to be an string!');
13
+ }
14
+
15
+ return target.slice(0, PSS_TARGET_HEX_LENGTH_MAX);
16
+ }