@ethersphere/bee-js 2.1.0 → 3.2.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 (160) hide show
  1. package/README.md +3 -3
  2. package/dist/index.browser.min.js +1 -1
  3. package/dist/index.browser.min.js.map +1 -1
  4. package/dist/index.min.js +1 -1
  5. package/dist/index.min.js.map +1 -1
  6. package/dist/src/bee-debug.d.ts +5 -1
  7. package/dist/src/bee.d.ts +23 -46
  8. package/dist/src/modules/chunk.d.ts +2 -10
  9. package/dist/src/modules/debug/status.d.ts +10 -4
  10. package/dist/src/types/debug.d.ts +19 -0
  11. package/dist/src/types/index.d.ts +31 -2
  12. package/dist/src/utils/bytes.d.ts +1 -1
  13. package/dist/src/utils/error.d.ts +21 -2
  14. package/dist/src/utils/expose.d.ts +1 -0
  15. package/dist/src/utils/http.d.ts +7 -9
  16. package/dist/src/utils/stamps.d.ts +12 -0
  17. package/dist/src/utils/type.d.ts +6 -0
  18. package/package.json +28 -27
  19. package/.babelrc.js +0 -33
  20. package/.depcheckrc.json +0 -27
  21. package/.editorconfig +0 -6
  22. package/.eslintignore +0 -1
  23. package/.eslintrc.js +0 -109
  24. package/.gitattributes +0 -1
  25. package/.github/config.yaml +0 -23
  26. package/.github/workflows/check.yaml +0 -57
  27. package/.github/workflows/publish_npmjs.yaml +0 -20
  28. package/.github/workflows/release_github.yaml +0 -20
  29. package/.github/workflows/tests.yaml +0 -128
  30. package/.huskyrc +0 -5
  31. package/.prettierrc +0 -13
  32. package/.vscode/launch.json +0 -49
  33. package/.vscode/settings.json +0 -39
  34. package/CHANGELOG.md +0 -520
  35. package/CODEOWNERS +0 -1
  36. package/CODE_OF_CONDUCT.md +0 -76
  37. package/commitlint.config.js +0 -3
  38. package/dist/src/modules/stamps.d.ts +0 -4
  39. package/jest-puppeteer.config.js +0 -5
  40. package/jest.config.ts +0 -85
  41. package/src/bee-debug.ts +0 -601
  42. package/src/bee.ts +0 -1087
  43. package/src/chunk/bmt.ts +0 -55
  44. package/src/chunk/cac.ts +0 -79
  45. package/src/chunk/serialize.ts +0 -16
  46. package/src/chunk/signer.ts +0 -141
  47. package/src/chunk/soc.ts +0 -190
  48. package/src/chunk/span.ts +0 -33
  49. package/src/feed/index.ts +0 -217
  50. package/src/feed/json.ts +0 -33
  51. package/src/feed/topic.ts +0 -23
  52. package/src/feed/type.ts +0 -12
  53. package/src/index.ts +0 -24
  54. package/src/modules/bytes.ts +0 -70
  55. package/src/modules/bzz.ts +0 -179
  56. package/src/modules/chunk.ts +0 -69
  57. package/src/modules/debug/balance.ts +0 -63
  58. package/src/modules/debug/chequebook.ts +0 -175
  59. package/src/modules/debug/chunk.ts +0 -39
  60. package/src/modules/debug/connectivity.ts +0 -61
  61. package/src/modules/debug/settlements.ts +0 -33
  62. package/src/modules/debug/stamps.ts +0 -96
  63. package/src/modules/debug/states.ts +0 -35
  64. package/src/modules/debug/status.ts +0 -34
  65. package/src/modules/debug/tag.ts +0 -19
  66. package/src/modules/debug/transactions.ts +0 -79
  67. package/src/modules/feed.ts +0 -105
  68. package/src/modules/pinning.ts +0 -74
  69. package/src/modules/pss.ts +0 -49
  70. package/src/modules/soc.ts +0 -40
  71. package/src/modules/stamps.ts +0 -59
  72. package/src/modules/status.ts +0 -13
  73. package/src/modules/stewardship.ts +0 -32
  74. package/src/modules/tag.ts +0 -83
  75. package/src/types/debug.ts +0 -204
  76. package/src/types/index.ts +0 -561
  77. package/src/types/ky-universal/common.d.ts +0 -17
  78. package/src/types/ky-universal/hooks.d.ts +0 -103
  79. package/src/types/ky-universal/index.d.ts +0 -125
  80. package/src/types/ky-universal/options.d.ts +0 -235
  81. package/src/types/ky-universal/response.d.ts +0 -38
  82. package/src/types/ky-universal/retry.d.ts +0 -39
  83. package/src/types/tar-js.d.ts +0 -1
  84. package/src/types/webpack-bundle-analyzer/index.d.ts +0 -13
  85. package/src/utils/bytes.ts +0 -144
  86. package/src/utils/collection.ts +0 -151
  87. package/src/utils/data.browser.ts +0 -68
  88. package/src/utils/data.ts +0 -48
  89. package/src/utils/error.ts +0 -23
  90. package/src/utils/eth.ts +0 -229
  91. package/src/utils/expose.ts +0 -48
  92. package/src/utils/file.ts +0 -40
  93. package/src/utils/hash.ts +0 -16
  94. package/src/utils/headers.ts +0 -63
  95. package/src/utils/hex.ts +0 -186
  96. package/src/utils/http.ts +0 -141
  97. package/src/utils/merge.ts +0 -34
  98. package/src/utils/pss.ts +0 -16
  99. package/src/utils/stream.ts +0 -146
  100. package/src/utils/tar.ts +0 -30
  101. package/src/utils/type.ts +0 -394
  102. package/src/utils/uint64.ts +0 -31
  103. package/src/utils/url.ts +0 -52
  104. package/test/bee.sh +0 -187
  105. package/test/data/1.txt +0 -1
  106. package/test/data/2.txt +0 -0
  107. package/test/data/empty +0 -0
  108. package/test/data/sub/3.txt +0 -1
  109. package/test/data/sub//303/251 +0 -0
  110. package/test/data/sub//360/237/230/216 +0 -0
  111. package/test/integration/bee-class.browser.spec.ts +0 -272
  112. package/test/integration/bee-class.spec.ts +0 -648
  113. package/test/integration/bee-debug-class.spec.ts +0 -114
  114. package/test/integration/chunk/bmt.spec.ts +0 -33
  115. package/test/integration/chunk/cac.spec.ts +0 -27
  116. package/test/integration/chunk/soc.spec.ts +0 -38
  117. package/test/integration/feed/index.spec.ts +0 -84
  118. package/test/integration/modules/bytes.spec.ts +0 -23
  119. package/test/integration/modules/bzz.spec.ts +0 -265
  120. package/test/integration/modules/chunk.spec.ts +0 -29
  121. package/test/integration/modules/debug/balance.spec.ts +0 -70
  122. package/test/integration/modules/debug/chequebook.spec.ts +0 -64
  123. package/test/integration/modules/debug/connectivity.spec.ts +0 -69
  124. package/test/integration/modules/debug/settlements.spec.ts +0 -34
  125. package/test/integration/modules/debug/states.spec.ts +0 -34
  126. package/test/integration/modules/debug/status.spec.ts +0 -65
  127. package/test/integration/modules/debug/tag.spec.ts +0 -27
  128. package/test/integration/modules/debug/transactions.spec.ts +0 -10
  129. package/test/integration/modules/feed.spec.ts +0 -61
  130. package/test/integration/modules/pinning.spec.ts +0 -155
  131. package/test/integration/modules/pss.spec.ts +0 -105
  132. package/test/integration/modules/status.spec.ts +0 -10
  133. package/test/integration/modules/stewardship.spec.ts +0 -32
  134. package/test/integration/modules/tag.spec.ts +0 -51
  135. package/test/test-type-sequencer.js +0 -36
  136. package/test/testpage/testpage.html +0 -20
  137. package/test/tests-setup.ts +0 -94
  138. package/test/unit/assertions.ts +0 -460
  139. package/test/unit/bee-class.spec.ts +0 -976
  140. package/test/unit/bee-debug-class.spec.ts +0 -435
  141. package/test/unit/chunk/bmt.spec.ts +0 -16
  142. package/test/unit/chunk/cac.spec.ts +0 -29
  143. package/test/unit/chunk/serialize.spec.ts +0 -24
  144. package/test/unit/chunk/signer.spec.ts +0 -140
  145. package/test/unit/chunk/soc.spec.ts +0 -25
  146. package/test/unit/chunk/span.spec.ts +0 -38
  147. package/test/unit/feed/json.spec.ts +0 -73
  148. package/test/unit/nock.ts +0 -151
  149. package/test/unit/utils/bytes.spec.ts +0 -32
  150. package/test/unit/utils/collection.spec.ts +0 -22
  151. package/test/unit/utils/eth.spec.ts +0 -224
  152. package/test/unit/utils/hex.spec.ts +0 -115
  153. package/test/unit/utils/stream.spec.ts +0 -30
  154. package/test/unit/utils/type.spec.ts +0 -32
  155. package/test/unit/utils/uint64.spec.ts +0 -102
  156. package/test/utils.ts +0 -426
  157. package/tsconfig.json +0 -18
  158. package/tsconfig.test.json +0 -11
  159. package/typedoc.json +0 -7
  160. package/webpack.config.ts +0 -141
@@ -1,648 +0,0 @@
1
- import { Bee, BeeArgumentError, BeeDebug, Collection, PssSubscription } from '../../src'
2
- import { makeSigner } from '../../src/chunk/signer'
3
- import { makeSOCAddress, uploadSingleOwnerChunkData } from '../../src/chunk/soc'
4
- import { ChunkReference } from '../../src/feed'
5
- import * as bzz from '../../src/modules/bzz'
6
- import { REFERENCE_HEX_LENGTH } from '../../src/types'
7
- import { makeBytes } from '../../src/utils/bytes'
8
- import { makeEthAddress } from '../../src/utils/eth'
9
- import { bytesToHex, HexString } from '../../src/utils/hex'
10
- import {
11
- beeDebugUrl,
12
- beeKy,
13
- beePeerDebugUrl,
14
- beePeerUrl,
15
- beeUrl,
16
- commonMatchers,
17
- createRandomNodeReadable,
18
- createReadableStream,
19
- ERR_TIMEOUT,
20
- FEED_TIMEOUT,
21
- getPostageBatch,
22
- makeTestTarget,
23
- BLOCKCHAIN_TRANSACTION_TIMEOUT,
24
- PSS_TIMEOUT,
25
- randomByteArray,
26
- sleep,
27
- testChunkPayload,
28
- testIdentity,
29
- testJsonHash,
30
- testJsonPayload,
31
- tryDeleteChunkFromLocalStorage,
32
- } from '../utils'
33
- import { Readable } from 'stream'
34
- import { TextEncoder } from 'util'
35
-
36
- commonMatchers()
37
-
38
- describe('Bee class', () => {
39
- const BEE_URL = beeUrl()
40
- const BEE_KY = beeKy()
41
- const BEE_PEER_URL = beePeerUrl()
42
- const BEE_DEBUG_PEER_URL = beePeerDebugUrl()
43
- const bee = new Bee(BEE_URL)
44
- const beePeer = new Bee(BEE_PEER_URL)
45
-
46
- it('should strip trailing slash', () => {
47
- const bee = new Bee('http://localhost:1633/')
48
- expect(bee.url).toEqual('http://localhost:1633')
49
- })
50
-
51
- describe('files', () => {
52
- it('should work with files', async () => {
53
- const content = new Uint8Array([1, 2, 3])
54
- const name = 'hello.txt'
55
- const contentType = 'text/html'
56
-
57
- const result = await bee.uploadFile(getPostageBatch(), content, name, { contentType })
58
- const file = await bee.downloadFile(result.reference)
59
-
60
- expect(file.name).toEqual(name)
61
- expect(file.data).toEqual(content)
62
- })
63
-
64
- it('should work with files and tags', async () => {
65
- const tag = await bee.createTag()
66
-
67
- // Should fit into 4 chunks
68
- const content = randomByteArray(13000)
69
- const name = 'hello.txt'
70
- const contentType = 'text/html'
71
-
72
- const result = await bee.uploadFile(getPostageBatch(), content, name, { contentType, tag: tag.uid })
73
- const file = await bee.downloadFile(result.reference)
74
-
75
- expect(file.name).toEqual(name)
76
- expect(file.data).toEqual(content)
77
-
78
- const retrievedTag = await bee.retrieveTag(tag)
79
- expect(retrievedTag.total).toEqual(8)
80
- })
81
-
82
- it('should work with file object', async () => {
83
- const content = new Uint8Array([1, 2, 3])
84
- const name = 'hello.txt'
85
- const type = 'text/plain'
86
- const file = {
87
- arrayBuffer: () => content,
88
- name,
89
- type,
90
- } as unknown as File
91
-
92
- const result = await bee.uploadFile(getPostageBatch(), file)
93
- const downloadedFile = await bee.downloadFile(result.reference)
94
-
95
- expect(downloadedFile.data).toEqual(content)
96
- expect(downloadedFile.name).toEqual(name)
97
- expect(downloadedFile.contentType).toEqual(type)
98
- })
99
-
100
- it('should work with file object and name overridden', async () => {
101
- const content = new Uint8Array([1, 2, 3])
102
- const name = 'hello.txt'
103
- const file = {
104
- arrayBuffer: () => content,
105
- name,
106
- } as unknown as File
107
- const nameOverride = 'hello-override.txt'
108
-
109
- const result = await bee.uploadFile(getPostageBatch(), file, nameOverride)
110
- const downloadedFile = await bee.downloadFile(result.reference)
111
-
112
- expect(downloadedFile.data).toEqual(content)
113
- expect(downloadedFile.name).toEqual(nameOverride)
114
- })
115
-
116
- it('should work with file object and content-type overridden', async () => {
117
- const content = new Uint8Array([1, 2, 3])
118
- const file = {
119
- arrayBuffer: () => content,
120
- name: 'hello.txt',
121
- type: 'text/plain',
122
- } as unknown as File
123
- const contentTypeOverride = 'text/plain+override'
124
-
125
- const result = await bee.uploadFile(getPostageBatch(), file, undefined, { contentType: contentTypeOverride })
126
- const downloadedFile = await bee.downloadFile(result.reference)
127
-
128
- expect(downloadedFile.data).toEqual(content)
129
- expect(downloadedFile.contentType).toEqual(contentTypeOverride)
130
- })
131
-
132
- it('should work with NodeJS readable', async () => {
133
- const readable = Readable.from([new TextEncoder().encode('hello '), new TextEncoder().encode('world')])
134
- const name = 'hello.txt'
135
- const contentType = 'text/plain'
136
-
137
- const result = await bee.uploadFile(getPostageBatch(), readable, name, { contentType })
138
- const file = await bee.downloadFile(result.reference)
139
-
140
- expect(file.name).toEqual(name)
141
- expect(file.data.text()).toEqual('hello world')
142
- })
143
-
144
- it('should work with WHATWG readable-stream', async () => {
145
- const readable = createReadableStream([new TextEncoder().encode('hello '), new TextEncoder().encode('world')])
146
- const name = 'hello.txt'
147
- const contentType = 'text/plain'
148
-
149
- const result = await bee.uploadFile(getPostageBatch(), readable, name, { contentType })
150
- const file = await bee.downloadFile(result.reference)
151
-
152
- expect(file.name).toEqual(name)
153
- expect(file.data.text()).toEqual('hello world')
154
- }, 1000000)
155
-
156
- it('should work with readable and tags', async () => {
157
- const tag = await bee.createTag()
158
-
159
- const readable = createRandomNodeReadable(13000)
160
- const name = 'hello.txt'
161
- const contentType = 'text/plain'
162
-
163
- const result = await bee.uploadFile(getPostageBatch(), readable, name, {
164
- contentType,
165
- tag: tag.uid,
166
- })
167
-
168
- const file = await bee.downloadFile(result.reference)
169
-
170
- expect(file.name).toEqual(name)
171
- expect(file.data.length).toEqual(13000)
172
-
173
- const retrievedTag = await bee.retrieveTag(tag)
174
- expect(retrievedTag.total).toEqual(8)
175
- })
176
- })
177
-
178
- describe('collections', () => {
179
- it('should work with directory with unicode filenames', async () => {
180
- const result = await bee.uploadFilesFromDirectory(getPostageBatch(), './test/data')
181
-
182
- expect(result.reference.length).toEqual(REFERENCE_HEX_LENGTH)
183
- })
184
-
185
- it('should upload collection', async () => {
186
- const directoryStructure: Collection<Uint8Array> = [
187
- {
188
- path: '0',
189
- data: new TextEncoder().encode('hello-world'),
190
- },
191
- ]
192
-
193
- const result = await bee.uploadCollection(getPostageBatch(), directoryStructure)
194
- const file = await bee.downloadFile(result.reference, directoryStructure[0].path)
195
-
196
- expect(file.name).toEqual(directoryStructure[0].path)
197
- expect(file.data.text()).toEqual('hello-world')
198
- })
199
- })
200
-
201
- describe('tags', () => {
202
- it('should list tags', async () => {
203
- const originalTags = await bee.getAllTags({ limit: 1000 })
204
- const createdTag = await bee.createTag()
205
- const updatedTags = await bee.getAllTags({ limit: 1000 })
206
-
207
- expect(updatedTags.length - originalTags.length).toEqual(1)
208
- expect(originalTags.find(tag => tag.uid === createdTag.uid)).toBeFalsy()
209
- expect(updatedTags.find(tag => tag.uid === createdTag.uid)).toBeTruthy()
210
- })
211
-
212
- it('should retrieve previously created empty tag', async () => {
213
- const tag = await bee.createTag()
214
- const tag2 = await bee.retrieveTag(tag)
215
-
216
- expect(tag).toEqual(tag2)
217
- })
218
-
219
- it('should delete tag', async () => {
220
- const createdTag = await bee.createTag()
221
- const originalTags = await bee.getAllTags({ limit: 1000 })
222
- expect(originalTags.find(tag => tag.uid === createdTag.uid)).toBeTruthy()
223
-
224
- await bee.deleteTag(createdTag)
225
- const updatedTags = await bee.getAllTags({ limit: 1000 })
226
-
227
- expect(updatedTags.length - originalTags.length).toEqual(-1)
228
- expect(updatedTags.find(tag => tag.uid === createdTag.uid)).toBeFalsy()
229
- })
230
- })
231
-
232
- describe('pinning', () => {
233
- it('should list all pins', async () => {
234
- const content = new Uint8Array([1, 2, 3])
235
- const result = await bee.uploadFile(getPostageBatch(), content)
236
-
237
- await bee.pin(result.reference) // Nothing is asserted as nothing is returned, will throw error if something is wrong
238
-
239
- const pinnedChunks = await bee.getAllPins()
240
- expect(pinnedChunks).toBeType('array')
241
- expect(pinnedChunks.includes(result.reference)).toBeTruthy()
242
- })
243
-
244
- it('should get pinning status', async () => {
245
- const content = randomByteArray(16, Date.now())
246
- const result = await bee.uploadFile(getPostageBatch(), content, 'test', {
247
- pin: false,
248
- })
249
-
250
- const statusBeforePinning = bee.getPin(result.reference)
251
- await expect(statusBeforePinning).rejects.toThrowError('Not Found')
252
-
253
- await bee.pin(result.reference) // Nothing is asserted as nothing is returned, will throw error if something is wrong
254
-
255
- const statusAfterPinning = await bee.getPin(result.reference)
256
- expect(statusAfterPinning).toHaveProperty('reference', result.reference)
257
- })
258
-
259
- it('should pin and unpin files', async () => {
260
- const content = new Uint8Array([1, 2, 3])
261
-
262
- const result = await bee.uploadFile(getPostageBatch(), content)
263
-
264
- await bee.pin(result.reference) // Nothing is asserted as nothing is returned, will throw error if something is wrong
265
- await bee.unpin(result.reference) // Nothing is asserted as nothing is returned, will throw error if something is wrong
266
- })
267
-
268
- it('should pin and unpin collection from directory', async () => {
269
- const path = './test/data/'
270
- const result = await bee.uploadFilesFromDirectory(getPostageBatch(), path)
271
-
272
- await bee.pin(result.reference) // Nothing is asserted as nothing is returned, will throw error if something is wrong
273
- await bee.unpin(result.reference) // Nothing is asserted as nothing is returned, will throw error if something is wrong
274
- })
275
-
276
- it('should pin and unpin data', async () => {
277
- const content = new Uint8Array([1, 2, 3])
278
-
279
- const result = await bee.uploadData(getPostageBatch(), content)
280
-
281
- await bee.pin(result.reference) // Nothing is asserted as nothing is returned, will throw error if something is wrong
282
- await bee.unpin(result.reference) // Nothing is asserted as nothing is returned, will throw error if something is wrong
283
- })
284
- })
285
-
286
- describe('stewardship', () => {
287
- it('should reupload pinned data', async () => {
288
- const content = randomByteArray(16, Date.now())
289
- const result = await bee.uploadData(getPostageBatch(), content, { pin: true })
290
-
291
- await sleep(10)
292
- await bee.reuploadPinnedData(result.reference) // Does not return anything, but will throw exception if something is going wrong
293
- })
294
-
295
- it(
296
- 'should check if reference is retrievable',
297
- async () => {
298
- const content = randomByteArray(16, Date.now())
299
- const result = await bee.uploadData(getPostageBatch(), content, { pin: true })
300
-
301
- await sleep(10)
302
- await expect(bee.isReferenceRetrievable(result.reference)).resolves.toEqual(true)
303
-
304
- // Reference that has correct form, but should not exist on the network
305
- await expect(
306
- bee.isReferenceRetrievable('ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4332'),
307
- ).resolves.toEqual(false)
308
- },
309
- ERR_TIMEOUT,
310
- )
311
- })
312
-
313
- describe('pss', () => {
314
- it(
315
- 'should send and receive data',
316
- async () => {
317
- return new Promise<void>((resolve, reject) => {
318
- ;(async () => {
319
- const topic = 'bee-class-topic'
320
- const message = new Uint8Array([1, 2, 3])
321
- const beeDebug = new BeeDebug(beeDebugUrl())
322
-
323
- bee.pssReceive(topic).then(receivedMessage => {
324
- expect(receivedMessage).toEqual(message)
325
- resolve()
326
- })
327
-
328
- const { overlay } = await beeDebug.getNodeAddresses()
329
- await beePeer.pssSend(getPostageBatch(BEE_DEBUG_PEER_URL), topic, makeTestTarget(overlay), message)
330
- })().catch(reject)
331
- })
332
- },
333
- PSS_TIMEOUT,
334
- )
335
-
336
- it(
337
- 'should send and receive data with public key',
338
- async () => {
339
- return new Promise<void>((resolve, reject) => {
340
- // Jest does not allow use `done` and return Promise so this wrapper work arounds that.
341
- ;(async () => {
342
- const topic = 'bee-class-topic-publickey'
343
- const message = new Uint8Array([1, 2, 3])
344
- const beeDebug = new BeeDebug(beeDebugUrl())
345
-
346
- bee.pssReceive(topic).then(receivedMessage => {
347
- expect(receivedMessage).toEqual(message)
348
- resolve()
349
- })
350
-
351
- const { overlay, pssPublicKey } = await beeDebug.getNodeAddresses()
352
- await beePeer.pssSend(
353
- getPostageBatch(BEE_DEBUG_PEER_URL),
354
- topic,
355
- makeTestTarget(overlay),
356
- message,
357
- pssPublicKey,
358
- )
359
- })().catch(reject)
360
- })
361
- },
362
- PSS_TIMEOUT,
363
- )
364
-
365
- it(
366
- 'should subscribe to topic',
367
- async () => {
368
- return new Promise<void>((resolve, reject) => {
369
- let subscription: PssSubscription
370
- ;(async () => {
371
- const topic = 'bee-class-subscribe-topic'
372
- const message = new Uint8Array([1, 2, 3])
373
- const beeDebug = new BeeDebug(beeDebugUrl())
374
-
375
- subscription = bee.pssSubscribe(topic, {
376
- onMessage: receivedMessage => {
377
- // without cancel jest complains for leaking handles and may hang
378
- subscription?.cancel()
379
-
380
- expect(receivedMessage).toEqual(message)
381
- resolve()
382
- },
383
- onError: e => {
384
- throw e
385
- },
386
- })
387
-
388
- const { overlay } = await beeDebug.getNodeAddresses()
389
- await beePeer.pssSend(getPostageBatch(BEE_DEBUG_PEER_URL), topic, makeTestTarget(overlay), message)
390
- })().catch(e => {
391
- // without cancel jest complains for leaking handles and may hang
392
- subscription?.cancel()
393
- reject(e)
394
- })
395
- })
396
- },
397
- PSS_TIMEOUT,
398
- )
399
-
400
- it('should time out', async () => {
401
- const topic = 'bee-class-receive-timeout'
402
-
403
- await expect(bee.pssReceive(topic, 1)).rejects.toThrow('pssReceive timeout')
404
- })
405
- })
406
-
407
- describe('feeds', () => {
408
- const owner = testIdentity.address
409
- const signer = testIdentity.privateKey
410
- const topic = randomByteArray(32, Date.now())
411
-
412
- it(
413
- 'feed writer with two updates',
414
- async () => {
415
- const feed = bee.makeFeedWriter('sequence', topic, signer)
416
- const referenceZero = makeBytes(32) // all zeroes
417
-
418
- await feed.upload(getPostageBatch(), referenceZero)
419
- const firstUpdateReferenceResponse = await feed.download()
420
-
421
- expect(firstUpdateReferenceResponse.reference).toEqual(bytesToHex(referenceZero))
422
- expect(firstUpdateReferenceResponse.feedIndex).toEqual('0000000000000000')
423
-
424
- const referenceOne = new Uint8Array([...new Uint8Array([1]), ...new Uint8Array(31)]) as ChunkReference
425
-
426
- await feed.upload(getPostageBatch(), referenceOne)
427
- const secondUpdateReferenceResponse = await feed.download()
428
-
429
- expect(secondUpdateReferenceResponse.reference).toEqual(bytesToHex(referenceOne))
430
- expect(secondUpdateReferenceResponse.feedIndex).toEqual('0000000000000001')
431
- // TODO the timeout was increased because this test is flaky
432
- // most likely there is an issue with the lookup
433
- // https://github.com/ethersphere/bee/issues/1248#issuecomment-786588911
434
- },
435
- FEED_TIMEOUT,
436
- )
437
-
438
- it(
439
- 'create feeds manifest and retrieve the data',
440
- async () => {
441
- const directoryStructure: Collection<Uint8Array> = [
442
- {
443
- path: 'index.html',
444
- data: new TextEncoder().encode('some data'),
445
- },
446
- ]
447
- const cacResult = await bzz.uploadCollection(BEE_KY, directoryStructure, getPostageBatch())
448
-
449
- const feed = bee.makeFeedWriter('sequence', topic, signer)
450
- await feed.upload(getPostageBatch(), cacResult.reference)
451
- const manifestReference = await bee.createFeedManifest(getPostageBatch(), 'sequence', topic, owner)
452
-
453
- expect(typeof manifestReference).toBe('string')
454
-
455
- // this calls /bzz endpoint that should resolve the manifest and the feed returning the latest feed's content
456
- const file = await bee.downloadFile(manifestReference, 'index.html')
457
- expect(new TextDecoder().decode(file.data)).toEqual('some data')
458
- },
459
- FEED_TIMEOUT,
460
- )
461
-
462
- describe('topic', () => {
463
- it('create feed topic', () => {
464
- const topic = bee.makeFeedTopic('swarm.eth:application:handshake')
465
- const feed = bee.makeFeedReader('sequence', topic, owner)
466
-
467
- expect(feed.topic).toEqual(topic)
468
- })
469
- })
470
- })
471
-
472
- describe('soc', () => {
473
- const socHash = '9d453ebb73b2fedaaf44ceddcf7a0aa37f3e3d6453fea5841c31f0ea6d61dc85' as HexString
474
-
475
- describe('writer', () => {
476
- it('should read and write', async () => {
477
- const identifier = makeBytes(32) // all zeroes
478
- const socAddress = makeSOCAddress(identifier, makeEthAddress(testIdentity.address))
479
- await tryDeleteChunkFromLocalStorage(socAddress)
480
-
481
- const socWriter = bee.makeSOCWriter(testIdentity.privateKey)
482
-
483
- const reference = await socWriter.upload(getPostageBatch(), identifier, testChunkPayload)
484
- expect(reference).toEqual(socHash)
485
-
486
- const soc = await socWriter.download(identifier)
487
- const payload = soc.payload()
488
- expect(payload).toEqual(testChunkPayload)
489
- })
490
- })
491
-
492
- describe('reader', () => {
493
- it('should read', async () => {
494
- const signer = makeSigner(testIdentity.privateKey)
495
- const identifier = makeBytes(32) // all zeroes
496
- const socAddress = makeSOCAddress(identifier, makeEthAddress(testIdentity.address))
497
- await tryDeleteChunkFromLocalStorage(socAddress)
498
- await uploadSingleOwnerChunkData(BEE_KY, signer, getPostageBatch(), identifier, testChunkPayload)
499
-
500
- const socReader = bee.makeSOCReader(testIdentity.address)
501
- const soc = await socReader.download(identifier)
502
- const payload = soc.payload()
503
- expect(payload).toEqual(testChunkPayload)
504
- })
505
- })
506
- })
507
-
508
- describe('signer', () => {
509
- it('should be possible to pass it in constructor', async () => {
510
- const identifier = makeBytes(32)
511
- identifier[31] = 1
512
-
513
- const socAddress = makeSOCAddress(identifier, makeEthAddress(testIdentity.address))
514
- await tryDeleteChunkFromLocalStorage(socAddress)
515
-
516
- const bee = new Bee(BEE_URL, { signer: testIdentity.privateKey })
517
- const socWriter = bee.makeSOCWriter()
518
-
519
- const reference = await socWriter.upload(getPostageBatch(), identifier, testChunkPayload)
520
- expect(reference).toEqual('00019ec85e8859aa641cf149fbd1147ac7965a9cad1dfe4ab7beaa12d5dc8027')
521
- })
522
-
523
- it('should prioritize signer passed to method', async () => {
524
- const identifier = makeBytes(32)
525
- identifier[31] = 2
526
-
527
- const socAddress = makeSOCAddress(identifier, makeEthAddress(testIdentity.address))
528
- await tryDeleteChunkFromLocalStorage(socAddress)
529
-
530
- // We pass different private key to the instance
531
- const bee = new Bee(BEE_URL, {
532
- signer: '634fb5a872396d9611e5c9f9d7233cfa93f395c093371017ff44aa9ae6564cdd',
533
- })
534
- const socWriter = bee.makeSOCWriter(testIdentity.privateKey)
535
-
536
- const reference = await socWriter.upload(getPostageBatch(), identifier, testChunkPayload)
537
- expect(reference).toEqual('d1a21cce4c86411f6af2f621ce9a3a0aa3cc5cea6cc9e1b28523d28411398cfb')
538
- })
539
-
540
- it('should throw if no signers are passed', () => {
541
- const bee = new Bee(BEE_URL)
542
- expect(() => bee.makeSOCWriter()).toThrow()
543
- })
544
- })
545
-
546
- describe('JsonFeed', () => {
547
- const TOPIC = 'some=very%nice#topic'
548
-
549
- it(
550
- 'should set JSON to feed',
551
- async () => {
552
- await bee.setJsonFeed(getPostageBatch(), TOPIC, testJsonPayload, { signer: testIdentity.privateKey })
553
-
554
- const hashedTopic = bee.makeFeedTopic(TOPIC)
555
- const reader = bee.makeFeedReader('sequence', hashedTopic, testIdentity.address)
556
- const chunkReferenceResponse = await reader.download()
557
- expect(chunkReferenceResponse.reference).toEqual(testJsonHash)
558
-
559
- const downloadedData = await bee.downloadData(chunkReferenceResponse.reference)
560
- expect(downloadedData.json()).toEqual(testJsonPayload)
561
- },
562
- FEED_TIMEOUT,
563
- )
564
-
565
- it(
566
- 'should get JSON from feed',
567
- async () => {
568
- const data = [{ some: { other: 'object' } }]
569
-
570
- const hashedTopic = bee.makeFeedTopic(TOPIC)
571
- const writer = bee.makeFeedWriter('sequence', hashedTopic, testIdentity.privateKey)
572
- const dataChunkResult = await bee.uploadData(getPostageBatch(), JSON.stringify(data))
573
- await writer.upload(getPostageBatch(), dataChunkResult.reference)
574
-
575
- const fetchedData = await bee.getJsonFeed(TOPIC, { signer: testIdentity.privateKey })
576
- expect(fetchedData).toEqual(data)
577
- },
578
- FEED_TIMEOUT,
579
- )
580
- it(
581
- 'should get JSON from feed with address',
582
- async () => {
583
- const data = [{ some: { other: 'object' } }]
584
-
585
- const hashedTopic = bee.makeFeedTopic(TOPIC)
586
- const writer = bee.makeFeedWriter('sequence', hashedTopic, testIdentity.privateKey)
587
- const dataChunkResult = await bee.uploadData(getPostageBatch(), JSON.stringify(data))
588
- await writer.upload(getPostageBatch(), dataChunkResult.reference)
589
-
590
- const fetchedData = await bee.getJsonFeed(TOPIC, { address: testIdentity.address })
591
- expect(fetchedData).toEqual(data)
592
- },
593
- FEED_TIMEOUT,
594
- )
595
- })
596
-
597
- describe('PostageBatch', () => {
598
- it(
599
- 'should create a new postage batch with zero amount',
600
- async () => {
601
- const batchId = await bee.createPostageBatch('0', 17)
602
- const allBatches = await bee.getAllPostageBatch()
603
-
604
- expect(allBatches.find(batch => batch.batchID === batchId)).toBeTruthy()
605
- },
606
- BLOCKCHAIN_TRANSACTION_TIMEOUT,
607
- )
608
-
609
- it(
610
- 'should have both immutable true and false',
611
- async () => {
612
- await bee.createPostageBatch('1', 17, { immutableFlag: true })
613
- await bee.createPostageBatch('1', 17, { immutableFlag: false })
614
- const allBatches = await bee.getAllPostageBatch()
615
-
616
- expect(allBatches.find(batch => batch.immutableFlag === true)).toBeTruthy()
617
- expect(allBatches.find(batch => batch.immutableFlag === false)).toBeTruthy()
618
- },
619
- BLOCKCHAIN_TRANSACTION_TIMEOUT * 2,
620
- )
621
-
622
- it('should have all properties', async () => {
623
- const allBatches = await bee.getAllPostageBatch()
624
-
625
- expect(allBatches.length).toBeGreaterThan(0)
626
-
627
- expect(allBatches).toEqual(
628
- expect.arrayContaining([
629
- expect.objectContaining({
630
- batchID: expect.any(String),
631
- utilization: expect.any(Number),
632
- usable: expect.any(Boolean),
633
- label: expect.any(String),
634
- depth: expect.any(Number),
635
- amount: expect.any(String),
636
- bucketDepth: expect.any(Number),
637
- blockNumber: expect.any(Number),
638
- immutableFlag: expect.any(Boolean),
639
- }),
640
- ]),
641
- )
642
- })
643
-
644
- it('should error with negative amount', async () => {
645
- await expect(bee.createPostageBatch('-1', 17)).rejects.toThrowError(BeeArgumentError)
646
- })
647
- })
648
- })