UNPKG

@ethersphere/bee-js

Version:
56 lines 2.01 kB
import { Optional, Types } from 'cafe-utility'; import { prepareRequestHeaders } from "../utils/headers.js"; import { http } from "../utils/http.js"; import { makeTagUid } from "../utils/type.js"; import { Reference } from "../utils/typed-bytes.js"; const endpoint = 'chunks'; /** * Upload chunk to a Bee node * * The chunk data consists of 8 byte span and up to 4096 bytes of payload data. * The span stores the length of the payload in uint64 little endian encoding. * Upload expects the chuck data to be set accordingly. * * @param requestOptions Options for making requests * @param data Chunk data to be uploaded * @param stamp BatchId or marshaled stamp to be used for the upload * @param options Additional options like tag, encryption, pinning */ export async function upload(requestOptions, data, stamp, options) { const response = await http(requestOptions, { method: 'post', url: `${endpoint}`, data, headers: { 'content-type': 'application/octet-stream', ...prepareRequestHeaders(stamp, options) }, responseType: 'json' }); const body = Types.asObject(response.data, { name: 'response.data' }); return { reference: new Reference(Types.asString(body.reference, { name: 'reference' })), tagUid: response.headers['swarm-tag'] ? makeTagUid(response.headers['swarm-tag']) : undefined, historyAddress: response.headers['swarm-act-history-address'] ? Optional.of(new Reference(response.headers['swarm-act-history-address'])) : Optional.empty() }; } /** * Download chunk data as a byte array * * @param requestOptions Options for making requests * @param hash Bee content reference * */ export async function download(requestOptions, reference, options) { reference = new Reference(reference); const response = await http(requestOptions, { responseType: 'arraybuffer', url: `${endpoint}/${reference}`, headers: prepareRequestHeaders(null, options) }); return new Uint8Array(response.data); }