UNPKG

@ethersphere/bee-js

Version:
60 lines (59 loc) 2.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.download = exports.upload = void 0; const cafe_utility_1 = require("cafe-utility"); const headers_1 = require("../utils/headers"); const http_1 = require("../utils/http"); const type_1 = require("../utils/type"); const typed_bytes_1 = require("../utils/typed-bytes"); 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 */ async function upload(requestOptions, data, stamp, options) { const response = await (0, http_1.http)(requestOptions, { method: 'post', url: `${endpoint}`, data, headers: { 'content-type': 'application/octet-stream', ...(0, headers_1.prepareRequestHeaders)(stamp, options), }, responseType: 'json', }); const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' }); return { reference: new typed_bytes_1.Reference(cafe_utility_1.Types.asString(body.reference, { name: 'reference' })), tagUid: response.headers['swarm-tag'] ? (0, type_1.makeTagUid)(response.headers['swarm-tag']) : undefined, historyAddress: response.headers['swarm-act-history-address'] ? cafe_utility_1.Optional.of(new typed_bytes_1.Reference(response.headers['swarm-act-history-address'])) : cafe_utility_1.Optional.empty(), }; } exports.upload = upload; /** * Download chunk data as a byte array * * @param requestOptions Options for making requests * @param hash Bee content reference * */ async function download(requestOptions, reference, options) { reference = new typed_bytes_1.Reference(reference); const response = await (0, http_1.http)(requestOptions, { responseType: 'arraybuffer', url: `${endpoint}/${reference}`, headers: (0, headers_1.prepareRequestHeaders)(null, options), }); return new Uint8Array(response.data); } exports.download = download;