forked from peers/peerjs-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
212 lines (177 loc) · 6.01 KB
/
server.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
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
var PeerServer = require('../').PeerServer;
var expect = require('expect.js');
var sinon = require('sinon');
describe('PeerServer', function() {
describe('constructor', function() {
before(function() {
PeerServer.prototype._initializeWSS = sinon.stub();
PeerServer.prototype._initializeHTTP = sinon.stub();
});
it('should be able to be created without the `new` keyword', function() {
var p = PeerServer();
expect(p.constructor).to.be(PeerServer);
});
it('should default to port 80, key `peerjs`', function() {
var p = new PeerServer();
expect(p._options.key).to.be('peerjs');
expect(p._options.port).to.be(80);
});
it('should accept a custom port', function() {
var p = new PeerServer({ port: 8000 });
expect(p._options.port).to.be(8000);
});
});
describe('#_initializeWSS', function() {
WebSocketServer = sinon.stub();
});
describe('#_configureWS', function() {
});
describe('#_checkKey', function() {
var p;
before(function() {
PeerServer.prototype._initializeHTTP = sinon.stub();
p = new PeerServer({ port: 8000 });
p._checkKey('peerjs', 'myip', function() {});
});
it('should reject keys that are not the default', function(done) {
p._checkKey('bad key', null, function(response) {
expect(response).to.be('Invalid key provided');
done();
});
});
it('should accept valid key/ip pairs', function(done) {
p._checkKey('peerjs', 'myip', function(response) {
expect(response).to.be(null);
done();
});
});
it('should reject ips that are at their limit', function(done) {
p._options.ip_limit = 0;
p._checkKey('peerjs', 'myip', function(response) {
expect(response).to.be('myip has reached its concurrent user limit');
done();
});
});
it('should reject when the server is at its limit', function(done) {
p._options.concurrent_limit = 0;
p._checkKey('peerjs', 'myip', function(response) {
expect(response).to.be('Server has reached its concurrent user limit');
done();
});
});
});
describe('#_initializeHTTP', function() {
});
describe('#_startStreaming', function() {
});
describe('#_pruneOutstanding', function() {
});
describe('#_processOutstanding', function() {
});
describe('#_removePeer', function() {
var p;
before(function() {
PeerServer.prototype._initializeHTTP = sinon.stub();
p = new PeerServer({ port: 8000 });
var fake = {ip: '0.0.0.0'};
p._ips[fake.ip] = 1;
p._clients['peerjs'] = {};
p._clients['peerjs']['test'] = fake;
});
it('should decrement the number of ips being used and remove the connection', function() {
expect(p._ips['0.0.0.0']).to.be(1);
p._removePeer('peerjs', 'test');
expect(p._ips['0.0.0.0']).to.be(0);
expect(p._clients['peerjs']['test']).to.be(undefined);
});
});
describe('#_handleTransmission', function() {
var p;
var KEY = 'peerjs';
var ID = 'test';
before(function() {
PeerServer.prototype._initializeHTTP = sinon.stub();
p = new PeerServer({ port: 8000 });
p._clients[KEY] = {};
});
it('should send to the socket when appropriate', function() {
var send = sinon.spy();
var write = sinon.spy();
var message = {dst: ID};
p._clients[KEY][ID] = {
socket: {
send: send
},
res: {
write: write
}
}
p._handleTransmission(KEY, message);
expect(send.calledWith(JSON.stringify(message))).to.be(true);
expect(write.calledWith(JSON.stringify(message))).to.be(false);
});
it('should write to the response with a newline when appropriate', function() {
var write = sinon.spy();
var message = {dst: ID};
p._clients[KEY][ID] = {
res: {
write: write
}
}
p._handleTransmission(KEY, message);
expect(write.calledWith(JSON.stringify(message) + '\n')).to.be(true);
});
// no destination.
it('should push to outstanding messages if the destination is not found', function() {
var message = {dst: ID};
p._outstanding[KEY] = {};
p._clients[KEY] = {};
p._handleTransmission(KEY, message);
expect(p._outstanding[KEY][ID][0]).to.be(message);
});
it('should not push to outstanding messages if the message is a LEAVE or EXPIRE', function() {
var message = {dst: ID, type: 'LEAVE'};
p._outstanding[KEY] = {};
p._clients[KEY] = {};
p._handleTransmission(KEY, message);
expect(p._outstanding[KEY][ID]).to.be(undefined);
message = {dst: ID, type: 'EXPIRE'};
p._handleTransmission(KEY, message);
expect(p._outstanding[KEY][ID]).to.be(undefined);
});
it('should remove the peer if there is no dst in the message', function() {
var message = {type: 'LEAVE'};
p._removePeer = sinon.spy();
p._outstanding[KEY] = {};
p._handleTransmission(KEY, message);
expect(p._removePeer.calledWith(KEY, undefined)).to.be(true);
});
it('should remove the peer and send a LEAVE message if the socket appears to be closed', function() {
var send = sinon.stub().throws();
var message = {dst: ID};
var leaveMessage = {type: 'LEAVE', dst: undefined, src: ID};
var oldHandleTransmission = p._handleTransmission;
p._removePeer = function() {
// Hacks!
p._handleTransmission = sinon.spy();
};
p._clients[KEY][ID] = {
socket: {
send: send
}
}
p._handleTransmission(KEY, message);
expect(p._handleTransmission.calledWith(KEY, leaveMessage)).to.be(true);
});
});
describe('#_generateClientId', function() {
var p;
before(function() {
PeerServer.prototype._initializeHTTP = sinon.stub();
p = new PeerServer({ port: 8000 });
});
it('should generate a 16-character ID', function() {
expect(p._generateClientId('anykey').length).to.be(16);
});
});
});