@ethersphere/bee-js 3.3.0 → 3.3.2-pre.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 +8 -8
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/types/debug.js +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 +60 -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 +58 -38
- 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 +6 -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/bee-debug.d.ts +3 -3
- package/dist/types/modules/debug/stamps.d.ts +3 -3
- package/dist/types/modules/debug/status.d.ts +3 -3
- package/dist/types/types/debug.d.ts +1 -0
- package/dist/types/types/index.d.ts +2 -2
- 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/stamps.d.ts +2 -2
- package/dist/types/utils/stream.d.ts +11 -8
- package/package.json +22 -19
- package/dist/index.js +0 -28326
- package/dist/index.js.map +0 -1
package/dist/mjs/feed/index.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { keccak256Hash } from
|
|
2
|
-
import { serializeBytes } from
|
|
3
|
-
import { uploadSingleOwnerChunkData, makeSingleOwnerChunkFromData } from
|
|
4
|
-
import { fetchFeedUpdate } from
|
|
5
|
-
import { REFERENCE_HEX_LENGTH, ENCRYPTED_REFERENCE_HEX_LENGTH
|
|
6
|
-
import { makeBytes, bytesAtOffset } from
|
|
7
|
-
import { BeeResponseError } from
|
|
8
|
-
import { bytesToHex, hexToBytes, makeHexString } from
|
|
9
|
-
import { readUint64BigEndian, writeUint64BigEndian } from
|
|
10
|
-
import * as chunkAPI from
|
|
11
|
-
import { makeHexEthAddress } from
|
|
12
|
-
import { assertAddress } from
|
|
1
|
+
import { keccak256Hash } from "../utils/hash.js";
|
|
2
|
+
import { serializeBytes } from "../chunk/serialize.js";
|
|
3
|
+
import { uploadSingleOwnerChunkData, makeSingleOwnerChunkFromData } from "../chunk/soc.js";
|
|
4
|
+
import { fetchFeedUpdate } from "../modules/feed.js";
|
|
5
|
+
import { REFERENCE_HEX_LENGTH, ENCRYPTED_REFERENCE_HEX_LENGTH } from "../types/index.js";
|
|
6
|
+
import { makeBytes, bytesAtOffset } from "../utils/bytes.js";
|
|
7
|
+
import { BeeResponseError } from "../utils/error.js";
|
|
8
|
+
import { bytesToHex, hexToBytes, makeHexString } from "../utils/hex.js";
|
|
9
|
+
import { readUint64BigEndian, writeUint64BigEndian } from "../utils/uint64.js";
|
|
10
|
+
import * as chunkAPI from "../modules/chunk.js";
|
|
11
|
+
import { makeHexEthAddress } from "../utils/eth.js";
|
|
12
|
+
import { assertAddress } from "../utils/type.js";
|
|
13
13
|
const TIMESTAMP_PAYLOAD_OFFSET = 0;
|
|
14
14
|
const TIMESTAMP_PAYLOAD_SIZE = 8;
|
|
15
15
|
const REFERENCE_PAYLOAD_OFFSET = TIMESTAMP_PAYLOAD_SIZE;
|
|
@@ -17,120 +17,129 @@ const REFERENCE_PAYLOAD_MIN_SIZE = 32;
|
|
|
17
17
|
const REFERENCE_PAYLOAD_MAX_SIZE = 64;
|
|
18
18
|
const INDEX_HEX_LENGTH = 16;
|
|
19
19
|
export function isEpoch(epoch) {
|
|
20
|
-
|
|
20
|
+
return typeof epoch === 'object' && epoch !== null && 'time' in epoch && 'level' in epoch;
|
|
21
21
|
}
|
|
22
|
+
|
|
22
23
|
function hashFeedIdentifier(topic, index) {
|
|
23
|
-
|
|
24
|
+
return keccak256Hash(hexToBytes(topic), index);
|
|
24
25
|
}
|
|
26
|
+
|
|
25
27
|
export function makeSequentialFeedIdentifier(topic, index) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
+
const indexBytes = writeUint64BigEndian(index);
|
|
29
|
+
return hashFeedIdentifier(topic, indexBytes);
|
|
28
30
|
}
|
|
29
31
|
export function makeFeedIndexBytes(s) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
+
const hex = makeHexString(s, INDEX_HEX_LENGTH);
|
|
33
|
+
return hexToBytes(hex);
|
|
32
34
|
}
|
|
33
35
|
export function makeFeedIdentifier(topic, index) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return hashFeedIdentifier(topic, index);
|
|
36
|
+
if (typeof index === 'number') {
|
|
37
|
+
return makeSequentialFeedIdentifier(topic, index);
|
|
38
|
+
} else if (typeof index === 'string') {
|
|
39
|
+
const indexBytes = makeFeedIndexBytes(index);
|
|
40
|
+
return hashFeedIdentifier(topic, indexBytes);
|
|
41
|
+
} else if (isEpoch(index)) {
|
|
42
|
+
throw new TypeError('epoch is not yet implemented');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return hashFeedIdentifier(topic, index);
|
|
45
46
|
}
|
|
46
47
|
export async function uploadFeedUpdate(ky, signer, topic, index, reference, postageBatchId, options) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
48
|
+
const identifier = makeFeedIdentifier(topic, index);
|
|
49
|
+
const at = options?.at ?? Date.now() / 1000.0;
|
|
50
|
+
const timestamp = writeUint64BigEndian(at);
|
|
51
|
+
const payloadBytes = serializeBytes(timestamp, reference);
|
|
52
|
+
return uploadSingleOwnerChunkData(ky, signer, postageBatchId, identifier, payloadBytes, options);
|
|
52
53
|
}
|
|
53
54
|
export async function findNextIndex(ky, owner, topic, options) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
return bytesToHex(makeBytes(8));
|
|
61
|
-
}
|
|
62
|
-
throw e;
|
|
55
|
+
try {
|
|
56
|
+
const feedUpdate = await fetchFeedUpdate(ky, owner, topic, options);
|
|
57
|
+
return makeHexString(feedUpdate.feedIndexNext, INDEX_HEX_LENGTH);
|
|
58
|
+
} catch (e) {
|
|
59
|
+
if (e instanceof BeeResponseError && e.status === 404) {
|
|
60
|
+
return bytesToHex(makeBytes(8));
|
|
63
61
|
}
|
|
62
|
+
|
|
63
|
+
throw e;
|
|
64
|
+
}
|
|
64
65
|
}
|
|
65
66
|
export async function updateFeed(ky, signer, topic, reference, postageBatchId, options) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
67
|
+
const ownerHex = makeHexEthAddress(signer.address);
|
|
68
|
+
const nextIndex = await findNextIndex(ky, ownerHex, topic, options);
|
|
69
|
+
return uploadFeedUpdate(ky, signer, topic, nextIndex, reference, postageBatchId, options);
|
|
69
70
|
}
|
|
71
|
+
|
|
70
72
|
function verifyChunkReferenceAtOffset(offset, data) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
73
|
+
try {
|
|
74
|
+
return bytesAtOffset(data, offset, REFERENCE_PAYLOAD_MAX_SIZE);
|
|
75
|
+
} catch (e) {
|
|
76
|
+
return bytesAtOffset(data, offset, REFERENCE_PAYLOAD_MIN_SIZE);
|
|
77
|
+
}
|
|
77
78
|
}
|
|
79
|
+
|
|
78
80
|
export function verifyChunkReference(data) {
|
|
79
|
-
|
|
81
|
+
return verifyChunkReferenceAtOffset(0, data);
|
|
80
82
|
}
|
|
81
83
|
export async function downloadFeedUpdate(ky, owner, topic, index) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
84
|
+
const identifier = makeFeedIdentifier(topic, index);
|
|
85
|
+
const address = keccak256Hash(identifier, owner);
|
|
86
|
+
const addressHex = bytesToHex(address);
|
|
87
|
+
const data = await chunkAPI.download(ky, addressHex);
|
|
88
|
+
const soc = makeSingleOwnerChunkFromData(data, address);
|
|
89
|
+
const payload = soc.payload();
|
|
90
|
+
const timestampBytes = bytesAtOffset(payload, TIMESTAMP_PAYLOAD_OFFSET, TIMESTAMP_PAYLOAD_SIZE);
|
|
91
|
+
const timestamp = readUint64BigEndian(timestampBytes);
|
|
92
|
+
const reference = verifyChunkReferenceAtOffset(REFERENCE_PAYLOAD_OFFSET, payload);
|
|
93
|
+
return {
|
|
94
|
+
timestamp,
|
|
95
|
+
reference
|
|
96
|
+
};
|
|
95
97
|
}
|
|
96
98
|
export function makeFeedReader(ky, type, topic, owner) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
99
|
+
const download = async options => fetchFeedUpdate(ky, owner, topic, { ...options,
|
|
100
|
+
type
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
type,
|
|
105
|
+
owner,
|
|
106
|
+
topic,
|
|
107
|
+
download
|
|
108
|
+
};
|
|
104
109
|
}
|
|
110
|
+
|
|
105
111
|
function makeChunkReference(reference) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
else if (reference instanceof Uint8Array) {
|
|
122
|
-
return verifyChunkReference(reference);
|
|
112
|
+
if (typeof reference === 'string') {
|
|
113
|
+
try {
|
|
114
|
+
// Non-encrypted chunk hex string reference
|
|
115
|
+
const hexReference = makeHexString(reference, REFERENCE_HEX_LENGTH);
|
|
116
|
+
return hexToBytes(hexReference);
|
|
117
|
+
} catch (e) {
|
|
118
|
+
if (!(e instanceof TypeError)) {
|
|
119
|
+
throw e;
|
|
120
|
+
} // Encrypted chunk hex string reference
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
const hexReference = makeHexString(reference, ENCRYPTED_REFERENCE_HEX_LENGTH);
|
|
124
|
+
return hexToBytes(hexReference);
|
|
123
125
|
}
|
|
124
|
-
|
|
126
|
+
} else if (reference instanceof Uint8Array) {
|
|
127
|
+
return verifyChunkReference(reference);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
throw new TypeError('invalid chunk reference');
|
|
125
131
|
}
|
|
132
|
+
|
|
126
133
|
export function makeFeedWriter(ky, type, topic, signer) {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
134
|
+
const upload = async (postageBatchId, reference, options) => {
|
|
135
|
+
assertAddress(postageBatchId);
|
|
136
|
+
const canonicalReference = makeChunkReference(reference);
|
|
137
|
+
return updateFeed(ky, signer, topic, canonicalReference, postageBatchId, { ...options,
|
|
138
|
+
type
|
|
139
|
+
});
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
return { ...makeFeedReader(ky, type, topic, makeHexEthAddress(signer.address)),
|
|
143
|
+
upload
|
|
144
|
+
};
|
|
145
|
+
}
|
package/dist/mjs/feed/json.js
CHANGED
|
@@ -1,23 +1,27 @@
|
|
|
1
|
-
import { isError } from
|
|
1
|
+
import { isError } from "../utils/type.js";
|
|
2
|
+
|
|
2
3
|
function serializeJson(data) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
e.message = `JsonFeed: ${e.message}`;
|
|
10
|
-
}
|
|
11
|
-
throw e;
|
|
4
|
+
try {
|
|
5
|
+
const jsonString = JSON.stringify(data);
|
|
6
|
+
return new TextEncoder().encode(jsonString);
|
|
7
|
+
} catch (e) {
|
|
8
|
+
if (isError(e)) {
|
|
9
|
+
e.message = `JsonFeed: ${e.message}`;
|
|
12
10
|
}
|
|
11
|
+
|
|
12
|
+
throw e;
|
|
13
|
+
}
|
|
13
14
|
}
|
|
15
|
+
|
|
14
16
|
export async function getJsonData(bee, reader) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
const feedUpdate = await reader.download();
|
|
18
|
+
const retrievedData = await bee.downloadData(feedUpdate.reference);
|
|
19
|
+
return retrievedData.json();
|
|
18
20
|
}
|
|
19
21
|
export async function setJsonData(bee, writer, postageBatchId, data, options) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
22
|
+
const serializedData = serializeJson(data);
|
|
23
|
+
const {
|
|
24
|
+
reference
|
|
25
|
+
} = await bee.uploadData(postageBatchId, serializedData, options);
|
|
26
|
+
return writer.upload(postageBatchId, reference);
|
|
27
|
+
}
|
package/dist/mjs/feed/topic.js
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
|
-
import { keccak256Hash } from
|
|
2
|
-
import { assertBytes } from
|
|
3
|
-
import { makeHexString, bytesToHex } from
|
|
4
|
-
import { TOPIC_BYTES_LENGTH, TOPIC_HEX_LENGTH } from
|
|
1
|
+
import { keccak256Hash } from "../utils/hash.js";
|
|
2
|
+
import { assertBytes } from "../utils/bytes.js";
|
|
3
|
+
import { makeHexString, bytesToHex } from "../utils/hex.js";
|
|
4
|
+
import { TOPIC_BYTES_LENGTH, TOPIC_HEX_LENGTH } from "../types/index.js";
|
|
5
5
|
export function makeTopic(topic) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
if (typeof topic === 'string') {
|
|
7
|
+
return makeHexString(topic, TOPIC_HEX_LENGTH);
|
|
8
|
+
} else if (topic instanceof Uint8Array) {
|
|
9
|
+
assertBytes(topic, TOPIC_BYTES_LENGTH);
|
|
10
|
+
return bytesToHex(topic, TOPIC_HEX_LENGTH);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
throw new TypeError('invalid topic');
|
|
14
14
|
}
|
|
15
15
|
export function makeTopicFromString(s) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
if (typeof s !== 'string') {
|
|
17
|
+
throw new TypeError('topic has to be string!');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return bytesToHex(keccak256Hash(s), TOPIC_HEX_LENGTH);
|
|
21
|
+
}
|
package/dist/mjs/feed/type.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
const feedTypes = ['sequence', 'epoch'];
|
|
2
2
|
export const DEFAULT_FEED_TYPE = 'sequence';
|
|
3
3
|
export function isFeedType(type) {
|
|
4
|
-
|
|
4
|
+
return typeof type === 'string' && feedTypes.includes(type);
|
|
5
5
|
}
|
|
6
6
|
export function assertFeedType(type) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
7
|
+
if (!isFeedType(type)) {
|
|
8
|
+
throw new TypeError('invalid feed type');
|
|
9
|
+
}
|
|
10
|
+
}
|
package/dist/mjs/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Bee } from
|
|
2
|
-
import { BeeDebug } from
|
|
3
|
-
export * as Utils from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
6
|
-
export { SUPPORTED_BEE_VERSION, SUPPORTED_BEE_VERSION_EXACT } from
|
|
7
|
-
export { Bee, BeeDebug };
|
|
1
|
+
import { Bee } from "./bee.js";
|
|
2
|
+
import { BeeDebug } from "./bee-debug.js";
|
|
3
|
+
export * as Utils from "./utils/expose.js";
|
|
4
|
+
export * from "./types/index.js";
|
|
5
|
+
export * from "./utils/error.js";
|
|
6
|
+
export { SUPPORTED_BEE_VERSION, SUPPORTED_BEE_VERSION_EXACT } from "./modules/debug/status.js";
|
|
7
|
+
export { Bee, BeeDebug };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { prepareData } from
|
|
2
|
-
import { extractUploadHeaders } from
|
|
3
|
-
import { http } from
|
|
4
|
-
import { wrapBytesWithHelpers } from
|
|
5
|
-
import { makeTagUid } from
|
|
1
|
+
import { prepareData } from "../utils/data.js";
|
|
2
|
+
import { extractUploadHeaders } from "../utils/headers.js";
|
|
3
|
+
import { http } from "../utils/http.js";
|
|
4
|
+
import { wrapBytesWithHelpers } from "../utils/bytes.js";
|
|
5
|
+
import { makeTagUid } from "../utils/type.js";
|
|
6
6
|
const endpoint = 'bytes';
|
|
7
7
|
/**
|
|
8
8
|
* Upload data to a Bee node
|
|
@@ -12,21 +12,22 @@ const endpoint = 'bytes';
|
|
|
12
12
|
* @param postageBatchId Postage BatchId that will be assigned to uploaded data
|
|
13
13
|
* @param options Additional options like tag, encryption, pinning
|
|
14
14
|
*/
|
|
15
|
+
|
|
15
16
|
export async function upload(ky, data, postageBatchId, options) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
17
|
+
const response = await http(ky, {
|
|
18
|
+
path: endpoint,
|
|
19
|
+
method: 'post',
|
|
20
|
+
responseType: 'json',
|
|
21
|
+
body: await prepareData(data),
|
|
22
|
+
headers: {
|
|
23
|
+
'content-type': 'application/octet-stream',
|
|
24
|
+
...extractUploadHeaders(postageBatchId, options)
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
return {
|
|
28
|
+
reference: response.data.reference,
|
|
29
|
+
tagUid: makeTagUid(response.headers.get('swarm-tag'))
|
|
30
|
+
};
|
|
30
31
|
}
|
|
31
32
|
/**
|
|
32
33
|
* Download data as a byte array
|
|
@@ -34,12 +35,13 @@ export async function upload(ky, data, postageBatchId, options) {
|
|
|
34
35
|
* @param ky
|
|
35
36
|
* @param hash Bee content reference
|
|
36
37
|
*/
|
|
38
|
+
|
|
37
39
|
export async function download(ky, hash) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
const response = await http(ky, {
|
|
41
|
+
responseType: 'arraybuffer',
|
|
42
|
+
path: `${endpoint}/${hash}`
|
|
43
|
+
});
|
|
44
|
+
return wrapBytesWithHelpers(new Uint8Array(response.data));
|
|
43
45
|
}
|
|
44
46
|
/**
|
|
45
47
|
* Download data as a readable stream
|
|
@@ -47,10 +49,11 @@ export async function download(ky, hash) {
|
|
|
47
49
|
* @param ky
|
|
48
50
|
* @param hash Bee content reference
|
|
49
51
|
*/
|
|
52
|
+
|
|
50
53
|
export async function downloadReadable(ky, hash) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
54
|
+
const response = await http(ky, {
|
|
55
|
+
responseType: 'stream',
|
|
56
|
+
path: `${endpoint}/${hash}`
|
|
57
|
+
});
|
|
58
|
+
return response.data;
|
|
59
|
+
}
|
package/dist/mjs/modules/bzz.js
CHANGED
|
@@ -1,19 +1,18 @@
|
|
|
1
|
-
import { extractUploadHeaders, readFileHeaders } from
|
|
2
|
-
import { http } from
|
|
3
|
-
import { prepareData } from
|
|
4
|
-
import { makeTar } from
|
|
5
|
-
import { assertCollection } from
|
|
6
|
-
import { wrapBytesWithHelpers } from
|
|
7
|
-
import { isReadable } from
|
|
8
|
-
import { makeTagUid } from
|
|
1
|
+
import { extractUploadHeaders, readFileHeaders } from "../utils/headers.js";
|
|
2
|
+
import { http } from "../utils/http.js";
|
|
3
|
+
import { prepareData } from "../utils/data.js";
|
|
4
|
+
import { makeTar } from "../utils/tar.js";
|
|
5
|
+
import { assertCollection } from "../utils/collection.js";
|
|
6
|
+
import { wrapBytesWithHelpers } from "../utils/bytes.js";
|
|
7
|
+
import { isReadable } from "../utils/stream.js";
|
|
8
|
+
import { makeTagUid } from "../utils/type.js";
|
|
9
9
|
const bzzEndpoint = 'bzz';
|
|
10
|
+
|
|
10
11
|
function extractFileUploadHeaders(postageBatchId, options) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
headers['content-type'] = options.contentType;
|
|
16
|
-
return headers;
|
|
12
|
+
const headers = extractUploadHeaders(postageBatchId, options);
|
|
13
|
+
if (options?.size) headers['content-length'] = String(options.size);
|
|
14
|
+
if (options?.contentType) headers['content-type'] = options.contentType;
|
|
15
|
+
return headers;
|
|
17
16
|
}
|
|
18
17
|
/**
|
|
19
18
|
* Upload single file
|
|
@@ -24,26 +23,29 @@ function extractFileUploadHeaders(postageBatchId, options) {
|
|
|
24
23
|
* @param name Name that will be attached to the uploaded file. Wraps the data into manifest with set index document.
|
|
25
24
|
* @param options
|
|
26
25
|
*/
|
|
26
|
+
|
|
27
|
+
|
|
27
28
|
export async function uploadFile(ky, data, postageBatchId, name, options) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
29
|
+
if (isReadable(data) && !options?.contentType) {
|
|
30
|
+
if (!options) options = {};
|
|
31
|
+
options.contentType = 'application/octet-stream';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const response = await http(ky, {
|
|
35
|
+
method: 'post',
|
|
36
|
+
path: bzzEndpoint,
|
|
37
|
+
body: await prepareData(data),
|
|
38
|
+
headers: { ...extractFileUploadHeaders(postageBatchId, options)
|
|
39
|
+
},
|
|
40
|
+
searchParams: {
|
|
41
|
+
name
|
|
42
|
+
},
|
|
43
|
+
responseType: 'json'
|
|
44
|
+
});
|
|
45
|
+
return {
|
|
46
|
+
reference: response.data.reference,
|
|
47
|
+
tagUid: makeTagUid(response.headers.get('swarm-tag'))
|
|
48
|
+
};
|
|
47
49
|
}
|
|
48
50
|
/**
|
|
49
51
|
* Download single file as a buffer
|
|
@@ -52,17 +54,17 @@ export async function uploadFile(ky, data, postageBatchId, name, options) {
|
|
|
52
54
|
* @param hash Bee file or collection hash
|
|
53
55
|
* @param path If hash is collection then this defines path to a single file in the collection
|
|
54
56
|
*/
|
|
57
|
+
|
|
55
58
|
export async function downloadFile(ky, hash, path = '') {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
return file;
|
|
59
|
+
const response = await http(ky, {
|
|
60
|
+
method: 'GET',
|
|
61
|
+
responseType: 'arraybuffer',
|
|
62
|
+
path: `${bzzEndpoint}/${hash}/${path}`
|
|
63
|
+
});
|
|
64
|
+
const file = { ...readFileHeaders(response.headers),
|
|
65
|
+
data: wrapBytesWithHelpers(new Uint8Array(response.data))
|
|
66
|
+
};
|
|
67
|
+
return file;
|
|
66
68
|
}
|
|
67
69
|
/**
|
|
68
70
|
* Download single file as a readable stream
|
|
@@ -71,25 +73,24 @@ export async function downloadFile(ky, hash, path = '') {
|
|
|
71
73
|
* @param hash Bee file or collection hash
|
|
72
74
|
* @param path If hash is collection then this defines path to a single file in the collection
|
|
73
75
|
*/
|
|
76
|
+
|
|
74
77
|
export async function downloadFileReadable(ky, hash, path = '') {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
return file;
|
|
78
|
+
const response = await http(ky, {
|
|
79
|
+
method: 'GET',
|
|
80
|
+
responseType: 'stream',
|
|
81
|
+
path: `${bzzEndpoint}/${hash}/${path}`
|
|
82
|
+
});
|
|
83
|
+
const file = { ...readFileHeaders(response.headers),
|
|
84
|
+
data: response.data
|
|
85
|
+
};
|
|
86
|
+
return file;
|
|
85
87
|
}
|
|
88
|
+
|
|
86
89
|
function extractCollectionUploadHeaders(postageBatchId, options) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
headers['swarm-error-document'] = options.errorDocument;
|
|
92
|
-
return headers;
|
|
90
|
+
const headers = extractUploadHeaders(postageBatchId, options);
|
|
91
|
+
if (options?.indexDocument) headers['swarm-index-document'] = options.indexDocument;
|
|
92
|
+
if (options?.errorDocument) headers['swarm-error-document'] = options.errorDocument;
|
|
93
|
+
return headers;
|
|
93
94
|
}
|
|
94
95
|
/**
|
|
95
96
|
* Upload collection
|
|
@@ -98,22 +99,24 @@ function extractCollectionUploadHeaders(postageBatchId, options) {
|
|
|
98
99
|
* @param postageBatchId Postage BatchId that will be assigned to uploaded data
|
|
99
100
|
* @param options
|
|
100
101
|
*/
|
|
102
|
+
|
|
103
|
+
|
|
101
104
|
export async function uploadCollection(ky, collection, postageBatchId, options) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
}
|
|
105
|
+
assertCollection(collection);
|
|
106
|
+
const tarData = makeTar(collection);
|
|
107
|
+
const response = await http(ky, {
|
|
108
|
+
method: 'post',
|
|
109
|
+
path: bzzEndpoint,
|
|
110
|
+
body: tarData,
|
|
111
|
+
responseType: 'json',
|
|
112
|
+
headers: {
|
|
113
|
+
'content-type': 'application/x-tar',
|
|
114
|
+
'swarm-collection': 'true',
|
|
115
|
+
...extractCollectionUploadHeaders(postageBatchId, options)
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
return {
|
|
119
|
+
reference: response.data.reference,
|
|
120
|
+
tagUid: makeTagUid(response.headers.get('swarm-tag'))
|
|
121
|
+
};
|
|
122
|
+
}
|