@ethersphere/bee-js 3.3.0 → 3.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/dist/cjs/chunk/bmt.js +7 -2
  2. package/dist/cjs/chunk/signer.js +9 -4
  3. package/dist/cjs/modules/debug/status.js +5 -5
  4. package/dist/cjs/package.json +1 -0
  5. package/dist/cjs/types/ky-options.js +8 -0
  6. package/dist/cjs/types/ky-universal/common.js +8 -0
  7. package/dist/cjs/types/ky-universal/hooks.js +8 -0
  8. package/dist/cjs/types/ky-universal/retry.js +8 -0
  9. package/dist/cjs/utils/eth.js +8 -3
  10. package/dist/cjs/utils/hash.js +7 -2
  11. package/dist/cjs/utils/stream.js +14 -5
  12. package/dist/index.browser.min.js +3 -0
  13. package/dist/index.browser.min.js.LICENSE.txt +52 -0
  14. package/dist/index.browser.min.js.map +1 -0
  15. package/dist/mjs/bee-debug.js +608 -508
  16. package/dist/mjs/bee.js +935 -836
  17. package/dist/mjs/chunk/bmt.js +34 -24
  18. package/dist/mjs/chunk/cac.js +24 -22
  19. package/dist/mjs/chunk/serialize.js +9 -9
  20. package/dist/mjs/chunk/signer.js +92 -73
  21. package/dist/mjs/chunk/soc.js +78 -66
  22. package/dist/mjs/chunk/span.js +19 -16
  23. package/dist/mjs/feed/index.js +110 -101
  24. package/dist/mjs/feed/json.js +21 -17
  25. package/dist/mjs/feed/topic.js +18 -17
  26. package/dist/mjs/feed/type.js +5 -5
  27. package/dist/mjs/index.js +7 -7
  28. package/dist/mjs/modules/bytes.js +33 -30
  29. package/dist/mjs/modules/bzz.js +80 -77
  30. package/dist/mjs/modules/chunk.js +22 -20
  31. package/dist/mjs/modules/debug/balance.js +26 -22
  32. package/dist/mjs/modules/debug/chequebook.js +84 -65
  33. package/dist/mjs/modules/debug/chunk.js +15 -13
  34. package/dist/mjs/modules/debug/connectivity.js +34 -34
  35. package/dist/mjs/modules/debug/settlements.js +14 -12
  36. package/dist/mjs/modules/debug/stamps.js +52 -47
  37. package/dist/mjs/modules/debug/states.js +16 -14
  38. package/dist/mjs/modules/debug/status.js +55 -35
  39. package/dist/mjs/modules/debug/tag.js +8 -7
  40. package/dist/mjs/modules/debug/transactions.js +31 -25
  41. package/dist/mjs/modules/feed.js +39 -33
  42. package/dist/mjs/modules/pinning.js +34 -28
  43. package/dist/mjs/modules/pss.js +18 -14
  44. package/dist/mjs/modules/soc.js +18 -15
  45. package/dist/mjs/modules/status.js +6 -5
  46. package/dist/mjs/modules/stewardship.js +13 -12
  47. package/dist/mjs/modules/tag.js +38 -30
  48. package/dist/mjs/package.json +1 -0
  49. package/dist/mjs/types/debug.js +5 -4
  50. package/dist/mjs/types/index.js +6 -2
  51. package/dist/mjs/types/ky-options.js +7 -0
  52. package/dist/mjs/types/ky-universal/common.js +7 -0
  53. package/dist/mjs/types/ky-universal/hooks.js +7 -0
  54. package/dist/mjs/types/ky-universal/retry.js +7 -0
  55. package/dist/mjs/utils/bytes.js +32 -26
  56. package/dist/mjs/utils/collection.browser.js +4 -3
  57. package/dist/mjs/utils/collection.js +47 -35
  58. package/dist/mjs/utils/collection.node.js +53 -42
  59. package/dist/mjs/utils/data.browser.js +64 -48
  60. package/dist/mjs/utils/data.js +29 -25
  61. package/dist/mjs/utils/error.js +47 -39
  62. package/dist/mjs/utils/eth.js +119 -104
  63. package/dist/mjs/utils/expose.js +9 -9
  64. package/dist/mjs/utils/file.js +22 -19
  65. package/dist/mjs/utils/hash.js +10 -5
  66. package/dist/mjs/utils/headers.js +43 -39
  67. package/dist/mjs/utils/hex.js +78 -63
  68. package/dist/mjs/utils/http.js +119 -100
  69. package/dist/mjs/utils/merge.js +26 -20
  70. package/dist/mjs/utils/pss.js +8 -6
  71. package/dist/mjs/utils/stamps.js +7 -3
  72. package/dist/mjs/utils/stream.js +107 -82
  73. package/dist/mjs/utils/tar.js +17 -14
  74. package/dist/mjs/utils/type.js +249 -209
  75. package/dist/mjs/utils/uint64.js +16 -16
  76. package/dist/mjs/utils/url.js +32 -25
  77. package/dist/types/types/index.d.ts +1 -1
  78. package/dist/types/types/ky-options.d.ts +221 -0
  79. package/dist/types/types/ky-universal/common.d.ts +13 -0
  80. package/dist/types/types/ky-universal/hooks.d.ts +92 -0
  81. package/dist/types/types/ky-universal/retry.d.ts +38 -0
  82. package/dist/types/utils/stream.d.ts +11 -8
  83. package/package.json +9 -6
  84. package/dist/index.js +0 -28326
  85. package/dist/index.js.map +0 -1
@@ -1,15 +1,15 @@
1
- import { keccak256Hash } from '../utils/hash';
2
- import { serializeBytes } from '../chunk/serialize';
3
- import { uploadSingleOwnerChunkData, makeSingleOwnerChunkFromData } from '../chunk/soc';
4
- import { fetchFeedUpdate } from '../modules/feed';
5
- import { REFERENCE_HEX_LENGTH, ENCRYPTED_REFERENCE_HEX_LENGTH, } from '../types';
6
- import { makeBytes, bytesAtOffset } from '../utils/bytes';
7
- import { BeeResponseError } from '../utils/error';
8
- import { bytesToHex, hexToBytes, makeHexString } from '../utils/hex';
9
- import { readUint64BigEndian, writeUint64BigEndian } from '../utils/uint64';
10
- import * as chunkAPI from '../modules/chunk';
11
- import { makeHexEthAddress } from '../utils/eth';
12
- import { assertAddress } from '../utils/type';
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
- return typeof epoch === 'object' && epoch !== null && 'time' in epoch && 'level' in epoch;
20
+ return typeof epoch === 'object' && epoch !== null && 'time' in epoch && 'level' in epoch;
21
21
  }
22
+
22
23
  function hashFeedIdentifier(topic, index) {
23
- return keccak256Hash(hexToBytes(topic), index);
24
+ return keccak256Hash(hexToBytes(topic), index);
24
25
  }
26
+
25
27
  export function makeSequentialFeedIdentifier(topic, index) {
26
- const indexBytes = writeUint64BigEndian(index);
27
- return hashFeedIdentifier(topic, indexBytes);
28
+ const indexBytes = writeUint64BigEndian(index);
29
+ return hashFeedIdentifier(topic, indexBytes);
28
30
  }
29
31
  export function makeFeedIndexBytes(s) {
30
- const hex = makeHexString(s, INDEX_HEX_LENGTH);
31
- return hexToBytes(hex);
32
+ const hex = makeHexString(s, INDEX_HEX_LENGTH);
33
+ return hexToBytes(hex);
32
34
  }
33
35
  export function makeFeedIdentifier(topic, index) {
34
- if (typeof index === 'number') {
35
- return makeSequentialFeedIdentifier(topic, index);
36
- }
37
- else if (typeof index === 'string') {
38
- const indexBytes = makeFeedIndexBytes(index);
39
- return hashFeedIdentifier(topic, indexBytes);
40
- }
41
- else if (isEpoch(index)) {
42
- throw new TypeError('epoch is not yet implemented');
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
- const identifier = makeFeedIdentifier(topic, index);
48
- const at = options?.at ?? Date.now() / 1000.0;
49
- const timestamp = writeUint64BigEndian(at);
50
- const payloadBytes = serializeBytes(timestamp, reference);
51
- return uploadSingleOwnerChunkData(ky, signer, postageBatchId, identifier, payloadBytes, options);
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
- try {
55
- const feedUpdate = await fetchFeedUpdate(ky, owner, topic, options);
56
- return makeHexString(feedUpdate.feedIndexNext, INDEX_HEX_LENGTH);
57
- }
58
- catch (e) {
59
- if (e instanceof BeeResponseError && e.status === 404) {
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
- const ownerHex = makeHexEthAddress(signer.address);
67
- const nextIndex = await findNextIndex(ky, ownerHex, topic, options);
68
- return uploadFeedUpdate(ky, signer, topic, nextIndex, reference, postageBatchId, options);
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
- try {
72
- return bytesAtOffset(data, offset, REFERENCE_PAYLOAD_MAX_SIZE);
73
- }
74
- catch (e) {
75
- return bytesAtOffset(data, offset, REFERENCE_PAYLOAD_MIN_SIZE);
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
- return verifyChunkReferenceAtOffset(0, data);
81
+ return verifyChunkReferenceAtOffset(0, data);
80
82
  }
81
83
  export async function downloadFeedUpdate(ky, owner, topic, index) {
82
- const identifier = makeFeedIdentifier(topic, index);
83
- const address = keccak256Hash(identifier, owner);
84
- const addressHex = bytesToHex(address);
85
- const data = await chunkAPI.download(ky, addressHex);
86
- const soc = makeSingleOwnerChunkFromData(data, address);
87
- const payload = soc.payload();
88
- const timestampBytes = bytesAtOffset(payload, TIMESTAMP_PAYLOAD_OFFSET, TIMESTAMP_PAYLOAD_SIZE);
89
- const timestamp = readUint64BigEndian(timestampBytes);
90
- const reference = verifyChunkReferenceAtOffset(REFERENCE_PAYLOAD_OFFSET, payload);
91
- return {
92
- timestamp,
93
- reference,
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
- const download = async (options) => fetchFeedUpdate(ky, owner, topic, { ...options, type });
98
- return {
99
- type,
100
- owner,
101
- topic,
102
- download,
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
- if (typeof reference === 'string') {
107
- try {
108
- // Non-encrypted chunk hex string reference
109
- const hexReference = makeHexString(reference, REFERENCE_HEX_LENGTH);
110
- return hexToBytes(hexReference);
111
- }
112
- catch (e) {
113
- if (!(e instanceof TypeError)) {
114
- throw e;
115
- }
116
- // Encrypted chunk hex string reference
117
- const hexReference = makeHexString(reference, ENCRYPTED_REFERENCE_HEX_LENGTH);
118
- return hexToBytes(hexReference);
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
- throw new TypeError('invalid chunk reference');
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
- const upload = async (postageBatchId, reference, options) => {
128
- assertAddress(postageBatchId);
129
- const canonicalReference = makeChunkReference(reference);
130
- return updateFeed(ky, signer, topic, canonicalReference, postageBatchId, { ...options, type });
131
- };
132
- return {
133
- ...makeFeedReader(ky, type, topic, makeHexEthAddress(signer.address)),
134
- upload,
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
+ }
@@ -1,23 +1,27 @@
1
- import { isError } from '../utils/type';
1
+ import { isError } from "../utils/type.js";
2
+
2
3
  function serializeJson(data) {
3
- try {
4
- const jsonString = JSON.stringify(data);
5
- return new TextEncoder().encode(jsonString);
6
- }
7
- catch (e) {
8
- if (isError(e)) {
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
- const feedUpdate = await reader.download();
16
- const retrievedData = await bee.downloadData(feedUpdate.reference);
17
- return retrievedData.json();
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
- const serializedData = serializeJson(data);
21
- const { reference } = await bee.uploadData(postageBatchId, serializedData, options);
22
- return writer.upload(postageBatchId, reference);
23
- }
22
+ const serializedData = serializeJson(data);
23
+ const {
24
+ reference
25
+ } = await bee.uploadData(postageBatchId, serializedData, options);
26
+ return writer.upload(postageBatchId, reference);
27
+ }
@@ -1,20 +1,21 @@
1
- import { keccak256Hash } from '../utils/hash';
2
- import { assertBytes } from '../utils/bytes';
3
- import { makeHexString, bytesToHex } from '../utils/hex';
4
- import { TOPIC_BYTES_LENGTH, TOPIC_HEX_LENGTH } from '../types';
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
- if (typeof topic === 'string') {
7
- return makeHexString(topic, TOPIC_HEX_LENGTH);
8
- }
9
- else if (topic instanceof Uint8Array) {
10
- assertBytes(topic, TOPIC_BYTES_LENGTH);
11
- return bytesToHex(topic, TOPIC_HEX_LENGTH);
12
- }
13
- throw new TypeError('invalid topic');
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
- if (typeof s !== 'string') {
17
- throw new TypeError('topic has to be string!');
18
- }
19
- return bytesToHex(keccak256Hash(s), TOPIC_HEX_LENGTH);
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
+ }
@@ -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
- return typeof type === 'string' && feedTypes.includes(type);
4
+ return typeof type === 'string' && feedTypes.includes(type);
5
5
  }
6
6
  export function assertFeedType(type) {
7
- if (!isFeedType(type)) {
8
- throw new TypeError('invalid feed type');
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 './bee';
2
- import { BeeDebug } from './bee-debug';
3
- export * as Utils from './utils/expose';
4
- export * from './types';
5
- export * from './utils/error';
6
- export { SUPPORTED_BEE_VERSION, SUPPORTED_BEE_VERSION_EXACT } from './modules/debug/status';
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 '../utils/data';
2
- import { extractUploadHeaders } from '../utils/headers';
3
- import { http } from '../utils/http';
4
- import { wrapBytesWithHelpers } from '../utils/bytes';
5
- import { makeTagUid } from '../utils/type';
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
- const response = await http(ky, {
17
- path: endpoint,
18
- method: 'post',
19
- responseType: 'json',
20
- body: await prepareData(data),
21
- headers: {
22
- 'content-type': 'application/octet-stream',
23
- ...extractUploadHeaders(postageBatchId, options),
24
- },
25
- });
26
- return {
27
- reference: response.data.reference,
28
- tagUid: makeTagUid(response.headers.get('swarm-tag')),
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
- const response = await http(ky, {
39
- responseType: 'arraybuffer',
40
- path: `${endpoint}/${hash}`,
41
- });
42
- return wrapBytesWithHelpers(new Uint8Array(response.data));
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
- const response = await http(ky, {
52
- responseType: 'stream',
53
- path: `${endpoint}/${hash}`,
54
- });
55
- return response.data;
56
- }
54
+ const response = await http(ky, {
55
+ responseType: 'stream',
56
+ path: `${endpoint}/${hash}`
57
+ });
58
+ return response.data;
59
+ }
@@ -1,19 +1,18 @@
1
- import { extractUploadHeaders, readFileHeaders } from '../utils/headers';
2
- import { http } from '../utils/http';
3
- import { prepareData } from '../utils/data';
4
- import { makeTar } from '../utils/tar';
5
- import { assertCollection } from '../utils/collection';
6
- import { wrapBytesWithHelpers } from '../utils/bytes';
7
- import { isReadable } from '../utils/stream';
8
- import { makeTagUid } from '../utils/type';
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
- const headers = extractUploadHeaders(postageBatchId, options);
12
- if (options?.size)
13
- headers['content-length'] = String(options.size);
14
- if (options?.contentType)
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
- if (isReadable(data) && !options?.contentType) {
29
- if (!options)
30
- options = {};
31
- options.contentType = 'application/octet-stream';
32
- }
33
- const response = await http(ky, {
34
- method: 'post',
35
- path: bzzEndpoint,
36
- body: await prepareData(data),
37
- headers: {
38
- ...extractFileUploadHeaders(postageBatchId, options),
39
- },
40
- searchParams: { name },
41
- responseType: 'json',
42
- });
43
- return {
44
- reference: response.data.reference,
45
- tagUid: makeTagUid(response.headers.get('swarm-tag')),
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
- const response = await http(ky, {
57
- method: 'GET',
58
- responseType: 'arraybuffer',
59
- path: `${bzzEndpoint}/${hash}/${path}`,
60
- });
61
- const file = {
62
- ...readFileHeaders(response.headers),
63
- data: wrapBytesWithHelpers(new Uint8Array(response.data)),
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
- const response = await http(ky, {
76
- method: 'GET',
77
- responseType: 'stream',
78
- path: `${bzzEndpoint}/${hash}/${path}`,
79
- });
80
- const file = {
81
- ...readFileHeaders(response.headers),
82
- data: response.data,
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
- const headers = extractUploadHeaders(postageBatchId, options);
88
- if (options?.indexDocument)
89
- headers['swarm-index-document'] = options.indexDocument;
90
- if (options?.errorDocument)
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
- assertCollection(collection);
103
- const tarData = makeTar(collection);
104
- const response = await http(ky, {
105
- method: 'post',
106
- path: bzzEndpoint,
107
- body: tarData,
108
- responseType: 'json',
109
- headers: {
110
- 'content-type': 'application/x-tar',
111
- 'swarm-collection': 'true',
112
- ...extractCollectionUploadHeaders(postageBatchId, options),
113
- },
114
- });
115
- return {
116
- reference: response.data.reference,
117
- tagUid: makeTagUid(response.headers.get('swarm-tag')),
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
+ }