UNPKG

@u4/adbkit-monkey

Version:

A Node.js interface to the Android monkey tool.

63 lines (62 loc) 2.22 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /* * decaffeinate suggestions: * DS002: Fix invalid constructor * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ const events_1 = require("events"); const reply_1 = __importDefault(require("./reply")); class Parser extends events_1.EventEmitter { constructor() { super(); this.column = 0; this.buffer = Buffer.from(''); } parse(chunk) { this.buffer = Buffer.concat([this.buffer, chunk]); while (this.column < this.buffer.length) { if (this.buffer[this.column] === 0x0a) { this._parseLine(this.buffer.slice(0, this.column)); this.buffer = this.buffer.slice(this.column + 1); this.column = 0; } this.column += 1; } if (this.buffer.length) { this.emit('wait'); } else { this.emit('drain'); } } _parseLine(line) { switch (line[0]) { case 0x4f: // 'O' if (line.length === 2) { // 'OK' this.emit('reply', new reply_1.default(reply_1.default.OK, null)); } else { // 'OK:' this.emit('reply', new reply_1.default(reply_1.default.OK, line.toString('ascii', 3))); } break; case 0x45: // 'E' if (line.length === 5) { // 'ERROR' this.emit('reply', new reply_1.default(reply_1.default.ERROR, null)); } else { // 'ERROR:' this.emit('reply', new reply_1.default(reply_1.default.ERROR, line.toString('ascii', 6))); } break; default: this._complain(line); } } _complain(line) { this.emit('error', new SyntaxError(`Unparseable line '${line}'`)); } } exports.default = Parser;