@socket-mesh/server 18.1.3 → 18.1.5

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 (46) 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 +1 -1
  6. package/dist/broker/exchange.js +2 -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 +7 -7
  12. package/dist/broker/simple-exchange.js +15 -12
  13. package/dist/events/index.d.ts +49 -0
  14. package/dist/handlers/authenticate.d.ts +12 -13
  15. package/dist/handlers/authenticate.js +29 -28
  16. package/dist/handlers/handshake.d.ts +3 -7
  17. package/dist/handlers/handshake.js +17 -13
  18. package/dist/handlers/index.d.ts +7 -0
  19. package/dist/handlers/index.js +7 -0
  20. package/dist/handlers/publish.d.ts +4 -10
  21. package/dist/handlers/publish.js +2 -2
  22. package/dist/handlers/remove-auth-token.d.ts +4 -3
  23. package/dist/handlers/server-request-handler.d.ts +7 -0
  24. package/dist/handlers/subscribe.d.ts +3 -7
  25. package/dist/handlers/subscribe.js +3 -3
  26. package/dist/handlers/unsubscribe.d.ts +2 -6
  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 +25 -25
  31. package/dist/server-options.d.ts +12 -11
  32. package/dist/server-socket.d.ts +21 -19
  33. package/dist/server-socket.js +11 -5
  34. package/dist/server-transport.d.ts +24 -23
  35. package/dist/server-transport.js +44 -36
  36. package/dist/server.d.ts +91 -91
  37. package/dist/server.js +100 -73
  38. package/package.json +55 -26
  39. package/dist/maps/client-map.d.ts +0 -10
  40. package/dist/maps/server-map.d.ts +0 -24
  41. package/dist/maps/socket-map.d.ts +0 -19
  42. package/dist/maps/socket-map.js +0 -1
  43. package/dist/server-event.d.ts +0 -52
  44. package/dist/server-event.js +0 -1
  45. /package/dist/{maps/client-map.js → events/index.js} +0 -0
  46. /package/dist/{maps/server-map.js → handlers/server-request-handler.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,4 @@
1
- import { ChannelMap, Channels } from "@socket-mesh/channels";
1
+ import { ChannelMap, Channels } from '@socket-mesh/channels';
2
2
  export declare abstract class Exchange<T extends ChannelMap> extends Channels<T> {
3
3
  id: string;
4
4
  }
@@ -1,3 +1,4 @@
1
- import { Channels } from "@socket-mesh/channels";
1
+ import { Channels } from '@socket-mesh/channels';
2
2
  export class Exchange extends Channels {
3
+ id;
3
4
  }
@@ -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,13 @@
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;
6
+ readonly id: string;
7
7
  constructor(broker: Broker<T>, options?: ChannelsOptions);
8
- protected trySubscribe(channel: ChannelDetails): Promise<void>;
9
- protected tryUnsubscribe(channel: ChannelDetails): Promise<void>;
8
+ invokePublish<U extends keyof T & string>(channelName: U, data: T[U]): Promise<void>;
10
9
  transmit(event: '#publish', packet: PublishOptions): Promise<void>;
11
10
  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>;
11
+ protected trySubscribe(channel: ChannelDetails): Promise<void>;
12
+ protected tryUnsubscribe(channel: ChannelDetails): Promise<void>;
13
13
  }
@@ -1,10 +1,24 @@
1
- import { Exchange } from "./exchange.js";
1
+ import { Exchange } from './exchange.js';
2
2
  export class SimpleExchange extends Exchange {
3
+ _broker;
4
+ id;
3
5
  constructor(broker, options) {
4
6
  super(options);
5
7
  this.id = 'exchange';
6
8
  this._broker = broker;
7
9
  }
10
+ async invokePublish(channelName, data) {
11
+ await this._broker.invokePublish(channelName, data);
12
+ }
13
+ async transmit(event, packet) {
14
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
15
+ if (event === '#publish') {
16
+ this._channelDataDemux.write(packet.channel, packet.data);
17
+ }
18
+ }
19
+ async transmitPublish(channelName, data) {
20
+ await this._broker.transmitPublish(channelName, data);
21
+ }
8
22
  async trySubscribe(channel) {
9
23
  channel.state = 'subscribed';
10
24
  this._channelEventDemux.write(`${channel.name}/subscribe`, { channel: channel.name });
@@ -19,15 +33,4 @@ export class SimpleExchange extends Exchange {
19
33
  this.emit('unsubscribe', { channel: channel.name });
20
34
  }
21
35
  }
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
36
  }
@@ -0,0 +1,49 @@
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, RequestEvent, ResponseEvent, CloseEvent as SCloseEvent, ErrorEvent as SErrorEvent, ServiceMap } 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<TChannel extends ChannelMap, TService extends ServiceMap, TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object, TState extends object> {
9
+ socket: ServerSocket<TIncoming, TChannel, TService, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>;
10
+ upgradeReq: IncomingMessage;
11
+ }
12
+ export interface ErrorEvent {
13
+ error: Error;
14
+ }
15
+ export interface HandshakeEvent<TChannel extends ChannelMap, TService extends ServiceMap, TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object, TState extends object> {
16
+ socket: ServerSocket<TIncoming, TChannel, TService, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>;
17
+ }
18
+ export interface HeadersEvent {
19
+ headers: string[];
20
+ request: IncomingMessage;
21
+ }
22
+ export interface ListeningEvent {
23
+ }
24
+ export type ServerEvent<TChannel extends ChannelMap, TService extends ServiceMap, TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object, TState extends object> = CloseEvent | ConnectionEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState> | ErrorEvent | HandshakeEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState> | HeadersEvent | ListeningEvent | SocketAuthenticateEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState> | SocketAuthStateChangeEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState> | SocketBadAuthTokenEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState> | SocketCloseEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState> | SocketConnectEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState> | SocketConnectingEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState> | SocketDeauthenticateEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState> | SocketDisconnectEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState> | SocketErrorEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState> | SocketMessageEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState> | SocketPingEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState> | SocketPongEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState> | SocketRemoveAuthTokenEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState> | SocketRequestEvent<TService, TIncoming, TPrivateIncoming> | SocketResponseEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState> | WarningEvent;
25
+ export interface ServerSocketEvent<TChannel extends ChannelMap, TService extends ServiceMap, TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object, TState extends object> {
26
+ socket: ClientSocket<PublicMethodMap, TChannel, TService, TState, TOutgoing & TPrivateOutgoing, TPrivateIncoming> | ServerSocket<TIncoming, TChannel, TService, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>;
27
+ }
28
+ export type SocketAuthenticateEvent<TChannel extends ChannelMap, TService extends ServiceMap, TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object, TState extends object> = AuthenticateEvent & ServerSocketEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>;
29
+ export type SocketAuthStateChangeEvent<TChannel extends ChannelMap, TService extends ServiceMap, TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object, TState extends object> = AuthStateChangeEvent & ServerSocketEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>;
30
+ export type SocketBadAuthTokenEvent<TChannel extends ChannelMap, TService extends ServiceMap, TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object, TState extends object> = BadAuthTokenEvent & ServerSocketEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>;
31
+ export type SocketCloseEvent<TChannel extends ChannelMap, TService extends ServiceMap, TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object, TState extends object> = SCloseEvent & ServerSocketEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>;
32
+ export type SocketConnectEvent<TChannel extends ChannelMap, TService extends ServiceMap, TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object, TState extends object> = ConnectEvent & ServerSocketEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>;
33
+ export type SocketConnectingEvent<TChannel extends ChannelMap, TService extends ServiceMap, TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object, TState extends object> = ConnectingEvent & ServerSocketEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>;
34
+ export type SocketDeauthenticateEvent<TChannel extends ChannelMap, TService extends ServiceMap, TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object, TState extends object> = DeauthenticateEvent & ServerSocketEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>;
35
+ export type SocketDisconnectEvent<TChannel extends ChannelMap, TService extends ServiceMap, TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object, TState extends object> = DisconnectEvent & ServerSocketEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>;
36
+ export type SocketErrorEvent<TChannel extends ChannelMap, TService extends ServiceMap, TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object, TState extends object> = SErrorEvent & ServerSocketEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>;
37
+ export type SocketMessageEvent<TChannel extends ChannelMap, TService extends ServiceMap, TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object, TState extends object> = MessageEvent & ServerSocketEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>;
38
+ export type SocketPingEvent<TChannel extends ChannelMap, TService extends ServiceMap, TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object, TState extends object> = PingEvent & ServerSocketEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>;
39
+ export type SocketPongEvent<TChannel extends ChannelMap, TService extends ServiceMap, TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object, TState extends object> = PongEvent & ServerSocketEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>;
40
+ export type SocketRemoveAuthTokenEvent<TChannel extends ChannelMap, TService extends ServiceMap, TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object, TState extends object> = RemoveAuthTokenEvent & ServerSocketEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>;
41
+ export type SocketRequestEvent<TService extends ServiceMap, TIncoming extends PublicMethodMap, TPrivateIncoming extends PrivateMethodMap> = RequestEvent<TIncoming & TPrivateIncoming & ServerPrivateMap, TService>;
42
+ export type SocketResponseEvent<TChannel extends ChannelMap, TService extends ServiceMap, TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object, TState extends object> = ResponseEvent<TOutgoing, TPrivateOutgoing, TService> & ServerSocketEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>;
43
+ export type SocketSubscribeEvent<TChannel extends ChannelMap, TService extends ServiceMap, TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object, TState extends object> = SubscribeEvent & ServerSocketEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>;
44
+ export type SocketSubscribeFailEvent<TChannel extends ChannelMap, TService extends ServiceMap, TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object, TState extends object> = SubscribeFailEvent & ServerSocketEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>;
45
+ export type SocketSubscribeStateChangeEvent<TChannel extends ChannelMap, TService extends ServiceMap, TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object, TState extends object> = SubscribeStateChangeEvent & ServerSocketEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>;
46
+ export type SocketUnsubscribeEvent<TChannel extends ChannelMap, TService extends ServiceMap, TIncoming extends PublicMethodMap, TOutgoing extends PublicMethodMap, TPrivateIncoming extends PrivateMethodMap, TPrivateOutgoing extends PrivateMethodMap, TServerState extends object, TState extends object> = UnsubscribeEvent & ServerSocketEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>;
47
+ export interface WarningEvent {
48
+ warning: Error;
49
+ }
@@ -1,20 +1,19 @@
1
- import { RequestHandlerArgs, SocketTransport } from "@socket-mesh/core";
2
- import jwt from "jsonwebtoken";
3
- import { AuthTokenError } from "@socket-mesh/errors";
4
- import { AuthEngine } from "@socket-mesh/auth-engine";
5
- import { AuthToken, SignedAuthToken } from "@socket-mesh/auth";
6
- import { BasicSocketMapServer } from "../maps/socket-map.js";
7
- import { ServerSocket } from "../server-socket.js";
8
- import { BasicServerMap } from "../maps/server-map.js";
9
- 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;
10
9
  export interface InvalidAuthInfo {
11
- signedAuthToken: SignedAuthToken;
12
10
  authError: AuthTokenError;
11
+ signedAuthToken: SignedAuthToken;
13
12
  }
14
13
  export interface ValidAuthInfo {
15
- signedAuthToken: SignedAuthToken;
16
14
  authToken: AuthToken;
15
+ signedAuthToken: SignedAuthToken;
17
16
  }
18
- export declare function authenticateHandler({ isRpc, options: signedAuthToken, socket, transport }: RequestHandlerArgs<string, BasicSocketMapServer, ServerSocket<BasicServerMap>>): Promise<void>;
17
+ export declare function authenticateHandler({ isRpc, options: signedAuthToken, socket, transport }: ServerRequestHandlerArgs<string>): Promise<void>;
18
+ export declare function processAuthentication(socket: ServerSocket, transport: ServerTransport, authInfo: AuthInfo): Promise<boolean>;
19
19
  export declare function validateAuthToken(auth: AuthEngine, authToken: SignedAuthToken, verificationOptions?: jwt.VerifyOptions): Promise<AuthInfo>;
20
- export declare function processAuthentication(socket: ServerSocket<BasicServerMap>, transport: SocketTransport<BasicSocketMapServer>, authInfo: AuthInfo): Promise<boolean>;
@@ -1,4 +1,4 @@
1
- import { AuthTokenError, AuthTokenExpiredError, AuthTokenInvalidError, AuthTokenNotBeforeError, InvalidActionError } from "@socket-mesh/errors";
1
+ import { AuthTokenError, AuthTokenExpiredError, AuthTokenInvalidError, AuthTokenNotBeforeError, InvalidActionError } from '@socket-mesh/errors';
2
2
  const HANDSHAKE_REJECTION_STATUS_CODE = 4008;
3
3
  ;
4
4
  export async function authenticateHandler({ isRpc, options: signedAuthToken, socket, transport }) {
@@ -9,38 +9,12 @@ export async function authenticateHandler({ isRpc, options: signedAuthToken, soc
9
9
  const authInfo = await validateAuthToken(socket.server.auth, signedAuthToken);
10
10
  await processAuthentication(socket, transport, authInfo);
11
11
  }
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
12
  export async function processAuthentication(socket, transport, authInfo) {
40
13
  if ('authError' in authInfo) {
41
14
  await transport.changeToUnauthenticatedState();
42
15
  // If the error is related to the JWT being badly formatted, then we will
43
16
  // treat the error as a socket error.
17
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
44
18
  if (authInfo.signedAuthToken != null) {
45
19
  transport.onError(authInfo.authError);
46
20
  if (authInfo.authError instanceof AuthTokenError) {
@@ -65,3 +39,30 @@ export async function processAuthentication(socket, transport, authInfo) {
65
39
  }
66
40
  return await transport.setAuthorization(authInfo.signedAuthToken, authInfo.authToken);
67
41
  }
42
+ function processTokenError(err) {
43
+ if (err.name === 'TokenExpiredError' && 'expiredAt' in err) {
44
+ return new AuthTokenExpiredError(err.message, err.expiredAt);
45
+ }
46
+ if (err.name === 'JsonWebTokenError') {
47
+ return new AuthTokenInvalidError(err.message);
48
+ }
49
+ if (err.name === 'NotBeforeError' && 'date' in err) {
50
+ // In this case, the token is good; it's just not active yet.
51
+ return new AuthTokenNotBeforeError(err.message, err.date);
52
+ }
53
+ return new AuthTokenError(err.message);
54
+ }
55
+ export async function validateAuthToken(auth, authToken, verificationOptions) {
56
+ try {
57
+ return {
58
+ authToken: await auth.verifyToken(authToken, verificationOptions),
59
+ signedAuthToken: authToken
60
+ };
61
+ }
62
+ catch (error) {
63
+ return {
64
+ authError: processTokenError(error),
65
+ signedAuthToken: authToken
66
+ };
67
+ }
68
+ }
@@ -1,7 +1,3 @@
1
- import { HandshakeOptions, HandshakeStatus } from "@socket-mesh/client";
2
- import { RequestHandlerArgs } from "@socket-mesh/core";
3
- import { BasicServerMap } from "../maps/server-map.js";
4
- import { BasicSocketMapServer } from "../maps/socket-map.js";
5
- import { ServerSocket } from "../server-socket.js";
6
- import { ServerTransport } from "../server-transport.js";
7
- export declare function handshakeHandler({ options, socket, transport }: RequestHandlerArgs<HandshakeOptions, BasicSocketMapServer, ServerSocket<BasicServerMap>, ServerTransport<BasicServerMap>>): Promise<HandshakeStatus>;
1
+ import { HandshakeOptions, HandshakeStatus } from '@socket-mesh/client';
2
+ import { ServerRequestHandlerArgs } from './server-request-handler.js';
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 { 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,11 +10,11 @@ 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) {
@@ -26,11 +26,15 @@ export async function handshakeHandler({ options, socket, transport }) {
26
26
  }
27
27
  }
28
28
  let authError = undefined;
29
- let changed;
29
+ let changed = false;
30
30
  try {
31
31
  changed = await processAuthentication(socket, transport, authInfo);
32
32
  if (socket.status === 'closed') {
33
- return;
33
+ return {
34
+ authToken: options.authToken,
35
+ id: socket.id,
36
+ pingTimeoutMs: server.pingTimeoutMs
37
+ };
34
38
  }
35
39
  }
36
40
  catch (err) {
@@ -48,14 +52,14 @@ export async function handshakeHandler({ options, socket, transport }) {
48
52
  }
49
53
  if (authError) {
50
54
  return {
55
+ authError,
51
56
  id: socket.id,
52
- pingTimeoutMs: server.pingTimeoutMs,
53
- authError
57
+ pingTimeoutMs: server.pingTimeoutMs
54
58
  };
55
59
  }
56
60
  return {
61
+ authToken: options.authToken,
57
62
  id: socket.id,
58
- pingTimeoutMs: server.pingTimeoutMs,
59
- authToken: options.authToken
63
+ pingTimeoutMs: server.pingTimeoutMs
60
64
  };
61
65
  }
@@ -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,11 +1,5 @@
1
- import { RequestHandlerArgs } from "@socket-mesh/core";
2
- import { BasicSocketMapServer } from "../maps/socket-map.js";
3
- import { PublishOptions } from "@socket-mesh/channels";
4
- import { ServerSocket } from "../server-socket.js";
5
- import { BasicServerMap } from "../maps/server-map.js";
6
- import { ServerTransport } from "../server-transport.js";
7
- export declare function publishHandler({ socket, transport, options }: RequestHandlerArgs<PublishOptions, BasicSocketMapServer<{}, {
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, {}, {
8
4
  [channel: string]: any;
9
- }>, ServerSocket<BasicServerMap<{}, {
10
- [channel: string]: any;
11
- }>>, ServerTransport<BasicServerMap>>): Promise<void>;
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,3 +1,4 @@
1
- import { BasicSocketMapServer } from "../maps/socket-map.js";
2
- import { SocketTransport } from "@socket-mesh/core";
3
- export declare function deauthenticate(transport: SocketTransport<BasicSocketMapServer>): Promise<boolean>;
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>;
@@ -0,0 +1,7 @@
1
+ import { ChannelMap } from '@socket-mesh/channels';
2
+ import { ClientPrivateMap, ServerPrivateMap } from '@socket-mesh/client';
3
+ import { PrivateMethodMap, PublicMethodMap, RequestHandlerArgs, ServiceMap } from '@socket-mesh/core';
4
+ import { ServerSocketState } from '../server-socket-state.js';
5
+ import { ServerSocket } from '../server-socket.js';
6
+ import { ServerTransport } from '../server-transport.js';
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,7 +1,3 @@
1
- import { BasicServerMap } from "../maps/server-map.js";
2
- import { BasicSocketMapServer } from "../maps/socket-map.js";
3
- import { SubscribeOptions } from "@socket-mesh/client";
4
- import { RequestHandlerArgs } from "@socket-mesh/core";
5
- import { ServerSocket } from "../server-socket.js";
6
- import { ServerTransport } from "../server-transport.js";
7
- export declare function subscribeHandler({ socket, transport, options }: RequestHandlerArgs<SubscribeOptions, BasicSocketMapServer, ServerSocket<BasicServerMap>, ServerTransport<BasicServerMap>>): 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;
@@ -1,6 +1,2 @@
1
- import { RequestHandlerArgs } from "@socket-mesh/core";
2
- import { BasicSocketMapServer } from "../maps/socket-map.js";
3
- import { ServerSocket } from "../server-socket.js";
4
- import { BasicServerMap } from "../maps/server-map.js";
5
- import { ServerTransport } from "../server-transport.js";
6
- export declare function unsubscribeHandler({ transport, options: channel }: RequestHandlerArgs<string, BasicSocketMapServer, ServerSocket<BasicServerMap>, ServerTransport<BasicServerMap>>): Promise<void>;
1
+ import { ServerRequestHandlerArgs } from './server-request-handler.js';
2
+ export declare function unsubscribeHandler({ options: channel, transport }: ServerRequestHandlerArgs<string>): Promise<void>;