| 783 | // Fake SFTP that exposes a tree and implements the handful of callbacks |
| 784 | // SSHKaos actually calls. Anything not needed is left unimplemented. |
| 785 | function makeTreeSftp(root: TreeNode): unknown { |
| 786 | return { |
| 787 | realpath(path: string, cb: (err: Error | null, abs: string) => void): void { |
| 788 | cb(null, path); |
| 789 | }, |
| 790 | stat(path: string, cb: (err: Error | null, stats?: unknown) => void): void { |
| 791 | const node = lookup(root, path); |
| 792 | if (!node) { |
| 793 | const err = new Error(`no such file: ${path}`); |
| 794 | (err as unknown as { code: number }).code = 2; |
| 795 | cb(err); |
| 796 | return; |
| 797 | } |
| 798 | cb(null, makeStats(node)); |
| 799 | }, |
| 800 | lstat(path: string, cb: (err: Error | null, stats?: unknown) => void): void { |
| 801 | const node = lookup(root, path); |
| 802 | if (!node) { |
| 803 | const err = new Error(`no such file: ${path}`); |
| 804 | (err as unknown as { code: number }).code = 2; |
| 805 | cb(err); |
| 806 | return; |
| 807 | } |
| 808 | cb(null, makeStats(node)); |
| 809 | }, |
| 810 | readdir(path: string, cb: (err: Error | null, list?: unknown[]) => void): void { |
| 811 | const node = lookup(root, path); |
| 812 | if (!node || node.type !== 'dir' || !node.children) { |
| 813 | const err = new Error(`not a directory: ${path}`); |
| 814 | (err as unknown as { code: number }).code = 2; |
| 815 | cb(err); |
| 816 | return; |
| 817 | } |
| 818 | cb( |
| 819 | null, |
| 820 | Object.entries(node.children).map(([filename, child]) => ({ |
| 821 | filename, |
| 822 | attrs: makeStats(child), |
| 823 | })), |
| 824 | ); |
| 825 | }, |
| 826 | readFile(path: string, cb: (err: Error | null, data?: Buffer) => void): void { |
| 827 | const node = lookup(root, path); |
| 828 | if (!node || node.type !== 'file') { |
| 829 | const err = new Error(`no such file: ${path}`); |
| 830 | (err as unknown as { code: number }).code = 2; |
| 831 | cb(err); |
| 832 | return; |
| 833 | } |
| 834 | cb(null, node.content ?? Buffer.alloc(0)); |
| 835 | }, |
| 836 | }; |
| 837 | } |
| 838 | |
| 839 | function makeFakeKaos(sftp: unknown, cwd = '/'): SSHKaos { |
| 840 | const instance = Object.create(SSHKaos.prototype) as SSHKaos; |