flatbuffers.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. const flatbuffers = {};
  2. flatbuffers.BinaryReader = class {
  3. static open(data, offset) {
  4. offset = offset || 0;
  5. if (data && data.length >= (offset + 8)) {
  6. const position = data instanceof Uint8Array ? -1 : data.position;
  7. const reader = data instanceof Uint8Array ?
  8. new flatbuffers.BufferReader(data) :
  9. new flatbuffers.StreamReader(data);
  10. reader.root = reader.int32(offset) + offset;
  11. let value = false;
  12. if (reader.root > 0 && reader.root < reader.length) {
  13. const buffer = reader.read(offset + 4, 4);
  14. reader.identifier = buffer.every((c) => c >= 32 && c <= 128) ? String.fromCharCode(...buffer) : '';
  15. const vtable = reader.int32(reader.root);
  16. if (vtable < 0 || (vtable > 4 && vtable < 1024)) {
  17. const start = reader.root - vtable;
  18. if (start > 0 && (start + 4) < reader.length) {
  19. const last = reader.int16(start) + start;
  20. if (last < reader.length) {
  21. const max = reader.int16(start + 2);
  22. if (max > 0 && (max & 1) === 0) {
  23. const offsets = [];
  24. for (let i = start + 4; i < last; i += 2) {
  25. const offset = reader.int16(i);
  26. offsets.push(offset);
  27. }
  28. value = max > Math.max(...offsets);
  29. }
  30. }
  31. }
  32. }
  33. }
  34. if (position !== -1) {
  35. data.seek(position);
  36. }
  37. if (value) {
  38. return reader;
  39. }
  40. }
  41. return null;
  42. }
  43. bool(offset) {
  44. return Boolean(this.int8(offset));
  45. }
  46. bool_(position, offset, defaultValue) {
  47. offset = this.__offset(position, offset);
  48. return offset ? this.bool(position + offset) : defaultValue;
  49. }
  50. int8(offset) {
  51. return this.uint8(offset) << 24 >> 24;
  52. }
  53. int8_(position, offset, defaultValue) {
  54. offset = this.__offset(position, offset);
  55. return offset ? this.int8(position + offset) : defaultValue;
  56. }
  57. uint8_(position, offset, defaultValue) {
  58. offset = this.__offset(position, offset);
  59. return offset ? this.uint8(position + offset) : defaultValue;
  60. }
  61. int16_(position, offset, defaultValue) {
  62. offset = this.__offset(position, offset);
  63. return offset ? this.int16(position + offset) : defaultValue;
  64. }
  65. uint16_(position, offset, defaultValue) {
  66. offset = this.__offset(position, offset);
  67. return offset ? this.uint16(position + offset) : defaultValue;
  68. }
  69. int32_(position, offset, defaultValue) {
  70. offset = this.__offset(position, offset);
  71. return offset ? this.int32(position + offset) : defaultValue;
  72. }
  73. uint32_(position, offset, defaultValue) {
  74. offset = this.__offset(position, offset);
  75. return offset ? this.uint32(position + offset) : defaultValue;
  76. }
  77. int64_(position, offset, defaultValue) {
  78. offset = this.__offset(position, offset);
  79. return offset ? this.int64(position + offset) : defaultValue;
  80. }
  81. uint64_(position, offset, defaultValue) {
  82. offset = this.__offset(position, offset);
  83. return offset ? this.uint64(position + offset) : defaultValue;
  84. }
  85. float32_(position, offset, defaultValue) {
  86. offset = this.__offset(position, offset);
  87. return offset ? this.float32(position + offset) : defaultValue;
  88. }
  89. float64_(position, offset, defaultValue) {
  90. offset = this.__offset(position, offset);
  91. return offset ? this.float64(position + offset) : defaultValue;
  92. }
  93. string(offset, encoding) {
  94. offset += this.int32(offset);
  95. const length = this.int32(offset);
  96. offset += 4;
  97. if (encoding === 1) {
  98. return this.read(offset, length);
  99. }
  100. let text = '';
  101. for (let i = 0; i < length;) {
  102. let codePoint = 0;
  103. const a = this.uint8(offset + i++);
  104. if (a < 0xC0) {
  105. codePoint = a;
  106. } else {
  107. const b = this.uint8(offset + i++);
  108. if (a < 0xE0) {
  109. codePoint = ((a & 0x1F) << 6) | (b & 0x3F);
  110. } else {
  111. const c = this.uint8(offset + i++);
  112. if (a < 0xF0) {
  113. codePoint = ((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F);
  114. } else {
  115. const d = this.uint8(offset + i++);
  116. codePoint = ((a & 0x07) << 18) | ((b & 0x3F) << 12) | ((c & 0x3F) << 6) | (d & 0x3F);
  117. }
  118. }
  119. }
  120. // Encode UTF-16
  121. if (codePoint < 0x10000) {
  122. text += String.fromCharCode(codePoint);
  123. } else {
  124. codePoint -= 0x10000;
  125. text += String.fromCharCode((codePoint >> 10) + 0xD800, (codePoint & ((1 << 10) - 1)) + 0xDC00);
  126. }
  127. }
  128. return text;
  129. }
  130. string_(position, offset, defaultValue) {
  131. offset = this.__offset(position, offset);
  132. return offset ? this.string(position + offset) : defaultValue;
  133. }
  134. bools_(position, offset) {
  135. offset = this.__offset(position, offset);
  136. if (offset) {
  137. const length = this.__vector_len(position + offset);
  138. offset = this.__vector(position + offset);
  139. const array = new Array(length);
  140. for (let i = 0; i < length; i++) {
  141. array[i] = this.uint8(offset + i) ? true : false;
  142. }
  143. return array;
  144. }
  145. return [];
  146. }
  147. int64s_(position, offset) {
  148. offset = this.__offset(position, offset);
  149. if (offset) {
  150. const length = this.__vector_len(position + offset);
  151. offset = this.__vector(position + offset);
  152. const array = new Array(length);
  153. for (let i = 0; i < length; i++) {
  154. array[i] = this.int64(offset + (i << 3));
  155. }
  156. return array;
  157. }
  158. return [];
  159. }
  160. uint64s_(position, offset) {
  161. offset = this.__offset(position, offset);
  162. if (offset) {
  163. const length = this.__vector_len(position + offset);
  164. offset = this.__vector(position + offset);
  165. const array = new Array(length);
  166. for (let i = 0; i < length; i++) {
  167. array[i] = this.uint64(offset + (i << 3));
  168. }
  169. return array;
  170. }
  171. return [];
  172. }
  173. strings_(position, offset) {
  174. offset = this.__offset(position, offset);
  175. if (offset) {
  176. const length = this.__vector_len(position + offset);
  177. offset = this.__vector(position + offset);
  178. const array = new Array(length);
  179. for (let i = 0; i < length; i++) {
  180. array[i] = this.string(offset + i * 4);
  181. }
  182. return array;
  183. }
  184. return [];
  185. }
  186. struct(position, offset, type) {
  187. offset = this.__offset(position, offset);
  188. return offset ? type.decode(this, position + offset) : null;
  189. }
  190. table(position, offset, type) {
  191. offset = this.__offset(position, offset);
  192. return offset ? type.decode(this, this.__indirect(position + offset)) : null;
  193. }
  194. union(position, offset, type) {
  195. const type_offset = this.__offset(position, offset);
  196. const union_type = type_offset ? this.uint8(position + type_offset) : 0;
  197. offset = this.__offset(position, offset + 2);
  198. return offset ? type.decode(this, this.__union(position + offset), union_type) : null;
  199. }
  200. array(position, offset, type) {
  201. offset = this.__offset(position, offset);
  202. if (offset) {
  203. const length = this.__vector_len(position + offset);
  204. offset = this.__vector(position + offset);
  205. const buffer = this.read(offset, length * type.BYTES_PER_ELEMENT);
  206. return new type(buffer.buffer, buffer.byteOffset, length);
  207. }
  208. return new type(0);
  209. }
  210. unions(/* position, offset, decode */) {
  211. return new flatbuffers.Error('Not implemented.');
  212. }
  213. structs(position, offset, type) {
  214. offset = this.__offset(position, offset);
  215. const length = offset ? this.__vector_len(position + offset) : 0;
  216. const list = new Array(length);
  217. for (let i = 0; i < length; i++) {
  218. list[i] = type.decode(this, this.__vector(position + offset) + i * 8);
  219. }
  220. return list;
  221. }
  222. tables(position, offset, type) {
  223. offset = this.__offset(position, offset);
  224. const length = offset ? this.__vector_len(position + offset) : 0;
  225. const list = new Array(length);
  226. for (let i = 0; i < length; i++) {
  227. list[i] = type.decode(this, this.__indirect(this.__vector(position + offset) + i * 4));
  228. }
  229. return list;
  230. }
  231. __offset(bb_pos, vtableOffset) {
  232. const vtable = bb_pos - this.int32(bb_pos);
  233. return vtableOffset < this.int16(vtable) ? this.int16(vtable + vtableOffset) : 0;
  234. }
  235. __indirect(offset) {
  236. return offset + this.int32(offset);
  237. }
  238. __vector(offset) {
  239. return offset + this.int32(offset) + 4;
  240. }
  241. __vector_len(offset) {
  242. return this.int32(offset + this.int32(offset));
  243. }
  244. __union(offset) {
  245. return offset + this.int32(offset);
  246. }
  247. };
  248. flatbuffers.BufferReader = class extends flatbuffers.BinaryReader {
  249. constructor(data) {
  250. super();
  251. this.length = data.length;
  252. this._buffer = data;
  253. this._view = new DataView(data.buffer, data.byteOffset, data.byteLength);
  254. }
  255. read(offset, length) {
  256. return this._buffer.slice(offset, offset + length);
  257. }
  258. uint8(offset) {
  259. return this._buffer[offset];
  260. }
  261. int16(offset) {
  262. return this._view.getInt16(offset, true);
  263. }
  264. uint16(offset) {
  265. return this._view.getUint16(offset, true);
  266. }
  267. int32(offset) {
  268. return this._view.getInt32(offset, true);
  269. }
  270. uint32(offset) {
  271. return this._view.getUint32(offset, true);
  272. }
  273. int64(offset) {
  274. return this._view.getBigInt64(offset, true);
  275. }
  276. uint64(offset) {
  277. return this._view.getBigUint64(offset, true);
  278. }
  279. float32(offset) {
  280. return this._view.getFloat32(offset, true);
  281. }
  282. float64(offset) {
  283. return this._view.getFloat64(offset, true);
  284. }
  285. };
  286. flatbuffers.StreamReader = class extends flatbuffers.BinaryReader {
  287. constructor(stream) {
  288. super();
  289. this._length = stream.length;
  290. this._stream = stream;
  291. this._size = 0x10000000;
  292. this._offset = 0;
  293. this._window = Math.min(0x1000, stream.length);
  294. const buffer = this._stream.peek(this._window);
  295. this._buffer = buffer;
  296. this._view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
  297. this._chunk = -1;
  298. }
  299. get length() {
  300. return this._length;
  301. }
  302. read(offset, length) {
  303. const buffer = new Uint8Array(length);
  304. this._read(buffer, offset);
  305. return buffer;
  306. }
  307. uint8(offset) {
  308. const position = this._fill(offset, 1);
  309. return this._view.getUint8(position);
  310. }
  311. int16(offset) {
  312. const position = this._fill(offset, 2);
  313. return this._view.getInt16(position, true);
  314. }
  315. uint16(offset) {
  316. const position = this._fill(offset, 2);
  317. return this._view.getUint16(position, true);
  318. }
  319. int32(offset) {
  320. const position = this._fill(offset, 4);
  321. return this._view.getInt32(position, true);
  322. }
  323. uint32(offset) {
  324. const position = this._fill(offset, 4);
  325. return this._view.getUint32(position, true);
  326. }
  327. int64(offset) {
  328. const position = this._fill(offset, 8);
  329. return this._view.getBigInt64(position, true);
  330. }
  331. uint64(offset) {
  332. const position = this._fill(offset, 8);
  333. return this._view.getBigUint64(position, true);
  334. }
  335. float32(offset) {
  336. const position = this._fill(offset, 4);
  337. return this._view.getFloat32(position, true);
  338. }
  339. float64(offset) {
  340. const position = this._fill(offset, 8);
  341. return this._view.getFloat64(position, true);
  342. }
  343. _fill(offset, length) {
  344. if (offset + length > this._length) {
  345. throw new Error(`Expected ${offset + length - this._length} more bytes. The file might be corrupted. Unexpected end of file.`);
  346. }
  347. if (offset < this._offset || offset + length > this._offset + this._window) {
  348. const remainder = offset % this. _size;
  349. const last = this._last;
  350. if (this._chunk !== -1) {
  351. this._last = [this._chunk, this._buffer, this._view];
  352. }
  353. if (remainder + length > this._size) {
  354. const buffer = new Uint8Array(length);
  355. this._read(buffer, length);
  356. this._chunk = -1;
  357. this._offset = offset;
  358. this._window = length;
  359. this._buffer = buffer;
  360. this._view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
  361. } else {
  362. const chunk = Math.floor(offset / this._size);
  363. this._offset = chunk * this._size;
  364. this._window = Math.min(this._length - this._offset, this._size);
  365. if (last && last[0] === chunk) {
  366. [this._chunk, this._buffer, this._view] = last;
  367. } else {
  368. this._chunk = chunk;
  369. this._stream.seek(this._offset);
  370. const buffer = this._stream.read(this._window);
  371. this._buffer = buffer;
  372. this._view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
  373. this._stream.seek(0);
  374. }
  375. }
  376. }
  377. return offset - this._offset;
  378. }
  379. _read(buffer, offset) {
  380. const length = buffer.length;
  381. if (offset < this._offset || offset + length > this._offset + this._window) {
  382. this._stream.seek(offset);
  383. const data = this._stream.read(length);
  384. buffer.set(data, 0);
  385. this._stream.seek(0);
  386. } else {
  387. offset -= this._offset;
  388. const data = this._buffer.subarray(offset, offset + length);
  389. buffer.set(data, 0);
  390. }
  391. }
  392. };
  393. flatbuffers.TextReader = class {
  394. static open(obj) {
  395. return new flatbuffers.TextReader(obj);
  396. }
  397. constructor(obj) {
  398. this._root = obj;
  399. }
  400. get root() {
  401. return this._root;
  402. }
  403. int64(obj, defaultValue) {
  404. return obj === undefined ? defaultValue : BigInt(obj);
  405. }
  406. uint64(obj, defaultValue) {
  407. return obj === undefined ? defaultValue : BigInt(obj);
  408. }
  409. value(obj, defaultValue) {
  410. return obj === undefined ? defaultValue : obj;
  411. }
  412. object(obj, type) {
  413. return obj === undefined ? obj : type.decodeText(this, obj);
  414. }
  415. array(obj, type) {
  416. type = type || Array;
  417. if (Array.isArray(obj)) {
  418. const length = obj.length;
  419. const target = new type(length);
  420. for (let i = 0; i < length; i++) {
  421. target[i] = obj[i];
  422. }
  423. return target;
  424. }
  425. if (obj) {
  426. throw new flatbuffers.Error('Inalid value array.');
  427. }
  428. return new type(0);
  429. }
  430. objects(obj, type) {
  431. if (Array.isArray(obj)) {
  432. const target = new Array(obj.length);
  433. for (let i = 0; i < obj.length; i++) {
  434. target[i] = type.decodeText(this, obj[i]);
  435. }
  436. return target;
  437. }
  438. if (!obj) {
  439. return [];
  440. }
  441. throw new flatbuffers.Error('Inalid object array.');
  442. }
  443. };
  444. flatbuffers.Error = class extends Error {
  445. constructor(message) {
  446. super(message);
  447. this.name = 'FlatBuffers Error';
  448. this.message = message;
  449. }
  450. };
  451. export const BinaryReader = flatbuffers.BinaryReader;
  452. export const TextReader = flatbuffers.TextReader;