Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 1x 1x 1x 1x 1x 1x 8x 1x 3x 3x 3x 1x 1x 1x 1x 1x 1x 3x 1x 7x 7x 7x 7x 5x 4x 4x 1x 1x 1x 1x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 1x 4x 4x 4x 4x 4x 4x 4x 1x 1x | /**
* TOML Utility Functions
*
* Provides TOML parsing and serialization capabilities for OpenFang configuration files.
* Supports environment variable interpolation in the format ${VAR_NAME}.
*
* @module toml-utils
*/
import TOML from 'smol-toml';
/**
* Error class for TOML parsing errors
*/
export class TomlParseError extends Error {
constructor(
message: string,
public readonly cause?: unknown
) {
super(message);
this.name = 'TomlParseError';
}
}
/**
* Error class for TOML serialization errors
*/
export class TomlStringifyError extends Error {
constructor(
message: string,
public readonly cause?: unknown
) {
super(message);
this.name = 'TomlStringifyError';
}
}
/**
* TOML utility functions for parsing and serializing configuration files
*/
export const tomlUtils = {
/**
* Parse a TOML string into a JavaScript object
*
* @param content - The TOML string to parse
* @returns The parsed JavaScript object
* @throws TomlParseError if the TOML content is invalid
*
* @example
* ```typescript
* const config = tomlUtils.parse(`
* [server]
* host = "127.0.0.1"
* port = 4200
* `);
* // config = { server: { host: "127.0.0.1", port: 4200 } }
* ```
*/
parse: <T = Record<string, unknown>>(content: string): T => {
try {
return TOML.parse(content) as T;
} catch (error) {
console.error('[TOML] Parse error:', error);
throw new TomlParseError(
`TOML parse error: ${error instanceof Error ? error.message : String(error)}`,
error
);
}
},
/**
* Serialize a JavaScript object to a TOML string
*
* @param data - The JavaScript object to serialize
* @returns The TOML string representation
* @throws TomlStringifyError if the object cannot be serialized to TOML
*
* @example
* ```typescript
* const toml = tomlUtils.stringify({
* server: { host: "127.0.0.1", port: 4200 }
* });
* ```
*/
stringify: (data: Record<string, unknown>): string => {
try {
return TOML.stringify(data);
} catch (error) {
console.error('[TOML] Stringify error:', error);
throw new TomlStringifyError(
`TOML stringify error: ${error instanceof Error ? error.message : String(error)}`,
error
);
}
},
/**
* Resolve environment variables in TOML content
*
* Replaces ${VAR_NAME} patterns with the corresponding environment variable values.
* If the environment variable is not set, it's replaced with an empty string.
*
* Note: In browser/Tauri context, this function has limited access to environment
* variables. For full resolution, use the Tauri backend to read env vars.
*
* @param content - The TOML content with potential ${VAR_NAME} patterns
* @param envVars - Optional object containing environment variables (for testing or Tauri-provided values)
* @returns The content with environment variables resolved
*
* @example
* ```typescript
* const content = 'api_key = "${OPENAI_API_KEY}"';
* const resolved = tomlUtils.resolveEnvVars(content, { OPENAI_API_KEY: 'sk-...' });
* // resolved = 'api_key = "sk-..."'
* ```
*/
resolveEnvVars: (
content: string,
envVars?: Record<string, string | undefined>
): string => {
return content.replace(/\$\{([^}]+)\}/g, (_, varName: string) => {
// If envVars provided, use them; otherwise try to access from window or return empty
if (envVars) {
return envVars[varName] ?? '';
}
// In browser context, we can't access process.env directly
// This will be handled by passing envVars from Tauri backend
console.warn(
`[TOML] Environment variable ${varName} not resolved - no envVars provided`
);
return '';
});
},
/**
* Parse TOML content with environment variable resolution
*
* Convenience method that combines resolveEnvVars and parse.
*
* @param content - The TOML content with potential ${VAR_NAME} patterns
* @param envVars - Optional object containing environment variables
* @returns The parsed and resolved JavaScript object
*
* @example
* ```typescript
* const config = tomlUtils.parseWithEnvVars(tomlContent, {
* ZHIPU_API_KEY: 'your-api-key'
* });
* ```
*/
parseWithEnvVars: <T = Record<string, unknown>>(
content: string,
envVars?: Record<string, string | undefined>
): T => {
const resolved = tomlUtils.resolveEnvVars(content, envVars);
return tomlUtils.parse<T>(resolved);
},
/**
* Check if a string contains unresolved environment variable placeholders
*
* @param content - The content to check
* @returns true if there are unresolved ${VAR_NAME} patterns
*/
hasUnresolvedEnvVars: (content: string): boolean => {
return /\$\{[^}]+\}/.test(content);
},
/**
* Extract environment variable names from TOML content
*
* @param content - The TOML content to scan
* @returns Array of environment variable names found
*/
extractEnvVarNames: (content: string): string[] => {
const matches = content.matchAll(/\$\{([^}]+)\}/g);
const names = new Set<string>();
for (const match of matches) {
names.add(match[1]);
}
return Array.from(names);
},
};
export default tomlUtils;
|