@ethersphere/swarm-cli
Version:
CLI tool for Bee
104 lines (103 loc) • 4.07 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Download = void 0;
const bee_js_1 = require("@ethersphere/bee-js");
const fs_1 = __importDefault(require("fs"));
const furious_commander_1 = require("furious-commander");
const bzz_address_1 = require("../utils/bzz-address");
const download_1 = require("./manifest/download");
const root_command_1 = require("./root-command");
class Download extends root_command_1.RootCommand {
constructor() {
super(...arguments);
Object.defineProperty(this, "name", {
enumerable: true,
configurable: true,
writable: true,
value: 'download'
});
Object.defineProperty(this, "alias", {
enumerable: true,
configurable: true,
writable: true,
value: 'dl'
});
Object.defineProperty(this, "description", {
enumerable: true,
configurable: true,
writable: true,
value: 'Download arbitrary Swarm hash'
});
Object.defineProperty(this, "manifestDownload", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "address", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
}
async run() {
super.init();
this.address = await (0, bzz_address_1.makeBzzAddress)(this.bee, this.manifestDownload.bzzUrl);
if (await this.isManifest()) {
this.manifestDownload.address = this.address;
await this.manifestDownload.run();
}
else {
await this.downloadData();
}
}
async downloadData() {
let response;
if (this.manifestDownload.act) {
const responseAct = await this.bee.downloadFile(this.address.hash, this.manifestDownload.destination, {
actPublisher: this.manifestDownload.actPublisher,
actHistoryAddress: this.manifestDownload.actHistoryAddress,
actTimestamp: this.manifestDownload.actTimestamp,
});
response = responseAct.data;
}
else {
response = await this.bee.downloadData(this.address.hash, undefined);
}
if (this.manifestDownload.stdout) {
process.stdout.write(response.toUtf8());
}
else {
const path = this.manifestDownload.destination || this.address.hash;
await fs_1.default.promises.writeFile(path, response.toUint8Array());
}
}
async isManifest() {
try {
const node = await bee_js_1.MantarayNode.unmarshal(this.bee, this.address.hash);
await node.loadRecursively(this.bee);
return true;
}
catch {
return false;
}
}
}
__decorate([
(0, furious_commander_1.Aggregation)(['manifest download']),
__metadata("design:type", download_1.Download)
], Download.prototype, "manifestDownload", void 0);
exports.Download = Download;