UNPKG

3.89 kBTypeScriptView Raw
1import { Packr as MsgPack } from 'msgpackr';
2import { ReadBuffer, WriteBuffer } from './message-buffer';
3/**
4 * This code lets you encode rpc protocol messages (request/reply/notification/error/cancel)
5 * into a channel write buffer and decode the same messages from a read buffer.
6 * Custom encoders/decoders can be registered to specially handling certain types of values
7 * to be encoded. Clients are responsible for ensuring that the set of tags for encoders
8 * is distinct and the same at both ends of a channel.
9 */
10export declare type RpcMessage = RequestMessage | ReplyMessage | ReplyErrMessage | CancelMessage | NotificationMessage;
11export declare const enum RpcMessageType {
12 Request = 1,
13 Notification = 2,
14 Reply = 3,
15 ReplyErr = 4,
16 Cancel = 5
17}
18export interface CancelMessage {
19 type: RpcMessageType.Cancel;
20 id: number;
21}
22export interface RequestMessage {
23 type: RpcMessageType.Request;
24 id: number;
25 method: string;
26 args: any[];
27}
28export interface NotificationMessage {
29 type: RpcMessageType.Notification;
30 id: number;
31 method: string;
32 args: any[];
33}
34export interface ReplyMessage {
35 type: RpcMessageType.Reply;
36 id: number;
37 res: any;
38}
39export interface ReplyErrMessage {
40 type: RpcMessageType.ReplyErr;
41 id: number;
42 err: any;
43}
44export interface SerializedError {
45 readonly $isError: true;
46 readonly name: string;
47 readonly message: string;
48 readonly stack: string;
49}
50/**
51 * A special error that can be returned in case a request
52 * has failed. Provides additional information i.e. an error code
53 * and additional error data.
54 */
55export declare class ResponseError extends Error {
56 readonly code: number;
57 readonly data: any;
58 constructor(code: number, message: string, data: any);
59}
60/**
61 * Custom error thrown by the {@link RpcMessageEncoder} if an error occurred during the encoding and the
62 * object could not be written to the given {@link WriteBuffer}
63 */
64export declare class EncodingError extends Error {
65 cause?: Error | undefined;
66 constructor(msg: string, cause?: Error | undefined);
67}
68/**
69 * A `RpcMessageDecoder` parses a a binary message received via {@link ReadBuffer} into a {@link RpcMessage}
70 */
71export interface RpcMessageDecoder {
72 parse(buffer: ReadBuffer): RpcMessage;
73}
74/**
75 * A `RpcMessageEncoder` writes {@link RpcMessage} objects to a {@link WriteBuffer}. Note that it is
76 * up to clients to commit the message. This allows for multiple messages being
77 * encoded before sending.
78 */
79export interface RpcMessageEncoder {
80 cancel(buf: WriteBuffer, requestId: number): void;
81 notification(buf: WriteBuffer, requestId: number, method: string, args: any[]): void;
82 request(buf: WriteBuffer, requestId: number, method: string, args: any[]): void;
83 replyOK(buf: WriteBuffer, requestId: number, res: any): void;
84 replyErr(buf: WriteBuffer, requestId: number, err: any): void;
85}
86export declare const defaultMsgPack: MsgPack;
87export declare class MsgPackMessageEncoder implements RpcMessageEncoder {
88 protected readonly msgPack: MsgPack;
89 constructor(msgPack?: MsgPack);
90 cancel(buf: WriteBuffer, requestId: number): void;
91 notification(buf: WriteBuffer, requestId: number, method: string, args: any[]): void;
92 request(buf: WriteBuffer, requestId: number, method: string, args: any[]): void;
93 replyOK(buf: WriteBuffer, requestId: number, res: any): void;
94 replyErr(buf: WriteBuffer, requestId: number, err: any): void;
95 encode<T = unknown>(buf: WriteBuffer, value: T): void;
96}
97export declare class MsgPackMessageDecoder implements RpcMessageDecoder {
98 protected readonly msgPack: MsgPack;
99 constructor(msgPack?: MsgPack);
100 decode<T = any>(buf: ReadBuffer): T;
101 parse(buffer: ReadBuffer): RpcMessage;
102}
103export declare function registerMsgPackExtensions(): void;
104//# sourceMappingURL=rpc-message-encoder.d.ts.map
\No newline at end of file