@upcoming/bee-js 0.5.5 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/bee.js +15 -12
- package/dist/cjs/manifest/manifest.js +83 -0
- package/dist/cjs/modules/bytes.js +4 -6
- package/dist/cjs/modules/bzz.js +2 -3
- package/dist/cjs/utils/resource-locator.js +17 -0
- package/dist/index.browser.min.js +1 -1
- package/dist/index.browser.min.js.map +1 -1
- package/dist/mjs/bee.js +16 -13
- package/dist/mjs/manifest/manifest.js +86 -0
- package/dist/mjs/modules/bytes.js +4 -6
- package/dist/mjs/modules/bzz.js +2 -3
- package/dist/mjs/utils/resource-locator.js +13 -0
- package/dist/types/bee.d.ts +8 -6
- package/dist/types/manifest/manifest.d.ts +61 -0
- package/dist/types/modules/bytes.d.ts +3 -2
- package/dist/types/modules/bzz.d.ts +2 -1
- package/dist/types/utils/resource-locator.d.ts +6 -0
- package/package.json +2 -2
package/dist/cjs/bee.js
CHANGED
|
@@ -58,6 +58,7 @@ const collection_node_1 = require("./utils/collection.node");
|
|
|
58
58
|
const data_1 = require("./utils/data");
|
|
59
59
|
const error_1 = require("./utils/error");
|
|
60
60
|
const file_1 = require("./utils/file");
|
|
61
|
+
const resource_locator_1 = require("./utils/resource-locator");
|
|
61
62
|
const tokens_1 = require("./utils/tokens");
|
|
62
63
|
const type_1 = require("./utils/type");
|
|
63
64
|
const typed_bytes_1 = require("./utils/typed-bytes");
|
|
@@ -122,36 +123,34 @@ class Bee {
|
|
|
122
123
|
/**
|
|
123
124
|
* Download data as a byte array
|
|
124
125
|
*
|
|
125
|
-
* @param
|
|
126
|
+
* @param resource Swarm reference, Swarm CID, or ENS domain
|
|
126
127
|
* @param options Options that affects the request behavior
|
|
127
128
|
* @throws TypeError if some of the input parameters is not expected type
|
|
128
129
|
* @throws BeeArgumentError if there is passed ENS domain with invalid unicode characters
|
|
129
130
|
* @see [Bee docs - Upload and download](https://docs.ethswarm.org/docs/develop/access-the-swarm/upload-and-download)
|
|
130
131
|
* @see [Bee API reference - `GET /bytes`](https://docs.ethswarm.org/api/#tag/Bytes/paths/~1bytes~1{reference}/get)
|
|
131
132
|
*/
|
|
132
|
-
async downloadData(
|
|
133
|
-
reference = new typed_bytes_1.Reference(reference);
|
|
133
|
+
async downloadData(resource, options, requestOptions) {
|
|
134
134
|
if (options) {
|
|
135
135
|
options = (0, type_1.prepareDownloadOptions)(options);
|
|
136
136
|
}
|
|
137
|
-
return bytes.download(this.getRequestOptionsForCall(requestOptions),
|
|
137
|
+
return bytes.download(this.getRequestOptionsForCall(requestOptions), new resource_locator_1.ResourceLocator(resource), options);
|
|
138
138
|
}
|
|
139
139
|
/**
|
|
140
140
|
* Download data as a Readable stream
|
|
141
141
|
*
|
|
142
|
-
* @param
|
|
142
|
+
* @param resource Swarm reference, Swarm CID, or ENS domain
|
|
143
143
|
* @param options Options that affects the request behavior
|
|
144
144
|
* @throws TypeError if some of the input parameters is not expected type
|
|
145
145
|
* @throws BeeArgumentError if there is passed ENS domain with invalid unicode characters
|
|
146
146
|
* @see [Bee docs - Upload and download](https://docs.ethswarm.org/docs/develop/access-the-swarm/upload-and-download)
|
|
147
147
|
* @see [Bee API reference - `GET /bytes`](https://docs.ethswarm.org/api/#tag/Bytes/paths/~1bytes~1{reference}/get)
|
|
148
148
|
*/
|
|
149
|
-
async downloadReadableData(
|
|
150
|
-
reference = new typed_bytes_1.Reference(reference);
|
|
149
|
+
async downloadReadableData(resource, options, requestOptions) {
|
|
151
150
|
if (options) {
|
|
152
151
|
options = (0, type_1.prepareDownloadOptions)(options);
|
|
153
152
|
}
|
|
154
|
-
return bytes.downloadReadable(this.getRequestOptionsForCall(requestOptions),
|
|
153
|
+
return bytes.downloadReadable(this.getRequestOptionsForCall(requestOptions), new resource_locator_1.ResourceLocator(resource), options);
|
|
155
154
|
}
|
|
156
155
|
/**
|
|
157
156
|
* Upload chunk to a Bee node
|
|
@@ -276,7 +275,7 @@ class Bee {
|
|
|
276
275
|
/**
|
|
277
276
|
* Download single file.
|
|
278
277
|
*
|
|
279
|
-
* @param
|
|
278
|
+
* @param resource Swarm reference, Swarm CID, or ENS domain
|
|
280
279
|
* @param path If reference points to manifest, then this parameter defines path to the file
|
|
281
280
|
* @param options Options that affects the request behavior
|
|
282
281
|
* @throws TypeError if some of the input parameters is not expected type
|
|
@@ -285,12 +284,11 @@ class Bee {
|
|
|
285
284
|
* @see [Bee docs - Upload and download](https://docs.ethswarm.org/docs/develop/access-the-swarm/upload-and-download)
|
|
286
285
|
* @see [Bee API reference - `GET /bzz`](https://docs.ethswarm.org/api/#tag/BZZ/paths/~1bzz~1%7Breference%7D~1%7Bpath%7D/get)
|
|
287
286
|
*/
|
|
288
|
-
async downloadFile(
|
|
289
|
-
reference = new typed_bytes_1.Reference(reference);
|
|
287
|
+
async downloadFile(resource, path = '', options, requestOptions) {
|
|
290
288
|
if (options) {
|
|
291
289
|
options = (0, type_1.prepareDownloadOptions)(options);
|
|
292
290
|
}
|
|
293
|
-
return bzz.downloadFile(this.getRequestOptionsForCall(requestOptions),
|
|
291
|
+
return bzz.downloadFile(this.getRequestOptionsForCall(requestOptions), new resource_locator_1.ResourceLocator(resource), path, options);
|
|
294
292
|
}
|
|
295
293
|
/**
|
|
296
294
|
* Download single file as a readable stream
|
|
@@ -824,6 +822,11 @@ class Bee {
|
|
|
824
822
|
}
|
|
825
823
|
return (0, feed_1.makeFeedWriter)(this.getRequestOptionsForCall(options), topic, signer);
|
|
826
824
|
}
|
|
825
|
+
async fetchLatestFeedUpdate(topic, owner, requestOptions) {
|
|
826
|
+
topic = new typed_bytes_1.Topic(topic);
|
|
827
|
+
owner = new typed_bytes_1.EthAddress(owner);
|
|
828
|
+
return (0, feed_2.fetchLatestFeedUpdate)(this.getRequestOptionsForCall(requestOptions), owner, topic);
|
|
829
|
+
}
|
|
827
830
|
/**
|
|
828
831
|
* Returns an object for reading single owner chunks
|
|
829
832
|
*
|
|
@@ -102,6 +102,9 @@ class MantarayNode {
|
|
|
102
102
|
get fullPathString() {
|
|
103
103
|
return DECODER.decode(this.fullPath);
|
|
104
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* Returns the metadata at the `/` path to access idiomatic properties.
|
|
107
|
+
*/
|
|
105
108
|
getRootMetadata() {
|
|
106
109
|
const node = this.find('/');
|
|
107
110
|
if (node && node.metadata) {
|
|
@@ -109,6 +112,34 @@ class MantarayNode {
|
|
|
109
112
|
}
|
|
110
113
|
return cafe_utility_1.Optional.empty();
|
|
111
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* Returns the `swarm-index-document` and `swarm-error-document` metadata values.
|
|
117
|
+
*/
|
|
118
|
+
getDocsMetadata() {
|
|
119
|
+
const node = this.find('/');
|
|
120
|
+
if (!node || !node.metadata) {
|
|
121
|
+
return { indexDocument: null, errorDocument: null };
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
indexDocument: node.metadata['website-index-document'] ?? null,
|
|
125
|
+
errorDocument: node.metadata['website-error-document'] ?? null,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Attempts to resolve the manifest as a feed, returning the latest update.
|
|
130
|
+
*/
|
|
131
|
+
async resolveFeed(bee, requestOptions) {
|
|
132
|
+
const node = this.find('/');
|
|
133
|
+
if (!node || !node.metadata) {
|
|
134
|
+
return cafe_utility_1.Optional.empty();
|
|
135
|
+
}
|
|
136
|
+
const owner = node.metadata['swarm-feed-owner'];
|
|
137
|
+
const topic = node.metadata['swarm-feed-topic'];
|
|
138
|
+
return cafe_utility_1.Optional.of(await bee.fetchLatestFeedUpdate(topic, owner, requestOptions));
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Gets the binary representation of the node.
|
|
142
|
+
*/
|
|
112
143
|
async marshal() {
|
|
113
144
|
for (const fork of this.forks.values()) {
|
|
114
145
|
if (!fork.node.selfAddress) {
|
|
@@ -135,8 +166,21 @@ class MantarayNode {
|
|
|
135
166
|
: this.targetAddress, forkBitmap, ...forks), this.obfuscationKey);
|
|
136
167
|
return cafe_utility_1.Binary.concatBytes(this.obfuscationKey, data);
|
|
137
168
|
}
|
|
169
|
+
/**
|
|
170
|
+
* Downloads and unmarshals a MantarayNode from the given reference.
|
|
171
|
+
*
|
|
172
|
+
* Do not forget calling `loadRecursively` on the returned node to load the entire tree.
|
|
173
|
+
*/
|
|
138
174
|
static async unmarshal(bee, reference, options, requestOptions) {
|
|
139
175
|
const data = (await bee.downloadData(reference, options, requestOptions)).toUint8Array();
|
|
176
|
+
return this.unmarshalFromData(data);
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Unmarshals a MantarayNode from the given data.
|
|
180
|
+
*
|
|
181
|
+
* Do not forget calling `loadRecursively` on the returned node to load the entire tree.
|
|
182
|
+
*/
|
|
183
|
+
static unmarshalFromData(data) {
|
|
140
184
|
const obfuscationKey = data.subarray(0, 32);
|
|
141
185
|
const decrypted = cafe_utility_1.Binary.xorCypher(data.subarray(32), obfuscationKey);
|
|
142
186
|
const reader = new cafe_utility_1.Uint8ArrayReader(decrypted);
|
|
@@ -157,6 +201,9 @@ class MantarayNode {
|
|
|
157
201
|
}
|
|
158
202
|
return node;
|
|
159
203
|
}
|
|
204
|
+
/**
|
|
205
|
+
* Adds a fork to the node.
|
|
206
|
+
*/
|
|
160
207
|
addFork(path, reference, metadata) {
|
|
161
208
|
this.selfAddress = null;
|
|
162
209
|
path = path instanceof Uint8Array ? path : ENCODER.encode(path);
|
|
@@ -196,6 +243,9 @@ class MantarayNode {
|
|
|
196
243
|
}
|
|
197
244
|
}
|
|
198
245
|
}
|
|
246
|
+
/**
|
|
247
|
+
* Removes a fork from the node.
|
|
248
|
+
*/
|
|
199
249
|
removeFork(path) {
|
|
200
250
|
this.selfAddress = null;
|
|
201
251
|
path = path instanceof Uint8Array ? path : ENCODER.encode(path);
|
|
@@ -212,12 +262,18 @@ class MantarayNode {
|
|
|
212
262
|
parent.addFork(cafe_utility_1.Binary.concatBytes(match.path, fork.prefix), fork.node.targetAddress, fork.node.metadata);
|
|
213
263
|
}
|
|
214
264
|
}
|
|
265
|
+
/**
|
|
266
|
+
* Calculates the self address of the node.
|
|
267
|
+
*/
|
|
215
268
|
async calculateSelfAddress() {
|
|
216
269
|
if (this.selfAddress) {
|
|
217
270
|
return new typed_bytes_1.Reference(this.selfAddress);
|
|
218
271
|
}
|
|
219
272
|
return new typed_bytes_1.Reference((await cafe_utility_1.MerkleTree.root(await this.marshal())).hash());
|
|
220
273
|
}
|
|
274
|
+
/**
|
|
275
|
+
* Saves the node and its children recursively.
|
|
276
|
+
*/
|
|
221
277
|
async saveRecursively(bee, postageBatchId, options, requestOptions) {
|
|
222
278
|
for (const fork of this.forks.values()) {
|
|
223
279
|
await fork.node.saveRecursively(bee, postageBatchId, options, requestOptions);
|
|
@@ -226,6 +282,9 @@ class MantarayNode {
|
|
|
226
282
|
this.selfAddress = result.reference.toUint8Array();
|
|
227
283
|
return result;
|
|
228
284
|
}
|
|
285
|
+
/**
|
|
286
|
+
* Loads the node and its children recursively.
|
|
287
|
+
*/
|
|
229
288
|
async loadRecursively(bee, options, requestOptions) {
|
|
230
289
|
for (const fork of this.forks.values()) {
|
|
231
290
|
const node = await MantarayNode.unmarshal(bee, fork.node.targetAddress, options, requestOptions);
|
|
@@ -236,10 +295,16 @@ class MantarayNode {
|
|
|
236
295
|
await fork.node.loadRecursively(bee, options, requestOptions);
|
|
237
296
|
}
|
|
238
297
|
}
|
|
298
|
+
/**
|
|
299
|
+
* Finds a node in the tree by its path.
|
|
300
|
+
*/
|
|
239
301
|
find(path) {
|
|
240
302
|
const [closest, match] = this.findClosest(path);
|
|
241
303
|
return match.length === path.length ? closest : null;
|
|
242
304
|
}
|
|
305
|
+
/**
|
|
306
|
+
* Finds the closest node in the tree to the given path.
|
|
307
|
+
*/
|
|
243
308
|
findClosest(path, current = new Uint8Array()) {
|
|
244
309
|
path = path instanceof Uint8Array ? path : ENCODER.encode(path);
|
|
245
310
|
if (path.length === 0) {
|
|
@@ -251,6 +316,11 @@ class MantarayNode {
|
|
|
251
316
|
}
|
|
252
317
|
return [this, current];
|
|
253
318
|
}
|
|
319
|
+
/**
|
|
320
|
+
* Returns an array of all nodes in the tree which have a target address set.
|
|
321
|
+
*
|
|
322
|
+
* Must be called after `loadRecursively`.
|
|
323
|
+
*/
|
|
254
324
|
collect(nodes = []) {
|
|
255
325
|
for (const fork of this.forks.values()) {
|
|
256
326
|
if (!cafe_utility_1.Binary.equals(fork.node.targetAddress, __1.NULL_ADDRESS)) {
|
|
@@ -260,6 +330,19 @@ class MantarayNode {
|
|
|
260
330
|
}
|
|
261
331
|
return nodes;
|
|
262
332
|
}
|
|
333
|
+
/**
|
|
334
|
+
* Returns a path:reference map of all nodes in the tree which have a target address set.
|
|
335
|
+
*
|
|
336
|
+
* Must be called after `loadRecursively`.
|
|
337
|
+
*/
|
|
338
|
+
collectAndMap() {
|
|
339
|
+
const nodes = this.collect();
|
|
340
|
+
const result = {};
|
|
341
|
+
for (const node of nodes) {
|
|
342
|
+
result[node.fullPathString] = new typed_bytes_1.Reference(node.targetAddress).toHex();
|
|
343
|
+
}
|
|
344
|
+
return result;
|
|
345
|
+
}
|
|
263
346
|
determineType() {
|
|
264
347
|
let type = 0;
|
|
265
348
|
if (!cafe_utility_1.Binary.equals(this.targetAddress, __1.NULL_ADDRESS) || cafe_utility_1.Binary.equals(this.path, PATH_SEPARATOR)) {
|
|
@@ -61,14 +61,13 @@ exports.head = head;
|
|
|
61
61
|
* @param requestOptions Options for making requests
|
|
62
62
|
* @param hash Bee content reference
|
|
63
63
|
*/
|
|
64
|
-
async function download(requestOptions,
|
|
65
|
-
reference = new typed_bytes_1.Reference(reference);
|
|
64
|
+
async function download(requestOptions, resource, options) {
|
|
66
65
|
if (options) {
|
|
67
66
|
options = (0, type_1.prepareDownloadOptions)(options);
|
|
68
67
|
}
|
|
69
68
|
const response = await (0, http_1.http)(requestOptions, {
|
|
70
69
|
responseType: 'arraybuffer',
|
|
71
|
-
url: `${endpoint}/${
|
|
70
|
+
url: `${endpoint}/${resource}`,
|
|
72
71
|
headers: (0, headers_1.prepareRequestHeaders)(null, options),
|
|
73
72
|
});
|
|
74
73
|
return new bytes_1.Bytes(response.data);
|
|
@@ -80,14 +79,13 @@ exports.download = download;
|
|
|
80
79
|
* @param requestOptions Options for making requests
|
|
81
80
|
* @param hash Bee content reference
|
|
82
81
|
*/
|
|
83
|
-
async function downloadReadable(requestOptions,
|
|
84
|
-
reference = new typed_bytes_1.Reference(reference);
|
|
82
|
+
async function downloadReadable(requestOptions, resource, options) {
|
|
85
83
|
if (options) {
|
|
86
84
|
options = (0, type_1.prepareDownloadOptions)(options);
|
|
87
85
|
}
|
|
88
86
|
const response = await (0, http_1.http)(requestOptions, {
|
|
89
87
|
responseType: 'stream',
|
|
90
|
-
url: `${endpoint}/${
|
|
88
|
+
url: `${endpoint}/${resource}`,
|
|
91
89
|
headers: (0, headers_1.prepareRequestHeaders)(null, options),
|
|
92
90
|
});
|
|
93
91
|
return response.data;
|
package/dist/cjs/modules/bzz.js
CHANGED
|
@@ -51,12 +51,11 @@ exports.uploadFile = uploadFile;
|
|
|
51
51
|
* @param hash Bee file or collection hash
|
|
52
52
|
* @param path If hash is collection then this defines path to a single file in the collection
|
|
53
53
|
*/
|
|
54
|
-
async function downloadFile(requestOptions,
|
|
55
|
-
reference = new typed_bytes_1.Reference(reference);
|
|
54
|
+
async function downloadFile(requestOptions, resource, path = '', options) {
|
|
56
55
|
const response = await (0, http_1.http)(requestOptions, {
|
|
57
56
|
method: 'GET',
|
|
58
57
|
responseType: 'arraybuffer',
|
|
59
|
-
url: `${bzzEndpoint}/${
|
|
58
|
+
url: `${bzzEndpoint}/${resource}/${path}`,
|
|
60
59
|
headers: (0, headers_1.prepareRequestHeaders)(null, options),
|
|
61
60
|
});
|
|
62
61
|
const file = {
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ResourceLocator = void 0;
|
|
4
|
+
const cafe_utility_1 = require("cafe-utility");
|
|
5
|
+
const typed_bytes_1 = require("./typed-bytes");
|
|
6
|
+
class ResourceLocator {
|
|
7
|
+
constructor(raw) {
|
|
8
|
+
this.raw = raw;
|
|
9
|
+
}
|
|
10
|
+
toString() {
|
|
11
|
+
if (cafe_utility_1.Types.isString(this.raw) && this.raw.includes('.eth')) {
|
|
12
|
+
return this.raw;
|
|
13
|
+
}
|
|
14
|
+
return new typed_bytes_1.Reference(this.raw).toHex();
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.ResourceLocator = ResourceLocator;
|