75 lines
2.8 KiB
JavaScript
75 lines
2.8 KiB
JavaScript
|
"use strict";
|
||
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||
|
return new (P || (P = Promise))(function (resolve, reject) {
|
||
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||
|
});
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
exports.downloadReadable = exports.download = exports.upload = void 0;
|
||
|
const data_1 = require("../utils/data");
|
||
|
const headers_1 = require("../utils/headers");
|
||
|
const http_1 = require("../utils/http");
|
||
|
const bytes_1 = require("../utils/bytes");
|
||
|
const type_1 = require("../utils/type");
|
||
|
const endpoint = 'bytes';
|
||
|
/**
|
||
|
* Upload data to a Bee node
|
||
|
*
|
||
|
* @param ky Ky instance
|
||
|
* @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
|
||
|
*/
|
||
|
function upload(ky, data, postageBatchId, options) {
|
||
|
return __awaiter(this, void 0, void 0, function* () {
|
||
|
const response = yield (0, http_1.http)(ky, {
|
||
|
path: endpoint,
|
||
|
method: 'post',
|
||
|
responseType: 'json',
|
||
|
body: yield (0, data_1.prepareData)(data),
|
||
|
headers: Object.assign({ 'content-type': 'application/octet-stream' }, (0, headers_1.extractUploadHeaders)(postageBatchId, options)),
|
||
|
});
|
||
|
return {
|
||
|
reference: response.data.reference,
|
||
|
tagUid: (0, type_1.makeTagUid)(response.headers.get('swarm-tag')),
|
||
|
};
|
||
|
});
|
||
|
}
|
||
|
exports.upload = upload;
|
||
|
/**
|
||
|
* Download data as a byte array
|
||
|
*
|
||
|
* @param ky
|
||
|
* @param hash Bee content reference
|
||
|
*/
|
||
|
function download(ky, hash) {
|
||
|
return __awaiter(this, void 0, void 0, function* () {
|
||
|
const response = yield (0, http_1.http)(ky, {
|
||
|
responseType: 'arraybuffer',
|
||
|
path: `${endpoint}/${hash}`,
|
||
|
});
|
||
|
return (0, bytes_1.wrapBytesWithHelpers)(new Uint8Array(response.data));
|
||
|
});
|
||
|
}
|
||
|
exports.download = download;
|
||
|
/**
|
||
|
* Download data as a readable stream
|
||
|
*
|
||
|
* @param ky
|
||
|
* @param hash Bee content reference
|
||
|
*/
|
||
|
function downloadReadable(ky, hash) {
|
||
|
return __awaiter(this, void 0, void 0, function* () {
|
||
|
const response = yield (0, http_1.http)(ky, {
|
||
|
responseType: 'stream',
|
||
|
path: `${endpoint}/${hash}`,
|
||
|
});
|
||
|
return response.data;
|
||
|
});
|
||
|
}
|
||
|
exports.downloadReadable = downloadReadable;
|