示例工程 37-local-persistence
37-local-persistence/app/shared/ipc.ts
import type { BenchResult } from "./bench";
import type { JsonSource, Note } from "./json-store";
export const CH = {
snapshot: "store:snapshot",
addNote: "store:add-note",
reload: "store:reload",
corrupt: "store:corrupt-json",
bench: "store:bench",
refresh: "store:refresh",
} as const;
export type EventRow = {
at: number;
kind: string;
detail: string;
};
export type JsonInfo = {
dir: string;
path: string;
backupPath: string;
count: number;
size: number;
backupBytes: number;
source: JsonSource;
error: string;
writeMs: number;
readMs: number;
updatedAt: string;
tmpFiles: number;
notes: Note[];
};
export type SqliteInfo = {
available: boolean;
path: string;
count: number;
size: number;
journalMode: string;
writeMs: number;
readMs: number;
error: string;
notes: Note[];
};
export type PathInfo = {
userData: string;
appPath: string;
resources: string;
dbInAsar: boolean;
};
export type Snapshot = {
platform: string;
electronVersion: string;
nodeVersion: string;
json: JsonInfo;
sqlite: SqliteInfo;
paths: PathInfo;
bench: BenchResult | null;
events: EventRow[];
};
export function assertAddNoteReq(
value: unknown,
): asserts value is { text: string } {
if (typeof value !== "object" || value === null) {
throw new Error("BAD_NOTE_REQ");
}
const text = (value as { text?: unknown }).text;
if (typeof text !== "string") throw new Error("BAD_NOTE_TEXT");
const trimmed = text.trim();
if (trimmed.length === 0 || trimmed.length > 500) {
throw new Error("BAD_NOTE_TEXT");
}
}
export function assertBenchReq(
value: unknown,
): asserts value is { count: number } {
if (typeof value !== "object" || value === null) {
throw new Error("BAD_BENCH_REQ");
}
const count = (value as { count?: unknown }).count;
if (count !== 1000 && count !== 5000 && count !== 10000) {
throw new Error("BAD_BENCH_COUNT");
}
}