forked from coder/code-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.ts
More file actions
91 lines (75 loc) · 2.54 KB
/
Copy pathcommand.ts
File metadata and controls
91 lines (75 loc) · 2.54 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
import * as events from "events";
import * as stream from "stream";
import { SendableConnection } from "../common/connection";
import { ShutdownSessionMessage, ClientMessage, SessionOutputMessage, WriteToSessionMessage, ResizeSessionTTYMessage, TTYDimensions as ProtoTTYDimensions } from "../proto";
export interface TTYDimensions {
readonly columns: number;
readonly rows: number;
}
export interface SpawnOptions {
cwd?: string;
env?: { readonly [key: string]: string };
tty?: TTYDimensions;
}
export interface ChildProcess {
readonly stdin: stream.Writable;
readonly stdout: stream.Readable;
readonly stderr: stream.Readable;
readonly killed?: boolean;
readonly pid: number | undefined;
kill(signal?: string): void;
send(message: string | Uint8Array): void;
on(event: "error", listener: (err: Error) => void): void;
on(event: "exit", listener: (code: number, signal: string) => void): void;
resize?(dimensions: TTYDimensions): void;
}
export class ServerProcess extends events.EventEmitter implements ChildProcess {
public readonly stdin = new stream.Writable();
public readonly stdout = new stream.Readable({ read: () => true });
public readonly stderr = new stream.Readable({ read: () => true });
public pid: number | undefined;
private _killed: boolean = false;
public constructor(
private readonly connection: SendableConnection,
private readonly id: number,
private readonly hasTty: boolean = false,
) {
super();
if (!this.hasTty) {
delete this.resize;
}
}
public get killed(): boolean {
return this._killed;
}
public kill(signal?: string): void {
const kill = new ShutdownSessionMessage();
kill.setId(this.id);
if (signal) {
kill.setSignal(signal);
}
const client = new ClientMessage();
client.setShutdownSession(kill);
this.connection.send(client.serializeBinary());
this._killed = true;
}
public send(message: string | Uint8Array): void {
const send = new WriteToSessionMessage();
send.setId(this.id);
send.setData(typeof message === "string" ? new TextEncoder().encode(message) : message);
const client = new ClientMessage();
client.setWriteToSession(send);
this.connection.send(client.serializeBinary());
}
public resize(dimensions: TTYDimensions) {
const resize = new ResizeSessionTTYMessage();
resize.setId(this.id);
const tty = new ProtoTTYDimensions();
tty.setHeight(dimensions.rows);
tty.setWidth(dimensions.columns);
resize.setTtyDimensions(tty);
const client = new ClientMessage();
client.setResizeSessionTty(resize);
this.connection.send(client.serializeBinary());
}
}