-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
metacom.d.ts
184 lines (168 loc) Β· 4.27 KB
/
metacom.d.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { EventEmitter } from 'node:events';
import { ClientRequest, ServerResponse } from 'node:http';
import { Writable } from 'node:stream';
import WebSocket from 'ws';
import { Semaphore } from 'metautil';
export interface MetacomError extends Error {
code: string;
}
export class MetaReadable extends EventEmitter {
id: number;
name: string;
size: number;
constructor(
id: number,
name: string,
size: number,
options?: {
highWaterMark?: number;
},
);
push(data: ArrayBufferView): Promise<ArrayBufferView>;
finalize(writable: Writable): Promise<void>;
pipe(writable: Writable): Writable;
toBlob(type?: string): Promise<Blob>;
}
export class MetaWritable extends EventEmitter {
id: number;
name: string;
size: number;
constructor(id: number, name: string, size: number, transport: Transport);
write(data: ArrayBufferView): void;
end(): void;
terminate(): void;
}
export interface BlobUploader {
id: number;
upload(): Promise<void>;
}
export class Metacom extends EventEmitter {
url: string;
socket: WebSocket;
api: object;
callId: number;
calls: Map<number, [Function, Function]>;
constructor(url: string);
static create(url: string, options?: unknown): Metacom;
ready(): Promise<void>;
load(...interfaces: Array<string>): Promise<void>;
httpCall(
unit: string,
ver: string,
): (methodName: string) => (args: object) => Promise<void>;
socketCall(
unit: string,
ver: string,
): (methodName: string) => (args: object) => Promise<void>;
getStream(id: number): MetaReadable;
createStream(name: string, size: number): MetaWritable;
createBlobUploader(blob: Blob): BlobUploader;
}
export interface Options {
concurrency: number;
host: string;
port: number;
kind: 'server' | 'balancer';
protocol: string;
ports: Array<number>;
queue: object;
}
export interface ErrorOptions {
id?: number;
error?: Error;
pass?: boolean;
}
export interface Auth {
generateToken(): string;
saveSession(token: string, data: object): Promise<void>;
createSession(token: string, data: object, fields?: object): Promise<void>;
readSession(token: string): Promise<object | null>;
deleteSession(token: string): Promise<void>;
registerUser(login: string, password: string): Promise<object>;
getUser(login: string): Promise<object>;
}
export class Client extends EventEmitter {
ip: string | undefined;
session: Session;
}
export class Transport {
console: Console;
req: ClientRequest;
res?: ServerResponse;
connection?: WebSocket;
ip: string;
constructor(
console: Console,
req: ClientRequest,
target: ServerResponse | WebSocket,
);
error(code: number, errorOptions?: ErrorOptions): void;
write(data: string | Buffer, httpCode?: number, ext?: string): void;
send(obj: object, httpCode?: number): void;
redirect?(location: string): void;
options?(): void;
getCookies?(): object;
sendSessionCookie(token: string): void;
removeSessionCookie(): void;
close(): void;
}
export interface CallPacket {
type: 'call';
id: number;
method: string;
args: object;
meta: object;
}
export interface StreamPacket {
type: 'stream';
id: number;
name: string;
size: number;
}
export class Server {
application: object;
options: Options;
balancer: boolean;
console: Console;
semaphore: Semaphore;
httpServer: any;
wsServer: any;
clients: Set<Client>;
constructor(options: Options, application: object);
init(): void;
listen(): Promise<void>;
message(client: Client, data: string): void;
rpc(client: Client, packet: CallPacket): Promise<void>;
binary(client: Client, data: Buffer): void;
handleRpcPacket(client: Client, packet: CallPacket): void;
handleStreamPacket(client: Client, packet: StreamPacket): Promise<void>;
handleRequest(
client: Client,
transport: Transport,
data: Buffer,
application: object,
): void;
hook(
client: Client,
proc: object,
packet: CallPacket,
verb: string,
headers: object,
): Promise<void>;
balancing(transport: Transport): void;
closeClients(): void;
close(): Promise<void>;
}
export interface State {
[key: string]: any;
}
export interface Session {
token: string;
state: State;
}
export interface Context {
client: Client;
uuid: string;
state: State;
session: Session;
}