Files
code-server/packages/vscode/src/protocol.ts
Asher 72bf4547d4 Getting the client to run (#12)
* Clean up workbench and integrate initialization data

* Uncomment Electron fill

* Run server & client together

* Clean up Electron fill & patch

* Bind fs methods

This makes them usable with the promise form:
`promisify(access)(...)`.

* Add space between tag and title to browser logger

* Add typescript dep to server and default __dirname for path

* Serve web files from server

* Adjust some dev options

* Rework workbench a bit to use a class and catch unexpected errors

* No mkdirs for now, fix util fill, use bash with exec

* More fills, make general client abstract

* More fills

* Fix cp.exec

* Fix require calls in fs fill being aliased

* Create data and storage dir

* Implement fs.watch

Using exec for now.

* Implement storage database fill

* Fix os export and homedir

* Add comment to use navigator.sendBeacon

* Fix fs callbacks (some args are optional)

* Make sure data directory exists when passing it back

* Update patch

* Target es5

* More fills

* Add APIs required for bootstrap-fork to function (#15)

* Add bootstrap-fork execution

* Add createConnection

* Bundle bootstrap-fork into cli

* Remove .node directory created from spdlog

* Fix npm start

* Remove unnecessary comment

* Add webpack-hot-middleware if CLI env is not set

* Add restarting to shared process

* Fix starting with yarn
2019-02-05 11:15:50 -06:00

130 lines
2.9 KiB
TypeScript

import { ChildProcess } from "child_process";
import { EventEmitter } from "events";
import { Protocol as VSProtocol } from "vs/base/parts/ipc/node/ipc.net";
import { LogLevel } from "vs/platform/log/common/log";
export interface ISharedProcessInitData {
socketPath: string;
serviceUrl: string;
logsDir: string;
windowId: number;
logLevel: LogLevel;
}
export interface IStdio {
onMessage: (cb: (data: string | Buffer) => void) => void;
sendMessage: (data: string | Buffer) => void;
onExit?: (cb: () => void) => void;
}
/**
* An implementation of net.Socket that uses stdio streams.
*/
class Socket {
private readonly emitter: EventEmitter;
public constructor(private readonly stdio: IStdio, ignoreFirst: boolean = false) {
this.emitter = new EventEmitter();
let first = true;
stdio.onMessage((data) => {
if (ignoreFirst && first) {
first = false;
return;
}
this.emitter.emit("data", Buffer.from(data.toString()));
});
if (stdio.onExit) {
stdio.onExit(() => {
this.emitter.emit("close");
});
}
}
public removeListener(event: string, listener: () => void): void {
this.emitter.removeListener(event, listener);
}
public once(event: string, listener: () => void): void {
this.emitter.once(event, listener);
}
public on(event: string, listener: () => void): void {
this.emitter.on(event, listener);
}
public end(): void {
// TODO: figure it out
}
public get destroyed(): boolean {
return false;
}
public write(data: string | Buffer): void {
this.stdio.sendMessage(data);
}
}
/**
* A protocol around a process, stream, or worker.
*/
export class Protocol extends VSProtocol {
public static fromProcess(childProcess: ChildProcess): Protocol {
return Protocol.fromStdio({
onMessage: (cb): void => {
childProcess.stdout.on("data", (data: string | Buffer) => {
cb(data);
});
},
sendMessage: (data): void => {
childProcess.stdin.write(data);
},
onExit: (cb): void => {
childProcess.on("exit", cb);
},
});
}
public static fromStream(
inStream: { on: (event: "data", cb: (b: string | Buffer) => void) => void },
outStream: { write: (b: string | Buffer) => void },
): Protocol {
return Protocol.fromStdio({
onMessage: (cb): void => {
inStream.on("data", (data) => {
cb(data);
});
},
sendMessage: (data): void => {
outStream.write(data);
},
});
}
public static fromWorker(worker: {
onmessage: (event: MessageEvent) => void;
postMessage: (data: string, origin?: string | string[]) => void;
}, ignoreFirst: boolean = false): Protocol {
return Protocol.fromStdio({
onMessage: (cb): void => {
worker.onmessage = (event: MessageEvent): void => {
cb(event.data);
};
},
sendMessage: (data): void => {
worker.postMessage(data.toString());
},
}, ignoreFirst);
}
public static fromStdio(stdio: IStdio, ignoreFirst?: boolean): Protocol {
return new Protocol(new Socket(stdio, ignoreFirst));
}
}