| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- import * as fs from "fs";
- import {
- SandboxRuntimeConfigSchema,
- type SandboxRuntimeConfig,
- } from "../sandbox/sandbox-config.js";
- /**
- * Parse and validate sandbox configuration from a string
- * Used for parsing config from control fd (JSON lines protocol)
- */
- export function loadConfigFromString(
- content: string,
- ): SandboxRuntimeConfig | null {
- if (!content.trim()) {
- return null;
- }
- try {
- const parsed = JSON.parse(content);
- const result = SandboxRuntimeConfigSchema.safeParse(parsed);
- if (!result.success) {
- return null;
- }
- return result.data;
- } catch {
- return null;
- }
- }
- /**
- * Load and validate sandbox configuration from a file
- */
- export function loadConfig(filePath: string): SandboxRuntimeConfig | null {
- try {
- if (!fs.existsSync(filePath)) {
- return null;
- }
- const content = fs.readFileSync(filePath, "utf-8");
- if (content.trim() === "") {
- return null;
- }
- // Parse JSON
- const parsed = JSON.parse(content);
- // Validate with zod schema
- const result = SandboxRuntimeConfigSchema.safeParse(parsed);
- if (!result.success) {
- console.error(`Invalid configuration in ${filePath}:`);
- result.error.issues.forEach((issue) => {
- const path = issue.path.join(".");
- console.error(` - ${path}: ${issue.message}`);
- });
- return null;
- }
- return result.data;
- } catch (error) {
- // Log parse errors to help users debug invalid config files
- if (error instanceof SyntaxError) {
- console.error(
- `Invalid JSON in config file ${filePath}: ${error.message}`,
- );
- } else {
- console.error(`Failed to load config from ${filePath}: ${error}`);
- }
- return null;
- }
- }
|