| 90 | } |
| 91 | |
| 92 | class Peer { |
| 93 | |
| 94 | constructor(serverConnection, peerId) { |
| 95 | this._server = serverConnection; |
| 96 | this._peerId = peerId; |
| 97 | this._filesQueue = []; |
| 98 | this._busy = false; |
| 99 | } |
| 100 | |
| 101 | sendJSON(message) { |
| 102 | this._send(JSON.stringify(message)); |
| 103 | } |
| 104 | |
| 105 | sendFiles(files) { |
| 106 | for (let i = 0; i < files.length; i++) { |
| 107 | this._filesQueue.push(files[i]); |
| 108 | } |
| 109 | if (this._busy) return; |
| 110 | this._dequeueFile(); |
| 111 | } |
| 112 | |
| 113 | _dequeueFile() { |
| 114 | if (!this._filesQueue.length) return; |
| 115 | this._busy = true; |
| 116 | const file = this._filesQueue.shift(); |
| 117 | this._sendFile(file); |
| 118 | } |
| 119 | |
| 120 | _sendFile(file) { |
| 121 | this.sendJSON({ |
| 122 | type: 'header', |
| 123 | name: file.name, |
| 124 | mime: file.type, |
| 125 | size: file.size |
| 126 | }); |
| 127 | this._chunker = new FileChunker(file, |
| 128 | chunk => this._send(chunk), |
| 129 | offset => this._onPartitionEnd(offset)); |
| 130 | this._chunker.nextPartition(); |
| 131 | } |
| 132 | |
| 133 | _onPartitionEnd(offset) { |
| 134 | this.sendJSON({ type: 'partition', offset: offset }); |
| 135 | } |
| 136 | |
| 137 | _onReceivedPartitionEnd(offset) { |
| 138 | this.sendJSON({ type: 'partition-received', offset: offset }); |
| 139 | } |
| 140 | |
| 141 | _sendNextPartition() { |
| 142 | if (!this._chunker || this._chunker.isFileEnd()) return; |
| 143 | this._chunker.nextPartition(); |
| 144 | } |
| 145 | |
| 146 | _sendProgress(progress) { |
| 147 | this.sendJSON({ type: 'progress', progress: progress }); |
| 148 | } |
| 149 |
nothing calls this directly
no outgoing calls
no test coverage detected