|
1 | 1 | import { Chunk, StorageAdapter, StorageKey } from '@automerge/automerge-repo'; |
2 | 2 |
|
3 | 3 | export class OPFSStorageAdapter extends StorageAdapter { |
4 | | - private baseDirectory: string |
| 4 | + private baseDirectory: string; |
| 5 | + private baseDirectoryHandle: FileSystemDirectoryHandle; |
| 6 | + private root: FileSystemDirectoryHandle; |
5 | 7 |
|
6 | | - load(key: StorageKey): Promise<Uint8Array | undefined> { |
7 | | - throw new Error('Method not implemented.'); |
| 8 | + constructor(baseDirectory: string = "automerge-repo-data") { |
| 9 | + super(); |
| 10 | + this.baseDirectory = baseDirectory; |
| 11 | + this.initialize(); |
8 | 12 | } |
9 | | - save(key: StorageKey, data: Uint8Array): Promise<void> { |
10 | | - throw new Error('Method not implemented.'); |
| 13 | + |
| 14 | + async load(key: StorageKey): Promise<Uint8Array | undefined> { |
| 15 | + const fileName = getKey(key); |
| 16 | + |
| 17 | + try { |
| 18 | + const fileHandle = await this.baseDirectoryHandle.getFileHandle(fileName); |
| 19 | + const file = await fileHandle.getFile(); |
| 20 | + const arrayBuffer = await file.arrayBuffer(); |
| 21 | + return new Uint8Array(arrayBuffer); |
| 22 | + } catch (error) { |
| 23 | + // don't throw if file not found |
| 24 | + if (error.code === "NotFoundError") return undefined |
| 25 | + throw error; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + async save(key: StorageKey, data: Uint8Array): Promise<void> { |
| 30 | + const fileName = getKey(key); |
| 31 | + |
| 32 | + const fileHandle = await this.baseDirectoryHandle.getFileHandle(fileName, { create: true }); |
| 33 | + const fileWritable = await fileHandle.createWritable(); |
| 34 | + await fileWritable.write(data); |
| 35 | + return fileWritable.close(); |
| 36 | + } |
| 37 | + |
| 38 | + async remove(key: StorageKey): Promise<void> { |
| 39 | + const fileName = getKey(key); |
| 40 | + |
| 41 | + try { |
| 42 | + return this.baseDirectoryHandle.removeEntry(fileName); |
| 43 | + } catch (error) { |
| 44 | + // don't throw if file not found |
| 45 | + if (error.code === "NotFoundError") return undefined |
| 46 | + throw error; |
| 47 | + } |
11 | 48 | } |
12 | | - remove(key: StorageKey): Promise<void> { |
13 | | - throw new Error('Method not implemented.'); |
| 49 | + |
| 50 | + async loadRange(keyPrefix: StorageKey): Promise<Chunk[]> { |
| 51 | + const chunks: Chunk[] = []; |
| 52 | + const prefix = getKey(keyPrefix); |
| 53 | + |
| 54 | + for await (const [name] of this.baseDirectoryHandle) { |
| 55 | + if (name.startsWith(prefix)) { |
| 56 | + const fileHandle = await this.baseDirectoryHandle.getFileHandle(name); |
| 57 | + const file = await fileHandle.getFile(); |
| 58 | + const arrayBuffer = await file.arrayBuffer(); |
| 59 | + chunks.push({key: name.split("_"), data: new Uint8Array(arrayBuffer)}); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return chunks; |
14 | 64 | } |
15 | | - loadRange(keyPrefix: StorageKey): Promise<Chunk[]> { |
16 | | - throw new Error('Method not implemented.'); |
| 65 | + |
| 66 | + async removeRange(keyPrefix: StorageKey): Promise<void> { |
| 67 | + const prefix = getKey(keyPrefix); |
| 68 | + |
| 69 | + for await (const [name] of this.baseDirectoryHandle) { |
| 70 | + if (name.startsWith(prefix)) { |
| 71 | + await this.baseDirectoryHandle.removeEntry(name); |
| 72 | + } |
| 73 | + } |
17 | 74 | } |
18 | | - removeRange(keyPrefix: StorageKey): Promise<void> { |
19 | | - throw new Error('Method not implemented.'); |
| 75 | + |
| 76 | + private async initialize() { |
| 77 | + this.root = await navigator.storage.getDirectory(); |
| 78 | + this.baseDirectoryHandle = await this.root.getDirectoryHandle(this.baseDirectory, { create: true }); |
20 | 79 | } |
| 80 | + |
21 | 81 | } |
| 82 | + |
| 83 | +const getKey = (key: StorageKey): string => key.join("_"); |
0 commit comments