| 35 | } |
| 36 | |
| 37 | export class WorkspaceFileStore implements FileStore { |
| 38 | constructor(private readonly ws: WorkspaceLike) {} |
| 39 | |
| 40 | async stat(path: string): Promise<FileStat | null> { |
| 41 | try { |
| 42 | const s = await this.ws.fs.stat(path); |
| 43 | if (!s.isFile) return null; |
| 44 | return { size: s.size, mtime: s.mtime, mode: s.mode }; |
| 45 | } catch (err) { |
| 46 | if (isEnoent(err)) return null; |
| 47 | throw err; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | async readAll(path: string): Promise<Uint8Array | null> { |
| 52 | try { |
| 53 | const stream = await this.ws.fs.readFile(path); |
| 54 | return await drain(stream); |
| 55 | } catch (err) { |
| 56 | if (isEnoent(err)) return null; |
| 57 | throw err; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | async write(path: string, content: Uint8Array, opts?: { mode?: number }): Promise<void> { |
| 62 | await ensureParentDir(this.ws, path); |
| 63 | await this.ws.fs.writeFile(path, content, opts); |
| 64 | } |
| 65 | |
| 66 | async *readChunks(path: string, byteOffset = 0, byteLength?: number): AsyncIterable<Uint8Array> { |
| 67 | if (byteOffset < 0) throw new Error("readChunks: byteOffset must be non-negative"); |
| 68 | if (byteLength !== undefined && byteLength < 0) { |
| 69 | throw new Error("readChunks: byteLength must be non-negative"); |
| 70 | } |
| 71 | if (byteLength === 0) return; |
| 72 | |
| 73 | const stream = await this.ws.fs.readFile(path); |
| 74 | const reader = stream.getReader(); |
| 75 | let skipped = 0; |
| 76 | let yielded = 0; |
| 77 | let completed = false; |
| 78 | try { |
| 79 | while (true) { |
| 80 | const { value, done } = await reader.read(); |
| 81 | if (done) { |
| 82 | completed = true; |
| 83 | break; |
| 84 | } |
| 85 | if (!value || value.byteLength === 0) continue; |
| 86 | |
| 87 | let start = 0; |
| 88 | if (skipped < byteOffset) { |
| 89 | const needed = byteOffset - skipped; |
| 90 | if (value.byteLength <= needed) { |
| 91 | skipped += value.byteLength; |
| 92 | continue; |
| 93 | } |
| 94 | start = needed; |
nothing calls this directly
no outgoing calls
no test coverage detected