index.js 78 KB

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