index.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const events_1 = require("events");
  4. // import * as streampkg from 'stream-pkg';
  5. // import * as profiler from 'v8-profiler';
  6. // import * as fs from 'fs';
  7. const ST_LENGTH = 1; // state that we should read length
  8. const ST_DATA = 2; // state that we should read data
  9. const ST_ERROR = 3; // state that something wrong has happened
  10. class Composer extends events_1.EventEmitter {
  11. constructor() {
  12. super();
  13. this.headbuf = new Buffer(2);
  14. this.headoffset = 0;
  15. this.state = ST_LENGTH;
  16. this.dataoffset = 0;
  17. this.buffoffset = 0;
  18. this.end = 0;
  19. this.length = 0;
  20. this.left = 0;
  21. this.hugePackMode = 0;
  22. this.hugePackModeFinished = 0;
  23. this.hugeBuf = Buffer.alloc(0);
  24. }
  25. feed(data) {
  26. if (!data) {
  27. return;
  28. }
  29. if (this.state === ST_ERROR) {
  30. throw new Error('compose in error state, reset it first');
  31. }
  32. this.dataoffset = 0;
  33. this.end = data.length;
  34. this.length = this.length || 0;
  35. // data.copy(this._buf, this.offset);
  36. // this._buf.write(data);
  37. // Buffer.concat([this._buf, data]);
  38. while (this.dataoffset < this.end) {
  39. if (this.state === ST_LENGTH) {
  40. if (this.headoffset && (this.end - this.dataoffset + this.headoffset) >= 2) {
  41. data.copy(this.headbuf, this.headoffset, this.dataoffset, this.dataoffset + 2 - this.headoffset);
  42. this.dataoffset += 2 - this.headoffset;
  43. this.headoffset = 0;
  44. this.left = this.headbuf.readUInt16LE(0);
  45. if (this.hugePackMode && this.left < 32701) {
  46. this.hugePackModeFinished = 1;
  47. this.hugePackMode = 0;
  48. }
  49. if (this.left > 32700) {
  50. this.left = 32700;
  51. this.hugePackMode = 1;
  52. this.hugePackModeFinished = 0;
  53. if (!this.hugeBuf) {
  54. this.hugeBuf = Buffer.alloc(0);
  55. }
  56. }
  57. this.state = ST_DATA;
  58. this.buf = new Buffer(this.left);
  59. }
  60. else {
  61. if (this.end - this.dataoffset >= 2) {
  62. this.left = data.readUInt16LE(this.dataoffset);
  63. if (this.hugePackMode && this.left < 32701) {
  64. this.hugePackModeFinished = 1;
  65. this.hugePackMode = 0;
  66. }
  67. if (this.left > 32700) {
  68. this.left = 32700;
  69. this.hugePackMode = 1;
  70. this.hugePackModeFinished = 0;
  71. if (!this.hugeBuf) {
  72. this.hugeBuf = Buffer.alloc(0);
  73. }
  74. }
  75. this.state = ST_DATA;
  76. this.dataoffset = this.dataoffset + 2;
  77. this.buf = new Buffer(this.left);
  78. }
  79. else {
  80. data.copy(this.headbuf, 0, this.dataoffset, this.end);
  81. this.headoffset = this.end - this.dataoffset;
  82. this.dataoffset += this.headoffset;
  83. }
  84. }
  85. }
  86. if (this.state === ST_DATA) {
  87. let length = Math.min(this.left, data.length - this.dataoffset);
  88. data.copy(this.buf, this.buf.length - this.left, this.dataoffset, this.dataoffset + length);
  89. this.left -= length;
  90. this.dataoffset += length;
  91. if ((this.hugePackMode || this.hugePackModeFinished) && this.left === 0) {
  92. if (this.hugePackModeFinished === 0) {
  93. this.hugeBuf = Buffer.concat([this.hugeBuf, this.buf]);
  94. this.left = 0;
  95. this.state = ST_LENGTH;
  96. delete this.buf;
  97. }
  98. if (this.hugePackModeFinished === 1) {
  99. this.hugeBuf = Buffer.concat([this.hugeBuf, this.buf]);
  100. this.emit('data', this.hugeBuf);
  101. this.hugePackModeFinished = 0;
  102. this.hugePackMode = 0;
  103. this.left = 0;
  104. this.state = ST_LENGTH;
  105. delete this.buf;
  106. delete this.hugeBuf;
  107. }
  108. }
  109. else {
  110. if (this.left === 0) {
  111. this.emit('data', this.buf);
  112. this.left = 0;
  113. this.state = ST_LENGTH;
  114. delete this.buf;
  115. }
  116. }
  117. // return;
  118. }
  119. if (this.state === ST_ERROR) {
  120. break;
  121. }
  122. }
  123. }
  124. compose(data) {
  125. if (data.length === 0) {
  126. throw new Error('data should not be empty.');
  127. }
  128. let d = Buffer.from(data);
  129. let totalLength = d.length;
  130. let offset = 0;
  131. let buf = new Buffer(d.length + 2 * (Math.floor(totalLength / 32700) + 1));
  132. let loop = 0;
  133. while (totalLength !== 0) {
  134. let writelength = totalLength;
  135. let currentLength = totalLength;
  136. if (writelength > 32700) {
  137. writelength = 32701;
  138. currentLength = 32700;
  139. }
  140. buf.writeUInt16LE(writelength, offset + 2 * (loop));
  141. d.copy(buf, 2 * (loop + 1) + offset, offset, offset + currentLength);
  142. offset = offset + currentLength;
  143. totalLength = totalLength - currentLength;
  144. loop = loop + 1;
  145. }
  146. return buf;
  147. }
  148. }
  149. exports.Composer = Composer;
  150. // profiler.startProfiling();
  151. // let comp = new Compose();
  152. // let buf = comp.compose(JSON.stringify({
  153. // 'id': 10,
  154. // 'msg': {
  155. // 'namespace': 'user', 'serverType': 'test', 'service': 'service', 'method': 'echo',
  156. // 'args': [{ 'index': 9, 'time': 1498919947021 }],
  157. // },
  158. // }));
  159. // console.time('1');
  160. // for (let i = 0; i < 1; i++) {
  161. // comp.compose('你好世界');
  162. // }
  163. // console.timeEnd('1');
  164. // comp.on('data', (d: Buffer) => {
  165. // let s = JSON.parse(d.toString());
  166. // console.log(s);
  167. // });
  168. // console.time('1');
  169. // for (let i = 0; i < 100; i++) {
  170. // buf = comp.compose(JSON.stringify({
  171. // 'id': 10,
  172. // 'msg': {
  173. // 'namespace': 'user', 'serverType': 'test', 'service': 'service', 'method': 'echo',
  174. // 'args': [{ 'index': 9, 'time': 1498919947021 }],
  175. // },
  176. // }));
  177. // comp.feed(buf);
  178. // comp.feed(buf.slice(0, 5));
  179. // comp.feed(buf.slice(5, buf.length));
  180. // }
  181. // console.timeEnd('1');
  182. // let sp = new streampkg();
  183. // buf = sp.compose('你好世界');
  184. // console.time('1');
  185. // for (let i = 0; i < 1000000; i++) {
  186. // sp.compose('你好世界');
  187. // }
  188. // console.timeEnd('1');
  189. // sp.on('data', (d) => {
  190. // // let s = d.toString();
  191. // //console.log(s);
  192. // });
  193. // console.time('1');
  194. // for (let i = 0; i < 1000000; i++) {
  195. // sp.feed(buf);
  196. // sp.feed(buf.slice(0, 5));
  197. // sp.feed(buf.slice(5, buf.length));
  198. // }
  199. // console.timeEnd('1');
  200. // profiler.stopProfiling().export((error, result) => {
  201. // fs.writeFileSync('client.cpuprofile', result);
  202. // console.log('clientside profile saved');
  203. // });