@socket-mesh/core 1.0.3 → 1.0.4
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/index.d.ts +10 -10
- package/dist/index.js +10 -10
- package/dist/maps/handler-map.d.ts +2 -2
- package/dist/maps/index.d.ts +2 -2
- package/dist/maps/index.js +2 -2
- package/dist/maps/method-map.d.ts +8 -8
- package/dist/maps/method-map.js +1 -0
- package/dist/packet.d.ts +15 -15
- package/dist/packet.js +3 -1
- package/dist/plugins/index.d.ts +1 -1
- package/dist/plugins/index.js +1 -1
- package/dist/plugins/plugin.d.ts +27 -27
- package/dist/request-handler.d.ts +7 -7
- package/dist/request-handler.js +9 -2
- package/dist/request.d.ts +39 -39
- package/dist/request.js +13 -11
- package/dist/response.d.ts +14 -14
- package/dist/response.js +3 -1
- package/dist/socket-event.d.ts +21 -24
- package/dist/socket-transport.d.ts +39 -41
- package/dist/socket-transport.js +218 -195
- package/dist/socket.d.ts +25 -25
- package/dist/socket.js +21 -16
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +3 -3
- package/package.json +15 -12
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
6
|
-
export * from
|
|
7
|
-
export * from
|
|
8
|
-
export * from
|
|
9
|
-
export * from
|
|
10
|
-
export * from
|
|
1
|
+
export * from './maps/index.js';
|
|
2
|
+
export * from './packet.js';
|
|
3
|
+
export * from './plugins/index.js';
|
|
4
|
+
export * from './request-handler.js';
|
|
5
|
+
export * from './request.js';
|
|
6
|
+
export * from './response.js';
|
|
7
|
+
export * from './socket-event.js';
|
|
8
|
+
export * from './socket-transport.js';
|
|
9
|
+
export * from './socket.js';
|
|
10
|
+
export * from './utils.js';
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
6
|
-
export * from
|
|
7
|
-
export * from
|
|
8
|
-
export * from
|
|
9
|
-
export * from
|
|
10
|
-
export * from
|
|
1
|
+
export * from './maps/index.js';
|
|
2
|
+
export * from './packet.js';
|
|
3
|
+
export * from './plugins/index.js';
|
|
4
|
+
export * from './request-handler.js';
|
|
5
|
+
export * from './request.js';
|
|
6
|
+
export * from './response.js';
|
|
7
|
+
export * from './socket-event.js';
|
|
8
|
+
export * from './socket-transport.js';
|
|
9
|
+
export * from './socket.js';
|
|
10
|
+
export * from './utils.js';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { RequestHandler } from
|
|
2
|
-
import { MethodMap, PrivateMethodMap, PublicMethodMap, ServiceMap } from
|
|
1
|
+
import { RequestHandler } from '../request-handler.js';
|
|
2
|
+
import { MethodMap, PrivateMethodMap, PublicMethodMap, ServiceMap } from './method-map.js';
|
|
3
3
|
export type HandlerMap<TIncoming extends MethodMap, TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap, TState extends object> = Partial<{
|
|
4
4
|
[K in keyof TIncoming]: RequestHandler<Parameters<TIncoming[K]>[0], ReturnType<TIncoming[K]>, TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>;
|
|
5
5
|
}>;
|
package/dist/maps/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
1
|
+
export * from './handler-map.js';
|
|
2
|
+
export * from './method-map.js';
|
package/dist/maps/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
1
|
+
export * from './handler-map.js';
|
|
2
|
+
export * from './method-map.js';
|
|
@@ -1,12 +1,5 @@
|
|
|
1
|
-
export type RemoveIndexSignature<T> = {
|
|
2
|
-
[K in keyof T as string extends K ? never : number extends K ? never : symbol extends K ? never : K]: T[K];
|
|
3
|
-
};
|
|
4
|
-
export type MappedFunction = (...args: any) => any;
|
|
5
|
-
export type PromiseReturnType<T> = T extends Promise<infer Return> ? Return : T;
|
|
6
|
-
export type ServiceMap = {
|
|
7
|
-
[service: string]: MethodMap;
|
|
8
|
-
};
|
|
9
1
|
export type FunctionReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : never;
|
|
2
|
+
export type MappedFunction = (...args: any) => any;
|
|
10
3
|
export type MethodMap = {
|
|
11
4
|
[method: string]: MappedFunction;
|
|
12
5
|
};
|
|
@@ -15,8 +8,15 @@ export type PrivateMethodMap = {
|
|
|
15
8
|
} & {
|
|
16
9
|
[method: `#${string}`]: MappedFunction;
|
|
17
10
|
};
|
|
11
|
+
export type PromiseReturnType<T> = T extends Promise<infer Return> ? Return : T;
|
|
18
12
|
export type PublicMethodMap = {
|
|
19
13
|
[method: string]: MappedFunction;
|
|
20
14
|
} & {
|
|
21
15
|
[method: `#${string}`]: never;
|
|
22
16
|
};
|
|
17
|
+
export type RemoveIndexSignature<T> = {
|
|
18
|
+
[K in keyof T as string extends K ? never : number extends K ? never : symbol extends K ? never : K]: T[K];
|
|
19
|
+
};
|
|
20
|
+
export type ServiceMap = {
|
|
21
|
+
[service: string]: MethodMap;
|
|
22
|
+
};
|
package/dist/maps/method-map.js
CHANGED
package/dist/packet.d.ts
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
import { MethodMap, ServiceMap } from
|
|
2
|
-
export type AnyPacket<TIncoming extends MethodMap, TService extends ServiceMap> =
|
|
1
|
+
import { MethodMap, ServiceMap } from './maps/method-map.js';
|
|
2
|
+
export type AnyPacket<TIncoming extends MethodMap, TService extends ServiceMap> = MethodPacket<TIncoming> | ServicePacket<TService>;
|
|
3
|
+
export type MethodPacket<TMethodMap extends MethodMap> = {
|
|
4
|
+
[TMethod in keyof TMethodMap]: MethodRequestPacket<TMethodMap, TMethod>;
|
|
5
|
+
}[keyof TMethodMap];
|
|
6
|
+
export interface MethodRequestPacket<TMethodMap extends MethodMap, TMethod extends keyof TMethodMap> extends RequestPacket {
|
|
7
|
+
ackTimeoutMs?: boolean | number;
|
|
8
|
+
data: Parameters<TMethodMap[TMethod]>[0];
|
|
9
|
+
method: TMethod;
|
|
10
|
+
}
|
|
11
|
+
interface RequestPacket {
|
|
12
|
+
cid?: number;
|
|
13
|
+
}
|
|
3
14
|
export type ServicePacket<TServiceMap extends ServiceMap> = {
|
|
4
15
|
[TService in keyof TServiceMap]: {
|
|
5
16
|
[TMethod in keyof TServiceMap[TService]]: ServiceRequestPacket<TServiceMap, TService, TMethod>;
|
|
6
17
|
}[keyof TServiceMap[TService]];
|
|
7
18
|
}[keyof TServiceMap];
|
|
8
|
-
interface RequestPacket {
|
|
9
|
-
cid?: number;
|
|
10
|
-
}
|
|
11
|
-
export type MethodPacket<TMethodMap extends MethodMap> = {
|
|
12
|
-
[TMethod in keyof TMethodMap]: MethodRequestPacket<TMethodMap, TMethod>;
|
|
13
|
-
}[keyof TMethodMap];
|
|
14
19
|
export interface ServiceRequestPacket<TServiceMap extends ServiceMap, TService extends keyof TServiceMap, TMethod extends keyof TServiceMap[TService]> extends RequestPacket {
|
|
15
|
-
|
|
16
|
-
method: TMethod;
|
|
17
|
-
ackTimeoutMs?: number | boolean;
|
|
20
|
+
ackTimeoutMs?: boolean | number;
|
|
18
21
|
data?: Parameters<TServiceMap[TService][TMethod]>[0];
|
|
19
|
-
}
|
|
20
|
-
export interface MethodRequestPacket<TMethodMap extends MethodMap, TMethod extends keyof TMethodMap> extends RequestPacket {
|
|
21
22
|
method: TMethod;
|
|
22
|
-
|
|
23
|
-
ackTimeoutMs?: number | boolean;
|
|
23
|
+
service: TService;
|
|
24
24
|
}
|
|
25
25
|
export declare function isRequestPacket<TIncoming extends MethodMap, TService extends ServiceMap>(packet: unknown): packet is AnyPacket<TIncoming, TService>;
|
|
26
26
|
export {};
|
package/dist/packet.js
CHANGED
package/dist/plugins/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from
|
|
1
|
+
export * from './plugin.js';
|
package/dist/plugins/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from
|
|
1
|
+
export * from './plugin.js';
|
package/dist/plugins/plugin.d.ts
CHANGED
|
@@ -1,40 +1,26 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
export type PluginType = 'request' | 'response' | 'handshake';
|
|
10
|
-
export interface PluginArgs<TIncoming extends MethodMap, TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap, TState extends object> {
|
|
11
|
-
socket: Socket<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>;
|
|
12
|
-
transport: SocketTransport<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>;
|
|
13
|
-
}
|
|
1
|
+
import ws from 'isomorphic-ws';
|
|
2
|
+
import { HandlerMap } from '../maps/handler-map.js';
|
|
3
|
+
import { MethodMap, PrivateMethodMap, PublicMethodMap, ServiceMap } from '../maps/method-map.js';
|
|
4
|
+
import { AnyPacket } from '../packet.js';
|
|
5
|
+
import { AnyRequest } from '../request.js';
|
|
6
|
+
import { AnyResponse } from '../response.js';
|
|
7
|
+
import { SocketTransport } from '../socket-transport.js';
|
|
8
|
+
import { Socket, SocketStatus } from '../socket.js';
|
|
14
9
|
export interface DisconnectedPluginArgs<TIncoming extends MethodMap, TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap, TState extends object> extends PluginArgs<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState> {
|
|
15
|
-
status: SocketStatus;
|
|
16
10
|
code: number;
|
|
17
11
|
reason?: string;
|
|
12
|
+
status: SocketStatus;
|
|
18
13
|
}
|
|
19
14
|
export interface MessagePluginArgs<TIncoming extends MethodMap, TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap, TState extends object> extends PluginArgs<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState> {
|
|
20
|
-
timestamp: Date;
|
|
21
15
|
packet: AnyPacket<TIncoming, TService> | AnyResponse<TOutgoing, TPrivateOutgoing, TService>;
|
|
16
|
+
timestamp: Date;
|
|
22
17
|
}
|
|
23
18
|
export interface MessageRawPluginArgs<TIncoming extends MethodMap, TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap, TState extends object> extends PluginArgs<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState> {
|
|
24
|
-
|
|
25
|
-
message: ws.RawData | string;
|
|
19
|
+
message: string | ws.RawData;
|
|
26
20
|
promise: Promise<void>;
|
|
27
|
-
|
|
28
|
-
export interface SendRequestPluginArgs<TIncoming extends MethodMap, TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap, TState extends object> extends PluginArgs<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState> {
|
|
29
|
-
requests: AnyRequest<TOutgoing, TPrivateOutgoing, TService>[];
|
|
30
|
-
cont: (requests: AnyRequest<TOutgoing, TPrivateOutgoing, TService>[]) => void;
|
|
31
|
-
}
|
|
32
|
-
export interface SendResponsePluginArgs<TIncoming extends MethodMap, TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap, TState extends object> extends PluginArgs<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState> {
|
|
33
|
-
responses: AnyResponse<TOutgoing, TPrivateOutgoing, TService>[];
|
|
34
|
-
cont: (requests: AnyResponse<TOutgoing, TPrivateOutgoing, TService>[]) => void;
|
|
21
|
+
timestamp: Date;
|
|
35
22
|
}
|
|
36
23
|
export interface Plugin<TIncoming extends MethodMap, TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap, TState extends object> {
|
|
37
|
-
type: string;
|
|
38
24
|
handlers?: HandlerMap<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>;
|
|
39
25
|
onAuthenticated?(options: PluginArgs<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>): void;
|
|
40
26
|
onClose?(options: PluginArgs<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>): void;
|
|
@@ -42,9 +28,23 @@ export interface Plugin<TIncoming extends MethodMap, TOutgoing extends PublicMet
|
|
|
42
28
|
onDisconnected?(options: DisconnectedPluginArgs<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>): void;
|
|
43
29
|
onEnd?(options: PluginArgs<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>): void;
|
|
44
30
|
onMessage?(options: MessagePluginArgs<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>): Promise<AnyPacket<TIncoming, TService> | AnyResponse<TOutgoing, TPrivateOutgoing, TService>>;
|
|
45
|
-
onMessageRaw?(options: MessageRawPluginArgs<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>): Promise<ws.RawData
|
|
31
|
+
onMessageRaw?(options: MessageRawPluginArgs<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>): Promise<string | ws.RawData>;
|
|
46
32
|
onOpen?(options: PluginArgs<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>): void;
|
|
47
33
|
onReady?(options: PluginArgs<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>): void;
|
|
48
34
|
sendRequest?(options: SendRequestPluginArgs<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>): void;
|
|
49
35
|
sendResponse?(options: SendResponsePluginArgs<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>): void;
|
|
36
|
+
type: string;
|
|
37
|
+
}
|
|
38
|
+
export interface PluginArgs<TIncoming extends MethodMap, TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap, TState extends object> {
|
|
39
|
+
socket: Socket<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>;
|
|
40
|
+
transport: SocketTransport<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>;
|
|
41
|
+
}
|
|
42
|
+
export type PluginType = 'handshake' | 'request' | 'response';
|
|
43
|
+
export interface SendRequestPluginArgs<TIncoming extends MethodMap, TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap, TState extends object> extends PluginArgs<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState> {
|
|
44
|
+
cont: (requests: AnyRequest<TOutgoing, TPrivateOutgoing, TService>[]) => void;
|
|
45
|
+
requests: AnyRequest<TOutgoing, TPrivateOutgoing, TService>[];
|
|
46
|
+
}
|
|
47
|
+
export interface SendResponsePluginArgs<TIncoming extends MethodMap, TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap, TState extends object> extends PluginArgs<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState> {
|
|
48
|
+
cont: (requests: AnyResponse<TOutgoing, TPrivateOutgoing, TService>[]) => void;
|
|
49
|
+
responses: AnyResponse<TOutgoing, TPrivateOutgoing, TService>[];
|
|
50
50
|
}
|
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { SocketTransport } from
|
|
3
|
-
import {
|
|
1
|
+
import { MethodMap, PrivateMethodMap, PublicMethodMap, ServiceMap } from './maps/method-map.js';
|
|
2
|
+
import { SocketTransport } from './socket-transport.js';
|
|
3
|
+
import { Socket } from './socket.js';
|
|
4
|
+
export type RequestHandler<TOptions, U, TIncoming extends MethodMap, TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap, TState extends object, TSocket extends Socket<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState> = Socket<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>, TTransport extends SocketTransport<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState> = SocketTransport<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>> = (args: RequestHandlerArgs<TOptions, TIncoming, TOutgoing, TPrivateOutgoing, TService, TState, TSocket, TTransport>) => Promise<U>;
|
|
4
5
|
export interface RequestHandlerArgsOptions<TOptions, TIncoming extends MethodMap, TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap, TState extends object, TSocket extends Socket<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState> = Socket<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>, TTransport extends SocketTransport<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState> = SocketTransport<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>> {
|
|
5
6
|
isRpc: boolean;
|
|
6
7
|
method: string;
|
|
8
|
+
options: TOptions;
|
|
7
9
|
socket: TSocket;
|
|
10
|
+
timeoutMs?: boolean | number;
|
|
8
11
|
transport: TTransport;
|
|
9
|
-
timeoutMs?: number | boolean;
|
|
10
|
-
options?: TOptions;
|
|
11
12
|
}
|
|
12
13
|
export declare class RequestHandlerArgs<TOptions, TIncoming extends MethodMap = {}, TOutgoing extends PublicMethodMap = {}, TPrivateOutgoing extends PrivateMethodMap = {}, TService extends ServiceMap = {}, TState extends object = {}, TSocket extends Socket<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState> = Socket<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>, TTransport extends SocketTransport<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState> = SocketTransport<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>> {
|
|
13
14
|
isRpc: boolean;
|
|
@@ -15,10 +16,9 @@ export declare class RequestHandlerArgs<TOptions, TIncoming extends MethodMap =
|
|
|
15
16
|
options: TOptions;
|
|
16
17
|
requestedAt: Date;
|
|
17
18
|
socket: TSocket;
|
|
18
|
-
timeoutMs?:
|
|
19
|
+
timeoutMs?: boolean | number;
|
|
19
20
|
transport: TTransport;
|
|
20
21
|
constructor(options: RequestHandlerArgsOptions<TOptions, TIncoming, TOutgoing, TPrivateOutgoing, TService, TState, TSocket, TTransport>);
|
|
21
22
|
checkTimeout(timeLeftMs?: number): void;
|
|
22
23
|
getRemainingTimeMs(): number;
|
|
23
24
|
}
|
|
24
|
-
export type RequestHandler<TOptions, U, TIncoming extends MethodMap, TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap, TState extends object, TSocket extends Socket<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState> = Socket<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>, TTransport extends SocketTransport<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState> = SocketTransport<TIncoming, TOutgoing, TPrivateOutgoing, TService, TState>> = (args: RequestHandlerArgs<TOptions, TIncoming, TOutgoing, TPrivateOutgoing, TService, TState, TSocket, TTransport>) => Promise<U>;
|
package/dist/request-handler.js
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
|
-
import { TimeoutError } from
|
|
1
|
+
import { TimeoutError } from '@socket-mesh/errors';
|
|
2
2
|
export class RequestHandlerArgs {
|
|
3
|
+
isRpc;
|
|
4
|
+
method;
|
|
5
|
+
options;
|
|
6
|
+
requestedAt;
|
|
7
|
+
socket;
|
|
8
|
+
timeoutMs;
|
|
9
|
+
transport;
|
|
3
10
|
constructor(options) {
|
|
4
11
|
this.isRpc = options.isRpc;
|
|
5
12
|
this.method = options.method;
|
|
@@ -11,7 +18,7 @@ export class RequestHandlerArgs {
|
|
|
11
18
|
}
|
|
12
19
|
checkTimeout(timeLeftMs = 0) {
|
|
13
20
|
if (typeof this.timeoutMs === 'number' && this.getRemainingTimeMs() <= timeLeftMs) {
|
|
14
|
-
throw new TimeoutError(`Method
|
|
21
|
+
throw new TimeoutError(`Method '${this.method}' timed out.`);
|
|
15
22
|
}
|
|
16
23
|
}
|
|
17
24
|
getRemainingTimeMs() {
|
package/dist/request.d.ts
CHANGED
|
@@ -1,54 +1,54 @@
|
|
|
1
|
-
import { MethodMap, PrivateMethodMap, PublicMethodMap, ServiceMap } from
|
|
2
|
-
export type AnyRequest<TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap> =
|
|
1
|
+
import { MethodMap, PrivateMethodMap, PublicMethodMap, ServiceMap } from './maps/method-map.js';
|
|
2
|
+
export type AnyRequest<TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap> = MethodRequest<TOutgoing> | MethodRequest<TPrivateOutgoing> | ServiceRequest<TService>;
|
|
3
|
+
export interface InvokeMethodRequest<TMethodMap extends MethodMap, TMethod extends keyof TMethodMap> extends TransmitMethodRequest<TMethodMap, TMethod> {
|
|
4
|
+
ackTimeoutMs: false | number;
|
|
5
|
+
callback: ((err: Error | null, result?: TMethodMap[TMethod]) => void) | null;
|
|
6
|
+
cid: number;
|
|
7
|
+
timeoutId?: NodeJS.Timeout;
|
|
8
|
+
}
|
|
9
|
+
export interface InvokeServiceRequest<TServiceMap extends ServiceMap, TService extends keyof TServiceMap, TMethod extends keyof TServiceMap[TService]> extends TransmitServiceRequest<TServiceMap, TService, TMethod> {
|
|
10
|
+
ackTimeoutMs: false | number;
|
|
11
|
+
callback: ((err: Error | null, result?: TServiceMap[TService][TMethod]) => void) | null;
|
|
12
|
+
cid: number;
|
|
13
|
+
timeoutId?: NodeJS.Timeout;
|
|
14
|
+
}
|
|
15
|
+
export type MethodRequest<TMethodMap extends MethodMap> = {
|
|
16
|
+
[TMethod in keyof TMethodMap]: InvokeMethodRequest<TMethodMap, TMethod> | TransmitMethodRequest<TMethodMap, TMethod>;
|
|
17
|
+
}[keyof TMethodMap];
|
|
18
|
+
export interface Request {
|
|
19
|
+
promise: Promise<void>;
|
|
20
|
+
sentCallback?: (err?: Error) => void;
|
|
21
|
+
}
|
|
22
|
+
export type ServiceRequest<TServiceMap extends ServiceMap> = {
|
|
23
|
+
[TService in keyof TServiceMap]: {
|
|
24
|
+
[TMethod in keyof TServiceMap[TService]]: InvokeServiceRequest<TServiceMap, TService, TMethod> | TransmitServiceRequest<TServiceMap, TService, TMethod>;
|
|
25
|
+
}[keyof TServiceMap[TService]];
|
|
26
|
+
}[keyof TServiceMap];
|
|
27
|
+
export interface TransmitMethodRequest<TMethodMap extends MethodMap, TMethod extends keyof TMethodMap> extends Request {
|
|
28
|
+
data?: Parameters<TMethodMap[TMethod]>[0];
|
|
29
|
+
method: TMethod;
|
|
30
|
+
}
|
|
31
|
+
export interface TransmitServiceRequest<TServiceMap extends ServiceMap, TService extends keyof TServiceMap, TMethod extends keyof TServiceMap[TService]> extends Request {
|
|
32
|
+
data?: Parameters<TServiceMap[TService][TMethod]>[0];
|
|
33
|
+
method: TMethod;
|
|
34
|
+
service: TService;
|
|
35
|
+
}
|
|
3
36
|
export declare function abortRequest<TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap>(request: AnyRequest<TOutgoing, TPrivateOutgoing, TService>, err: Error): void;
|
|
37
|
+
export declare function isRequestDone<TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap>(request: AnyRequest<TOutgoing, TPrivateOutgoing, TService>): boolean;
|
|
4
38
|
export declare class RequestCollection<TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap> {
|
|
5
|
-
private readonly _requests;
|
|
6
39
|
private readonly _callbacks;
|
|
40
|
+
private readonly _requests;
|
|
7
41
|
constructor(requests: AnyRequest<TOutgoing, TPrivateOutgoing, TService> | AnyRequest<TOutgoing, TPrivateOutgoing, TService>[]);
|
|
42
|
+
isDone(): boolean;
|
|
8
43
|
get items(): ReadonlyArray<AnyRequest<TOutgoing, TPrivateOutgoing, TService>>;
|
|
9
44
|
listen(cb: () => void): void;
|
|
10
|
-
isDone(): boolean;
|
|
11
45
|
[Symbol.iterator](): {
|
|
12
46
|
next(): {
|
|
13
|
-
value: AnyRequest<TOutgoing, TPrivateOutgoing, TService>;
|
|
14
47
|
done: boolean;
|
|
48
|
+
value: AnyRequest<TOutgoing, TPrivateOutgoing, TService> | undefined;
|
|
15
49
|
} | {
|
|
16
50
|
done: boolean;
|
|
17
51
|
value?: undefined;
|
|
18
52
|
};
|
|
19
53
|
};
|
|
20
54
|
}
|
|
21
|
-
export type ServiceRequest<TServiceMap extends ServiceMap> = {
|
|
22
|
-
[TService in keyof TServiceMap]: {
|
|
23
|
-
[TMethod in keyof TServiceMap[TService]]: TransmitServiceRequest<TServiceMap, TService, TMethod> | InvokeServiceRequest<TServiceMap, TService, TMethod>;
|
|
24
|
-
}[keyof TServiceMap[TService]];
|
|
25
|
-
}[keyof TServiceMap];
|
|
26
|
-
export type MethodRequest<TMethodMap extends MethodMap> = {
|
|
27
|
-
[TMethod in keyof TMethodMap]: TransmitMethodRequest<TMethodMap, TMethod> | InvokeMethodRequest<TMethodMap, TMethod>;
|
|
28
|
-
}[keyof TMethodMap];
|
|
29
|
-
export interface Request {
|
|
30
|
-
promise: Promise<void>;
|
|
31
|
-
sentCallback?: (err?: Error) => void;
|
|
32
|
-
}
|
|
33
|
-
export interface TransmitServiceRequest<TServiceMap extends ServiceMap, TService extends keyof TServiceMap, TMethod extends keyof TServiceMap[TService]> extends Request {
|
|
34
|
-
service: TService;
|
|
35
|
-
method: TMethod;
|
|
36
|
-
data?: Parameters<TServiceMap[TService][TMethod]>[0];
|
|
37
|
-
}
|
|
38
|
-
export interface TransmitMethodRequest<TMethodMap extends MethodMap, TMethod extends keyof TMethodMap> extends Request {
|
|
39
|
-
method: TMethod;
|
|
40
|
-
data?: Parameters<TMethodMap[TMethod]>[0];
|
|
41
|
-
}
|
|
42
|
-
export interface InvokeServiceRequest<TServiceMap extends ServiceMap, TService extends keyof TServiceMap, TMethod extends keyof TServiceMap[TService]> extends TransmitServiceRequest<TServiceMap, TService, TMethod> {
|
|
43
|
-
cid: number;
|
|
44
|
-
ackTimeoutMs: number | false;
|
|
45
|
-
timeoutId?: NodeJS.Timeout;
|
|
46
|
-
callback: (err: Error, result?: TServiceMap[TService][TMethod]) => void | null;
|
|
47
|
-
}
|
|
48
|
-
export interface InvokeMethodRequest<TMethodMap extends MethodMap, TMethod extends keyof TMethodMap> extends TransmitMethodRequest<TMethodMap, TMethod> {
|
|
49
|
-
cid: number;
|
|
50
|
-
ackTimeoutMs: number | false;
|
|
51
|
-
timeoutId?: NodeJS.Timeout;
|
|
52
|
-
callback: (err: Error, result?: TMethodMap[TMethod]) => void | null;
|
|
53
|
-
}
|
|
54
|
-
export declare function isRequestDone<TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap>(request: AnyRequest<TOutgoing, TPrivateOutgoing, TService>): boolean;
|
package/dist/request.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { toArray } from
|
|
1
|
+
import { toArray } from './utils.js';
|
|
2
2
|
export function abortRequest(request, err) {
|
|
3
3
|
if (request.sentCallback) {
|
|
4
4
|
request.sentCallback(err);
|
|
@@ -7,11 +7,22 @@ export function abortRequest(request, err) {
|
|
|
7
7
|
request.callback(err);
|
|
8
8
|
}
|
|
9
9
|
}
|
|
10
|
+
export function isRequestDone(request) {
|
|
11
|
+
if ('callback' in request) {
|
|
12
|
+
return (request.callback === null);
|
|
13
|
+
}
|
|
14
|
+
return !request.sentCallback;
|
|
15
|
+
}
|
|
10
16
|
export class RequestCollection {
|
|
17
|
+
_callbacks;
|
|
18
|
+
_requests;
|
|
11
19
|
constructor(requests) {
|
|
12
20
|
this._requests = toArray(requests).filter(req => !isRequestDone(req));
|
|
13
21
|
this._callbacks = [];
|
|
14
22
|
}
|
|
23
|
+
isDone() {
|
|
24
|
+
return this._requests.length === 0;
|
|
25
|
+
}
|
|
15
26
|
get items() {
|
|
16
27
|
return this._requests;
|
|
17
28
|
}
|
|
@@ -29,9 +40,6 @@ export class RequestCollection {
|
|
|
29
40
|
});
|
|
30
41
|
}
|
|
31
42
|
}
|
|
32
|
-
isDone() {
|
|
33
|
-
return this._requests.length === 0;
|
|
34
|
-
}
|
|
35
43
|
[Symbol.iterator]() {
|
|
36
44
|
const values = this._requests;
|
|
37
45
|
let index = 0;
|
|
@@ -40,7 +48,7 @@ export class RequestCollection {
|
|
|
40
48
|
if (index < values.length) {
|
|
41
49
|
const val = values[index];
|
|
42
50
|
index++;
|
|
43
|
-
return {
|
|
51
|
+
return { done: false, value: val };
|
|
44
52
|
}
|
|
45
53
|
else
|
|
46
54
|
return { done: true };
|
|
@@ -48,9 +56,3 @@ export class RequestCollection {
|
|
|
48
56
|
};
|
|
49
57
|
}
|
|
50
58
|
}
|
|
51
|
-
export function isRequestDone(request) {
|
|
52
|
-
if ('callback' in request) {
|
|
53
|
-
return (request.callback === null);
|
|
54
|
-
}
|
|
55
|
-
return !request.sentCallback;
|
|
56
|
-
}
|
package/dist/response.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { MethodMap, PrivateMethodMap, PublicMethodMap, ServiceMap } from
|
|
2
|
-
export type AnyResponse<TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap> =
|
|
3
|
-
export
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
1
|
+
import { MethodMap, PrivateMethodMap, PublicMethodMap, ServiceMap } from './maps/method-map.js';
|
|
2
|
+
export type AnyResponse<TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap> = ErrorResponse | MethodDataResponse<TOutgoing> | MethodDataResponse<TPrivateOutgoing> | Response | ServiceDataResponse<TService>;
|
|
3
|
+
export interface DataResponse<T> extends Response {
|
|
4
|
+
data: T;
|
|
5
|
+
}
|
|
6
|
+
export interface ErrorResponse extends Response {
|
|
7
|
+
error: Error;
|
|
8
|
+
}
|
|
9
9
|
export type MethodDataResponse<TMethodMap extends MethodMap> = {
|
|
10
10
|
[TMethod in keyof TMethodMap]: DataResponse<ReturnType<TMethodMap[TMethod]>>;
|
|
11
11
|
}[keyof TMethodMap];
|
|
@@ -13,9 +13,9 @@ export interface Response {
|
|
|
13
13
|
rid: number;
|
|
14
14
|
timeoutAt?: Date;
|
|
15
15
|
}
|
|
16
|
-
export
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
16
|
+
export type ServiceDataResponse<TServiceMap extends ServiceMap> = {
|
|
17
|
+
[TService in keyof TServiceMap]: {
|
|
18
|
+
[TMethod in keyof TServiceMap[TService]]: DataResponse<ReturnType<TServiceMap[TService][TMethod]>>;
|
|
19
|
+
}[keyof TServiceMap[TService]];
|
|
20
|
+
}[keyof TServiceMap];
|
|
21
|
+
export declare function isResponsePacket<TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap>(packet?: unknown): packet is AnyResponse<TOutgoing, TPrivateOutgoing, TService>;
|
package/dist/response.js
CHANGED
package/dist/socket-event.d.ts
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
export type SocketEvent<TIncoming extends MethodMap, TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap> = AuthStateChangeEvent | RemoveAuthTokenEvent | AuthenticateEvent | BadAuthTokenEvent | CloseEvent | ConnectEvent | ConnectingEvent | DeauthenticateEvent | DisconnectEvent | ErrorEvent | MessageEvent | PingEvent | PongEvent | RequestEvent<TIncoming, TService> | ResponseEvent<TOutgoing, TPrivateOutgoing, TService>;
|
|
7
|
-
export type AuthStateChangeEvent = AuthenticatedChangeEvent | DeauthenticatedChangeEvent;
|
|
1
|
+
import { AuthToken, SignedAuthToken } from '@socket-mesh/auth';
|
|
2
|
+
import ws from 'isomorphic-ws';
|
|
3
|
+
import { MethodMap, PrivateMethodMap, PublicMethodMap, ServiceMap } from './maps/method-map.js';
|
|
4
|
+
import { MethodPacket, ServicePacket } from './packet.js';
|
|
5
|
+
import { AnyResponse } from './response.js';
|
|
8
6
|
export interface AuthenticatedChangeEvent {
|
|
7
|
+
authToken: AuthToken | null;
|
|
9
8
|
isAuthenticated: true;
|
|
10
|
-
wasAuthenticated: boolean;
|
|
11
9
|
signedAuthToken: SignedAuthToken;
|
|
12
|
-
|
|
10
|
+
wasAuthenticated: boolean;
|
|
13
11
|
}
|
|
14
12
|
export interface AuthenticateEvent {
|
|
15
|
-
|
|
13
|
+
authToken: AuthToken | null;
|
|
16
14
|
signedAuthToken: SignedAuthToken;
|
|
17
|
-
|
|
15
|
+
wasSigned: boolean;
|
|
18
16
|
}
|
|
17
|
+
export type AuthStateChangeEvent = AuthenticatedChangeEvent | DeauthenticatedChangeEvent;
|
|
19
18
|
export interface BadAuthTokenEvent {
|
|
20
19
|
error: Error;
|
|
21
20
|
signedAuthToken: SignedAuthToken;
|
|
@@ -25,21 +24,20 @@ export interface CloseEvent {
|
|
|
25
24
|
reason?: string;
|
|
26
25
|
}
|
|
27
26
|
export interface ConnectEvent {
|
|
28
|
-
|
|
27
|
+
authError?: Error;
|
|
28
|
+
id: null | string;
|
|
29
29
|
isAuthenticated: boolean;
|
|
30
30
|
pingTimeoutMs: number;
|
|
31
|
-
authError?: Error;
|
|
32
|
-
}
|
|
33
|
-
export interface ConnectingEvent {
|
|
34
|
-
}
|
|
35
|
-
export interface DeauthenticateEvent {
|
|
36
|
-
signedAuthToken: SignedAuthToken;
|
|
37
|
-
authToken: AuthToken;
|
|
38
31
|
}
|
|
32
|
+
export type ConnectingEvent = object;
|
|
39
33
|
export interface DeauthenticatedChangeEvent {
|
|
40
34
|
isAuthenticated: false;
|
|
41
35
|
wasAuthenticated: true;
|
|
42
36
|
}
|
|
37
|
+
export interface DeauthenticateEvent {
|
|
38
|
+
authToken: AuthToken | null;
|
|
39
|
+
signedAuthToken: SignedAuthToken;
|
|
40
|
+
}
|
|
43
41
|
export interface DisconnectEvent {
|
|
44
42
|
code: number;
|
|
45
43
|
reason?: string;
|
|
@@ -51,16 +49,15 @@ export interface MessageEvent {
|
|
|
51
49
|
data: string | ws.RawData;
|
|
52
50
|
isBinary: boolean;
|
|
53
51
|
}
|
|
54
|
-
export
|
|
55
|
-
|
|
56
|
-
export interface PongEvent {
|
|
57
|
-
}
|
|
52
|
+
export type PingEvent = object;
|
|
53
|
+
export type PongEvent = object;
|
|
58
54
|
export interface RemoveAuthTokenEvent {
|
|
59
55
|
oldAuthToken: SignedAuthToken;
|
|
60
56
|
}
|
|
61
57
|
export interface RequestEvent<TIncoming extends MethodMap, TService extends ServiceMap> {
|
|
62
|
-
request:
|
|
58
|
+
request: MethodPacket<TIncoming> | ServicePacket<TService>;
|
|
63
59
|
}
|
|
64
60
|
export interface ResponseEvent<TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap> {
|
|
65
61
|
response: AnyResponse<TOutgoing, TPrivateOutgoing, TService>;
|
|
66
62
|
}
|
|
63
|
+
export type SocketEvent<TIncoming extends MethodMap, TOutgoing extends PublicMethodMap, TPrivateOutgoing extends PrivateMethodMap, TService extends ServiceMap> = AuthenticateEvent | AuthStateChangeEvent | BadAuthTokenEvent | CloseEvent | ConnectEvent | ConnectingEvent | DeauthenticateEvent | DisconnectEvent | ErrorEvent | MessageEvent | PingEvent | PongEvent | RemoveAuthTokenEvent | RequestEvent<TIncoming, TService> | ResponseEvent<TOutgoing, TPrivateOutgoing, TService>;
|