Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265/** Field reader over one untrusted journal record, failing closed with the record and field named.
@module */import type { AddedPathRecord, } from './commit-transaction-added-paths.ts';import type { PreparationBase, } from './commit-transaction-capture.ts';import type { FileIdentity, LockIdentity,} from './commit-transaction-journal-states.ts';import { CommitTransactionRecoveryError, } from './commit-transaction-recovery-validation.ts';
/** Optional record field is absent. */export const FIELD_ABSENT: unique symbol = Symbol('journal record field absent',);
/** Typed accessors over one record object. */export type RecordReader = Readonly<{ /** Reads a required string. */ string: (key: string) => string; /** Reads an optional string. */ optionalString: (key: string) => string | typeof FIELD_ABSENT; /** Reads a required boolean. */ boolean: (key: string) => boolean; /** Reads a required positive safe integer. */ positiveInteger: (key: string) => number; /** Reads a required array of strings. */ strings: (key: string) => readonly string[]; /** Reads a required nested object. */ object: (key: string) => RecordReader; /** Reads a preparation base. */ base: (key: string) => PreparationBase; /** Reads a file identity. */ identity: (key: string) => FileIdentity; /** Reads a lock identity. */ lock: (key: string) => LockIdentity; /** Reads added-path or selected-worktree records. */ addedPaths: (key: string) => readonly AddedPathRecord[];}>;
/** Builds a reader over one parsed object.
@param name - record name used in diagnostics
@param value - parsed JSON object
@returns field reader
@example ```ts createRecordReader({ name: 'preparing.json', value: {} }).string('mode'); ``` */export function createRecordReader({ name, value,}: Readonly<{ name: string; value: object;}>,): RecordReader { /** Builds the malformed-field failure.
@param key - field name
@returns recovery error */ function malformed(key: string,): CommitTransactionRecoveryError { return new CommitTransactionRecoveryError(`Transaction record ${name} has a malformed ${key} field.`,); }
/** Untrusted field value.
@param key - field name
@returns field value */ function field(key: string,): unknown { return Reflect.get( value, key, ); }
/** Reads a required string.
@param key - field name
@returns string */ function readString(key: string,): string { /** Candidate value. */ const candidate = field(key,); if ((typeof candidate) !== 'string') throw malformed(key,); return candidate; }
/** Reads a nested object.
@param key - field name
@returns nested reader */ function readObject(key: string,): RecordReader { /** Candidate value. */ const candidate = field(key,); if (((typeof candidate) !== 'object') || (candidate === null) || Array.isArray(candidate,)) throw malformed(key,); return createRecordReader({ name: `${name}#${key}`, value: candidate, },); }
/** Reads a file identity.
@param key - field name
@returns identity */ function readIdentity(key: string,): FileIdentity { /** Nested identity. */ const nested = readObject(key,); return { device: nested.string('device',), inode: nested.string('inode',), }; }
return { string: readString, optionalString: function readOptionalString(key: string,): string | typeof FIELD_ABSENT { return (key in value) ? readString(key,) : FIELD_ABSENT; }, boolean: function readBoolean(key: string,): boolean { /** Candidate value. */ const candidate = field(key,); if ((typeof candidate) !== 'boolean') throw malformed(key,); return candidate; }, positiveInteger: function readPositiveInteger(key: string,): number { /** Candidate value. */ const candidate = field(key,); if (((typeof candidate) !== 'number') || (!Number.isSafeInteger(candidate,)) || (candidate < 1)) throw malformed(key,); return candidate; }, strings: function readStrings(key: string,): readonly string[] { /** Candidate value. */ const candidate = field(key,); if (!Array.isArray(candidate,)) throw malformed(key,); return candidate.map(function stringItem(item: unknown,): string { if ((typeof item) !== 'string') throw malformed(key,); return item; },); }, object: readObject, base: function readBase(key: string,): PreparationBase { /** Nested base. */ const nested = readObject(key,); /** Base discriminator. */ const kind = nested.string('kind',); if (kind === 'unborn') return { kind: 'unborn', }; if (kind !== 'commit') throw malformed(key,); return { kind: 'commit', oid: nested.string('oid',), }; }, identity: readIdentity, lock: function readLock(key: string,): LockIdentity { return { ...readIdentity(key,), fsId: readObject(key,) .string('fsId',), }; }, addedPaths: function readAddedPaths(key: string,): readonly AddedPathRecord[] { /** Candidate value. */ const candidate = field(key,); if (!Array.isArray(candidate,)) throw malformed(key,); return candidate.map(function parseRecord(item: unknown,): AddedPathRecord { if (((typeof item) !== 'object') || (item === null)) throw malformed(key,); /** Nested record reader. */ const nested = createRecordReader({ name: `${name}#${key}`, value: item, },); /** Recorded Git mode. */ const gitMode = nested.string('gitMode',); if ((gitMode !== '100644') && (gitMode !== '100755')) throw malformed(key,); return { path: nested.string('path',), gitMode, originalOid: nested.string('originalOid',), intendedOid: nested.string('intendedOid',), }; },); }, };}