-
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
-
199
-
200
-
201
-
202
-
203
-
204
-
205
-
206
-
207
-
208
-
209
-
210
-
211
-
212
-
213
-
214
-
215
-
216
-
217
-
218
-
219
-
220
-
221
-
222
-
223
-
224
-
225
-
226
-
227
-
228
-
229
-
230
-
231
-
232
-
233
-
234
-
235
-
236
-
237
-
238
-
239
-
240
-
241
-
242
-
243
-
244
-
245
-
246
-
247
-
248
-
249
-
250
-
251
-
252
-
253
-
254
-
255
-
256
-
257
-
258
-
259
-
260
-
261
-
262
-
263
-
264
-
265
-
266
-
267
-
268
-
269
-
270
-
271
-
272
-
273
-
274
-
275
-
276
// SPDX-FileCopyrightText: 2024 Shota FUJI <pockawoooh@gmail.com>
//
// SPDX-License-Identifier: Apache-2.0
import { relative, SEPARATOR } from "../../deps/deno.land/std/path/mod.ts";
import { logger } from "../logger.ts";
import type { FileSystemReader } from "./interface.ts";
import type {
DirectoryReader,
FileReader,
RootDirectoryReader,
} from "../types.ts";
/**
* FileSystem Reader for native file system, using Deno runtime API.
*
* This writer requires "read" permission for the `rootDirectory`.
*/
export class DenoFsReader implements FileSystemReader {
#root: string;
constructor(rootDirectory: string | URL) {
this.#root = typeof rootDirectory === "string"
? rootDirectory
: rootDirectory.pathname;
// Append trailing slash for easy path resolving
if (!(/[\\/]$/.test(this.#root))) {
this.#root += SEPARATOR;
}
}
#resolve = (path: readonly string[]): string => {
let normalized: string[] = [];
for (const segment of path) {
switch (segment) {
case ".":
break;
case "..":
// TODO: Abort for above the root traversal.
normalized = normalized.slice(0, -1);
break;
default:
normalized.push(segment);
break;
}
}
return this.#root + normalized.join(SEPARATOR);
};
#fromDirEntry = (
dirEntry: Deno.DirEntry,
parent: DirectoryReader | RootDirectoryReader,
): FileReader | DirectoryReader | null => {
const { name, isSymlink, isFile } = dirEntry;
const path = parent.type === "root" ? [name] : [...parent.path, name];
if (isSymlink) {
logger().warn(`Found symlink, skipping`, {
path,
});
return null;
}
if (isFile) {
return {
type: "file",
name,
path,
parent,
read: () => {
return Deno.readFile(this.#resolve(path));
},
stat: async () => {
const stat = await Deno.stat(this.#resolve(path));
return {
contentUpdatedAt: stat.mtime ?? undefined,
createdAt: stat.birthtime ?? undefined,
};
},
};
}
const dir: DirectoryReader = {
type: "directory",
name,
path,
parent,
read: async () => {
const converted: Array<FileReader | DirectoryReader> = [];
for await (const entry of Deno.readDir(this.#resolve(path))) {
const c = this.#fromDirEntry(entry, dir);
if (c) {
converted.push(c);
}
}
return converted;
},
stat: async () => {
const stat = await Deno.stat(this.#resolve(path));
return {
contentUpdatedAt: stat.mtime ?? undefined,
createdAt: stat.birthtime ?? undefined,
};
},
};
return dir;
};
async getRootDirectory(): Promise<RootDirectoryReader> {
await Deno.permissions.request({ name: "read", path: this.#root });
const root: RootDirectoryReader = {
type: "root",
read: async () => {
const converted: Array<FileReader | DirectoryReader> = [];
for await (const entry of Deno.readDir(this.#root)) {
const c = this.#fromDirEntry(entry, root);
if (c) {
converted.push(c);
}
}
return converted;
},
openFile: (path) => {
const resolvedPath = this.#resolve(path);
const fileInfo = Deno.statSync(resolvedPath);
if (!fileInfo.isFile) {
throw new Error(`DenoFsReader: ${resolvedPath} is not a file`);
}
if (!path.length) {
throw new Error(`DenoFsReader: file path cannot be empty`);
}
const parent = this.#constructParents(path, root);
const file = path[path.length - 1];
return {
type: "file",
name: file,
path,
parent,
read: async () => {
return Deno.readFile(resolvedPath);
},
stat: async () => {
const stat = await Deno.stat(this.#resolve(path));
return {
contentUpdatedAt: stat.mtime ?? undefined,
createdAt: stat.birthtime ?? undefined,
};
},
};
},
openDirectory: (path) => {
const resolvedPath = this.#resolve(path);
const stat = Deno.statSync(resolvedPath);
if (!stat.isDirectory) {
throw new Error(``);
}
if (!path.length) {
throw new Error(`DenoFsReader: directory path cannot be empty`);
}
const parent = this.#constructParents(path, root);
const name = path[path.length - 1];
const dir: DirectoryReader = {
type: "directory",
name,
path,
parent,
read: async () => {
const converted: Array<FileReader | DirectoryReader> = [];
for await (const entry of Deno.readDir(this.#resolve(path))) {
const c = this.#fromDirEntry(entry, dir);
if (c) {
converted.push(c);
}
}
return converted;
},
stat: async () => {
const stat = await Deno.stat(this.#resolve(path));
return {
contentUpdatedAt: stat.mtime ?? undefined,
createdAt: stat.birthtime ?? undefined,
};
},
};
return dir;
},
};
return root;
}
/**
* This method MAY require "read" permission for CWD.
*/
fromFsPath(path: string): readonly string[] {
const rel = relative(this.#root, path).split(SEPARATOR);
return rel.filter((segment) => {
if (segment === "..") {
throw new Error(
"DenoFsReader: Access to outside the reader root directory.",
);
}
return segment !== ".";
});
}
#constructParents(
path: readonly string[],
root: RootDirectoryReader,
): DirectoryReader | RootDirectoryReader {
let parent: DirectoryReader | RootDirectoryReader = root;
for (let i = 0, l = path.length - 1; i < l; i++) {
const dir: DirectoryReader = {
type: "directory",
name: path[i],
path: parent.type === "root" ? [path[i]] : [...parent.path, path[i]],
parent,
read: async () => {
const converted: Array<FileReader | DirectoryReader> = [];
for await (const entry of Deno.readDir(this.#resolve(path))) {
const c = this.#fromDirEntry(entry, dir);
if (c) {
converted.push(c);
}
}
return converted;
},
stat: async () => {
const stat = await Deno.stat(this.#resolve(path));
return {
contentUpdatedAt: stat.mtime ?? undefined,
createdAt: stat.birthtime ?? undefined,
};
},
};
parent = dir;
}
return parent;
}
}