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