@socket-mesh/server 18.1.4 → 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.
- package/dist/broker/broker-events.d.ts +4 -4
- package/dist/broker/broker.d.ts +10 -10
- package/dist/broker/broker.js +2 -1
- package/dist/broker/exchange-client.d.ts +1 -1
- package/dist/broker/exchange.d.ts +1 -1
- package/dist/broker/exchange.js +2 -1
- package/dist/broker/index.d.ts +5 -0
- package/dist/broker/index.js +5 -0
- package/dist/broker/simple-broker.d.ts +8 -8
- package/dist/broker/simple-broker.js +26 -23
- package/dist/broker/simple-exchange.d.ts +7 -7
- package/dist/broker/simple-exchange.js +15 -12
- package/dist/{server-event.d.ts → events/index.d.ts} +15 -15
- package/dist/handlers/authenticate.d.ts +11 -11
- package/dist/handlers/authenticate.js +29 -28
- package/dist/handlers/handshake.d.ts +2 -2
- package/dist/handlers/handshake.js +17 -13
- package/dist/handlers/index.d.ts +7 -0
- package/dist/handlers/index.js +7 -0
- package/dist/handlers/publish.d.ts +3 -3
- package/dist/handlers/publish.js +2 -2
- package/dist/handlers/remove-auth-token.d.ts +3 -3
- package/dist/handlers/server-request-handler.d.ts +6 -6
- package/dist/handlers/subscribe.d.ts +3 -3
- package/dist/handlers/subscribe.js +3 -3
- package/dist/handlers/unsubscribe.d.ts +2 -2
- package/dist/handlers/unsubscribe.js +2 -2
- package/dist/index.d.ts +20 -18
- package/dist/index.js +19 -20
- package/dist/plugin/server-plugin.d.ts +15 -15
- package/dist/server-options.d.ts +11 -11
- package/dist/server-socket.d.ts +16 -15
- package/dist/server-socket.js +11 -5
- package/dist/server-transport.d.ts +14 -13
- package/dist/server-transport.js +44 -36
- package/dist/server.d.ts +54 -54
- package/dist/server.js +100 -73
- package/package.json +46 -17
- /package/dist/{server-event.js → events/index.js} +0 -0
package/dist/server.d.ts
CHANGED
|
@@ -1,69 +1,56 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AsyncStreamEmitter } from '@socket-mesh/async-stream-emitter';
|
|
2
|
+
import { AuthEngine } from '@socket-mesh/auth-engine';
|
|
3
|
+
import { ChannelMap } from '@socket-mesh/channels';
|
|
4
|
+
import { PrivateMethodMap, PublicMethodMap, ServiceMap, StreamCleanupMode } from '@socket-mesh/core';
|
|
5
|
+
import { CodecEngine } from '@socket-mesh/formatter';
|
|
6
|
+
import { DemuxedConsumableStream, StreamEvent } from '@socket-mesh/stream-demux';
|
|
2
7
|
import { Server as HttpServer } from 'http';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { ServerPlugin } from
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { DemuxedConsumableStream, StreamEvent } from "@socket-mesh/stream-demux";
|
|
10
|
-
import { ServerOptions } from "./server-options.js";
|
|
11
|
-
import { Broker } from "./broker/broker.js";
|
|
12
|
-
import { Exchange } from "./broker/exchange.js";
|
|
13
|
-
import { ChannelMap } from "@socket-mesh/channels";
|
|
8
|
+
import { Broker } from './broker/broker.js';
|
|
9
|
+
import { Exchange } from './broker/exchange.js';
|
|
10
|
+
import { CloseEvent, ConnectionEvent, ErrorEvent, HandshakeEvent, HeadersEvent, ListeningEvent, ServerEvent, SocketAuthenticateEvent, SocketAuthStateChangeEvent, SocketBadAuthTokenEvent, SocketCloseEvent, SocketConnectEvent, SocketConnectingEvent, SocketDeauthenticateEvent, SocketDisconnectEvent, SocketErrorEvent, SocketMessageEvent, SocketPingEvent, SocketPongEvent, SocketRemoveAuthTokenEvent, SocketRequestEvent, SocketResponseEvent, SocketSubscribeEvent, SocketSubscribeFailEvent, SocketSubscribeStateChangeEvent, SocketUnsubscribeEvent, WarningEvent } from './events/index.js';
|
|
11
|
+
import { ServerPlugin } from './plugin/server-plugin.js';
|
|
12
|
+
import { ServerOptions } from './server-options.js';
|
|
13
|
+
import { ServerSocket } from './server-socket.js';
|
|
14
14
|
export declare class Server<TIncoming extends PublicMethodMap = {}, TChannel extends ChannelMap = {}, TService extends ServiceMap = {}, TOutgoing extends PublicMethodMap = {}, TPrivateIncoming extends PrivateMethodMap = {}, TPrivateOutgoing extends PrivateMethodMap = {}, TServerState extends object = {}, TState extends object = {}> extends AsyncStreamEmitter<ServerEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>> {
|
|
15
15
|
private readonly _callIdGenerator;
|
|
16
|
-
private
|
|
17
|
-
private _isReady;
|
|
16
|
+
private _handlers;
|
|
18
17
|
private _isListening;
|
|
18
|
+
private _isReady;
|
|
19
19
|
private _pingIntervalRef;
|
|
20
|
-
private
|
|
20
|
+
private readonly _wss;
|
|
21
21
|
ackTimeoutMs: number;
|
|
22
22
|
allowClientPublish: boolean;
|
|
23
|
-
pingIntervalMs: number;
|
|
24
|
-
isPingTimeoutDisabled: boolean;
|
|
25
|
-
origins: string;
|
|
26
|
-
pingTimeoutMs: number;
|
|
27
|
-
socketChannelLimit?: number;
|
|
28
|
-
strictHandshake: boolean;
|
|
29
23
|
readonly auth: AuthEngine;
|
|
30
24
|
readonly brokerEngine: Broker<TChannel>;
|
|
25
|
+
clientCount: number;
|
|
31
26
|
readonly clients: {
|
|
32
27
|
[id: string]: ServerSocket<TIncoming, TChannel, TService, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>;
|
|
33
28
|
};
|
|
34
|
-
clientCount: number;
|
|
35
29
|
readonly codecEngine: CodecEngine;
|
|
30
|
+
readonly httpServer: HttpServer;
|
|
31
|
+
isPingTimeoutDisabled: boolean;
|
|
32
|
+
origins: string;
|
|
33
|
+
pendingClientCount: number;
|
|
36
34
|
readonly pendingClients: {
|
|
37
35
|
[id: string]: ServerSocket<TIncoming, TChannel, TService, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>;
|
|
38
36
|
};
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
readonly httpServer: HttpServer;
|
|
37
|
+
pingIntervalMs: number;
|
|
38
|
+
pingTimeoutMs: number;
|
|
42
39
|
readonly plugins: ServerPlugin<TIncoming, TChannel, TService, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>[];
|
|
40
|
+
socketChannelLimit?: number;
|
|
41
|
+
readonly socketStreamCleanupMode: StreamCleanupMode;
|
|
42
|
+
strictHandshake: boolean;
|
|
43
43
|
constructor(options?: ServerOptions<TIncoming, TChannel, TService, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>);
|
|
44
44
|
addPlugin(...plugin: ServerPlugin<TIncoming, TChannel, TService, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>[]): void;
|
|
45
45
|
private bind;
|
|
46
46
|
close(keepSocketsOpen?: boolean): Promise<void>;
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
private onListening;
|
|
55
|
-
private onUnhandledRequest;
|
|
56
|
-
private socketDisconnected;
|
|
57
|
-
private startPinging;
|
|
58
|
-
private stopPinging;
|
|
59
|
-
private verifyClient;
|
|
60
|
-
emit(event: "close", data: CloseEvent): void;
|
|
61
|
-
emit(event: "connection", data: ConnectionEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>): void;
|
|
62
|
-
emit(event: "error", data: ErrorEvent): void;
|
|
63
|
-
emit(event: "headers", data: HeadersEvent): void;
|
|
64
|
-
emit(event: "handshake", data: HandshakeEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>): void;
|
|
65
|
-
emit(event: "listening", data: ListeningEvent): void;
|
|
66
|
-
emit(event: "ready", data: {}): void;
|
|
47
|
+
emit(event: 'close', data: CloseEvent): void;
|
|
48
|
+
emit(event: 'connection', data: ConnectionEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>): void;
|
|
49
|
+
emit(event: 'error', data: ErrorEvent): void;
|
|
50
|
+
emit(event: 'headers', data: HeadersEvent): void;
|
|
51
|
+
emit(event: 'handshake', data: HandshakeEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>): void;
|
|
52
|
+
emit(event: 'listening', data: ListeningEvent): void;
|
|
53
|
+
emit(event: 'ready', data: {}): void;
|
|
67
54
|
emit(event: 'socketAuthStateChange', data: SocketAuthStateChangeEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>): void;
|
|
68
55
|
emit(event: 'socketAuthenticate', data: SocketAuthenticateEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>): void;
|
|
69
56
|
emit(event: 'socketBadAuthToken', data: SocketBadAuthTokenEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>): void;
|
|
@@ -85,15 +72,18 @@ export declare class Server<TIncoming extends PublicMethodMap = {}, TChannel ext
|
|
|
85
72
|
emit(event: 'socketSubscribeRequest', data: SocketSubscribeEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>): void;
|
|
86
73
|
emit(event: 'socketSubscribeStateChange', data: SocketSubscribeStateChangeEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>): void;
|
|
87
74
|
emit(event: 'socketUnsubscribe', data: SocketUnsubscribeEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>): void;
|
|
88
|
-
emit(event:
|
|
75
|
+
emit(event: 'warning', data: WarningEvent): void;
|
|
76
|
+
get exchange(): Exchange<TChannel>;
|
|
77
|
+
get isListening(): boolean;
|
|
78
|
+
get isReady(): boolean;
|
|
89
79
|
listen(): DemuxedConsumableStream<StreamEvent<ServerEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>>>;
|
|
90
|
-
listen(event:
|
|
91
|
-
listen(event:
|
|
92
|
-
listen(event:
|
|
93
|
-
listen(event:
|
|
94
|
-
listen(event:
|
|
95
|
-
listen(event:
|
|
96
|
-
listen(event:
|
|
80
|
+
listen(event: 'close'): DemuxedConsumableStream<CloseEvent>;
|
|
81
|
+
listen(event: 'connection'): DemuxedConsumableStream<ConnectionEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>>;
|
|
82
|
+
listen(event: 'error'): DemuxedConsumableStream<ErrorEvent>;
|
|
83
|
+
listen(event: 'handshake'): DemuxedConsumableStream<HandshakeEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>>;
|
|
84
|
+
listen(event: 'headers'): DemuxedConsumableStream<HeadersEvent>;
|
|
85
|
+
listen(event: 'listening'): DemuxedConsumableStream<ListeningEvent>;
|
|
86
|
+
listen(event: 'ready'): DemuxedConsumableStream<{}>;
|
|
97
87
|
listen(event: 'socketAuthStateChange'): DemuxedConsumableStream<SocketAuthStateChangeEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>>;
|
|
98
88
|
listen(event: 'socketAuthenticate'): DemuxedConsumableStream<SocketAuthenticateEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>>;
|
|
99
89
|
listen(event: 'socketBadAuthToken'): DemuxedConsumableStream<SocketBadAuthTokenEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>>;
|
|
@@ -115,5 +105,15 @@ export declare class Server<TIncoming extends PublicMethodMap = {}, TChannel ext
|
|
|
115
105
|
listen(event: 'socketSubscribeRequest'): DemuxedConsumableStream<SocketSubscribeEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>>;
|
|
116
106
|
listen(event: 'socketSubscribeStateChange'): DemuxedConsumableStream<SocketSubscribeStateChangeEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>>;
|
|
117
107
|
listen(event: 'socketUnsubscribe'): DemuxedConsumableStream<SocketUnsubscribeEvent<TChannel, TService, TIncoming, TOutgoing, TPrivateIncoming, TPrivateOutgoing, TServerState, TState>>;
|
|
118
|
-
listen(event:
|
|
108
|
+
listen(event: 'warning'): DemuxedConsumableStream<WarningEvent>;
|
|
109
|
+
private onClose;
|
|
110
|
+
private onConnection;
|
|
111
|
+
private onError;
|
|
112
|
+
private onHeaders;
|
|
113
|
+
private onListening;
|
|
114
|
+
private onUnhandledRequest;
|
|
115
|
+
private socketDisconnected;
|
|
116
|
+
private startPinging;
|
|
117
|
+
private stopPinging;
|
|
118
|
+
private verifyClient;
|
|
119
119
|
}
|
package/dist/server.js
CHANGED
|
@@ -1,17 +1,42 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import { authenticateHandler } from
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
1
|
+
import { AsyncStreamEmitter } from '@socket-mesh/async-stream-emitter';
|
|
2
|
+
import { defaultAuthEngine, isAuthEngine } from '@socket-mesh/auth-engine';
|
|
3
|
+
import { removeAuthTokenHandler } from '@socket-mesh/client';
|
|
4
|
+
import { ServerProtocolError } from '@socket-mesh/errors';
|
|
5
|
+
import defaultCodec from '@socket-mesh/formatter';
|
|
6
|
+
import { WebSocketServer } from 'ws';
|
|
7
|
+
import { SimpleBroker } from './broker/simple-broker.js';
|
|
8
|
+
import { authenticateHandler } from './handlers/authenticate.js';
|
|
9
|
+
import { handshakeHandler } from './handlers/handshake.js';
|
|
10
|
+
import { publishHandler } from './handlers/publish.js';
|
|
11
|
+
import { subscribeHandler } from './handlers/subscribe.js';
|
|
12
|
+
import { unsubscribeHandler } from './handlers/unsubscribe.js';
|
|
13
|
+
import { ServerSocket } from './server-socket.js';
|
|
14
14
|
export class Server extends AsyncStreamEmitter {
|
|
15
|
+
_callIdGenerator;
|
|
16
|
+
_handlers;
|
|
17
|
+
_isListening;
|
|
18
|
+
_isReady;
|
|
19
|
+
_pingIntervalRef;
|
|
20
|
+
_wss;
|
|
21
|
+
// | ServerSocket<TIncomingMap, TServiceMap, TOutgoingMap, TPrivateIncomingMap, TPrivateOutgoingMap, TServerState, TSocketState>
|
|
22
|
+
ackTimeoutMs;
|
|
23
|
+
allowClientPublish;
|
|
24
|
+
auth;
|
|
25
|
+
brokerEngine;
|
|
26
|
+
clientCount;
|
|
27
|
+
clients;
|
|
28
|
+
codecEngine;
|
|
29
|
+
httpServer;
|
|
30
|
+
isPingTimeoutDisabled;
|
|
31
|
+
origins;
|
|
32
|
+
pendingClientCount;
|
|
33
|
+
pendingClients;
|
|
34
|
+
pingIntervalMs;
|
|
35
|
+
pingTimeoutMs;
|
|
36
|
+
plugins;
|
|
37
|
+
socketChannelLimit;
|
|
38
|
+
socketStreamCleanupMode;
|
|
39
|
+
strictHandshake;
|
|
15
40
|
constructor(options) {
|
|
16
41
|
super();
|
|
17
42
|
let cid = 1;
|
|
@@ -30,12 +55,12 @@ export class Server extends AsyncStreamEmitter {
|
|
|
30
55
|
this.clientCount = 0;
|
|
31
56
|
this.codecEngine = options.codecEngine || defaultCodec;
|
|
32
57
|
this._handlers = Object.assign({
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
58
|
+
'#authenticate': authenticateHandler,
|
|
59
|
+
'#handshake': handshakeHandler,
|
|
60
|
+
'#publish': publishHandler,
|
|
61
|
+
'#removeAuthToken': removeAuthTokenHandler,
|
|
62
|
+
'#subscribe': subscribeHandler,
|
|
63
|
+
'#unsubscribe': unsubscribeHandler
|
|
39
64
|
}, options.handlers);
|
|
40
65
|
this.httpServer = options.server;
|
|
41
66
|
this.plugins = options.plugins || [];
|
|
@@ -49,14 +74,14 @@ export class Server extends AsyncStreamEmitter {
|
|
|
49
74
|
this.socketStreamCleanupMode = options.socketStreamCleanupMode || 'kill';
|
|
50
75
|
this.strictHandshake = options.strictHandshake ?? true;
|
|
51
76
|
options.verifyClient = this.verifyClient.bind(this);
|
|
52
|
-
this._wss = new
|
|
77
|
+
this._wss = new WebSocketServer(options);
|
|
53
78
|
this._wss.on('close', this.onClose.bind(this));
|
|
54
79
|
this._wss.on('connection', this.onConnection.bind(this));
|
|
55
80
|
this._wss.on('error', this.onError.bind(this));
|
|
56
81
|
this._wss.on('headers', this.onHeaders.bind(this));
|
|
57
82
|
this._wss.on('listening', this.onListening.bind(this));
|
|
58
83
|
(async () => {
|
|
59
|
-
for await (
|
|
84
|
+
for await (const { error } of this.brokerEngine.listen('error')) {
|
|
60
85
|
this.emit('warning', { warning: error });
|
|
61
86
|
}
|
|
62
87
|
})();
|
|
@@ -80,34 +105,34 @@ export class Server extends AsyncStreamEmitter {
|
|
|
80
105
|
}
|
|
81
106
|
bind(socket) {
|
|
82
107
|
/*
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
106
|
-
})();
|
|
108
|
+
if (socket.type === 'client') {
|
|
109
|
+
(async () => {
|
|
110
|
+
for await (let event of socket.listen()) {
|
|
111
|
+
this.emit(
|
|
112
|
+
`socket${event.stream[0].toUpperCase()}${event.stream.substring(1)}` as any,
|
|
113
|
+
Object.assign(
|
|
114
|
+
{ socket },
|
|
115
|
+
event.value
|
|
116
|
+
)
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
})();
|
|
120
|
+
|
|
121
|
+
(async () => {
|
|
122
|
+
for await (let event of socket.channels.listen()) {
|
|
123
|
+
this.emit(
|
|
124
|
+
`socket${event.stream[0].toUpperCase()}${event.stream.substring(1)}` as any,
|
|
125
|
+
Object.assign(
|
|
126
|
+
{ socket },
|
|
127
|
+
event.value
|
|
128
|
+
)
|
|
129
|
+
);
|
|
107
130
|
}
|
|
108
|
-
|
|
131
|
+
})();
|
|
132
|
+
}
|
|
133
|
+
*/
|
|
109
134
|
(async () => {
|
|
110
|
-
for await (
|
|
135
|
+
for await (const _ of socket.listen('connect')) {
|
|
111
136
|
if (this.pendingClients[socket.id]) {
|
|
112
137
|
delete this.pendingClients[socket.id];
|
|
113
138
|
this.pendingClientCount--;
|
|
@@ -118,12 +143,12 @@ export class Server extends AsyncStreamEmitter {
|
|
|
118
143
|
}
|
|
119
144
|
})();
|
|
120
145
|
(async () => {
|
|
121
|
-
for await (
|
|
146
|
+
for await (const _ of socket.listen('connectAbort')) {
|
|
122
147
|
this.socketDisconnected(socket);
|
|
123
148
|
}
|
|
124
149
|
})();
|
|
125
150
|
(async () => {
|
|
126
|
-
for await (
|
|
151
|
+
for await (const _ of socket.listen('disconnect')) {
|
|
127
152
|
this.socketDisconnected(socket);
|
|
128
153
|
}
|
|
129
154
|
})();
|
|
@@ -139,12 +164,15 @@ export class Server extends AsyncStreamEmitter {
|
|
|
139
164
|
resolve();
|
|
140
165
|
});
|
|
141
166
|
if (!keepSocketsOpen) {
|
|
142
|
-
for (
|
|
167
|
+
for (const socket of Object.values(this.clients)) {
|
|
143
168
|
socket.disconnect();
|
|
144
169
|
}
|
|
145
170
|
}
|
|
146
171
|
});
|
|
147
172
|
}
|
|
173
|
+
emit(event, data) {
|
|
174
|
+
super.emit(event, data);
|
|
175
|
+
}
|
|
148
176
|
get exchange() {
|
|
149
177
|
return this.brokerEngine.exchange;
|
|
150
178
|
}
|
|
@@ -154,32 +182,35 @@ export class Server extends AsyncStreamEmitter {
|
|
|
154
182
|
get isReady() {
|
|
155
183
|
return this._isReady;
|
|
156
184
|
}
|
|
185
|
+
listen(event) {
|
|
186
|
+
return event ? super.listen(event) : super.listen();
|
|
187
|
+
}
|
|
157
188
|
onClose(code, reason) {
|
|
158
189
|
this.emit('close', {});
|
|
159
190
|
}
|
|
160
191
|
onConnection(wsSocket, upgradeReq) {
|
|
161
192
|
/*
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
193
|
+
if (!wsSocket.upgradeReq) {
|
|
194
|
+
// Normalize ws modules to match.
|
|
195
|
+
wsSocket.upgradeReq = upgradeReq;
|
|
196
|
+
}
|
|
197
|
+
*/
|
|
167
198
|
const socket = new ServerSocket({
|
|
168
199
|
ackTimeoutMs: this.ackTimeoutMs,
|
|
169
200
|
callIdGenerator: this._callIdGenerator,
|
|
170
201
|
codecEngine: this.codecEngine,
|
|
171
202
|
handlers: this._handlers,
|
|
172
|
-
plugins: this.plugins,
|
|
173
203
|
onUnhandledRequest: this.onUnhandledRequest.bind(this),
|
|
204
|
+
plugins: this.plugins,
|
|
174
205
|
request: upgradeReq,
|
|
175
|
-
socket: wsSocket,
|
|
176
206
|
server: this,
|
|
207
|
+
socket: wsSocket,
|
|
177
208
|
state: {},
|
|
178
209
|
streamCleanupMode: this.socketStreamCleanupMode
|
|
179
210
|
});
|
|
180
211
|
this.pendingClientCount++;
|
|
181
212
|
this.bind(this.pendingClients[socket.id] = socket);
|
|
182
|
-
//
|
|
213
|
+
// ws.on('error', console.error);
|
|
183
214
|
this.emit('connection', { socket, upgradeReq });
|
|
184
215
|
// Emit event to signal that a socket handshake has been initiated.
|
|
185
216
|
this.emit('handshake', { socket });
|
|
@@ -200,11 +231,11 @@ export class Server extends AsyncStreamEmitter {
|
|
|
200
231
|
onUnhandledRequest(socket, packet) {
|
|
201
232
|
}
|
|
202
233
|
socketDisconnected(socket) {
|
|
203
|
-
if (
|
|
234
|
+
if (this.pendingClients[socket.id]) {
|
|
204
235
|
delete this.pendingClients[socket.id];
|
|
205
236
|
this.pendingClientCount--;
|
|
206
237
|
}
|
|
207
|
-
if (
|
|
238
|
+
if (this.clients[socket.id]) {
|
|
208
239
|
delete this.clients[socket.id];
|
|
209
240
|
this.clientCount--;
|
|
210
241
|
}
|
|
@@ -218,7 +249,7 @@ export class Server extends AsyncStreamEmitter {
|
|
|
218
249
|
for (const id in this.clients) {
|
|
219
250
|
this.clients[id]
|
|
220
251
|
.ping()
|
|
221
|
-
.catch(err => {
|
|
252
|
+
.catch((err) => {
|
|
222
253
|
this.onError(err);
|
|
223
254
|
});
|
|
224
255
|
}
|
|
@@ -237,16 +268,18 @@ export class Server extends AsyncStreamEmitter {
|
|
|
237
268
|
info.origin = '*';
|
|
238
269
|
}
|
|
239
270
|
if (this.origins.indexOf('*:*') === -1) {
|
|
240
|
-
let
|
|
271
|
+
let isOk = false;
|
|
241
272
|
try {
|
|
242
273
|
const url = new URL(info.origin);
|
|
243
274
|
url.port = url.port || (url.protocol === 'https:' ? '443' : '80');
|
|
244
|
-
|
|
245
|
-
~this.origins.indexOf(url.hostname + ':*')
|
|
246
|
-
~this.origins.indexOf('*:' + url.port));
|
|
275
|
+
isOk = !!(~this.origins.indexOf(url.hostname + ':' + url.port)
|
|
276
|
+
|| ~this.origins.indexOf(url.hostname + ':*')
|
|
277
|
+
|| ~this.origins.indexOf('*:' + url.port));
|
|
278
|
+
}
|
|
279
|
+
catch (e) {
|
|
280
|
+
// Intentional
|
|
247
281
|
}
|
|
248
|
-
|
|
249
|
-
if (!ok) {
|
|
282
|
+
if (!isOk) {
|
|
250
283
|
const error = new ServerProtocolError(`Failed to authorize socket handshake - Invalid origin: ${info.origin}`);
|
|
251
284
|
this.emit('warning', { warning: error });
|
|
252
285
|
callback(false, 403, error.message);
|
|
@@ -272,10 +305,4 @@ export class Server extends AsyncStreamEmitter {
|
|
|
272
305
|
callback(false, 403, typeof err === 'string' ? err : err.message);
|
|
273
306
|
}
|
|
274
307
|
}
|
|
275
|
-
emit(event, data) {
|
|
276
|
-
super.emit(event, data);
|
|
277
|
-
}
|
|
278
|
-
listen(event) {
|
|
279
|
-
return super.listen(event);
|
|
280
|
-
}
|
|
281
308
|
}
|
package/package.json
CHANGED
|
@@ -1,18 +1,42 @@
|
|
|
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": "18.1.
|
|
4
|
+
"version": "18.1.5",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
8
|
"files": ["./dist"],
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./broker": {
|
|
16
|
+
"types": "./dist/broker/index.d.ts",
|
|
17
|
+
"import": "./dist/broker/index.js",
|
|
18
|
+
"default": "./dist/broker/index.js"
|
|
19
|
+
},
|
|
20
|
+
"./events": {
|
|
21
|
+
"types": "./dist/events/index.d.ts",
|
|
22
|
+
"import": "./dist/events/index.js",
|
|
23
|
+
"default": "./dist/events/index.js"
|
|
24
|
+
},
|
|
25
|
+
"./handlers": {
|
|
26
|
+
"types": "./dist/handlers/index.d.ts",
|
|
27
|
+
"import": "./dist/handlers/index.js",
|
|
28
|
+
"default": "./dist/handlers/index.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
9
31
|
"author": {
|
|
10
32
|
"name": "Greg Kimmy"
|
|
11
33
|
},
|
|
12
34
|
"scripts": {
|
|
13
35
|
"build": "node ../../scripts/build.mjs && tsc --project tsconfig.build.json",
|
|
14
|
-
"deploy": "
|
|
15
|
-
"
|
|
36
|
+
"deploy": "node ../../scripts/publish.mjs",
|
|
37
|
+
"lint": "eslint . --c ../../eslint.config.mjs",
|
|
38
|
+
"lint:fix": "eslint . --c ../../eslint.config.mjs --fix",
|
|
39
|
+
"test": "tsx --tsconfig ./tsconfig.build.json --test ./src/**/*.{spec,test}.ts ./test/**/*.{spec,test}.ts"
|
|
16
40
|
},
|
|
17
41
|
"repository": {
|
|
18
42
|
"type": "git",
|
|
@@ -22,22 +46,27 @@
|
|
|
22
46
|
"url": "https://github.com/socket-mesh/client-server/labels/server"
|
|
23
47
|
},
|
|
24
48
|
"devDependencies": {
|
|
25
|
-
"@socket-mesh/local-storage": "
|
|
26
|
-
"@
|
|
27
|
-
"@types/
|
|
28
|
-
"@types/
|
|
49
|
+
"@socket-mesh/local-storage": "workspace:^",
|
|
50
|
+
"@socket-mesh/typescript-config": "workspace:^",
|
|
51
|
+
"@types/base64id": "catalog:base64id",
|
|
52
|
+
"@types/jsonwebtoken": "catalog:json-web-token",
|
|
53
|
+
"@types/ws": "catalog:ws"
|
|
29
54
|
},
|
|
30
55
|
"dependencies": {
|
|
31
|
-
"@socket-mesh/async-stream-emitter": "
|
|
32
|
-
"@socket-mesh/auth": "
|
|
33
|
-
"@socket-mesh/auth-engine": "
|
|
34
|
-
"@socket-mesh/
|
|
35
|
-
"@socket-mesh/
|
|
36
|
-
"@socket-mesh/
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
56
|
+
"@socket-mesh/async-stream-emitter": "workspace:^",
|
|
57
|
+
"@socket-mesh/auth": "workspace:^",
|
|
58
|
+
"@socket-mesh/auth-engine": "workspace:^",
|
|
59
|
+
"@socket-mesh/channels": "workspace:^",
|
|
60
|
+
"@socket-mesh/client": "workspace:^",
|
|
61
|
+
"@socket-mesh/core": "workspace:^",
|
|
62
|
+
"@socket-mesh/errors": "workspace:^",
|
|
63
|
+
"@socket-mesh/formatter": "workspace:^",
|
|
64
|
+
"@socket-mesh/stream-demux": "workspace:^",
|
|
65
|
+
"@socket-mesh/writable-consumable-stream": "workspace:^",
|
|
66
|
+
"base64id": "catalog:base64id",
|
|
67
|
+
"events": "catalog:",
|
|
68
|
+
"jsonwebtoken": "catalog:json-web-token",
|
|
69
|
+
"ws": "catalog:ws"
|
|
41
70
|
},
|
|
42
71
|
"keywords": [
|
|
43
72
|
"ncom",
|
|
File without changes
|