@ethersphere/bee-js 5.1.1 → 5.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -6
- package/dist/223.index.browser.min.js +3 -0
- package/dist/223.index.browser.min.js.LICENSE.txt +1 -0
- package/dist/223.index.browser.min.js.map +1 -0
- package/dist/cjs/bee-debug.js +208 -312
- package/dist/cjs/bee.js +258 -353
- package/dist/cjs/chunk/signer.js +11 -22
- package/dist/cjs/chunk/soc.js +33 -50
- package/dist/cjs/chunk/span.js +1 -1
- package/dist/cjs/feed/index.js +50 -65
- package/dist/cjs/feed/json.js +8 -21
- package/dist/cjs/feed/retrievable.js +14 -27
- package/dist/cjs/modules/bytes.js +25 -37
- package/dist/cjs/modules/bzz.js +63 -68
- package/dist/cjs/modules/chunk.js +17 -27
- package/dist/cjs/modules/debug/balance.js +24 -41
- package/dist/cjs/modules/debug/chequebook.js +71 -96
- package/dist/cjs/modules/debug/chunk.js +13 -26
- package/dist/cjs/modules/debug/connectivity.js +32 -53
- package/dist/cjs/modules/debug/settlements.js +12 -25
- package/dist/cjs/modules/debug/stake.js +20 -33
- package/dist/cjs/modules/debug/stamps.js +45 -66
- package/dist/cjs/modules/debug/states.js +27 -40
- package/dist/cjs/modules/debug/status.js +53 -80
- package/dist/cjs/modules/debug/tag.js +6 -17
- package/dist/cjs/modules/debug/transactions.js +23 -40
- package/dist/cjs/modules/feed.js +17 -27
- package/dist/cjs/modules/pinning.js +24 -41
- package/dist/cjs/modules/pss.js +9 -20
- package/dist/cjs/modules/soc.js +12 -20
- package/dist/cjs/modules/status.js +4 -15
- package/dist/cjs/modules/stewardship.js +10 -23
- package/dist/cjs/modules/tag.js +28 -47
- package/dist/cjs/utils/collection.browser.js +4 -17
- package/dist/cjs/utils/collection.js +11 -22
- package/dist/cjs/utils/collection.node.js +42 -86
- package/dist/cjs/utils/data.browser.js +46 -59
- package/dist/cjs/utils/data.js +22 -35
- package/dist/cjs/utils/eth.js +31 -43
- package/dist/cjs/utils/file.js +9 -20
- package/dist/cjs/utils/headers.js +4 -4
- package/dist/cjs/utils/http.js +78 -93
- package/dist/cjs/utils/merge.js +1 -1
- package/dist/cjs/utils/sleep.js +2 -13
- package/dist/cjs/utils/type.js +14 -12
- package/dist/index.browser.min.js +1 -1
- package/dist/index.browser.min.js.LICENSE.txt +1 -3
- package/dist/index.browser.min.js.map +1 -1
- package/dist/mjs/bee-debug.js +209 -450
- package/dist/mjs/bee.js +256 -491
- package/dist/mjs/chunk/bmt.js +4 -10
- package/dist/mjs/chunk/cac.js +0 -3
- package/dist/mjs/chunk/signer.js +12 -63
- package/dist/mjs/chunk/soc.js +33 -85
- package/dist/mjs/chunk/span.js +3 -6
- package/dist/mjs/feed/identifier.js +0 -6
- package/dist/mjs/feed/index.js +53 -97
- package/dist/mjs/feed/json.js +10 -49
- package/dist/mjs/feed/retrievable.js +13 -64
- package/dist/mjs/feed/topic.js +0 -2
- package/dist/mjs/index.js +1 -2
- package/dist/mjs/modules/bytes.js +25 -65
- package/dist/mjs/modules/bzz.js +64 -108
- package/dist/mjs/modules/chunk.js +17 -54
- package/dist/mjs/modules/debug/balance.js +24 -68
- package/dist/mjs/modules/debug/chequebook.js +75 -138
- package/dist/mjs/modules/debug/chunk.js +13 -51
- package/dist/mjs/modules/debug/connectivity.js +32 -76
- package/dist/mjs/modules/debug/settlements.js +12 -50
- package/dist/mjs/modules/debug/stake.js +20 -61
- package/dist/mjs/modules/debug/stamps.js +47 -94
- package/dist/mjs/modules/debug/states.js +25 -66
- package/dist/mjs/modules/debug/status.js +66 -125
- package/dist/mjs/modules/debug/tag.js +6 -41
- package/dist/mjs/modules/debug/transactions.js +25 -69
- package/dist/mjs/modules/feed.js +17 -57
- package/dist/mjs/modules/pinning.js +24 -68
- package/dist/mjs/modules/pss.js +11 -47
- package/dist/mjs/modules/soc.js +14 -48
- package/dist/mjs/modules/status.js +4 -39
- package/dist/mjs/modules/stewardship.js +10 -47
- package/dist/mjs/modules/tag.js +31 -78
- package/dist/mjs/types/debug.js +0 -1
- package/dist/mjs/types/index.js +0 -4
- package/dist/mjs/utils/bytes.js +6 -14
- package/dist/mjs/utils/collection.browser.js +4 -42
- package/dist/mjs/utils/collection.js +11 -57
- package/dist/mjs/utils/collection.node.js +42 -150
- package/dist/mjs/utils/data.browser.js +49 -94
- package/dist/mjs/utils/data.js +19 -62
- package/dist/mjs/utils/error.js +0 -5
- package/dist/mjs/utils/eth.js +37 -95
- package/dist/mjs/utils/file.js +11 -50
- package/dist/mjs/utils/hash.js +0 -1
- package/dist/mjs/utils/headers.js +6 -14
- package/dist/mjs/utils/hex.js +6 -25
- package/dist/mjs/utils/http.js +80 -138
- package/dist/mjs/utils/merge.js +3 -7
- package/dist/mjs/utils/pss.js +0 -2
- package/dist/mjs/utils/reference.js +2 -5
- package/dist/mjs/utils/sleep.js +2 -37
- package/dist/mjs/utils/stream.js +0 -23
- package/dist/mjs/utils/tar.js +2 -5
- package/dist/mjs/utils/type.js +18 -82
- package/dist/mjs/utils/uint64.js +0 -1
- package/dist/mjs/utils/url.js +2 -10
- package/dist/types/bee-debug.d.ts +2 -2
- package/dist/types/bee.d.ts +2 -26
- package/dist/types/chunk/cac.d.ts +1 -1
- package/dist/types/chunk/soc.d.ts +8 -7
- package/dist/types/feed/index.d.ts +9 -8
- package/dist/types/feed/type.d.ts +1 -1
- package/dist/types/modules/bytes.d.ts +6 -5
- package/dist/types/modules/bzz.d.ts +9 -8
- package/dist/types/modules/chunk.d.ts +5 -4
- package/dist/types/modules/debug/balance.d.ts +10 -9
- package/dist/types/modules/debug/chequebook.d.ts +18 -17
- package/dist/types/modules/debug/chunk.d.ts +6 -5
- package/dist/types/modules/debug/connectivity.d.ts +8 -7
- package/dist/types/modules/debug/settlements.d.ts +6 -5
- package/dist/types/modules/debug/stake.d.ts +5 -4
- package/dist/types/modules/debug/stamps.d.ts +8 -7
- package/dist/types/modules/debug/states.d.ts +8 -7
- package/dist/types/modules/debug/status.d.ts +15 -15
- package/dist/types/modules/debug/tag.d.ts +4 -3
- package/dist/types/modules/debug/transactions.d.ts +6 -5
- package/dist/types/modules/feed.d.ts +4 -3
- package/dist/types/modules/pinning.d.ts +8 -7
- package/dist/types/modules/pss.d.ts +4 -3
- package/dist/types/modules/soc.d.ts +3 -2
- package/dist/types/modules/status.d.ts +3 -3
- package/dist/types/modules/stewardship.d.ts +4 -3
- package/dist/types/modules/tag.d.ts +7 -6
- package/dist/types/types/debug.d.ts +1 -1
- package/dist/types/types/index.d.ts +30 -34
- package/dist/types/utils/eth.d.ts +3 -3
- package/dist/types/utils/hex.d.ts +2 -2
- package/dist/types/utils/http.d.ts +6 -6
- package/package.json +34 -39
- package/dist/cjs/types/ky-options.js +0 -8
- package/dist/cjs/types/ky-universal/common.js +0 -8
- package/dist/cjs/types/ky-universal/hooks.js +0 -8
- package/dist/cjs/types/ky-universal/retry.js +0 -8
- package/dist/mjs/types/ky-options.js +0 -7
- package/dist/mjs/types/ky-universal/common.js +0 -7
- package/dist/mjs/types/ky-universal/hooks.js +0 -7
- package/dist/mjs/types/ky-universal/retry.js +0 -7
- package/dist/types/types/ky-options.d.ts +0 -221
- package/dist/types/types/ky-universal/common.d.ts +0 -13
- package/dist/types/types/ky-universal/hooks.d.ts +0 -92
- package/dist/types/types/ky-universal/retry.d.ts +0 -38
package/dist/mjs/utils/hex.js
CHANGED
|
@@ -7,24 +7,19 @@ import { makeBytes } from "./bytes.js";
|
|
|
7
7
|
* @param input
|
|
8
8
|
* @param len of the resulting HexString WITHOUT prefix!
|
|
9
9
|
*/
|
|
10
|
-
|
|
11
10
|
export function makeHexString(input, len) {
|
|
12
11
|
if (typeof input === 'number') {
|
|
13
12
|
return intToHex(input, len);
|
|
14
13
|
}
|
|
15
|
-
|
|
16
14
|
if (input instanceof Uint8Array) {
|
|
17
15
|
return bytesToHex(input, len);
|
|
18
16
|
}
|
|
19
|
-
|
|
20
17
|
if (typeof input === 'string') {
|
|
21
18
|
if (isPrefixedHexString(input)) {
|
|
22
19
|
const hex = input.slice(2);
|
|
23
|
-
|
|
24
20
|
if (len && hex.length !== len) {
|
|
25
21
|
throw new TypeError(`Length mismatch for valid hex string. Expecting length ${len}: ${hex}`);
|
|
26
22
|
}
|
|
27
|
-
|
|
28
23
|
return hex;
|
|
29
24
|
} else {
|
|
30
25
|
// We use assertHexString() as there might be more reasons why a string is not valid hex string
|
|
@@ -34,7 +29,6 @@ export function makeHexString(input, len) {
|
|
|
34
29
|
return input;
|
|
35
30
|
}
|
|
36
31
|
}
|
|
37
|
-
|
|
38
32
|
throw new TypeError('Not HexString compatible type!');
|
|
39
33
|
}
|
|
40
34
|
/**
|
|
@@ -42,16 +36,13 @@ export function makeHexString(input, len) {
|
|
|
42
36
|
*
|
|
43
37
|
* @param hex string input without 0x prefix!
|
|
44
38
|
*/
|
|
45
|
-
|
|
46
39
|
export function hexToBytes(hex) {
|
|
47
40
|
assertHexString(hex);
|
|
48
41
|
const bytes = makeBytes(hex.length / 2);
|
|
49
|
-
|
|
50
42
|
for (let i = 0; i < bytes.length; i++) {
|
|
51
43
|
const hexByte = hex.substr(i * 2, 2);
|
|
52
44
|
bytes[i] = parseInt(hexByte, 16);
|
|
53
45
|
}
|
|
54
|
-
|
|
55
46
|
return bytes;
|
|
56
47
|
}
|
|
57
48
|
/**
|
|
@@ -60,16 +51,13 @@ export function hexToBytes(hex) {
|
|
|
60
51
|
* @param bytes The input array
|
|
61
52
|
* @param len The length of the non prefixed HexString
|
|
62
53
|
*/
|
|
63
|
-
|
|
64
54
|
export function bytesToHex(bytes, len) {
|
|
65
55
|
const hexByte = n => n.toString(16).padStart(2, '0');
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
56
|
+
const hex = Array.from(bytes, hexByte).join('');
|
|
57
|
+
// TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
|
|
69
58
|
if (len && hex.length !== len) {
|
|
70
59
|
throw new TypeError(`Resulting HexString does not have expected length ${len}: ${hex}`);
|
|
71
60
|
}
|
|
72
|
-
|
|
73
61
|
return hex;
|
|
74
62
|
}
|
|
75
63
|
/**
|
|
@@ -80,17 +68,15 @@ export function bytesToHex(bytes, len) {
|
|
|
80
68
|
* @param int The positive integer to be converted
|
|
81
69
|
* @param len The length of the non prefixed HexString
|
|
82
70
|
*/
|
|
83
|
-
|
|
84
71
|
export function intToHex(int, len) {
|
|
85
72
|
if (!Number.isInteger(int)) throw new TypeError('the value provided is not integer');
|
|
86
73
|
if (int > Number.MAX_SAFE_INTEGER) throw new TypeError('the value provided exceeds safe integer');
|
|
87
74
|
if (int < 0) throw new TypeError('the value provided is a negative integer');
|
|
88
|
-
const hex = int.toString(16);
|
|
89
|
-
|
|
75
|
+
const hex = int.toString(16);
|
|
76
|
+
// TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
|
|
90
77
|
if (len && hex.length !== len) {
|
|
91
78
|
throw new TypeError(`Resulting HexString does not have expected length ${len}: ${hex}`);
|
|
92
79
|
}
|
|
93
|
-
|
|
94
80
|
return hex;
|
|
95
81
|
}
|
|
96
82
|
/**
|
|
@@ -102,7 +88,6 @@ export function intToHex(int, len) {
|
|
|
102
88
|
* @param s string input
|
|
103
89
|
* @param len expected length of the HexString
|
|
104
90
|
*/
|
|
105
|
-
|
|
106
91
|
export function isHexString(s, len) {
|
|
107
92
|
return typeof s === 'string' && /^[0-9a-f]+$/i.test(s) && (!len || s.length === len);
|
|
108
93
|
}
|
|
@@ -112,7 +97,6 @@ export function isHexString(s, len) {
|
|
|
112
97
|
*
|
|
113
98
|
* @param s string input
|
|
114
99
|
*/
|
|
115
|
-
|
|
116
100
|
export function isPrefixedHexString(s) {
|
|
117
101
|
return typeof s === 'string' && /^0x[0-9a-f]+$/i.test(s);
|
|
118
102
|
}
|
|
@@ -126,14 +110,12 @@ export function isPrefixedHexString(s) {
|
|
|
126
110
|
* @param name optional name for the asserted value
|
|
127
111
|
* @returns HexString or throws error
|
|
128
112
|
*/
|
|
129
|
-
|
|
130
113
|
export function assertHexString(s, len, name = 'value') {
|
|
131
114
|
if (!isHexString(s, len)) {
|
|
132
115
|
if (isPrefixedHexString(s)) {
|
|
133
116
|
throw new TypeError(`${name} not valid non prefixed hex string (has 0x prefix): ${s}`);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
|
|
117
|
+
}
|
|
118
|
+
// Don't display length error if no length specified in order not to confuse user
|
|
137
119
|
const lengthMsg = len ? ` of length ${len}` : '';
|
|
138
120
|
throw new TypeError(`${name} not valid hex string${lengthMsg}: ${s}`);
|
|
139
121
|
}
|
|
@@ -146,7 +128,6 @@ export function assertHexString(s, len, name = 'value') {
|
|
|
146
128
|
* @param name optional name for the asserted value
|
|
147
129
|
* @returns HexString or throws error
|
|
148
130
|
*/
|
|
149
|
-
|
|
150
131
|
export function assertPrefixedHexString(s, name = 'value') {
|
|
151
132
|
if (!isPrefixedHexString(s)) {
|
|
152
133
|
throw new TypeError(`${name} not valid prefixed hex string: ${s}`);
|
package/dist/mjs/utils/http.js
CHANGED
|
@@ -1,73 +1,25 @@
|
|
|
1
|
-
var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) {
|
|
3
|
-
return value instanceof P ? value : new P(function (resolve) {
|
|
4
|
-
resolve(value);
|
|
5
|
-
});
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
9
|
-
function fulfilled(value) {
|
|
10
|
-
try {
|
|
11
|
-
step(generator.next(value));
|
|
12
|
-
} catch (e) {
|
|
13
|
-
reject(e);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function rejected(value) {
|
|
18
|
-
try {
|
|
19
|
-
step(generator["throw"](value));
|
|
20
|
-
} catch (e) {
|
|
21
|
-
reject(e);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function step(result) {
|
|
26
|
-
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
30
|
-
});
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
var __rest = this && this.__rest || function (s, e) {
|
|
34
|
-
var t = {};
|
|
35
|
-
|
|
36
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
|
|
37
|
-
|
|
38
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
39
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
|
|
40
|
-
}
|
|
41
|
-
return t;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
1
|
import { BeeError, BeeNotAJsonError, BeeRequestError, BeeResponseError } from "./error.js";
|
|
45
|
-
import kyFactory from 'ky-universal';
|
|
46
2
|
import { normalizeToReadableStream } from "./stream.js";
|
|
47
|
-
import { deepMerge } from "./merge.js";
|
|
48
3
|
import { isObject, isStrictlyObject } from "./type.js";
|
|
49
|
-
|
|
4
|
+
import { deepMerge } from "./merge.js";
|
|
5
|
+
export const DEFAULT_KY_CONFIG = {
|
|
50
6
|
headers: {
|
|
51
7
|
accept: 'application/json, text/plain, */*',
|
|
52
8
|
'user-agent': `bee-js`
|
|
53
9
|
}
|
|
54
10
|
};
|
|
55
|
-
|
|
56
11
|
function isHttpError(e) {
|
|
57
12
|
return isObject(e) && typeof e.response !== 'undefined';
|
|
58
13
|
}
|
|
59
|
-
|
|
60
14
|
function isHttpRequestError(e) {
|
|
61
15
|
return isObject(e) && typeof e.request !== 'undefined';
|
|
62
16
|
}
|
|
63
|
-
|
|
64
17
|
function headersToObject(header) {
|
|
65
18
|
return [...header.entries()].reduce((obj, [key, val]) => {
|
|
66
19
|
obj[key] = val;
|
|
67
20
|
return obj;
|
|
68
21
|
}, {});
|
|
69
22
|
}
|
|
70
|
-
|
|
71
23
|
function wrapRequest(request) {
|
|
72
24
|
return {
|
|
73
25
|
url: request.url,
|
|
@@ -75,21 +27,20 @@ function wrapRequest(request) {
|
|
|
75
27
|
headers: headersToObject(request.headers)
|
|
76
28
|
};
|
|
77
29
|
}
|
|
78
|
-
|
|
79
30
|
export function wrapRequestClosure(cb) {
|
|
80
|
-
return request =>
|
|
81
|
-
|
|
82
|
-
}
|
|
31
|
+
return async request => {
|
|
32
|
+
await cb(wrapRequest(request));
|
|
33
|
+
};
|
|
83
34
|
}
|
|
84
35
|
export function wrapResponseClosure(cb) {
|
|
85
|
-
return (request, options, response) =>
|
|
86
|
-
|
|
36
|
+
return async (request, options, response) => {
|
|
37
|
+
await cb({
|
|
87
38
|
headers: headersToObject(response.headers),
|
|
88
39
|
status: response.status,
|
|
89
40
|
statusText: response.statusText,
|
|
90
41
|
request: wrapRequest(request)
|
|
91
42
|
});
|
|
92
|
-
}
|
|
43
|
+
};
|
|
93
44
|
}
|
|
94
45
|
/**
|
|
95
46
|
* Filters out entries that has undefined value from headers object.
|
|
@@ -98,111 +49,102 @@ export function wrapResponseClosure(cb) {
|
|
|
98
49
|
* @param obj
|
|
99
50
|
*/
|
|
100
51
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
101
|
-
|
|
102
52
|
export function filterHeaders(obj) {
|
|
103
53
|
if (obj === undefined) {
|
|
104
54
|
return undefined;
|
|
105
55
|
}
|
|
106
|
-
|
|
107
56
|
isStrictlyObject(obj);
|
|
108
57
|
const typedObj = obj;
|
|
109
|
-
|
|
110
58
|
for (const key in typedObj) {
|
|
111
59
|
if (typedObj[key] === undefined) {
|
|
112
60
|
delete typedObj[key];
|
|
113
61
|
}
|
|
114
62
|
}
|
|
115
|
-
|
|
116
63
|
if (Object.keys(typedObj).length === 0) {
|
|
117
64
|
return undefined;
|
|
118
65
|
}
|
|
119
|
-
|
|
120
66
|
return typedObj;
|
|
121
67
|
}
|
|
122
68
|
/**
|
|
123
69
|
* Main utility function to make HTTP requests.
|
|
124
|
-
* @param
|
|
70
|
+
* @param kyOptions
|
|
125
71
|
* @param config
|
|
126
72
|
*/
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
if (!response.body) {
|
|
144
|
-
throw new BeeError('Response was expected to get data but did not get any!');
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
response.data = normalizeToReadableStream(response.body);
|
|
148
|
-
break;
|
|
149
|
-
|
|
150
|
-
case 'arraybuffer':
|
|
151
|
-
response.data = yield response.arrayBuffer();
|
|
152
|
-
break;
|
|
153
|
-
|
|
154
|
-
case 'json':
|
|
155
|
-
try {
|
|
156
|
-
response.data = yield response.json();
|
|
157
|
-
} catch (e) {
|
|
158
|
-
throw new BeeNotAJsonError();
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
break;
|
|
162
|
-
|
|
163
|
-
default:
|
|
164
|
-
break;
|
|
165
|
-
// If responseType is not set, then no data are expected
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
return response;
|
|
169
|
-
} catch (e) {
|
|
170
|
-
// Passthrough thrown errors
|
|
171
|
-
if (e instanceof BeeNotAJsonError) {
|
|
172
|
-
throw e;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
if (isHttpError(e)) {
|
|
176
|
-
let message; // We store the response body here as it can be read only once in Response's lifecycle so to make it exposed
|
|
177
|
-
// to the user in the BeeResponseError, for further analysis.
|
|
178
|
-
|
|
179
|
-
const body = yield e.response.text();
|
|
180
|
-
|
|
181
|
-
try {
|
|
182
|
-
// The response can be Bee's JSON with structure `{code, message}` lets try to parse it
|
|
183
|
-
message = JSON.parse(body).message;
|
|
184
|
-
} catch (e) {}
|
|
185
|
-
|
|
186
|
-
if (message) {
|
|
187
|
-
throw new BeeResponseError(e.response.status, e.response, body, config, `${e.response.statusText}: ${message}`);
|
|
188
|
-
} else {
|
|
189
|
-
throw new BeeResponseError(e.response.status, e.response, body, config, e.response.statusText);
|
|
73
|
+
export async function http(kyOptions, config) {
|
|
74
|
+
try {
|
|
75
|
+
const ky = await getKy();
|
|
76
|
+
const {
|
|
77
|
+
path,
|
|
78
|
+
responseType,
|
|
79
|
+
...kyConfig
|
|
80
|
+
} = deepMerge(kyOptions, config);
|
|
81
|
+
const response = await ky(path, {
|
|
82
|
+
...kyConfig,
|
|
83
|
+
searchParams: filterHeaders(kyConfig.searchParams)
|
|
84
|
+
});
|
|
85
|
+
switch (responseType) {
|
|
86
|
+
case 'stream':
|
|
87
|
+
if (!response.body) {
|
|
88
|
+
throw new BeeError('Response was expected to get data but did not get any!');
|
|
190
89
|
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
90
|
+
response.parsedData = normalizeToReadableStream(response.body);
|
|
91
|
+
break;
|
|
92
|
+
case 'arraybuffer':
|
|
93
|
+
response.parsedData = await response.arrayBuffer();
|
|
94
|
+
break;
|
|
95
|
+
case 'json':
|
|
96
|
+
try {
|
|
97
|
+
response.parsedData = await response.json();
|
|
98
|
+
} catch (e) {
|
|
99
|
+
throw new BeeNotAJsonError();
|
|
199
100
|
}
|
|
101
|
+
break;
|
|
102
|
+
default:
|
|
103
|
+
break;
|
|
104
|
+
// If responseType is not set, then no data are expected
|
|
105
|
+
}
|
|
200
106
|
|
|
201
|
-
|
|
107
|
+
return response;
|
|
108
|
+
} catch (e) {
|
|
109
|
+
// Passthrough thrown errors
|
|
110
|
+
if (e instanceof BeeNotAJsonError) {
|
|
111
|
+
throw e;
|
|
112
|
+
}
|
|
113
|
+
if (isHttpError(e)) {
|
|
114
|
+
let message;
|
|
115
|
+
// We store the response body here as it can be read only once in Response's lifecycle so to make it exposed
|
|
116
|
+
// to the user in the BeeResponseError, for further analysis.
|
|
117
|
+
const body = await e.response.text();
|
|
118
|
+
try {
|
|
119
|
+
// The response can be Bee's JSON with structure `{code, message}` lets try to parse it
|
|
120
|
+
message = JSON.parse(body).message;
|
|
121
|
+
} catch (e) {}
|
|
122
|
+
if (message) {
|
|
123
|
+
throw new BeeResponseError(e.response.status, e.response, body, config, `${e.response.statusText}: ${message}`);
|
|
124
|
+
} else {
|
|
125
|
+
throw new BeeResponseError(e.response.status, e.response, body, config, e.response.statusText);
|
|
126
|
+
}
|
|
127
|
+
} else if (isHttpRequestError(e)) {
|
|
128
|
+
throw new BeeRequestError(e.message, config);
|
|
129
|
+
} else {
|
|
130
|
+
// Node 18 has native `fetch` implementation called Undici. Errors from this implementation have top level generic
|
|
131
|
+
// message "fetch failed" with the more specific error placed into `cause` property. Instead of "fetch failed" we
|
|
132
|
+
// expose the underlying problem.
|
|
133
|
+
if (e.cause) {
|
|
134
|
+
throw new BeeError(e.cause.message);
|
|
202
135
|
}
|
|
136
|
+
throw new BeeError(e.message);
|
|
203
137
|
}
|
|
204
|
-
}
|
|
138
|
+
}
|
|
205
139
|
}
|
|
206
|
-
|
|
207
|
-
|
|
140
|
+
let ky;
|
|
141
|
+
async function getKy() {
|
|
142
|
+
if (ky) {
|
|
143
|
+
return ky;
|
|
144
|
+
}
|
|
145
|
+
ky = (await import('ky-universal')).default;
|
|
146
|
+
if (!ky) {
|
|
147
|
+
throw new Error('Ky was not found while it should have been!');
|
|
148
|
+
}
|
|
149
|
+
return ky;
|
|
208
150
|
}
|
package/dist/mjs/utils/merge.js
CHANGED
|
@@ -6,17 +6,14 @@ import { isObject } from "./type.js";
|
|
|
6
6
|
* @licence MIT
|
|
7
7
|
* @param sources
|
|
8
8
|
*/
|
|
9
|
-
|
|
10
9
|
export function deepMerge(...sources) {
|
|
11
10
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
12
11
|
let returnValue = {};
|
|
13
|
-
|
|
14
12
|
for (const source of sources) {
|
|
15
13
|
if (Array.isArray(source)) {
|
|
16
14
|
if (!Array.isArray(returnValue)) {
|
|
17
15
|
returnValue = [];
|
|
18
16
|
}
|
|
19
|
-
|
|
20
17
|
returnValue = [...returnValue, ...source];
|
|
21
18
|
} else if (isObject(source)) {
|
|
22
19
|
// eslint-disable-next-line prefer-const
|
|
@@ -24,13 +21,12 @@ export function deepMerge(...sources) {
|
|
|
24
21
|
if (isObject(value) && key in returnValue) {
|
|
25
22
|
value = deepMerge(returnValue[key], value);
|
|
26
23
|
}
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
returnValue = {
|
|
25
|
+
...returnValue,
|
|
29
26
|
[key]: value
|
|
30
|
-
}
|
|
27
|
+
};
|
|
31
28
|
}
|
|
32
29
|
}
|
|
33
30
|
}
|
|
34
|
-
|
|
35
31
|
return returnValue;
|
|
36
32
|
}
|
package/dist/mjs/utils/pss.js
CHANGED
|
@@ -6,11 +6,9 @@ import { PSS_TARGET_HEX_LENGTH_MAX } from "../types/index.js";
|
|
|
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
|
-
|
|
10
9
|
export function makeMaxTarget(target) {
|
|
11
10
|
if (typeof target !== 'string') {
|
|
12
11
|
throw new TypeError('target has to be an string!');
|
|
13
12
|
}
|
|
14
|
-
|
|
15
13
|
return target.slice(0, PSS_TARGET_HEX_LENGTH_MAX);
|
|
16
14
|
}
|
|
@@ -6,7 +6,6 @@ export function makeBytesReference(reference, offset = 0) {
|
|
|
6
6
|
if (offset) {
|
|
7
7
|
throw new Error('Offset property can be set only for UintArray reference!');
|
|
8
8
|
}
|
|
9
|
-
|
|
10
9
|
try {
|
|
11
10
|
// Non-encrypted chunk hex string reference
|
|
12
11
|
const hexReference = makeHexString(reference, REFERENCE_HEX_LENGTH);
|
|
@@ -14,9 +13,8 @@ export function makeBytesReference(reference, offset = 0) {
|
|
|
14
13
|
} catch (e) {
|
|
15
14
|
if (!(e instanceof TypeError)) {
|
|
16
15
|
throw e;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
}
|
|
17
|
+
// Encrypted chunk hex string reference
|
|
20
18
|
const hexReference = makeHexString(reference, ENCRYPTED_REFERENCE_HEX_LENGTH);
|
|
21
19
|
return hexToBytes(hexReference);
|
|
22
20
|
}
|
|
@@ -27,6 +25,5 @@ export function makeBytesReference(reference, offset = 0) {
|
|
|
27
25
|
return bytesAtOffset(reference, offset, REFERENCE_BYTES_LENGTH);
|
|
28
26
|
}
|
|
29
27
|
}
|
|
30
|
-
|
|
31
28
|
throw new TypeError('invalid chunk reference');
|
|
32
29
|
}
|
package/dist/mjs/utils/sleep.js
CHANGED
|
@@ -1,43 +1,8 @@
|
|
|
1
|
-
var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) {
|
|
3
|
-
return value instanceof P ? value : new P(function (resolve) {
|
|
4
|
-
resolve(value);
|
|
5
|
-
});
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
9
|
-
function fulfilled(value) {
|
|
10
|
-
try {
|
|
11
|
-
step(generator.next(value));
|
|
12
|
-
} catch (e) {
|
|
13
|
-
reject(e);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function rejected(value) {
|
|
18
|
-
try {
|
|
19
|
-
step(generator["throw"](value));
|
|
20
|
-
} catch (e) {
|
|
21
|
-
reject(e);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function step(result) {
|
|
26
|
-
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
30
|
-
});
|
|
31
|
-
};
|
|
32
1
|
/**
|
|
33
2
|
* Sleep for N miliseconds
|
|
34
3
|
*
|
|
35
4
|
* @param ms Number of miliseconds to sleep
|
|
36
5
|
*/
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
export function sleep(ms) {
|
|
40
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
41
|
-
return new Promise(resolve => setTimeout(() => resolve(), ms));
|
|
42
|
-
});
|
|
6
|
+
export async function sleep(ms) {
|
|
7
|
+
return new Promise(resolve => setTimeout(() => resolve(), ms));
|
|
43
8
|
}
|
package/dist/mjs/utils/stream.js
CHANGED
|
@@ -8,7 +8,6 @@ const NodeReadable = NodeReadableNative || class {};
|
|
|
8
8
|
*
|
|
9
9
|
* @param entry
|
|
10
10
|
*/
|
|
11
|
-
|
|
12
11
|
export function isReadable(entry) {
|
|
13
12
|
return isReadableStream(entry) || isNodeReadable(entry);
|
|
14
13
|
}
|
|
@@ -16,26 +15,20 @@ export function isReadableStream(entry) {
|
|
|
16
15
|
if (!isStrictlyObject(entry)) {
|
|
17
16
|
return false;
|
|
18
17
|
}
|
|
19
|
-
|
|
20
18
|
const browserReadable = entry;
|
|
21
|
-
|
|
22
19
|
if (typeof browserReadable.getReader === 'function' && browserReadable.locked !== undefined && typeof browserReadable.cancel === 'function' && typeof browserReadable.pipeTo === 'function' && typeof browserReadable.pipeThrough === 'function') {
|
|
23
20
|
return true;
|
|
24
21
|
}
|
|
25
|
-
|
|
26
22
|
return false;
|
|
27
23
|
}
|
|
28
24
|
export function isNodeReadable(entry) {
|
|
29
25
|
if (!isStrictlyObject(entry)) {
|
|
30
26
|
return false;
|
|
31
27
|
}
|
|
32
|
-
|
|
33
28
|
const nodeReadable = entry;
|
|
34
|
-
|
|
35
29
|
if (typeof nodeReadable.pipe === 'function' && nodeReadable.readable && typeof nodeReadable._read === 'function') {
|
|
36
30
|
return true;
|
|
37
31
|
}
|
|
38
|
-
|
|
39
32
|
return false;
|
|
40
33
|
}
|
|
41
34
|
/**
|
|
@@ -48,7 +41,6 @@ export function isNodeReadable(entry) {
|
|
|
48
41
|
* @licence Apache License 2.0 https://github.com/gwicke/node-web-streams/blob/master/LICENSE
|
|
49
42
|
* @param nodeStream
|
|
50
43
|
*/
|
|
51
|
-
|
|
52
44
|
export function readableNodeToWeb(nodeStream) {
|
|
53
45
|
return new ReadableStream({
|
|
54
46
|
start(controller) {
|
|
@@ -59,21 +51,17 @@ export function readableNodeToWeb(nodeStream) {
|
|
|
59
51
|
} else {
|
|
60
52
|
controller.enqueue(chunk);
|
|
61
53
|
}
|
|
62
|
-
|
|
63
54
|
nodeStream.pause();
|
|
64
55
|
});
|
|
65
56
|
nodeStream.on('end', () => controller.close());
|
|
66
57
|
nodeStream.on('error', e => controller.error(e));
|
|
67
58
|
},
|
|
68
|
-
|
|
69
59
|
pull() {
|
|
70
60
|
nodeStream.resume();
|
|
71
61
|
},
|
|
72
|
-
|
|
73
62
|
cancel() {
|
|
74
63
|
nodeStream.pause();
|
|
75
64
|
}
|
|
76
|
-
|
|
77
65
|
});
|
|
78
66
|
}
|
|
79
67
|
/**
|
|
@@ -83,7 +71,6 @@ export function readableNodeToWeb(nodeStream) {
|
|
|
83
71
|
* @author https://github.com/gwicke
|
|
84
72
|
* @licence Apache License 2.0 https://github.com/gwicke/node-web-streams/blob/master/LICENSE
|
|
85
73
|
*/
|
|
86
|
-
|
|
87
74
|
class NodeReadableWrapper extends NodeReadable {
|
|
88
75
|
constructor(webStream, options) {
|
|
89
76
|
super(options);
|
|
@@ -91,21 +78,17 @@ class NodeReadableWrapper extends NodeReadable {
|
|
|
91
78
|
this._reader = webStream.getReader();
|
|
92
79
|
this._reading = false;
|
|
93
80
|
}
|
|
94
|
-
|
|
95
81
|
_read() {
|
|
96
82
|
if (this._reading) {
|
|
97
83
|
return;
|
|
98
84
|
}
|
|
99
|
-
|
|
100
85
|
this._reading = true;
|
|
101
|
-
|
|
102
86
|
const doRead = () => {
|
|
103
87
|
this._reader.read().then(res => {
|
|
104
88
|
if (res.done) {
|
|
105
89
|
this.push(null);
|
|
106
90
|
return;
|
|
107
91
|
}
|
|
108
|
-
|
|
109
92
|
if (this.push(res.value)) {
|
|
110
93
|
return doRead();
|
|
111
94
|
} else {
|
|
@@ -113,10 +96,8 @@ class NodeReadableWrapper extends NodeReadable {
|
|
|
113
96
|
}
|
|
114
97
|
});
|
|
115
98
|
};
|
|
116
|
-
|
|
117
99
|
doRead();
|
|
118
100
|
}
|
|
119
|
-
|
|
120
101
|
}
|
|
121
102
|
/**
|
|
122
103
|
* Function that converts WHATWG ReadableStream into Node's Readable
|
|
@@ -132,13 +113,10 @@ class NodeReadableWrapper extends NodeReadable {
|
|
|
132
113
|
* @param webStream
|
|
133
114
|
* @param options
|
|
134
115
|
*/
|
|
135
|
-
|
|
136
|
-
|
|
137
116
|
export function readableWebToNode(webStream, options) {
|
|
138
117
|
if (!NodeReadableNative) {
|
|
139
118
|
throw new Error("The Node's Readable is not available! If you are running this in browser you have to polyfill 'stream' package!");
|
|
140
119
|
}
|
|
141
|
-
|
|
142
120
|
return new NodeReadableWrapper(webStream, options);
|
|
143
121
|
}
|
|
144
122
|
export function normalizeToReadableStream(stream) {
|
|
@@ -147,6 +125,5 @@ export function normalizeToReadableStream(stream) {
|
|
|
147
125
|
} else if (isReadableStream(stream)) {
|
|
148
126
|
return stream;
|
|
149
127
|
}
|
|
150
|
-
|
|
151
128
|
throw new TypeError('Passed stream is not Node Readable nor ReadableStream!');
|
|
152
129
|
}
|
package/dist/mjs/utils/tar.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import Tar from 'tar-js';
|
|
1
|
+
import Tar from 'tar-js';
|
|
2
|
+
// converts a string to utf8 Uint8Array and returns it as a string-like
|
|
2
3
|
// object that `tar.append` accepts as path
|
|
3
|
-
|
|
4
4
|
function fixUnicodePath(path) {
|
|
5
5
|
const codes = new TextEncoder().encode(path);
|
|
6
6
|
return {
|
|
@@ -8,14 +8,11 @@ function fixUnicodePath(path) {
|
|
|
8
8
|
charCodeAt: index => codes[index]
|
|
9
9
|
};
|
|
10
10
|
}
|
|
11
|
-
|
|
12
11
|
export function makeTar(data) {
|
|
13
12
|
const tar = new Tar();
|
|
14
|
-
|
|
15
13
|
for (const entry of data) {
|
|
16
14
|
const path = fixUnicodePath(entry.path);
|
|
17
15
|
tar.append(path, entry.data);
|
|
18
16
|
}
|
|
19
|
-
|
|
20
17
|
return tar.out;
|
|
21
18
|
}
|