This repository was archived by the owner on Jul 7, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessage.ts
131 lines (116 loc) · 2.97 KB
/
message.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* The message type, must be the integer zero (0) for "Request" messages.
*/
export type MessageTypeRequest = 0;
/**
* The message type, must be the integer one (1) for "Response" messages.
*/
export type MessageTypeResponse = 1;
/**
* The message type, must be the integer two (2) for "Notification" messages.
*/
export type MessageTypeNotification = 2;
/**
* A 32-bit unsigned integer number. This number is used as a sequence number.
* The server's response to the "Request" will have the same msgid.
*/
export type MessageId = number;
/**
* A string which represents the method name.
*/
export type MessageMethod = string;
/**
* An array of the function arguments. The elements of this array are arbitarary objects.
*/
export type MessageParams = unknown[];
/**
* If the method is executed correctly, this field is Nil.
* If the error occured at the server-side, then this field is an arbitrary object which
* represents the error.
*/
export type MessageError = unknown | null;
/**
* An arbitrary object, which represents the returned result of the function.
* If an error occured, this field should be nil.
*/
export type MessageResult = unknown | null;
/**
* Request message which is sent from client to server.
*/
export type RequestMessage = [
MessageTypeRequest,
MessageId,
MessageMethod,
MessageParams,
];
/**
* Response message which is sent from server to client as a response
*/
export type ResponseMessage = [
MessageTypeResponse,
MessageId,
MessageError,
MessageResult,
];
/**
* Notification message which is sent from client to server.
*/
export type NotificationMessage = [
MessageTypeNotification,
MessageMethod,
MessageParams,
];
/**
* Message used in MessagePack-RPC
*/
export type Message = RequestMessage | ResponseMessage | NotificationMessage;
/**
* Check if an arbitrary data is RequestMessage or not
*/
export function isRequestMessage(data: unknown): data is RequestMessage {
return (
Array.isArray(data) &&
data.length === 4 &&
data[0] === 0 &&
typeof data[1] === "number" &&
typeof data[2] === "string" &&
Array.isArray(data[3])
);
}
/**
* Check if an arbitrary data is ResponseMessage or not
*/
export function isResponseMessage(data: unknown): data is ResponseMessage {
return (
Array.isArray(data) &&
data.length === 4 &&
data[0] === 1 &&
typeof data[1] === "number" &&
typeof data[2] !== "undefined" &&
typeof data[3] !== "undefined"
);
}
/**
* Check if an arbitrary data is NotificationMessage or not
*/
export function isNotificationMessage(
data: unknown,
): data is NotificationMessage {
return (
Array.isArray(data) &&
data.length === 3 &&
data[0] === 2 &&
typeof data[1] === "string" &&
Array.isArray(data[2])
);
}
/**
* Check if an arbitrary data is Message or not
*/
export function isMessage(data: unknown): data is Message {
return (
isRequestMessage(data) ||
isResponseMessage(data) ||
isNotificationMessage(data)
);
}