Repository navigation
Expand file tree
/
Copy pathgithub1sfs.ts
More file actions
270 lines (240 loc) · 7.64 KB
/
Copy pathgithub1sfs.ts
File metadata and controls
270 lines (240 loc) · 7.64 KB
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
/**
* @file VSCode GitHub1sFs Provider
* @author netcon
*/
import {
workspace,
Disposable,
FileSystemProvider,
FileSystemError,
Event,
EventEmitter,
FileChangeEvent,
FileStat,
FileType,
Uri,
} from 'vscode';
import { noop, dirname, reuseable, hasValidToken } from './util';
import { parseUri, readGitHubDirectory, readGitHubFile } from './api';
import { apolloClient, githubObjectQuery } from './client';
import { toUint8Array as decodeBase64 } from 'js-base64';
const textEncoder = new TextEncoder();
// graph_sql has some problems now, disable it temporary
// https://github.com/conwnet/github1s/issues/90
const ENABLE_GRAPH_SQL: boolean = false;
export class File implements FileStat {
type: FileType;
ctime: number;
mtime: number;
size: number;
name: string;
sha: string;
data?: Uint8Array;
constructor(public uri: Uri, name: string, options?: any) {
this.type = FileType.File;
this.ctime = Date.now();
this.mtime = Date.now();
this.name = name;
this.sha = (options && ('sha' in options)) ? options.sha : '';
this.size = (options && ('size' in options)) ? options.size : 0;
this.data = (options && ('data' in options)) ? options.data : null;
}
}
export class Directory implements FileStat {
type: FileType;
ctime: number;
mtime: number;
size: number;
sha: string;
name: string;
entries: Map<string, File | Directory> | null;
constructor(public uri: Uri, name: string, options?: any) {
this.type = FileType.Directory;
this.ctime = Date.now();
this.mtime = Date.now();
this.size = 0;
this.name = name;
this.entries = null;
this.sha = (options && ('sha' in options)) ? options.sha : '';
this.size = (options && ('size' in options)) ? options.size : 0;
}
getNameTypePairs () {
return Array.from(this.entries?.values() || [])
.map((item: Entry) => [item.name, item instanceof Directory ? FileType.Directory : FileType.File]);
}
}
export type Entry = File | Directory;
/**
* This funtion must be used for only GraphQL output
*
* @param entries the entries of a GitObject
* @param uri the parent URI
*/
const entriesToMap = (entries, uri) => {
if (!entries) {
return null;
}
const map = new Map<string, Entry>();
entries.forEach((item: any) => {
const fileType: FileType = item.type === 'tree' ? FileType.Directory : FileType.File;
const entry = fileType === FileType.Directory
? new Directory(uri, item.name, { sha: item.oid })
: new File(uri, item.name, { sha: item.oid, size: item.object?.byteSize, data: textEncoder.encode(item?.object?.text) });
map.set(item.name, entry);
});
return map;
};
export class GitHub1sFS implements FileSystemProvider, Disposable {
static scheme = 'github1s';
private readonly disposable: Disposable;
private _emitter = new EventEmitter<FileChangeEvent[]>();
private root: Directory = null;
onDidChangeFile: Event<FileChangeEvent[]> = this._emitter.event;
constructor() {
this.disposable = Disposable.from(
workspace.registerFileSystemProvider(GitHub1sFS.scheme, this, { isCaseSensitive: true, isReadonly: true }),
);
}
dispose() {
this.disposable?.dispose();
}
// --- lookup
private async _lookup(uri: Uri, silent: false): Promise<Entry>;
private async _lookup(uri: Uri, silent: boolean): Promise<Entry | undefined>;
private async _lookup(uri: Uri, silent: boolean): Promise<Entry | undefined> {
let parts = uri.path.split('/').filter(Boolean);
let entry: Entry = this.root || (this.root = new Directory(uri.with({ path: '/' }), ''));
for (const part of parts) {
let child: Entry | undefined;
if (entry instanceof Directory) {
if (entry.entries === null) {
await this.readDirectory(Uri.joinPath(entry.uri, entry.name));
}
child = entry.entries.get(part);
}
if (!child) {
if (!silent) {
throw FileSystemError.FileNotFound(uri);
} else {
return undefined;
}
}
entry = child;
}
return entry;
}
private async _lookupAsDirectory(uri: Uri, silent: boolean): Promise<Directory> {
const entry = await this._lookup(uri, silent);
if (entry instanceof Directory) {
return entry;
}
if (!silent) {
throw FileSystemError.FileNotADirectory(uri);
}
}
private async _lookupAsFile(uri: Uri, silent: boolean): Promise<File> {
const entry = await this._lookup(uri, silent);
if (entry instanceof File) {
return entry;
}
if (!silent) {
throw FileSystemError.FileIsADirectory(uri);
}
}
private _lookupParentDirectory(uri: Uri): Promise<Directory> {
const _dirname = uri.with({ path: dirname(uri.path) });
return this._lookupAsDirectory(_dirname, false);
}
watch(uri: Uri, options: { recursive: boolean; excludes: string[]; }): Disposable {
return new Disposable(noop);
}
stat(uri: Uri): FileStat | Thenable<FileStat> {
return this._lookup(uri, false);
}
readDirectory = reuseable((uri: Uri): [string, FileType][] | Thenable<[string, FileType][]> => {
if (!uri.authority) {
throw FileSystemError.FileNotFound(uri);
}
return this._lookupAsDirectory(uri, false).then(parent => {
if (parent.entries !== null) {
return parent.getNameTypePairs();
}
if (hasValidToken() && ENABLE_GRAPH_SQL) {
const state = parseUri(uri);
const directory = state.path.substring(1);
return apolloClient.query({
query: githubObjectQuery, variables: {
owner: state.owner,
repo: state.repo,
expression: `${state.branch}:${directory}`
}
})
.then((response) => {
const entries = response.data?.repository?.object?.entries;
if (!entries) {
throw FileSystemError.FileNotADirectory(uri);
}
parent.entries = entriesToMap(entries, uri);
return parent.getNameTypePairs();
});
}
return readGitHubDirectory(uri).then(data => {
parent.entries = new Map<string, Entry>();
return data.tree.map((item: any) => {
const fileType: FileType = item.type === 'tree' ? FileType.Directory : FileType.File;
parent.entries.set(
item.path, fileType === FileType.Directory
? new Directory(uri, item.path, { sha: item.sha })
: new File(uri, item.path, { sha: item.sha, size: item.size })
);
return [item.path, fileType];
});
});
});
}, (uri: Uri) => uri.toString());
readFile = reuseable((uri: Uri): Uint8Array | Thenable<Uint8Array> => {
if (!uri.authority) {
throw FileSystemError.FileNotFound(uri);
}
return this._lookupAsFile(uri, false).then(file => {
if (file.data !== null) {
return file.data;
}
if (hasValidToken() && ENABLE_GRAPH_SQL) {
const state = parseUri(uri);
const path = state.path.substring(1);
const directory = dirname(path);
return apolloClient.query({
query: githubObjectQuery, variables: {
owner: state.owner,
repo: state.repo,
expression: `${state.branch}:${directory}`
}
})
.then((response) => {
const entry = (response.data?.repository?.object?.entries || []).find(x => x.path === path);
return textEncoder.encode(entry?.object?.text);
});
}
return readGitHubFile(uri, file.sha).then(blob => {
file.data = decodeBase64(blob.content);
return file.data;
});
});
}, (uri: Uri) => uri.toString());
createDirectory(uri: Uri): void | Thenable<void> {
return Promise.resolve();
}
writeFile(uri: Uri, content: Uint8Array, options: { create: boolean; overwrite: boolean; }): void | Thenable<void> {
return Promise.resolve();
}
delete(uri: Uri, options: { recursive: boolean; }): void | Thenable<void> {
return Promise.resolve();
}
rename(oldUri: Uri, newUri: Uri, options: { overwrite: boolean; }): void | Thenable<void> {
return Promise.resolve();
}
copy?(source: Uri, destination: Uri, options: { overwrite: boolean; }): void | Thenable<void> {
return Promise.resolve();
}
}