JsonDecoder.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.JsonDecoder = void 0;
  4. const decodeUtf8_1 = require("@jsonjoy.com/util/lib/buffers/utf8/decodeUtf8");
  5. const Reader_1 = require("@jsonjoy.com/util/lib/buffers/Reader");
  6. const fromBase64Bin_1 = require("@jsonjoy.com/base64/lib/fromBase64Bin");
  7. const util_1 = require("./util");
  8. const REGEX_REPLACE_ESCAPED_CHARS = /\\(b|f|n|r|t|"|\/|\\)/g;
  9. const escapedCharReplacer = (char) => {
  10. switch (char) {
  11. case '\\b':
  12. return '\b';
  13. case '\\f':
  14. return '\f';
  15. case '\\n':
  16. return '\n';
  17. case '\\r':
  18. return '\r';
  19. case '\\t':
  20. return '\t';
  21. case '\\"':
  22. return '"';
  23. case '\\/':
  24. return '/';
  25. case '\\\\':
  26. return '\\';
  27. }
  28. return char;
  29. };
  30. const hasBinaryPrefix = (u8, x) => u8[x] === 0x64 &&
  31. u8[x + 1] === 0x61 &&
  32. u8[x + 2] === 0x74 &&
  33. u8[x + 3] === 0x61 &&
  34. u8[x + 4] === 0x3a &&
  35. u8[x + 5] === 0x61 &&
  36. u8[x + 6] === 0x70 &&
  37. u8[x + 7] === 0x70 &&
  38. u8[x + 8] === 0x6c &&
  39. u8[x + 9] === 0x69 &&
  40. u8[x + 10] === 0x63 &&
  41. u8[x + 11] === 0x61 &&
  42. u8[x + 12] === 0x74 &&
  43. u8[x + 13] === 0x69 &&
  44. u8[x + 14] === 0x6f &&
  45. u8[x + 15] === 0x6e &&
  46. u8[x + 16] === 0x2f &&
  47. u8[x + 17] === 0x6f &&
  48. u8[x + 18] === 0x63 &&
  49. u8[x + 19] === 0x74 &&
  50. u8[x + 20] === 0x65 &&
  51. u8[x + 21] === 0x74 &&
  52. u8[x + 22] === 0x2d &&
  53. u8[x + 23] === 0x73 &&
  54. u8[x + 24] === 0x74 &&
  55. u8[x + 25] === 0x72 &&
  56. u8[x + 26] === 0x65 &&
  57. u8[x + 27] === 0x61 &&
  58. u8[x + 28] === 0x6d &&
  59. u8[x + 29] === 0x3b &&
  60. u8[x + 30] === 0x62 &&
  61. u8[x + 31] === 0x61 &&
  62. u8[x + 32] === 0x73 &&
  63. u8[x + 33] === 0x65 &&
  64. u8[x + 34] === 0x36 &&
  65. u8[x + 35] === 0x34 &&
  66. u8[x + 36] === 0x2c;
  67. const isUndefined = (u8, x) => u8[x++] === 0x61 &&
  68. u8[x++] === 0x74 &&
  69. u8[x++] === 0x61 &&
  70. u8[x++] === 0x3a &&
  71. u8[x++] === 0x61 &&
  72. u8[x++] === 0x70 &&
  73. u8[x++] === 0x70 &&
  74. u8[x++] === 0x6c &&
  75. u8[x++] === 0x69 &&
  76. u8[x++] === 0x63 &&
  77. u8[x++] === 0x61 &&
  78. u8[x++] === 0x74 &&
  79. u8[x++] === 0x69 &&
  80. u8[x++] === 0x6f &&
  81. u8[x++] === 0x6e &&
  82. u8[x++] === 0x2f &&
  83. u8[x++] === 0x63 &&
  84. u8[x++] === 0x62 &&
  85. u8[x++] === 0x6f &&
  86. u8[x++] === 0x72 &&
  87. u8[x++] === 0x2c &&
  88. u8[x++] === 0x62 &&
  89. u8[x++] === 0x61 &&
  90. u8[x++] === 0x73 &&
  91. u8[x++] === 0x65 &&
  92. u8[x++] === 0x36 &&
  93. u8[x++] === 0x34 &&
  94. u8[x++] === 0x3b &&
  95. u8[x++] === 0x39 &&
  96. u8[x++] === 0x77 &&
  97. u8[x++] === 0x3d &&
  98. u8[x++] === 0x3d &&
  99. u8[x++] === 0x22;
  100. const fromCharCode = String.fromCharCode;
  101. const readShortUtf8StrAndUnescape = (reader) => {
  102. const buf = reader.uint8;
  103. const len = buf.length;
  104. const points = [];
  105. let x = reader.x;
  106. let prev = 0;
  107. while (x < len) {
  108. let code = buf[x++];
  109. if ((code & 0x80) === 0) {
  110. if (prev === 92) {
  111. switch (code) {
  112. case 98:
  113. code = 8;
  114. break;
  115. case 102:
  116. code = 12;
  117. break;
  118. case 110:
  119. code = 10;
  120. break;
  121. case 114:
  122. code = 13;
  123. break;
  124. case 116:
  125. code = 9;
  126. break;
  127. case 34:
  128. code = 34;
  129. break;
  130. case 47:
  131. code = 47;
  132. break;
  133. case 92:
  134. code = 92;
  135. break;
  136. default:
  137. throw new Error('Invalid JSON');
  138. }
  139. prev = 0;
  140. }
  141. else {
  142. if (code === 34)
  143. break;
  144. prev = code;
  145. if (prev === 92)
  146. continue;
  147. }
  148. }
  149. else {
  150. const octet2 = buf[x++] & 0x3f;
  151. if ((code & 0xe0) === 0xc0) {
  152. code = ((code & 0x1f) << 6) | octet2;
  153. }
  154. else {
  155. const octet3 = buf[x++] & 0x3f;
  156. if ((code & 0xf0) === 0xe0) {
  157. code = ((code & 0x1f) << 12) | (octet2 << 6) | octet3;
  158. }
  159. else {
  160. if ((code & 0xf8) === 0xf0) {
  161. const octet4 = buf[x++] & 0x3f;
  162. let unit = ((code & 0x07) << 0x12) | (octet2 << 0x0c) | (octet3 << 0x06) | octet4;
  163. if (unit > 0xffff) {
  164. unit -= 0x10000;
  165. const unit0 = ((unit >>> 10) & 0x3ff) | 0xd800;
  166. unit = 0xdc00 | (unit & 0x3ff);
  167. points.push(unit0);
  168. code = unit;
  169. }
  170. else {
  171. code = unit;
  172. }
  173. }
  174. }
  175. }
  176. }
  177. points.push(code);
  178. }
  179. reader.x = x;
  180. return fromCharCode.apply(String, points);
  181. };
  182. class JsonDecoder {
  183. constructor() {
  184. this.reader = new Reader_1.Reader();
  185. }
  186. read(uint8) {
  187. this.reader.reset(uint8);
  188. return this.readAny();
  189. }
  190. decode(uint8) {
  191. this.reader.reset(uint8);
  192. return this.readAny();
  193. }
  194. readAny() {
  195. this.skipWhitespace();
  196. const reader = this.reader;
  197. const x = reader.x;
  198. const uint8 = reader.uint8;
  199. const char = uint8[x];
  200. switch (char) {
  201. case 34: {
  202. if (uint8[x + 1] === 0x64) {
  203. const bin = this.tryReadBin();
  204. if (bin)
  205. return bin;
  206. if (isUndefined(uint8, x + 2)) {
  207. reader.x = x + 35;
  208. return undefined;
  209. }
  210. }
  211. return this.readStr();
  212. }
  213. case 91:
  214. return this.readArr();
  215. case 102:
  216. return this.readFalse();
  217. case 110:
  218. return this.readNull();
  219. case 116:
  220. return this.readTrue();
  221. case 123:
  222. return this.readObj();
  223. default:
  224. if ((char >= 48 && char <= 57) || char === 45)
  225. return this.readNum();
  226. throw new Error('Invalid JSON');
  227. }
  228. }
  229. skipWhitespace() {
  230. const reader = this.reader;
  231. const uint8 = reader.uint8;
  232. let x = reader.x;
  233. let char = 0;
  234. while (true) {
  235. char = uint8[x];
  236. switch (char) {
  237. case 32:
  238. case 9:
  239. case 10:
  240. case 13:
  241. x++;
  242. continue;
  243. default:
  244. reader.x = x;
  245. return;
  246. }
  247. }
  248. }
  249. readNull() {
  250. if (this.reader.u32() !== 0x6e756c6c)
  251. throw new Error('Invalid JSON');
  252. return null;
  253. }
  254. readTrue() {
  255. if (this.reader.u32() !== 0x74727565)
  256. throw new Error('Invalid JSON');
  257. return true;
  258. }
  259. readFalse() {
  260. const reader = this.reader;
  261. if (reader.u8() !== 0x66 || reader.u32() !== 0x616c7365)
  262. throw new Error('Invalid JSON');
  263. return false;
  264. }
  265. readBool() {
  266. const reader = this.reader;
  267. switch (reader.uint8[reader.x]) {
  268. case 102:
  269. return this.readFalse();
  270. case 116:
  271. return this.readTrue();
  272. default:
  273. throw new Error('Invalid JSON');
  274. }
  275. }
  276. readNum() {
  277. const reader = this.reader;
  278. const uint8 = reader.uint8;
  279. let x = reader.x;
  280. let c = uint8[x++];
  281. const c1 = c;
  282. c = uint8[x++];
  283. if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
  284. reader.x = x - 1;
  285. const num = +fromCharCode(c1);
  286. if (num !== num)
  287. throw new Error('Invalid JSON');
  288. return num;
  289. }
  290. const c2 = c;
  291. c = uint8[x++];
  292. if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
  293. reader.x = x - 1;
  294. const num = +fromCharCode(c1, c2);
  295. if (num !== num)
  296. throw new Error('Invalid JSON');
  297. return num;
  298. }
  299. const c3 = c;
  300. c = uint8[x++];
  301. if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
  302. reader.x = x - 1;
  303. const num = +fromCharCode(c1, c2, c3);
  304. if (num !== num)
  305. throw new Error('Invalid JSON');
  306. return num;
  307. }
  308. const c4 = c;
  309. c = uint8[x++];
  310. if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
  311. reader.x = x - 1;
  312. const num = +fromCharCode(c1, c2, c3, c4);
  313. if (num !== num)
  314. throw new Error('Invalid JSON');
  315. return num;
  316. }
  317. const c5 = c;
  318. c = uint8[x++];
  319. if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
  320. reader.x = x - 1;
  321. const num = +fromCharCode(c1, c2, c3, c4, c5);
  322. if (num !== num)
  323. throw new Error('Invalid JSON');
  324. return num;
  325. }
  326. const c6 = c;
  327. c = uint8[x++];
  328. if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
  329. reader.x = x - 1;
  330. const num = +fromCharCode(c1, c2, c3, c4, c5, c6);
  331. if (num !== num)
  332. throw new Error('Invalid JSON');
  333. return num;
  334. }
  335. const c7 = c;
  336. c = uint8[x++];
  337. if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
  338. reader.x = x - 1;
  339. const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7);
  340. if (num !== num)
  341. throw new Error('Invalid JSON');
  342. return num;
  343. }
  344. const c8 = c;
  345. c = uint8[x++];
  346. if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
  347. reader.x = x - 1;
  348. const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8);
  349. if (num !== num)
  350. throw new Error('Invalid JSON');
  351. return num;
  352. }
  353. const c9 = c;
  354. c = uint8[x++];
  355. if (!c || ((c < 45 || c > 57) && c !== 69 && c !== 101)) {
  356. reader.x = x - 1;
  357. const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8, c9);
  358. if (num !== num)
  359. throw new Error('Invalid JSON');
  360. return num;
  361. }
  362. const c10 = c;
  363. c = uint8[x++];
  364. if (!c || ((c < 45 || c > 57) && c !== 69 && c !== 101)) {
  365. reader.x = x - 1;
  366. const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10);
  367. if (num !== num)
  368. throw new Error('Invalid JSON');
  369. return num;
  370. }
  371. const c11 = c;
  372. c = uint8[x++];
  373. if (!c || ((c < 45 || c > 57) && c !== 69 && c !== 101)) {
  374. reader.x = x - 1;
  375. const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11);
  376. if (num !== num)
  377. throw new Error('Invalid JSON');
  378. return num;
  379. }
  380. const c12 = c;
  381. c = uint8[x++];
  382. if (!c || ((c < 45 || c > 57) && c !== 69 && c !== 101)) {
  383. reader.x = x - 1;
  384. const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12);
  385. if (num !== num)
  386. throw new Error('Invalid JSON');
  387. return num;
  388. }
  389. const c13 = c;
  390. c = uint8[x++];
  391. if (!c || ((c < 45 || c > 57) && c !== 69 && c !== 101)) {
  392. reader.x = x - 1;
  393. const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13);
  394. if (num !== num)
  395. throw new Error('Invalid JSON');
  396. return num;
  397. }
  398. const c14 = c;
  399. c = uint8[x++];
  400. if (!c || ((c < 45 || c > 57) && c !== 69 && c !== 101)) {
  401. reader.x = x - 1;
  402. const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14);
  403. if (num !== num)
  404. throw new Error('Invalid JSON');
  405. return num;
  406. }
  407. const c15 = c;
  408. c = uint8[x++];
  409. if (!c || ((c < 45 || c > 57) && c !== 69 && c !== 101)) {
  410. reader.x = x - 1;
  411. const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15);
  412. if (num !== num)
  413. throw new Error('Invalid JSON');
  414. return num;
  415. }
  416. const c16 = c;
  417. c = uint8[x++];
  418. if (!c || ((c < 45 || c > 57) && c !== 69 && c !== 101)) {
  419. reader.x = x - 1;
  420. const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16);
  421. if (num !== num)
  422. throw new Error('Invalid JSON');
  423. return num;
  424. }
  425. const c17 = c;
  426. c = uint8[x++];
  427. if (!c || ((c < 45 || c > 57) && c !== 69 && c !== 101)) {
  428. reader.x = x - 1;
  429. const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17);
  430. if (num !== num)
  431. throw new Error('Invalid JSON');
  432. return num;
  433. }
  434. const c18 = c;
  435. c = uint8[x++];
  436. if (!c || ((c < 45 || c > 57) && c !== 69 && c !== 101)) {
  437. reader.x = x - 1;
  438. const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17, c18);
  439. if (num !== num)
  440. throw new Error('Invalid JSON');
  441. return num;
  442. }
  443. const c19 = c;
  444. c = uint8[x++];
  445. if (!c || ((c < 45 || c > 57) && c !== 69 && c !== 101)) {
  446. reader.x = x - 1;
  447. const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17, c18, c19);
  448. if (num !== num)
  449. throw new Error('Invalid JSON');
  450. return num;
  451. }
  452. const c20 = c;
  453. c = uint8[x++];
  454. if (!c || ((c < 45 || c > 57) && c !== 69 && c !== 101)) {
  455. reader.x = x - 1;
  456. const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17, c18, c19, c20);
  457. if (num !== num)
  458. throw new Error('Invalid JSON');
  459. return num;
  460. }
  461. const c21 = c;
  462. c = uint8[x++];
  463. if (!c || ((c < 45 || c > 57) && c !== 69 && c !== 101)) {
  464. reader.x = x - 1;
  465. const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17, c18, c19, c20, c21);
  466. if (num !== num)
  467. throw new Error('Invalid JSON');
  468. return num;
  469. }
  470. const c22 = c;
  471. c = uint8[x++];
  472. if (!c || ((c < 45 || c > 57) && c !== 69 && c !== 101)) {
  473. reader.x = x - 1;
  474. const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17, c18, c19, c20, c21, c22);
  475. if (num !== num)
  476. throw new Error('Invalid JSON');
  477. return num;
  478. }
  479. const c23 = c;
  480. c = uint8[x++];
  481. if (!c || ((c < 45 || c > 57) && c !== 69 && c !== 101)) {
  482. reader.x = x - 1;
  483. const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17, c18, c19, c20, c21, c22, c23);
  484. if (num !== num)
  485. throw new Error('Invalid JSON');
  486. return num;
  487. }
  488. const c24 = c;
  489. c = uint8[x++];
  490. if (!c || ((c < 45 || c > 57) && c !== 69 && c !== 101)) {
  491. reader.x = x - 1;
  492. const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17, c18, c19, c20, c21, c22, c24);
  493. if (num !== num)
  494. throw new Error('Invalid JSON');
  495. return num;
  496. }
  497. throw new Error('Invalid JSON');
  498. }
  499. readStr() {
  500. const reader = this.reader;
  501. const uint8 = reader.uint8;
  502. const char = uint8[reader.x++];
  503. if (char !== 0x22)
  504. throw new Error('Invalid JSON');
  505. const x0 = reader.x;
  506. const x1 = (0, util_1.findEndingQuote)(uint8, x0);
  507. let str = (0, decodeUtf8_1.decodeUtf8)(uint8, x0, x1 - x0);
  508. str = str.replace(REGEX_REPLACE_ESCAPED_CHARS, escapedCharReplacer);
  509. reader.x = x1 + 1;
  510. return str;
  511. }
  512. tryReadBin() {
  513. const reader = this.reader;
  514. const u8 = reader.uint8;
  515. let x = reader.x;
  516. if (u8[x++] !== 0x22)
  517. return undefined;
  518. const hasDataUrlPrefix = hasBinaryPrefix(u8, x);
  519. if (!hasDataUrlPrefix)
  520. return undefined;
  521. x += 37;
  522. const x0 = x;
  523. x = (0, util_1.findEndingQuote)(u8, x);
  524. reader.x = x0;
  525. const bin = (0, fromBase64Bin_1.fromBase64Bin)(reader.view, x0, x - x0);
  526. reader.x = x + 1;
  527. return bin;
  528. }
  529. readBin() {
  530. const reader = this.reader;
  531. const u8 = reader.uint8;
  532. let x = reader.x;
  533. if (u8[x++] !== 0x22)
  534. throw new Error('Invalid JSON');
  535. const hasDataUrlPrefix = hasBinaryPrefix(u8, x);
  536. if (!hasDataUrlPrefix)
  537. throw new Error('Invalid JSON');
  538. x += 37;
  539. const x0 = x;
  540. x = (0, util_1.findEndingQuote)(u8, x);
  541. reader.x = x0;
  542. const bin = (0, fromBase64Bin_1.fromBase64Bin)(reader.view, x0, x - x0);
  543. reader.x = x + 1;
  544. return bin;
  545. }
  546. readArr() {
  547. const reader = this.reader;
  548. if (reader.u8() !== 0x5b)
  549. throw new Error('Invalid JSON');
  550. const arr = [];
  551. const uint8 = reader.uint8;
  552. while (true) {
  553. this.skipWhitespace();
  554. const char = uint8[reader.x];
  555. if (char === 0x5d)
  556. return reader.x++, arr;
  557. if (char === 0x2c) {
  558. reader.x++;
  559. continue;
  560. }
  561. arr.push(this.readAny());
  562. }
  563. }
  564. readObj() {
  565. const reader = this.reader;
  566. if (reader.u8() !== 0x7b)
  567. throw new Error('Invalid JSON');
  568. const obj = {};
  569. const uint8 = reader.uint8;
  570. while (true) {
  571. this.skipWhitespace();
  572. let char = uint8[reader.x];
  573. if (char === 0x7d)
  574. return reader.x++, obj;
  575. if (char === 0x2c) {
  576. reader.x++;
  577. continue;
  578. }
  579. char = uint8[reader.x++];
  580. if (char !== 0x22)
  581. throw new Error('Invalid JSON');
  582. const key = readShortUtf8StrAndUnescape(reader);
  583. if (key === '__proto__')
  584. throw new Error('Invalid JSON');
  585. this.skipWhitespace();
  586. if (reader.u8() !== 0x3a)
  587. throw new Error('Invalid JSON');
  588. this.skipWhitespace();
  589. obj[key] = this.readAny();
  590. }
  591. }
  592. }
  593. exports.JsonDecoder = JsonDecoder;
  594. //# sourceMappingURL=JsonDecoder.js.map