Skip to content
Source
Sign inSign up
samwillis/neon-realtime-electric
Open History to see the last commit

object-store.ts

40 lines1.3 KB

1import type { LogObjectStore } from '../../../packages/server/src/object-log.js'
2​
3export class FixtureObjectStore implements LogObjectStore {
4 readonly objects = new Map<string, Uint8Array>()
5 readonly puts: { key: string; bytes: Uint8Array }[] = []
6 readonly gets: string[] = []
7 readonly deletes: string[] = []
8 putError?: Error
9 getError?: Error
10 deleteError?: Error
11 beforePut?: () => Promise<void>
12 beforeGet?: (signal?: AbortSignal) => Promise<void>
13 async put(
14 key: string,
15 bytes: Uint8Array,
16 signal?: AbortSignal
17 ): Promise<void> {
18 signal?.throwIfAborted()
19 this.puts.push({ key, bytes: Uint8Array.from(bytes) })
20 await this.beforePut?.()
21 signal?.throwIfAborted()
22 if (this.putError) throw this.putError
23 this.objects.set(key, Uint8Array.from(bytes))
24 }
25 async get(key: string, signal?: AbortSignal): Promise<Uint8Array> {
26 signal?.throwIfAborted()
27 this.gets.push(key)
28 await this.beforeGet?.(signal)
29 signal?.throwIfAborted()
30 if (this.getError) throw this.getError
31 const bytes = this.objects.get(key)
32 if (!bytes) throw new Error('Missing object')
33 return Uint8Array.from(bytes)
34 }
35 async delete(key: string): Promise<void> {
36 this.deletes.push(key)
37 if (this.deleteError) throw this.deleteError
38 this.objects.delete(key)
39 }