index-no-eval.cjs 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  3. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.msgpackr = {}));
  5. })(this, (function (exports) { 'use strict';
  6. var decoder;
  7. try {
  8. decoder = new TextDecoder();
  9. } catch(error) {}
  10. var src;
  11. var srcEnd;
  12. var position$1 = 0;
  13. var currentUnpackr = {};
  14. var currentStructures;
  15. var srcString;
  16. var srcStringStart = 0;
  17. var srcStringEnd = 0;
  18. var bundledStrings$1;
  19. var referenceMap;
  20. var currentExtensions = [];
  21. var dataView;
  22. var defaultOptions = {
  23. useRecords: false,
  24. mapsAsObjects: true
  25. };
  26. class C1Type {}
  27. const C1 = new C1Type();
  28. C1.name = 'MessagePack 0xC1';
  29. var sequentialMode = false;
  30. var inlineObjectReadThreshold = 2;
  31. var readStruct;
  32. var BlockedFunction; // we use search and replace to change the next call to BlockedFunction to avoid CSP issues for
  33. // no-eval build
  34. try {
  35. new BlockedFunction ('');
  36. } catch(error) {
  37. // if eval variants are not supported, do not create inline object readers ever
  38. inlineObjectReadThreshold = Infinity;
  39. }
  40. class Unpackr {
  41. constructor(options) {
  42. if (options) {
  43. if (options.useRecords === false && options.mapsAsObjects === undefined)
  44. options.mapsAsObjects = true;
  45. if (options.sequential && options.trusted !== false) {
  46. options.trusted = true;
  47. if (!options.structures && options.useRecords != false) {
  48. options.structures = [];
  49. if (!options.maxSharedStructures)
  50. options.maxSharedStructures = 0;
  51. }
  52. }
  53. if (options.structures)
  54. options.structures.sharedLength = options.structures.length;
  55. else if (options.getStructures) {
  56. (options.structures = []).uninitialized = true; // this is what we use to denote an uninitialized structures
  57. options.structures.sharedLength = 0;
  58. }
  59. if (options.int64AsNumber) {
  60. options.int64AsType = 'number';
  61. }
  62. }
  63. Object.assign(this, options);
  64. }
  65. unpack(source, options) {
  66. if (src) {
  67. // re-entrant execution, save the state and restore it after we do this unpack
  68. return saveState(() => {
  69. clearSource();
  70. return this ? this.unpack(source, options) : Unpackr.prototype.unpack.call(defaultOptions, source, options)
  71. })
  72. }
  73. if (!source.buffer && source.constructor === ArrayBuffer)
  74. source = typeof Buffer !== 'undefined' ? Buffer.from(source) : new Uint8Array(source);
  75. if (typeof options === 'object') {
  76. srcEnd = options.end || source.length;
  77. position$1 = options.start || 0;
  78. } else {
  79. position$1 = 0;
  80. srcEnd = options > -1 ? options : source.length;
  81. }
  82. srcStringEnd = 0;
  83. srcString = null;
  84. bundledStrings$1 = null;
  85. src = source;
  86. // this provides cached access to the data view for a buffer if it is getting reused, which is a recommend
  87. // technique for getting data from a database where it can be copied into an existing buffer instead of creating
  88. // new ones
  89. try {
  90. dataView = source.dataView || (source.dataView = new DataView(source.buffer, source.byteOffset, source.byteLength));
  91. } catch(error) {
  92. // if it doesn't have a buffer, maybe it is the wrong type of object
  93. src = null;
  94. if (source instanceof Uint8Array)
  95. throw error
  96. throw new Error('Source must be a Uint8Array or Buffer but was a ' + ((source && typeof source == 'object') ? source.constructor.name : typeof source))
  97. }
  98. if (this instanceof Unpackr) {
  99. currentUnpackr = this;
  100. if (this.structures) {
  101. currentStructures = this.structures;
  102. return checkedRead(options)
  103. } else if (!currentStructures || currentStructures.length > 0) {
  104. currentStructures = [];
  105. }
  106. } else {
  107. currentUnpackr = defaultOptions;
  108. if (!currentStructures || currentStructures.length > 0)
  109. currentStructures = [];
  110. }
  111. return checkedRead(options)
  112. }
  113. unpackMultiple(source, forEach) {
  114. let values, lastPosition = 0;
  115. try {
  116. sequentialMode = true;
  117. let size = source.length;
  118. let value = this ? this.unpack(source, size) : defaultUnpackr.unpack(source, size);
  119. if (forEach) {
  120. if (forEach(value, lastPosition, position$1) === false) return;
  121. while(position$1 < size) {
  122. lastPosition = position$1;
  123. if (forEach(checkedRead(), lastPosition, position$1) === false) {
  124. return
  125. }
  126. }
  127. }
  128. else {
  129. values = [ value ];
  130. while(position$1 < size) {
  131. lastPosition = position$1;
  132. values.push(checkedRead());
  133. }
  134. return values
  135. }
  136. } catch(error) {
  137. error.lastPosition = lastPosition;
  138. error.values = values;
  139. throw error
  140. } finally {
  141. sequentialMode = false;
  142. clearSource();
  143. }
  144. }
  145. _mergeStructures(loadedStructures, existingStructures) {
  146. loadedStructures = loadedStructures || [];
  147. if (Object.isFrozen(loadedStructures))
  148. loadedStructures = loadedStructures.map(structure => structure.slice(0));
  149. for (let i = 0, l = loadedStructures.length; i < l; i++) {
  150. let structure = loadedStructures[i];
  151. if (structure) {
  152. structure.isShared = true;
  153. if (i >= 32)
  154. structure.highByte = (i - 32) >> 5;
  155. }
  156. }
  157. loadedStructures.sharedLength = loadedStructures.length;
  158. for (let id in existingStructures || []) {
  159. if (id >= 0) {
  160. let structure = loadedStructures[id];
  161. let existing = existingStructures[id];
  162. if (existing) {
  163. if (structure)
  164. (loadedStructures.restoreStructures || (loadedStructures.restoreStructures = []))[id] = structure;
  165. loadedStructures[id] = existing;
  166. }
  167. }
  168. }
  169. return this.structures = loadedStructures
  170. }
  171. decode(source, options) {
  172. return this.unpack(source, options)
  173. }
  174. }
  175. function checkedRead(options) {
  176. try {
  177. if (!currentUnpackr.trusted && !sequentialMode) {
  178. let sharedLength = currentStructures.sharedLength || 0;
  179. if (sharedLength < currentStructures.length)
  180. currentStructures.length = sharedLength;
  181. }
  182. let result;
  183. if (currentUnpackr.randomAccessStructure && src[position$1] < 0x40 && src[position$1] >= 0x20 && readStruct) {
  184. result = readStruct(src, position$1, srcEnd, currentUnpackr);
  185. src = null; // dispose of this so that recursive unpack calls don't save state
  186. if (!(options && options.lazy) && result)
  187. result = result.toJSON();
  188. position$1 = srcEnd;
  189. } else
  190. result = read();
  191. if (bundledStrings$1) { // bundled strings to skip past
  192. position$1 = bundledStrings$1.postBundlePosition;
  193. bundledStrings$1 = null;
  194. }
  195. if (sequentialMode)
  196. // we only need to restore the structures if there was an error, but if we completed a read,
  197. // we can clear this out and keep the structures we read
  198. currentStructures.restoreStructures = null;
  199. if (position$1 == srcEnd) {
  200. // finished reading this source, cleanup references
  201. if (currentStructures && currentStructures.restoreStructures)
  202. restoreStructures();
  203. currentStructures = null;
  204. src = null;
  205. if (referenceMap)
  206. referenceMap = null;
  207. } else if (position$1 > srcEnd) {
  208. // over read
  209. throw new Error('Unexpected end of MessagePack data')
  210. } else if (!sequentialMode) {
  211. let jsonView;
  212. try {
  213. jsonView = JSON.stringify(result, (_, value) => typeof value === "bigint" ? `${value}n` : value).slice(0, 100);
  214. } catch(error) {
  215. jsonView = '(JSON view not available ' + error + ')';
  216. }
  217. throw new Error('Data read, but end of buffer not reached ' + jsonView)
  218. }
  219. // else more to read, but we are reading sequentially, so don't clear source yet
  220. return result
  221. } catch(error) {
  222. if (currentStructures && currentStructures.restoreStructures)
  223. restoreStructures();
  224. clearSource();
  225. if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer') || position$1 > srcEnd) {
  226. error.incomplete = true;
  227. }
  228. throw error
  229. }
  230. }
  231. function restoreStructures() {
  232. for (let id in currentStructures.restoreStructures) {
  233. currentStructures[id] = currentStructures.restoreStructures[id];
  234. }
  235. currentStructures.restoreStructures = null;
  236. }
  237. function read() {
  238. let token = src[position$1++];
  239. if (token < 0xa0) {
  240. if (token < 0x80) {
  241. if (token < 0x40)
  242. return token
  243. else {
  244. let structure = currentStructures[token & 0x3f] ||
  245. currentUnpackr.getStructures && loadStructures()[token & 0x3f];
  246. if (structure) {
  247. if (!structure.read) {
  248. structure.read = createStructureReader(structure, token & 0x3f);
  249. }
  250. return structure.read()
  251. } else
  252. return token
  253. }
  254. } else if (token < 0x90) {
  255. // map
  256. token -= 0x80;
  257. if (currentUnpackr.mapsAsObjects) {
  258. let object = {};
  259. for (let i = 0; i < token; i++) {
  260. let key = readKey();
  261. if (key === '__proto__')
  262. key = '__proto_';
  263. object[key] = read();
  264. }
  265. return object
  266. } else {
  267. let map = new Map();
  268. for (let i = 0; i < token; i++) {
  269. map.set(read(), read());
  270. }
  271. return map
  272. }
  273. } else {
  274. token -= 0x90;
  275. let array = new Array(token);
  276. for (let i = 0; i < token; i++) {
  277. array[i] = read();
  278. }
  279. if (currentUnpackr.freezeData)
  280. return Object.freeze(array)
  281. return array
  282. }
  283. } else if (token < 0xc0) {
  284. // fixstr
  285. let length = token - 0xa0;
  286. if (srcStringEnd >= position$1) {
  287. return srcString.slice(position$1 - srcStringStart, (position$1 += length) - srcStringStart)
  288. }
  289. if (srcStringEnd == 0 && srcEnd < 140) {
  290. // for small blocks, avoiding the overhead of the extract call is helpful
  291. let string = length < 16 ? shortStringInJS(length) : longStringInJS(length);
  292. if (string != null)
  293. return string
  294. }
  295. return readFixedString(length)
  296. } else {
  297. let value;
  298. switch (token) {
  299. case 0xc0: return null
  300. case 0xc1:
  301. if (bundledStrings$1) {
  302. value = read(); // followed by the length of the string in characters (not bytes!)
  303. if (value > 0)
  304. return bundledStrings$1[1].slice(bundledStrings$1.position1, bundledStrings$1.position1 += value)
  305. else
  306. return bundledStrings$1[0].slice(bundledStrings$1.position0, bundledStrings$1.position0 -= value)
  307. }
  308. return C1; // "never-used", return special object to denote that
  309. case 0xc2: return false
  310. case 0xc3: return true
  311. case 0xc4:
  312. // bin 8
  313. value = src[position$1++];
  314. if (value === undefined)
  315. throw new Error('Unexpected end of buffer')
  316. return readBin(value)
  317. case 0xc5:
  318. // bin 16
  319. value = dataView.getUint16(position$1);
  320. position$1 += 2;
  321. return readBin(value)
  322. case 0xc6:
  323. // bin 32
  324. value = dataView.getUint32(position$1);
  325. position$1 += 4;
  326. return readBin(value)
  327. case 0xc7:
  328. // ext 8
  329. return readExt(src[position$1++])
  330. case 0xc8:
  331. // ext 16
  332. value = dataView.getUint16(position$1);
  333. position$1 += 2;
  334. return readExt(value)
  335. case 0xc9:
  336. // ext 32
  337. value = dataView.getUint32(position$1);
  338. position$1 += 4;
  339. return readExt(value)
  340. case 0xca:
  341. value = dataView.getFloat32(position$1);
  342. if (currentUnpackr.useFloat32 > 2) {
  343. // this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
  344. let multiplier = mult10[((src[position$1] & 0x7f) << 1) | (src[position$1 + 1] >> 7)];
  345. position$1 += 4;
  346. return ((multiplier * value + (value > 0 ? 0.5 : -0.5)) >> 0) / multiplier
  347. }
  348. position$1 += 4;
  349. return value
  350. case 0xcb:
  351. value = dataView.getFloat64(position$1);
  352. position$1 += 8;
  353. return value
  354. // uint handlers
  355. case 0xcc:
  356. return src[position$1++]
  357. case 0xcd:
  358. value = dataView.getUint16(position$1);
  359. position$1 += 2;
  360. return value
  361. case 0xce:
  362. value = dataView.getUint32(position$1);
  363. position$1 += 4;
  364. return value
  365. case 0xcf:
  366. if (currentUnpackr.int64AsType === 'number') {
  367. value = dataView.getUint32(position$1) * 0x100000000;
  368. value += dataView.getUint32(position$1 + 4);
  369. } else if (currentUnpackr.int64AsType === 'string') {
  370. value = dataView.getBigUint64(position$1).toString();
  371. } else if (currentUnpackr.int64AsType === 'auto') {
  372. value = dataView.getBigUint64(position$1);
  373. if (value<=BigInt(2)<<BigInt(52)) value=Number(value);
  374. } else
  375. value = dataView.getBigUint64(position$1);
  376. position$1 += 8;
  377. return value
  378. // int handlers
  379. case 0xd0:
  380. return dataView.getInt8(position$1++)
  381. case 0xd1:
  382. value = dataView.getInt16(position$1);
  383. position$1 += 2;
  384. return value
  385. case 0xd2:
  386. value = dataView.getInt32(position$1);
  387. position$1 += 4;
  388. return value
  389. case 0xd3:
  390. if (currentUnpackr.int64AsType === 'number') {
  391. value = dataView.getInt32(position$1) * 0x100000000;
  392. value += dataView.getUint32(position$1 + 4);
  393. } else if (currentUnpackr.int64AsType === 'string') {
  394. value = dataView.getBigInt64(position$1).toString();
  395. } else if (currentUnpackr.int64AsType === 'auto') {
  396. value = dataView.getBigInt64(position$1);
  397. if (value>=BigInt(-2)<<BigInt(52)&&value<=BigInt(2)<<BigInt(52)) value=Number(value);
  398. } else
  399. value = dataView.getBigInt64(position$1);
  400. position$1 += 8;
  401. return value
  402. case 0xd4:
  403. // fixext 1
  404. value = src[position$1++];
  405. if (value == 0x72) {
  406. return recordDefinition(src[position$1++] & 0x3f)
  407. } else {
  408. let extension = currentExtensions[value];
  409. if (extension) {
  410. if (extension.read) {
  411. position$1++; // skip filler byte
  412. return extension.read(read())
  413. } else if (extension.noBuffer) {
  414. position$1++; // skip filler byte
  415. return extension()
  416. } else
  417. return extension(src.subarray(position$1, ++position$1))
  418. } else
  419. throw new Error('Unknown extension ' + value)
  420. }
  421. case 0xd5:
  422. // fixext 2
  423. value = src[position$1];
  424. if (value == 0x72) {
  425. position$1++;
  426. return recordDefinition(src[position$1++] & 0x3f, src[position$1++])
  427. } else
  428. return readExt(2)
  429. case 0xd6:
  430. // fixext 4
  431. return readExt(4)
  432. case 0xd7:
  433. // fixext 8
  434. return readExt(8)
  435. case 0xd8:
  436. // fixext 16
  437. return readExt(16)
  438. case 0xd9:
  439. // str 8
  440. value = src[position$1++];
  441. if (srcStringEnd >= position$1) {
  442. return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
  443. }
  444. return readString8(value)
  445. case 0xda:
  446. // str 16
  447. value = dataView.getUint16(position$1);
  448. position$1 += 2;
  449. if (srcStringEnd >= position$1) {
  450. return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
  451. }
  452. return readString16(value)
  453. case 0xdb:
  454. // str 32
  455. value = dataView.getUint32(position$1);
  456. position$1 += 4;
  457. if (srcStringEnd >= position$1) {
  458. return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
  459. }
  460. return readString32(value)
  461. case 0xdc:
  462. // array 16
  463. value = dataView.getUint16(position$1);
  464. position$1 += 2;
  465. return readArray(value)
  466. case 0xdd:
  467. // array 32
  468. value = dataView.getUint32(position$1);
  469. position$1 += 4;
  470. return readArray(value)
  471. case 0xde:
  472. // map 16
  473. value = dataView.getUint16(position$1);
  474. position$1 += 2;
  475. return readMap(value)
  476. case 0xdf:
  477. // map 32
  478. value = dataView.getUint32(position$1);
  479. position$1 += 4;
  480. return readMap(value)
  481. default: // negative int
  482. if (token >= 0xe0)
  483. return token - 0x100
  484. if (token === undefined) {
  485. let error = new Error('Unexpected end of MessagePack data');
  486. error.incomplete = true;
  487. throw error
  488. }
  489. throw new Error('Unknown MessagePack token ' + token)
  490. }
  491. }
  492. }
  493. const validName = /^[a-zA-Z_$][a-zA-Z\d_$]*$/;
  494. function createStructureReader(structure, firstId) {
  495. function readObject() {
  496. // This initial function is quick to instantiate, but runs slower. After several iterations pay the cost to build the faster function
  497. if (readObject.count++ > inlineObjectReadThreshold) {
  498. let readObject = structure.read = (new BlockedFunction ('r', 'return function(){return ' + (currentUnpackr.freezeData ? 'Object.freeze' : '') +
  499. '({' + structure.map(key => key === '__proto__' ? '__proto_:r()' : validName.test(key) ? key + ':r()' : ('[' + JSON.stringify(key) + ']:r()')).join(',') + '})}'))(read);
  500. if (structure.highByte === 0)
  501. structure.read = createSecondByteReader(firstId, structure.read);
  502. return readObject() // second byte is already read, if there is one so immediately read object
  503. }
  504. let object = {};
  505. for (let i = 0, l = structure.length; i < l; i++) {
  506. let key = structure[i];
  507. if (key === '__proto__')
  508. key = '__proto_';
  509. object[key] = read();
  510. }
  511. if (currentUnpackr.freezeData)
  512. return Object.freeze(object);
  513. return object
  514. }
  515. readObject.count = 0;
  516. if (structure.highByte === 0) {
  517. return createSecondByteReader(firstId, readObject)
  518. }
  519. return readObject
  520. }
  521. const createSecondByteReader = (firstId, read0) => {
  522. return function() {
  523. let highByte = src[position$1++];
  524. if (highByte === 0)
  525. return read0()
  526. let id = firstId < 32 ? -(firstId + (highByte << 5)) : firstId + (highByte << 5);
  527. let structure = currentStructures[id] || loadStructures()[id];
  528. if (!structure) {
  529. throw new Error('Record id is not defined for ' + id)
  530. }
  531. if (!structure.read)
  532. structure.read = createStructureReader(structure, firstId);
  533. return structure.read()
  534. }
  535. };
  536. function loadStructures() {
  537. let loadedStructures = saveState(() => {
  538. // save the state in case getStructures modifies our buffer
  539. src = null;
  540. return currentUnpackr.getStructures()
  541. });
  542. return currentStructures = currentUnpackr._mergeStructures(loadedStructures, currentStructures)
  543. }
  544. var readFixedString = readStringJS;
  545. var readString8 = readStringJS;
  546. var readString16 = readStringJS;
  547. var readString32 = readStringJS;
  548. let isNativeAccelerationEnabled = false;
  549. function readStringJS(length) {
  550. let result;
  551. if (length < 16) {
  552. if (result = shortStringInJS(length))
  553. return result
  554. }
  555. if (length > 64 && decoder)
  556. return decoder.decode(src.subarray(position$1, position$1 += length))
  557. const end = position$1 + length;
  558. const units = [];
  559. result = '';
  560. while (position$1 < end) {
  561. const byte1 = src[position$1++];
  562. if ((byte1 & 0x80) === 0) {
  563. // 1 byte
  564. units.push(byte1);
  565. } else if ((byte1 & 0xe0) === 0xc0) {
  566. // 2 bytes
  567. const byte2 = src[position$1++] & 0x3f;
  568. units.push(((byte1 & 0x1f) << 6) | byte2);
  569. } else if ((byte1 & 0xf0) === 0xe0) {
  570. // 3 bytes
  571. const byte2 = src[position$1++] & 0x3f;
  572. const byte3 = src[position$1++] & 0x3f;
  573. units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
  574. } else if ((byte1 & 0xf8) === 0xf0) {
  575. // 4 bytes
  576. const byte2 = src[position$1++] & 0x3f;
  577. const byte3 = src[position$1++] & 0x3f;
  578. const byte4 = src[position$1++] & 0x3f;
  579. let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
  580. if (unit > 0xffff) {
  581. unit -= 0x10000;
  582. units.push(((unit >>> 10) & 0x3ff) | 0xd800);
  583. unit = 0xdc00 | (unit & 0x3ff);
  584. }
  585. units.push(unit);
  586. } else {
  587. units.push(byte1);
  588. }
  589. if (units.length >= 0x1000) {
  590. result += fromCharCode.apply(String, units);
  591. units.length = 0;
  592. }
  593. }
  594. if (units.length > 0) {
  595. result += fromCharCode.apply(String, units);
  596. }
  597. return result
  598. }
  599. function readArray(length) {
  600. let array = new Array(length);
  601. for (let i = 0; i < length; i++) {
  602. array[i] = read();
  603. }
  604. if (currentUnpackr.freezeData)
  605. return Object.freeze(array)
  606. return array
  607. }
  608. function readMap(length) {
  609. if (currentUnpackr.mapsAsObjects) {
  610. let object = {};
  611. for (let i = 0; i < length; i++) {
  612. let key = readKey();
  613. if (key === '__proto__')
  614. key = '__proto_';
  615. object[key] = read();
  616. }
  617. return object
  618. } else {
  619. let map = new Map();
  620. for (let i = 0; i < length; i++) {
  621. map.set(read(), read());
  622. }
  623. return map
  624. }
  625. }
  626. var fromCharCode = String.fromCharCode;
  627. function longStringInJS(length) {
  628. let start = position$1;
  629. let bytes = new Array(length);
  630. for (let i = 0; i < length; i++) {
  631. const byte = src[position$1++];
  632. if ((byte & 0x80) > 0) {
  633. position$1 = start;
  634. return
  635. }
  636. bytes[i] = byte;
  637. }
  638. return fromCharCode.apply(String, bytes)
  639. }
  640. function shortStringInJS(length) {
  641. if (length < 4) {
  642. if (length < 2) {
  643. if (length === 0)
  644. return ''
  645. else {
  646. let a = src[position$1++];
  647. if ((a & 0x80) > 1) {
  648. position$1 -= 1;
  649. return
  650. }
  651. return fromCharCode(a)
  652. }
  653. } else {
  654. let a = src[position$1++];
  655. let b = src[position$1++];
  656. if ((a & 0x80) > 0 || (b & 0x80) > 0) {
  657. position$1 -= 2;
  658. return
  659. }
  660. if (length < 3)
  661. return fromCharCode(a, b)
  662. let c = src[position$1++];
  663. if ((c & 0x80) > 0) {
  664. position$1 -= 3;
  665. return
  666. }
  667. return fromCharCode(a, b, c)
  668. }
  669. } else {
  670. let a = src[position$1++];
  671. let b = src[position$1++];
  672. let c = src[position$1++];
  673. let d = src[position$1++];
  674. if ((a & 0x80) > 0 || (b & 0x80) > 0 || (c & 0x80) > 0 || (d & 0x80) > 0) {
  675. position$1 -= 4;
  676. return
  677. }
  678. if (length < 6) {
  679. if (length === 4)
  680. return fromCharCode(a, b, c, d)
  681. else {
  682. let e = src[position$1++];
  683. if ((e & 0x80) > 0) {
  684. position$1 -= 5;
  685. return
  686. }
  687. return fromCharCode(a, b, c, d, e)
  688. }
  689. } else if (length < 8) {
  690. let e = src[position$1++];
  691. let f = src[position$1++];
  692. if ((e & 0x80) > 0 || (f & 0x80) > 0) {
  693. position$1 -= 6;
  694. return
  695. }
  696. if (length < 7)
  697. return fromCharCode(a, b, c, d, e, f)
  698. let g = src[position$1++];
  699. if ((g & 0x80) > 0) {
  700. position$1 -= 7;
  701. return
  702. }
  703. return fromCharCode(a, b, c, d, e, f, g)
  704. } else {
  705. let e = src[position$1++];
  706. let f = src[position$1++];
  707. let g = src[position$1++];
  708. let h = src[position$1++];
  709. if ((e & 0x80) > 0 || (f & 0x80) > 0 || (g & 0x80) > 0 || (h & 0x80) > 0) {
  710. position$1 -= 8;
  711. return
  712. }
  713. if (length < 10) {
  714. if (length === 8)
  715. return fromCharCode(a, b, c, d, e, f, g, h)
  716. else {
  717. let i = src[position$1++];
  718. if ((i & 0x80) > 0) {
  719. position$1 -= 9;
  720. return
  721. }
  722. return fromCharCode(a, b, c, d, e, f, g, h, i)
  723. }
  724. } else if (length < 12) {
  725. let i = src[position$1++];
  726. let j = src[position$1++];
  727. if ((i & 0x80) > 0 || (j & 0x80) > 0) {
  728. position$1 -= 10;
  729. return
  730. }
  731. if (length < 11)
  732. return fromCharCode(a, b, c, d, e, f, g, h, i, j)
  733. let k = src[position$1++];
  734. if ((k & 0x80) > 0) {
  735. position$1 -= 11;
  736. return
  737. }
  738. return fromCharCode(a, b, c, d, e, f, g, h, i, j, k)
  739. } else {
  740. let i = src[position$1++];
  741. let j = src[position$1++];
  742. let k = src[position$1++];
  743. let l = src[position$1++];
  744. if ((i & 0x80) > 0 || (j & 0x80) > 0 || (k & 0x80) > 0 || (l & 0x80) > 0) {
  745. position$1 -= 12;
  746. return
  747. }
  748. if (length < 14) {
  749. if (length === 12)
  750. return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l)
  751. else {
  752. let m = src[position$1++];
  753. if ((m & 0x80) > 0) {
  754. position$1 -= 13;
  755. return
  756. }
  757. return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m)
  758. }
  759. } else {
  760. let m = src[position$1++];
  761. let n = src[position$1++];
  762. if ((m & 0x80) > 0 || (n & 0x80) > 0) {
  763. position$1 -= 14;
  764. return
  765. }
  766. if (length < 15)
  767. return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
  768. let o = src[position$1++];
  769. if ((o & 0x80) > 0) {
  770. position$1 -= 15;
  771. return
  772. }
  773. return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
  774. }
  775. }
  776. }
  777. }
  778. }
  779. function readOnlyJSString() {
  780. let token = src[position$1++];
  781. let length;
  782. if (token < 0xc0) {
  783. // fixstr
  784. length = token - 0xa0;
  785. } else {
  786. switch(token) {
  787. case 0xd9:
  788. // str 8
  789. length = src[position$1++];
  790. break
  791. case 0xda:
  792. // str 16
  793. length = dataView.getUint16(position$1);
  794. position$1 += 2;
  795. break
  796. case 0xdb:
  797. // str 32
  798. length = dataView.getUint32(position$1);
  799. position$1 += 4;
  800. break
  801. default:
  802. throw new Error('Expected string')
  803. }
  804. }
  805. return readStringJS(length)
  806. }
  807. function readBin(length) {
  808. return currentUnpackr.copyBuffers ?
  809. // specifically use the copying slice (not the node one)
  810. Uint8Array.prototype.slice.call(src, position$1, position$1 += length) :
  811. src.subarray(position$1, position$1 += length)
  812. }
  813. function readExt(length) {
  814. let type = src[position$1++];
  815. if (currentExtensions[type]) {
  816. let end;
  817. return currentExtensions[type](src.subarray(position$1, end = (position$1 += length)), (readPosition) => {
  818. position$1 = readPosition;
  819. try {
  820. return read();
  821. } finally {
  822. position$1 = end;
  823. }
  824. })
  825. }
  826. else
  827. throw new Error('Unknown extension type ' + type)
  828. }
  829. var keyCache = new Array(4096);
  830. function readKey() {
  831. let length = src[position$1++];
  832. if (length >= 0xa0 && length < 0xc0) {
  833. // fixstr, potentially use key cache
  834. length = length - 0xa0;
  835. if (srcStringEnd >= position$1) // if it has been extracted, must use it (and faster anyway)
  836. return srcString.slice(position$1 - srcStringStart, (position$1 += length) - srcStringStart)
  837. else if (!(srcStringEnd == 0 && srcEnd < 180))
  838. return readFixedString(length)
  839. } else { // not cacheable, go back and do a standard read
  840. position$1--;
  841. return asSafeString(read())
  842. }
  843. let key = ((length << 5) ^ (length > 1 ? dataView.getUint16(position$1) : length > 0 ? src[position$1] : 0)) & 0xfff;
  844. let entry = keyCache[key];
  845. let checkPosition = position$1;
  846. let end = position$1 + length - 3;
  847. let chunk;
  848. let i = 0;
  849. if (entry && entry.bytes == length) {
  850. while (checkPosition < end) {
  851. chunk = dataView.getUint32(checkPosition);
  852. if (chunk != entry[i++]) {
  853. checkPosition = 0x70000000;
  854. break
  855. }
  856. checkPosition += 4;
  857. }
  858. end += 3;
  859. while (checkPosition < end) {
  860. chunk = src[checkPosition++];
  861. if (chunk != entry[i++]) {
  862. checkPosition = 0x70000000;
  863. break
  864. }
  865. }
  866. if (checkPosition === end) {
  867. position$1 = checkPosition;
  868. return entry.string
  869. }
  870. end -= 3;
  871. checkPosition = position$1;
  872. }
  873. entry = [];
  874. keyCache[key] = entry;
  875. entry.bytes = length;
  876. while (checkPosition < end) {
  877. chunk = dataView.getUint32(checkPosition);
  878. entry.push(chunk);
  879. checkPosition += 4;
  880. }
  881. end += 3;
  882. while (checkPosition < end) {
  883. chunk = src[checkPosition++];
  884. entry.push(chunk);
  885. }
  886. // for small blocks, avoiding the overhead of the extract call is helpful
  887. let string = length < 16 ? shortStringInJS(length) : longStringInJS(length);
  888. if (string != null)
  889. return entry.string = string
  890. return entry.string = readFixedString(length)
  891. }
  892. function asSafeString(property) {
  893. // protect against expensive (DoS) string conversions
  894. if (typeof property === 'string') return property;
  895. if (typeof property === 'number' || typeof property === 'boolean' || typeof property === 'bigint') return property.toString();
  896. if (property == null) return property + '';
  897. throw new Error('Invalid property type for record', typeof property);
  898. }
  899. // the registration of the record definition extension (as "r")
  900. const recordDefinition = (id, highByte) => {
  901. let structure = read().map(asSafeString); // ensure that all keys are strings and
  902. // that the array is mutable
  903. let firstByte = id;
  904. if (highByte !== undefined) {
  905. id = id < 32 ? -((highByte << 5) + id) : ((highByte << 5) + id);
  906. structure.highByte = highByte;
  907. }
  908. let existingStructure = currentStructures[id];
  909. // If it is a shared structure, we need to restore any changes after reading.
  910. // Also in sequential mode, we may get incomplete reads and thus errors, and we need to restore
  911. // to the state prior to an incomplete read in order to properly resume.
  912. if (existingStructure && (existingStructure.isShared || sequentialMode)) {
  913. (currentStructures.restoreStructures || (currentStructures.restoreStructures = []))[id] = existingStructure;
  914. }
  915. currentStructures[id] = structure;
  916. structure.read = createStructureReader(structure, firstByte);
  917. return structure.read()
  918. };
  919. currentExtensions[0] = () => {}; // notepack defines extension 0 to mean undefined, so use that as the default here
  920. currentExtensions[0].noBuffer = true;
  921. currentExtensions[0x42] = (data) => {
  922. // decode bigint
  923. let length = data.length;
  924. let value = BigInt(data[0] & 0x80 ? data[0] - 0x100 : data[0]);
  925. for (let i = 1; i < length; i++) {
  926. value <<= BigInt(8);
  927. value += BigInt(data[i]);
  928. }
  929. return value;
  930. };
  931. let errors = { Error, TypeError, ReferenceError };
  932. currentExtensions[0x65] = () => {
  933. let data = read();
  934. return (errors[data[0]] || Error)(data[1], { cause: data[2] })
  935. };
  936. currentExtensions[0x69] = (data) => {
  937. // id extension (for structured clones)
  938. if (currentUnpackr.structuredClone === false) throw new Error('Structured clone extension is disabled')
  939. let id = dataView.getUint32(position$1 - 4);
  940. if (!referenceMap)
  941. referenceMap = new Map();
  942. let token = src[position$1];
  943. let target;
  944. // TODO: handle Maps, Sets, and other types that can cycle; this is complicated, because you potentially need to read
  945. // ahead past references to record structure definitions
  946. if (token >= 0x90 && token < 0xa0 || token == 0xdc || token == 0xdd)
  947. target = [];
  948. else
  949. target = {};
  950. let refEntry = { target }; // a placeholder object
  951. referenceMap.set(id, refEntry);
  952. let targetProperties = read(); // read the next value as the target object to id
  953. if (refEntry.used) // there is a cycle, so we have to assign properties to original target
  954. return Object.assign(target, targetProperties)
  955. refEntry.target = targetProperties; // the placeholder wasn't used, replace with the deserialized one
  956. return targetProperties // no cycle, can just use the returned read object
  957. };
  958. currentExtensions[0x70] = (data) => {
  959. // pointer extension (for structured clones)
  960. if (currentUnpackr.structuredClone === false) throw new Error('Structured clone extension is disabled')
  961. let id = dataView.getUint32(position$1 - 4);
  962. let refEntry = referenceMap.get(id);
  963. refEntry.used = true;
  964. return refEntry.target
  965. };
  966. currentExtensions[0x73] = () => new Set(read());
  967. const typedArrays = ['Int8','Uint8','Uint8Clamped','Int16','Uint16','Int32','Uint32','Float32','Float64','BigInt64','BigUint64'].map(type => type + 'Array');
  968. let glbl = typeof globalThis === 'object' ? globalThis : window;
  969. currentExtensions[0x74] = (data) => {
  970. let typeCode = data[0];
  971. let typedArrayName = typedArrays[typeCode];
  972. if (!typedArrayName) {
  973. if (typeCode === 16) {
  974. let ab = new ArrayBuffer(data.length - 1);
  975. let u8 = new Uint8Array(ab);
  976. u8.set(data.subarray(1));
  977. return ab;
  978. }
  979. throw new Error('Could not find typed array for code ' + typeCode)
  980. }
  981. // we have to always slice/copy here to get a new ArrayBuffer that is word/byte aligned
  982. return new glbl[typedArrayName](Uint8Array.prototype.slice.call(data, 1).buffer)
  983. };
  984. currentExtensions[0x78] = () => {
  985. let data = read();
  986. return new RegExp(data[0], data[1])
  987. };
  988. const TEMP_BUNDLE = [];
  989. currentExtensions[0x62] = (data) => {
  990. let dataSize = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3];
  991. let dataPosition = position$1;
  992. position$1 += dataSize - data.length;
  993. bundledStrings$1 = TEMP_BUNDLE;
  994. bundledStrings$1 = [readOnlyJSString(), readOnlyJSString()];
  995. bundledStrings$1.position0 = 0;
  996. bundledStrings$1.position1 = 0;
  997. bundledStrings$1.postBundlePosition = position$1;
  998. position$1 = dataPosition;
  999. return read()
  1000. };
  1001. currentExtensions[0xff] = (data) => {
  1002. // 32-bit date extension
  1003. if (data.length == 4)
  1004. return new Date((data[0] * 0x1000000 + (data[1] << 16) + (data[2] << 8) + data[3]) * 1000)
  1005. else if (data.length == 8)
  1006. return new Date(
  1007. ((data[0] << 22) + (data[1] << 14) + (data[2] << 6) + (data[3] >> 2)) / 1000000 +
  1008. ((data[3] & 0x3) * 0x100000000 + data[4] * 0x1000000 + (data[5] << 16) + (data[6] << 8) + data[7]) * 1000)
  1009. else if (data.length == 12)// TODO: Implement support for negative
  1010. return new Date(
  1011. ((data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]) / 1000000 +
  1012. (((data[4] & 0x80) ? -0x1000000000000 : 0) + data[6] * 0x10000000000 + data[7] * 0x100000000 + data[8] * 0x1000000 + (data[9] << 16) + (data[10] << 8) + data[11]) * 1000)
  1013. else
  1014. return new Date('invalid')
  1015. }; // notepack defines extension 0 to mean undefined, so use that as the default here
  1016. // registration of bulk record definition?
  1017. // currentExtensions[0x52] = () =>
  1018. function saveState(callback) {
  1019. let savedSrcEnd = srcEnd;
  1020. let savedPosition = position$1;
  1021. let savedSrcStringStart = srcStringStart;
  1022. let savedSrcStringEnd = srcStringEnd;
  1023. let savedSrcString = srcString;
  1024. let savedReferenceMap = referenceMap;
  1025. let savedBundledStrings = bundledStrings$1;
  1026. // TODO: We may need to revisit this if we do more external calls to user code (since it could be slow)
  1027. let savedSrc = new Uint8Array(src.slice(0, srcEnd)); // we copy the data in case it changes while external data is processed
  1028. let savedStructures = currentStructures;
  1029. let savedStructuresContents = currentStructures.slice(0, currentStructures.length);
  1030. let savedPackr = currentUnpackr;
  1031. let savedSequentialMode = sequentialMode;
  1032. let value = callback();
  1033. srcEnd = savedSrcEnd;
  1034. position$1 = savedPosition;
  1035. srcStringStart = savedSrcStringStart;
  1036. srcStringEnd = savedSrcStringEnd;
  1037. srcString = savedSrcString;
  1038. referenceMap = savedReferenceMap;
  1039. bundledStrings$1 = savedBundledStrings;
  1040. src = savedSrc;
  1041. sequentialMode = savedSequentialMode;
  1042. currentStructures = savedStructures;
  1043. currentStructures.splice(0, currentStructures.length, ...savedStructuresContents);
  1044. currentUnpackr = savedPackr;
  1045. dataView = new DataView(src.buffer, src.byteOffset, src.byteLength);
  1046. return value
  1047. }
  1048. function clearSource() {
  1049. src = null;
  1050. referenceMap = null;
  1051. currentStructures = null;
  1052. }
  1053. function addExtension$1(extension) {
  1054. if (extension.unpack)
  1055. currentExtensions[extension.type] = extension.unpack;
  1056. else
  1057. currentExtensions[extension.type] = extension;
  1058. }
  1059. const mult10 = new Array(147); // this is a table matching binary exponents to the multiplier to determine significant digit rounding
  1060. for (let i = 0; i < 256; i++) {
  1061. mult10[i] = +('1e' + Math.floor(45.15 - i * 0.30103));
  1062. }
  1063. const Decoder = Unpackr;
  1064. var defaultUnpackr = new Unpackr({ useRecords: false });
  1065. const unpack = defaultUnpackr.unpack;
  1066. const unpackMultiple = defaultUnpackr.unpackMultiple;
  1067. const decode = defaultUnpackr.unpack;
  1068. const FLOAT32_OPTIONS = {
  1069. NEVER: 0,
  1070. ALWAYS: 1,
  1071. DECIMAL_ROUND: 3,
  1072. DECIMAL_FIT: 4
  1073. };
  1074. let f32Array = new Float32Array(1);
  1075. let u8Array = new Uint8Array(f32Array.buffer, 0, 4);
  1076. function roundFloat32(float32Number) {
  1077. f32Array[0] = float32Number;
  1078. let multiplier = mult10[((u8Array[3] & 0x7f) << 1) | (u8Array[2] >> 7)];
  1079. return ((multiplier * float32Number + (float32Number > 0 ? 0.5 : -0.5)) >> 0) / multiplier
  1080. }
  1081. let textEncoder;
  1082. try {
  1083. textEncoder = new TextEncoder();
  1084. } catch (error) {}
  1085. let extensions, extensionClasses;
  1086. const hasNodeBuffer = typeof Buffer !== 'undefined';
  1087. const ByteArrayAllocate = hasNodeBuffer ?
  1088. function(length) { return Buffer.allocUnsafeSlow(length) } : Uint8Array;
  1089. const ByteArray = hasNodeBuffer ? Buffer : Uint8Array;
  1090. const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000;
  1091. let target, keysTarget;
  1092. let targetView;
  1093. let position = 0;
  1094. let safeEnd;
  1095. let bundledStrings = null;
  1096. let writeStructSlots;
  1097. const MAX_BUNDLE_SIZE = 0x5500; // maximum characters such that the encoded bytes fits in 16 bits.
  1098. const hasNonLatin = /[\u0080-\uFFFF]/;
  1099. const RECORD_SYMBOL = Symbol('record-id');
  1100. class Packr extends Unpackr {
  1101. constructor(options) {
  1102. super(options);
  1103. this.offset = 0;
  1104. let start;
  1105. let hasSharedUpdate;
  1106. let structures;
  1107. let referenceMap;
  1108. let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position) {
  1109. return target.utf8Write(string, position, target.byteLength - position)
  1110. } : (textEncoder && textEncoder.encodeInto) ?
  1111. function(string, position) {
  1112. return textEncoder.encodeInto(string, target.subarray(position)).written
  1113. } : false;
  1114. let packr = this;
  1115. if (!options)
  1116. options = {};
  1117. let isSequential = options && options.sequential;
  1118. let hasSharedStructures = options.structures || options.saveStructures;
  1119. let maxSharedStructures = options.maxSharedStructures;
  1120. if (maxSharedStructures == null)
  1121. maxSharedStructures = hasSharedStructures ? 32 : 0;
  1122. if (maxSharedStructures > 8160)
  1123. throw new Error('Maximum maxSharedStructure is 8160')
  1124. if (options.structuredClone && options.moreTypes == undefined) {
  1125. this.moreTypes = true;
  1126. }
  1127. let maxOwnStructures = options.maxOwnStructures;
  1128. if (maxOwnStructures == null)
  1129. maxOwnStructures = hasSharedStructures ? 32 : 64;
  1130. if (!this.structures && options.useRecords != false)
  1131. this.structures = [];
  1132. // two byte record ids for shared structures
  1133. let useTwoByteRecords = maxSharedStructures > 32 || (maxOwnStructures + maxSharedStructures > 64);
  1134. let sharedLimitId = maxSharedStructures + 0x40;
  1135. let maxStructureId = maxSharedStructures + maxOwnStructures + 0x40;
  1136. if (maxStructureId > 8256) {
  1137. throw new Error('Maximum maxSharedStructure + maxOwnStructure is 8192')
  1138. }
  1139. let recordIdsToRemove = [];
  1140. let transitionsCount = 0;
  1141. let serializationsSinceTransitionRebuild = 0;
  1142. this.pack = this.encode = function(value, encodeOptions) {
  1143. if (!target) {
  1144. target = new ByteArrayAllocate(8192);
  1145. targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, 8192));
  1146. position = 0;
  1147. }
  1148. safeEnd = target.length - 10;
  1149. if (safeEnd - position < 0x800) {
  1150. // don't start too close to the end,
  1151. target = new ByteArrayAllocate(target.length);
  1152. targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, target.length));
  1153. safeEnd = target.length - 10;
  1154. position = 0;
  1155. } else
  1156. position = (position + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
  1157. start = position;
  1158. if (encodeOptions & RESERVE_START_SPACE) position += (encodeOptions & 0xff);
  1159. referenceMap = packr.structuredClone ? new Map() : null;
  1160. if (packr.bundleStrings && typeof value !== 'string') {
  1161. bundledStrings = [];
  1162. bundledStrings.size = Infinity; // force a new bundle start on first string
  1163. } else
  1164. bundledStrings = null;
  1165. structures = packr.structures;
  1166. if (structures) {
  1167. if (structures.uninitialized)
  1168. structures = packr._mergeStructures(packr.getStructures());
  1169. let sharedLength = structures.sharedLength || 0;
  1170. if (sharedLength > maxSharedStructures) {
  1171. //if (maxSharedStructures <= 32 && structures.sharedLength > 32) // TODO: could support this, but would need to update the limit ids
  1172. throw new Error('Shared structures is larger than maximum shared structures, try increasing maxSharedStructures to ' + structures.sharedLength)
  1173. }
  1174. if (!structures.transitions) {
  1175. // rebuild our structure transitions
  1176. structures.transitions = Object.create(null);
  1177. for (let i = 0; i < sharedLength; i++) {
  1178. let keys = structures[i];
  1179. if (!keys)
  1180. continue
  1181. let nextTransition, transition = structures.transitions;
  1182. for (let j = 0, l = keys.length; j < l; j++) {
  1183. let key = keys[j];
  1184. nextTransition = transition[key];
  1185. if (!nextTransition) {
  1186. nextTransition = transition[key] = Object.create(null);
  1187. }
  1188. transition = nextTransition;
  1189. }
  1190. transition[RECORD_SYMBOL] = i + 0x40;
  1191. }
  1192. this.lastNamedStructuresLength = sharedLength;
  1193. }
  1194. if (!isSequential) {
  1195. structures.nextId = sharedLength + 0x40;
  1196. }
  1197. }
  1198. if (hasSharedUpdate)
  1199. hasSharedUpdate = false;
  1200. let encodingError;
  1201. try {
  1202. if (packr.randomAccessStructure && value && value.constructor && value.constructor === Object)
  1203. writeStruct(value);
  1204. else
  1205. pack(value);
  1206. let lastBundle = bundledStrings;
  1207. if (bundledStrings)
  1208. writeBundles(start, pack, 0);
  1209. if (referenceMap && referenceMap.idsToInsert) {
  1210. let idsToInsert = referenceMap.idsToInsert.sort((a, b) => a.offset > b.offset ? 1 : -1);
  1211. let i = idsToInsert.length;
  1212. let incrementPosition = -1;
  1213. while (lastBundle && i > 0) {
  1214. let insertionPoint = idsToInsert[--i].offset + start;
  1215. if (insertionPoint < (lastBundle.stringsPosition + start) && incrementPosition === -1)
  1216. incrementPosition = 0;
  1217. if (insertionPoint > (lastBundle.position + start)) {
  1218. if (incrementPosition >= 0)
  1219. incrementPosition += 6;
  1220. } else {
  1221. if (incrementPosition >= 0) {
  1222. // update the bundle reference now
  1223. targetView.setUint32(lastBundle.position + start,
  1224. targetView.getUint32(lastBundle.position + start) + incrementPosition);
  1225. incrementPosition = -1; // reset
  1226. }
  1227. lastBundle = lastBundle.previous;
  1228. i++;
  1229. }
  1230. }
  1231. if (incrementPosition >= 0 && lastBundle) {
  1232. // update the bundle reference now
  1233. targetView.setUint32(lastBundle.position + start,
  1234. targetView.getUint32(lastBundle.position + start) + incrementPosition);
  1235. }
  1236. position += idsToInsert.length * 6;
  1237. if (position > safeEnd)
  1238. makeRoom(position);
  1239. packr.offset = position;
  1240. let serialized = insertIds(target.subarray(start, position), idsToInsert);
  1241. referenceMap = null;
  1242. return serialized
  1243. }
  1244. packr.offset = position; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
  1245. if (encodeOptions & REUSE_BUFFER_MODE) {
  1246. target.start = start;
  1247. target.end = position;
  1248. return target
  1249. }
  1250. return target.subarray(start, position) // position can change if we call pack again in saveStructures, so we get the buffer now
  1251. } catch(error) {
  1252. encodingError = error;
  1253. throw error;
  1254. } finally {
  1255. if (structures) {
  1256. resetStructures();
  1257. if (hasSharedUpdate && packr.saveStructures) {
  1258. let sharedLength = structures.sharedLength || 0;
  1259. // we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
  1260. let returnBuffer = target.subarray(start, position);
  1261. let newSharedData = prepareStructures(structures, packr);
  1262. if (!encodingError) { // TODO: If there is an encoding error, should make the structures as uninitialized so they get rebuilt next time
  1263. if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
  1264. // get updated structures and try again if the update failed
  1265. return packr.pack(value, encodeOptions)
  1266. }
  1267. packr.lastNamedStructuresLength = sharedLength;
  1268. // don't keep large buffers around
  1269. if (target.length > 0x40000000) target = null;
  1270. return returnBuffer
  1271. }
  1272. }
  1273. }
  1274. // don't keep large buffers around, they take too much memory and cause problems (limit at 1GB)
  1275. if (target.length > 0x40000000) target = null;
  1276. if (encodeOptions & RESET_BUFFER_MODE)
  1277. position = start;
  1278. }
  1279. };
  1280. const resetStructures = () => {
  1281. if (serializationsSinceTransitionRebuild < 10)
  1282. serializationsSinceTransitionRebuild++;
  1283. let sharedLength = structures.sharedLength || 0;
  1284. if (structures.length > sharedLength && !isSequential)
  1285. structures.length = sharedLength;
  1286. if (transitionsCount > 10000) {
  1287. // force a rebuild occasionally after a lot of transitions so it can get cleaned up
  1288. structures.transitions = null;
  1289. serializationsSinceTransitionRebuild = 0;
  1290. transitionsCount = 0;
  1291. if (recordIdsToRemove.length > 0)
  1292. recordIdsToRemove = [];
  1293. } else if (recordIdsToRemove.length > 0 && !isSequential) {
  1294. for (let i = 0, l = recordIdsToRemove.length; i < l; i++) {
  1295. recordIdsToRemove[i][RECORD_SYMBOL] = 0;
  1296. }
  1297. recordIdsToRemove = [];
  1298. }
  1299. };
  1300. const packArray = (value) => {
  1301. var length = value.length;
  1302. if (length < 0x10) {
  1303. target[position++] = 0x90 | length;
  1304. } else if (length < 0x10000) {
  1305. target[position++] = 0xdc;
  1306. target[position++] = length >> 8;
  1307. target[position++] = length & 0xff;
  1308. } else {
  1309. target[position++] = 0xdd;
  1310. targetView.setUint32(position, length);
  1311. position += 4;
  1312. }
  1313. for (let i = 0; i < length; i++) {
  1314. pack(value[i]);
  1315. }
  1316. };
  1317. const pack = (value) => {
  1318. if (position > safeEnd)
  1319. target = makeRoom(position);
  1320. var type = typeof value;
  1321. var length;
  1322. if (type === 'string') {
  1323. let strLength = value.length;
  1324. if (bundledStrings && strLength >= 4 && strLength < 0x1000) {
  1325. if ((bundledStrings.size += strLength) > MAX_BUNDLE_SIZE) {
  1326. let extStart;
  1327. let maxBytes = (bundledStrings[0] ? bundledStrings[0].length * 3 + bundledStrings[1].length : 0) + 10;
  1328. if (position + maxBytes > safeEnd)
  1329. target = makeRoom(position + maxBytes);
  1330. let lastBundle;
  1331. if (bundledStrings.position) { // here we use the 0x62 extension to write the last bundle and reserve space for the reference pointer to the next/current bundle
  1332. lastBundle = bundledStrings;
  1333. target[position] = 0xc8; // ext 16
  1334. position += 3; // reserve for the writing bundle size
  1335. target[position++] = 0x62; // 'b'
  1336. extStart = position - start;
  1337. position += 4; // reserve for writing bundle reference
  1338. writeBundles(start, pack, 0); // write the last bundles
  1339. targetView.setUint16(extStart + start - 3, position - start - extStart);
  1340. } else { // here we use the 0x62 extension just to reserve the space for the reference pointer to the bundle (will be updated once the bundle is written)
  1341. target[position++] = 0xd6; // fixext 4
  1342. target[position++] = 0x62; // 'b'
  1343. extStart = position - start;
  1344. position += 4; // reserve for writing bundle reference
  1345. }
  1346. bundledStrings = ['', '']; // create new ones
  1347. bundledStrings.previous = lastBundle;
  1348. bundledStrings.size = 0;
  1349. bundledStrings.position = extStart;
  1350. }
  1351. let twoByte = hasNonLatin.test(value);
  1352. bundledStrings[twoByte ? 0 : 1] += value;
  1353. target[position++] = 0xc1;
  1354. pack(twoByte ? -strLength : strLength);
  1355. return
  1356. }
  1357. let headerSize;
  1358. // first we estimate the header size, so we can write to the correct location
  1359. if (strLength < 0x20) {
  1360. headerSize = 1;
  1361. } else if (strLength < 0x100) {
  1362. headerSize = 2;
  1363. } else if (strLength < 0x10000) {
  1364. headerSize = 3;
  1365. } else {
  1366. headerSize = 5;
  1367. }
  1368. let maxBytes = strLength * 3;
  1369. if (position + maxBytes > safeEnd)
  1370. target = makeRoom(position + maxBytes);
  1371. if (strLength < 0x40 || !encodeUtf8) {
  1372. let i, c1, c2, strPosition = position + headerSize;
  1373. for (i = 0; i < strLength; i++) {
  1374. c1 = value.charCodeAt(i);
  1375. if (c1 < 0x80) {
  1376. target[strPosition++] = c1;
  1377. } else if (c1 < 0x800) {
  1378. target[strPosition++] = c1 >> 6 | 0xc0;
  1379. target[strPosition++] = c1 & 0x3f | 0x80;
  1380. } else if (
  1381. (c1 & 0xfc00) === 0xd800 &&
  1382. ((c2 = value.charCodeAt(i + 1)) & 0xfc00) === 0xdc00
  1383. ) {
  1384. c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff);
  1385. i++;
  1386. target[strPosition++] = c1 >> 18 | 0xf0;
  1387. target[strPosition++] = c1 >> 12 & 0x3f | 0x80;
  1388. target[strPosition++] = c1 >> 6 & 0x3f | 0x80;
  1389. target[strPosition++] = c1 & 0x3f | 0x80;
  1390. } else {
  1391. target[strPosition++] = c1 >> 12 | 0xe0;
  1392. target[strPosition++] = c1 >> 6 & 0x3f | 0x80;
  1393. target[strPosition++] = c1 & 0x3f | 0x80;
  1394. }
  1395. }
  1396. length = strPosition - position - headerSize;
  1397. } else {
  1398. length = encodeUtf8(value, position + headerSize);
  1399. }
  1400. if (length < 0x20) {
  1401. target[position++] = 0xa0 | length;
  1402. } else if (length < 0x100) {
  1403. if (headerSize < 2) {
  1404. target.copyWithin(position + 2, position + 1, position + 1 + length);
  1405. }
  1406. target[position++] = 0xd9;
  1407. target[position++] = length;
  1408. } else if (length < 0x10000) {
  1409. if (headerSize < 3) {
  1410. target.copyWithin(position + 3, position + 2, position + 2 + length);
  1411. }
  1412. target[position++] = 0xda;
  1413. target[position++] = length >> 8;
  1414. target[position++] = length & 0xff;
  1415. } else {
  1416. if (headerSize < 5) {
  1417. target.copyWithin(position + 5, position + 3, position + 3 + length);
  1418. }
  1419. target[position++] = 0xdb;
  1420. targetView.setUint32(position, length);
  1421. position += 4;
  1422. }
  1423. position += length;
  1424. } else if (type === 'number') {
  1425. if (value >>> 0 === value) {// positive integer, 32-bit or less
  1426. // positive uint
  1427. if (value < 0x20 || (value < 0x80 && this.useRecords === false) || (value < 0x40 && !this.randomAccessStructure)) {
  1428. target[position++] = value;
  1429. } else if (value < 0x100) {
  1430. target[position++] = 0xcc;
  1431. target[position++] = value;
  1432. } else if (value < 0x10000) {
  1433. target[position++] = 0xcd;
  1434. target[position++] = value >> 8;
  1435. target[position++] = value & 0xff;
  1436. } else {
  1437. target[position++] = 0xce;
  1438. targetView.setUint32(position, value);
  1439. position += 4;
  1440. }
  1441. } else if (value >> 0 === value) { // negative integer
  1442. if (value >= -0x20) {
  1443. target[position++] = 0x100 + value;
  1444. } else if (value >= -0x80) {
  1445. target[position++] = 0xd0;
  1446. target[position++] = value + 0x100;
  1447. } else if (value >= -0x8000) {
  1448. target[position++] = 0xd1;
  1449. targetView.setInt16(position, value);
  1450. position += 2;
  1451. } else {
  1452. target[position++] = 0xd2;
  1453. targetView.setInt32(position, value);
  1454. position += 4;
  1455. }
  1456. } else {
  1457. let useFloat32;
  1458. if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
  1459. target[position++] = 0xca;
  1460. targetView.setFloat32(position, value);
  1461. let xShifted;
  1462. if (useFloat32 < 4 ||
  1463. // this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
  1464. ((xShifted = value * mult10[((target[position] & 0x7f) << 1) | (target[position + 1] >> 7)]) >> 0) === xShifted) {
  1465. position += 4;
  1466. return
  1467. } else
  1468. position--; // move back into position for writing a double
  1469. }
  1470. target[position++] = 0xcb;
  1471. targetView.setFloat64(position, value);
  1472. position += 8;
  1473. }
  1474. } else if (type === 'object' || type === 'function') {
  1475. if (!value)
  1476. target[position++] = 0xc0;
  1477. else {
  1478. if (referenceMap) {
  1479. let referee = referenceMap.get(value);
  1480. if (referee) {
  1481. if (!referee.id) {
  1482. let idsToInsert = referenceMap.idsToInsert || (referenceMap.idsToInsert = []);
  1483. referee.id = idsToInsert.push(referee);
  1484. }
  1485. target[position++] = 0xd6; // fixext 4
  1486. target[position++] = 0x70; // "p" for pointer
  1487. targetView.setUint32(position, referee.id);
  1488. position += 4;
  1489. return
  1490. } else
  1491. referenceMap.set(value, { offset: position - start });
  1492. }
  1493. let constructor = value.constructor;
  1494. if (constructor === Object) {
  1495. writeObject(value);
  1496. } else if (constructor === Array) {
  1497. packArray(value);
  1498. } else if (constructor === Map) {
  1499. if (this.mapAsEmptyObject) target[position++] = 0x80;
  1500. else {
  1501. length = value.size;
  1502. if (length < 0x10) {
  1503. target[position++] = 0x80 | length;
  1504. } else if (length < 0x10000) {
  1505. target[position++] = 0xde;
  1506. target[position++] = length >> 8;
  1507. target[position++] = length & 0xff;
  1508. } else {
  1509. target[position++] = 0xdf;
  1510. targetView.setUint32(position, length);
  1511. position += 4;
  1512. }
  1513. for (let [key, entryValue] of value) {
  1514. pack(key);
  1515. pack(entryValue);
  1516. }
  1517. }
  1518. } else {
  1519. for (let i = 0, l = extensions.length; i < l; i++) {
  1520. let extensionClass = extensionClasses[i];
  1521. if (value instanceof extensionClass) {
  1522. let extension = extensions[i];
  1523. if (extension.write) {
  1524. if (extension.type) {
  1525. target[position++] = 0xd4; // one byte "tag" extension
  1526. target[position++] = extension.type;
  1527. target[position++] = 0;
  1528. }
  1529. let writeResult = extension.write.call(this, value);
  1530. if (writeResult === value) { // avoid infinite recursion
  1531. if (Array.isArray(value)) {
  1532. packArray(value);
  1533. } else {
  1534. writeObject(value);
  1535. }
  1536. } else {
  1537. pack(writeResult);
  1538. }
  1539. return
  1540. }
  1541. let currentTarget = target;
  1542. let currentTargetView = targetView;
  1543. let currentPosition = position;
  1544. target = null;
  1545. let result;
  1546. try {
  1547. result = extension.pack.call(this, value, (size) => {
  1548. // restore target and use it
  1549. target = currentTarget;
  1550. currentTarget = null;
  1551. position += size;
  1552. if (position > safeEnd)
  1553. makeRoom(position);
  1554. return {
  1555. target, targetView, position: position - size
  1556. }
  1557. }, pack);
  1558. } finally {
  1559. // restore current target information (unless already restored)
  1560. if (currentTarget) {
  1561. target = currentTarget;
  1562. targetView = currentTargetView;
  1563. position = currentPosition;
  1564. safeEnd = target.length - 10;
  1565. }
  1566. }
  1567. if (result) {
  1568. if (result.length + position > safeEnd)
  1569. makeRoom(result.length + position);
  1570. position = writeExtensionData(result, target, position, extension.type);
  1571. }
  1572. return
  1573. }
  1574. }
  1575. // check isArray after extensions, because extensions can extend Array
  1576. if (Array.isArray(value)) {
  1577. packArray(value);
  1578. } else {
  1579. // use this as an alternate mechanism for expressing how to serialize
  1580. if (value.toJSON) {
  1581. const json = value.toJSON();
  1582. // if for some reason value.toJSON returns itself it'll loop forever
  1583. if (json !== value)
  1584. return pack(json)
  1585. }
  1586. // if there is a writeFunction, use it, otherwise just encode as undefined
  1587. if (type === 'function')
  1588. return pack(this.writeFunction && this.writeFunction(value));
  1589. // no extension found, write as plain object
  1590. writeObject(value);
  1591. }
  1592. }
  1593. }
  1594. } else if (type === 'boolean') {
  1595. target[position++] = value ? 0xc3 : 0xc2;
  1596. } else if (type === 'bigint') {
  1597. if (value < (BigInt(1)<<BigInt(63)) && value >= -(BigInt(1)<<BigInt(63))) {
  1598. // use a signed int as long as it fits
  1599. target[position++] = 0xd3;
  1600. targetView.setBigInt64(position, value);
  1601. } else if (value < (BigInt(1)<<BigInt(64)) && value > 0) {
  1602. // if we can fit an unsigned int, use that
  1603. target[position++] = 0xcf;
  1604. targetView.setBigUint64(position, value);
  1605. } else {
  1606. // overflow
  1607. if (this.largeBigIntToFloat) {
  1608. target[position++] = 0xcb;
  1609. targetView.setFloat64(position, Number(value));
  1610. } else if (this.largeBigIntToString) {
  1611. return pack(value.toString());
  1612. } else if (this.useBigIntExtension && value < BigInt(2)**BigInt(1023) && value > -(BigInt(2)**BigInt(1023))) {
  1613. target[position++] = 0xc7;
  1614. position++;
  1615. target[position++] = 0x42; // "B" for BigInt
  1616. let bytes = [];
  1617. let alignedSign;
  1618. do {
  1619. let byte = value & BigInt(0xff);
  1620. alignedSign = (byte & BigInt(0x80)) === (value < BigInt(0) ? BigInt(0x80) : BigInt(0));
  1621. bytes.push(byte);
  1622. value >>= BigInt(8);
  1623. } while (!((value === BigInt(0) || value === BigInt(-1)) && alignedSign));
  1624. target[position-2] = bytes.length;
  1625. for (let i = bytes.length; i > 0;) {
  1626. target[position++] = Number(bytes[--i]);
  1627. }
  1628. return
  1629. } else {
  1630. throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, use' +
  1631. ' useBigIntExtension, or set largeBigIntToFloat to convert to float-64, or set' +
  1632. ' largeBigIntToString to convert to string')
  1633. }
  1634. }
  1635. position += 8;
  1636. } else if (type === 'undefined') {
  1637. if (this.encodeUndefinedAsNil)
  1638. target[position++] = 0xc0;
  1639. else {
  1640. target[position++] = 0xd4; // a number of implementations use fixext1 with type 0, data 0 to denote undefined, so we follow suite
  1641. target[position++] = 0;
  1642. target[position++] = 0;
  1643. }
  1644. } else {
  1645. throw new Error('Unknown type: ' + type)
  1646. }
  1647. };
  1648. const writePlainObject = (this.variableMapSize || this.coercibleKeyAsNumber || this.skipValues) ? (object) => {
  1649. // this method is slightly slower, but generates "preferred serialization" (optimally small for smaller objects)
  1650. let keys;
  1651. if (this.skipValues) {
  1652. keys = [];
  1653. for (let key in object) {
  1654. if ((typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) &&
  1655. !this.skipValues.includes(object[key]))
  1656. keys.push(key);
  1657. }
  1658. } else {
  1659. keys = Object.keys(object);
  1660. }
  1661. let length = keys.length;
  1662. if (length < 0x10) {
  1663. target[position++] = 0x80 | length;
  1664. } else if (length < 0x10000) {
  1665. target[position++] = 0xde;
  1666. target[position++] = length >> 8;
  1667. target[position++] = length & 0xff;
  1668. } else {
  1669. target[position++] = 0xdf;
  1670. targetView.setUint32(position, length);
  1671. position += 4;
  1672. }
  1673. let key;
  1674. if (this.coercibleKeyAsNumber) {
  1675. for (let i = 0; i < length; i++) {
  1676. key = keys[i];
  1677. let num = Number(key);
  1678. pack(isNaN(num) ? key : num);
  1679. pack(object[key]);
  1680. }
  1681. } else {
  1682. for (let i = 0; i < length; i++) {
  1683. pack(key = keys[i]);
  1684. pack(object[key]);
  1685. }
  1686. }
  1687. } :
  1688. (object) => {
  1689. target[position++] = 0xde; // always using map 16, so we can preallocate and set the length afterwards
  1690. let objectOffset = position - start;
  1691. position += 2;
  1692. let size = 0;
  1693. for (let key in object) {
  1694. if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
  1695. pack(key);
  1696. pack(object[key]);
  1697. size++;
  1698. }
  1699. }
  1700. if (size > 0xffff) {
  1701. throw new Error('Object is too large to serialize with fast 16-bit map size,' +
  1702. ' use the "variableMapSize" option to serialize this object');
  1703. }
  1704. target[objectOffset++ + start] = size >> 8;
  1705. target[objectOffset + start] = size & 0xff;
  1706. };
  1707. const writeRecord = this.useRecords === false ? writePlainObject :
  1708. (options.progressiveRecords && !useTwoByteRecords) ? // this is about 2% faster for highly stable structures, since it only requires one for-in loop (but much more expensive when new structure needs to be written)
  1709. (object) => {
  1710. let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
  1711. let objectOffset = position++ - start;
  1712. let wroteKeys;
  1713. for (let key in object) {
  1714. if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
  1715. nextTransition = transition[key];
  1716. if (nextTransition)
  1717. transition = nextTransition;
  1718. else {
  1719. // record doesn't exist, create full new record and insert it
  1720. let keys = Object.keys(object);
  1721. let lastTransition = transition;
  1722. transition = structures.transitions;
  1723. let newTransitions = 0;
  1724. for (let i = 0, l = keys.length; i < l; i++) {
  1725. let key = keys[i];
  1726. nextTransition = transition[key];
  1727. if (!nextTransition) {
  1728. nextTransition = transition[key] = Object.create(null);
  1729. newTransitions++;
  1730. }
  1731. transition = nextTransition;
  1732. }
  1733. if (objectOffset + start + 1 == position) {
  1734. // first key, so we don't need to insert, we can just write record directly
  1735. position--;
  1736. newRecord(transition, keys, newTransitions);
  1737. } else // otherwise we need to insert the record, moving existing data after the record
  1738. insertNewRecord(transition, keys, objectOffset, newTransitions);
  1739. wroteKeys = true;
  1740. transition = lastTransition[key];
  1741. }
  1742. pack(object[key]);
  1743. }
  1744. }
  1745. if (!wroteKeys) {
  1746. let recordId = transition[RECORD_SYMBOL];
  1747. if (recordId)
  1748. target[objectOffset + start] = recordId;
  1749. else
  1750. insertNewRecord(transition, Object.keys(object), objectOffset, 0);
  1751. }
  1752. } :
  1753. (object) => {
  1754. let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
  1755. let newTransitions = 0;
  1756. for (let key in object) if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
  1757. nextTransition = transition[key];
  1758. if (!nextTransition) {
  1759. nextTransition = transition[key] = Object.create(null);
  1760. newTransitions++;
  1761. }
  1762. transition = nextTransition;
  1763. }
  1764. let recordId = transition[RECORD_SYMBOL];
  1765. if (recordId) {
  1766. if (recordId >= 0x60 && useTwoByteRecords) {
  1767. target[position++] = ((recordId -= 0x60) & 0x1f) + 0x60;
  1768. target[position++] = recordId >> 5;
  1769. } else
  1770. target[position++] = recordId;
  1771. } else {
  1772. newRecord(transition, transition.__keys__ || Object.keys(object), newTransitions);
  1773. }
  1774. // now write the values
  1775. for (let key in object)
  1776. if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
  1777. pack(object[key]);
  1778. }
  1779. };
  1780. // create reference to useRecords if useRecords is a function
  1781. const checkUseRecords = typeof this.useRecords == 'function' && this.useRecords;
  1782. const writeObject = checkUseRecords ? (object) => {
  1783. checkUseRecords(object) ? writeRecord(object) : writePlainObject(object);
  1784. } : writeRecord;
  1785. const makeRoom = (end) => {
  1786. let newSize;
  1787. if (end > 0x1000000) {
  1788. // special handling for really large buffers
  1789. if ((end - start) > MAX_BUFFER_SIZE)
  1790. throw new Error('Packed buffer would be larger than maximum buffer size')
  1791. newSize = Math.min(MAX_BUFFER_SIZE,
  1792. Math.round(Math.max((end - start) * (end > 0x4000000 ? 1.25 : 2), 0x400000) / 0x1000) * 0x1000);
  1793. } else // faster handling for smaller buffers
  1794. newSize = ((Math.max((end - start) << 2, target.length - 1) >> 12) + 1) << 12;
  1795. let newBuffer = new ByteArrayAllocate(newSize);
  1796. targetView = newBuffer.dataView || (newBuffer.dataView = new DataView(newBuffer.buffer, 0, newSize));
  1797. end = Math.min(end, target.length);
  1798. if (target.copy)
  1799. target.copy(newBuffer, 0, start, end);
  1800. else
  1801. newBuffer.set(target.slice(start, end));
  1802. position -= start;
  1803. start = 0;
  1804. safeEnd = newBuffer.length - 10;
  1805. return target = newBuffer
  1806. };
  1807. const newRecord = (transition, keys, newTransitions) => {
  1808. let recordId = structures.nextId;
  1809. if (!recordId)
  1810. recordId = 0x40;
  1811. if (recordId < sharedLimitId && this.shouldShareStructure && !this.shouldShareStructure(keys)) {
  1812. recordId = structures.nextOwnId;
  1813. if (!(recordId < maxStructureId))
  1814. recordId = sharedLimitId;
  1815. structures.nextOwnId = recordId + 1;
  1816. } else {
  1817. if (recordId >= maxStructureId)// cycle back around
  1818. recordId = sharedLimitId;
  1819. structures.nextId = recordId + 1;
  1820. }
  1821. let highByte = keys.highByte = recordId >= 0x60 && useTwoByteRecords ? (recordId - 0x60) >> 5 : -1;
  1822. transition[RECORD_SYMBOL] = recordId;
  1823. transition.__keys__ = keys;
  1824. structures[recordId - 0x40] = keys;
  1825. if (recordId < sharedLimitId) {
  1826. keys.isShared = true;
  1827. structures.sharedLength = recordId - 0x3f;
  1828. hasSharedUpdate = true;
  1829. if (highByte >= 0) {
  1830. target[position++] = (recordId & 0x1f) + 0x60;
  1831. target[position++] = highByte;
  1832. } else {
  1833. target[position++] = recordId;
  1834. }
  1835. } else {
  1836. if (highByte >= 0) {
  1837. target[position++] = 0xd5; // fixext 2
  1838. target[position++] = 0x72; // "r" record defintion extension type
  1839. target[position++] = (recordId & 0x1f) + 0x60;
  1840. target[position++] = highByte;
  1841. } else {
  1842. target[position++] = 0xd4; // fixext 1
  1843. target[position++] = 0x72; // "r" record defintion extension type
  1844. target[position++] = recordId;
  1845. }
  1846. if (newTransitions)
  1847. transitionsCount += serializationsSinceTransitionRebuild * newTransitions;
  1848. // record the removal of the id, we can maintain our shared structure
  1849. if (recordIdsToRemove.length >= maxOwnStructures)
  1850. recordIdsToRemove.shift()[RECORD_SYMBOL] = 0; // we are cycling back through, and have to remove old ones
  1851. recordIdsToRemove.push(transition);
  1852. pack(keys);
  1853. }
  1854. };
  1855. const insertNewRecord = (transition, keys, insertionOffset, newTransitions) => {
  1856. let mainTarget = target;
  1857. let mainPosition = position;
  1858. let mainSafeEnd = safeEnd;
  1859. let mainStart = start;
  1860. target = keysTarget;
  1861. position = 0;
  1862. start = 0;
  1863. if (!target)
  1864. keysTarget = target = new ByteArrayAllocate(8192);
  1865. safeEnd = target.length - 10;
  1866. newRecord(transition, keys, newTransitions);
  1867. keysTarget = target;
  1868. let keysPosition = position;
  1869. target = mainTarget;
  1870. position = mainPosition;
  1871. safeEnd = mainSafeEnd;
  1872. start = mainStart;
  1873. if (keysPosition > 1) {
  1874. let newEnd = position + keysPosition - 1;
  1875. if (newEnd > safeEnd)
  1876. makeRoom(newEnd);
  1877. let insertionPosition = insertionOffset + start;
  1878. target.copyWithin(insertionPosition + keysPosition, insertionPosition + 1, position);
  1879. target.set(keysTarget.slice(0, keysPosition), insertionPosition);
  1880. position = newEnd;
  1881. } else {
  1882. target[insertionOffset + start] = keysTarget[0];
  1883. }
  1884. };
  1885. const writeStruct = (object) => {
  1886. let newPosition = writeStructSlots(object, target, start, position, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
  1887. if (notifySharedUpdate)
  1888. return hasSharedUpdate = true;
  1889. position = newPosition;
  1890. let startTarget = target;
  1891. pack(value);
  1892. resetStructures();
  1893. if (startTarget !== target) {
  1894. return { position, targetView, target }; // indicate the buffer was re-allocated
  1895. }
  1896. return position;
  1897. }, this);
  1898. if (newPosition === 0) // bail and go to a msgpack object
  1899. return writeObject(object);
  1900. position = newPosition;
  1901. };
  1902. }
  1903. useBuffer(buffer) {
  1904. // this means we are finished using our own buffer and we can write over it safely
  1905. target = buffer;
  1906. target.dataView || (target.dataView = new DataView(target.buffer, target.byteOffset, target.byteLength));
  1907. position = 0;
  1908. }
  1909. set position (value) {
  1910. position = value;
  1911. }
  1912. get position() {
  1913. return position;
  1914. }
  1915. clearSharedData() {
  1916. if (this.structures)
  1917. this.structures = [];
  1918. if (this.typedStructs)
  1919. this.typedStructs = [];
  1920. }
  1921. }
  1922. extensionClasses = [ Date, Set, Error, RegExp, ArrayBuffer, Object.getPrototypeOf(Uint8Array.prototype).constructor /*TypedArray*/, C1Type ];
  1923. extensions = [{
  1924. pack(date, allocateForWrite, pack) {
  1925. let seconds = date.getTime() / 1000;
  1926. if ((this.useTimestamp32 || date.getMilliseconds() === 0) && seconds >= 0 && seconds < 0x100000000) {
  1927. // Timestamp 32
  1928. let { target, targetView, position} = allocateForWrite(6);
  1929. target[position++] = 0xd6;
  1930. target[position++] = 0xff;
  1931. targetView.setUint32(position, seconds);
  1932. } else if (seconds > 0 && seconds < 0x100000000) {
  1933. // Timestamp 64
  1934. let { target, targetView, position} = allocateForWrite(10);
  1935. target[position++] = 0xd7;
  1936. target[position++] = 0xff;
  1937. targetView.setUint32(position, date.getMilliseconds() * 4000000 + ((seconds / 1000 / 0x100000000) >> 0));
  1938. targetView.setUint32(position + 4, seconds);
  1939. } else if (isNaN(seconds)) {
  1940. if (this.onInvalidDate) {
  1941. allocateForWrite(0);
  1942. return pack(this.onInvalidDate())
  1943. }
  1944. // Intentionally invalid timestamp
  1945. let { target, targetView, position} = allocateForWrite(3);
  1946. target[position++] = 0xd4;
  1947. target[position++] = 0xff;
  1948. target[position++] = 0xff;
  1949. } else {
  1950. // Timestamp 96
  1951. let { target, targetView, position} = allocateForWrite(15);
  1952. target[position++] = 0xc7;
  1953. target[position++] = 12;
  1954. target[position++] = 0xff;
  1955. targetView.setUint32(position, date.getMilliseconds() * 1000000);
  1956. targetView.setBigInt64(position + 4, BigInt(Math.floor(seconds)));
  1957. }
  1958. }
  1959. }, {
  1960. pack(set, allocateForWrite, pack) {
  1961. if (this.setAsEmptyObject) {
  1962. allocateForWrite(0);
  1963. return pack({})
  1964. }
  1965. let array = Array.from(set);
  1966. let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
  1967. if (this.moreTypes) {
  1968. target[position++] = 0xd4;
  1969. target[position++] = 0x73; // 's' for Set
  1970. target[position++] = 0;
  1971. }
  1972. pack(array);
  1973. }
  1974. }, {
  1975. pack(error, allocateForWrite, pack) {
  1976. let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
  1977. if (this.moreTypes) {
  1978. target[position++] = 0xd4;
  1979. target[position++] = 0x65; // 'e' for error
  1980. target[position++] = 0;
  1981. }
  1982. pack([ error.name, error.message, error.cause ]);
  1983. }
  1984. }, {
  1985. pack(regex, allocateForWrite, pack) {
  1986. let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
  1987. if (this.moreTypes) {
  1988. target[position++] = 0xd4;
  1989. target[position++] = 0x78; // 'x' for regeXp
  1990. target[position++] = 0;
  1991. }
  1992. pack([ regex.source, regex.flags ]);
  1993. }
  1994. }, {
  1995. pack(arrayBuffer, allocateForWrite) {
  1996. if (this.moreTypes)
  1997. writeExtBuffer(arrayBuffer, 0x10, allocateForWrite);
  1998. else
  1999. writeBuffer(hasNodeBuffer ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite);
  2000. }
  2001. }, {
  2002. pack(typedArray, allocateForWrite) {
  2003. let constructor = typedArray.constructor;
  2004. if (constructor !== ByteArray && this.moreTypes)
  2005. writeExtBuffer(typedArray, typedArrays.indexOf(constructor.name), allocateForWrite);
  2006. else
  2007. writeBuffer(typedArray, allocateForWrite);
  2008. }
  2009. }, {
  2010. pack(c1, allocateForWrite) { // specific 0xC1 object
  2011. let { target, position} = allocateForWrite(1);
  2012. target[position] = 0xc1;
  2013. }
  2014. }];
  2015. function writeExtBuffer(typedArray, type, allocateForWrite, encode) {
  2016. let length = typedArray.byteLength;
  2017. if (length + 1 < 0x100) {
  2018. var { target, position } = allocateForWrite(4 + length);
  2019. target[position++] = 0xc7;
  2020. target[position++] = length + 1;
  2021. } else if (length + 1 < 0x10000) {
  2022. var { target, position } = allocateForWrite(5 + length);
  2023. target[position++] = 0xc8;
  2024. target[position++] = (length + 1) >> 8;
  2025. target[position++] = (length + 1) & 0xff;
  2026. } else {
  2027. var { target, position, targetView } = allocateForWrite(7 + length);
  2028. target[position++] = 0xc9;
  2029. targetView.setUint32(position, length + 1); // plus one for the type byte
  2030. position += 4;
  2031. }
  2032. target[position++] = 0x74; // "t" for typed array
  2033. target[position++] = type;
  2034. if (!typedArray.buffer) typedArray = new Uint8Array(typedArray);
  2035. target.set(new Uint8Array(typedArray.buffer, typedArray.byteOffset, typedArray.byteLength), position);
  2036. }
  2037. function writeBuffer(buffer, allocateForWrite) {
  2038. let length = buffer.byteLength;
  2039. var target, position;
  2040. if (length < 0x100) {
  2041. var { target, position } = allocateForWrite(length + 2);
  2042. target[position++] = 0xc4;
  2043. target[position++] = length;
  2044. } else if (length < 0x10000) {
  2045. var { target, position } = allocateForWrite(length + 3);
  2046. target[position++] = 0xc5;
  2047. target[position++] = length >> 8;
  2048. target[position++] = length & 0xff;
  2049. } else {
  2050. var { target, position, targetView } = allocateForWrite(length + 5);
  2051. target[position++] = 0xc6;
  2052. targetView.setUint32(position, length);
  2053. position += 4;
  2054. }
  2055. target.set(buffer, position);
  2056. }
  2057. function writeExtensionData(result, target, position, type) {
  2058. let length = result.length;
  2059. switch (length) {
  2060. case 1:
  2061. target[position++] = 0xd4;
  2062. break
  2063. case 2:
  2064. target[position++] = 0xd5;
  2065. break
  2066. case 4:
  2067. target[position++] = 0xd6;
  2068. break
  2069. case 8:
  2070. target[position++] = 0xd7;
  2071. break
  2072. case 16:
  2073. target[position++] = 0xd8;
  2074. break
  2075. default:
  2076. if (length < 0x100) {
  2077. target[position++] = 0xc7;
  2078. target[position++] = length;
  2079. } else if (length < 0x10000) {
  2080. target[position++] = 0xc8;
  2081. target[position++] = length >> 8;
  2082. target[position++] = length & 0xff;
  2083. } else {
  2084. target[position++] = 0xc9;
  2085. target[position++] = length >> 24;
  2086. target[position++] = (length >> 16) & 0xff;
  2087. target[position++] = (length >> 8) & 0xff;
  2088. target[position++] = length & 0xff;
  2089. }
  2090. }
  2091. target[position++] = type;
  2092. target.set(result, position);
  2093. position += length;
  2094. return position
  2095. }
  2096. function insertIds(serialized, idsToInsert) {
  2097. // insert the ids that need to be referenced for structured clones
  2098. let nextId;
  2099. let distanceToMove = idsToInsert.length * 6;
  2100. let lastEnd = serialized.length - distanceToMove;
  2101. while (nextId = idsToInsert.pop()) {
  2102. let offset = nextId.offset;
  2103. let id = nextId.id;
  2104. serialized.copyWithin(offset + distanceToMove, offset, lastEnd);
  2105. distanceToMove -= 6;
  2106. let position = offset + distanceToMove;
  2107. serialized[position++] = 0xd6;
  2108. serialized[position++] = 0x69; // 'i'
  2109. serialized[position++] = id >> 24;
  2110. serialized[position++] = (id >> 16) & 0xff;
  2111. serialized[position++] = (id >> 8) & 0xff;
  2112. serialized[position++] = id & 0xff;
  2113. lastEnd = offset;
  2114. }
  2115. return serialized
  2116. }
  2117. function writeBundles(start, pack, incrementPosition) {
  2118. if (bundledStrings.length > 0) {
  2119. targetView.setUint32(bundledStrings.position + start, position + incrementPosition - bundledStrings.position - start);
  2120. bundledStrings.stringsPosition = position - start;
  2121. let writeStrings = bundledStrings;
  2122. bundledStrings = null;
  2123. pack(writeStrings[0]);
  2124. pack(writeStrings[1]);
  2125. }
  2126. }
  2127. function addExtension(extension) {
  2128. if (extension.Class) {
  2129. if (!extension.pack && !extension.write)
  2130. throw new Error('Extension has no pack or write function')
  2131. if (extension.pack && !extension.type)
  2132. throw new Error('Extension has no type (numeric code to identify the extension)')
  2133. extensionClasses.unshift(extension.Class);
  2134. extensions.unshift(extension);
  2135. }
  2136. addExtension$1(extension);
  2137. }
  2138. function prepareStructures(structures, packr) {
  2139. structures.isCompatible = (existingStructures) => {
  2140. let compatible = !existingStructures || ((packr.lastNamedStructuresLength || 0) === existingStructures.length);
  2141. if (!compatible) // we want to merge these existing structures immediately since we already have it and we are in the right transaction
  2142. packr._mergeStructures(existingStructures);
  2143. return compatible;
  2144. };
  2145. return structures
  2146. }
  2147. let defaultPackr = new Packr({ useRecords: false });
  2148. const pack = defaultPackr.pack;
  2149. const encode = defaultPackr.pack;
  2150. const Encoder = Packr;
  2151. const { NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT } = FLOAT32_OPTIONS;
  2152. const REUSE_BUFFER_MODE = 512;
  2153. const RESET_BUFFER_MODE = 1024;
  2154. const RESERVE_START_SPACE = 2048;
  2155. /**
  2156. * Given an Iterable first argument, returns an Iterable where each value is packed as a Buffer
  2157. * If the argument is only Async Iterable, the return value will be an Async Iterable.
  2158. * @param {Iterable|Iterator|AsyncIterable|AsyncIterator} objectIterator - iterable source, like a Readable object stream, an array, Set, or custom object
  2159. * @param {options} [options] - msgpackr pack options
  2160. * @returns {IterableIterator|Promise.<AsyncIterableIterator>}
  2161. */
  2162. function packIter (objectIterator, options = {}) {
  2163. if (!objectIterator || typeof objectIterator !== 'object') {
  2164. throw new Error('first argument must be an Iterable, Async Iterable, or a Promise for an Async Iterable')
  2165. } else if (typeof objectIterator[Symbol.iterator] === 'function') {
  2166. return packIterSync(objectIterator, options)
  2167. } else if (typeof objectIterator.then === 'function' || typeof objectIterator[Symbol.asyncIterator] === 'function') {
  2168. return packIterAsync(objectIterator, options)
  2169. } else {
  2170. throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a Promise')
  2171. }
  2172. }
  2173. function * packIterSync (objectIterator, options) {
  2174. const packr = new Packr(options);
  2175. for (const value of objectIterator) {
  2176. yield packr.pack(value);
  2177. }
  2178. }
  2179. async function * packIterAsync (objectIterator, options) {
  2180. const packr = new Packr(options);
  2181. for await (const value of objectIterator) {
  2182. yield packr.pack(value);
  2183. }
  2184. }
  2185. /**
  2186. * Given an Iterable/Iterator input which yields buffers, returns an IterableIterator which yields sync decoded objects
  2187. * Or, given an Async Iterable/Iterator which yields promises resolving in buffers, returns an AsyncIterableIterator.
  2188. * @param {Iterable|Iterator|AsyncIterable|AsyncIterableIterator} bufferIterator
  2189. * @param {object} [options] - unpackr options
  2190. * @returns {IterableIterator|Promise.<AsyncIterableIterator}
  2191. */
  2192. function unpackIter (bufferIterator, options = {}) {
  2193. if (!bufferIterator || typeof bufferIterator !== 'object') {
  2194. throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a promise')
  2195. }
  2196. const unpackr = new Unpackr(options);
  2197. let incomplete;
  2198. const parser = (chunk) => {
  2199. let yields;
  2200. // if there's incomplete data from previous chunk, concatinate and try again
  2201. if (incomplete) {
  2202. chunk = Buffer.concat([incomplete, chunk]);
  2203. incomplete = undefined;
  2204. }
  2205. try {
  2206. yields = unpackr.unpackMultiple(chunk);
  2207. } catch (err) {
  2208. if (err.incomplete) {
  2209. incomplete = chunk.slice(err.lastPosition);
  2210. yields = err.values;
  2211. } else {
  2212. throw err
  2213. }
  2214. }
  2215. return yields
  2216. };
  2217. if (typeof bufferIterator[Symbol.iterator] === 'function') {
  2218. return (function * iter () {
  2219. for (const value of bufferIterator) {
  2220. yield * parser(value);
  2221. }
  2222. })()
  2223. } else if (typeof bufferIterator[Symbol.asyncIterator] === 'function') {
  2224. return (async function * iter () {
  2225. for await (const value of bufferIterator) {
  2226. yield * parser(value);
  2227. }
  2228. })()
  2229. }
  2230. }
  2231. const decodeIter = unpackIter;
  2232. const encodeIter = packIter;
  2233. const useRecords = false;
  2234. const mapsAsObjects = true;
  2235. exports.ALWAYS = ALWAYS;
  2236. exports.C1 = C1;
  2237. exports.DECIMAL_FIT = DECIMAL_FIT;
  2238. exports.DECIMAL_ROUND = DECIMAL_ROUND;
  2239. exports.Decoder = Decoder;
  2240. exports.Encoder = Encoder;
  2241. exports.FLOAT32_OPTIONS = FLOAT32_OPTIONS;
  2242. exports.NEVER = NEVER;
  2243. exports.Packr = Packr;
  2244. exports.RESERVE_START_SPACE = RESERVE_START_SPACE;
  2245. exports.RESET_BUFFER_MODE = RESET_BUFFER_MODE;
  2246. exports.REUSE_BUFFER_MODE = REUSE_BUFFER_MODE;
  2247. exports.Unpackr = Unpackr;
  2248. exports.addExtension = addExtension;
  2249. exports.clearSource = clearSource;
  2250. exports.decode = decode;
  2251. exports.decodeIter = decodeIter;
  2252. exports.encode = encode;
  2253. exports.encodeIter = encodeIter;
  2254. exports.isNativeAccelerationEnabled = isNativeAccelerationEnabled;
  2255. exports.mapsAsObjects = mapsAsObjects;
  2256. exports.pack = pack;
  2257. exports.roundFloat32 = roundFloat32;
  2258. exports.unpack = unpack;
  2259. exports.unpackMultiple = unpackMultiple;
  2260. exports.useRecords = useRecords;
  2261. }));
  2262. //# sourceMappingURL=index-no-eval.cjs.map