-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfs.ts
592 lines (530 loc) · 18.1 KB
/
fs.ts
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
import type { Backend, FileSystemMetadata } from "@zenfs/core";
import * as zenfs from "@zenfs/core";
import { Errno, ErrnoError, FileSystem, FileType, InMemory, PreloadFile, Stats } from "@zenfs/core";
import { basename, dirname, join } from "@zenfs/core/emulation/path.js";
import { log, tag } from "./logger.ts";
class FetchError extends Error {
constructor(
public readonly response: Response,
message?: string,
) {
super(message || `${response.status}: ${response.statusText}`);
}
}
function statsToMetadata(stats: zenfs.StatsLike) {
return {
atimeMs: stats.atimeMs,
mtimeMs: stats.mtimeMs,
ctimeMs: stats.ctimeMs,
birthtimeMs: stats.birthtimeMs,
uid: stats.uid,
gid: stats.gid,
size: stats.size,
mode: stats.mode,
ino: stats.ino,
};
}
export class CloudflareKVFileSystem extends zenfs.FileSystem {
private readonly fetch: (
method: string,
path: string,
body?: Uint8Array,
headers?: Record<string, string>,
) => Promise<Response>;
private cache: Map<string, Stats> = new Map();
constructor(
readonly prefix: string,
readonly token: string,
) {
super();
this.fetch = (method: string, path: string, body?: Uint8Array, headers?: Record<string, string>) => {
log.debug(tag.kvfs, "fetch", method, path);
const url = `${prefix}${path}`;
return fetch(url, {
method,
body,
headers: {
Authorization: `Bearer ${token}`,
...(headers ?? {}),
},
});
};
}
async ready(): Promise<void> {
await this.reload();
}
async reload(): Promise<void> {
this.cache = new Map(await this.readList());
}
async rename(oldPath: string, newPath: string, cred: zenfs.Cred): Promise<void> {
log.debug(tag.kvfs, "rename", { oldPath, newPath });
const oldFile = await this.openFile(oldPath, "r", cred);
const stats = await oldFile.stat();
const buffer = new Uint8Array(stats.size);
await oldFile.read(buffer, 0, stats.size, 0);
await oldFile.close();
const newFile = await this.createFile(newPath, "w", stats.mode, cred);
await newFile.write(buffer, 0, buffer.length, 0);
await newFile.close();
await this.unlink(oldPath, cred);
this.cache.delete(oldPath);
this.cache.set(newPath, stats);
}
renameSync(_oldPath: string, _newPath: string, _cred: zenfs.Cred): void {
throw new Error("Method not implemented.");
}
async stat(path: string, _cred: zenfs.Cred): Promise<zenfs.Stats> {
// log.debug(tag.kvfs, "stat", path);
return this.statSync(path, _cred);
}
statSync(path: string, _cred: zenfs.Cred): zenfs.Stats {
const stats = this.cache.get(path);
if (!stats) {
throw new ErrnoError(Errno.ENOENT, "path", path, "stat");
}
return stats;
}
async openFile(path: string, flag: string, cred: zenfs.Cred): Promise<zenfs.File> {
log.debug(tag.kvfs, "openFile", { path, flag, cred });
let buffer: ArrayBufferLike;
let stats = this.cache.get(path);
if (zenfs.isWriteable(flag)) {
buffer = new ArrayBuffer(0);
stats = new zenfs.Stats({ mode: 0o777 | zenfs.FileType.FILE, size: 0 });
this.cache.set(path, stats);
} else {
if (!stats) {
throw ErrnoError.With("ENOENT", path, "openFile");
}
if (!stats.hasAccess(zenfs.flagToMode(flag), cred)) {
throw ErrnoError.With("EACCES", path, "openFile");
}
const r = await this.fetch("GET", path);
if (!r.ok) {
throw new FetchError(r);
}
buffer = await (await r.blob()).arrayBuffer();
}
return new zenfs.PreloadFile(this, path, flag, stats, new Uint8Array(buffer));
}
openFileSync(_path: string, _flag: string, _cred: zenfs.Cred): zenfs.File {
throw new Error("Method not implemented.");
}
async createFile(path: string, flag: string, mode: number, cred: zenfs.Cred): Promise<zenfs.File> {
log.debug(tag.kvfs, "createFile", { path, flag, mode, cred });
const data = new Uint8Array(0);
const r = await this.fetch("PUT", path, data);
if (!r.ok) {
throw new FetchError(r);
}
const stats = new zenfs.Stats({ mode: mode | zenfs.FileType.FILE, size: 0 });
this.cache.set(path, stats);
return new zenfs.PreloadFile(this, path, flag, stats, data);
}
createFileSync(_path: string, _flag: string, _mode: number, _cred: zenfs.Cred): zenfs.File {
throw new Error("Method not implemented.");
}
async unlink(path: string, _cred: zenfs.Cred): Promise<void> {
log.debug(tag.kvfs, "unlink", { path });
const r = await this.fetch("DELETE", path);
if (!r.ok) {
throw new FetchError(r);
}
this.cache.delete(path);
}
unlinkSync(_path: string, _cred: zenfs.Cred): void {
throw new Error("Method not implemented.");
}
async rmdir(path: string, _cred: zenfs.Cred): Promise<void> {
log.debug(tag.kvfs, "rmdir", { path });
const r = await this.fetch("DELETE", path);
if (!r.ok) {
throw new FetchError(r);
}
this.cache.delete(path);
}
rmdirSync(_path: string, _cred: zenfs.Cred): void {
throw new Error("Method not implemented.");
}
async mkdir(path: string, mode: number, _cred: zenfs.Cred): Promise<void> {
log.debug(tag.kvfs, "mkdir", { path, mode });
const stats = new zenfs.Stats({ mode: mode | zenfs.FileType.DIRECTORY, size: 4096 });
const r = await this.fetch("PUT", path, new Uint8Array(0), {
"x-metadata": JSON.stringify(statsToMetadata(stats)),
});
if (!r.ok) {
throw new FetchError(r);
}
this.cache.set(path, stats);
}
mkdirSync(_path: string, _mode: number, _cred: zenfs.Cred): void {
throw new Error("Method not implemented.");
}
async readdir(path: string, cred: zenfs.Cred): Promise<string[]> {
log.debug(tag.kvfs, "readdir", { path });
return this.readdirSync(path, cred);
}
readdirSync(path: string, _cred: zenfs.Cred): string[] {
const prefix = !path.endsWith("/") ? `${path}/` : path;
return [...this.cache.keys()]
.filter((_) => _.startsWith(prefix) && _.substring(prefix.length).split("/").length === 1)
.map((_) => _.substring(prefix.length))
.filter((_) => _.length > 0);
}
link(_srcpath: string, _dstpath: string, _cred: zenfs.Cred): Promise<void> {
throw new Error("Method not implemented.");
}
linkSync(_srcpath: string, _dstpath: string, _cred: zenfs.Cred): void {
throw new Error("Method not implemented.");
}
async sync(path: string, data: Uint8Array, stats: Readonly<zenfs.Stats>): Promise<void> {
log.debug(tag.kvfs, "sync", { path, data, stats });
const metadata = statsToMetadata(stats);
const body = new Uint8Array(data.byteLength);
body.set(data);
const r = await this.fetch("PUT", path, body, { "x-metadata": JSON.stringify(metadata) });
if (!r.ok) {
log.error(tag.kvfs, "sync", path, r.status, r.statusText);
throw ErrnoError.With("EIO", path, "sync");
}
this.cache.set(path, new Stats(stats));
}
syncSync(_path: string, _data: Uint8Array, _stats: Readonly<zenfs.Stats>): void {
throw new Error("Method not implemented.");
}
protected async readList() {
const r = await this.fetch("GET", "");
if (!r.ok) {
throw new FetchError(r);
}
const list = [
["/", new zenfs.Stats({ mode: 0o777 | zenfs.FileType.DIRECTORY, size: 4096 })],
...(await r.json()).map((_: { name: string; metadata: { dir: boolean; size: number } }) => [
`/${_.name}`,
new zenfs.Stats(_.metadata),
]),
];
log.debug(tag.kvfs, "readList", { list });
return list;
}
}
export interface CloudflareKVOptions {
prefix: string;
token: string;
}
export const CloudflareKV = {
name: "CloudflareKV",
options: {
prefix: {
type: "string",
required: true,
description: "The URL prefix to use for requests",
},
token: {
type: "string",
required: true,
description: "The JWT token to use",
},
},
isAvailable(): boolean {
return true;
},
create(options: CloudflareKVOptions) {
return new CloudflareKVFileSystem(options.prefix, options.token);
},
} as const satisfies Backend<CloudflareKVFileSystem, CloudflareKVOptions>;
/**
* Converts a DOMException into an Errno
* @see https://developer.mozilla.org/Web/API/DOMException
*/
function errnoForDOMException(ex: DOMException): keyof typeof Errno {
switch (ex.name) {
case "IndexSizeError":
case "HierarchyRequestError":
case "InvalidCharacterError":
case "InvalidStateError":
case "SyntaxError":
case "NamespaceError":
case "TypeMismatchError":
case "ConstraintError":
case "VersionError":
case "URLMismatchError":
case "InvalidNodeTypeError":
return "EINVAL";
case "WrongDocumentError":
return "EXDEV";
case "NoModificationAllowedError":
case "InvalidModificationError":
case "InvalidAccessError":
case "SecurityError":
case "NotAllowedError":
return "EACCES";
case "NotFoundError":
return "ENOENT";
case "NotSupportedError":
return "ENOTSUP";
case "InUseAttributeError":
return "EBUSY";
case "NetworkError":
return "ENETDOWN";
case "AbortError":
return "EINTR";
case "QuotaExceededError":
return "ENOSPC";
case "TimeoutError":
return "ETIMEDOUT";
case "ReadOnlyError":
return "EROFS";
default:
return "EIO";
}
}
/**
* @internal
*/
export type ConvertException = ErrnoError | DOMException | Error;
/**
* Handles converting errors, then rethrowing them
* @internal
*/
export function convertException(ex: ConvertException, path?: string, syscall?: string): ErrnoError {
if (ex instanceof ErrnoError) {
return ex;
}
const code = ex instanceof DOMException ? Errno[errnoForDOMException(ex)] : Errno.EIO;
const error = new ErrnoError(code, ex.message, path, syscall);
error.stack = ex.stack!;
(error as any).cause = (ex as any).cause;
return error;
}
declare global {
interface FileSystemDirectoryHandle {
[Symbol.iterator](): IterableIterator<[string, FileSystemHandle]>;
entries(): IterableIterator<[string, FileSystemHandle]>;
keys(): IterableIterator<string>;
values(): IterableIterator<FileSystemHandle>;
}
}
export interface WebAccessOptions {
handle: FileSystemDirectoryHandle;
}
export class WebAccessFS extends FileSystem {
renameSync(_oldPath: string, _newPath: string, _cred: zenfs.Cred): void {
throw new Error("Method not implemented.");
}
statSync(_path: string, _cred: zenfs.Cred): zenfs.Stats {
throw new Error("Method not implemented.");
}
openFileSync(_path: string, _flag: string, _cred: zenfs.Cred): zenfs.File {
throw new Error("Method not implemented.");
}
createFileSync(_path: string, _flag: string, _mode: number, _cred: zenfs.Cred): zenfs.File {
throw new Error("Method not implemented.");
}
unlinkSync(_path: string, _cred: zenfs.Cred): void {
throw new Error("Method not implemented.");
}
rmdirSync(_path: string, _cred: zenfs.Cred): void {
throw new Error("Method not implemented.");
}
mkdirSync(_path: string, _mode: number, _cred: zenfs.Cred): void {
throw new Error("Method not implemented.");
}
readdirSync(_path: string, _cred: zenfs.Cred): string[] {
throw new Error("Method not implemented.");
}
linkSync(_srcpath: string, _dstpath: string, _cred: zenfs.Cred): void {
throw new Error("Method not implemented.");
}
syncSync(_path: string, _data: Uint8Array, _stats: Readonly<zenfs.Stats>): void {
throw new Error("Method not implemented.");
}
private _handles: Map<string, FileSystemHandle> = new Map();
/**
* @hidden
*/
_sync: FileSystem = InMemory.create({ name: "accessfs-cache" });
public constructor(handle: FileSystemDirectoryHandle) {
super();
this._handles.set("/", handle);
}
public metadata(): FileSystemMetadata {
return {
...super.metadata(),
name: "WebAccess",
};
}
public async sync(path: string, data: Uint8Array, stats: Stats): Promise<void> {
const currentStats = await this.stat(path);
if (stats.mtime !== currentStats!.mtime) {
await this.writeFile(path, data);
}
}
public async rename(oldPath: string, newPath: string): Promise<void> {
try {
const handle = await this.getHandle(oldPath);
if (handle instanceof FileSystemDirectoryHandle) {
const files = await this.readdir(oldPath);
await this.mkdir(newPath);
if (files.length === 0) {
await this.unlink(oldPath);
} else {
for (const file of files) {
await this.rename(join(oldPath, file), join(newPath, file));
await this.unlink(oldPath);
}
}
}
if (!(handle instanceof FileSystemFileHandle)) {
return;
}
const oldFile = await handle.getFile();
const destFolder = await this.getHandle(dirname(newPath));
if (!(destFolder instanceof FileSystemDirectoryHandle)) {
return;
}
const newFile = await destFolder.getFileHandle(basename(newPath), { create: true });
const writable = await newFile.createWritable();
await writable.write(await oldFile.arrayBuffer());
writable.close();
await this.unlink(oldPath);
for (const key of this._handles.keys()) {
if (key !== "/") {
this._handles.delete(key);
}
}
} catch (ex) {
throw convertException(ex as ConvertException, oldPath, "rename");
}
}
public async writeFile(fname: string, data: Uint8Array): Promise<void> {
const handle = await this.getHandle(dirname(fname));
if (!(handle instanceof FileSystemDirectoryHandle)) {
return;
}
const file = await handle.getFileHandle(basename(fname), { create: true });
const writable = await file.createWritable();
const copy = new Uint8Array(data.byteLength);
copy.set(data);
await writable.write(copy);
await writable.close();
}
public async createFile(path: string, flag: string): Promise<PreloadFile<this>> {
await this.writeFile(path, new Uint8Array());
return this.openFile(path, flag);
}
public async stat(path: string): Promise<Stats> {
const handle = await this.getHandle(path);
if (!handle) {
throw ErrnoError.With("ENOENT", path, "stat");
}
if (handle instanceof FileSystemDirectoryHandle) {
return new Stats({ mode: 0o777 | FileType.DIRECTORY, size: 4096 });
}
if (handle instanceof FileSystemFileHandle) {
const { lastModified, size } = await handle.getFile();
return new Stats({ mode: 0o777 | FileType.FILE, size, mtimeMs: lastModified });
}
throw new ErrnoError(Errno.EBADE, "Handle is not a directory or file", path, "stat");
}
public async openFile(path: string, flag: string): Promise<PreloadFile<this>> {
const handle = await this.getHandle(path);
if (!(handle instanceof FileSystemFileHandle)) {
throw ErrnoError.With("EISDIR", path, "openFile");
}
try {
const file = await handle.getFile();
const data = new Uint8Array(await file.arrayBuffer());
const stats = new Stats({ mode: 0o777 | FileType.FILE, size: file.size, mtimeMs: file.lastModified });
return new PreloadFile(this, path, flag, stats, data);
} catch (ex) {
throw convertException(ex as ConvertException, path, "openFile");
}
}
public async unlink(path: string): Promise<void> {
const handle = await this.getHandle(dirname(path));
if (handle instanceof FileSystemDirectoryHandle) {
try {
await handle.removeEntry(basename(path), { recursive: true });
} catch (ex) {
throw convertException(ex as ConvertException, path, "unlink");
}
}
}
public async link(srcpath: string): Promise<void> {
throw ErrnoError.With("ENOSYS", srcpath, "WebAccessFS.link");
}
public async rmdir(path: string): Promise<void> {
return this.unlink(path);
}
public async mkdir(path: string): Promise<void> {
const existingHandle = await this.getHandle(path);
if (existingHandle) {
throw ErrnoError.With("EEXIST", path, "mkdir");
}
const handle = await this.getHandle(dirname(path));
if (!(handle instanceof FileSystemDirectoryHandle)) {
throw ErrnoError.With("ENOTDIR", path, "mkdir");
}
await handle.getDirectoryHandle(basename(path), { create: true });
}
public async readdir(path: string): Promise<string[]> {
const handle = await this.getHandle(path);
if (!(handle instanceof FileSystemDirectoryHandle)) {
throw ErrnoError.With("ENOTDIR", path, "readdir");
}
const _keys: string[] = [];
for await (const key of handle.keys()) {
_keys.push(key);
}
return _keys;
}
protected async getHandle(path: string): Promise<FileSystemHandle> {
if (this._handles.has(path)) {
return this._handles.get(path)!;
}
let walked = "/";
for (const part of path.split("/").slice(1)) {
const handle = this._handles.get(walked);
if (!(handle instanceof FileSystemDirectoryHandle)) {
throw ErrnoError.With("ENOTDIR", walked, "getHandle");
}
walked = join(walked, part);
try {
const dirHandle = await handle.getDirectoryHandle(part);
this._handles.set(walked, dirHandle);
} catch (_ex) {
const ex = _ex as DOMException;
if (ex.name == "TypeMismatchError") {
try {
const fileHandle = await handle.getFileHandle(part);
this._handles.set(walked, fileHandle);
} catch (ex) {
convertException(ex as ConvertException, walked, "getHandle");
}
}
if (ex.name === "TypeError") {
throw new ErrnoError(Errno.ENOENT, ex.message, walked, "getHandle");
}
convertException(ex, walked, "getHandle");
}
}
return this._handles.get(path)!;
}
}
export const WebAccess = {
name: "WebAccess",
options: {
handle: {
type: "object",
required: true,
description: "The directory handle to use for the root",
},
},
isAvailable(): boolean {
return typeof FileSystemHandle === "function";
},
create(options: WebAccessOptions) {
return new WebAccessFS(options.handle);
},
} as const satisfies Backend<WebAccessFS, WebAccessOptions>;