@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.
- package/dist/cjs/bee-debug.js +615 -0
- package/dist/cjs/bee.js +922 -0
- package/dist/cjs/chunk/bmt.js +55 -0
- package/dist/cjs/chunk/cac.js +56 -0
- package/dist/cjs/chunk/serialize.js +19 -0
- package/dist/cjs/chunk/signer.js +137 -0
- package/dist/cjs/chunk/soc.js +172 -0
- package/dist/cjs/chunk/span.js +29 -0
- package/dist/cjs/feed/index.js +184 -0
- package/dist/cjs/feed/json.js +41 -0
- package/dist/cjs/feed/topic.js +25 -0
- package/dist/cjs/feed/type.js +15 -0
- package/dist/cjs/index.js +35 -0
- package/dist/cjs/modules/bytes.js +74 -0
- package/dist/cjs/modules/bzz.js +131 -0
- package/dist/cjs/modules/chunk.js +58 -0
- package/dist/cjs/modules/debug/balance.js +77 -0
- package/dist/cjs/modules/debug/chequebook.js +167 -0
- package/dist/cjs/modules/debug/chunk.js +51 -0
- package/dist/cjs/modules/debug/connectivity.js +75 -0
- package/dist/cjs/modules/debug/settlements.js +45 -0
- package/dist/cjs/modules/debug/stamps.js +89 -0
- package/dist/cjs/modules/debug/states.js +47 -0
- package/dist/cjs/modules/debug/status.js +153 -0
- package/dist/cjs/modules/debug/tag.js +30 -0
- package/dist/cjs/modules/debug/transactions.js +81 -0
- package/dist/cjs/modules/feed.js +76 -0
- package/dist/cjs/modules/pinning.js +84 -0
- package/dist/cjs/modules/pss.js +55 -0
- package/dist/cjs/modules/soc.js +40 -0
- package/dist/cjs/modules/status.js +26 -0
- package/dist/cjs/modules/stewardship.js +41 -0
- package/dist/cjs/modules/tag.js +96 -0
- package/dist/cjs/package.json +8 -0
- package/dist/cjs/types/debug.js +9 -0
- package/dist/cjs/types/index.js +46 -0
- package/dist/cjs/types/ky-options.js +8 -0
- package/dist/cjs/types/ky-universal/common.js +8 -0
- package/dist/cjs/types/ky-universal/hooks.js +8 -0
- package/dist/cjs/types/ky-universal/retry.js +8 -0
- package/dist/cjs/utils/bytes.js +107 -0
- package/dist/cjs/utils/collection.browser.js +36 -0
- package/dist/cjs/utils/collection.js +70 -0
- package/dist/cjs/utils/collection.node.js +115 -0
- package/dist/cjs/utils/data.browser.js +78 -0
- package/dist/cjs/utils/data.js +60 -0
- package/dist/cjs/utils/error.js +50 -0
- package/dist/cjs/utils/eth.js +211 -0
- package/dist/cjs/utils/expose.js +44 -0
- package/dist/cjs/utils/file.js +49 -0
- package/dist/cjs/utils/hash.js +21 -0
- package/dist/cjs/utils/headers.js +59 -0
- package/dist/cjs/utils/hex.js +150 -0
- package/dist/cjs/utils/http.js +166 -0
- package/dist/cjs/utils/merge.js +34 -0
- package/dist/cjs/utils/pss.js +18 -0
- package/dist/cjs/utils/stamps.js +17 -0
- package/dist/cjs/utils/stream.js +146 -0
- package/dist/cjs/utils/tar.js +25 -0
- package/dist/cjs/utils/type.js +327 -0
- package/dist/cjs/utils/uint64.js +29 -0
- package/dist/cjs/utils/url.js +56 -0
- package/dist/index.browser.min.js +1 -1
- package/dist/index.browser.min.js.LICENSE.txt +12 -11
- package/dist/index.browser.min.js.map +1 -1
- package/dist/mjs/bee-debug.js +609 -0
- package/dist/mjs/bee.js +944 -0
- package/dist/mjs/chunk/bmt.js +56 -0
- package/dist/mjs/chunk/cac.js +52 -0
- package/dist/mjs/chunk/serialize.js +15 -0
- package/dist/mjs/chunk/signer.js +131 -0
- package/dist/mjs/chunk/soc.js +139 -0
- package/dist/mjs/chunk/span.js +28 -0
- package/dist/mjs/feed/index.js +145 -0
- package/dist/mjs/feed/json.js +27 -0
- package/dist/mjs/feed/topic.js +21 -0
- package/dist/mjs/feed/type.js +10 -0
- package/dist/mjs/index.js +7 -0
- package/dist/mjs/modules/bytes.js +59 -0
- package/dist/mjs/modules/bzz.js +122 -0
- package/dist/mjs/modules/chunk.js +45 -0
- package/dist/mjs/modules/debug/balance.js +57 -0
- package/dist/mjs/modules/debug/chequebook.js +150 -0
- package/dist/mjs/modules/debug/chunk.js +35 -0
- package/dist/mjs/modules/debug/connectivity.js +45 -0
- package/dist/mjs/modules/debug/settlements.js +29 -0
- package/dist/mjs/modules/debug/stamps.js +64 -0
- package/dist/mjs/modules/debug/states.js +31 -0
- package/dist/mjs/modules/debug/status.js +134 -0
- package/dist/mjs/modules/debug/tag.js +16 -0
- package/dist/mjs/modules/debug/transactions.js +63 -0
- package/dist/mjs/modules/feed.js +67 -0
- package/dist/mjs/modules/pinning.js +66 -0
- package/dist/mjs/modules/pss.js +40 -0
- package/dist/mjs/modules/soc.js +31 -0
- package/dist/mjs/modules/status.js +12 -0
- package/dist/mjs/modules/stewardship.js +24 -0
- package/dist/mjs/modules/tag.js +77 -0
- package/dist/mjs/package.json +8 -0
- package/dist/mjs/types/debug.js +7 -0
- package/dist/mjs/types/index.js +37 -0
- package/dist/mjs/types/ky-options.js +7 -0
- package/dist/mjs/types/ky-universal/common.js +7 -0
- package/dist/mjs/types/ky-universal/hooks.js +7 -0
- package/dist/mjs/types/ky-universal/retry.js +7 -0
- package/dist/mjs/utils/bytes.js +101 -0
- package/dist/mjs/utils/collection.browser.js +19 -0
- package/dist/mjs/utils/collection.js +64 -0
- package/dist/mjs/utils/collection.node.js +74 -0
- package/dist/mjs/utils/data.browser.js +73 -0
- package/dist/mjs/utils/data.js +43 -0
- package/dist/mjs/utils/error.js +56 -0
- package/dist/mjs/utils/eth.js +199 -0
- package/dist/mjs/utils/expose.js +9 -0
- package/dist/mjs/utils/file.js +36 -0
- package/dist/mjs/utils/hash.js +17 -0
- package/dist/mjs/utils/headers.js +58 -0
- package/dist/mjs/utils/hex.js +154 -0
- package/dist/mjs/utils/http.js +155 -0
- package/dist/mjs/utils/merge.js +36 -0
- package/dist/mjs/utils/pss.js +16 -0
- package/dist/mjs/utils/stamps.js +17 -0
- package/dist/mjs/utils/stream.js +156 -0
- package/dist/mjs/utils/tar.js +21 -0
- package/dist/mjs/utils/type.js +336 -0
- package/dist/mjs/utils/uint64.js +23 -0
- package/dist/mjs/utils/url.js +57 -0
- package/dist/{src → types}/bee-debug.d.ts +47 -1
- package/dist/{src → types}/bee.d.ts +0 -0
- package/dist/{src → types}/chunk/bmt.d.ts +0 -0
- package/dist/{src → types}/chunk/cac.d.ts +0 -0
- package/dist/{src → types}/chunk/serialize.d.ts +0 -0
- package/dist/{src → types}/chunk/signer.d.ts +0 -0
- package/dist/{src → types}/chunk/soc.d.ts +0 -0
- package/dist/{src → types}/chunk/span.d.ts +0 -0
- package/dist/{src → types}/feed/index.d.ts +0 -0
- package/dist/{src → types}/feed/json.d.ts +0 -0
- package/dist/{src → types}/feed/topic.d.ts +0 -0
- package/dist/{src → types}/feed/type.d.ts +0 -0
- package/dist/{src → types}/index.d.ts +0 -0
- package/dist/{src → types}/modules/bytes.d.ts +0 -0
- package/dist/{src → types}/modules/bzz.d.ts +0 -0
- package/dist/{src → types}/modules/chunk.d.ts +0 -0
- package/dist/{src → types}/modules/debug/balance.d.ts +0 -0
- package/dist/{src → types}/modules/debug/chequebook.d.ts +0 -0
- package/dist/{src → types}/modules/debug/chunk.d.ts +0 -0
- package/dist/{src → types}/modules/debug/connectivity.d.ts +0 -0
- package/dist/{src → types}/modules/debug/settlements.d.ts +0 -0
- package/dist/{src → types}/modules/debug/stamps.d.ts +0 -0
- package/dist/{src → types}/modules/debug/states.d.ts +0 -0
- package/dist/types/modules/debug/status.d.ts +72 -0
- package/dist/{src → types}/modules/debug/tag.d.ts +0 -0
- package/dist/{src → types}/modules/debug/transactions.d.ts +0 -0
- package/dist/{src → types}/modules/feed.d.ts +0 -0
- package/dist/{src → types}/modules/pinning.d.ts +0 -0
- package/dist/{src → types}/modules/pss.d.ts +0 -0
- package/dist/{src → types}/modules/soc.d.ts +0 -0
- package/dist/{src → types}/modules/status.d.ts +0 -0
- package/dist/{src → types}/modules/stewardship.d.ts +0 -0
- package/dist/{src → types}/modules/tag.d.ts +0 -0
- package/dist/{src → types}/types/debug.d.ts +8 -0
- package/dist/{src → types}/types/index.d.ts +3 -2
- package/dist/types/types/ky-options.d.ts +221 -0
- package/dist/types/types/ky-universal/common.d.ts +13 -0
- package/dist/types/types/ky-universal/hooks.d.ts +92 -0
- package/dist/types/types/ky-universal/retry.d.ts +38 -0
- package/dist/{src → types}/utils/bytes.d.ts +0 -0
- package/dist/types/utils/collection.browser.d.ts +15 -0
- package/dist/{src → types}/utils/collection.d.ts +0 -14
- package/dist/types/utils/collection.node.d.ts +15 -0
- package/dist/{src → types}/utils/data.browser.d.ts +0 -0
- package/dist/{src → types}/utils/data.d.ts +0 -0
- package/dist/{src → types}/utils/error.d.ts +0 -0
- package/dist/{src → types}/utils/eth.d.ts +0 -0
- package/dist/{src → types}/utils/expose.d.ts +2 -1
- package/dist/{src → types}/utils/file.d.ts +0 -0
- package/dist/{src → types}/utils/hash.d.ts +0 -0
- package/dist/{src → types}/utils/headers.d.ts +0 -0
- package/dist/{src → types}/utils/hex.d.ts +0 -0
- package/dist/{src → types}/utils/http.d.ts +0 -0
- package/dist/{src → types}/utils/merge.d.ts +0 -0
- package/dist/{src → types}/utils/pss.d.ts +0 -0
- package/dist/{src → types}/utils/stamps.d.ts +0 -0
- package/dist/{src → types}/utils/stream.d.ts +11 -8
- package/dist/{src → types}/utils/tar.d.ts +0 -0
- package/dist/{src → types}/utils/type.d.ts +0 -0
- package/dist/{src → types}/utils/uint64.d.ts +0 -0
- package/dist/{src → types}/utils/url.d.ts +0 -0
- package/package.json +55 -36
- package/dist/index.min.js +0 -3
- package/dist/index.min.js.LICENSE.txt +0 -50
- package/dist/index.min.js.map +0 -1
- package/dist/src/modules/debug/status.d.ts +0 -24
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { BeeError } from "./error.js";
|
|
2
|
+
/**
|
|
3
|
+
* Read the filename from the content-disposition header
|
|
4
|
+
* See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
|
|
5
|
+
*
|
|
6
|
+
* @param header the content-disposition header value
|
|
7
|
+
*
|
|
8
|
+
* @returns the filename
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
function readContentDispositionFilename(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');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function readTagUid(header) {
|
|
28
|
+
if (!header) {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return parseInt(header, 10);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function readFileHeaders(headers) {
|
|
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
|
+
};
|
|
44
|
+
}
|
|
45
|
+
export function extractUploadHeaders(postageBatchId, options) {
|
|
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
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { makeBytes } from "./bytes.js";
|
|
2
|
+
/**
|
|
3
|
+
* Creates unprefixed hex string from wide range of data.
|
|
4
|
+
*
|
|
5
|
+
* TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
|
|
6
|
+
*
|
|
7
|
+
* @param input
|
|
8
|
+
* @param len of the resulting HexString WITHOUT prefix!
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export function makeHexString(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;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
throw new TypeError('Not HexString compatible type!');
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Converts a hex string to Uint8Array
|
|
42
|
+
*
|
|
43
|
+
* @param hex string input without 0x prefix!
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
export function hexToBytes(hex) {
|
|
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;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Converts array of number or Uint8Array to HexString without prefix.
|
|
59
|
+
*
|
|
60
|
+
* @param bytes The input array
|
|
61
|
+
* @param len The length of the non prefixed HexString
|
|
62
|
+
*/
|
|
63
|
+
|
|
64
|
+
export function bytesToHex(bytes, len) {
|
|
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;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Converts integer number to hex string.
|
|
77
|
+
*
|
|
78
|
+
* Optionally provides '0x' prefix or padding
|
|
79
|
+
*
|
|
80
|
+
* @param int The positive integer to be converted
|
|
81
|
+
* @param len The length of the non prefixed HexString
|
|
82
|
+
*/
|
|
83
|
+
|
|
84
|
+
export function intToHex(int, len) {
|
|
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;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Type guard for HexStrings.
|
|
98
|
+
* Requires no 0x prefix!
|
|
99
|
+
*
|
|
100
|
+
* TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
|
|
101
|
+
*
|
|
102
|
+
* @param s string input
|
|
103
|
+
* @param len expected length of the HexString
|
|
104
|
+
*/
|
|
105
|
+
|
|
106
|
+
export function isHexString(s, len) {
|
|
107
|
+
return typeof s === 'string' && /^[0-9a-f]+$/i.test(s) && (!len || s.length === len);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Type guard for PrefixedHexStrings.
|
|
111
|
+
* Does enforce presence of 0x prefix!
|
|
112
|
+
*
|
|
113
|
+
* @param s string input
|
|
114
|
+
*/
|
|
115
|
+
|
|
116
|
+
export function isPrefixedHexString(s) {
|
|
117
|
+
return typeof s === 'string' && /^0x[0-9a-f]+$/i.test(s);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Verifies if the provided input is a HexString.
|
|
121
|
+
*
|
|
122
|
+
* TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
|
|
123
|
+
*
|
|
124
|
+
* @param s string input
|
|
125
|
+
* @param len expected length of the HexString
|
|
126
|
+
* @param name optional name for the asserted value
|
|
127
|
+
* @returns HexString or throws error
|
|
128
|
+
*/
|
|
129
|
+
|
|
130
|
+
export function assertHexString(s, len, name = 'value') {
|
|
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
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Verifies if the provided input is a PrefixedHexString.
|
|
143
|
+
*
|
|
144
|
+
* @param s string input
|
|
145
|
+
* @param len expected length of the HexString
|
|
146
|
+
* @param name optional name for the asserted value
|
|
147
|
+
* @returns HexString or throws error
|
|
148
|
+
*/
|
|
149
|
+
|
|
150
|
+
export function assertPrefixedHexString(s, name = 'value') {
|
|
151
|
+
if (!isPrefixedHexString(s)) {
|
|
152
|
+
throw new TypeError(`${name} not valid prefixed hex string: ${s}`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { BeeError, BeeNotAJsonError, BeeRequestError, BeeResponseError } from "./error.js";
|
|
2
|
+
import kyFactory from 'ky-universal';
|
|
3
|
+
import { normalizeToReadableStream } from "./stream.js";
|
|
4
|
+
import { deepMerge } from "./merge.js";
|
|
5
|
+
import { isObject, isStrictlyObject } from "./type.js";
|
|
6
|
+
const DEFAULT_KY_CONFIG = {
|
|
7
|
+
headers: {
|
|
8
|
+
accept: 'application/json, text/plain, */*',
|
|
9
|
+
'user-agent': `bee-js`
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
function isHttpError(e) {
|
|
14
|
+
return isObject(e) && typeof e.response !== 'undefined';
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function isHttpRequestError(e) {
|
|
18
|
+
return isObject(e) && typeof e.request !== 'undefined';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function headersToObject(header) {
|
|
22
|
+
return [...header.entries()].reduce((obj, [key, val]) => {
|
|
23
|
+
obj[key] = val;
|
|
24
|
+
return obj;
|
|
25
|
+
}, {});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function wrapRequest(request) {
|
|
29
|
+
return {
|
|
30
|
+
url: request.url,
|
|
31
|
+
method: request.method.toUpperCase(),
|
|
32
|
+
headers: headersToObject(request.headers)
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function wrapRequestClosure(cb) {
|
|
37
|
+
return async request => {
|
|
38
|
+
await cb(wrapRequest(request));
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
export function wrapResponseClosure(cb) {
|
|
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
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Filters out entries that has undefined value from headers object.
|
|
53
|
+
* Modifies the original object!
|
|
54
|
+
*
|
|
55
|
+
* @param obj
|
|
56
|
+
*/
|
|
57
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
58
|
+
|
|
59
|
+
export function filterHeaders(obj) {
|
|
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];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (Object.keys(typedObj).length === 0) {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return typedObj;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Main utility function to make HTTP requests.
|
|
81
|
+
* @param ky
|
|
82
|
+
* @param config
|
|
83
|
+
*/
|
|
84
|
+
|
|
85
|
+
export async function http(ky, config) {
|
|
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!');
|
|
100
|
+
}
|
|
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();
|
|
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;
|
|
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
|
+
}
|
|
152
|
+
}
|
|
153
|
+
export function makeDefaultKy(kyConfig) {
|
|
154
|
+
return kyFactory.create(deepMerge(DEFAULT_KY_CONFIG, kyConfig));
|
|
155
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { isObject } from "./type.js";
|
|
2
|
+
/**
|
|
3
|
+
* Function that deep merges objects
|
|
4
|
+
*
|
|
5
|
+
* @copyright https://github.com/sindresorhus/ky/blob/b3c9e88fa49d50150dbb6e6b771b4af56cb40c98/source/utils/merge.ts
|
|
6
|
+
* @licence MIT
|
|
7
|
+
* @param sources
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export function deepMerge(...sources) {
|
|
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);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
returnValue = { ...returnValue,
|
|
29
|
+
[key]: value
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return returnValue;
|
|
36
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { PSS_TARGET_HEX_LENGTH_MAX } from "../types/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Utility function that for given strings/reference takes the most specific
|
|
4
|
+
* target that Bee node will except.
|
|
5
|
+
*
|
|
6
|
+
* @param target is a non-prefixed hex string Bee address
|
|
7
|
+
* @see [Bee docs - PSS](https://docs.ethswarm.org/docs/dapps-on-swarm/pss)
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export function makeMaxTarget(target) {
|
|
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
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility function that calculates usage of postage batch based on its utilization, depth and bucket depth.
|
|
3
|
+
*
|
|
4
|
+
* Be aware for small depths (17, 18) this does not provide that much information as the provided set of distinct values
|
|
5
|
+
* is small.
|
|
6
|
+
*
|
|
7
|
+
* @param utilization
|
|
8
|
+
* @param depth
|
|
9
|
+
* @param bucketDepth
|
|
10
|
+
*/
|
|
11
|
+
export function getStampUsage({
|
|
12
|
+
utilization,
|
|
13
|
+
depth,
|
|
14
|
+
bucketDepth
|
|
15
|
+
}) {
|
|
16
|
+
return utilization / Math.pow(2, depth - bucketDepth);
|
|
17
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { Readable as NodeReadableNative } from 'stream';
|
|
2
|
+
import { isStrictlyObject } from "./type.js";
|
|
3
|
+
import { ReadableStream } from 'web-streams-polyfill';
|
|
4
|
+
const NodeReadable = NodeReadableNative || class {};
|
|
5
|
+
/**
|
|
6
|
+
* Validates if passed object is either browser's ReadableStream
|
|
7
|
+
* or Node's Readable.
|
|
8
|
+
*
|
|
9
|
+
* @param entry
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export function isReadable(entry) {
|
|
13
|
+
return isReadableStream(entry) || isNodeReadable(entry);
|
|
14
|
+
}
|
|
15
|
+
export function isReadableStream(entry) {
|
|
16
|
+
if (!isStrictlyObject(entry)) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const browserReadable = entry;
|
|
21
|
+
|
|
22
|
+
if (typeof browserReadable.getReader === 'function' && browserReadable.locked !== undefined && typeof browserReadable.cancel === 'function' && typeof browserReadable.pipeTo === 'function' && typeof browserReadable.pipeThrough === 'function') {
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
export function isNodeReadable(entry) {
|
|
29
|
+
if (!isStrictlyObject(entry)) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const nodeReadable = entry;
|
|
34
|
+
|
|
35
|
+
if (typeof nodeReadable.pipe === 'function' && nodeReadable.readable && typeof nodeReadable._read === 'function') {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Function that converts Node's Readable into WHATWG ReadableStream
|
|
43
|
+
*
|
|
44
|
+
* Taken over from https://github.com/gwicke/node-web-streams/blob/master/lib/conversions.js
|
|
45
|
+
* Because it uses forked web-streams-polyfill that are outdated.
|
|
46
|
+
*
|
|
47
|
+
* @author https://github.com/gwicke
|
|
48
|
+
* @licence Apache License 2.0 https://github.com/gwicke/node-web-streams/blob/master/LICENSE
|
|
49
|
+
* @param nodeStream
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
export function readableNodeToWeb(nodeStream) {
|
|
53
|
+
return new ReadableStream({
|
|
54
|
+
start(controller) {
|
|
55
|
+
nodeStream.pause();
|
|
56
|
+
nodeStream.on('data', chunk => {
|
|
57
|
+
if (Buffer.isBuffer(chunk)) {
|
|
58
|
+
controller.enqueue(new Uint8Array(chunk.buffer));
|
|
59
|
+
} else {
|
|
60
|
+
controller.enqueue(chunk);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
nodeStream.pause();
|
|
64
|
+
});
|
|
65
|
+
nodeStream.on('end', () => controller.close());
|
|
66
|
+
nodeStream.on('error', e => controller.error(e));
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
pull() {
|
|
70
|
+
nodeStream.resume();
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
cancel() {
|
|
74
|
+
nodeStream.pause();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Taken over from https://github.com/gwicke/node-web-streams/blob/master/lib/conversions.js
|
|
81
|
+
* Because it uses forked web-streams-polyfill that are outdated.
|
|
82
|
+
*
|
|
83
|
+
* @author https://github.com/gwicke
|
|
84
|
+
* @licence Apache License 2.0 https://github.com/gwicke/node-web-streams/blob/master/LICENSE
|
|
85
|
+
*/
|
|
86
|
+
|
|
87
|
+
class NodeReadableWrapper extends NodeReadable {
|
|
88
|
+
_webStream;
|
|
89
|
+
_reader;
|
|
90
|
+
_reading;
|
|
91
|
+
|
|
92
|
+
constructor(webStream, options) {
|
|
93
|
+
super(options);
|
|
94
|
+
this._webStream = webStream;
|
|
95
|
+
this._reader = webStream.getReader();
|
|
96
|
+
this._reading = false;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
_read() {
|
|
100
|
+
if (this._reading) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
this._reading = true;
|
|
105
|
+
|
|
106
|
+
const doRead = () => {
|
|
107
|
+
this._reader.read().then(res => {
|
|
108
|
+
if (res.done) {
|
|
109
|
+
this.push(null);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (this.push(res.value)) {
|
|
114
|
+
return doRead();
|
|
115
|
+
} else {
|
|
116
|
+
this._reading = false;
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
doRead();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Function that converts WHATWG ReadableStream into Node's Readable
|
|
127
|
+
*
|
|
128
|
+
* Taken over from https://github.com/gwicke/node-web-streams/blob/master/lib/conversions.js
|
|
129
|
+
* Because it uses forked web-streams-polyfill that is outdated.
|
|
130
|
+
*
|
|
131
|
+
* **Warning!**
|
|
132
|
+
* If you want to use this function in browser you have to polyfill `stream` package with your bundler.
|
|
133
|
+
*
|
|
134
|
+
* @author https://github.com/gwicke
|
|
135
|
+
* @licence Apache License 2.0 https://github.com/gwicke/node-web-streams/blob/master/LICENSE
|
|
136
|
+
* @param webStream
|
|
137
|
+
* @param options
|
|
138
|
+
*/
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
export function readableWebToNode(webStream, options) {
|
|
142
|
+
if (!NodeReadableNative) {
|
|
143
|
+
throw new Error("The Node's Readable is not available! If you are running this in browser you have to polyfill 'stream' package!");
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return new NodeReadableWrapper(webStream, options);
|
|
147
|
+
}
|
|
148
|
+
export function normalizeToReadableStream(stream) {
|
|
149
|
+
if (isNodeReadable(stream)) {
|
|
150
|
+
return readableNodeToWeb(stream);
|
|
151
|
+
} else if (isReadableStream(stream)) {
|
|
152
|
+
return stream;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
throw new TypeError('Passed stream is not Node Readable nor ReadableStream!');
|
|
156
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import Tar from 'tar-js'; // converts a string to utf8 Uint8Array and returns it as a string-like
|
|
2
|
+
// object that `tar.append` accepts as path
|
|
3
|
+
|
|
4
|
+
function fixUnicodePath(path) {
|
|
5
|
+
const codes = new TextEncoder().encode(path);
|
|
6
|
+
return {
|
|
7
|
+
length: codes.length,
|
|
8
|
+
charCodeAt: index => codes[index]
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function makeTar(data) {
|
|
13
|
+
const tar = new Tar();
|
|
14
|
+
|
|
15
|
+
for (const entry of data) {
|
|
16
|
+
const path = fixUnicodePath(entry.path);
|
|
17
|
+
tar.append(path, entry.data);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return tar.out;
|
|
21
|
+
}
|