Skip to content

Commit bab2b2e

Browse files
slapec93bosi95
andauthored
fix: streamable data upload and preserve http request data (#1249)
Co-authored-by: Balint Ujvari <balint.ujvari@solarpunk.buzz>
1 parent a89553d commit bab2b2e

4 files changed

Lines changed: 16 additions & 8 deletions

File tree

src/api/bytes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { BatchId, Bytes, Reference } from '@ethersphere/core-sdk'
22
import { Optional } from 'cafe-utility'
3+
import { Readable } from 'stream'
34
import type { BeeRequestOptions, DownloadOptions, RedundantUploadOptions, ReferenceInformation } from '../types'
45
import { UploadResult } from '../types'
56
import { UploadResultBody } from '../types/schema/upload'
@@ -17,7 +18,7 @@ const endpoint = 'bytes'
1718

1819
export async function upload(
1920
requestOptions: BeeRequestOptions,
20-
data: string | Uint8Array,
21+
data: string | Uint8Array | Blob | Readable,
2122
postageBatchId: BatchId,
2223
options?: RedundantUploadOptions,
2324
): Promise<UploadResult> {

src/modules/data.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { BatchId, Bytes, Reference } from '@ethersphere/core-sdk'
2+
import { Readable } from 'stream'
3+
import * as bytes from '../api/bytes'
4+
import * as stewardship from '../api/stewardship'
25
import type {
36
BeeRequestOptions,
47
DownloadOptions,
@@ -9,8 +12,6 @@ import type {
912
import { ResourceLocator } from '../utils/resource-locator'
1013
import { DownloadOptionsSchema, RedundantUploadOptionsSchema } from '../utils/schema'
1114
import { assertData } from '../utils/type'
12-
import * as bytes from '../api/bytes'
13-
import * as stewardship from '../api/stewardship'
1415
import type { BeeContext } from './context'
1516

1617
/**
@@ -33,7 +34,7 @@ export class Data {
3334
*/
3435
async upload(
3536
postageBatchId: BatchId | Uint8Array | string,
36-
data: string | Uint8Array,
37+
data: string | Uint8Array | Blob | Readable,
3738
options?: RedundantUploadOptions,
3839
requestOptions?: BeeRequestOptions,
3940
): Promise<UploadResult> {

src/utils/http.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ export interface BeeRequestConfig extends RequestInit {
3939
* @param config Internal settings and/or Bee settings
4040
*/
4141
export async function http<T>(options: BeeRequestOptions, config: BeeRequestConfig): Promise<BeeResponse<T>> {
42-
const requestConfig: BeeRequestConfig = Objects.deepMerge3(DEFAULT_HTTP_CONFIG, config, options)
42+
const rawData = config.data
43+
const requestConfig: BeeRequestConfig = Objects.deepMerge3(
44+
DEFAULT_HTTP_CONFIG,
45+
{ ...config, data: undefined },
46+
options,
47+
)
48+
requestConfig.data = rawData
4349

4450
if (options.signal) {
4551
requestConfig.signal = options.signal

src/utils/type.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ export function isTag(value: unknown): value is Tag {
3636
* @param value
3737
* @throws TypeError if not valid
3838
*/
39-
export function assertData(value: unknown): asserts value is string | Uint8Array {
40-
if (typeof value !== 'string' && !(value instanceof Uint8Array)) {
41-
throw new TypeError('Data must be either string or Uint8Array!')
39+
export function assertData(value: unknown): asserts value is string | Uint8Array | Blob | stream.Readable {
40+
if (typeof value !== 'string' && !(value instanceof Uint8Array) && !(value instanceof Blob) && !isReadable(value)) {
41+
throw new TypeError('Data must be either string, Uint8Array, Blob or Readable!')
4242
}
4343
}
4444

0 commit comments

Comments
 (0)