@ethersphere/bee-js 2.1.0 → 3.2.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/README.md +3 -3
- package/dist/index.browser.min.js +1 -1
- package/dist/index.browser.min.js.map +1 -1
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +1 -1
- package/dist/src/bee-debug.d.ts +5 -1
- package/dist/src/bee.d.ts +23 -46
- package/dist/src/modules/chunk.d.ts +2 -10
- package/dist/src/modules/debug/status.d.ts +10 -4
- package/dist/src/types/debug.d.ts +19 -0
- package/dist/src/types/index.d.ts +31 -2
- package/dist/src/utils/bytes.d.ts +1 -1
- package/dist/src/utils/error.d.ts +21 -2
- package/dist/src/utils/expose.d.ts +1 -0
- package/dist/src/utils/http.d.ts +7 -9
- package/dist/src/utils/stamps.d.ts +12 -0
- package/dist/src/utils/type.d.ts +6 -0
- package/package.json +28 -27
- package/.babelrc.js +0 -33
- package/.depcheckrc.json +0 -27
- package/.editorconfig +0 -6
- package/.eslintignore +0 -1
- package/.eslintrc.js +0 -109
- package/.gitattributes +0 -1
- package/.github/config.yaml +0 -23
- package/.github/workflows/check.yaml +0 -57
- package/.github/workflows/publish_npmjs.yaml +0 -20
- package/.github/workflows/release_github.yaml +0 -20
- package/.github/workflows/tests.yaml +0 -128
- package/.huskyrc +0 -5
- package/.prettierrc +0 -13
- package/.vscode/launch.json +0 -49
- package/.vscode/settings.json +0 -39
- package/CHANGELOG.md +0 -520
- package/CODEOWNERS +0 -1
- package/CODE_OF_CONDUCT.md +0 -76
- package/commitlint.config.js +0 -3
- package/dist/src/modules/stamps.d.ts +0 -4
- package/jest-puppeteer.config.js +0 -5
- package/jest.config.ts +0 -85
- package/src/bee-debug.ts +0 -601
- package/src/bee.ts +0 -1087
- package/src/chunk/bmt.ts +0 -55
- package/src/chunk/cac.ts +0 -79
- package/src/chunk/serialize.ts +0 -16
- package/src/chunk/signer.ts +0 -141
- package/src/chunk/soc.ts +0 -190
- package/src/chunk/span.ts +0 -33
- package/src/feed/index.ts +0 -217
- package/src/feed/json.ts +0 -33
- package/src/feed/topic.ts +0 -23
- package/src/feed/type.ts +0 -12
- package/src/index.ts +0 -24
- package/src/modules/bytes.ts +0 -70
- package/src/modules/bzz.ts +0 -179
- package/src/modules/chunk.ts +0 -69
- package/src/modules/debug/balance.ts +0 -63
- package/src/modules/debug/chequebook.ts +0 -175
- package/src/modules/debug/chunk.ts +0 -39
- package/src/modules/debug/connectivity.ts +0 -61
- package/src/modules/debug/settlements.ts +0 -33
- package/src/modules/debug/stamps.ts +0 -96
- package/src/modules/debug/states.ts +0 -35
- package/src/modules/debug/status.ts +0 -34
- package/src/modules/debug/tag.ts +0 -19
- package/src/modules/debug/transactions.ts +0 -79
- package/src/modules/feed.ts +0 -105
- package/src/modules/pinning.ts +0 -74
- package/src/modules/pss.ts +0 -49
- package/src/modules/soc.ts +0 -40
- package/src/modules/stamps.ts +0 -59
- package/src/modules/status.ts +0 -13
- package/src/modules/stewardship.ts +0 -32
- package/src/modules/tag.ts +0 -83
- package/src/types/debug.ts +0 -204
- package/src/types/index.ts +0 -561
- package/src/types/ky-universal/common.d.ts +0 -17
- package/src/types/ky-universal/hooks.d.ts +0 -103
- package/src/types/ky-universal/index.d.ts +0 -125
- package/src/types/ky-universal/options.d.ts +0 -235
- package/src/types/ky-universal/response.d.ts +0 -38
- package/src/types/ky-universal/retry.d.ts +0 -39
- package/src/types/tar-js.d.ts +0 -1
- package/src/types/webpack-bundle-analyzer/index.d.ts +0 -13
- package/src/utils/bytes.ts +0 -144
- package/src/utils/collection.ts +0 -151
- package/src/utils/data.browser.ts +0 -68
- package/src/utils/data.ts +0 -48
- package/src/utils/error.ts +0 -23
- package/src/utils/eth.ts +0 -229
- package/src/utils/expose.ts +0 -48
- package/src/utils/file.ts +0 -40
- package/src/utils/hash.ts +0 -16
- package/src/utils/headers.ts +0 -63
- package/src/utils/hex.ts +0 -186
- package/src/utils/http.ts +0 -141
- package/src/utils/merge.ts +0 -34
- package/src/utils/pss.ts +0 -16
- package/src/utils/stream.ts +0 -146
- package/src/utils/tar.ts +0 -30
- package/src/utils/type.ts +0 -394
- package/src/utils/uint64.ts +0 -31
- package/src/utils/url.ts +0 -52
- package/test/bee.sh +0 -187
- package/test/data/1.txt +0 -1
- package/test/data/2.txt +0 -0
- package/test/data/empty +0 -0
- package/test/data/sub/3.txt +0 -1
- package/test/data/sub//303/251 +0 -0
- package/test/data/sub//360/237/230/216 +0 -0
- package/test/integration/bee-class.browser.spec.ts +0 -272
- package/test/integration/bee-class.spec.ts +0 -648
- package/test/integration/bee-debug-class.spec.ts +0 -114
- package/test/integration/chunk/bmt.spec.ts +0 -33
- package/test/integration/chunk/cac.spec.ts +0 -27
- package/test/integration/chunk/soc.spec.ts +0 -38
- package/test/integration/feed/index.spec.ts +0 -84
- package/test/integration/modules/bytes.spec.ts +0 -23
- package/test/integration/modules/bzz.spec.ts +0 -265
- package/test/integration/modules/chunk.spec.ts +0 -29
- package/test/integration/modules/debug/balance.spec.ts +0 -70
- package/test/integration/modules/debug/chequebook.spec.ts +0 -64
- package/test/integration/modules/debug/connectivity.spec.ts +0 -69
- package/test/integration/modules/debug/settlements.spec.ts +0 -34
- package/test/integration/modules/debug/states.spec.ts +0 -34
- package/test/integration/modules/debug/status.spec.ts +0 -65
- package/test/integration/modules/debug/tag.spec.ts +0 -27
- package/test/integration/modules/debug/transactions.spec.ts +0 -10
- package/test/integration/modules/feed.spec.ts +0 -61
- package/test/integration/modules/pinning.spec.ts +0 -155
- package/test/integration/modules/pss.spec.ts +0 -105
- package/test/integration/modules/status.spec.ts +0 -10
- package/test/integration/modules/stewardship.spec.ts +0 -32
- package/test/integration/modules/tag.spec.ts +0 -51
- package/test/test-type-sequencer.js +0 -36
- package/test/testpage/testpage.html +0 -20
- package/test/tests-setup.ts +0 -94
- package/test/unit/assertions.ts +0 -460
- package/test/unit/bee-class.spec.ts +0 -976
- package/test/unit/bee-debug-class.spec.ts +0 -435
- package/test/unit/chunk/bmt.spec.ts +0 -16
- package/test/unit/chunk/cac.spec.ts +0 -29
- package/test/unit/chunk/serialize.spec.ts +0 -24
- package/test/unit/chunk/signer.spec.ts +0 -140
- package/test/unit/chunk/soc.spec.ts +0 -25
- package/test/unit/chunk/span.spec.ts +0 -38
- package/test/unit/feed/json.spec.ts +0 -73
- package/test/unit/nock.ts +0 -151
- package/test/unit/utils/bytes.spec.ts +0 -32
- package/test/unit/utils/collection.spec.ts +0 -22
- package/test/unit/utils/eth.spec.ts +0 -224
- package/test/unit/utils/hex.spec.ts +0 -115
- package/test/unit/utils/stream.spec.ts +0 -30
- package/test/unit/utils/type.spec.ts +0 -32
- package/test/unit/utils/uint64.spec.ts +0 -102
- package/test/utils.ts +0 -426
- package/tsconfig.json +0 -18
- package/tsconfig.test.json +0 -11
- package/typedoc.json +0 -7
- package/webpack.config.ts +0 -141
package/src/utils/hex.ts
DELETED
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
import { Bytes, makeBytes } from './bytes'
|
|
2
|
-
import { BrandedType, FlavoredType } from '../types'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Nominal type to represent hex strings WITHOUT '0x' prefix.
|
|
6
|
-
* For example for 32 bytes hex representation you have to use 64 length.
|
|
7
|
-
* TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
|
|
8
|
-
*/
|
|
9
|
-
export type HexString<Length extends number = number> = FlavoredType<
|
|
10
|
-
string & {
|
|
11
|
-
readonly length: Length
|
|
12
|
-
},
|
|
13
|
-
'HexString'
|
|
14
|
-
>
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Type for HexString with prefix.
|
|
18
|
-
* The main hex type used internally should be non-prefixed HexString
|
|
19
|
-
* and therefore this type should be used as least as possible.
|
|
20
|
-
* Because of that it does not contain the Length property as the variables
|
|
21
|
-
* should be validated and converted to HexString ASAP.
|
|
22
|
-
*/
|
|
23
|
-
export type PrefixedHexString = BrandedType<string, 'PrefixedHexString'>
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Creates unprefixed hex string from wide range of data.
|
|
27
|
-
*
|
|
28
|
-
* TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
|
|
29
|
-
*
|
|
30
|
-
* @param input
|
|
31
|
-
* @param len of the resulting HexString WITHOUT prefix!
|
|
32
|
-
*/
|
|
33
|
-
export function makeHexString<L extends number>(input: string | number | Uint8Array | unknown, len?: L): HexString<L> {
|
|
34
|
-
if (typeof input === 'number') {
|
|
35
|
-
return intToHex<L>(input, len)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (input instanceof Uint8Array) {
|
|
39
|
-
return bytesToHex<L>(input, len)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (typeof input === 'string') {
|
|
43
|
-
if (isPrefixedHexString(input)) {
|
|
44
|
-
const hex = input.slice(2) as HexString<L>
|
|
45
|
-
|
|
46
|
-
if (len && hex.length !== len) {
|
|
47
|
-
throw new TypeError(`Length mismatch for valid hex string. Expecting length ${len}: ${hex}`)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return hex
|
|
51
|
-
} else {
|
|
52
|
-
// We use assertHexString() as there might be more reasons why a string is not valid hex string
|
|
53
|
-
// and usage of isHexString() would not give enough information to the user on what is going
|
|
54
|
-
// wrong.
|
|
55
|
-
assertHexString<L>(input, len)
|
|
56
|
-
|
|
57
|
-
return input
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
throw new TypeError('Not HexString compatible type!')
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Converts a hex string to Uint8Array
|
|
66
|
-
*
|
|
67
|
-
* @param hex string input without 0x prefix!
|
|
68
|
-
*/
|
|
69
|
-
export function hexToBytes<Length extends number, LengthHex extends number = number>(
|
|
70
|
-
hex: HexString<LengthHex>,
|
|
71
|
-
): Bytes<Length> {
|
|
72
|
-
assertHexString(hex)
|
|
73
|
-
|
|
74
|
-
const bytes = makeBytes(hex.length / 2)
|
|
75
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
76
|
-
const hexByte = hex.substr(i * 2, 2)
|
|
77
|
-
bytes[i] = parseInt(hexByte, 16)
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return bytes as Bytes<Length>
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Converts array of number or Uint8Array to HexString without prefix.
|
|
85
|
-
*
|
|
86
|
-
* @param bytes The input array
|
|
87
|
-
* @param len The length of the non prefixed HexString
|
|
88
|
-
*/
|
|
89
|
-
export function bytesToHex<Length extends number = number>(bytes: Uint8Array, len?: Length): HexString<Length> {
|
|
90
|
-
const hexByte = (n: number) => n.toString(16).padStart(2, '0')
|
|
91
|
-
const hex = Array.from(bytes, hexByte).join('') as HexString<Length>
|
|
92
|
-
|
|
93
|
-
// TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
|
|
94
|
-
if (len && hex.length !== len) {
|
|
95
|
-
throw new TypeError(`Resulting HexString does not have expected length ${len}: ${hex}`)
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
return hex
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Converts integer number to hex string.
|
|
103
|
-
*
|
|
104
|
-
* Optionally provides '0x' prefix or padding
|
|
105
|
-
*
|
|
106
|
-
* @param int The positive integer to be converted
|
|
107
|
-
* @param len The length of the non prefixed HexString
|
|
108
|
-
*/
|
|
109
|
-
export function intToHex<Length extends number = number>(int: number, len?: Length): HexString<Length> {
|
|
110
|
-
if (!Number.isInteger(int)) throw new TypeError('the value provided is not integer')
|
|
111
|
-
|
|
112
|
-
if (int > Number.MAX_SAFE_INTEGER) throw new TypeError('the value provided exceeds safe integer')
|
|
113
|
-
|
|
114
|
-
if (int < 0) throw new TypeError('the value provided is a negative integer')
|
|
115
|
-
const hex = int.toString(16) as HexString<Length>
|
|
116
|
-
|
|
117
|
-
// TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
|
|
118
|
-
if (len && hex.length !== len) {
|
|
119
|
-
throw new TypeError(`Resulting HexString does not have expected length ${len}: ${hex}`)
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
return hex
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Type guard for HexStrings.
|
|
127
|
-
* Requires no 0x prefix!
|
|
128
|
-
*
|
|
129
|
-
* TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
|
|
130
|
-
*
|
|
131
|
-
* @param s string input
|
|
132
|
-
* @param len expected length of the HexString
|
|
133
|
-
*/
|
|
134
|
-
export function isHexString<Length extends number = number>(s: unknown, len?: number): s is HexString<Length> {
|
|
135
|
-
return typeof s === 'string' && /^[0-9a-f]+$/i.test(s) && (!len || s.length === len)
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Type guard for PrefixedHexStrings.
|
|
140
|
-
* Does enforce presence of 0x prefix!
|
|
141
|
-
*
|
|
142
|
-
* @param s string input
|
|
143
|
-
*/
|
|
144
|
-
export function isPrefixedHexString(s: unknown): s is PrefixedHexString {
|
|
145
|
-
return typeof s === 'string' && /^0x[0-9a-f]+$/i.test(s)
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Verifies if the provided input is a HexString.
|
|
150
|
-
*
|
|
151
|
-
* TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
|
|
152
|
-
*
|
|
153
|
-
* @param s string input
|
|
154
|
-
* @param len expected length of the HexString
|
|
155
|
-
* @param name optional name for the asserted value
|
|
156
|
-
* @returns HexString or throws error
|
|
157
|
-
*/
|
|
158
|
-
export function assertHexString<Length extends number = number>(
|
|
159
|
-
s: unknown,
|
|
160
|
-
len?: number,
|
|
161
|
-
name = 'value',
|
|
162
|
-
): asserts s is HexString<Length> {
|
|
163
|
-
if (!isHexString(s, len)) {
|
|
164
|
-
if (isPrefixedHexString(s)) {
|
|
165
|
-
throw new TypeError(`${name} not valid non prefixed hex string (has 0x prefix): ${s}`)
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
// Don't display length error if no length specified in order not to confuse user
|
|
169
|
-
const lengthMsg = len ? ` of length ${len}` : ''
|
|
170
|
-
throw new TypeError(`${name} not valid hex string${lengthMsg}: ${s}`)
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Verifies if the provided input is a PrefixedHexString.
|
|
176
|
-
*
|
|
177
|
-
* @param s string input
|
|
178
|
-
* @param len expected length of the HexString
|
|
179
|
-
* @param name optional name for the asserted value
|
|
180
|
-
* @returns HexString or throws error
|
|
181
|
-
*/
|
|
182
|
-
export function assertPrefixedHexString(s: string, name = 'value'): asserts s is PrefixedHexString {
|
|
183
|
-
if (!isPrefixedHexString(s)) {
|
|
184
|
-
throw new TypeError(`${name} not valid prefixed hex string: ${s}`)
|
|
185
|
-
}
|
|
186
|
-
}
|
package/src/utils/http.ts
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import { BeeError, BeeRequestError, BeeResponseError } from './error'
|
|
2
|
-
import type { BeeRequest, BeeResponse, HookCallback, HttpMethod, Ky } from '../types'
|
|
3
|
-
import kyFactory, { Options as KyOptions } from 'ky-universal'
|
|
4
|
-
import { normalizeToReadableStream } from './stream'
|
|
5
|
-
import { deepMerge } from './merge'
|
|
6
|
-
import { version as beeJsVersion } from '../../package.json'
|
|
7
|
-
import { isStrictlyObject } from './type'
|
|
8
|
-
|
|
9
|
-
const DEFAULT_KY_CONFIG: KyOptions = {
|
|
10
|
-
headers: {
|
|
11
|
-
accept: 'application/json, text/plain, */*',
|
|
12
|
-
'user-agent': `bee-js/${beeJsVersion}`,
|
|
13
|
-
},
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
interface HttpOptions extends Omit<KyOptions, 'searchParams'> {
|
|
17
|
-
path: string
|
|
18
|
-
responseType?: 'json' | 'arraybuffer' | 'stream'
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Overridden parameter that allows undefined as a value.
|
|
22
|
-
*/
|
|
23
|
-
searchParams?: Record<string, string | number | boolean | undefined>
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
interface KyResponse<T> extends Response {
|
|
27
|
-
data: T
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function headersToObject(header: Headers) {
|
|
31
|
-
return [...header.entries()].reduce<Record<string, string>>((obj, [key, val]) => {
|
|
32
|
-
obj[key] = val
|
|
33
|
-
|
|
34
|
-
return obj
|
|
35
|
-
}, {})
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function wrapRequest(request: Request): BeeRequest {
|
|
39
|
-
return {
|
|
40
|
-
url: request.url,
|
|
41
|
-
method: request.method.toUpperCase() as HttpMethod,
|
|
42
|
-
headers: headersToObject(request.headers),
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function wrapRequestClosure(cb: HookCallback<BeeRequest>): (request: Request) => Promise<void> {
|
|
47
|
-
return async (request: Request) => {
|
|
48
|
-
await cb(wrapRequest(request))
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export function wrapResponseClosure(
|
|
53
|
-
cb: HookCallback<BeeResponse>,
|
|
54
|
-
): (request: Request, options: unknown, response: Response) => Promise<void> {
|
|
55
|
-
return async (request: Request, options: unknown, response: Response) => {
|
|
56
|
-
await cb({
|
|
57
|
-
headers: headersToObject(response.headers),
|
|
58
|
-
status: response.status,
|
|
59
|
-
statusText: response.statusText,
|
|
60
|
-
request: wrapRequest(request),
|
|
61
|
-
})
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Filters out entries that has undefined value from headers object.
|
|
67
|
-
* Modifies the original object!
|
|
68
|
-
*
|
|
69
|
-
* @param obj
|
|
70
|
-
*/
|
|
71
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
72
|
-
export function filterHeaders(obj?: object): Record<string, string> | undefined {
|
|
73
|
-
if (obj === undefined) {
|
|
74
|
-
return undefined
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
isStrictlyObject(obj)
|
|
78
|
-
|
|
79
|
-
const typedObj = obj as Record<string, string>
|
|
80
|
-
|
|
81
|
-
for (const key in typedObj) {
|
|
82
|
-
if (typedObj[key] === undefined) {
|
|
83
|
-
delete typedObj[key]
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
if (Object.keys(typedObj).length === 0) {
|
|
88
|
-
return undefined
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
return typedObj
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export async function http<T>(ky: Ky, config: HttpOptions): Promise<KyResponse<T>> {
|
|
95
|
-
try {
|
|
96
|
-
const { path, responseType, ...kyConfig } = config
|
|
97
|
-
|
|
98
|
-
const response = (await ky(path, {
|
|
99
|
-
...kyConfig,
|
|
100
|
-
searchParams: filterHeaders(kyConfig.searchParams),
|
|
101
|
-
})) as KyResponse<T>
|
|
102
|
-
|
|
103
|
-
switch (responseType) {
|
|
104
|
-
case 'stream':
|
|
105
|
-
if (!response.body) {
|
|
106
|
-
throw new BeeError('Response was expected to get data but did not get any!')
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
response.data = normalizeToReadableStream(response.body) as unknown as T
|
|
110
|
-
break
|
|
111
|
-
case 'arraybuffer':
|
|
112
|
-
response.data = (await response.arrayBuffer()) as unknown as T
|
|
113
|
-
break
|
|
114
|
-
case 'json':
|
|
115
|
-
response.data = (await response.json()) as unknown as T
|
|
116
|
-
break
|
|
117
|
-
default:
|
|
118
|
-
break // If responseType is not set, then no data are expected
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
return response
|
|
122
|
-
} catch (e) {
|
|
123
|
-
if (e.response) {
|
|
124
|
-
const message = (await e.response.json()).message
|
|
125
|
-
|
|
126
|
-
if (message) {
|
|
127
|
-
throw new BeeResponseError(e.response.status, `${e.response.statusText}: ${message}`)
|
|
128
|
-
} else {
|
|
129
|
-
throw new BeeResponseError(e.response.status, e.response.statusText)
|
|
130
|
-
}
|
|
131
|
-
} else if (e.request) {
|
|
132
|
-
throw new BeeRequestError(e.message)
|
|
133
|
-
} else {
|
|
134
|
-
throw new BeeError(e.message)
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
export function makeDefaultKy(kyConfig: KyOptions): Ky {
|
|
140
|
-
return kyFactory.create(deepMerge(DEFAULT_KY_CONFIG, kyConfig))
|
|
141
|
-
}
|
package/src/utils/merge.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { isObject } from './type'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Function that deep merges objects
|
|
5
|
-
*
|
|
6
|
-
* @copyright https://github.com/sindresorhus/ky/blob/b3c9e88fa49d50150dbb6e6b771b4af56cb40c98/source/utils/merge.ts
|
|
7
|
-
* @licence MIT
|
|
8
|
-
* @param sources
|
|
9
|
-
*/
|
|
10
|
-
export function deepMerge<T>(...sources: Array<Partial<T> | undefined>): T {
|
|
11
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
12
|
-
let returnValue: any = {}
|
|
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, [key]: value }
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return returnValue
|
|
34
|
-
}
|
package/src/utils/pss.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { AddressPrefix, PSS_TARGET_HEX_LENGTH_MAX } from '../types'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Utility function that for given strings/reference takes the most specific
|
|
5
|
-
* target that Bee node will except.
|
|
6
|
-
*
|
|
7
|
-
* @param target is a non-prefixed hex string Bee address
|
|
8
|
-
* @see [Bee docs - PSS](https://docs.ethswarm.org/docs/dapps-on-swarm/pss)
|
|
9
|
-
*/
|
|
10
|
-
export function makeMaxTarget(target: string): AddressPrefix {
|
|
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
|
-
}
|
package/src/utils/stream.ts
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
import type { Readable as NodeReadableType } from 'stream'
|
|
2
|
-
import { isStrictlyObject } from './type'
|
|
3
|
-
import { ReadableStream } from 'web-streams-polyfill/ponyfill'
|
|
4
|
-
import { Readable as NodeReadable, ReadableOptions as NodeReadableOptions } from 'readable-stream'
|
|
5
|
-
|
|
6
|
-
import { Readable } from '../types'
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Validates if passed object is either browser's ReadableStream
|
|
10
|
-
* or Node's Readable.
|
|
11
|
-
*
|
|
12
|
-
* @param entry
|
|
13
|
-
*/
|
|
14
|
-
export function isReadable(entry: unknown): entry is NodeReadableType {
|
|
15
|
-
return isReadableStream(entry) || isNodeReadable(entry)
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export function isReadableStream(entry: unknown): entry is ReadableStream {
|
|
19
|
-
if (!isStrictlyObject(entry)) {
|
|
20
|
-
return false
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const browserReadable = entry as ReadableStream
|
|
24
|
-
|
|
25
|
-
if (
|
|
26
|
-
typeof browserReadable.getReader === 'function' &&
|
|
27
|
-
browserReadable.locked !== undefined &&
|
|
28
|
-
typeof browserReadable.cancel === 'function' &&
|
|
29
|
-
typeof browserReadable.pipeTo === 'function' &&
|
|
30
|
-
typeof browserReadable.pipeThrough === 'function'
|
|
31
|
-
) {
|
|
32
|
-
return true
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return false
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function isNodeReadable(entry: unknown): entry is NodeReadableType {
|
|
39
|
-
if (!isStrictlyObject(entry)) {
|
|
40
|
-
return false
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const nodeReadable = entry as NodeReadableType
|
|
44
|
-
|
|
45
|
-
if (typeof nodeReadable.pipe === 'function' && nodeReadable.readable && typeof nodeReadable._read === 'function') {
|
|
46
|
-
return true
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return false
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Function that converts Node's Readable into WHATWG ReadableStream
|
|
54
|
-
*
|
|
55
|
-
* Taken over from https://github.com/gwicke/node-web-streams/blob/master/lib/conversions.js
|
|
56
|
-
* Because it uses forked web-streams-polyfill that are outdated.
|
|
57
|
-
*
|
|
58
|
-
* @author https://github.com/gwicke
|
|
59
|
-
* @licence Apache License 2.0 https://github.com/gwicke/node-web-streams/blob/master/LICENSE
|
|
60
|
-
* @param nodeStream
|
|
61
|
-
*/
|
|
62
|
-
export function readableNodeToWeb(nodeStream: NodeReadableType): ReadableStream<Uint8Array> {
|
|
63
|
-
return new ReadableStream({
|
|
64
|
-
start(controller) {
|
|
65
|
-
nodeStream.pause()
|
|
66
|
-
nodeStream.on('data', chunk => {
|
|
67
|
-
controller.enqueue(chunk)
|
|
68
|
-
nodeStream.pause()
|
|
69
|
-
})
|
|
70
|
-
nodeStream.on('end', () => controller.close())
|
|
71
|
-
nodeStream.on('error', e => controller.error(e))
|
|
72
|
-
},
|
|
73
|
-
pull() {
|
|
74
|
-
nodeStream.resume()
|
|
75
|
-
},
|
|
76
|
-
cancel() {
|
|
77
|
-
nodeStream.pause()
|
|
78
|
-
},
|
|
79
|
-
})
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Taken over from https://github.com/gwicke/node-web-streams/blob/master/lib/conversions.js
|
|
84
|
-
* Because it uses forked web-streams-polyfill that are outdated.
|
|
85
|
-
*
|
|
86
|
-
* @author https://github.com/gwicke
|
|
87
|
-
* @licence Apache License 2.0 https://github.com/gwicke/node-web-streams/blob/master/LICENSE
|
|
88
|
-
*/
|
|
89
|
-
class NodeReadableWrapper extends NodeReadable {
|
|
90
|
-
private _webStream: ReadableStream
|
|
91
|
-
private _reader: ReadableStreamDefaultReader<any>
|
|
92
|
-
private _reading: boolean
|
|
93
|
-
constructor(webStream: ReadableStream, options?: NodeReadableOptions) {
|
|
94
|
-
super(options)
|
|
95
|
-
this._webStream = webStream
|
|
96
|
-
this._reader = webStream.getReader()
|
|
97
|
-
this._reading = false
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
_read() {
|
|
101
|
-
if (this._reading) {
|
|
102
|
-
return
|
|
103
|
-
}
|
|
104
|
-
this._reading = true
|
|
105
|
-
const doRead = () => {
|
|
106
|
-
this._reader.read().then(res => {
|
|
107
|
-
if (res.done) {
|
|
108
|
-
this.push(null)
|
|
109
|
-
|
|
110
|
-
return
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
if (this.push(res.value)) {
|
|
114
|
-
return doRead()
|
|
115
|
-
} else {
|
|
116
|
-
this._reading = false
|
|
117
|
-
}
|
|
118
|
-
})
|
|
119
|
-
}
|
|
120
|
-
doRead()
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Function that converts WHATWG ReadableStream into Node's Readable
|
|
126
|
-
*
|
|
127
|
-
* Taken over from https://github.com/gwicke/node-web-streams/blob/master/lib/conversions.js
|
|
128
|
-
* Because it uses forked web-streams-polyfill that are outdated.
|
|
129
|
-
*
|
|
130
|
-
* @author https://github.com/gwicke
|
|
131
|
-
* @licence Apache License 2.0 https://github.com/gwicke/node-web-streams/blob/master/LICENSE
|
|
132
|
-
* @param webStream
|
|
133
|
-
*/
|
|
134
|
-
export function readableWebToNode(webStream: ReadableStream, options?: NodeReadableOptions): NodeReadableType {
|
|
135
|
-
return new NodeReadableWrapper(webStream, options) as unknown as NodeReadableType
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export function normalizeToReadableStream(stream: Readable): ReadableStream {
|
|
139
|
-
if (isNodeReadable(stream)) {
|
|
140
|
-
return readableNodeToWeb(stream)
|
|
141
|
-
} else if (isReadableStream(stream)) {
|
|
142
|
-
return stream
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
throw new TypeError('Passed stream is not Node Readable nor ReadableStream!')
|
|
146
|
-
}
|
package/src/utils/tar.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { Collection } from '../types'
|
|
2
|
-
import Tar from 'tar-js'
|
|
3
|
-
|
|
4
|
-
// this is a workaround type so that we are able to pass in Uint8Arrays
|
|
5
|
-
// as string to `tar.append`
|
|
6
|
-
interface StringLike {
|
|
7
|
-
readonly length: number
|
|
8
|
-
charCodeAt: (index: number) => number
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
// converts a string to utf8 Uint8Array and returns it as a string-like
|
|
12
|
-
// object that `tar.append` accepts as path
|
|
13
|
-
function fixUnicodePath(path: string): StringLike {
|
|
14
|
-
const codes = new TextEncoder().encode(path)
|
|
15
|
-
|
|
16
|
-
return {
|
|
17
|
-
length: codes.length,
|
|
18
|
-
charCodeAt: index => codes[index],
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export function makeTar(data: Collection<Uint8Array>): Uint8Array {
|
|
23
|
-
const tar = new Tar()
|
|
24
|
-
for (const entry of data) {
|
|
25
|
-
const path = fixUnicodePath(entry.path)
|
|
26
|
-
tar.append(path, entry.data)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return tar.out
|
|
30
|
-
}
|