]>
Commit | Line | Data |
---|---|---|
8727f598 JD |
1 | import Base64 from '../core/base64.js'; |
2 | ||
dfae3209 | 3 | // PhantomJS can't create Event objects directly, so we need to use this |
95632e41 | 4 | function makeEvent(name, props) { |
2b5f94fa | 5 | const evt = document.createEvent('Event'); |
dfae3209 SR |
6 | evt.initEvent(name, true, true); |
7 | if (props) { | |
2b5f94fa | 8 | for (let prop in props) { |
dfae3209 | 9 | evt[prop] = props[prop]; |
2cccf753 | 10 | } |
2cccf753 | 11 | } |
dfae3209 SR |
12 | return evt; |
13 | } | |
2cccf753 | 14 | |
0e4808bf JD |
15 | export default class FakeWebSocket { |
16 | constructor(uri, protocols) { | |
17 | this.url = uri; | |
18 | this.binaryType = "arraybuffer"; | |
19 | this.extensions = ""; | |
2cccf753 | 20 | |
0e4808bf JD |
21 | if (!protocols || typeof protocols === 'string') { |
22 | this.protocol = protocols; | |
23 | } else { | |
24 | this.protocol = protocols[0]; | |
25 | } | |
2cccf753 | 26 | |
95632e41 | 27 | this._sendQueue = new Uint8Array(20000); |
2cccf753 | 28 | |
0e4808bf JD |
29 | this.readyState = FakeWebSocket.CONNECTING; |
30 | this.bufferedAmount = 0; | |
2cccf753 | 31 | |
95632e41 | 32 | this._isFake = true; |
0e4808bf | 33 | } |
2cccf753 | 34 | |
0e4808bf | 35 | close(code, reason) { |
dfae3209 SR |
36 | this.readyState = FakeWebSocket.CLOSED; |
37 | if (this.onclose) { | |
95632e41 | 38 | this.onclose(makeEvent("close", { 'code': code, 'reason': reason, 'wasClean': true })); |
dfae3209 | 39 | } |
0e4808bf | 40 | } |
2cccf753 | 41 | |
0e4808bf | 42 | send(data) { |
dfae3209 SR |
43 | if (this.protocol == 'base64') { |
44 | data = Base64.decode(data); | |
45 | } else { | |
46 | data = new Uint8Array(data); | |
47 | } | |
95632e41 | 48 | this._sendQueue.set(data, this.bufferedAmount); |
dfae3209 | 49 | this.bufferedAmount += data.length; |
0e4808bf | 50 | } |
2cccf753 | 51 | |
95632e41 SM |
52 | _getSentData() { |
53 | const res = new Uint8Array(this._sendQueue.buffer, 0, this.bufferedAmount); | |
dfae3209 SR |
54 | this.bufferedAmount = 0; |
55 | return res; | |
0e4808bf | 56 | } |
2cccf753 | 57 | |
0e4808bf | 58 | _open() { |
dfae3209 SR |
59 | this.readyState = FakeWebSocket.OPEN; |
60 | if (this.onopen) { | |
95632e41 | 61 | this.onopen(makeEvent('open')); |
2cccf753 | 62 | } |
0e4808bf | 63 | } |
2cccf753 | 64 | |
95632e41 | 65 | _receiveData(data) { |
70e67958 PO |
66 | // Break apart the data to expose bugs where we assume data is |
67 | // neatly packaged | |
68 | for (let i = 0;i < data.length;i++) { | |
69 | let buf = data.subarray(i, i+1); | |
95632e41 | 70 | this.onmessage(makeEvent("message", { 'data': buf })); |
70e67958 | 71 | } |
dfae3209 | 72 | } |
0e4808bf | 73 | } |
2cccf753 | 74 | |
dfae3209 SR |
75 | FakeWebSocket.OPEN = WebSocket.OPEN; |
76 | FakeWebSocket.CONNECTING = WebSocket.CONNECTING; | |
77 | FakeWebSocket.CLOSING = WebSocket.CLOSING; | |
78 | FakeWebSocket.CLOSED = WebSocket.CLOSED; | |
2cccf753 | 79 | |
95632e41 | 80 | FakeWebSocket._isFake = true; |
2cccf753 | 81 | |
651c23ec | 82 | FakeWebSocket.replace = () => { |
95632e41 SM |
83 | if (!WebSocket._isFake) { |
84 | const realVersion = WebSocket; | |
8727f598 | 85 | // eslint-disable-next-line no-global-assign |
dfae3209 | 86 | WebSocket = FakeWebSocket; |
95632e41 | 87 | FakeWebSocket._realVersion = realVersion; |
dfae3209 SR |
88 | } |
89 | }; | |
90 | ||
651c23ec | 91 | FakeWebSocket.restore = () => { |
95632e41 | 92 | if (WebSocket._isFake) { |
8727f598 | 93 | // eslint-disable-next-line no-global-assign |
95632e41 | 94 | WebSocket = WebSocket._realVersion; |
dfae3209 SR |
95 | } |
96 | }; |