@inline-chat/realtime-sdk 0.0.7 → 0.0.9

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.
@@ -65,7 +65,7 @@ export declare class ProtocolClient {
65
65
  private trySendPendingRpcRequest;
66
66
  }
67
67
  export declare class ProtocolClientError extends Error {
68
- constructor(code: "not-authorized" | "not-connected" | "rpc-error" | "stopped" | "timeout", details?: {
68
+ constructor(code: "not-authorized" | "not-connected" | "rpc-error" | "stopped" | "timeout" | "invalid-rpc-method", details?: {
69
69
  code?: number;
70
70
  message?: string;
71
71
  });
@@ -3,6 +3,11 @@ import { AsyncChannel } from "../utils/async-channel.js";
3
3
  import { PingPongService } from "./ping-pong.js";
4
4
  const emptyRpcInput = { oneofKind: undefined };
5
5
  const defaultRpcTimeoutMs = 30_000;
6
+ const assertValidRpcMethod = (method) => {
7
+ if (typeof method !== "number" || !Number.isInteger(method) || method <= 0) {
8
+ throw new ProtocolClientError("invalid-rpc-method", { message: `Invalid rpc method: ${String(method)}` });
9
+ }
10
+ };
6
11
  export class ProtocolClient {
7
12
  events = new AsyncChannel();
8
13
  transport;
@@ -52,6 +57,7 @@ export class ProtocolClient {
52
57
  }
53
58
  async sendRpc(method, input = emptyRpcInput) {
54
59
  this.ensureOpenForRpc();
60
+ assertValidRpcMethod(method);
55
61
  const message = this.wrapMessage({
56
62
  oneofKind: "rpcCall",
57
63
  rpcCall: { method, input },
@@ -60,6 +66,7 @@ export class ProtocolClient {
60
66
  return message.id;
61
67
  }
62
68
  async callRpc(method, input = emptyRpcInput, options) {
69
+ assertValidRpcMethod(method);
63
70
  const message = this.wrapMessage({
64
71
  oneofKind: "rpcCall",
65
72
  rpcCall: { method, input },
@@ -24,6 +24,7 @@ export declare class WebSocketTransport implements Transport {
24
24
  skipDelay?: boolean;
25
25
  }): Promise<void>;
26
26
  private cleanUpPreviousConnection;
27
+ private closeSocketSafely;
27
28
  private getReconnectionDelaySeconds;
28
29
  private openConnection;
29
30
  private connectionDidOpen;
@@ -55,9 +55,30 @@ export class WebSocketTransport {
55
55
  this.reconnectionTimer = null;
56
56
  }
57
57
  if (this.socket) {
58
- this.socket.removeAllListeners();
59
- this.socket.close();
58
+ const socket = this.socket;
60
59
  this.socket = null;
60
+ socket.removeAllListeners();
61
+ this.closeSocketSafely(socket);
62
+ }
63
+ }
64
+ closeSocketSafely(socket) {
65
+ try {
66
+ if (socket.readyState === WebSocket.CONNECTING) {
67
+ socket.terminate();
68
+ return;
69
+ }
70
+ if (socket.readyState === WebSocket.OPEN || socket.readyState === WebSocket.CLOSING) {
71
+ socket.close();
72
+ }
73
+ }
74
+ catch (error) {
75
+ this.log.warn?.("WebSocket cleanup close failed", error);
76
+ try {
77
+ socket.terminate();
78
+ }
79
+ catch {
80
+ // no-op
81
+ }
61
82
  }
62
83
  }
63
84
  getReconnectionDelaySeconds(attemptNo) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inline-chat/realtime-sdk",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "files": [
@@ -20,7 +20,7 @@
20
20
  "test": "vitest run --coverage"
21
21
  },
22
22
  "dependencies": {
23
- "@inline-chat/protocol": "^0.0.2",
23
+ "@inline-chat/protocol": "^0.0.4",
24
24
  "ws": "^8.18.3"
25
25
  },
26
26
  "devDependencies": {