@socket-mesh/server 17.3.1

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/README.md ADDED
@@ -0,0 +1,97 @@
1
+ # SocketCluster server
2
+ Minimal server module for SocketCluster.
3
+
4
+ This is a stand-alone server module for SocketCluster.
5
+ SocketCluster's protocol is backwards compatible with the SocketCluster protocol.
6
+
7
+ ## Setting up
8
+
9
+ You will need to install both ```socketcluster-server``` and ```socketcluster-client``` (https://github.com/SocketCluster/socketcluster-client).
10
+
11
+ To install this module:
12
+ ```bash
13
+ npm install socketcluster-server
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ You need to attach it to an existing Node.js http or https server (example):
19
+ ```js
20
+ const http = require('http');
21
+ const socketClusterServer = require('socketcluster-server');
22
+
23
+ let httpServer = http.createServer();
24
+ let agServer = socketClusterServer.attach(httpServer);
25
+
26
+ (async () => {
27
+ // Handle new inbound sockets.
28
+ for await (let {socket} of agServer.listener('connection')) {
29
+
30
+ (async () => {
31
+ // Set up a loop to handle and respond to RPCs for a procedure.
32
+ for await (let req of socket.procedure('customProc')) {
33
+ if (req.data.bad) {
34
+ let error = new Error('Server failed to execute the procedure');
35
+ error.name = 'BadCustomError';
36
+ req.error(error);
37
+ } else {
38
+ req.end('Success');
39
+ }
40
+ }
41
+ })();
42
+
43
+ (async () => {
44
+ // Set up a loop to handle remote transmitted events.
45
+ for await (let data of socket.receiver('customRemoteEvent')) {
46
+ // ...
47
+ }
48
+ })();
49
+
50
+ }
51
+ })();
52
+
53
+ httpServer.listen(8000);
54
+ ```
55
+
56
+ For more detailed examples of how to use SocketCluster, see `test/integration.js`.
57
+ Also, see tests from the `socketcluster-client` module.
58
+
59
+ SocketCluster can work without the `for-await-of` loop; a `while` loop with `await` statements can be used instead.
60
+ See https://github.com/SocketCluster/stream-demux#usage
61
+
62
+ ## Compatibility mode
63
+
64
+ For compatibility with existing SocketCluster clients, set the `protocolVersion` to `1` and make sure that the `path` matches your old client path:
65
+
66
+ ```js
67
+ let agServer = socketClusterServer.attach(httpServer, {
68
+ protocolVersion: 1,
69
+ path: '/socketcluster/'
70
+ });
71
+ ```
72
+
73
+ ## Running the tests
74
+
75
+ - Clone this repo: `git clone git@github.com:SocketCluster/socketcluster-server.git`
76
+ - Navigate to project directory: `cd socketcluster-server`
77
+ - Install all dependencies: `npm install`
78
+ - Run the tests: `npm test`
79
+
80
+ ## Benefits of async `Iterable` over `EventEmitter`
81
+
82
+ - **More readable**: Code is written sequentially from top to bottom. It avoids event handler callback hell. It's also much easier to write and read complex integration test scenarios.
83
+ - **More succinct**: Event streams can be easily chained, filtered and combined using a declarative syntax (e.g. using async generators).
84
+ - **More manageable**: No need to remember to unbind listeners with `removeListener(...)`; just `break` out of the `for-await-of` loop to stop consuming. This also encourages a more declarative style of coding which reduces the likelihood of memory leaks and unintended side effects.
85
+ - **Less error-prone**: Each event/RPC/message can be processed sequentially in the same order that they were sent without missing any data; even if asynchronous calls are made inside middleware or listeners. On the other hand, with `EventEmitter`, the listener function for the same event cannot be prevented from running multiple times in parallel; also, asynchronous calls within middleware and listeners can affect the final order of actions; all this can cause unintended side effects.
86
+
87
+ ## License
88
+
89
+ (The MIT License)
90
+
91
+ Copyright (c) 2013-2023 SocketCluster.io
92
+
93
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
94
+
95
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
96
+
97
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/action.d.ts ADDED
@@ -0,0 +1,109 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import { AuthTokenError, AuthTokenExpiredError } from "@socket-mesh/errors";
4
+ import { AuthState, AuthToken, SignedAuthToken } from "@socket-mesh/auth";
5
+ import { IncomingMessage } from 'http';
6
+ import { DataPacket } from "@socket-mesh/simple-broker";
7
+ import { ChannelOptions } from "@socket-mesh/channel";
8
+ import { Request } from "@socket-mesh/request";
9
+ import { ServerSocket } from "./serversocket";
10
+ export declare enum ActionType {
11
+ HANDSHAKE_WS = "handshakeWS",
12
+ HANDSHAKE_SC = "handshakeSC",
13
+ MESSAGE = "message",
14
+ TRANSMIT = "transmit",
15
+ INVOKE = "invoke",
16
+ SUBSCRIBE = "subscribe",
17
+ PUBLISH_IN = "publishIn",
18
+ PUBLISH_OUT = "publishOut",
19
+ AUTHENTICATE = "authenticate"
20
+ }
21
+ export interface ActionResult<T> {
22
+ data: T;
23
+ options?: ActionOptions;
24
+ }
25
+ export interface ActionOptions {
26
+ useCache: boolean;
27
+ }
28
+ export declare abstract class Action<T, U extends ActionType = ActionType> {
29
+ readonly type: U;
30
+ readonly promise: Promise<ActionResult<T> | undefined>;
31
+ outcome: 'allowed' | 'blocked' | null;
32
+ private _resolve;
33
+ private _reject;
34
+ constructor(type: U);
35
+ allow(packet?: ActionResult<T>): void;
36
+ block(error: Error): void;
37
+ }
38
+ export type MiddlewareAction<T> = ActionTransmit<T> | ActionInvoke<T> | ActionPublishIn<T> | ActionPublishOut<T> | ActionSubscribe<T> | ActionHandshakeSC<T> | ActionHandshakeWS | ActionAuthenticate<T> | ActionMessage<T>;
39
+ export type InboundAction<T> = ActionAuthenticate<T> | ActionTransmit<T> | ActionInvoke<T> | ActionPublishIn<T> | ActionSubscribe<T>;
40
+ export declare class ActionHandshakeWS extends Action<undefined, ActionType.HANDSHAKE_WS> {
41
+ constructor(request: IncomingMessage);
42
+ request: IncomingMessage;
43
+ }
44
+ export type AuthInfo = {
45
+ signedAuthToken: SignedAuthToken;
46
+ authTokenError: AuthTokenError;
47
+ authToken: null;
48
+ authState: AuthState;
49
+ } | {
50
+ signedAuthToken: SignedAuthToken;
51
+ authTokenError: null;
52
+ authToken: AuthToken;
53
+ authState: AuthState;
54
+ };
55
+ export declare class ActionHandshakeSC<T> extends Action<AuthInfo, ActionType.HANDSHAKE_SC> {
56
+ constructor(socket: ServerSocket<T>, request: Request<{
57
+ authToken: string;
58
+ }>, data: AuthInfo);
59
+ request: Request<{
60
+ authToken: string;
61
+ }>;
62
+ socket: ServerSocket<T>;
63
+ data: AuthInfo;
64
+ }
65
+ export declare class ActionMessage<T> extends Action<string | ArrayBuffer | Buffer[], ActionType.MESSAGE> {
66
+ constructor(socket: ServerSocket<T>, data: string | ArrayBuffer | Buffer[]);
67
+ socket: ServerSocket<T>;
68
+ data: string | ArrayBuffer | Buffer[];
69
+ }
70
+ export declare class ActionTransmit<T> extends Action<T, ActionType.TRANSMIT> {
71
+ constructor(socket: ServerSocket<T>, receiver: string, packet: DataPacket<T>);
72
+ socket: ServerSocket<T>;
73
+ authTokenExpiredError?: AuthTokenExpiredError;
74
+ receiver: string;
75
+ data?: DataPacket<T>;
76
+ }
77
+ export declare class ActionInvoke<T> extends Action<T, ActionType.INVOKE> {
78
+ constructor(socket: ServerSocket<T>, procedure: string, packet?: DataPacket<T>);
79
+ socket: ServerSocket<T>;
80
+ authTokenExpiredError?: AuthTokenExpiredError;
81
+ procedure: string;
82
+ data?: DataPacket<T>;
83
+ }
84
+ export declare class ActionSubscribe<T> extends Action<ChannelOptions, ActionType.SUBSCRIBE> {
85
+ constructor(socket: ServerSocket<T>, packet?: DataPacket<ChannelOptions>);
86
+ socket: ServerSocket<T>;
87
+ authTokenExpiredError?: AuthTokenExpiredError;
88
+ channel?: string;
89
+ data?: ChannelOptions;
90
+ }
91
+ export declare class ActionPublishIn<T> extends Action<T, ActionType.PUBLISH_IN> {
92
+ constructor(socket: ServerSocket<T>, packet?: Partial<DataPacket<T>>);
93
+ socket: ServerSocket<T>;
94
+ authTokenExpiredError?: AuthTokenExpiredError;
95
+ channel?: string;
96
+ data?: T;
97
+ }
98
+ export declare class ActionPublishOut<T> extends Action<T, ActionType.PUBLISH_OUT> {
99
+ constructor(socket: ServerSocket<T>);
100
+ socket: ServerSocket<T>;
101
+ channel?: string;
102
+ data?: T;
103
+ }
104
+ export declare class ActionAuthenticate<T> extends Action<undefined, ActionType.AUTHENTICATE> {
105
+ constructor(socket: ServerSocket<T>, authToken: AuthToken, signedAuthToken: SignedAuthToken);
106
+ socket: ServerSocket<T>;
107
+ authToken: AuthToken;
108
+ signedAuthToken: string;
109
+ }
package/action.js ADDED
@@ -0,0 +1,112 @@
1
+ import { InvalidActionError } from "@socket-mesh/errors";
2
+ export var ActionType;
3
+ (function (ActionType) {
4
+ ActionType["HANDSHAKE_WS"] = "handshakeWS";
5
+ ActionType["HANDSHAKE_SC"] = "handshakeSC";
6
+ ActionType["MESSAGE"] = "message";
7
+ ActionType["TRANSMIT"] = "transmit";
8
+ ActionType["INVOKE"] = "invoke";
9
+ ActionType["SUBSCRIBE"] = "subscribe";
10
+ ActionType["PUBLISH_IN"] = "publishIn";
11
+ ActionType["PUBLISH_OUT"] = "publishOut";
12
+ ActionType["AUTHENTICATE"] = "authenticate";
13
+ })(ActionType || (ActionType = {}));
14
+ export class Action {
15
+ constructor(type) {
16
+ this.type = type;
17
+ this.outcome = null;
18
+ this.promise = new Promise((resolve, reject) => {
19
+ this._resolve = resolve;
20
+ this._reject = reject;
21
+ });
22
+ }
23
+ allow(packet) {
24
+ if (this.outcome) {
25
+ throw new InvalidActionError(`Action ${this.type} has already been ${this.outcome}; cannot allow`);
26
+ }
27
+ this.outcome = 'allowed';
28
+ this._resolve(packet);
29
+ }
30
+ block(error) {
31
+ if (this.outcome) {
32
+ throw new InvalidActionError(`Action ${this.type} has already been ${this.outcome}; cannot block`);
33
+ }
34
+ this.outcome = 'blocked';
35
+ this._reject(error);
36
+ }
37
+ }
38
+ export class ActionHandshakeWS extends Action {
39
+ constructor(request) {
40
+ super(ActionType.HANDSHAKE_WS);
41
+ this.request = request;
42
+ }
43
+ }
44
+ export class ActionHandshakeSC extends Action {
45
+ constructor(socket, request, data) {
46
+ super(ActionType.HANDSHAKE_SC);
47
+ this.socket = socket;
48
+ this.request = request;
49
+ this.data = data;
50
+ }
51
+ }
52
+ export class ActionMessage extends Action {
53
+ constructor(socket, data) {
54
+ super(ActionType.MESSAGE);
55
+ this.socket = socket;
56
+ this.data = data;
57
+ }
58
+ }
59
+ export class ActionTransmit extends Action {
60
+ constructor(socket, receiver, packet) {
61
+ super(ActionType.TRANSMIT);
62
+ this.socket = socket;
63
+ this.receiver = receiver;
64
+ if (packet !== undefined) {
65
+ this.data = packet;
66
+ }
67
+ }
68
+ }
69
+ export class ActionInvoke extends Action {
70
+ constructor(socket, procedure, packet) {
71
+ super(ActionType.INVOKE);
72
+ this.socket = socket;
73
+ this.procedure = procedure;
74
+ if (packet !== undefined) {
75
+ this.data = packet;
76
+ }
77
+ }
78
+ }
79
+ export class ActionSubscribe extends Action {
80
+ constructor(socket, packet) {
81
+ super(ActionType.SUBSCRIBE);
82
+ this.socket = socket;
83
+ if (packet) {
84
+ this.channel = packet.channel;
85
+ this.data = packet.data;
86
+ }
87
+ }
88
+ }
89
+ export class ActionPublishIn extends Action {
90
+ constructor(socket, packet) {
91
+ super(ActionType.PUBLISH_IN);
92
+ this.socket = socket;
93
+ if (packet) {
94
+ this.channel = packet.channel;
95
+ this.data = packet.data;
96
+ }
97
+ }
98
+ }
99
+ export class ActionPublishOut extends Action {
100
+ constructor(socket) {
101
+ super(ActionType.PUBLISH_OUT);
102
+ this.socket = socket;
103
+ }
104
+ }
105
+ export class ActionAuthenticate extends Action {
106
+ constructor(socket, authToken, signedAuthToken) {
107
+ super(ActionType.AUTHENTICATE);
108
+ this.socket = socket;
109
+ this.authToken = authToken;
110
+ this.signedAuthToken = signedAuthToken;
111
+ }
112
+ }
package/events.d.ts ADDED
@@ -0,0 +1,54 @@
1
+ /// <reference types="node" />
2
+ import { AuthToken, SignedAuthToken, AuthStateChange } from "@socket-mesh/auth";
3
+ import { ChannelOptions } from "@socket-mesh/channel";
4
+ export type ServerSocketEvents = RawMessage | AuthStateChanged | AuthTokenSigned | Authenticated | Deauthenticated | BadAuthToken | Connected | Subscribed | Unsubscribed | ConnectAborted | Disconnected | Closed | ErrorOccurred | void;
5
+ export type RawMessage = {
6
+ message: string | ArrayBuffer | Buffer[];
7
+ };
8
+ export interface AuthStateChanged extends AuthStateChange {
9
+ authToken?: AuthToken | null;
10
+ }
11
+ export interface AuthTokenSigned {
12
+ signedAuthToken: SignedAuthToken;
13
+ }
14
+ export interface Authenticated {
15
+ authToken?: AuthToken | null;
16
+ }
17
+ export interface Deauthenticated {
18
+ oldAuthToken?: AuthToken | null;
19
+ }
20
+ export interface BadAuthToken {
21
+ authError: Error;
22
+ signedAuthToken: string;
23
+ }
24
+ export interface Connected {
25
+ id: string;
26
+ pingTimeout: number;
27
+ authError?: Error;
28
+ isAuthenticated: boolean;
29
+ }
30
+ export interface Subscribed {
31
+ channel: string;
32
+ subscriptionOptions: ChannelOptions;
33
+ }
34
+ export interface Unsubscribed {
35
+ channel: string;
36
+ }
37
+ export interface ConnectAborted {
38
+ code: number;
39
+ reason: string;
40
+ }
41
+ export interface Disconnected {
42
+ code: number;
43
+ reason: string;
44
+ }
45
+ export interface Closed {
46
+ code: number;
47
+ reason: string;
48
+ }
49
+ export interface ErrorOccurred {
50
+ error: Error;
51
+ }
52
+ export interface WarningOccurred {
53
+ warning: Error;
54
+ }
package/events.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,48 @@
1
+ import { SignedAuthToken } from "@socket-mesh/auth";
2
+ import { ChannelOptions } from "@socket-mesh/channel";
3
+ import { DataPacket } from "@socket-mesh/simple-broker";
4
+ export type InboundPacket<T> = InboundHandshakePacket | InboundDataPacket<T> | InboundPublishPacket<T> | InboundSubscribePacket | InboundUnsubscribePacket | InboundAuthenticatePacket | InboundRemoveAuthTokenPacket;
5
+ export interface InboundHandshakePacket {
6
+ event: '#handshake';
7
+ cid: number;
8
+ data: {
9
+ authToken: SignedAuthToken;
10
+ };
11
+ }
12
+ export interface InboundSubscribePacket {
13
+ event: '#subscribe';
14
+ cid: number;
15
+ data: DataPacket<ChannelOptions>;
16
+ }
17
+ export interface InboundUnsubscribePacket {
18
+ event: '#unsubscribe';
19
+ cid?: number;
20
+ data: string;
21
+ }
22
+ export interface InboundPublishPacket<T> {
23
+ event: '#publish';
24
+ cid?: number;
25
+ data?: Partial<DataPacket<T>>;
26
+ }
27
+ export interface InboundAuthenticatePacket {
28
+ event: '#authenticate';
29
+ cid: number;
30
+ data: string;
31
+ }
32
+ export interface InboundRemoveAuthTokenPacket {
33
+ event: '#removeAuthToken';
34
+ data: undefined;
35
+ }
36
+ export interface InboundDataPacket<T> {
37
+ event: string | null;
38
+ rid: number | null;
39
+ cid?: number | null;
40
+ data: DataPacket<T>;
41
+ error: any;
42
+ }
43
+ export declare function isHandshakePacket<T>(packet: InboundPacket<T>): packet is InboundHandshakePacket;
44
+ export declare function isSubscribePacket<T>(packet: InboundPacket<T>): packet is InboundSubscribePacket;
45
+ export declare function isUnsubscribePacket<T>(packet: InboundPacket<T>): packet is InboundUnsubscribePacket;
46
+ export declare function isPublishPacket<T>(packet: InboundPacket<T>): packet is InboundPublishPacket<T>;
47
+ export declare function isAuthenticatePacket<T>(packet: InboundPacket<T>): packet is InboundAuthenticatePacket;
48
+ export declare function isRemoveAuthTokenPacket<T>(packet: InboundPacket<T>): packet is InboundRemoveAuthTokenPacket;
@@ -0,0 +1,18 @@
1
+ export function isHandshakePacket(packet) {
2
+ return packet.event === '#handshake';
3
+ }
4
+ export function isSubscribePacket(packet) {
5
+ return packet.event === '#subscribe';
6
+ }
7
+ export function isUnsubscribePacket(packet) {
8
+ return packet.event === '#unsubscribe';
9
+ }
10
+ export function isPublishPacket(packet) {
11
+ return packet.event === '#publish';
12
+ }
13
+ export function isAuthenticatePacket(packet) {
14
+ return packet.event === '#authenticate';
15
+ }
16
+ export function isRemoveAuthTokenPacket(packet) {
17
+ return packet.event === '#removeAuthToken';
18
+ }
package/index.d.ts ADDED
@@ -0,0 +1,28 @@
1
+ /// <reference types="node" />
2
+ import http from "http";
3
+ import { Server } from "./server";
4
+ import { ServerOptions } from "./server-options";
5
+ export { Server } from "./server";
6
+ export { ServerSocket } from "./serversocket";
7
+ /**
8
+ * Creates an http.Server exclusively used for WS upgrades.
9
+ *
10
+ * @param {Number} port
11
+ * @param {Function} callback
12
+ * @param {Object} options
13
+ * @return {AGServer} websocket cluster server
14
+ * @api public
15
+ */
16
+ export declare function listen<T>(): Server<T>;
17
+ export declare function listen<T>(port: number, fn: () => void): Server<T>;
18
+ export declare function listen<T>(port: number, options: ServerOptions): Server<T>;
19
+ export declare function listen<T>(port: number, options: ServerOptions, fn: () => void): Server<T>;
20
+ /**
21
+ * Captures upgrade requests for a http.Server.
22
+ *
23
+ * @param {http.Server} server
24
+ * @param {Object} options
25
+ * @return {AGServer} websocket cluster server
26
+ * @api public
27
+ */
28
+ export declare function attach<T>(server: http.Server, options?: ServerOptions): Server<T>;
package/index.js ADDED
@@ -0,0 +1,35 @@
1
+ import http from "http";
2
+ import { Server } from "./server";
3
+ export { Server } from "./server";
4
+ export { ServerSocket } from "./serversocket";
5
+ export function listen(port, options, fn) {
6
+ if (typeof options === 'function') {
7
+ fn = options;
8
+ options = {};
9
+ }
10
+ const server = http.createServer((req, res) => {
11
+ res.writeHead(501);
12
+ res.end('Not Implemented');
13
+ });
14
+ const socketClusterServer = attach(server, options);
15
+ socketClusterServer.httpServer = server;
16
+ server.listen(port, fn);
17
+ return socketClusterServer;
18
+ }
19
+ ;
20
+ /**
21
+ * Captures upgrade requests for a http.Server.
22
+ *
23
+ * @param {http.Server} server
24
+ * @param {Object} options
25
+ * @return {AGServer} websocket cluster server
26
+ * @api public
27
+ */
28
+ export function attach(server, options) {
29
+ if (options == null) {
30
+ options = {};
31
+ }
32
+ options.httpServer = server;
33
+ return new Server(options);
34
+ }
35
+ ;
@@ -0,0 +1,7 @@
1
+ import { WritableConsumableStream } from "@socket-mesh/writable-consumable-stream";
2
+ import { MiddlewareType } from "./middleware-type";
3
+ import { Action } from "./action";
4
+ export declare class MiddlewareStream<T> extends WritableConsumableStream<Action<T>> {
5
+ readonly type: MiddlewareType;
6
+ constructor(type: MiddlewareType);
7
+ }
@@ -0,0 +1,7 @@
1
+ import { WritableConsumableStream } from "@socket-mesh/writable-consumable-stream";
2
+ export class MiddlewareStream extends WritableConsumableStream {
3
+ constructor(type) {
4
+ super();
5
+ this.type = type;
6
+ }
7
+ }
@@ -0,0 +1,6 @@
1
+ export declare enum MiddlewareType {
2
+ MIDDLEWARE_HANDSHAKE = "handshake",
3
+ MIDDLEWARE_INBOUND_RAW = "inboundRaw",
4
+ MIDDLEWARE_INBOUND = "inbound",
5
+ MIDDLEWARE_OUTBOUND = "outbound"
6
+ }
@@ -0,0 +1,7 @@
1
+ export var MiddlewareType;
2
+ (function (MiddlewareType) {
3
+ MiddlewareType["MIDDLEWARE_HANDSHAKE"] = "handshake";
4
+ MiddlewareType["MIDDLEWARE_INBOUND_RAW"] = "inboundRaw";
5
+ MiddlewareType["MIDDLEWARE_INBOUND"] = "inbound";
6
+ MiddlewareType["MIDDLEWARE_OUTBOUND"] = "outbound";
7
+ })(MiddlewareType || (MiddlewareType = {}));
@@ -0,0 +1,25 @@
1
+ import { DataPacket } from "@socket-mesh/simple-broker";
2
+ export type OutboundPacket<T> = OutboundPublishPacket<T> | OutboundTransmitPacket<T> | OutboundInvokePacket<T>;
3
+ export interface OutboundPublishPacket<T> {
4
+ event: '#publish';
5
+ data: DataPacket<T>;
6
+ options?: OutboundPacketOptions;
7
+ }
8
+ export interface OutboundTransmitPacket<T> {
9
+ event: string;
10
+ data: T;
11
+ options?: OutboundPacketOptions;
12
+ }
13
+ export interface OutboundInvokePacket<T> {
14
+ event: string;
15
+ data: T;
16
+ options?: OutboundPacketOptions;
17
+ resolve: (value: any) => void;
18
+ reject: (err: Error) => void;
19
+ }
20
+ export interface OutboundPacketOptions {
21
+ ackTimeout?: number;
22
+ useCache?: boolean;
23
+ stringifiedData?: string;
24
+ }
25
+ export declare function isPublishOutPacket<T>(packet: OutboundPacket<T>): packet is OutboundPublishPacket<T>;
@@ -0,0 +1,3 @@
1
+ export function isPublishOutPacket(packet) {
2
+ return packet.event === '#publish';
3
+ }
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "@socket-mesh/server",
3
+ "version": "17.3.1",
4
+ "description": "Server module for SocketMesh",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git://github.com/socket-mesh/server.git"
10
+ },
11
+ "dependencies": {
12
+ "@socket-mesh/async-stream-emitter": "^4.2.4",
13
+ "@socket-mesh/consumable-stream": "3.2.0",
14
+ "@socket-mesh/auth": "^2.1.5",
15
+ "@socket-mesh/errors": "^3.0.4",
16
+ "@socket-mesh/formatter": "^4.0.3",
17
+ "@socket-mesh/request": "^2.0.3",
18
+ "@socket-mesh/server-auth": "^0.0.3",
19
+ "@socket-mesh/simple-broker": "^5.1.5",
20
+ "@socket-mesh/stream-demux": "^9.0.5",
21
+ "@socket-mesh/writable-consumable-stream": "^4.2.0",
22
+ "@socket-mesh/channel": "6.0.15",
23
+ "base64id": "^2.0.0",
24
+ "clone-deep": "^4.0.1",
25
+ "jsonwebtoken": "^9.0.0",
26
+ "ws": "^8.13.0"
27
+ },
28
+ "devDependencies": {
29
+ "@socket-mesh/local-storage": "^1.0.0",
30
+ "@types/base64id": "^2.0.0",
31
+ "@types/clone-deep": "^4.0.1",
32
+ "@types/jsonwebtoken": "^9.0.1",
33
+ "@types/ws": "^8.5.4",
34
+ "cross-env": "^7.0.3",
35
+ "jest": "^29.5.0",
36
+ "ts-jest": "^29.0.5",
37
+ "ts-node": "^10.9.1",
38
+ "typescript": "^5.0.4"
39
+ },
40
+ "scripts": {
41
+ "build": "node ./scripts/build.mjs && tsc",
42
+ "deploy": "cd dist && npm publish --access public",
43
+ "test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --config ./jest.config.ts"
44
+ },
45
+ "keywords": [
46
+ "websocket",
47
+ "realtime",
48
+ "socketcluster",
49
+ "socketmesh"
50
+ ],
51
+ "author": "Jonathan Gros-Dubois <grosjona@yahoo.com.au>",
52
+ "contributors": [
53
+ "Greg Kimmy",
54
+ {
55
+ "name": "Jonathan Gros-Dubois",
56
+ "email": "grosjona@yahoo.com.au"
57
+ }
58
+ ],
59
+ "license": "MIT"
60
+ }
package/request.d.ts ADDED
@@ -0,0 +1,26 @@
1
+ import { Request } from "@socket-mesh/request";
2
+ import { RequestSocket } from "@socket-mesh/request/request";
3
+ import { Connected } from "./events";
4
+ import { DataPacket } from "@socket-mesh/simple-broker";
5
+ import { ChannelOptions } from "@socket-mesh/channel";
6
+ export type InboundRequest<T> = HandshakeRequest | SubscribeRequest | UnsubscribeRequest | AuthenticateRequest | PublishRequest<T>;
7
+ export interface HandshakeData {
8
+ authToken: string;
9
+ }
10
+ export declare class HandshakeRequest extends Request<HandshakeData, Connected> {
11
+ constructor(socket: RequestSocket, id: number, procedureName: '#handshake', data: HandshakeData);
12
+ }
13
+ export type SubscribeRequest = Request<Partial<DataPacket<ChannelOptions>>, void>;
14
+ export type PublishRequest<T> = Request<Partial<DataPacket<T>>, void>;
15
+ export declare class UnsubscribeRequest extends Request<string, void> {
16
+ constructor(socket: RequestSocket, id: number, procedureName: '#unsubscribe', data: string);
17
+ }
18
+ export interface AuthenticateData {
19
+ isAuthenticated: boolean;
20
+ authError: Error | null;
21
+ }
22
+ export declare class AuthenticateRequest extends Request<string, AuthenticateData> {
23
+ constructor(socket: RequestSocket, id: number, procedureName: '#authenticate', data: string);
24
+ }
25
+ export declare function isSubscribeRequest(request: Request<unknown>): request is SubscribeRequest;
26
+ export declare function isPublishRequest<T>(request: Request<unknown>): request is PublishRequest<T>;