@ethersphere/bee-js 8.3.1 → 9.0.2
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 +192 -50
- package/dist/cjs/bee-dev.js +78 -0
- package/dist/cjs/bee.js +341 -417
- package/dist/cjs/chunk/bmt.js +15 -32
- package/dist/cjs/chunk/cac.js +26 -36
- package/dist/cjs/chunk/soc.js +56 -51
- package/dist/cjs/feed/identifier.js +3 -28
- package/dist/cjs/feed/index.js +95 -44
- package/dist/cjs/feed/retrievable.js +11 -22
- package/dist/cjs/index.js +17 -1
- package/dist/cjs/manifest/manifest.js +369 -0
- package/dist/cjs/modules/bytes.js +24 -12
- package/dist/cjs/modules/bzz.js +24 -34
- package/dist/cjs/modules/chunk.js +13 -7
- package/dist/cjs/modules/debug/balance.js +29 -4
- package/dist/cjs/modules/debug/chequebook.js +60 -16
- package/dist/cjs/modules/debug/connectivity.js +82 -4
- package/dist/cjs/modules/debug/settlements.js +22 -2
- package/dist/cjs/modules/debug/stake.js +29 -11
- package/dist/cjs/modules/debug/stamps.js +88 -7
- package/dist/cjs/modules/debug/states.js +25 -3
- package/dist/cjs/modules/debug/status.js +45 -47
- package/dist/cjs/modules/debug/transactions.js +27 -5
- package/dist/cjs/modules/envelope.js +8 -6
- package/dist/cjs/modules/feed.js +25 -10
- package/dist/cjs/modules/grantee.js +18 -12
- package/dist/cjs/modules/gsoc.js +24 -0
- package/dist/cjs/modules/pinning.js +13 -2
- package/dist/cjs/modules/pss.js +9 -3
- package/dist/cjs/modules/soc.js +9 -4
- package/dist/cjs/modules/stewardship.js +7 -3
- package/dist/cjs/modules/tag.js +35 -3
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/stamper/stamper.js +43 -0
- package/dist/cjs/types/debug.js +16 -1
- package/dist/cjs/types/index.js +2 -24
- package/dist/cjs/utils/bytes.js +67 -115
- package/dist/cjs/utils/chunk-size.js +17 -0
- package/dist/cjs/utils/chunk-stream.browser.js +85 -0
- package/dist/cjs/utils/chunk-stream.js +84 -0
- package/dist/cjs/utils/cid.js +5 -6
- package/dist/cjs/utils/collection.browser.js +2 -2
- package/dist/cjs/utils/collection.js +3 -2
- package/dist/cjs/utils/collection.node.js +0 -2
- package/dist/cjs/utils/constants.js +7 -3
- package/dist/cjs/utils/data.browser.js +6 -3
- package/dist/cjs/utils/data.js +8 -4
- package/dist/cjs/utils/duration.js +51 -0
- package/dist/cjs/utils/expose.js +11 -40
- package/dist/cjs/utils/headers.js +79 -52
- package/dist/cjs/utils/http.js +34 -10
- package/dist/cjs/utils/mime.js +78 -0
- package/dist/cjs/utils/pss.js +3 -4
- package/dist/cjs/utils/redundancy.js +18 -14
- package/dist/cjs/utils/resource-locator.js +17 -0
- package/dist/cjs/utils/size.js +35 -0
- package/dist/cjs/utils/stamps.js +67 -51
- package/dist/cjs/utils/tar-uploader.browser.js +2 -2
- package/dist/cjs/utils/tar-uploader.js +2 -2
- package/dist/cjs/utils/tokens.js +144 -0
- package/dist/cjs/utils/type.js +142 -350
- package/dist/cjs/utils/typed-bytes.js +179 -0
- package/dist/cjs/utils/upload-progress.js +2 -0
- package/dist/cjs/utils/url.js +0 -4
- package/dist/cjs/utils/workaround.js +27 -0
- package/dist/index.browser.min.js +1 -2
- package/dist/index.browser.min.js.map +1 -1
- package/dist/mjs/bee-dev.js +98 -0
- package/dist/mjs/bee.js +367 -411
- package/dist/mjs/chunk/bmt.js +13 -29
- package/dist/mjs/chunk/cac.js +26 -34
- package/dist/mjs/chunk/soc.js +57 -53
- package/dist/mjs/feed/identifier.js +3 -26
- package/dist/mjs/feed/index.js +98 -51
- package/dist/mjs/feed/retrievable.js +12 -23
- package/dist/mjs/index.js +10 -1
- package/dist/mjs/manifest/manifest.js +371 -0
- package/dist/mjs/modules/bytes.js +27 -15
- package/dist/mjs/modules/bzz.js +25 -32
- package/dist/mjs/modules/chunk.js +16 -8
- package/dist/mjs/modules/debug/balance.js +61 -4
- package/dist/mjs/modules/debug/chequebook.js +116 -16
- package/dist/mjs/modules/debug/connectivity.js +144 -3
- package/dist/mjs/modules/debug/settlements.js +46 -2
- package/dist/mjs/modules/debug/stake.js +69 -11
- package/dist/mjs/modules/debug/stamps.js +184 -7
- package/dist/mjs/modules/debug/states.js +55 -3
- package/dist/mjs/modules/debug/status.js +97 -45
- package/dist/mjs/modules/debug/transactions.js +61 -5
- package/dist/mjs/modules/envelope.js +11 -9
- package/dist/mjs/modules/feed.js +26 -10
- package/dist/mjs/modules/grantee.js +35 -13
- package/dist/mjs/modules/gsoc.js +16 -0
- package/dist/mjs/modules/pinning.js +23 -2
- package/dist/mjs/modules/pss.js +10 -4
- package/dist/mjs/modules/soc.js +10 -5
- package/dist/mjs/modules/stewardship.js +14 -4
- package/dist/mjs/modules/tag.js +93 -3
- package/dist/mjs/package.json +2 -1
- package/dist/mjs/stamper/stamper.js +39 -0
- package/dist/mjs/types/debug.js +15 -1
- package/dist/mjs/types/index.js +2 -24
- package/dist/mjs/utils/bytes.js +63 -104
- package/dist/mjs/utils/chunk-size.js +13 -0
- package/dist/mjs/utils/chunk-stream.browser.js +85 -0
- package/dist/mjs/utils/chunk-stream.js +87 -0
- package/dist/mjs/utils/cid.js +5 -6
- package/dist/mjs/utils/collection.browser.js +2 -2
- package/dist/mjs/utils/collection.js +2 -2
- package/dist/mjs/utils/collection.node.js +0 -2
- package/dist/mjs/utils/constants.js +6 -2
- package/dist/mjs/utils/data.browser.js +9 -3
- package/dist/mjs/utils/data.js +12 -4
- package/dist/mjs/utils/duration.js +47 -0
- package/dist/mjs/utils/expose.js +2 -7
- package/dist/mjs/utils/headers.js +73 -48
- package/dist/mjs/utils/http.js +33 -11
- package/dist/mjs/utils/mime.js +75 -0
- package/dist/mjs/utils/pss.js +3 -4
- package/dist/mjs/utils/redundancy.js +18 -8
- package/dist/mjs/utils/resource-locator.js +13 -0
- package/dist/mjs/utils/size.js +31 -0
- package/dist/mjs/utils/stamps.js +47 -44
- package/dist/mjs/utils/tar-uploader.browser.js +2 -2
- package/dist/mjs/utils/tar-uploader.js +2 -2
- package/dist/mjs/utils/tokens.js +139 -0
- package/dist/mjs/utils/type.js +216 -310
- package/dist/mjs/utils/typed-bytes.js +160 -0
- package/dist/mjs/utils/upload-progress.js +1 -0
- package/dist/mjs/utils/url.js +0 -4
- package/dist/mjs/utils/workaround.js +22 -0
- package/dist/types/bee-dev.d.ts +5 -0
- package/dist/types/bee.d.ts +88 -165
- package/dist/types/chunk/bmt.d.ts +2 -2
- package/dist/types/chunk/cac.d.ts +7 -24
- package/dist/types/chunk/soc.d.ts +15 -15
- package/dist/types/feed/identifier.d.ts +2 -4
- package/dist/types/feed/index.d.ts +14 -18
- package/dist/types/feed/retrievable.d.ts +3 -4
- package/dist/types/index.d.ts +28 -1
- package/dist/types/manifest/manifest.d.ts +106 -0
- package/dist/types/modules/bytes.d.ts +8 -5
- package/dist/types/modules/bzz.d.ts +8 -10
- package/dist/types/modules/chunk.d.ts +4 -3
- package/dist/types/modules/debug/balance.d.ts +3 -2
- package/dist/types/modules/debug/chequebook.d.ts +7 -6
- package/dist/types/modules/debug/connectivity.d.ts +5 -3
- package/dist/types/modules/debug/settlements.d.ts +2 -1
- package/dist/types/modules/debug/stake.d.ts +4 -2
- package/dist/types/modules/debug/stamps.d.ts +5 -4
- package/dist/types/modules/debug/status.d.ts +6 -25
- package/dist/types/modules/debug/transactions.d.ts +5 -4
- package/dist/types/modules/envelope.d.ts +3 -2
- package/dist/types/modules/feed.d.ts +20 -16
- package/dist/types/modules/grantee.d.ts +7 -6
- package/dist/types/modules/gsoc.d.ts +7 -0
- package/dist/types/modules/pinning.d.ts +2 -4
- package/dist/types/modules/pss.d.ts +4 -3
- package/dist/types/modules/soc.d.ts +3 -2
- package/dist/types/modules/stewardship.d.ts +4 -4
- package/dist/types/modules/tag.d.ts +2 -1
- package/dist/types/stamper/stamper.d.ts +15 -0
- package/dist/types/types/debug.d.ts +63 -115
- package/dist/types/types/index.d.ts +103 -204
- package/dist/types/utils/bytes.d.ts +16 -90
- package/dist/types/utils/chunk-size.d.ts +1 -0
- package/dist/types/utils/chunk-stream.browser.d.ts +6 -0
- package/dist/types/utils/chunk-stream.d.ts +6 -0
- package/dist/types/utils/cid.d.ts +3 -2
- package/dist/types/utils/collection.browser.d.ts +2 -2
- package/dist/types/utils/collection.d.ts +2 -1
- package/dist/types/utils/collection.node.d.ts +0 -1
- package/dist/types/utils/constants.d.ts +4 -1
- package/dist/types/utils/duration.d.ts +17 -0
- package/dist/types/utils/error.d.ts +2 -2
- package/dist/types/utils/expose.d.ts +2 -7
- package/dist/types/utils/headers.d.ts +3 -4
- package/dist/types/utils/mime.d.ts +1 -0
- package/dist/types/utils/pss.d.ts +2 -2
- package/dist/types/utils/resource-locator.d.ts +6 -0
- package/dist/types/utils/size.d.ts +16 -0
- package/dist/types/utils/stamps.d.ts +27 -33
- package/dist/types/utils/tar-uploader.browser.d.ts +3 -4
- package/dist/types/utils/tar-uploader.d.ts +3 -4
- package/dist/types/utils/tokens.d.ts +77 -0
- package/dist/types/utils/type.d.ts +20 -52
- package/dist/types/utils/typed-bytes.d.ts +68 -0
- package/dist/types/utils/upload-progress.d.ts +4 -0
- package/dist/types/utils/workaround.d.ts +2 -0
- package/package.json +11 -17
- package/dist/cjs/chunk/signer.js +0 -126
- package/dist/cjs/chunk/span.js +0 -25
- package/dist/cjs/feed/json.js +0 -28
- package/dist/cjs/feed/topic.js +0 -25
- package/dist/cjs/feed/type.js +0 -15
- package/dist/cjs/modules/debug/chunk.js +0 -21
- package/dist/cjs/modules/debug/tag.js +0 -19
- package/dist/cjs/utils/eth.js +0 -216
- package/dist/cjs/utils/hash.js +0 -21
- package/dist/cjs/utils/hex.js +0 -150
- package/dist/cjs/utils/reference.js +0 -36
- package/dist/index.browser.min.js.LICENSE.txt +0 -8
- package/dist/mjs/chunk/signer.js +0 -114
- package/dist/mjs/chunk/span.js +0 -21
- package/dist/mjs/feed/json.js +0 -26
- package/dist/mjs/feed/topic.js +0 -19
- package/dist/mjs/feed/type.js +0 -10
- package/dist/mjs/modules/debug/chunk.js +0 -17
- package/dist/mjs/modules/debug/tag.js +0 -15
- package/dist/mjs/utils/eth.js +0 -192
- package/dist/mjs/utils/hash.js +0 -16
- package/dist/mjs/utils/hex.js +0 -135
- package/dist/mjs/utils/reference.js +0 -29
- package/dist/types/chunk/signer.d.ts +0 -31
- package/dist/types/chunk/span.d.ts +0 -10
- package/dist/types/feed/json.d.ts +0 -4
- package/dist/types/feed/topic.d.ts +0 -3
- package/dist/types/feed/type.d.ts +0 -6
- package/dist/types/modules/debug/chunk.d.ts +0 -10
- package/dist/types/modules/debug/tag.d.ts +0 -8
- package/dist/types/utils/eth.d.ts +0 -67
- package/dist/types/utils/hash.d.ts +0 -9
- package/dist/types/utils/hex.d.ts +0 -86
- package/dist/types/utils/reference.d.ts +0 -2
package/dist/mjs/utils/bytes.js
CHANGED
|
@@ -1,108 +1,67 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
1
|
+
import { Binary, Types } from 'cafe-utility';
|
|
2
|
+
const DECODER = new TextDecoder();
|
|
3
|
+
const ENCODER = new TextEncoder();
|
|
4
|
+
export class Bytes {
|
|
5
|
+
constructor(bytes, byteLength) {
|
|
6
|
+
if (bytes instanceof Bytes) {
|
|
7
|
+
this.bytes = bytes.bytes;
|
|
8
|
+
} else if (typeof bytes === 'string') {
|
|
9
|
+
this.bytes = Binary.hexToUint8Array(Types.asHexString(bytes, {
|
|
10
|
+
name: 'Bytes#constructor(bytes)'
|
|
11
|
+
}));
|
|
12
|
+
} else if (bytes instanceof ArrayBuffer) {
|
|
13
|
+
this.bytes = new Uint8Array(bytes);
|
|
14
|
+
} else {
|
|
15
|
+
this.bytes = bytes;
|
|
16
|
+
}
|
|
17
|
+
this.length = this.bytes.length;
|
|
18
|
+
if (byteLength) {
|
|
19
|
+
if (Array.isArray(byteLength)) {
|
|
20
|
+
if (!byteLength.includes(this.length)) {
|
|
21
|
+
throw new Error(`Bytes#checkByteLength: bytes length is ${this.length} but expected ${byteLength.join(' or ')}`);
|
|
22
|
+
}
|
|
23
|
+
} else if (this.length !== byteLength) {
|
|
24
|
+
throw new Error(`Bytes#checkByteLength: bytes length is ${this.length} but expected ${byteLength}`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
20
27
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Verifies if a byte array has a certain length
|
|
26
|
-
*
|
|
27
|
-
* @param b The byte array
|
|
28
|
-
* @param length The specified length
|
|
29
|
-
*/
|
|
30
|
-
export function assertBytes(b, length) {
|
|
31
|
-
if (!isBytes(b, length)) {
|
|
32
|
-
throw new TypeError(`Parameter is not valid Bytes of length: ${length} !== ${b.length}`);
|
|
28
|
+
static keccak256(bytes) {
|
|
29
|
+
return new Bytes(Binary.keccak256(new Bytes(bytes).toUint8Array()));
|
|
33
30
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
31
|
+
static fromUtf8(utf8) {
|
|
32
|
+
return new Bytes(ENCODER.encode(utf8));
|
|
33
|
+
}
|
|
34
|
+
static fromSlice(bytes, start, length) {
|
|
35
|
+
if (length === undefined) {
|
|
36
|
+
return new Bytes(bytes.slice(start));
|
|
37
|
+
}
|
|
38
|
+
return new Bytes(bytes.slice(start, start + length));
|
|
39
|
+
}
|
|
40
|
+
offset(index) {
|
|
41
|
+
return new Uint8Array(this.bytes.slice(index));
|
|
42
|
+
}
|
|
43
|
+
toUint8Array() {
|
|
44
|
+
return new Uint8Array(this.bytes);
|
|
45
|
+
}
|
|
46
|
+
toHex() {
|
|
47
|
+
return Binary.uint8ArrayToHex(this.bytes);
|
|
48
|
+
}
|
|
49
|
+
toBase64() {
|
|
50
|
+
return Binary.uint8ArrayToBase64(this.bytes);
|
|
51
|
+
}
|
|
52
|
+
toBase32() {
|
|
53
|
+
return Binary.uint8ArrayToBase32(this.bytes);
|
|
54
|
+
}
|
|
55
|
+
toString() {
|
|
56
|
+
return this.toHex();
|
|
57
|
+
}
|
|
58
|
+
toUtf8() {
|
|
59
|
+
return DECODER.decode(this.bytes);
|
|
60
|
+
}
|
|
61
|
+
toJSON() {
|
|
62
|
+
return JSON.parse(this.toUtf8());
|
|
63
|
+
}
|
|
64
|
+
equals(other) {
|
|
65
|
+
return this.toHex() === new Bytes(other).toHex();
|
|
55
66
|
}
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Return `length` bytes starting from `offset`
|
|
59
|
-
*
|
|
60
|
-
* @param data The original data
|
|
61
|
-
* @param offset The offset to start from
|
|
62
|
-
* @param length The length of data to be returned
|
|
63
|
-
*/
|
|
64
|
-
export function bytesAtOffset(data, offset, length) {
|
|
65
|
-
const offsetBytes = data.slice(offset, offset + length);
|
|
66
|
-
// We are returning strongly typed Bytes so we have to verify that length is really what we claim
|
|
67
|
-
assertBytes(offsetBytes, length);
|
|
68
|
-
return offsetBytes;
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Return flex bytes starting from `offset`
|
|
72
|
-
*
|
|
73
|
-
* @param data The original data
|
|
74
|
-
* @param offset The offset to start from
|
|
75
|
-
* @param _min The minimum size of the data
|
|
76
|
-
* @param _max The maximum size of the data
|
|
77
|
-
*/
|
|
78
|
-
export function flexBytesAtOffset(data, offset,
|
|
79
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
80
|
-
_min,
|
|
81
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
82
|
-
_max) {
|
|
83
|
-
return data.slice(offset);
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Returns true if two byte arrays are equal
|
|
87
|
-
*
|
|
88
|
-
* @param a Byte array to compare
|
|
89
|
-
* @param b Byte array to compare
|
|
90
|
-
*/
|
|
91
|
-
export function bytesEqual(a, b) {
|
|
92
|
-
return a.length === b.length && a.every((value, index) => value === b[index]);
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Returns a new byte array filled with zeroes with the specified length
|
|
96
|
-
*
|
|
97
|
-
* @param length The length of data to be returned
|
|
98
|
-
*/
|
|
99
|
-
export function makeBytes(length) {
|
|
100
|
-
return new Uint8Array(length);
|
|
101
|
-
}
|
|
102
|
-
export function wrapBytesWithHelpers(data) {
|
|
103
|
-
return Object.assign(data, {
|
|
104
|
-
text: () => new TextDecoder('utf-8').decode(data),
|
|
105
|
-
json: () => JSON.parse(new TextDecoder('utf-8').decode(data)),
|
|
106
|
-
hex: () => bytesToHex(data)
|
|
107
|
-
});
|
|
108
67
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export function totalChunks(fileSize) {
|
|
2
|
+
const chunkSize = 4096;
|
|
3
|
+
const hashesPerChunk = 128;
|
|
4
|
+
function chunksAtLevel(chunkCount) {
|
|
5
|
+
if (chunkCount <= 1) {
|
|
6
|
+
return chunkCount;
|
|
7
|
+
}
|
|
8
|
+
const upperLevelChunks = Math.ceil(chunkCount / hashesPerChunk);
|
|
9
|
+
return chunkCount + chunksAtLevel(upperLevelChunks);
|
|
10
|
+
}
|
|
11
|
+
const baseLevelChunks = Math.ceil(fileSize / chunkSize);
|
|
12
|
+
return chunksAtLevel(baseLevelChunks);
|
|
13
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { AsyncQueue, MerkleTree, Strings } from 'cafe-utility';
|
|
2
|
+
import { NULL_ADDRESS } from "../index.js";
|
|
3
|
+
import { MantarayNode } from "../manifest/manifest.js";
|
|
4
|
+
import { totalChunks } from "./chunk-size.js";
|
|
5
|
+
import { makeFilePath } from "./collection.js";
|
|
6
|
+
import { mimes } from "./mime.js";
|
|
7
|
+
import { BatchId } from "./typed-bytes.js";
|
|
8
|
+
export async function hashDirectory(_dir) {
|
|
9
|
+
throw new Error('Hashing directories is not supported in browsers!');
|
|
10
|
+
}
|
|
11
|
+
export async function streamDirectory(_bee, _dir, _postageBatchId, _onUploadProgress, _options, _requestOptions) {
|
|
12
|
+
throw new Error('Streaming directories is not supported in browsers!');
|
|
13
|
+
}
|
|
14
|
+
export async function streamFiles(bee, files, postageBatchId, onUploadProgress, options, requestOptions) {
|
|
15
|
+
const queue = new AsyncQueue(64, 64);
|
|
16
|
+
let total = 0;
|
|
17
|
+
let processed = 0;
|
|
18
|
+
for (const file of files) {
|
|
19
|
+
total += totalChunks(file.size);
|
|
20
|
+
}
|
|
21
|
+
postageBatchId = new BatchId(postageBatchId);
|
|
22
|
+
async function onChunk(chunk) {
|
|
23
|
+
await queue.enqueue(async () => {
|
|
24
|
+
await bee.uploadChunk(postageBatchId, chunk.build(), options, requestOptions);
|
|
25
|
+
onUploadProgress?.({
|
|
26
|
+
total,
|
|
27
|
+
processed: ++processed
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
const mantaray = new MantarayNode();
|
|
32
|
+
for (const file of files) {
|
|
33
|
+
const rootChunk = await new Promise((resolve, reject) => {
|
|
34
|
+
const tree = new MerkleTree(onChunk);
|
|
35
|
+
let offset = 0;
|
|
36
|
+
const reader = new FileReader();
|
|
37
|
+
reader.onerror = () => {
|
|
38
|
+
reject(reader.error);
|
|
39
|
+
};
|
|
40
|
+
const readNextChunk = async () => {
|
|
41
|
+
if (offset >= file.size) {
|
|
42
|
+
const rootChunk = await tree.finalize();
|
|
43
|
+
resolve(rootChunk);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const slice = file.slice(offset, offset + 4096);
|
|
47
|
+
reader.readAsArrayBuffer(slice);
|
|
48
|
+
};
|
|
49
|
+
reader.onload = async event => {
|
|
50
|
+
if (!event.target) {
|
|
51
|
+
reject('No event target');
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const data = event.target.result;
|
|
55
|
+
if (data) {
|
|
56
|
+
await tree.append(new Uint8Array(data));
|
|
57
|
+
offset += 4096;
|
|
58
|
+
}
|
|
59
|
+
readNextChunk();
|
|
60
|
+
};
|
|
61
|
+
readNextChunk();
|
|
62
|
+
});
|
|
63
|
+
await queue.drain();
|
|
64
|
+
const {
|
|
65
|
+
filename,
|
|
66
|
+
extension
|
|
67
|
+
} = Strings.parseFilename(file.name);
|
|
68
|
+
mantaray.addFork(makeFilePath(file), rootChunk.hash(), {
|
|
69
|
+
'Content-Type': maybeEnrichMime(mimes[extension.toLowerCase()] || 'application/octet-stream'),
|
|
70
|
+
Filename: filename
|
|
71
|
+
});
|
|
72
|
+
if (file.name === 'index.html') {
|
|
73
|
+
mantaray.addFork('/', NULL_ADDRESS, {
|
|
74
|
+
'website-index-document': 'index.html'
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return mantaray.saveRecursively(bee, postageBatchId, options, requestOptions);
|
|
79
|
+
}
|
|
80
|
+
function maybeEnrichMime(mime) {
|
|
81
|
+
if (['text/html', 'text/css'].includes(mime)) {
|
|
82
|
+
return `${mime}; charset=utf-8`;
|
|
83
|
+
}
|
|
84
|
+
return mime;
|
|
85
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { AsyncQueue, MerkleTree, Strings } from 'cafe-utility';
|
|
2
|
+
import { createReadStream } from 'fs';
|
|
3
|
+
import { NULL_ADDRESS } from "../index.js";
|
|
4
|
+
import { MantarayNode } from "../manifest/manifest.js";
|
|
5
|
+
import { totalChunks } from "./chunk-size.js";
|
|
6
|
+
import { makeCollectionFromFS } from "./collection.node.js";
|
|
7
|
+
import { mimes } from "./mime.js";
|
|
8
|
+
import { BatchId } from "./typed-bytes.js";
|
|
9
|
+
export async function hashDirectory(dir) {
|
|
10
|
+
const files = await makeCollectionFromFS(dir);
|
|
11
|
+
const mantaray = new MantarayNode();
|
|
12
|
+
for (const file of files) {
|
|
13
|
+
const tree = new MerkleTree(MerkleTree.NOOP);
|
|
14
|
+
if (!file.fsPath) {
|
|
15
|
+
throw Error('File does not have fsPath, which should never happen in node. Please report this issue.');
|
|
16
|
+
}
|
|
17
|
+
const readStream = createReadStream(file.fsPath);
|
|
18
|
+
for await (const data of readStream) {
|
|
19
|
+
await tree.append(data);
|
|
20
|
+
}
|
|
21
|
+
const rootChunk = await tree.finalize();
|
|
22
|
+
const {
|
|
23
|
+
filename,
|
|
24
|
+
extension
|
|
25
|
+
} = Strings.parseFilename(file.path);
|
|
26
|
+
mantaray.addFork(file.path, rootChunk.hash(), {
|
|
27
|
+
'Content-Type': maybeEnrichMime(mimes[extension.toLowerCase()] || 'application/octet-stream'),
|
|
28
|
+
Filename: filename
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
return mantaray.calculateSelfAddress();
|
|
32
|
+
}
|
|
33
|
+
export async function streamDirectory(bee, dir, postageBatchId, onUploadProgress, options, requestOptions) {
|
|
34
|
+
const queue = new AsyncQueue(64, 64);
|
|
35
|
+
let total = 0;
|
|
36
|
+
let processed = 0;
|
|
37
|
+
postageBatchId = new BatchId(postageBatchId);
|
|
38
|
+
const files = await makeCollectionFromFS(dir);
|
|
39
|
+
for (const file of files) {
|
|
40
|
+
total += totalChunks(file.size);
|
|
41
|
+
}
|
|
42
|
+
async function onChunk(chunk) {
|
|
43
|
+
await queue.enqueue(async () => {
|
|
44
|
+
await bee.uploadChunk(postageBatchId, chunk.build(), options, requestOptions);
|
|
45
|
+
onUploadProgress?.({
|
|
46
|
+
total,
|
|
47
|
+
processed: ++processed
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
const mantaray = new MantarayNode();
|
|
52
|
+
for (const file of files) {
|
|
53
|
+
if (!file.fsPath) {
|
|
54
|
+
throw Error('File does not have fsPath, which should never happen in node. Please report this issue.');
|
|
55
|
+
}
|
|
56
|
+
const readStream = createReadStream(file.fsPath);
|
|
57
|
+
const tree = new MerkleTree(onChunk);
|
|
58
|
+
for await (const data of readStream) {
|
|
59
|
+
await tree.append(data);
|
|
60
|
+
}
|
|
61
|
+
const rootChunk = await tree.finalize();
|
|
62
|
+
await queue.drain();
|
|
63
|
+
const {
|
|
64
|
+
filename,
|
|
65
|
+
extension
|
|
66
|
+
} = Strings.parseFilename(file.path);
|
|
67
|
+
mantaray.addFork(file.path, rootChunk.hash(), {
|
|
68
|
+
'Content-Type': maybeEnrichMime(mimes[extension.toLowerCase()] || 'application/octet-stream'),
|
|
69
|
+
Filename: filename
|
|
70
|
+
});
|
|
71
|
+
if (file.path === 'index.html') {
|
|
72
|
+
mantaray.addFork('/', NULL_ADDRESS, {
|
|
73
|
+
'website-index-document': 'index.html'
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return mantaray.saveRecursively(bee, postageBatchId, options, requestOptions);
|
|
78
|
+
}
|
|
79
|
+
function maybeEnrichMime(mime) {
|
|
80
|
+
if (['text/html', 'text/css'].includes(mime)) {
|
|
81
|
+
return `${mime}; charset=utf-8`;
|
|
82
|
+
}
|
|
83
|
+
return mime;
|
|
84
|
+
}
|
|
85
|
+
export async function streamFiles(_bee, _files, _postageBatchId, _onUploadProgress, _options, _requestOptions) {
|
|
86
|
+
throw new Error('Streaming files is not supported in Node.js');
|
|
87
|
+
}
|
package/dist/mjs/utils/cid.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Binary } from 'cafe-utility';
|
|
2
|
+
import { Reference } from "./typed-bytes.js";
|
|
2
3
|
export const SWARM_MANIFEST_CODEC = 0xfa;
|
|
3
4
|
export const SWARM_FEED_CODEC = 0xfb;
|
|
4
5
|
const CODEC_TABLE = {
|
|
@@ -6,9 +7,7 @@ const CODEC_TABLE = {
|
|
|
6
7
|
[SWARM_FEED_CODEC]: 'feed'
|
|
7
8
|
};
|
|
8
9
|
export function convertReferenceToCid(reference, type) {
|
|
9
|
-
|
|
10
|
-
throw TypeError('Only 32-byte, non-encrypted references are supported');
|
|
11
|
-
}
|
|
10
|
+
reference = new Reference(reference);
|
|
12
11
|
const base32 = 'b';
|
|
13
12
|
const version = new Uint8Array([1]);
|
|
14
13
|
const codec = new Uint8Array([type === 'feed' ? SWARM_FEED_CODEC : SWARM_MANIFEST_CODEC]);
|
|
@@ -16,7 +15,7 @@ export function convertReferenceToCid(reference, type) {
|
|
|
16
15
|
const sha256 = new Uint8Array([27]);
|
|
17
16
|
const size = new Uint8Array([32]);
|
|
18
17
|
const header = Binary.uint8ArrayToBase32(Binary.concatBytes(version, codec, unknown, sha256, size)).replace(/\=+$/, '');
|
|
19
|
-
const hash =
|
|
18
|
+
const hash = reference.toBase32().replace(/\=+$/, '');
|
|
20
19
|
return `${base32}${header}${hash}`.toLowerCase();
|
|
21
20
|
}
|
|
22
21
|
export function convertCidToReference(cid) {
|
|
@@ -25,9 +24,9 @@ export function convertCidToReference(cid) {
|
|
|
25
24
|
if (!CODEC_TABLE[codec]) {
|
|
26
25
|
throw new Error('Unknown codec');
|
|
27
26
|
}
|
|
28
|
-
const reference = bytes.slice(-32);
|
|
27
|
+
const reference = new Reference(bytes.slice(-32));
|
|
29
28
|
return {
|
|
30
29
|
type: CODEC_TABLE[codec],
|
|
31
|
-
reference
|
|
30
|
+
reference
|
|
32
31
|
};
|
|
33
32
|
}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*
|
|
5
5
|
* @param dir path to the directory
|
|
6
6
|
*/
|
|
7
|
-
export async function makeCollectionFromFS(
|
|
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
|
/**
|
|
@@ -13,6 +13,6 @@ 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
|
-
export async function getFolderSize(
|
|
16
|
+
export async function getFolderSize(_dir) {
|
|
17
17
|
throw new Error('Creating Collection from File System is not supported in browsers!');
|
|
18
18
|
}
|
|
@@ -10,7 +10,7 @@ export function assertCollection(data) {
|
|
|
10
10
|
throw new BeeArgumentError('invalid collection', data);
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
|
-
function makeFilePath(file) {
|
|
13
|
+
export function makeFilePath(file) {
|
|
14
14
|
if (file.webkitRelativePath && file.webkitRelativePath !== '') {
|
|
15
15
|
return file.webkitRelativePath.replace(/.*?\//i, '');
|
|
16
16
|
}
|
|
@@ -19,7 +19,7 @@ function makeFilePath(file) {
|
|
|
19
19
|
}
|
|
20
20
|
throw new TypeError('file is not valid File object');
|
|
21
21
|
}
|
|
22
|
-
export
|
|
22
|
+
export function makeCollectionFromFileList(fileList) {
|
|
23
23
|
return Array.from(fileList).map(file => ({
|
|
24
24
|
path: makeFilePath(file),
|
|
25
25
|
size: file.size,
|
|
@@ -2,7 +2,6 @@ import fs from 'fs';
|
|
|
2
2
|
import path from 'path';
|
|
3
3
|
/**
|
|
4
4
|
* Creates array in the format of Collection with data loaded from directory on filesystem.
|
|
5
|
-
* The function loads all the data into memory!
|
|
6
5
|
*
|
|
7
6
|
* @param dir path to the directory
|
|
8
7
|
*/
|
|
@@ -16,7 +15,6 @@ export async function makeCollectionFromFS(dir) {
|
|
|
16
15
|
return buildCollectionRelative(dir, '');
|
|
17
16
|
}
|
|
18
17
|
async function buildCollectionRelative(dir, relativePath) {
|
|
19
|
-
// Handles case when the dir is not existing or it is a file ==> throws an error
|
|
20
18
|
const dirname = path.join(dir, relativePath);
|
|
21
19
|
const entries = await fs.promises.opendir(dirname);
|
|
22
20
|
let collection = [];
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import { BatchId, Topic } from "./typed-bytes.js";
|
|
1
2
|
export const SWARM_GATEWAY_URL = 'https://api.gateway.ethswarm.org';
|
|
2
|
-
export const NULL_STAMP = '0000000000000000000000000000000000000000000000000000000000000000';
|
|
3
|
-
export const NULL_TOPIC = '0000000000000000000000000000000000000000000000000000000000000000';
|
|
3
|
+
export const NULL_STAMP = new BatchId('0000000000000000000000000000000000000000000000000000000000000000');
|
|
4
|
+
export const NULL_TOPIC = new Topic('0000000000000000000000000000000000000000000000000000000000000000');
|
|
5
|
+
export const NULL_ADDRESS = new Uint8Array(32);
|
|
6
|
+
export const NULL_IDENTIFIER = new Uint8Array(32);
|
|
7
|
+
export const NULL_OWNER = new Uint8Array(20);
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
export async function prepareWebsocketData(data) {
|
|
2
|
-
if (typeof data === 'string')
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
if (typeof data === 'string') {
|
|
3
|
+
return new TextEncoder().encode(data);
|
|
4
|
+
}
|
|
5
|
+
if (data instanceof ArrayBuffer) {
|
|
6
|
+
return new Uint8Array(data);
|
|
7
|
+
}
|
|
8
|
+
if (data instanceof Blob) {
|
|
9
|
+
return new Uint8Array(await new Response(data).arrayBuffer());
|
|
10
|
+
}
|
|
5
11
|
throw new TypeError('unknown websocket data type');
|
|
6
12
|
}
|
package/dist/mjs/utils/data.js
CHANGED
|
@@ -2,9 +2,17 @@ function isBufferArray(buffer) {
|
|
|
2
2
|
return Array.isArray(buffer) && buffer.length > 0 && buffer.every(data => data instanceof Buffer);
|
|
3
3
|
}
|
|
4
4
|
export async function prepareWebsocketData(data) {
|
|
5
|
-
if (typeof data === 'string')
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
if (
|
|
5
|
+
if (typeof data === 'string') {
|
|
6
|
+
return new TextEncoder().encode(data);
|
|
7
|
+
}
|
|
8
|
+
if (data instanceof Buffer) {
|
|
9
|
+
return new Uint8Array(data);
|
|
10
|
+
}
|
|
11
|
+
if (data instanceof ArrayBuffer) {
|
|
12
|
+
return new Uint8Array(data);
|
|
13
|
+
}
|
|
14
|
+
if (isBufferArray(data)) {
|
|
15
|
+
return new Uint8Array(Buffer.concat(data));
|
|
16
|
+
}
|
|
9
17
|
throw new TypeError('unknown websocket data type');
|
|
10
18
|
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export class Duration {
|
|
2
|
+
constructor(seconds) {
|
|
3
|
+
this.seconds = Math.ceil(seconds);
|
|
4
|
+
if (seconds <= 0) {
|
|
5
|
+
throw Error('Duration must be greater than 0');
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
static fromMilliseconds(milliseconds) {
|
|
9
|
+
return new Duration(milliseconds / 1000);
|
|
10
|
+
}
|
|
11
|
+
static fromSeconds(seconds) {
|
|
12
|
+
return new Duration(seconds);
|
|
13
|
+
}
|
|
14
|
+
static fromHours(hours) {
|
|
15
|
+
return new Duration(hours * 60 * 60);
|
|
16
|
+
}
|
|
17
|
+
static fromDays(days) {
|
|
18
|
+
return new Duration(days * 24 * 60 * 60);
|
|
19
|
+
}
|
|
20
|
+
static fromWeeks(weeks) {
|
|
21
|
+
return new Duration(weeks * 7 * 24 * 60 * 60);
|
|
22
|
+
}
|
|
23
|
+
static fromYears(years) {
|
|
24
|
+
return new Duration(years * 365 * 24 * 60 * 60);
|
|
25
|
+
}
|
|
26
|
+
static fromEndDate(endDate, startDate) {
|
|
27
|
+
return new Duration((endDate.getTime() - (startDate ?? new Date()).getTime()) / 1000);
|
|
28
|
+
}
|
|
29
|
+
toSeconds() {
|
|
30
|
+
return this.seconds;
|
|
31
|
+
}
|
|
32
|
+
toHours() {
|
|
33
|
+
return this.seconds / 60 / 60;
|
|
34
|
+
}
|
|
35
|
+
toDays() {
|
|
36
|
+
return this.seconds / 24 / 60 / 60;
|
|
37
|
+
}
|
|
38
|
+
toWeeks() {
|
|
39
|
+
return this.seconds / 7 / 24 / 60 / 60;
|
|
40
|
+
}
|
|
41
|
+
toYears() {
|
|
42
|
+
return this.seconds / 365 / 24 / 60 / 60;
|
|
43
|
+
}
|
|
44
|
+
toEndDate(startDate) {
|
|
45
|
+
return new Date((startDate ?? new Date()).getTime() + this.seconds * 1000);
|
|
46
|
+
}
|
|
47
|
+
}
|
package/dist/mjs/utils/expose.js
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
export { getCollectionSize } from "./collection.js";
|
|
1
|
+
export { getCollectionSize, makeCollectionFromFileList } from "./collection.js";
|
|
2
2
|
export { getFolderSize } from "./collection.node.js";
|
|
3
|
-
export { assertBytes, assertFlexBytes, bytesAtOffset, bytesEqual, flexBytesAtOffset, isBytes, isFlexBytes } from "./bytes.js";
|
|
4
|
-
export { assertHexString, assertPrefixedHexString, bytesToHex, hexToBytes, intToHex, isHexString, makeHexString } from "./hex.js";
|
|
5
|
-
export { capitalizeAddressERC55, ethToSwarmAddress, fromLittleEndian, isHexEthAddress, makeEthAddress, makeEthereumWalletSigner, makeHexEthAddress, toLittleEndian } from "./eth.js";
|
|
6
|
-
export { keccak256Hash } from "./hash.js";
|
|
7
3
|
export { makeMaxTarget } from "./pss.js";
|
|
8
|
-
export { getAmountForTtl, getDepthForCapacity, getStampCostInBzz, getStampCostInPlur, getStampEffectiveBytes, getStampMaximumCapacityBytes, getStampTtlSeconds, getStampUsage } from "./stamps.js";
|
|
9
4
|
export { approximateOverheadForRedundancyLevel, getRedundancyStat, getRedundancyStats } from "./redundancy.js";
|
|
10
|
-
export {
|
|
5
|
+
export { getAmountForDuration, getDepthForSize, getStampCost, getStampDuration, getStampEffectiveBytes, getStampEffectiveBytesBreakpoints, getStampTheoreticalBytes, getStampUsage } from "./stamps.js";
|