UNPKG

@ethersphere/bee-js

Version:
94 lines (93 loc) 3.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.downloadReadable = exports.download = exports.head = exports.upload = void 0; const cafe_utility_1 = require("cafe-utility"); const bytes_1 = require("../utils/bytes"); 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 = 'bytes'; /** * Upload data to a Bee node * * @param requestOptions Options for making requests * @param data Data to be uploaded * @param postageBatchId Postage BatchId that will be assigned to uploaded data * @param options Additional options like tag, encryption, pinning */ async function upload(requestOptions, data, postageBatchId, options) { const response = await (0, http_1.http)(requestOptions, { url: endpoint, method: 'post', responseType: 'json', data, headers: { 'content-type': 'application/octet-stream', ...(0, headers_1.prepareRequestHeaders)(postageBatchId, options), }, }); const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' }); return { reference: new typed_bytes_1.Reference(cafe_utility_1.Types.asHexString(body.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; /** * Requests content length for a reference * * @param requestOptions Options for making requests * @param hash Bee content reference */ async function head(requestOptions, reference) { reference = new typed_bytes_1.Reference(reference); const response = await (0, http_1.http)(requestOptions, { url: `${endpoint}/${reference}`, method: 'head', responseType: 'json', }); return { contentLength: parseInt(response.headers['content-length']), }; } exports.head = head; /** * Download data as a byte array * * @param requestOptions Options for making requests * @param hash Bee content reference */ async function download(requestOptions, resource, options) { if (options) { options = (0, type_1.prepareDownloadOptions)(options); } const response = await (0, http_1.http)(requestOptions, { responseType: 'arraybuffer', url: `${endpoint}/${resource}`, headers: (0, headers_1.prepareRequestHeaders)(null, options), }); return new bytes_1.Bytes(response.data); } exports.download = download; /** * Download data as a readable stream * * @param requestOptions Options for making requests * @param hash Bee content reference */ async function downloadReadable(requestOptions, resource, options) { if (options) { options = (0, type_1.prepareDownloadOptions)(options); } const response = await (0, http_1.http)(requestOptions, { responseType: 'stream', url: `${endpoint}/${resource}`, headers: (0, headers_1.prepareRequestHeaders)(null, options), }); return response.data; } exports.downloadReadable = downloadReadable;