FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScriptSolidServer/test/webrtc.test.js at gh-pages · JavaScriptSolidServer/JavaScriptSolidServer · GitHub
JavaScriptSolidServer
JavaScriptSolidServer
Repository navigation
Code
Issues
164
(164)
Pull requests
14
(14)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScriptSolidServer
/
test
/
webrtc.test.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
380 lines (314 loc) · 12.2 KB
Breadcrumbs
JavaScriptSolidServer
/
test
/
webrtc.test.js
Copy path
File metadata and controls
380 lines (314 loc) · 12.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/**
* WebRTC Signaling Server Tests
*/
import
{
describe
,
it
,
before
,
after
}
from
'node:test'
;
import
assert
from
'node:assert'
;
import
{
WebSocket
}
from
'ws'
;
import
{
startTestServer
,
stopTestServer
,
createTestPod
,
getBaseUrl
,
getPodToken
}
from
'./helpers.js'
;
describe
(
'WebRTC Signaling'
,
(
)
=>
{
let
wsUrl
;
before
(
async
(
)
=>
{
await
startTestServer
(
{
webrtc
:
true
}
)
;
await
createTestPod
(
'alice'
)
;
await
createTestPod
(
'bob'
)
;
const
base
=
getBaseUrl
(
)
;
wsUrl
=
base
.
replace
(
'http'
,
'ws'
)
+
'/.webrtc'
;
}
)
;
after
(
async
(
)
=>
{
await
stopTestServer
(
)
;
}
)
;
/** Create an authenticated WebSocket for a pod user */
function
connectPeer
(
podName
)
{
const
token
=
getPodToken
(
podName
)
;
const
ws
=
new
WebSocket
(
wsUrl
,
{
headers
:
{
'Authorization'
:
`Bearer
${
token
}
`
}
}
)
;
return
ws
;
}
/** Connect a peer and wait for the 'peers' welcome message */
async
function
connectAndWait
(
podName
)
{
const
ws
=
connectPeer
(
podName
)
;
const
msg
=
await
waitForMessage
(
ws
,
'peers'
)
;
return
{
ws
,
...
msg
}
;
}
/** Wait for a specific message type from a WebSocket */
function
waitForMessage
(
ws
,
type
,
timeout
=
3000
)
{
return
new
Promise
(
(
resolve
,
reject
)
=>
{
function
handler
(
data
)
{
const
msg
=
JSON
.
parse
(
data
.
toString
(
)
)
;
if
(
msg
.
type
===
type
)
{
clearTimeout
(
timer
)
;
ws
.
removeListener
(
'message'
,
handler
)
;
ws
.
removeListener
(
'close'
,
onClose
)
;
resolve
(
msg
)
;
}
}
function
onClose
(
)
{
clearTimeout
(
timer
)
;
ws
.
removeListener
(
'message'
,
handler
)
;
reject
(
new
Error
(
`WebSocket closed while waiting for "
${
type
}
"`
)
)
;
}
const
timer
=
setTimeout
(
(
)
=>
{
ws
.
removeListener
(
'message'
,
handler
)
;
ws
.
removeListener
(
'close'
,
onClose
)
;
reject
(
new
Error
(
`Timeout waiting for "
${
type
}
"`
)
)
;
}
,
timeout
)
;
ws
.
on
(
'message'
,
handler
)
;
ws
.
on
(
'close'
,
onClose
)
;
}
)
;
}
/** Collect messages from a WebSocket for a duration */
function
collectMessages
(
ws
,
duration
=
500
)
{
return
new
Promise
(
(
resolve
)
=>
{
const
msgs
=
[
]
;
const
handler
=
(
data
)
=>
msgs
.
push
(
JSON
.
parse
(
data
.
toString
(
)
)
)
;
ws
.
on
(
'message'
,
handler
)
;
setTimeout
(
(
)
=>
{
ws
.
removeListener
(
'message'
,
handler
)
;
resolve
(
msgs
)
;
}
,
duration
)
;
}
)
;
}
describe
(
'Authentication'
,
(
)
=>
{
it
(
'should allow unauthenticated connections for tracker protocol'
,
async
(
)
=>
{
const
ws
=
new
WebSocket
(
wsUrl
)
;
await
new
Promise
(
(
resolve
)
=>
{
ws
.
onopen
=
resolve
;
}
)
;
// Unauthenticated clients can use tracker protocol
ws
.
send
(
JSON
.
stringify
(
{
action
:
'announce'
,
info_hash
:
'01234567890123456789'
,
peer_id
:
'98765432109876543210'
,
offers
:
[
]
}
)
)
;
const
msg
=
await
waitForMessage
(
ws
,
'announce'
,
3000
)
.
catch
(
(
)
=>
null
)
;
// Should get a response (not get disconnected)
ws
.
close
(
)
;
await
new
Promise
(
r
=>
setTimeout
(
r
,
50
)
)
;
}
)
;
it
(
'should reject unauthenticated identity-based signaling'
,
async
(
)
=>
{
const
ws
=
new
WebSocket
(
wsUrl
)
;
await
new
Promise
(
(
resolve
)
=>
{
ws
.
onopen
=
resolve
;
}
)
;
ws
.
send
(
JSON
.
stringify
(
{
type
:
'offer'
,
to
:
'someone'
,
sdp
:
'test'
}
)
)
;
const
msg
=
await
waitForMessage
(
ws
,
'error'
)
;
assert
.
ok
(
msg
.
message
.
includes
(
'Authentication'
)
)
;
ws
.
close
(
)
;
await
new
Promise
(
r
=>
setTimeout
(
r
,
50
)
)
;
}
)
;
it
(
'should accept authenticated connections'
,
async
(
)
=>
{
const
ws
=
connectPeer
(
'alice'
)
;
const
msg
=
await
waitForMessage
(
ws
,
'peers'
)
;
assert
.
strictEqual
(
msg
.
type
,
'peers'
)
;
assert
.
ok
(
msg
.
you
,
'Should include own WebID'
)
;
assert
.
ok
(
Array
.
isArray
(
msg
.
peers
)
,
'Should include peers list'
)
;
ws
.
close
(
)
;
}
)
;
}
)
;
describe
(
'Peer Presence and Signaling Relay'
,
(
)
=>
{
it
(
'should handle full signaling lifecycle'
,
async
(
)
=>
{
// Alice connects first — should see no peers
const
{
ws
:
alice
,
you
:
aliceId
}
=
await
connectAndWait
(
'alice'
)
;
// Bob joins — set up listener for peer-joined before bob connects
const
joinPromise
=
waitForMessage
(
alice
,
'peer-joined'
)
;
const
{
ws
:
bob
,
you
:
bobId
,
peers
:
bobPeerList
}
=
await
connectAndWait
(
'bob'
)
;
// Bob should see alice in the peer list
assert
.
strictEqual
(
bobPeerList
.
length
,
1
,
'Bob should see Alice'
)
;
// Alice should get peer-joined notification
const
joinMsg
=
await
joinPromise
;
assert
.
strictEqual
(
joinMsg
.
type
,
'peer-joined'
)
;
// 1. Alice sends offer to Bob
const
offerPromise
=
waitForMessage
(
bob
,
'offer'
)
;
alice
.
send
(
JSON
.
stringify
(
{
type
:
'offer'
,
to
:
bobId
,
sdp
:
'v=0\r\n'
}
)
)
;
const
offer
=
await
offerPromise
;
assert
.
strictEqual
(
offer
.
type
,
'offer'
)
;
assert
.
strictEqual
(
offer
.
from
,
aliceId
)
;
assert
.
ok
(
offer
.
sdp
,
'Should include SDP'
)
;
assert
.
strictEqual
(
offer
.
to
,
undefined
,
'Should strip "to" field'
)
;
// 2. Bob sends answer to Alice
const
answerPromise
=
waitForMessage
(
alice
,
'answer'
)
;
bob
.
send
(
JSON
.
stringify
(
{
type
:
'answer'
,
to
:
aliceId
,
sdp
:
'v=0\r\n'
}
)
)
;
const
answer
=
await
answerPromise
;
assert
.
strictEqual
(
answer
.
type
,
'answer'
)
;
assert
.
strictEqual
(
answer
.
from
,
bobId
)
;
// 3. Alice sends ICE candidate to Bob
const
candidatePromise
=
waitForMessage
(
bob
,
'candidate'
)
;
alice
.
send
(
JSON
.
stringify
(
{
type
:
'candidate'
,
to
:
bobId
,
candidate
:
{
candidate
:
'candidate:1 1 UDP 2122252543 192.168.1.1 12345 typ host'
,
sdpMid
:
'0'
}
}
)
)
;
const
candidate
=
await
candidatePromise
;
assert
.
strictEqual
(
candidate
.
type
,
'candidate'
)
;
assert
.
ok
(
candidate
.
candidate
.
candidate
)
;
// 4. Alice sends hangup to Bob
const
hangupPromise
=
waitForMessage
(
bob
,
'hangup'
)
;
alice
.
send
(
JSON
.
stringify
(
{
type
:
'hangup'
,
to
:
bobId
}
)
)
;
const
hangup
=
await
hangupPromise
;
assert
.
strictEqual
(
hangup
.
type
,
'hangup'
)
;
assert
.
strictEqual
(
hangup
.
from
,
aliceId
)
;
// 5. Bob leaves — alice should get notified
const
leavePromise
=
waitForMessage
(
alice
,
'peer-left'
)
;
bob
.
close
(
)
;
const
leaveMsg
=
await
leavePromise
;
assert
.
strictEqual
(
leaveMsg
.
type
,
'peer-left'
)
;
alice
.
close
(
)
;
await
new
Promise
(
r
=>
setTimeout
(
r
,
100
)
)
;
}
)
;
}
)
;
describe
(
'Error Handling'
,
(
)
=>
{
it
(
'should reject invalid JSON'
,
async
(
)
=>
{
const
alice
=
connectPeer
(
'alice'
)
;
await
waitForMessage
(
alice
,
'peers'
)
;
alice
.
send
(
'not json'
)
;
const
err
=
await
waitForMessage
(
alice
,
'error'
)
;
assert
.
strictEqual
(
err
.
message
,
'Invalid JSON'
)
;
alice
.
close
(
)
;
}
)
;
it
(
'should reject messages without "to" field'
,
async
(
)
=>
{
const
alice
=
connectPeer
(
'alice'
)
;
await
waitForMessage
(
alice
,
'peers'
)
;
alice
.
send
(
JSON
.
stringify
(
{
type
:
'offer'
,
sdp
:
'...'
}
)
)
;
const
err
=
await
waitForMessage
(
alice
,
'error'
)
;
assert
.
ok
(
err
.
message
.
includes
(
'Missing'
)
)
;
alice
.
close
(
)
;
}
)
;
it
(
'should error when target peer is not online'
,
async
(
)
=>
{
const
alice
=
connectPeer
(
'alice'
)
;
await
waitForMessage
(
alice
,
'peers'
)
;
alice
.
send
(
JSON
.
stringify
(
{
type
:
'offer'
,
to
:
'https://nobody.example/profile/card#me'
,
sdp
:
'...'
}
)
)
;
const
err
=
await
waitForMessage
(
alice
,
'error'
)
;
assert
.
ok
(
err
.
message
.
includes
(
'not online'
)
)
;
alice
.
close
(
)
;
await
new
Promise
(
r
=>
setTimeout
(
r
,
50
)
)
;
}
)
;
}
)
;
describe
(
'Content-Addressed Peer Discovery'
,
(
)
=>
{
const
RESOURCE_HASH
=
'a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2'
;
it
(
'should return peer count on announce'
,
async
(
)
=>
{
const
{
ws
:
alice
}
=
await
connectAndWait
(
'alice'
)
;
alice
.
send
(
JSON
.
stringify
(
{
type
:
'announce'
,
resource
:
RESOURCE_HASH
,
offers
:
[
]
}
)
)
;
const
msg
=
await
waitForMessage
(
alice
,
'resource-peers'
)
;
assert
.
strictEqual
(
msg
.
resource
,
RESOURCE_HASH
)
;
assert
.
strictEqual
(
msg
.
count
,
0
)
;
alice
.
close
(
)
;
await
new
Promise
(
r
=>
setTimeout
(
r
,
50
)
)
;
}
)
;
it
(
'should relay offers between peers sharing a resource'
,
async
(
)
=>
{
const
{
ws
:
alice
,
peerId
:
alicePeerId
}
=
await
connectAndWait
(
'alice'
)
;
// Alice announces with no offers (first in group)
alice
.
send
(
JSON
.
stringify
(
{
type
:
'announce'
,
resource
:
RESOURCE_HASH
,
offers
:
[
]
}
)
)
;
await
waitForMessage
(
alice
,
'resource-peers'
)
;
// Bob announces with an offer — should be relayed to Alice
const
offerPromise
=
waitForMessage
(
alice
,
'offer'
)
;
const
{
ws
:
bob
,
peerId
:
bobPeerId
}
=
await
connectAndWait
(
'bob'
)
;
bob
.
send
(
JSON
.
stringify
(
{
type
:
'announce'
,
resource
:
RESOURCE_HASH
,
offers
:
[
{
sdp
:
'v=0\r\nbob-offer'
,
offer_id
:
'offer1'
}
]
}
)
)
;
const
offer
=
await
offerPromise
;
assert
.
strictEqual
(
offer
.
type
,
'offer'
)
;
assert
.
strictEqual
(
offer
.
resource
,
RESOURCE_HASH
)
;
assert
.
strictEqual
(
offer
.
from
,
bobPeerId
)
;
assert
.
strictEqual
(
offer
.
offer_id
,
'offer1'
)
;
assert
.
ok
(
offer
.
sdp
.
includes
(
'bob-offer'
)
)
;
// Alice answers Bob
const
answerPromise
=
waitForMessage
(
bob
,
'answer'
)
;
alice
.
send
(
JSON
.
stringify
(
{
type
:
'answer'
,
resource
:
RESOURCE_HASH
,
to
:
bobPeerId
,
offer_id
:
'offer1'
,
sdp
:
'v=0\r\nalice-answer'
}
)
)
;
const
answer
=
await
answerPromise
;
assert
.
strictEqual
(
answer
.
type
,
'answer'
)
;
assert
.
strictEqual
(
answer
.
resource
,
RESOURCE_HASH
)
;
assert
.
strictEqual
(
answer
.
from
,
alicePeerId
)
;
assert
.
ok
(
answer
.
sdp
.
includes
(
'alice-answer'
)
)
;
alice
.
close
(
)
;
bob
.
close
(
)
;
await
new
Promise
(
r
=>
setTimeout
(
r
,
50
)
)
;
}
)
;
it
(
'should clean up resources on disconnect'
,
async
(
)
=>
{
const
{
ws
:
alice
}
=
await
connectAndWait
(
'alice'
)
;
alice
.
send
(
JSON
.
stringify
(
{
type
:
'announce'
,
resource
:
RESOURCE_HASH
,
offers
:
[
]
}
)
)
;
await
waitForMessage
(
alice
,
'resource-peers'
)
;
// Bob joins the resource group
const
{
ws
:
bob
}
=
await
connectAndWait
(
'bob'
)
;
bob
.
send
(
JSON
.
stringify
(
{
type
:
'announce'
,
resource
:
RESOURCE_HASH
,
offers
:
[
]
}
)
)
;
const
bobPeers
=
await
waitForMessage
(
bob
,
'resource-peers'
)
;
assert
.
strictEqual
(
bobPeers
.
count
,
1
)
;
// alice is there
// Alice disconnects
alice
.
close
(
)
;
await
new
Promise
(
r
=>
setTimeout
(
r
,
200
)
)
;
// Charlie joins — should see only bob
const
{
ws
:
charlie
}
=
await
connectAndWait
(
'bob'
)
;
// reuse bob pod
charlie
.
send
(
JSON
.
stringify
(
{
type
:
'announce'
,
resource
:
RESOURCE_HASH
,
offers
:
[
]
}
)
)
;
const
charliePeers
=
await
waitForMessage
(
charlie
,
'resource-peers'
)
;
// bob was reconnected (old connection closed), so count depends on timing
assert
.
ok
(
charliePeers
.
count
>=
0
)
;
bob
.
close
(
)
;
charlie
.
close
(
)
;
await
new
Promise
(
r
=>
setTimeout
(
r
,
50
)
)
;
}
)
;
it
(
'should handle leave message'
,
async
(
)
=>
{
const
{
ws
:
alice
}
=
await
connectAndWait
(
'alice'
)
;
alice
.
send
(
JSON
.
stringify
(
{
type
:
'announce'
,
resource
:
RESOURCE_HASH
,
offers
:
[
]
}
)
)
;
await
waitForMessage
(
alice
,
'resource-peers'
)
;
// Leave the resource group
alice
.
send
(
JSON
.
stringify
(
{
type
:
'leave'
,
resource
:
RESOURCE_HASH
}
)
)
;
// Bob joins — should see 0 peers (alice left)
const
{
ws
:
bob
}
=
await
connectAndWait
(
'bob'
)
;
bob
.
send
(
JSON
.
stringify
(
{
type
:
'announce'
,
resource
:
RESOURCE_HASH
,
offers
:
[
]
}
)
)
;
const
msg
=
await
waitForMessage
(
bob
,
'resource-peers'
)
;
assert
.
strictEqual
(
msg
.
count
,
0
)
;
alice
.
close
(
)
;
bob
.
close
(
)
;
await
new
Promise
(
r
=>
setTimeout
(
r
,
50
)
)
;
}
)
;
it
(
'should reject invalid resource hash'
,
async
(
)
=>
{
const
{
ws
:
alice
}
=
await
connectAndWait
(
'alice'
)
;
alice
.
send
(
JSON
.
stringify
(
{
type
:
'announce'
,
resource
:
'not-a-hex-hash!'
,
offers
:
[
]
}
)
)
;
const
err
=
await
waitForMessage
(
alice
,
'error'
)
;
assert
.
ok
(
err
.
message
.
includes
(
'Invalid resource hash'
)
)
;
alice
.
close
(
)
;
await
new
Promise
(
r
=>
setTimeout
(
r
,
50
)
)
;
}
)
;
}
)
;
}
)
;
Back
|
FazBrowse Home
|
New Git URL