javascript.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. var events = require("events"),
  2. util = require("../util");
  3. function Packet(type, size) {
  4. this.type = type;
  5. this.size = +size;
  6. }
  7. exports.name = "javascript";
  8. exports.debug_mode = false;
  9. function ReplyParser(options) {
  10. this.name = exports.name;
  11. this.options = options || { };
  12. this._buffer = null;
  13. this._offset = 0;
  14. this._encoding = "utf-8";
  15. this._debug_mode = options.debug_mode;
  16. this._reply_type = null;
  17. }
  18. util.inherits(ReplyParser, events.EventEmitter);
  19. exports.Parser = ReplyParser;
  20. function IncompleteReadBuffer(message) {
  21. this.name = "IncompleteReadBuffer";
  22. this.message = message;
  23. }
  24. util.inherits(IncompleteReadBuffer, Error);
  25. // Buffer.toString() is quite slow for small strings
  26. function small_toString(buf, start, end) {
  27. var tmp = "", i;
  28. for (i = start; i < end; i++) {
  29. tmp += String.fromCharCode(buf[i]);
  30. }
  31. return tmp;
  32. }
  33. ReplyParser.prototype._parseResult = function (type) {
  34. var start, end, offset, packetHeader;
  35. if (type === 43 || type === 45) { // + or -
  36. // up to the delimiter
  37. end = this._packetEndOffset() - 1;
  38. start = this._offset;
  39. // include the delimiter
  40. this._offset = end + 2;
  41. if (end > this._buffer.length) {
  42. this._offset = start;
  43. throw new IncompleteReadBuffer("Wait for more data.");
  44. }
  45. if (this.options.return_buffers) {
  46. return this._buffer.slice(start, end);
  47. } else {
  48. if (end - start < 65536) { // completely arbitrary
  49. return small_toString(this._buffer, start, end);
  50. } else {
  51. return this._buffer.toString(this._encoding, start, end);
  52. }
  53. }
  54. } else if (type === 58) { // :
  55. // up to the delimiter
  56. end = this._packetEndOffset() - 1;
  57. start = this._offset;
  58. // include the delimiter
  59. this._offset = end + 2;
  60. if (end > this._buffer.length) {
  61. this._offset = start;
  62. throw new IncompleteReadBuffer("Wait for more data.");
  63. }
  64. if (this.options.return_buffers) {
  65. return this._buffer.slice(start, end);
  66. }
  67. // return the coerced numeric value
  68. return +small_toString(this._buffer, start, end);
  69. } else if (type === 36) { // $
  70. // set a rewind point, as the packet could be larger than the
  71. // buffer in memory
  72. offset = this._offset - 1;
  73. packetHeader = new Packet(type, this.parseHeader());
  74. // packets with a size of -1 are considered null
  75. if (packetHeader.size === -1) {
  76. return undefined;
  77. }
  78. end = this._offset + packetHeader.size;
  79. start = this._offset;
  80. // set the offset to after the delimiter
  81. this._offset = end + 2;
  82. if (end > this._buffer.length) {
  83. this._offset = offset;
  84. throw new IncompleteReadBuffer("Wait for more data.");
  85. }
  86. if (this.options.return_buffers) {
  87. return this._buffer.slice(start, end);
  88. } else {
  89. return this._buffer.toString(this._encoding, start, end);
  90. }
  91. } else if (type === 42) { // *
  92. offset = this._offset;
  93. packetHeader = new Packet(type, this.parseHeader());
  94. if (packetHeader.size < 0) {
  95. return null;
  96. }
  97. if (packetHeader.size > this._bytesRemaining()) {
  98. this._offset = offset - 1;
  99. throw new IncompleteReadBuffer("Wait for more data.");
  100. }
  101. var reply = [ ];
  102. var ntype, i, res;
  103. offset = this._offset - 1;
  104. for (i = 0; i < packetHeader.size; i++) {
  105. ntype = this._buffer[this._offset++];
  106. if (this._offset > this._buffer.length) {
  107. throw new IncompleteReadBuffer("Wait for more data.");
  108. }
  109. res = this._parseResult(ntype);
  110. if (res === undefined) {
  111. res = null;
  112. }
  113. reply.push(res);
  114. }
  115. return reply;
  116. }
  117. };
  118. ReplyParser.prototype.execute = function (buffer) {
  119. this.append(buffer);
  120. var type, ret, offset;
  121. while (true) {
  122. offset = this._offset;
  123. try {
  124. // at least 4 bytes: :1\r\n
  125. if (this._bytesRemaining() < 4) {
  126. break;
  127. }
  128. type = this._buffer[this._offset++];
  129. if (type === 43) { // +
  130. ret = this._parseResult(type);
  131. if (ret === null) {
  132. break;
  133. }
  134. this.send_reply(ret);
  135. } else if (type === 45) { // -
  136. ret = this._parseResult(type);
  137. if (ret === null) {
  138. break;
  139. }
  140. this.send_error(ret);
  141. } else if (type === 58) { // :
  142. ret = this._parseResult(type);
  143. if (ret === null) {
  144. break;
  145. }
  146. this.send_reply(ret);
  147. } else if (type === 36) { // $
  148. ret = this._parseResult(type);
  149. if (ret === null) {
  150. break;
  151. }
  152. // check the state for what is the result of
  153. // a -1, set it back up for a null reply
  154. if (ret === undefined) {
  155. ret = null;
  156. }
  157. this.send_reply(ret);
  158. } else if (type === 42) { // *
  159. // set a rewind point. if a failure occurs,
  160. // wait for the next execute()/append() and try again
  161. offset = this._offset - 1;
  162. ret = this._parseResult(type);
  163. this.send_reply(ret);
  164. }
  165. } catch (err) {
  166. // catch the error (not enough data), rewind, and wait
  167. // for the next packet to appear
  168. if (! (err instanceof IncompleteReadBuffer)) {
  169. throw err;
  170. }
  171. this._offset = offset;
  172. break;
  173. }
  174. }
  175. };
  176. ReplyParser.prototype.append = function (newBuffer) {
  177. if (!newBuffer) {
  178. return;
  179. }
  180. // first run
  181. if (this._buffer === null) {
  182. this._buffer = newBuffer;
  183. return;
  184. }
  185. // out of data
  186. if (this._offset >= this._buffer.length) {
  187. this._buffer = newBuffer;
  188. this._offset = 0;
  189. return;
  190. }
  191. // very large packet
  192. // check for concat, if we have it, use it
  193. if (Buffer.concat !== undefined) {
  194. this._buffer = Buffer.concat([this._buffer.slice(this._offset), newBuffer]);
  195. } else {
  196. var remaining = this._bytesRemaining(),
  197. newLength = remaining + newBuffer.length,
  198. tmpBuffer = new Buffer(newLength);
  199. this._buffer.copy(tmpBuffer, 0, this._offset);
  200. newBuffer.copy(tmpBuffer, remaining, 0);
  201. this._buffer = tmpBuffer;
  202. }
  203. this._offset = 0;
  204. };
  205. ReplyParser.prototype.parseHeader = function () {
  206. var end = this._packetEndOffset(),
  207. value = small_toString(this._buffer, this._offset, end - 1);
  208. this._offset = end + 1;
  209. return value;
  210. };
  211. ReplyParser.prototype._packetEndOffset = function () {
  212. var offset = this._offset;
  213. while (this._buffer[offset] !== 0x0d && this._buffer[offset + 1] !== 0x0a) {
  214. offset++;
  215. if (offset >= this._buffer.length) {
  216. throw new IncompleteReadBuffer("didn't see LF after NL reading multi bulk count (" + offset + " => " + this._buffer.length + ", " + this._offset + ")");
  217. }
  218. }
  219. offset++;
  220. return offset;
  221. };
  222. ReplyParser.prototype._bytesRemaining = function () {
  223. return (this._buffer.length - this._offset) < 0 ? 0 : (this._buffer.length - this._offset);
  224. };
  225. ReplyParser.prototype.parser_error = function (message) {
  226. this.emit("error", message);
  227. };
  228. ReplyParser.prototype.send_error = function (reply) {
  229. this.emit("reply error", reply);
  230. };
  231. ReplyParser.prototype.send_reply = function (reply) {
  232. this.emit("reply", reply);
  233. };