@upcoming/bee-js 0.4.0 → 0.5.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-dev.js +0 -3
- package/dist/cjs/bee.js +69 -113
- package/dist/cjs/feed/retrievable.js +4 -4
- package/dist/cjs/modules/bytes.js +12 -4
- package/dist/cjs/modules/bzz.js +12 -27
- package/dist/cjs/modules/chunk.js +6 -3
- package/dist/cjs/modules/feed.js +1 -1
- package/dist/cjs/modules/grantee.js +9 -8
- package/dist/cjs/modules/pss.js +1 -1
- package/dist/cjs/modules/soc.js +4 -2
- package/dist/cjs/utils/collection.js +1 -1
- package/dist/cjs/utils/collection.node.js +0 -1
- package/dist/cjs/utils/expose.js +2 -1
- package/dist/cjs/utils/headers.js +61 -54
- package/dist/cjs/utils/http.js +9 -0
- package/dist/cjs/utils/tar-uploader.browser.js +2 -2
- package/dist/cjs/utils/tar-uploader.js +2 -2
- package/dist/cjs/utils/type.js +119 -191
- package/dist/index.browser.min.js +1 -1
- package/dist/index.browser.min.js.map +1 -1
- package/dist/mjs/bee-dev.js +0 -3
- package/dist/mjs/bee.js +75 -117
- package/dist/mjs/feed/retrievable.js +4 -4
- package/dist/mjs/modules/bytes.js +13 -7
- package/dist/mjs/modules/bzz.js +9 -25
- package/dist/mjs/modules/chunk.js +7 -6
- package/dist/mjs/modules/feed.js +2 -2
- package/dist/mjs/modules/grantee.js +12 -11
- package/dist/mjs/modules/pss.js +2 -2
- package/dist/mjs/modules/soc.js +4 -4
- package/dist/mjs/utils/collection.js +1 -1
- package/dist/mjs/utils/collection.node.js +0 -1
- package/dist/mjs/utils/expose.js +1 -1
- package/dist/mjs/utils/headers.js +59 -50
- package/dist/mjs/utils/http.js +9 -0
- package/dist/mjs/utils/tar-uploader.browser.js +2 -2
- package/dist/mjs/utils/tar-uploader.js +2 -2
- package/dist/mjs/utils/type.js +202 -163
- package/dist/types/bee.d.ts +20 -20
- package/dist/types/feed/retrievable.d.ts +2 -2
- package/dist/types/modules/bytes.d.ts +4 -4
- package/dist/types/modules/bzz.d.ts +5 -10
- package/dist/types/modules/chunk.d.ts +2 -2
- package/dist/types/modules/debug/chequebook.d.ts +2 -2
- package/dist/types/modules/grantee.d.ts +4 -4
- package/dist/types/types/debug.d.ts +0 -1
- package/dist/types/types/index.d.ts +12 -6
- package/dist/types/utils/collection.d.ts +1 -1
- package/dist/types/utils/collection.node.d.ts +0 -1
- package/dist/types/utils/expose.d.ts +1 -1
- package/dist/types/utils/headers.d.ts +2 -4
- package/dist/types/utils/tar-uploader.browser.d.ts +2 -2
- package/dist/types/utils/tar-uploader.d.ts +2 -2
- package/dist/types/utils/type.d.ts +12 -18
- package/package.json +2 -2
|
@@ -9,9 +9,9 @@ import { getFeedUpdateChunkReference } from "./index.js";
|
|
|
9
9
|
* @param ref
|
|
10
10
|
* @param options
|
|
11
11
|
*/
|
|
12
|
-
async function isChunkRetrievable(bee, reference, requestOptions) {
|
|
12
|
+
async function isChunkRetrievable(bee, reference, options, requestOptions) {
|
|
13
13
|
try {
|
|
14
|
-
await bee.downloadChunk(reference, requestOptions);
|
|
14
|
+
await bee.downloadChunk(reference, options, requestOptions);
|
|
15
15
|
return true;
|
|
16
16
|
} catch (e) {
|
|
17
17
|
const status = Objects.getDeep(e, 'status');
|
|
@@ -36,7 +36,7 @@ function getAllSequenceUpdateReferences(owner, topic, index) {
|
|
|
36
36
|
}
|
|
37
37
|
return updateReferences;
|
|
38
38
|
}
|
|
39
|
-
export async function areAllSequentialFeedsUpdateRetrievable(bee, owner, topic, index, requestOptions) {
|
|
40
|
-
const chunkRetrievablePromises = getAllSequenceUpdateReferences(owner, topic, index).map(async reference => isChunkRetrievable(bee, reference, requestOptions));
|
|
39
|
+
export async function areAllSequentialFeedsUpdateRetrievable(bee, owner, topic, index, options, requestOptions) {
|
|
40
|
+
const chunkRetrievablePromises = getAllSequenceUpdateReferences(owner, topic, index).map(async reference => isChunkRetrievable(bee, reference, options, requestOptions));
|
|
41
41
|
return (await Promise.all(chunkRetrievablePromises)).every(result => result);
|
|
42
42
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Types } from 'cafe-utility';
|
|
1
|
+
import { Optional, Types } from 'cafe-utility';
|
|
2
2
|
import { Bytes } from "../utils/bytes.js";
|
|
3
|
-
import {
|
|
3
|
+
import { prepareRequestHeaders } from "../utils/headers.js";
|
|
4
4
|
import { http } from "../utils/http.js";
|
|
5
|
-
import { makeTagUid } from "../utils/type.js";
|
|
5
|
+
import { makeTagUid, prepareDownloadOptions } from "../utils/type.js";
|
|
6
6
|
import { Reference } from "../utils/typed-bytes.js";
|
|
7
7
|
const endpoint = 'bytes';
|
|
8
8
|
/**
|
|
@@ -21,7 +21,7 @@ export async function upload(requestOptions, data, postageBatchId, options) {
|
|
|
21
21
|
data,
|
|
22
22
|
headers: {
|
|
23
23
|
'content-type': 'application/octet-stream',
|
|
24
|
-
...
|
|
24
|
+
...prepareRequestHeaders(postageBatchId, options)
|
|
25
25
|
}
|
|
26
26
|
});
|
|
27
27
|
const body = Types.asObject(response.data, {
|
|
@@ -30,7 +30,7 @@ export async function upload(requestOptions, data, postageBatchId, options) {
|
|
|
30
30
|
return {
|
|
31
31
|
reference: new Reference(Types.asHexString(body.reference)),
|
|
32
32
|
tagUid: response.headers['swarm-tag'] ? makeTagUid(response.headers['swarm-tag']) : undefined,
|
|
33
|
-
historyAddress: response.headers['swarm-act-history-address']
|
|
33
|
+
historyAddress: response.headers['swarm-act-history-address'] ? Optional.of(new Reference(response.headers['swarm-act-history-address'])) : Optional.empty()
|
|
34
34
|
};
|
|
35
35
|
}
|
|
36
36
|
/**
|
|
@@ -58,10 +58,13 @@ export async function head(requestOptions, reference) {
|
|
|
58
58
|
*/
|
|
59
59
|
export async function download(requestOptions, reference, options) {
|
|
60
60
|
reference = new Reference(reference);
|
|
61
|
+
if (options) {
|
|
62
|
+
options = prepareDownloadOptions(options);
|
|
63
|
+
}
|
|
61
64
|
const response = await http(requestOptions, {
|
|
62
65
|
responseType: 'arraybuffer',
|
|
63
66
|
url: `${endpoint}/${reference}`,
|
|
64
|
-
headers:
|
|
67
|
+
headers: prepareRequestHeaders(null, options)
|
|
65
68
|
});
|
|
66
69
|
return new Bytes(response.data);
|
|
67
70
|
}
|
|
@@ -73,10 +76,13 @@ export async function download(requestOptions, reference, options) {
|
|
|
73
76
|
*/
|
|
74
77
|
export async function downloadReadable(requestOptions, reference, options) {
|
|
75
78
|
reference = new Reference(reference);
|
|
79
|
+
if (options) {
|
|
80
|
+
options = prepareDownloadOptions(options);
|
|
81
|
+
}
|
|
76
82
|
const response = await http(requestOptions, {
|
|
77
83
|
responseType: 'stream',
|
|
78
84
|
url: `${endpoint}/${reference}`,
|
|
79
|
-
headers:
|
|
85
|
+
headers: prepareRequestHeaders(null, options)
|
|
80
86
|
});
|
|
81
87
|
return response.data;
|
|
82
88
|
}
|
package/dist/mjs/modules/bzz.js
CHANGED
|
@@ -1,18 +1,12 @@
|
|
|
1
|
-
import { Types } from 'cafe-utility';
|
|
1
|
+
import { Optional, Types } from 'cafe-utility';
|
|
2
2
|
import { Bytes } from "../utils/bytes.js";
|
|
3
3
|
import { assertCollection } from "../utils/collection.js";
|
|
4
|
-
import {
|
|
4
|
+
import { prepareRequestHeaders, readFileHeaders } from "../utils/headers.js";
|
|
5
5
|
import { http } from "../utils/http.js";
|
|
6
6
|
import { uploadTar } from "../utils/tar-uploader.js";
|
|
7
7
|
import { isReadable, makeTagUid } from "../utils/type.js";
|
|
8
8
|
import { Reference } from "../utils/typed-bytes.js";
|
|
9
9
|
const bzzEndpoint = 'bzz';
|
|
10
|
-
function extractFileUploadHeaders(postageBatchId, options) {
|
|
11
|
-
const headers = extractRedundantUploadHeaders(postageBatchId, options);
|
|
12
|
-
if (options?.size) headers['content-length'] = String(options.size);
|
|
13
|
-
if (options?.contentType) headers['content-type'] = options.contentType;
|
|
14
|
-
return headers;
|
|
15
|
-
}
|
|
16
10
|
/**
|
|
17
11
|
* Upload single file
|
|
18
12
|
*
|
|
@@ -33,9 +27,7 @@ export async function uploadFile(requestOptions, data, postageBatchId, name, opt
|
|
|
33
27
|
method: 'post',
|
|
34
28
|
url: bzzEndpoint,
|
|
35
29
|
data,
|
|
36
|
-
headers:
|
|
37
|
-
...extractFileUploadHeaders(postageBatchId, options)
|
|
38
|
-
},
|
|
30
|
+
headers: prepareRequestHeaders(postageBatchId, options),
|
|
39
31
|
params: {
|
|
40
32
|
name
|
|
41
33
|
},
|
|
@@ -47,7 +39,7 @@ export async function uploadFile(requestOptions, data, postageBatchId, name, opt
|
|
|
47
39
|
return {
|
|
48
40
|
reference: new Reference(Types.asHexString(body.reference)),
|
|
49
41
|
tagUid: response.headers['swarm-tag'] ? makeTagUid(response.headers['swarm-tag']) : undefined,
|
|
50
|
-
historyAddress: response.headers['swarm-act-history-address']
|
|
42
|
+
historyAddress: response.headers['swarm-act-history-address'] ? Optional.of(new Reference(response.headers['swarm-act-history-address'])) : Optional.empty()
|
|
51
43
|
};
|
|
52
44
|
}
|
|
53
45
|
/**
|
|
@@ -63,7 +55,7 @@ export async function downloadFile(requestOptions, reference, path = '', options
|
|
|
63
55
|
method: 'GET',
|
|
64
56
|
responseType: 'arraybuffer',
|
|
65
57
|
url: `${bzzEndpoint}/${reference}/${path}`,
|
|
66
|
-
headers:
|
|
58
|
+
headers: prepareRequestHeaders(null, options)
|
|
67
59
|
});
|
|
68
60
|
const file = {
|
|
69
61
|
...readFileHeaders(response.headers),
|
|
@@ -84,7 +76,7 @@ export async function downloadFileReadable(requestOptions, reference, path = '',
|
|
|
84
76
|
method: 'GET',
|
|
85
77
|
responseType: 'stream',
|
|
86
78
|
url: `${bzzEndpoint}/${reference}/${path}`,
|
|
87
|
-
headers:
|
|
79
|
+
headers: prepareRequestHeaders(null, options)
|
|
88
80
|
});
|
|
89
81
|
const file = {
|
|
90
82
|
...readFileHeaders(response.headers),
|
|
@@ -92,16 +84,8 @@ export async function downloadFileReadable(requestOptions, reference, path = '',
|
|
|
92
84
|
};
|
|
93
85
|
return file;
|
|
94
86
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
if (options?.indexDocument) {
|
|
98
|
-
headers['swarm-index-document'] = options.indexDocument;
|
|
99
|
-
}
|
|
100
|
-
if (options?.errorDocument) {
|
|
101
|
-
headers['swarm-error-document'] = options.errorDocument;
|
|
102
|
-
}
|
|
103
|
-
return headers;
|
|
104
|
-
}
|
|
87
|
+
/*******************************************************************************************************************/
|
|
88
|
+
// Collections
|
|
105
89
|
/**
|
|
106
90
|
* Upload collection
|
|
107
91
|
* @param requestOptions Options for making requests
|
|
@@ -118,6 +102,6 @@ export async function uploadCollection(requestOptions, collection, postageBatchI
|
|
|
118
102
|
return {
|
|
119
103
|
reference: new Reference(Types.asHexString(body.reference)),
|
|
120
104
|
tagUid: response.headers['swarm-tag'] ? makeTagUid(response.headers['swarm-tag']) : undefined,
|
|
121
|
-
historyAddress: response.headers['swarm-act-history-address']
|
|
105
|
+
historyAddress: response.headers['swarm-act-history-address'] ? Optional.of(new Reference(response.headers['swarm-act-history-address'])) : Optional.empty()
|
|
122
106
|
};
|
|
123
107
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Types } from 'cafe-utility';
|
|
2
|
-
import {
|
|
1
|
+
import { Optional, Types } from 'cafe-utility';
|
|
2
|
+
import { prepareRequestHeaders } from "../utils/headers.js";
|
|
3
3
|
import { http } from "../utils/http.js";
|
|
4
4
|
import { makeTagUid } from "../utils/type.js";
|
|
5
5
|
import { Reference } from "../utils/typed-bytes.js";
|
|
@@ -23,7 +23,7 @@ export async function upload(requestOptions, data, stamp, options) {
|
|
|
23
23
|
data,
|
|
24
24
|
headers: {
|
|
25
25
|
'content-type': 'application/octet-stream',
|
|
26
|
-
...
|
|
26
|
+
...prepareRequestHeaders(stamp, options)
|
|
27
27
|
},
|
|
28
28
|
responseType: 'json'
|
|
29
29
|
});
|
|
@@ -35,7 +35,7 @@ export async function upload(requestOptions, data, stamp, options) {
|
|
|
35
35
|
name: 'reference'
|
|
36
36
|
})),
|
|
37
37
|
tagUid: response.headers['swarm-tag'] ? makeTagUid(response.headers['swarm-tag']) : undefined,
|
|
38
|
-
historyAddress: response.headers['swarm-act-history-address']
|
|
38
|
+
historyAddress: response.headers['swarm-act-history-address'] ? Optional.of(new Reference(response.headers['swarm-act-history-address'])) : Optional.empty()
|
|
39
39
|
};
|
|
40
40
|
}
|
|
41
41
|
/**
|
|
@@ -45,11 +45,12 @@ export async function upload(requestOptions, data, stamp, options) {
|
|
|
45
45
|
* @param hash Bee content reference
|
|
46
46
|
*
|
|
47
47
|
*/
|
|
48
|
-
export async function download(requestOptions, reference) {
|
|
48
|
+
export async function download(requestOptions, reference, options) {
|
|
49
49
|
reference = new Reference(reference);
|
|
50
50
|
const response = await http(requestOptions, {
|
|
51
51
|
responseType: 'arraybuffer',
|
|
52
|
-
url: `${endpoint}/${reference}
|
|
52
|
+
url: `${endpoint}/${reference}`,
|
|
53
|
+
headers: prepareRequestHeaders(null, options)
|
|
53
54
|
});
|
|
54
55
|
return new Uint8Array(response.data);
|
|
55
56
|
}
|
package/dist/mjs/modules/feed.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Types } from 'cafe-utility';
|
|
2
2
|
import { Bytes } from "../utils/bytes.js";
|
|
3
3
|
import { BeeError } from "../utils/error.js";
|
|
4
|
-
import {
|
|
4
|
+
import { prepareRequestHeaders } from "../utils/headers.js";
|
|
5
5
|
import { http } from "../utils/http.js";
|
|
6
6
|
import { FeedIndex, Reference } from "../utils/typed-bytes.js";
|
|
7
7
|
const feedEndpoint = 'feeds';
|
|
@@ -19,7 +19,7 @@ export async function createFeedManifest(requestOptions, owner, topic, stamp, op
|
|
|
19
19
|
method: 'post',
|
|
20
20
|
responseType: 'json',
|
|
21
21
|
url: `${feedEndpoint}/${owner}/${topic}`,
|
|
22
|
-
headers:
|
|
22
|
+
headers: prepareRequestHeaders(stamp, options)
|
|
23
23
|
});
|
|
24
24
|
const body = Types.asObject(response.data, {
|
|
25
25
|
name: 'response.data'
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Types } from 'cafe-utility';
|
|
2
|
-
import {
|
|
2
|
+
import { prepareRequestHeaders } from "../utils/headers.js";
|
|
3
3
|
import { http } from "../utils/http.js";
|
|
4
|
-
import { Reference } from "../utils/typed-bytes.js";
|
|
4
|
+
import { PublicKey, Reference } from "../utils/typed-bytes.js";
|
|
5
5
|
const granteeEndpoint = 'grantee';
|
|
6
6
|
export async function getGrantees(reference, requestOptions) {
|
|
7
7
|
const response = await http(requestOptions, {
|
|
@@ -11,13 +11,13 @@ export async function getGrantees(reference, requestOptions) {
|
|
|
11
11
|
});
|
|
12
12
|
const body = Types.asArray(response.data, {
|
|
13
13
|
name: 'response.data'
|
|
14
|
-
}).map(x => Types.asString(x, {
|
|
14
|
+
}).map(x => new PublicKey(Types.asString(x, {
|
|
15
15
|
name: 'grantee'
|
|
16
|
-
}));
|
|
16
|
+
})));
|
|
17
17
|
return {
|
|
18
18
|
status: response.status,
|
|
19
19
|
statusText: response.statusText,
|
|
20
|
-
|
|
20
|
+
grantees: body
|
|
21
21
|
};
|
|
22
22
|
}
|
|
23
23
|
export async function createGrantees(requestOptions, postageBatchId, grantees) {
|
|
@@ -25,11 +25,9 @@ export async function createGrantees(requestOptions, postageBatchId, grantees) {
|
|
|
25
25
|
method: 'post',
|
|
26
26
|
url: granteeEndpoint,
|
|
27
27
|
data: {
|
|
28
|
-
grantees
|
|
29
|
-
},
|
|
30
|
-
headers: {
|
|
31
|
-
...extractRedundantUploadHeaders(postageBatchId)
|
|
28
|
+
grantees: grantees.map(x => x.toCompressedHex())
|
|
32
29
|
},
|
|
30
|
+
headers: prepareRequestHeaders(postageBatchId),
|
|
33
31
|
responseType: 'json'
|
|
34
32
|
});
|
|
35
33
|
const body = Types.asObject(response.data, {
|
|
@@ -50,9 +48,12 @@ export async function patchGrantees(postageBatchId, reference, historyRef, grant
|
|
|
50
48
|
const response = await http(requestOptions, {
|
|
51
49
|
method: 'patch',
|
|
52
50
|
url: `${granteeEndpoint}/${reference}`,
|
|
53
|
-
data:
|
|
51
|
+
data: {
|
|
52
|
+
add: grantees.add?.map(x => x.toCompressedHex()),
|
|
53
|
+
revoke: grantees.revoke?.map(x => x.toCompressedHex())
|
|
54
|
+
},
|
|
54
55
|
headers: {
|
|
55
|
-
...
|
|
56
|
+
...prepareRequestHeaders(postageBatchId),
|
|
56
57
|
'swarm-act-history-address': historyRef.toHex()
|
|
57
58
|
},
|
|
58
59
|
responseType: 'json'
|
package/dist/mjs/modules/pss.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import WebSocket from 'isomorphic-ws';
|
|
2
|
-
import {
|
|
2
|
+
import { prepareRequestHeaders } from "../utils/headers.js";
|
|
3
3
|
import { http } from "../utils/http.js";
|
|
4
4
|
const endpoint = 'pss';
|
|
5
5
|
/**
|
|
@@ -22,7 +22,7 @@ export async function send(requestOptions, topic, target, data, postageBatchId,
|
|
|
22
22
|
params: {
|
|
23
23
|
recipient
|
|
24
24
|
},
|
|
25
|
-
headers:
|
|
25
|
+
headers: prepareRequestHeaders(postageBatchId)
|
|
26
26
|
});
|
|
27
27
|
}
|
|
28
28
|
/**
|
package/dist/mjs/modules/soc.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Types } from 'cafe-utility';
|
|
2
|
-
import {
|
|
1
|
+
import { Optional, Types } from 'cafe-utility';
|
|
2
|
+
import { prepareRequestHeaders } from "../utils/headers.js";
|
|
3
3
|
import { http } from "../utils/http.js";
|
|
4
4
|
import { makeTagUid } from "../utils/type.js";
|
|
5
5
|
import { Reference } from "../utils/typed-bytes.js";
|
|
@@ -22,7 +22,7 @@ export async function upload(requestOptions, owner, identifier, signature, data,
|
|
|
22
22
|
data,
|
|
23
23
|
headers: {
|
|
24
24
|
'content-type': 'application/octet-stream',
|
|
25
|
-
...
|
|
25
|
+
...prepareRequestHeaders(stamp, options)
|
|
26
26
|
},
|
|
27
27
|
responseType: 'json',
|
|
28
28
|
params: {
|
|
@@ -35,6 +35,6 @@ export async function upload(requestOptions, owner, identifier, signature, data,
|
|
|
35
35
|
return {
|
|
36
36
|
reference: new Reference(Types.asHexString(body.reference)),
|
|
37
37
|
tagUid: response.headers['swarm-tag'] ? makeTagUid(response.headers['swarm-tag']) : undefined,
|
|
38
|
-
historyAddress: response.headers['swarm-act-history-address']
|
|
38
|
+
historyAddress: response.headers['swarm-act-history-address'] ? Optional.of(new Reference(response.headers['swarm-act-history-address'])) : Optional.empty()
|
|
39
39
|
};
|
|
40
40
|
}
|
|
@@ -19,7 +19,7 @@ export 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,
|
package/dist/mjs/utils/expose.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { getCollectionSize } from "./collection.js";
|
|
1
|
+
export { getCollectionSize, makeCollectionFromFileList } from "./collection.js";
|
|
2
2
|
export { getFolderSize } from "./collection.node.js";
|
|
3
3
|
export { makeMaxTarget } from "./pss.js";
|
|
4
4
|
export { approximateOverheadForRedundancyLevel, getRedundancyStat, getRedundancyStats } from "./redundancy.js";
|
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import { Types } from 'cafe-utility';
|
|
2
2
|
import { BeeError } from "./error.js";
|
|
3
3
|
import { convertEnvelopeToMarshaledStamp } from "./stamps.js";
|
|
4
|
-
import { BatchId } from "./typed-bytes.js";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
import { BatchId, PublicKey, Reference } from "./typed-bytes.js";
|
|
5
|
+
export function readFileHeaders(headers) {
|
|
6
|
+
const name = readContentDispositionFilename(headers['content-disposition']);
|
|
7
|
+
const tagUid = readTagUid(headers['swarm-tag-uid']);
|
|
8
|
+
const contentType = headers['content-type'] || undefined;
|
|
9
|
+
return {
|
|
10
|
+
name,
|
|
11
|
+
tagUid,
|
|
12
|
+
contentType
|
|
13
|
+
};
|
|
14
|
+
}
|
|
13
15
|
function readContentDispositionFilename(header) {
|
|
14
16
|
if (!header) {
|
|
15
17
|
throw new BeeError('missing content-disposition header');
|
|
@@ -28,42 +30,69 @@ function readTagUid(header) {
|
|
|
28
30
|
}
|
|
29
31
|
return parseInt(header, 10);
|
|
30
32
|
}
|
|
31
|
-
export function
|
|
32
|
-
const name = readContentDispositionFilename(headers['content-disposition']);
|
|
33
|
-
const tagUid = readTagUid(headers['swarm-tag-uid']);
|
|
34
|
-
const contentType = headers['content-type'] || undefined;
|
|
35
|
-
return {
|
|
36
|
-
name,
|
|
37
|
-
tagUid,
|
|
38
|
-
contentType
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
export function extractUploadHeaders(stamp, options) {
|
|
42
|
-
if (!stamp) {
|
|
43
|
-
throw new BeeError('Stamp has to be specified!');
|
|
44
|
-
}
|
|
33
|
+
export function prepareRequestHeaders(stamp, nullableOptions) {
|
|
45
34
|
const headers = {};
|
|
46
35
|
if (isEnvelopeWithBatchId(stamp)) {
|
|
47
36
|
headers['swarm-postage-stamp'] = convertEnvelopeToMarshaledStamp(stamp).toHex();
|
|
48
|
-
} else {
|
|
37
|
+
} else if (stamp) {
|
|
49
38
|
stamp = new BatchId(stamp);
|
|
50
39
|
headers['swarm-postage-batch-id'] = stamp.toHex();
|
|
51
40
|
}
|
|
52
|
-
if (
|
|
41
|
+
if (!nullableOptions) {
|
|
42
|
+
return headers;
|
|
43
|
+
}
|
|
44
|
+
const options = Types.asObject(nullableOptions);
|
|
45
|
+
if (options.size) {
|
|
46
|
+
headers['content-length'] = String(options.size);
|
|
47
|
+
}
|
|
48
|
+
if (options.contentType) {
|
|
49
|
+
headers['content-type'] = String(options.contentType);
|
|
50
|
+
}
|
|
51
|
+
if (options.redundancyLevel) {
|
|
52
|
+
headers['swarm-redundancy-level'] = String(options.redundancyLevel);
|
|
53
|
+
}
|
|
54
|
+
if (Types.isBoolean(options.act)) {
|
|
53
55
|
headers['swarm-act'] = String(options.act);
|
|
54
56
|
}
|
|
55
|
-
if (options
|
|
57
|
+
if (Types.isBoolean(options.pin)) {
|
|
56
58
|
headers['swarm-pin'] = String(options.pin);
|
|
57
59
|
}
|
|
58
|
-
if (options
|
|
59
|
-
headers['swarm-encrypt'] =
|
|
60
|
+
if (Types.isBoolean(options.encrypt)) {
|
|
61
|
+
headers['swarm-encrypt'] = options.encrypt.toString();
|
|
60
62
|
}
|
|
61
|
-
if (options
|
|
63
|
+
if (options.tag) {
|
|
62
64
|
headers['swarm-tag'] = String(options.tag);
|
|
63
65
|
}
|
|
64
|
-
if (
|
|
66
|
+
if (Types.isBoolean(options.deferred)) {
|
|
65
67
|
headers['swarm-deferred-upload'] = options.deferred.toString();
|
|
66
68
|
}
|
|
69
|
+
if (options.redundancyStrategy) {
|
|
70
|
+
headers['swarm-redundancy-strategy'] = String(options.redundancyStrategy);
|
|
71
|
+
}
|
|
72
|
+
if (Types.isBoolean(options.fallback)) {
|
|
73
|
+
headers['swarm-redundancy-fallback-mode'] = options.fallback.toString();
|
|
74
|
+
}
|
|
75
|
+
if (options.timeoutMs) {
|
|
76
|
+
headers['swarm-chunk-retrieval-timeout'] = String(options.timeoutMs);
|
|
77
|
+
}
|
|
78
|
+
if (options.indexDocument) {
|
|
79
|
+
headers['swarm-index-document'] = String(options.indexDocument);
|
|
80
|
+
}
|
|
81
|
+
if (options.errorDocument) {
|
|
82
|
+
headers['swarm-error-document'] = String(options.errorDocument);
|
|
83
|
+
}
|
|
84
|
+
if (options.actPublisher) {
|
|
85
|
+
headers['swarm-act-publisher'] = new PublicKey(options.actPublisher).toCompressedHex();
|
|
86
|
+
}
|
|
87
|
+
if (options.actHistoryAddress) {
|
|
88
|
+
headers['swarm-act-history-address'] = new Reference(options.actHistoryAddress).toHex();
|
|
89
|
+
}
|
|
90
|
+
if (options.actTimestamp) {
|
|
91
|
+
headers['swarm-act-timestamp'] = String(options.actTimestamp);
|
|
92
|
+
}
|
|
93
|
+
if (options.actPublisher || options.actHistoryAddress || options.actTimestamp) {
|
|
94
|
+
headers['swarm-act'] = 'true';
|
|
95
|
+
}
|
|
67
96
|
return headers;
|
|
68
97
|
}
|
|
69
98
|
function isEnvelopeWithBatchId(value) {
|
|
@@ -72,24 +101,4 @@ function isEnvelopeWithBatchId(value) {
|
|
|
72
101
|
}
|
|
73
102
|
const envelope = value;
|
|
74
103
|
return envelope.issuer !== undefined && envelope.index !== undefined && envelope.signature !== undefined && envelope.timestamp !== undefined && envelope.batchId !== undefined;
|
|
75
|
-
}
|
|
76
|
-
export function extractRedundantUploadHeaders(postageBatchId, options) {
|
|
77
|
-
const headers = extractUploadHeaders(postageBatchId, options);
|
|
78
|
-
if (options?.redundancyLevel) {
|
|
79
|
-
headers['swarm-redundancy-level'] = String(options.redundancyLevel);
|
|
80
|
-
}
|
|
81
|
-
return headers;
|
|
82
|
-
}
|
|
83
|
-
export function extractDownloadHeaders(options) {
|
|
84
|
-
const headers = {};
|
|
85
|
-
if (options?.redundancyStrategy) {
|
|
86
|
-
headers['swarm-redundancy-strategy'] = String(options.redundancyStrategy);
|
|
87
|
-
}
|
|
88
|
-
if (options?.fallback === false) {
|
|
89
|
-
headers['swarm-redundancy-fallback-mode'] = 'false';
|
|
90
|
-
}
|
|
91
|
-
if (options?.timeoutMs !== undefined) {
|
|
92
|
-
headers['swarm-chunk-retrieval-timeout'] = String(options.timeoutMs);
|
|
93
|
-
}
|
|
94
|
-
return headers;
|
|
95
104
|
}
|
package/dist/mjs/utils/http.js
CHANGED
|
@@ -19,6 +19,15 @@ export const DEFAULT_HTTP_CONFIG = {
|
|
|
19
19
|
*/
|
|
20
20
|
export async function http(options, config) {
|
|
21
21
|
const requestConfig = Objects.deepMerge3(DEFAULT_HTTP_CONFIG, config, options);
|
|
22
|
+
if (requestConfig.params) {
|
|
23
|
+
const keys = Object.keys(requestConfig.params);
|
|
24
|
+
for (const key of keys) {
|
|
25
|
+
const value = requestConfig.params[key];
|
|
26
|
+
if (value === undefined) {
|
|
27
|
+
delete requestConfig.params[key];
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
22
31
|
let failedAttempts = 0;
|
|
23
32
|
while (failedAttempts < MAX_FAILED_ATTEMPTS) {
|
|
24
33
|
try {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { prepareRequestHeaders } from "./headers.js";
|
|
2
2
|
import { http } from "./http.js";
|
|
3
3
|
import { TarStream } from "./tar.js";
|
|
4
4
|
import { writeTar } from "./tar-writer.js";
|
|
@@ -15,7 +15,7 @@ export async function uploadTar(requestOptions, collection, postageBatchId, opti
|
|
|
15
15
|
headers: {
|
|
16
16
|
'content-type': 'application/x-tar',
|
|
17
17
|
'swarm-collection': 'true',
|
|
18
|
-
...
|
|
18
|
+
...prepareRequestHeaders(postageBatchId, options)
|
|
19
19
|
}
|
|
20
20
|
});
|
|
21
21
|
return response;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { prepareRequestHeaders } from "./headers.js";
|
|
2
2
|
import { http } from "./http.js";
|
|
3
3
|
import { TarStream } from "./tar.js";
|
|
4
4
|
import { writeTar } from "./tar-writer.js";
|
|
@@ -13,7 +13,7 @@ export async function uploadTar(requestOptions, collection, postageBatchId, opti
|
|
|
13
13
|
headers: {
|
|
14
14
|
'content-type': 'application/x-tar',
|
|
15
15
|
'swarm-collection': 'true',
|
|
16
|
-
...
|
|
16
|
+
...prepareRequestHeaders(postageBatchId, options)
|
|
17
17
|
}
|
|
18
18
|
});
|
|
19
19
|
await writeTar(collection, tarStream);
|