@ethersphere/bee-js 6.5.1 → 6.7.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 +4 -1
- package/dist/cjs/utils/http.js +27 -3
- package/dist/cjs/utils/stamps.js +60 -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 +5 -1
- package/dist/mjs/utils/stamps.js +56 -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/stamps.d.ts +28 -0
- package/package.json +1 -1
package/dist/cjs/utils/data.js
CHANGED
|
@@ -1,33 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.prepareWebsocketData =
|
|
7
|
-
const fetch_blob_1 = __importDefault(require("fetch-blob"));
|
|
8
|
-
const stream_1 = require("./stream");
|
|
9
|
-
/**
|
|
10
|
-
* Prepare data for valid input for node-fetch.
|
|
11
|
-
*
|
|
12
|
-
* node-fetch is not using WHATWG ReadableStream but NodeJS Readable so we need to convert in case of ReadableStream,
|
|
13
|
-
* but the typings are set to use ReadableStream so hence why type conversion here.
|
|
14
|
-
*
|
|
15
|
-
* @param data any string, ArrayBuffer, Uint8Array or Readable
|
|
16
|
-
*/
|
|
17
|
-
async function prepareData(data) {
|
|
18
|
-
if (typeof data === 'string')
|
|
19
|
-
return new fetch_blob_1.default([data], { type: 'text/plain' });
|
|
20
|
-
if (data instanceof Uint8Array || data instanceof ArrayBuffer) {
|
|
21
|
-
return new fetch_blob_1.default([data], { type: 'application/octet-stream' });
|
|
22
|
-
}
|
|
23
|
-
if (data instanceof fetch_blob_1.default || (0, stream_1.isNodeReadable)(data))
|
|
24
|
-
return data;
|
|
25
|
-
if ((0, stream_1.isReadableStream)(data)) {
|
|
26
|
-
return (0, stream_1.readableWebToNode)(data);
|
|
27
|
-
}
|
|
28
|
-
throw new TypeError('unknown data type');
|
|
29
|
-
}
|
|
30
|
-
exports.prepareData = prepareData;
|
|
3
|
+
exports.prepareWebsocketData = void 0;
|
|
31
4
|
function isBufferArray(buffer) {
|
|
32
5
|
return Array.isArray(buffer) && buffer.length > 0 && buffer.every(data => data instanceof Buffer);
|
|
33
6
|
}
|
package/dist/cjs/utils/error.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.BeeArgumentError = exports.BeeError = void 0;
|
|
3
|
+
exports.BeeResponseError = exports.BeeArgumentError = exports.BeeError = void 0;
|
|
4
4
|
class BeeError extends Error {
|
|
5
5
|
constructor(message) {
|
|
6
6
|
super(message);
|
|
@@ -14,3 +14,15 @@ class BeeArgumentError extends BeeError {
|
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
exports.BeeArgumentError = BeeArgumentError;
|
|
17
|
+
class BeeResponseError extends BeeError {
|
|
18
|
+
constructor(message, code, axiosStatus, status, config, request, response) {
|
|
19
|
+
super(message);
|
|
20
|
+
this.code = code;
|
|
21
|
+
this.axiosStatus = axiosStatus;
|
|
22
|
+
this.status = status;
|
|
23
|
+
this.config = config;
|
|
24
|
+
this.request = request;
|
|
25
|
+
this.response = response;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.BeeResponseError = BeeResponseError;
|
package/dist/cjs/utils/expose.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getStampUsage = exports.getStampTtlSeconds = exports.getStampMaximumCapacityBytes = exports.getStampCostInPlur = exports.getStampCostInBzz = exports.makeMaxTarget = exports.keccak256Hash = exports.readableWebToNode = exports.readableNodeToWeb = exports.normalizeToReadableStream = exports.isReadableStream = exports.isReadable = exports.isNodeReadable = exports.toLittleEndian = exports.makeHexEthAddress = exports.makeEthereumWalletSigner = exports.makeEthAddress = exports.isHexEthAddress = exports.fromLittleEndian = exports.ethToSwarmAddress = exports.makeHexString = exports.isHexString = exports.intToHex = exports.hexToBytes = exports.bytesToHex = exports.assertPrefixedHexString = exports.assertHexString = exports.isFlexBytes = exports.isBytes = exports.flexBytesAtOffset = exports.bytesEqual = exports.bytesAtOffset = exports.assertFlexBytes = exports.assertBytes = exports.getFolderSize = exports.getCollectionSize = void 0;
|
|
3
|
+
exports.getStampUsage = exports.getStampTtlSeconds = exports.getStampEffectiveBytes = exports.getStampMaximumCapacityBytes = exports.getStampCostInPlur = exports.getStampCostInBzz = exports.getDepthForCapacity = exports.getAmountForTtl = exports.makeMaxTarget = exports.keccak256Hash = exports.readableWebToNode = exports.readableNodeToWeb = exports.normalizeToReadableStream = exports.isReadableStream = exports.isReadable = exports.isNodeReadable = exports.toLittleEndian = exports.makeHexEthAddress = exports.makeEthereumWalletSigner = exports.makeEthAddress = exports.isHexEthAddress = exports.fromLittleEndian = exports.ethToSwarmAddress = exports.makeHexString = exports.isHexString = exports.intToHex = exports.hexToBytes = exports.bytesToHex = exports.assertPrefixedHexString = exports.assertHexString = exports.isFlexBytes = exports.isBytes = exports.flexBytesAtOffset = exports.bytesEqual = exports.bytesAtOffset = exports.assertFlexBytes = exports.assertBytes = exports.getFolderSize = exports.getCollectionSize = void 0;
|
|
4
4
|
var collection_1 = require("./collection");
|
|
5
5
|
Object.defineProperty(exports, "getCollectionSize", { enumerable: true, get: function () { return collection_1.getCollectionSize; } });
|
|
6
6
|
var collection_node_1 = require("./collection.node");
|
|
@@ -41,8 +41,11 @@ Object.defineProperty(exports, "keccak256Hash", { enumerable: true, get: functio
|
|
|
41
41
|
var pss_1 = require("./pss");
|
|
42
42
|
Object.defineProperty(exports, "makeMaxTarget", { enumerable: true, get: function () { return pss_1.makeMaxTarget; } });
|
|
43
43
|
var stamps_1 = require("./stamps");
|
|
44
|
+
Object.defineProperty(exports, "getAmountForTtl", { enumerable: true, get: function () { return stamps_1.getAmountForTtl; } });
|
|
45
|
+
Object.defineProperty(exports, "getDepthForCapacity", { enumerable: true, get: function () { return stamps_1.getDepthForCapacity; } });
|
|
44
46
|
Object.defineProperty(exports, "getStampCostInBzz", { enumerable: true, get: function () { return stamps_1.getStampCostInBzz; } });
|
|
45
47
|
Object.defineProperty(exports, "getStampCostInPlur", { enumerable: true, get: function () { return stamps_1.getStampCostInPlur; } });
|
|
46
48
|
Object.defineProperty(exports, "getStampMaximumCapacityBytes", { enumerable: true, get: function () { return stamps_1.getStampMaximumCapacityBytes; } });
|
|
49
|
+
Object.defineProperty(exports, "getStampEffectiveBytes", { enumerable: true, get: function () { return stamps_1.getStampEffectiveBytes; } });
|
|
47
50
|
Object.defineProperty(exports, "getStampTtlSeconds", { enumerable: true, get: function () { return stamps_1.getStampTtlSeconds; } });
|
|
48
51
|
Object.defineProperty(exports, "getStampUsage", { enumerable: true, get: function () { return stamps_1.getStampUsage; } });
|
package/dist/cjs/utils/http.js
CHANGED
|
@@ -1,11 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
4
24
|
};
|
|
5
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
26
|
exports.http = exports.DEFAULT_HTTP_CONFIG = void 0;
|
|
7
|
-
const axios_1 =
|
|
27
|
+
const axios_1 = __importStar(require("axios"));
|
|
8
28
|
const cafe_utility_1 = require("cafe-utility");
|
|
29
|
+
const index_1 = require("../index");
|
|
9
30
|
exports.DEFAULT_HTTP_CONFIG = {
|
|
10
31
|
headers: {
|
|
11
32
|
accept: 'application/json, text/plain, */*',
|
|
@@ -26,6 +47,9 @@ async function http(options, config) {
|
|
|
26
47
|
return response;
|
|
27
48
|
}
|
|
28
49
|
catch (e) {
|
|
50
|
+
if (e instanceof axios_1.AxiosError) {
|
|
51
|
+
throw new index_1.BeeResponseError(e.message, e.code, e.status, e.response?.status, e.config, e.request, e.response);
|
|
52
|
+
}
|
|
29
53
|
throw e;
|
|
30
54
|
}
|
|
31
55
|
}
|
package/dist/cjs/utils/stamps.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getStampTtlSeconds = exports.getStampCostInBzz = exports.getStampCostInPlur = exports.getStampMaximumCapacityBytes = exports.getStampUsage = void 0;
|
|
3
|
+
exports.getDepthForCapacity = exports.getAmountForTtl = exports.getStampTtlSeconds = exports.getStampCostInBzz = exports.getStampCostInPlur = exports.getStampEffectiveBytes = exports.getStampMaximumCapacityBytes = exports.getStampUsage = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* Utility function that calculates usage of postage batch based on its utilization, depth and bucket depth.
|
|
6
6
|
*
|
|
@@ -23,6 +23,40 @@ function getStampMaximumCapacityBytes(depth) {
|
|
|
23
23
|
return 2 ** depth * 4096;
|
|
24
24
|
}
|
|
25
25
|
exports.getStampMaximumCapacityBytes = getStampMaximumCapacityBytes;
|
|
26
|
+
/**
|
|
27
|
+
* Based on https://docs.ethswarm.org/docs/learn/technology/contracts/postage-stamp/#effective-utilisation-table
|
|
28
|
+
*/
|
|
29
|
+
const utilisationRateMap = {
|
|
30
|
+
22: 0.2867,
|
|
31
|
+
23: 0.4956,
|
|
32
|
+
24: 0.6433,
|
|
33
|
+
25: 0.7478,
|
|
34
|
+
26: 0.8217,
|
|
35
|
+
27: 0.8739,
|
|
36
|
+
28: 0.9108,
|
|
37
|
+
29: 0.9369,
|
|
38
|
+
30: 0.9554,
|
|
39
|
+
31: 0.9685,
|
|
40
|
+
32: 0.9777,
|
|
41
|
+
33: 0.9842,
|
|
42
|
+
34: 0.9889,
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Utility function that calculates the effective volume of a postage batch based on its depth.
|
|
46
|
+
*
|
|
47
|
+
* Below 22 depth the effective volume is 0
|
|
48
|
+
* Above 34 it's always > 99%
|
|
49
|
+
*
|
|
50
|
+
* @returns {number} The effective volume of the postage batch in bytes.
|
|
51
|
+
*/
|
|
52
|
+
function getStampEffectiveBytes(depth) {
|
|
53
|
+
if (depth < 22) {
|
|
54
|
+
return 0;
|
|
55
|
+
}
|
|
56
|
+
const utilRate = utilisationRateMap[depth] ?? 0.99;
|
|
57
|
+
return getStampMaximumCapacityBytes(depth) * utilRate;
|
|
58
|
+
}
|
|
59
|
+
exports.getStampEffectiveBytes = getStampEffectiveBytes;
|
|
26
60
|
/**
|
|
27
61
|
* Utility function that calculates the cost of a postage batch based on its depth and amount.
|
|
28
62
|
*
|
|
@@ -53,3 +87,28 @@ function getStampTtlSeconds(amount, pricePerBlock = 24000, blockTime = 5) {
|
|
|
53
87
|
return (amount * blockTime) / pricePerBlock;
|
|
54
88
|
}
|
|
55
89
|
exports.getStampTtlSeconds = getStampTtlSeconds;
|
|
90
|
+
/**
|
|
91
|
+
* Utility function that calculates the amount of tokens required to maintain a given Time To Live (TTL) for a postage batch.
|
|
92
|
+
*
|
|
93
|
+
* This function estimates the required amount based on the provided TTL in days.
|
|
94
|
+
*
|
|
95
|
+
* @param {number} days - The Time To Live (TTL) in days.
|
|
96
|
+
* @returns {NumberString} The estimated amount of tokens needed for the specified TTL.
|
|
97
|
+
*/
|
|
98
|
+
function getAmountForTtl(days) {
|
|
99
|
+
// 414720000 = (24 * 60 * 60 * 24_000) / 5
|
|
100
|
+
return ((days <= 0 ? 1 : days) * 414720000).toString();
|
|
101
|
+
}
|
|
102
|
+
exports.getAmountForTtl = getAmountForTtl;
|
|
103
|
+
/**
|
|
104
|
+
* Utility function that calculates the depth required for a postage batch to achieve the specified capacity in gigabytes.
|
|
105
|
+
*
|
|
106
|
+
* The depth is determined based on the given gigabytes, and the result is adjusted to a minimum depth of 18.
|
|
107
|
+
*
|
|
108
|
+
* @param {number} gigabytes - The desired capacity of the postage batch in gigabytes.
|
|
109
|
+
* @returns {number} The calculated depth necessary to achieve the specified capacity.
|
|
110
|
+
*/
|
|
111
|
+
function getDepthForCapacity(gigabytes) {
|
|
112
|
+
return gigabytes <= 1 ? 18 : Math.ceil(Math.log2(Math.ceil(gigabytes)) + 18);
|
|
113
|
+
}
|
|
114
|
+
exports.getDepthForCapacity = getDepthForCapacity;
|