UNPKG

spanwright

Version:

CLI tool to generate Cloud Spanner E2E testing framework projects with Go database tools and Playwright browser automation

174 lines 6.5 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createSecureTempDir = createSecureTempDir; exports.withTempDir = withTempDir; exports.createSecureTempFile = createSecureTempFile; exports.createSecureTempDirSync = createSecureTempDirSync; const fs = __importStar(require("fs")); const os = __importStar(require("os")); const path = __importStar(require("path")); const util_1 = require("util"); const crypto_1 = __importDefault(require("crypto")); const fsPromises = fs.promises; // Cleanup handlers registry const cleanupHandlers = new Set(); let cleanupRegistered = false; /** * Register cleanup handlers for process exit */ function registerCleanupHandlers() { if (cleanupRegistered) return; cleanupRegistered = true; const runCleanup = async () => { const handlers = Array.from(cleanupHandlers); cleanupHandlers.clear(); for (const handler of handlers) { try { await handler(); } catch (error) { // Suppress errors during cleanup to avoid interrupting other handlers console.error('Cleanup handler error:', error); } } }; // Handle various exit scenarios process.on('exit', runCleanup); process.on('SIGINT', async () => { await runCleanup(); process.exit(130); // Standard exit code for SIGINT }); process.on('SIGTERM', async () => { await runCleanup(); process.exit(143); // Standard exit code for SIGTERM }); } /** * Create a secure temporary directory * @param prefix - Optional prefix for the directory name * @returns Path to the created temporary directory */ async function createSecureTempDir(prefix = 'spanwright-') { // Get the real path of the system temp directory const realTmpDir = await fsPromises.realpath(os.tmpdir()); // Create secure temporary directory with random suffix // mkdtemp expects a path that ends with XXXXXX or will append random characters const tempDir = await fsPromises.mkdtemp(path.join(realTmpDir, prefix)); // Register cleanup handler registerCleanupHandlers(); const cleanup = async () => { try { await fsPromises.rm(tempDir, { recursive: true, force: true }); } catch { // Ignore errors during cleanup } }; cleanupHandlers.add(cleanup); return tempDir; } /** * Execute a function with a temporary directory that is automatically cleaned up * @param fn - Async function that receives the temp directory path * @param prefix - Optional prefix for the directory name * @returns The result of the function */ async function withTempDir(fn, prefix = 'spanwright-') { const tempDir = await createSecureTempDir(prefix); try { return await fn(tempDir); } finally { // Clean up the directory try { await fsPromises.rm(tempDir, { recursive: true, force: true }); } catch (error) { // Log but don't throw - the operation succeeded even if cleanup failed console.error('Failed to clean up temp directory:', tempDir, error); } // Remove from cleanup handlers since we already cleaned up cleanupHandlers.forEach(handler => { if (handler.toString().includes(tempDir)) { cleanupHandlers.delete(handler); } }); } } /** * Create a secure temporary file within a directory * @param dir - Directory to create the file in * @param prefix - Prefix for the filename * @returns Object with file descriptor and path */ async function createSecureTempFile(dir, prefix = 'tmp-') { // Generate a unique filename with crypto-random suffix const randomBytes = await (0, util_1.promisify)(crypto_1.default.randomBytes)(16); const randomSuffix = randomBytes.toString('hex'); const filename = `${prefix}${randomSuffix}`; const filePath = path.join(dir, filename); // Open file with exclusive creation flag (O_EXCL) const fd = await fsPromises.open(filePath, 'wx', 0o600); return { fd, path: filePath }; } /** * Synchronous version of createSecureTempDir for compatibility * @param prefix - Optional prefix for the directory name * @returns Path to the created temporary directory */ function createSecureTempDirSync(prefix = 'spanwright-') { // Get the real path of the system temp directory const realTmpDir = fs.realpathSync(os.tmpdir()); // Create secure temporary directory with random suffix const tempDir = fs.mkdtempSync(path.join(realTmpDir, prefix)); // Register cleanup handler registerCleanupHandlers(); const cleanup = () => { try { fs.rmSync(tempDir, { recursive: true, force: true }); } catch { // Ignore errors during cleanup } }; cleanupHandlers.add(cleanup); return tempDir; } //# sourceMappingURL=secure-temp.js.map