-
Notifications
You must be signed in to change notification settings - Fork 328
/
ral.ts
105 lines (86 loc) · 3.04 KB
/
ral.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
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import type { Disposable } from './disposable';
import type { ContentTypeEncoder, ContentTypeDecoder } from './encoding';
interface _MessageBuffer {
readonly encoding: RAL.MessageBufferEncoding;
/**
* Append data to the message buffer.
*
* @param chunk the data to append.
*/
append(chunk: Uint8Array | string): void;
/**
* Tries to read the headers from the buffer
*
* @returns the header properties or undefined in not enough data can be read.
*/
tryReadHeaders(): Map<string, string> | undefined;
/**
* Tries to read the body of the given length.
*
* @param length the amount of bytes to read.
* @returns the data or undefined int less data is available.
*/
tryReadBody(length: number): Uint8Array | undefined;
}
type _MessageBufferEncoding = 'ascii' | 'utf-8';
interface _ReadableStream {
onData(listener: (data: Uint8Array) => void): Disposable;
onClose(listener: () => void): Disposable;
onError(listener: (error: any) => void): Disposable;
onEnd(listener: () => void): Disposable;
}
interface _WritableStream {
onClose(listener: () => void): Disposable;
onError(listener: (error: any) => void): Disposable;
onEnd(listener: () => void): Disposable;
write(data: Uint8Array): Promise<void>;
write(data: string, encoding: _MessageBufferEncoding): Promise<void>;
end(): void;
}
interface _DuplexStream extends _ReadableStream, _WritableStream {
}
interface RAL {
readonly applicationJson: {
readonly encoder: ContentTypeEncoder;
readonly decoder: ContentTypeDecoder;
};
readonly messageBuffer: {
create(encoding: RAL.MessageBufferEncoding): RAL.MessageBuffer;
};
readonly console: {
info(message?: any, ...optionalParams: any[]): void;
log(message?: any, ...optionalParams: any[]): void;
warn(message?: any, ...optionalParams: any[]): void;
error(message?: any, ...optionalParams: any[]): void;
};
readonly timer: {
setTimeout(callback: (...args: any[]) => void, ms: number, ...args: any[]): Disposable;
setImmediate(callback: (...args: any[]) => void, ...args: any[]): Disposable;
setInterval(callback: (...args: any[]) => void, ms: number, ...args: any[]): Disposable;
};
}
let _ral: RAL | undefined;
function RAL(): RAL {
if (_ral === undefined) {
throw new Error(`No runtime abstraction layer installed`);
}
return _ral;
}
namespace RAL {
export type MessageBuffer = _MessageBuffer;
export type MessageBufferEncoding = _MessageBufferEncoding;
export type ReadableStream = _ReadableStream;
export type WritableStream = _WritableStream;
export type DuplexStream = _DuplexStream;
export function install(ral: RAL): void {
if (ral === undefined) {
throw new Error(`No runtime abstraction layer provided`);
}
_ral = ral;
}
}
export default RAL;