@socket-mesh/server 18.1.5 → 19.1.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.
- package/dist/broker/exchange.d.ts +6 -1
- package/dist/broker/exchange.js +4 -0
- package/dist/broker/simple-exchange.d.ts +0 -1
- package/dist/broker/simple-exchange.js +4 -3
- package/dist/events/index.d.ts +29 -29
- package/dist/handlers/authenticate.js +8 -6
- package/dist/handlers/handshake.js +5 -4
- package/dist/handlers/publish.d.ts +1 -1
- package/dist/handlers/remove-auth-token.d.ts +2 -2
- package/dist/handlers/server-request-handler.d.ts +1 -2
- package/dist/index.d.ts +4 -4
- package/dist/plugin/server-plugin.d.ts +14 -16
- package/dist/server-options.d.ts +10 -4
- package/dist/server-socket.d.ts +55 -8
- package/dist/server-socket.js +17 -4
- package/dist/server-transport.d.ts +20 -11
- package/dist/server-transport.js +16 -15
- package/dist/server.d.ts +108 -56
- package/dist/server.js +81 -15
- package/package.json +1 -1
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import { ChannelMap, Channels } from '@socket-mesh/channels';
|
|
1
|
+
import { ChannelMap, Channels, ChannelsOptions } from '@socket-mesh/channels';
|
|
2
|
+
interface ExchangeOptions extends ChannelsOptions {
|
|
3
|
+
id: string;
|
|
4
|
+
}
|
|
2
5
|
export declare abstract class Exchange<T extends ChannelMap> extends Channels<T> {
|
|
3
6
|
id: string;
|
|
7
|
+
constructor(options: ExchangeOptions);
|
|
4
8
|
}
|
|
9
|
+
export {};
|
package/dist/broker/exchange.js
CHANGED
|
@@ -3,7 +3,6 @@ import { Broker } from './broker.js';
|
|
|
3
3
|
import { Exchange } from './exchange.js';
|
|
4
4
|
export declare class SimpleExchange<T extends ChannelMap> extends Exchange<T> {
|
|
5
5
|
private readonly _broker;
|
|
6
|
-
readonly id: string;
|
|
7
6
|
constructor(broker: Broker<T>, options?: ChannelsOptions);
|
|
8
7
|
invokePublish<U extends keyof T & string>(channelName: U, data: T[U]): Promise<void>;
|
|
9
8
|
transmit(event: '#publish', packet: PublishOptions): Promise<void>;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { Exchange } from './exchange.js';
|
|
2
2
|
export class SimpleExchange extends Exchange {
|
|
3
3
|
_broker;
|
|
4
|
-
id;
|
|
5
4
|
constructor(broker, options) {
|
|
6
|
-
super(
|
|
7
|
-
|
|
5
|
+
super({
|
|
6
|
+
...(options || {}),
|
|
7
|
+
id: 'exchange'
|
|
8
|
+
});
|
|
8
9
|
this._broker = broker;
|
|
9
10
|
}
|
|
10
11
|
async invokePublish(channelName, data) {
|
package/dist/events/index.d.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import { ChannelMap, SubscribeEvent, SubscribeFailEvent, SubscribeStateChangeEvent, UnsubscribeEvent } from '@socket-mesh/channels';
|
|
2
|
-
import { ClientSocket, ServerPrivateMap } from '@socket-mesh/client';
|
|
3
|
-
import { AuthenticateEvent, AuthStateChangeEvent, BadAuthTokenEvent, ConnectEvent, ConnectingEvent, DeauthenticateEvent, DisconnectEvent, MessageEvent, PingEvent, PongEvent, PrivateMethodMap, PublicMethodMap, RemoveAuthTokenEvent,
|
|
2
|
+
import { ClientPrivateMap, ClientSocket, ServerPrivateMap } from '@socket-mesh/client';
|
|
3
|
+
import { AuthenticateEvent, AuthStateChangeEvent, BadAuthTokenEvent, ConnectEvent, ConnectingEvent, DeauthenticateEvent, DisconnectEvent, MessageEvent, PingEvent, PongEvent, PrivateMethodMap, PublicMethodMap, RemoveAuthTokenEvent, CloseEvent as SCloseEvent, ErrorEvent as SErrorEvent, ServiceMap, TypedRequestEvent, TypedResponseEvent } from '@socket-mesh/core';
|
|
4
4
|
import { IncomingMessage } from 'http';
|
|
5
5
|
import { ServerSocket } from '../server-socket.js';
|
|
6
6
|
export interface CloseEvent {
|
|
7
7
|
}
|
|
8
|
-
export interface ConnectionEvent<
|
|
9
|
-
socket: ServerSocket<TIncoming, TChannel, TService,
|
|
8
|
+
export interface ConnectionEvent<TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap, TState extends object, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object> {
|
|
9
|
+
socket: ServerSocket<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
10
10
|
upgradeReq: IncomingMessage;
|
|
11
11
|
}
|
|
12
12
|
export interface ErrorEvent {
|
|
13
13
|
error: Error;
|
|
14
14
|
}
|
|
15
|
-
export interface HandshakeEvent<
|
|
16
|
-
socket: ServerSocket<TIncoming, TChannel, TService,
|
|
15
|
+
export interface HandshakeEvent<TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap, TState extends object, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object> {
|
|
16
|
+
socket: ServerSocket<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
17
17
|
}
|
|
18
18
|
export interface HeadersEvent {
|
|
19
19
|
headers: string[];
|
|
@@ -21,29 +21,29 @@ export interface HeadersEvent {
|
|
|
21
21
|
}
|
|
22
22
|
export interface ListeningEvent {
|
|
23
23
|
}
|
|
24
|
-
export type ServerEvent<
|
|
25
|
-
export interface ServerSocketEvent<
|
|
26
|
-
socket: ClientSocket<
|
|
27
|
-
}
|
|
28
|
-
export type SocketAuthenticateEvent<
|
|
29
|
-
export type SocketAuthStateChangeEvent<
|
|
30
|
-
export type SocketBadAuthTokenEvent<
|
|
31
|
-
export type SocketCloseEvent<
|
|
32
|
-
export type SocketConnectEvent<
|
|
33
|
-
export type SocketConnectingEvent<
|
|
34
|
-
export type SocketDeauthenticateEvent<
|
|
35
|
-
export type SocketDisconnectEvent<
|
|
36
|
-
export type SocketErrorEvent<
|
|
37
|
-
export type SocketMessageEvent<
|
|
38
|
-
export type SocketPingEvent<
|
|
39
|
-
export type SocketPongEvent<
|
|
40
|
-
export type SocketRemoveAuthTokenEvent<
|
|
41
|
-
export type SocketRequestEvent<
|
|
42
|
-
export type SocketResponseEvent<
|
|
43
|
-
export type SocketSubscribeEvent<
|
|
44
|
-
export type SocketSubscribeFailEvent<
|
|
45
|
-
export type SocketSubscribeStateChangeEvent<
|
|
46
|
-
export type SocketUnsubscribeEvent<
|
|
24
|
+
export type ServerEvent<TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap, TState extends object, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object> = CloseEvent | ConnectionEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState> | ErrorEvent | HandshakeEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState> | HeadersEvent | ListeningEvent | SocketAuthenticateEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState> | SocketAuthStateChangeEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState> | SocketBadAuthTokenEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState> | SocketCloseEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState> | SocketConnectEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState> | SocketConnectingEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState> | SocketDeauthenticateEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState> | SocketDisconnectEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState> | SocketErrorEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState> | SocketMessageEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState> | SocketPingEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState> | SocketPongEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState> | SocketRemoveAuthTokenEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState> | SocketRequestEvent<TIncoming, TService, TPrivateIncoming> | SocketResponseEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState> | WarningEvent;
|
|
25
|
+
export interface ServerSocketEvent<TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap, TState extends object, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object> {
|
|
26
|
+
socket: ClientSocket<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateOutgoing> | ServerSocket<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
27
|
+
}
|
|
28
|
+
export type SocketAuthenticateEvent<TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap, TState extends object, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object> = AuthenticateEvent & ServerSocketEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
29
|
+
export type SocketAuthStateChangeEvent<TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap, TState extends object, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object> = AuthStateChangeEvent & ServerSocketEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
30
|
+
export type SocketBadAuthTokenEvent<TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap, TState extends object, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object> = BadAuthTokenEvent & ServerSocketEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
31
|
+
export type SocketCloseEvent<TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap, TState extends object, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object> = SCloseEvent & ServerSocketEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
32
|
+
export type SocketConnectEvent<TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap, TState extends object, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object> = ConnectEvent & ServerSocketEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
33
|
+
export type SocketConnectingEvent<TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap, TState extends object, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object> = ConnectingEvent & ServerSocketEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
34
|
+
export type SocketDeauthenticateEvent<TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap, TState extends object, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object> = DeauthenticateEvent & ServerSocketEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
35
|
+
export type SocketDisconnectEvent<TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap, TState extends object, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object> = DisconnectEvent & ServerSocketEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
36
|
+
export type SocketErrorEvent<TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap, TState extends object, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object> = SErrorEvent & ServerSocketEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
37
|
+
export type SocketMessageEvent<TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap, TState extends object, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object> = MessageEvent & ServerSocketEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
38
|
+
export type SocketPingEvent<TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap, TState extends object, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object> = PingEvent & ServerSocketEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
39
|
+
export type SocketPongEvent<TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap, TState extends object, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object> = PongEvent & ServerSocketEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
40
|
+
export type SocketRemoveAuthTokenEvent<TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap, TState extends object, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object> = RemoveAuthTokenEvent & ServerSocketEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
41
|
+
export type SocketRequestEvent<TIncoming extends PublicMethodMap, TService extends ServiceMap, TPrivateIncoming extends PrivateMethodMap> = TypedRequestEvent<TIncoming & TPrivateIncoming & ServerPrivateMap, TService>;
|
|
42
|
+
export type SocketResponseEvent<TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap, TState extends object, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object> = TypedResponseEvent<TOutgoing, TPrivateOutgoing & ClientPrivateMap, TService> & ServerSocketEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
43
|
+
export type SocketSubscribeEvent<TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap, TState extends object, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object> = SubscribeEvent & ServerSocketEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
44
|
+
export type SocketSubscribeFailEvent<TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap, TState extends object, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object> = SubscribeFailEvent & ServerSocketEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
45
|
+
export type SocketSubscribeStateChangeEvent<TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap, TState extends object, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object> = SubscribeStateChangeEvent & ServerSocketEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
46
|
+
export type SocketUnsubscribeEvent<TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap, TState extends object, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object> = UnsubscribeEvent & ServerSocketEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
47
47
|
export interface WarningEvent {
|
|
48
48
|
warning: Error;
|
|
49
49
|
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { toError } from '@socket-mesh/core';
|
|
1
2
|
import { AuthTokenError, AuthTokenExpiredError, AuthTokenInvalidError, AuthTokenNotBeforeError, InvalidActionError } from '@socket-mesh/errors';
|
|
3
|
+
import jwt from 'jsonwebtoken';
|
|
2
4
|
const HANDSHAKE_REJECTION_STATUS_CODE = 4008;
|
|
3
5
|
;
|
|
4
6
|
export async function authenticateHandler({ isRpc, options: signedAuthToken, socket, transport }) {
|
|
@@ -40,17 +42,17 @@ export async function processAuthentication(socket, transport, authInfo) {
|
|
|
40
42
|
return await transport.setAuthorization(authInfo.signedAuthToken, authInfo.authToken);
|
|
41
43
|
}
|
|
42
44
|
function processTokenError(err) {
|
|
43
|
-
if (err
|
|
45
|
+
if (err instanceof jwt.TokenExpiredError) {
|
|
44
46
|
return new AuthTokenExpiredError(err.message, err.expiredAt);
|
|
45
47
|
}
|
|
46
|
-
if (err
|
|
47
|
-
return new AuthTokenInvalidError(err.message);
|
|
48
|
-
}
|
|
49
|
-
if (err.name === 'NotBeforeError' && 'date' in err) {
|
|
48
|
+
if (err instanceof jwt.NotBeforeError) {
|
|
50
49
|
// In this case, the token is good; it's just not active yet.
|
|
51
50
|
return new AuthTokenNotBeforeError(err.message, err.date);
|
|
52
51
|
}
|
|
53
|
-
|
|
52
|
+
if (err instanceof jwt.JsonWebTokenError) {
|
|
53
|
+
return new AuthTokenInvalidError(err.message);
|
|
54
|
+
}
|
|
55
|
+
return new AuthTokenError(toError(err).message);
|
|
54
56
|
}
|
|
55
57
|
export async function validateAuthToken(auth, authToken, verificationOptions) {
|
|
56
58
|
try {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { wait } from '@socket-mesh/core';
|
|
1
|
+
import { toError, wait } from '@socket-mesh/core';
|
|
2
2
|
import { dehydrateError } from '@socket-mesh/errors';
|
|
3
3
|
import { processAuthentication, validateAuthToken } from './authenticate.js';
|
|
4
4
|
const HANDSHAKE_REJECTION_STATUS_CODE = 4008;
|
|
@@ -18,10 +18,11 @@ export async function handshakeHandler({ options, socket, transport }) {
|
|
|
18
18
|
});
|
|
19
19
|
}
|
|
20
20
|
catch (err) {
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
const error = toError(err);
|
|
22
|
+
if (!('statusCode' in error)) {
|
|
23
|
+
error.statusCode = HANDSHAKE_REJECTION_STATUS_CODE;
|
|
23
24
|
}
|
|
24
|
-
throw
|
|
25
|
+
throw error;
|
|
25
26
|
}
|
|
26
27
|
}
|
|
27
28
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { PublishOptions } from '@socket-mesh/channels';
|
|
2
2
|
import { ServerRequestHandlerArgs } from './server-request-handler.js';
|
|
3
|
-
export declare function publishHandler({ options, socket, transport }: ServerRequestHandlerArgs<PublishOptions, {}, {
|
|
3
|
+
export declare function publishHandler({ options, socket, transport }: ServerRequestHandlerArgs<PublishOptions, {}, {}, {
|
|
4
4
|
[channel: string]: any;
|
|
5
5
|
}>): Promise<void>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ClientPrivateMap
|
|
1
|
+
import { ClientPrivateMap } from '@socket-mesh/client';
|
|
2
2
|
import { SocketTransport } from '@socket-mesh/core';
|
|
3
3
|
import { ServerSocketState } from '../server-socket-state.js';
|
|
4
|
-
export declare function deauthenticate(transport: SocketTransport<
|
|
4
|
+
export declare function deauthenticate(transport: SocketTransport<{}, {}, ServerSocketState, {}, {}, ClientPrivateMap>): Promise<boolean>;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { ChannelMap } from '@socket-mesh/channels';
|
|
2
|
-
import { ClientPrivateMap, ServerPrivateMap } from '@socket-mesh/client';
|
|
3
2
|
import { PrivateMethodMap, PublicMethodMap, RequestHandlerArgs, ServiceMap } from '@socket-mesh/core';
|
|
4
3
|
import { ServerSocketState } from '../server-socket-state.js';
|
|
5
4
|
import { ServerSocket } from '../server-socket.js';
|
|
6
5
|
import { ServerTransport } from '../server-transport.js';
|
|
7
|
-
export type ServerRequestHandlerArgs<TOptions, TIncoming extends PublicMethodMap =
|
|
6
|
+
export type ServerRequestHandlerArgs<TOptions, TIncoming extends PublicMethodMap = any, TOutgoing extends PublicMethodMap = any, TChannel extends ChannelMap = any, TService extends ServiceMap = any, TState extends object = any, TPrivateIncoming extends PrivateMethodMap = any, TPrivateOutgoing extends PrivateMethodMap = any, TServerState extends object = any> = RequestHandlerArgs<TOptions, TState & ServerSocketState, ServerSocket<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>, ServerTransport<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
package/dist/index.d.ts
CHANGED
|
@@ -17,7 +17,7 @@ export type { PluginType } from '@socket-mesh/core';
|
|
|
17
17
|
* @return {AGServer} websocket cluster server
|
|
18
18
|
* @api public
|
|
19
19
|
*/
|
|
20
|
-
export declare function attach<
|
|
20
|
+
export declare function attach<TIncoming extends PublicMethodMap = {}, TOutgoing extends PublicMethodMap = {}, TChannel extends ChannelMap = {}, TService extends ServiceMap = {}, TState extends object = {}, TPrivateIncoming extends PrivateMethodMap = {}, TPrivateOutgoing extends PrivateMethodMap = {}, TServerState extends object = {}>(server: http.Server, options?: ServerOptions<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): Server<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
21
21
|
/**
|
|
22
22
|
* Creates an http.Server exclusively used for WS upgrades.
|
|
23
23
|
*
|
|
@@ -27,6 +27,6 @@ export declare function attach<TChannel extends ChannelMap = {}, TService extend
|
|
|
27
27
|
* @return {AGServer} websocket cluster server
|
|
28
28
|
* @api public
|
|
29
29
|
*/
|
|
30
|
-
export declare function listen<TIncoming extends PublicMethodMap = {},
|
|
31
|
-
export declare function listen<TIncoming extends PublicMethodMap = {},
|
|
32
|
-
export declare function listen<TIncoming extends PublicMethodMap = {},
|
|
30
|
+
export declare function listen<TIncoming extends PublicMethodMap = {}, TOutgoing extends PublicMethodMap = {}, TChannel extends ChannelMap = {}, TService extends ServiceMap = {}, TState extends object = {}, TPrivateIncoming extends PrivateMethodMap = {}, TPrivateOutgoing extends PrivateMethodMap = {}, TServerState extends object = {}>(): Server<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
31
|
+
export declare function listen<TIncoming extends PublicMethodMap = {}, TOutgoing extends PublicMethodMap = {}, TChannel extends ChannelMap = {}, TService extends ServiceMap = {}, TState extends object = {}, TPrivateIncoming extends PrivateMethodMap = {}, TPrivateOutgoing extends PrivateMethodMap = {}, TServerState extends object = {}>(port: number, options: ServerOptions<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): Server<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
32
|
+
export declare function listen<TIncoming extends PublicMethodMap = {}, TOutgoing extends PublicMethodMap = {}, TChannel extends ChannelMap = {}, TService extends ServiceMap = {}, TState extends object = {}, TPrivateIncoming extends PrivateMethodMap = {}, TPrivateOutgoing extends PrivateMethodMap = {}, TServerState extends object = {}>(port: number, options: ServerOptions<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>, fn: () => void): Server<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
@@ -1,33 +1,31 @@
|
|
|
1
1
|
import { ChannelMap, ChannelOptions } from '@socket-mesh/channels';
|
|
2
|
-
import { ClientPrivateMap, ServerPrivateMap } from '@socket-mesh/client';
|
|
3
2
|
import { Plugin, PrivateMethodMap, PublicMethodMap, ServiceMap } from '@socket-mesh/core';
|
|
4
3
|
import { IncomingMessage } from 'http';
|
|
5
4
|
import { AuthInfo } from '../handlers/authenticate.js';
|
|
6
|
-
import { ServerSocketState } from '../server-socket-state.js';
|
|
7
5
|
import { ServerSocket } from '../server-socket.js';
|
|
8
6
|
import { ServerTransport } from '../server-transport.js';
|
|
9
|
-
export interface HandshakePluginArgs<
|
|
7
|
+
export interface HandshakePluginArgs<TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap, TState extends object, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object> {
|
|
10
8
|
authInfo: AuthInfo;
|
|
11
|
-
socket: ServerSocket<TIncoming, TChannel, TService,
|
|
12
|
-
transport: ServerTransport<TIncoming, TChannel, TService,
|
|
9
|
+
socket: ServerSocket<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
10
|
+
transport: ServerTransport<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
13
11
|
}
|
|
14
|
-
export interface PublishPluginArgs<
|
|
12
|
+
export interface PublishPluginArgs<TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap, TState extends object, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object> {
|
|
15
13
|
channel: string;
|
|
16
14
|
data: any;
|
|
17
|
-
socket: ServerSocket<TIncoming, TChannel, TService,
|
|
18
|
-
transport: ServerTransport<TIncoming, TChannel, TService,
|
|
15
|
+
socket: ServerSocket<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
16
|
+
transport: ServerTransport<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
19
17
|
}
|
|
20
|
-
export interface ServerPlugin<TIncoming extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap,
|
|
18
|
+
export interface ServerPlugin<TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap, TState extends object, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object> extends Plugin<ServerSocket<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>, ServerTransport<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>> {
|
|
21
19
|
onAuthenticate?: (authInfo: AuthInfo) => void;
|
|
22
20
|
onConnection?: (request: IncomingMessage) => Promise<void>;
|
|
23
|
-
onHandshake?: (options: HandshakePluginArgs<TChannel, TService,
|
|
24
|
-
onPublishIn?: (options: PublishPluginArgs<TChannel, TService,
|
|
25
|
-
onPublishOut?: (options: PublishPluginArgs<TChannel, TService,
|
|
26
|
-
onSubscribe?: (options: SubscribePluginArgs<TChannel, TService,
|
|
21
|
+
onHandshake?: (options: HandshakePluginArgs<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>) => Promise<void>;
|
|
22
|
+
onPublishIn?: (options: PublishPluginArgs<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>) => Promise<any>;
|
|
23
|
+
onPublishOut?: (options: PublishPluginArgs<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>) => Promise<any>;
|
|
24
|
+
onSubscribe?: (options: SubscribePluginArgs<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>) => Promise<void>;
|
|
27
25
|
}
|
|
28
|
-
export interface SubscribePluginArgs<
|
|
26
|
+
export interface SubscribePluginArgs<TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap, TState extends object, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object> {
|
|
29
27
|
channel: string;
|
|
30
28
|
options: ChannelOptions;
|
|
31
|
-
socket: ServerSocket<TIncoming, TChannel, TService,
|
|
32
|
-
transport: ServerTransport<TIncoming, TChannel, TService,
|
|
29
|
+
socket: ServerSocket<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
30
|
+
transport: ServerTransport<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
33
31
|
}
|
package/dist/server-options.d.ts
CHANGED
|
@@ -1,25 +1,31 @@
|
|
|
1
1
|
import { AuthEngine, AuthOptions } from '@socket-mesh/auth-engine';
|
|
2
2
|
import { ChannelMap } from '@socket-mesh/channels';
|
|
3
|
-
import {
|
|
3
|
+
import { ServerPrivateMap } from '@socket-mesh/client';
|
|
4
4
|
import { CallIdGenerator, HandlerMap, PrivateMethodMap, PublicMethodMap, ServiceMap, StreamCleanupMode } from '@socket-mesh/core';
|
|
5
5
|
import { CodecEngine } from '@socket-mesh/formatter';
|
|
6
6
|
import { ServerOptions as WebSocketServerOptions } from 'ws';
|
|
7
7
|
import { Broker } from './broker/broker.js';
|
|
8
8
|
import { ServerPlugin } from './plugin/server-plugin.js';
|
|
9
|
-
|
|
9
|
+
import { ServerSocketState } from './server-socket-state.js';
|
|
10
|
+
import { ServerSocket } from './server-socket.js';
|
|
11
|
+
import { ServerTransport } from './server-transport.js';
|
|
12
|
+
export interface ServerOptions<TIncoming extends PublicMethodMap = {}, TOutgoing extends PublicMethodMap = {}, TChannel extends ChannelMap = {}, TService extends ServiceMap = {}, TState extends object = {}, TPrivateIncoming extends PrivateMethodMap = {}, TPrivateOutgoing extends PrivateMethodMap = {}, TServerState extends object = {}> extends WebSocketServerOptions {
|
|
10
13
|
ackTimeoutMs?: number;
|
|
11
14
|
allowClientPublish?: boolean;
|
|
12
15
|
authEngine?: AuthEngine | AuthOptions;
|
|
13
16
|
brokerEngine?: Broker<TChannel>;
|
|
14
17
|
callIdGenerator?: CallIdGenerator;
|
|
15
18
|
codecEngine?: CodecEngine;
|
|
16
|
-
handlers?: HandlerMap<TIncoming & TPrivateIncoming & ServerPrivateMap, TOutgoing, TPrivateOutgoing
|
|
19
|
+
handlers?: HandlerMap<TIncoming & TPrivateIncoming & ServerPrivateMap, TState & ServerSocketState, ServerSocket<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>, ServerTransport<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
17
20
|
handshakeTimeoutMs?: number;
|
|
18
21
|
isPingTimeoutDisabled?: boolean;
|
|
19
22
|
origins?: string;
|
|
20
23
|
pingIntervalMs?: number;
|
|
21
24
|
pingTimeoutMs?: number;
|
|
22
|
-
plugins?: ServerPlugin<TIncoming, TChannel, TService,
|
|
25
|
+
plugins?: ServerPlugin<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>[];
|
|
26
|
+
serviceHandlers?: {
|
|
27
|
+
[TServiceName in keyof TService & string]?: HandlerMap<TService[TServiceName], TState & ServerSocketState, ServerSocket<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>, ServerTransport<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
28
|
+
};
|
|
23
29
|
socketChannelLimit?: number;
|
|
24
30
|
socketStreamCleanupMode?: StreamCleanupMode;
|
|
25
31
|
strictHandshake?: boolean;
|
package/dist/server-socket.d.ts
CHANGED
|
@@ -1,30 +1,77 @@
|
|
|
1
1
|
import { ChannelMap } from '@socket-mesh/channels';
|
|
2
2
|
import { ClientPrivateMap, ServerPrivateMap } from '@socket-mesh/client';
|
|
3
|
-
import {
|
|
3
|
+
import { AuthenticateEvent, AuthStateChangeEvent, BadAuthTokenEvent, BaseSocket, BaseSocketOptions, 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 { IncomingMessage } from 'http';
|
|
5
6
|
import { WebSocket } from 'ws';
|
|
6
7
|
import { Exchange } from './broker/exchange.js';
|
|
7
8
|
import { ServerPlugin } from './plugin/server-plugin.js';
|
|
8
9
|
import { ServerSocketState } from './server-socket-state.js';
|
|
9
10
|
import { Server } from './server.js';
|
|
10
|
-
export interface ServerSocketOptions<TIncoming extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap,
|
|
11
|
-
handlers: HandlerMap<TIncoming & TPrivateIncoming & ServerPrivateMap, TOutgoing, TPrivateOutgoing & ClientPrivateMap, TService, TState & ServerSocketState>;
|
|
11
|
+
export interface ServerSocketOptions<TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TChannel extends ChannelMap, TService extends ServiceMap, TState extends object, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object> extends BaseSocketOptions<TState & ServerSocketState> {
|
|
12
12
|
id?: string;
|
|
13
|
-
plugins?: ServerPlugin<TIncoming, TChannel, TService,
|
|
13
|
+
plugins?: ServerPlugin<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>[];
|
|
14
14
|
request: IncomingMessage;
|
|
15
|
-
server: Server<TIncoming, TChannel, TService,
|
|
15
|
+
server: Server<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
16
16
|
service?: string;
|
|
17
17
|
socket: WebSocket;
|
|
18
18
|
}
|
|
19
|
-
export declare class ServerSocket<TIncoming extends PublicMethodMap = {}, TChannel extends ChannelMap = {}, TService extends ServiceMap = {},
|
|
19
|
+
export declare class ServerSocket<TIncoming extends PublicMethodMap = {}, TOutgoing extends PublicMethodMap = {}, TChannel extends ChannelMap = {}, TService extends ServiceMap = {}, TState extends object = {}, TPrivateIncoming extends PrivateMethodMap = {}, TPrivateOutgoing extends PrivateMethodMap = {}, TServerState extends object = {}> extends BaseSocket<TState & ServerSocketState> implements Socket<TIncoming & TPrivateIncoming & ServerPrivateMap, TOutgoing, TState & ServerSocketState, TService, {}, TPrivateOutgoing & ClientPrivateMap> {
|
|
20
20
|
private _serverTransport;
|
|
21
|
-
readonly server: Server<TIncoming, TChannel, TService,
|
|
22
|
-
constructor(options: ServerSocketOptions<TIncoming, TChannel, TService,
|
|
21
|
+
readonly server: Server<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
22
|
+
constructor(options: ServerSocketOptions<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>);
|
|
23
23
|
deauthenticate(rejectOnFailedDelivery?: boolean): Promise<boolean>;
|
|
24
|
+
emit(event: 'request', data: TypedRequestEvent<TIncoming & TPrivateIncoming & ServerPrivateMap, TService>): void;
|
|
25
|
+
emit(event: 'response', data: TypedResponseEvent<TOutgoing, TPrivateOutgoing & ClientPrivateMap, TService>): void;
|
|
26
|
+
emit(event: 'authStateChange', data: AuthStateChangeEvent): void;
|
|
27
|
+
emit(event: 'authenticate', data: AuthenticateEvent): void;
|
|
28
|
+
emit(event: 'badAuthToken', data: BadAuthTokenEvent): void;
|
|
29
|
+
emit(event: 'close', data: CloseEvent): void;
|
|
30
|
+
emit(event: 'connect', data: ConnectEvent): void;
|
|
31
|
+
emit(event: 'connectAbort', data: DisconnectEvent): void;
|
|
32
|
+
emit(event: 'connecting', data: ConnectingEvent): void;
|
|
33
|
+
emit(event: 'deauthenticate', data: DeauthenticateEvent): void;
|
|
34
|
+
emit(event: 'disconnect', data: DisconnectEvent): void;
|
|
35
|
+
emit(event: 'end'): void;
|
|
36
|
+
emit(event: 'error', data: ErrorEvent): void;
|
|
37
|
+
emit(event: 'message', data: MessageEvent): void;
|
|
38
|
+
emit(event: 'ping', data: PingEvent): void;
|
|
39
|
+
emit(event: 'pong', data: PongEvent): void;
|
|
40
|
+
emit(event: 'removeAuthToken', data: RemoveAuthTokenEvent): void;
|
|
41
|
+
emit(event: 'request', data: RequestEvent): void;
|
|
42
|
+
emit(event: 'response', data: ResponseEvent): void;
|
|
24
43
|
get exchange(): Exchange<TChannel>;
|
|
25
44
|
get id(): string;
|
|
45
|
+
invoke<TMethod extends keyof TOutgoing & string>(method: TMethod, arg?: Parameters<TOutgoing[TMethod]>[0]): Promise<FunctionReturnType<TOutgoing[TMethod]>>;
|
|
46
|
+
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]>>;
|
|
47
|
+
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]>>;
|
|
48
|
+
invoke<TMethod extends keyof TOutgoing & string>(options: InvokeMethodOptions<TOutgoing, TMethod>, arg?: Parameters<TOutgoing[TMethod]>[0]): Promise<FunctionReturnType<TOutgoing[TMethod]>>;
|
|
49
|
+
invoke<TMethod extends keyof (TPrivateOutgoing & ClientPrivateMap) & string>(method: TMethod, arg: Parameters<(TPrivateOutgoing & ClientPrivateMap)[TMethod]>[0]): Promise<FunctionReturnType<(TPrivateOutgoing & ClientPrivateMap)[TMethod]>>;
|
|
50
|
+
invoke<TMethod extends keyof (TPrivateOutgoing & ClientPrivateMap) & string>(options: InvokeMethodOptions<(TPrivateOutgoing & ClientPrivateMap), TMethod>, arg?: Parameters<(TPrivateOutgoing & ClientPrivateMap)[TMethod]>[0]): Promise<FunctionReturnType<(TPrivateOutgoing & ClientPrivateMap)[TMethod]>>;
|
|
26
51
|
kickOut(channel: string, message: string): Promise<void[]>;
|
|
52
|
+
listen(): DemuxedConsumableStream<StreamEvent<TypedSocketEvent<TIncoming & TPrivateIncoming & ServerPrivateMap, TOutgoing, TPrivateOutgoing & ClientPrivateMap, TService>>>;
|
|
53
|
+
listen(event: 'authStateChange'): DemuxedConsumableStream<AuthStateChangeEvent>;
|
|
54
|
+
listen(event: 'authenticate'): DemuxedConsumableStream<AuthenticateEvent>;
|
|
55
|
+
listen(event: 'badAuthToken'): DemuxedConsumableStream<BadAuthTokenEvent>;
|
|
56
|
+
listen(event: 'close'): DemuxedConsumableStream<CloseEvent>;
|
|
57
|
+
listen(event: 'connect'): DemuxedConsumableStream<ConnectEvent>;
|
|
58
|
+
listen(event: 'connectAbort'): DemuxedConsumableStream<DisconnectEvent>;
|
|
59
|
+
listen(event: 'connecting'): DemuxedConsumableStream<ConnectingEvent>;
|
|
60
|
+
listen(event: 'deauthenticate'): DemuxedConsumableStream<DeauthenticateEvent>;
|
|
61
|
+
listen(event: 'disconnect'): DemuxedConsumableStream<DisconnectEvent>;
|
|
62
|
+
listen(event: 'end'): DemuxedConsumableStream<void>;
|
|
63
|
+
listen(event: 'error'): DemuxedConsumableStream<ErrorEvent>;
|
|
64
|
+
listen(event: 'message'): DemuxedConsumableStream<MessageEvent>;
|
|
65
|
+
listen(event: 'ping'): DemuxedConsumableStream<PingEvent>;
|
|
66
|
+
listen(event: 'pong'): DemuxedConsumableStream<PongEvent>;
|
|
67
|
+
listen(event: 'removeAuthToken'): DemuxedConsumableStream<RemoveAuthTokenEvent>;
|
|
68
|
+
listen(event: 'request'): DemuxedConsumableStream<TypedRequestEvent<TIncoming & TPrivateIncoming & ServerPrivateMap, TService>>;
|
|
69
|
+
listen(event: 'response'): DemuxedConsumableStream<TypedResponseEvent<TOutgoing, TPrivateOutgoing & ClientPrivateMap, TService>>;
|
|
70
|
+
listen<U extends TypedSocketEvent<TIncoming & TPrivateIncoming & ServerPrivateMap, TOutgoing, TPrivateOutgoing & ClientPrivateMap, TService>, V = U>(event: string): DemuxedConsumableStream<V>;
|
|
27
71
|
ping(): Promise<void>;
|
|
28
72
|
get service(): string | undefined;
|
|
73
|
+
transmit<TMethod extends keyof TOutgoing & string>(method: TMethod, arg?: Parameters<TOutgoing[TMethod]>[0]): Promise<void>;
|
|
74
|
+
transmit<TServiceName extends ServiceName<TService>, TMethod extends ServiceMethodName<TService, TServiceName>>(options: [TServiceName, TMethod], arg?: Parameters<TService[TServiceName][TMethod]>[0]): Promise<void>;
|
|
75
|
+
transmit<TMethod extends keyof (TPrivateOutgoing & ClientPrivateMap) & string>(method: TMethod, arg?: Parameters<(TPrivateOutgoing & ClientPrivateMap)[TMethod]>[0]): Promise<void>;
|
|
29
76
|
get type(): 'server';
|
|
30
77
|
}
|
package/dist/server-socket.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BaseSocket, toError } from '@socket-mesh/core';
|
|
2
2
|
import { ServerTransport } from './server-transport.js';
|
|
3
|
-
export class ServerSocket extends
|
|
3
|
+
export class ServerSocket extends BaseSocket {
|
|
4
4
|
_serverTransport;
|
|
5
5
|
server;
|
|
6
6
|
constructor(options) {
|
|
@@ -15,7 +15,8 @@ export class ServerSocket extends Socket {
|
|
|
15
15
|
try {
|
|
16
16
|
await this._serverTransport.invoke('#removeAuthToken', undefined)[0];
|
|
17
17
|
}
|
|
18
|
-
catch (
|
|
18
|
+
catch (err) {
|
|
19
|
+
const error = toError(err);
|
|
19
20
|
this._serverTransport.onError(error);
|
|
20
21
|
throw error;
|
|
21
22
|
}
|
|
@@ -25,18 +26,24 @@ export class ServerSocket extends Socket {
|
|
|
25
26
|
await this.transmit('#removeAuthToken');
|
|
26
27
|
}
|
|
27
28
|
catch (err) {
|
|
28
|
-
if (err.name !== 'BadConnectionError') {
|
|
29
|
+
if (toError(err).name !== 'BadConnectionError') {
|
|
29
30
|
throw err;
|
|
30
31
|
}
|
|
31
32
|
}
|
|
32
33
|
return result;
|
|
33
34
|
}
|
|
35
|
+
emit(event, data) {
|
|
36
|
+
super.emit(event, data);
|
|
37
|
+
}
|
|
34
38
|
get exchange() {
|
|
35
39
|
return this.server.exchange;
|
|
36
40
|
}
|
|
37
41
|
get id() {
|
|
38
42
|
return this._serverTransport.id;
|
|
39
43
|
}
|
|
44
|
+
invoke(methodOptions, arg) {
|
|
45
|
+
return super.invoke(methodOptions, arg);
|
|
46
|
+
}
|
|
40
47
|
kickOut(channel, message) {
|
|
41
48
|
const channels = channel ? [channel] : Object.keys(this.state.channelSubscriptions || {});
|
|
42
49
|
return Promise.all(channels.map((channelName) => {
|
|
@@ -44,12 +51,18 @@ export class ServerSocket extends Socket {
|
|
|
44
51
|
return this._serverTransport.unsubscribe(channelName);
|
|
45
52
|
}));
|
|
46
53
|
}
|
|
54
|
+
listen(event) {
|
|
55
|
+
return super.listen(event ?? '');
|
|
56
|
+
}
|
|
47
57
|
ping() {
|
|
48
58
|
return this._serverTransport.ping();
|
|
49
59
|
}
|
|
50
60
|
get service() {
|
|
51
61
|
return this._serverTransport.service;
|
|
52
62
|
}
|
|
63
|
+
transmit(serviceAndMethod, arg) {
|
|
64
|
+
return super.transmit(serviceAndMethod, arg);
|
|
65
|
+
}
|
|
53
66
|
get type() {
|
|
54
67
|
return this._serverTransport.type;
|
|
55
68
|
}
|
|
@@ -2,37 +2,46 @@ import { AuthToken, SignedAuthToken } from '@socket-mesh/auth';
|
|
|
2
2
|
import { AuthTokenOptions } from '@socket-mesh/auth-engine';
|
|
3
3
|
import { ChannelMap, PublishOptions } from '@socket-mesh/channels';
|
|
4
4
|
import { ClientPrivateMap, ServerPrivateMap } from '@socket-mesh/client';
|
|
5
|
-
import { AnyPacket, AnyResponse, InboundMessage,
|
|
5
|
+
import { AnyPacket, AnyRequest, AnyResponse, BaseSocketTransport, FunctionReturnType, InboundMessage, InvokeMethodOptions, InvokeServiceOptions, PrivateMethodMap, PublicMethodMap, ServiceMap, ServiceMethodName, ServiceName, SocketStatus, SocketTransport } from '@socket-mesh/core';
|
|
6
6
|
import { IncomingMessage } from 'http';
|
|
7
7
|
import { Data } from 'ws';
|
|
8
8
|
import { ServerPlugin } from './plugin/server-plugin.js';
|
|
9
9
|
import { ServerSocketState } from './server-socket-state.js';
|
|
10
10
|
import { ServerSocket, ServerSocketOptions } from './server-socket.js';
|
|
11
|
-
export declare class ServerTransport<TIncoming extends PublicMethodMap = {}, TChannel extends ChannelMap = {}, TService extends ServiceMap = {},
|
|
11
|
+
export declare class ServerTransport<TIncoming extends PublicMethodMap = {}, TOutgoing extends PublicMethodMap = {}, TChannel extends ChannelMap = {}, TService extends ServiceMap = {}, TState extends object = {}, TPrivateIncoming extends PrivateMethodMap = {}, TPrivateOutgoing extends PrivateMethodMap = {}, TServerState extends object = {}> extends BaseSocketTransport<TState & ServerSocketState> implements SocketTransport<TIncoming & TPrivateIncoming & ServerPrivateMap, TOutgoing, TState & ServerSocketState, TService, {}, TPrivateOutgoing & ClientPrivateMap> {
|
|
12
12
|
id: string;
|
|
13
|
-
readonly plugins: ServerPlugin<TIncoming, TChannel, TService,
|
|
13
|
+
readonly plugins: ServerPlugin<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>[];
|
|
14
14
|
readonly request: IncomingMessage;
|
|
15
15
|
readonly service?: string;
|
|
16
16
|
type: 'server';
|
|
17
|
-
constructor(options: ServerSocketOptions<TIncoming, TChannel, TService,
|
|
17
|
+
constructor(options: ServerSocketOptions<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>);
|
|
18
18
|
changeToUnauthenticatedState(): Promise<boolean>;
|
|
19
|
-
protected handleInboudMessage({ packet, timestamp }: InboundMessage
|
|
19
|
+
protected handleInboudMessage({ packet, timestamp }: InboundMessage): Promise<void>;
|
|
20
|
+
invoke<TMethod extends keyof TOutgoing & string>(method: TMethod, arg?: Parameters<TOutgoing[TMethod]>[0]): [Promise<FunctionReturnType<TOutgoing[TMethod]>>, () => void];
|
|
21
|
+
invoke<TMethod extends keyof TOutgoing & string>(options: InvokeMethodOptions<TOutgoing, TMethod>, arg?: Parameters<TOutgoing[TMethod]>[0]): [Promise<FunctionReturnType<TOutgoing[TMethod]>>, () => void];
|
|
22
|
+
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]>>, () => void];
|
|
23
|
+
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]>>, () => void];
|
|
24
|
+
invoke<TMethod extends keyof (TPrivateOutgoing & ClientPrivateMap) & string>(method: TMethod, arg: Parameters<(TPrivateOutgoing & ClientPrivateMap)[TMethod]>[0]): [Promise<FunctionReturnType<(TPrivateOutgoing & ClientPrivateMap)[TMethod]>>, () => void];
|
|
25
|
+
invoke<TMethod extends keyof (TPrivateOutgoing & ClientPrivateMap) & string>(options: InvokeMethodOptions<(TPrivateOutgoing & ClientPrivateMap), TMethod>, arg?: Parameters<(TPrivateOutgoing & ClientPrivateMap)[TMethod]>[0]): [Promise<FunctionReturnType<(TPrivateOutgoing & ClientPrivateMap)[TMethod]>>, () => void];
|
|
20
26
|
protected onClose(code: number, reason?: Buffer | string): void;
|
|
21
27
|
protected onDisconnect(status: SocketStatus, code: number, reason?: string): void;
|
|
22
28
|
onError(error: Error): void;
|
|
23
|
-
protected onInvoke
|
|
29
|
+
protected onInvoke(request: AnyRequest): void;
|
|
24
30
|
protected onMessage(data: Data, isBinary: boolean): void;
|
|
25
31
|
protected onPingPong(): void;
|
|
26
32
|
protected onPublish(options: PublishOptions): Promise<void>;
|
|
27
|
-
protected onRequest(packet: AnyPacket
|
|
28
|
-
protected onResponse(response: AnyResponse
|
|
29
|
-
protected onTransmit
|
|
33
|
+
protected onRequest(packet: AnyPacket, timestamp: Date, pluginError?: Error): Promise<boolean>;
|
|
34
|
+
protected onResponse(response: AnyResponse): void;
|
|
35
|
+
protected onTransmit(request: AnyRequest): void;
|
|
30
36
|
ping(): Promise<void>;
|
|
31
37
|
setAuthorization(authToken: AuthToken, options?: AuthTokenOptions): Promise<boolean>;
|
|
32
38
|
setAuthorization(signedAuthToken: SignedAuthToken, authToken?: AuthToken): Promise<boolean>;
|
|
33
39
|
setReadyStatus(pingTimeoutMs: number, authError?: Error): void;
|
|
34
|
-
get socket(): ServerSocket<TIncoming, TChannel, TService,
|
|
35
|
-
set socket(value: ServerSocket<TIncoming, TChannel, TService,
|
|
40
|
+
get socket(): ServerSocket<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
41
|
+
set socket(value: ServerSocket<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>);
|
|
42
|
+
transmit<TMethod extends keyof TOutgoing & string>(method: TMethod, arg?: Parameters<TOutgoing[TMethod]>[0]): Promise<void>;
|
|
43
|
+
transmit<TServiceName extends ServiceName<TService>, TMethod extends ServiceMethodName<TService, TServiceName>>(options: [TServiceName, TMethod], arg?: Parameters<TService[TServiceName][TMethod]>[0]): Promise<void>;
|
|
44
|
+
transmit<TMethod extends keyof (TPrivateOutgoing & ClientPrivateMap) & string>(method: TMethod, arg?: Parameters<(TPrivateOutgoing & ClientPrivateMap)[TMethod]>[0]): Promise<void>;
|
|
36
45
|
triggerAuthenticationEvents(wasSigned: boolean, wasAuthenticated: boolean): void;
|
|
37
46
|
unsubscribe(channel: string): Promise<void>;
|
|
38
47
|
}
|
package/dist/server-transport.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { abortRequest,
|
|
1
|
+
import { abortRequest, BaseSocketTransport, toError } from '@socket-mesh/core';
|
|
2
2
|
import { AuthError, BrokerError, InvalidActionError, SocketProtocolErrorStatuses } from '@socket-mesh/errors';
|
|
3
3
|
import base64id from 'base64id';
|
|
4
|
-
export class ServerTransport extends
|
|
4
|
+
export class ServerTransport extends BaseSocketTransport {
|
|
5
5
|
id;
|
|
6
6
|
plugins;
|
|
7
7
|
request;
|
|
@@ -36,6 +36,9 @@ export class ServerTransport extends SocketTransport {
|
|
|
36
36
|
}
|
|
37
37
|
return await super.handleInboudMessage({ packet, timestamp });
|
|
38
38
|
}
|
|
39
|
+
invoke(methodOptions, arg) {
|
|
40
|
+
return super.invoke(methodOptions, arg);
|
|
41
|
+
}
|
|
39
42
|
onClose(code, reason) {
|
|
40
43
|
const status = this.status;
|
|
41
44
|
const strReason = reason?.toString() || SocketProtocolErrorStatuses[code];
|
|
@@ -179,9 +182,10 @@ export class ServerTransport extends SocketTransport {
|
|
|
179
182
|
signedAuthToken = await auth.signToken(authToken, options);
|
|
180
183
|
}
|
|
181
184
|
catch (err) {
|
|
182
|
-
|
|
183
|
-
this.
|
|
184
|
-
|
|
185
|
+
const error = toError(err);
|
|
186
|
+
this.onError(error);
|
|
187
|
+
this.disconnect(4002, error.toString());
|
|
188
|
+
throw error;
|
|
185
189
|
}
|
|
186
190
|
const changed = await super.setAuthorization(signedAuthToken, authToken);
|
|
187
191
|
if (changed && this.status === 'ready') {
|
|
@@ -192,15 +196,9 @@ export class ServerTransport extends SocketTransport {
|
|
|
192
196
|
await this.invoke('#setAuthToken', signedAuthToken)[0];
|
|
193
197
|
}
|
|
194
198
|
catch (err) {
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
}
|
|
199
|
-
else {
|
|
200
|
-
error = new AuthError('Failed to confirm delivery of auth token to client due to malformatted error response');
|
|
201
|
-
}
|
|
202
|
-
this.onError(error);
|
|
203
|
-
throw error;
|
|
199
|
+
const authError = new AuthError(`Failed to deliver auth token to client - ${toError(err).message}`);
|
|
200
|
+
this.onError(authError);
|
|
201
|
+
throw authError;
|
|
204
202
|
}
|
|
205
203
|
return changed;
|
|
206
204
|
}
|
|
@@ -208,7 +206,7 @@ export class ServerTransport extends SocketTransport {
|
|
|
208
206
|
await this.transmit('#setAuthToken', signedAuthToken);
|
|
209
207
|
}
|
|
210
208
|
catch (err) {
|
|
211
|
-
if (err.name !== 'BadConnectionError') {
|
|
209
|
+
if (toError(err).name !== 'BadConnectionError') {
|
|
212
210
|
throw err;
|
|
213
211
|
}
|
|
214
212
|
}
|
|
@@ -224,6 +222,9 @@ export class ServerTransport extends SocketTransport {
|
|
|
224
222
|
set socket(value) {
|
|
225
223
|
super.socket = value;
|
|
226
224
|
}
|
|
225
|
+
transmit(serviceAndMethod, arg) {
|
|
226
|
+
return super.transmit(serviceAndMethod, arg);
|
|
227
|
+
}
|
|
227
228
|
triggerAuthenticationEvents(wasSigned, wasAuthenticated) {
|
|
228
229
|
if (!this.signedAuthToken) {
|
|
229
230
|
throw new AuthError('Signed auth token should be set to trigger authentication events');
|
package/dist/server.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { AsyncStreamEmitter } from '@socket-mesh/async-stream-emitter';
|
|
2
2
|
import { AuthEngine } from '@socket-mesh/auth-engine';
|
|
3
3
|
import { ChannelMap } from '@socket-mesh/channels';
|
|
4
|
-
import {
|
|
4
|
+
import { ServerPrivateMap } from '@socket-mesh/client';
|
|
5
|
+
import { HandlerMap, MethodMap, PrivateMethodMap, PublicMethodMap, ServiceMap, StreamCleanupMode } from '@socket-mesh/core';
|
|
5
6
|
import { CodecEngine } from '@socket-mesh/formatter';
|
|
6
7
|
import { DemuxedConsumableStream, StreamEvent } from '@socket-mesh/stream-demux';
|
|
7
8
|
import { Server as HttpServer } from 'http';
|
|
@@ -10,10 +11,12 @@ import { Exchange } from './broker/exchange.js';
|
|
|
10
11
|
import { CloseEvent, ConnectionEvent, ErrorEvent, HandshakeEvent, HeadersEvent, ListeningEvent, ServerEvent, SocketAuthenticateEvent, SocketAuthStateChangeEvent, SocketBadAuthTokenEvent, SocketCloseEvent, SocketConnectEvent, SocketConnectingEvent, SocketDeauthenticateEvent, SocketDisconnectEvent, SocketErrorEvent, SocketMessageEvent, SocketPingEvent, SocketPongEvent, SocketRemoveAuthTokenEvent, SocketRequestEvent, SocketResponseEvent, SocketSubscribeEvent, SocketSubscribeFailEvent, SocketSubscribeStateChangeEvent, SocketUnsubscribeEvent, WarningEvent } from './events/index.js';
|
|
11
12
|
import { ServerPlugin } from './plugin/server-plugin.js';
|
|
12
13
|
import { ServerOptions } from './server-options.js';
|
|
14
|
+
import { ServerSocketState } from './server-socket-state.js';
|
|
13
15
|
import { ServerSocket } from './server-socket.js';
|
|
14
|
-
|
|
16
|
+
import { ServerTransport } from './server-transport.js';
|
|
17
|
+
export declare class Server<TIncoming extends PublicMethodMap = {}, TOutgoing extends PublicMethodMap = {}, TChannel extends ChannelMap = {}, TService extends ServiceMap = {}, TState extends object = {}, TPrivateIncoming extends PrivateMethodMap = {}, TPrivateOutgoing extends PrivateMethodMap = {}, TServerState extends object = {}> extends AsyncStreamEmitter<ServerEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>> {
|
|
15
18
|
private readonly _callIdGenerator;
|
|
16
|
-
private _handlers;
|
|
19
|
+
private readonly _handlers;
|
|
17
20
|
private _isListening;
|
|
18
21
|
private _isReady;
|
|
19
22
|
private _pingIntervalRef;
|
|
@@ -24,7 +27,7 @@ export declare class Server<TIncoming extends PublicMethodMap = {}, TChannel ext
|
|
|
24
27
|
readonly brokerEngine: Broker<TChannel>;
|
|
25
28
|
clientCount: number;
|
|
26
29
|
readonly clients: {
|
|
27
|
-
[id: string]: ServerSocket<TIncoming, TChannel, TService,
|
|
30
|
+
[id: string]: ServerSocket<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
28
31
|
};
|
|
29
32
|
readonly codecEngine: CodecEngine;
|
|
30
33
|
readonly httpServer: HttpServer;
|
|
@@ -32,86 +35,135 @@ export declare class Server<TIncoming extends PublicMethodMap = {}, TChannel ext
|
|
|
32
35
|
origins: string;
|
|
33
36
|
pendingClientCount: number;
|
|
34
37
|
readonly pendingClients: {
|
|
35
|
-
[id: string]: ServerSocket<TIncoming, TChannel, TService,
|
|
38
|
+
[id: string]: ServerSocket<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
36
39
|
};
|
|
37
40
|
pingIntervalMs: number;
|
|
38
41
|
pingTimeoutMs: number;
|
|
39
|
-
readonly plugins: ServerPlugin<TIncoming, TChannel, TService,
|
|
42
|
+
readonly plugins: ServerPlugin<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>[];
|
|
40
43
|
socketChannelLimit?: number;
|
|
41
44
|
readonly socketStreamCleanupMode: StreamCleanupMode;
|
|
42
45
|
strictHandshake: boolean;
|
|
43
|
-
constructor(options?: ServerOptions<TIncoming, TChannel, TService,
|
|
44
|
-
|
|
46
|
+
constructor(options?: ServerOptions<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>);
|
|
47
|
+
/**
|
|
48
|
+
* Register a group of strongly-typed request handlers.
|
|
49
|
+
*
|
|
50
|
+
* Handlers added this way can be added or replaced after the server has
|
|
51
|
+
* started and are immediately visible to all existing and future
|
|
52
|
+
* connections (the underlying handler map is shared by reference).
|
|
53
|
+
*
|
|
54
|
+
* When called with a service name, the handlers are grouped under that
|
|
55
|
+
* service and surfaced via {@link services} so UI/tooling can list which
|
|
56
|
+
* groups are currently installed. When the service name is known to the
|
|
57
|
+
* server's `TService` generic, TypeScript validates the handler shape
|
|
58
|
+
* against the declared method map. For ad-hoc/dynamic services not
|
|
59
|
+
* present in `TService`, pass an explicit generic argument with the
|
|
60
|
+
* method map for the new service.
|
|
61
|
+
*
|
|
62
|
+
* When called without a service name, the handlers are registered as
|
|
63
|
+
* flat (top-level) handlers routed by method name alone — the same
|
|
64
|
+
* surface as `options.handlers` on the server constructor. These do not
|
|
65
|
+
* appear in {@link services}.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* // Flat handlers (no service name):
|
|
69
|
+
* server.addHandlers({ doThing: async (args) => { ... } });
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* // Statically declared on the server generic:
|
|
73
|
+
* server.addHandlers('account', { find: async (args) => { ... } });
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* // Dynamically added from a module at runtime:
|
|
77
|
+
* server.addHandlers<'inventory', InventoryMethodMap>('inventory', handlers);
|
|
78
|
+
*/
|
|
79
|
+
addHandlers(handlers: HandlerMap<TIncoming & TPrivateIncoming & ServerPrivateMap, TState & ServerSocketState, ServerSocket<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>, ServerTransport<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>): void;
|
|
80
|
+
addHandlers<TServiceName extends keyof TService & string>(service: TServiceName, handlers: HandlerMap<TService[TServiceName], TState & ServerSocketState, ServerSocket<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>, ServerTransport<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>): void;
|
|
81
|
+
addHandlers<TServiceName extends string, TServiceMethodMap extends MethodMap>(service: TServiceName, handlers: HandlerMap<TServiceMethodMap, TState & ServerSocketState, ServerSocket<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>, ServerTransport<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>): void;
|
|
82
|
+
addPlugin(...plugin: ServerPlugin<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>[]): void;
|
|
45
83
|
private bind;
|
|
46
84
|
close(keepSocketsOpen?: boolean): Promise<void>;
|
|
47
85
|
emit(event: 'close', data: CloseEvent): void;
|
|
48
|
-
emit(event: 'connection', data: ConnectionEvent<TChannel, TService,
|
|
86
|
+
emit(event: 'connection', data: ConnectionEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
49
87
|
emit(event: 'error', data: ErrorEvent): void;
|
|
50
88
|
emit(event: 'headers', data: HeadersEvent): void;
|
|
51
|
-
emit(event: 'handshake', data: HandshakeEvent<TChannel, TService,
|
|
89
|
+
emit(event: 'handshake', data: HandshakeEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
52
90
|
emit(event: 'listening', data: ListeningEvent): void;
|
|
53
91
|
emit(event: 'ready', data: {}): void;
|
|
54
|
-
emit(event: 'socketAuthStateChange', data: SocketAuthStateChangeEvent<TChannel, TService,
|
|
55
|
-
emit(event: 'socketAuthenticate', data: SocketAuthenticateEvent<TChannel, TService,
|
|
56
|
-
emit(event: 'socketBadAuthToken', data: SocketBadAuthTokenEvent<TChannel, TService,
|
|
57
|
-
emit(event: 'socketClose', data: SocketCloseEvent<TChannel, TService,
|
|
58
|
-
emit(event: 'socketConnect', data: SocketConnectEvent<TChannel, TService,
|
|
59
|
-
emit(event: 'socketConnectAbort', data: SocketDisconnectEvent<TChannel, TService,
|
|
60
|
-
emit(event: 'socketConnecting', data: SocketConnectingEvent<TChannel, TService,
|
|
61
|
-
emit(event: 'socketDeauthenticate', data: SocketDeauthenticateEvent<TChannel, TService,
|
|
62
|
-
emit(event: 'socketDisconnect', data: SocketDisconnectEvent<TChannel, TService,
|
|
63
|
-
emit(event: 'socketError', data: SocketErrorEvent<TChannel, TService,
|
|
64
|
-
emit(event: 'socketMessage', data: SocketMessageEvent<TChannel, TService,
|
|
65
|
-
emit(event: 'socketPing', data: SocketPingEvent<TChannel, TService,
|
|
66
|
-
emit(event: 'socketPong', data: SocketPongEvent<TChannel, TService,
|
|
67
|
-
emit(event: 'socketRemoveAuthToken', data: SocketRemoveAuthTokenEvent<TChannel, TService,
|
|
68
|
-
emit(event: 'socketRequest', data: SocketRequestEvent<
|
|
69
|
-
emit(event: 'socketResponse', data: SocketResponseEvent<TChannel, TService,
|
|
70
|
-
emit(event: 'socketSubscribe', data: SocketSubscribeEvent<TChannel, TService,
|
|
71
|
-
emit(event: 'socketSubscribeFail', data: SocketSubscribeFailEvent<TChannel, TService,
|
|
72
|
-
emit(event: 'socketSubscribeRequest', data: SocketSubscribeEvent<TChannel, TService,
|
|
73
|
-
emit(event: 'socketSubscribeStateChange', data: SocketSubscribeStateChangeEvent<TChannel, TService,
|
|
74
|
-
emit(event: 'socketUnsubscribe', data: SocketUnsubscribeEvent<TChannel, TService,
|
|
92
|
+
emit(event: 'socketAuthStateChange', data: SocketAuthStateChangeEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
93
|
+
emit(event: 'socketAuthenticate', data: SocketAuthenticateEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
94
|
+
emit(event: 'socketBadAuthToken', data: SocketBadAuthTokenEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
95
|
+
emit(event: 'socketClose', data: SocketCloseEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
96
|
+
emit(event: 'socketConnect', data: SocketConnectEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
97
|
+
emit(event: 'socketConnectAbort', data: SocketDisconnectEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
98
|
+
emit(event: 'socketConnecting', data: SocketConnectingEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
99
|
+
emit(event: 'socketDeauthenticate', data: SocketDeauthenticateEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
100
|
+
emit(event: 'socketDisconnect', data: SocketDisconnectEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
101
|
+
emit(event: 'socketError', data: SocketErrorEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
102
|
+
emit(event: 'socketMessage', data: SocketMessageEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
103
|
+
emit(event: 'socketPing', data: SocketPingEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
104
|
+
emit(event: 'socketPong', data: SocketPongEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
105
|
+
emit(event: 'socketRemoveAuthToken', data: SocketRemoveAuthTokenEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
106
|
+
emit(event: 'socketRequest', data: SocketRequestEvent<TIncoming, TService, TPrivateIncoming>): void;
|
|
107
|
+
emit(event: 'socketResponse', data: SocketResponseEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
108
|
+
emit(event: 'socketSubscribe', data: SocketSubscribeEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
109
|
+
emit(event: 'socketSubscribeFail', data: SocketSubscribeFailEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
110
|
+
emit(event: 'socketSubscribeRequest', data: SocketSubscribeEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
111
|
+
emit(event: 'socketSubscribeStateChange', data: SocketSubscribeStateChangeEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
112
|
+
emit(event: 'socketUnsubscribe', data: SocketUnsubscribeEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
75
113
|
emit(event: 'warning', data: WarningEvent): void;
|
|
76
114
|
get exchange(): Exchange<TChannel>;
|
|
115
|
+
/** Method names registered under a given service, or an empty array if none. */
|
|
116
|
+
getServiceMethods(service: string): string[];
|
|
77
117
|
get isListening(): boolean;
|
|
78
118
|
get isReady(): boolean;
|
|
79
|
-
listen(): DemuxedConsumableStream<StreamEvent<ServerEvent<TChannel, TService,
|
|
119
|
+
listen(): DemuxedConsumableStream<StreamEvent<ServerEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>>;
|
|
80
120
|
listen(event: 'close'): DemuxedConsumableStream<CloseEvent>;
|
|
81
|
-
listen(event: 'connection'): DemuxedConsumableStream<ConnectionEvent<TChannel, TService,
|
|
121
|
+
listen(event: 'connection'): DemuxedConsumableStream<ConnectionEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
82
122
|
listen(event: 'error'): DemuxedConsumableStream<ErrorEvent>;
|
|
83
|
-
listen(event: 'handshake'): DemuxedConsumableStream<HandshakeEvent<TChannel, TService,
|
|
123
|
+
listen(event: 'handshake'): DemuxedConsumableStream<HandshakeEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
84
124
|
listen(event: 'headers'): DemuxedConsumableStream<HeadersEvent>;
|
|
85
125
|
listen(event: 'listening'): DemuxedConsumableStream<ListeningEvent>;
|
|
86
126
|
listen(event: 'ready'): DemuxedConsumableStream<{}>;
|
|
87
|
-
listen(event: 'socketAuthStateChange'): DemuxedConsumableStream<SocketAuthStateChangeEvent<TChannel, TService,
|
|
88
|
-
listen(event: 'socketAuthenticate'): DemuxedConsumableStream<SocketAuthenticateEvent<TChannel, TService,
|
|
89
|
-
listen(event: 'socketBadAuthToken'): DemuxedConsumableStream<SocketBadAuthTokenEvent<TChannel, TService,
|
|
90
|
-
listen(event: 'socketClose'): DemuxedConsumableStream<SocketCloseEvent<TChannel, TService,
|
|
91
|
-
listen(event: 'socketConnect'): DemuxedConsumableStream<SocketConnectEvent<TChannel, TService,
|
|
92
|
-
listen(event: 'socketConnectAbort'): DemuxedConsumableStream<SocketDisconnectEvent<TChannel, TService,
|
|
93
|
-
listen(event: 'socketConnecting'): DemuxedConsumableStream<SocketConnectingEvent<TChannel, TService,
|
|
94
|
-
listen(event: 'socketDeauthenticate'): DemuxedConsumableStream<SocketDeauthenticateEvent<TChannel, TService,
|
|
95
|
-
listen(event: 'socketDisconnect'): DemuxedConsumableStream<SocketDisconnectEvent<TChannel, TService,
|
|
96
|
-
listen(event: 'socketError'): DemuxedConsumableStream<SocketErrorEvent<TChannel, TService,
|
|
97
|
-
listen(event: 'socketMessage'): DemuxedConsumableStream<SocketMessageEvent<TChannel, TService,
|
|
98
|
-
listen(event: 'socketPing'): DemuxedConsumableStream<SocketPingEvent<TChannel, TService,
|
|
99
|
-
listen(event: 'socketPong'): DemuxedConsumableStream<SocketPongEvent<TChannel, TService,
|
|
100
|
-
listen(event: 'socketRemoveAuthToken'): DemuxedConsumableStream<SocketRemoveAuthTokenEvent<TChannel, TService,
|
|
101
|
-
listen(event: 'socketRequest'): DemuxedConsumableStream<SocketRequestEvent<
|
|
102
|
-
listen(event: 'socketResponse'): DemuxedConsumableStream<SocketResponseEvent<TChannel, TService,
|
|
103
|
-
listen(event: 'socketSubscribe'): DemuxedConsumableStream<SocketSubscribeEvent<TChannel, TService,
|
|
104
|
-
listen(event: 'socketSubscribeFail'): DemuxedConsumableStream<SocketSubscribeFailEvent<TChannel, TService,
|
|
105
|
-
listen(event: 'socketSubscribeRequest'): DemuxedConsumableStream<SocketSubscribeEvent<TChannel, TService,
|
|
106
|
-
listen(event: 'socketSubscribeStateChange'): DemuxedConsumableStream<SocketSubscribeStateChangeEvent<TChannel, TService,
|
|
107
|
-
listen(event: 'socketUnsubscribe'): DemuxedConsumableStream<SocketUnsubscribeEvent<TChannel, TService,
|
|
127
|
+
listen(event: 'socketAuthStateChange'): DemuxedConsumableStream<SocketAuthStateChangeEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
128
|
+
listen(event: 'socketAuthenticate'): DemuxedConsumableStream<SocketAuthenticateEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
129
|
+
listen(event: 'socketBadAuthToken'): DemuxedConsumableStream<SocketBadAuthTokenEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
130
|
+
listen(event: 'socketClose'): DemuxedConsumableStream<SocketCloseEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
131
|
+
listen(event: 'socketConnect'): DemuxedConsumableStream<SocketConnectEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
132
|
+
listen(event: 'socketConnectAbort'): DemuxedConsumableStream<SocketDisconnectEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
133
|
+
listen(event: 'socketConnecting'): DemuxedConsumableStream<SocketConnectingEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
134
|
+
listen(event: 'socketDeauthenticate'): DemuxedConsumableStream<SocketDeauthenticateEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
135
|
+
listen(event: 'socketDisconnect'): DemuxedConsumableStream<SocketDisconnectEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
136
|
+
listen(event: 'socketError'): DemuxedConsumableStream<SocketErrorEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
137
|
+
listen(event: 'socketMessage'): DemuxedConsumableStream<SocketMessageEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
138
|
+
listen(event: 'socketPing'): DemuxedConsumableStream<SocketPingEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
139
|
+
listen(event: 'socketPong'): DemuxedConsumableStream<SocketPongEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
140
|
+
listen(event: 'socketRemoveAuthToken'): DemuxedConsumableStream<SocketRemoveAuthTokenEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
141
|
+
listen(event: 'socketRequest'): DemuxedConsumableStream<SocketRequestEvent<TIncoming, TService, TPrivateIncoming>>;
|
|
142
|
+
listen(event: 'socketResponse'): DemuxedConsumableStream<SocketResponseEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
143
|
+
listen(event: 'socketSubscribe'): DemuxedConsumableStream<SocketSubscribeEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
144
|
+
listen(event: 'socketSubscribeFail'): DemuxedConsumableStream<SocketSubscribeFailEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
145
|
+
listen(event: 'socketSubscribeRequest'): DemuxedConsumableStream<SocketSubscribeEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
146
|
+
listen(event: 'socketSubscribeStateChange'): DemuxedConsumableStream<SocketSubscribeStateChangeEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
147
|
+
listen(event: 'socketUnsubscribe'): DemuxedConsumableStream<SocketUnsubscribeEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
108
148
|
listen(event: 'warning'): DemuxedConsumableStream<WarningEvent>;
|
|
109
149
|
private onClose;
|
|
110
150
|
private onConnection;
|
|
111
151
|
private onError;
|
|
112
152
|
private onHeaders;
|
|
113
153
|
private onListening;
|
|
114
|
-
|
|
154
|
+
/**
|
|
155
|
+
* Unregister either a whole service (when `methods` is omitted) or a
|
|
156
|
+
* specific set of methods within a service. Removing a service empties
|
|
157
|
+
* the group and drops the key so it no longer appears in {@link services}.
|
|
158
|
+
*
|
|
159
|
+
* Call with an array of method names (no service) to remove flat
|
|
160
|
+
* (top-level) handlers by name. The flat slot itself is never dropped
|
|
161
|
+
* because it holds the built-in protocol handlers.
|
|
162
|
+
*/
|
|
163
|
+
removeHandlers(methods: readonly string[]): void;
|
|
164
|
+
removeHandlers(service: string, methods?: readonly string[] | string): void;
|
|
165
|
+
/** Names of all service handler groups currently registered on the server. */
|
|
166
|
+
get services(): string[];
|
|
115
167
|
private socketDisconnected;
|
|
116
168
|
private startPinging;
|
|
117
169
|
private stopPinging;
|
package/dist/server.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { AsyncStreamEmitter } from '@socket-mesh/async-stream-emitter';
|
|
2
2
|
import { defaultAuthEngine, isAuthEngine } from '@socket-mesh/auth-engine';
|
|
3
3
|
import { removeAuthTokenHandler } from '@socket-mesh/client';
|
|
4
|
+
import { toError } from '@socket-mesh/core';
|
|
4
5
|
import { ServerProtocolError } from '@socket-mesh/errors';
|
|
5
6
|
import defaultCodec from '@socket-mesh/formatter';
|
|
6
7
|
import { WebSocketServer } from 'ws';
|
|
@@ -44,6 +45,9 @@ export class Server extends AsyncStreamEmitter {
|
|
|
44
45
|
options = {};
|
|
45
46
|
}
|
|
46
47
|
options.clientTracking = true;
|
|
48
|
+
this._isListening = false;
|
|
49
|
+
this._isReady = false;
|
|
50
|
+
this._pingIntervalRef = null;
|
|
47
51
|
this.ackTimeoutMs = options.ackTimeoutMs || 10000;
|
|
48
52
|
this.allowClientPublish = options.allowClientPublish ?? true;
|
|
49
53
|
this.auth = isAuthEngine(options.authEngine) ? options.authEngine : defaultAuthEngine(options.authEngine);
|
|
@@ -54,14 +58,23 @@ export class Server extends AsyncStreamEmitter {
|
|
|
54
58
|
this.clients = {};
|
|
55
59
|
this.clientCount = 0;
|
|
56
60
|
this.codecEngine = options.codecEngine || defaultCodec;
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
'
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
61
|
+
// Flat handlers live under the empty-string service key so dispatch
|
|
62
|
+
// (and Server.addHandlers/removeHandlers) only has to consult one map.
|
|
63
|
+
this._handlers = {
|
|
64
|
+
'': Object.assign({
|
|
65
|
+
'#authenticate': authenticateHandler,
|
|
66
|
+
'#handshake': handshakeHandler,
|
|
67
|
+
'#publish': publishHandler,
|
|
68
|
+
'#removeAuthToken': removeAuthTokenHandler,
|
|
69
|
+
'#subscribe': subscribeHandler,
|
|
70
|
+
'#unsubscribe': unsubscribeHandler
|
|
71
|
+
}, options.handlers)
|
|
72
|
+
};
|
|
73
|
+
if (options.serviceHandlers) {
|
|
74
|
+
for (const service of Object.keys(options.serviceHandlers)) {
|
|
75
|
+
this._handlers[service] = { ...options.serviceHandlers[service] };
|
|
76
|
+
}
|
|
77
|
+
}
|
|
65
78
|
this.httpServer = options.server;
|
|
66
79
|
this.plugins = options.plugins || [];
|
|
67
80
|
this.origins = options.origins || '*:*';
|
|
@@ -100,6 +113,14 @@ export class Server extends AsyncStreamEmitter {
|
|
|
100
113
|
})();
|
|
101
114
|
}
|
|
102
115
|
}
|
|
116
|
+
addHandlers(serviceOrHandlers, handlers) {
|
|
117
|
+
const service = typeof serviceOrHandlers === 'string' ? serviceOrHandlers : '';
|
|
118
|
+
const map = typeof serviceOrHandlers === 'string' ? handlers : serviceOrHandlers;
|
|
119
|
+
if (!this._handlers[service]) {
|
|
120
|
+
this._handlers[service] = {};
|
|
121
|
+
}
|
|
122
|
+
Object.assign(this._handlers[service], map);
|
|
123
|
+
}
|
|
103
124
|
addPlugin(...plugin) {
|
|
104
125
|
this.plugins.push(...plugin);
|
|
105
126
|
}
|
|
@@ -176,6 +197,16 @@ export class Server extends AsyncStreamEmitter {
|
|
|
176
197
|
get exchange() {
|
|
177
198
|
return this.brokerEngine.exchange;
|
|
178
199
|
}
|
|
200
|
+
/** Method names registered under a given service, or an empty array if none. */
|
|
201
|
+
getServiceMethods(service) {
|
|
202
|
+
if (service === '') {
|
|
203
|
+
// The empty-string slot stores flat (non-service) handlers internally
|
|
204
|
+
// and is not part of the public service surface.
|
|
205
|
+
return [];
|
|
206
|
+
}
|
|
207
|
+
const group = this._handlers[service];
|
|
208
|
+
return group ? Object.keys(group) : [];
|
|
209
|
+
}
|
|
179
210
|
get isListening() {
|
|
180
211
|
return this._isListening;
|
|
181
212
|
}
|
|
@@ -185,6 +216,7 @@ export class Server extends AsyncStreamEmitter {
|
|
|
185
216
|
listen(event) {
|
|
186
217
|
return event ? super.listen(event) : super.listen();
|
|
187
218
|
}
|
|
219
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
188
220
|
onClose(code, reason) {
|
|
189
221
|
this.emit('close', {});
|
|
190
222
|
}
|
|
@@ -199,11 +231,10 @@ export class Server extends AsyncStreamEmitter {
|
|
|
199
231
|
ackTimeoutMs: this.ackTimeoutMs,
|
|
200
232
|
callIdGenerator: this._callIdGenerator,
|
|
201
233
|
codecEngine: this.codecEngine,
|
|
202
|
-
handlers: this._handlers,
|
|
203
|
-
onUnhandledRequest: this.onUnhandledRequest.bind(this),
|
|
204
234
|
plugins: this.plugins,
|
|
205
235
|
request: upgradeReq,
|
|
206
236
|
server: this,
|
|
237
|
+
serviceHandlers: this._handlers,
|
|
207
238
|
socket: wsSocket,
|
|
208
239
|
state: {},
|
|
209
240
|
streamCleanupMode: this.socketStreamCleanupMode
|
|
@@ -228,7 +259,40 @@ export class Server extends AsyncStreamEmitter {
|
|
|
228
259
|
this._isListening = true;
|
|
229
260
|
this.emit('listening', {});
|
|
230
261
|
}
|
|
231
|
-
|
|
262
|
+
removeHandlers(serviceOrMethods, methods) {
|
|
263
|
+
let service;
|
|
264
|
+
let list;
|
|
265
|
+
if (Array.isArray(serviceOrMethods)) {
|
|
266
|
+
service = '';
|
|
267
|
+
list = serviceOrMethods;
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
service = serviceOrMethods;
|
|
271
|
+
list = typeof methods === 'string' ? [methods] : methods;
|
|
272
|
+
}
|
|
273
|
+
const group = this._handlers[service];
|
|
274
|
+
if (!group) {
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
if (list === undefined) {
|
|
278
|
+
// Never drop the flat slot — it holds built-in protocol handlers.
|
|
279
|
+
if (service !== '') {
|
|
280
|
+
delete this._handlers[service];
|
|
281
|
+
}
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
for (const method of list) {
|
|
285
|
+
delete group[method];
|
|
286
|
+
}
|
|
287
|
+
if (service !== '' && Object.keys(group).length === 0) {
|
|
288
|
+
delete this._handlers[service];
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
/** Names of all service handler groups currently registered on the server. */
|
|
292
|
+
get services() {
|
|
293
|
+
// The empty-string slot holds flat (non-service) handlers internally
|
|
294
|
+
// and is not part of the public service surface.
|
|
295
|
+
return Object.keys(this._handlers).filter(service => service !== '');
|
|
232
296
|
}
|
|
233
297
|
socketDisconnected(socket) {
|
|
234
298
|
if (this.pendingClients[socket.id]) {
|
|
@@ -294,15 +358,17 @@ export class Server extends AsyncStreamEmitter {
|
|
|
294
358
|
}
|
|
295
359
|
}
|
|
296
360
|
catch (err) {
|
|
297
|
-
|
|
361
|
+
const error = toError(err);
|
|
362
|
+
callback(false, 401, typeof err === 'string' ? err : error.message);
|
|
298
363
|
return;
|
|
299
364
|
}
|
|
300
365
|
callback(true);
|
|
301
366
|
}
|
|
302
367
|
catch (err) {
|
|
303
|
-
|
|
304
|
-
this.
|
|
305
|
-
|
|
368
|
+
const error = toError(err);
|
|
369
|
+
this.onError(error);
|
|
370
|
+
this.emit('warning', { warning: error });
|
|
371
|
+
callback(false, 403, typeof err === 'string' ? err : error.message);
|
|
306
372
|
}
|
|
307
373
|
}
|
|
308
374
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@socket-mesh/server",
|
|
3
3
|
"description": "A TCP socket pair for easily transmitting full messages without worrying about request size limits.",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "19.1.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|