@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
@@ -1,179 +0,0 @@
1
- import { assertAllIsDone, downloadDataMock, fetchFeedUpdateMock, MOCK_SERVER_URL, uploadFileMock } from '../nock'
2
- import { testBatchId, testChunkHash, testIdentity, testJsonHash } from '../../utils'
3
- import { Bee, ReferenceResponse } from '../../../src'
4
- import * as hooks from '../../../src/utils/hooks'
5
-
6
- describe('hooks', () => {
7
- it('should call with request', async () => {
8
- const bee = new Bee(MOCK_SERVER_URL)
9
-
10
- const topic = bee.makeFeedTopic('some-topic')
11
- fetchFeedUpdateMock(testIdentity.address, topic).reply(200, {
12
- reference: testJsonHash,
13
- } as ReferenceResponse)
14
-
15
- const requestSpy = jest.fn()
16
- const responseSpy = jest.fn()
17
- hooks.onRequest(requestSpy)
18
- hooks.onResponse(responseSpy)
19
-
20
- const feedReader = await bee.makeFeedReader('sequence', topic, testIdentity.address)
21
- const feedUpdate = await feedReader.download()
22
-
23
- expect(feedUpdate.reference).toEqual(testJsonHash)
24
-
25
- expect(requestSpy.mock.calls.length).toEqual(1)
26
- expect(requestSpy.mock.calls[0].length).toEqual(1)
27
- expect(requestSpy.mock.calls[0][0]).toEqual({
28
- url: `${MOCK_SERVER_URL}feeds/${testIdentity.address}/${topic}`,
29
- method: 'get',
30
- params: { type: 'sequence' },
31
- headers: { Accept: 'application/json, text/plain, */*' },
32
- })
33
-
34
- expect(responseSpy.mock.calls.length).toEqual(1)
35
- expect(responseSpy.mock.calls[0].length).toEqual(1)
36
- expect(responseSpy.mock.calls[0][0]).toEqual({
37
- status: 200,
38
- statusText: null,
39
- headers: {
40
- 'content-type': 'application/json',
41
- },
42
- data: {
43
- reference: testJsonHash,
44
- },
45
- request: {
46
- url: `${MOCK_SERVER_URL}feeds/${testIdentity.address}/${topic}`,
47
- method: 'get',
48
- data: undefined,
49
- params: { type: 'sequence' },
50
- headers: { Accept: 'application/json, text/plain, */*', 'User-Agent': 'axios/0.21.1' },
51
- },
52
- })
53
-
54
- assertAllIsDone()
55
- })
56
-
57
- it('should clear when requested', async () => {
58
- const bee = new Bee(MOCK_SERVER_URL)
59
-
60
- const topic = bee.makeFeedTopic('some-topic')
61
- fetchFeedUpdateMock(testIdentity.address, topic).reply(200, {
62
- reference: testJsonHash,
63
- } as ReferenceResponse)
64
- downloadDataMock(testChunkHash).reply(200, 'hello-world')
65
-
66
- const requestSpy = jest.fn()
67
- const responseSpy = jest.fn()
68
- const requestId = hooks.onRequest(requestSpy)
69
- const responseId = hooks.onResponse(responseSpy)
70
-
71
- const feedReader = await bee.makeFeedReader('sequence', topic, testIdentity.address)
72
- const feedUpdate = await feedReader.download()
73
-
74
- expect(feedUpdate.reference).toEqual(testJsonHash)
75
-
76
- expect(requestSpy.mock.calls.length).toEqual(1)
77
- expect(requestSpy.mock.calls[0].length).toEqual(1)
78
- expect(requestSpy.mock.calls[0][0]).toEqual({
79
- url: `${MOCK_SERVER_URL}feeds/${testIdentity.address}/${topic}`,
80
- method: 'get',
81
- params: { type: 'sequence' },
82
- headers: { Accept: 'application/json, text/plain, */*' },
83
- })
84
-
85
- expect(responseSpy.mock.calls.length).toEqual(1)
86
- expect(responseSpy.mock.calls[0].length).toEqual(1)
87
- expect(responseSpy.mock.calls[0][0]).toEqual({
88
- status: 200,
89
- statusText: null,
90
- headers: {
91
- 'content-type': 'application/json',
92
- },
93
- data: {
94
- reference: testJsonHash,
95
- },
96
- request: {
97
- url: `${MOCK_SERVER_URL}feeds/${testIdentity.address}/${topic}`,
98
- method: 'get',
99
- data: undefined,
100
- params: { type: 'sequence' },
101
- headers: { Accept: 'application/json, text/plain, */*', 'User-Agent': 'axios/0.21.1' },
102
- },
103
- })
104
-
105
- // Removing the hooks
106
- hooks.clearOnRequest(requestId)
107
- hooks.clearOnResponse(responseId)
108
- expect((await bee.downloadData(testChunkHash)).text()).toEqual('hello-world')
109
-
110
- // Asserting that the spy did not changed from last request
111
- expect(requestSpy.mock.calls.length).toEqual(1)
112
- expect(requestSpy.mock.calls[0].length).toEqual(1)
113
- expect(responseSpy.mock.calls.length).toEqual(1)
114
- expect(responseSpy.mock.calls[0].length).toEqual(1)
115
-
116
- assertAllIsDone()
117
- })
118
-
119
- it('should call with request with correct headers', async () => {
120
- const bee = new Bee(MOCK_SERVER_URL)
121
-
122
- uploadFileMock(testBatchId, 'nice.txt', { encrypt: true }).reply(200, {
123
- reference: testJsonHash,
124
- } as ReferenceResponse)
125
-
126
- const requestSpy = jest.fn()
127
- const responseSpy = jest.fn()
128
- hooks.onRequest(requestSpy)
129
- hooks.onResponse(responseSpy)
130
-
131
- const reference = await bee.uploadFile(testBatchId, 'hello world', 'nice.txt', { encrypt: true })
132
-
133
- expect(reference).toEqual(testJsonHash)
134
-
135
- expect(requestSpy.mock.calls.length).toEqual(1)
136
- expect(requestSpy.mock.calls[0].length).toEqual(1)
137
- expect(requestSpy.mock.calls[0][0]).toEqual({
138
- url: `${MOCK_SERVER_URL}bzz`,
139
- method: 'post',
140
- data: new Uint8Array([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]),
141
- params: { name: 'nice.txt' },
142
- headers: {
143
- Accept: 'application/json, text/plain, */*',
144
- 'Content-Type': 'application/x-www-form-urlencoded',
145
- 'swarm-encrypt': 'true',
146
- 'swarm-postage-batch-id': testBatchId,
147
- },
148
- })
149
-
150
- expect(responseSpy.mock.calls.length).toEqual(1)
151
- expect(responseSpy.mock.calls[0].length).toEqual(1)
152
- expect(responseSpy.mock.calls[0][0]).toEqual({
153
- status: 200,
154
- statusText: null,
155
- headers: {
156
- 'content-type': 'application/json',
157
- },
158
- data: {
159
- reference: testJsonHash,
160
- },
161
- request: {
162
- url: `${MOCK_SERVER_URL}bzz`,
163
- method: 'post',
164
- data: new ArrayBuffer(0),
165
- params: { name: 'nice.txt' },
166
- headers: {
167
- Accept: 'application/json, text/plain, */*',
168
- 'Content-Type': 'application/x-www-form-urlencoded',
169
- 'User-Agent': 'axios/0.21.1',
170
- 'Content-Length': 11,
171
- 'swarm-encrypt': 'true',
172
- 'swarm-postage-batch-id': testBatchId,
173
- },
174
- },
175
- })
176
-
177
- assertAllIsDone()
178
- })
179
- })
@@ -1,20 +0,0 @@
1
- import nock from 'nock'
2
- import { Bee } from '../../../src'
3
- import { setDefaultHeaders } from '../../../src/utils/safe-axios'
4
-
5
- describe('safeAxios', () => {
6
- describe('setDefaultHeaders', () => {
7
- it('should set default headers that are added to all requests', async () => {
8
- const MOCK_SERVER_URL = 'http://localhost:12345/'
9
- const scope = nock(MOCK_SERVER_URL).get('/').matchHeader('X-Custom-Header', 'some value').reply(200)
10
-
11
- const bee = new Bee(MOCK_SERVER_URL)
12
- setDefaultHeaders({ 'X-Custom-Header': 'some value' })
13
- await bee.checkConnection()
14
-
15
- if (!scope.isDone()) {
16
- throw new Error('Expected call with the header was not performed!')
17
- }
18
- })
19
- })
20
- })
@@ -1,32 +0,0 @@
1
- /* eslint @typescript-eslint/no-empty-function: 0 */
2
- import { NumberString } from '../../../src/types'
3
- import { isInteger } from '../../../src/utils/type'
4
-
5
- describe('type', () => {
6
- describe('isInteger', () => {
7
- const wrongValues = [
8
- () => {},
9
- Number.MAX_SAFE_INTEGER + 1,
10
- Number.MIN_SAFE_INTEGER - 1,
11
- 5.000000000000001,
12
- false,
13
- true,
14
- Infinity,
15
- NaN,
16
- [1],
17
- ]
18
- const correctValues = [5, 0, '10', 5.0000000000000001, '-1']
19
-
20
- wrongValues.forEach((v: unknown | NumberString) =>
21
- test(`should return false for value ${v}`, () => {
22
- expect(isInteger(v)).toEqual(false)
23
- }),
24
- )
25
-
26
- correctValues.forEach((v: unknown | NumberString) =>
27
- test(`should return true for value ${v}`, () => {
28
- expect(isInteger(v)).toEqual(true)
29
- }),
30
- )
31
- })
32
- })
@@ -1,102 +0,0 @@
1
- import { Bytes, makeBytes } from '../../../src/utils/bytes'
2
- import { HexString, hexToBytes } from '../../../src/utils/hex'
3
- import { readUint64BigEndian, writeUint64BigEndian, writeUint64LittleEndian } from '../../../src/utils/uint64'
4
-
5
- describe('uint64', () => {
6
- describe('little endian', () => {
7
- test('zero', () => {
8
- const uint64 = writeUint64LittleEndian(0)
9
- const zero = makeBytes(8)
10
-
11
- expect(uint64).toEqual(zero)
12
- })
13
-
14
- test('one', () => {
15
- const uint64 = writeUint64LittleEndian(1)
16
- const one = hexToBytes('0100000000000000' as HexString)
17
-
18
- expect(uint64).toEqual(one)
19
- })
20
-
21
- test('deadbeef', () => {
22
- const uint64 = writeUint64LittleEndian(0xdeadbeef)
23
- const deadbeef = hexToBytes('efbeadde00000000' as HexString)
24
-
25
- expect(uint64).toEqual(deadbeef)
26
- })
27
- })
28
-
29
- describe('big endian', () => {
30
- describe('write', () => {
31
- test('zero', () => {
32
- const uint64 = writeUint64BigEndian(0)
33
- const zero = makeBytes(8)
34
-
35
- expect(uint64).toEqual(zero)
36
- })
37
-
38
- test('one', () => {
39
- const uint64 = writeUint64BigEndian(1)
40
- const one = hexToBytes('0000000000000001' as HexString)
41
-
42
- expect(uint64).toEqual(one)
43
- })
44
-
45
- test('deadbeef', () => {
46
- const uint64 = writeUint64BigEndian(0xdeadbeef)
47
- const deadbeef = hexToBytes('00000000deadbeef' as HexString)
48
-
49
- expect(uint64).toEqual(deadbeef)
50
- })
51
- })
52
-
53
- describe('read', () => {
54
- test('zero', () => {
55
- const zero = makeBytes(8)
56
- const value = readUint64BigEndian(zero)
57
-
58
- expect(value).toEqual(0)
59
- })
60
-
61
- test('one', () => {
62
- const one = hexToBytes('0000000000000001' as HexString) as Bytes<8>
63
- const value = readUint64BigEndian(one)
64
-
65
- expect(value).toEqual(1)
66
- })
67
-
68
- test('deadbeef', () => {
69
- const deadbeef = hexToBytes('00000000deadbeef' as HexString) as Bytes<8>
70
- const value = readUint64BigEndian(deadbeef)
71
-
72
- expect(value).toEqual(0xdeadbeef)
73
- })
74
- })
75
-
76
- describe('read and write', () => {
77
- test('zero', () => {
78
- const zero = makeBytes(8)
79
- const num = readUint64BigEndian(zero)
80
- const value = writeUint64BigEndian(num)
81
-
82
- expect(value).toEqual(zero)
83
- })
84
-
85
- test('one', () => {
86
- const one = hexToBytes('0000000000000001' as HexString) as Bytes<8>
87
- const num = readUint64BigEndian(one)
88
- const value = writeUint64BigEndian(num)
89
-
90
- expect(value).toEqual(one)
91
- })
92
-
93
- test('deadbeef', () => {
94
- const deadbeef = hexToBytes('00000000deadbeef' as HexString) as Bytes<8>
95
- const num = readUint64BigEndian(deadbeef)
96
- const value = writeUint64BigEndian(num)
97
-
98
- expect(value).toEqual(deadbeef)
99
- })
100
- })
101
- })
102
- })
package/test/utils.ts DELETED
@@ -1,292 +0,0 @@
1
- import { Readable } from 'stream'
2
- import type { BeeGenericResponse, Reference, Address, BatchId } from '../src/types'
3
- import { bytesToHex, HexString } from '../src/utils/hex'
4
- import { deleteChunkFromLocalStorage } from '../src/modules/debug/chunk'
5
- import { BeeResponseError } from '../src'
6
- import { ChunkAddress } from '../src/chunk/cac'
7
- import { assertBytes } from '../src/utils/bytes'
8
-
9
- declare global {
10
- // eslint-disable-next-line @typescript-eslint/no-namespace
11
- namespace jest {
12
- interface Matchers<R> {
13
- toBeHashReference(): R
14
- toBeBeeResponse(expectedStatusCode: number): R
15
- toBeOneOf(el: unknown[]): R
16
- toBeType(type: string): R
17
- toBeNumberString(): R
18
- }
19
- }
20
- }
21
-
22
- /**
23
- * Load common own Jest Matchers which can be used to check particular return values.
24
- */
25
- export function commonMatchers(): void {
26
- expect.extend({
27
- toBeHashReference(received: string) {
28
- const result = {
29
- pass: false,
30
- message: () => 'Given input is not a Swarm hash reference',
31
- }
32
-
33
- if (typeof received === 'string' && /^[0-9a-fA-F]{64}$/.test(received)) {
34
- result.pass = true
35
- result.message = () => 'Given string is semantically valid Swarm hash reference'
36
- }
37
-
38
- return result
39
- },
40
- toBeBeeResponse(received: BeeGenericResponse, expectedStatusCode: number) {
41
- const result = {
42
- pass: false,
43
- message: () =>
44
- `Bee response does not have status code ${expectedStatusCode}. Got: ${received.code}\nResponse message: ${received.message}`,
45
- }
46
-
47
- if (received.code === expectedStatusCode) {
48
- result.pass = true
49
- result.message = () => 'Bee response meets with its requirements'
50
- }
51
-
52
- return result
53
- },
54
- toBeOneOf(received, argument) {
55
- const validValues = Array.isArray(argument) ? argument : [argument]
56
- let containsValidValue = false
57
-
58
- for (const validValue of validValues) {
59
- try {
60
- expect(received).toEqual(validValue)
61
- containsValidValue = true
62
- // eslint-disable-next-line no-empty
63
- } catch (e) {}
64
- }
65
-
66
- if (containsValidValue) {
67
- return {
68
- message: () => `expected ${JSON.stringify(received)} not to be one of [${validValues.join(', ')}]`,
69
- pass: true,
70
- }
71
- }
72
-
73
- return {
74
- message: () => `expected ${JSON.stringify(received)} to be one of [${validValues.join(', ')}]`,
75
- pass: false,
76
- }
77
- },
78
- toBeType(received, argument) {
79
- const initialType = typeof received
80
- const isArray = Array.isArray(received) ? 'array' : initialType
81
- const type = initialType === 'object' ? isArray : initialType
82
-
83
- return type === argument
84
- ? {
85
- message: () => `expected ${received} to be type ${argument}`,
86
- pass: true,
87
- }
88
- : {
89
- message: () => `expected ${received} to be type ${argument}`,
90
- pass: false,
91
- }
92
- },
93
- toBeNumberString(received) {
94
- const message = () => `expected ${received} to be a number in a string type`
95
-
96
- return /^-?(0|[1-9][0-9]*)$/g.test(received)
97
- ? {
98
- message,
99
- pass: true,
100
- }
101
- : {
102
- message,
103
- pass: false,
104
- }
105
- },
106
- })
107
- }
108
-
109
- /**
110
- * Sleep for N miliseconds
111
- *
112
- * @param ms Number of miliseconds to sleep
113
- */
114
- export async function sleep(ms: number): Promise<void> {
115
- return new Promise<void>(resolve => setTimeout(() => resolve(), ms))
116
- }
117
-
118
- export function createRandomReadable(totalSize: number, chunkSize = 1000): Readable {
119
- if (totalSize % chunkSize !== 0) {
120
- throw new Error(`totalSize ${totalSize} is not dividable without remainder by chunkSize ${chunkSize}`)
121
- }
122
-
123
- const stream = new Readable()
124
- // eslint-disable-next-line @typescript-eslint/no-empty-function
125
- stream._read = (): void => {}
126
-
127
- for (let i = 0; i < totalSize / chunkSize; i++) {
128
- stream.push(randomByteArray(chunkSize))
129
- }
130
-
131
- stream.push(null)
132
-
133
- return stream
134
- }
135
-
136
- /**
137
- * Lehmer random number generator with seed (minstd_rand in C++11)
138
- * !!! Very fast but not well distributed pseudo-random function !!!
139
- *
140
- * @param seed Seed for the pseudo-random generator
141
- */
142
- function lrng(seed: number): () => number {
143
- return (): number => ((2 ** 31 - 1) & (seed = Math.imul(48271, seed))) / 2 ** 31
144
- }
145
-
146
- /**
147
- * Utility function for generating random Buffer
148
- * !!! IT IS NOT CRYPTO SAFE !!!
149
- * For that use `crypto.randomBytes()`
150
- *
151
- * @param length Number of bytes to generate
152
- * @param seed Seed for the pseudo-random generator
153
- */
154
- export function randomByteArray(length: number, seed = 500): Uint8Array {
155
- const rand = lrng(seed)
156
- const buf = new Uint8Array(length)
157
-
158
- for (let i = 0; i < length; ++i) {
159
- buf[i] = (rand() * 0xff) << 0
160
- }
161
-
162
- return buf
163
- }
164
-
165
- /**
166
- * Returns a url for testing the Bee public API
167
- */
168
- export function beeUrl(): string {
169
- return process.env.BEE_API_URL || 'http://localhost:1633'
170
- }
171
-
172
- /**
173
- * Returns a url of another peer for testing the Bee public API
174
- */
175
- export function beePeerUrl(): string {
176
- return process.env.BEE_PEER_API_URL || 'http://localhost:11633'
177
- }
178
-
179
- /**
180
- * Helper function that create monster batch for all the tests.
181
- * There is semaphore mechanism that allows only creation of one batch across all the
182
- * parallel running tests that have to wait until it is created.
183
- */
184
- export function getPostageBatch(url = beeDebugUrl()): BatchId {
185
- let stamp: BatchId
186
-
187
- switch (url) {
188
- case beeDebugUrl():
189
- stamp = process.env.BEE_POSTAGE as BatchId
190
- break
191
- case beePeerDebugUrl():
192
- stamp = process.env.BEE_PEER_POSTAGE as BatchId
193
- break
194
- default:
195
- throw new Error('Unknown URL ' + url)
196
- }
197
-
198
- if (!stamp) {
199
- throw new Error('There is no postage stamp configured for URL ' + url)
200
- }
201
-
202
- return stamp
203
- }
204
-
205
- /**
206
- * Returns a url for testing the Bee Debug API
207
- */
208
- export function beeDebugUrl(): string {
209
- return process.env.BEE_DEBUG_API_URL || 'http://localhost:1635'
210
- }
211
-
212
- /**
213
- * Returns a url for testing the Bee Debug API
214
- */
215
- export function beePeerDebugUrl(): string {
216
- return process.env.BEE_PEER_DEBUG_API_URL || 'http://localhost:11635'
217
- }
218
-
219
- /**
220
- * Try to delete a chunk from local storage, ignoring all errors
221
- *
222
- * @param address Swarm address of chunk
223
- */
224
- export async function tryDeleteChunkFromLocalStorage(address: string | ChunkAddress): Promise<void> {
225
- if (typeof address !== 'string') {
226
- assertBytes(address, 32)
227
- address = bytesToHex(address)
228
- }
229
-
230
- try {
231
- await deleteChunkFromLocalStorage(beeDebugUrl(), address)
232
- } catch (e) {
233
- // ignore not found errors
234
- if (e instanceof BeeResponseError && e.status === 404) {
235
- return
236
- }
237
- throw e
238
- }
239
- }
240
-
241
- /**
242
- * Formatting utility for displaying long strings like hexstrings.
243
- *
244
- * @param inputStr
245
- * @param len
246
- */
247
- export function shorten(inputStr: unknown, len = 17): string {
248
- const str = typeof inputStr === 'string' ? inputStr : (inputStr as string).toString()
249
-
250
- if (str.length <= len) {
251
- return str
252
- }
253
-
254
- return `${str.slice(0, 6)}...${str.slice(-6)} (length: ${str.length})`
255
- }
256
-
257
- export const invalidReference = '0000000000000000000000000000000000000000000000000000000000000000' as Reference
258
-
259
- export const okResponse: BeeGenericResponse = {
260
- code: 200,
261
- message: 'OK',
262
- }
263
-
264
- export const createdResponse: BeeGenericResponse = {
265
- code: 201,
266
- message: 'Created',
267
- }
268
-
269
- export const ERR_TIMEOUT = 40000
270
- export const BIG_FILE_TIMEOUT = 100000
271
- export const PSS_TIMEOUT = 120000
272
- export const FEED_TIMEOUT = 120000
273
- export const POSTAGE_BATCH_TIMEOUT = 40000
274
-
275
- export const testChunkPayload = new Uint8Array([1, 2, 3])
276
- // span is the payload length encoded as uint64 little endian
277
- export const testChunkSpan = new Uint8Array([testChunkPayload.length, 0, 0, 0, 0, 0, 0, 0])
278
- export const testChunkData = new Uint8Array([...testChunkSpan, ...testChunkPayload])
279
- // the hash is hardcoded because we would need the bmt hasher otherwise
280
- export const testChunkHash = 'ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4338' as Reference
281
- export const testAddress = 'ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4338' as Address
282
- export const testBatchId = 'ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4338' as BatchId
283
-
284
- export const testJsonPayload = [{ some: 'object' }]
285
- export const testJsonStringPayload = JSON.stringify(testJsonPayload)
286
- export const testJsonHash = '872a858115b8bee4408b1427b49e472883fdc2512d5a8f2d428b97ecc8f7ccfa'
287
-
288
- export const testIdentity = {
289
- privateKey: '634fb5a872396d9693e5c9f9d7233cfa93f395c093371017ff44aa9ae6564cdd' as HexString,
290
- publicKey: '03c32bb011339667a487b6c1c35061f15f7edc36aa9a0f8648aba07a4b8bd741b4' as HexString,
291
- address: '8d3766440f0d7b949a5e32995d09619a7f86e632' as HexString,
292
- }
package/tsconfig.json DELETED
@@ -1,18 +0,0 @@
1
- {
2
- "include": ["src"],
3
- "compilerOptions": {
4
- "alwaysStrict": true,
5
- "target": "ES6",
6
- "esModuleInterop": true,
7
- "allowSyntheticDefaultImports": true,
8
- "moduleResolution": "node",
9
- "module": "commonjs",
10
- "strict": true,
11
- "resolveJsonModule": true,
12
- "experimentalDecorators": true,
13
- "emitDecoratorMetadata": true,
14
- "typeRoots": ["./src/types", "node_modules/@types"],
15
- "rootDirs": ["src"],
16
- "outDir": "dist"
17
- },
18
- }
@@ -1,11 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "include": [
4
- "src",
5
- "test",
6
- "jest.config.ts"
7
- ],
8
- "compilerOptions": {
9
- "noEmit": true
10
- }
11
- }
package/typedoc.json DELETED
@@ -1,7 +0,0 @@
1
- {
2
- "entryPoints": ["./src/index.ts"],
3
- "readme": "none",
4
- "logLevel": "Verbose",
5
- "excludePrivate": true,
6
- "out": "docs"
7
- }