Skip to content

Commit 1e0d995

Browse files
committed
Handle all floating promises
1 parent ed5b8e4 commit 1e0d995

File tree

17 files changed

+118
-80
lines changed

17 files changed

+118
-80
lines changed

packages/protocol/src/browser/modules/child_process.ts

+19-8
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@ import { ClientProxy } from "../../common/proxy";
66
import { ChildProcessModuleProxy, ChildProcessProxy, ChildProcessProxies } from "../../node/modules/child_process";
77
import { Readable, Writable } from "./stream";
88

9+
// tslint:disable completed-docs
10+
911
export class ChildProcess extends ClientProxy<ChildProcessProxy> implements cp.ChildProcess {
1012
public readonly stdin: stream.Writable;
1113
public readonly stdout: stream.Readable;
1214
public readonly stderr: stream.Readable;
13-
public readonly stdio: [stream.Writable, stream.Readable, stream.Readable];
15+
public readonly stdio: [
16+
stream.Writable | null,
17+
stream.Readable | null,
18+
stream.Readable | null,
19+
stream.Readable | stream.Writable | null | undefined,
20+
stream.Readable | stream.Writable | null | undefined
21+
];
1422

1523
private _connected: boolean = false;
1624
private _killed: boolean = false;
@@ -21,12 +29,12 @@ export class ChildProcess extends ClientProxy<ChildProcessProxy> implements cp.C
2129
this.stdin = new Writable(proxyPromises.then((p) => p.stdin!));
2230
this.stdout = new Readable(proxyPromises.then((p) => p.stdout!));
2331
this.stderr = new Readable(proxyPromises.then((p) => p.stderr!));
24-
this.stdio = [this.stdin, this.stdout, this.stderr];
32+
this.stdio = [this.stdin, this.stdout, this.stderr, undefined, undefined];
2533

26-
this.proxy.getPid().then((pid) => {
34+
this.catch(this.proxy.getPid().then((pid) => {
2735
this._pid = pid;
2836
this._connected = true;
29-
});
37+
}));
3038
this.on("disconnect", () => this._connected = false);
3139
this.on("exit", () => {
3240
this._connected = false;
@@ -48,19 +56,19 @@ export class ChildProcess extends ClientProxy<ChildProcessProxy> implements cp.C
4856

4957
public kill(): void {
5058
this._killed = true;
51-
this.proxy.kill();
59+
this.catch(this.proxy.kill());
5260
}
5361

5462
public disconnect(): void {
55-
this.proxy.disconnect();
63+
this.catch(this.proxy.disconnect());
5664
}
5765

5866
public ref(): void {
59-
this.proxy.ref();
67+
this.catch(this.proxy.ref());
6068
}
6169

6270
public unref(): void {
63-
this.proxy.unref();
71+
this.catch(this.proxy.unref());
6472
}
6573

6674
public send(
@@ -88,6 +96,9 @@ export class ChildProcess extends ClientProxy<ChildProcessProxy> implements cp.C
8896
return true; // Always true since we can't get this synchronously.
8997
}
9098

99+
/**
100+
* Exit and close the process when disconnected.
101+
*/
91102
protected handleDisconnect(): void {
92103
this.emit("exit", 1);
93104
this.emit("close");

packages/protocol/src/browser/modules/fs.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { FsModuleProxy, Stats as IStats, WatcherProxy, WriteStreamProxy } from "
66
import { Writable } from "./stream";
77

88
// tslint:disable no-any
9+
// tslint:disable completed-docs
910

1011
class StatBatch extends Batch<IStats, { path: fs.PathLike }> {
1112
public constructor(private readonly proxy: FsModuleProxy) {
@@ -39,7 +40,7 @@ class ReaddirBatch extends Batch<Buffer[] | fs.Dirent[] | string[], { path: fs.P
3940

4041
class Watcher extends ClientProxy<WatcherProxy> implements fs.FSWatcher {
4142
public close(): void {
42-
this.proxy.close();
43+
this.catch(this.proxy.close());
4344
}
4445

4546
protected handleDisconnect(): void {
@@ -57,7 +58,7 @@ class WriteStream extends Writable<WriteStreamProxy> implements fs.WriteStream {
5758
}
5859

5960
public close(): void {
60-
this.proxy.close();
61+
this.catch(this.proxy.close());
6162
}
6263
}
6364

packages/protocol/src/browser/modules/net.ts

+13-19
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { ClientProxy } from "../../common/proxy";
44
import { NetModuleProxy, NetServerProxy, NetSocketProxy } from "../../node/modules/net";
55
import { Duplex } from "./stream";
66

7+
// tslint:disable completed-docs
8+
79
export class Socket extends Duplex<NetSocketProxy> implements net.Socket {
810
private _connecting: boolean = false;
911
private _destroyed: boolean = false;
@@ -29,9 +31,8 @@ export class Socket extends Duplex<NetSocketProxy> implements net.Socket {
2931
if (callback) {
3032
this.on("connect", callback as () => void);
3133
}
32-
this.proxy.connect(options, host);
3334

34-
return this;
35+
return this.catch(this.proxy.connect(options, host));
3536
}
3637

3738
// tslint:disable-next-line no-any
@@ -117,11 +118,11 @@ export class Socket extends Duplex<NetSocketProxy> implements net.Socket {
117118
}
118119

119120
public unref(): void {
120-
this.proxy.unref();
121+
this.catch(this.proxy.unref());
121122
}
122123

123124
public ref(): void {
124-
this.proxy.ref();
125+
this.catch(this.proxy.ref());
125126
}
126127
}
127128

@@ -133,14 +134,14 @@ export class Server extends ClientProxy<NetServerProxy> implements net.Server {
133134
public constructor(proxyPromise: Promise<NetServerProxy> | NetServerProxy) {
134135
super(proxyPromise);
135136

136-
this.proxy.onConnection((socketProxy) => {
137+
this.catch(this.proxy.onConnection((socketProxy) => {
137138
const socket = new Socket(socketProxy);
138139
const socketId = this.socketId++;
139140
this.sockets.set(socketId, socket);
140-
socket.on("error", () => this.sockets.delete(socketId))
141-
socket.on("close", () => this.sockets.delete(socketId))
141+
socket.on("error", () => this.sockets.delete(socketId));
142+
socket.on("close", () => this.sockets.delete(socketId));
142143
this.emit("connection", socket);
143-
});
144+
}));
144145

145146
this.on("listening", () => this._listening = true);
146147
this.on("error", () => this._listening = false);
@@ -160,9 +161,7 @@ export class Server extends ClientProxy<NetServerProxy> implements net.Server {
160161
this.on("listening", callback as () => void);
161162
}
162163

163-
this.proxy.listen(handle, hostname, backlog);
164-
165-
return this;
164+
return this.catch(this.proxy.listen(handle, hostname, backlog));
166165
}
167166

168167
public get connections(): number {
@@ -186,21 +185,16 @@ export class Server extends ClientProxy<NetServerProxy> implements net.Server {
186185
if (callback) {
187186
this.on("close", callback);
188187
}
189-
this.proxy.close();
190188

191-
return this;
189+
return this.catch(this.proxy.close());
192190
}
193191

194192
public ref(): this {
195-
this.proxy.ref();
196-
197-
return this;
193+
return this.catch(this.proxy.ref());
198194
}
199195

200196
public unref(): this {
201-
this.proxy.unref();
202-
203-
return this;
197+
return this.catch(this.proxy.unref());
204198
}
205199

206200
public getConnections(cb: (error: Error | null, count: number) => void): void {

packages/protocol/src/browser/modules/node-pty.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import * as pty from "node-pty";
22
import { ClientProxy } from "../../common/proxy";
33
import { NodePtyModuleProxy, NodePtyProcessProxy } from "../../node/modules/node-pty";
44

5+
// tslint:disable completed-docs
6+
57
export class NodePtyProcess extends ClientProxy<NodePtyProcessProxy> implements pty.IPty {
68
private _pid = -1;
79
private _process = "";
@@ -16,10 +18,10 @@ export class NodePtyProcess extends ClientProxy<NodePtyProcessProxy> implements
1618
this.on("process", (process) => this._process = process);
1719
}
1820

19-
protected initialize(proxyPromise: Promise<NodePtyProcessProxy>) {
21+
protected initialize(proxyPromise: Promise<NodePtyProcessProxy>): void {
2022
super.initialize(proxyPromise);
21-
this.proxy.getPid().then((pid) => this._pid = pid);
22-
this.proxy.getProcess().then((process) => this._process = process);
23+
this.catch(this.proxy.getPid().then((p) => this._pid = p));
24+
this.catch(this.proxy.getProcess().then((p) => this._process = p));
2325
}
2426

2527
public get pid(): number {
@@ -31,15 +33,15 @@ export class NodePtyProcess extends ClientProxy<NodePtyProcessProxy> implements
3133
}
3234

3335
public resize(columns: number, rows: number): void {
34-
this.proxy.resize(columns, rows);
36+
this.catch(this.proxy.resize(columns, rows));
3537
}
3638

3739
public write(data: string): void {
38-
this.proxy.write(data);
40+
this.catch(this.proxy.write(data));
3941
}
4042

4143
public kill(signal?: string): void {
42-
this.proxy.kill(signal);
44+
this.catch(this.proxy.kill(signal));
4345
}
4446

4547
protected handleDisconnect(): void {

packages/protocol/src/browser/modules/spdlog.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import * as spdlog from "spdlog";
22
import { ClientProxy } from "../../common/proxy";
33
import { RotatingLoggerProxy, SpdlogModuleProxy } from "../../node/modules/spdlog";
44

5+
// tslint:disable completed-docs
6+
57
class RotatingLogger extends ClientProxy<RotatingLoggerProxy> implements spdlog.RotatingLogger {
68
public constructor(
79
private readonly moduleProxy: SpdlogModuleProxy,
@@ -13,16 +15,16 @@ class RotatingLogger extends ClientProxy<RotatingLoggerProxy> implements spdlog.
1315
super(moduleProxy.createLogger(name, filename, filesize, filecount));
1416
}
1517

16-
public async trace (message: string): Promise<void> { this.proxy.trace(message); }
17-
public async debug (message: string): Promise<void> { this.proxy.debug(message); }
18-
public async info (message: string): Promise<void> { this.proxy.info(message); }
19-
public async warn (message: string): Promise<void> { this.proxy.warn(message); }
20-
public async error (message: string): Promise<void> { this.proxy.error(message); }
21-
public async critical (message: string): Promise<void> { this.proxy.critical(message); }
22-
public async setLevel (level: number): Promise<void> { this.proxy.setLevel(level); }
23-
public async clearFormatters (): Promise<void> { this.proxy.clearFormatters(); }
24-
public async flush (): Promise<void> { this.proxy.flush(); }
25-
public async drop (): Promise<void> { this.proxy.drop(); }
18+
public trace (message: string): void { this.catch(this.proxy.trace(message)); }
19+
public debug (message: string): void { this.catch(this.proxy.debug(message)); }
20+
public info (message: string): void { this.catch(this.proxy.info(message)); }
21+
public warn (message: string): void { this.catch(this.proxy.warn(message)); }
22+
public error (message: string): void { this.catch(this.proxy.error(message)); }
23+
public critical (message: string): void { this.catch(this.proxy.critical(message)); }
24+
public setLevel (level: number): void { this.catch(this.proxy.setLevel(level)); }
25+
public clearFormatters (): void { this.catch(this.proxy.clearFormatters()); }
26+
public flush (): void { this.catch(this.proxy.flush()); }
27+
public drop (): void { this.catch(this.proxy.drop()); }
2628

2729
protected handleDisconnect(): void {
2830
this.initialize(this.moduleProxy.createLogger(this.name, this.filename, this.filesize, this.filecount));
@@ -40,7 +42,7 @@ export class SpdlogModule {
4042
};
4143
}
4244

43-
public setAsyncMode = (bufferSize: number, flushInterval: number): void => {
44-
this.proxy.setAsyncMode(bufferSize, flushInterval);
45+
public setAsyncMode = (bufferSize: number, flushInterval: number): Promise<void> => {
46+
return this.proxy.setAsyncMode(bufferSize, flushInterval);
4547
}
4648
}

packages/protocol/src/browser/modules/stream.ts

+7-11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { callbackify } from "util";
33
import { ClientProxy } from "../../common/proxy";
44
import { DuplexProxy, IReadableProxy, WritableProxy } from "../../node/modules/stream";
55

6+
// tslint:disable completed-docs
7+
68
export class Writable<T extends WritableProxy = WritableProxy> extends ClientProxy<T> implements stream.Writable {
79
public get writable(): boolean {
810
throw new Error("not implemented");
@@ -41,13 +43,11 @@ export class Writable<T extends WritableProxy = WritableProxy> extends ClientPro
4143
}
4244

4345
public destroy(): void {
44-
this.proxy.destroy();
46+
this.catch(this.proxy.destroy());
4547
}
4648

4749
public setDefaultEncoding(encoding: string): this {
48-
this.proxy.setDefaultEncoding(encoding);
49-
50-
return this;
50+
return this.catch(this.proxy.setDefaultEncoding(encoding));
5151
}
5252

5353
// tslint:disable-next-line no-any
@@ -151,13 +151,11 @@ export class Readable<T extends IReadableProxy = IReadableProxy> extends ClientP
151151
}
152152

153153
public destroy(): void {
154-
this.proxy.destroy();
154+
this.catch(this.proxy.destroy());
155155
}
156156

157157
public setEncoding(encoding: string): this {
158-
this.proxy.setEncoding(encoding);
159-
160-
return this;
158+
return this.catch(this.proxy.setEncoding(encoding));
161159
}
162160

163161
protected handleDisconnect(): void {
@@ -236,9 +234,7 @@ export class Duplex<T extends DuplexProxy = DuplexProxy> extends Writable<T> imp
236234
}
237235

238236
public setEncoding(encoding: string): this {
239-
this.proxy.setEncoding(encoding);
240-
241-
return this;
237+
return this.catch(this.proxy.setEncoding(encoding));
242238
}
243239

244240
protected handleDisconnect(): void {

packages/protocol/src/browser/modules/trash.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as trash from "trash";
22
import { TrashModuleProxy } from "../../node/modules/trash";
33

4+
// tslint:disable completed-docs
5+
46
export class TrashModule {
57
public constructor(private readonly proxy: TrashModuleProxy) {}
68

packages/protocol/src/common/proxy.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,32 @@ export abstract class ClientProxy<T extends ServerProxy> extends EventEmitter {
6161
return this._proxy;
6262
}
6363

64+
/**
65+
* Initialize the proxy by unpromisifying if necessary and binding to its
66+
* events.
67+
*/
6468
protected initialize(proxyPromise: Promise<T> | T): void {
6569
this._proxy = isPromise(proxyPromise) ? unpromisify(proxyPromise) : proxyPromise;
6670
if (this.bindEvents) {
67-
this.proxy.onEvent((event, ...args): void => {
71+
this.catch(this.proxy.onEvent((event, ...args): void => {
6872
this.emit(event, ...args);
69-
});
73+
}));
7074
}
7175
}
7276

77+
/**
78+
* Perform necessary cleanup on disconnect (or reconnect).
79+
*/
7380
protected abstract handleDisconnect(): void;
81+
82+
/**
83+
* Emit an error event if the promise errors.
84+
*/
85+
protected catch(promise: Promise<any>): this {
86+
promise.catch((e) => this.emit("error", e));
87+
88+
return this;
89+
}
7490
}
7591

7692
/**

0 commit comments

Comments
 (0)