@socket-mesh/client 18.1.4 → 18.1.6
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/dist/client-auth-engine.d.ts +7 -7
- package/dist/client-auth-engine.js +15 -12
- package/dist/client-channels.d.ts +12 -12
- package/dist/client-channels.js +75 -72
- package/dist/client-socket-options.d.ts +18 -15
- package/dist/client-socket-options.js +1 -1
- package/dist/client-socket.d.ts +11 -10
- package/dist/client-socket.js +18 -14
- package/dist/client-transport.d.ts +27 -29
- package/dist/client-transport.js +72 -53
- package/dist/handlers/index.d.ts +4 -4
- package/dist/handlers/index.js +4 -4
- package/dist/handlers/kickout.d.ts +7 -6
- package/dist/handlers/kickout.js +1 -3
- package/dist/handlers/publish.d.ts +7 -7
- package/dist/handlers/publish.js +1 -1
- package/dist/handlers/remove-auth-token.d.ts +1 -1
- package/dist/handlers/set-auth-token.d.ts +3 -3
- package/dist/handlers/set-auth-token.js +1 -1
- package/dist/index.d.ts +8 -8
- package/dist/index.js +8 -8
- package/dist/maps/client-map.d.ts +8 -17
- package/dist/maps/index.d.ts +2 -3
- package/dist/maps/index.js +2 -3
- package/dist/maps/server-map.d.ts +11 -11
- package/dist/plugins/batching-plugin.d.ts +18 -18
- package/dist/plugins/batching-plugin.js +32 -16
- package/dist/plugins/in-order-plugin.d.ts +6 -6
- package/dist/plugins/in-order-plugin.js +30 -28
- package/dist/plugins/index.d.ts +3 -3
- package/dist/plugins/index.js +3 -3
- package/dist/plugins/offline-plugin.d.ts +7 -7
- package/dist/plugins/offline-plugin.js +27 -21
- package/package.json +42 -40
- package/dist/maps/socket-map.d.ts +0 -17
- package/dist/maps/socket-map.js +0 -1
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import { SignedAuthToken } from
|
|
1
|
+
import { SignedAuthToken } from '@socket-mesh/auth';
|
|
2
2
|
export interface ClientAuthEngine {
|
|
3
|
+
loadToken(): Promise<null | SignedAuthToken>;
|
|
4
|
+
removeToken(): Promise<null | SignedAuthToken>;
|
|
3
5
|
saveToken(token: SignedAuthToken, options?: {
|
|
4
6
|
[key: string]: any;
|
|
5
7
|
}): Promise<SignedAuthToken>;
|
|
6
|
-
removeToken(): Promise<SignedAuthToken>;
|
|
7
|
-
loadToken(): Promise<SignedAuthToken | null>;
|
|
8
8
|
}
|
|
9
|
-
export declare function isAuthEngine(auth: ClientAuthEngine | LocalStorageAuthEngineOptions): auth is ClientAuthEngine;
|
|
10
9
|
export interface LocalStorageAuthEngineOptions {
|
|
11
10
|
authTokenName?: string;
|
|
12
11
|
}
|
|
12
|
+
export declare function isAuthEngine(auth?: ClientAuthEngine | LocalStorageAuthEngineOptions | null): auth is ClientAuthEngine;
|
|
13
13
|
export declare class LocalStorageAuthEngine implements ClientAuthEngine {
|
|
14
14
|
private readonly _authTokenName;
|
|
15
15
|
private readonly _internalStorage;
|
|
16
16
|
readonly isLocalStorageEnabled: boolean;
|
|
17
17
|
constructor({ authTokenName }?: LocalStorageAuthEngineOptions);
|
|
18
|
-
private
|
|
18
|
+
private checkLocalStorageEnabled;
|
|
19
|
+
loadToken(): Promise<null | SignedAuthToken>;
|
|
20
|
+
removeToken(): Promise<null | SignedAuthToken>;
|
|
19
21
|
saveToken(token: string): Promise<SignedAuthToken>;
|
|
20
|
-
removeToken(): Promise<SignedAuthToken>;
|
|
21
|
-
loadToken(): Promise<SignedAuthToken>;
|
|
22
22
|
}
|
|
@@ -1,18 +1,21 @@
|
|
|
1
1
|
export function isAuthEngine(auth) {
|
|
2
|
-
return (typeof auth === 'object' && 'saveToken' in auth && 'removeToken' in auth && 'loadToken' in auth);
|
|
2
|
+
return (!!auth && typeof auth === 'object' && 'saveToken' in auth && 'removeToken' in auth && 'loadToken' in auth);
|
|
3
3
|
}
|
|
4
4
|
export class LocalStorageAuthEngine {
|
|
5
|
+
_authTokenName;
|
|
6
|
+
_internalStorage;
|
|
7
|
+
isLocalStorageEnabled;
|
|
5
8
|
constructor({ authTokenName } = {}) {
|
|
6
9
|
this._internalStorage = {};
|
|
7
|
-
this.isLocalStorageEnabled = this.
|
|
10
|
+
this.isLocalStorageEnabled = this.checkLocalStorageEnabled();
|
|
8
11
|
this._authTokenName = authTokenName ?? 'socketmesh.authToken';
|
|
9
12
|
}
|
|
10
|
-
|
|
13
|
+
checkLocalStorageEnabled() {
|
|
11
14
|
let err;
|
|
12
15
|
try {
|
|
13
16
|
// Safari, in Private Browsing Mode, looks like it supports localStorage but all calls to setItem
|
|
14
17
|
// throw QuotaExceededError. We're going to detect this and avoid hard to debug edge cases.
|
|
15
|
-
localStorage.setItem('__localStorageTest',
|
|
18
|
+
localStorage.setItem('__localStorageTest', '1');
|
|
16
19
|
localStorage.removeItem('__localStorageTest');
|
|
17
20
|
}
|
|
18
21
|
catch (e) {
|
|
@@ -20,17 +23,18 @@ export class LocalStorageAuthEngine {
|
|
|
20
23
|
}
|
|
21
24
|
return !err;
|
|
22
25
|
}
|
|
23
|
-
async
|
|
26
|
+
async loadToken() {
|
|
27
|
+
let token;
|
|
24
28
|
if (this.isLocalStorageEnabled) {
|
|
25
|
-
localStorage.
|
|
29
|
+
token = localStorage.getItem(this._authTokenName);
|
|
26
30
|
}
|
|
27
31
|
else {
|
|
28
|
-
this._internalStorage[this._authTokenName]
|
|
32
|
+
token = this._internalStorage[this._authTokenName] || null;
|
|
29
33
|
}
|
|
30
34
|
return token;
|
|
31
35
|
}
|
|
32
36
|
async removeToken() {
|
|
33
|
-
|
|
37
|
+
const loadPromise = this.loadToken();
|
|
34
38
|
if (this.isLocalStorageEnabled) {
|
|
35
39
|
localStorage.removeItem(this._authTokenName);
|
|
36
40
|
}
|
|
@@ -39,13 +43,12 @@ export class LocalStorageAuthEngine {
|
|
|
39
43
|
}
|
|
40
44
|
return loadPromise;
|
|
41
45
|
}
|
|
42
|
-
async
|
|
43
|
-
let token;
|
|
46
|
+
async saveToken(token) {
|
|
44
47
|
if (this.isLocalStorageEnabled) {
|
|
45
|
-
|
|
48
|
+
localStorage.setItem(this._authTokenName, token);
|
|
46
49
|
}
|
|
47
50
|
else {
|
|
48
|
-
|
|
51
|
+
this._internalStorage[this._authTokenName] = token;
|
|
49
52
|
}
|
|
50
53
|
return token;
|
|
51
54
|
}
|
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import { ChannelDetails, Channels, ChannelsOptions } from
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { ChannelDetails, ChannelMap, Channels, ChannelsOptions } from '@socket-mesh/channels';
|
|
2
|
+
import { MethodMap, PrivateMethodMap, PublicMethodMap, ServiceMap } from '@socket-mesh/core';
|
|
3
|
+
import { ClientTransport } from './client-transport.js';
|
|
4
4
|
export interface ClientChannelsOptions extends ChannelsOptions {
|
|
5
5
|
autoSubscribeOnConnect?: boolean;
|
|
6
6
|
}
|
|
7
|
-
export declare class ClientChannels<
|
|
8
|
-
autoSubscribeOnConnect: boolean;
|
|
9
|
-
protected readonly _transport: ClientTransport<T>;
|
|
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> {
|
|
10
8
|
protected _preparingPendingSubscriptions: boolean;
|
|
11
|
-
|
|
9
|
+
protected readonly _transport: ClientTransport<TIncoming, TService, TOutgoing, TPrivateOutgoing, TState>;
|
|
10
|
+
autoSubscribeOnConnect: boolean;
|
|
11
|
+
constructor(transport: ClientTransport<TIncoming, TService, TOutgoing, TPrivateOutgoing, TState>, options?: ClientChannelsOptions);
|
|
12
|
+
invokePublish<U extends keyof TChannel & string>(channelName: keyof TChannel & string, data: TChannel[U]): Promise<void>;
|
|
13
|
+
private processPendingSubscriptions;
|
|
12
14
|
private suspendSubscriptions;
|
|
15
|
+
transmitPublish<U extends keyof TChannel & string>(channelName: U, data: TChannel[U]): Promise<void>;
|
|
16
|
+
private triggerChannelSubscribeFail;
|
|
13
17
|
protected trySubscribe(channel: ChannelDetails): void;
|
|
14
|
-
private processPendingSubscriptions;
|
|
15
|
-
unsubscribe(channelName: keyof T['Channel'] & string): void;
|
|
16
18
|
protected tryUnsubscribe(channel: ChannelDetails): void;
|
|
17
|
-
|
|
18
|
-
transmitPublish<U extends keyof T['Channel'] & string>(channelName: U, data: T['Channel'][U]): Promise<void>;
|
|
19
|
-
invokePublish<U extends keyof T['Channel'] & string>(channelName: keyof T['Channel'] & string, data: T['Channel'][U]): Promise<void>;
|
|
19
|
+
unsubscribe(channelName: keyof TChannel & string): void;
|
|
20
20
|
}
|
package/dist/client-channels.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import { Channels } from
|
|
1
|
+
import { Channels } from '@socket-mesh/channels';
|
|
2
2
|
export class ClientChannels extends Channels {
|
|
3
|
+
_preparingPendingSubscriptions;
|
|
4
|
+
_transport;
|
|
5
|
+
autoSubscribeOnConnect;
|
|
3
6
|
constructor(transport, options) {
|
|
4
7
|
if (!options) {
|
|
5
8
|
options = {};
|
|
@@ -9,20 +12,51 @@ export class ClientChannels extends Channels {
|
|
|
9
12
|
this._transport = transport;
|
|
10
13
|
this._preparingPendingSubscriptions = false;
|
|
11
14
|
this._transport.plugins.push({
|
|
12
|
-
type: 'channels',
|
|
13
15
|
onAuthenticated: () => {
|
|
14
16
|
if (!this._preparingPendingSubscriptions) {
|
|
15
17
|
this.processPendingSubscriptions();
|
|
16
18
|
}
|
|
17
19
|
},
|
|
20
|
+
onClose: () => {
|
|
21
|
+
this.suspendSubscriptions();
|
|
22
|
+
},
|
|
18
23
|
onReady: () => {
|
|
19
24
|
if (this.autoSubscribeOnConnect) {
|
|
20
25
|
this.processPendingSubscriptions();
|
|
21
26
|
}
|
|
22
27
|
},
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
28
|
+
type: 'channels'
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
invokePublish(channelName, data) {
|
|
32
|
+
const pubData = {
|
|
33
|
+
channel: this.decorateChannelName(channelName),
|
|
34
|
+
data
|
|
35
|
+
};
|
|
36
|
+
return this._transport.invoke('#publish', pubData)[0];
|
|
37
|
+
}
|
|
38
|
+
processPendingSubscriptions() {
|
|
39
|
+
this._preparingPendingSubscriptions = false;
|
|
40
|
+
const pendingChannels = [];
|
|
41
|
+
Object.keys(this._channelMap).forEach((channelName) => {
|
|
42
|
+
const channel = this._channelMap[channelName];
|
|
43
|
+
if (channel.state === 'pending') {
|
|
44
|
+
pendingChannels.push(channel);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
pendingChannels.sort((a, b) => {
|
|
48
|
+
const ap = a.options.priority || 0;
|
|
49
|
+
const bp = b.options.priority || 0;
|
|
50
|
+
if (ap > bp) {
|
|
51
|
+
return -1;
|
|
52
|
+
}
|
|
53
|
+
if (ap < bp) {
|
|
54
|
+
return 1;
|
|
55
|
+
}
|
|
56
|
+
return 0;
|
|
57
|
+
});
|
|
58
|
+
pendingChannels.forEach((channel) => {
|
|
59
|
+
this.trySubscribe(channel);
|
|
26
60
|
});
|
|
27
61
|
}
|
|
28
62
|
suspendSubscriptions() {
|
|
@@ -30,13 +64,37 @@ export class ClientChannels extends Channels {
|
|
|
30
64
|
this.triggerChannelUnsubscribe(this._channelMap[channel], true);
|
|
31
65
|
}
|
|
32
66
|
}
|
|
67
|
+
transmitPublish(channelName, data) {
|
|
68
|
+
const pubData = {
|
|
69
|
+
channel: this.decorateChannelName(channelName),
|
|
70
|
+
data
|
|
71
|
+
};
|
|
72
|
+
return this._transport.transmit('#publish', pubData);
|
|
73
|
+
}
|
|
74
|
+
triggerChannelSubscribeFail(err, channel, options) {
|
|
75
|
+
const hasAuthRequirements = !channel.options.waitForAuth || !!this._transport.signedAuthToken;
|
|
76
|
+
const hasChannel = !!this._channelMap[channel.name];
|
|
77
|
+
if (hasChannel && hasAuthRequirements) {
|
|
78
|
+
delete this._channelMap[channel.name];
|
|
79
|
+
this._channelEventDemux.write(`${channel.name}/subscribeFail`, {
|
|
80
|
+
channel: channel.name,
|
|
81
|
+
error: err,
|
|
82
|
+
options
|
|
83
|
+
});
|
|
84
|
+
this.emit('subscribeFail', {
|
|
85
|
+
channel: channel.name,
|
|
86
|
+
error: err,
|
|
87
|
+
options
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
33
91
|
trySubscribe(channel) {
|
|
34
|
-
const
|
|
92
|
+
const hasAuthRequirements = !channel.options.waitForAuth || !!this._transport.signedAuthToken;
|
|
35
93
|
// We can only ever have one pending subscribe action at any given time on a channel
|
|
36
|
-
if (this._transport.status === 'ready'
|
|
37
|
-
!this._preparingPendingSubscriptions
|
|
38
|
-
!channel.subscribePromise
|
|
39
|
-
|
|
94
|
+
if (this._transport.status === 'ready'
|
|
95
|
+
&& !this._preparingPendingSubscriptions
|
|
96
|
+
&& !channel.subscribePromise
|
|
97
|
+
&& hasAuthRequirements) {
|
|
40
98
|
const subscriptionOptions = {};
|
|
41
99
|
if (channel.options.waitForAuth) {
|
|
42
100
|
subscriptionOptions.waitForAuth = true;
|
|
@@ -44,7 +102,7 @@ export class ClientChannels extends Channels {
|
|
|
44
102
|
if (channel.options.data) {
|
|
45
103
|
subscriptionOptions.data = channel.options.data;
|
|
46
104
|
}
|
|
47
|
-
[channel.subscribePromise, channel.subscribeAbort] = this._transport.invoke({ method: '#subscribe'
|
|
105
|
+
[channel.subscribePromise, channel.subscribeAbort] = this._transport.invoke({ ackTimeoutMs: false, method: '#subscribe' }, {
|
|
48
106
|
channel: this.decorateChannelName(channel.name),
|
|
49
107
|
...subscriptionOptions
|
|
50
108
|
});
|
|
@@ -52,7 +110,7 @@ export class ClientChannels extends Channels {
|
|
|
52
110
|
delete channel.subscribePromise;
|
|
53
111
|
delete channel.subscribeAbort;
|
|
54
112
|
this.triggerChannelSubscribe(channel, subscriptionOptions);
|
|
55
|
-
}).catch(err => {
|
|
113
|
+
}).catch((err) => {
|
|
56
114
|
if (err.name === 'BadConnectionError') {
|
|
57
115
|
// In case of a failed connection, keep the subscription
|
|
58
116
|
// as pending; it will try again on reconnect.
|
|
@@ -70,36 +128,6 @@ export class ClientChannels extends Channels {
|
|
|
70
128
|
});
|
|
71
129
|
}
|
|
72
130
|
}
|
|
73
|
-
processPendingSubscriptions() {
|
|
74
|
-
this._preparingPendingSubscriptions = false;
|
|
75
|
-
const pendingChannels = [];
|
|
76
|
-
Object.keys(this._channelMap).forEach((channelName) => {
|
|
77
|
-
const channel = this._channelMap[channelName];
|
|
78
|
-
if (channel.state === 'pending') {
|
|
79
|
-
pendingChannels.push(channel);
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
pendingChannels.sort((a, b) => {
|
|
83
|
-
const ap = a.options.priority || 0;
|
|
84
|
-
const bp = b.options.priority || 0;
|
|
85
|
-
if (ap > bp) {
|
|
86
|
-
return -1;
|
|
87
|
-
}
|
|
88
|
-
if (ap < bp) {
|
|
89
|
-
return 1;
|
|
90
|
-
}
|
|
91
|
-
return 0;
|
|
92
|
-
});
|
|
93
|
-
pendingChannels.forEach((channel) => {
|
|
94
|
-
this.trySubscribe(channel);
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
unsubscribe(channelName) {
|
|
98
|
-
const channel = this._channelMap[channelName];
|
|
99
|
-
if (channel) {
|
|
100
|
-
this.tryUnsubscribe(channel);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
131
|
tryUnsubscribe(channel) {
|
|
104
132
|
this.triggerChannelUnsubscribe(channel);
|
|
105
133
|
if (this._transport.status === 'ready') {
|
|
@@ -112,38 +140,13 @@ export class ClientChannels extends Channels {
|
|
|
112
140
|
// the operation on the server side.
|
|
113
141
|
this._transport
|
|
114
142
|
.transmit('#unsubscribe', decoratedChannelName)
|
|
115
|
-
.catch(err => { });
|
|
143
|
+
.catch((err) => { });
|
|
116
144
|
}
|
|
117
145
|
}
|
|
118
|
-
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
delete this._channelMap[channel.name];
|
|
123
|
-
this._channelEventDemux.write(`${channel.name}/subscribeFail`, {
|
|
124
|
-
channel: channel.name,
|
|
125
|
-
error: err,
|
|
126
|
-
options
|
|
127
|
-
});
|
|
128
|
-
this.emit('subscribeFail', {
|
|
129
|
-
error: err,
|
|
130
|
-
channel: channel.name,
|
|
131
|
-
options
|
|
132
|
-
});
|
|
146
|
+
unsubscribe(channelName) {
|
|
147
|
+
const channel = this._channelMap[channelName];
|
|
148
|
+
if (channel) {
|
|
149
|
+
this.tryUnsubscribe(channel);
|
|
133
150
|
}
|
|
134
151
|
}
|
|
135
|
-
transmitPublish(channelName, data) {
|
|
136
|
-
const pubData = {
|
|
137
|
-
channel: this.decorateChannelName(channelName),
|
|
138
|
-
data
|
|
139
|
-
};
|
|
140
|
-
return this._transport.transmit('#publish', pubData);
|
|
141
|
-
}
|
|
142
|
-
invokePublish(channelName, data) {
|
|
143
|
-
const pubData = {
|
|
144
|
-
channel: this.decorateChannelName(channelName),
|
|
145
|
-
data
|
|
146
|
-
};
|
|
147
|
-
return this._transport.invoke('#publish', pubData)[0];
|
|
148
|
-
}
|
|
149
152
|
}
|
|
@@ -1,24 +1,27 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import { ClientAuthEngine, LocalStorageAuthEngineOptions } from
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
1
|
+
import { MethodMap, PrivateMethodMap, PublicMethodMap, ServiceMap, SocketOptions } from '@socket-mesh/core';
|
|
2
|
+
import ws from 'isomorphic-ws';
|
|
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
6
|
export interface AutoReconnectOptions {
|
|
7
7
|
initialDelay: number;
|
|
8
|
-
randomness: number;
|
|
9
|
-
multiplier: number;
|
|
10
8
|
maxDelayMs: number;
|
|
9
|
+
multiplier: number;
|
|
10
|
+
randomness: number;
|
|
11
|
+
}
|
|
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> {
|
|
13
|
+
address: string | URL;
|
|
14
|
+
authEngine?: ClientAuthEngine | LocalStorageAuthEngineOptions | null;
|
|
15
|
+
autoConnect?: boolean;
|
|
16
|
+
autoReconnect?: boolean | Partial<AutoReconnectOptions>;
|
|
17
|
+
autoSubscribeOnConnect?: boolean;
|
|
18
|
+
channelPrefix?: string;
|
|
19
|
+
connectTimeoutMs?: number;
|
|
20
|
+
wsOptions?: ws.ClientOptions;
|
|
11
21
|
}
|
|
12
22
|
export interface ConnectOptions {
|
|
13
23
|
address?: string | URL;
|
|
14
24
|
connectTimeoutMs?: number;
|
|
15
25
|
wsOptions?: ws.ClientOptions;
|
|
16
26
|
}
|
|
17
|
-
export
|
|
18
|
-
autoConnect?: boolean;
|
|
19
|
-
authEngine?: ClientAuthEngine | LocalStorageAuthEngineOptions | null;
|
|
20
|
-
autoReconnect?: Partial<AutoReconnectOptions> | boolean;
|
|
21
|
-
autoSubscribeOnConnect?: boolean;
|
|
22
|
-
channelPrefix?: string;
|
|
23
|
-
}
|
|
24
|
-
export declare function parseClientOptions<T extends ClientMap>(options: ClientSocketOptions<T> | string | URL): ClientSocketOptions<T>;
|
|
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>;
|
package/dist/client-socket.d.ts
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { Socket } from
|
|
4
|
-
import { ClientChannels } from
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
1
|
+
import { SignedAuthToken } from '@socket-mesh/auth';
|
|
2
|
+
import { ChannelMap } from '@socket-mesh/channels';
|
|
3
|
+
import { MethodMap, PrivateMethodMap, PublicMethodMap, ServiceMap, Socket } from '@socket-mesh/core';
|
|
4
|
+
import { ClientChannels } from './client-channels.js';
|
|
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 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> {
|
|
8
9
|
private readonly _clientTransport;
|
|
9
|
-
readonly channels: ClientChannels<
|
|
10
|
+
readonly channels: ClientChannels<TChannel, TIncoming, TService, TOutgoing, TPrivateOutgoing, TState>;
|
|
10
11
|
constructor(address: string | URL);
|
|
11
|
-
constructor(options: ClientSocketOptions<
|
|
12
|
+
constructor(options: ClientSocketOptions<TOutgoing, TService, TIncoming, TPrivateOutgoing, TState>);
|
|
12
13
|
authenticate(signedAuthToken: SignedAuthToken): Promise<void>;
|
|
13
14
|
get autoReconnect(): AutoReconnectOptions | false;
|
|
14
|
-
set autoReconnect(value: Partial<AutoReconnectOptions>
|
|
15
|
+
set autoReconnect(value: boolean | Partial<AutoReconnectOptions>);
|
|
15
16
|
connect(options?: ConnectOptions): void;
|
|
16
17
|
get connectTimeoutMs(): number;
|
|
17
18
|
set connectTimeoutMs(timeoutMs: number);
|
package/dist/client-socket.js
CHANGED
|
@@ -1,21 +1,23 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
1
|
+
import { Socket, wait } from '@socket-mesh/core';
|
|
2
|
+
import { hydrateError } from '@socket-mesh/errors';
|
|
3
|
+
import { ClientChannels } from './client-channels.js';
|
|
4
|
+
import { parseClientOptions } from './client-socket-options.js';
|
|
5
|
+
import { ClientTransport } from './client-transport.js';
|
|
6
|
+
import { kickOutHandler } from './handlers/kickout.js';
|
|
7
|
+
import { publishHandler } from './handlers/publish.js';
|
|
8
|
+
import { removeAuthTokenHandler } from './handlers/remove-auth-token.js';
|
|
9
|
+
import { setAuthTokenHandler } from './handlers/set-auth-token.js';
|
|
10
10
|
export class ClientSocket extends Socket {
|
|
11
|
+
_clientTransport;
|
|
12
|
+
channels;
|
|
11
13
|
constructor(options) {
|
|
12
14
|
options = parseClientOptions(options);
|
|
13
15
|
options.handlers =
|
|
14
16
|
Object.assign({
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
'#kickOut': kickOutHandler,
|
|
18
|
+
'#publish': publishHandler,
|
|
19
|
+
'#removeAuthToken': removeAuthTokenHandler,
|
|
20
|
+
'#setAuthToken': setAuthTokenHandler
|
|
19
21
|
}, options.handlers);
|
|
20
22
|
const clientTransport = new ClientTransport(options);
|
|
21
23
|
super(clientTransport, options);
|
|
@@ -68,7 +70,9 @@ export class ClientSocket extends Socket {
|
|
|
68
70
|
this._clientTransport.onError(err);
|
|
69
71
|
return;
|
|
70
72
|
}
|
|
71
|
-
|
|
73
|
+
if (oldAuthToken) {
|
|
74
|
+
this.emit('removeAuthToken', { oldAuthToken });
|
|
75
|
+
}
|
|
72
76
|
})();
|
|
73
77
|
if (this.status !== 'closed') {
|
|
74
78
|
await this._clientTransport.transmit('#removeAuthToken');
|
|
@@ -1,29 +1,35 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import { AutoReconnectOptions, ClientSocketOptions, ConnectOptions } from
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
export declare class ClientTransport<T extends ClientMap> extends SocketTransport<SocketMapFromClient<T>> {
|
|
10
|
-
readonly authEngine: ClientAuthEngine;
|
|
11
|
-
private _uri;
|
|
12
|
-
private _wsOptions;
|
|
13
|
-
connectTimeoutMs: number;
|
|
14
|
-
private _connectTimeoutRef;
|
|
1
|
+
import { AuthToken } from '@socket-mesh/auth';
|
|
2
|
+
import { FunctionReturnType, InvokeMethodOptions, InvokeServiceOptions, MethodMap, PrivateMethodMap, PublicMethodMap, ServiceMap, SocketStatus, SocketTransport } from '@socket-mesh/core';
|
|
3
|
+
import ws from 'isomorphic-ws';
|
|
4
|
+
import { ClientAuthEngine } from './client-auth-engine.js';
|
|
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> {
|
|
15
9
|
private _autoReconnect;
|
|
16
10
|
private _connectAttempts;
|
|
11
|
+
private _connectTimeoutRef;
|
|
17
12
|
private _pendingReconnectTimeout;
|
|
18
13
|
private _pingTimeoutMs;
|
|
14
|
+
private _uri;
|
|
15
|
+
private _wsOptions;
|
|
16
|
+
readonly authEngine: ClientAuthEngine;
|
|
17
|
+
connectTimeoutMs: number;
|
|
19
18
|
isPingTimeoutDisabled: boolean;
|
|
20
|
-
|
|
19
|
+
type: 'client';
|
|
20
|
+
constructor(options: ClientSocketOptions<TOutgoing, TService, TIncoming, TPrivateOutgoing, TState>);
|
|
21
21
|
get autoReconnect(): AutoReconnectOptions | false;
|
|
22
|
-
set autoReconnect(value: Partial<AutoReconnectOptions>
|
|
22
|
+
set autoReconnect(value: boolean | Partial<AutoReconnectOptions>);
|
|
23
23
|
connect(options?: ConnectOptions): void;
|
|
24
24
|
get connectAttempts(): number;
|
|
25
25
|
disconnect(code?: number, reason?: string): void;
|
|
26
26
|
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];
|
|
27
33
|
protected onClose(code: number, reason?: Buffer): void;
|
|
28
34
|
protected onOpen(): void;
|
|
29
35
|
protected onPingPong(): void;
|
|
@@ -32,21 +38,13 @@ export declare class ClientTransport<T extends ClientMap> extends SocketTranspor
|
|
|
32
38
|
set pingTimeoutMs(value: number);
|
|
33
39
|
private resetReconnect;
|
|
34
40
|
send(data: Buffer | string): Promise<void>;
|
|
35
|
-
setAuthorization(authToken: AuthToken): Promise<boolean>;
|
|
36
41
|
setAuthorization(signedAuthToken: string, authToken?: AuthToken): Promise<boolean>;
|
|
37
42
|
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>;
|
|
38
46
|
private tryReconnect;
|
|
39
|
-
type: 'client';
|
|
40
47
|
get uri(): URL;
|
|
41
|
-
protected get webSocket(): ws.WebSocket
|
|
42
|
-
protected set webSocket(value: ws.WebSocket
|
|
43
|
-
transmit<TMethod extends keyof SocketMapFromClient<T>['Outgoing']>(method: TMethod, arg?: Parameters<SocketMapFromClient<T>['Outgoing'][TMethod]>[0]): Promise<void>;
|
|
44
|
-
transmit<TService extends keyof SocketMapFromClient<T>['Service'], TMethod extends keyof SocketMapFromClient<T>['Service'][TService]>(options: [TService, TMethod], arg?: Parameters<SocketMapFromClient<T>['Service'][TService][TMethod]>[0]): Promise<void>;
|
|
45
|
-
transmit<TMethod extends keyof (SocketMapFromClient<T>['PrivateOutgoing'] & ServerPrivateMap)>(method: TMethod, arg?: Parameters<(SocketMapFromClient<T>['PrivateOutgoing'] & ServerPrivateMap)[TMethod]>[0]): Promise<void>;
|
|
46
|
-
invoke<TMethod extends keyof SocketMapFromClient<T>['Outgoing']>(method: TMethod, arg?: Parameters<SocketMapFromClient<T>['Outgoing'][TMethod]>[0]): [Promise<FunctionReturnType<SocketMapFromClient<T>['Outgoing'][TMethod]>>, () => void];
|
|
47
|
-
invoke<TService extends keyof SocketMapFromClient<T>['Service'], TMethod extends keyof SocketMapFromClient<T>['Service'][TService]>(options: [TService, TMethod, (number | false)?], arg?: Parameters<SocketMapFromClient<T>['Service'][TService][TMethod]>[0]): [Promise<FunctionReturnType<SocketMapFromClient<T>['Service'][TService][TMethod]>>, () => void];
|
|
48
|
-
invoke<TService extends keyof SocketMapFromClient<T>['Service'], TMethod extends keyof SocketMapFromClient<T>['Service'][TService]>(options: InvokeServiceOptions<SocketMapFromClient<T>['Service'], TService, TMethod>, arg?: Parameters<SocketMapFromClient<T>['Service'][TService][TMethod]>[0]): [Promise<FunctionReturnType<SocketMapFromClient<T>['Service'][TService][TMethod]>>, () => void];
|
|
49
|
-
invoke<TMethod extends keyof SocketMapFromClient<T>['Outgoing']>(options: InvokeMethodOptions<SocketMapFromClient<T>['Outgoing'], TMethod>, arg?: Parameters<SocketMapFromClient<T>['Outgoing'][TMethod]>[0]): [Promise<FunctionReturnType<SocketMapFromClient<T>['Outgoing'][TMethod]>>, () => void];
|
|
50
|
-
invoke<TMethod extends keyof (SocketMapFromClient<T>['PrivateOutgoing'] & ServerPrivateMap)>(method: TMethod, arg: Parameters<(SocketMapFromClient<T>['PrivateOutgoing'] & ServerPrivateMap)[TMethod]>[0]): [Promise<FunctionReturnType<(SocketMapFromClient<T>['PrivateOutgoing'] & ServerPrivateMap)[TMethod]>>, () => void];
|
|
51
|
-
invoke<TMethod extends keyof (SocketMapFromClient<T>['PrivateOutgoing'] & ServerPrivateMap)>(options: InvokeMethodOptions<(SocketMapFromClient<T>['PrivateOutgoing'] & ServerPrivateMap), TMethod>, arg?: Parameters<(SocketMapFromClient<T>['PrivateOutgoing'] & ServerPrivateMap)[TMethod]>[0]): [Promise<FunctionReturnType<(SocketMapFromClient<T>['PrivateOutgoing'] & ServerPrivateMap)[TMethod]>>, () => void];
|
|
48
|
+
protected get webSocket(): null | ws.WebSocket;
|
|
49
|
+
protected set webSocket(value: null | ws.WebSocket);
|
|
52
50
|
}
|