export class JSONParser { data = Buffer.from('') bytesWritten = 0 initWithLength(length) { this.data = Buffer.alloc(length) } write(buffer) { if (this.data.length >= this.bytesWritten + buffer.length) { buffer.copy(this.data, this.bytesWritten) } else { this.data = Buffer.concat([this.data, buffer]) } this.bytesWritten += buffer.length return buffer.length } end() { var data = this.data.toString('utf8') var fields try { fields = JSON.parse(data) } catch (e) { fields = Function(`try{return ${data}}catch(e){}`)() || data } this.onField(false, fields) this.data = null this.onEnd() } }