@ethersphere/swarm-cli
Version:
CLI tool for Bee
158 lines (157 loc) • 4.95 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isPrivateKey = exports.normalizePrivateKey = exports.parseHeaders = exports.readStdin = exports.getFieldOrNull = exports.hasField = exports.getFiles = exports.readdirDeepAsync = exports.getByteSize = exports.isGateway = exports.directoryExists = exports.expectFile = exports.fileExists = void 0;
const fs_1 = require("fs");
const path_1 = require("path");
const error_1 = require("./error");
function fileExists(path) {
try {
const stat = (0, fs_1.statSync)(path);
return stat.isFile();
}
catch {
return false;
}
}
exports.fileExists = fileExists;
function expectFile(path) {
if (!fileExists(path)) {
throw new error_1.CommandLineError(`Expected file at path '${path}', found none`);
}
}
exports.expectFile = expectFile;
function directoryExists(path) {
try {
const stat = (0, fs_1.statSync)(path);
return !stat.isFile();
}
catch {
return false;
}
}
exports.directoryExists = directoryExists;
function isGateway(url) {
return url.includes('gateway.ethswarm.org');
}
exports.isGateway = isGateway;
function getByteSize(data) {
if (data instanceof Uint8Array) {
return data.byteLength;
}
return Buffer.byteLength(data, 'utf-8');
}
exports.getByteSize = getByteSize;
/**
* Lists all files recursively in a folder
* @param path folder path
* @returns an async generator of path strings
*/
async function* walkTreeAsync(path) {
for await (const entry of await fs_1.promises.opendir(path)) {
const entryPath = (0, path_1.join)(path, entry.name);
if (entry.isDirectory()) {
yield* walkTreeAsync(entryPath);
}
else if (entry.isFile()) {
yield entryPath;
}
}
}
function removeLeadingDirectory(path, directory) {
if (directory.startsWith('./')) {
directory = directory.slice(2);
}
if (!directory.endsWith('/')) {
directory = directory + '/';
}
return path.replace(directory, '');
}
/**
* Lists all files recursively in a folder, considering cwd for the relative path
* @param path folder path
* @param cwd for relative paths
* @returns an async generator of path strings
*/
async function readdirDeepAsync(path, cwd) {
const entries = [];
for await (const entry of walkTreeAsync(path)) {
entries.push(cwd ? removeLeadingDirectory(entry, cwd) : entry);
}
return entries;
}
exports.readdirDeepAsync = readdirDeepAsync;
async function getFiles(path) {
const stat = (0, fs_1.statSync)(path);
if (stat.isDirectory()) {
return await readdirDeepAsync(path, path);
}
else {
return [path];
}
}
exports.getFiles = getFiles;
function hasField(some, key) {
return typeof some === 'object' && some !== null && key in some;
}
exports.hasField = hasField;
function getFieldOrNull(some, key) {
return typeof some === 'object' && some !== null ? Reflect.get(some, key) : null;
}
exports.getFieldOrNull = getFieldOrNull;
function readStdin(commandLog) {
const INTERVAL_SECS = 5;
return new Promise((resolve, reject) => {
let sizeCounter = 0;
let intervals = 0;
process.stdin.resume();
const chunks = [];
const interval = setInterval(() => {
if (!chunks.length) {
commandLog.info(`Nothing to read from stdin for ${++intervals * INTERVAL_SECS} seconds...`);
}
else {
clearInterval(interval);
}
}, INTERVAL_SECS * 1000);
process.stdin.on('data', chunk => {
sizeCounter += chunk.length;
if (sizeCounter > 1e9) {
reject('Reading more than 1 gigabyte from stdin is currently not supported');
return;
}
chunks.push(chunk);
});
process.stdin.on('end', () => {
clearInterval(interval);
resolve(Buffer.concat(chunks));
});
});
}
exports.readStdin = readStdin;
function parseHeaders(headers) {
const object = {};
for (const item of headers) {
const separatorIndex = item.indexOf(':');
if (separatorIndex === -1) {
continue;
}
const key = item.slice(0, separatorIndex).trim();
const value = item.slice(separatorIndex + 1).trim();
object[key] = value;
}
return object;
}
exports.parseHeaders = parseHeaders;
function normalizePrivateKey(string) {
let normalized = string.toLowerCase();
if (normalized.startsWith('0x') && normalized.length === 66) {
normalized = normalized.slice(2);
}
return normalized;
}
exports.normalizePrivateKey = normalizePrivateKey;
function isPrivateKey(string) {
const normalized = normalizePrivateKey(string);
return /^[a-f0-9]{64}$/.test(normalized);
}
exports.isPrivateKey = isPrivateKey;