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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285/** Readers that reparse what the browser sinks persisted, plus record normalization and breadcrumb capture, for the browser property bundle. Browser APIs only: this module is bundled into a page.
@module */
import { _compareLogKeys as compareLogKeys, _parseLogKey as parseLogKey, type LogRecord,} from '@monochromatic-dev/module-logger';
//region Records
/** Canonical text of one record, so two record lists compare by value whatever object shape they arrived in.
@param record - Parsed or original record.
@returns Stable JSON of level, message, and timestamp.
@example ```ts canonical({ level: 'info', message: 'a', timestamp: 0 }); ``` */export function canonical(record: LogRecord,): string { return JSON.stringify([ record.level, record.message, record.timestamp, ],);}
/** Whether a parsed JSONL line has the record shape.
@param value - Parsed line.
@returns Whether it carries a level, a message, and a timestamp.
@example ```ts isRecord(JSON.parse(line)); ``` */export function isRecord(value: unknown,): value is LogRecord { return ((typeof value) === 'object') && (value !== null) && ('level' in value) && ('message' in value) && ('timestamp' in value);}
/** Parses newline-separated JSONL into records, dropping anything that is not a record.
@param text - Batch text.
@returns Records in line order.
@example ```ts recordsInBatch({ text: '{"level":"info","message":"a","timestamp":0}' }); ``` */export function recordsInBatch({ text, }: { readonly text: string; },): readonly LogRecord[] { return text.split('\n',) .filter(function nonEmpty(line,): boolean { return line.length > 0; },) .map(function parseLine(line,): unknown { return JSON.parse(line,); },) .filter(isRecord,);}
//endregion Records
//region localStorage
/** Key prefix shared by both web storage sinks. */const KEY_PREFIX = 'monochromatic.log.';
/** One run-scoped batch found in localStorage. */type StoredBatch = { readonly parsed: NonNullable<ReturnType<typeof parseLogKey>['parsed']>; readonly text: string;};
/** Reads every run-scoped batch the localStorage sink persisted, oldest run and lowest index first, and reparses the records.
@returns Records in persistence order.
@example ```ts readLocalStorageRecords(); ``` */export function readLocalStorageRecords(): readonly LogRecord[] { /** Batches keyed by their parsed identity. */ const batches: StoredBatch[] = []; for (let slot = 0; slot < localStorage.length; slot += 1) { /** Key at this slot. */ const key = localStorage.key(slot,); if ((key === null) || (!key.startsWith(KEY_PREFIX,))) continue; /** Run identity, absent for keys the sink does not own. */ const { parsed, } = parseLogKey(key,); /** Batch text under the key. */ const text = localStorage.getItem(key,); if ((parsed === undefined) || (text === null)) continue; batches.push({ parsed, text, },); } return batches .toSorted(function oldestFirst( left, right, ): number { return compareLogKeys({ first: left.parsed, second: right.parsed, },); },) .flatMap(function parseBatch(batch,): readonly LogRecord[] { return recordsInBatch({ text: batch.text, },); },);}
//endregion localStorage
//region IndexedDB
/** Database the IndexedDB sink opens. */const DATABASE_NAME = 'monochromatic.log';
/** Schema version the IndexedDB sink opens. */const DATABASE_VERSION = 1;
/** Object store holding one JSONL batch per entry. */const BATCH_STORE = 'batch';
/** Settles a request's promise from its `success` and `error` events.
@param request - IndexedDB request to await.
@returns Request result.
@example ```ts await settle({ request: store.getAll() }); ``` */async function settle<Result,>({ request, }: { readonly request: IDBRequest<Result>; },): Promise<Result> { /** Resolvers the event listeners settle. */ const pending = Promise.withResolvers<Result>(); request.addEventListener( 'success', function onSuccess(): void { pending.resolve(request.result,); }, ); request.addEventListener( 'error', function onError(): void { pending.reject(request.error ?? new Error('IndexedDB request failed',),); }, ); return await pending.promise;}
/** Reads every batch the IndexedDB sink persisted, in key order, and reparses the records.
@returns Records in persistence order.
@example ```ts await readIndexedDbRecords(); ``` */export async function readIndexedDbRecords(): Promise<readonly LogRecord[]> { /** Open connection to the sink's database. */ const database = await settle({ request: indexedDB.open( DATABASE_NAME, DATABASE_VERSION, ), },); /** Every stored batch text, in key order. */ const batches = await settle({ request: database .transaction( BATCH_STORE, 'readonly', ) .objectStore(BATCH_STORE,) .getAll(), },); database.close(); return batches.flatMap(function parseBatch(batch: unknown,): readonly LogRecord[] { return ((typeof batch) === 'string') ? recordsInBatch({ text: batch, },) : []; },);}
//endregion IndexedDB
//region Breadcrumbs
/** Captured `console.warn` lines; disposing restores the method. */export type BreadcrumbCapture = Disposable & { readonly lines: readonly string[];};
/** Replaces `console.warn` with a recorder for a scope, so a property can assert that a sink wrote no breadcrumb.
@returns Capture exposing the recorded lines.
@example ```ts using breadcrumbs = captureBreadcrumbs(); ``` */export function captureBreadcrumbs(): BreadcrumbCapture { /** Original method to restore. */ const original = console.warn; /** Recorded first arguments. */ const lines: string[] = []; console.warn = function record(first: unknown,): void { lines.push(String(first,),); }; return { lines, [Symbol.dispose](): void { console.warn = original; }, };}
//endregion Breadcrumbs