@typerighter/rpc-server 0.1.2 → 0.1.4

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
@@ -5,7 +5,7 @@ export interface RpcServerOptions {
5
5
  root?: string;
6
6
  /** Bind address (default: "127.0.0.1") */
7
7
  addr?: string;
8
- /** Bind port (default: 4747) */
8
+ /** Bind port (default: 0, OS picks a free port) */
9
9
  port?: number;
10
10
  }
11
11
 
@@ -27,6 +27,9 @@ export class RpcServer extends EventEmitter {
27
27
  /** Stop the server. Emits "close" when the process exits */
28
28
  close(): this;
29
29
 
30
+ /** Unref the child process so it does not prevent Node from exiting */
31
+ unref(): this;
32
+
30
33
  on(event: 'listening', listener: () => void): this;
31
34
  on(event: 'close', listener: (code: number | null, signal: string | null) => void): this;
32
35
  on(event: 'error', listener: (error: Error) => void): this;
package/index.js CHANGED
@@ -4,16 +4,18 @@ import { EventEmitter } from "node:events";
4
4
  import { binPath } from "./platform.js";
5
5
 
6
6
  function resolveBin() {
7
- const local = binPath();
8
- if (existsSync(local)) return local;
9
-
10
- // Fall back to system PATH
7
+ // Prefer user's system binary
11
8
  try {
12
- return execFileSync("which", ["typedown-rpc"], { encoding: "utf-8" }).trim();
9
+ const system = execFileSync("which", ["typedown-rpc"], { encoding: "utf-8" }).trim();
10
+ if (system) return system;
13
11
  } catch {
14
12
  // ignore
15
13
  }
16
14
 
15
+ // Fall back to downloaded binary
16
+ const local = binPath();
17
+ if (existsSync(local)) return local;
18
+
17
19
  throw new Error(
18
20
  `typedown-rpc binary not found. Run "pnpm install" to download it, ` +
19
21
  `install via Nix, or build manually with "cargo build --release -p typedown-server".`,
@@ -22,14 +24,13 @@ function resolveBin() {
22
24
 
23
25
  const bin = resolveBin();
24
26
 
25
- const DEFAULT_PORT = 4747;
26
-
27
27
  export class RpcServer extends EventEmitter {
28
28
  constructor({ root, addr, port } = {}) {
29
29
  super();
30
30
  this._root = root ?? process.cwd();
31
31
  this._addr = addr ?? "127.0.0.1";
32
- this._port = port ?? DEFAULT_PORT;
32
+ // Port 0 lets the OS pick a free port, avoiding conflicts between dev and build
33
+ this._port = port ?? 0;
33
34
  this._process = undefined;
34
35
  this._listening = false;
35
36
  this._resolvedAddress = undefined;
@@ -95,6 +96,14 @@ export class RpcServer extends EventEmitter {
95
96
  return this;
96
97
  }
97
98
 
99
+ // Unref the child process so it does not keep the Node event loop alive
100
+ unref() {
101
+ if (this._process) {
102
+ this._process.unref();
103
+ }
104
+ return this;
105
+ }
106
+
98
107
  close() {
99
108
  if (this._process) {
100
109
  this._process.kill();
package/install.js CHANGED
@@ -13,10 +13,6 @@ import {
13
13
 
14
14
  const bin = binPath();
15
15
 
16
- if (existsSync(bin)) {
17
- process.exit(0);
18
- }
19
-
20
16
  // In dev mode, we compile and `bin` will just automatically point to the artifact
21
17
  if (dev()) {
22
18
  console.log("[rpc-server] Development mode: building typedown-rpc with cargo");
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.1.2",
4
+ "version": "0.1.4",
5
5
  "description": "Typedown RPC server binary.",
6
6
  "repository": {
7
7
  "type": "git",