UNPKG

3 kBTypeScriptView Raw
1import RAL from './ral';
2import { Event } from './events';
3import { Message } from './messages';
4import { ContentDecoder, ContentTypeDecoder } from './encoding';
5import { Disposable } from './api';
6/**
7 * A callback that receives each incoming JSON-RPC message.
8 */
9export interface DataCallback {
10 (data: Message): void;
11}
12export interface PartialMessageInfo {
13 readonly messageToken: number;
14 readonly waitingTime: number;
15}
16/** Reads JSON-RPC messages from some underlying transport. */
17export interface MessageReader {
18 /** Raised whenever an error occurs while reading a message. */
19 readonly onError: Event<Error>;
20 /** An event raised when the end of the underlying transport has been reached. */
21 readonly onClose: Event<void>;
22 /**
23 * An event that *may* be raised to inform the owner that only part of a message has been received.
24 * A MessageReader implementation may choose to raise this event after a timeout elapses while waiting for more of a partially received message to be received.
25 */
26 readonly onPartialMessage: Event<PartialMessageInfo>;
27 /**
28 * Begins listening for incoming messages. To be called at most once.
29 * @param callback A callback for receiving decoded messages.
30 */
31 listen(callback: DataCallback): Disposable;
32 /** Releases resources incurred from reading or raising events. Does NOT close the underlying transport, if any. */
33 dispose(): void;
34}
35export declare namespace MessageReader {
36 function is(value: any): value is MessageReader;
37}
38export declare abstract class AbstractMessageReader implements MessageReader {
39 private errorEmitter;
40 private closeEmitter;
41 private partialMessageEmitter;
42 constructor();
43 dispose(): void;
44 get onError(): Event<Error>;
45 protected fireError(error: any): void;
46 get onClose(): Event<void>;
47 protected fireClose(): void;
48 get onPartialMessage(): Event<PartialMessageInfo>;
49 protected firePartialMessage(info: PartialMessageInfo): void;
50 private asError;
51 abstract listen(callback: DataCallback): Disposable;
52}
53export interface MessageReaderOptions {
54 charset?: RAL.MessageBufferEncoding;
55 contentDecoder?: ContentDecoder;
56 contentDecoders?: ContentDecoder[];
57 contentTypeDecoder?: ContentTypeDecoder;
58 contentTypeDecoders?: ContentTypeDecoder[];
59}
60export declare class ReadableStreamMessageReader extends AbstractMessageReader {
61 private readable;
62 private options;
63 private callback;
64 private nextMessageLength;
65 private messageToken;
66 private buffer;
67 private partialMessageTimer;
68 private _partialMessageTimeout;
69 private readSemaphore;
70 constructor(readable: RAL.ReadableStream, options?: RAL.MessageBufferEncoding | MessageReaderOptions);
71 set partialMessageTimeout(timeout: number);
72 get partialMessageTimeout(): number;
73 listen(callback: DataCallback): Disposable;
74 private onData;
75 private clearPartialMessageTimer;
76 private setPartialMessageTimer;
77}