@socket-mesh/client 18.1.0 → 18.1.3

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.
@@ -1,4 +1,4 @@
1
- import { AnyPacket, AnyResponse, FunctionReturnType, InvokeMethodOptions, InvokeServiceOptions, SocketTransport, SocketStatus } from "@socket-mesh/core";
1
+ import { FunctionReturnType, InvokeMethodOptions, InvokeServiceOptions, SocketTransport, SocketStatus } from "@socket-mesh/core";
2
2
  import ws from "isomorphic-ws";
3
3
  import { ClientAuthEngine } from "./client-auth-engine.js";
4
4
  import { ServerPrivateMap } from "./maps/server-map.js";
@@ -22,16 +22,16 @@ export declare class ClientTransport<T extends ClientMap> extends SocketTranspor
22
22
  set autoReconnect(value: Partial<AutoReconnectOptions> | boolean);
23
23
  connect(options?: ConnectOptions): void;
24
24
  get connectAttempts(): number;
25
- protected decode(data: string | ws.RawData): AnyPacket<SocketMapFromClient<T>> | AnyPacket<SocketMapFromClient<T>>[] | AnyResponse<SocketMapFromClient<T>> | AnyResponse<SocketMapFromClient<T>>[];
26
25
  disconnect(code?: number, reason?: string): void;
27
26
  private handshake;
28
- protected onOpen(): void;
29
- protected onPing(data: Buffer): void;
30
27
  protected onClose(code: number, reason?: Buffer): void;
28
+ protected onOpen(): void;
29
+ protected onPingPong(): void;
31
30
  get pendingReconnect(): boolean;
32
31
  get pingTimeoutMs(): number;
33
32
  set pingTimeoutMs(value: number);
34
33
  private resetReconnect;
34
+ send(data: Buffer | string): Promise<void>;
35
35
  setAuthorization(authToken: AuthToken): Promise<boolean>;
36
36
  setAuthorization(signedAuthToken: string, authToken?: AuthToken): Promise<boolean>;
37
37
  get status(): SocketStatus;
@@ -57,7 +57,7 @@ export class ClientTransport extends SocketTransport {
57
57
  }
58
58
  }
59
59
  if (this.status === 'closed') {
60
- this.webSocket = new ws.WebSocket(this._uri, this._wsOptions);
60
+ this.webSocket = new ws(this._uri, this._wsOptions);
61
61
  this.socket.emit('connecting', {});
62
62
  this._connectTimeoutRef = setTimeout(() => {
63
63
  this.disconnect(4007);
@@ -67,9 +67,6 @@ export class ClientTransport extends SocketTransport {
67
67
  get connectAttempts() {
68
68
  return this._connectAttempts;
69
69
  }
70
- decode(data) {
71
- return super.decode(data);
72
- }
73
70
  disconnect(code = 1000, reason) {
74
71
  if (code !== 4007) {
75
72
  this.resetReconnect();
@@ -87,40 +84,6 @@ export class ClientTransport extends SocketTransport {
87
84
  }
88
85
  return status;
89
86
  }
90
- onOpen() {
91
- super.onOpen();
92
- clearTimeout(this._connectTimeoutRef);
93
- this._connectTimeoutRef = null;
94
- this.resetReconnect();
95
- this.resetPingTimeout(this.isPingTimeoutDisabled ? false : this.pingTimeoutMs, 4000);
96
- let authError;
97
- this.handshake()
98
- .then(status => {
99
- this.id = status.id;
100
- this.pingTimeoutMs = status.pingTimeoutMs;
101
- if ('authToken' in status && status.authToken) {
102
- return this.setAuthorization(status.authToken);
103
- }
104
- if ('authError' in status) {
105
- authError = status.authError;
106
- }
107
- return this.changeToUnauthenticatedState();
108
- })
109
- .then(() => {
110
- this.setReadyStatus(this.pingTimeoutMs, authError);
111
- })
112
- .catch(err => {
113
- if (err.statusCode == null) {
114
- err.statusCode = 4003;
115
- }
116
- this.onError(err);
117
- this.disconnect(err.statusCode, err.toString());
118
- });
119
- }
120
- onPing(data) {
121
- this.resetPingTimeout(this.isPingTimeoutDisabled ? false : this.pingTimeoutMs, 4000);
122
- super.onPing(data);
123
- }
124
87
  onClose(code, reason) {
125
88
  const status = this.status;
126
89
  let reconnecting = false;
@@ -153,6 +116,41 @@ export class ClientTransport extends SocketTransport {
153
116
  this.onDisconnect(status, code, strReason);
154
117
  }
155
118
  }
119
+ onOpen() {
120
+ super.onOpen();
121
+ clearTimeout(this._connectTimeoutRef);
122
+ this._connectTimeoutRef = null;
123
+ this.resetReconnect();
124
+ this.resetPingTimeout(this.isPingTimeoutDisabled ? false : this.pingTimeoutMs, 4000);
125
+ let authError;
126
+ this.handshake()
127
+ .then(status => {
128
+ this.id = status.id;
129
+ this.pingTimeoutMs = status.pingTimeoutMs;
130
+ if ('authToken' in status && status.authToken) {
131
+ return this.setAuthorization(status.authToken);
132
+ }
133
+ if ('authError' in status) {
134
+ authError = status.authError;
135
+ }
136
+ return this.changeToUnauthenticatedState();
137
+ })
138
+ .then(() => {
139
+ this.setReadyStatus(this.pingTimeoutMs, authError);
140
+ })
141
+ .catch(err => {
142
+ if (err.statusCode == null) {
143
+ err.statusCode = 4003;
144
+ }
145
+ this.onError(err);
146
+ this.disconnect(err.statusCode, err.toString());
147
+ });
148
+ }
149
+ onPingPong() {
150
+ this.send('');
151
+ this.resetPingTimeout(this.isPingTimeoutDisabled ? false : this.pingTimeoutMs, 4000);
152
+ this.socket.emit('ping', {});
153
+ }
156
154
  get pendingReconnect() {
157
155
  return (this._pendingReconnectTimeout !== null);
158
156
  }
@@ -167,6 +165,9 @@ export class ClientTransport extends SocketTransport {
167
165
  this._pendingReconnectTimeout = null;
168
166
  this._connectAttempts = 0;
169
167
  }
168
+ async send(data) {
169
+ this.webSocket.send(data);
170
+ }
170
171
  async setAuthorization(signedAuthToken, authToken) {
171
172
  const wasAuthenticated = !!this.signedAuthToken;
172
173
  const changed = await super.setAuthorization(signedAuthToken, authToken);
@@ -220,7 +221,7 @@ export class ClientTransport extends SocketTransport {
220
221
  }
221
222
  }
222
223
  super.webSocket = value;
223
- if (this.webSocket) {
224
+ if (this.webSocket && this.webSocket.on) {
224
225
  // WebSockets will throw an error if they are closed before they are completely open.
225
226
  // We hook into these events to supress those errors and clean them up after a connection is established.
226
227
  function onOpenCloseError() {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@socket-mesh/client",
3
3
  "description": "A TCP socket pair for easily transmitting full messages without worrying about request size limits.",
4
- "version": "18.1.0",
4
+ "version": "18.1.3",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -18,7 +18,15 @@
18
18
  "deploy": "npm run build && node ../../scripts/publish.mjs",
19
19
  "test": "cross-env node --test --import=./run-test.js test/client-test.ts"
20
20
  },
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/socket-mesh/client-server.git"
24
+ },
25
+ "bugs": {
26
+ "url": "https://github.com/socket-mesh/client-server/labels/client"
27
+ },
21
28
  "devDependencies": {
29
+ "@kineticcafe/rollup-plugin-delete": "^3.0.0",
22
30
  "@rollup/plugin-alias": "^5.1.0",
23
31
  "@rollup/plugin-commonjs": "^26.0.1",
24
32
  "@rollup/plugin-node-resolve": "^15.2.3",
@@ -31,13 +39,14 @@
31
39
  "@types/ws": "^8.5.10",
32
40
  "jsonwebtoken": "^9.0.2",
33
41
  "rollup": "^4.18.1",
42
+ "rollup-plugin-dts": "^6.1.1",
34
43
  "tslib": "^2.6.3"
35
44
  },
36
45
  "dependencies": {
37
46
  "@socket-mesh/async-stream-emitter": "^7.1.2",
38
47
  "@socket-mesh/auth": "^2.2.0",
39
- "@socket-mesh/channels": "^6.1.0",
40
- "@socket-mesh/core": "^1.0.0",
48
+ "@socket-mesh/channels": "^6.1.1",
49
+ "@socket-mesh/core": "^1.0.2",
41
50
  "@socket-mesh/errors": "^3.2.0",
42
51
  "@socket-mesh/formatter": "^4.1.0",
43
52
  "buffer": "^5.2.1",