@socket-mesh/server 18.1.4 → 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.
Files changed (40) hide show
  1. package/dist/broker/broker-events.d.ts +4 -4
  2. package/dist/broker/broker.d.ts +10 -10
  3. package/dist/broker/broker.js +2 -1
  4. package/dist/broker/exchange-client.d.ts +1 -1
  5. package/dist/broker/exchange.d.ts +6 -1
  6. package/dist/broker/exchange.js +6 -1
  7. package/dist/broker/index.d.ts +5 -0
  8. package/dist/broker/index.js +5 -0
  9. package/dist/broker/simple-broker.d.ts +8 -8
  10. package/dist/broker/simple-broker.js +26 -23
  11. package/dist/broker/simple-exchange.d.ts +6 -7
  12. package/dist/broker/simple-exchange.js +18 -14
  13. package/dist/events/index.d.ts +49 -0
  14. package/dist/handlers/authenticate.d.ts +11 -11
  15. package/dist/handlers/authenticate.js +31 -28
  16. package/dist/handlers/handshake.d.ts +2 -2
  17. package/dist/handlers/handshake.js +21 -16
  18. package/dist/handlers/index.d.ts +7 -0
  19. package/dist/handlers/index.js +7 -0
  20. package/dist/handlers/publish.d.ts +3 -3
  21. package/dist/handlers/publish.js +2 -2
  22. package/dist/handlers/remove-auth-token.d.ts +4 -4
  23. package/dist/handlers/server-request-handler.d.ts +6 -7
  24. package/dist/handlers/subscribe.d.ts +3 -3
  25. package/dist/handlers/subscribe.js +3 -3
  26. package/dist/handlers/unsubscribe.d.ts +2 -2
  27. package/dist/handlers/unsubscribe.js +2 -2
  28. package/dist/index.d.ts +20 -18
  29. package/dist/index.js +19 -20
  30. package/dist/plugin/server-plugin.d.ts +23 -25
  31. package/dist/server-options.d.ts +14 -11
  32. package/dist/server-socket.d.ts +68 -19
  33. package/dist/server-socket.js +27 -8
  34. package/dist/server-transport.d.ts +33 -23
  35. package/dist/server-transport.js +59 -50
  36. package/dist/server.d.ts +89 -90
  37. package/dist/server.js +110 -79
  38. package/package.json +46 -17
  39. package/dist/server-event.d.ts +0 -49
  40. /package/dist/{server-event.js → events/index.js} +0 -0
@@ -2,6 +2,10 @@ export type BrokerEvent<T> = ErrorEvent | PublishEvent<T> | ReadyEvent | Subscri
2
2
  export interface ErrorEvent {
3
3
  error: Error;
4
4
  }
5
+ export interface PublishEvent<T> {
6
+ channel: string;
7
+ data: T;
8
+ }
5
9
  export interface ReadyEvent {
6
10
  }
7
11
  export interface SubscribeEvent {
@@ -10,7 +14,3 @@ export interface SubscribeEvent {
10
14
  export interface UnsubscribeEvent {
11
15
  channel: string;
12
16
  }
13
- export interface PublishEvent<T> {
14
- channel: string;
15
- data: T;
16
- }
@@ -1,10 +1,11 @@
1
- import { AsyncStreamEmitter } from "@socket-mesh/async-stream-emitter";
2
- import { DemuxedConsumableStream, StreamEvent } from "@socket-mesh/stream-demux";
3
- import { BrokerEvent, ErrorEvent, PublishEvent, ReadyEvent, SubscribeEvent, UnsubscribeEvent } from "./broker-events.js";
4
- import { ChannelMap } from "@socket-mesh/channels";
5
- import { Exchange } from "./exchange.js";
6
- import { ExchangeClient } from "./exchange-client.js";
1
+ import { AsyncStreamEmitter } from '@socket-mesh/async-stream-emitter';
2
+ import { ChannelMap } from '@socket-mesh/channels';
3
+ import { DemuxedConsumableStream, StreamEvent } from '@socket-mesh/stream-demux';
4
+ import { BrokerEvent, ErrorEvent, PublishEvent, ReadyEvent, SubscribeEvent, UnsubscribeEvent } from './broker-events.js';
5
+ import { ExchangeClient } from './exchange-client.js';
6
+ import { Exchange } from './exchange.js';
7
7
  export declare abstract class Broker<T extends ChannelMap> extends AsyncStreamEmitter<BrokerEvent<T[keyof T]>> {
8
+ abstract readonly exchange: Exchange<T>;
8
9
  isReady: boolean;
9
10
  constructor();
10
11
  emit(event: 'error', data: ErrorEvent): void;
@@ -12,17 +13,16 @@ export declare abstract class Broker<T extends ChannelMap> extends AsyncStreamEm
12
13
  emit(event: 'ready', data: ReadyEvent): void;
13
14
  emit(event: 'subscribe', data: SubscribeEvent): void;
14
15
  emit(event: 'unsubscribe', data: UnsubscribeEvent): void;
16
+ abstract invokePublish<U extends keyof T & string>(channelName: U, data: T[U], suppressEvent?: boolean): Promise<void>;
17
+ abstract isSubscribed(channelName: string): boolean;
15
18
  listen(): DemuxedConsumableStream<StreamEvent<BrokerEvent<T[keyof T]>>>;
16
19
  listen(event: 'ready'): DemuxedConsumableStream<ReadyEvent>;
17
20
  listen(event: 'publish'): DemuxedConsumableStream<PublishEvent<T[keyof T]>>;
18
21
  listen(event: 'error'): DemuxedConsumableStream<ErrorEvent>;
19
22
  listen(event: 'subscribe'): DemuxedConsumableStream<SubscribeEvent>;
20
23
  listen(event: 'unsubscribe'): DemuxedConsumableStream<UnsubscribeEvent>;
21
- abstract readonly exchange: Exchange<T>;
22
24
  abstract subscribe(socket: ExchangeClient, channelName: string): Promise<void>;
23
25
  abstract subscriptions(): string[];
24
- abstract isSubscribed(channelName: string): boolean;
25
- abstract unsubscribe(socket: ExchangeClient, channelName: string): Promise<void>;
26
26
  abstract transmitPublish<U extends keyof T & string>(channelName: U, data: T[U], suppressEvent?: boolean): Promise<void>;
27
- abstract invokePublish<U extends keyof T & string>(channelName: U, data: T[U], suppressEvent?: boolean): Promise<void>;
27
+ abstract unsubscribe(socket: ExchangeClient, channelName: string): Promise<void>;
28
28
  }
@@ -1,5 +1,6 @@
1
- import { AsyncStreamEmitter } from "@socket-mesh/async-stream-emitter";
1
+ import { AsyncStreamEmitter } from '@socket-mesh/async-stream-emitter';
2
2
  export class Broker extends AsyncStreamEmitter {
3
+ isReady;
3
4
  constructor() {
4
5
  super();
5
6
  this.isReady = false;
@@ -1,4 +1,4 @@
1
- import { PublishOptions } from "@socket-mesh/channels";
1
+ import { PublishOptions } from '@socket-mesh/channels';
2
2
  export interface ExchangeClient {
3
3
  id: string;
4
4
  transmit(event: '#publish', options: PublishOptions): Promise<void>;
@@ -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 {};
@@ -1,3 +1,8 @@
1
- import { Channels } from "@socket-mesh/channels";
1
+ import { Channels } from '@socket-mesh/channels';
2
2
  export class Exchange extends Channels {
3
+ id;
4
+ constructor(options) {
5
+ super(options);
6
+ this.id = options.id;
7
+ }
3
8
  }
@@ -0,0 +1,5 @@
1
+ export * from './broker-events.js';
2
+ export * from './broker.js';
3
+ export * from './exchange-client.js';
4
+ export * from './simple-broker.js';
5
+ export * from './simple-exchange.js';
@@ -0,0 +1,5 @@
1
+ export * from './broker-events.js';
2
+ export * from './broker.js';
3
+ export * from './exchange-client.js';
4
+ export * from './simple-broker.js';
5
+ export * from './simple-exchange.js';
@@ -1,16 +1,16 @@
1
- import { Broker } from "./broker.js";
2
- import { ChannelMap } from "@socket-mesh/channels";
3
- import { SimpleExchange } from "./simple-exchange.js";
4
- import { ExchangeClient } from "./exchange-client.js";
1
+ import { ChannelMap } from '@socket-mesh/channels';
2
+ import { Broker } from './broker.js';
3
+ import { ExchangeClient } from './exchange-client.js';
4
+ import { SimpleExchange } from './simple-exchange.js';
5
5
  export declare class SimpleBroker<T extends ChannelMap> extends Broker<T> {
6
- readonly exchange: SimpleExchange<T>;
7
6
  private readonly _clientSubscribers;
8
7
  private readonly _clientSubscribersCounter;
8
+ readonly exchange: SimpleExchange<T>;
9
9
  constructor();
10
+ invokePublish<U extends keyof T & string>(channelName: U, data: T[U], suppressEvent?: boolean): Promise<void>;
11
+ isSubscribed(channelName: string): boolean;
10
12
  subscribe(client: ExchangeClient, channelName: string): Promise<void>;
11
- unsubscribe(client: ExchangeClient, channelName: string): Promise<void>;
12
13
  subscriptions(): string[];
13
- isSubscribed(channelName: string): boolean;
14
- invokePublish<U extends keyof T & string>(channelName: U, data: T[U], suppressEvent?: boolean): Promise<void>;
15
14
  transmitPublish<U extends keyof T & string>(channelName: U, data: T[U], suppressEvent?: boolean): Promise<void>;
15
+ unsubscribe(client: ExchangeClient, channelName: string): Promise<void>;
16
16
  }
@@ -1,12 +1,23 @@
1
- import { Broker } from "./broker.js";
2
- import { SimpleExchange } from "./simple-exchange.js";
1
+ import { Broker } from './broker.js';
2
+ import { SimpleExchange } from './simple-exchange.js';
3
3
  export class SimpleBroker extends Broker {
4
+ _clientSubscribers;
5
+ _clientSubscribersCounter;
6
+ exchange;
4
7
  constructor() {
5
8
  super();
6
9
  this.exchange = new SimpleExchange(this);
7
10
  this._clientSubscribers = {};
8
11
  this._clientSubscribersCounter = {};
9
12
  }
13
+ // In this implementation of the broker engine, both invokePublish and transmitPublish
14
+ // methods are the same. In alternative implementations, they could be different.
15
+ invokePublish(channelName, data, suppressEvent) {
16
+ return this.transmitPublish(channelName, data, suppressEvent);
17
+ }
18
+ isSubscribed(channelName) {
19
+ return !!this._clientSubscribers[channelName];
20
+ }
10
21
  async subscribe(client, channelName) {
11
22
  if (!this._clientSubscribers[channelName]) {
12
23
  this._clientSubscribers[channelName] = {};
@@ -18,30 +29,9 @@ export class SimpleBroker extends Broker {
18
29
  }
19
30
  this._clientSubscribers[channelName][client.id] = client;
20
31
  }
21
- async unsubscribe(client, channelName) {
22
- if (this._clientSubscribers[channelName]) {
23
- if (this._clientSubscribers[channelName][client.id]) {
24
- this._clientSubscribersCounter[channelName]--;
25
- delete this._clientSubscribers[channelName][client.id];
26
- if (this._clientSubscribersCounter[channelName] <= 0) {
27
- delete this._clientSubscribers[channelName];
28
- delete this._clientSubscribersCounter[channelName];
29
- this.emit('unsubscribe', { channel: channelName });
30
- }
31
- }
32
- }
33
- }
34
32
  subscriptions() {
35
33
  return Object.keys(this._clientSubscribers);
36
34
  }
37
- isSubscribed(channelName) {
38
- return !!this._clientSubscribers[channelName];
39
- }
40
- // In this implementation of the broker engine, both invokePublish and transmitPublish
41
- // methods are the same. In alternative implementations, they could be different.
42
- invokePublish(channelName, data, suppressEvent) {
43
- return this.transmitPublish(channelName, data, suppressEvent);
44
- }
45
35
  async transmitPublish(channelName, data, suppressEvent) {
46
36
  const packet = {
47
37
  channel: channelName,
@@ -62,4 +52,17 @@ export class SimpleBroker extends Broker {
62
52
  this.emit('publish', packet);
63
53
  }
64
54
  }
55
+ async unsubscribe(client, channelName) {
56
+ if (this._clientSubscribers[channelName]) {
57
+ if (this._clientSubscribers[channelName][client.id]) {
58
+ this._clientSubscribersCounter[channelName]--;
59
+ delete this._clientSubscribers[channelName][client.id];
60
+ if (this._clientSubscribersCounter[channelName] <= 0) {
61
+ delete this._clientSubscribers[channelName];
62
+ delete this._clientSubscribersCounter[channelName];
63
+ this.emit('unsubscribe', { channel: channelName });
64
+ }
65
+ }
66
+ }
67
+ }
65
68
  }
@@ -1,13 +1,12 @@
1
- import { ChannelDetails, ChannelMap, ChannelsOptions, PublishOptions } from "@socket-mesh/channels";
2
- import { Broker } from "./broker.js";
3
- import { Exchange } from "./exchange.js";
1
+ import { ChannelDetails, ChannelMap, ChannelsOptions, PublishOptions } from '@socket-mesh/channels';
2
+ import { Broker } from './broker.js';
3
+ import { Exchange } from './exchange.js';
4
4
  export declare class SimpleExchange<T extends ChannelMap> extends Exchange<T> {
5
- readonly id: string;
6
5
  private readonly _broker;
7
6
  constructor(broker: Broker<T>, options?: ChannelsOptions);
8
- protected trySubscribe(channel: ChannelDetails): Promise<void>;
9
- protected tryUnsubscribe(channel: ChannelDetails): Promise<void>;
7
+ invokePublish<U extends keyof T & string>(channelName: U, data: T[U]): Promise<void>;
10
8
  transmit(event: '#publish', packet: PublishOptions): Promise<void>;
11
9
  transmitPublish<U extends keyof T & string>(channelName: U, data: T[U]): Promise<void>;
12
- invokePublish<U extends keyof T & string>(channelName: U, data: T[U]): Promise<void>;
10
+ protected trySubscribe(channel: ChannelDetails): Promise<void>;
11
+ protected tryUnsubscribe(channel: ChannelDetails): Promise<void>;
13
12
  }
@@ -1,10 +1,25 @@
1
- import { Exchange } from "./exchange.js";
1
+ import { Exchange } from './exchange.js';
2
2
  export class SimpleExchange extends Exchange {
3
+ _broker;
3
4
  constructor(broker, options) {
4
- super(options);
5
- this.id = 'exchange';
5
+ super({
6
+ ...(options || {}),
7
+ id: 'exchange'
8
+ });
6
9
  this._broker = broker;
7
10
  }
11
+ async invokePublish(channelName, data) {
12
+ await this._broker.invokePublish(channelName, data);
13
+ }
14
+ async transmit(event, packet) {
15
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
16
+ if (event === '#publish') {
17
+ this._channelDataDemux.write(packet.channel, packet.data);
18
+ }
19
+ }
20
+ async transmitPublish(channelName, data) {
21
+ await this._broker.transmitPublish(channelName, data);
22
+ }
8
23
  async trySubscribe(channel) {
9
24
  channel.state = 'subscribed';
10
25
  this._channelEventDemux.write(`${channel.name}/subscribe`, { channel: channel.name });
@@ -19,15 +34,4 @@ export class SimpleExchange extends Exchange {
19
34
  this.emit('unsubscribe', { channel: channel.name });
20
35
  }
21
36
  }
22
- async transmit(event, packet) {
23
- if (event === '#publish') {
24
- this._channelDataDemux.write(packet.channel, packet.data);
25
- }
26
- }
27
- async transmitPublish(channelName, data) {
28
- await this._broker.transmitPublish(channelName, data);
29
- }
30
- async invokePublish(channelName, data) {
31
- await this._broker.invokePublish(channelName, data);
32
- }
33
37
  }
@@ -0,0 +1,49 @@
1
+ import { ChannelMap, SubscribeEvent, SubscribeFailEvent, SubscribeStateChangeEvent, UnsubscribeEvent } from '@socket-mesh/channels';
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
+ import { IncomingMessage } from 'http';
5
+ import { ServerSocket } from '../server-socket.js';
6
+ export interface CloseEvent {
7
+ }
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
+ upgradeReq: IncomingMessage;
11
+ }
12
+ export interface ErrorEvent {
13
+ error: Error;
14
+ }
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
+ }
18
+ export interface HeadersEvent {
19
+ headers: string[];
20
+ request: IncomingMessage;
21
+ }
22
+ export interface ListeningEvent {
23
+ }
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
+ export interface WarningEvent {
48
+ warning: Error;
49
+ }
@@ -1,19 +1,19 @@
1
- import jwt from "jsonwebtoken";
2
- import { AuthTokenError } from "@socket-mesh/errors";
3
- import { AuthEngine } from "@socket-mesh/auth-engine";
4
- import { AuthToken, SignedAuthToken } from "@socket-mesh/auth";
5
- import { ServerSocket } from "../server-socket.js";
6
- import { ServerTransport } from "../server-transport.js";
7
- import { ServerRequestHandlerArgs } from "./server-request-handler.js";
8
- export type AuthInfo = ValidAuthInfo | InvalidAuthInfo;
1
+ import { AuthToken, SignedAuthToken } from '@socket-mesh/auth';
2
+ import { AuthEngine } from '@socket-mesh/auth-engine';
3
+ import { AuthTokenError } from '@socket-mesh/errors';
4
+ import jwt from 'jsonwebtoken';
5
+ import { ServerSocket } from '../server-socket.js';
6
+ import { ServerTransport } from '../server-transport.js';
7
+ import { ServerRequestHandlerArgs } from './server-request-handler.js';
8
+ export type AuthInfo = InvalidAuthInfo | ValidAuthInfo;
9
9
  export interface InvalidAuthInfo {
10
- signedAuthToken: SignedAuthToken;
11
10
  authError: AuthTokenError;
11
+ signedAuthToken: SignedAuthToken;
12
12
  }
13
13
  export interface ValidAuthInfo {
14
- signedAuthToken: SignedAuthToken;
15
14
  authToken: AuthToken;
15
+ signedAuthToken: SignedAuthToken;
16
16
  }
17
17
  export declare function authenticateHandler({ isRpc, options: signedAuthToken, socket, transport }: ServerRequestHandlerArgs<string>): Promise<void>;
18
- export declare function validateAuthToken(auth: AuthEngine, authToken: SignedAuthToken, verificationOptions?: jwt.VerifyOptions): Promise<AuthInfo>;
19
18
  export declare function processAuthentication(socket: ServerSocket, transport: ServerTransport, authInfo: AuthInfo): Promise<boolean>;
19
+ export declare function validateAuthToken(auth: AuthEngine, authToken: SignedAuthToken, verificationOptions?: jwt.VerifyOptions): Promise<AuthInfo>;
@@ -1,4 +1,6 @@
1
- import { AuthTokenError, AuthTokenExpiredError, AuthTokenInvalidError, AuthTokenNotBeforeError, InvalidActionError } from "@socket-mesh/errors";
1
+ import { toError } from '@socket-mesh/core';
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 }) {
@@ -9,38 +11,12 @@ export async function authenticateHandler({ isRpc, options: signedAuthToken, soc
9
11
  const authInfo = await validateAuthToken(socket.server.auth, signedAuthToken);
10
12
  await processAuthentication(socket, transport, authInfo);
11
13
  }
12
- export async function validateAuthToken(auth, authToken, verificationOptions) {
13
- try {
14
- return {
15
- signedAuthToken: authToken,
16
- authToken: await auth.verifyToken(authToken, verificationOptions)
17
- };
18
- }
19
- catch (error) {
20
- return {
21
- signedAuthToken: authToken,
22
- authError: processTokenError(error)
23
- };
24
- }
25
- }
26
- function processTokenError(err) {
27
- if (err.name === 'TokenExpiredError' && 'expiredAt' in err) {
28
- return new AuthTokenExpiredError(err.message, err.expiredAt);
29
- }
30
- if (err.name === 'JsonWebTokenError') {
31
- return new AuthTokenInvalidError(err.message);
32
- }
33
- if (err.name === 'NotBeforeError' && 'date' in err) {
34
- // In this case, the token is good; it's just not active yet.
35
- return new AuthTokenNotBeforeError(err.message, err.date);
36
- }
37
- return new AuthTokenError(err.message);
38
- }
39
14
  export async function processAuthentication(socket, transport, authInfo) {
40
15
  if ('authError' in authInfo) {
41
16
  await transport.changeToUnauthenticatedState();
42
17
  // If the error is related to the JWT being badly formatted, then we will
43
18
  // treat the error as a socket error.
19
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
44
20
  if (authInfo.signedAuthToken != null) {
45
21
  transport.onError(authInfo.authError);
46
22
  if (authInfo.authError instanceof AuthTokenError) {
@@ -65,3 +41,30 @@ export async function processAuthentication(socket, transport, authInfo) {
65
41
  }
66
42
  return await transport.setAuthorization(authInfo.signedAuthToken, authInfo.authToken);
67
43
  }
44
+ function processTokenError(err) {
45
+ if (err instanceof jwt.TokenExpiredError) {
46
+ return new AuthTokenExpiredError(err.message, err.expiredAt);
47
+ }
48
+ if (err instanceof jwt.NotBeforeError) {
49
+ // In this case, the token is good; it's just not active yet.
50
+ return new AuthTokenNotBeforeError(err.message, err.date);
51
+ }
52
+ if (err instanceof jwt.JsonWebTokenError) {
53
+ return new AuthTokenInvalidError(err.message);
54
+ }
55
+ return new AuthTokenError(toError(err).message);
56
+ }
57
+ export async function validateAuthToken(auth, authToken, verificationOptions) {
58
+ try {
59
+ return {
60
+ authToken: await auth.verifyToken(authToken, verificationOptions),
61
+ signedAuthToken: authToken
62
+ };
63
+ }
64
+ catch (error) {
65
+ return {
66
+ authError: processTokenError(error),
67
+ signedAuthToken: authToken
68
+ };
69
+ }
70
+ }
@@ -1,3 +1,3 @@
1
- import { HandshakeOptions, HandshakeStatus } from "@socket-mesh/client";
2
- import { ServerRequestHandlerArgs } from "./server-request-handler.js";
1
+ import { HandshakeOptions, HandshakeStatus } from '@socket-mesh/client';
2
+ import { ServerRequestHandlerArgs } from './server-request-handler.js';
3
3
  export declare function handshakeHandler({ options, socket, transport }: ServerRequestHandlerArgs<HandshakeOptions>): Promise<HandshakeStatus>;
@@ -1,6 +1,6 @@
1
- import { wait } from "@socket-mesh/core";
2
- import { processAuthentication, validateAuthToken } from "./authenticate.js";
3
- import { dehydrateError } from "@socket-mesh/errors";
1
+ import { toError, wait } from '@socket-mesh/core';
2
+ import { dehydrateError } from '@socket-mesh/errors';
3
+ import { processAuthentication, validateAuthToken } from './authenticate.js';
4
4
  const HANDSHAKE_REJECTION_STATUS_CODE = 4008;
5
5
  export async function handshakeHandler({ options, socket, transport }) {
6
6
  const server = socket.server;
@@ -10,27 +10,32 @@ export async function handshakeHandler({ options, socket, transport }) {
10
10
  if (plugin.onHandshake) {
11
11
  try {
12
12
  await plugin.onHandshake({
13
+ authInfo: 'authError' in authInfo
14
+ ? { authError: authInfo.authError, signedAuthToken: options.authToken }
15
+ : { authToken: authInfo.authToken, signedAuthToken: options.authToken },
13
16
  socket,
14
- transport,
15
- authInfo: 'authError' in authInfo ?
16
- { signedAuthToken: options.authToken, authError: authInfo.authError } :
17
- { signedAuthToken: options.authToken, authToken: authInfo.authToken }
17
+ transport
18
18
  });
19
19
  }
20
20
  catch (err) {
21
- if (err.statusCode == null) {
22
- err.statusCode = HANDSHAKE_REJECTION_STATUS_CODE;
21
+ const error = toError(err);
22
+ if (!('statusCode' in error)) {
23
+ error.statusCode = HANDSHAKE_REJECTION_STATUS_CODE;
23
24
  }
24
- throw err;
25
+ throw error;
25
26
  }
26
27
  }
27
28
  }
28
29
  let authError = undefined;
29
- let changed;
30
+ let changed = false;
30
31
  try {
31
32
  changed = await processAuthentication(socket, transport, authInfo);
32
33
  if (socket.status === 'closed') {
33
- return;
34
+ return {
35
+ authToken: options.authToken,
36
+ id: socket.id,
37
+ pingTimeoutMs: server.pingTimeoutMs
38
+ };
34
39
  }
35
40
  }
36
41
  catch (err) {
@@ -48,14 +53,14 @@ export async function handshakeHandler({ options, socket, transport }) {
48
53
  }
49
54
  if (authError) {
50
55
  return {
56
+ authError,
51
57
  id: socket.id,
52
- pingTimeoutMs: server.pingTimeoutMs,
53
- authError
58
+ pingTimeoutMs: server.pingTimeoutMs
54
59
  };
55
60
  }
56
61
  return {
62
+ authToken: options.authToken,
57
63
  id: socket.id,
58
- pingTimeoutMs: server.pingTimeoutMs,
59
- authToken: options.authToken
64
+ pingTimeoutMs: server.pingTimeoutMs
60
65
  };
61
66
  }
@@ -0,0 +1,7 @@
1
+ export * from './authenticate.js';
2
+ export * from './handshake.js';
3
+ export * from './publish.js';
4
+ export * from './remove-auth-token.js';
5
+ export * from './server-request-handler.js';
6
+ export * from './subscribe.js';
7
+ export * from './unsubscribe.js';
@@ -0,0 +1,7 @@
1
+ export * from './authenticate.js';
2
+ export * from './handshake.js';
3
+ export * from './publish.js';
4
+ export * from './remove-auth-token.js';
5
+ export * from './server-request-handler.js';
6
+ export * from './subscribe.js';
7
+ export * from './unsubscribe.js';
@@ -1,5 +1,5 @@
1
- import { PublishOptions } from "@socket-mesh/channels";
2
- import { ServerRequestHandlerArgs } from "./server-request-handler.js";
3
- export declare function publishHandler({ socket, transport, options }: ServerRequestHandlerArgs<PublishOptions, {}, {
1
+ import { PublishOptions } from '@socket-mesh/channels';
2
+ import { ServerRequestHandlerArgs } from './server-request-handler.js';
3
+ export declare function publishHandler({ options, socket, transport }: ServerRequestHandlerArgs<PublishOptions, {}, {}, {
4
4
  [channel: string]: any;
5
5
  }>): Promise<void>;
@@ -1,5 +1,5 @@
1
- import { InvalidActionError } from "@socket-mesh/errors";
2
- export async function publishHandler({ socket, transport, options }) {
1
+ import { InvalidActionError } from '@socket-mesh/errors';
2
+ export async function publishHandler({ options, socket, transport }) {
3
3
  if (!socket.server.allowClientPublish) {
4
4
  throw new InvalidActionError('Client publish feature is disabled');
5
5
  }
@@ -1,4 +1,4 @@
1
- import { ClientPrivateMap, ServerPrivateMap } from "@socket-mesh/client";
2
- import { SocketTransport } from "@socket-mesh/core";
3
- import { ServerSocketState } from "../server-socket-state.js";
4
- export declare function deauthenticate(transport: SocketTransport<ServerPrivateMap, {}, ClientPrivateMap, {}, ServerSocketState>): Promise<boolean>;
1
+ import { ClientPrivateMap } from '@socket-mesh/client';
2
+ import { SocketTransport } from '@socket-mesh/core';
3
+ import { ServerSocketState } from '../server-socket-state.js';
4
+ export declare function deauthenticate(transport: SocketTransport<{}, {}, ServerSocketState, {}, {}, ClientPrivateMap>): Promise<boolean>;
@@ -1,7 +1,6 @@
1
- import { ClientPrivateMap, ServerPrivateMap } from "@socket-mesh/client";
2
- import { PrivateMethodMap, PublicMethodMap, RequestHandlerArgs, ServiceMap } from "@socket-mesh/core";
3
- import { ServerSocketState } from "../server-socket-state.js";
4
- import { ServerSocket } from "../server-socket.js";
5
- import { ServerTransport } from "../server-transport.js";
6
- import { ChannelMap } from "@socket-mesh/channels";
7
- export type ServerRequestHandlerArgs<TOptions, TIncoming extends PublicMethodMap = {}, TChannel extends ChannelMap = {}, TService extends ServiceMap = {}, TOutgoing extends PublicMethodMap = {}, TPrivateIncoming extends PrivateMethodMap = {}, TPrivateOutgoing extends PrivateMethodMap = {}, TServerState extends object = {}, TState extends object = {}> = RequestHandlerArgs<TOptions, TIncoming & TPrivateIncoming & ServerPrivateMap, TOutgoing, TPrivateOutgoing & ClientPrivateMap, TService, TState & ServerSocketState, ServerSocket<TIncoming, TChannel, TService, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>, ServerTransport<TIncoming, TChannel, TService, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>>;
1
+ import { ChannelMap } from '@socket-mesh/channels';
2
+ import { PrivateMethodMap, PublicMethodMap, RequestHandlerArgs, ServiceMap } from '@socket-mesh/core';
3
+ import { ServerSocketState } from '../server-socket-state.js';
4
+ import { ServerSocket } from '../server-socket.js';
5
+ import { ServerTransport } from '../server-transport.js';
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>>;
@@ -1,3 +1,3 @@
1
- import { SubscribeOptions } from "@socket-mesh/client";
2
- import { ServerRequestHandlerArgs } from "./server-request-handler.js";
3
- export declare function subscribeHandler({ socket, transport, options }: ServerRequestHandlerArgs<SubscribeOptions>): Promise<void>;
1
+ import { SubscribeOptions } from '@socket-mesh/client';
2
+ import { ServerRequestHandlerArgs } from './server-request-handler.js';
3
+ export declare function subscribeHandler({ options, socket, transport }: ServerRequestHandlerArgs<SubscribeOptions>): Promise<void>;
@@ -1,5 +1,5 @@
1
- import { BrokerError, InvalidActionError } from "@socket-mesh/errors";
2
- export async function subscribeHandler({ socket, transport, options }) {
1
+ import { BrokerError, InvalidActionError } from '@socket-mesh/errors';
2
+ export async function subscribeHandler({ options, socket, transport }) {
3
3
  if (socket.status !== 'ready') {
4
4
  // This is an invalid state; it means the client tried to subscribe before
5
5
  // having completed the handshake.
@@ -7,7 +7,7 @@ export async function subscribeHandler({ socket, transport, options }) {
7
7
  }
8
8
  const state = socket.state;
9
9
  const server = socket.server;
10
- if (server.socketChannelLimit && state.channelSubscriptionsCount >= server.socketChannelLimit) {
10
+ if (server.socketChannelLimit && (state.channelSubscriptionsCount || 0) >= server.socketChannelLimit) {
11
11
  throw new InvalidActionError(`Socket ${socket.id} tried to exceed the channel subscription limit of ${server.socketChannelLimit}`);
12
12
  }
13
13
  const { channel, ...channelOptions } = options;