| 50 | if (typeof data === "string") return encUtf8.encode(data); |
| 51 | if (data instanceof Uint8Array) return data.slice(); |
| 52 | return new Uint8Array(data).slice(); |
| 53 | } |
| 54 | |
| 55 | /* -------- implementation -------- */ |
| 56 | |
| 57 | const fsStub: FileSystemModule = { |
| 58 | existsSync(path: string): boolean { |
| 59 | const p = norm(path); |
| 60 | return files.has(p) || dirs.has(p); |
| 61 | }, |
| 62 | |
| 63 | removeSync(path: string): boolean { |
| 64 | const p = norm(path); |
| 65 | if (files.delete(p)) return true; |
| 66 | if (dirs.has(p)) { |
| 67 | // recursive remove directory |
| 68 | let removed = false; |
| 69 | // remove all nested files/dirs |
| 70 | for (const f of Array.from(files.keys())) { |
| 71 | if (f === p || f.startsWith(p + "/")) { |
| 72 | files.delete(f); |
| 73 | removed = true; |
| 74 | } |
| 75 | } |
| 76 | for (const d of Array.from(dirs.values())) { |
| 77 | if (d !== ROOT && (d === p || d.startsWith(p + "/"))) { |
| 78 | dirs.delete(d); |
| 79 | removed = true; |