javascript.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*global Buffer require exports console setTimeout */
  2. // TODO - incorporate these V8 pro tips:
  3. // pre-allocate Arrays if length is known in advance
  4. // do not use delete
  5. // use numbers for parser state
  6. var events = require("events"),
  7. util = require("../util");
  8. exports.debug_mode = false;
  9. exports.name = "javascript";
  10. function RedisReplyParser(options) {
  11. this.name = exports.name;
  12. this.options = options || {};
  13. this.reset();
  14. events.EventEmitter.call(this);
  15. }
  16. util.inherits(RedisReplyParser, events.EventEmitter);
  17. exports.Parser = RedisReplyParser;
  18. // Buffer.toString() is quite slow for small strings
  19. function small_toString(buf, len) {
  20. var tmp = "", i;
  21. for (i = 0; i < len; i += 1) {
  22. tmp += String.fromCharCode(buf[i]);
  23. }
  24. return tmp;
  25. }
  26. // Reset parser to it's original state.
  27. RedisReplyParser.prototype.reset = function () {
  28. this.return_buffer = new Buffer(16384); // for holding replies, might grow
  29. this.return_string = "";
  30. this.tmp_string = ""; // for holding size fields
  31. this.multi_bulk_length = 0;
  32. this.multi_bulk_replies = null;
  33. this.multi_bulk_pos = 0;
  34. this.multi_bulk_nested_length = 0;
  35. this.multi_bulk_nested_replies = null;
  36. this.states = {
  37. TYPE: 1,
  38. SINGLE_LINE: 2,
  39. MULTI_BULK_COUNT: 3,
  40. INTEGER_LINE: 4,
  41. BULK_LENGTH: 5,
  42. ERROR_LINE: 6,
  43. BULK_DATA: 7,
  44. UNKNOWN_TYPE: 8,
  45. FINAL_CR: 9,
  46. FINAL_LF: 10,
  47. MULTI_BULK_COUNT_LF: 11,
  48. BULK_LF: 12
  49. };
  50. this.state = this.states.TYPE;
  51. };
  52. RedisReplyParser.prototype.parser_error = function (message) {
  53. this.emit("error", message);
  54. this.reset();
  55. };
  56. RedisReplyParser.prototype.execute = function (incoming_buf) {
  57. var pos = 0, bd_tmp, bd_str, i, il, states = this.states;
  58. //, state_times = {}, start_execute = new Date(), start_switch, end_switch, old_state;
  59. //start_switch = new Date();
  60. while (pos < incoming_buf.length) {
  61. // old_state = this.state;
  62. // console.log("execute: " + this.state + ", " + pos + "/" + incoming_buf.length + ", " + String.fromCharCode(incoming_buf[pos]));
  63. switch (this.state) {
  64. case 1: // states.TYPE
  65. this.type = incoming_buf[pos];
  66. pos += 1;
  67. switch (this.type) {
  68. case 43: // +
  69. this.state = states.SINGLE_LINE;
  70. this.return_buffer.end = 0;
  71. this.return_string = "";
  72. break;
  73. case 42: // *
  74. this.state = states.MULTI_BULK_COUNT;
  75. this.tmp_string = "";
  76. break;
  77. case 58: // :
  78. this.state = states.INTEGER_LINE;
  79. this.return_buffer.end = 0;
  80. this.return_string = "";
  81. break;
  82. case 36: // $
  83. this.state = states.BULK_LENGTH;
  84. this.tmp_string = "";
  85. break;
  86. case 45: // -
  87. this.state = states.ERROR_LINE;
  88. this.return_buffer.end = 0;
  89. this.return_string = "";
  90. break;
  91. default:
  92. this.state = states.UNKNOWN_TYPE;
  93. }
  94. break;
  95. case 4: // states.INTEGER_LINE
  96. if (incoming_buf[pos] === 13) {
  97. this.send_reply(+small_toString(this.return_buffer, this.return_buffer.end));
  98. this.state = states.FINAL_LF;
  99. } else {
  100. this.return_buffer[this.return_buffer.end] = incoming_buf[pos];
  101. this.return_buffer.end += 1;
  102. }
  103. pos += 1;
  104. break;
  105. case 6: // states.ERROR_LINE
  106. if (incoming_buf[pos] === 13) {
  107. this.send_error(this.return_buffer.toString("ascii", 0, this.return_buffer.end));
  108. this.state = states.FINAL_LF;
  109. } else {
  110. this.return_buffer[this.return_buffer.end] = incoming_buf[pos];
  111. this.return_buffer.end += 1;
  112. }
  113. pos += 1;
  114. break;
  115. case 2: // states.SINGLE_LINE
  116. if (incoming_buf[pos] === 13) {
  117. this.send_reply(this.return_string);
  118. this.state = states.FINAL_LF;
  119. } else {
  120. this.return_string += String.fromCharCode(incoming_buf[pos]);
  121. }
  122. pos += 1;
  123. break;
  124. case 3: // states.MULTI_BULK_COUNT
  125. if (incoming_buf[pos] === 13) { // \r
  126. this.state = states.MULTI_BULK_COUNT_LF;
  127. } else {
  128. this.tmp_string += String.fromCharCode(incoming_buf[pos]);
  129. }
  130. pos += 1;
  131. break;
  132. case 11: // states.MULTI_BULK_COUNT_LF
  133. if (incoming_buf[pos] === 10) { // \n
  134. if (this.multi_bulk_length) { // nested multi-bulk
  135. this.multi_bulk_nested_length = this.multi_bulk_length;
  136. this.multi_bulk_nested_replies = this.multi_bulk_replies;
  137. this.multi_bulk_nested_pos = this.multi_bulk_pos;
  138. }
  139. this.multi_bulk_length = +this.tmp_string;
  140. this.multi_bulk_pos = 0;
  141. this.state = states.TYPE;
  142. if (this.multi_bulk_length < 0) {
  143. this.send_reply(null);
  144. this.multi_bulk_length = 0;
  145. } else if (this.multi_bulk_length === 0) {
  146. this.multi_bulk_pos = 0;
  147. this.multi_bulk_replies = null;
  148. this.send_reply([]);
  149. } else {
  150. this.multi_bulk_replies = new Array(this.multi_bulk_length);
  151. }
  152. } else {
  153. this.parser_error(new Error("didn't see LF after NL reading multi bulk count"));
  154. return;
  155. }
  156. pos += 1;
  157. break;
  158. case 5: // states.BULK_LENGTH
  159. if (incoming_buf[pos] === 13) { // \r
  160. this.state = states.BULK_LF;
  161. } else {
  162. this.tmp_string += String.fromCharCode(incoming_buf[pos]);
  163. }
  164. pos += 1;
  165. break;
  166. case 12: // states.BULK_LF
  167. if (incoming_buf[pos] === 10) { // \n
  168. this.bulk_length = +this.tmp_string;
  169. if (this.bulk_length === -1) {
  170. this.send_reply(null);
  171. this.state = states.TYPE;
  172. } else if (this.bulk_length === 0) {
  173. this.send_reply(new Buffer(""));
  174. this.state = states.FINAL_CR;
  175. } else {
  176. this.state = states.BULK_DATA;
  177. if (this.bulk_length > this.return_buffer.length) {
  178. if (exports.debug_mode) {
  179. console.log("Growing return_buffer from " + this.return_buffer.length + " to " + this.bulk_length);
  180. }
  181. this.return_buffer = new Buffer(this.bulk_length);
  182. }
  183. this.return_buffer.end = 0;
  184. }
  185. } else {
  186. this.parser_error(new Error("didn't see LF after NL while reading bulk length"));
  187. return;
  188. }
  189. pos += 1;
  190. break;
  191. case 7: // states.BULK_DATA
  192. this.return_buffer[this.return_buffer.end] = incoming_buf[pos];
  193. this.return_buffer.end += 1;
  194. pos += 1;
  195. if (this.return_buffer.end === this.bulk_length) {
  196. bd_tmp = new Buffer(this.bulk_length);
  197. // When the response is small, Buffer.copy() is a lot slower.
  198. if (this.bulk_length > 10) {
  199. this.return_buffer.copy(bd_tmp, 0, 0, this.bulk_length);
  200. } else {
  201. for (i = 0, il = this.bulk_length; i < il; i += 1) {
  202. bd_tmp[i] = this.return_buffer[i];
  203. }
  204. }
  205. this.send_reply(bd_tmp);
  206. this.state = states.FINAL_CR;
  207. }
  208. break;
  209. case 9: // states.FINAL_CR
  210. if (incoming_buf[pos] === 13) { // \r
  211. this.state = states.FINAL_LF;
  212. pos += 1;
  213. } else {
  214. this.parser_error(new Error("saw " + incoming_buf[pos] + " when expecting final CR"));
  215. return;
  216. }
  217. break;
  218. case 10: // states.FINAL_LF
  219. if (incoming_buf[pos] === 10) { // \n
  220. this.state = states.TYPE;
  221. pos += 1;
  222. } else {
  223. this.parser_error(new Error("saw " + incoming_buf[pos] + " when expecting final LF"));
  224. return;
  225. }
  226. break;
  227. default:
  228. this.parser_error(new Error("invalid state " + this.state));
  229. }
  230. // end_switch = new Date();
  231. // if (state_times[old_state] === undefined) {
  232. // state_times[old_state] = 0;
  233. // }
  234. // state_times[old_state] += (end_switch - start_switch);
  235. // start_switch = end_switch;
  236. }
  237. // console.log("execute ran for " + (Date.now() - start_execute) + " ms, on " + incoming_buf.length + " Bytes. ");
  238. // Object.keys(state_times).forEach(function (state) {
  239. // console.log(" " + state + ": " + state_times[state]);
  240. // });
  241. };
  242. RedisReplyParser.prototype.send_error = function (reply) {
  243. if (this.multi_bulk_length > 0 || this.multi_bulk_nested_length > 0) {
  244. // TODO - can this happen? Seems like maybe not.
  245. this.add_multi_bulk_reply(reply);
  246. } else {
  247. this.emit("reply error", reply);
  248. }
  249. };
  250. RedisReplyParser.prototype.send_reply = function (reply) {
  251. if (this.multi_bulk_length > 0 || this.multi_bulk_nested_length > 0) {
  252. if (!this.options.return_buffers && Buffer.isBuffer(reply)) {
  253. this.add_multi_bulk_reply(reply.toString("utf8"));
  254. } else {
  255. this.add_multi_bulk_reply(reply);
  256. }
  257. } else {
  258. if (!this.options.return_buffers && Buffer.isBuffer(reply)) {
  259. this.emit("reply", reply.toString("utf8"));
  260. } else {
  261. this.emit("reply", reply);
  262. }
  263. }
  264. };
  265. RedisReplyParser.prototype.add_multi_bulk_reply = function (reply) {
  266. if (this.multi_bulk_replies) {
  267. this.multi_bulk_replies[this.multi_bulk_pos] = reply;
  268. this.multi_bulk_pos += 1;
  269. if (this.multi_bulk_pos < this.multi_bulk_length) {
  270. return;
  271. }
  272. } else {
  273. this.multi_bulk_replies = reply;
  274. }
  275. if (this.multi_bulk_nested_length > 0) {
  276. this.multi_bulk_nested_replies[this.multi_bulk_nested_pos] = this.multi_bulk_replies;
  277. this.multi_bulk_nested_pos += 1;
  278. this.multi_bulk_length = 0;
  279. this.multi_bulk_replies = null;
  280. this.multi_bulk_pos = 0;
  281. if (this.multi_bulk_nested_length === this.multi_bulk_nested_pos) {
  282. this.emit("reply", this.multi_bulk_nested_replies);
  283. this.multi_bulk_nested_length = 0;
  284. this.multi_bulk_nested_pos = 0;
  285. this.multi_bulk_nested_replies = null;
  286. }
  287. } else {
  288. this.emit("reply", this.multi_bulk_replies);
  289. this.multi_bulk_length = 0;
  290. this.multi_bulk_replies = null;
  291. this.multi_bulk_pos = 0;
  292. }
  293. };