code-server/channel.ts

211 lines
7.7 KiB
TypeScript
Raw Normal View History

2019-06-29 01:37:23 +03:00
import * as path from "path";
2019-07-02 18:34:03 +03:00
import { getPathFromAmdModule } from "vs/base/common/amd";
import { VSBuffer } from "vs/base/common/buffer";
2019-06-29 01:37:23 +03:00
import { Emitter, Event } from "vs/base/common/event";
import { IDisposable } from "vs/base/common/lifecycle";
2019-06-29 01:37:23 +03:00
import { OS } from "vs/base/common/platform";
import { URI, UriComponents } from "vs/base/common/uri";
2019-07-02 18:34:03 +03:00
import { URITransformer, IRawURITransformer, transformOutgoingURIs } from "vs/base/common/uriIpc";
2019-06-29 01:37:23 +03:00
import { IServerChannel } from "vs/base/parts/ipc/common/ipc";
import { IDiagnosticInfo } from "vs/platform/diagnostics/common/diagnosticsService";
import { IEnvironmentService } from "vs/platform/environment/common/environment";
import { FileDeleteOptions, FileOverwriteOptions, FileType, IStat, IWatchOptions, FileOpenOptions } from "vs/platform/files/common/files";
import { ILogService } from "vs/platform/log/common/log";
2019-06-29 01:37:23 +03:00
import { IRemoteAgentEnvironment } from "vs/platform/remote/common/remoteAgentEnvironment";
import { DiskFileSystemProvider } from "vs/workbench/services/files/node/diskFileSystemProvider";
/**
* Extend the file provider to allow unwatching.
*/
class Watcher extends DiskFileSystemProvider {
public readonly watches = new Map<number, IDisposable>();
public dispose(): void {
this.watches.forEach((w) => w.dispose());
this.watches.clear();
super.dispose();
}
public _watch(req: number, resource: URI, opts: IWatchOptions): void {
this.watches.set(req, this.watch(resource, opts));
}
public unwatch(req: number): void {
this.watches.get(req)!.dispose();
this.watches.delete(req);
}
}
2019-06-29 01:37:23 +03:00
/**
* See: src/vs/platform/remote/common/remoteAgentFileSystemChannel.ts.
*/
export class FileProviderChannel implements IServerChannel {
private readonly provider: DiskFileSystemProvider;
private readonly watchers = new Map<string, Watcher>();
public constructor(private readonly logService: ILogService) {
this.provider = new DiskFileSystemProvider(this.logService);
}
2019-07-02 18:34:03 +03:00
public listen(context: any, event: string, args?: any): Event<any> {
2019-06-29 01:37:23 +03:00
switch (event) {
// This is where the actual file changes are sent. The watch method just
// adds things that will fire here. That means we have to split up
// watchers based on the session otherwise sessions would get events for
// other sessions. There is also no point in having the watcher unless
// something is listening. I'm not sure there is a different way to
// dispose, anyway.
2019-06-29 01:37:23 +03:00
case "filechange":
const session = args[0];
const emitter = new Emitter({
onFirstListenerAdd: () => {
const provider = new Watcher(this.logService);
this.watchers.set(session, provider);
2019-07-02 18:34:03 +03:00
const transformer = getUriTransformer(context.remoteAuthority);
provider.onDidChangeFile((events) => {
emitter.fire(events.map((event) => ({
...event,
2019-07-02 18:34:03 +03:00
resource: transformer.transformOutgoing(event.resource),
})));
});
provider.onDidErrorOccur((event) => emitter.fire(event));
},
onLastListenerRemove: () => {
this.watchers.get(session)!.dispose();
this.watchers.delete(session);
},
});
return emitter.event;
2019-06-29 01:37:23 +03:00
}
throw new Error(`Invalid listen "${event}"`);
}
public call(_: unknown, command: string, args?: any): Promise<any> {
switch (command) {
case "stat": return this.stat(args[0]);
case "open": return this.open(args[0], args[1]);
case "close": return this.close(args[0]);
case "read": return this.read(args[0], args[1], args[2]);
2019-06-29 01:37:23 +03:00
case "write": return this.write(args[0], args[1], args[2], args[3], args[4]);
case "delete": return this.delete(args[0], args[1]);
case "mkdir": return this.mkdir(args[0]);
case "readdir": return this.readdir(args[0]);
case "rename": return this.rename(args[0], args[1], args[2]);
case "copy": return this.copy(args[0], args[1], args[2]);
case "watch": return this.watch(args[0], args[1], args[2], args[3]);
case "unwatch": return this.unwatch(args[0], args[1]);
2019-06-29 01:37:23 +03:00
}
throw new Error(`Invalid call "${command}"`);
}
private async stat(resource: UriComponents): Promise<IStat> {
return this.provider.stat(URI.from(resource));
2019-06-29 01:37:23 +03:00
}
private async open(resource: UriComponents, opts: FileOpenOptions): Promise<number> {
return this.provider.open(URI.from(resource), opts);
2019-06-29 01:37:23 +03:00
}
private async close(fd: number): Promise<void> {
return this.provider.close(fd);
2019-06-29 01:37:23 +03:00
}
private async read(fd: number, pos: number, length: number): Promise<[VSBuffer, number]> {
const buffer = VSBuffer.alloc(length);
const bytesRead = await this.provider.read(fd, pos, buffer.buffer, 0, length);
return [buffer, bytesRead];
2019-06-29 01:37:23 +03:00
}
private write(fd: number, pos: number, buffer: VSBuffer, offset: number, length: number): Promise<number> {
return this.provider.write(fd, pos, buffer.buffer, offset, length);
2019-06-29 01:37:23 +03:00
}
private async delete(resource: UriComponents, opts: FileDeleteOptions): Promise<void> {
return this.provider.delete(URI.from(resource), opts);
2019-06-29 01:37:23 +03:00
}
private async mkdir(resource: UriComponents): Promise<void> {
return this.provider.mkdir(URI.from(resource));
2019-06-29 01:37:23 +03:00
}
private async readdir(resource: UriComponents): Promise<[string, FileType][]> {
return this.provider.readdir(URI.from(resource));
2019-06-29 01:37:23 +03:00
}
private async rename(resource: UriComponents, target: UriComponents, opts: FileOverwriteOptions): Promise<void> {
return this.provider.rename(URI.from(resource), URI.from(target), opts);
2019-06-29 01:37:23 +03:00
}
private copy(resource: UriComponents, target: UriComponents, opts: FileOverwriteOptions): Promise<void> {
return this.provider.copy(URI.from(resource), URI.from(target), opts);
2019-06-29 01:37:23 +03:00
}
private async watch(session: string, req: number, resource: UriComponents, opts: IWatchOptions): Promise<void> {
this.watchers.get(session)!._watch(req, URI.from(resource), opts);
2019-06-29 01:37:23 +03:00
}
private async unwatch(session: string, req: number): Promise<void> {
this.watchers.get(session)!.unwatch(req);
2019-06-29 01:37:23 +03:00
}
}
/**
* See: src/vs/workbench/services/remote/common/remoteAgentEnvironmentChannel.ts.
*/
export class ExtensionEnvironmentChannel implements IServerChannel {
public constructor(private readonly environment: IEnvironmentService) {}
2019-07-02 18:34:03 +03:00
public listen(_: unknown, event: string): Event<any> {
2019-06-29 01:37:23 +03:00
throw new Error(`Invalid listen "${event}"`);
}
2019-07-02 18:34:03 +03:00
public async call(context: any, command: string, _args?: any): Promise<any> {
2019-06-29 01:37:23 +03:00
switch (command) {
2019-07-02 18:34:03 +03:00
case "getEnvironmentData":
return transformOutgoingURIs(
await this.getEnvironmentData(),
getUriTransformer(context.remoteAuthority),
);
2019-06-29 01:37:23 +03:00
case "getDiagnosticInfo": return this.getDiagnosticInfo();
case "disableTelemetry": return this.disableTelemetry();
}
throw new Error(`Invalid call "${command}"`);
}
private async getEnvironmentData(): Promise<IRemoteAgentEnvironment> {
return {
pid: process.pid,
2019-07-02 18:34:03 +03:00
appRoot: URI.file(this.environment.appRoot),
appSettingsHome: this.environment.appSettingsHome,
settingsPath: this.environment.machineSettingsHome,
logsPath: URI.file(this.environment.logsPath),
extensionsPath: URI.file(this.environment.extensionsPath),
extensionHostLogsPath: URI.file(path.join(this.environment.logsPath, "extension-host")), // TODO
globalStorageHome: URI.file(this.environment.globalStorageHome),
userHome: URI.file(this.environment.userHome),
2019-06-29 01:37:23 +03:00
extensions: [], // TODO
os: OS,
};
}
private getDiagnosticInfo(): Promise<IDiagnosticInfo> {
throw new Error("not implemented");
}
private disableTelemetry(): Promise<void> {
throw new Error("not implemented");
}
}
2019-07-02 18:34:03 +03:00
export const uriTransformerPath = getPathFromAmdModule(require, "vs/server/uriTransformer");
export const getUriTransformer = (remoteAuthority: string): URITransformer => {
const rawURITransformerFactory = <any>require.__$__nodeRequire(uriTransformerPath);
const rawURITransformer = <IRawURITransformer>rawURITransformerFactory(remoteAuthority);
return new URITransformer(rawURITransformer);
};