@socket-mesh/client 18.1.5 → 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 +10 -10
- package/dist/client-channels.js +75 -72
- package/dist/client-socket-options.d.ts +17 -14
- package/dist/client-socket-options.js +1 -1
- package/dist/client-socket.d.ts +8 -8
- package/dist/client-socket.js +18 -14
- package/dist/client-transport.d.ts +24 -25
- 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 -7
- 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 -8
- package/dist/maps/index.d.ts +2 -2
- package/dist/maps/index.js +2 -2
- package/dist/maps/server-map.d.ts +11 -11
- package/dist/plugins/batching-plugin.d.ts +15 -15
- package/dist/plugins/batching-plugin.js +32 -16
- package/dist/plugins/in-order-plugin.d.ts +4 -4
- 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 +6 -6
- package/dist/plugins/offline-plugin.js +27 -21
- package/package.json +30 -28
package/dist/client-transport.js
CHANGED
|
@@ -1,16 +1,27 @@
|
|
|
1
|
-
import { SocketTransport } from
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
1
|
+
import { SocketTransport } from '@socket-mesh/core';
|
|
2
|
+
import { hydrateError, SocketClosedError, SocketProtocolErrorStatuses } from '@socket-mesh/errors';
|
|
3
|
+
import ws from 'isomorphic-ws';
|
|
4
|
+
import { isAuthEngine, LocalStorageAuthEngine } from './client-auth-engine.js';
|
|
5
5
|
export class ClientTransport extends SocketTransport {
|
|
6
|
+
_autoReconnect;
|
|
7
|
+
_connectAttempts;
|
|
8
|
+
_connectTimeoutRef;
|
|
9
|
+
_pendingReconnectTimeout;
|
|
10
|
+
_pingTimeoutMs;
|
|
11
|
+
_uri;
|
|
12
|
+
_wsOptions;
|
|
13
|
+
authEngine;
|
|
14
|
+
connectTimeoutMs;
|
|
15
|
+
isPingTimeoutDisabled;
|
|
16
|
+
type;
|
|
6
17
|
constructor(options) {
|
|
7
18
|
super(options);
|
|
8
19
|
this.type = 'client';
|
|
9
20
|
this._uri = typeof options.address === 'string' ? new URL(options.address) : options.address;
|
|
10
21
|
this.authEngine =
|
|
11
|
-
isAuthEngine(options.authEngine)
|
|
12
|
-
options.authEngine
|
|
13
|
-
new LocalStorageAuthEngine(Object.assign({ authTokenName: `socketmesh.authToken${this._uri.protocol}${this._uri.hostname}` }, options.authEngine));
|
|
22
|
+
isAuthEngine(options.authEngine)
|
|
23
|
+
? options.authEngine
|
|
24
|
+
: new LocalStorageAuthEngine(Object.assign({ authTokenName: `socketmesh.authToken${this._uri.protocol}${this._uri.hostname}` }, options.authEngine));
|
|
14
25
|
this.connectTimeoutMs = options.connectTimeoutMs ?? 20000;
|
|
15
26
|
this._pingTimeoutMs = this.connectTimeoutMs;
|
|
16
27
|
if (options.wsOptions) {
|
|
@@ -18,7 +29,7 @@ export class ClientTransport extends SocketTransport {
|
|
|
18
29
|
}
|
|
19
30
|
this._connectAttempts = 0;
|
|
20
31
|
this._pendingReconnectTimeout = null;
|
|
21
|
-
this.autoReconnect = options.autoReconnect;
|
|
32
|
+
this.autoReconnect = options.autoReconnect ?? false;
|
|
22
33
|
this.isPingTimeoutDisabled = (options.isPingTimeoutDisabled === true);
|
|
23
34
|
}
|
|
24
35
|
get autoReconnect() {
|
|
@@ -26,12 +37,13 @@ export class ClientTransport extends SocketTransport {
|
|
|
26
37
|
}
|
|
27
38
|
set autoReconnect(value) {
|
|
28
39
|
if (value) {
|
|
29
|
-
this._autoReconnect =
|
|
40
|
+
this._autoReconnect = {
|
|
30
41
|
initialDelay: 10000,
|
|
31
|
-
|
|
42
|
+
maxDelayMs: 60000,
|
|
32
43
|
multiplier: 1.5,
|
|
33
|
-
|
|
34
|
-
|
|
44
|
+
randomness: 10000,
|
|
45
|
+
...(value === true ? {} : value)
|
|
46
|
+
};
|
|
35
47
|
}
|
|
36
48
|
else {
|
|
37
49
|
this._autoReconnect = false;
|
|
@@ -40,19 +52,19 @@ export class ClientTransport extends SocketTransport {
|
|
|
40
52
|
connect(options) {
|
|
41
53
|
let timeoutMs = this.connectTimeoutMs;
|
|
42
54
|
if (options) {
|
|
43
|
-
let
|
|
55
|
+
let didOptionsChange = false;
|
|
44
56
|
if (options.connectTimeoutMs) {
|
|
45
57
|
timeoutMs = options.connectTimeoutMs;
|
|
46
58
|
}
|
|
47
59
|
if (options.address) {
|
|
48
|
-
|
|
60
|
+
didOptionsChange = true;
|
|
49
61
|
this._uri = typeof options.address === 'string' ? new URL(options.address) : options.address;
|
|
50
62
|
}
|
|
51
63
|
if (options.wsOptions) {
|
|
52
|
-
|
|
64
|
+
didOptionsChange = true;
|
|
53
65
|
this._wsOptions = options.wsOptions;
|
|
54
66
|
}
|
|
55
|
-
if (
|
|
67
|
+
if (didOptionsChange && this.status !== 'closed') {
|
|
56
68
|
this.disconnect(1000, 'Socket was disconnected by the client to initiate a new connection');
|
|
57
69
|
}
|
|
58
70
|
}
|
|
@@ -84,9 +96,29 @@ export class ClientTransport extends SocketTransport {
|
|
|
84
96
|
}
|
|
85
97
|
return status;
|
|
86
98
|
}
|
|
99
|
+
invoke(methodOptions, arg) {
|
|
100
|
+
let wasAborted = false;
|
|
101
|
+
let abort = () => { wasAborted = true; };
|
|
102
|
+
const promise = Promise.resolve()
|
|
103
|
+
.then(() => {
|
|
104
|
+
if (this.status === 'closed') {
|
|
105
|
+
this.connect();
|
|
106
|
+
return this.socket.listen('connect').once();
|
|
107
|
+
}
|
|
108
|
+
})
|
|
109
|
+
.then(() => {
|
|
110
|
+
const result = super.invoke(methodOptions, arg);
|
|
111
|
+
abort = result[1];
|
|
112
|
+
if (wasAborted) {
|
|
113
|
+
abort();
|
|
114
|
+
}
|
|
115
|
+
return result[0];
|
|
116
|
+
});
|
|
117
|
+
return [promise, () => abort()];
|
|
118
|
+
}
|
|
87
119
|
onClose(code, reason) {
|
|
88
120
|
const status = this.status;
|
|
89
|
-
let
|
|
121
|
+
let isReconnecting = false;
|
|
90
122
|
super.onClose(code, reason);
|
|
91
123
|
// Try to reconnect
|
|
92
124
|
// on server ping timeout (4000)
|
|
@@ -101,30 +133,32 @@ export class ClientTransport extends SocketTransport {
|
|
|
101
133
|
// status, don't wait before trying to reconnect - These could happen
|
|
102
134
|
// if the client wakes up after a period of inactivity and in this case we
|
|
103
135
|
// want to re-establish the connection as soon as possible.
|
|
104
|
-
|
|
136
|
+
isReconnecting = !!this.autoReconnect;
|
|
105
137
|
this.tryReconnect(0);
|
|
106
138
|
// Codes 4500 and above will be treated as permanent disconnects.
|
|
107
139
|
// Socket will not try to auto-reconnect.
|
|
108
140
|
}
|
|
109
141
|
else if (code !== 1000 && code < 4500) {
|
|
110
|
-
|
|
142
|
+
isReconnecting = !!this.autoReconnect;
|
|
111
143
|
this.tryReconnect();
|
|
112
144
|
}
|
|
113
145
|
}
|
|
114
|
-
if (!
|
|
115
|
-
const strReason = reason?.toString() ||
|
|
146
|
+
if (!isReconnecting) {
|
|
147
|
+
const strReason = reason?.toString() || SocketProtocolErrorStatuses[code];
|
|
116
148
|
this.onDisconnect(status, code, strReason);
|
|
117
149
|
}
|
|
118
150
|
}
|
|
119
151
|
onOpen() {
|
|
120
152
|
super.onOpen();
|
|
121
|
-
|
|
122
|
-
|
|
153
|
+
if (this._connectTimeoutRef) {
|
|
154
|
+
clearTimeout(this._connectTimeoutRef);
|
|
155
|
+
this._connectTimeoutRef = null;
|
|
156
|
+
}
|
|
123
157
|
this.resetReconnect();
|
|
124
158
|
this.resetPingTimeout(this.isPingTimeoutDisabled ? false : this.pingTimeoutMs, 4000);
|
|
125
159
|
let authError;
|
|
126
160
|
this.handshake()
|
|
127
|
-
.then(status => {
|
|
161
|
+
.then((status) => {
|
|
128
162
|
this.id = status.id;
|
|
129
163
|
this.pingTimeoutMs = status.pingTimeoutMs;
|
|
130
164
|
if ('authToken' in status && status.authToken) {
|
|
@@ -138,7 +172,7 @@ export class ClientTransport extends SocketTransport {
|
|
|
138
172
|
.then(() => {
|
|
139
173
|
this.setReadyStatus(this.pingTimeoutMs, authError);
|
|
140
174
|
})
|
|
141
|
-
.catch(err => {
|
|
175
|
+
.catch((err) => {
|
|
142
176
|
if (err.statusCode == null) {
|
|
143
177
|
err.statusCode = 4003;
|
|
144
178
|
}
|
|
@@ -166,6 +200,9 @@ export class ClientTransport extends SocketTransport {
|
|
|
166
200
|
this._connectAttempts = 0;
|
|
167
201
|
}
|
|
168
202
|
async send(data) {
|
|
203
|
+
if (!this.webSocket) {
|
|
204
|
+
throw new SocketClosedError('Web socket is closed.');
|
|
205
|
+
}
|
|
169
206
|
this.webSocket.send(data);
|
|
170
207
|
}
|
|
171
208
|
async setAuthorization(signedAuthToken, authToken) {
|
|
@@ -174,8 +211,8 @@ export class ClientTransport extends SocketTransport {
|
|
|
174
211
|
if (changed) {
|
|
175
212
|
this.triggerAuthenticationEvents(false, wasAuthenticated);
|
|
176
213
|
// Even if saving the auth token failes we do NOT want to throw an exception.
|
|
177
|
-
this.authEngine.saveToken(
|
|
178
|
-
.catch(err => {
|
|
214
|
+
this.authEngine.saveToken(signedAuthToken)
|
|
215
|
+
.catch((err) => {
|
|
179
216
|
this.onError(err);
|
|
180
217
|
});
|
|
181
218
|
}
|
|
@@ -187,6 +224,13 @@ export class ClientTransport extends SocketTransport {
|
|
|
187
224
|
}
|
|
188
225
|
return super.status;
|
|
189
226
|
}
|
|
227
|
+
async transmit(serviceAndMethod, arg) {
|
|
228
|
+
if (this.status === 'closed') {
|
|
229
|
+
this.connect();
|
|
230
|
+
await this.socket.listen('connect').once();
|
|
231
|
+
}
|
|
232
|
+
await super.transmit(serviceAndMethod, arg);
|
|
233
|
+
}
|
|
190
234
|
tryReconnect(initialDelay) {
|
|
191
235
|
if (!this.autoReconnect) {
|
|
192
236
|
return;
|
|
@@ -221,7 +265,7 @@ export class ClientTransport extends SocketTransport {
|
|
|
221
265
|
}
|
|
222
266
|
}
|
|
223
267
|
super.webSocket = value;
|
|
224
|
-
if (this.webSocket
|
|
268
|
+
if (this.webSocket?.on) {
|
|
225
269
|
// WebSockets will throw an error if they are closed before they are completely open.
|
|
226
270
|
// We hook into these events to supress those errors and clean them up after a connection is established.
|
|
227
271
|
function onOpenCloseError() {
|
|
@@ -234,29 +278,4 @@ export class ClientTransport extends SocketTransport {
|
|
|
234
278
|
this.webSocket.on('error', onOpenCloseError);
|
|
235
279
|
}
|
|
236
280
|
}
|
|
237
|
-
async transmit(serviceAndMethod, arg) {
|
|
238
|
-
if (this.status === 'closed') {
|
|
239
|
-
this.connect();
|
|
240
|
-
await this.socket.listen('connect').once();
|
|
241
|
-
}
|
|
242
|
-
await super.transmit(serviceAndMethod, arg);
|
|
243
|
-
}
|
|
244
|
-
invoke(methodOptions, arg) {
|
|
245
|
-
let abort;
|
|
246
|
-
return [
|
|
247
|
-
Promise.resolve()
|
|
248
|
-
.then(() => {
|
|
249
|
-
if (this.status === 'closed') {
|
|
250
|
-
this.connect();
|
|
251
|
-
return this.socket.listen('connect').once();
|
|
252
|
-
}
|
|
253
|
-
})
|
|
254
|
-
.then(() => {
|
|
255
|
-
const result = super.invoke(methodOptions, arg);
|
|
256
|
-
abort = result[1];
|
|
257
|
-
return result[0];
|
|
258
|
-
}),
|
|
259
|
-
abort
|
|
260
|
-
];
|
|
261
|
-
}
|
|
262
281
|
}
|
package/dist/handlers/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
1
|
+
export * from './kickout.js';
|
|
2
|
+
export * from './publish.js';
|
|
3
|
+
export * from './remove-auth-token.js';
|
|
4
|
+
export * from './set-auth-token.js';
|
package/dist/handlers/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
1
|
+
export * from './kickout.js';
|
|
2
|
+
export * from './publish.js';
|
|
3
|
+
export * from './remove-auth-token.js';
|
|
4
|
+
export * from './set-auth-token.js';
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
export declare function kickOutHandler({
|
|
1
|
+
import { ChannelMap } from '@socket-mesh/channels';
|
|
2
|
+
import { RequestHandlerArgs } from '@socket-mesh/core';
|
|
3
|
+
import { ClientSocket } from '../client-socket.js';
|
|
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>;
|
package/dist/handlers/kickout.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { ChannelMap, PublishOptions } from
|
|
2
|
-
import { RequestHandlerArgs } from
|
|
3
|
-
import { ClientSocket } from
|
|
4
|
-
import { ClientTransport } from
|
|
5
|
-
import { ClientPrivateMap } from
|
|
6
|
-
import { ServerPrivateMap } from
|
|
7
|
-
export declare function publishHandler({
|
|
1
|
+
import { ChannelMap, PublishOptions } from '@socket-mesh/channels';
|
|
2
|
+
import { RequestHandlerArgs } from '@socket-mesh/core';
|
|
3
|
+
import { ClientSocket } from '../client-socket.js';
|
|
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>;
|
package/dist/handlers/publish.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { RequestHandlerArgs } from
|
|
1
|
+
import { RequestHandlerArgs } from '@socket-mesh/core';
|
|
2
2
|
export declare function removeAuthTokenHandler({ transport }: RequestHandlerArgs<void>): Promise<void>;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { SignedAuthToken } from
|
|
2
|
-
import { RequestHandlerArgs } from
|
|
3
|
-
export declare function setAuthTokenHandler({
|
|
1
|
+
import { SignedAuthToken } from '@socket-mesh/auth';
|
|
2
|
+
import { RequestHandlerArgs } from '@socket-mesh/core';
|
|
3
|
+
export declare function setAuthTokenHandler({ options, transport }: RequestHandlerArgs<SignedAuthToken>): Promise<void>;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
6
|
-
export * from
|
|
7
|
-
export * from
|
|
8
|
-
export * from
|
|
1
|
+
export * from './client-auth-engine.js';
|
|
2
|
+
export * from './client-channels.js';
|
|
3
|
+
export * from './client-socket-options.js';
|
|
4
|
+
export * from './client-socket.js';
|
|
5
|
+
export * from './client-transport.js';
|
|
6
|
+
export * from './handlers/index.js';
|
|
7
|
+
export * from './maps/index.js';
|
|
8
|
+
export * from './plugins/index.js';
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
6
|
-
export * from
|
|
7
|
-
export * from
|
|
8
|
-
export * from
|
|
1
|
+
export * from './client-auth-engine.js';
|
|
2
|
+
export * from './client-channels.js';
|
|
3
|
+
export * from './client-socket-options.js';
|
|
4
|
+
export * from './client-socket.js';
|
|
5
|
+
export * from './client-transport.js';
|
|
6
|
+
export * from './handlers/index.js';
|
|
7
|
+
export * from './maps/index.js';
|
|
8
|
+
export * from './plugins/index.js';
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { SignedAuthToken } from
|
|
2
|
-
import { PublishOptions } from
|
|
3
|
-
export interface KickOutOptions {
|
|
4
|
-
channel: string;
|
|
5
|
-
message: string;
|
|
6
|
-
}
|
|
1
|
+
import { SignedAuthToken } from '@socket-mesh/auth';
|
|
2
|
+
import { PublishOptions } from '@socket-mesh/channels';
|
|
7
3
|
export type ClientPrivateMap = {
|
|
8
4
|
'#kickOut': (options: KickOutOptions) => void;
|
|
9
|
-
'#setAuthToken': (token: SignedAuthToken) => void;
|
|
10
|
-
'#removeAuthToken': () => void;
|
|
11
5
|
'#publish': (options: PublishOptions) => void;
|
|
6
|
+
'#removeAuthToken': () => void;
|
|
7
|
+
'#setAuthToken': (token: SignedAuthToken) => void;
|
|
12
8
|
};
|
|
9
|
+
export interface KickOutOptions {
|
|
10
|
+
channel: string;
|
|
11
|
+
message: string;
|
|
12
|
+
}
|
package/dist/maps/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
1
|
+
export * from './client-map.js';
|
|
2
|
+
export * from './server-map.js';
|
package/dist/maps/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
1
|
+
export * from './client-map.js';
|
|
2
|
+
export * from './server-map.js';
|
|
@@ -1,22 +1,19 @@
|
|
|
1
|
-
import { SignedAuthToken } from
|
|
2
|
-
import { ChannelOptions, PublishOptions } from
|
|
3
|
-
export interface
|
|
1
|
+
import { SignedAuthToken } from '@socket-mesh/auth';
|
|
2
|
+
import { ChannelOptions, PublishOptions } from '@socket-mesh/channels';
|
|
3
|
+
export interface HandshakeAuthenticatedStatus {
|
|
4
4
|
authToken: SignedAuthToken;
|
|
5
|
-
}
|
|
6
|
-
export type HandshakeStatus = HandshakeErrorStatus | HandshakeAuthenticatedStatus;
|
|
7
|
-
export interface HandshakeErrorStatus {
|
|
8
5
|
id: string;
|
|
9
6
|
pingTimeoutMs: number;
|
|
10
|
-
authError: Error;
|
|
11
7
|
}
|
|
12
|
-
export interface
|
|
8
|
+
export interface HandshakeErrorStatus {
|
|
9
|
+
authError: Error;
|
|
13
10
|
id: string;
|
|
14
11
|
pingTimeoutMs: number;
|
|
15
|
-
authToken: SignedAuthToken;
|
|
16
12
|
}
|
|
17
|
-
export interface
|
|
18
|
-
|
|
13
|
+
export interface HandshakeOptions {
|
|
14
|
+
authToken: SignedAuthToken;
|
|
19
15
|
}
|
|
16
|
+
export type HandshakeStatus = HandshakeAuthenticatedStatus | HandshakeErrorStatus;
|
|
20
17
|
export type ServerPrivateMap = {
|
|
21
18
|
'#authenticate': (authToken: string) => void;
|
|
22
19
|
'#handshake': (options: HandshakeOptions) => HandshakeStatus;
|
|
@@ -25,3 +22,6 @@ export type ServerPrivateMap = {
|
|
|
25
22
|
'#subscribe': (options: SubscribeOptions) => void;
|
|
26
23
|
'#unsubscribe': (channelName: string) => void;
|
|
27
24
|
};
|
|
25
|
+
export interface SubscribeOptions extends ChannelOptions {
|
|
26
|
+
channel: string;
|
|
27
|
+
}
|
|
@@ -1,41 +1,41 @@
|
|
|
1
|
-
import { MethodMap, PrivateMethodMap, PublicMethodMap, ServiceMap } from
|
|
2
|
-
import { Plugin, SendRequestPluginArgs, SendResponsePluginArgs } from
|
|
1
|
+
import { MethodMap, PrivateMethodMap, PublicMethodMap, ServiceMap } from '@socket-mesh/core';
|
|
2
|
+
import { Plugin, SendRequestPluginArgs, SendResponsePluginArgs } from '@socket-mesh/core';
|
|
3
3
|
export interface BatchingPluginOptions {
|
|
4
|
-
batchOnHandshakeDuration?: number | false;
|
|
5
4
|
batchInterval?: number;
|
|
5
|
+
batchOnHandshakeDuration?: false | number;
|
|
6
6
|
}
|
|
7
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> {
|
|
8
|
-
batchOnHandshakeDuration: number | boolean;
|
|
9
|
-
batchInterval: number;
|
|
10
8
|
private _batchingIntervalId;
|
|
11
9
|
private _handshakeTimeoutId;
|
|
12
10
|
private _isBatching;
|
|
13
|
-
|
|
11
|
+
batchInterval: number;
|
|
12
|
+
batchOnHandshakeDuration: boolean | number;
|
|
14
13
|
type: string;
|
|
14
|
+
constructor(options?: BatchingPluginOptions);
|
|
15
15
|
cancelBatching(): void;
|
|
16
16
|
protected abstract flush(): void;
|
|
17
17
|
get isBatching(): boolean;
|
|
18
|
-
onReady(): void;
|
|
19
18
|
onDisconnected(): void;
|
|
20
|
-
|
|
19
|
+
onReady(): void;
|
|
21
20
|
private start;
|
|
22
|
-
|
|
21
|
+
startBatching(): void;
|
|
23
22
|
private stop;
|
|
23
|
+
stopBatching(): void;
|
|
24
24
|
}
|
|
25
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> {
|
|
26
|
-
private _requests;
|
|
27
26
|
private _continue;
|
|
27
|
+
private _requests;
|
|
28
|
+
type: 'requestBatching';
|
|
28
29
|
constructor(options?: BatchingPluginOptions);
|
|
29
30
|
cancelBatching(): void;
|
|
30
31
|
protected flush(): void;
|
|
31
|
-
sendRequest({
|
|
32
|
-
type: 'requestBatching';
|
|
32
|
+
sendRequest({ cont, requests }: SendRequestPluginArgs<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>): void;
|
|
33
33
|
}
|
|
34
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> {
|
|
35
|
-
private _responses;
|
|
36
35
|
private _continue;
|
|
36
|
+
private _responses;
|
|
37
|
+
type: 'responseBatching';
|
|
37
38
|
constructor(options?: BatchingPluginOptions);
|
|
38
39
|
protected flush(): void;
|
|
39
|
-
sendResponse({
|
|
40
|
-
type: 'responseBatching';
|
|
40
|
+
sendResponse({ cont, responses }: SendResponsePluginArgs<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>): void;
|
|
41
41
|
}
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
export class BatchingPlugin {
|
|
2
|
+
_batchingIntervalId;
|
|
3
|
+
_handshakeTimeoutId;
|
|
4
|
+
_isBatching;
|
|
5
|
+
batchInterval;
|
|
6
|
+
batchOnHandshakeDuration;
|
|
7
|
+
type;
|
|
2
8
|
constructor(options) {
|
|
3
9
|
this._isBatching = false;
|
|
4
10
|
this.batchInterval = options?.batchInterval || 50;
|
|
@@ -16,6 +22,9 @@ export class BatchingPlugin {
|
|
|
16
22
|
get isBatching() {
|
|
17
23
|
return this._isBatching || this._batchingIntervalId !== null;
|
|
18
24
|
}
|
|
25
|
+
onDisconnected() {
|
|
26
|
+
this.cancelBatching();
|
|
27
|
+
}
|
|
19
28
|
onReady() {
|
|
20
29
|
if (this._isBatching) {
|
|
21
30
|
this.start();
|
|
@@ -27,13 +36,6 @@ export class BatchingPlugin {
|
|
|
27
36
|
}, this.batchOnHandshakeDuration);
|
|
28
37
|
}
|
|
29
38
|
}
|
|
30
|
-
onDisconnected() {
|
|
31
|
-
this.cancelBatching();
|
|
32
|
-
}
|
|
33
|
-
startBatching() {
|
|
34
|
-
this._isBatching = true;
|
|
35
|
-
this.start();
|
|
36
|
-
}
|
|
37
39
|
start() {
|
|
38
40
|
if (this._batchingIntervalId !== null) {
|
|
39
41
|
return;
|
|
@@ -42,9 +44,9 @@ export class BatchingPlugin {
|
|
|
42
44
|
this.flush();
|
|
43
45
|
}, this.batchInterval);
|
|
44
46
|
}
|
|
45
|
-
|
|
46
|
-
this._isBatching =
|
|
47
|
-
this.
|
|
47
|
+
startBatching() {
|
|
48
|
+
this._isBatching = true;
|
|
49
|
+
this.start();
|
|
48
50
|
}
|
|
49
51
|
stop() {
|
|
50
52
|
if (this._batchingIntervalId !== null) {
|
|
@@ -57,8 +59,15 @@ export class BatchingPlugin {
|
|
|
57
59
|
}
|
|
58
60
|
this.flush();
|
|
59
61
|
}
|
|
62
|
+
stopBatching() {
|
|
63
|
+
this._isBatching = false;
|
|
64
|
+
this.stop();
|
|
65
|
+
}
|
|
60
66
|
}
|
|
61
67
|
export class RequestBatchingPlugin extends BatchingPlugin {
|
|
68
|
+
_continue;
|
|
69
|
+
_requests;
|
|
70
|
+
type;
|
|
62
71
|
constructor(options) {
|
|
63
72
|
super(options);
|
|
64
73
|
this.type = 'requestBatching';
|
|
@@ -72,12 +81,14 @@ export class RequestBatchingPlugin extends BatchingPlugin {
|
|
|
72
81
|
}
|
|
73
82
|
flush() {
|
|
74
83
|
if (this._requests.length) {
|
|
75
|
-
this._continue
|
|
84
|
+
if (this._continue) {
|
|
85
|
+
this._continue(this._requests);
|
|
86
|
+
this._continue = null;
|
|
87
|
+
}
|
|
76
88
|
this._requests = [];
|
|
77
|
-
this._continue = null;
|
|
78
89
|
}
|
|
79
90
|
}
|
|
80
|
-
sendRequest({
|
|
91
|
+
sendRequest({ cont, requests }) {
|
|
81
92
|
if (!this.isBatching) {
|
|
82
93
|
cont(requests);
|
|
83
94
|
return;
|
|
@@ -87,6 +98,9 @@ export class RequestBatchingPlugin extends BatchingPlugin {
|
|
|
87
98
|
}
|
|
88
99
|
}
|
|
89
100
|
export class ResponseBatchingPlugin extends BatchingPlugin {
|
|
101
|
+
_continue;
|
|
102
|
+
_responses;
|
|
103
|
+
type;
|
|
90
104
|
constructor(options) {
|
|
91
105
|
super(options);
|
|
92
106
|
this.type = 'responseBatching';
|
|
@@ -95,12 +109,14 @@ export class ResponseBatchingPlugin extends BatchingPlugin {
|
|
|
95
109
|
}
|
|
96
110
|
flush() {
|
|
97
111
|
if (this._responses.length) {
|
|
98
|
-
this._continue
|
|
112
|
+
if (this._continue) {
|
|
113
|
+
this._continue(this._responses);
|
|
114
|
+
this._continue = null;
|
|
115
|
+
}
|
|
99
116
|
this._responses = [];
|
|
100
|
-
this._continue = null;
|
|
101
117
|
}
|
|
102
118
|
}
|
|
103
|
-
sendResponse({
|
|
119
|
+
sendResponse({ cont, responses }) {
|
|
104
120
|
if (!this.isBatching) {
|
|
105
121
|
cont(responses);
|
|
106
122
|
return;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { MessageRawPluginArgs, MethodMap, Plugin, PluginArgs, PrivateMethodMap, PublicMethodMap, ServiceMap } from '@socket-mesh/core';
|
|
2
|
+
import { RawData } from 'ws';
|
|
3
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> {
|
|
4
|
-
type: 'inOrder';
|
|
5
4
|
private readonly _inboundMessageStream;
|
|
5
|
+
type: 'inOrder';
|
|
6
6
|
constructor();
|
|
7
7
|
handleInboundMessageStream(): void;
|
|
8
8
|
onEnd({ transport }: PluginArgs<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>): void;
|
|
9
|
-
onMessageRaw(options: MessageRawPluginArgs<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>): Promise<
|
|
9
|
+
onMessageRaw(options: MessageRawPluginArgs<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>): Promise<RawData | string>;
|
|
10
10
|
}
|