@ethersphere/bee-js 3.3.0 → 3.3.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/chunk/bmt.js +7 -2
- package/dist/cjs/chunk/signer.js +9 -4
- package/dist/cjs/modules/debug/status.js +5 -5
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/types/ky-options.js +8 -0
- package/dist/cjs/types/ky-universal/common.js +8 -0
- package/dist/cjs/types/ky-universal/hooks.js +8 -0
- package/dist/cjs/types/ky-universal/retry.js +8 -0
- package/dist/cjs/utils/eth.js +8 -3
- package/dist/cjs/utils/hash.js +7 -2
- package/dist/cjs/utils/stream.js +14 -5
- package/dist/index.browser.min.js +3 -0
- package/dist/index.browser.min.js.LICENSE.txt +52 -0
- package/dist/index.browser.min.js.map +1 -0
- package/dist/mjs/bee-debug.js +608 -508
- package/dist/mjs/bee.js +935 -836
- package/dist/mjs/chunk/bmt.js +34 -24
- package/dist/mjs/chunk/cac.js +24 -22
- package/dist/mjs/chunk/serialize.js +9 -9
- package/dist/mjs/chunk/signer.js +92 -73
- package/dist/mjs/chunk/soc.js +78 -66
- package/dist/mjs/chunk/span.js +19 -16
- package/dist/mjs/feed/index.js +110 -101
- package/dist/mjs/feed/json.js +21 -17
- package/dist/mjs/feed/topic.js +18 -17
- package/dist/mjs/feed/type.js +5 -5
- package/dist/mjs/index.js +7 -7
- package/dist/mjs/modules/bytes.js +33 -30
- package/dist/mjs/modules/bzz.js +80 -77
- package/dist/mjs/modules/chunk.js +22 -20
- package/dist/mjs/modules/debug/balance.js +26 -22
- package/dist/mjs/modules/debug/chequebook.js +84 -65
- package/dist/mjs/modules/debug/chunk.js +15 -13
- package/dist/mjs/modules/debug/connectivity.js +34 -34
- package/dist/mjs/modules/debug/settlements.js +14 -12
- package/dist/mjs/modules/debug/stamps.js +52 -47
- package/dist/mjs/modules/debug/states.js +16 -14
- package/dist/mjs/modules/debug/status.js +55 -35
- package/dist/mjs/modules/debug/tag.js +8 -7
- package/dist/mjs/modules/debug/transactions.js +31 -25
- package/dist/mjs/modules/feed.js +39 -33
- package/dist/mjs/modules/pinning.js +34 -28
- package/dist/mjs/modules/pss.js +18 -14
- package/dist/mjs/modules/soc.js +18 -15
- package/dist/mjs/modules/status.js +6 -5
- package/dist/mjs/modules/stewardship.js +13 -12
- package/dist/mjs/modules/tag.js +38 -30
- package/dist/mjs/package.json +1 -0
- package/dist/mjs/types/debug.js +5 -4
- package/dist/mjs/types/index.js +6 -2
- package/dist/mjs/types/ky-options.js +7 -0
- package/dist/mjs/types/ky-universal/common.js +7 -0
- package/dist/mjs/types/ky-universal/hooks.js +7 -0
- package/dist/mjs/types/ky-universal/retry.js +7 -0
- package/dist/mjs/utils/bytes.js +32 -26
- package/dist/mjs/utils/collection.browser.js +4 -3
- package/dist/mjs/utils/collection.js +47 -35
- package/dist/mjs/utils/collection.node.js +53 -42
- package/dist/mjs/utils/data.browser.js +64 -48
- package/dist/mjs/utils/data.js +29 -25
- package/dist/mjs/utils/error.js +47 -39
- package/dist/mjs/utils/eth.js +119 -104
- package/dist/mjs/utils/expose.js +9 -9
- package/dist/mjs/utils/file.js +22 -19
- package/dist/mjs/utils/hash.js +10 -5
- package/dist/mjs/utils/headers.js +43 -39
- package/dist/mjs/utils/hex.js +78 -63
- package/dist/mjs/utils/http.js +119 -100
- package/dist/mjs/utils/merge.js +26 -20
- package/dist/mjs/utils/pss.js +8 -6
- package/dist/mjs/utils/stamps.js +7 -3
- package/dist/mjs/utils/stream.js +107 -82
- package/dist/mjs/utils/tar.js +17 -14
- package/dist/mjs/utils/type.js +249 -209
- package/dist/mjs/utils/uint64.js +16 -16
- package/dist/mjs/utils/url.js +32 -25
- package/dist/types/types/index.d.ts +1 -1
- package/dist/types/types/ky-options.d.ts +221 -0
- package/dist/types/types/ky-universal/common.d.ts +13 -0
- package/dist/types/types/ky-universal/hooks.d.ts +92 -0
- package/dist/types/types/ky-universal/retry.d.ts +38 -0
- package/dist/types/utils/stream.d.ts +11 -8
- package/package.json +9 -6
- package/dist/index.js +0 -28326
- package/dist/index.js.map +0 -1
package/dist/mjs/modules/tag.js
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
import { http } from
|
|
1
|
+
import { http } from "../utils/http.js";
|
|
2
2
|
const endpoint = 'tags';
|
|
3
3
|
/**
|
|
4
4
|
* Create new tag on the Bee node
|
|
5
5
|
*
|
|
6
6
|
* @param url Bee tag URL
|
|
7
7
|
*/
|
|
8
|
+
|
|
8
9
|
export async function createTag(ky) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
const response = await http(ky, {
|
|
11
|
+
method: 'post',
|
|
12
|
+
path: endpoint,
|
|
13
|
+
responseType: 'json'
|
|
14
|
+
});
|
|
15
|
+
return response.data;
|
|
15
16
|
}
|
|
16
17
|
/**
|
|
17
18
|
* Retrieve tag information from Bee node
|
|
@@ -19,12 +20,13 @@ export async function createTag(ky) {
|
|
|
19
20
|
* @param url Bee tag URL
|
|
20
21
|
* @param uid UID of tag to be retrieved
|
|
21
22
|
*/
|
|
23
|
+
|
|
22
24
|
export async function retrieveTag(ky, uid) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
const response = await http(ky, {
|
|
26
|
+
path: `${endpoint}/${uid}`,
|
|
27
|
+
responseType: 'json'
|
|
28
|
+
});
|
|
29
|
+
return response.data;
|
|
28
30
|
}
|
|
29
31
|
/**
|
|
30
32
|
* Get limited listing of all tags.
|
|
@@ -33,24 +35,29 @@ export async function retrieveTag(ky, uid) {
|
|
|
33
35
|
* @param offset
|
|
34
36
|
* @param limit
|
|
35
37
|
*/
|
|
38
|
+
|
|
36
39
|
export async function getAllTags(ky, offset, limit) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
const response = await http(ky, {
|
|
41
|
+
path: `${endpoint}`,
|
|
42
|
+
searchParams: {
|
|
43
|
+
offset,
|
|
44
|
+
limit
|
|
45
|
+
},
|
|
46
|
+
responseType: 'json'
|
|
47
|
+
});
|
|
48
|
+
return response.data.tags;
|
|
43
49
|
}
|
|
44
50
|
/**
|
|
45
51
|
* Removes tag from the Bee node.
|
|
46
52
|
* @param url
|
|
47
53
|
* @param uid
|
|
48
54
|
*/
|
|
55
|
+
|
|
49
56
|
export async function deleteTag(ky, uid) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
57
|
+
await http(ky, {
|
|
58
|
+
method: 'delete',
|
|
59
|
+
path: `${endpoint}/${uid}`
|
|
60
|
+
});
|
|
54
61
|
}
|
|
55
62
|
/**
|
|
56
63
|
* Updates tag
|
|
@@ -58,12 +65,13 @@ export async function deleteTag(ky, uid) {
|
|
|
58
65
|
* @param uid
|
|
59
66
|
* @param reference
|
|
60
67
|
*/
|
|
68
|
+
|
|
61
69
|
export async function updateTag(ky, uid, reference) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
+
await http(ky, {
|
|
71
|
+
method: 'patch',
|
|
72
|
+
path: `${endpoint}/${uid}`,
|
|
73
|
+
json: {
|
|
74
|
+
reference
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
package/dist/mjs/package.json
CHANGED
package/dist/mjs/types/debug.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export var BeeModes;
|
|
2
|
+
|
|
2
3
|
(function (BeeModes) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
})(BeeModes || (BeeModes = {}));
|
|
4
|
+
BeeModes["FULL"] = "full";
|
|
5
|
+
BeeModes["LIGHT"] = "light";
|
|
6
|
+
BeeModes["DEV"] = "dev";
|
|
7
|
+
})(BeeModes || (BeeModes = {}));
|
package/dist/mjs/types/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export * from
|
|
1
|
+
export * from "./debug.js";
|
|
2
2
|
export const SPAN_SIZE = 8;
|
|
3
3
|
export const SECTION_SIZE = 32;
|
|
4
4
|
export const BRANCHES = 128;
|
|
@@ -14,20 +14,24 @@ export const ENCRYPTED_REFERENCE_BYTES_LENGTH = 64;
|
|
|
14
14
|
/**
|
|
15
15
|
* Minimal depth that can be used for creation of postage batch
|
|
16
16
|
*/
|
|
17
|
+
|
|
17
18
|
export const STAMPS_DEPTH_MIN = 17;
|
|
18
19
|
/**
|
|
19
20
|
* Maximal depth that can be used for creation of postage batch
|
|
20
21
|
*/
|
|
22
|
+
|
|
21
23
|
export const STAMPS_DEPTH_MAX = 255;
|
|
22
24
|
export const TAGS_LIMIT_MIN = 1;
|
|
23
25
|
export const TAGS_LIMIT_MAX = 1000;
|
|
24
26
|
/*********************************************************
|
|
25
27
|
* Writers and Readers interfaces
|
|
26
28
|
*/
|
|
29
|
+
|
|
27
30
|
export const TOPIC_BYTES_LENGTH = 32;
|
|
28
31
|
export const TOPIC_HEX_LENGTH = 64;
|
|
29
32
|
/*********************************************************
|
|
30
33
|
* Ethereum compatible signing interfaces and definitions
|
|
31
34
|
*/
|
|
35
|
+
|
|
32
36
|
export const SIGNATURE_HEX_LENGTH = 130;
|
|
33
|
-
export const SIGNATURE_BYTES_LENGTH = 65;
|
|
37
|
+
export const SIGNATURE_BYTES_LENGTH = 65;
|
package/dist/mjs/utils/bytes.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import { bytesToHex } from
|
|
1
|
+
import { bytesToHex } from "./hex.js";
|
|
2
2
|
/**
|
|
3
3
|
* Type guard for `Bytes<T>` type
|
|
4
4
|
*
|
|
5
5
|
* @param b The byte array
|
|
6
6
|
* @param length The length of the byte array
|
|
7
7
|
*/
|
|
8
|
+
|
|
8
9
|
export function isBytes(b, length) {
|
|
9
|
-
|
|
10
|
+
return b instanceof Uint8Array && b.length === length;
|
|
10
11
|
}
|
|
11
12
|
/**
|
|
12
13
|
* Verifies if a byte array has a certain length
|
|
@@ -14,10 +15,11 @@ export function isBytes(b, length) {
|
|
|
14
15
|
* @param b The byte array
|
|
15
16
|
* @param length The specified length
|
|
16
17
|
*/
|
|
18
|
+
|
|
17
19
|
export function assertBytes(b, length) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
if (!isBytes(b, length)) {
|
|
21
|
+
throw new TypeError(`Parameter is not valid Bytes of length: ${length} !== ${b.length}`);
|
|
22
|
+
}
|
|
21
23
|
}
|
|
22
24
|
/**
|
|
23
25
|
* Type guard for FlexBytes<Min,Max> type
|
|
@@ -26,8 +28,9 @@ export function assertBytes(b, length) {
|
|
|
26
28
|
* @param min Minimum size of the array
|
|
27
29
|
* @param max Maximum size of the array
|
|
28
30
|
*/
|
|
31
|
+
|
|
29
32
|
export function isFlexBytes(b, min, max) {
|
|
30
|
-
|
|
33
|
+
return b instanceof Uint8Array && b.length >= min && b.length <= max;
|
|
31
34
|
}
|
|
32
35
|
/**
|
|
33
36
|
* Verifies if a byte array has a certain length between min and max
|
|
@@ -36,10 +39,11 @@ export function isFlexBytes(b, min, max) {
|
|
|
36
39
|
* @param min Minimum size of the array
|
|
37
40
|
* @param max Maximum size of the array
|
|
38
41
|
*/
|
|
42
|
+
|
|
39
43
|
export function assertFlexBytes(b, min, max) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
44
|
+
if (!isFlexBytes(b, min, max)) {
|
|
45
|
+
throw new TypeError(`Parameter is not valid FlexBytes of min: ${min}, max: ${max}, length: ${b.length}`);
|
|
46
|
+
}
|
|
43
47
|
}
|
|
44
48
|
/**
|
|
45
49
|
* Return `length` bytes starting from `offset`
|
|
@@ -48,11 +52,12 @@ export function assertFlexBytes(b, min, max) {
|
|
|
48
52
|
* @param offset The offset to start from
|
|
49
53
|
* @param length The length of data to be returned
|
|
50
54
|
*/
|
|
55
|
+
|
|
51
56
|
export function bytesAtOffset(data, offset, length) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
57
|
+
const offsetBytes = data.slice(offset, offset + length); // We are returning strongly typed Bytes so we have to verify that length is really what we claim
|
|
58
|
+
|
|
59
|
+
assertBytes(offsetBytes, length);
|
|
60
|
+
return offsetBytes;
|
|
56
61
|
}
|
|
57
62
|
/**
|
|
58
63
|
* Return flex bytes starting from `offset`
|
|
@@ -62,12 +67,11 @@ export function bytesAtOffset(data, offset, length) {
|
|
|
62
67
|
* @param _min The minimum size of the data
|
|
63
68
|
* @param _max The maximum size of the data
|
|
64
69
|
*/
|
|
65
|
-
|
|
66
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
67
|
-
_min,
|
|
68
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
70
|
+
|
|
71
|
+
export function flexBytesAtOffset(data, offset, // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
72
|
+
_min, // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
69
73
|
_max) {
|
|
70
|
-
|
|
74
|
+
return data.slice(offset);
|
|
71
75
|
}
|
|
72
76
|
/**
|
|
73
77
|
* Returns true if two byte arrays are equal
|
|
@@ -75,21 +79,23 @@ _max) {
|
|
|
75
79
|
* @param a Byte array to compare
|
|
76
80
|
* @param b Byte array to compare
|
|
77
81
|
*/
|
|
82
|
+
|
|
78
83
|
export function bytesEqual(a, b) {
|
|
79
|
-
|
|
84
|
+
return a.length === b.length && a.every((value, index) => value === b[index]);
|
|
80
85
|
}
|
|
81
86
|
/**
|
|
82
87
|
* Returns a new byte array filled with zeroes with the specified length
|
|
83
88
|
*
|
|
84
89
|
* @param length The length of data to be returned
|
|
85
90
|
*/
|
|
91
|
+
|
|
86
92
|
export function makeBytes(length) {
|
|
87
|
-
|
|
93
|
+
return new Uint8Array(length);
|
|
88
94
|
}
|
|
89
95
|
export function wrapBytesWithHelpers(data) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
96
|
+
return Object.assign(data, {
|
|
97
|
+
text: () => new TextDecoder('utf-8').decode(data),
|
|
98
|
+
json: () => JSON.parse(new TextDecoder('utf-8').decode(data)),
|
|
99
|
+
hex: () => bytesToHex(data)
|
|
100
|
+
});
|
|
101
|
+
}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @param dir path to the directory
|
|
6
6
|
*/
|
|
7
7
|
export async function makeCollectionFromFS(dir) {
|
|
8
|
-
|
|
8
|
+
throw new Error('Creating Collection from File System is not supported in browsers!');
|
|
9
9
|
}
|
|
10
10
|
/**
|
|
11
11
|
* Calculate folder size recursively
|
|
@@ -13,6 +13,7 @@ export async function makeCollectionFromFS(dir) {
|
|
|
13
13
|
* @param dir the path to the folder to check
|
|
14
14
|
* @returns size in bytes
|
|
15
15
|
*/
|
|
16
|
+
|
|
16
17
|
export async function getFolderSize(dir) {
|
|
17
|
-
|
|
18
|
-
}
|
|
18
|
+
throw new Error('Creating Collection from File System is not supported in browsers!');
|
|
19
|
+
}
|
|
@@ -1,38 +1,46 @@
|
|
|
1
|
-
import { BeeArgumentError } from
|
|
2
|
-
import { fileArrayBuffer } from
|
|
3
|
-
import { isUint8Array } from
|
|
1
|
+
import { BeeArgumentError } from "./error.js";
|
|
2
|
+
import { fileArrayBuffer } from "./file.js";
|
|
3
|
+
import { isUint8Array } from "./type.js";
|
|
4
4
|
export function isCollection(data) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
if (!Array.isArray(data)) {
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
return data.every(entry => typeof entry === 'object' && entry.data && entry.path && isUint8Array(entry.data));
|
|
9
10
|
}
|
|
10
11
|
export function assertCollection(data) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
if (!isCollection(data)) {
|
|
13
|
+
throw new BeeArgumentError('invalid collection', data);
|
|
14
|
+
}
|
|
14
15
|
}
|
|
16
|
+
|
|
15
17
|
function makeFilePath(file) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
18
|
+
if (file.webkitRelativePath && file.webkitRelativePath !== '') {
|
|
19
|
+
return file.webkitRelativePath.replace(/.*?\//i, '');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (file.name) {
|
|
23
|
+
return file.name;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
throw new TypeError('file is not valid File object');
|
|
23
27
|
}
|
|
28
|
+
|
|
24
29
|
export async function makeCollectionFromFileList(fileList) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
const collection = [];
|
|
31
|
+
|
|
32
|
+
for (let i = 0; i < fileList.length; i++) {
|
|
33
|
+
const file = fileList[i];
|
|
34
|
+
|
|
35
|
+
if (file) {
|
|
36
|
+
collection.push({
|
|
37
|
+
path: makeFilePath(file),
|
|
38
|
+
data: new Uint8Array(await fileArrayBuffer(file))
|
|
39
|
+
});
|
|
34
40
|
}
|
|
35
|
-
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return collection;
|
|
36
44
|
}
|
|
37
45
|
/**
|
|
38
46
|
* Calculate cumulative size of files
|
|
@@ -40,13 +48,17 @@ export async function makeCollectionFromFileList(fileList) {
|
|
|
40
48
|
* @param fileList list of files to check
|
|
41
49
|
* @returns size in bytes
|
|
42
50
|
*/
|
|
51
|
+
|
|
43
52
|
export function getCollectionSize(fileList) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
53
|
+
let sum = 0;
|
|
54
|
+
|
|
55
|
+
for (let i = 0; i < fileList.length; i++) {
|
|
56
|
+
const file = fileList[i];
|
|
57
|
+
|
|
58
|
+
if (file) {
|
|
59
|
+
sum += file.size;
|
|
50
60
|
}
|
|
51
|
-
|
|
52
|
-
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return sum;
|
|
64
|
+
}
|
|
@@ -6,34 +6,40 @@ import path from 'path';
|
|
|
6
6
|
*
|
|
7
7
|
* @param dir path to the directory
|
|
8
8
|
*/
|
|
9
|
+
|
|
9
10
|
export async function makeCollectionFromFS(dir) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
if (typeof dir !== 'string') {
|
|
12
|
+
throw new TypeError('dir has to be string!');
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if (dir === '') {
|
|
16
|
+
throw new TypeError('dir must not be empty string!');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return buildCollectionRelative(dir, '');
|
|
17
20
|
}
|
|
21
|
+
|
|
18
22
|
async function buildCollectionRelative(dir, relativePath) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
23
|
+
// Handles case when the dir is not existing or it is a file ==> throws an error
|
|
24
|
+
const dirname = path.join(dir, relativePath);
|
|
25
|
+
const entries = await fs.promises.opendir(dirname);
|
|
26
|
+
let collection = [];
|
|
27
|
+
|
|
28
|
+
for await (const entry of entries) {
|
|
29
|
+
const fullPath = path.join(dir, relativePath, entry.name);
|
|
30
|
+
const entryPath = path.join(relativePath, entry.name);
|
|
31
|
+
|
|
32
|
+
if (entry.isFile()) {
|
|
33
|
+
collection.push({
|
|
34
|
+
path: entryPath,
|
|
35
|
+
data: new Uint8Array(await fs.promises.readFile(fullPath))
|
|
36
|
+
});
|
|
37
|
+
} else if (entry.isDirectory()) {
|
|
38
|
+
collection = [...(await buildCollectionRelative(dir, entryPath)), ...collection];
|
|
35
39
|
}
|
|
36
|
-
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return collection;
|
|
37
43
|
}
|
|
38
44
|
/**
|
|
39
45
|
* Calculate folder size recursively
|
|
@@ -41,23 +47,28 @@ async function buildCollectionRelative(dir, relativePath) {
|
|
|
41
47
|
* @param dir the path to the folder to check
|
|
42
48
|
* @returns size in bytes
|
|
43
49
|
*/
|
|
50
|
+
|
|
51
|
+
|
|
44
52
|
export async function getFolderSize(dir) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
53
|
+
if (typeof dir !== 'string') {
|
|
54
|
+
throw new TypeError('dir has to be string!');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (dir === '') {
|
|
58
|
+
throw new TypeError('dir must not be empty string!');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const entries = await fs.promises.opendir(dir);
|
|
62
|
+
let size = 0;
|
|
63
|
+
|
|
64
|
+
for await (const entry of entries) {
|
|
65
|
+
if (entry.isFile()) {
|
|
66
|
+
const stats = await fs.promises.stat(path.join(dir, entry.name));
|
|
67
|
+
size += stats.size;
|
|
68
|
+
} else if (entry.isDirectory()) {
|
|
69
|
+
size += await getFolderSize(path.join(dir, entry.name));
|
|
50
70
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const stats = await fs.promises.stat(path.join(dir, entry.name));
|
|
56
|
-
size += stats.size;
|
|
57
|
-
}
|
|
58
|
-
else if (entry.isDirectory()) {
|
|
59
|
-
size += await getFolderSize(path.join(dir, entry.name));
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
return size;
|
|
63
|
-
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return size;
|
|
74
|
+
}
|