// @generated by scripts/build-runtime.mjs; do not edit. // @ts-nocheck -- generated JavaScript uses a .ts extension for Pi's Jiti loader. // src/runtime/helpers.ts import { open, opendir, stat } from "node:fs/promises"; import { basename, delimiter, dirname, extname, join, resolve } from "node:path"; var MAX_METADATA_FILE_BYTES = 64 * 1024; var COMMAND_TIMEOUT_MS = 2e3; var MAX_COMMAND_OUTPUT_BYTES = 64 * 1024; var MAX_DIRECTORY_ENTRIES = 2048; var MAX_LABEL_LENGTH = 160; function createFileSystem(input) { const defaults = { async readFile(path, maxBytes) { let handle; try { handle = await open(path, "r"); const info = await handle.stat(); if (!info.isFile() || info.size > maxBytes) return void 0; const buffer = Buffer.alloc(Math.min(maxBytes + 1, Number(info.size) + 1)); const { bytesRead } = await handle.read(buffer, 0, buffer.length, 0); if (bytesRead > maxBytes) return void 0; return buffer.subarray(0, bytesRead).toString("utf8"); } catch { return void 0; } finally { await handle?.close().catch(() => void 0); } }, async readDirectory(path) { const entries = []; try { const directory = await opendir(path); for await (const entry of directory) { entries.push({ name: entry.name, isFile: entry.isFile(), isDirectory: entry.isDirectory() }); if (entries.length >= MAX_DIRECTORY_ENTRIES) break; } } catch { return []; } return entries; }, async fileExists(path) { try { await stat(path); return true; } catch { return false; } } }; return { ...defaults, ...input.fileSystem, ...input.fileExists ? { fileExists: input.fileExists } : {} }; } function safeMetadata(value, maxLength = MAX_LABEL_LENGTH) { if (typeof value !== "string") return void 0; const sanitized = Array.from(value, (character) => { const code = character.codePointAt(0) ?? 0; return code <= 31 || code >= 127 && code <= 159 || code >= 55296 && code <= 57343 ? "" : character; }).join("").trim(); if (!sanitized) return void 0; return Array.from(sanitized).slice(0, maxLength).join(""); } function isRecord(value) { return typeof value === "object" && value !== null && !Array.isArray(value); } function setString(record, key, value) { const safe = safeMetadata(value); if (!safe) return; Object.defineProperty(record, key, { value: safe, writable: true, enumerable: true, configurable: true }); } function optionString(context, module, key) { const value = context.options(module)[key]; return typeof value === "string" ? value : void 0; } function optionBoolean(context, module, key) { return context.options(module)[key] === true; } function optionNumber(context, module, key) { const value = context.options(module)[key]; return typeof value === "number" ? value : void 0; } function optionStrings(context, module, key) { const value = context.options(module)[key]; return Array.isArray(value) && value.every((item) => typeof item === "string") ? value : []; } function optionMap(context, module, key) { const value = context.options(module)[key]; return isRecord(value) ? value : {}; } function exactAlias(value, aliases) { if (!value) return void 0; return Object.hasOwn(aliases, value) ? safeMetadata(aliases[value]) : value; } function formatVersion(raw, format) { return (format ?? "v$raw").replaceAll("$raw", raw.replace(/^v/u, "")); } async function runBounded(context, command, args) { try { const result = await context.input.exec(command, args, { cwd: context.input.cwd, timeout: COMMAND_TIMEOUT_MS, maxOutputBytes: MAX_COMMAND_OUTPUT_BYTES, signal: context.input.signal, environment: context.input.environment }); if (result.code !== 0 || result.killed) return void 0; const selectedOutput = result.stdout || result.stderr; const output = Buffer.from(selectedOutput).subarray(0, MAX_COMMAND_OUTPUT_BYTES + 1); if (output.byteLength > MAX_COMMAND_OUTPUT_BYTES) return void 0; return output.toString("utf8"); } catch { return void 0; } } function parseIni(source) { const result = {}; let section = ""; for (const rawLine of source.split(/\r?\n/u)) { const line = rawLine.trim(); if (!line || line.startsWith("#") || line.startsWith(";")) continue; const sectionMatch = /^\[([^\]]+)\]$/u.exec(line); if (sectionMatch?.[1]) { section = sectionMatch[1].trim(); if (!Object.hasOwn(result, section)) setOwn(result, section, {}); continue; } const separator = line.indexOf("="); if (separator <= 0) continue; const key = line.slice(0, separator).trim(); const value = safeMetadata(line.slice(separator + 1)); if (!value) continue; if (!Object.hasOwn(result, section)) setOwn(result, section, {}); setOwn(result[section], key, value); } return result; } function directMatch(entries, files, extensions, folders) { const positiveFiles = files.filter((value) => !value.startsWith("!")); const negativeFiles = new Set( files.filter((value) => value.startsWith("!")).map((v) => v.slice(1)) ); const positiveExtensions = extensions.filter((value) => !value.startsWith("!")).map((value) => value.replace(/^\./u, "")); const negativeExtensions = new Set( extensions.filter((value) => value.startsWith("!")).map((value) => value.slice(1).replace(/^\./u, "")) ); const positiveFolders = folders.filter((value) => !value.startsWith("!")); const negativeFolders = new Set( folders.filter((value) => value.startsWith("!")).map((value) => value.slice(1)) ); if (entries.some( (entry) => negativeFiles.has(entry.name) || entry.isFile && negativeExtensions.has(extname(entry.name).slice(1)) || entry.isDirectory && negativeFolders.has(entry.name) )) { return false; } return entries.some( (entry) => positiveFiles.includes(entry.name) || entry.isFile && positiveExtensions.includes(extname(entry.name).slice(1)) || entry.isDirectory && positiveFolders.includes(entry.name) ); } function parentDirectories(start, limit) { const result = []; let current = resolve(start); for (let index = 0; index < limit; index += 1) { const parent = dirname(current); if (parent === current) break; result.push(parent); current = parent; } return result; } function pathName(path) { return basename(path.replace(/[\\/]+$/u, "")) || path; } function joinHome(input, ...parts) { return join(input.homeDir, ...parts); } function setOwn(record, key, value) { Object.defineProperty(record, key, { value, writable: true, enumerable: true, configurable: true }); } export { MAX_METADATA_FILE_BYTES, createFileSystem, safeMetadata, isRecord, setString, optionString, optionBoolean, optionNumber, optionStrings, optionMap, exactAlias, formatVersion, runBounded, parseIni, directMatch, parentDirectories, pathName, joinHome }; //# sourceMappingURL=chunk-PV5KIAAP.ts.map