@ethersphere/bee-js 6.8.1 → 6.9.1
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/feed/index.js +2 -2
- package/dist/cjs/modules/bytes.js +5 -3
- package/dist/cjs/modules/bzz.js +12 -7
- package/dist/cjs/types/index.js +30 -1
- package/dist/cjs/utils/expose.js +6 -2
- package/dist/cjs/utils/headers.js +31 -5
- package/dist/cjs/utils/redundancy.js +130 -0
- package/dist/index.browser.min.js +1 -1
- package/dist/index.browser.min.js.map +1 -1
- package/dist/mjs/feed/index.js +2 -2
- package/dist/mjs/modules/bytes.js +8 -6
- package/dist/mjs/modules/bzz.js +18 -10
- package/dist/mjs/types/index.js +29 -0
- package/dist/mjs/utils/expose.js +2 -1
- package/dist/mjs/utils/headers.js +32 -4
- package/dist/mjs/utils/redundancy.js +88 -0
- package/dist/types/bee.d.ts +6 -6
- package/dist/types/feed/index.d.ts +1 -1
- package/dist/types/modules/bytes.d.ts +4 -4
- package/dist/types/modules/bzz.d.ts +5 -5
- package/dist/types/types/index.d.ts +44 -0
- package/dist/types/utils/expose.d.ts +2 -1
- package/dist/types/utils/headers.d.ts +3 -1
- package/dist/types/utils/redundancy.d.ts +21 -0
- package/package.json +1 -1
package/dist/cjs/feed/index.js
CHANGED
|
@@ -53,9 +53,9 @@ async function findNextIndex(requestOptions, owner, topic, options) {
|
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
55
|
exports.findNextIndex = findNextIndex;
|
|
56
|
-
async function updateFeed(requestOptions, signer, topic, reference, postageBatchId, options
|
|
56
|
+
async function updateFeed(requestOptions, signer, topic, reference, postageBatchId, options) {
|
|
57
57
|
const ownerHex = (0, eth_1.makeHexEthAddress)(signer.address);
|
|
58
|
-
const nextIndex = index
|
|
58
|
+
const nextIndex = options?.index || (await findNextIndex(requestOptions, ownerHex, topic, options));
|
|
59
59
|
const identifier = (0, identifier_1.makeFeedIdentifier)(topic, nextIndex);
|
|
60
60
|
const at = options?.at ?? Date.now() / 1000.0;
|
|
61
61
|
const timestamp = (0, uint64_1.writeUint64BigEndian)(at);
|
|
@@ -22,7 +22,7 @@ async function upload(requestOptions, data, postageBatchId, options) {
|
|
|
22
22
|
data,
|
|
23
23
|
headers: {
|
|
24
24
|
'content-type': 'application/octet-stream',
|
|
25
|
-
...(0, headers_1.
|
|
25
|
+
...(0, headers_1.extractRedundantUploadHeaders)(postageBatchId, options),
|
|
26
26
|
},
|
|
27
27
|
});
|
|
28
28
|
return {
|
|
@@ -37,10 +37,11 @@ exports.upload = upload;
|
|
|
37
37
|
* @param ky
|
|
38
38
|
* @param hash Bee content reference
|
|
39
39
|
*/
|
|
40
|
-
async function download(requestOptions, hash) {
|
|
40
|
+
async function download(requestOptions, hash, options) {
|
|
41
41
|
const response = await (0, http_1.http)(requestOptions, {
|
|
42
42
|
responseType: 'arraybuffer',
|
|
43
43
|
url: `${endpoint}/${hash}`,
|
|
44
|
+
headers: (0, headers_1.extractDownloadHeaders)(options),
|
|
44
45
|
});
|
|
45
46
|
return (0, bytes_1.wrapBytesWithHelpers)(new Uint8Array(response.data));
|
|
46
47
|
}
|
|
@@ -51,10 +52,11 @@ exports.download = download;
|
|
|
51
52
|
* @param ky
|
|
52
53
|
* @param hash Bee content reference
|
|
53
54
|
*/
|
|
54
|
-
async function downloadReadable(requestOptions, hash) {
|
|
55
|
+
async function downloadReadable(requestOptions, hash, options) {
|
|
55
56
|
const response = await (0, http_1.http)(requestOptions, {
|
|
56
57
|
responseType: 'stream',
|
|
57
58
|
url: `${endpoint}/${hash}`,
|
|
59
|
+
headers: (0, headers_1.extractDownloadHeaders)(options),
|
|
58
60
|
});
|
|
59
61
|
return response.data;
|
|
60
62
|
}
|
package/dist/cjs/modules/bzz.js
CHANGED
|
@@ -10,7 +10,7 @@ const tar_1 = require("../utils/tar");
|
|
|
10
10
|
const type_1 = require("../utils/type");
|
|
11
11
|
const bzzEndpoint = 'bzz';
|
|
12
12
|
function extractFileUploadHeaders(postageBatchId, options) {
|
|
13
|
-
const headers = (0, headers_1.
|
|
13
|
+
const headers = (0, headers_1.extractRedundantUploadHeaders)(postageBatchId, options);
|
|
14
14
|
if (options?.size)
|
|
15
15
|
headers['content-length'] = String(options.size);
|
|
16
16
|
if (options?.contentType)
|
|
@@ -28,8 +28,9 @@ function extractFileUploadHeaders(postageBatchId, options) {
|
|
|
28
28
|
*/
|
|
29
29
|
async function uploadFile(requestOptions, data, postageBatchId, name, options) {
|
|
30
30
|
if ((0, stream_1.isReadable)(data) && !options?.contentType) {
|
|
31
|
-
if (!options)
|
|
31
|
+
if (!options) {
|
|
32
32
|
options = {};
|
|
33
|
+
}
|
|
33
34
|
options.contentType = 'application/octet-stream';
|
|
34
35
|
}
|
|
35
36
|
const response = await (0, http_1.http)(requestOptions, {
|
|
@@ -55,11 +56,12 @@ exports.uploadFile = uploadFile;
|
|
|
55
56
|
* @param hash Bee file or collection hash
|
|
56
57
|
* @param path If hash is collection then this defines path to a single file in the collection
|
|
57
58
|
*/
|
|
58
|
-
async function downloadFile(requestOptions, hash, path = '') {
|
|
59
|
+
async function downloadFile(requestOptions, hash, path = '', options) {
|
|
59
60
|
const response = await (0, http_1.http)(requestOptions, {
|
|
60
61
|
method: 'GET',
|
|
61
62
|
responseType: 'arraybuffer',
|
|
62
63
|
url: `${bzzEndpoint}/${hash}/${path}`,
|
|
64
|
+
headers: (0, headers_1.extractDownloadHeaders)(options),
|
|
63
65
|
});
|
|
64
66
|
const file = {
|
|
65
67
|
...(0, headers_1.readFileHeaders)(response.headers),
|
|
@@ -75,11 +77,12 @@ exports.downloadFile = downloadFile;
|
|
|
75
77
|
* @param hash Bee file or collection hash
|
|
76
78
|
* @param path If hash is collection then this defines path to a single file in the collection
|
|
77
79
|
*/
|
|
78
|
-
async function downloadFileReadable(requestOptions, hash, path = '') {
|
|
80
|
+
async function downloadFileReadable(requestOptions, hash, path = '', options) {
|
|
79
81
|
const response = await (0, http_1.http)(requestOptions, {
|
|
80
82
|
method: 'GET',
|
|
81
83
|
responseType: 'stream',
|
|
82
84
|
url: `${bzzEndpoint}/${hash}/${path}`,
|
|
85
|
+
headers: (0, headers_1.extractDownloadHeaders)(options),
|
|
83
86
|
});
|
|
84
87
|
const file = {
|
|
85
88
|
...(0, headers_1.readFileHeaders)(response.headers),
|
|
@@ -89,11 +92,13 @@ async function downloadFileReadable(requestOptions, hash, path = '') {
|
|
|
89
92
|
}
|
|
90
93
|
exports.downloadFileReadable = downloadFileReadable;
|
|
91
94
|
function extractCollectionUploadHeaders(postageBatchId, options) {
|
|
92
|
-
const headers = (0, headers_1.
|
|
93
|
-
if (options?.indexDocument)
|
|
95
|
+
const headers = (0, headers_1.extractRedundantUploadHeaders)(postageBatchId, options);
|
|
96
|
+
if (options?.indexDocument) {
|
|
94
97
|
headers['swarm-index-document'] = options.indexDocument;
|
|
95
|
-
|
|
98
|
+
}
|
|
99
|
+
if (options?.errorDocument) {
|
|
96
100
|
headers['swarm-error-document'] = options.errorDocument;
|
|
101
|
+
}
|
|
97
102
|
return headers;
|
|
98
103
|
}
|
|
99
104
|
/**
|
package/dist/cjs/types/index.js
CHANGED
|
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.SIGNATURE_BYTES_LENGTH = exports.SIGNATURE_HEX_LENGTH = exports.TOPIC_HEX_LENGTH = exports.TOPIC_BYTES_LENGTH = exports.FEED_INDEX_HEX_LENGTH = exports.TAGS_LIMIT_MAX = exports.TAGS_LIMIT_MIN = exports.STAMPS_DEPTH_MAX = exports.STAMPS_DEPTH_MIN = exports.ENCRYPTED_REFERENCE_BYTES_LENGTH = exports.REFERENCE_BYTES_LENGTH = exports.ENCRYPTED_REFERENCE_HEX_LENGTH = exports.REFERENCE_HEX_LENGTH = exports.BATCH_ID_HEX_LENGTH = exports.PUBKEY_HEX_LENGTH = exports.PSS_TARGET_HEX_LENGTH_MAX = exports.ADDRESS_HEX_LENGTH = exports.CHUNK_SIZE = exports.BRANCHES = exports.SECTION_SIZE = exports.SPAN_SIZE = void 0;
|
|
17
|
+
exports.SIGNATURE_BYTES_LENGTH = exports.SIGNATURE_HEX_LENGTH = exports.TOPIC_HEX_LENGTH = exports.TOPIC_BYTES_LENGTH = exports.RedundancyStrategy = exports.RedundancyLevel = exports.FEED_INDEX_HEX_LENGTH = exports.TAGS_LIMIT_MAX = exports.TAGS_LIMIT_MIN = exports.STAMPS_DEPTH_MAX = exports.STAMPS_DEPTH_MIN = exports.ENCRYPTED_REFERENCE_BYTES_LENGTH = exports.REFERENCE_BYTES_LENGTH = exports.ENCRYPTED_REFERENCE_HEX_LENGTH = exports.REFERENCE_HEX_LENGTH = exports.BATCH_ID_HEX_LENGTH = exports.PUBKEY_HEX_LENGTH = exports.PSS_TARGET_HEX_LENGTH_MAX = exports.ADDRESS_HEX_LENGTH = exports.CHUNK_SIZE = exports.BRANCHES = exports.SECTION_SIZE = exports.SPAN_SIZE = void 0;
|
|
18
18
|
__exportStar(require("./debug"), exports);
|
|
19
19
|
exports.SPAN_SIZE = 8;
|
|
20
20
|
exports.SECTION_SIZE = 32;
|
|
@@ -39,6 +39,35 @@ exports.STAMPS_DEPTH_MAX = 255;
|
|
|
39
39
|
exports.TAGS_LIMIT_MIN = 1;
|
|
40
40
|
exports.TAGS_LIMIT_MAX = 1000;
|
|
41
41
|
exports.FEED_INDEX_HEX_LENGTH = 16;
|
|
42
|
+
/**
|
|
43
|
+
* Add redundancy to the data being uploaded so that downloaders can download it with better UX.
|
|
44
|
+
* 0 value is default and does not add any redundancy to the file.
|
|
45
|
+
*/
|
|
46
|
+
var RedundancyLevel;
|
|
47
|
+
(function (RedundancyLevel) {
|
|
48
|
+
RedundancyLevel[RedundancyLevel["OFF"] = 0] = "OFF";
|
|
49
|
+
RedundancyLevel[RedundancyLevel["MEDIUM"] = 1] = "MEDIUM";
|
|
50
|
+
RedundancyLevel[RedundancyLevel["STRONG"] = 2] = "STRONG";
|
|
51
|
+
RedundancyLevel[RedundancyLevel["INSANE"] = 3] = "INSANE";
|
|
52
|
+
RedundancyLevel[RedundancyLevel["PARANOID"] = 4] = "PARANOID";
|
|
53
|
+
})(RedundancyLevel = exports.RedundancyLevel || (exports.RedundancyLevel = {}));
|
|
54
|
+
/**
|
|
55
|
+
* Specify the retrieve strategy on redundant data.
|
|
56
|
+
* The possible values are NONE, DATA, PROX and RACE.
|
|
57
|
+
* Strategy NONE means no prefetching takes place.
|
|
58
|
+
* Strategy DATA means only data chunks are prefetched.
|
|
59
|
+
* Strategy PROX means only chunks that are close to the node are prefetched.
|
|
60
|
+
* Strategy RACE means all chunks are prefetched: n data chunks and k parity chunks. The first n chunks to arrive are used to reconstruct the file.
|
|
61
|
+
* Multiple strategies can be used in a fallback cascade if the swarm redundancy fallback mode is set to true.
|
|
62
|
+
* The default strategy is NONE, DATA, falling back to PROX, falling back to RACE
|
|
63
|
+
*/
|
|
64
|
+
var RedundancyStrategy;
|
|
65
|
+
(function (RedundancyStrategy) {
|
|
66
|
+
RedundancyStrategy[RedundancyStrategy["NONE"] = 0] = "NONE";
|
|
67
|
+
RedundancyStrategy[RedundancyStrategy["DATA"] = 1] = "DATA";
|
|
68
|
+
RedundancyStrategy[RedundancyStrategy["PROX"] = 2] = "PROX";
|
|
69
|
+
RedundancyStrategy[RedundancyStrategy["RACE"] = 3] = "RACE";
|
|
70
|
+
})(RedundancyStrategy = exports.RedundancyStrategy || (exports.RedundancyStrategy = {}));
|
|
42
71
|
/*********************************************************
|
|
43
72
|
* Writers and Readers interfaces
|
|
44
73
|
*/
|
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.
|
|
3
|
+
exports.getRedundancyStats = exports.getRedundancyStat = exports.approximateOverheadForRedundancyLevel = exports.getStampUsage = exports.getStampTtlSeconds = exports.getStampMaximumCapacityBytes = exports.getStampEffectiveBytes = 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");
|
|
@@ -45,7 +45,11 @@ Object.defineProperty(exports, "getAmountForTtl", { enumerable: true, get: funct
|
|
|
45
45
|
Object.defineProperty(exports, "getDepthForCapacity", { enumerable: true, get: function () { return stamps_1.getDepthForCapacity; } });
|
|
46
46
|
Object.defineProperty(exports, "getStampCostInBzz", { enumerable: true, get: function () { return stamps_1.getStampCostInBzz; } });
|
|
47
47
|
Object.defineProperty(exports, "getStampCostInPlur", { enumerable: true, get: function () { return stamps_1.getStampCostInPlur; } });
|
|
48
|
-
Object.defineProperty(exports, "getStampMaximumCapacityBytes", { enumerable: true, get: function () { return stamps_1.getStampMaximumCapacityBytes; } });
|
|
49
48
|
Object.defineProperty(exports, "getStampEffectiveBytes", { enumerable: true, get: function () { return stamps_1.getStampEffectiveBytes; } });
|
|
49
|
+
Object.defineProperty(exports, "getStampMaximumCapacityBytes", { enumerable: true, get: function () { return stamps_1.getStampMaximumCapacityBytes; } });
|
|
50
50
|
Object.defineProperty(exports, "getStampTtlSeconds", { enumerable: true, get: function () { return stamps_1.getStampTtlSeconds; } });
|
|
51
51
|
Object.defineProperty(exports, "getStampUsage", { enumerable: true, get: function () { return stamps_1.getStampUsage; } });
|
|
52
|
+
var redundancy_1 = require("./redundancy");
|
|
53
|
+
Object.defineProperty(exports, "approximateOverheadForRedundancyLevel", { enumerable: true, get: function () { return redundancy_1.approximateOverheadForRedundancyLevel; } });
|
|
54
|
+
Object.defineProperty(exports, "getRedundancyStat", { enumerable: true, get: function () { return redundancy_1.getRedundancyStat; } });
|
|
55
|
+
Object.defineProperty(exports, "getRedundancyStats", { enumerable: true, get: function () { return redundancy_1.getRedundancyStats; } });
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.extractUploadHeaders = exports.readFileHeaders = void 0;
|
|
3
|
+
exports.extractDownloadHeaders = exports.extractRedundantUploadHeaders = exports.extractUploadHeaders = exports.readFileHeaders = void 0;
|
|
4
4
|
const error_1 = require("./error");
|
|
5
5
|
/**
|
|
6
6
|
* Read the filename from the content-disposition header
|
|
@@ -46,14 +46,40 @@ function extractUploadHeaders(postageBatchId, options) {
|
|
|
46
46
|
const headers = {
|
|
47
47
|
'swarm-postage-batch-id': postageBatchId,
|
|
48
48
|
};
|
|
49
|
-
if (options?.pin)
|
|
49
|
+
if (options?.pin) {
|
|
50
50
|
headers['swarm-pin'] = String(options.pin);
|
|
51
|
-
|
|
51
|
+
}
|
|
52
|
+
if (options?.encrypt) {
|
|
52
53
|
headers['swarm-encrypt'] = String(options.encrypt);
|
|
53
|
-
|
|
54
|
+
}
|
|
55
|
+
if (options?.tag) {
|
|
54
56
|
headers['swarm-tag'] = String(options.tag);
|
|
55
|
-
|
|
57
|
+
}
|
|
58
|
+
if (typeof options?.deferred === 'boolean') {
|
|
56
59
|
headers['swarm-deferred-upload'] = options.deferred.toString();
|
|
60
|
+
}
|
|
57
61
|
return headers;
|
|
58
62
|
}
|
|
59
63
|
exports.extractUploadHeaders = extractUploadHeaders;
|
|
64
|
+
function extractRedundantUploadHeaders(postageBatchId, options) {
|
|
65
|
+
const headers = extractUploadHeaders(postageBatchId, options);
|
|
66
|
+
if (options?.redundancyLevel) {
|
|
67
|
+
headers['swarm-redundancy-level'] = String(options.redundancyLevel);
|
|
68
|
+
}
|
|
69
|
+
return headers;
|
|
70
|
+
}
|
|
71
|
+
exports.extractRedundantUploadHeaders = extractRedundantUploadHeaders;
|
|
72
|
+
function extractDownloadHeaders(options) {
|
|
73
|
+
const headers = {};
|
|
74
|
+
if (options?.redundancyStrategy) {
|
|
75
|
+
headers['swarm-redundancy-strategy'] = String(options.redundancyStrategy);
|
|
76
|
+
}
|
|
77
|
+
if (options?.fallback === false) {
|
|
78
|
+
headers['swarm-redundancy-fallback-mode'] = 'false';
|
|
79
|
+
}
|
|
80
|
+
if (options?.timeoutMs !== undefined) {
|
|
81
|
+
headers['swarm-chunk-retrieval-timeout'] = String(options.timeoutMs);
|
|
82
|
+
}
|
|
83
|
+
return headers;
|
|
84
|
+
}
|
|
85
|
+
exports.extractDownloadHeaders = extractDownloadHeaders;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getRedundancyStat = exports.getRedundancyStats = exports.approximateOverheadForRedundancyLevel = void 0;
|
|
4
|
+
const __1 = require("..");
|
|
5
|
+
const mediumTable = [
|
|
6
|
+
[94, 68, 46, 28, 14, 5, 1],
|
|
7
|
+
[9, 8, 7, 6, 5, 4, 3],
|
|
8
|
+
];
|
|
9
|
+
const encMediumTable = [
|
|
10
|
+
[47, 34, 23, 14, 7, 2],
|
|
11
|
+
[9, 8, 7, 6, 5, 4],
|
|
12
|
+
];
|
|
13
|
+
const strongTable = [
|
|
14
|
+
[104, 95, 86, 77, 69, 61, 53, 46, 39, 32, 26, 20, 15, 10, 6, 3, 1],
|
|
15
|
+
[21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5],
|
|
16
|
+
];
|
|
17
|
+
const encStrongTable = [
|
|
18
|
+
[52, 47, 43, 38, 34, 30, 26, 23, 19, 16, 13, 10, 7, 5, 3, 1],
|
|
19
|
+
[21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6],
|
|
20
|
+
];
|
|
21
|
+
const insaneTable = [
|
|
22
|
+
[92, 87, 82, 77, 73, 68, 63, 59, 54, 50, 45, 41, 37, 33, 29, 26, 22, 19, 16, 13, 10, 8, 5, 3, 2, 1],
|
|
23
|
+
[31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6],
|
|
24
|
+
];
|
|
25
|
+
const encInsaneTable = [
|
|
26
|
+
[46, 43, 41, 38, 36, 34, 31, 29, 27, 25, 22, 20, 18, 16, 14, 13, 11, 9, 8, 6, 5, 4, 2, 1],
|
|
27
|
+
[31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 7],
|
|
28
|
+
];
|
|
29
|
+
const paranoidTable = [
|
|
30
|
+
[
|
|
31
|
+
37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9,
|
|
32
|
+
8, 7, 6, 5, 4, 3, 2, 1,
|
|
33
|
+
],
|
|
34
|
+
[
|
|
35
|
+
90, 88, 87, 85, 84, 82, 81, 79, 77, 76, 74, 72, 71, 69, 67, 66, 64, 62, 60, 59, 57, 55, 53, 51, 49, 48, 46, 44, 41,
|
|
36
|
+
39, 37, 35, 32, 30, 27, 24, 20,
|
|
37
|
+
],
|
|
38
|
+
];
|
|
39
|
+
const encParanoidTable = [
|
|
40
|
+
[18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
|
|
41
|
+
[88, 85, 82, 79, 76, 72, 69, 66, 62, 59, 55, 51, 48, 44, 39, 35, 30, 24],
|
|
42
|
+
];
|
|
43
|
+
const tables = {
|
|
44
|
+
[__1.RedundancyLevel.MEDIUM]: [mediumTable, encMediumTable],
|
|
45
|
+
[__1.RedundancyLevel.STRONG]: [strongTable, encStrongTable],
|
|
46
|
+
[__1.RedundancyLevel.INSANE]: [insaneTable, encInsaneTable],
|
|
47
|
+
[__1.RedundancyLevel.PARANOID]: [paranoidTable, encParanoidTable],
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Returns an approximate multiplier for the overhead of a given redundancy level.
|
|
51
|
+
* Redundancy level is a tradeoff between storage overhead and fault tolerance.
|
|
52
|
+
* Use this number to estimate the amount of chunks that will be stored for a given
|
|
53
|
+
* redundancy level.
|
|
54
|
+
*/
|
|
55
|
+
function approximateOverheadForRedundancyLevel(chunks, level, encrypted) {
|
|
56
|
+
const tableType = level === __1.RedundancyLevel.MEDIUM
|
|
57
|
+
? tables[__1.RedundancyLevel.MEDIUM]
|
|
58
|
+
: level === __1.RedundancyLevel.STRONG
|
|
59
|
+
? tables[__1.RedundancyLevel.STRONG]
|
|
60
|
+
: level === __1.RedundancyLevel.INSANE
|
|
61
|
+
? tables[__1.RedundancyLevel.INSANE]
|
|
62
|
+
: tables[__1.RedundancyLevel.PARANOID];
|
|
63
|
+
const table = encrypted ? tableType[1] : tableType[0];
|
|
64
|
+
const [supportedChunks, parities] = table;
|
|
65
|
+
for (let i = 0; i < supportedChunks.length; i++) {
|
|
66
|
+
if (chunks >= supportedChunks[i]) {
|
|
67
|
+
return parities[i] / supportedChunks[i];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return parities[parities.length - 1] / supportedChunks[supportedChunks.length - 1];
|
|
71
|
+
}
|
|
72
|
+
exports.approximateOverheadForRedundancyLevel = approximateOverheadForRedundancyLevel;
|
|
73
|
+
const medium = {
|
|
74
|
+
label: 'medium',
|
|
75
|
+
value: __1.RedundancyLevel.MEDIUM,
|
|
76
|
+
errorTolerance: 0.01,
|
|
77
|
+
};
|
|
78
|
+
const strong = {
|
|
79
|
+
label: 'strong',
|
|
80
|
+
value: __1.RedundancyLevel.STRONG,
|
|
81
|
+
errorTolerance: 0.05,
|
|
82
|
+
};
|
|
83
|
+
const insane = {
|
|
84
|
+
label: 'insane',
|
|
85
|
+
value: __1.RedundancyLevel.INSANE,
|
|
86
|
+
errorTolerance: 0.1,
|
|
87
|
+
};
|
|
88
|
+
const paranoid = {
|
|
89
|
+
label: 'paranoid',
|
|
90
|
+
value: __1.RedundancyLevel.PARANOID,
|
|
91
|
+
errorTolerance: 0.5,
|
|
92
|
+
};
|
|
93
|
+
function getRedundancyStats() {
|
|
94
|
+
return {
|
|
95
|
+
medium,
|
|
96
|
+
strong,
|
|
97
|
+
insane,
|
|
98
|
+
paranoid,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
exports.getRedundancyStats = getRedundancyStats;
|
|
102
|
+
function getRedundancyStat(level) {
|
|
103
|
+
if (typeof level === 'string') {
|
|
104
|
+
switch (level.toLowerCase()) {
|
|
105
|
+
case 'medium':
|
|
106
|
+
return medium;
|
|
107
|
+
case 'strong':
|
|
108
|
+
return strong;
|
|
109
|
+
case 'insane':
|
|
110
|
+
return insane;
|
|
111
|
+
case 'paranoid':
|
|
112
|
+
return paranoid;
|
|
113
|
+
default:
|
|
114
|
+
throw new Error(`Unknown redundancy level '${level}'`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
switch (level) {
|
|
118
|
+
case __1.RedundancyLevel.MEDIUM:
|
|
119
|
+
return medium;
|
|
120
|
+
case __1.RedundancyLevel.STRONG:
|
|
121
|
+
return strong;
|
|
122
|
+
case __1.RedundancyLevel.INSANE:
|
|
123
|
+
return insane;
|
|
124
|
+
case __1.RedundancyLevel.PARANOID:
|
|
125
|
+
return paranoid;
|
|
126
|
+
default:
|
|
127
|
+
throw new Error(`Unknown redundancy level '${level}'`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
exports.getRedundancyStat = getRedundancyStat;
|