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