-
Notifications
You must be signed in to change notification settings - Fork 20
/
fsExtensionMain.ts
130 lines (118 loc) · 4.81 KB
/
fsExtensionMain.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { xhr } from 'request-light';
import { Uri, FileStat, FileType, workspace, ExtensionContext, FileSystemError } from 'vscode';
import { Entry, MemFileSystemProvider, File, Directory } from './fsProvider';
const SCHEME = 'vscode-test-web';
export function activate(context: ExtensionContext) {
const serverUri = context.extensionUri.with({ path: '/static/mount', query: undefined });
const serverBackedRootDirectory = new ServerBackedDirectory(serverUri, '');
const disposable = workspace.registerFileSystemProvider(SCHEME, new MemFileSystemProvider(SCHEME, serverBackedRootDirectory));
context.subscriptions.push(disposable);
console.log(`vscode-test-web-support fs provider registers for ${SCHEME}, initial content from ${serverUri.toString(/*skipEncoding*/ true)}`);
}
class ServerBackedFile implements File {
readonly type = FileType.File;
private _stats: Promise<FileStat> | undefined;
private _content: Promise<Uint8Array> | undefined;
constructor(private readonly _serverUri: Uri, public name: string) {
}
get stats(): Promise<FileStat> {
if (this._stats === undefined) {
this._stats = getStats(this._serverUri);
}
return this._stats;
}
set stats(stats: Promise<FileStat>) {
this._stats = stats;
}
get content(): Promise<Uint8Array> {
if (this._content === undefined) {
this._content = getContent(this._serverUri);
}
return this._content;
}
set content(content: Promise<Uint8Array>) {
this._content = content;
}
}
class ServerBackedDirectory implements Directory {
readonly type = FileType.Directory;
private _stats: Promise<FileStat> | undefined;
private _entries: Promise<Map<string, Entry>> | undefined;
constructor(private readonly _serverUri: Uri, public name: string) {
}
get stats(): Promise<FileStat> {
if (this._stats === undefined) {
this._stats = getStats(this._serverUri);
}
return this._stats;
}
set stats(stats: Promise<FileStat>) {
this._stats = stats;
}
get entries(): Promise<Map<string, Entry>> {
if (this._entries === undefined) {
this._entries = getEntries(this._serverUri);
}
return this._entries;
}
set entries(entries: Promise<Map<string, Entry>>) {
this._entries = entries;
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isEntry(e: any): e is Entry {
return e && (e.type === FileType.Directory || e.type === FileType.File) && typeof e.name === 'string' && e.name.length > 0;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isStat(e: any): e is FileStat {
return e && (e.type === FileType.Directory || e.type === FileType.File) && typeof e.ctime === 'number' && typeof e.mtime === 'number' && typeof e.size === 'number';
}
async function getEntries(serverUri: Uri): Promise<Map<string, Entry>> {
const url = serverUri.with({ query: 'readdir' }).toString(/*skipEncoding*/ true);
const response = await xhr({ url });
if (response.status === 200 && response.status <= 204) {
try {
const res = JSON.parse(response.responseText);
if (Array.isArray(res)) {
const entries = new Map();
for (const r of res) {
if (isEntry(r)) {
const childPath = Uri.joinPath(serverUri, r.name);
const newEntry: Entry = r.type === FileType.Directory ? new ServerBackedDirectory(childPath, r.name) : new ServerBackedFile(childPath, r.name);
entries.set(newEntry.name, newEntry);
}
}
return entries;
}
} catch {
// ignore
}
console.log(`Invalid server response format for ${url}.`);
} else {
console.log(`Invalid server response for ${url}. Status ${response.status}`);
}
return new Map();
}
async function getStats(serverUri: Uri): Promise<FileStat> {
const url = serverUri.with({ query: 'stat' }).toString(/*skipEncoding*/ true);
const response = await xhr({ url });
if (response.status === 200 && response.status <= 204) {
const res = JSON.parse(response.responseText);
if (isStat(res)) {
return res;
}
throw FileSystemError.FileNotFound(`Invalid server response for ${serverUri.toString(/*skipEncoding*/ true)}.`);
}
throw FileSystemError.FileNotFound(`Invalid server response for ${serverUri.toString(/*skipEncoding*/ true)}. Status ${response.status}.`);
}
async function getContent(serverUri: Uri): Promise<Uint8Array> {
const response = await xhr({ url: serverUri.toString(/*skipEncoding*/ true) });
if (response.status >= 200 && response.status <= 204) {
return response.body;
}
throw FileSystemError.FileNotFound(`Invalid server response for ${serverUri.toString(/*skipEncoding*/ true)}. Status ${response.status}.`);
}