@ethersphere/bee-js 6.5.0 → 6.6.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/utils/data.js +1 -28
- package/dist/cjs/utils/error.js +13 -1
- package/dist/cjs/utils/expose.js +3 -1
- package/dist/cjs/utils/http.js +29 -3
- package/dist/cjs/utils/stamps.js +26 -1
- package/dist/index.browser.min.js +1 -1
- package/dist/index.browser.min.js.map +1 -1
- package/dist/mjs/utils/data.js +0 -25
- package/dist/mjs/utils/error.js +11 -0
- package/dist/mjs/utils/expose.js +1 -1
- package/dist/mjs/utils/http.js +8 -2
- package/dist/mjs/utils/stamps.js +23 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/utils/data.d.ts +1 -11
- package/dist/types/utils/error.d.ts +10 -0
- package/dist/types/utils/expose.d.ts +1 -1
- package/dist/types/utils/http.d.ts +1 -5
- package/dist/types/utils/stamps.d.ts +19 -0
- package/package.json +1 -1
package/dist/mjs/utils/data.js
CHANGED
|
@@ -1,28 +1,3 @@
|
|
|
1
|
-
import BlobPolyfill from 'fetch-blob';
|
|
2
|
-
import { isNodeReadable, isReadableStream, readableWebToNode } from "./stream.js";
|
|
3
|
-
/**
|
|
4
|
-
* Prepare data for valid input for node-fetch.
|
|
5
|
-
*
|
|
6
|
-
* node-fetch is not using WHATWG ReadableStream but NodeJS Readable so we need to convert in case of ReadableStream,
|
|
7
|
-
* but the typings are set to use ReadableStream so hence why type conversion here.
|
|
8
|
-
*
|
|
9
|
-
* @param data any string, ArrayBuffer, Uint8Array or Readable
|
|
10
|
-
*/
|
|
11
|
-
export async function prepareData(data) {
|
|
12
|
-
if (typeof data === 'string') return new BlobPolyfill([data], {
|
|
13
|
-
type: 'text/plain'
|
|
14
|
-
});
|
|
15
|
-
if (data instanceof Uint8Array || data instanceof ArrayBuffer) {
|
|
16
|
-
return new BlobPolyfill([data], {
|
|
17
|
-
type: 'application/octet-stream'
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
if (data instanceof BlobPolyfill || isNodeReadable(data)) return data;
|
|
21
|
-
if (isReadableStream(data)) {
|
|
22
|
-
return readableWebToNode(data);
|
|
23
|
-
}
|
|
24
|
-
throw new TypeError('unknown data type');
|
|
25
|
-
}
|
|
26
1
|
function isBufferArray(buffer) {
|
|
27
2
|
return Array.isArray(buffer) && buffer.length > 0 && buffer.every(data => data instanceof Buffer);
|
|
28
3
|
}
|
package/dist/mjs/utils/error.js
CHANGED
|
@@ -8,4 +8,15 @@ export class BeeArgumentError extends BeeError {
|
|
|
8
8
|
super(message);
|
|
9
9
|
this.value = value;
|
|
10
10
|
}
|
|
11
|
+
}
|
|
12
|
+
export class BeeResponseError extends BeeError {
|
|
13
|
+
constructor(message, code, axiosStatus, status, config, request, response) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.code = code;
|
|
16
|
+
this.axiosStatus = axiosStatus;
|
|
17
|
+
this.status = status;
|
|
18
|
+
this.config = config;
|
|
19
|
+
this.request = request;
|
|
20
|
+
this.response = response;
|
|
21
|
+
}
|
|
11
22
|
}
|
package/dist/mjs/utils/expose.js
CHANGED
|
@@ -6,4 +6,4 @@ export { ethToSwarmAddress, fromLittleEndian, isHexEthAddress, makeEthAddress, m
|
|
|
6
6
|
export { isNodeReadable, isReadable, isReadableStream, normalizeToReadableStream, readableNodeToWeb, readableWebToNode } from "./stream.js";
|
|
7
7
|
export { keccak256Hash } from "./hash.js";
|
|
8
8
|
export { makeMaxTarget } from "./pss.js";
|
|
9
|
-
export { getStampCostInBzz, getStampCostInPlur, getStampMaximumCapacityBytes, getStampTtlSeconds, getStampUsage } from "./stamps.js";
|
|
9
|
+
export { getAmountForTtl, getDepthForCapacity, getStampCostInBzz, getStampCostInPlur, getStampMaximumCapacityBytes, getStampTtlSeconds, getStampUsage } from "./stamps.js";
|
package/dist/mjs/utils/http.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import axios from 'axios';
|
|
1
|
+
import axios, { AxiosError } from 'axios';
|
|
2
2
|
import { Objects, Strings } from 'cafe-utility';
|
|
3
|
+
import { BeeResponseError } from "../index.js";
|
|
3
4
|
export const DEFAULT_HTTP_CONFIG = {
|
|
4
5
|
headers: {
|
|
5
6
|
accept: 'application/json, text/plain, */*'
|
|
6
|
-
}
|
|
7
|
+
},
|
|
8
|
+
maxBodyLength: Infinity,
|
|
9
|
+
maxContentLength: Infinity
|
|
7
10
|
};
|
|
8
11
|
/**
|
|
9
12
|
* Main function to make HTTP requests.
|
|
@@ -17,6 +20,9 @@ export async function http(options, config) {
|
|
|
17
20
|
const response = await axios(requestConfig);
|
|
18
21
|
return response;
|
|
19
22
|
} catch (e) {
|
|
23
|
+
if (e instanceof AxiosError) {
|
|
24
|
+
throw new BeeResponseError(e.message, e.code, e.status, e.response?.status, e.config, e.request, e.response);
|
|
25
|
+
}
|
|
20
26
|
throw e;
|
|
21
27
|
}
|
|
22
28
|
}
|
package/dist/mjs/utils/stamps.js
CHANGED
|
@@ -44,4 +44,27 @@ export function getStampCostInBzz(depth, amount) {
|
|
|
44
44
|
*/
|
|
45
45
|
export function getStampTtlSeconds(amount, pricePerBlock = 24000, blockTime = 5) {
|
|
46
46
|
return amount * blockTime / pricePerBlock;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Utility function that calculates the amount of tokens required to maintain a given Time To Live (TTL) for a postage batch.
|
|
50
|
+
*
|
|
51
|
+
* This function estimates the required amount based on the provided TTL in days.
|
|
52
|
+
*
|
|
53
|
+
* @param {number} days - The Time To Live (TTL) in days.
|
|
54
|
+
* @returns {NumberString} The estimated amount of tokens needed for the specified TTL.
|
|
55
|
+
*/
|
|
56
|
+
export function getAmountForTtl(days) {
|
|
57
|
+
// 414720000 = (24 * 60 * 60 * 24_000) / 5
|
|
58
|
+
return ((days <= 0 ? 1 : days) * 414720000).toString();
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Utility function that calculates the depth required for a postage batch to achieve the specified capacity in gigabytes.
|
|
62
|
+
*
|
|
63
|
+
* The depth is determined based on the given gigabytes, and the result is adjusted to a minimum depth of 18.
|
|
64
|
+
*
|
|
65
|
+
* @param {number} gigabytes - The desired capacity of the postage batch in gigabytes.
|
|
66
|
+
* @returns {number} The calculated depth necessary to achieve the specified capacity.
|
|
67
|
+
*/
|
|
68
|
+
export function getDepthForCapacity(gigabytes) {
|
|
69
|
+
return gigabytes <= 1 ? 18 : Math.ceil(Math.log2(Math.ceil(gigabytes)) + 18);
|
|
47
70
|
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ declare global {
|
|
|
13
13
|
Utils: typeof import('./utils/expose');
|
|
14
14
|
BeeError: typeof import('./utils/error').BeeError;
|
|
15
15
|
BeeArgumentError: typeof import('./utils/error').BeeArgumentError;
|
|
16
|
+
BeeResponseError: typeof import('./utils/error').BeeResponseError;
|
|
16
17
|
};
|
|
17
18
|
}
|
|
18
19
|
}
|
|
@@ -1,13 +1,3 @@
|
|
|
1
|
-
import type { Data } from 'ws';
|
|
2
1
|
import BlobPolyfill from 'fetch-blob';
|
|
3
|
-
import {
|
|
4
|
-
/**
|
|
5
|
-
* Prepare data for valid input for node-fetch.
|
|
6
|
-
*
|
|
7
|
-
* node-fetch is not using WHATWG ReadableStream but NodeJS Readable so we need to convert in case of ReadableStream,
|
|
8
|
-
* but the typings are set to use ReadableStream so hence why type conversion here.
|
|
9
|
-
*
|
|
10
|
-
* @param data any string, ArrayBuffer, Uint8Array or Readable
|
|
11
|
-
*/
|
|
12
|
-
export declare function prepareData(data: string | ArrayBuffer | Uint8Array | Readable): Promise<Blob | ReadableStream<Uint8Array> | never>;
|
|
2
|
+
import type { Data } from 'ws';
|
|
13
3
|
export declare function prepareWebsocketData(data: Data | BlobPolyfill): Promise<Uint8Array> | never;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
1
2
|
export declare class BeeError extends Error {
|
|
2
3
|
constructor(message: string);
|
|
3
4
|
}
|
|
@@ -5,3 +6,12 @@ export declare class BeeArgumentError extends BeeError {
|
|
|
5
6
|
readonly value: unknown;
|
|
6
7
|
constructor(message: string, value: unknown);
|
|
7
8
|
}
|
|
9
|
+
export declare class BeeResponseError extends BeeError {
|
|
10
|
+
code?: string | undefined;
|
|
11
|
+
axiosStatus?: string | undefined;
|
|
12
|
+
status?: number | undefined;
|
|
13
|
+
config?: AxiosRequestConfig<any> | undefined;
|
|
14
|
+
request?: any;
|
|
15
|
+
response?: AxiosResponse<any, any> | undefined;
|
|
16
|
+
constructor(message: string, code?: string | undefined, axiosStatus?: string | undefined, status?: number | undefined, config?: AxiosRequestConfig<any> | undefined, request?: any, response?: AxiosResponse<any, any> | undefined);
|
|
17
|
+
}
|
|
@@ -6,4 +6,4 @@ export { EthAddress, ethToSwarmAddress, fromLittleEndian, isHexEthAddress, makeE
|
|
|
6
6
|
export { isNodeReadable, isReadable, isReadableStream, normalizeToReadableStream, readableNodeToWeb, readableWebToNode, } from './stream';
|
|
7
7
|
export { keccak256Hash } from './hash';
|
|
8
8
|
export { makeMaxTarget } from './pss';
|
|
9
|
-
export { getStampCostInBzz, getStampCostInPlur, getStampMaximumCapacityBytes, getStampTtlSeconds, getStampUsage, } from './stamps';
|
|
9
|
+
export { getAmountForTtl, getDepthForCapacity, getStampCostInBzz, getStampCostInPlur, getStampMaximumCapacityBytes, getStampTtlSeconds, getStampUsage, } from './stamps';
|
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
import { AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
2
2
|
import { BeeRequestOptions } from '../index';
|
|
3
|
-
export declare const DEFAULT_HTTP_CONFIG:
|
|
4
|
-
headers: {
|
|
5
|
-
accept: string;
|
|
6
|
-
};
|
|
7
|
-
};
|
|
3
|
+
export declare const DEFAULT_HTTP_CONFIG: AxiosRequestConfig;
|
|
8
4
|
/**
|
|
9
5
|
* Main function to make HTTP requests.
|
|
10
6
|
* @param options User defined settings
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { NumberString } from '../types';
|
|
1
2
|
/**
|
|
2
3
|
* Utility function that calculates usage of postage batch based on its utilization, depth and bucket depth.
|
|
3
4
|
*
|
|
@@ -34,3 +35,21 @@ export declare function getStampCostInBzz(depth: number, amount: number): number
|
|
|
34
35
|
* @returns {number} The TTL of the postage batch in seconds.
|
|
35
36
|
*/
|
|
36
37
|
export declare function getStampTtlSeconds(amount: number, pricePerBlock?: number, blockTime?: number): number;
|
|
38
|
+
/**
|
|
39
|
+
* Utility function that calculates the amount of tokens required to maintain a given Time To Live (TTL) for a postage batch.
|
|
40
|
+
*
|
|
41
|
+
* This function estimates the required amount based on the provided TTL in days.
|
|
42
|
+
*
|
|
43
|
+
* @param {number} days - The Time To Live (TTL) in days.
|
|
44
|
+
* @returns {NumberString} The estimated amount of tokens needed for the specified TTL.
|
|
45
|
+
*/
|
|
46
|
+
export declare function getAmountForTtl(days: number): NumberString;
|
|
47
|
+
/**
|
|
48
|
+
* Utility function that calculates the depth required for a postage batch to achieve the specified capacity in gigabytes.
|
|
49
|
+
*
|
|
50
|
+
* The depth is determined based on the given gigabytes, and the result is adjusted to a minimum depth of 18.
|
|
51
|
+
*
|
|
52
|
+
* @param {number} gigabytes - The desired capacity of the postage batch in gigabytes.
|
|
53
|
+
* @returns {number} The calculated depth necessary to achieve the specified capacity.
|
|
54
|
+
*/
|
|
55
|
+
export declare function getDepthForCapacity(gigabytes: number): number;
|