-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhttp.ts
64 lines (61 loc) · 1.87 KB
/
http.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
import type { FileSystemDriver, FileSystemNode } from './index';
export class HTTPFS implements FileSystemDriver {
#root: string;
constructor(root: string) {
this.#root = root;
}
async resolveUri(path: string[]): Promise<string> {
const url = new URL(path.join('/'), this.#root);
url.hash = '';
url.search = '';
return url.href;
}
async access(path: string[]): Promise<boolean> {
const url = new URL(path.join('/'), this.#root);
url.hash = '';
url.search = '';
const response = await fetch(url.href, { method: 'HEAD', cache: 'force-cache' });
switch (response.status) {
case 200:
case 201:
return true;
case 404:
case 403:
return false;
default:
throw new Error(`EHTTP ${response.status} ${response.statusText}`);
}
}
async readDir(path: string[]): Promise<ReadableStream<FileSystemNode>> {
throw new Error('EACCESS');
}
async readFile(path: string[], offset = 0, length?: number): Promise<ReadableStream<Uint8Array>> {
if (path.length === 0)
throw new Error('EISDIR');
const url = new URL(path.join('/'), this.#root);
url.hash = '';
url.search = '';
const response = await fetch(url.href, { cache: 'force-cache' });
switch (response.status) {
case 200:
break;
case 404:
throw new Error('ENOTFOUND');
case 403:
throw new Error('EACCESS');
default:
throw new Error(`EHTTP ${response.status} ${response.statusText}`);
}
return response.body || new ReadableStream({
start(c) {
c.close();
}
});
}
async writeFile(path: string[], offset: 'before' | 'after' | 'override', create: boolean): Promise<WritableStream<Uint8Array>> {
throw new Error('EACCESS');
}
async deleteNode(path: string[], recursive: boolean): Promise<void> {
throw new Error('EACCESS');
}
}