| 1 | import { hashObject, type GitObject, type ObjectType } from "./objects.js"; |
| 2 | import type { ObjectStore } from "./store.js"; |
| 3 | |
| 4 | /** In-memory object store, used in tests and as a write buffer. */ |
| 5 | export class MemoryStore implements ObjectStore { |
| 6 | readonly objects = new Map<string, GitObject>(); |
| 7 | constructor(private fallback?: { read(oid: string): Promise<GitObject | null> }) {} |
| 8 | |
| 9 | async read(oid: string): Promise<GitObject | null> { |
| 10 | return this.objects.get(oid) ?? (this.fallback ? this.fallback.read(oid) : null); |
| 11 | } |
| 12 | |
| 13 | async write(type: ObjectType, content: Uint8Array): Promise<string> { |
| 14 | const oid = await hashObject(type, content); |
| 15 | if (!this.objects.has(oid)) this.objects.set(oid, { type, content }); |
| 16 | return oid; |
| 17 | } |
| 18 | } |