@ethersphere/bee-js
Version:
Javascript client for Bee
112 lines (111 loc) • 4.87 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.uploadCollection = exports.downloadFileReadable = exports.downloadFile = exports.uploadFile = void 0;
const cafe_utility_1 = require("cafe-utility");
const bytes_1 = require("../utils/bytes");
const collection_1 = require("../utils/collection");
const headers_1 = require("../utils/headers");
const http_1 = require("../utils/http");
const tar_uploader_1 = require("../utils/tar-uploader");
const type_1 = require("../utils/type");
const typed_bytes_1 = require("../utils/typed-bytes");
const bzzEndpoint = 'bzz';
/**
* Upload single file
*
* @param requestOptions Options for making requests
* @param data Files data
* @param postageBatchId Postage BatchId that will be assigned to uploaded data
* @param name Name that will be attached to the uploaded file. Wraps the data into manifest with set index document.
* @param options
*/
async function uploadFile(requestOptions, data, postageBatchId, name, options) {
if ((0, type_1.isReadable)(data) && !options?.contentType) {
if (!options) {
options = {};
}
options.contentType = 'application/octet-stream';
}
const response = await (0, http_1.http)(requestOptions, {
method: 'post',
url: bzzEndpoint,
data,
headers: (0, headers_1.prepareRequestHeaders)(postageBatchId, options),
params: { name },
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.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.uploadFile = uploadFile;
/**
* Download single file as a buffer
*
* @param requestOptions Options for making requests
* @param hash Bee file or collection hash
* @param path If hash is collection then this defines path to a single file in the collection
*/
async function downloadFile(requestOptions, resource, path = '', options) {
const response = await (0, http_1.http)(requestOptions, {
method: 'GET',
responseType: 'arraybuffer',
url: `${bzzEndpoint}/${resource}/${path}`,
headers: (0, headers_1.prepareRequestHeaders)(null, options),
});
const file = {
...(0, headers_1.readFileHeaders)(response.headers),
data: new bytes_1.Bytes(response.data),
};
return file;
}
exports.downloadFile = downloadFile;
/**
* Download single file as a readable stream
*
* @param requestOptions Options for making requests
* @param hash Bee file or collection hash
* @param path If hash is collection then this defines path to a single file in the collection
*/
async function downloadFileReadable(requestOptions, reference, path = '', options) {
reference = new typed_bytes_1.Reference(reference);
const response = await (0, http_1.http)(requestOptions, {
method: 'GET',
responseType: 'stream',
url: `${bzzEndpoint}/${reference}/${path}`,
headers: (0, headers_1.prepareRequestHeaders)(null, options),
});
const file = {
...(0, headers_1.readFileHeaders)(response.headers),
data: response.data,
};
return file;
}
exports.downloadFileReadable = downloadFileReadable;
/*******************************************************************************************************************/
// Collections
/**
* Upload collection
* @param requestOptions Options for making requests
* @param collection Collection of Uint8Array buffers to upload
* @param postageBatchId Postage BatchId that will be assigned to uploaded data
* @param options
*/
async function uploadCollection(requestOptions, collection, postageBatchId, options) {
(0, collection_1.assertCollection)(collection);
const response = await (0, tar_uploader_1.uploadTar)(requestOptions, collection, 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.uploadCollection = uploadCollection;