@cloudpss/ubrpc 0.4.11 → 0.4.13
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/auth.d.ts +5 -5
- package/dist/auth.js +51 -51
- package/dist/client.d.ts +16 -16
- package/dist/client.js +47 -47
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -3
- package/dist/server.d.ts +44 -44
- package/dist/server.js +117 -118
- package/dist/server.js.map +1 -1
- package/dist/socket.d.ts +67 -67
- package/dist/socket.js +296 -296
- package/dist/socket.js.map +1 -1
- package/dist/types/payload.d.ts +99 -99
- package/dist/types/payload.js +1 -1
- package/dist/types/utils.d.ts +32 -32
- package/dist/types/utils.js +21 -21
- package/dist/utils/messaging.d.ts +7 -7
- package/dist/utils/messaging.js +8 -8
- package/dist/utils/serialize.d.ts +9 -9
- package/dist/utils/serialize.js +72 -72
- package/dist/utils/websocket.d.ts +4 -4
- package/dist/utils/websocket.js +20 -20
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +2 -2
- package/src/server.ts +8 -9
- package/tests/server.js +8 -3
package/dist/types/payload.d.ts
CHANGED
|
@@ -1,99 +1,99 @@
|
|
|
1
|
-
/** RPC 连接 ID */
|
|
2
|
-
export type ConnectionID = string & {
|
|
3
|
-
tag: 'ConnectionID';
|
|
4
|
-
};
|
|
5
|
-
/** 认证信息 */
|
|
6
|
-
export type RpcMetadata = Record<string, unknown>;
|
|
7
|
-
/** RPC 错误 */
|
|
8
|
-
export interface RpcErrorLike {
|
|
9
|
-
/** 名称 */
|
|
10
|
-
name: string;
|
|
11
|
-
/** 消息 */
|
|
12
|
-
message: string;
|
|
13
|
-
/** 调用 */
|
|
14
|
-
stack?: string;
|
|
15
|
-
/** 引发错误的其他错误 */
|
|
16
|
-
errors?: RpcErrorLike[];
|
|
17
|
-
}
|
|
18
|
-
/** RPC 调用的通用属性 */
|
|
19
|
-
interface RpcPayloadBase {
|
|
20
|
-
/** 调用类型 */
|
|
21
|
-
type: string;
|
|
22
|
-
/** 序列号 */
|
|
23
|
-
seq: number;
|
|
24
|
-
}
|
|
25
|
-
/** RPC 认证调用 */
|
|
26
|
-
export interface RpcAuthPayload extends RpcPayloadBase {
|
|
27
|
-
/** @inheritdoc */
|
|
28
|
-
type: 'auth';
|
|
29
|
-
/** 连接 ID */
|
|
30
|
-
id: ConnectionID;
|
|
31
|
-
/** 协议版本 */
|
|
32
|
-
version: number;
|
|
33
|
-
/** 认证信息 */
|
|
34
|
-
metadata: RpcMetadata;
|
|
35
|
-
/** 异常 */
|
|
36
|
-
error?: RpcErrorLike;
|
|
37
|
-
}
|
|
38
|
-
/** RPC 方法调用 */
|
|
39
|
-
export interface RpcCallPayload extends RpcPayloadBase {
|
|
40
|
-
/** @inheritdoc */
|
|
41
|
-
type: 'call';
|
|
42
|
-
/** 调用的方法 */
|
|
43
|
-
method: string;
|
|
44
|
-
/** 参数 */
|
|
45
|
-
args: unknown[];
|
|
46
|
-
}
|
|
47
|
-
/** RPC 方法返回 */
|
|
48
|
-
export interface RpcReturnPayload extends RpcPayloadBase {
|
|
49
|
-
/** @inheritdoc */
|
|
50
|
-
type: 'return';
|
|
51
|
-
/** 返回值 */
|
|
52
|
-
result?: unknown;
|
|
53
|
-
/** 异常 */
|
|
54
|
-
error?: RpcErrorLike;
|
|
55
|
-
}
|
|
56
|
-
/** RPC 通知 */
|
|
57
|
-
export interface RpcNotifyPayload extends RpcPayloadBase {
|
|
58
|
-
/** @inheritdoc */
|
|
59
|
-
type: 'notify';
|
|
60
|
-
/** 调用的方法 */
|
|
61
|
-
method: string;
|
|
62
|
-
/** 参数 */
|
|
63
|
-
args: unknown[];
|
|
64
|
-
}
|
|
65
|
-
/** RPC 订阅 */
|
|
66
|
-
export interface RpcSubscribePayload extends RpcPayloadBase {
|
|
67
|
-
/** @inheritdoc */
|
|
68
|
-
type: 'subscribe';
|
|
69
|
-
/** 调用的方法 */
|
|
70
|
-
method: string;
|
|
71
|
-
/** 参数 */
|
|
72
|
-
args: unknown[];
|
|
73
|
-
}
|
|
74
|
-
/** RPC 发布 */
|
|
75
|
-
export interface RpcPublishPayload extends RpcPayloadBase {
|
|
76
|
-
/** @inheritdoc */
|
|
77
|
-
type: 'publish';
|
|
78
|
-
/** 发布的值 */
|
|
79
|
-
next?: unknown;
|
|
80
|
-
/** 异常结束 */
|
|
81
|
-
error?: RpcErrorLike;
|
|
82
|
-
/** 正常结束 */
|
|
83
|
-
complete?: true;
|
|
84
|
-
}
|
|
85
|
-
/** RPC 取消订阅 */
|
|
86
|
-
export interface RpcUnsubscribePayload extends RpcPayloadBase {
|
|
87
|
-
/** @inheritdoc */
|
|
88
|
-
type: 'unsubscribe';
|
|
89
|
-
}
|
|
90
|
-
/** RPC 错误 */
|
|
91
|
-
export interface RpcErrorPayload extends RpcPayloadBase {
|
|
92
|
-
/** @inheritdoc */
|
|
93
|
-
type: 'error';
|
|
94
|
-
/** 异常 */
|
|
95
|
-
error: RpcErrorLike;
|
|
96
|
-
}
|
|
97
|
-
/** RPC 调用 */
|
|
98
|
-
export type RpcPayload = RpcCallPayload | RpcReturnPayload | RpcNotifyPayload | RpcPublishPayload | RpcSubscribePayload | RpcUnsubscribePayload | RpcAuthPayload | RpcErrorPayload;
|
|
99
|
-
export {};
|
|
1
|
+
/** RPC 连接 ID */
|
|
2
|
+
export type ConnectionID = string & {
|
|
3
|
+
tag: 'ConnectionID';
|
|
4
|
+
};
|
|
5
|
+
/** 认证信息 */
|
|
6
|
+
export type RpcMetadata = Record<string, unknown>;
|
|
7
|
+
/** RPC 错误 */
|
|
8
|
+
export interface RpcErrorLike {
|
|
9
|
+
/** 名称 */
|
|
10
|
+
name: string;
|
|
11
|
+
/** 消息 */
|
|
12
|
+
message: string;
|
|
13
|
+
/** 调用 */
|
|
14
|
+
stack?: string;
|
|
15
|
+
/** 引发错误的其他错误 */
|
|
16
|
+
errors?: RpcErrorLike[];
|
|
17
|
+
}
|
|
18
|
+
/** RPC 调用的通用属性 */
|
|
19
|
+
interface RpcPayloadBase {
|
|
20
|
+
/** 调用类型 */
|
|
21
|
+
type: string;
|
|
22
|
+
/** 序列号 */
|
|
23
|
+
seq: number;
|
|
24
|
+
}
|
|
25
|
+
/** RPC 认证调用 */
|
|
26
|
+
export interface RpcAuthPayload extends RpcPayloadBase {
|
|
27
|
+
/** @inheritdoc */
|
|
28
|
+
type: 'auth';
|
|
29
|
+
/** 连接 ID */
|
|
30
|
+
id: ConnectionID;
|
|
31
|
+
/** 协议版本 */
|
|
32
|
+
version: number;
|
|
33
|
+
/** 认证信息 */
|
|
34
|
+
metadata: RpcMetadata;
|
|
35
|
+
/** 异常 */
|
|
36
|
+
error?: RpcErrorLike;
|
|
37
|
+
}
|
|
38
|
+
/** RPC 方法调用 */
|
|
39
|
+
export interface RpcCallPayload extends RpcPayloadBase {
|
|
40
|
+
/** @inheritdoc */
|
|
41
|
+
type: 'call';
|
|
42
|
+
/** 调用的方法 */
|
|
43
|
+
method: string;
|
|
44
|
+
/** 参数 */
|
|
45
|
+
args: unknown[];
|
|
46
|
+
}
|
|
47
|
+
/** RPC 方法返回 */
|
|
48
|
+
export interface RpcReturnPayload extends RpcPayloadBase {
|
|
49
|
+
/** @inheritdoc */
|
|
50
|
+
type: 'return';
|
|
51
|
+
/** 返回值 */
|
|
52
|
+
result?: unknown;
|
|
53
|
+
/** 异常 */
|
|
54
|
+
error?: RpcErrorLike;
|
|
55
|
+
}
|
|
56
|
+
/** RPC 通知 */
|
|
57
|
+
export interface RpcNotifyPayload extends RpcPayloadBase {
|
|
58
|
+
/** @inheritdoc */
|
|
59
|
+
type: 'notify';
|
|
60
|
+
/** 调用的方法 */
|
|
61
|
+
method: string;
|
|
62
|
+
/** 参数 */
|
|
63
|
+
args: unknown[];
|
|
64
|
+
}
|
|
65
|
+
/** RPC 订阅 */
|
|
66
|
+
export interface RpcSubscribePayload extends RpcPayloadBase {
|
|
67
|
+
/** @inheritdoc */
|
|
68
|
+
type: 'subscribe';
|
|
69
|
+
/** 调用的方法 */
|
|
70
|
+
method: string;
|
|
71
|
+
/** 参数 */
|
|
72
|
+
args: unknown[];
|
|
73
|
+
}
|
|
74
|
+
/** RPC 发布 */
|
|
75
|
+
export interface RpcPublishPayload extends RpcPayloadBase {
|
|
76
|
+
/** @inheritdoc */
|
|
77
|
+
type: 'publish';
|
|
78
|
+
/** 发布的值 */
|
|
79
|
+
next?: unknown;
|
|
80
|
+
/** 异常结束 */
|
|
81
|
+
error?: RpcErrorLike;
|
|
82
|
+
/** 正常结束 */
|
|
83
|
+
complete?: true;
|
|
84
|
+
}
|
|
85
|
+
/** RPC 取消订阅 */
|
|
86
|
+
export interface RpcUnsubscribePayload extends RpcPayloadBase {
|
|
87
|
+
/** @inheritdoc */
|
|
88
|
+
type: 'unsubscribe';
|
|
89
|
+
}
|
|
90
|
+
/** RPC 错误 */
|
|
91
|
+
export interface RpcErrorPayload extends RpcPayloadBase {
|
|
92
|
+
/** @inheritdoc */
|
|
93
|
+
type: 'error';
|
|
94
|
+
/** 异常 */
|
|
95
|
+
error: RpcErrorLike;
|
|
96
|
+
}
|
|
97
|
+
/** RPC 调用 */
|
|
98
|
+
export type RpcPayload = RpcCallPayload | RpcReturnPayload | RpcNotifyPayload | RpcPublishPayload | RpcSubscribePayload | RpcUnsubscribePayload | RpcAuthPayload | RpcErrorPayload;
|
|
99
|
+
export {};
|
package/dist/types/payload.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {};
|
|
1
|
+
export {};
|
|
2
2
|
//# sourceMappingURL=payload.js.map
|
package/dist/types/utils.d.ts
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
import type { InteropObservable, Observable } from 'rxjs';
|
|
2
|
-
/** 可转换为 Observable 的类型 */
|
|
3
|
-
export type ObservableLike<T> = Observable<T> | InteropObservable<T> | AsyncIterable<T>;
|
|
4
|
-
/** Returns a boolean for whether the two given types are equal. */
|
|
5
|
-
type IsEqual<A, B> = (<G>() => G extends A ? 1 : 2) extends <G>() => G extends B ? 1 : 2 ? true : false;
|
|
6
|
-
/** 作为通知输出的方法 */
|
|
7
|
-
type IsNotificationReturn<R> = IsEqual<R, void> extends true ? true : IsEqual<R, undefined> extends true ? true : R extends PromiseLike<void> | void ? true : false;
|
|
8
|
-
/** 作为 Observable 输出的方法 */
|
|
9
|
-
type ObservableReturn = ObservableLike<any> | PromiseLike<ObservableLike<any>>;
|
|
10
|
-
/** 提取类型中的方法 */
|
|
11
|
-
export type Methods<T> = {
|
|
12
|
-
[P in keyof T]: T[P] extends (...args: any[]) => infer R ? (R extends ObservableReturn ? never : P) : never;
|
|
13
|
-
}[keyof T] & string;
|
|
14
|
-
/** 提取类型中的通知 */
|
|
15
|
-
export type Notifications<T> = {
|
|
16
|
-
[P in keyof T]: T[P] extends (...args: any[]) => infer R ? IsNotificationReturn<R> extends true ? P : never : never;
|
|
17
|
-
}[keyof T] & string;
|
|
18
|
-
/** 提取类型中的订阅主题 */
|
|
19
|
-
export type Subjects<T> = {
|
|
20
|
-
[P in keyof T]: T[P] extends (...args: any[]) => ObservableReturn ? P : never;
|
|
21
|
-
}[keyof T] & string;
|
|
22
|
-
/** RPC 调用的参数 */
|
|
23
|
-
export type RpcParameters<T> = T extends (...args: infer R) => any ? R : never;
|
|
24
|
-
/** RPC 调用的返回 */
|
|
25
|
-
export type RpcReturns<T> = T extends (...args: any[]) => infer R ? Awaited<R> extends ObservableLike<infer U> ? U : Awaited<R> : never;
|
|
26
|
-
/** RPC 调用函数的实现 */
|
|
27
|
-
type RpcField<T> = T extends (...args: infer P) => infer R ? Awaited<R> extends ObservableReturn ? (...args: P) => ObservableLike<RpcReturns<T>> | PromiseLike<ObservableLike<RpcReturns<T>>> : (...args: P) => RpcReturns<T> | PromiseLike<RpcReturns<T>> : never;
|
|
28
|
-
/** RPC 调用对象的实现 */
|
|
29
|
-
export type RpcObject<T> = {
|
|
30
|
-
[K in Methods<T> | Subjects<T>]: RpcField<T[K]>;
|
|
31
|
-
};
|
|
32
|
-
export {};
|
|
1
|
+
import type { InteropObservable, Observable } from 'rxjs';
|
|
2
|
+
/** 可转换为 Observable 的类型 */
|
|
3
|
+
export type ObservableLike<T> = Observable<T> | InteropObservable<T> | AsyncIterable<T>;
|
|
4
|
+
/** Returns a boolean for whether the two given types are equal. */
|
|
5
|
+
type IsEqual<A, B> = (<G>() => G extends A ? 1 : 2) extends <G>() => G extends B ? 1 : 2 ? true : false;
|
|
6
|
+
/** 作为通知输出的方法 */
|
|
7
|
+
type IsNotificationReturn<R> = IsEqual<R, void> extends true ? true : IsEqual<R, undefined> extends true ? true : R extends PromiseLike<void> | void ? true : false;
|
|
8
|
+
/** 作为 Observable 输出的方法 */
|
|
9
|
+
type ObservableReturn = ObservableLike<any> | PromiseLike<ObservableLike<any>>;
|
|
10
|
+
/** 提取类型中的方法 */
|
|
11
|
+
export type Methods<T> = {
|
|
12
|
+
[P in keyof T]: T[P] extends (...args: any[]) => infer R ? (R extends ObservableReturn ? never : P) : never;
|
|
13
|
+
}[keyof T] & string;
|
|
14
|
+
/** 提取类型中的通知 */
|
|
15
|
+
export type Notifications<T> = {
|
|
16
|
+
[P in keyof T]: T[P] extends (...args: any[]) => infer R ? IsNotificationReturn<R> extends true ? P : never : never;
|
|
17
|
+
}[keyof T] & string;
|
|
18
|
+
/** 提取类型中的订阅主题 */
|
|
19
|
+
export type Subjects<T> = {
|
|
20
|
+
[P in keyof T]: T[P] extends (...args: any[]) => ObservableReturn ? P : never;
|
|
21
|
+
}[keyof T] & string;
|
|
22
|
+
/** RPC 调用的参数 */
|
|
23
|
+
export type RpcParameters<T> = T extends (...args: infer R) => any ? R : never;
|
|
24
|
+
/** RPC 调用的返回 */
|
|
25
|
+
export type RpcReturns<T> = T extends (...args: any[]) => infer R ? Awaited<R> extends ObservableLike<infer U> ? U : Awaited<R> : never;
|
|
26
|
+
/** RPC 调用函数的实现 */
|
|
27
|
+
type RpcField<T> = T extends (...args: infer P) => infer R ? Awaited<R> extends ObservableReturn ? (...args: P) => ObservableLike<RpcReturns<T>> | PromiseLike<ObservableLike<RpcReturns<T>>> : (...args: P) => RpcReturns<T> | PromiseLike<RpcReturns<T>> : never;
|
|
28
|
+
/** RPC 调用对象的实现 */
|
|
29
|
+
export type RpcObject<T> = {
|
|
30
|
+
[K in Methods<T> | Subjects<T>]: RpcField<T[K]>;
|
|
31
|
+
};
|
|
32
|
+
export {};
|
package/dist/types/utils.js
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
export {};
|
|
2
|
-
// interface A {
|
|
3
|
-
// field: number;
|
|
4
|
-
// methodA(x: number): number | undefined;
|
|
5
|
-
// methodB: (x: string, y: number) => Promise<number>;
|
|
6
|
-
// readonly methodC: () => Iterable<number>;
|
|
7
|
-
// readonly methodD: () => void | Promise<null | undefined>;
|
|
8
|
-
// readonly notificationA: () => undefined;
|
|
9
|
-
// readonly notificationB: () => Promise<void>;
|
|
10
|
-
// notificationC(): void;
|
|
11
|
-
// readonly notificationD: () => void | PromiseLike<undefined>;
|
|
12
|
-
// readonly notificationE: () => void | Promise<void>;
|
|
13
|
-
// subjectA(x: number): Observable<number>;
|
|
14
|
-
// subjectB: (x: string) => Promise<Observable<number>>;
|
|
15
|
-
// subjectC: () => PromiseLike<InteropObservable<number>>;
|
|
16
|
-
// subjectD: () => AsyncGenerator<number>;
|
|
17
|
-
// subjectE: () => PromiseLike<AsyncGenerator<number>>;
|
|
18
|
-
// }
|
|
19
|
-
// type X = Methods<A>;
|
|
20
|
-
// type Y = Subjects<A>;
|
|
21
|
-
// type Z = Notifications<A>;
|
|
1
|
+
export {};
|
|
2
|
+
// interface A {
|
|
3
|
+
// field: number;
|
|
4
|
+
// methodA(x: number): number | undefined;
|
|
5
|
+
// methodB: (x: string, y: number) => Promise<number>;
|
|
6
|
+
// readonly methodC: () => Iterable<number>;
|
|
7
|
+
// readonly methodD: () => void | Promise<null | undefined>;
|
|
8
|
+
// readonly notificationA: () => undefined;
|
|
9
|
+
// readonly notificationB: () => Promise<void>;
|
|
10
|
+
// notificationC(): void;
|
|
11
|
+
// readonly notificationD: () => void | PromiseLike<undefined>;
|
|
12
|
+
// readonly notificationE: () => void | Promise<void>;
|
|
13
|
+
// subjectA(x: number): Observable<number>;
|
|
14
|
+
// subjectB: (x: string) => Promise<Observable<number>>;
|
|
15
|
+
// subjectC: () => PromiseLike<InteropObservable<number>>;
|
|
16
|
+
// subjectD: () => AsyncGenerator<number>;
|
|
17
|
+
// subjectE: () => PromiseLike<AsyncGenerator<number>>;
|
|
18
|
+
// }
|
|
19
|
+
// type X = Methods<A>;
|
|
20
|
+
// type Y = Subjects<A>;
|
|
21
|
+
// type Z = Notifications<A>;
|
|
22
22
|
//# sourceMappingURL=utils.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
/// <reference types="ws" />
|
|
2
|
-
import type WebSocket from 'isomorphic-ws';
|
|
3
|
-
import type { RpcPayload } from '../types/payload.js';
|
|
4
|
-
/** 发送消息 */
|
|
5
|
-
export declare function send<T extends RpcPayload['type']>(socket: WebSocket, type: T, info: Omit<RpcPayload & {
|
|
6
|
-
type: T;
|
|
7
|
-
}, 'type'>): void;
|
|
1
|
+
/// <reference types="ws" />
|
|
2
|
+
import type WebSocket from 'isomorphic-ws';
|
|
3
|
+
import type { RpcPayload } from '../types/payload.js';
|
|
4
|
+
/** 发送消息 */
|
|
5
|
+
export declare function send<T extends RpcPayload['type']>(socket: WebSocket, type: T, info: Omit<RpcPayload & {
|
|
6
|
+
type: T;
|
|
7
|
+
}, 'type'>): void;
|
package/dist/utils/messaging.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { encode } from '@cloudpss/ubjson';
|
|
2
|
-
/** 发送消息 */
|
|
3
|
-
export function send(socket, type, info) {
|
|
4
|
-
socket.send(encode({
|
|
5
|
-
...info,
|
|
6
|
-
type,
|
|
7
|
-
}));
|
|
8
|
-
}
|
|
1
|
+
import { encode } from '@cloudpss/ubjson';
|
|
2
|
+
/** 发送消息 */
|
|
3
|
+
export function send(socket, type, info) {
|
|
4
|
+
socket.send(encode({
|
|
5
|
+
...info,
|
|
6
|
+
type,
|
|
7
|
+
}));
|
|
8
|
+
}
|
|
9
9
|
//# sourceMappingURL=messaging.js.map
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
/// <reference types="ws" />
|
|
2
|
-
import type WebSocket from 'isomorphic-ws';
|
|
3
|
-
import type { RpcErrorLike, RpcPayload } from '../types/payload.js';
|
|
4
|
-
/** 反序列化 */
|
|
5
|
-
export declare function decodePayload(data: WebSocket.Data): RpcPayload | undefined;
|
|
6
|
-
/** 序列化错误信息 */
|
|
7
|
-
export declare function serializeError(error: unknown): RpcErrorLike;
|
|
8
|
-
/** 反序列化错误信息 */
|
|
9
|
-
export declare function deserializeError(error: RpcErrorLike): Error;
|
|
1
|
+
/// <reference types="ws" />
|
|
2
|
+
import type WebSocket from 'isomorphic-ws';
|
|
3
|
+
import type { RpcErrorLike, RpcPayload } from '../types/payload.js';
|
|
4
|
+
/** 反序列化 */
|
|
5
|
+
export declare function decodePayload(data: WebSocket.Data): RpcPayload | undefined;
|
|
6
|
+
/** 序列化错误信息 */
|
|
7
|
+
export declare function serializeError(error: unknown): RpcErrorLike;
|
|
8
|
+
/** 反序列化错误信息 */
|
|
9
|
+
export declare function deserializeError(error: RpcErrorLike): Error;
|
package/dist/utils/serialize.js
CHANGED
|
@@ -1,73 +1,73 @@
|
|
|
1
|
-
import { decode } from '@cloudpss/ubjson';
|
|
2
|
-
/** 反序列化 */
|
|
3
|
-
export function decodePayload(data) {
|
|
4
|
-
if (!data || typeof data == 'string')
|
|
5
|
-
return undefined;
|
|
6
|
-
if (Array.isArray(data))
|
|
7
|
-
data = Buffer.concat(data);
|
|
8
|
-
try {
|
|
9
|
-
const payload = decode(data);
|
|
10
|
-
if (!payload || typeof payload != 'object')
|
|
11
|
-
return undefined;
|
|
12
|
-
if (typeof payload.type !== 'string')
|
|
13
|
-
return undefined;
|
|
14
|
-
if (typeof payload.seq !== 'number')
|
|
15
|
-
return undefined;
|
|
16
|
-
return payload;
|
|
17
|
-
}
|
|
18
|
-
catch (ex) {
|
|
19
|
-
return undefined;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
const serializing = Symbol('serializing error');
|
|
23
|
-
/** 序列化错误信息 */
|
|
24
|
-
export function serializeError(error) {
|
|
25
|
-
if (error == null)
|
|
26
|
-
return {
|
|
27
|
-
name: 'Error',
|
|
28
|
-
message: '',
|
|
29
|
-
};
|
|
30
|
-
if (typeof error != 'object')
|
|
31
|
-
return {
|
|
32
|
-
name: 'Error',
|
|
33
|
-
message: String(error),
|
|
34
|
-
};
|
|
35
|
-
try {
|
|
36
|
-
const reenter = !!error[serializing];
|
|
37
|
-
error[serializing] = true;
|
|
38
|
-
const { message, stack, name, errors, ...rest } = error;
|
|
39
|
-
return {
|
|
40
|
-
...rest,
|
|
41
|
-
name: String(name ?? 'Error'),
|
|
42
|
-
message: String(message ?? ''),
|
|
43
|
-
stack: stack,
|
|
44
|
-
errors: reenter ? undefined : Array.isArray(errors) ? errors.map(serializeError) : errors,
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
finally {
|
|
48
|
-
delete error[serializing];
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
/** 反序列化错误信息 */
|
|
52
|
-
export function deserializeError(error) {
|
|
53
|
-
if (error == null || typeof error != 'object')
|
|
54
|
-
return new Error(error ?? '');
|
|
55
|
-
const { message, stack, name, errors, ...rest } = error;
|
|
56
|
-
let ex;
|
|
57
|
-
if (Array.isArray(errors)) {
|
|
58
|
-
ex = new AggregateError(errors.map(deserializeError), message);
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
ex = new Error(message);
|
|
62
|
-
}
|
|
63
|
-
if (name !== ex?.name)
|
|
64
|
-
Object.defineProperty(ex, 'name', { value: String(name), configurable: true, writable: true });
|
|
65
|
-
Object.defineProperty(ex, 'stack', {
|
|
66
|
-
value: stack ? String(stack) : undefined,
|
|
67
|
-
configurable: true,
|
|
68
|
-
writable: true,
|
|
69
|
-
});
|
|
70
|
-
Object.assign(ex, rest);
|
|
71
|
-
return ex;
|
|
72
|
-
}
|
|
1
|
+
import { decode } from '@cloudpss/ubjson';
|
|
2
|
+
/** 反序列化 */
|
|
3
|
+
export function decodePayload(data) {
|
|
4
|
+
if (!data || typeof data == 'string')
|
|
5
|
+
return undefined;
|
|
6
|
+
if (Array.isArray(data))
|
|
7
|
+
data = Buffer.concat(data);
|
|
8
|
+
try {
|
|
9
|
+
const payload = decode(data);
|
|
10
|
+
if (!payload || typeof payload != 'object')
|
|
11
|
+
return undefined;
|
|
12
|
+
if (typeof payload.type !== 'string')
|
|
13
|
+
return undefined;
|
|
14
|
+
if (typeof payload.seq !== 'number')
|
|
15
|
+
return undefined;
|
|
16
|
+
return payload;
|
|
17
|
+
}
|
|
18
|
+
catch (ex) {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const serializing = Symbol('serializing error');
|
|
23
|
+
/** 序列化错误信息 */
|
|
24
|
+
export function serializeError(error) {
|
|
25
|
+
if (error == null)
|
|
26
|
+
return {
|
|
27
|
+
name: 'Error',
|
|
28
|
+
message: '',
|
|
29
|
+
};
|
|
30
|
+
if (typeof error != 'object')
|
|
31
|
+
return {
|
|
32
|
+
name: 'Error',
|
|
33
|
+
message: String(error),
|
|
34
|
+
};
|
|
35
|
+
try {
|
|
36
|
+
const reenter = !!error[serializing];
|
|
37
|
+
error[serializing] = true;
|
|
38
|
+
const { message, stack, name, errors, ...rest } = error;
|
|
39
|
+
return {
|
|
40
|
+
...rest,
|
|
41
|
+
name: String(name ?? 'Error'),
|
|
42
|
+
message: String(message ?? ''),
|
|
43
|
+
stack: stack,
|
|
44
|
+
errors: reenter ? undefined : Array.isArray(errors) ? errors.map(serializeError) : errors,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
delete error[serializing];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** 反序列化错误信息 */
|
|
52
|
+
export function deserializeError(error) {
|
|
53
|
+
if (error == null || typeof error != 'object')
|
|
54
|
+
return new Error(error ?? '');
|
|
55
|
+
const { message, stack, name, errors, ...rest } = error;
|
|
56
|
+
let ex;
|
|
57
|
+
if (Array.isArray(errors)) {
|
|
58
|
+
ex = new AggregateError(errors.map(deserializeError), message);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
ex = new Error(message);
|
|
62
|
+
}
|
|
63
|
+
if (name !== ex?.name)
|
|
64
|
+
Object.defineProperty(ex, 'name', { value: String(name), configurable: true, writable: true });
|
|
65
|
+
Object.defineProperty(ex, 'stack', {
|
|
66
|
+
value: stack ? String(stack) : undefined,
|
|
67
|
+
configurable: true,
|
|
68
|
+
writable: true,
|
|
69
|
+
});
|
|
70
|
+
Object.assign(ex, rest);
|
|
71
|
+
return ex;
|
|
72
|
+
}
|
|
73
73
|
//# sourceMappingURL=serialize.js.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/// <reference types="ws" />
|
|
2
|
-
import type WebSocket from 'isomorphic-ws';
|
|
3
|
-
/** 等待连接建立 */
|
|
4
|
-
export declare function ready(socket: WebSocket): Promise<void>;
|
|
1
|
+
/// <reference types="ws" />
|
|
2
|
+
import type WebSocket from 'isomorphic-ws';
|
|
3
|
+
/** 等待连接建立 */
|
|
4
|
+
export declare function ready(socket: WebSocket): Promise<void>;
|
package/dist/utils/websocket.js
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
/** 等待连接建立 */
|
|
2
|
-
export function ready(socket) {
|
|
3
|
-
switch (socket.readyState) {
|
|
4
|
-
case socket.CONNECTING:
|
|
5
|
-
return new Promise((resolve, reject) => {
|
|
6
|
-
socket.addEventListener('open', () => resolve(), { once: true });
|
|
7
|
-
socket.addEventListener('error', (ev) => reject(ev.error ?? new Error(`Failed to connect`)), {
|
|
8
|
-
once: true,
|
|
9
|
-
});
|
|
10
|
-
});
|
|
11
|
-
case socket.OPEN:
|
|
12
|
-
return Promise.resolve();
|
|
13
|
-
case socket.CLOSING:
|
|
14
|
-
return Promise.reject(new Error(`Socket is closing.`));
|
|
15
|
-
case socket.CLOSED:
|
|
16
|
-
return Promise.reject(new Error(`Socket is closed.`));
|
|
17
|
-
default:
|
|
18
|
-
return Promise.reject(new Error(`Socket is in unknown state.`));
|
|
19
|
-
}
|
|
20
|
-
}
|
|
1
|
+
/** 等待连接建立 */
|
|
2
|
+
export function ready(socket) {
|
|
3
|
+
switch (socket.readyState) {
|
|
4
|
+
case socket.CONNECTING:
|
|
5
|
+
return new Promise((resolve, reject) => {
|
|
6
|
+
socket.addEventListener('open', () => resolve(), { once: true });
|
|
7
|
+
socket.addEventListener('error', (ev) => reject(ev.error ?? new Error(`Failed to connect`)), {
|
|
8
|
+
once: true,
|
|
9
|
+
});
|
|
10
|
+
});
|
|
11
|
+
case socket.OPEN:
|
|
12
|
+
return Promise.resolve();
|
|
13
|
+
case socket.CLOSING:
|
|
14
|
+
return Promise.reject(new Error(`Socket is closing.`));
|
|
15
|
+
case socket.CLOSED:
|
|
16
|
+
return Promise.reject(new Error(`Socket is closed.`));
|
|
17
|
+
default:
|
|
18
|
+
return Promise.reject(new Error(`Socket is in unknown state.`));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
21
|
//# sourceMappingURL=websocket.js.map
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const VERSION = 1;
|
|
1
|
+
export declare const VERSION = 1;
|
package/dist/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export const VERSION = 1;
|
|
1
|
+
export const VERSION = 1;
|
|
2
2
|
//# sourceMappingURL=version.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudpss/ubrpc",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.13",
|
|
4
4
|
"author": "CloudPSS",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@cloudpss/ubjson": "~0.4.
|
|
31
|
+
"@cloudpss/ubjson": "~0.4.13",
|
|
32
32
|
"@types/ws": "^8.5.4",
|
|
33
33
|
"isomorphic-ws": "^5.0.0",
|
|
34
34
|
"rxjs": "^7.8.0",
|
package/src/server.ts
CHANGED
|
@@ -39,20 +39,19 @@ export class RpcServer<TRemote extends {}, TLocal extends {}> {
|
|
|
39
39
|
const s = socket as WebSocketWithMetadata;
|
|
40
40
|
const [id, metadata] = await this.authSocket(socket);
|
|
41
41
|
s[kMetadata] = metadata;
|
|
42
|
-
|
|
43
|
-
if (!
|
|
44
|
-
|
|
42
|
+
let client = this.sockets.get(id);
|
|
43
|
+
if (!client) {
|
|
44
|
+
client = new RpcServerSocket(id, this);
|
|
45
45
|
this.sockets.set(id, client);
|
|
46
|
-
return client;
|
|
47
46
|
} else {
|
|
48
47
|
const tid = this.disconnectingSockets.get(id);
|
|
49
48
|
if (tid != null) {
|
|
50
49
|
this.disconnectingSockets.delete(id);
|
|
51
50
|
clearTimeout(tid);
|
|
52
51
|
}
|
|
53
|
-
exist[kReplaceSocket](s);
|
|
54
|
-
return exist;
|
|
55
52
|
}
|
|
53
|
+
await client[kReplaceSocket](s);
|
|
54
|
+
return client;
|
|
56
55
|
}
|
|
57
56
|
/** 认证客户端 */
|
|
58
57
|
protected async authSocket(socket: WebSocket): Promise<[ConnectionID, RpcMetadata]> {
|
|
@@ -102,10 +101,9 @@ export class RpcServer<TRemote extends {}, TLocal extends {}> {
|
|
|
102
101
|
|
|
103
102
|
/** 由 WS Server 建立的 RPC 连接 */
|
|
104
103
|
export class RpcServerSocket<TRemote extends object, TLocal extends object> extends RpcSocket<TRemote, TLocal> {
|
|
105
|
-
constructor(id: ConnectionID, readonly server: RpcServer<TRemote, TLocal
|
|
104
|
+
constructor(id: ConnectionID, readonly server: RpcServer<TRemote, TLocal>) {
|
|
106
105
|
super(id);
|
|
107
106
|
this.seq = 1;
|
|
108
|
-
this.socket = socket;
|
|
109
107
|
}
|
|
110
108
|
/** @inheritdoc */
|
|
111
109
|
protected authSocket(): Promise<RpcMetadata> {
|
|
@@ -121,7 +119,8 @@ export class RpcServerSocket<TRemote extends object, TLocal extends object> exte
|
|
|
121
119
|
this.server[kOnClose](this, ev);
|
|
122
120
|
}
|
|
123
121
|
/** 替换 socket */
|
|
124
|
-
[kReplaceSocket](newSocket: WebSocketWithMetadata): void {
|
|
122
|
+
[kReplaceSocket](newSocket: WebSocketWithMetadata): Promise<void> {
|
|
125
123
|
this.socket = newSocket;
|
|
124
|
+
return this.ready;
|
|
126
125
|
}
|
|
127
126
|
}
|