(config)
| 112 | */ |
| 113 | class Protocol { |
| 114 | constructor(config) { |
| 115 | const onWrite = config.onWrite; |
| 116 | if (typeof onWrite !== 'function') |
| 117 | throw new Error('Missing onWrite function'); |
| 118 | this._onWrite = (data) => { onWrite(data); }; |
| 119 | |
| 120 | const onError = config.onError; |
| 121 | if (typeof onError !== 'function') |
| 122 | throw new Error('Missing onError function'); |
| 123 | this._onError = (err) => { onError(err); }; |
| 124 | |
| 125 | const debug = config.debug; |
| 126 | this._debug = (typeof debug === 'function' |
| 127 | ? (msg) => { debug(msg); } |
| 128 | : undefined); |
| 129 | |
| 130 | const onHeader = config.onHeader; |
| 131 | this._onHeader = (typeof onHeader === 'function' |
| 132 | ? (...args) => { onHeader(...args); } |
| 133 | : noop); |
| 134 | |
| 135 | const onPacket = config.onPacket; |
| 136 | this._onPacket = (typeof onPacket === 'function' |
| 137 | ? () => { onPacket(); } |
| 138 | : noop); |
| 139 | |
| 140 | let onHandshakeComplete = config.onHandshakeComplete; |
| 141 | if (typeof onHandshakeComplete !== 'function') |
| 142 | onHandshakeComplete = noop; |
| 143 | let firstHandshake; |
| 144 | this._onHandshakeComplete = (...args) => { |
| 145 | this._debug && this._debug('Handshake completed'); |
| 146 | if (firstHandshake === undefined) |
| 147 | firstHandshake = true; |
| 148 | else |
| 149 | firstHandshake = false; |
| 150 | |
| 151 | // Process packets queued during a rekey where necessary |
| 152 | const oldQueue = this._queue; |
| 153 | if (oldQueue) { |
| 154 | this._queue = undefined; |
| 155 | this._debug && this._debug( |
| 156 | `Draining outbound queue (${oldQueue.length}) ...` |
| 157 | ); |
| 158 | for (let i = 0; i < oldQueue.length; ++i) { |
| 159 | const data = oldQueue[i]; |
| 160 | // data === payload only |
| 161 | |
| 162 | // XXX: hacky |
| 163 | let finalized = this._packetRW.write.finalize(data); |
| 164 | if (finalized === data) { |
| 165 | const packet = this._cipher.allocPacket(data.length); |
| 166 | packet.set(data, 5); |
| 167 | finalized = packet; |
| 168 | } |
| 169 | |
| 170 | sendPacket(this, finalized); |
| 171 | } |
nothing calls this directly
no test coverage detected