pastebee-cli
Version:
Cli tool for pasting logs to Bee
148 lines (146 loc) • 6.79 kB
JavaScript
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());
});
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const yargs_1 = __importDefault(require("yargs"));
const ora_1 = __importDefault(require("ora"));
const bee_js_1 = require("@ethersphere/bee-js");
const util_1 = require("util");
const META_FILE_NAME = '.swarmgatewaymeta.json';
const DEFAULT_BEE = 'https://bee-9.gateway.ethswarm.org';
const PASTEBEE_PAGE = 'https://pastebee.bzz.link/?';
const DEFAULT_POSTAGE = '0000000000000000000000000000000000000000000000000000000000000000';
function read(stream) {
var stream_1, stream_1_1;
var e_1, _a;
return __awaiter(this, void 0, void 0, function* () {
const chunks = [];
try {
for (stream_1 = __asyncValues(stream); stream_1_1 = yield stream_1.next(), !stream_1_1.done;) {
const chunk = stream_1_1.value;
chunks.push(chunk);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (stream_1_1 && !stream_1_1.done && (_a = stream_1.return)) yield _a.call(stream_1);
}
finally { if (e_1) throw e_1.error; }
}
return Buffer.concat(chunks).toString('utf8');
});
}
// eslint-disable-next-line @typescript-eslint/no-extra-semi
;
(function root() {
return __awaiter(this, void 0, void 0, function* () {
const argv = yield yargs_1.default(process.argv.slice(2))
.usage('Usage: <some STDOUT producing command> | $0 [options]')
.option('bee', {
alias: 'b',
type: 'string',
describe: 'Bee node URL. By default Gateway is used.',
})
.option('stamp', {
alias: 'p',
type: 'string',
describe: 'Postage Batch Stamp ID. Required if custom Bee node is used.',
})
.option('name', {
alias: 'n',
type: 'string',
describe: 'Name of the file that is stored in metadata.',
})
.option('hash', {
alias: 'a',
type: 'boolean',
describe: 'Displays only the reference not as URL.',
})
.option('silence', {
alias: 's',
type: 'boolean',
describe: 'Output only the uploaded reference without any UX.',
})
.help('h')
.alias('h', 'help').epilog(`pastebee-cli is a simple tool for piping text to Swarm network.
If you want to know more about Swarm network visit https://ethswarm.org/`).argv;
const beeNode = argv.bee || process.env.BEE_URL || DEFAULT_BEE;
const beeStamp = argv.stamp || process.env.BEE_STAMP || DEFAULT_POSTAGE;
const silence = argv.silence;
if (beeNode !== DEFAULT_BEE && (!beeStamp || beeStamp === DEFAULT_POSTAGE)) {
process.stderr.write('If you use custom Bee node, than you have to also specify your Postage Stamp Batch ID!');
process.exit(1);
}
const spinner = ora_1.default('Reading data from STDIN');
try {
if (!silence) {
spinner.start();
setTimeout(() => {
spinner.text = 'Reading data from STDIN (it takes long time, have you piped in some data?)';
}, 5000);
setTimeout(() => {
spinner.text = 'Reading data from STDIN (it takes really long time, maybe you want to exit? If so press CTRL+C)';
}, 12000);
}
const data = yield read(process.stdin);
if (!silence)
spinner.text = 'Uploading data';
const bee = new bee_js_1.Bee(beeNode);
const date = new Date();
const filename = argv.name ||
`pastebee-cli-${date.getFullYear()}-${date.getMonth()}-${date.getDay()}-${date.getHours()}-${date.getMinutes()}.txt`;
const metadata = {
name: filename,
type: 'text/plain',
size: data.length,
};
const metafile = {
path: META_FILE_NAME,
data: new util_1.TextEncoder().encode(JSON.stringify(metadata)),
};
const file = {
path: filename,
data: new util_1.TextEncoder().encode(data),
};
const uploadResult = yield bee.uploadCollection(beeStamp, [metafile, file], { indexDocument: filename });
const textResult = argv.hash ? uploadResult.reference : `${PASTEBEE_PAGE}${uploadResult.reference}`;
if (silence) {
process.stdout.write(textResult);
}
else {
spinner.succeed(`Uploaded! ${textResult}`);
}
// We have running timeouts which leaves the command hanging until they expire, with this we force the exit.
process.exit(0);
}
catch (e) {
const textResult = `There was an error during upload: ${e}`;
if (silence) {
process.stderr.write(textResult);
}
else {
spinner.fail(textResult);
}
process.exit(1);
}
});
})();
;