@ethersphere/bee-js 6.0.0-pre.0 → 6.0.0-pre.10

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 (193) hide show
  1. package/README.md +18 -17
  2. package/dist/cjs/bee-debug.js +572 -0
  3. package/dist/cjs/bee.js +876 -0
  4. package/dist/cjs/chunk/bmt.js +55 -0
  5. package/dist/cjs/chunk/cac.js +56 -0
  6. package/dist/cjs/chunk/serialize.js +19 -0
  7. package/dist/cjs/chunk/signer.js +126 -0
  8. package/dist/cjs/chunk/soc.js +159 -0
  9. package/dist/cjs/chunk/span.js +29 -0
  10. package/dist/cjs/feed/identifier.js +35 -0
  11. package/dist/cjs/feed/index.js +116 -0
  12. package/dist/cjs/feed/json.js +28 -0
  13. package/dist/cjs/feed/retrievable.js +58 -0
  14. package/dist/cjs/feed/topic.js +25 -0
  15. package/dist/cjs/feed/type.js +15 -0
  16. package/dist/cjs/index.js +39 -0
  17. package/dist/cjs/modules/bytes.js +61 -0
  18. package/dist/cjs/modules/bzz.js +125 -0
  19. package/dist/cjs/modules/chunk.js +48 -0
  20. package/dist/cjs/modules/debug/balance.js +60 -0
  21. package/dist/cjs/modules/debug/chequebook.js +142 -0
  22. package/dist/cjs/modules/debug/chunk.js +38 -0
  23. package/dist/cjs/modules/debug/connectivity.js +54 -0
  24. package/dist/cjs/modules/debug/settlements.js +32 -0
  25. package/dist/cjs/modules/debug/stake.js +57 -0
  26. package/dist/cjs/modules/debug/stamps.js +68 -0
  27. package/dist/cjs/modules/debug/states.js +64 -0
  28. package/dist/cjs/modules/debug/status.js +148 -0
  29. package/dist/cjs/modules/debug/tag.js +19 -0
  30. package/dist/cjs/modules/debug/transactions.js +64 -0
  31. package/dist/cjs/modules/feed.js +66 -0
  32. package/dist/cjs/modules/pinning.js +63 -0
  33. package/dist/cjs/modules/pss.js +43 -0
  34. package/dist/cjs/modules/soc.js +32 -0
  35. package/dist/cjs/modules/status.js +15 -0
  36. package/dist/cjs/modules/stewardship.js +28 -0
  37. package/dist/cjs/modules/tag.js +77 -0
  38. package/dist/cjs/package.json +8 -0
  39. package/dist/cjs/types/debug.js +10 -0
  40. package/dist/cjs/types/index.js +51 -0
  41. package/dist/cjs/utils/bytes.js +121 -0
  42. package/dist/cjs/utils/collection.browser.js +23 -0
  43. package/dist/cjs/utils/collection.js +59 -0
  44. package/dist/cjs/utils/collection.node.js +71 -0
  45. package/dist/cjs/utils/data.browser.js +61 -0
  46. package/dist/cjs/utils/data.js +45 -0
  47. package/dist/cjs/utils/error.js +16 -0
  48. package/dist/cjs/utils/eth.js +199 -0
  49. package/dist/cjs/utils/expose.js +44 -0
  50. package/dist/cjs/utils/file.js +38 -0
  51. package/dist/cjs/utils/hash.js +21 -0
  52. package/dist/cjs/utils/headers.js +59 -0
  53. package/dist/cjs/utils/hex.js +150 -0
  54. package/dist/cjs/utils/http.js +40 -0
  55. package/dist/cjs/utils/pss.js +18 -0
  56. package/dist/cjs/utils/reference.js +36 -0
  57. package/dist/cjs/utils/stamps.js +17 -0
  58. package/dist/cjs/utils/stream.js +146 -0
  59. package/dist/cjs/utils/tar.js +25 -0
  60. package/dist/cjs/utils/type.js +434 -0
  61. package/dist/cjs/utils/uint64.js +29 -0
  62. package/dist/cjs/utils/url.js +56 -0
  63. package/dist/index.browser.min.js +3 -0
  64. package/dist/index.browser.min.js.LICENSE.txt +50 -0
  65. package/dist/index.browser.min.js.map +1 -0
  66. package/dist/{src → mjs}/bee-debug.js +102 -183
  67. package/dist/{src → mjs}/bee.js +82 -232
  68. package/dist/{src → mjs}/chunk/bmt.js +4 -10
  69. package/dist/{src → mjs}/chunk/cac.js +0 -3
  70. package/dist/{src → mjs}/chunk/signer.js +1 -18
  71. package/dist/{src → mjs}/chunk/soc.js +17 -29
  72. package/dist/{src → mjs}/chunk/span.js +2 -5
  73. package/dist/{src → mjs}/feed/identifier.js +0 -6
  74. package/dist/{src → mjs}/feed/index.js +24 -27
  75. package/dist/{src → mjs}/feed/json.js +2 -5
  76. package/dist/{src → mjs}/feed/retrievable.js +7 -21
  77. package/dist/{src → mjs}/feed/topic.js +0 -2
  78. package/dist/{src → mjs}/index.js +2 -2
  79. package/dist/{src → mjs}/modules/bytes.js +16 -20
  80. package/dist/{src → mjs}/modules/bzz.js +33 -40
  81. package/dist/{src → mjs}/modules/chunk.js +11 -13
  82. package/dist/mjs/modules/debug/balance.js +53 -0
  83. package/dist/mjs/modules/debug/chequebook.js +135 -0
  84. package/dist/{src → mjs}/modules/debug/chunk.js +10 -12
  85. package/dist/mjs/modules/debug/connectivity.js +45 -0
  86. package/dist/mjs/modules/debug/settlements.js +27 -0
  87. package/dist/mjs/modules/debug/stake.js +51 -0
  88. package/dist/mjs/modules/debug/stamps.js +61 -0
  89. package/dist/mjs/modules/debug/states.js +58 -0
  90. package/dist/{src → mjs}/modules/debug/status.js +46 -37
  91. package/dist/mjs/modules/debug/tag.js +15 -0
  92. package/dist/mjs/modules/debug/transactions.js +59 -0
  93. package/dist/{src → mjs}/modules/feed.js +15 -21
  94. package/dist/mjs/modules/pinning.js +56 -0
  95. package/dist/{src → mjs}/modules/pss.js +7 -10
  96. package/dist/{src → mjs}/modules/soc.js +6 -7
  97. package/dist/mjs/modules/status.js +11 -0
  98. package/dist/mjs/modules/stewardship.js +23 -0
  99. package/dist/mjs/modules/tag.js +72 -0
  100. package/dist/mjs/package.json +8 -0
  101. package/dist/{src → mjs}/types/debug.js +0 -1
  102. package/dist/{src → mjs}/types/index.js +0 -4
  103. package/dist/{src → mjs}/utils/bytes.js +8 -17
  104. package/dist/{src/utils/collection-browser.js → mjs/utils/collection.browser.js} +0 -1
  105. package/dist/{src → mjs}/utils/collection.js +0 -12
  106. package/dist/{src/utils/collection-node.js → mjs/utils/collection.node.js} +0 -13
  107. package/dist/{src → mjs}/utils/data.browser.js +2 -11
  108. package/dist/{src → mjs}/utils/data.js +0 -7
  109. package/dist/mjs/utils/error.js +11 -0
  110. package/dist/{src → mjs}/utils/eth.js +6 -28
  111. package/dist/{src → mjs}/utils/expose.js +1 -1
  112. package/dist/{src → mjs}/utils/file.js +4 -9
  113. package/dist/{src → mjs}/utils/hash.js +0 -1
  114. package/dist/{src → mjs}/utils/headers.js +5 -13
  115. package/dist/{src → mjs}/utils/hex.js +6 -25
  116. package/dist/mjs/utils/http.js +34 -0
  117. package/dist/{src → mjs}/utils/pss.js +0 -2
  118. package/dist/{src → mjs}/utils/reference.js +2 -5
  119. package/dist/{src → mjs}/utils/stream.js +3 -26
  120. package/dist/{src → mjs}/utils/tar.js +3 -6
  121. package/dist/{src → mjs}/utils/type.js +23 -80
  122. package/dist/{src → mjs}/utils/uint64.js +0 -1
  123. package/dist/{src → mjs}/utils/url.js +2 -10
  124. package/dist/types/bee-debug.d.ts +71 -46
  125. package/dist/types/bee.d.ts +34 -58
  126. package/dist/types/chunk/bmt.d.ts +1 -1
  127. package/dist/types/chunk/cac.d.ts +1 -1
  128. package/dist/types/chunk/soc.d.ts +9 -9
  129. package/dist/types/chunk/span.d.ts +1 -1
  130. package/dist/types/feed/index.d.ts +10 -10
  131. package/dist/types/feed/json.d.ts +2 -2
  132. package/dist/types/feed/retrievable.d.ts +2 -2
  133. package/dist/types/feed/type.d.ts +1 -1
  134. package/dist/types/index.d.ts +13 -2
  135. package/dist/types/modules/bytes.d.ts +5 -5
  136. package/dist/types/modules/bzz.d.ts +8 -8
  137. package/dist/types/modules/chunk.d.ts +4 -4
  138. package/dist/types/modules/debug/balance.d.ts +9 -9
  139. package/dist/types/modules/debug/chequebook.d.ts +17 -17
  140. package/dist/types/modules/debug/chunk.d.ts +5 -5
  141. package/dist/types/modules/debug/connectivity.d.ts +7 -7
  142. package/dist/types/modules/debug/settlements.d.ts +5 -5
  143. package/dist/types/modules/debug/stake.d.ts +21 -0
  144. package/dist/types/modules/debug/stamps.d.ts +7 -7
  145. package/dist/types/modules/debug/states.d.ts +7 -7
  146. package/dist/types/modules/debug/status.d.ts +21 -15
  147. package/dist/types/modules/debug/tag.d.ts +3 -3
  148. package/dist/types/modules/debug/transactions.d.ts +5 -5
  149. package/dist/types/modules/feed.d.ts +3 -3
  150. package/dist/types/modules/pinning.d.ts +7 -7
  151. package/dist/types/modules/pss.d.ts +3 -3
  152. package/dist/types/modules/soc.d.ts +2 -2
  153. package/dist/types/modules/status.d.ts +3 -3
  154. package/dist/types/modules/stewardship.d.ts +3 -3
  155. package/dist/types/modules/tag.d.ts +6 -6
  156. package/dist/types/types/debug.d.ts +35 -2
  157. package/dist/types/types/index.d.ts +48 -89
  158. package/dist/types/utils/error.d.ts +0 -26
  159. package/dist/types/utils/eth.d.ts +6 -6
  160. package/dist/types/utils/expose.d.ts +4 -7
  161. package/dist/types/utils/hash.d.ts +1 -1
  162. package/dist/types/utils/headers.d.ts +1 -1
  163. package/dist/types/utils/hex.d.ts +4 -4
  164. package/dist/types/utils/http.d.ts +11 -21
  165. package/dist/types/utils/stream.d.ts +1 -1
  166. package/dist/types/utils/type.d.ts +3 -2
  167. package/dist/types/utils/uint64.d.ts +1 -1
  168. package/package.json +59 -52
  169. package/dist/index.browser.js +0 -33
  170. package/dist/index.browser.js.map +0 -7
  171. package/dist/src/modules/debug/balance.js +0 -57
  172. package/dist/src/modules/debug/chequebook.js +0 -150
  173. package/dist/src/modules/debug/connectivity.js +0 -45
  174. package/dist/src/modules/debug/settlements.js +0 -29
  175. package/dist/src/modules/debug/stamps.js +0 -64
  176. package/dist/src/modules/debug/states.js +0 -46
  177. package/dist/src/modules/debug/tag.js +0 -16
  178. package/dist/src/modules/debug/transactions.js +0 -69
  179. package/dist/src/modules/pinning.js +0 -60
  180. package/dist/src/modules/status.js +0 -12
  181. package/dist/src/modules/stewardship.js +0 -24
  182. package/dist/src/modules/tag.js +0 -77
  183. package/dist/src/utils/error.js +0 -47
  184. package/dist/src/utils/http.js +0 -162
  185. package/dist/src/utils/merge.js +0 -36
  186. package/dist/src/utils/sleep.js +0 -8
  187. package/dist/types/utils/merge.d.ts +0 -8
  188. package/dist/types/utils/sleep.d.ts +0 -6
  189. /package/dist/{src → mjs}/chunk/serialize.js +0 -0
  190. /package/dist/{src → mjs}/feed/type.js +0 -0
  191. /package/dist/{src → mjs}/utils/stamps.js +0 -0
  192. /package/dist/types/utils/{collection-browser.d.ts → collection.browser.d.ts} +0 -0
  193. /package/dist/types/utils/{collection-node.d.ts → collection.node.d.ts} +0 -0
@@ -0,0 +1,876 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.Bee = void 0;
27
+ const swarm_cid_1 = require("@ethersphere/swarm-cid");
28
+ const cafe_utility_1 = require("cafe-utility");
29
+ const signer_1 = require("./chunk/signer");
30
+ const soc_1 = require("./chunk/soc");
31
+ const feed_1 = require("./feed");
32
+ const json_1 = require("./feed/json");
33
+ const retrievable_1 = require("./feed/retrievable");
34
+ const topic_1 = require("./feed/topic");
35
+ const type_1 = require("./feed/type");
36
+ const bytes = __importStar(require("./modules/bytes"));
37
+ const bzz = __importStar(require("./modules/bzz"));
38
+ const chunk = __importStar(require("./modules/chunk"));
39
+ const feed_2 = require("./modules/feed");
40
+ const pinning = __importStar(require("./modules/pinning"));
41
+ const pss = __importStar(require("./modules/pss"));
42
+ const status = __importStar(require("./modules/status"));
43
+ const stewardship = __importStar(require("./modules/stewardship"));
44
+ const tag = __importStar(require("./modules/tag"));
45
+ const types_1 = require("./types");
46
+ const bytes_1 = require("./utils/bytes");
47
+ const collection_1 = require("./utils/collection");
48
+ const collection_node_1 = require("./utils/collection.node");
49
+ const data_1 = require("./utils/data");
50
+ const error_1 = require("./utils/error");
51
+ const eth_1 = require("./utils/eth");
52
+ const file_1 = require("./utils/file");
53
+ const stream_1 = require("./utils/stream");
54
+ const type_2 = require("./utils/type");
55
+ const url_1 = require("./utils/url");
56
+ /**
57
+ * The main component that abstracts operations available on the main Bee API.
58
+ *
59
+ * Not all methods are always available as it depends in what mode is Bee node launched in.
60
+ * For example gateway mode and light node mode has only limited set of endpoints enabled.
61
+ */
62
+ class Bee {
63
+ /**
64
+ * @param url URL on which is the main API of Bee node exposed
65
+ * @param options
66
+ */
67
+ constructor(url, options) {
68
+ (0, url_1.assertBeeUrl)(url);
69
+ // Remove last slash if present, as our endpoint strings starts with `/...`
70
+ // which could lead to double slash in URL to which Bee responds with
71
+ // unnecessary redirects.
72
+ this.url = (0, url_1.stripLastSlash)(url);
73
+ if (options?.signer) {
74
+ this.signer = (0, signer_1.makeSigner)(options.signer);
75
+ }
76
+ const requestOptions = {
77
+ baseURL: this.url,
78
+ timeout: options?.timeout ?? false,
79
+ };
80
+ if (options?.headers) {
81
+ requestOptions.headers = options.headers;
82
+ }
83
+ if (options?.onRequest) {
84
+ requestOptions.onRequest = options.onRequest;
85
+ }
86
+ this.requestOptions = requestOptions;
87
+ }
88
+ /**
89
+ * Upload data to a Bee node
90
+ *
91
+ * @param postageBatchId Postage BatchId to be used to upload the data with
92
+ * @param data Data to be uploaded
93
+ * @param options Additional options like tag, encryption, pinning, content-type and request options
94
+ *
95
+ * @returns reference is a content hash of the data
96
+ * @see [Bee docs - Upload and download](https://docs.ethswarm.org/docs/access-the-swarm/upload-and-download)
97
+ * @see [Bee API reference - `POST /bytes`](https://docs.ethswarm.org/api/#tag/Bytes/paths/~1bytes/post)
98
+ */
99
+ async uploadData(postageBatchId, data, options, requestOptions) {
100
+ (0, type_2.assertBatchId)(postageBatchId);
101
+ (0, type_2.assertData)(data);
102
+ if (options)
103
+ (0, type_2.assertUploadOptions)(options);
104
+ return bytes.upload(this.getRequestOptionsForCall(requestOptions), data, postageBatchId, options);
105
+ }
106
+ /**
107
+ * Download data as a byte array
108
+ *
109
+ * @param reference Bee data reference in hex string (either 64 or 128 chars long) or ENS domain.
110
+ * @param options Options that affects the request behavior
111
+ * @throws TypeError if some of the input parameters is not expected type
112
+ * @throws BeeArgumentError if there is passed ENS domain with invalid unicode characters
113
+ * @see [Bee docs - Upload and download](https://docs.ethswarm.org/docs/access-the-swarm/upload-and-download)
114
+ * @see [Bee API reference - `GET /bytes`](https://docs.ethswarm.org/api/#tag/Bytes/paths/~1bytes~1{reference}/get)
115
+ */
116
+ async downloadData(reference, options) {
117
+ (0, type_2.assertRequestOptions)(options);
118
+ (0, type_2.assertReferenceOrEns)(reference);
119
+ return bytes.download(this.getRequestOptionsForCall(options), reference);
120
+ }
121
+ /**
122
+ * Download data as a Readable stream
123
+ *
124
+ * @param reference Bee data reference in hex string (either 64 or 128 chars long) or ENS domain.
125
+ * @param options Options that affects the request behavior
126
+ * @throws TypeError if some of the input parameters is not expected type
127
+ * @throws BeeArgumentError if there is passed ENS domain with invalid unicode characters
128
+ * @see [Bee docs - Upload and download](https://docs.ethswarm.org/docs/access-the-swarm/upload-and-download)
129
+ * @see [Bee API reference - `GET /bytes`](https://docs.ethswarm.org/api/#tag/Bytes/paths/~1bytes~1{reference}/get)
130
+ */
131
+ async downloadReadableData(reference, options) {
132
+ (0, type_2.assertRequestOptions)(options);
133
+ (0, type_2.assertReferenceOrEns)(reference);
134
+ return bytes.downloadReadable(this.getRequestOptionsForCall(options), reference);
135
+ }
136
+ /**
137
+ * Upload chunk to a Bee node
138
+ *
139
+ * @param postageBatchId Postage BatchId to be used to upload the chunk with
140
+ * @param data Raw chunk to be uploaded
141
+ * @param options Additional options like tag, encryption, pinning, content-type and request options
142
+ *
143
+ * @returns reference is a content hash of the data
144
+ * @see [Bee docs - Upload and download](https://docs.ethswarm.org/docs/access-the-swarm/upload-and-download)
145
+ * @see [Bee API reference - `POST /chunks`](https://docs.ethswarm.org/api/#tag/Chunk/paths/~1chunks/post)
146
+ */
147
+ async uploadChunk(postageBatchId, data, options, requestOptions) {
148
+ (0, type_2.assertBatchId)(postageBatchId);
149
+ if (!(data instanceof Uint8Array)) {
150
+ throw new TypeError('Data has to be Uint8Array instance!');
151
+ }
152
+ if (data.length < types_1.SPAN_SIZE) {
153
+ throw new error_1.BeeArgumentError(`Chunk has to have size of at least ${types_1.SPAN_SIZE}.`, data);
154
+ }
155
+ if (data.length > types_1.CHUNK_SIZE + types_1.SPAN_SIZE) {
156
+ throw new error_1.BeeArgumentError(`Chunk has to have size of at most ${types_1.CHUNK_SIZE}.`, data);
157
+ }
158
+ if (options)
159
+ (0, type_2.assertUploadOptions)(options);
160
+ return chunk.upload(this.getRequestOptionsForCall(requestOptions), data, postageBatchId, options);
161
+ }
162
+ /**
163
+ * Download chunk as a byte array
164
+ *
165
+ * @param reference Bee chunk reference in hex string (either 64 or 128 chars long) or ENS domain.
166
+ * @param options Options that affects the request behavior
167
+ * @throws TypeError if some of the input parameters is not expected type
168
+ * @throws BeeArgumentError if there is passed ENS domain with invalid unicode characters
169
+ * @see [Bee docs - Upload and download](https://docs.ethswarm.org/docs/access-the-swarm/upload-and-download)
170
+ * @see [Bee API reference - `GET /chunks`](https://docs.ethswarm.org/api/#tag/Chunk/paths/~1chunks~1{reference}/get)
171
+ */
172
+ async downloadChunk(reference, options) {
173
+ (0, type_2.assertRequestOptions)(options);
174
+ (0, type_2.assertReferenceOrEns)(reference);
175
+ return chunk.download(this.getRequestOptionsForCall(options), reference);
176
+ }
177
+ /**
178
+ * Upload single file to a Bee node.
179
+ *
180
+ * **To make sure that you won't loose critical data it is highly recommended to also
181
+ * locally pin the data with `options.pin = true`**
182
+ *
183
+ * @param postageBatchId Postage BatchId to be used to upload the data with
184
+ * @param data Data or file to be uploaded
185
+ * @param name Optional name of the uploaded file
186
+ * @param options Additional options like tag, encryption, pinning, content-type and request options
187
+ *
188
+ * @see [Bee docs - Keep your data alive / Postage stamps](https://docs.ethswarm.org/docs/access-the-swarm/keep-your-data-alive)
189
+ * @see [Bee docs - Upload and download](https://docs.ethswarm.org/docs/access-the-swarm/upload-and-download)
190
+ * @see [Bee API reference - `POST /bzz`](https://docs.ethswarm.org/api/#tag/File/paths/~1bzz/post)
191
+ * @returns reference is a content hash of the file
192
+ */
193
+ async uploadFile(postageBatchId, data, name, options, requestOptions) {
194
+ (0, type_2.assertBatchId)(postageBatchId);
195
+ (0, type_2.assertFileData)(data);
196
+ if (options)
197
+ (0, type_2.assertFileUploadOptions)(options);
198
+ if (name && typeof name !== 'string') {
199
+ throw new TypeError('name has to be string or undefined!');
200
+ }
201
+ if ((0, file_1.isFile)(data)) {
202
+ const fileData = await (0, file_1.fileArrayBuffer)(data);
203
+ const fileName = name ?? data.name;
204
+ const contentType = data.type;
205
+ const fileOptions = { contentType, ...options };
206
+ return (0, type_2.addCidConversionFunction)(await bzz.uploadFile(this.getRequestOptionsForCall(requestOptions), fileData, postageBatchId, fileName, fileOptions), swarm_cid_1.ReferenceType.MANIFEST);
207
+ }
208
+ else if ((0, stream_1.isReadable)(data) && options?.tag && !options.size) {
209
+ // TODO: Needed until https://github.com/ethersphere/bee/issues/2317 is resolved
210
+ const result = await bzz.uploadFile(this.getRequestOptionsForCall(requestOptions), data, postageBatchId, name, options);
211
+ await this.updateTag(options.tag, result.reference);
212
+ return (0, type_2.addCidConversionFunction)(result, swarm_cid_1.ReferenceType.MANIFEST);
213
+ }
214
+ else {
215
+ return (0, type_2.addCidConversionFunction)(await bzz.uploadFile(this.getRequestOptionsForCall(requestOptions), data, postageBatchId, name, options), swarm_cid_1.ReferenceType.MANIFEST);
216
+ }
217
+ }
218
+ /**
219
+ * Download single file.
220
+ *
221
+ * @param reference Bee file reference in hex string (either 64 or 128 chars long), ENS domain or Swarm CID.
222
+ * @param path If reference points to manifest, then this parameter defines path to the file
223
+ * @param options Options that affects the request behavior
224
+ * @throws TypeError if some of the input parameters is not expected type
225
+ * @throws BeeArgumentError if there is passed ENS domain with invalid unicode characters
226
+ * @see Data
227
+ * @see [Bee docs - Upload and download](https://docs.ethswarm.org/docs/access-the-swarm/upload-and-download)
228
+ * @see [Bee API reference - `GET /bzz`](https://docs.ethswarm.org/api/#tag/Collection/paths/~1bzz~1{reference}~1{path}/get)
229
+ */
230
+ async downloadFile(reference, path = '', options) {
231
+ (0, type_2.assertRequestOptions)(options);
232
+ reference = (0, type_2.makeReferenceOrEns)(reference, swarm_cid_1.ReferenceType.MANIFEST);
233
+ return bzz.downloadFile(this.getRequestOptionsForCall(options), reference, path);
234
+ }
235
+ /**
236
+ * Download single file as a readable stream
237
+ *
238
+ * @param reference Bee file reference in hex string (either 64 or 128 chars long), ENS domain or Swarm CID.
239
+ * @param path If reference points to manifest / collections, then this parameter defines path to the file
240
+ * @param options Options that affects the request behavior
241
+ * @throws TypeError if some of the input parameters is not expected type
242
+ * @throws BeeArgumentError if there is passed ENS domain with invalid unicode characters
243
+ *
244
+ * @see [Bee docs - Upload and download](https://docs.ethswarm.org/docs/access-the-swarm/upload-and-download)
245
+ * @see [Bee API reference - `GET /bzz`](https://docs.ethswarm.org/api/#tag/Collection/paths/~1bzz~1{reference}~1{path}/get)
246
+ */
247
+ async downloadReadableFile(reference, path = '', options) {
248
+ (0, type_2.assertRequestOptions)(options);
249
+ reference = (0, type_2.makeReferenceOrEns)(reference, swarm_cid_1.ReferenceType.MANIFEST);
250
+ return bzz.downloadFileReadable(this.getRequestOptionsForCall(options), reference, path);
251
+ }
252
+ /**
253
+ * Upload collection of files to a Bee node
254
+ *
255
+ * Uses the FileList API from the browser.
256
+ *
257
+ * The returned `UploadResult.tag` might be undefined if called in CORS-enabled environment.
258
+ * This will be fixed upon next Bee release. https://github.com/ethersphere/bee-js/issues/406
259
+ *
260
+ * @param postageBatchId Postage BatchId to be used to upload the data with
261
+ * @param fileList list of files to be uploaded
262
+ * @param options Additional options like tag, encryption, pinning and request options
263
+ *
264
+ * @see [Bee docs - Keep your data alive / Postage stamps](https://docs.ethswarm.org/docs/access-the-swarm/keep-your-data-alive)
265
+ * @see [Bee docs - Upload directory](https://docs.ethswarm.org/docs/access-the-swarm/upload-a-directory/)
266
+ * @see [Bee API reference - `POST /bzz`](https://docs.ethswarm.org/api/#tag/Collection/paths/~1bzz/post)
267
+ */
268
+ async uploadFiles(postageBatchId, fileList, options, requestOptions) {
269
+ (0, type_2.assertBatchId)(postageBatchId);
270
+ if (options)
271
+ (0, type_2.assertCollectionUploadOptions)(options);
272
+ const data = await (0, collection_1.makeCollectionFromFileList)(fileList);
273
+ return (0, type_2.addCidConversionFunction)(await bzz.uploadCollection(this.getRequestOptionsForCall(requestOptions), data, postageBatchId, options), swarm_cid_1.ReferenceType.MANIFEST);
274
+ }
275
+ /**
276
+ * Upload Collection that you can assembly yourself.
277
+ *
278
+ * The returned `UploadResult.tag` might be undefined if called in CORS-enabled environment.
279
+ * This will be fixed upon next Bee release. https://github.com/ethersphere/bee-js/issues/406
280
+ *
281
+ * @param postageBatchId
282
+ * @param collection
283
+ * @param options Collections and request options
284
+ */
285
+ async uploadCollection(postageBatchId, collection, options) {
286
+ (0, type_2.assertBatchId)(postageBatchId);
287
+ (0, collection_1.assertCollection)(collection);
288
+ if (options)
289
+ (0, type_2.assertCollectionUploadOptions)(options);
290
+ return (0, type_2.addCidConversionFunction)(await bzz.uploadCollection(this.requestOptions, collection, postageBatchId, options), swarm_cid_1.ReferenceType.MANIFEST);
291
+ }
292
+ /**
293
+ * Upload collection of files.
294
+ *
295
+ * Available only in Node.js as it uses the `fs` module.
296
+ *
297
+ * The returned `UploadResult.tag` might be undefined if called in CORS-enabled environment.
298
+ * This will be fixed upon next Bee release. https://github.com/ethersphere/bee-js/issues/406
299
+ *
300
+ * @param postageBatchId Postage BatchId to be used to upload the data with
301
+ * @param dir the path of the files to be uploaded
302
+ * @param options Additional options like tag, encryption, pinning and request options
303
+ *
304
+ * @see [Bee docs - Keep your data alive / Postage stamps](https://docs.ethswarm.org/docs/access-the-swarm/keep-your-data-alive)
305
+ * @see [Bee docs - Upload directory](https://docs.ethswarm.org/docs/access-the-swarm/upload-a-directory/)
306
+ * @see [Bee API reference - `POST /bzz`](https://docs.ethswarm.org/api/#tag/Collection/paths/~1bzz/post)
307
+ */
308
+ async uploadFilesFromDirectory(postageBatchId, dir, options, requestOptions) {
309
+ (0, type_2.assertBatchId)(postageBatchId);
310
+ if (options)
311
+ (0, type_2.assertCollectionUploadOptions)(options);
312
+ const data = await (0, collection_node_1.makeCollectionFromFS)(dir);
313
+ return (0, type_2.addCidConversionFunction)(await bzz.uploadCollection(this.getRequestOptionsForCall(requestOptions), data, postageBatchId, options), swarm_cid_1.ReferenceType.MANIFEST);
314
+ }
315
+ /**
316
+ * Create a new Tag which is meant for tracking progres of syncing data across network.
317
+ *
318
+ * @param options Options that affects the request behavior
319
+ * @see [Bee docs - Syncing / Tags](https://docs.ethswarm.org/docs/access-the-swarm/syncing)
320
+ * @see [Bee API reference - `POST /tags`](https://docs.ethswarm.org/api/#tag/Tag/paths/~1tags/post)
321
+ */
322
+ async createTag(options) {
323
+ (0, type_2.assertRequestOptions)(options);
324
+ return tag.createTag(this.getRequestOptionsForCall(options));
325
+ }
326
+ /**
327
+ * Fetches all tags.
328
+ *
329
+ * The listing is limited by options.limit. So you have to iterate using options.offset to get all tags.
330
+ *
331
+ * @param options Options that affects the request behavior
332
+ * @throws TypeError if limit or offset are not numbers or undefined
333
+ * @throws BeeArgumentError if limit or offset have invalid options
334
+ *
335
+ * @see [Bee docs - Syncing / Tags](https://docs.ethswarm.org/docs/access-the-swarm/syncing)
336
+ * @see [Bee API reference - `GET /tags`](https://docs.ethswarm.org/api/#tag/Tag/paths/~1tags/get)
337
+ */
338
+ async getAllTags(options) {
339
+ (0, type_2.assertRequestOptions)(options);
340
+ (0, type_2.assertAllTagsOptions)(options);
341
+ return tag.getAllTags(this.getRequestOptionsForCall(options), options?.offset, options?.limit);
342
+ }
343
+ /**
344
+ * Retrieve tag information from Bee node
345
+ *
346
+ * @param tagUid UID or tag object to be retrieved
347
+ * @param options Options that affects the request behavior
348
+ * @throws TypeError if tagUid is in not correct format
349
+ *
350
+ * @see [Bee docs - Syncing / Tags](https://docs.ethswarm.org/docs/access-the-swarm/syncing)
351
+ * @see [Bee API reference - `GET /tags/{uid}`](https://docs.ethswarm.org/api/#tag/Tag/paths/~1tags~1{uid}/get)
352
+ *
353
+ */
354
+ async retrieveTag(tagUid, options) {
355
+ (0, type_2.assertRequestOptions)(options);
356
+ tagUid = (0, type_2.makeTagUid)(tagUid);
357
+ return tag.retrieveTag(this.getRequestOptionsForCall(options), tagUid);
358
+ }
359
+ /**
360
+ * Delete Tag
361
+ *
362
+ * @param tagUid UID or tag object to be retrieved
363
+ * @param options Options that affects the request behavior
364
+ * @throws TypeError if tagUid is in not correct format
365
+ * @throws BeeResponse error if something went wrong on the Bee node side while deleting the tag.
366
+ *
367
+ * @see [Bee docs - Syncing / Tags](https://docs.ethswarm.org/docs/access-the-swarm/syncing)
368
+ * @see [Bee API reference - `DELETE /tags/{uid}`](https://docs.ethswarm.org/api/#tag/Tag/paths/~1tags~1{uid}/delete)
369
+ */
370
+ async deleteTag(tagUid, options) {
371
+ (0, type_2.assertRequestOptions)(options);
372
+ tagUid = (0, type_2.makeTagUid)(tagUid);
373
+ return tag.deleteTag(this.getRequestOptionsForCall(options), tagUid);
374
+ }
375
+ /**
376
+ * Update tag's total chunks count.
377
+ *
378
+ * This is important if you are uploading individual chunks with a tag. Then upon finishing the final root chunk,
379
+ * you can use this method to update the total chunks count for the tag.
380
+ *
381
+ * @param tagUid UID or tag object to be retrieved
382
+ * @param reference The root reference that contains all the chunks to be counted
383
+ * @param options Options that affects the request behavior
384
+ * @throws TypeError if tagUid is in not correct format
385
+ * @throws BeeResponse error if something went wrong on the Bee node side while deleting the tag.
386
+ *
387
+ * @see [Bee docs - Syncing / Tags](https://docs.ethswarm.org/docs/access-the-swarm/syncing)
388
+ * @see [Bee API reference - `PATCH /tags/{uid}`](https://docs.ethswarm.org/api/#tag/Tag/paths/~1tags~1{uid}/patch)
389
+ */
390
+ async updateTag(tagUid, reference, options) {
391
+ (0, type_2.assertReference)(reference);
392
+ (0, type_2.assertRequestOptions)(options);
393
+ tagUid = (0, type_2.makeTagUid)(tagUid);
394
+ return tag.updateTag(this.getRequestOptionsForCall(options), tagUid, reference);
395
+ }
396
+ /**
397
+ * Pin local data with given reference
398
+ *
399
+ * @param reference Data reference
400
+ * @param options Options that affects the request behavior
401
+ * @throws TypeError if reference is in not correct format
402
+ *
403
+ * @see [Bee docs - Pinning](https://docs.ethswarm.org/docs/access-the-swarm/pinning)
404
+ */
405
+ async pin(reference, options) {
406
+ (0, type_2.assertRequestOptions)(options);
407
+ (0, type_2.assertReference)(reference);
408
+ return pinning.pin(this.getRequestOptionsForCall(options), reference);
409
+ }
410
+ /**
411
+ * Unpin local data with given reference
412
+ *
413
+ * @param reference Data reference
414
+ * @param options Options that affects the request behavior
415
+ * @throws TypeError if reference is in not correct format
416
+ *
417
+ * @see [Bee docs - Pinning](https://docs.ethswarm.org/docs/access-the-swarm/pinning)
418
+ */
419
+ async unpin(reference, options) {
420
+ (0, type_2.assertRequestOptions)(options);
421
+ (0, type_2.assertReference)(reference);
422
+ return pinning.unpin(this.getRequestOptionsForCall(options), reference);
423
+ }
424
+ /**
425
+ * Get list of all locally pinned references
426
+ *
427
+ * @param options Options that affects the request behavior
428
+ * @see [Bee docs - Pinning](https://docs.ethswarm.org/docs/access-the-swarm/pinning)
429
+ */
430
+ async getAllPins(options) {
431
+ (0, type_2.assertRequestOptions)(options);
432
+ return pinning.getAllPins(this.getRequestOptionsForCall(options));
433
+ }
434
+ /**
435
+ * Get pinning status of chunk with given reference
436
+ *
437
+ * @param reference Bee data reference in hex string (either 64 or 128 chars long) or ENS domain.
438
+ * @param options Options that affects the request behavior
439
+ * @throws TypeError if some of the input parameters is not expected type
440
+ * @throws BeeArgumentError if there is passed ENS domain with invalid unicode characters
441
+ *
442
+ * @see [Bee docs - Pinning](https://docs.ethswarm.org/docs/access-the-swarm/pinning)
443
+ */
444
+ async getPin(reference, options) {
445
+ (0, type_2.assertRequestOptions)(options);
446
+ (0, type_2.assertReference)(reference);
447
+ return pinning.getPin(this.getRequestOptionsForCall(options), reference);
448
+ }
449
+ /**
450
+ * Instructs the Bee node to reupload a locally pinned data into the network.
451
+ *
452
+ * @param reference Bee data reference to be re-uploaded in hex string (either 64 or 128 chars long) or ENS domain.
453
+ * @param options Options that affects the request behavior
454
+ * @throws BeeArgumentError if the reference is not locally pinned
455
+ * @throws TypeError if some of the input parameters is not expected type
456
+ * @throws BeeArgumentError if there is passed ENS domain with invalid unicode characters
457
+ *
458
+ * @see [Bee API reference - `PUT /stewardship`](https://docs.ethswarm.org/api/#tag/Stewardship/paths/~1stewardship~1{reference}/put)
459
+ */
460
+ async reuploadPinnedData(reference, options) {
461
+ (0, type_2.assertRequestOptions)(options);
462
+ (0, type_2.assertReferenceOrEns)(reference);
463
+ await stewardship.reupload(this.getRequestOptionsForCall(options), reference);
464
+ }
465
+ /**
466
+ * Checks if content specified by reference is retrievable from the network.
467
+ *
468
+ * @param reference Bee data reference to be checked in hex string (either 64 or 128 chars long) or ENS domain.
469
+ * @param options Options that affects the request behavior
470
+ * @throws TypeError if some of the input parameters is not expected type
471
+ * @throws BeeArgumentError if there is passed ENS domain with invalid unicode characters
472
+ *
473
+ * @see [Bee API reference - `GET /stewardship`](https://docs.ethswarm.org/api/#tag/Stewardship/paths/~1stewardship~1{reference}/get)
474
+ */
475
+ async isReferenceRetrievable(reference, options) {
476
+ (0, type_2.assertRequestOptions)(options);
477
+ (0, type_2.assertReferenceOrEns)(reference);
478
+ return stewardship.isRetrievable(this.getRequestOptionsForCall(options), reference);
479
+ }
480
+ /**
481
+ * Functions that validates if feed is retrievable in the network.
482
+ *
483
+ * If no index is passed then it check for "latest" update, which is a weaker guarantee as nobody can be really
484
+ * sure what is the "latest" update.
485
+ *
486
+ * If index is passed then it validates all previous sequence index chunks if they are available as they are required
487
+ * to correctly resolve the feed upto the given index update.
488
+ *
489
+ * @param type
490
+ * @param owner
491
+ * @param topic
492
+ * @param index
493
+ * @param options
494
+ */
495
+ async isFeedRetrievable(type, owner, topic, index, options) {
496
+ const canonicalOwner = (0, eth_1.makeEthAddress)(owner);
497
+ const canonicalTopic = (0, topic_1.makeTopic)(topic);
498
+ if (!index) {
499
+ try {
500
+ await this.makeFeedReader(type, canonicalTopic, canonicalOwner).download();
501
+ return true;
502
+ }
503
+ catch (e) {
504
+ if (e?.response?.status === 404) {
505
+ return false;
506
+ }
507
+ throw e;
508
+ }
509
+ }
510
+ if (type !== 'sequence') {
511
+ throw new error_1.BeeError('Only Sequence type of Feeds is supported at the moment');
512
+ }
513
+ return (0, retrievable_1.areAllSequentialFeedsUpdateRetrievable)(this, canonicalOwner, canonicalTopic, index, this.getRequestOptionsForCall(options));
514
+ }
515
+ /**
516
+ * Send data to recipient or target with Postal Service for Swarm.
517
+ *
518
+ * Because sending a PSS message is slow and CPU intensive,
519
+ * it is not supposed to be used for general messaging but
520
+ * most likely for setting up an encrypted communication
521
+ * channel by sending an one-off message.
522
+ *
523
+ * **Warning! If the recipient Bee node is a light node, then he will never receive the message!**
524
+ * This is because light nodes does not fully participate in the data exchange in Swarm network and hence the message won't arrive to them.
525
+ *
526
+ * @param postageBatchId Postage BatchId that will be assigned to sent message
527
+ * @param topic Topic name
528
+ * @param target Target message address prefix. Has a limit on length. Recommend to use `Utils.Pss.makeMaxTarget()` to get the most specific target that Bee node will accept.
529
+ * @param data Message to be sent
530
+ * @param recipient Recipient public key
531
+ * @param options Options that affects the request behavior
532
+ * @throws TypeError if `data`, `batchId`, `target` or `recipient` are in invalid format
533
+ *
534
+ * @see [Bee docs - PSS](https://docs.ethswarm.org/docs/dapps-on-swarm/pss)
535
+ * @see [Bee API reference - `POST /pss`](https://docs.ethswarm.org/api/#tag/Postal-Service-for-Swarm/paths/~1pss~1send~1{topic}~1{targets}/post)
536
+ */
537
+ async pssSend(postageBatchId, topic, target, data, recipient, options) {
538
+ (0, type_2.assertRequestOptions)(options);
539
+ (0, type_2.assertData)(data);
540
+ (0, type_2.assertBatchId)(postageBatchId);
541
+ (0, type_2.assertAddressPrefix)(target);
542
+ if (typeof topic !== 'string') {
543
+ throw new TypeError('topic has to be an string!');
544
+ }
545
+ if (recipient) {
546
+ (0, type_2.assertPublicKey)(recipient);
547
+ return pss.send(this.getRequestOptionsForCall(options), topic, target, data, postageBatchId, recipient);
548
+ }
549
+ else {
550
+ return pss.send(this.getRequestOptionsForCall(options), topic, target, data, postageBatchId);
551
+ }
552
+ }
553
+ /**
554
+ * Subscribe to messages for given topic with Postal Service for Swarm
555
+ *
556
+ * **Warning! If connected Bee node is a light node, then he will never receive any message!**
557
+ * This is because light nodes does not fully participate in the data exchange in Swarm network and hence the message won't arrive to them.
558
+ *
559
+ * @param topic Topic name
560
+ * @param handler Message handler interface
561
+ *
562
+ * @returns Subscription to a given topic
563
+ *
564
+ * @see [Bee docs - PSS](https://docs.ethswarm.org/docs/dapps-on-swarm/pss)
565
+ * @see [Bee API reference - `GET /pss`](https://docs.ethswarm.org/api/#tag/Postal-Service-for-Swarm/paths/~1pss~1subscribe~1{topic}/get)
566
+ */
567
+ pssSubscribe(topic, handler) {
568
+ (0, type_2.assertPssMessageHandler)(handler);
569
+ if (typeof topic !== 'string') {
570
+ throw new TypeError('topic has to be an string!');
571
+ }
572
+ const ws = pss.subscribe(this.url, topic);
573
+ let cancelled = false;
574
+ const cancel = () => {
575
+ if (cancelled === false) {
576
+ cancelled = true;
577
+ // although the WebSocket API offers a `close` function, it seems that
578
+ // with the library that we are using (isomorphic-ws) it doesn't close
579
+ // the websocket properly, whereas `terminate` does
580
+ if (ws.terminate)
581
+ ws.terminate();
582
+ else
583
+ ws.close(); // standard Websocket in browser does not have terminate function
584
+ }
585
+ };
586
+ const subscription = {
587
+ topic,
588
+ cancel,
589
+ };
590
+ ws.onmessage = async (ev) => {
591
+ const data = await (0, data_1.prepareWebsocketData)(ev.data);
592
+ // ignore empty messages
593
+ if (data.length > 0) {
594
+ handler.onMessage((0, bytes_1.wrapBytesWithHelpers)(data), subscription);
595
+ }
596
+ };
597
+ ws.onerror = ev => {
598
+ // ignore errors after subscription was cancelled
599
+ if (!cancelled) {
600
+ handler.onError(new error_1.BeeError(ev.message), subscription);
601
+ }
602
+ };
603
+ return subscription;
604
+ }
605
+ /**
606
+ * Receive message with Postal Service for Swarm
607
+ *
608
+ * Because sending a PSS message is slow and CPU intensive,
609
+ * it is not supposed to be used for general messaging but
610
+ * most likely for setting up an encrypted communication
611
+ * channel by sending an one-off message.
612
+ *
613
+ * This is a helper function to wait for exactly one message to
614
+ * arrive and then cancel the subscription. Additionally a
615
+ * timeout can be provided for the message to arrive or else
616
+ * an error will be thrown.
617
+ *
618
+ * **Warning! If connected Bee node is a light node, then he will never receive any message!**
619
+ * This is because light nodes does not fully participate in the data exchange in Swarm network and hence the message won't arrive to them.
620
+ *
621
+ * @param topic Topic name
622
+ * @param timeoutMsec Timeout in milliseconds
623
+ *
624
+ * @returns Message in byte array
625
+ *
626
+ * @see [Bee docs - PSS](https://docs.ethswarm.org/docs/dapps-on-swarm/pss)
627
+ * @see [Bee API reference - `GET /pss`](https://docs.ethswarm.org/api/#tag/Postal-Service-for-Swarm/paths/~1pss~1subscribe~1{topic}/get)
628
+ */
629
+ async pssReceive(topic, timeoutMsec = 0) {
630
+ if (typeof topic !== 'string') {
631
+ throw new TypeError('topic has to be an string!');
632
+ }
633
+ if (typeof timeoutMsec !== 'number') {
634
+ throw new TypeError('timeoutMsc parameter has to be a number!');
635
+ }
636
+ return new Promise((resolve, reject) => {
637
+ let timeout;
638
+ const subscription = this.pssSubscribe(topic, {
639
+ onError: error => {
640
+ clearTimeout(timeout);
641
+ subscription.cancel();
642
+ reject(error.message);
643
+ },
644
+ onMessage: message => {
645
+ clearTimeout(timeout);
646
+ subscription.cancel();
647
+ resolve(message);
648
+ },
649
+ });
650
+ if (timeoutMsec > 0) {
651
+ // we need to cast the type because Typescript is getting confused with Node.js'
652
+ // alternative type definitions
653
+ timeout = setTimeout(() => {
654
+ subscription.cancel();
655
+ reject(new error_1.BeeError('pssReceive timeout'));
656
+ }, timeoutMsec);
657
+ }
658
+ });
659
+ }
660
+ /**
661
+ * Create feed manifest chunk and return the reference to it.
662
+ *
663
+ * Feed manifest chunk allows for a feed to be able to be resolved through `/bzz` endpoint.
664
+ *
665
+ * @param postageBatchId Postage BatchId to be used to create the Feed Manifest
666
+ * @param type The type of the feed, can be 'epoch' or 'sequence'
667
+ * @param topic Topic in hex or bytes
668
+ * @param owner Owner's ethereum address in hex or bytes
669
+ * @param options Options that affects the request behavior
670
+ *
671
+ * @see [Bee docs - Feeds](https://docs.ethswarm.org/docs/dapps-on-swarm/feeds)
672
+ * @see [Bee API reference - `POST /feeds`](https://docs.ethswarm.org/api/#tag/Feed/paths/~1feeds~1{owner}~1{topic}/post)
673
+ */
674
+ async createFeedManifest(postageBatchId, type, topic, owner, options) {
675
+ (0, type_2.assertRequestOptions)(options);
676
+ (0, type_1.assertFeedType)(type);
677
+ (0, type_2.assertBatchId)(postageBatchId);
678
+ const canonicalTopic = (0, topic_1.makeTopic)(topic);
679
+ const canonicalOwner = (0, eth_1.makeHexEthAddress)(owner);
680
+ const reference = await (0, feed_2.createFeedManifest)(this.getRequestOptionsForCall(options), canonicalOwner, canonicalTopic, postageBatchId, {
681
+ type,
682
+ });
683
+ return (0, type_2.addCidConversionFunction)({ reference }, swarm_cid_1.ReferenceType.FEED);
684
+ }
685
+ /**
686
+ * Make a new feed reader for downloading feed updates.
687
+ *
688
+ * @param type The type of the feed, can be 'epoch' or 'sequence'
689
+ * @param topic Topic in hex or bytes
690
+ * @param owner Owner's ethereum address in hex or bytes
691
+ * @param options Options that affects the request behavior
692
+ *
693
+ * @see [Bee docs - Feeds](https://docs.ethswarm.org/docs/dapps-on-swarm/feeds)
694
+ */
695
+ makeFeedReader(type, topic, owner, options) {
696
+ (0, type_2.assertRequestOptions)(options);
697
+ (0, type_1.assertFeedType)(type);
698
+ const canonicalTopic = (0, topic_1.makeTopic)(topic);
699
+ const canonicalOwner = (0, eth_1.makeHexEthAddress)(owner);
700
+ return (0, feed_1.makeFeedReader)(this.getRequestOptionsForCall(options), type, canonicalTopic, canonicalOwner);
701
+ }
702
+ /**
703
+ * Make a new feed writer for updating feeds
704
+ *
705
+ * @param type The type of the feed, can be 'epoch' or 'sequence'
706
+ * @param topic Topic in hex or bytes
707
+ * @param signer The signer's private key or a Signer instance that can sign data
708
+ * @param options Options that affects the request behavior
709
+ *
710
+ * @see [Bee docs - Feeds](https://docs.ethswarm.org/docs/dapps-on-swarm/feeds)
711
+ */
712
+ makeFeedWriter(type, topic, signer, options) {
713
+ (0, type_2.assertRequestOptions)(options);
714
+ (0, type_1.assertFeedType)(type);
715
+ const canonicalTopic = (0, topic_1.makeTopic)(topic);
716
+ const canonicalSigner = this.resolveSigner(signer);
717
+ return (0, feed_1.makeFeedWriter)(this.getRequestOptionsForCall(options), type, canonicalTopic, canonicalSigner);
718
+ }
719
+ /**
720
+ * High-level function that allows you to easily set JSON data to feed.
721
+ * JSON-like data types are supported.
722
+ *
723
+ * The default Signer of Bee instance is used if `options.signer` is not specified.
724
+ * If none of those two is set error is thrown.
725
+ *
726
+ * @param postageBatchId Postage BatchId to be used to upload the data with
727
+ * @param topic Human readable string, that is internally hashed so there are no constrains there.
728
+ * @param data JSON compatible data
729
+ * @param options
730
+ * @param options.signer Custom instance of Signer or string with private key.
731
+ * @param options.type Type of Feed
732
+ *
733
+ * @throws BeeError if `options.signer` is not specified nor the default Signer on Bee's instance is specified.
734
+ *
735
+ * @see [Bee docs - Feeds](https://docs.ethswarm.org/docs/dapps-on-swarm/feeds)
736
+ */
737
+ async setJsonFeed(postageBatchId, topic, data, options, requestOptions) {
738
+ (0, type_2.assertRequestOptions)(options, 'JsonFeedOptions');
739
+ (0, type_2.assertBatchId)(postageBatchId);
740
+ const hashedTopic = this.makeFeedTopic(topic);
741
+ const feedType = options?.type ?? type_1.DEFAULT_FEED_TYPE;
742
+ const writer = this.makeFeedWriter(feedType, hashedTopic, options?.signer, requestOptions);
743
+ return (0, json_1.setJsonData)(this, writer, postageBatchId, data, options, requestOptions);
744
+ }
745
+ /**
746
+ * High-level function that allows you to easily get data from feed.
747
+ * Returned data are parsed using JSON.parse().
748
+ *
749
+ * This method also supports specification of `signer` object passed to constructor. The order of evaluation is:
750
+ * - `options.address`
751
+ * - `options.signer`
752
+ * - `this.signer`
753
+ *
754
+ * At least one of these has to be specified!
755
+ *
756
+ * @param topic Human readable string, that is internally hashed so there are no constrains there.
757
+ * @param options
758
+ * @param options.signer Custom instance of Signer or string with private key. This option is exclusive with `address` option.
759
+ * @param options.address Ethereum address of owner of the feed that signed it. This option is exclusive with `signer` option.
760
+ * @param options.type Type of Feed
761
+ *
762
+ * @see [Bee docs - Feeds](https://docs.ethswarm.org/docs/dapps-on-swarm/feeds)
763
+ */
764
+ async getJsonFeed(topic, options) {
765
+ (0, type_2.assertRequestOptions)(options, 'JsonFeedOptions');
766
+ const hashedTopic = this.makeFeedTopic(topic);
767
+ const feedType = options?.type ?? type_1.DEFAULT_FEED_TYPE;
768
+ if (options?.signer && options?.address) {
769
+ throw new error_1.BeeError('Both options "signer" and "address" can not be specified at one time!');
770
+ }
771
+ let address;
772
+ if (options?.address) {
773
+ address = (0, eth_1.makeEthAddress)(options?.address);
774
+ }
775
+ else {
776
+ try {
777
+ address = this.resolveSigner(options?.signer).address;
778
+ }
779
+ catch (e) {
780
+ if (e instanceof error_1.BeeError) {
781
+ throw new error_1.BeeError('Either address, signer or default signer has to be specified!');
782
+ }
783
+ else {
784
+ throw e;
785
+ }
786
+ }
787
+ }
788
+ const reader = this.makeFeedReader(feedType, hashedTopic, address, options);
789
+ return (0, json_1.getJsonData)(this, reader);
790
+ }
791
+ /**
792
+ * Make a new feed topic from a string
793
+ *
794
+ * Because the topic has to be 32 bytes long this function
795
+ * hashes the input string to create a topic string of arbitrary length.
796
+ *
797
+ * @param topic The input string
798
+ */
799
+ makeFeedTopic(topic) {
800
+ return (0, topic_1.makeTopicFromString)(topic);
801
+ }
802
+ /**
803
+ * Returns an object for reading single owner chunks
804
+ *
805
+ * @param ownerAddress The ethereum address of the owner
806
+ * @param options Options that affects the request behavior
807
+ * @see [Bee docs - Chunk Types](https://docs.ethswarm.org/docs/dapps-on-swarm/chunk-types#single-owner-chunks)
808
+ */
809
+ makeSOCReader(ownerAddress, options) {
810
+ (0, type_2.assertRequestOptions)(options);
811
+ const canonicalOwner = (0, eth_1.makeEthAddress)(ownerAddress);
812
+ return {
813
+ owner: (0, eth_1.makeHexEthAddress)(canonicalOwner),
814
+ download: soc_1.downloadSingleOwnerChunk.bind(null, this.getRequestOptionsForCall(options), canonicalOwner),
815
+ };
816
+ }
817
+ /**
818
+ * Returns an object for reading and writing single owner chunks
819
+ *
820
+ * @param signer The signer's private key or a Signer instance that can sign data
821
+ * @param options Options that affects the request behavior
822
+ * @see [Bee docs - Chunk Types](https://docs.ethswarm.org/docs/dapps-on-swarm/chunk-types#single-owner-chunks)
823
+ */
824
+ makeSOCWriter(signer, options) {
825
+ (0, type_2.assertRequestOptions)(options);
826
+ const canonicalSigner = this.resolveSigner(signer);
827
+ return {
828
+ ...this.makeSOCReader(canonicalSigner.address, options),
829
+ upload: soc_1.uploadSingleOwnerChunkData.bind(null, this.getRequestOptionsForCall(options), canonicalSigner),
830
+ };
831
+ }
832
+ /**
833
+ * Ping the Bee node to see if there is a live Bee node on the given URL.
834
+ *
835
+ * @param options Options that affects the request behavior
836
+ * @throws If connection was not successful throw error
837
+ */
838
+ async checkConnection(options) {
839
+ (0, type_2.assertRequestOptions)(options, 'PostageBatchOptions');
840
+ return status.checkConnection(this.getRequestOptionsForCall(options));
841
+ }
842
+ /**
843
+ * Ping the Bee node to see if there is a live Bee node on the given URL.
844
+ *
845
+ * @param options Options that affects the request behavior
846
+ * @returns true if successful, false on error
847
+ */
848
+ async isConnected(options) {
849
+ (0, type_2.assertRequestOptions)(options, 'PostageBatchOptions');
850
+ try {
851
+ await status.checkConnection(this.getRequestOptionsForCall(options));
852
+ }
853
+ catch (e) {
854
+ return false;
855
+ }
856
+ return true;
857
+ }
858
+ /**
859
+ * @param signer
860
+ * @private
861
+ * @throws BeeError if either no Signer was passed or no default Signer was specified for the instance
862
+ */
863
+ resolveSigner(signer) {
864
+ if (signer) {
865
+ return (0, signer_1.makeSigner)(signer);
866
+ }
867
+ if (this.signer) {
868
+ return this.signer;
869
+ }
870
+ throw new error_1.BeeError('You have to pass Signer as property to either the method call or constructor! Non found.');
871
+ }
872
+ getRequestOptionsForCall(options) {
873
+ return options ? cafe_utility_1.Objects.deepMerge2(this.requestOptions, options) : this.requestOptions;
874
+ }
875
+ }
876
+ exports.Bee = Bee;