UNPKG

@ethersphere/swarm-cli

Version:
153 lines (152 loc) 4.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fileExists = fileExists; exports.expectFile = expectFile; exports.directoryExists = directoryExists; exports.getByteSize = getByteSize; exports.readdirDeepAsync = readdirDeepAsync; exports.getFiles = getFiles; exports.hasField = hasField; exports.getFieldOrNull = getFieldOrNull; exports.readStdin = readStdin; exports.parseHeaders = parseHeaders; exports.normalizePrivateKey = normalizePrivateKey; exports.isPrivateKey = isPrivateKey; 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; } } function expectFile(path) { if (!fileExists(path)) { throw new error_1.CommandLineError(`Expected file at path '${path}', found none`); } } function directoryExists(path) { try { const stat = (0, fs_1.statSync)(path); return !stat.isFile(); } catch { return false; } } function getByteSize(data) { if (data instanceof Uint8Array) { return data.byteLength; } return Buffer.byteLength(data, 'utf-8'); } /** * 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; } async function getFiles(path) { const stat = (0, fs_1.statSync)(path); if (stat.isDirectory()) { return await readdirDeepAsync(path, path); } else { return [path]; } } function hasField(some, key) { return typeof some === 'object' && some !== null && key in some; } function getFieldOrNull(some, key) { return typeof some === 'object' && some !== null ? Reflect.get(some, key) : null; } 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)); }); }); } 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; } function normalizePrivateKey(string) { let normalized = string.toLowerCase(); if (normalized.startsWith('0x') && normalized.length === 66) { normalized = normalized.slice(2); } return normalized; } function isPrivateKey(string) { const normalized = normalizePrivateKey(string); return /^[a-f0-9]{64}$/.test(normalized); }