Support explicit read-only RAG archives
This commit is contained in:
parent
b3191f4f2c
commit
bba41efbd6
6 changed files with 36 additions and 8 deletions
|
|
@ -7,20 +7,27 @@ export class SerializedFileStore<T> {
|
|||
public constructor(
|
||||
private readonly filePath: string,
|
||||
private readonly createDefault: () => T,
|
||||
private readonly options: { readOnly?: boolean } = {},
|
||||
) {}
|
||||
|
||||
public async snapshot(): Promise<T> {
|
||||
const store = await this.serialize(() => this.withInterprocessLock(() => this.readUnlocked()));
|
||||
const store = await this.serialize(() =>
|
||||
this.options.readOnly
|
||||
? this.readUnlocked()
|
||||
: this.withInterprocessLock(() => this.readUnlocked()),
|
||||
);
|
||||
return structuredClone(store);
|
||||
}
|
||||
|
||||
public async overwrite(nextValue: T): Promise<void> {
|
||||
this.requireWritable();
|
||||
await this.serialize(async () => {
|
||||
await this.withInterprocessLock(() => this.writeUnlocked(nextValue));
|
||||
});
|
||||
}
|
||||
|
||||
public async mutate<R>(mutator: (current: T) => R | Promise<R>): Promise<R> {
|
||||
this.requireWritable();
|
||||
return this.serialize(async () => {
|
||||
return this.withInterprocessLock(async () => {
|
||||
const current = await this.readUnlocked();
|
||||
|
|
@ -32,6 +39,7 @@ export class SerializedFileStore<T> {
|
|||
}
|
||||
|
||||
public async normalize(): Promise<void> {
|
||||
this.requireWritable();
|
||||
await this.serialize(async () => {
|
||||
await this.withInterprocessLock(async () => {
|
||||
const current = await this.readUnlocked();
|
||||
|
|
@ -49,6 +57,12 @@ export class SerializedFileStore<T> {
|
|||
return pending;
|
||||
}
|
||||
|
||||
private requireWritable(): void {
|
||||
if (this.options.readOnly) {
|
||||
throw new Error(`Read-only file store cannot mutate: ${this.filePath}`);
|
||||
}
|
||||
}
|
||||
|
||||
private async readUnlocked(): Promise<T> {
|
||||
try {
|
||||
const raw = await readFile(this.filePath, "utf8");
|
||||
|
|
|
|||
Loading…
Reference in a new issue