@typerighter/rpc-server 0.24.1 → 0.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +4 -1
- package/index.js +17 -5
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -12,9 +12,12 @@ export interface RpcServerOptions {
|
|
|
12
12
|
export class RpcServer extends EventEmitter {
|
|
13
13
|
constructor(options?: RpcServerOptions);
|
|
14
14
|
|
|
15
|
-
/** The
|
|
15
|
+
/** The host:port address the server is listening on, or undefined if not started */
|
|
16
16
|
get address(): string | undefined;
|
|
17
17
|
|
|
18
|
+
/** The host the server is bound to, or undefined if not started */
|
|
19
|
+
get host(): string | undefined;
|
|
20
|
+
|
|
18
21
|
/** The port the server is listening on, or undefined if not started */
|
|
19
22
|
get port(): number | undefined;
|
|
20
23
|
|
package/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { existsSync } from "node:fs";
|
|
2
2
|
import { execFileSync, spawn } from "node:child_process";
|
|
3
|
+
import { createInterface } from "node:readline";
|
|
3
4
|
import { EventEmitter } from "node:events";
|
|
4
5
|
import { binPath } from "./platform.js";
|
|
5
6
|
|
|
@@ -29,11 +30,11 @@ export class RpcServer extends EventEmitter {
|
|
|
29
30
|
super();
|
|
30
31
|
this._root = root ?? process.cwd();
|
|
31
32
|
this._addr = addr ?? "127.0.0.1";
|
|
32
|
-
// Port 0 lets the OS pick a free port, avoiding conflicts between dev and build
|
|
33
33
|
this._port = port ?? 0;
|
|
34
34
|
this._process = undefined;
|
|
35
35
|
this._listening = false;
|
|
36
36
|
this._resolvedAddress = undefined;
|
|
37
|
+
this._resolvedPort = undefined;
|
|
37
38
|
}
|
|
38
39
|
|
|
39
40
|
get address() {
|
|
@@ -41,9 +42,14 @@ export class RpcServer extends EventEmitter {
|
|
|
41
42
|
return this._resolvedAddress;
|
|
42
43
|
}
|
|
43
44
|
|
|
45
|
+
get host() {
|
|
46
|
+
if (!this._listening) return undefined;
|
|
47
|
+
return this._addr;
|
|
48
|
+
}
|
|
49
|
+
|
|
44
50
|
get port() {
|
|
45
51
|
if (!this._listening) return undefined;
|
|
46
|
-
return
|
|
52
|
+
return this._resolvedPort;
|
|
47
53
|
}
|
|
48
54
|
|
|
49
55
|
get listening() {
|
|
@@ -74,9 +80,14 @@ export class RpcServer extends EventEmitter {
|
|
|
74
80
|
return this;
|
|
75
81
|
}
|
|
76
82
|
|
|
77
|
-
// The server prints the
|
|
78
|
-
|
|
79
|
-
|
|
83
|
+
// The server prints addr:port as the first line to stdout
|
|
84
|
+
const reader = createInterface({ input: stdout });
|
|
85
|
+
reader.once("line", (line) => {
|
|
86
|
+
reader.close();
|
|
87
|
+
const address = line.trim();
|
|
88
|
+
this._resolvedAddress = address;
|
|
89
|
+
const colonIndex = address.lastIndexOf(":");
|
|
90
|
+
this._resolvedPort = Number(address.slice(colonIndex + 1));
|
|
80
91
|
this._listening = true;
|
|
81
92
|
this.emit("listening");
|
|
82
93
|
if (callback) callback();
|
|
@@ -90,6 +101,7 @@ export class RpcServer extends EventEmitter {
|
|
|
90
101
|
this._listening = false;
|
|
91
102
|
this._process = undefined;
|
|
92
103
|
this._resolvedAddress = undefined;
|
|
104
|
+
this._resolvedPort = undefined;
|
|
93
105
|
this.emit("close", code, signal);
|
|
94
106
|
});
|
|
95
107
|
|