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