-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.ts
331 lines (269 loc) · 8.04 KB
/
index.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import EventEmitter from "events";
import { Readable, Writable } from "stream";
import type { TypedEmitter } from "tiny-typed-emitter";
export class Controller {
events = new EventEmitter() as TypedEmitter<ControllerEvents>;
private handlers: { [stanzaName: string]: (payload: any, data: Buffer | null) => void };
private sources = new Map<number, Source>();
private nextEndpointId = 1;
private requests = new Map<StanzaId, RequestCallbacks>();
private nextRequestId = 1;
constructor() {
this.handlers = {
".create": this.onCreate,
".finish": this.onFinish,
".write": this.onWrite
};
}
open(label: string, details: StreamDetails = {}): OutgoingStream {
const endpoint: Endpoint = {
id: this.nextEndpointId++,
label,
details
};
return new Sink(this, endpoint);
}
receive(packet: Packet): void {
const stanza = packet.stanza as Stanza;
const { id, name, payload } = stanza;
const type = name[0];
if (type === ".") {
this.onRequest(id, name, payload, packet.data);
} else if (type === "+") {
this.onNotification(id, name, payload);
} else {
throw new Error("Unknown stanza: " + name);
}
}
private onCreate = (payload: CreateRequest): void => {
const endpoint = payload.endpoint;
const source = new Source(endpoint);
this.sources.set(endpoint.id, source);
this.events.emit("stream", source);
};
private onFinish = (payload: FinishRequest): void => {
const id = payload.endpoint.id;
const source = this.sources.get(id);
if (source === undefined) {
throw new Error("Invalid endpoint ID");
}
this.sources.delete(id);
source.push(null);
};
private onWrite = (payload: WriteRequest, data: Buffer | null): Promise<void> => {
const id = payload.endpoint.id;
const source = this.sources.get(id);
if (source === undefined) {
throw new Error("Invalid endpoint ID");
}
if (data === null) {
throw new Error("Invalid request: missing data");
}
return source.deliver(data);
};
_request(name: StanzaName, payload: StanzaPayload, data: Buffer | null) {
return new Promise((resolve, reject) => {
const id = this.nextRequestId++;
this.requests.set(id, {
resolve: resolve,
reject: reject
});
const stanza: Stanza = {
id,
name,
payload
};
this.events.emit("send", {
stanza,
data
});
});
}
private onRequest(id: StanzaId, name: StanzaName, payload: StanzaPayload, data: Buffer | null): void {
const handler = this.handlers[name];
if (handler === undefined) {
throw new Error(`Invalid request: ${name}`);
}
let result: any;
try {
result = handler(payload, data);
} catch (e) {
this.reject(id, e as Error);
return;
}
if (result instanceof Promise) {
result
.then(value => this.resolve(id, value))
.catch(error => this.reject(id, error))
} else {
this.resolve(id, result);
}
}
private resolve(id: StanzaId, value: StanzaPayload): void {
const stanza: Stanza = {
id: id,
name: "+result",
payload: value
};
this.events.emit("send", {
stanza,
data: null
});
}
private reject(id: StanzaId, error: Error): void {
const stanza: Stanza = {
id: id,
name: "+error",
payload: {
message: error.toString()
}
};
this.events.emit("send", {
stanza,
data: null
});
}
private onNotification(id: StanzaId, name: StanzaName, payload: StanzaPayload): void {
const request = this.requests.get(id);
if (request === undefined) {
throw new Error("Invalid request ID");
}
this.requests.delete(id);
if (name === "+result") {
request.resolve(payload);
} else if (name === "+error") {
const response = payload as ErrorResponse;
request.reject(new Error(response.message));
} else {
throw new Error("Unknown notification: " + name);
}
}
}
export default Controller;
type ControllerEvents = {
stream: (stream: IncomingStream) => void;
send: (packet: Packet) => void;
};
interface RequestCallbacks {
resolve: (value: StanzaPayload) => void;
reject: (error: Error) => void;
}
export interface IncomingStream extends Readable {
label: string;
details: StreamDetails;
}
export interface OutgoingStream extends Writable {
}
export interface StreamDetails {
[key: string]: any;
}
export interface Packet {
stanza: {
[key: string]: any;
};
data: Buffer | null;
}
interface Stanza {
id: StanzaId;
name: StanzaName;
payload: StanzaPayload
}
type StanzaId = number;
type StanzaName = string;
interface StanzaPayload {
[key: string]: any;
}
interface CreateRequest {
endpoint: Endpoint;
}
interface FinishRequest {
endpoint: Endpoint;
}
interface WriteRequest {
endpoint: Endpoint;
}
interface ErrorResponse {
message: string;
}
interface Endpoint {
id: number;
label: string;
details: StreamDetails
}
class Source extends Readable implements IncomingStream {
label: string;
details: StreamDetails;
private onReadComplete: ReadCallback | null = null;
private delivery: PendingDelivery | null = null;
constructor({ label, details }: Endpoint) {
super();
this.label = label;
this.details = details;
}
_read(size: number): void {
if (this.onReadComplete !== null) {
return;
}
this.onReadComplete = chunk => {
this.onReadComplete = null;
if (chunk.length === 0) {
this.push(null);
return false;
}
if (this.push(chunk)) {
this._read(size);
}
return true;
};
this.tryComplete();
}
deliver(chunk: Buffer): Promise<void> {
return new Promise<void>((resolve, reject) => {
if (this.delivery !== null) {
throw new Error("Protocol violation");
}
this.delivery = {
chunk: chunk,
resolve: resolve,
reject: reject
};
this.tryComplete();
});
}
private tryComplete(): void {
const { onReadComplete, delivery } = this;
if (onReadComplete === null || delivery === null) {
return;
}
this.onReadComplete = null;
this.delivery = null;
if (onReadComplete(delivery.chunk)) {
delivery.resolve();
} else {
delivery.reject(new Error("Stream closed"));
}
}
}
type ReadCallback = (chunk: Buffer) => boolean;
interface PendingDelivery {
chunk: any;
resolve: () => void;
reject: (error: Error) => void;
}
class Sink extends Writable implements OutgoingStream {
constructor(
private controller: Controller,
private endpoint: Endpoint) {
super();
this.controller._request(".create", { endpoint: this.endpoint }, null);
this.once("finish", this._onFinish.bind(this));
}
_write(chunk: any, encoding: any, callback: any): void {
this.controller._request(".write", { endpoint: this.endpoint }, chunk)
.then(_ => callback())
.catch(error => callback(error));
}
_onFinish(): void {
this.controller._request(".finish", { endpoint: this.endpoint }, null);
}
}