-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmounts.ts
78 lines (69 loc) · 2.52 KB
/
mounts.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { IConfig } from './main';
import * as Koa from 'koa';
import * as kstatic from 'koa-static';
import * as kmount from 'koa-mount';
import * as Router from '@koa/router';
import { Dirent, promises as fs, Stats } from 'fs';
import * as path from 'path';
const mountPrefix = '/static/mount';
export const fsProviderExtensionPrefix = '/static/extensions/fs';
export const fsProviderFolderUri = 'vscode-test-web://mount/';
export function configureMounts(config: IConfig, app: Koa): void {
const folderMountPath = config.folderMountPath;
if (folderMountPath) {
console.log(`Serving local content ${folderMountPath} at ${mountPrefix}`);
app.use(fileOps(mountPrefix, folderMountPath));
app.use(kmount(mountPrefix, kstatic(folderMountPath, { hidden: true })));
app.use(kmount(fsProviderExtensionPrefix, kstatic(path.join(__dirname, '../../fs-provider'), { hidden: true })));
}
}
function fileOps(mountPrefix: string, folderMountPath: string): Router.Middleware {
const router = new Router();
router.get(`${mountPrefix}(/.*)?`, async (ctx, next) => {
if (ctx.query.stat !== undefined) {
const p = path.join(folderMountPath, ctx.path.substring(mountPrefix.length));
try {
const stats = await fs.stat(p);
ctx.body = {
type: getFileType(stats),
ctime: stats.ctime.getTime(),
mtime: stats.mtime.getTime(),
size: stats.size,
};
} catch (e) {
ctx.body = { error: (e as NodeJS.ErrnoException).code };
}
} else if (ctx.query.readdir !== undefined) {
const p = path.join(folderMountPath, ctx.path.substring(mountPrefix.length));
try {
const entries = await fs.readdir(p, { withFileTypes: true });
ctx.body = entries.map((d) => ({ name: d.name, type: getFileType(d) }));
} catch (e) {
ctx.body = { error: (e as NodeJS.ErrnoException).code };
}
} else {
return next();
}
});
return router.routes();
}
enum FileType {
Unknown = 0,
File = 1,
Directory = 2,
SymbolicLink = 64,
}
function getFileType(stats: Stats | Dirent) {
if (stats.isFile()) {
return FileType.File;
} else if (stats.isDirectory()) {
return FileType.Directory;
} else if (stats.isSymbolicLink()) {
return FileType.SymbolicLink;
}
return FileType.Unknown;
}