-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
142 lines (108 loc) · 2.97 KB
/
index.js
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
import { packFrame, unpackFrame, printFrame } from './frame.js';
import { Stream } from './stream.js';
import { Connection } from './connection.js';
import { WebSocketClientTransport } from './transport.js';
const STATE_WAITING_FOR_FRAME = 0;
const STATE_RECEIVING_FRAME = 1;
//const DATAGRAM_STREAM_ID = 0;
class WebTransport {
constructor(uri) {
this._closedPromise = new Promise((resolve, reject) => {
this._closeSuccess = resolve;
this._closeFail = reject;
});
this._readyPromise = new Promise(async (resolve, reject) => {
this._conn = await connect({
uri,
});
this._conn.onError((e) => {
this._closeFail(e);
});
resolve();
});
}
get ready() {
return this._readyPromise;
}
get closed() {
return this._closedPromise;
}
get incomingBidirectionalStreams() {
return this._conn.incomingBidirectionalStreams;
}
createBidirectionalStream() {
return this._conn.open();
}
close() {
this._conn.close();
}
}
async function connect(config) {
let transport = new WebSocketClientTransport(config.uri);
await transport.ready;
//transport = new MuxadoTransportWrapper(transport);
return new Connection({
transport,
});
}
// Since muxado is designed to work with TCP streams, which we're simulating
// using WebSockets, frames might arrive split across multiple websocket
// frames. This wrapper combines them together as necessary.
class MuxadoTransportWrapper {
constructor(transport) {
this._transport = transport;
let state = STATE_WAITING_FOR_FRAME;
let frame;
this._transport.onMessage((message) => {
const evt = {
data: message,
};
switch (state) {
case STATE_WAITING_FOR_FRAME:
const frameArray = new Uint8Array(evt.data);
frame = unpackFrame(frameArray);
if (frame.bytesReceived < frame.length) {
state = STATE_RECEIVING_FRAME;
}
else {
delete frame.bytesReceived;
this._onMessageCallback(frameArray);
}
break;
case STATE_RECEIVING_FRAME:
const arr = new Uint8Array(evt.data);
frame.data.set(arr, frame.bytesReceived);
// TODO: make sure we're properly handling frames split across multiple websocket messages
frame.bytesReceived += evt.data.length;
if (frame.data.length === frame.length) {
state = STATE_WAITING_FOR_FRAME;
delete frame.bytesReceived;
this._onMessageCallback(packFrame(frame));
}
break;
}
});
}
get ready() {
return this._transport.ready;
}
get closed() {
return this._transport.closed;
}
send(msg) {
this._transport.send(msg);
}
onMessage(callback) {
this._onMessageCallback = callback;
}
onError(callback) {
this._errorCallback = callback;
}
close() {
this._transport.close();
}
}
export default {
connect,
WebTransport,
};