(path: string)
| 524 | // ── File I/O ─────────────────────────────────────────────────── |
| 525 | |
| 526 | async readFile(path: string): Promise<string | null> { |
| 527 | await this.ensureInit(); |
| 528 | const normalized = normalizePath(path); |
| 529 | const resolved = await this.resolveSymlink(normalized); |
| 530 | const T = this.tableName; |
| 531 | const rows = await this.sql.query<{ |
| 532 | type: string; |
| 533 | storage_backend: string; |
| 534 | r2_key: string | null; |
| 535 | content: string | null; |
| 536 | content_encoding: string; |
| 537 | }>( |
| 538 | `SELECT type, storage_backend, r2_key, content, content_encoding |
| 539 | FROM ${T} WHERE path = ?`, |
| 540 | resolved |
| 541 | ); |
| 542 | const r = rows[0]; |
| 543 | if (!r) return null; |
| 544 | if (r.type !== "file") throw new Error(`EISDIR: ${path} is a directory`); |
| 545 | this._observe("workspace:read", { |
| 546 | path: resolved, |
| 547 | storage: r.storage_backend as "inline" | "r2" |
| 548 | }); |
| 549 | |
| 550 | if (r.storage_backend === "r2" && r.r2_key) { |
| 551 | const r2 = this.getR2(); |
| 552 | if (!r2) { |
| 553 | throw new Error( |
| 554 | `File ${path} is stored in R2 but no R2 bucket was provided` |
| 555 | ); |
| 556 | } |
| 557 | const obj = await r2.get(r.r2_key); |
| 558 | if (!obj) return ""; |
| 559 | return await obj.text(); |
| 560 | } |
| 561 | |
| 562 | if (r.content_encoding === "base64" && r.content) { |
| 563 | const bytes = base64ToBytes(r.content); |
| 564 | return TEXT_DECODER.decode(bytes); |
| 565 | } |
| 566 | return r.content ?? ""; |
| 567 | } |
| 568 | |
| 569 | async readFileBytes(path: string): Promise<Uint8Array | null> { |
| 570 | await this.ensureInit(); |
no test coverage detected