UNPKG

2.83 kBTypeScriptView Raw
1import type { Disposable } from './disposable';
2import type { ContentTypeEncoder, ContentTypeDecoder } from './encoding';
3interface _MessageBuffer {
4 readonly encoding: RAL.MessageBufferEncoding;
5 /**
6 * Append data to the message buffer.
7 *
8 * @param chunk the data to append.
9 */
10 append(chunk: Uint8Array | string): void;
11 /**
12 * Tries to read the headers from the buffer
13 *
14 * @param lowerCaseKeys Whether the keys should be stored lower case. Doing
15 * so is recommended since HTTP headers are case insensitive.
16 *
17 * @returns the header properties or undefined in not enough data can be read.
18 */
19 tryReadHeaders(lowerCaseKeys?: boolean): Map<string, string> | undefined;
20 /**
21 * Tries to read the body of the given length.
22 *
23 * @param length the amount of bytes to read.
24 * @returns the data or undefined int less data is available.
25 */
26 tryReadBody(length: number): Uint8Array | undefined;
27}
28type _MessageBufferEncoding = 'ascii' | 'utf-8';
29interface _ReadableStream {
30 onData(listener: (data: Uint8Array) => void): Disposable;
31 onClose(listener: () => void): Disposable;
32 onError(listener: (error: any) => void): Disposable;
33 onEnd(listener: () => void): Disposable;
34}
35interface _WritableStream {
36 onClose(listener: () => void): Disposable;
37 onError(listener: (error: any) => void): Disposable;
38 onEnd(listener: () => void): Disposable;
39 write(data: Uint8Array): Promise<void>;
40 write(data: string, encoding: _MessageBufferEncoding): Promise<void>;
41 end(): void;
42}
43interface _DuplexStream extends _ReadableStream, _WritableStream {
44}
45interface RAL {
46 readonly applicationJson: {
47 readonly encoder: ContentTypeEncoder;
48 readonly decoder: ContentTypeDecoder;
49 };
50 readonly messageBuffer: {
51 create(encoding: RAL.MessageBufferEncoding): RAL.MessageBuffer;
52 };
53 readonly console: {
54 info(message?: any, ...optionalParams: any[]): void;
55 log(message?: any, ...optionalParams: any[]): void;
56 warn(message?: any, ...optionalParams: any[]): void;
57 error(message?: any, ...optionalParams: any[]): void;
58 };
59 readonly timer: {
60 setTimeout(callback: (...args: any[]) => void, ms: number, ...args: any[]): Disposable;
61 setImmediate(callback: (...args: any[]) => void, ...args: any[]): Disposable;
62 setInterval(callback: (...args: any[]) => void, ms: number, ...args: any[]): Disposable;
63 };
64}
65declare function RAL(): RAL;
66declare namespace RAL {
67 type MessageBuffer = _MessageBuffer;
68 type MessageBufferEncoding = _MessageBufferEncoding;
69 type ReadableStream = _ReadableStream;
70 type WritableStream = _WritableStream;
71 type DuplexStream = _DuplexStream;
72 function install(ral: RAL): void;
73}
74export default RAL;