@socket-mesh/client 18.1.6 → 19.0.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.
@@ -1,14 +1,15 @@
1
1
  import { ChannelDetails, ChannelMap, Channels, ChannelsOptions } from '@socket-mesh/channels';
2
- import { MethodMap, PrivateMethodMap, PublicMethodMap, ServiceMap } from '@socket-mesh/core';
2
+ import { SocketTransport } from '@socket-mesh/core';
3
3
  import { ClientTransport } from './client-transport.js';
4
+ import { ServerPrivateMap } from './maps/server-map.js';
4
5
  export interface ClientChannelsOptions extends ChannelsOptions {
5
6
  autoSubscribeOnConnect?: boolean;
6
7
  }
7
- export declare class ClientChannels<TChannel extends ChannelMap, TIncoming extends MethodMap, TService extends ServiceMap, TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TState extends object> extends Channels<TChannel> {
8
+ export declare class ClientChannels<TChannel extends ChannelMap, TState extends object = {}> extends Channels<TChannel> {
8
9
  protected _preparingPendingSubscriptions: boolean;
9
- protected readonly _transport: ClientTransport<TIncoming, TService, TOutgoing, TPrivateOutgoing, TState>;
10
+ protected readonly _transport: SocketTransport<{}, {}, TState, {}, {}, ServerPrivateMap>;
10
11
  autoSubscribeOnConnect: boolean;
11
- constructor(transport: ClientTransport<TIncoming, TService, TOutgoing, TPrivateOutgoing, TState>, options?: ClientChannelsOptions);
12
+ constructor(transport: ClientTransport<TState>, options?: ClientChannelsOptions);
12
13
  invokePublish<U extends keyof TChannel & string>(channelName: keyof TChannel & string, data: TChannel[U]): Promise<void>;
13
14
  private processPendingSubscriptions;
14
15
  private suspendSubscriptions;
@@ -1,4 +1,5 @@
1
1
  import { Channels } from '@socket-mesh/channels';
2
+ import { toError } from '@socket-mesh/core';
2
3
  export class ClientChannels extends Channels {
3
4
  _preparingPendingSubscriptions;
4
5
  _transport;
@@ -111,13 +112,14 @@ export class ClientChannels extends Channels {
111
112
  delete channel.subscribeAbort;
112
113
  this.triggerChannelSubscribe(channel, subscriptionOptions);
113
114
  }).catch((err) => {
114
- if (err.name === 'BadConnectionError') {
115
+ const error = toError(err);
116
+ if (error.name === 'BadConnectionError') {
115
117
  // In case of a failed connection, keep the subscription
116
118
  // as pending; it will try again on reconnect.
117
119
  return;
118
120
  }
119
- if (err.name !== 'AbortError') {
120
- this.triggerChannelSubscribeFail(err, channel, subscriptionOptions);
121
+ if (error.name !== 'AbortError') {
122
+ this.triggerChannelSubscribeFail(error, channel, subscriptionOptions);
121
123
  }
122
124
  delete channel.subscribePromise;
123
125
  delete channel.subscribeAbort;
@@ -140,7 +142,7 @@ export class ClientChannels extends Channels {
140
142
  // the operation on the server side.
141
143
  this._transport
142
144
  .transmit('#unsubscribe', decoratedChannelName)
143
- .catch((err) => { });
145
+ .catch((_) => { });
144
146
  }
145
147
  }
146
148
  unsubscribe(channelName) {
@@ -1,15 +1,13 @@
1
- import { MethodMap, PrivateMethodMap, PublicMethodMap, ServiceMap, SocketOptions } from '@socket-mesh/core';
1
+ import { BaseSocketOptions } from '@socket-mesh/core';
2
2
  import ws from 'isomorphic-ws';
3
3
  import { ClientAuthEngine, LocalStorageAuthEngineOptions } from './client-auth-engine.js';
4
- import { ClientPrivateMap } from './maps/client-map.js';
5
- import { ServerPrivateMap } from './maps/server-map.js';
6
4
  export interface AutoReconnectOptions {
7
5
  initialDelay: number;
8
6
  maxDelayMs: number;
9
7
  multiplier: number;
10
8
  randomness: number;
11
9
  }
12
- export interface ClientSocketOptions<TOutgoing extends PublicMethodMap = {}, TService extends ServiceMap = {}, TIncoming extends MethodMap = {}, TPrivateOutgoing extends PrivateMethodMap = {}, TState extends object = {}> extends SocketOptions<TIncoming & ClientPrivateMap, TOutgoing, TPrivateOutgoing & ServerPrivateMap, TService, TState> {
10
+ export interface ClientSocketOptions<TState extends object = {}> extends BaseSocketOptions<TState> {
13
11
  address: string | URL;
14
12
  authEngine?: ClientAuthEngine | LocalStorageAuthEngineOptions | null;
15
13
  autoConnect?: boolean;
@@ -24,4 +22,5 @@ export interface ConnectOptions {
24
22
  connectTimeoutMs?: number;
25
23
  wsOptions?: ws.ClientOptions;
26
24
  }
27
- export declare function parseClientOptions<TIncoming extends MethodMap, TService extends ServiceMap, TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TState extends object>(options: ClientSocketOptions<TOutgoing, TService, TIncoming, TPrivateOutgoing, TState> | string | URL): ClientSocketOptions<TOutgoing, TService, TIncoming, TPrivateOutgoing, TState>;
25
+ export declare function parseAutoReconnectOptions(value?: boolean | Partial<AutoReconnectOptions>): AutoReconnectOptions | false;
26
+ export declare function parseClientOptions<TState extends object>(options: ClientSocketOptions<TState> | string | URL): ClientSocketOptions<TState>;
@@ -1,3 +1,15 @@
1
+ export function parseAutoReconnectOptions(value) {
2
+ if (value) {
3
+ return {
4
+ initialDelay: 10000,
5
+ maxDelayMs: 60000,
6
+ multiplier: 1.5,
7
+ randomness: 10000,
8
+ ...(value === true ? {} : value)
9
+ };
10
+ }
11
+ return false;
12
+ }
1
13
  export function parseClientOptions(options) {
2
14
  if (typeof options === 'string' || 'pathname' in options) {
3
15
  options = { address: options };
@@ -1,15 +1,16 @@
1
1
  import { SignedAuthToken } from '@socket-mesh/auth';
2
2
  import { ChannelMap } from '@socket-mesh/channels';
3
- import { MethodMap, PrivateMethodMap, PublicMethodMap, ServiceMap, Socket } from '@socket-mesh/core';
3
+ import { AuthenticateEvent, AuthStateChangeEvent, BadAuthTokenEvent, BaseSocket, CloseEvent, ConnectEvent, ConnectingEvent, DeauthenticateEvent, DisconnectEvent, ErrorEvent, FunctionReturnType, InvokeMethodOptions, InvokeServiceOptions, MessageEvent, PingEvent, PongEvent, PrivateMethodMap, PublicMethodMap, RemoveAuthTokenEvent, RequestEvent, ResponseEvent, ServiceMap, ServiceMethodName, ServiceName, Socket, TypedRequestEvent, TypedResponseEvent, TypedSocketEvent } from '@socket-mesh/core';
4
+ import { DemuxedConsumableStream, StreamEvent } from '@socket-mesh/stream-demux';
4
5
  import { ClientChannels } from './client-channels.js';
5
6
  import { AutoReconnectOptions, ClientSocketOptions, ConnectOptions } from './client-socket-options.js';
6
7
  import { ClientPrivateMap } from './maps/client-map.js';
7
8
  import { ServerPrivateMap } from './maps/server-map.js';
8
- export declare class ClientSocket<TOutgoing extends PublicMethodMap = {}, TChannel extends ChannelMap = ChannelMap, TService extends ServiceMap = {}, TState extends object = {}, TIncoming extends MethodMap = {}, TPrivateOutgoing extends PrivateMethodMap = {}> extends Socket<TIncoming & ClientPrivateMap, TOutgoing, TPrivateOutgoing & ServerPrivateMap, TService, TState> {
9
+ export declare class ClientSocket<TIncoming extends PublicMethodMap = {}, TOutgoing extends PublicMethodMap = {}, TChannel extends ChannelMap = ChannelMap, TService extends ServiceMap = {}, TState extends object = {}, TPrivateOutgoing extends PrivateMethodMap = {}> extends BaseSocket<TState> implements Socket<TIncoming & ClientPrivateMap, TOutgoing, TState, TService, {}, TPrivateOutgoing & ServerPrivateMap> {
9
10
  private readonly _clientTransport;
10
- readonly channels: ClientChannels<TChannel, TIncoming, TService, TOutgoing, TPrivateOutgoing, TState>;
11
+ readonly channels: ClientChannels<TChannel, TState>;
11
12
  constructor(address: string | URL);
12
- constructor(options: ClientSocketOptions<TOutgoing, TService, TIncoming, TPrivateOutgoing, TState>);
13
+ constructor(options: ClientSocketOptions<TState>);
13
14
  authenticate(signedAuthToken: SignedAuthToken): Promise<void>;
14
15
  get autoReconnect(): AutoReconnectOptions | false;
15
16
  set autoReconnect(value: boolean | Partial<AutoReconnectOptions>);
@@ -17,11 +18,55 @@ export declare class ClientSocket<TOutgoing extends PublicMethodMap = {}, TChann
17
18
  get connectTimeoutMs(): number;
18
19
  set connectTimeoutMs(timeoutMs: number);
19
20
  deauthenticate(): Promise<boolean>;
21
+ emit(event: 'request', data: TypedRequestEvent<TIncoming & ClientPrivateMap, TService>): void;
22
+ emit(event: 'response', data: TypedResponseEvent<TOutgoing, TPrivateOutgoing & ServerPrivateMap, TService>): void;
23
+ emit(event: 'authStateChange', data: AuthStateChangeEvent): void;
24
+ emit(event: 'authenticate', data: AuthenticateEvent): void;
25
+ emit(event: 'badAuthToken', data: BadAuthTokenEvent): void;
26
+ emit(event: 'close', data: CloseEvent): void;
27
+ emit(event: 'connect', data: ConnectEvent): void;
28
+ emit(event: 'connectAbort', data: DisconnectEvent): void;
29
+ emit(event: 'connecting', data: ConnectingEvent): void;
30
+ emit(event: 'deauthenticate', data: DeauthenticateEvent): void;
31
+ emit(event: 'disconnect', data: DisconnectEvent): void;
32
+ emit(event: 'end'): void;
33
+ emit(event: 'error', data: ErrorEvent): void;
34
+ emit(event: 'message', data: MessageEvent): void;
35
+ emit(event: 'ping', data: PingEvent): void;
36
+ emit(event: 'pong', data: PongEvent): void;
37
+ emit(event: 'removeAuthToken', data: RemoveAuthTokenEvent): void;
38
+ emit(event: 'request', data: RequestEvent): void;
39
+ emit(event: 'response', data: ResponseEvent): void;
40
+ invoke<TMethod extends keyof TOutgoing & string>(method: TMethod, arg?: Parameters<TOutgoing[TMethod]>[0]): Promise<FunctionReturnType<TOutgoing[TMethod]>>;
41
+ invoke<TServiceName extends ServiceName<TService>, TMethod extends ServiceMethodName<TService, TServiceName>>(options: [TServiceName, TMethod, (false | number)?], arg?: Parameters<TService[TServiceName][TMethod]>[0]): Promise<FunctionReturnType<TService[TServiceName][TMethod]>>;
42
+ invoke<TServiceName extends ServiceName<TService>, TMethod extends ServiceMethodName<TService, TServiceName>>(options: InvokeServiceOptions<TService, TServiceName, TMethod>, arg?: Parameters<TService[TServiceName][TMethod]>[0]): Promise<FunctionReturnType<TService[TServiceName][TMethod]>>;
43
+ invoke<TMethod extends keyof TOutgoing & string>(options: InvokeMethodOptions<TOutgoing, TMethod>, arg?: Parameters<TOutgoing[TMethod]>[0]): Promise<FunctionReturnType<TOutgoing[TMethod]>>;
20
44
  get isPingTimeoutDisabled(): boolean;
21
45
  set isPingTimeoutDisabled(isDisabled: boolean);
46
+ listen(): DemuxedConsumableStream<StreamEvent<TypedSocketEvent<TIncoming & ClientPrivateMap, TOutgoing, TPrivateOutgoing & ServerPrivateMap, TService>>>;
47
+ listen(event: 'authStateChange'): DemuxedConsumableStream<AuthStateChangeEvent>;
48
+ listen(event: 'authenticate'): DemuxedConsumableStream<AuthenticateEvent>;
49
+ listen(event: 'badAuthToken'): DemuxedConsumableStream<BadAuthTokenEvent>;
50
+ listen(event: 'close'): DemuxedConsumableStream<CloseEvent>;
51
+ listen(event: 'connect'): DemuxedConsumableStream<ConnectEvent>;
52
+ listen(event: 'connectAbort'): DemuxedConsumableStream<DisconnectEvent>;
53
+ listen(event: 'connecting'): DemuxedConsumableStream<ConnectingEvent>;
54
+ listen(event: 'deauthenticate'): DemuxedConsumableStream<DeauthenticateEvent>;
55
+ listen(event: 'disconnect'): DemuxedConsumableStream<DisconnectEvent>;
56
+ listen(event: 'end'): DemuxedConsumableStream<void>;
57
+ listen(event: 'error'): DemuxedConsumableStream<ErrorEvent>;
58
+ listen(event: 'message'): DemuxedConsumableStream<MessageEvent>;
59
+ listen(event: 'ping'): DemuxedConsumableStream<PingEvent>;
60
+ listen(event: 'pong'): DemuxedConsumableStream<PongEvent>;
61
+ listen(event: 'removeAuthToken'): DemuxedConsumableStream<RemoveAuthTokenEvent>;
62
+ listen(event: 'request'): DemuxedConsumableStream<TypedRequestEvent<TIncoming & ClientPrivateMap, TService>>;
63
+ listen(event: 'response'): DemuxedConsumableStream<TypedResponseEvent<TOutgoing, TPrivateOutgoing & ServerPrivateMap, TService>>;
64
+ listen<U extends TypedSocketEvent<TIncoming & ClientPrivateMap, TOutgoing, TPrivateOutgoing & ServerPrivateMap, TService>, V = U>(event: string): DemuxedConsumableStream<V>;
22
65
  get pingTimeoutMs(): number;
23
66
  set pingTimeoutMs(timeoutMs: number);
24
67
  reconnect(code?: number, reason?: string): void;
68
+ transmit<TMethod extends keyof TOutgoing & string>(method: TMethod, arg?: Parameters<TOutgoing[TMethod]>[0]): Promise<void>;
69
+ transmit<TServiceName extends ServiceName<TService>, TMethod extends ServiceMethodName<TService, TServiceName>>(options: [TServiceName, TMethod], arg?: Parameters<TService[TServiceName][TMethod]>[0]): Promise<void>;
25
70
  get type(): 'client';
26
71
  get uri(): URL;
27
72
  }
@@ -1,4 +1,4 @@
1
- import { Socket, wait } from '@socket-mesh/core';
1
+ import { BaseSocket, toError, wait } from '@socket-mesh/core';
2
2
  import { hydrateError } from '@socket-mesh/errors';
3
3
  import { ClientChannels } from './client-channels.js';
4
4
  import { parseClientOptions } from './client-socket-options.js';
@@ -7,7 +7,7 @@ import { kickOutHandler } from './handlers/kickout.js';
7
7
  import { publishHandler } from './handlers/publish.js';
8
8
  import { removeAuthTokenHandler } from './handlers/remove-auth-token.js';
9
9
  import { setAuthTokenHandler } from './handlers/set-auth-token.js';
10
- export class ClientSocket extends Socket {
10
+ export class ClientSocket extends BaseSocket {
11
11
  _clientTransport;
12
12
  channels;
13
13
  constructor(options) {
@@ -35,14 +35,15 @@ export class ClientSocket extends Socket {
35
35
  await wait(0);
36
36
  }
37
37
  catch (err) {
38
- if (err.name !== 'BadConnectionError' && err.name !== 'TimeoutError') {
38
+ const error = toError(err);
39
+ if (error.name !== 'BadConnectionError' && error.name !== 'TimeoutError') {
39
40
  // In case of a bad/closed connection or a timeout, we maintain the last
40
41
  // known auth state since those errors don't mean that the token is invalid.
41
42
  await this._clientTransport.changeToUnauthenticatedState();
42
43
  // In order for the events to trigger we need to wait for the next tick.
43
44
  await wait(0);
44
45
  }
45
- throw hydrateError(err);
46
+ throw hydrateError(error);
46
47
  }
47
48
  }
48
49
  get autoReconnect() {
@@ -67,7 +68,7 @@ export class ClientSocket extends Socket {
67
68
  oldAuthToken = await this._clientTransport.authEngine.removeToken();
68
69
  }
69
70
  catch (err) {
70
- this._clientTransport.onError(err);
71
+ this._clientTransport.onError(toError(err));
71
72
  return;
72
73
  }
73
74
  if (oldAuthToken) {
@@ -79,12 +80,21 @@ export class ClientSocket extends Socket {
79
80
  }
80
81
  return await super.deauthenticate();
81
82
  }
83
+ emit(event, data) {
84
+ super.emit(event, data);
85
+ }
86
+ invoke(methodOptions, arg) {
87
+ return super.invoke(methodOptions, arg);
88
+ }
82
89
  get isPingTimeoutDisabled() {
83
90
  return this._clientTransport.isPingTimeoutDisabled;
84
91
  }
85
92
  set isPingTimeoutDisabled(isDisabled) {
86
93
  this._clientTransport.isPingTimeoutDisabled = isDisabled;
87
94
  }
95
+ listen(event) {
96
+ return super.listen(event ?? '');
97
+ }
88
98
  get pingTimeoutMs() {
89
99
  return this._clientTransport.pingTimeoutMs;
90
100
  }
@@ -95,6 +105,9 @@ export class ClientSocket extends Socket {
95
105
  this.disconnect(code, reason);
96
106
  this.connect();
97
107
  }
108
+ transmit(serviceAndMethod, arg) {
109
+ return super.transmit(serviceAndMethod, arg);
110
+ }
98
111
  get type() {
99
112
  return this._clientTransport.type;
100
113
  }
@@ -1,11 +1,9 @@
1
1
  import { AuthToken } from '@socket-mesh/auth';
2
- import { FunctionReturnType, InvokeMethodOptions, InvokeServiceOptions, MethodMap, PrivateMethodMap, PublicMethodMap, ServiceMap, SocketStatus, SocketTransport } from '@socket-mesh/core';
2
+ import { BaseSocketTransport, InvokeMethodOptions, InvokeServiceOptions, SocketStatus, SocketTransport } from '@socket-mesh/core';
3
3
  import ws from 'isomorphic-ws';
4
4
  import { ClientAuthEngine } from './client-auth-engine.js';
5
5
  import { AutoReconnectOptions, ClientSocketOptions, ConnectOptions } from './client-socket-options.js';
6
- import { ClientPrivateMap } from './maps/client-map.js';
7
- import { ServerPrivateMap } from './maps/server-map.js';
8
- export declare class ClientTransport<TIncoming extends MethodMap, TService extends ServiceMap, TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TState extends object> extends SocketTransport<TIncoming & ClientPrivateMap, TOutgoing, TPrivateOutgoing & ServerPrivateMap, TService, TState> {
6
+ export declare class ClientTransport<TState extends object = {}> extends BaseSocketTransport<TState> implements SocketTransport<{}, {}, TState> {
9
7
  private _autoReconnect;
10
8
  private _connectAttempts;
11
9
  private _connectTimeoutRef;
@@ -17,19 +15,14 @@ export declare class ClientTransport<TIncoming extends MethodMap, TService exten
17
15
  connectTimeoutMs: number;
18
16
  isPingTimeoutDisabled: boolean;
19
17
  type: 'client';
20
- constructor(options: ClientSocketOptions<TOutgoing, TService, TIncoming, TPrivateOutgoing, TState>);
18
+ constructor(options: ClientSocketOptions<TState>);
21
19
  get autoReconnect(): AutoReconnectOptions | false;
22
20
  set autoReconnect(value: boolean | Partial<AutoReconnectOptions>);
23
21
  connect(options?: ConnectOptions): void;
24
22
  get connectAttempts(): number;
25
23
  disconnect(code?: number, reason?: string): void;
26
24
  private handshake;
27
- invoke<TMethod extends keyof TOutgoing>(method: TMethod, arg?: Parameters<TOutgoing[TMethod]>[0]): [Promise<FunctionReturnType<TOutgoing[TMethod]>>, () => void];
28
- invoke<TServiceName extends keyof TService, TMethod extends keyof TService[TServiceName]>(options: [TServiceName, TMethod, (false | number)?], arg?: Parameters<TService[TServiceName][TMethod]>[0]): [Promise<FunctionReturnType<TService[TServiceName][TMethod]>>, () => void];
29
- invoke<TServiceName extends keyof TService, TMethod extends keyof TService[TServiceName]>(options: InvokeServiceOptions<TService, TServiceName, TMethod>, arg?: Parameters<TService[TServiceName][TMethod]>[0]): [Promise<FunctionReturnType<TService[TServiceName][TMethod]>>, () => void];
30
- invoke<TMethod extends keyof TOutgoing>(options: InvokeMethodOptions<TOutgoing, TMethod>, arg?: Parameters<TOutgoing[TMethod]>[0]): [Promise<FunctionReturnType<TOutgoing[TMethod]>>, () => void];
31
- invoke<TMethod extends keyof (TPrivateOutgoing & ServerPrivateMap)>(method: TMethod, arg: Parameters<(TPrivateOutgoing & ServerPrivateMap)[TMethod]>[0]): [Promise<FunctionReturnType<(TPrivateOutgoing & ServerPrivateMap)[TMethod]>>, () => void];
32
- invoke<TMethod extends keyof (TPrivateOutgoing & ServerPrivateMap)>(options: InvokeMethodOptions<(TPrivateOutgoing & ServerPrivateMap), TMethod>, arg?: Parameters<(TPrivateOutgoing & ServerPrivateMap)[TMethod]>[0]): [Promise<FunctionReturnType<(TPrivateOutgoing & ServerPrivateMap)[TMethod]>>, () => void];
25
+ invoke(methodOptions: [string, string, (false | number)?] | InvokeMethodOptions | InvokeServiceOptions | string, arg?: unknown): [Promise<unknown>, () => void];
33
26
  protected onClose(code: number, reason?: Buffer): void;
34
27
  protected onOpen(): void;
35
28
  protected onPingPong(): void;
@@ -40,9 +33,7 @@ export declare class ClientTransport<TIncoming extends MethodMap, TService exten
40
33
  send(data: Buffer | string): Promise<void>;
41
34
  setAuthorization(signedAuthToken: string, authToken?: AuthToken): Promise<boolean>;
42
35
  get status(): SocketStatus;
43
- transmit<TMethod extends keyof TOutgoing>(method: TMethod, arg?: Parameters<TOutgoing[TMethod]>[0]): Promise<void>;
44
- transmit<TServiceName extends keyof TService, TMethod extends keyof TService[TServiceName]>(options: [TServiceName, TMethod], arg?: Parameters<TService[TServiceName][TMethod]>[0]): Promise<void>;
45
- transmit<TMethod extends keyof (TPrivateOutgoing & ServerPrivateMap)>(method: TMethod, arg?: Parameters<(TPrivateOutgoing & ServerPrivateMap)[TMethod]>[0]): Promise<void>;
36
+ transmit(serviceAndMethod: [string, string] | string, arg?: unknown): Promise<void>;
46
37
  private tryReconnect;
47
38
  get uri(): URL;
48
39
  protected get webSocket(): null | ws.WebSocket;
@@ -1,11 +1,12 @@
1
- import { SocketTransport } from '@socket-mesh/core';
1
+ import { BaseSocketTransport, toError } from '@socket-mesh/core';
2
2
  import { hydrateError, SocketClosedError, SocketProtocolErrorStatuses } from '@socket-mesh/errors';
3
3
  import ws from 'isomorphic-ws';
4
4
  import { isAuthEngine, LocalStorageAuthEngine } from './client-auth-engine.js';
5
- export class ClientTransport extends SocketTransport {
5
+ import { parseAutoReconnectOptions } from './client-socket-options.js';
6
+ export class ClientTransport extends BaseSocketTransport {
6
7
  _autoReconnect;
7
8
  _connectAttempts;
8
- _connectTimeoutRef;
9
+ _connectTimeoutRef = null;
9
10
  _pendingReconnectTimeout;
10
11
  _pingTimeoutMs;
11
12
  _uri;
@@ -29,25 +30,14 @@ export class ClientTransport extends SocketTransport {
29
30
  }
30
31
  this._connectAttempts = 0;
31
32
  this._pendingReconnectTimeout = null;
32
- this.autoReconnect = options.autoReconnect ?? false;
33
+ this._autoReconnect = parseAutoReconnectOptions(options.autoReconnect);
33
34
  this.isPingTimeoutDisabled = (options.isPingTimeoutDisabled === true);
34
35
  }
35
36
  get autoReconnect() {
36
37
  return this._autoReconnect;
37
38
  }
38
39
  set autoReconnect(value) {
39
- if (value) {
40
- this._autoReconnect = {
41
- initialDelay: 10000,
42
- maxDelayMs: 60000,
43
- multiplier: 1.5,
44
- randomness: 10000,
45
- ...(value === true ? {} : value)
46
- };
47
- }
48
- else {
49
- this._autoReconnect = false;
50
- }
40
+ this._autoReconnect = parseAutoReconnectOptions(value);
51
41
  }
52
42
  connect(options) {
53
43
  let timeoutMs = this.connectTimeoutMs;
@@ -173,11 +163,10 @@ export class ClientTransport extends SocketTransport {
173
163
  this.setReadyStatus(this.pingTimeoutMs, authError);
174
164
  })
175
165
  .catch((err) => {
176
- if (err.statusCode == null) {
177
- err.statusCode = 4003;
178
- }
179
- this.onError(err);
180
- this.disconnect(err.statusCode, err.toString());
166
+ const error = toError(err);
167
+ const statusCode = (err != null && typeof err === 'object' && 'statusCode' in err) ? err.statusCode : 4003;
168
+ this.onError(error);
169
+ this.disconnect(statusCode, error.toString());
181
170
  });
182
171
  }
183
172
  onPingPong() {
@@ -2,6 +2,5 @@ import { ChannelMap } from '@socket-mesh/channels';
2
2
  import { RequestHandlerArgs } from '@socket-mesh/core';
3
3
  import { ClientSocket } from '../client-socket.js';
4
4
  import { ClientTransport } from '../client-transport.js';
5
- import { ClientPrivateMap, KickOutOptions } from '../maps/client-map.js';
6
- import { ServerPrivateMap } from '../maps/server-map.js';
7
- export declare function kickOutHandler({ options, socket }: RequestHandlerArgs<KickOutOptions, ClientPrivateMap, {}, ServerPrivateMap, {}, {}, ClientSocket<{}, ChannelMap>, ClientTransport<{}, {}, {}, {}, {}>>): Promise<void>;
5
+ import { KickOutOptions } from '../maps/client-map.js';
6
+ export declare function kickOutHandler({ options, socket }: RequestHandlerArgs<KickOutOptions, {}, ClientSocket<{}, {}, ChannelMap>, ClientTransport<{}>>): Promise<void>;
@@ -2,6 +2,4 @@ import { ChannelMap, PublishOptions } from '@socket-mesh/channels';
2
2
  import { RequestHandlerArgs } from '@socket-mesh/core';
3
3
  import { ClientSocket } from '../client-socket.js';
4
4
  import { ClientTransport } from '../client-transport.js';
5
- import { ClientPrivateMap } from '../maps/client-map.js';
6
- import { ServerPrivateMap } from '../maps/server-map.js';
7
- export declare function publishHandler({ options, socket }: RequestHandlerArgs<PublishOptions, ClientPrivateMap, {}, ServerPrivateMap, {}, {}, ClientSocket<{}, ChannelMap>, ClientTransport<{}, {}, {}, {}, {}>>): Promise<void>;
5
+ export declare function publishHandler({ options, socket }: RequestHandlerArgs<PublishOptions, {}, ClientSocket<{}, {}, ChannelMap>, ClientTransport<{}>>): Promise<void>;
@@ -1,17 +1,17 @@
1
- import { MethodMap, PrivateMethodMap, PublicMethodMap, ServiceMap } from '@socket-mesh/core';
2
1
  import { Plugin, SendRequestPluginArgs, SendResponsePluginArgs } from '@socket-mesh/core';
3
- export interface BatchingPluginOptions {
2
+ export interface BatchingPluginOptions<TType = string> {
4
3
  batchInterval?: number;
5
4
  batchOnHandshakeDuration?: false | number;
5
+ type: TType;
6
6
  }
7
- export declare abstract class BatchingPlugin<TIncoming extends MethodMap, TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap, TState extends object> implements Plugin<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState> {
7
+ export declare abstract class BatchingPlugin<TType extends string> implements Plugin {
8
8
  private _batchingIntervalId;
9
9
  private _handshakeTimeoutId;
10
10
  private _isBatching;
11
11
  batchInterval: number;
12
12
  batchOnHandshakeDuration: boolean | number;
13
- type: string;
14
- constructor(options?: BatchingPluginOptions);
13
+ type: TType;
14
+ constructor(options: BatchingPluginOptions<TType>);
15
15
  cancelBatching(): void;
16
16
  protected abstract flush(): void;
17
17
  get isBatching(): boolean;
@@ -22,20 +22,18 @@ export declare abstract class BatchingPlugin<TIncoming extends MethodMap, TOutgo
22
22
  private stop;
23
23
  stopBatching(): void;
24
24
  }
25
- export declare class RequestBatchingPlugin<TIncoming extends MethodMap, TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap, TState extends object> extends BatchingPlugin<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState> {
25
+ export declare class RequestBatchingPlugin extends BatchingPlugin<'requestBatching'> {
26
26
  private _continue;
27
27
  private _requests;
28
- type: 'requestBatching';
29
- constructor(options?: BatchingPluginOptions);
28
+ constructor(options?: Omit<BatchingPluginOptions, 'type'>);
30
29
  cancelBatching(): void;
31
30
  protected flush(): void;
32
- sendRequest({ cont, requests }: SendRequestPluginArgs<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>): void;
31
+ sendRequest({ cont, requests }: SendRequestPluginArgs): void;
33
32
  }
34
- export declare class ResponseBatchingPlugin<TIncoming extends MethodMap, TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap, TState extends object> extends BatchingPlugin<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState> {
33
+ export declare class ResponseBatchingPlugin extends BatchingPlugin<'responseBatching'> {
35
34
  private _continue;
36
35
  private _responses;
37
- type: 'responseBatching';
38
- constructor(options?: BatchingPluginOptions);
36
+ constructor(options?: Omit<BatchingPluginOptions, 'type'>);
39
37
  protected flush(): void;
40
- sendResponse({ cont, responses }: SendResponsePluginArgs<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>): void;
38
+ sendResponse({ cont, responses }: SendResponsePluginArgs): void;
41
39
  }
@@ -7,8 +7,9 @@ export class BatchingPlugin {
7
7
  type;
8
8
  constructor(options) {
9
9
  this._isBatching = false;
10
- this.batchInterval = options?.batchInterval || 50;
11
- this.batchOnHandshakeDuration = options?.batchOnHandshakeDuration ?? false;
10
+ this.type = options.type;
11
+ this.batchInterval = options.batchInterval || 50;
12
+ this.batchOnHandshakeDuration = options.batchOnHandshakeDuration ?? false;
12
13
  this._batchingIntervalId = null;
13
14
  this._handshakeTimeoutId = null;
14
15
  }
@@ -67,10 +68,11 @@ export class BatchingPlugin {
67
68
  export class RequestBatchingPlugin extends BatchingPlugin {
68
69
  _continue;
69
70
  _requests;
70
- type;
71
71
  constructor(options) {
72
- super(options);
73
- this.type = 'requestBatching';
72
+ super({
73
+ ...(options ?? {}),
74
+ type: 'requestBatching'
75
+ });
74
76
  this._requests = [];
75
77
  this._continue = null;
76
78
  }
@@ -100,10 +102,11 @@ export class RequestBatchingPlugin extends BatchingPlugin {
100
102
  export class ResponseBatchingPlugin extends BatchingPlugin {
101
103
  _continue;
102
104
  _responses;
103
- type;
104
105
  constructor(options) {
105
- super(options);
106
- this.type = 'responseBatching';
106
+ super({
107
+ ...(options ?? {}),
108
+ type: 'responseBatching'
109
+ });
107
110
  this._responses = [];
108
111
  this._continue = null;
109
112
  }
@@ -1,10 +1,10 @@
1
- import { MessageRawPluginArgs, MethodMap, Plugin, PluginArgs, PrivateMethodMap, PublicMethodMap, ServiceMap } from '@socket-mesh/core';
1
+ import { MessageRawPluginArgs, Plugin, PluginArgs } from '@socket-mesh/core';
2
2
  import { RawData } from 'ws';
3
- export declare class InOrderPlugin<TIncoming extends MethodMap, TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap, TState extends object> implements Plugin<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState> {
3
+ export declare class InOrderPlugin implements Plugin {
4
4
  private readonly _inboundMessageStream;
5
5
  type: 'inOrder';
6
6
  constructor();
7
7
  handleInboundMessageStream(): void;
8
- onEnd({ transport }: PluginArgs<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>): void;
9
- onMessageRaw(options: MessageRawPluginArgs<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>): Promise<RawData | string>;
8
+ onEnd({ transport }: PluginArgs): void;
9
+ onMessageRaw(options: MessageRawPluginArgs): Promise<RawData | string>;
10
10
  }
@@ -6,6 +6,7 @@ export class InOrderPlugin {
6
6
  constructor() {
7
7
  this._inboundMessageStream = new WritableConsumableStream();
8
8
  // this._outboundMessageStream = new WritableConsumableStream<SendRequestPluginArgs<T>>;
9
+ this.type = 'inOrder';
9
10
  this.handleInboundMessageStream();
10
11
  // this.handleOutboundMessageStream();
11
12
  }
@@ -1,5 +1,5 @@
1
- import { MethodMap, Plugin, PrivateMethodMap, PublicMethodMap, SendRequestPluginArgs, ServiceMap } from '@socket-mesh/core';
2
- export declare class OfflinePlugin<TIncoming extends MethodMap, TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap, TState extends object> implements Plugin<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState> {
1
+ import { Plugin, SendRequestPluginArgs } from '@socket-mesh/core';
2
+ export declare class OfflinePlugin implements Plugin {
3
3
  private _continue;
4
4
  private _isReady;
5
5
  private _requests;
@@ -9,5 +9,5 @@ export declare class OfflinePlugin<TIncoming extends MethodMap, TOutgoing extend
9
9
  onClose(): void;
10
10
  onDisconnected(): void;
11
11
  onReady(): void;
12
- sendRequest({ cont, requests }: SendRequestPluginArgs<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>): void;
12
+ sendRequest({ cont, requests }: SendRequestPluginArgs): void;
13
13
  }
@@ -37,10 +37,10 @@ export class OfflinePlugin {
37
37
  cont(requests);
38
38
  return;
39
39
  }
40
- const systemRequests = requests.filter(item => SYSTEM_METHODS.indexOf(String(item.method)) > -1);
40
+ const systemRequests = requests.filter(item => SYSTEM_METHODS.indexOf(item.method) > -1);
41
41
  let otherRequests = requests;
42
42
  if (systemRequests.length) {
43
- otherRequests = (systemRequests.length === requests.length) ? [] : requests.filter(item => SYSTEM_METHODS.indexOf(String(item.method)) < 0);
43
+ otherRequests = (systemRequests.length === requests.length) ? [] : requests.filter(item => SYSTEM_METHODS.indexOf(item.method) < 0);
44
44
  }
45
45
  if (otherRequests.length) {
46
46
  this._continue = cont;
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.6",
4
+ "version": "19.0.0",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",