|
| 1 | +import { EventEmitter } from "events"; |
| 2 | +import * as iconv from "iconv-lite"; |
| 3 | +import * as net from "net"; |
| 4 | +import { DOMParser } from "xmldom"; |
| 5 | + |
| 6 | +/** The encoding all XDebug messages are encoded with */ |
| 7 | +export const ENCODING = "iso-8859-1"; |
| 8 | + |
| 9 | +/** The two states the connection switches between */ |
| 10 | +enum ParsingState { |
| 11 | + DataLength, |
| 12 | + Response, |
| 13 | +} |
| 14 | + |
| 15 | +/** Wraps the NodeJS Socket and calls handleResponse() whenever a full response arrives */ |
| 16 | +export class DbgpConnection extends EventEmitter { |
| 17 | + private _socket: net.Socket; |
| 18 | + private _parsingState: ParsingState; |
| 19 | + private _chunksDataLength: number; |
| 20 | + private _chunks: Buffer[]; |
| 21 | + private _dataLength: number; |
| 22 | + |
| 23 | + constructor(socket: net.Socket) { |
| 24 | + super(); |
| 25 | + this._socket = socket; |
| 26 | + this._parsingState = ParsingState.DataLength; |
| 27 | + this._chunksDataLength = 0; |
| 28 | + this._chunks = []; |
| 29 | + socket.on("data", (data: Buffer) => this._handleDataChunk(data)); |
| 30 | + socket.on("error", (error: Error) => this.emit("error", error)); |
| 31 | + socket.on("close", () => this.emit("close")); |
| 32 | + } |
| 33 | + |
| 34 | + public write(command: Buffer): Promise<void> { |
| 35 | + return new Promise<void>((resolve, reject) => { |
| 36 | + if (this._socket.writable) { |
| 37 | + this._socket.write(command, () => { |
| 38 | + resolve(); |
| 39 | + }); |
| 40 | + } else { |
| 41 | + reject(new Error("socket not writable")); |
| 42 | + } |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + /** closes the underlying socket */ |
| 47 | + public close(): Promise<void> { |
| 48 | + return new Promise<void>((resolve, reject) => { |
| 49 | + this._socket.once("close", resolve); |
| 50 | + this._socket.end(); |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + private _handleDataChunk(data: Buffer) { |
| 55 | + // Anatomy of packets: [data length] [NULL] [xml] [NULL] |
| 56 | + // are we waiting for the data length or for the response? |
| 57 | + if (this._parsingState === ParsingState.DataLength) { |
| 58 | + // does data contain a NULL byte? |
| 59 | + const nullByteIndex = data.indexOf(0); |
| 60 | + if (nullByteIndex !== -1) { |
| 61 | + // YES -> we received the data length and are ready to receive the response |
| 62 | + const lastPiece = data.slice(0, nullByteIndex); |
| 63 | + this._chunks.push(lastPiece); |
| 64 | + this._chunksDataLength += lastPiece.length; |
| 65 | + this._dataLength = parseInt(iconv.decode(Buffer.concat(this._chunks, this._chunksDataLength), ENCODING), 10); |
| 66 | + // reset buffered chunks |
| 67 | + this._chunks = []; |
| 68 | + this._chunksDataLength = 0; |
| 69 | + // switch to response parsing state |
| 70 | + this._parsingState = ParsingState.Response; |
| 71 | + // if data contains more info (except the NULL byte) |
| 72 | + if (data.length > nullByteIndex + 1) { |
| 73 | + // handle the rest of the packet as part of the response |
| 74 | + const rest = data.slice(nullByteIndex + 1); |
| 75 | + this._handleDataChunk(rest); |
| 76 | + } |
| 77 | + } else { |
| 78 | + // NO -> this is only part of the data length. We wait for the next data event |
| 79 | + this._chunks.push(data); |
| 80 | + this._chunksDataLength += data.length; |
| 81 | + } |
| 82 | + } else if (this._parsingState === ParsingState.Response) { |
| 83 | + // does the new data together with the buffered data add up to the data length? |
| 84 | + if (this._chunksDataLength + data.length >= this._dataLength) { |
| 85 | + // YES -> we received the whole response |
| 86 | + // append the last piece of the response |
| 87 | + const lastResponsePiece = data.slice(0, this._dataLength - this._chunksDataLength); |
| 88 | + this._chunks.push(lastResponsePiece); |
| 89 | + this._chunksDataLength += data.length; |
| 90 | + const response = Buffer.concat(this._chunks, this._chunksDataLength); |
| 91 | + // call response handler |
| 92 | + const xml = iconv.decode(response, ENCODING); |
| 93 | + const parser = new DOMParser({ |
| 94 | + errorHandler: { |
| 95 | + error: (error) => { |
| 96 | + this.emit("error", error instanceof Error ? error : new Error(error)); |
| 97 | + }, |
| 98 | + fatalError: (error) => { |
| 99 | + this.emit("error", error instanceof Error ? error : new Error(error)); |
| 100 | + }, |
| 101 | + warning: (warning) => { |
| 102 | + this.emit("warning", warning); |
| 103 | + }, |
| 104 | + }, |
| 105 | + }); |
| 106 | + const document = parser.parseFromString(xml, "application/xml"); |
| 107 | + this.emit("message", document); |
| 108 | + // reset buffer |
| 109 | + this._chunks = []; |
| 110 | + this._chunksDataLength = 0; |
| 111 | + // switch to data length parsing state |
| 112 | + this._parsingState = ParsingState.DataLength; |
| 113 | + // if data contains more info (except the NULL byte) |
| 114 | + if (data.length > lastResponsePiece.length + 1) { |
| 115 | + // handle the rest of the packet (after the NULL byte) as data length |
| 116 | + const rest = data.slice(lastResponsePiece.length + 1); |
| 117 | + this._handleDataChunk(rest); |
| 118 | + } |
| 119 | + } else { |
| 120 | + // NO -> this is not the whole response yet. We buffer it and wait for the next data event. |
| 121 | + this._chunks.push(data); |
| 122 | + this._chunksDataLength += data.length; |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments