@typerighter/rpc-server 0.24.1 → 0.26.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 CHANGED
@@ -12,9 +12,12 @@ export interface RpcServerOptions {
12
12
  export class RpcServer extends EventEmitter {
13
13
  constructor(options?: RpcServerOptions);
14
14
 
15
- /** The ws:// address the server is listening on, or undefined if not started */
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 Number(new URL(this._resolvedAddress).port);
52
+ return this._resolvedPort;
47
53
  }
48
54
 
49
55
  get listening() {
@@ -64,6 +70,8 @@ export class RpcServer extends EventEmitter {
64
70
  TYPEDOWN_RPC_PORT: String(this._port),
65
71
  },
66
72
  stdio: ["ignore", "pipe", "inherit"],
73
+ // Detach so the server can save its cache after Node exits
74
+ detached: true,
67
75
  });
68
76
 
69
77
  this._process = child;
@@ -74,9 +82,14 @@ export class RpcServer extends EventEmitter {
74
82
  return this;
75
83
  }
76
84
 
77
- // The server prints the ws:// address when ready
78
- stdout.once("data", (data) => {
79
- this._resolvedAddress = data.toString().trim();
85
+ // The server prints addr:port as the first line to stdout
86
+ const reader = createInterface({ input: stdout });
87
+ reader.once("line", (line) => {
88
+ reader.close();
89
+ const address = line.trim();
90
+ this._resolvedAddress = address;
91
+ const colonIndex = address.lastIndexOf(":");
92
+ this._resolvedPort = Number(address.slice(colonIndex + 1));
80
93
  this._listening = true;
81
94
  this.emit("listening");
82
95
  if (callback) callback();
@@ -90,6 +103,7 @@ export class RpcServer extends EventEmitter {
90
103
  this._listening = false;
91
104
  this._process = undefined;
92
105
  this._resolvedAddress = undefined;
106
+ this._resolvedPort = undefined;
93
107
  this.emit("close", code, signal);
94
108
  });
95
109
 
@@ -104,10 +118,9 @@ export class RpcServer extends EventEmitter {
104
118
  return this;
105
119
  }
106
120
 
121
+ // Let the server detect the socket close and save cache in the background
122
+ // The process is already unref'd so it won't block Node from exiting
107
123
  close() {
108
- if (this._process) {
109
- this._process.kill();
110
- }
111
124
  return this;
112
125
  }
113
126
  }
package/install.js CHANGED
@@ -18,7 +18,7 @@ const bin = binPath();
18
18
  if (dev()) {
19
19
  console.log("[rpc-server] Development mode: building typedown-rpc with cargo");
20
20
  try {
21
- execFileSync("cargo", ["build", "-p", "typedown-server"], {
21
+ execFileSync("cargo", ["build", "--release", "-p", "typedown-server"], {
22
22
  cwd: repoRoot(),
23
23
  stdio: "inherit",
24
24
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@typerighter/rpc-server",
4
- "version": "0.24.1",
4
+ "version": "0.26.0",
5
5
  "description": "Typedown RPC server binary.",
6
6
  "repository": {
7
7
  "type": "git",
package/platform.js CHANGED
@@ -74,7 +74,7 @@ export function repoRoot() {
74
74
  export function binPath() {
75
75
  const ext = process.platform === "win32" ? ".exe" : "";
76
76
  if (isDev) {
77
- return path.join(repoRoot(), "target", "debug", `typedown-rpc${ext}`);
77
+ return path.join(repoRoot(), "target", "release", `typedown-rpc${ext}`);
78
78
  }
79
79
  return path.join(path.dirname(import.meta.filename), "bin", `typedown-rpc${ext}`);
80
80
  }