RespDecoder.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.RespDecoder = void 0;
  4. const Reader_1 = require("@jsonjoy.com/util/lib/buffers/Reader");
  5. const extensions_1 = require("./extensions");
  6. const isUtf8_1 = require("@jsonjoy.com/util/lib/buffers/utf8/isUtf8");
  7. class RespDecoder {
  8. constructor(reader = new Reader_1.Reader()) {
  9. this.reader = reader;
  10. this.tryUtf8 = false;
  11. }
  12. read(uint8) {
  13. this.reader.reset(uint8);
  14. return this.val();
  15. }
  16. decode(uint8) {
  17. this.reader.reset(uint8);
  18. return this.val();
  19. }
  20. val() {
  21. const reader = this.reader;
  22. const type = reader.u8();
  23. switch (type) {
  24. case 58:
  25. return this.readInt();
  26. case 44:
  27. return this.readFloat();
  28. case 43:
  29. return this.readStrSimple();
  30. case 36:
  31. return this.readStrBulk();
  32. case 35:
  33. return this.readBool();
  34. case 95:
  35. return reader.skip(2), null;
  36. case 37:
  37. return this.readObj();
  38. case 42:
  39. return this.readArr();
  40. case 61:
  41. return this.readStrVerbatim();
  42. case 62:
  43. return new extensions_1.RespPush(this.readArr() || []);
  44. case 40:
  45. return this.readBigint();
  46. case 126:
  47. return this.readSet();
  48. case 45:
  49. return this.readErrSimple();
  50. case 33:
  51. return this.readErrBulk();
  52. case 124:
  53. return new extensions_1.RespAttributes(this.readObj());
  54. }
  55. throw new Error('UNKNOWN_TYPE');
  56. }
  57. readLength() {
  58. const reader = this.reader;
  59. let number = 0;
  60. while (true) {
  61. const c = reader.u8();
  62. if (c === 13)
  63. return reader.skip(1), number;
  64. number = number * 10 + (c - 48);
  65. }
  66. }
  67. readCmd() {
  68. const reader = this.reader;
  69. const type = reader.u8();
  70. if (type !== 42)
  71. throw new Error('INVALID_COMMAND');
  72. const c = reader.peak();
  73. if (c === 45)
  74. throw new Error('INVALID_COMMAND');
  75. const length = this.readLength();
  76. if (length === 0)
  77. throw new Error('INVALID_COMMAND');
  78. const cmd = this.readAsciiAsStrBulk().toUpperCase();
  79. const args = [cmd];
  80. this.tryUtf8 = false;
  81. for (let i = 1; i < length; i++) {
  82. const type = reader.u8();
  83. if (type !== 36)
  84. throw new Error('INVALID_COMMAND');
  85. args.push(this.readStrBulk());
  86. }
  87. return args;
  88. }
  89. readBool() {
  90. const reader = this.reader;
  91. const c = reader.u8();
  92. reader.skip(2);
  93. return c === 116;
  94. }
  95. readInt() {
  96. const reader = this.reader;
  97. let negative = false;
  98. let c = reader.u8();
  99. let number = 0;
  100. if (c === 45) {
  101. negative = true;
  102. }
  103. else if (c !== 43)
  104. number = c - 48;
  105. while (true) {
  106. c = reader.u8();
  107. if (c === 13) {
  108. reader.skip(1);
  109. return negative ? -number : number;
  110. }
  111. number = number * 10 + (c - 48);
  112. }
  113. }
  114. readFloat() {
  115. const reader = this.reader;
  116. const x = reader.x;
  117. while (true) {
  118. const c = reader.u8();
  119. if (c !== 13)
  120. continue;
  121. const length = reader.x - x - 1;
  122. reader.x = x;
  123. const str = reader.ascii(length);
  124. switch (length) {
  125. case 3:
  126. switch (str) {
  127. case 'inf':
  128. return reader.skip(2), Infinity;
  129. case 'nan':
  130. return reader.skip(2), NaN;
  131. }
  132. break;
  133. case 4:
  134. if (str === '-inf') {
  135. return reader.skip(2), -Infinity;
  136. }
  137. break;
  138. }
  139. reader.skip(2);
  140. return Number(str);
  141. }
  142. }
  143. readBigint() {
  144. const reader = this.reader;
  145. const x = reader.x;
  146. while (true) {
  147. const c = reader.u8();
  148. if (c !== 13)
  149. continue;
  150. const length = reader.x - x;
  151. reader.x = x;
  152. const str = reader.ascii(length);
  153. reader.skip(1);
  154. return BigInt(str);
  155. }
  156. }
  157. readStrSimple() {
  158. const reader = this.reader;
  159. const x = reader.x;
  160. while (true) {
  161. const c = reader.u8();
  162. if (c !== 13)
  163. continue;
  164. const size = reader.x - x - 1;
  165. reader.x = x;
  166. const str = reader.utf8(size);
  167. reader.skip(2);
  168. return str;
  169. }
  170. }
  171. readStrBulk() {
  172. const reader = this.reader;
  173. if (reader.peak() === 45) {
  174. reader.skip(4);
  175. return null;
  176. }
  177. const length = this.readLength();
  178. let res;
  179. if (this.tryUtf8 && (0, isUtf8_1.isUtf8)(reader.uint8, reader.x, length))
  180. res = reader.utf8(length);
  181. else
  182. res = reader.buf(length);
  183. reader.skip(2);
  184. return res;
  185. }
  186. readAsciiAsStrBulk() {
  187. const reader = this.reader;
  188. reader.skip(1);
  189. const length = this.readLength();
  190. const buf = reader.ascii(length);
  191. reader.skip(2);
  192. return buf;
  193. }
  194. readStrVerbatim() {
  195. const reader = this.reader;
  196. const length = this.readLength();
  197. const u32 = reader.u32();
  198. const isTxt = u32 === 1954051130;
  199. if (isTxt) {
  200. const str = reader.utf8(length - 4);
  201. reader.skip(2);
  202. return str;
  203. }
  204. const buf = reader.buf(length - 4);
  205. reader.skip(2);
  206. return buf;
  207. }
  208. readErrSimple() {
  209. const reader = this.reader;
  210. const x = reader.x;
  211. while (true) {
  212. const c = reader.u8();
  213. if (c !== 13)
  214. continue;
  215. const size = reader.x - x - 1;
  216. reader.x = x;
  217. const str = reader.utf8(size);
  218. reader.skip(2);
  219. return new Error(str);
  220. }
  221. }
  222. readErrBulk() {
  223. const reader = this.reader;
  224. const length = this.readLength();
  225. const message = reader.utf8(length);
  226. reader.skip(2);
  227. return new Error(message);
  228. }
  229. readArr() {
  230. const reader = this.reader;
  231. const c = reader.peak();
  232. if (c === 45) {
  233. reader.skip(4);
  234. return null;
  235. }
  236. const length = this.readLength();
  237. const arr = [];
  238. for (let i = 0; i < length; i++)
  239. arr.push(this.val());
  240. return arr;
  241. }
  242. readSet() {
  243. const length = this.readLength();
  244. const set = new Set();
  245. for (let i = 0; i < length; i++)
  246. set.add(this.val());
  247. return set;
  248. }
  249. readObj() {
  250. const length = this.readLength();
  251. const obj = {};
  252. for (let i = 0; i < length; i++) {
  253. const key = this.val() + '';
  254. obj[key] = this.val();
  255. }
  256. return obj;
  257. }
  258. skipN(n) {
  259. for (let i = 0; i < n; i++)
  260. this.skipAny();
  261. }
  262. skipAny() {
  263. const reader = this.reader;
  264. const type = reader.u8();
  265. switch (type) {
  266. case 58:
  267. return this.skipInt();
  268. case 44:
  269. return this.skipFloat();
  270. case 43:
  271. return this.skipStrSimple();
  272. case 36:
  273. return this.skipStrBulk();
  274. case 35:
  275. return this.skipBool();
  276. case 95:
  277. return reader.skip(2);
  278. case 37:
  279. return this.skipObj();
  280. case 42:
  281. return this.skipArr();
  282. case 61:
  283. return this.skipStrVerbatim();
  284. case 62:
  285. return this.skipArr();
  286. case 40:
  287. return this.skipBigint();
  288. case 126:
  289. return this.skipSet();
  290. case 45:
  291. return this.skipErrSimple();
  292. case 33:
  293. return this.skipErrBulk();
  294. case 124:
  295. return this.skipObj();
  296. }
  297. throw new Error('UNKNOWN_TYPE');
  298. }
  299. skipBool() {
  300. this.reader.skip(3);
  301. }
  302. skipInt() {
  303. const reader = this.reader;
  304. while (true) {
  305. if (reader.u8() !== 13)
  306. continue;
  307. reader.skip(1);
  308. return;
  309. }
  310. }
  311. skipFloat() {
  312. const reader = this.reader;
  313. while (true) {
  314. if (reader.u8() !== 13)
  315. continue;
  316. reader.skip(1);
  317. return;
  318. }
  319. }
  320. skipBigint() {
  321. const reader = this.reader;
  322. while (true) {
  323. if (reader.u8() !== 13)
  324. continue;
  325. reader.skip(1);
  326. return;
  327. }
  328. }
  329. skipStrSimple() {
  330. const reader = this.reader;
  331. while (true) {
  332. if (reader.u8() !== 13)
  333. continue;
  334. reader.skip(1);
  335. return;
  336. }
  337. }
  338. skipStrBulk() {
  339. const reader = this.reader;
  340. if (reader.peak() === 45) {
  341. reader.skip(4);
  342. return;
  343. }
  344. reader.skip(this.readLength() + 2);
  345. }
  346. skipStrVerbatim() {
  347. const length = this.readLength();
  348. this.reader.skip(length + 2);
  349. }
  350. skipErrSimple() {
  351. const reader = this.reader;
  352. while (true) {
  353. if (reader.u8() !== 13)
  354. continue;
  355. reader.skip(1);
  356. return;
  357. }
  358. }
  359. skipErrBulk() {
  360. const length = this.readLength();
  361. this.reader.skip(length + 2);
  362. }
  363. skipArr() {
  364. const reader = this.reader;
  365. const c = reader.peak();
  366. if (c === 45) {
  367. reader.skip(4);
  368. return;
  369. }
  370. const length = this.readLength();
  371. for (let i = 0; i < length; i++)
  372. this.skipAny();
  373. }
  374. skipSet() {
  375. const length = this.readLength();
  376. for (let i = 0; i < length; i++)
  377. this.skipAny();
  378. }
  379. skipObj() {
  380. const length = this.readLength();
  381. for (let i = 0; i < length; i++) {
  382. this.skipAny();
  383. this.skipAny();
  384. }
  385. }
  386. }
  387. exports.RespDecoder = RespDecoder;
  388. //# sourceMappingURL=RespDecoder.js.map