@ethersphere/bee-js 2.0.0 → 3.1.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 (159) hide show
  1. package/CHANGELOG.md +91 -0
  2. package/README.md +2 -2
  3. package/dist/index.browser.min.js +1 -1
  4. package/dist/index.browser.min.js.map +1 -1
  5. package/dist/index.min.js +1 -1
  6. package/dist/index.min.js.map +1 -1
  7. package/dist/src/bee-debug.d.ts +38 -6
  8. package/dist/src/bee.d.ts +34 -46
  9. package/dist/src/modules/chunk.d.ts +2 -10
  10. package/dist/src/modules/debug/stamps.d.ts +2 -0
  11. package/dist/src/modules/debug/status.d.ts +10 -4
  12. package/dist/src/modules/stewardship.d.ts +1 -0
  13. package/dist/src/types/debug.d.ts +11 -0
  14. package/dist/src/types/index.d.ts +6 -7
  15. package/dist/src/utils/bytes.d.ts +1 -1
  16. package/dist/src/utils/expose.d.ts +1 -0
  17. package/dist/src/utils/stamps.d.ts +12 -0
  18. package/package.json +10 -8
  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/CODEOWNERS +0 -1
  35. package/CODE_OF_CONDUCT.md +0 -76
  36. package/commitlint.config.js +0 -3
  37. package/dist/src/modules/stamps.d.ts +0 -4
  38. package/jest-puppeteer.config.js +0 -5
  39. package/jest.config.ts +0 -85
  40. package/src/bee-debug.ts +0 -559
  41. package/src/bee.ts +0 -1070
  42. package/src/chunk/bmt.ts +0 -55
  43. package/src/chunk/cac.ts +0 -79
  44. package/src/chunk/serialize.ts +0 -16
  45. package/src/chunk/signer.ts +0 -141
  46. package/src/chunk/soc.ts +0 -190
  47. package/src/chunk/span.ts +0 -33
  48. package/src/feed/index.ts +0 -217
  49. package/src/feed/json.ts +0 -33
  50. package/src/feed/topic.ts +0 -23
  51. package/src/feed/type.ts +0 -12
  52. package/src/index.ts +0 -24
  53. package/src/modules/bytes.ts +0 -70
  54. package/src/modules/bzz.ts +0 -179
  55. package/src/modules/chunk.ts +0 -69
  56. package/src/modules/debug/balance.ts +0 -63
  57. package/src/modules/debug/chequebook.ts +0 -175
  58. package/src/modules/debug/chunk.ts +0 -39
  59. package/src/modules/debug/connectivity.ts +0 -61
  60. package/src/modules/debug/settlements.ts +0 -33
  61. package/src/modules/debug/stamps.ts +0 -76
  62. package/src/modules/debug/states.ts +0 -35
  63. package/src/modules/debug/status.ts +0 -35
  64. package/src/modules/debug/tag.ts +0 -19
  65. package/src/modules/debug/transactions.ts +0 -79
  66. package/src/modules/feed.ts +0 -105
  67. package/src/modules/pinning.ts +0 -74
  68. package/src/modules/pss.ts +0 -49
  69. package/src/modules/soc.ts +0 -40
  70. package/src/modules/stamps.ts +0 -59
  71. package/src/modules/status.ts +0 -13
  72. package/src/modules/stewardship.ts +0 -18
  73. package/src/modules/tag.ts +0 -83
  74. package/src/types/debug.ts +0 -204
  75. package/src/types/index.ts +0 -566
  76. package/src/types/ky-universal/common.d.ts +0 -17
  77. package/src/types/ky-universal/hooks.d.ts +0 -103
  78. package/src/types/ky-universal/index.d.ts +0 -125
  79. package/src/types/ky-universal/options.d.ts +0 -235
  80. package/src/types/ky-universal/response.d.ts +0 -38
  81. package/src/types/ky-universal/retry.d.ts +0 -39
  82. package/src/types/tar-js.d.ts +0 -1
  83. package/src/types/webpack-bundle-analyzer/index.d.ts +0 -13
  84. package/src/utils/bytes.ts +0 -144
  85. package/src/utils/collection.ts +0 -151
  86. package/src/utils/data.browser.ts +0 -68
  87. package/src/utils/data.ts +0 -48
  88. package/src/utils/error.ts +0 -23
  89. package/src/utils/eth.ts +0 -229
  90. package/src/utils/expose.ts +0 -48
  91. package/src/utils/file.ts +0 -40
  92. package/src/utils/hash.ts +0 -16
  93. package/src/utils/headers.ts +0 -63
  94. package/src/utils/hex.ts +0 -186
  95. package/src/utils/http.ts +0 -141
  96. package/src/utils/merge.ts +0 -34
  97. package/src/utils/pss.ts +0 -16
  98. package/src/utils/stream.ts +0 -146
  99. package/src/utils/tar.ts +0 -30
  100. package/src/utils/type.ts +0 -394
  101. package/src/utils/uint64.ts +0 -31
  102. package/src/utils/url.ts +0 -52
  103. package/test/bee.sh +0 -187
  104. package/test/data/1.txt +0 -1
  105. package/test/data/2.txt +0 -0
  106. package/test/data/empty +0 -0
  107. package/test/data/sub/3.txt +0 -1
  108. package/test/data/sub//303/251 +0 -0
  109. package/test/data/sub//360/237/230/216 +0 -0
  110. package/test/integration/bee-class.browser.spec.ts +0 -272
  111. package/test/integration/bee-class.spec.ts +0 -631
  112. package/test/integration/bee-debug-class.spec.ts +0 -85
  113. package/test/integration/chunk/bmt.spec.ts +0 -33
  114. package/test/integration/chunk/cac.spec.ts +0 -27
  115. package/test/integration/chunk/soc.spec.ts +0 -38
  116. package/test/integration/feed/index.spec.ts +0 -84
  117. package/test/integration/modules/bytes.spec.ts +0 -23
  118. package/test/integration/modules/bzz.spec.ts +0 -265
  119. package/test/integration/modules/chunk.spec.ts +0 -29
  120. package/test/integration/modules/debug/balance.spec.ts +0 -70
  121. package/test/integration/modules/debug/chequebook.spec.ts +0 -64
  122. package/test/integration/modules/debug/connectivity.spec.ts +0 -69
  123. package/test/integration/modules/debug/settlements.spec.ts +0 -34
  124. package/test/integration/modules/debug/states.spec.ts +0 -34
  125. package/test/integration/modules/debug/status.spec.ts +0 -65
  126. package/test/integration/modules/debug/tag.spec.ts +0 -27
  127. package/test/integration/modules/debug/transactions.spec.ts +0 -10
  128. package/test/integration/modules/feed.spec.ts +0 -61
  129. package/test/integration/modules/pinning.spec.ts +0 -155
  130. package/test/integration/modules/pss.spec.ts +0 -97
  131. package/test/integration/modules/status.spec.ts +0 -10
  132. package/test/integration/modules/stewardship.spec.ts +0 -32
  133. package/test/integration/modules/tag.spec.ts +0 -51
  134. package/test/test-type-sequencer.js +0 -36
  135. package/test/testpage/testpage.html +0 -20
  136. package/test/tests-setup.ts +0 -94
  137. package/test/unit/assertions.ts +0 -460
  138. package/test/unit/bee-class.spec.ts +0 -976
  139. package/test/unit/bee-debug-class.spec.ts +0 -435
  140. package/test/unit/chunk/bmt.spec.ts +0 -16
  141. package/test/unit/chunk/cac.spec.ts +0 -29
  142. package/test/unit/chunk/serialize.spec.ts +0 -24
  143. package/test/unit/chunk/signer.spec.ts +0 -140
  144. package/test/unit/chunk/soc.spec.ts +0 -25
  145. package/test/unit/chunk/span.spec.ts +0 -38
  146. package/test/unit/feed/json.spec.ts +0 -73
  147. package/test/unit/nock.ts +0 -151
  148. package/test/unit/utils/bytes.spec.ts +0 -32
  149. package/test/unit/utils/collection.spec.ts +0 -22
  150. package/test/unit/utils/eth.spec.ts +0 -224
  151. package/test/unit/utils/hex.spec.ts +0 -115
  152. package/test/unit/utils/stream.spec.ts +0 -30
  153. package/test/unit/utils/type.spec.ts +0 -32
  154. package/test/unit/utils/uint64.spec.ts +0 -102
  155. package/test/utils.ts +0 -328
  156. package/tsconfig.json +0 -18
  157. package/tsconfig.test.json +0 -11
  158. package/typedoc.json +0 -7
  159. package/webpack.config.ts +0 -141
package/src/feed/index.ts DELETED
@@ -1,217 +0,0 @@
1
- import { keccak256Hash } from '../utils/hash'
2
- import { serializeBytes } from '../chunk/serialize'
3
- import { Identifier, uploadSingleOwnerChunkData, makeSingleOwnerChunkFromData } from '../chunk/soc'
4
- import { FeedUpdateOptions, fetchFeedUpdate } from '../modules/feed'
5
- import {
6
- REFERENCE_HEX_LENGTH,
7
- Reference,
8
- UploadOptions,
9
- ENCRYPTED_REFERENCE_HEX_LENGTH,
10
- ENCRYPTED_REFERENCE_BYTES_LENGTH,
11
- REFERENCE_BYTES_LENGTH,
12
- Signer,
13
- FeedReader,
14
- FeedWriter,
15
- Topic,
16
- Address,
17
- BatchId,
18
- Ky,
19
- } from '../types'
20
- import { Bytes, makeBytes, bytesAtOffset } from '../utils/bytes'
21
- import { BeeResponseError } from '../utils/error'
22
- import { bytesToHex, HexString, hexToBytes, makeHexString } from '../utils/hex'
23
- import { readUint64BigEndian, writeUint64BigEndian } from '../utils/uint64'
24
- import * as chunkAPI from '../modules/chunk'
25
- import { EthAddress, HexEthAddress, makeHexEthAddress } from '../utils/eth'
26
-
27
- import type { FeedType } from './type'
28
- import { assertAddress } from '../utils/type'
29
-
30
- const TIMESTAMP_PAYLOAD_OFFSET = 0
31
- const TIMESTAMP_PAYLOAD_SIZE = 8
32
- const REFERENCE_PAYLOAD_OFFSET = TIMESTAMP_PAYLOAD_SIZE
33
- const REFERENCE_PAYLOAD_MIN_SIZE = 32
34
- const REFERENCE_PAYLOAD_MAX_SIZE = 64
35
- const INDEX_HEX_LENGTH = 16
36
-
37
- export interface Epoch {
38
- time: number
39
- level: number
40
- }
41
- export type IndexBytes = Bytes<8>
42
- export type Index = number | Epoch | IndexBytes | string
43
-
44
- export interface FeedUploadOptions extends UploadOptions, FeedUpdateOptions {}
45
-
46
- type PlainChunkReference = Bytes<32>
47
- type EncryptedChunkReference = Bytes<64>
48
- export type ChunkReference = PlainChunkReference | EncryptedChunkReference
49
-
50
- export interface FeedUpdate {
51
- timestamp: number
52
- reference: ChunkReference
53
- }
54
-
55
- export function isEpoch(epoch: unknown): epoch is Epoch {
56
- return typeof epoch === 'object' && epoch !== null && 'time' in epoch && 'level' in epoch
57
- }
58
-
59
- function hashFeedIdentifier(topic: Topic, index: IndexBytes): Identifier {
60
- return keccak256Hash(hexToBytes(topic), index)
61
- }
62
-
63
- export function makeSequentialFeedIdentifier(topic: Topic, index: number): Identifier {
64
- const indexBytes = writeUint64BigEndian(index)
65
-
66
- return hashFeedIdentifier(topic, indexBytes)
67
- }
68
-
69
- export function makeFeedIndexBytes(s: string): IndexBytes {
70
- const hex = makeHexString(s, INDEX_HEX_LENGTH)
71
-
72
- return hexToBytes(hex)
73
- }
74
-
75
- export function makeFeedIdentifier(topic: Topic, index: Index): Identifier {
76
- if (typeof index === 'number') {
77
- return makeSequentialFeedIdentifier(topic, index)
78
- } else if (typeof index === 'string') {
79
- const indexBytes = makeFeedIndexBytes(index)
80
-
81
- return hashFeedIdentifier(topic, indexBytes)
82
- } else if (isEpoch(index)) {
83
- throw new TypeError('epoch is not yet implemented')
84
- }
85
-
86
- return hashFeedIdentifier(topic, index)
87
- }
88
-
89
- export async function uploadFeedUpdate(
90
- ky: Ky,
91
- signer: Signer,
92
- topic: Topic,
93
- index: Index,
94
- reference: ChunkReference,
95
- postageBatchId: BatchId,
96
- options?: FeedUploadOptions,
97
- ): Promise<Reference> {
98
- const identifier = makeFeedIdentifier(topic, index)
99
- const at = options?.at ?? Date.now() / 1000.0
100
- const timestamp = writeUint64BigEndian(at)
101
- const payloadBytes = serializeBytes(timestamp, reference)
102
-
103
- return uploadSingleOwnerChunkData(ky, signer, postageBatchId, identifier, payloadBytes, options)
104
- }
105
-
106
- export async function findNextIndex(
107
- ky: Ky,
108
- owner: HexEthAddress,
109
- topic: Topic,
110
- options?: FeedUpdateOptions,
111
- ): Promise<HexString<typeof INDEX_HEX_LENGTH>> {
112
- try {
113
- const feedUpdate = await fetchFeedUpdate(ky, owner, topic, options)
114
-
115
- return makeHexString(feedUpdate.feedIndexNext, INDEX_HEX_LENGTH)
116
- } catch (e) {
117
- if (e instanceof BeeResponseError && e.status === 404) {
118
- return bytesToHex(makeBytes(8))
119
- }
120
- throw e
121
- }
122
- }
123
-
124
- export async function updateFeed(
125
- ky: Ky,
126
- signer: Signer,
127
- topic: Topic,
128
- reference: ChunkReference,
129
- postageBatchId: BatchId,
130
- options?: FeedUploadOptions,
131
- ): Promise<Reference> {
132
- const ownerHex = makeHexEthAddress(signer.address)
133
- const nextIndex = await findNextIndex(ky, ownerHex, topic, options)
134
-
135
- return uploadFeedUpdate(ky, signer, topic, nextIndex, reference, postageBatchId, options)
136
- }
137
-
138
- function verifyChunkReferenceAtOffset(offset: number, data: Uint8Array): ChunkReference {
139
- try {
140
- return bytesAtOffset(data, offset, REFERENCE_PAYLOAD_MAX_SIZE)
141
- } catch (e) {
142
- return bytesAtOffset(data, offset, REFERENCE_PAYLOAD_MIN_SIZE)
143
- }
144
- }
145
-
146
- export function verifyChunkReference(data: Uint8Array): ChunkReference {
147
- return verifyChunkReferenceAtOffset(0, data)
148
- }
149
-
150
- export async function downloadFeedUpdate(ky: Ky, owner: EthAddress, topic: Topic, index: Index): Promise<FeedUpdate> {
151
- const identifier = makeFeedIdentifier(topic, index)
152
- const address = keccak256Hash(identifier, owner)
153
- const addressHex = bytesToHex(address)
154
- const data = await chunkAPI.download(ky, addressHex)
155
- const soc = makeSingleOwnerChunkFromData(data, address)
156
- const payload = soc.payload()
157
- const timestampBytes = bytesAtOffset(payload, TIMESTAMP_PAYLOAD_OFFSET, TIMESTAMP_PAYLOAD_SIZE)
158
- const timestamp = readUint64BigEndian(timestampBytes)
159
- const reference = verifyChunkReferenceAtOffset(REFERENCE_PAYLOAD_OFFSET, payload)
160
-
161
- return {
162
- timestamp,
163
- reference,
164
- }
165
- }
166
-
167
- export function makeFeedReader(ky: Ky, type: FeedType, topic: Topic, owner: HexEthAddress): FeedReader {
168
- const download = async (options?: FeedUpdateOptions) => fetchFeedUpdate(ky, owner, topic, { ...options, type })
169
-
170
- return {
171
- type,
172
- owner,
173
- topic,
174
- download,
175
- }
176
- }
177
-
178
- function makeChunkReference(reference: ChunkReference | Reference): ChunkReference {
179
- if (typeof reference === 'string') {
180
- try {
181
- // Non-encrypted chunk hex string reference
182
- const hexReference = makeHexString(reference, REFERENCE_HEX_LENGTH)
183
-
184
- return hexToBytes<typeof REFERENCE_BYTES_LENGTH>(hexReference)
185
- } catch (e) {
186
- if (!(e instanceof TypeError)) {
187
- throw e
188
- }
189
-
190
- // Encrypted chunk hex string reference
191
- const hexReference = makeHexString(reference, ENCRYPTED_REFERENCE_HEX_LENGTH)
192
-
193
- return hexToBytes<typeof ENCRYPTED_REFERENCE_BYTES_LENGTH>(hexReference)
194
- }
195
- } else if (reference instanceof Uint8Array) {
196
- return verifyChunkReference(reference)
197
- }
198
- throw new TypeError('invalid chunk reference')
199
- }
200
-
201
- export function makeFeedWriter(ky: Ky, type: FeedType, topic: Topic, signer: Signer): FeedWriter {
202
- const upload = async (
203
- postageBatchId: string | Address,
204
- reference: ChunkReference | Reference,
205
- options?: FeedUploadOptions,
206
- ) => {
207
- assertAddress(postageBatchId)
208
- const canonicalReference = makeChunkReference(reference)
209
-
210
- return updateFeed(ky, signer, topic, canonicalReference, postageBatchId, { ...options, type })
211
- }
212
-
213
- return {
214
- ...makeFeedReader(ky, type, topic, makeHexEthAddress(signer.address)),
215
- upload,
216
- }
217
- }
package/src/feed/json.ts DELETED
@@ -1,33 +0,0 @@
1
- import { FeedWriter, FeedReader, AnyJson, BatchId, Reference, RequestOptions } from '../types'
2
- import { Bee } from '../bee'
3
-
4
- function serializeJson(data: AnyJson): Uint8Array {
5
- try {
6
- const jsonString = JSON.stringify(data)
7
-
8
- return new TextEncoder().encode(jsonString)
9
- } catch (e) {
10
- e.message = `JsonFeed: ${e.message}`
11
- throw e
12
- }
13
- }
14
-
15
- export async function getJsonData<T extends AnyJson>(bee: Bee, reader: FeedReader): Promise<T> {
16
- const feedUpdate = await reader.download()
17
- const retrievedData = await bee.downloadData(feedUpdate.reference)
18
-
19
- return retrievedData.json() as T
20
- }
21
-
22
- export async function setJsonData(
23
- bee: Bee,
24
- writer: FeedWriter,
25
- postageBatchId: BatchId,
26
- data: AnyJson,
27
- options?: RequestOptions,
28
- ): Promise<Reference> {
29
- const serializedData = serializeJson(data)
30
- const { reference } = await bee.uploadData(postageBatchId, serializedData, options)
31
-
32
- return writer.upload(postageBatchId, reference)
33
- }
package/src/feed/topic.ts DELETED
@@ -1,23 +0,0 @@
1
- import { keccak256Hash } from '../utils/hash'
2
- import { assertBytes } from '../utils/bytes'
3
- import { makeHexString, bytesToHex } from '../utils/hex'
4
- import { Topic, TOPIC_BYTES_LENGTH, TOPIC_HEX_LENGTH } from '../types'
5
-
6
- export function makeTopic(topic: Uint8Array | string): Topic {
7
- if (typeof topic === 'string') {
8
- return makeHexString(topic, TOPIC_HEX_LENGTH)
9
- } else if (topic instanceof Uint8Array) {
10
- assertBytes(topic, TOPIC_BYTES_LENGTH)
11
-
12
- return bytesToHex(topic, TOPIC_HEX_LENGTH)
13
- }
14
- throw new TypeError('invalid topic')
15
- }
16
-
17
- export function makeTopicFromString(s: string): Topic {
18
- if (typeof s !== 'string') {
19
- throw new TypeError('topic has to be string!')
20
- }
21
-
22
- return bytesToHex(keccak256Hash(s), TOPIC_HEX_LENGTH)
23
- }
package/src/feed/type.ts DELETED
@@ -1,12 +0,0 @@
1
- const feedTypes = ['sequence', 'epoch'] as const
2
- export type FeedType = typeof feedTypes[number]
3
- export const DEFAULT_FEED_TYPE: FeedType = 'sequence'
4
-
5
- export function isFeedType(type: unknown): type is FeedType {
6
- return typeof type === 'string' && feedTypes.includes(type as FeedType)
7
- }
8
- export function assertFeedType(type: unknown): asserts type is FeedType {
9
- if (!isFeedType(type)) {
10
- throw new TypeError('invalid feed type')
11
- }
12
- }
package/src/index.ts DELETED
@@ -1,24 +0,0 @@
1
- import { Bee } from './bee'
2
- import { BeeDebug } from './bee-debug'
3
-
4
- export * as Utils from './utils/expose'
5
- export * from './types'
6
- export * from './utils/error'
7
- export { SUPPORTED_BEE_VERSION, SUPPORTED_BEE_VERSION_EXACT } from './modules/debug/status'
8
- export { Bee, BeeDebug }
9
-
10
- // for requrie-like imports
11
- declare global {
12
- interface Window {
13
- // binded as 'BeeJs' via Webpack
14
- BeeJs: {
15
- Bee: typeof import('./bee').Bee
16
- BeeDebug: typeof import('./bee-debug').BeeDebug
17
- Utils: typeof import('./utils/expose')
18
- BeeError: typeof import('./utils/error').BeeError
19
- BeeRequestError: typeof import('./utils/error').BeeRequestError
20
- BeeResponseError: typeof import('./utils/error').BeeResponseError
21
- BeeArgumentError: typeof import('./utils/error').BeeArgumentError
22
- }
23
- }
24
- }
@@ -1,70 +0,0 @@
1
- import type { BatchId, Data, Ky, Reference, UploadOptions } from '../types'
2
- import { prepareData } from '../utils/data'
3
- import { extractUploadHeaders } from '../utils/headers'
4
- import { http } from '../utils/http'
5
- import { wrapBytesWithHelpers } from '../utils/bytes'
6
- import { UploadResult } from '../types'
7
- import { makeTagUid } from '../utils/type'
8
-
9
- const endpoint = 'bytes'
10
-
11
- /**
12
- * Upload data to a Bee node
13
- *
14
- * @param ky Ky instance
15
- * @param data Data to be uploaded
16
- * @param postageBatchId Postage BatchId that will be assigned to uploaded data
17
- * @param options Additional options like tag, encryption, pinning
18
- */
19
- export async function upload(
20
- ky: Ky,
21
- data: string | Uint8Array,
22
- postageBatchId: BatchId,
23
- options?: UploadOptions,
24
- ): Promise<UploadResult> {
25
- const response = await http<{ reference: Reference }>(ky, {
26
- path: endpoint,
27
- method: 'post',
28
- responseType: 'json',
29
- body: await prepareData(data),
30
- headers: {
31
- 'content-type': 'application/octet-stream',
32
- ...extractUploadHeaders(postageBatchId, options),
33
- },
34
- })
35
-
36
- return {
37
- reference: response.data.reference,
38
- tagUid: makeTagUid(response.headers.get('swarm-tag')),
39
- }
40
- }
41
-
42
- /**
43
- * Download data as a byte array
44
- *
45
- * @param ky
46
- * @param hash Bee content reference
47
- */
48
- export async function download(ky: Ky, hash: Reference): Promise<Data> {
49
- const response = await http<ArrayBuffer>(ky, {
50
- responseType: 'arraybuffer',
51
- path: `${endpoint}/${hash}`,
52
- })
53
-
54
- return wrapBytesWithHelpers(new Uint8Array(response.data))
55
- }
56
-
57
- /**
58
- * Download data as a readable stream
59
- *
60
- * @param ky
61
- * @param hash Bee content reference
62
- */
63
- export async function downloadReadable(ky: Ky, hash: Reference): Promise<ReadableStream<Uint8Array>> {
64
- const response = await http<ReadableStream<Uint8Array>>(ky, {
65
- responseType: 'stream',
66
- path: `${endpoint}/${hash}`,
67
- })
68
-
69
- return response.data
70
- }
@@ -1,179 +0,0 @@
1
- import {
2
- BatchId,
3
- Collection,
4
- CollectionUploadOptions,
5
- Data,
6
- FileData,
7
- FileUploadOptions,
8
- Ky,
9
- Readable,
10
- Reference,
11
- UploadHeaders,
12
- UploadResult,
13
- } from '../types'
14
- import { extractUploadHeaders, readFileHeaders } from '../utils/headers'
15
- import { http } from '../utils/http'
16
- import { prepareData } from '../utils/data'
17
- import { makeTar } from '../utils/tar'
18
- import { assertCollection } from '../utils/collection'
19
- import { wrapBytesWithHelpers } from '../utils/bytes'
20
- import { isReadable } from '../utils/stream'
21
- import { makeTagUid } from '../utils/type'
22
-
23
- const bzzEndpoint = 'bzz'
24
-
25
- interface FileUploadHeaders extends UploadHeaders {
26
- 'content-length'?: string
27
- 'content-type'?: string
28
- }
29
-
30
- function extractFileUploadHeaders(postageBatchId: BatchId, options?: FileUploadOptions): FileUploadHeaders {
31
- const headers: FileUploadHeaders = extractUploadHeaders(postageBatchId, options)
32
-
33
- if (options?.size) headers['content-length'] = String(options.size)
34
-
35
- if (options?.contentType) headers['content-type'] = options.contentType
36
-
37
- return headers
38
- }
39
-
40
- /**
41
- * Upload single file
42
- *
43
- * @param ky
44
- * @param data Files data
45
- * @param postageBatchId Postage BatchId that will be assigned to uploaded data
46
- * @param name Name that will be attached to the uploaded file. Wraps the data into manifest with set index document.
47
- * @param options
48
- */
49
- export async function uploadFile(
50
- ky: Ky,
51
- data: string | Uint8Array | Readable | ArrayBuffer,
52
- postageBatchId: BatchId,
53
- name?: string,
54
- options?: FileUploadOptions,
55
- ): Promise<UploadResult> {
56
- if (isReadable(data) && !options?.contentType) {
57
- if (!options) options = {}
58
-
59
- options.contentType = 'application/octet-stream'
60
- }
61
-
62
- const response = await http<{ reference: Reference }>(ky, {
63
- method: 'post',
64
- path: bzzEndpoint,
65
- body: await prepareData(data),
66
- headers: {
67
- ...extractFileUploadHeaders(postageBatchId, options),
68
- },
69
- searchParams: { name },
70
- responseType: 'json',
71
- })
72
-
73
- return {
74
- reference: response.data.reference,
75
- tagUid: makeTagUid(response.headers.get('swarm-tag')),
76
- }
77
- }
78
-
79
- /**
80
- * Download single file as a buffer
81
- *
82
- * @param ky Ky instance for given Bee class instance
83
- * @param hash Bee file or collection hash
84
- * @param path If hash is collection then this defines path to a single file in the collection
85
- */
86
- export async function downloadFile(ky: Ky, hash: string, path = ''): Promise<FileData<Data>> {
87
- const response = await http<ArrayBuffer>(ky, {
88
- method: 'GET',
89
- responseType: 'arraybuffer',
90
- path: `${bzzEndpoint}/${hash}/${path}`,
91
- })
92
- const file = {
93
- ...readFileHeaders(response.headers),
94
- data: wrapBytesWithHelpers(new Uint8Array(response.data)),
95
- }
96
-
97
- return file
98
- }
99
-
100
- /**
101
- * Download single file as a readable stream
102
- *
103
- * @param ky Ky instance for given Bee class instance
104
- * @param hash Bee file or collection hash
105
- * @param path If hash is collection then this defines path to a single file in the collection
106
- */
107
- export async function downloadFileReadable(
108
- ky: Ky,
109
- hash: string,
110
- path = '',
111
- ): Promise<FileData<ReadableStream<Uint8Array>>> {
112
- const response = await http<ReadableStream<Uint8Array>>(ky, {
113
- method: 'GET',
114
- responseType: 'stream',
115
- path: `${bzzEndpoint}/${hash}/${path}`,
116
- })
117
- const file = {
118
- ...readFileHeaders(response.headers),
119
- data: response.data,
120
- }
121
-
122
- return file
123
- }
124
-
125
- /*******************************************************************************************************************/
126
-
127
- // Collections
128
-
129
- interface CollectionUploadHeaders extends UploadHeaders {
130
- 'swarm-index-document'?: string
131
- 'swarm-error-document'?: string
132
- }
133
-
134
- function extractCollectionUploadHeaders(
135
- postageBatchId: BatchId,
136
- options?: CollectionUploadOptions,
137
- ): CollectionUploadHeaders {
138
- const headers: CollectionUploadHeaders = extractUploadHeaders(postageBatchId, options)
139
-
140
- if (options?.indexDocument) headers['swarm-index-document'] = options.indexDocument
141
-
142
- if (options?.errorDocument) headers['swarm-error-document'] = options.errorDocument
143
-
144
- return headers
145
- }
146
-
147
- /**
148
- * Upload collection
149
- * @param ky Ky instance for given Bee class instance
150
- * @param collection Collection of Uint8Array buffers to upload
151
- * @param postageBatchId Postage BatchId that will be assigned to uploaded data
152
- * @param options
153
- */
154
- export async function uploadCollection(
155
- ky: Ky,
156
- collection: Collection<Uint8Array>,
157
- postageBatchId: BatchId,
158
- options?: CollectionUploadOptions,
159
- ): Promise<UploadResult> {
160
- assertCollection(collection)
161
- const tarData = makeTar(collection)
162
-
163
- const response = await http<{ reference: Reference }>(ky, {
164
- method: 'post',
165
- path: bzzEndpoint,
166
- body: tarData,
167
- responseType: 'json',
168
- headers: {
169
- 'content-type': 'application/x-tar',
170
- 'swarm-collection': 'true',
171
- ...extractCollectionUploadHeaders(postageBatchId, options),
172
- },
173
- })
174
-
175
- return {
176
- reference: response.data.reference,
177
- tagUid: response.headers.get('swarm-tag') ? makeTagUid(response.headers.get('swarm-tag')) : undefined,
178
- }
179
- }
@@ -1,69 +0,0 @@
1
- import type { BatchId, Ky, ReferenceResponse, UploadOptions } from '../types'
2
- import { extractUploadHeaders } from '../utils/headers'
3
- import { http } from '../utils/http'
4
- import { Reference } from '../types'
5
-
6
- const endpoint = 'chunks'
7
-
8
- /**
9
- * Upload chunk to a Bee node
10
- *
11
- * The chunk data consists of 8 byte span and up to 4096 bytes of payload data.
12
- * The span stores the length of the payload in uint64 little endian encoding.
13
- * Upload expects the chuck data to be set accordingly.
14
- *
15
- * @param ky Ky instance
16
- * @param data Chunk data to be uploaded
17
- * @param postageBatchId Postage BatchId that will be assigned to uploaded data
18
- * @param options Additional options like tag, encryption, pinning
19
- */
20
- export async function upload(
21
- ky: Ky,
22
- data: Uint8Array,
23
- postageBatchId: BatchId,
24
- options?: UploadOptions,
25
- ): Promise<Reference> {
26
- const response = await http<ReferenceResponse>(ky, {
27
- method: 'post',
28
- path: `${endpoint}`,
29
- body: data,
30
- headers: {
31
- 'content-type': 'application/octet-stream',
32
- ...extractUploadHeaders(postageBatchId, options),
33
- },
34
- responseType: 'json',
35
- })
36
-
37
- return response.data.reference
38
- }
39
-
40
- /**
41
- * Download chunk data as a byte array
42
- *
43
- * @param ky Ky instance for given Bee class instance
44
- * @param hash Bee content reference
45
- *
46
- */
47
- export async function download(ky: Ky, hash: string): Promise<Uint8Array> {
48
- const response = await http<ArrayBuffer>(ky, {
49
- responseType: 'arraybuffer',
50
- path: `${endpoint}/${hash}`,
51
- })
52
-
53
- return new Uint8Array(response.data)
54
- }
55
-
56
- /**
57
- * Download chunk data as a readable stream
58
- *
59
- * @param ky Ky instance for given Bee class instance
60
- * @param hash Bee content reference
61
- */
62
- export async function downloadReadable(ky: Ky, hash: string): Promise<ReadableStream<Uint8Array>> {
63
- const response = await http<ReadableStream<Uint8Array>>(ky, {
64
- responseType: 'stream',
65
- path: `${endpoint}/${hash}`,
66
- })
67
-
68
- return response.data
69
- }
@@ -1,63 +0,0 @@
1
- import { http } from '../../utils/http'
2
- import type { PeerBalance, BalanceResponse, Ky } from '../../types'
3
-
4
- const balancesEndpoint = 'balances'
5
- const consumedEndpoint = 'consumed'
6
-
7
- /**
8
- * Get the balances with all known peers including prepaid services
9
- *
10
- * @param ky Ky debug instance
11
- */
12
- export async function getAllBalances(ky: Ky): Promise<BalanceResponse> {
13
- const response = await http<BalanceResponse>(ky, {
14
- path: balancesEndpoint,
15
- responseType: 'json',
16
- })
17
-
18
- return response.data
19
- }
20
-
21
- /**
22
- * Get the balances with a specific peer including prepaid services
23
- *
24
- * @param ky Ky debug instance
25
- * @param address Swarm address of peer
26
- */
27
- export async function getPeerBalance(ky: Ky, address: string): Promise<PeerBalance> {
28
- const response = await http<PeerBalance>(ky, {
29
- path: `${balancesEndpoint}/${address}`,
30
- responseType: 'json',
31
- })
32
-
33
- return response.data
34
- }
35
-
36
- /**
37
- * Get the past due consumption balances with all known peers
38
- *
39
- * @param ky Ky debug instance
40
- */
41
- export async function getPastDueConsumptionBalances(ky: Ky): Promise<BalanceResponse> {
42
- const response = await http<BalanceResponse>(ky, {
43
- path: consumedEndpoint,
44
- responseType: 'json',
45
- })
46
-
47
- return response.data
48
- }
49
-
50
- /**
51
- * Get the past due consumption balance with a specific peer
52
- *
53
- * @param ky Ky debug instance
54
- * @param address Swarm address of peer
55
- */
56
- export async function getPastDueConsumptionPeerBalance(ky: Ky, address: string): Promise<PeerBalance> {
57
- const response = await http<PeerBalance>(ky, {
58
- path: `${consumedEndpoint}/${address}`,
59
- responseType: 'json',
60
- })
61
-
62
- return response.data
63
- }