@ethersphere/bee-js 3.2.0 → 3.3.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 +50 -0
- package/dist/cjs/chunk/cac.js +56 -0
- package/dist/cjs/chunk/serialize.js +19 -0
- package/dist/cjs/chunk/signer.js +132 -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 +7 -0
- package/dist/cjs/types/debug.js +9 -0
- package/dist/cjs/types/index.js +46 -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 +206 -0
- package/dist/cjs/utils/expose.js +44 -0
- package/dist/cjs/utils/file.js +49 -0
- package/dist/cjs/utils/hash.js +16 -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 +137 -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.js +28326 -0
- package/dist/index.js.map +1 -0
- package/dist/mjs/bee-debug.js +509 -0
- package/dist/mjs/bee.js +845 -0
- package/dist/mjs/chunk/bmt.js +46 -0
- package/dist/mjs/chunk/cac.js +50 -0
- package/dist/mjs/chunk/serialize.js +15 -0
- package/dist/mjs/chunk/signer.js +112 -0
- package/dist/mjs/chunk/soc.js +127 -0
- package/dist/mjs/chunk/span.js +25 -0
- package/dist/mjs/feed/index.js +136 -0
- package/dist/mjs/feed/json.js +23 -0
- package/dist/mjs/feed/topic.js +20 -0
- package/dist/mjs/feed/type.js +10 -0
- package/dist/mjs/index.js +7 -0
- package/dist/mjs/modules/bytes.js +56 -0
- package/dist/mjs/modules/bzz.js +119 -0
- package/dist/mjs/modules/chunk.js +43 -0
- package/dist/mjs/modules/debug/balance.js +53 -0
- package/dist/mjs/modules/debug/chequebook.js +131 -0
- package/dist/mjs/modules/debug/chunk.js +33 -0
- package/dist/mjs/modules/debug/connectivity.js +45 -0
- package/dist/mjs/modules/debug/settlements.js +27 -0
- package/dist/mjs/modules/debug/stamps.js +59 -0
- package/dist/mjs/modules/debug/states.js +29 -0
- package/dist/mjs/modules/debug/status.js +114 -0
- package/dist/mjs/modules/debug/tag.js +15 -0
- package/dist/mjs/modules/debug/transactions.js +57 -0
- package/dist/mjs/modules/feed.js +61 -0
- package/dist/mjs/modules/pinning.js +60 -0
- package/dist/mjs/modules/pss.js +36 -0
- package/dist/mjs/modules/soc.js +28 -0
- package/dist/mjs/modules/status.js +11 -0
- package/dist/mjs/modules/stewardship.js +23 -0
- package/dist/mjs/modules/tag.js +69 -0
- package/dist/mjs/package.json +7 -0
- package/dist/mjs/types/debug.js +6 -0
- package/dist/mjs/types/index.js +33 -0
- package/dist/mjs/utils/bytes.js +95 -0
- package/dist/mjs/utils/collection.browser.js +18 -0
- package/dist/mjs/utils/collection.js +52 -0
- package/dist/mjs/utils/collection.node.js +63 -0
- package/dist/mjs/utils/data.browser.js +57 -0
- package/dist/mjs/utils/data.js +39 -0
- package/dist/mjs/utils/error.js +48 -0
- package/dist/mjs/utils/eth.js +184 -0
- package/dist/mjs/utils/expose.js +9 -0
- package/dist/mjs/utils/file.js +33 -0
- package/dist/mjs/utils/hash.js +12 -0
- package/dist/mjs/utils/headers.js +54 -0
- package/dist/mjs/utils/hex.js +139 -0
- package/dist/mjs/utils/http.js +136 -0
- package/dist/mjs/utils/merge.js +30 -0
- package/dist/mjs/utils/pss.js +14 -0
- package/dist/mjs/utils/stamps.js +13 -0
- package/dist/mjs/utils/stream.js +131 -0
- package/dist/mjs/utils/tar.js +18 -0
- package/dist/mjs/utils/type.js +296 -0
- package/dist/mjs/utils/uint64.js +23 -0
- package/dist/mjs/utils/url.js +50 -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 +1 -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 +0 -0
- 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 +39 -23
- package/dist/index.browser.min.js +0 -3
- package/dist/index.browser.min.js.LICENSE.txt +0 -59
- package/dist/index.browser.min.js.map +0 -1
- 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,150 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.assertPrefixedHexString = exports.assertHexString = exports.isPrefixedHexString = exports.isHexString = exports.intToHex = exports.bytesToHex = exports.hexToBytes = exports.makeHexString = void 0;
|
|
4
|
+
const bytes_1 = require("./bytes");
|
|
5
|
+
/**
|
|
6
|
+
* Creates unprefixed hex string from wide range of data.
|
|
7
|
+
*
|
|
8
|
+
* TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
|
|
9
|
+
*
|
|
10
|
+
* @param input
|
|
11
|
+
* @param len of the resulting HexString WITHOUT prefix!
|
|
12
|
+
*/
|
|
13
|
+
function makeHexString(input, len) {
|
|
14
|
+
if (typeof input === 'number') {
|
|
15
|
+
return intToHex(input, len);
|
|
16
|
+
}
|
|
17
|
+
if (input instanceof Uint8Array) {
|
|
18
|
+
return bytesToHex(input, len);
|
|
19
|
+
}
|
|
20
|
+
if (typeof input === 'string') {
|
|
21
|
+
if (isPrefixedHexString(input)) {
|
|
22
|
+
const hex = input.slice(2);
|
|
23
|
+
if (len && hex.length !== len) {
|
|
24
|
+
throw new TypeError(`Length mismatch for valid hex string. Expecting length ${len}: ${hex}`);
|
|
25
|
+
}
|
|
26
|
+
return hex;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
// We use assertHexString() as there might be more reasons why a string is not valid hex string
|
|
30
|
+
// and usage of isHexString() would not give enough information to the user on what is going
|
|
31
|
+
// wrong.
|
|
32
|
+
assertHexString(input, len);
|
|
33
|
+
return input;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
throw new TypeError('Not HexString compatible type!');
|
|
37
|
+
}
|
|
38
|
+
exports.makeHexString = makeHexString;
|
|
39
|
+
/**
|
|
40
|
+
* Converts a hex string to Uint8Array
|
|
41
|
+
*
|
|
42
|
+
* @param hex string input without 0x prefix!
|
|
43
|
+
*/
|
|
44
|
+
function hexToBytes(hex) {
|
|
45
|
+
assertHexString(hex);
|
|
46
|
+
const bytes = (0, bytes_1.makeBytes)(hex.length / 2);
|
|
47
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
48
|
+
const hexByte = hex.substr(i * 2, 2);
|
|
49
|
+
bytes[i] = parseInt(hexByte, 16);
|
|
50
|
+
}
|
|
51
|
+
return bytes;
|
|
52
|
+
}
|
|
53
|
+
exports.hexToBytes = hexToBytes;
|
|
54
|
+
/**
|
|
55
|
+
* Converts array of number or Uint8Array to HexString without prefix.
|
|
56
|
+
*
|
|
57
|
+
* @param bytes The input array
|
|
58
|
+
* @param len The length of the non prefixed HexString
|
|
59
|
+
*/
|
|
60
|
+
function bytesToHex(bytes, len) {
|
|
61
|
+
const hexByte = (n) => n.toString(16).padStart(2, '0');
|
|
62
|
+
const hex = Array.from(bytes, hexByte).join('');
|
|
63
|
+
// TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
|
|
64
|
+
if (len && hex.length !== len) {
|
|
65
|
+
throw new TypeError(`Resulting HexString does not have expected length ${len}: ${hex}`);
|
|
66
|
+
}
|
|
67
|
+
return hex;
|
|
68
|
+
}
|
|
69
|
+
exports.bytesToHex = bytesToHex;
|
|
70
|
+
/**
|
|
71
|
+
* Converts integer number to hex string.
|
|
72
|
+
*
|
|
73
|
+
* Optionally provides '0x' prefix or padding
|
|
74
|
+
*
|
|
75
|
+
* @param int The positive integer to be converted
|
|
76
|
+
* @param len The length of the non prefixed HexString
|
|
77
|
+
*/
|
|
78
|
+
function intToHex(int, len) {
|
|
79
|
+
if (!Number.isInteger(int))
|
|
80
|
+
throw new TypeError('the value provided is not integer');
|
|
81
|
+
if (int > Number.MAX_SAFE_INTEGER)
|
|
82
|
+
throw new TypeError('the value provided exceeds safe integer');
|
|
83
|
+
if (int < 0)
|
|
84
|
+
throw new TypeError('the value provided is a negative integer');
|
|
85
|
+
const hex = int.toString(16);
|
|
86
|
+
// TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
|
|
87
|
+
if (len && hex.length !== len) {
|
|
88
|
+
throw new TypeError(`Resulting HexString does not have expected length ${len}: ${hex}`);
|
|
89
|
+
}
|
|
90
|
+
return hex;
|
|
91
|
+
}
|
|
92
|
+
exports.intToHex = intToHex;
|
|
93
|
+
/**
|
|
94
|
+
* Type guard for HexStrings.
|
|
95
|
+
* Requires no 0x prefix!
|
|
96
|
+
*
|
|
97
|
+
* TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
|
|
98
|
+
*
|
|
99
|
+
* @param s string input
|
|
100
|
+
* @param len expected length of the HexString
|
|
101
|
+
*/
|
|
102
|
+
function isHexString(s, len) {
|
|
103
|
+
return typeof s === 'string' && /^[0-9a-f]+$/i.test(s) && (!len || s.length === len);
|
|
104
|
+
}
|
|
105
|
+
exports.isHexString = isHexString;
|
|
106
|
+
/**
|
|
107
|
+
* Type guard for PrefixedHexStrings.
|
|
108
|
+
* Does enforce presence of 0x prefix!
|
|
109
|
+
*
|
|
110
|
+
* @param s string input
|
|
111
|
+
*/
|
|
112
|
+
function isPrefixedHexString(s) {
|
|
113
|
+
return typeof s === 'string' && /^0x[0-9a-f]+$/i.test(s);
|
|
114
|
+
}
|
|
115
|
+
exports.isPrefixedHexString = isPrefixedHexString;
|
|
116
|
+
/**
|
|
117
|
+
* Verifies if the provided input is a HexString.
|
|
118
|
+
*
|
|
119
|
+
* TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
|
|
120
|
+
*
|
|
121
|
+
* @param s string input
|
|
122
|
+
* @param len expected length of the HexString
|
|
123
|
+
* @param name optional name for the asserted value
|
|
124
|
+
* @returns HexString or throws error
|
|
125
|
+
*/
|
|
126
|
+
function assertHexString(s, len, name = 'value') {
|
|
127
|
+
if (!isHexString(s, len)) {
|
|
128
|
+
if (isPrefixedHexString(s)) {
|
|
129
|
+
throw new TypeError(`${name} not valid non prefixed hex string (has 0x prefix): ${s}`);
|
|
130
|
+
}
|
|
131
|
+
// Don't display length error if no length specified in order not to confuse user
|
|
132
|
+
const lengthMsg = len ? ` of length ${len}` : '';
|
|
133
|
+
throw new TypeError(`${name} not valid hex string${lengthMsg}: ${s}`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
exports.assertHexString = assertHexString;
|
|
137
|
+
/**
|
|
138
|
+
* Verifies if the provided input is a PrefixedHexString.
|
|
139
|
+
*
|
|
140
|
+
* @param s string input
|
|
141
|
+
* @param len expected length of the HexString
|
|
142
|
+
* @param name optional name for the asserted value
|
|
143
|
+
* @returns HexString or throws error
|
|
144
|
+
*/
|
|
145
|
+
function assertPrefixedHexString(s, name = 'value') {
|
|
146
|
+
if (!isPrefixedHexString(s)) {
|
|
147
|
+
throw new TypeError(`${name} not valid prefixed hex string: ${s}`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
exports.assertPrefixedHexString = assertPrefixedHexString;
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
12
|
+
var t = {};
|
|
13
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
14
|
+
t[p] = s[p];
|
|
15
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
16
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
17
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
18
|
+
t[p[i]] = s[p[i]];
|
|
19
|
+
}
|
|
20
|
+
return t;
|
|
21
|
+
};
|
|
22
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
23
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.makeDefaultKy = exports.http = exports.filterHeaders = exports.wrapResponseClosure = exports.wrapRequestClosure = void 0;
|
|
27
|
+
const error_1 = require("./error");
|
|
28
|
+
const ky_universal_1 = __importDefault(require("ky-universal"));
|
|
29
|
+
const stream_1 = require("./stream");
|
|
30
|
+
const merge_1 = require("./merge");
|
|
31
|
+
const type_1 = require("./type");
|
|
32
|
+
const DEFAULT_KY_CONFIG = {
|
|
33
|
+
headers: {
|
|
34
|
+
accept: 'application/json, text/plain, */*',
|
|
35
|
+
'user-agent': `bee-js`,
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
function isHttpError(e) {
|
|
39
|
+
return (0, type_1.isObject)(e) && typeof e.response !== 'undefined';
|
|
40
|
+
}
|
|
41
|
+
function isHttpRequestError(e) {
|
|
42
|
+
return (0, type_1.isObject)(e) && typeof e.request !== 'undefined';
|
|
43
|
+
}
|
|
44
|
+
function headersToObject(header) {
|
|
45
|
+
return [...header.entries()].reduce((obj, [key, val]) => {
|
|
46
|
+
obj[key] = val;
|
|
47
|
+
return obj;
|
|
48
|
+
}, {});
|
|
49
|
+
}
|
|
50
|
+
function wrapRequest(request) {
|
|
51
|
+
return {
|
|
52
|
+
url: request.url,
|
|
53
|
+
method: request.method.toUpperCase(),
|
|
54
|
+
headers: headersToObject(request.headers),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function wrapRequestClosure(cb) {
|
|
58
|
+
return (request) => __awaiter(this, void 0, void 0, function* () {
|
|
59
|
+
yield cb(wrapRequest(request));
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
exports.wrapRequestClosure = wrapRequestClosure;
|
|
63
|
+
function wrapResponseClosure(cb) {
|
|
64
|
+
return (request, options, response) => __awaiter(this, void 0, void 0, function* () {
|
|
65
|
+
yield cb({
|
|
66
|
+
headers: headersToObject(response.headers),
|
|
67
|
+
status: response.status,
|
|
68
|
+
statusText: response.statusText,
|
|
69
|
+
request: wrapRequest(request),
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
exports.wrapResponseClosure = wrapResponseClosure;
|
|
74
|
+
/**
|
|
75
|
+
* Filters out entries that has undefined value from headers object.
|
|
76
|
+
* Modifies the original object!
|
|
77
|
+
*
|
|
78
|
+
* @param obj
|
|
79
|
+
*/
|
|
80
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
81
|
+
function filterHeaders(obj) {
|
|
82
|
+
if (obj === undefined) {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
(0, type_1.isStrictlyObject)(obj);
|
|
86
|
+
const typedObj = obj;
|
|
87
|
+
for (const key in typedObj) {
|
|
88
|
+
if (typedObj[key] === undefined) {
|
|
89
|
+
delete typedObj[key];
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (Object.keys(typedObj).length === 0) {
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
return typedObj;
|
|
96
|
+
}
|
|
97
|
+
exports.filterHeaders = filterHeaders;
|
|
98
|
+
/**
|
|
99
|
+
* Main utility function to make HTTP requests.
|
|
100
|
+
* @param ky
|
|
101
|
+
* @param config
|
|
102
|
+
*/
|
|
103
|
+
function http(ky, config) {
|
|
104
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
105
|
+
try {
|
|
106
|
+
const { path, responseType } = config, kyConfig = __rest(config, ["path", "responseType"]);
|
|
107
|
+
const response = (yield ky(path, Object.assign(Object.assign({}, kyConfig), { searchParams: filterHeaders(kyConfig.searchParams) })));
|
|
108
|
+
switch (responseType) {
|
|
109
|
+
case 'stream':
|
|
110
|
+
if (!response.body) {
|
|
111
|
+
throw new error_1.BeeError('Response was expected to get data but did not get any!');
|
|
112
|
+
}
|
|
113
|
+
response.data = (0, stream_1.normalizeToReadableStream)(response.body);
|
|
114
|
+
break;
|
|
115
|
+
case 'arraybuffer':
|
|
116
|
+
response.data = (yield response.arrayBuffer());
|
|
117
|
+
break;
|
|
118
|
+
case 'json':
|
|
119
|
+
try {
|
|
120
|
+
response.data = (yield response.json());
|
|
121
|
+
}
|
|
122
|
+
catch (e) {
|
|
123
|
+
throw new error_1.BeeNotAJsonError();
|
|
124
|
+
}
|
|
125
|
+
break;
|
|
126
|
+
default:
|
|
127
|
+
break; // If responseType is not set, then no data are expected
|
|
128
|
+
}
|
|
129
|
+
return response;
|
|
130
|
+
}
|
|
131
|
+
catch (e) {
|
|
132
|
+
// Passthrough thrown errors
|
|
133
|
+
if (e instanceof error_1.BeeNotAJsonError) {
|
|
134
|
+
throw e;
|
|
135
|
+
}
|
|
136
|
+
if (isHttpError(e)) {
|
|
137
|
+
let message;
|
|
138
|
+
// We store the response body here as it can be read only once in Response's lifecycle so to make it exposed
|
|
139
|
+
// to the user in the BeeResponseError, for further analysis.
|
|
140
|
+
const body = yield e.response.text();
|
|
141
|
+
try {
|
|
142
|
+
// The response can be Bee's JSON with structure `{code, message}` lets try to parse it
|
|
143
|
+
message = JSON.parse(body).message;
|
|
144
|
+
}
|
|
145
|
+
catch (e) { }
|
|
146
|
+
if (message) {
|
|
147
|
+
throw new error_1.BeeResponseError(e.response.status, e.response, body, config, `${e.response.statusText}: ${message}`);
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
throw new error_1.BeeResponseError(e.response.status, e.response, body, config, e.response.statusText);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
else if (isHttpRequestError(e)) {
|
|
154
|
+
throw new error_1.BeeRequestError(e.message, config);
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
throw new error_1.BeeError(e.message);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
exports.http = http;
|
|
163
|
+
function makeDefaultKy(kyConfig) {
|
|
164
|
+
return ky_universal_1.default.create((0, merge_1.deepMerge)(DEFAULT_KY_CONFIG, kyConfig));
|
|
165
|
+
}
|
|
166
|
+
exports.makeDefaultKy = makeDefaultKy;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.deepMerge = void 0;
|
|
4
|
+
const type_1 = require("./type");
|
|
5
|
+
/**
|
|
6
|
+
* Function that deep merges objects
|
|
7
|
+
*
|
|
8
|
+
* @copyright https://github.com/sindresorhus/ky/blob/b3c9e88fa49d50150dbb6e6b771b4af56cb40c98/source/utils/merge.ts
|
|
9
|
+
* @licence MIT
|
|
10
|
+
* @param sources
|
|
11
|
+
*/
|
|
12
|
+
function deepMerge(...sources) {
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
14
|
+
let returnValue = {};
|
|
15
|
+
for (const source of sources) {
|
|
16
|
+
if (Array.isArray(source)) {
|
|
17
|
+
if (!Array.isArray(returnValue)) {
|
|
18
|
+
returnValue = [];
|
|
19
|
+
}
|
|
20
|
+
returnValue = [...returnValue, ...source];
|
|
21
|
+
}
|
|
22
|
+
else if ((0, type_1.isObject)(source)) {
|
|
23
|
+
// eslint-disable-next-line prefer-const
|
|
24
|
+
for (let [key, value] of Object.entries(source)) {
|
|
25
|
+
if ((0, type_1.isObject)(value) && key in returnValue) {
|
|
26
|
+
value = deepMerge(returnValue[key], value);
|
|
27
|
+
}
|
|
28
|
+
returnValue = Object.assign(Object.assign({}, returnValue), { [key]: value });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return returnValue;
|
|
33
|
+
}
|
|
34
|
+
exports.deepMerge = deepMerge;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.makeMaxTarget = void 0;
|
|
4
|
+
const types_1 = require("../types");
|
|
5
|
+
/**
|
|
6
|
+
* Utility function that for given strings/reference takes the most specific
|
|
7
|
+
* target that Bee node will except.
|
|
8
|
+
*
|
|
9
|
+
* @param target is a non-prefixed hex string Bee address
|
|
10
|
+
* @see [Bee docs - PSS](https://docs.ethswarm.org/docs/dapps-on-swarm/pss)
|
|
11
|
+
*/
|
|
12
|
+
function makeMaxTarget(target) {
|
|
13
|
+
if (typeof target !== 'string') {
|
|
14
|
+
throw new TypeError('target has to be an string!');
|
|
15
|
+
}
|
|
16
|
+
return target.slice(0, types_1.PSS_TARGET_HEX_LENGTH_MAX);
|
|
17
|
+
}
|
|
18
|
+
exports.makeMaxTarget = makeMaxTarget;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getStampUsage = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Utility function that calculates usage of postage batch based on its utilization, depth and bucket depth.
|
|
6
|
+
*
|
|
7
|
+
* Be aware for small depths (17, 18) this does not provide that much information as the provided set of distinct values
|
|
8
|
+
* is small.
|
|
9
|
+
*
|
|
10
|
+
* @param utilization
|
|
11
|
+
* @param depth
|
|
12
|
+
* @param bucketDepth
|
|
13
|
+
*/
|
|
14
|
+
function getStampUsage({ utilization, depth, bucketDepth }) {
|
|
15
|
+
return utilization / Math.pow(2, depth - bucketDepth);
|
|
16
|
+
}
|
|
17
|
+
exports.getStampUsage = getStampUsage;
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalizeToReadableStream = exports.readableWebToNode = exports.readableNodeToWeb = exports.isNodeReadable = exports.isReadableStream = exports.isReadable = void 0;
|
|
4
|
+
const type_1 = require("./type");
|
|
5
|
+
const ponyfill_1 = require("web-streams-polyfill/ponyfill");
|
|
6
|
+
const readable_stream_1 = require("readable-stream");
|
|
7
|
+
/**
|
|
8
|
+
* Validates if passed object is either browser's ReadableStream
|
|
9
|
+
* or Node's Readable.
|
|
10
|
+
*
|
|
11
|
+
* @param entry
|
|
12
|
+
*/
|
|
13
|
+
function isReadable(entry) {
|
|
14
|
+
return isReadableStream(entry) || isNodeReadable(entry);
|
|
15
|
+
}
|
|
16
|
+
exports.isReadable = isReadable;
|
|
17
|
+
function isReadableStream(entry) {
|
|
18
|
+
if (!(0, type_1.isStrictlyObject)(entry)) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
const browserReadable = entry;
|
|
22
|
+
if (typeof browserReadable.getReader === 'function' &&
|
|
23
|
+
browserReadable.locked !== undefined &&
|
|
24
|
+
typeof browserReadable.cancel === 'function' &&
|
|
25
|
+
typeof browserReadable.pipeTo === 'function' &&
|
|
26
|
+
typeof browserReadable.pipeThrough === 'function') {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
exports.isReadableStream = isReadableStream;
|
|
32
|
+
function isNodeReadable(entry) {
|
|
33
|
+
if (!(0, type_1.isStrictlyObject)(entry)) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
const nodeReadable = entry;
|
|
37
|
+
if (typeof nodeReadable.pipe === 'function' && nodeReadable.readable && typeof nodeReadable._read === 'function') {
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
exports.isNodeReadable = isNodeReadable;
|
|
43
|
+
/**
|
|
44
|
+
* Function that converts Node's Readable into WHATWG ReadableStream
|
|
45
|
+
*
|
|
46
|
+
* Taken over from https://github.com/gwicke/node-web-streams/blob/master/lib/conversions.js
|
|
47
|
+
* Because it uses forked web-streams-polyfill that are outdated.
|
|
48
|
+
*
|
|
49
|
+
* @author https://github.com/gwicke
|
|
50
|
+
* @licence Apache License 2.0 https://github.com/gwicke/node-web-streams/blob/master/LICENSE
|
|
51
|
+
* @param nodeStream
|
|
52
|
+
*/
|
|
53
|
+
function readableNodeToWeb(nodeStream) {
|
|
54
|
+
return new ponyfill_1.ReadableStream({
|
|
55
|
+
start(controller) {
|
|
56
|
+
nodeStream.pause();
|
|
57
|
+
nodeStream.on('data', chunk => {
|
|
58
|
+
if (Buffer.isBuffer(chunk)) {
|
|
59
|
+
controller.enqueue(new Uint8Array(chunk.buffer));
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
controller.enqueue(chunk);
|
|
63
|
+
}
|
|
64
|
+
nodeStream.pause();
|
|
65
|
+
});
|
|
66
|
+
nodeStream.on('end', () => controller.close());
|
|
67
|
+
nodeStream.on('error', e => controller.error(e));
|
|
68
|
+
},
|
|
69
|
+
pull() {
|
|
70
|
+
nodeStream.resume();
|
|
71
|
+
},
|
|
72
|
+
cancel() {
|
|
73
|
+
nodeStream.pause();
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
exports.readableNodeToWeb = readableNodeToWeb;
|
|
78
|
+
/**
|
|
79
|
+
* Taken over from https://github.com/gwicke/node-web-streams/blob/master/lib/conversions.js
|
|
80
|
+
* Because it uses forked web-streams-polyfill that are outdated.
|
|
81
|
+
*
|
|
82
|
+
* @author https://github.com/gwicke
|
|
83
|
+
* @licence Apache License 2.0 https://github.com/gwicke/node-web-streams/blob/master/LICENSE
|
|
84
|
+
*/
|
|
85
|
+
class NodeReadableWrapper extends readable_stream_1.Readable {
|
|
86
|
+
constructor(webStream, options) {
|
|
87
|
+
super(options);
|
|
88
|
+
this._webStream = webStream;
|
|
89
|
+
this._reader = webStream.getReader();
|
|
90
|
+
this._reading = false;
|
|
91
|
+
}
|
|
92
|
+
_read() {
|
|
93
|
+
if (this._reading) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
this._reading = true;
|
|
97
|
+
const doRead = () => {
|
|
98
|
+
this._reader.read().then(res => {
|
|
99
|
+
if (res.done) {
|
|
100
|
+
this.push(null);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (this.push(res.value)) {
|
|
104
|
+
return doRead();
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
this._reading = false;
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
};
|
|
111
|
+
doRead();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Function that converts WHATWG ReadableStream into Node's Readable
|
|
116
|
+
*
|
|
117
|
+
* Taken over from https://github.com/gwicke/node-web-streams/blob/master/lib/conversions.js
|
|
118
|
+
* Because it uses forked web-streams-polyfill that are outdated.
|
|
119
|
+
*
|
|
120
|
+
* @author https://github.com/gwicke
|
|
121
|
+
* @licence Apache License 2.0 https://github.com/gwicke/node-web-streams/blob/master/LICENSE
|
|
122
|
+
* @param webStream
|
|
123
|
+
*/
|
|
124
|
+
function readableWebToNode(webStream, options) {
|
|
125
|
+
return new NodeReadableWrapper(webStream, options);
|
|
126
|
+
}
|
|
127
|
+
exports.readableWebToNode = readableWebToNode;
|
|
128
|
+
function normalizeToReadableStream(stream) {
|
|
129
|
+
if (isNodeReadable(stream)) {
|
|
130
|
+
return readableNodeToWeb(stream);
|
|
131
|
+
}
|
|
132
|
+
else if (isReadableStream(stream)) {
|
|
133
|
+
return stream;
|
|
134
|
+
}
|
|
135
|
+
throw new TypeError('Passed stream is not Node Readable nor ReadableStream!');
|
|
136
|
+
}
|
|
137
|
+
exports.normalizeToReadableStream = normalizeToReadableStream;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.makeTar = void 0;
|
|
7
|
+
const tar_js_1 = __importDefault(require("tar-js"));
|
|
8
|
+
// converts a string to utf8 Uint8Array and returns it as a string-like
|
|
9
|
+
// object that `tar.append` accepts as path
|
|
10
|
+
function fixUnicodePath(path) {
|
|
11
|
+
const codes = new TextEncoder().encode(path);
|
|
12
|
+
return {
|
|
13
|
+
length: codes.length,
|
|
14
|
+
charCodeAt: index => codes[index],
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function makeTar(data) {
|
|
18
|
+
const tar = new tar_js_1.default();
|
|
19
|
+
for (const entry of data) {
|
|
20
|
+
const path = fixUnicodePath(entry.path);
|
|
21
|
+
tar.append(path, entry.data);
|
|
22
|
+
}
|
|
23
|
+
return tar.out;
|
|
24
|
+
}
|
|
25
|
+
exports.makeTar = makeTar;
|