| 179 | } |
| 180 | |
| 181 | _handleChunk(chunk) { |
| 182 | this._unprocessed = Buffer.concat([this._unprocessed, chunk]); |
| 183 | |
| 184 | while (this._unprocessed.length > 2) { |
| 185 | const { |
| 186 | closed, |
| 187 | payload: payloadBuffer, |
| 188 | rest, |
| 189 | } = decodeFrameHybi17(this._unprocessed); |
| 190 | this._unprocessed = rest; |
| 191 | |
| 192 | if (closed) { |
| 193 | this.reset(); |
| 194 | return; |
| 195 | } |
| 196 | if (payloadBuffer === null || payloadBuffer.length === 0) break; |
| 197 | |
| 198 | const payloadStr = payloadBuffer.toString(); |
| 199 | debuglog('< %s', payloadStr); |
| 200 | const lastChar = payloadStr[payloadStr.length - 1]; |
| 201 | if (payloadStr[0] !== '{' || lastChar !== '}') { |
| 202 | throw new ERR_DEBUGGER_ERROR(`Payload does not look like JSON: ${payloadStr}`); |
| 203 | } |
| 204 | let payload; |
| 205 | try { |
| 206 | payload = JSONParse(payloadStr); |
| 207 | } catch (parseError) { |
| 208 | parseError.string = payloadStr; |
| 209 | throw parseError; |
| 210 | } |
| 211 | |
| 212 | const { id, method, params, result, error } = payload; |
| 213 | if (id) { |
| 214 | const handler = this._pending[id]; |
| 215 | if (handler) { |
| 216 | delete this._pending[id]; |
| 217 | handler(error, result); |
| 218 | } |
| 219 | } else if (method) { |
| 220 | this.emit('debugEvent', method, params); |
| 221 | this.emit(method, params); |
| 222 | } else { |
| 223 | throw new ERR_DEBUGGER_ERROR(`Unsupported response: ${payloadStr}`); |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | reset() { |
| 229 | const pending = this._pending; |