RespDecoder.js 11 KB

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