UNPKG

2.19 kBTypeScriptView Raw
1import RAL from './ral';
2import { Message } from './messages';
3import { Event } from './events';
4import { ContentEncoder, ContentTypeEncoder } from './encoding';
5/**
6 * Writes JSON-RPC messages to an underlying transport.
7 */
8export interface MessageWriter {
9 /**
10 * Raised whenever an error occurs while writing a message.
11 */
12 readonly onError: Event<[Error, Message | undefined, number | undefined]>;
13 /**
14 * An event raised when the underlying transport has closed and writing is no longer possible.
15 */
16 readonly onClose: Event<void>;
17 /**
18 * Sends a JSON-RPC message.
19 * @param msg The JSON-RPC message to be sent.
20 * @description Implementations should guarantee messages are transmitted in the same order that they are received by this method.
21 */
22 write(msg: Message): Promise<void>;
23 /**
24 * Call when the connection using this message writer ends
25 * (e.g. MessageConnection.end() is called)
26 */
27 end(): void;
28 /** Releases resources incurred from writing or raising events. Does NOT close the underlying transport, if any. */
29 dispose(): void;
30}
31export declare namespace MessageWriter {
32 function is(value: any): value is MessageWriter;
33}
34export declare abstract class AbstractMessageWriter {
35 private errorEmitter;
36 private closeEmitter;
37 constructor();
38 dispose(): void;
39 get onError(): Event<[Error, Message | undefined, number | undefined]>;
40 protected fireError(error: any, message?: Message, count?: number): void;
41 get onClose(): Event<void>;
42 protected fireClose(): void;
43 private asError;
44}
45export interface MessageWriterOptions {
46 charset?: RAL.MessageBufferEncoding;
47 contentEncoder?: ContentEncoder;
48 contentTypeEncoder?: ContentTypeEncoder;
49}
50export declare class WriteableStreamMessageWriter extends AbstractMessageWriter implements MessageWriter {
51 private writable;
52 private options;
53 private errorCount;
54 private writeSemaphore;
55 constructor(writable: RAL.WritableStream, options?: RAL.MessageBufferEncoding | MessageWriterOptions);
56 write(msg: Message): Promise<void>;
57 private doWrite;
58 private handleError;
59 end(): void;
60}