base.js 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  1. const base = {};
  2. base.Complex = class Complex {
  3. constructor(real, imaginary) {
  4. this.real = real;
  5. this.imaginary = imaginary;
  6. }
  7. toString(/* radix */) {
  8. const sign = this.imaginary < 0 ? '-' : '+';
  9. const imaginary = Math.abs(this.imaginary);
  10. return `${this.real} ${sign} ${imaginary}i`;
  11. }
  12. };
  13. /* eslint-disable no-extend-native */
  14. BigInt.prototype.toNumber = function() {
  15. if (this > Number.MAX_SAFE_INTEGER || this < Number.MIN_SAFE_INTEGER) {
  16. throw new Error(`64-bit value 0x${this.toString(16)} exceeds safe integer.`);
  17. }
  18. return Number(this);
  19. };
  20. if (!DataView.prototype.getFloat16) {
  21. DataView.prototype.getFloat16 = function(byteOffset, littleEndian) {
  22. const value = this.getUint16(byteOffset, littleEndian);
  23. const e = (value & 0x7C00) >> 10;
  24. let f = value & 0x03FF;
  25. if (e === 0) {
  26. f = 0.00006103515625 * (f / 1024);
  27. } else if (e === 0x1F) {
  28. f = f ? NaN : Infinity;
  29. } else {
  30. f = DataView.__float16_pow[e] * (1 + (f / 1024));
  31. }
  32. return value & 0x8000 ? -f : f;
  33. };
  34. DataView.__float16_pow = {
  35. 1: 1 / 16384, 2: 1 / 8192, 3: 1 / 4096, 4: 1 / 2048, 5: 1 / 1024, 6: 1 / 512, 7: 1 / 256, 8: 1 / 128,
  36. 9: 1 / 64, 10: 1 / 32, 11: 1 / 16, 12: 1 / 8, 13: 1 / 4, 14: 1 / 2, 15: 1, 16: 2,
  37. 17: 4, 18: 8, 19: 16, 20: 32, 21: 64, 22: 128, 23: 256, 24: 512,
  38. 25: 1024, 26: 2048, 27: 4096, 28: 8192, 29: 16384, 30: 32768, 31: 65536
  39. };
  40. }
  41. if (!DataView.prototype.setFloat16) {
  42. DataView.prototype.setFloat16 = function(byteOffset, value, littleEndian) {
  43. DataView.__float16_float[0] = value;
  44. [value] = DataView.__float16_int;
  45. const s = (value >>> 16) & 0x8000;
  46. const e = (value >>> 23) & 0xff;
  47. const f = value & 0x7fffff;
  48. const v = s | DataView.__float16_base[e] | (f >> DataView.__float16_shift[e]);
  49. this.setUint16(byteOffset, v, littleEndian);
  50. };
  51. DataView.__float16_float = new Float32Array(1);
  52. DataView.__float16_int = new Uint32Array(DataView.__float16_float.buffer, 0, DataView.__float16_float.length);
  53. DataView.__float16_base = new Uint32Array(256);
  54. DataView.__float16_shift = new Uint32Array(256);
  55. for (let i = 0; i < 256; ++i) {
  56. const e = i - 127;
  57. if (e < -27) {
  58. DataView.__float16_base[i] = 0x0000;
  59. DataView.__float16_shift[i] = 24;
  60. } else if (e < -14) {
  61. DataView.__float16_base[i] = 0x0400 >> -e - 14;
  62. DataView.__float16_shift[i] = -e - 1;
  63. } else if (e <= 15) {
  64. DataView.__float16_base[i] = e + 15 << 10;
  65. DataView.__float16_shift[i] = 13;
  66. } else if (e < 128) {
  67. DataView.__float16_base[i] = 0x7c00;
  68. DataView.__float16_shift[i] = 24;
  69. } else {
  70. DataView.__float16_base[i] = 0x7c00;
  71. DataView.__float16_shift[i] = 13;
  72. }
  73. }
  74. }
  75. if (!DataView.prototype.getBfloat16) {
  76. DataView.prototype.getBfloat16 = function(byteOffset, littleEndian) {
  77. const value = this.getUint16(byteOffset, littleEndian);
  78. DataView.__bfloat16_get_u32[0] = (value << 16) >>> 0;
  79. return DataView.__bfloat16_get_f32[0];
  80. };
  81. DataView.__bfloat16_get_f32 = new Float32Array(1);
  82. DataView.__bfloat16_get_u32 = new Uint32Array(DataView.__bfloat16_get_f32.buffer);
  83. }
  84. if (!DataView.prototype.getFloat4e2m1) {
  85. DataView.__float4e2m1_float32 = new Float32Array([0, 0.5, 1, 1.5, 2, 3, 4, 6, -0, -0.5, -1, -1.5, -2, -3, -4, -6]);
  86. DataView.prototype.getFloat4e2m1 = function(byteOffset) {
  87. let value = this.getUint8(byteOffset >> 1);
  88. value = byteOffset & 1 ? value >> 4 : value & 0x0F;
  89. return DataView.__float4e2m1_float32[value];
  90. };
  91. }
  92. if (!DataView.prototype.getFloat8e4m3) {
  93. DataView.__float8e4m3_float32 = new Float32Array(1);
  94. DataView.__float8e4m3_uint32 = new Uint32Array(DataView.__float8e4m3_float32.buffer, DataView.__float8e4m3_float32.byteOffset, 1);
  95. DataView.prototype.getFloat8e4m3 = function(byteOffset, fn, uz) {
  96. const value = this.getUint8(byteOffset);
  97. let exponent_bias = 7;
  98. if (uz) {
  99. exponent_bias = 8;
  100. if (value === 0x80) {
  101. return NaN;
  102. }
  103. } else if (value === 255) {
  104. return -NaN;
  105. } else if (value === 0x7f) {
  106. return NaN;
  107. }
  108. let expo = (value & 0x78) >> 3;
  109. let mant = value & 0x07;
  110. const sign = value & 0x80;
  111. let res = sign << 24;
  112. if (expo === 0) {
  113. if (mant > 0) {
  114. expo = 0x7F - exponent_bias;
  115. if ((mant & 0x4) === 0) {
  116. mant &= 0x3;
  117. mant <<= 1;
  118. expo -= 1;
  119. }
  120. if ((mant & 0x4) === 0) {
  121. mant &= 0x3;
  122. mant <<= 1;
  123. expo -= 1;
  124. }
  125. res |= (mant & 0x3) << 21;
  126. res |= expo << 23;
  127. }
  128. } else {
  129. res |= mant << 20;
  130. expo += 0x7F - exponent_bias;
  131. res |= expo << 23;
  132. }
  133. DataView.__float8e4m3_uint32[0] = res;
  134. return DataView.__float8e4m3_float32[0];
  135. };
  136. }
  137. if (!DataView.prototype.getFloat8e5m2) {
  138. DataView.__float8e5m2_float32 = new Float32Array(1);
  139. DataView.__float8e5m2_uint32 = new Uint32Array(DataView.__float8e5m2_float32.buffer, DataView.__float8e5m2_float32.byteOffset, 1);
  140. DataView.prototype.getFloat8e5m2 = function(byteOffset, fn, uz) {
  141. const value = this.getUint8(byteOffset);
  142. let exponent_bias = NaN;
  143. if (fn && uz) {
  144. if (value === 0x80) {
  145. return NaN;
  146. }
  147. exponent_bias = 16;
  148. } else if (!fn && !uz) {
  149. if (value >= 253 && value <= 255) {
  150. return -NaN;
  151. }
  152. if (value >= 126 && value <= 127) {
  153. return NaN;
  154. }
  155. if (value === 252) {
  156. return -Infinity;
  157. }
  158. if (value === 124) {
  159. return Infinity;
  160. }
  161. exponent_bias = 15;
  162. }
  163. let expo = (value & 0x7C) >> 2;
  164. let mant = value & 0x03;
  165. let res = (value & 0x80) << 24;
  166. if (expo === 0) {
  167. if (mant > 0) {
  168. expo = 0x7F - exponent_bias;
  169. if ((mant & 0x2) === 0) {
  170. mant &= 0x1;
  171. mant <<= 1;
  172. expo -= 1;
  173. }
  174. res |= (mant & 0x1) << 22;
  175. res |= expo << 23;
  176. }
  177. } else {
  178. res |= mant << 21;
  179. expo += 0x7F - exponent_bias;
  180. res |= expo << 23;
  181. }
  182. DataView.__float8e5m2_uint32[0] = res;
  183. return DataView.__float8e5m2_float32[0];
  184. };
  185. }
  186. if (!DataView.prototype.getFloat8e3m4) {
  187. DataView.__float8e3m4_float32 = new Float32Array(1);
  188. DataView.__float8e3m4_uint32 = new Uint32Array(DataView.__float8e3m4_float32.buffer, DataView.__float8e3m4_float32.byteOffset, 1);
  189. DataView.prototype.getFloat8e3m4 = function(byteOffset) {
  190. const value = this.getUint8(byteOffset);
  191. const exponent_bias = 3;
  192. if (value === 0x7f) {
  193. return NaN;
  194. }
  195. if (value === 0xff) {
  196. return -NaN;
  197. }
  198. let expo = (value & 0x70) >> 4;
  199. let mant = value & 0x0F;
  200. const sign = value & 0x80;
  201. let res = sign << 24;
  202. if (expo === 0) {
  203. if (mant > 0) {
  204. expo = 0x7F - exponent_bias;
  205. if ((mant & 0x8) === 0) {
  206. mant &= 0x7;
  207. mant <<= 1;
  208. expo -= 1;
  209. }
  210. if ((mant & 0x8) === 0) {
  211. mant &= 0x7;
  212. mant <<= 1;
  213. expo -= 1;
  214. }
  215. if ((mant & 0x8) === 0) {
  216. mant &= 0x7;
  217. mant <<= 1;
  218. expo -= 1;
  219. }
  220. res |= (mant & 0x7) << 20;
  221. res |= expo << 23;
  222. }
  223. } else {
  224. res |= mant << 19;
  225. expo += 0x7F - exponent_bias;
  226. res |= expo << 23;
  227. }
  228. DataView.__float8e3m4_uint32[0] = res;
  229. return DataView.__float8e3m4_float32[0];
  230. };
  231. }
  232. DataView.prototype.getIntBits = DataView.prototype.getUintBits || function(offset, bits, littleEndian) {
  233. offset *= bits;
  234. const position = Math.floor(offset / 8);
  235. const remainder = offset % 8;
  236. let value = 0;
  237. if ((remainder + bits) <= 8) {
  238. value = littleEndian ? this.getUint8(position) >> remainder : this.getUint8(position) >> (8 - remainder - bits);
  239. } else {
  240. value = littleEndian ? this.getUint16(position, true) >> remainder : this.getUint16(position, false) >> (16 - remainder - bits);
  241. }
  242. value &= (1 << bits) - 1;
  243. if (value & (1 << (bits - 1))) {
  244. value -= 1 << bits;
  245. }
  246. return value;
  247. };
  248. DataView.prototype.getUintBits = DataView.prototype.getUintBits || function(offset, bits, littleEndian) {
  249. offset *= bits;
  250. const position = Math.floor(offset / 8);
  251. const remainder = offset % 8;
  252. let value = 0;
  253. if ((remainder + bits) <= 8) {
  254. value = littleEndian ? this.getUint8(position) >> remainder : this.getUint8(position) >> (8 - remainder - bits);
  255. } else {
  256. value = littleEndian ? this.getUint16(position, true) >> remainder : this.getUint16(position, false) >> (16 - remainder - bits);
  257. }
  258. return value & ((1 << bits) - 1);
  259. };
  260. DataView.prototype.getComplexInt32 = DataView.prototype.getComplexInt32 || function(byteOffset, littleEndian) {
  261. const real = littleEndian ? this.getInt32(byteOffset, littleEndian) : this.getInt32(byteOffset + 4, littleEndian);
  262. const imaginary = littleEndian ? this.getInt32(byteOffset + 4, littleEndian) : this.getInt32(byteOffset, littleEndian);
  263. return new base.Complex(real, imaginary);
  264. };
  265. DataView.prototype.getComplexFloat16 = DataView.prototype.getComplexFloat16 || function(byteOffset, littleEndian) {
  266. const real = littleEndian ? this.getFloat16(byteOffset, littleEndian) : this.getFloat16(byteOffset + 2, littleEndian);
  267. const imaginary = littleEndian ? this.getFloat16(byteOffset + 2, littleEndian) : this.getFloat16(byteOffset, littleEndian);
  268. return new base.Complex(real, imaginary);
  269. };
  270. DataView.prototype.setComplexFloat16 = DataView.prototype.setComplexFloat16 || function(byteOffset, value, littleEndian) {
  271. if (littleEndian) {
  272. this.setFloat16(byteOffset, value.real, littleEndian);
  273. this.setFloat16(byteOffset + 2, value.imaginary, littleEndian);
  274. } else {
  275. this.setFloat16(byteOffset + 2, value.real, littleEndian);
  276. this.setFloat16(byteOffset, value.imaginary, littleEndian);
  277. }
  278. };
  279. DataView.prototype.getComplexFloat32 = DataView.prototype.getComplexFloat32 || function(byteOffset, littleEndian) {
  280. const real = littleEndian ? this.getFloat32(byteOffset, littleEndian) : this.getFloat32(byteOffset + 4, littleEndian);
  281. const imaginary = littleEndian ? this.getFloat32(byteOffset + 4, littleEndian) : this.getFloat32(byteOffset, littleEndian);
  282. return new base.Complex(real, imaginary);
  283. };
  284. DataView.prototype.setComplexFloat32 = DataView.prototype.setComplexFloat32 || function(byteOffset, value, littleEndian) {
  285. if (littleEndian) {
  286. this.setFloat32(byteOffset, value.real, littleEndian);
  287. this.setFloat32(byteOffset + 4, value.imaginary, littleEndian);
  288. } else {
  289. this.setFloat32(byteOffset + 4, value.real, littleEndian);
  290. this.setFloat32(byteOffset, value.imaginary, littleEndian);
  291. }
  292. };
  293. DataView.prototype.getComplexFloat64 = DataView.prototype.getComplexFloat64 || function(byteOffset, littleEndian) {
  294. const real = littleEndian ? this.getFloat64(byteOffset, littleEndian) : this.getFloat64(byteOffset + 8, littleEndian);
  295. const imaginary = littleEndian ? this.getFloat64(byteOffset + 8, littleEndian) : this.getFloat64(byteOffset, littleEndian);
  296. return new base.Complex(real, imaginary);
  297. };
  298. DataView.prototype.setComplexFloat64 = DataView.prototype.setComplexFloat64 || function(byteOffset, value, littleEndian) {
  299. if (littleEndian) {
  300. this.setFloat64(byteOffset, value.real, littleEndian);
  301. this.setFloat64(byteOffset + 8, value.imaginary, littleEndian);
  302. } else {
  303. this.setFloat64(byteOffset + 8, value.real, littleEndian);
  304. this.setFloat64(byteOffset, value.imaginary, littleEndian);
  305. }
  306. };
  307. /* eslint-enable no-extend-native */
  308. base.BinaryStream = class {
  309. constructor(buffer) {
  310. this._buffer = buffer;
  311. this._length = buffer.length;
  312. this._position = 0;
  313. }
  314. get position() {
  315. return this._position;
  316. }
  317. get length() {
  318. return this._length;
  319. }
  320. stream(length) {
  321. const buffer = this.read(length);
  322. return new base.BinaryStream(buffer.slice(0));
  323. }
  324. seek(position) {
  325. this._position = position >= 0 ? position : this._length + position;
  326. if (this._position > this._buffer.length || this._position < 0) {
  327. throw new Error(`Expected ${this._position - this._buffer.length} more bytes. The file might be corrupted. Unexpected end of file.`);
  328. }
  329. }
  330. skip(offset) {
  331. this._position += offset;
  332. if (this._position > this._buffer.length || this._position < 0) {
  333. throw new Error(`Expected ${this._position - this._buffer.length} more bytes. The file might be corrupted. Unexpected end of file.`);
  334. }
  335. }
  336. peek(length) {
  337. if (this._position === 0 && length === undefined) {
  338. return this._buffer;
  339. }
  340. const position = this._position;
  341. this.skip(length === undefined ? this._length - this._position : length);
  342. const end = this._position;
  343. this.seek(position);
  344. return this._buffer.subarray(position, end);
  345. }
  346. read(length) {
  347. if (this._position === 0 && length === undefined) {
  348. this._position = this._length;
  349. return this._buffer;
  350. }
  351. const position = this._position;
  352. this.skip(length === undefined ? this._length - this._position : length);
  353. return this._buffer.subarray(position, this._position);
  354. }
  355. };
  356. base.BinaryReader = class {
  357. static open(data, littleEndian) {
  358. if (data instanceof Uint8Array || data.length <= 0x20000000) {
  359. return new base.BufferReader(data, littleEndian);
  360. }
  361. return new base.StreamReader(data, littleEndian);
  362. }
  363. };
  364. base.BufferReader = class {
  365. constructor(data, littleEndian) {
  366. this._buffer = data instanceof Uint8Array ? data : data.peek();
  367. this._littleEndian = littleEndian !== false;
  368. this._position = 0;
  369. this._length = this._buffer.length;
  370. this._view = new DataView(this._buffer.buffer, this._buffer.byteOffset, this._buffer.byteLength);
  371. }
  372. get length() {
  373. return this._length;
  374. }
  375. get position() {
  376. return this._position;
  377. }
  378. seek(position) {
  379. this._position = position >= 0 ? position : this._length + position;
  380. if (this._position > this._length || this._position < 0) {
  381. throw new Error(`Expected ${this._position - this._length} more bytes. The file might be corrupted. Unexpected end of file.`);
  382. }
  383. }
  384. skip(offset) {
  385. this._position += offset;
  386. if (this._position > this._length) {
  387. throw new Error(`Expected ${this._position - this._length} more bytes. The file might be corrupted. Unexpected end of file.`);
  388. }
  389. }
  390. align(size) {
  391. const remainder = this.position % size;
  392. if (remainder !== 0) {
  393. this.skip(size - remainder);
  394. }
  395. }
  396. stream(length) {
  397. const buffer = this.read(length);
  398. return new base.BinaryStream(buffer);
  399. }
  400. peek(length) {
  401. if (this._position === 0 && length === undefined) {
  402. return this._buffer;
  403. }
  404. const position = this._position;
  405. this.skip(length === undefined ? this._length - this._position : length);
  406. const end = this._position;
  407. this._position = position;
  408. return this._buffer.slice(position, end);
  409. }
  410. read(length) {
  411. if (this._position === 0 && length === undefined) {
  412. this._position = this._length;
  413. return this._buffer;
  414. }
  415. const position = this._position;
  416. this.skip(length === undefined ? this._length - this._position : length);
  417. return this._buffer.slice(position, this._position);
  418. }
  419. byte() {
  420. const position = this._position;
  421. this.skip(1);
  422. return this._buffer[position];
  423. }
  424. int8() {
  425. const position = this._position;
  426. this.skip(1);
  427. return this._view.getInt8(position, this._littleEndian);
  428. }
  429. int16() {
  430. const position = this._position;
  431. this.skip(2);
  432. return this._view.getInt16(position, this._littleEndian);
  433. }
  434. int32() {
  435. const position = this._position;
  436. this.skip(4);
  437. return this._view.getInt32(position, this._littleEndian);
  438. }
  439. int64() {
  440. const position = this._position;
  441. this.skip(8);
  442. return this._view.getBigInt64(position, this._littleEndian);
  443. }
  444. uint16() {
  445. const position = this._position;
  446. this.skip(2);
  447. return this._view.getUint16(position, this._littleEndian);
  448. }
  449. uint32() {
  450. const position = this._position;
  451. this.skip(4);
  452. return this._view.getUint32(position, this._littleEndian);
  453. }
  454. uint64() {
  455. const position = this._position;
  456. this.skip(8);
  457. return this._view.getBigUint64(position, this._littleEndian);
  458. }
  459. float32() {
  460. const position = this._position;
  461. this.skip(4);
  462. return this._view.getFloat32(position, this._littleEndian);
  463. }
  464. float64() {
  465. const position = this._position;
  466. this.skip(8);
  467. return this._view.getFloat64(position, this._littleEndian);
  468. }
  469. string() {
  470. const length = this.uint32();
  471. const position = this._position;
  472. this.skip(length);
  473. const data = this._buffer.subarray(position, this._position);
  474. this._decoder = this._decoder || new TextDecoder('utf-8');
  475. return this._decoder.decode(data);
  476. }
  477. boolean() {
  478. return this.byte() === 0 ? false : true;
  479. }
  480. };
  481. base.StreamReader = class {
  482. constructor(stream, littleEndian) {
  483. this._stream = stream;
  484. this._littleEndian = littleEndian !== false;
  485. this._buffer = new Uint8Array(8);
  486. this._view = new DataView(this._buffer.buffer, this._buffer.byteOffset, this._buffer.byteLength);
  487. }
  488. get position() {
  489. return this._stream.position;
  490. }
  491. get length() {
  492. return this._stream.length;
  493. }
  494. seek(position) {
  495. this._stream.seek(position);
  496. }
  497. skip(offset) {
  498. this._stream.skip(offset);
  499. }
  500. align(size) {
  501. const remainder = this.position % size;
  502. if (remainder !== 0) {
  503. this.skip(size - remainder);
  504. }
  505. }
  506. stream(length) {
  507. return this._stream.stream(length);
  508. }
  509. peek(length) {
  510. return this._stream.peek(length).slice(0);
  511. }
  512. read(length) {
  513. return this._stream.read(length).slice(0);
  514. }
  515. byte() {
  516. return this._stream.read(1)[0];
  517. }
  518. int8() {
  519. const buffer = this._stream.read(1);
  520. this._buffer.set(buffer, 0);
  521. return this._view.getInt8(0);
  522. }
  523. int16() {
  524. const buffer = this._stream.read(2);
  525. this._buffer.set(buffer, 0);
  526. return this._view.getInt16(0, this._littleEndian);
  527. }
  528. int32() {
  529. const buffer = this._stream.read(4);
  530. this._buffer.set(buffer, 0);
  531. return this._view.getInt32(0, this._littleEndian);
  532. }
  533. int64() {
  534. const buffer = this._stream.read(8);
  535. this._buffer.set(buffer, 0);
  536. return this._view.getBigInt64(0, this._littleEndian);
  537. }
  538. uint16() {
  539. const buffer = this._stream.read(2);
  540. this._buffer.set(buffer, 0);
  541. return this._view.getUint16(0, this._littleEndian);
  542. }
  543. uint32() {
  544. const buffer = this._stream.read(4);
  545. this._buffer.set(buffer, 0);
  546. return this._view.getUint32(0, this._littleEndian);
  547. }
  548. uint64() {
  549. const buffer = this._stream.read(8);
  550. this._buffer.set(buffer, 0);
  551. return this._view.getBigUint64(0, this._littleEndian);
  552. }
  553. float32() {
  554. const buffer = this._stream.read(4);
  555. this._buffer.set(buffer, 0);
  556. return this._view.getFloat32(0, this._littleEndian);
  557. }
  558. float64() {
  559. const buffer = this._stream.read(8);
  560. this._buffer.set(buffer, 0);
  561. return this._view.getFloat64(0, this._littleEndian);
  562. }
  563. boolean() {
  564. return this.byte() === 0 ? false : true;
  565. }
  566. };
  567. base.Tensor = class {
  568. constructor(tensor) {
  569. this._tensor = tensor;
  570. this.name = tensor.name || '';
  571. this.encoding = tensor.encoding;
  572. this.encoding = this.encoding === '' || this.encoding === undefined ? '<' : this.encoding;
  573. this.type = tensor.type;
  574. this.layout = tensor.type.layout;
  575. this.stride = tensor.stride;
  576. base.Tensor._dataTypes = base.Tensor._dataTypes || new Map([
  577. ['boolean', 1],
  578. ['qint8', 1], ['qint16', 2], ['qint32', 4],
  579. ['quint8', 1], ['quint16', 2], ['quint32', 4],
  580. ['xint8', 1],
  581. ['int8', 1], ['int16', 2], ['int32', 4], ['int64', 8], ['int128', 16],
  582. ['uint8', 1], ['uint16', 2], ['uint32', 4,], ['uint64', 8],
  583. ['float16', 2], ['float32', 4], ['float64', 8], ['bfloat16', 2],
  584. ['complex<float32>', 8], ['complex<float64>', 16], ['complex<int32>', 8],
  585. ['float8e4m3fn', 1], ['float8e4m3fnuz', 1], ['float8e5m2', 1], ['float8e5m2fnuz', 1], ['float8e4m3b11fnuz', 1], ['float8e3m4', 1], ['float8e4m3', 1], ['float4e2m1fn', 1], ['float6e2m3fn', 1], ['float6e3m2fn', 1], ['float8e8m0fnu', 1]
  586. ]);
  587. }
  588. get values() {
  589. this._read();
  590. return this._values;
  591. }
  592. get indices() {
  593. this._read();
  594. return this._indices;
  595. }
  596. get data() {
  597. this._read();
  598. if (this._data && this._data.peek) {
  599. this._data = this._data.peek();
  600. }
  601. return this._data;
  602. }
  603. get metrics() {
  604. return this._tensor.metrics;
  605. }
  606. get empty() {
  607. switch (this.layout) {
  608. case 'sparse':
  609. case 'sparse.coo': {
  610. return !this.values || !this.indices || this.values.values === null || this.values.values.length === 0;
  611. }
  612. default: {
  613. switch (this.encoding) {
  614. case '<':
  615. case '>':
  616. return !(Array.isArray(this.data) || this.data instanceof Uint8Array || this.data instanceof Int8Array) || this.data.length === 0;
  617. case '|':
  618. return !(Array.isArray(this.values) || ArrayBuffer.isView(this.values)) || this.values.length === 0;
  619. default:
  620. throw new Error(`Unsupported tensor encoding '${this.encoding}'.`);
  621. }
  622. }
  623. }
  624. }
  625. get value() {
  626. const context = this._context();
  627. context.limit = Number.MAX_SAFE_INTEGER;
  628. switch (context.encoding) {
  629. case '<':
  630. case '>': {
  631. return this._decodeData(context, 0, 0);
  632. }
  633. case '|': {
  634. return this._decodeValues(context, 0, 0);
  635. }
  636. default: {
  637. throw new Error(`Unsupported tensor encoding '${context.encoding}'.`);
  638. }
  639. }
  640. }
  641. toString() {
  642. const context = this._context();
  643. context.limit = 10000;
  644. switch (context.encoding) {
  645. case '<':
  646. case '>': {
  647. const value = this._decodeData(context, 0, 0);
  648. return base.Tensor._stringify(value, '', ' ');
  649. }
  650. case '|': {
  651. const value = this._decodeValues(context, 0, 0);
  652. return base.Tensor._stringify(value, '', ' ');
  653. }
  654. default: {
  655. throw new Error(`Unsupported tensor encoding '${context.encoding}'.`);
  656. }
  657. }
  658. }
  659. _context() {
  660. this._read();
  661. if (this.encoding !== '<' && this.encoding !== '>' && this.encoding !== '|') {
  662. throw new Error(`Tensor encoding '${this.encoding}' is not supported.`);
  663. }
  664. if (this.layout && (this.layout !== 'sparse' && this.layout !== 'sparse.coo')) {
  665. throw new Error(`Tensor layout '${this.layout}' is not supported.`);
  666. }
  667. const dataType = this.type.dataType;
  668. const context = {};
  669. context.encoding = this.encoding;
  670. context.dimensions = this.type.shape.dimensions.map((value) => typeof value === 'bigint' ? value.toNumber() : value);
  671. context.dataType = dataType;
  672. const shape = context.dimensions;
  673. context.stride = this.stride;
  674. if (!Array.isArray(context.stride) ||
  675. (Array.isArray(context.stride) && context.stride.length === 0 && shape.length === 0)) {
  676. const length = shape.length === 0 ? 1 : shape.length;
  677. context.stride = new Array(length);
  678. let value = 1;
  679. for (let i = length - 1; i >= 0; i--) {
  680. context.stride[i] = value;
  681. value *= shape[i];
  682. }
  683. }
  684. switch (this.layout) {
  685. case 'sparse': {
  686. const indices = new base.Tensor(this.indices).value;
  687. const values = new base.Tensor(this.values).value;
  688. context.data = this._decodeSparse(dataType, context.dimensions, indices, values);
  689. context.encoding = '|';
  690. break;
  691. }
  692. case 'sparse.coo': {
  693. const values = new base.Tensor(this.values).value;
  694. const data = new base.Tensor(this.indices).value;
  695. const dimensions = context.dimensions.length;
  696. let stride = 1;
  697. const strides = context.dimensions.slice().reverse().map((dim) => {
  698. const value = stride;
  699. stride *= dim;
  700. return value;
  701. }).reverse();
  702. const indices = new Uint32Array(values.length);
  703. for (let i = 0; i < dimensions; i++) {
  704. const stride = strides[i];
  705. const dimension = data[i];
  706. for (let j = 0; j < indices.length; j++) {
  707. indices[j] += dimension[j].toNumber() * stride;
  708. }
  709. }
  710. context.data = this._decodeSparse(dataType, context.dimensions, indices, values);
  711. context.encoding = '|';
  712. break;
  713. }
  714. default: {
  715. switch (this.encoding) {
  716. case '<':
  717. case '>': {
  718. context.data = (this.data instanceof Uint8Array || this.data instanceof Int8Array) ? this.data : this.data.peek();
  719. context.view = new DataView(context.data.buffer, context.data.byteOffset, context.data.byteLength);
  720. if (base.Tensor._dataTypes.has(dataType)) {
  721. const itemsize = base.Tensor._dataTypes.get(dataType);
  722. const length = context.data.length;
  723. const stride = context.stride;
  724. if (length < (itemsize * shape.reduce((a, v) => a * v, 1))) {
  725. const max = stride.reduce((a, v, i) => v > stride[i] ? i : a, 0);
  726. if (length !== (itemsize * stride[max] * shape[max])) {
  727. throw new Error('Invalid tensor data size.');
  728. }
  729. }
  730. context.itemsize = itemsize;
  731. context.stride = stride.map((v) => v * itemsize);
  732. } else if (dataType.startsWith('uint') && !isNaN(parseInt(dataType.substring(4), 10))) {
  733. context.dataType = 'uint';
  734. context.bits = parseInt(dataType.substring(4), 10);
  735. context.itemsize = 1;
  736. } else if (dataType.startsWith('int') && !isNaN(parseInt(dataType.substring(3), 10))) {
  737. context.dataType = 'int';
  738. context.bits = parseInt(dataType.substring(3), 10);
  739. context.itemsize = 1;
  740. } else if (dataType === 'float4e2m1') {
  741. context.dataType = 'float4e2m1';
  742. context.bits = 4;
  743. context.itemsize = 1;
  744. } else {
  745. throw new Error(`Tensor data type '${dataType}' is not implemented.`);
  746. }
  747. break;
  748. }
  749. case '|': {
  750. context.data = this.values;
  751. const integer = (dataType.startsWith('int') && !isNaN(parseInt(dataType.substring(3), 10))) || (dataType.startsWith('uint') && !isNaN(parseInt(dataType.substring(4), 10)));
  752. if (!base.Tensor._dataTypes.has(dataType) && dataType !== 'string' && dataType !== 'object' && dataType !== 'datetime' && dataType !== 'void' && !integer) {
  753. throw new Error(`Tensor data type '${dataType}' is not implemented.`);
  754. }
  755. const size = context.dimensions.reduce((a, v) => a * v, 1);
  756. if (size !== this.values.length) {
  757. throw new Error('Invalid tensor data length.');
  758. }
  759. break;
  760. }
  761. default: {
  762. throw new Error(`Unsupported tensor encoding '${this.encoding}'.`);
  763. }
  764. }
  765. }
  766. }
  767. context.index = 0;
  768. context.count = 0;
  769. return context;
  770. }
  771. _decodeSparse(dataType, dimensions, indices, values) {
  772. const size = dimensions.reduce((a, b) => a * b, 1);
  773. const array = new Array(size);
  774. switch (dataType) {
  775. case 'boolean':
  776. array.fill(false);
  777. break;
  778. default:
  779. array.fill(0);
  780. break;
  781. }
  782. if (indices.length > 0) {
  783. if (Object.prototype.hasOwnProperty.call(indices[0], 'low')) {
  784. for (let i = 0; i < indices.length; i++) {
  785. const index = indices[i].toNumber();
  786. array[index] = values[i];
  787. }
  788. } else {
  789. for (let i = 0; i < indices.length; i++) {
  790. array[indices[i]] = values[i];
  791. }
  792. }
  793. }
  794. return array;
  795. }
  796. _decodeData(context, dimension, offset) {
  797. const results = [];
  798. const shape = context.dimensions.length === 0 ? [1] : context.dimensions;
  799. const size = shape[dimension];
  800. const dataType = context.dataType;
  801. const view = context.view;
  802. const stride = context.stride[dimension];
  803. if (dimension === shape.length - 1) {
  804. const ellipsis = (context.count + size) > context.limit;
  805. const length = ellipsis ? context.limit - context.count : size;
  806. const max = offset + (length * stride);
  807. switch (dataType) {
  808. case 'boolean':
  809. for (; offset < max; offset += stride) {
  810. results.push(view.getUint8(offset) !== 0);
  811. }
  812. break;
  813. case 'qint8':
  814. case 'xint8':
  815. case 'int8':
  816. for (; offset < max; offset += stride) {
  817. results.push(view.getInt8(offset));
  818. }
  819. break;
  820. case 'qint16':
  821. case 'int16':
  822. for (; offset < max; offset += stride) {
  823. results.push(view.getInt16(offset, this._littleEndian));
  824. }
  825. break;
  826. case 'qint32':
  827. case 'int32':
  828. for (; offset < max; offset += stride) {
  829. results.push(view.getInt32(offset, this._littleEndian));
  830. }
  831. break;
  832. case 'int64':
  833. for (; offset < max; offset += stride) {
  834. results.push(view.getBigInt64(offset, this._littleEndian));
  835. }
  836. break;
  837. case 'int':
  838. for (; offset < max; offset += stride) {
  839. results.push(view.getIntBits(offset, context.bits, this._littleEndian));
  840. }
  841. break;
  842. case 'quint8':
  843. case 'uint8':
  844. for (; offset < max; offset += stride) {
  845. results.push(view.getUint8(offset));
  846. }
  847. break;
  848. case 'quint16':
  849. case 'uint16':
  850. for (; offset < max; offset += stride) {
  851. results.push(view.getUint16(offset, this._littleEndian));
  852. }
  853. break;
  854. case 'quint32':
  855. case 'uint32':
  856. for (; offset < max; offset += stride) {
  857. results.push(view.getUint32(offset, this._littleEndian));
  858. }
  859. break;
  860. case 'uint64':
  861. for (; offset < max; offset += stride) {
  862. results.push(view.getBigUint64(offset, this._littleEndian));
  863. }
  864. break;
  865. case 'uint':
  866. for (; offset < max; offset += stride) {
  867. results.push(view.getUintBits(offset, context.bits, this._littleEndian));
  868. }
  869. break;
  870. case 'float16':
  871. for (; offset < max; offset += stride) {
  872. results.push(view.getFloat16(offset, this._littleEndian));
  873. }
  874. break;
  875. case 'float32':
  876. for (; offset < max; offset += stride) {
  877. results.push(view.getFloat32(offset, this._littleEndian));
  878. }
  879. break;
  880. case 'float64':
  881. for (; offset < max; offset += stride) {
  882. results.push(view.getFloat64(offset, this._littleEndian));
  883. }
  884. break;
  885. case 'bfloat16':
  886. for (; offset < max; offset += stride) {
  887. results.push(view.getBfloat16(offset, this._littleEndian));
  888. }
  889. break;
  890. case 'complex<int32>':
  891. for (; offset < max; offset += stride) {
  892. results.push(view.getComplexInt32(offset, this._littleEndian));
  893. }
  894. break;
  895. case 'complex<float16>':
  896. for (; offset < max; offset += stride) {
  897. results.push(view.getComplexFloat16(offset, this._littleEndian));
  898. }
  899. break;
  900. case 'complex<float32>':
  901. for (; offset < max; offset += stride) {
  902. results.push(view.getComplexFloat32(offset, this._littleEndian));
  903. }
  904. break;
  905. case 'complex<float64>':
  906. for (; offset < max; offset += stride) {
  907. results.push(view.getComplexFloat64(offset, this._littleEndian));
  908. }
  909. break;
  910. case 'float4e2m1':
  911. for (; offset < max; offset += stride) {
  912. results.push(view.getFloat4e2m1(offset));
  913. }
  914. break;
  915. case 'float8e3m4':
  916. for (; offset < max; offset += stride) {
  917. results.push(view.getFloat8e3m4(offset));
  918. }
  919. break;
  920. case 'float8e4m3fn':
  921. for (; offset < max; offset += stride) {
  922. results.push(view.getFloat8e4m3(offset, true, false));
  923. }
  924. break;
  925. case 'float8e4m3fnuz':
  926. for (; offset < max; offset += stride) {
  927. results.push(view.getFloat8e4m3(offset, true, true));
  928. }
  929. break;
  930. case 'float8e5m2':
  931. for (; offset < max; offset += stride) {
  932. results.push(view.getFloat8e5m2(offset, false, false));
  933. }
  934. break;
  935. case 'float8e5m2fnuz':
  936. for (; offset < max; offset += stride) {
  937. results.push(view.getFloat8e5m2(offset, true, true));
  938. }
  939. break;
  940. default:
  941. throw new Error(`Unsupported tensor data type '${dataType}'.`);
  942. }
  943. context.count += length;
  944. if (ellipsis) {
  945. results.push('...');
  946. }
  947. } else {
  948. for (let j = 0; j < size; j++) {
  949. if (context.count >= context.limit) {
  950. results.push('...');
  951. return results;
  952. }
  953. const nextOffset = offset + (j * stride);
  954. results.push(this._decodeData(context, dimension + 1, nextOffset));
  955. }
  956. }
  957. if (context.dimensions.length === 0) {
  958. return results[0];
  959. }
  960. return results;
  961. }
  962. _decodeValues(context, dimension, position) {
  963. const results = [];
  964. const shape = (context.dimensions.length === 0) ? [1] : context.dimensions;
  965. const size = shape[dimension];
  966. const dataType = context.dataType;
  967. const stride = context.stride[dimension];
  968. if (dimension === shape.length - 1) {
  969. const ellipsis = (context.count + size) > context.limit;
  970. const length = ellipsis ? context.limit - context.count : size;
  971. const data = context.data;
  972. for (let i = 0; i < length; i++) {
  973. if (context.count > context.limit) {
  974. results.push('...');
  975. return results;
  976. }
  977. switch (dataType) {
  978. case 'boolean':
  979. results.push(data[position] === 0 || data[position] === false ? false : true);
  980. break;
  981. default:
  982. results.push(data[position]);
  983. break;
  984. }
  985. position += stride;
  986. context.count++;
  987. }
  988. } else {
  989. for (let i = 0; i < size; i++) {
  990. if (context.count >= context.limit) {
  991. results.push('...');
  992. return results;
  993. }
  994. const nextPosition = position + (i * stride);
  995. results.push(this._decodeValues(context, dimension + 1, nextPosition));
  996. }
  997. }
  998. if (context.dimensions.length === 0) {
  999. return results[0];
  1000. }
  1001. return results;
  1002. }
  1003. static _stringify(value, indentation, indent) {
  1004. if (Array.isArray(value)) {
  1005. const length = value.length;
  1006. if (length > 0) {
  1007. const array = new Array(length);
  1008. const space = indentation + indent;
  1009. for (let i = 0; i < length; i++) {
  1010. array[i] = base.Tensor._stringify(value[i], space, indent);
  1011. }
  1012. return `${indentation}[\n${array.join(',\n')}\n${indentation}]`;
  1013. }
  1014. return `${indentation}[\n${indentation}]`;
  1015. }
  1016. if (value === null) {
  1017. return `${indentation}null`;
  1018. }
  1019. switch (typeof value) {
  1020. case 'boolean':
  1021. case 'number':
  1022. case 'bigint':
  1023. return `${indentation}${value}`;
  1024. case 'string':
  1025. return `${indentation}"${value}"`;
  1026. default:
  1027. if (value instanceof Uint8Array) {
  1028. let content = '';
  1029. for (let i = 0; i < value.length; i++) {
  1030. const x = value[i];
  1031. content += x >= 32 && x <= 126 ? String.fromCharCode(x) : `\\x${x.toString(16).padStart(2, '0')}`;
  1032. }
  1033. return `${indentation}"${content}"`;
  1034. }
  1035. if (value && value.toString) {
  1036. return `${indentation}${value.toString()}`;
  1037. }
  1038. return `${indentation}(undefined)`;
  1039. }
  1040. }
  1041. _read() {
  1042. if (this._values === undefined) {
  1043. this._values = null;
  1044. switch (this.encoding) {
  1045. case undefined:
  1046. case '<': {
  1047. this._data = this._tensor.values;
  1048. this._littleEndian = true;
  1049. break;
  1050. }
  1051. case '>': {
  1052. this._data = this._tensor.values;
  1053. this._littleEndian = false;
  1054. break;
  1055. }
  1056. case '|': {
  1057. this._values = this._tensor.values;
  1058. break;
  1059. }
  1060. default: {
  1061. throw new Error(`Unsupported tensor encoding '${this.encoding}'.`);
  1062. }
  1063. }
  1064. switch (this.layout) {
  1065. case 'sparse':
  1066. case 'sparse.coo': {
  1067. this._indices = this._tensor.indices;
  1068. this._values = this._tensor.values;
  1069. break;
  1070. }
  1071. default: {
  1072. break;
  1073. }
  1074. }
  1075. }
  1076. }
  1077. };
  1078. base.Telemetry = class {
  1079. constructor(window) {
  1080. this._window = window;
  1081. this._navigator = window.navigator;
  1082. this._config = new Map();
  1083. this._metadata = {};
  1084. this._schema = new Map([
  1085. ['protocol_version', 'v'],
  1086. ['tracking_id', 'tid'],
  1087. ['hash_info', 'gtm'],
  1088. ['_page_id', '_p'],
  1089. ['client_id', 'cid'],
  1090. ['language', 'ul'],
  1091. ['screen_resolution', 'sr'],
  1092. ['_user_agent_architecture', 'uaa'],
  1093. ['_user_agent_bitness', 'uab'],
  1094. ['_user_agent_full_version_list', 'uafvl'],
  1095. ['_user_agent_mobile', 'uamb'],
  1096. ['_user_agent_model', 'uam'],
  1097. ['_user_agent_platform', 'uap'],
  1098. ['_user_agent_platform_version', 'uapv'],
  1099. ['_user_agent_wow64', 'uaw'],
  1100. ['hit_count', '_s'],
  1101. ['session_id', 'sid'],
  1102. ['session_number', 'sct'],
  1103. ['session_engaged', 'seg'],
  1104. ['engagement_time_msec', '_et'],
  1105. ['page_location', 'dl'],
  1106. ['page_title', 'dt'],
  1107. ['page_referrer', 'dr'],
  1108. ['is_first_visit', '_fv'],
  1109. ['is_external_event', '_ee'],
  1110. ['is_new_to_site', '_nsi'],
  1111. ['is_session_start', '_ss'],
  1112. ['event_name', 'en']
  1113. ]);
  1114. }
  1115. async start(measurement_id, client_id, session) {
  1116. this._session = session && typeof session === 'string' ? session.replace(/^GS1\.1\./, '').split('.') : null;
  1117. this._session = Array.isArray(this._session) && this._session.length >= 7 ? this._session : ['0', '0', '0', '0', '0', '0', '0'];
  1118. this._session[0] = Date.now();
  1119. this._session[1] = parseInt(this._session[1], 10) + 1;
  1120. this._engagement_time_msec = 0;
  1121. if (this._config.size > 0) {
  1122. throw new Error('Invalid session state.');
  1123. }
  1124. this.set('protocol_version', 2);
  1125. this.set('tracking_id', measurement_id);
  1126. this.set('hash_info', '2oebu0');
  1127. this.set('_page_id', Math.floor(Math.random() * 2147483648));
  1128. client_id = client_id ? client_id.replace(/^(GA1\.\d\.)*/, '') : null;
  1129. if (client_id && client_id.indexOf('.') !== 1) {
  1130. this.set('client_id', client_id);
  1131. } else {
  1132. const random = String(Math.round(0x7FFFFFFF * Math.random()));
  1133. const time = Date.now();
  1134. const value = [random, Math.round(time / 1e3)].join('.');
  1135. this.set('client_id', value);
  1136. this._metadata.is_first_visit = 1;
  1137. this._metadata.is_new_to_site = 1;
  1138. }
  1139. this.set('language', ((this._navigator && (this._navigator.language || this._navigator.browserLanguage)) || '').toLowerCase());
  1140. this.set('screen_resolution', `${window.screen ? window.screen.width : 0}x${window.screen ? window.screen.height : 0}`);
  1141. if (this._navigator && this._navigator.userAgentData && this._navigator.userAgentData.getHighEntropyValues) {
  1142. const values = await this._navigator.userAgentData.getHighEntropyValues(['platform', 'platformVersion', 'architecture', 'model', 'uaFullVersion', 'bitness', 'fullVersionList', 'wow64']);
  1143. if (values) {
  1144. this.set('_user_agent_architecture', values.architecture);
  1145. this.set('_user_agent_bitness', values.bitness);
  1146. this.set('_user_agent_full_version_list', Array.isArray(values.fullVersionList) ? values.fullVersionList.map((h) => `${encodeURIComponent(h.brand || '')};${encodeURIComponent(h.version || '')}`).join('|') : '');
  1147. this.set('_user_agent_mobile', values.mobile ? 1 : 0);
  1148. this.set('_user_agent_model', values.model);
  1149. this.set('_user_agent_platform', values.platform);
  1150. this.set('_user_agent_platform_version', values.platformVersion);
  1151. this.set('_user_agent_wow64', values.wow64 ? 1 : 0);
  1152. }
  1153. }
  1154. this.set('hit_count', 1);
  1155. this.set('session_id', this._session[0]);
  1156. this.set('session_number', this._session[1]);
  1157. this.set('session_engaged', 0);
  1158. this._metadata.is_session_start = 1;
  1159. this._metadata.is_external_event = 1;
  1160. window.addEventListener('focus', () => this._update(true, undefined, undefined));
  1161. window.addEventListener('blur', () => this._update(false, undefined, undefined));
  1162. window.addEventListener('pageshow', () => this._update(undefined, true, undefined));
  1163. window.addEventListener('pagehide', () => this._update(undefined, false, undefined));
  1164. window.addEventListener('visibilitychange', () => this._update(undefined, undefined, window.document.visibilityState !== 'hidden'));
  1165. window.addEventListener('beforeunload', () => this._update() && this.send('user_engagement', {}));
  1166. }
  1167. get session() {
  1168. return this._session ? this._session.join('.') : null;
  1169. }
  1170. set(name, value) {
  1171. const key = this._schema.get(name);
  1172. if (value !== undefined && value !== null) {
  1173. this._config.set(key, value);
  1174. } else if (this._config.has(key)) {
  1175. this._config.delete(key);
  1176. }
  1177. this._cache = null;
  1178. }
  1179. get(name) {
  1180. const key = this._schema.get(name);
  1181. return this._config.get(key);
  1182. }
  1183. send(name, params) {
  1184. if (this._session) {
  1185. try {
  1186. params = { event_name: name, ...this._metadata, ...params };
  1187. this._metadata = {};
  1188. if (this._update()) {
  1189. params.engagement_time_msec = this._engagement_time_msec;
  1190. this._engagement_time_msec = 0;
  1191. }
  1192. const build = (entries) => entries.map(([name, value]) => `${name}=${encodeURIComponent(value)}`).join('&');
  1193. this._cache = this._cache || build(Array.from(this._config));
  1194. const key = (name, value) => this._schema.get(name) || (typeof value === 'number' && !isNaN(value) ? 'epn.' : 'ep.') + name;
  1195. const body = build(Object.entries(params).map(([name, value]) => [key(name, value), value]));
  1196. const url = `https://analytics.google.com/g/collect?${this._cache}`;
  1197. this._navigator.sendBeacon(url, body);
  1198. this._session[2] = this.get('session_engaged') || '0';
  1199. this.set('hit_count', this.get('hit_count') + 1);
  1200. } catch {
  1201. // continue regardless of error
  1202. }
  1203. }
  1204. }
  1205. _update(focused, page, visible) {
  1206. this._focused = focused === true || focused === false ? focused : this._focused;
  1207. this._page = page === true || page === false ? page : this._page;
  1208. this._visible = visible === true || visible === false ? visible : this._visible;
  1209. const time = Date.now();
  1210. if (this._start_time) {
  1211. this._engagement_time_msec += (time - this._start_time);
  1212. this._start_time = 0;
  1213. }
  1214. if (this._focused !== false && this._page !== false && this._visible !== false) {
  1215. this._start_time = time;
  1216. }
  1217. return this._engagement_time_msec > 20;
  1218. }
  1219. };
  1220. base.Metadata = class {
  1221. get extensions() {
  1222. return [
  1223. 'onnx', 'tflite', 'pb', 'pt', 'pt2', 'pte', 'pth', 'h5', 'pbtxt', 'prototxt', 'caffemodel', 'mlmodel', 'mlpackage',
  1224. 'model', 'json', 'xml', 'cfg', 'weights', 'bin',
  1225. 'ort',
  1226. 'dnn', 'cmf',
  1227. 'gguf',
  1228. 'hd5', 'hdf5', 'keras',
  1229. 'tfl', 'circle', 'lite',
  1230. 'mlir', 'mlirbc', 'mlnet', 'mar', 'maxviz', 'meta', 'nn', 'ngf', 'hn',
  1231. 'param', 'params',
  1232. 'paddle', 'pdiparams', 'pdmodel', 'pdopt', 'pdparams', 'nb',
  1233. 'pkl', 'pickle', 'joblib', 'safetensors',
  1234. 'ptl', 't7',
  1235. 'dlc', 'uff', 'armnn', 'kann', 'kgraph',
  1236. 'mnn', 'ms', 'ncnn', 'om', 'tm', 'mge', 'tmfile', 'tnnproto', 'xmodel', 'kmodel', 'rknn',
  1237. 'tar', 'zip'
  1238. ];
  1239. }
  1240. };
  1241. export const Complex = base.Complex;
  1242. export const BinaryStream = base.BinaryStream;
  1243. export const BinaryReader = base.BinaryReader;
  1244. export const Tensor = base.Tensor;
  1245. export const Telemetry = base.Telemetry;
  1246. export const Metadata = base.Metadata;