@socket-mesh/server 18.1.5 → 19.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +7 -4
- package/dist/server-socket.d.ts +56 -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 +53 -54
- package/dist/server.js +11 -7
- 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,28 @@
|
|
|
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>[];
|
|
23
26
|
socketChannelLimit?: number;
|
|
24
27
|
socketStreamCleanupMode?: StreamCleanupMode;
|
|
25
28
|
strictHandshake?: boolean;
|
package/dist/server-socket.d.ts
CHANGED
|
@@ -1,30 +1,78 @@
|
|
|
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, LooseHandlerMap, 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:
|
|
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
|
+
handlers: LooseHandlerMap;
|
|
12
13
|
id?: string;
|
|
13
|
-
plugins?: ServerPlugin<TIncoming, TChannel, TService,
|
|
14
|
+
plugins?: ServerPlugin<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>[];
|
|
14
15
|
request: IncomingMessage;
|
|
15
|
-
server: Server<TIncoming, TChannel, TService,
|
|
16
|
+
server: Server<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
16
17
|
service?: string;
|
|
17
18
|
socket: WebSocket;
|
|
18
19
|
}
|
|
19
|
-
export declare class ServerSocket<TIncoming extends PublicMethodMap = {}, TChannel extends ChannelMap = {}, TService extends ServiceMap = {},
|
|
20
|
+
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
21
|
private _serverTransport;
|
|
21
|
-
readonly server: Server<TIncoming, TChannel, TService,
|
|
22
|
-
constructor(options: ServerSocketOptions<TIncoming, TChannel, TService,
|
|
22
|
+
readonly server: Server<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
23
|
+
constructor(options: ServerSocketOptions<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>);
|
|
23
24
|
deauthenticate(rejectOnFailedDelivery?: boolean): Promise<boolean>;
|
|
25
|
+
emit(event: 'request', data: TypedRequestEvent<TIncoming & TPrivateIncoming & ServerPrivateMap, TService>): void;
|
|
26
|
+
emit(event: 'response', data: TypedResponseEvent<TOutgoing, TPrivateOutgoing & ClientPrivateMap, TService>): void;
|
|
27
|
+
emit(event: 'authStateChange', data: AuthStateChangeEvent): void;
|
|
28
|
+
emit(event: 'authenticate', data: AuthenticateEvent): void;
|
|
29
|
+
emit(event: 'badAuthToken', data: BadAuthTokenEvent): void;
|
|
30
|
+
emit(event: 'close', data: CloseEvent): void;
|
|
31
|
+
emit(event: 'connect', data: ConnectEvent): void;
|
|
32
|
+
emit(event: 'connectAbort', data: DisconnectEvent): void;
|
|
33
|
+
emit(event: 'connecting', data: ConnectingEvent): void;
|
|
34
|
+
emit(event: 'deauthenticate', data: DeauthenticateEvent): void;
|
|
35
|
+
emit(event: 'disconnect', data: DisconnectEvent): void;
|
|
36
|
+
emit(event: 'end'): void;
|
|
37
|
+
emit(event: 'error', data: ErrorEvent): void;
|
|
38
|
+
emit(event: 'message', data: MessageEvent): void;
|
|
39
|
+
emit(event: 'ping', data: PingEvent): void;
|
|
40
|
+
emit(event: 'pong', data: PongEvent): void;
|
|
41
|
+
emit(event: 'removeAuthToken', data: RemoveAuthTokenEvent): void;
|
|
42
|
+
emit(event: 'request', data: RequestEvent): void;
|
|
43
|
+
emit(event: 'response', data: ResponseEvent): void;
|
|
24
44
|
get exchange(): Exchange<TChannel>;
|
|
25
45
|
get id(): string;
|
|
46
|
+
invoke<TMethod extends keyof TOutgoing & string>(method: TMethod, arg?: Parameters<TOutgoing[TMethod]>[0]): Promise<FunctionReturnType<TOutgoing[TMethod]>>;
|
|
47
|
+
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]>>;
|
|
48
|
+
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]>>;
|
|
49
|
+
invoke<TMethod extends keyof TOutgoing & string>(options: InvokeMethodOptions<TOutgoing, TMethod>, arg?: Parameters<TOutgoing[TMethod]>[0]): Promise<FunctionReturnType<TOutgoing[TMethod]>>;
|
|
50
|
+
invoke<TMethod extends keyof (TPrivateOutgoing & ClientPrivateMap) & string>(method: TMethod, arg: Parameters<(TPrivateOutgoing & ClientPrivateMap)[TMethod]>[0]): Promise<FunctionReturnType<(TPrivateOutgoing & ClientPrivateMap)[TMethod]>>;
|
|
51
|
+
invoke<TMethod extends keyof (TPrivateOutgoing & ClientPrivateMap) & string>(options: InvokeMethodOptions<(TPrivateOutgoing & ClientPrivateMap), TMethod>, arg?: Parameters<(TPrivateOutgoing & ClientPrivateMap)[TMethod]>[0]): Promise<FunctionReturnType<(TPrivateOutgoing & ClientPrivateMap)[TMethod]>>;
|
|
26
52
|
kickOut(channel: string, message: string): Promise<void[]>;
|
|
53
|
+
listen(): DemuxedConsumableStream<StreamEvent<TypedSocketEvent<TIncoming & TPrivateIncoming & ServerPrivateMap, TOutgoing, TPrivateOutgoing & ClientPrivateMap, TService>>>;
|
|
54
|
+
listen(event: 'authStateChange'): DemuxedConsumableStream<AuthStateChangeEvent>;
|
|
55
|
+
listen(event: 'authenticate'): DemuxedConsumableStream<AuthenticateEvent>;
|
|
56
|
+
listen(event: 'badAuthToken'): DemuxedConsumableStream<BadAuthTokenEvent>;
|
|
57
|
+
listen(event: 'close'): DemuxedConsumableStream<CloseEvent>;
|
|
58
|
+
listen(event: 'connect'): DemuxedConsumableStream<ConnectEvent>;
|
|
59
|
+
listen(event: 'connectAbort'): DemuxedConsumableStream<DisconnectEvent>;
|
|
60
|
+
listen(event: 'connecting'): DemuxedConsumableStream<ConnectingEvent>;
|
|
61
|
+
listen(event: 'deauthenticate'): DemuxedConsumableStream<DeauthenticateEvent>;
|
|
62
|
+
listen(event: 'disconnect'): DemuxedConsumableStream<DisconnectEvent>;
|
|
63
|
+
listen(event: 'end'): DemuxedConsumableStream<void>;
|
|
64
|
+
listen(event: 'error'): DemuxedConsumableStream<ErrorEvent>;
|
|
65
|
+
listen(event: 'message'): DemuxedConsumableStream<MessageEvent>;
|
|
66
|
+
listen(event: 'ping'): DemuxedConsumableStream<PingEvent>;
|
|
67
|
+
listen(event: 'pong'): DemuxedConsumableStream<PongEvent>;
|
|
68
|
+
listen(event: 'removeAuthToken'): DemuxedConsumableStream<RemoveAuthTokenEvent>;
|
|
69
|
+
listen(event: 'request'): DemuxedConsumableStream<TypedRequestEvent<TIncoming & TPrivateIncoming & ServerPrivateMap, TService>>;
|
|
70
|
+
listen(event: 'response'): DemuxedConsumableStream<TypedResponseEvent<TOutgoing, TPrivateOutgoing & ClientPrivateMap, TService>>;
|
|
71
|
+
listen<U extends TypedSocketEvent<TIncoming & TPrivateIncoming & ServerPrivateMap, TOutgoing, TPrivateOutgoing & ClientPrivateMap, TService>, V = U>(event: string): DemuxedConsumableStream<V>;
|
|
27
72
|
ping(): Promise<void>;
|
|
28
73
|
get service(): string | undefined;
|
|
74
|
+
transmit<TMethod extends keyof TOutgoing & string>(method: TMethod, arg?: Parameters<TOutgoing[TMethod]>[0]): Promise<void>;
|
|
75
|
+
transmit<TServiceName extends ServiceName<TService>, TMethod extends ServiceMethodName<TService, TServiceName>>(options: [TServiceName, TMethod], arg?: Parameters<TService[TServiceName][TMethod]>[0]): Promise<void>;
|
|
76
|
+
transmit<TMethod extends keyof (TPrivateOutgoing & ClientPrivateMap) & string>(method: TMethod, arg?: Parameters<(TPrivateOutgoing & ClientPrivateMap)[TMethod]>[0]): Promise<void>;
|
|
29
77
|
get type(): 'server';
|
|
30
78
|
}
|
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
|
@@ -11,7 +11,7 @@ import { CloseEvent, ConnectionEvent, ErrorEvent, HandshakeEvent, HeadersEvent,
|
|
|
11
11
|
import { ServerPlugin } from './plugin/server-plugin.js';
|
|
12
12
|
import { ServerOptions } from './server-options.js';
|
|
13
13
|
import { ServerSocket } from './server-socket.js';
|
|
14
|
-
export declare class Server<TIncoming extends PublicMethodMap = {},
|
|
14
|
+
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
15
|
private readonly _callIdGenerator;
|
|
16
16
|
private _handlers;
|
|
17
17
|
private _isListening;
|
|
@@ -24,7 +24,7 @@ export declare class Server<TIncoming extends PublicMethodMap = {}, TChannel ext
|
|
|
24
24
|
readonly brokerEngine: Broker<TChannel>;
|
|
25
25
|
clientCount: number;
|
|
26
26
|
readonly clients: {
|
|
27
|
-
[id: string]: ServerSocket<TIncoming, TChannel, TService,
|
|
27
|
+
[id: string]: ServerSocket<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
28
28
|
};
|
|
29
29
|
readonly codecEngine: CodecEngine;
|
|
30
30
|
readonly httpServer: HttpServer;
|
|
@@ -32,86 +32,85 @@ export declare class Server<TIncoming extends PublicMethodMap = {}, TChannel ext
|
|
|
32
32
|
origins: string;
|
|
33
33
|
pendingClientCount: number;
|
|
34
34
|
readonly pendingClients: {
|
|
35
|
-
[id: string]: ServerSocket<TIncoming, TChannel, TService,
|
|
35
|
+
[id: string]: ServerSocket<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>;
|
|
36
36
|
};
|
|
37
37
|
pingIntervalMs: number;
|
|
38
38
|
pingTimeoutMs: number;
|
|
39
|
-
readonly plugins: ServerPlugin<TIncoming, TChannel, TService,
|
|
39
|
+
readonly plugins: ServerPlugin<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>[];
|
|
40
40
|
socketChannelLimit?: number;
|
|
41
41
|
readonly socketStreamCleanupMode: StreamCleanupMode;
|
|
42
42
|
strictHandshake: boolean;
|
|
43
|
-
constructor(options?: ServerOptions<TIncoming, TChannel, TService,
|
|
44
|
-
addPlugin(...plugin: ServerPlugin<TIncoming, TChannel, TService,
|
|
43
|
+
constructor(options?: ServerOptions<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>);
|
|
44
|
+
addPlugin(...plugin: ServerPlugin<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>[]): void;
|
|
45
45
|
private bind;
|
|
46
46
|
close(keepSocketsOpen?: boolean): Promise<void>;
|
|
47
47
|
emit(event: 'close', data: CloseEvent): void;
|
|
48
|
-
emit(event: 'connection', data: ConnectionEvent<TChannel, TService,
|
|
48
|
+
emit(event: 'connection', data: ConnectionEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
49
49
|
emit(event: 'error', data: ErrorEvent): void;
|
|
50
50
|
emit(event: 'headers', data: HeadersEvent): void;
|
|
51
|
-
emit(event: 'handshake', data: HandshakeEvent<TChannel, TService,
|
|
51
|
+
emit(event: 'handshake', data: HandshakeEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
52
52
|
emit(event: 'listening', data: ListeningEvent): void;
|
|
53
53
|
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,
|
|
54
|
+
emit(event: 'socketAuthStateChange', data: SocketAuthStateChangeEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
55
|
+
emit(event: 'socketAuthenticate', data: SocketAuthenticateEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
56
|
+
emit(event: 'socketBadAuthToken', data: SocketBadAuthTokenEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
57
|
+
emit(event: 'socketClose', data: SocketCloseEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
58
|
+
emit(event: 'socketConnect', data: SocketConnectEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
59
|
+
emit(event: 'socketConnectAbort', data: SocketDisconnectEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
60
|
+
emit(event: 'socketConnecting', data: SocketConnectingEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
61
|
+
emit(event: 'socketDeauthenticate', data: SocketDeauthenticateEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
62
|
+
emit(event: 'socketDisconnect', data: SocketDisconnectEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
63
|
+
emit(event: 'socketError', data: SocketErrorEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
64
|
+
emit(event: 'socketMessage', data: SocketMessageEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
65
|
+
emit(event: 'socketPing', data: SocketPingEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
66
|
+
emit(event: 'socketPong', data: SocketPongEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
67
|
+
emit(event: 'socketRemoveAuthToken', data: SocketRemoveAuthTokenEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
68
|
+
emit(event: 'socketRequest', data: SocketRequestEvent<TIncoming, TService, TPrivateIncoming>): void;
|
|
69
|
+
emit(event: 'socketResponse', data: SocketResponseEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
70
|
+
emit(event: 'socketSubscribe', data: SocketSubscribeEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
71
|
+
emit(event: 'socketSubscribeFail', data: SocketSubscribeFailEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
72
|
+
emit(event: 'socketSubscribeRequest', data: SocketSubscribeEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
73
|
+
emit(event: 'socketSubscribeStateChange', data: SocketSubscribeStateChangeEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
74
|
+
emit(event: 'socketUnsubscribe', data: SocketUnsubscribeEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>): void;
|
|
75
75
|
emit(event: 'warning', data: WarningEvent): void;
|
|
76
76
|
get exchange(): Exchange<TChannel>;
|
|
77
77
|
get isListening(): boolean;
|
|
78
78
|
get isReady(): boolean;
|
|
79
|
-
listen(): DemuxedConsumableStream<StreamEvent<ServerEvent<TChannel, TService,
|
|
79
|
+
listen(): DemuxedConsumableStream<StreamEvent<ServerEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>>;
|
|
80
80
|
listen(event: 'close'): DemuxedConsumableStream<CloseEvent>;
|
|
81
|
-
listen(event: 'connection'): DemuxedConsumableStream<ConnectionEvent<TChannel, TService,
|
|
81
|
+
listen(event: 'connection'): DemuxedConsumableStream<ConnectionEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
82
82
|
listen(event: 'error'): DemuxedConsumableStream<ErrorEvent>;
|
|
83
|
-
listen(event: 'handshake'): DemuxedConsumableStream<HandshakeEvent<TChannel, TService,
|
|
83
|
+
listen(event: 'handshake'): DemuxedConsumableStream<HandshakeEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
84
84
|
listen(event: 'headers'): DemuxedConsumableStream<HeadersEvent>;
|
|
85
85
|
listen(event: 'listening'): DemuxedConsumableStream<ListeningEvent>;
|
|
86
86
|
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,
|
|
87
|
+
listen(event: 'socketAuthStateChange'): DemuxedConsumableStream<SocketAuthStateChangeEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
88
|
+
listen(event: 'socketAuthenticate'): DemuxedConsumableStream<SocketAuthenticateEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
89
|
+
listen(event: 'socketBadAuthToken'): DemuxedConsumableStream<SocketBadAuthTokenEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
90
|
+
listen(event: 'socketClose'): DemuxedConsumableStream<SocketCloseEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
91
|
+
listen(event: 'socketConnect'): DemuxedConsumableStream<SocketConnectEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
92
|
+
listen(event: 'socketConnectAbort'): DemuxedConsumableStream<SocketDisconnectEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
93
|
+
listen(event: 'socketConnecting'): DemuxedConsumableStream<SocketConnectingEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
94
|
+
listen(event: 'socketDeauthenticate'): DemuxedConsumableStream<SocketDeauthenticateEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
95
|
+
listen(event: 'socketDisconnect'): DemuxedConsumableStream<SocketDisconnectEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
96
|
+
listen(event: 'socketError'): DemuxedConsumableStream<SocketErrorEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
97
|
+
listen(event: 'socketMessage'): DemuxedConsumableStream<SocketMessageEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
98
|
+
listen(event: 'socketPing'): DemuxedConsumableStream<SocketPingEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
99
|
+
listen(event: 'socketPong'): DemuxedConsumableStream<SocketPongEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
100
|
+
listen(event: 'socketRemoveAuthToken'): DemuxedConsumableStream<SocketRemoveAuthTokenEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
101
|
+
listen(event: 'socketRequest'): DemuxedConsumableStream<SocketRequestEvent<TIncoming, TService, TPrivateIncoming>>;
|
|
102
|
+
listen(event: 'socketResponse'): DemuxedConsumableStream<SocketResponseEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
103
|
+
listen(event: 'socketSubscribe'): DemuxedConsumableStream<SocketSubscribeEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
104
|
+
listen(event: 'socketSubscribeFail'): DemuxedConsumableStream<SocketSubscribeFailEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
105
|
+
listen(event: 'socketSubscribeRequest'): DemuxedConsumableStream<SocketSubscribeEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
106
|
+
listen(event: 'socketSubscribeStateChange'): DemuxedConsumableStream<SocketSubscribeStateChangeEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
107
|
+
listen(event: 'socketUnsubscribe'): DemuxedConsumableStream<SocketUnsubscribeEvent<TIncoming, TOutgoing, TChannel, TService, TState, TPrivateIncoming, TPrivateOutgoing, TServerState>>;
|
|
108
108
|
listen(event: 'warning'): DemuxedConsumableStream<WarningEvent>;
|
|
109
109
|
private onClose;
|
|
110
110
|
private onConnection;
|
|
111
111
|
private onError;
|
|
112
112
|
private onHeaders;
|
|
113
113
|
private onListening;
|
|
114
|
-
private onUnhandledRequest;
|
|
115
114
|
private socketDisconnected;
|
|
116
115
|
private startPinging;
|
|
117
116
|
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);
|
|
@@ -185,6 +189,7 @@ export class Server extends AsyncStreamEmitter {
|
|
|
185
189
|
listen(event) {
|
|
186
190
|
return event ? super.listen(event) : super.listen();
|
|
187
191
|
}
|
|
192
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
188
193
|
onClose(code, reason) {
|
|
189
194
|
this.emit('close', {});
|
|
190
195
|
}
|
|
@@ -200,7 +205,6 @@ export class Server extends AsyncStreamEmitter {
|
|
|
200
205
|
callIdGenerator: this._callIdGenerator,
|
|
201
206
|
codecEngine: this.codecEngine,
|
|
202
207
|
handlers: this._handlers,
|
|
203
|
-
onUnhandledRequest: this.onUnhandledRequest.bind(this),
|
|
204
208
|
plugins: this.plugins,
|
|
205
209
|
request: upgradeReq,
|
|
206
210
|
server: this,
|
|
@@ -228,8 +232,6 @@ export class Server extends AsyncStreamEmitter {
|
|
|
228
232
|
this._isListening = true;
|
|
229
233
|
this.emit('listening', {});
|
|
230
234
|
}
|
|
231
|
-
onUnhandledRequest(socket, packet) {
|
|
232
|
-
}
|
|
233
235
|
socketDisconnected(socket) {
|
|
234
236
|
if (this.pendingClients[socket.id]) {
|
|
235
237
|
delete this.pendingClients[socket.id];
|
|
@@ -294,15 +296,17 @@ export class Server extends AsyncStreamEmitter {
|
|
|
294
296
|
}
|
|
295
297
|
}
|
|
296
298
|
catch (err) {
|
|
297
|
-
|
|
299
|
+
const error = toError(err);
|
|
300
|
+
callback(false, 401, typeof err === 'string' ? err : error.message);
|
|
298
301
|
return;
|
|
299
302
|
}
|
|
300
303
|
callback(true);
|
|
301
304
|
}
|
|
302
305
|
catch (err) {
|
|
303
|
-
|
|
304
|
-
this.
|
|
305
|
-
|
|
306
|
+
const error = toError(err);
|
|
307
|
+
this.onError(error);
|
|
308
|
+
this.emit('warning', { warning: error });
|
|
309
|
+
callback(false, 403, typeof err === 'string' ? err : error.message);
|
|
306
310
|
}
|
|
307
311
|
}
|
|
308
312
|
}
|
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.0.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|