Files
openclaw/src/shared/path-array-index.ts
2026-06-17 16:06:41 +08:00

14 lines
573 B
TypeScript

/** Upper bound for config path array indexes to reject impractical sparse writes. */
const MAX_CONFIG_PATH_ARRAY_INDEX = 100_000;
const CANONICAL_ARRAY_INDEX_SEGMENT = /^(0|[1-9]\d*)$/;
/** Parses a canonical non-negative array index segment used by config and JSON paths. */
export function parseConfigPathArrayIndex(segment: string): number | undefined {
if (!CANONICAL_ARRAY_INDEX_SEGMENT.test(segment)) {
return undefined;
}
const index = Number(segment);
return Number.isSafeInteger(index) && index <= MAX_CONFIG_PATH_ARRAY_INDEX ? index : undefined;
}