-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
-
198
// SPDX-FileCopyrightText: 2024 Shota FUJI <pockawoooh@gmail.com>
//
// SPDX-License-Identifier: Apache-2.0
import type { FileSystemReader } from "./interface.ts";
import type {
DirectoryReader,
FileReader,
RootDirectoryReader,
} from "../types.ts";
interface FileBuilder {
path: string | readonly string[];
content: string | Uint8Array;
}
const SEP = "/";
type InternalTree = Map<string, Uint8Array | InternalTree>;
/**
* In-memory readonly filesystem.
*
* This was created for testing purpose.
*/
export class MemoryFsReader implements FileSystemReader {
#tree: InternalTree;
constructor(files: readonly FileBuilder[]) {
this.#tree = new Map();
const encoder = new TextEncoder();
for (const file of files) {
const path = typeof file.path === "string"
? file.path.split(SEP)
: file.path;
const content = typeof file.content === "string"
? encoder.encode(file.content)
: file.content;
this.#createRecur(path, content);
}
}
#createRecur(
path: readonly string[],
content: Uint8Array,
parent: InternalTree = this.#tree,
): void {
switch (path.length) {
case 0:
throw new Error("Path can't be empty");
case 1: {
const [name] = path;
const existing = parent.get(name);
if (existing && existing instanceof Map) {
throw new Error(
`Trying to create a file named "${name}", but directory with same name already exists.`,
);
}
parent.set(name, content);
return;
}
default: {
const [name, ...rest] = path;
let dir = parent.get(name);
if (dir && dir instanceof Uint8Array) {
throw new Error(
`Trying to create a directory named "${name}", but file with same name already exists.`,
);
}
if (!dir) {
dir = new Map();
parent.set(name, dir);
}
this.#createRecur(rest, content, dir);
return;
}
}
}
#mapToReaders(
map: InternalTree,
parent: DirectoryReader | RootDirectoryReader,
): Array<FileReader | DirectoryReader> {
const readers: Array<FileReader | DirectoryReader> = [];
for (const [name, contentOrSubTree] of map.entries()) {
const path = parent.type === "root" ? [name] : [...parent.path, name];
if (contentOrSubTree instanceof Map) {
const dir: DirectoryReader = {
type: "directory",
name,
path,
parent,
read: () =>
Promise.resolve(this.#mapToReaders(contentOrSubTree, dir)),
};
readers.push(dir);
continue;
}
readers.push({
type: "file",
name,
path,
parent,
read: () => Promise.resolve(contentOrSubTree),
});
}
return readers;
}
async #getAtRecur(
path: readonly string[],
parent: DirectoryReader | RootDirectoryReader,
): Promise<FileReader | DirectoryReader> {
switch (path.length) {
case 0:
throw new Error("MemoryFsReader: path cannot be empty");
case 1: {
const [name] = path;
for (const child of await parent.read()) {
if (child.name === name) {
return child;
}
}
const parentPath = parent.type === "root" ? [] : parent.path;
throw new Error(
`MemoryFsReader: file or directory not found at ${
[...parentPath, name].join(SEP)
}`,
);
}
default: {
const [name, ...rest] = path;
for (const child of await parent.read()) {
if (child.name === name) {
if (child.type === "file") {
throw new Error(
`MemoryFsReader: ${child.path.join(SEP)} is directory`,
);
}
return this.#getAtRecur(rest, child);
}
}
const parentPath = parent.type === "root" ? [] : parent.path;
throw new Error(
`MemoryFsReader: file or directory not found at ${
[...parentPath, name].join(SEP)
}`,
);
}
}
}
getRootDirectory(): Promise<RootDirectoryReader> {
const root: RootDirectoryReader = {
type: "root",
read: () => Promise.resolve(this.#mapToReaders(this.#tree, root)),
openFile: async (path) => {
const file = await this.#getAtRecur(path, root);
if (file.type === "directory") {
throw new Error(`MemoryFsReader: ${path.join(SEP)} is directory`);
}
return file;
},
openDirectory: async (path) => {
const dir = await this.#getAtRecur(path, root);
if (dir.type === "file") {
throw new Error(`MemoryFsReader: ${path.join(SEP)} is file`);
}
return dir;
},
};
return Promise.resolve(root);
}
}