@ethersphere/bee-js 1.2.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (181) hide show
  1. package/CHANGELOG.md +135 -2
  2. package/README.md +3 -1
  3. package/dist/index.browser.min.js +1 -1
  4. package/dist/index.browser.min.js.LICENSE.txt +51 -0
  5. package/dist/index.browser.min.js.map +1 -1
  6. package/dist/index.min.js +1 -1
  7. package/dist/index.min.js.LICENSE.txt +42 -0
  8. package/dist/index.min.js.map +1 -1
  9. package/dist/src/bee-debug.d.ts +75 -38
  10. package/dist/src/bee.d.ts +88 -82
  11. package/dist/src/chunk/soc.d.ts +6 -6
  12. package/dist/src/feed/index.d.ts +7 -7
  13. package/dist/src/feed/json.d.ts +2 -2
  14. package/dist/src/modules/bytes.d.ts +8 -11
  15. package/dist/src/modules/bzz.d.ts +9 -14
  16. package/dist/src/modules/chunk.d.ts +7 -11
  17. package/dist/src/modules/debug/balance.d.ts +9 -9
  18. package/dist/src/modules/debug/chequebook.d.ts +17 -18
  19. package/dist/src/modules/debug/chunk.d.ts +5 -5
  20. package/dist/src/modules/debug/connectivity.d.ts +7 -7
  21. package/dist/src/modules/debug/settlements.d.ts +5 -5
  22. package/dist/src/modules/debug/stamps.d.ts +7 -5
  23. package/dist/src/modules/debug/states.d.ts +5 -5
  24. package/dist/src/modules/debug/status.d.ts +5 -4
  25. package/dist/src/modules/debug/tag.d.ts +3 -3
  26. package/dist/src/modules/debug/transactions.d.ts +9 -9
  27. package/dist/src/modules/feed.d.ts +5 -5
  28. package/dist/src/modules/pinning.d.ts +9 -9
  29. package/dist/src/modules/pss.d.ts +4 -3
  30. package/dist/src/modules/soc.d.ts +3 -3
  31. package/dist/src/modules/status.d.ts +3 -2
  32. package/dist/src/modules/stewardship.d.ts +4 -4
  33. package/dist/src/modules/tag.d.ts +6 -6
  34. package/dist/src/types/debug.d.ts +4 -2
  35. package/dist/src/types/index.d.ts +71 -19
  36. package/dist/src/utils/bytes.d.ts +1 -1
  37. package/dist/src/utils/data.browser.d.ts +3 -1
  38. package/dist/src/utils/data.d.ts +7 -4
  39. package/dist/src/utils/expose.d.ts +6 -9
  40. package/dist/src/utils/headers.d.ts +3 -3
  41. package/dist/src/utils/http.d.ts +25 -0
  42. package/dist/src/utils/merge.d.ts +8 -0
  43. package/dist/src/utils/pss.d.ts +9 -0
  44. package/dist/src/utils/stream.d.ts +37 -0
  45. package/dist/src/utils/type.d.ts +19 -9
  46. package/package.json +15 -8
  47. package/.babelrc.js +0 -33
  48. package/.depcheckrc.json +0 -27
  49. package/.editorconfig +0 -6
  50. package/.eslintignore +0 -1
  51. package/.eslintrc.js +0 -108
  52. package/.gitattributes +0 -1
  53. package/.github/config.yaml +0 -23
  54. package/.github/workflows/check.yaml +0 -57
  55. package/.github/workflows/publish_npmjs.yaml +0 -20
  56. package/.github/workflows/release_github.yaml +0 -20
  57. package/.github/workflows/tests.yaml +0 -128
  58. package/.huskyrc +0 -5
  59. package/.prettierrc +0 -13
  60. package/.vscode/launch.json +0 -49
  61. package/.vscode/settings.json +0 -39
  62. package/CODEOWNERS +0 -1
  63. package/CODE_OF_CONDUCT.md +0 -76
  64. package/commitlint.config.js +0 -3
  65. package/dist/src/modules/stamps.d.ts +0 -4
  66. package/dist/src/utils/hooks.d.ts +0 -31
  67. package/dist/src/utils/safe-axios.d.ts +0 -8
  68. package/jest-puppeteer.config.js +0 -5
  69. package/jest.config.ts +0 -170
  70. package/src/bee-debug.ts +0 -440
  71. package/src/bee.ts +0 -941
  72. package/src/chunk/bmt.ts +0 -55
  73. package/src/chunk/cac.ts +0 -79
  74. package/src/chunk/serialize.ts +0 -16
  75. package/src/chunk/signer.ts +0 -141
  76. package/src/chunk/soc.ts +0 -190
  77. package/src/chunk/span.ts +0 -33
  78. package/src/feed/index.ts +0 -221
  79. package/src/feed/json.ts +0 -32
  80. package/src/feed/topic.ts +0 -23
  81. package/src/feed/type.ts +0 -12
  82. package/src/index.ts +0 -24
  83. package/src/modules/bytes.ts +0 -75
  84. package/src/modules/bzz.ts +0 -184
  85. package/src/modules/chunk.ts +0 -75
  86. package/src/modules/debug/balance.ts +0 -63
  87. package/src/modules/debug/chequebook.ts +0 -174
  88. package/src/modules/debug/chunk.ts +0 -39
  89. package/src/modules/debug/connectivity.ts +0 -61
  90. package/src/modules/debug/settlements.ts +0 -33
  91. package/src/modules/debug/stamps.ts +0 -69
  92. package/src/modules/debug/states.ts +0 -35
  93. package/src/modules/debug/status.ts +0 -34
  94. package/src/modules/debug/tag.ts +0 -19
  95. package/src/modules/debug/transactions.ts +0 -79
  96. package/src/modules/feed.ts +0 -92
  97. package/src/modules/pinning.ts +0 -74
  98. package/src/modules/pss.ts +0 -48
  99. package/src/modules/soc.ts +0 -41
  100. package/src/modules/stamps.ts +0 -59
  101. package/src/modules/status.ts +0 -13
  102. package/src/modules/stewardship.ts +0 -21
  103. package/src/modules/tag.ts +0 -83
  104. package/src/types/axios/index.d.ts +0 -18
  105. package/src/types/debug.ts +0 -204
  106. package/src/types/index.ts +0 -497
  107. package/src/types/tar-js.d.ts +0 -1
  108. package/src/types/webpack-bundle-analyzer/index.d.ts +0 -13
  109. package/src/utils/bytes.ts +0 -144
  110. package/src/utils/collection.ts +0 -151
  111. package/src/utils/data.browser.ts +0 -24
  112. package/src/utils/data.ts +0 -35
  113. package/src/utils/error.ts +0 -23
  114. package/src/utils/eth.ts +0 -229
  115. package/src/utils/expose.ts +0 -10
  116. package/src/utils/file.ts +0 -40
  117. package/src/utils/hash.ts +0 -16
  118. package/src/utils/headers.ts +0 -63
  119. package/src/utils/hex.ts +0 -186
  120. package/src/utils/hooks.ts +0 -89
  121. package/src/utils/safe-axios.ts +0 -98
  122. package/src/utils/tar.ts +0 -30
  123. package/src/utils/type.ts +0 -302
  124. package/src/utils/uint64.ts +0 -31
  125. package/src/utils/url.ts +0 -52
  126. package/test/bee.sh +0 -187
  127. package/test/data/1.txt +0 -1
  128. package/test/data/2.txt +0 -0
  129. package/test/data/empty +0 -0
  130. package/test/data/sub/3.txt +0 -1
  131. package/test/data/sub//303/251 +0 -0
  132. package/test/data/sub//360/237/230/216 +0 -0
  133. package/test/integration/bee-class.browser.spec.ts +0 -215
  134. package/test/integration/bee-class.spec.ts +0 -595
  135. package/test/integration/bee-debug-class.spec.ts +0 -85
  136. package/test/integration/chunk/bmt.spec.ts +0 -33
  137. package/test/integration/chunk/cac.spec.ts +0 -27
  138. package/test/integration/chunk/soc.spec.ts +0 -38
  139. package/test/integration/feed/index.spec.ts +0 -84
  140. package/test/integration/modules/bytes.spec.ts +0 -23
  141. package/test/integration/modules/bzz.spec.ts +0 -270
  142. package/test/integration/modules/chunk.spec.ts +0 -29
  143. package/test/integration/modules/debug/balance.spec.ts +0 -70
  144. package/test/integration/modules/debug/chequebook.spec.ts +0 -64
  145. package/test/integration/modules/debug/connectivity.spec.ts +0 -69
  146. package/test/integration/modules/debug/settlements.spec.ts +0 -34
  147. package/test/integration/modules/debug/states.spec.ts +0 -34
  148. package/test/integration/modules/debug/status.spec.ts +0 -65
  149. package/test/integration/modules/debug/tag.spec.ts +0 -27
  150. package/test/integration/modules/feed.spec.ts +0 -61
  151. package/test/integration/modules/pinning.spec.ts +0 -155
  152. package/test/integration/modules/pss.spec.ts +0 -92
  153. package/test/integration/modules/status.spec.ts +0 -10
  154. package/test/integration/modules/stewardship.spec.ts +0 -32
  155. package/test/integration/modules/tag.spec.ts +0 -51
  156. package/test/test-type-sequencer.js +0 -36
  157. package/test/testpage/testpage.html +0 -20
  158. package/test/unit/assertions.ts +0 -376
  159. package/test/unit/bee-class.spec.ts +0 -685
  160. package/test/unit/bee-debug-class.spec.ts +0 -365
  161. package/test/unit/chunk/bmt.spec.ts +0 -16
  162. package/test/unit/chunk/cac.spec.ts +0 -29
  163. package/test/unit/chunk/serialize.spec.ts +0 -24
  164. package/test/unit/chunk/signer.spec.ts +0 -140
  165. package/test/unit/chunk/soc.spec.ts +0 -25
  166. package/test/unit/chunk/span.spec.ts +0 -38
  167. package/test/unit/feed/json.spec.ts +0 -73
  168. package/test/unit/nock.ts +0 -134
  169. package/test/unit/utils/bytes.spec.ts +0 -32
  170. package/test/unit/utils/collection.spec.ts +0 -22
  171. package/test/unit/utils/eth.spec.ts +0 -224
  172. package/test/unit/utils/hex.spec.ts +0 -115
  173. package/test/unit/utils/hooks.spec.ts +0 -179
  174. package/test/unit/utils/safe-axios.spec.ts +0 -20
  175. package/test/unit/utils/type.spec.ts +0 -32
  176. package/test/unit/utils/uint64.spec.ts +0 -102
  177. package/test/utils.ts +0 -292
  178. package/tsconfig.json +0 -18
  179. package/tsconfig.test.json +0 -11
  180. package/typedoc.json +0 -7
  181. package/webpack.config.ts +0 -141
package/src/feed/index.ts DELETED
@@ -1,221 +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
- } from '../types'
19
- import { Bytes, makeBytes, bytesAtOffset } from '../utils/bytes'
20
- import { BeeResponseError } from '../utils/error'
21
- import { bytesToHex, HexString, hexToBytes, makeHexString } from '../utils/hex'
22
- import { readUint64BigEndian, writeUint64BigEndian } from '../utils/uint64'
23
- import * as chunkAPI from '../modules/chunk'
24
- import { EthAddress, HexEthAddress, makeHexEthAddress } from '../utils/eth'
25
-
26
- import type { FeedType } from './type'
27
- import { assertAddress } from '../utils/type'
28
-
29
- const TIMESTAMP_PAYLOAD_OFFSET = 0
30
- const TIMESTAMP_PAYLOAD_SIZE = 8
31
- const REFERENCE_PAYLOAD_OFFSET = TIMESTAMP_PAYLOAD_SIZE
32
- const REFERENCE_PAYLOAD_MIN_SIZE = 32
33
- const REFERENCE_PAYLOAD_MAX_SIZE = 64
34
- const INDEX_HEX_LENGTH = 16
35
-
36
- export interface Epoch {
37
- time: number
38
- level: number
39
- }
40
- export type IndexBytes = Bytes<8>
41
- export type Index = number | Epoch | IndexBytes | string
42
-
43
- export interface FeedUploadOptions extends UploadOptions, FeedUpdateOptions {}
44
-
45
- type PlainChunkReference = Bytes<32>
46
- type EncryptedChunkReference = Bytes<64>
47
- export type ChunkReference = PlainChunkReference | EncryptedChunkReference
48
-
49
- export interface FeedUpdate {
50
- timestamp: number
51
- reference: ChunkReference
52
- }
53
-
54
- export function isEpoch(epoch: unknown): epoch is Epoch {
55
- return typeof epoch === 'object' && epoch !== null && 'time' in epoch && 'level' in epoch
56
- }
57
-
58
- function hashFeedIdentifier(topic: Topic, index: IndexBytes): Identifier {
59
- return keccak256Hash(hexToBytes(topic), index)
60
- }
61
-
62
- export function makeSequentialFeedIdentifier(topic: Topic, index: number): Identifier {
63
- const indexBytes = writeUint64BigEndian(index)
64
-
65
- return hashFeedIdentifier(topic, indexBytes)
66
- }
67
-
68
- export function makeFeedIndexBytes(s: string): IndexBytes {
69
- const hex = makeHexString(s, INDEX_HEX_LENGTH)
70
-
71
- return hexToBytes(hex)
72
- }
73
-
74
- export function makeFeedIdentifier(topic: Topic, index: Index): Identifier {
75
- if (typeof index === 'number') {
76
- return makeSequentialFeedIdentifier(topic, index)
77
- } else if (typeof index === 'string') {
78
- const indexBytes = makeFeedIndexBytes(index)
79
-
80
- return hashFeedIdentifier(topic, indexBytes)
81
- } else if (isEpoch(index)) {
82
- throw new TypeError('epoch is not yet implemented')
83
- }
84
-
85
- return hashFeedIdentifier(topic, index)
86
- }
87
-
88
- export async function uploadFeedUpdate(
89
- url: string,
90
- signer: Signer,
91
- topic: Topic,
92
- index: Index,
93
- reference: ChunkReference,
94
- postageBatchId: BatchId,
95
- options?: FeedUploadOptions,
96
- ): Promise<Reference> {
97
- const identifier = makeFeedIdentifier(topic, index)
98
- const at = options?.at ?? Date.now() / 1000.0
99
- const timestamp = writeUint64BigEndian(at)
100
- const payloadBytes = serializeBytes(timestamp, reference)
101
-
102
- return uploadSingleOwnerChunkData(url, signer, postageBatchId, identifier, payloadBytes, options)
103
- }
104
-
105
- export async function findNextIndex(
106
- url: string,
107
- owner: HexEthAddress,
108
- topic: Topic,
109
- options?: FeedUpdateOptions,
110
- ): Promise<HexString<typeof INDEX_HEX_LENGTH>> {
111
- try {
112
- const feedUpdate = await fetchFeedUpdate(url, owner, topic, options)
113
-
114
- return makeHexString(feedUpdate.feedIndexNext, INDEX_HEX_LENGTH)
115
- } catch (e) {
116
- if (e instanceof BeeResponseError && e.status === 404) {
117
- return bytesToHex(makeBytes(8))
118
- }
119
- throw e
120
- }
121
- }
122
-
123
- export async function updateFeed(
124
- url: string,
125
- signer: Signer,
126
- topic: Topic,
127
- reference: ChunkReference,
128
- postageBatchId: BatchId,
129
- options?: FeedUploadOptions,
130
- ): Promise<Reference> {
131
- const ownerHex = makeHexEthAddress(signer.address)
132
- const nextIndex = await findNextIndex(url, ownerHex, topic, options)
133
-
134
- return uploadFeedUpdate(url, signer, topic, nextIndex, reference, postageBatchId, options)
135
- }
136
-
137
- function verifyChunkReferenceAtOffset(offset: number, data: Uint8Array): ChunkReference {
138
- try {
139
- return bytesAtOffset(data, offset, REFERENCE_PAYLOAD_MAX_SIZE)
140
- } catch (e) {
141
- return bytesAtOffset(data, offset, REFERENCE_PAYLOAD_MIN_SIZE)
142
- }
143
- }
144
-
145
- export function verifyChunkReference(data: Uint8Array): ChunkReference {
146
- return verifyChunkReferenceAtOffset(0, data)
147
- }
148
-
149
- export async function downloadFeedUpdate(
150
- url: string,
151
- owner: EthAddress,
152
- topic: Topic,
153
- index: Index,
154
- ): Promise<FeedUpdate> {
155
- const identifier = makeFeedIdentifier(topic, index)
156
- const address = keccak256Hash(identifier, owner)
157
- const addressHex = bytesToHex(address)
158
- const data = await chunkAPI.download(url, addressHex)
159
- const soc = makeSingleOwnerChunkFromData(data, address)
160
- const payload = soc.payload()
161
- const timestampBytes = bytesAtOffset(payload, TIMESTAMP_PAYLOAD_OFFSET, TIMESTAMP_PAYLOAD_SIZE)
162
- const timestamp = readUint64BigEndian(timestampBytes)
163
- const reference = verifyChunkReferenceAtOffset(REFERENCE_PAYLOAD_OFFSET, payload)
164
-
165
- return {
166
- timestamp,
167
- reference,
168
- }
169
- }
170
-
171
- export function makeFeedReader(url: string, type: FeedType, topic: Topic, owner: HexEthAddress): FeedReader {
172
- const download = async (options?: FeedUpdateOptions) => fetchFeedUpdate(url, owner, topic, { ...options, type })
173
-
174
- return {
175
- type,
176
- owner,
177
- topic,
178
- download,
179
- }
180
- }
181
-
182
- function makeChunkReference(reference: ChunkReference | Reference): ChunkReference {
183
- if (typeof reference === 'string') {
184
- try {
185
- // Non-encrypted chunk hex string reference
186
- const hexReference = makeHexString(reference, REFERENCE_HEX_LENGTH)
187
-
188
- return hexToBytes<typeof REFERENCE_BYTES_LENGTH>(hexReference)
189
- } catch (e) {
190
- if (!(e instanceof TypeError)) {
191
- throw e
192
- }
193
-
194
- // Encrypted chunk hex string reference
195
- const hexReference = makeHexString(reference, ENCRYPTED_REFERENCE_HEX_LENGTH)
196
-
197
- return hexToBytes<typeof ENCRYPTED_REFERENCE_BYTES_LENGTH>(hexReference)
198
- }
199
- } else if (reference instanceof Uint8Array) {
200
- return verifyChunkReference(reference)
201
- }
202
- throw new TypeError('invalid chunk reference')
203
- }
204
-
205
- export function makeFeedWriter(url: string, type: FeedType, topic: Topic, signer: Signer): FeedWriter {
206
- const upload = async (
207
- postageBatchId: string | Address,
208
- reference: ChunkReference | Reference,
209
- options?: FeedUploadOptions,
210
- ) => {
211
- assertAddress(postageBatchId)
212
- const canonicalReference = makeChunkReference(reference)
213
-
214
- return updateFeed(url, signer, topic, canonicalReference, postageBatchId, { ...options, type })
215
- }
216
-
217
- return {
218
- ...makeFeedReader(url, type, topic, makeHexEthAddress(signer.address)),
219
- upload,
220
- }
221
- }
package/src/feed/json.ts DELETED
@@ -1,32 +0,0 @@
1
- import { FeedWriter, FeedReader, AnyJson, BatchId, Reference } 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
- ): Promise<Reference> {
28
- const serializedData = serializeJson(data)
29
- const reference = await bee.uploadData(postageBatchId, serializedData)
30
-
31
- return writer.upload(postageBatchId, reference)
32
- }
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,75 +0,0 @@
1
- import type { AxiosRequestConfig } from 'axios'
2
- import type { Readable } from 'stream'
3
- import { BatchId, Data, Reference, UploadOptions } from '../types'
4
- import { prepareData } from '../utils/data'
5
- import { extractUploadHeaders } from '../utils/headers'
6
- import { safeAxios } from '../utils/safe-axios'
7
- import { wrapBytesWithHelpers } from '../utils/bytes'
8
-
9
- const endpoint = '/bytes'
10
-
11
- /**
12
- * Upload data to a Bee node
13
- *
14
- * @param url Bee URL
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
- url: string,
21
- data: string | Uint8Array,
22
- postageBatchId: BatchId,
23
- options?: UploadOptions,
24
- ): Promise<Reference> {
25
- const response = await safeAxios<{ reference: Reference }>({
26
- ...options?.axiosOptions,
27
- method: 'post',
28
- url: url + endpoint,
29
- data: await prepareData(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 data as a byte array
42
- *
43
- * @param url Bee URL
44
- * @param hash Bee content reference
45
- */
46
- export async function download(url: string, hash: Reference): Promise<Data> {
47
- const response = await safeAxios<ArrayBuffer>({
48
- responseType: 'arraybuffer',
49
- url: `${url}${endpoint}/${hash}`,
50
- })
51
-
52
- return wrapBytesWithHelpers(new Uint8Array(response.data))
53
- }
54
-
55
- /**
56
- * Download data as a readable stream
57
- *
58
- * @param url Bee URL
59
- * @param hash Bee content reference
60
- * @param axiosOptions optional - alter default options of axios HTTP client
61
- */
62
- export async function downloadReadable(
63
- url: string,
64
- hash: Reference,
65
- axiosOptions?: AxiosRequestConfig,
66
- ): Promise<Readable> {
67
- const response = await safeAxios<Readable>({
68
- ...axiosOptions,
69
- method: 'GET',
70
- responseType: 'stream',
71
- url: `${url}${endpoint}/${hash}`,
72
- })
73
-
74
- return response.data
75
- }
@@ -1,184 +0,0 @@
1
- import {
2
- BatchId,
3
- Collection,
4
- CollectionUploadOptions,
5
- Data,
6
- FileData,
7
- FileUploadOptions,
8
- Reference,
9
- UploadHeaders,
10
- } from '../types'
11
- import { extractUploadHeaders, readFileHeaders } from '../utils/headers'
12
- import { safeAxios } from '../utils/safe-axios'
13
- import { prepareData } from '../utils/data'
14
- import { BeeArgumentError } from '../utils/error'
15
- import { makeTar } from '../utils/tar'
16
- import { assertCollection } from '../utils/collection'
17
- import { AxiosRequestConfig } from 'axios'
18
- import { wrapBytesWithHelpers } from '../utils/bytes'
19
- import { Readable } from 'stream'
20
-
21
- const bzzEndpoint = '/bzz'
22
-
23
- interface FileUploadHeaders extends UploadHeaders {
24
- 'content-length'?: string
25
- 'content-type'?: string
26
- }
27
-
28
- function extractFileUploadHeaders(postageBatchId: BatchId, options?: FileUploadOptions): FileUploadHeaders {
29
- const headers: FileUploadHeaders = extractUploadHeaders(postageBatchId, options)
30
-
31
- if (options?.size) headers['content-length'] = String(options.size)
32
-
33
- if (options?.contentType) headers['content-type'] = options.contentType
34
-
35
- return headers
36
- }
37
-
38
- /**
39
- * Upload single file
40
- *
41
- * @param url Bee URL
42
- * @param data Files data
43
- * @param postageBatchId Postage BatchId that will be assigned to uploaded data
44
- * @param name Name that will be attached to the uploaded file. Wraps the data into manifest with set index document.
45
- * @param options
46
- */
47
- export async function uploadFile(
48
- url: string,
49
- data: string | Uint8Array | Readable | ArrayBuffer,
50
- postageBatchId: BatchId,
51
- name?: string,
52
- options?: FileUploadOptions,
53
- ): Promise<Reference> {
54
- if (!url) {
55
- throw new BeeArgumentError('url parameter is required and cannot be empty', url)
56
- }
57
-
58
- const response = await safeAxios<{ reference: Reference }>({
59
- ...options?.axiosOptions,
60
- method: 'post',
61
- url: url + bzzEndpoint,
62
- data: prepareData(data),
63
- headers: {
64
- ...extractFileUploadHeaders(postageBatchId, options),
65
- },
66
- params: { name },
67
- responseType: 'json',
68
- })
69
-
70
- return response.data.reference
71
- }
72
-
73
- /**
74
- * Download single file as a buffer
75
- *
76
- * @param url Bee URL
77
- * @param hash Bee file or collection hash
78
- * @param path If hash is collection then this defines path to a single file in the collection
79
- * @param axiosOptions optional - alter default options of axios HTTP client
80
- */
81
- export async function downloadFile(
82
- url: string,
83
- hash: string,
84
- path = '',
85
- axiosOptions?: AxiosRequestConfig,
86
- ): Promise<FileData<Data>> {
87
- const response = await safeAxios<ArrayBuffer>({
88
- ...axiosOptions,
89
- method: 'GET',
90
- responseType: 'arraybuffer',
91
- url: `${url}${bzzEndpoint}/${hash}/${path}`,
92
- })
93
- const file = {
94
- ...readFileHeaders(response.headers),
95
- data: wrapBytesWithHelpers(new Uint8Array(response.data)),
96
- }
97
-
98
- return file
99
- }
100
-
101
- /**
102
- * Download single file as a readable stream
103
- *
104
- * @param url Bee URL
105
- * @param hash Bee file or collection hash
106
- * @param path If hash is collection then this defines path to a single file in the collection
107
- * @param axiosOptions optional - alter default options of axios HTTP client
108
- */
109
- export async function downloadFileReadable(
110
- url: string,
111
- hash: string,
112
- path = '',
113
- axiosOptions?: AxiosRequestConfig,
114
- ): Promise<FileData<Readable>> {
115
- const response = await safeAxios<Readable>({
116
- ...axiosOptions,
117
- method: 'GET',
118
- responseType: 'stream',
119
- url: `${url}${bzzEndpoint}/${hash}/${path}`,
120
- })
121
- const file = {
122
- ...readFileHeaders(response.headers),
123
- data: response.data,
124
- }
125
-
126
- return file
127
- }
128
-
129
- /*******************************************************************************************************************/
130
- // Collections
131
-
132
- interface CollectionUploadHeaders extends UploadHeaders {
133
- 'swarm-index-document'?: string
134
- 'swarm-error-document'?: string
135
- }
136
-
137
- function extractCollectionUploadHeaders(
138
- postageBatchId: BatchId,
139
- options?: CollectionUploadOptions,
140
- ): CollectionUploadHeaders {
141
- const headers: CollectionUploadHeaders = extractUploadHeaders(postageBatchId, options)
142
-
143
- if (options?.indexDocument) headers['swarm-index-document'] = options.indexDocument
144
-
145
- if (options?.errorDocument) headers['swarm-error-document'] = options.errorDocument
146
-
147
- return headers
148
- }
149
-
150
- /**
151
- * Upload collection
152
- * @param url Bee URL
153
- * @param collection Collection of Uint8Array buffers to upload
154
- * @param postageBatchId Postage BatchId that will be assigned to uploaded data
155
- * @param options
156
- */
157
- export async function uploadCollection(
158
- url: string,
159
- collection: Collection<Uint8Array>,
160
- postageBatchId: BatchId,
161
- options?: CollectionUploadOptions,
162
- ): Promise<Reference> {
163
- if (!url) {
164
- throw new BeeArgumentError('url parameter is required and cannot be empty', url)
165
- }
166
-
167
- assertCollection(collection)
168
- const tarData = makeTar(collection)
169
-
170
- const response = await safeAxios<{ reference: Reference }>({
171
- ...options?.axiosOptions,
172
- method: 'post',
173
- url: url + bzzEndpoint,
174
- data: tarData,
175
- responseType: 'json',
176
- headers: {
177
- 'content-type': 'application/x-tar',
178
- 'swarm-collection': 'true',
179
- ...extractCollectionUploadHeaders(postageBatchId, options),
180
- },
181
- })
182
-
183
- return response.data.reference
184
- }
@@ -1,75 +0,0 @@
1
- import type { AxiosRequestConfig } from 'axios'
2
- import type { Readable } from 'stream'
3
- import type { BatchId, ReferenceResponse, UploadOptions } from '../types'
4
- import { extractUploadHeaders } from '../utils/headers'
5
- import { safeAxios } from '../utils/safe-axios'
6
- import { Reference } from '../types'
7
-
8
- const endpoint = '/chunks'
9
-
10
- /**
11
- * Upload chunk to a Bee node
12
- *
13
- * The chunk data consists of 8 byte span and up to 4096 bytes of payload data.
14
- * The span stores the length of the payload in uint64 little endian encoding.
15
- * Upload expects the chuck data to be set accordingly.
16
- *
17
- * @param url Bee URL
18
- * @param data Chunk data to be uploaded
19
- * @param postageBatchId Postage BatchId that will be assigned to uploaded data
20
- * @param options Additional options like tag, encryption, pinning
21
- */
22
- export async function upload(
23
- url: string,
24
- data: Uint8Array,
25
- postageBatchId: BatchId,
26
- options?: UploadOptions,
27
- ): Promise<Reference> {
28
- const response = await safeAxios<ReferenceResponse>({
29
- ...options?.axiosOptions,
30
- method: 'post',
31
- url: `${url}${endpoint}`,
32
- data,
33
- headers: {
34
- 'content-type': 'application/octet-stream',
35
- ...extractUploadHeaders(postageBatchId, options),
36
- },
37
- responseType: 'json',
38
- })
39
-
40
- return response.data.reference
41
- }
42
-
43
- /**
44
- * Download chunk data as a byte array
45
- *
46
- * @param url Bee URL
47
- * @param hash Bee content reference
48
- *
49
- */
50
- export async function download(url: string, hash: string): Promise<Uint8Array> {
51
- const response = await safeAxios<ArrayBuffer>({
52
- responseType: 'arraybuffer',
53
- url: `${url}${endpoint}/${hash}`,
54
- })
55
-
56
- return new Uint8Array(response.data)
57
- }
58
-
59
- /**
60
- * Download chunk data as a readable stream
61
- *
62
- * @param url Bee URL
63
- * @param hash Bee content reference
64
- * @param axiosOptions optional - alter default options of axios HTTP client
65
- */
66
- export async function downloadReadable(url: string, hash: string, axiosOptions: AxiosRequestConfig): Promise<Readable> {
67
- const response = await safeAxios<Readable>({
68
- ...axiosOptions,
69
- method: 'GET',
70
- responseType: 'stream',
71
- url: `${url}${endpoint}/${hash}`,
72
- })
73
-
74
- return response.data
75
- }