|
| 1 | +import { Socket } from 'net'; |
| 2 | +import { StringDecoder } from 'string_decoder'; |
| 3 | +import { |
| 4 | + CLOSE_EVENT, |
| 5 | + CONNECT_EVENT, |
| 6 | + DATA_EVENT, |
| 7 | + ERROR_EVENT, |
| 8 | + MESSAGE_EVENT, |
| 9 | +} from '../constants'; |
| 10 | +import { CorruptedPacketLengthException } from '../errors/corrupted-packet-length.exception'; |
| 11 | +import { InvalidJSONFormatException } from '../errors/invalid-json-format.exception'; |
| 12 | +import { NetSocketClosedException } from '../errors/net-socket-closed.exception'; |
| 13 | + |
| 14 | +export class JsonSocket { |
| 15 | + private contentLength: number | null = null; |
| 16 | + private isClosed = false; |
| 17 | + private buffer = ''; |
| 18 | + |
| 19 | + private readonly stringDecoder = new StringDecoder(); |
| 20 | + private readonly delimeter = '#'; |
| 21 | + |
| 22 | + public get netSocket() { |
| 23 | + return this.socket; |
| 24 | + } |
| 25 | + |
| 26 | + constructor(public readonly socket: Socket) { |
| 27 | + this.socket.on(DATA_EVENT, this.onData.bind(this)); |
| 28 | + this.socket.on(CONNECT_EVENT, () => (this.isClosed = false)); |
| 29 | + this.socket.on(CLOSE_EVENT, () => (this.isClosed = true)); |
| 30 | + this.socket.on(ERROR_EVENT, () => (this.isClosed = true)); |
| 31 | + } |
| 32 | + |
| 33 | + public connect(port: number, host: string) { |
| 34 | + this.socket.connect(port, host); |
| 35 | + return this; |
| 36 | + } |
| 37 | + |
| 38 | + public on(event: string, callback: (err?: any) => void) { |
| 39 | + this.socket.on(event, callback); |
| 40 | + return this; |
| 41 | + } |
| 42 | + |
| 43 | + public once(event: string, callback: (err?: any) => void) { |
| 44 | + this.socket.once(event, callback); |
| 45 | + return this; |
| 46 | + } |
| 47 | + |
| 48 | + public end() { |
| 49 | + this.socket.end(); |
| 50 | + return this; |
| 51 | + } |
| 52 | + |
| 53 | + public sendMessage(message: any, callback?: (err?: any) => void) { |
| 54 | + if (this.isClosed) { |
| 55 | + callback && callback(new NetSocketClosedException()); |
| 56 | + return; |
| 57 | + } |
| 58 | + this.socket.write(this.formatMessageData(message), 'utf-8', callback); |
| 59 | + } |
| 60 | + |
| 61 | + private onData(dataRaw: Buffer | string) { |
| 62 | + const data = Buffer.isBuffer(dataRaw) |
| 63 | + ? this.stringDecoder.write(dataRaw) |
| 64 | + : dataRaw; |
| 65 | + |
| 66 | + try { |
| 67 | + this.handleData(data); |
| 68 | + } catch (e) { |
| 69 | + this.socket.emit(ERROR_EVENT, e.message); |
| 70 | + this.socket.end(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private handleData(data: string) { |
| 75 | + this.buffer += data; |
| 76 | + |
| 77 | + if (this.contentLength == null) { |
| 78 | + const i = this.buffer.indexOf(this.delimeter); |
| 79 | + /** |
| 80 | + * Check if the buffer has the delimeter (#), |
| 81 | + * if not, the end of the buffer string might be in the middle of a content length string |
| 82 | + */ |
| 83 | + if (i !== -1) { |
| 84 | + const rawContentLength = this.buffer.substring(0, i); |
| 85 | + this.contentLength = parseInt(rawContentLength, 10); |
| 86 | + |
| 87 | + if (isNaN(this.contentLength)) { |
| 88 | + this.contentLength = null; |
| 89 | + this.buffer = ''; |
| 90 | + throw new CorruptedPacketLengthException(rawContentLength); |
| 91 | + } |
| 92 | + this.buffer = this.buffer.substring(i + 1); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if (this.contentLength !== null) { |
| 97 | + const length = this.buffer.length; |
| 98 | + |
| 99 | + if (length === this.contentLength) { |
| 100 | + this.handleMessage(this.buffer); |
| 101 | + } else if (length > this.contentLength) { |
| 102 | + const message = this.buffer.substring(0, this.contentLength); |
| 103 | + const rest = this.buffer.substring(this.contentLength); |
| 104 | + this.handleMessage(message); |
| 105 | + this.onData(rest); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private handleMessage(data: string) { |
| 111 | + this.contentLength = null; |
| 112 | + this.buffer = ''; |
| 113 | + |
| 114 | + let message: Record<string, unknown>; |
| 115 | + try { |
| 116 | + message = JSON.parse(data); |
| 117 | + } catch (e) { |
| 118 | + throw new InvalidJSONFormatException(e, data); |
| 119 | + } |
| 120 | + message = message || {}; |
| 121 | + this.socket.emit(MESSAGE_EVENT, message); |
| 122 | + } |
| 123 | + |
| 124 | + private formatMessageData(message: any) { |
| 125 | + const messageData = JSON.stringify(message); |
| 126 | + const length = messageData.length; |
| 127 | + const data = length + this.delimeter + messageData; |
| 128 | + return data; |
| 129 | + } |
| 130 | +} |
0 commit comments