@rpgjs/tiledmap 5.0.0-beta.2 → 5.0.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,13 +1,3999 @@
1
- import { defineModule } from "@rpgjs/common";
2
- import { prepareTiledPhysicsData } from "./index5.js";
3
- const client = defineModule({
4
- componentAnimations: [],
5
- sceneMap: {
6
- onPhysicsInit(map, context) {
7
- prepareTiledPhysicsData(context?.mapData, map);
8
- }
9
- }
10
- });
11
- export {
12
- client as default
1
+ //#region ../../node_modules/.pnpm/@canvasengine+tiled@2.0.0-beta.58/node_modules/@canvasengine/tiled/dist/index.js
2
+ var __commonJSMin = (cb, mod) => () => (mod || (cb((mod = { exports: {} }).exports, mod), cb = null), mod.exports);
3
+ var TiledLayerType = /* @__PURE__ */ function(TiledLayerType) {
4
+ TiledLayerType["Tile"] = "tilelayer";
5
+ TiledLayerType["ObjectGroup"] = "objectgroup";
6
+ TiledLayerType["Image"] = "imagelayer";
7
+ TiledLayerType["Group"] = "group";
8
+ return TiledLayerType;
9
+ }({});
10
+ /**
11
+ * Join path segments with forward slashes
12
+ * @param {...string} segments - Path segments to join
13
+ * @returns {string} Joined path
14
+ * @example
15
+ * joinPath('base', 'static', 'file.json') // returns 'base/static/file.json'
16
+ */
17
+ function joinPath(...segments) {
18
+ return segments.filter((segment) => segment && segment.length > 0).join("/").replace(/\/+/g, "/");
19
+ }
20
+ var require___vite_browser_external = /* @__PURE__ */ __commonJSMin(((exports, module) => {
21
+ module.exports = {};
22
+ }));
23
+ var require_sax = /* @__PURE__ */ __commonJSMin(((exports) => {
24
+ (function(sax) {
25
+ sax.parser = function(strict, opt) {
26
+ return new SAXParser(strict, opt);
27
+ };
28
+ sax.SAXParser = SAXParser;
29
+ sax.SAXStream = SAXStream;
30
+ sax.createStream = createStream;
31
+ sax.MAX_BUFFER_LENGTH = 64 * 1024;
32
+ var buffers = [
33
+ "comment",
34
+ "sgmlDecl",
35
+ "textNode",
36
+ "tagName",
37
+ "doctype",
38
+ "procInstName",
39
+ "procInstBody",
40
+ "entity",
41
+ "attribName",
42
+ "attribValue",
43
+ "cdata",
44
+ "script"
45
+ ];
46
+ sax.EVENTS = [
47
+ "text",
48
+ "processinginstruction",
49
+ "sgmldeclaration",
50
+ "doctype",
51
+ "comment",
52
+ "opentagstart",
53
+ "attribute",
54
+ "opentag",
55
+ "closetag",
56
+ "opencdata",
57
+ "cdata",
58
+ "closecdata",
59
+ "error",
60
+ "end",
61
+ "ready",
62
+ "script",
63
+ "opennamespace",
64
+ "closenamespace"
65
+ ];
66
+ function SAXParser(strict, opt) {
67
+ if (!(this instanceof SAXParser)) return new SAXParser(strict, opt);
68
+ var parser = this;
69
+ clearBuffers(parser);
70
+ parser.q = parser.c = "";
71
+ parser.bufferCheckPosition = sax.MAX_BUFFER_LENGTH;
72
+ parser.opt = opt || {};
73
+ parser.opt.lowercase = parser.opt.lowercase || parser.opt.lowercasetags;
74
+ parser.looseCase = parser.opt.lowercase ? "toLowerCase" : "toUpperCase";
75
+ parser.tags = [];
76
+ parser.closed = parser.closedRoot = parser.sawRoot = false;
77
+ parser.tag = parser.error = null;
78
+ parser.strict = !!strict;
79
+ parser.noscript = !!(strict || parser.opt.noscript);
80
+ parser.state = S.BEGIN;
81
+ parser.strictEntities = parser.opt.strictEntities;
82
+ parser.ENTITIES = parser.strictEntities ? Object.create(sax.XML_ENTITIES) : Object.create(sax.ENTITIES);
83
+ parser.attribList = [];
84
+ if (parser.opt.xmlns) parser.ns = Object.create(rootNS);
85
+ if (parser.opt.unquotedAttributeValues === void 0) parser.opt.unquotedAttributeValues = !strict;
86
+ parser.trackPosition = parser.opt.position !== false;
87
+ if (parser.trackPosition) parser.position = parser.line = parser.column = 0;
88
+ emit(parser, "onready");
89
+ }
90
+ if (!Object.create) Object.create = function(o) {
91
+ function F() {}
92
+ F.prototype = o;
93
+ return new F();
94
+ };
95
+ if (!Object.keys) Object.keys = function(o) {
96
+ var a = [];
97
+ for (var i in o) if (o.hasOwnProperty(i)) a.push(i);
98
+ return a;
99
+ };
100
+ function checkBufferLength(parser) {
101
+ var maxAllowed = Math.max(sax.MAX_BUFFER_LENGTH, 10);
102
+ var maxActual = 0;
103
+ for (var i = 0, l = buffers.length; i < l; i++) {
104
+ var len = parser[buffers[i]].length;
105
+ if (len > maxAllowed) switch (buffers[i]) {
106
+ case "textNode":
107
+ closeText(parser);
108
+ break;
109
+ case "cdata":
110
+ emitNode(parser, "oncdata", parser.cdata);
111
+ parser.cdata = "";
112
+ break;
113
+ case "script":
114
+ emitNode(parser, "onscript", parser.script);
115
+ parser.script = "";
116
+ break;
117
+ default: error(parser, "Max buffer length exceeded: " + buffers[i]);
118
+ }
119
+ maxActual = Math.max(maxActual, len);
120
+ }
121
+ parser.bufferCheckPosition = sax.MAX_BUFFER_LENGTH - maxActual + parser.position;
122
+ }
123
+ function clearBuffers(parser) {
124
+ for (var i = 0, l = buffers.length; i < l; i++) parser[buffers[i]] = "";
125
+ }
126
+ function flushBuffers(parser) {
127
+ closeText(parser);
128
+ if (parser.cdata !== "") {
129
+ emitNode(parser, "oncdata", parser.cdata);
130
+ parser.cdata = "";
131
+ }
132
+ if (parser.script !== "") {
133
+ emitNode(parser, "onscript", parser.script);
134
+ parser.script = "";
135
+ }
136
+ }
137
+ SAXParser.prototype = {
138
+ end: function() {
139
+ end(this);
140
+ },
141
+ write,
142
+ resume: function() {
143
+ this.error = null;
144
+ return this;
145
+ },
146
+ close: function() {
147
+ return this.write(null);
148
+ },
149
+ flush: function() {
150
+ flushBuffers(this);
151
+ }
152
+ };
153
+ var Stream;
154
+ try {
155
+ Stream = require___vite_browser_external().Stream;
156
+ } catch (ex) {
157
+ Stream = function() {};
158
+ }
159
+ if (!Stream) Stream = function() {};
160
+ var streamWraps = sax.EVENTS.filter(function(ev) {
161
+ return ev !== "error" && ev !== "end";
162
+ });
163
+ function createStream(strict, opt) {
164
+ return new SAXStream(strict, opt);
165
+ }
166
+ function SAXStream(strict, opt) {
167
+ if (!(this instanceof SAXStream)) return new SAXStream(strict, opt);
168
+ Stream.apply(this);
169
+ this._parser = new SAXParser(strict, opt);
170
+ this.writable = true;
171
+ this.readable = true;
172
+ var me = this;
173
+ this._parser.onend = function() {
174
+ me.emit("end");
175
+ };
176
+ this._parser.onerror = function(er) {
177
+ me.emit("error", er);
178
+ me._parser.error = null;
179
+ };
180
+ this._decoder = null;
181
+ streamWraps.forEach(function(ev) {
182
+ Object.defineProperty(me, "on" + ev, {
183
+ get: function() {
184
+ return me._parser["on" + ev];
185
+ },
186
+ set: function(h) {
187
+ if (!h) {
188
+ me.removeAllListeners(ev);
189
+ me._parser["on" + ev] = h;
190
+ return h;
191
+ }
192
+ me.on(ev, h);
193
+ },
194
+ enumerable: true,
195
+ configurable: false
196
+ });
197
+ });
198
+ }
199
+ SAXStream.prototype = Object.create(Stream.prototype, { constructor: { value: SAXStream } });
200
+ SAXStream.prototype.write = function(data) {
201
+ if (typeof Buffer === "function" && typeof Buffer.isBuffer === "function" && Buffer.isBuffer(data)) {
202
+ if (!this._decoder) {
203
+ var SD = require___vite_browser_external().StringDecoder;
204
+ this._decoder = new SD("utf8");
205
+ }
206
+ data = this._decoder.write(data);
207
+ }
208
+ this._parser.write(data.toString());
209
+ this.emit("data", data);
210
+ return true;
211
+ };
212
+ SAXStream.prototype.end = function(chunk) {
213
+ if (chunk && chunk.length) this.write(chunk);
214
+ this._parser.end();
215
+ return true;
216
+ };
217
+ SAXStream.prototype.on = function(ev, handler) {
218
+ var me = this;
219
+ if (!me._parser["on" + ev] && streamWraps.indexOf(ev) !== -1) me._parser["on" + ev] = function() {
220
+ var args = arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments);
221
+ args.splice(0, 0, ev);
222
+ me.emit.apply(me, args);
223
+ };
224
+ return Stream.prototype.on.call(me, ev, handler);
225
+ };
226
+ var CDATA = "[CDATA[";
227
+ var DOCTYPE = "DOCTYPE";
228
+ var XML_NAMESPACE = "http://www.w3.org/XML/1998/namespace";
229
+ var XMLNS_NAMESPACE = "http://www.w3.org/2000/xmlns/";
230
+ var rootNS = {
231
+ xml: XML_NAMESPACE,
232
+ xmlns: XMLNS_NAMESPACE
233
+ };
234
+ var nameStart = /[:_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]/;
235
+ var nameBody = /[:_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u00B7\u0300-\u036F\u203F-\u2040.\d-]/;
236
+ var entityStart = /[#:_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]/;
237
+ var entityBody = /[#:_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u00B7\u0300-\u036F\u203F-\u2040.\d-]/;
238
+ function isWhitespace(c) {
239
+ return c === " " || c === "\n" || c === "\r" || c === " ";
240
+ }
241
+ function isQuote(c) {
242
+ return c === "\"" || c === "'";
243
+ }
244
+ function isAttribEnd(c) {
245
+ return c === ">" || isWhitespace(c);
246
+ }
247
+ function isMatch(regex, c) {
248
+ return regex.test(c);
249
+ }
250
+ function notMatch(regex, c) {
251
+ return !isMatch(regex, c);
252
+ }
253
+ var S = 0;
254
+ sax.STATE = {
255
+ BEGIN: S++,
256
+ BEGIN_WHITESPACE: S++,
257
+ TEXT: S++,
258
+ TEXT_ENTITY: S++,
259
+ OPEN_WAKA: S++,
260
+ SGML_DECL: S++,
261
+ SGML_DECL_QUOTED: S++,
262
+ DOCTYPE: S++,
263
+ DOCTYPE_QUOTED: S++,
264
+ DOCTYPE_DTD: S++,
265
+ DOCTYPE_DTD_QUOTED: S++,
266
+ COMMENT_STARTING: S++,
267
+ COMMENT: S++,
268
+ COMMENT_ENDING: S++,
269
+ COMMENT_ENDED: S++,
270
+ CDATA: S++,
271
+ CDATA_ENDING: S++,
272
+ CDATA_ENDING_2: S++,
273
+ PROC_INST: S++,
274
+ PROC_INST_BODY: S++,
275
+ PROC_INST_ENDING: S++,
276
+ OPEN_TAG: S++,
277
+ OPEN_TAG_SLASH: S++,
278
+ ATTRIB: S++,
279
+ ATTRIB_NAME: S++,
280
+ ATTRIB_NAME_SAW_WHITE: S++,
281
+ ATTRIB_VALUE: S++,
282
+ ATTRIB_VALUE_QUOTED: S++,
283
+ ATTRIB_VALUE_CLOSED: S++,
284
+ ATTRIB_VALUE_UNQUOTED: S++,
285
+ ATTRIB_VALUE_ENTITY_Q: S++,
286
+ ATTRIB_VALUE_ENTITY_U: S++,
287
+ CLOSE_TAG: S++,
288
+ CLOSE_TAG_SAW_WHITE: S++,
289
+ SCRIPT: S++,
290
+ SCRIPT_ENDING: S++
291
+ };
292
+ sax.XML_ENTITIES = {
293
+ "amp": "&",
294
+ "gt": ">",
295
+ "lt": "<",
296
+ "quot": "\"",
297
+ "apos": "'"
298
+ };
299
+ sax.ENTITIES = {
300
+ "amp": "&",
301
+ "gt": ">",
302
+ "lt": "<",
303
+ "quot": "\"",
304
+ "apos": "'",
305
+ "AElig": 198,
306
+ "Aacute": 193,
307
+ "Acirc": 194,
308
+ "Agrave": 192,
309
+ "Aring": 197,
310
+ "Atilde": 195,
311
+ "Auml": 196,
312
+ "Ccedil": 199,
313
+ "ETH": 208,
314
+ "Eacute": 201,
315
+ "Ecirc": 202,
316
+ "Egrave": 200,
317
+ "Euml": 203,
318
+ "Iacute": 205,
319
+ "Icirc": 206,
320
+ "Igrave": 204,
321
+ "Iuml": 207,
322
+ "Ntilde": 209,
323
+ "Oacute": 211,
324
+ "Ocirc": 212,
325
+ "Ograve": 210,
326
+ "Oslash": 216,
327
+ "Otilde": 213,
328
+ "Ouml": 214,
329
+ "THORN": 222,
330
+ "Uacute": 218,
331
+ "Ucirc": 219,
332
+ "Ugrave": 217,
333
+ "Uuml": 220,
334
+ "Yacute": 221,
335
+ "aacute": 225,
336
+ "acirc": 226,
337
+ "aelig": 230,
338
+ "agrave": 224,
339
+ "aring": 229,
340
+ "atilde": 227,
341
+ "auml": 228,
342
+ "ccedil": 231,
343
+ "eacute": 233,
344
+ "ecirc": 234,
345
+ "egrave": 232,
346
+ "eth": 240,
347
+ "euml": 235,
348
+ "iacute": 237,
349
+ "icirc": 238,
350
+ "igrave": 236,
351
+ "iuml": 239,
352
+ "ntilde": 241,
353
+ "oacute": 243,
354
+ "ocirc": 244,
355
+ "ograve": 242,
356
+ "oslash": 248,
357
+ "otilde": 245,
358
+ "ouml": 246,
359
+ "szlig": 223,
360
+ "thorn": 254,
361
+ "uacute": 250,
362
+ "ucirc": 251,
363
+ "ugrave": 249,
364
+ "uuml": 252,
365
+ "yacute": 253,
366
+ "yuml": 255,
367
+ "copy": 169,
368
+ "reg": 174,
369
+ "nbsp": 160,
370
+ "iexcl": 161,
371
+ "cent": 162,
372
+ "pound": 163,
373
+ "curren": 164,
374
+ "yen": 165,
375
+ "brvbar": 166,
376
+ "sect": 167,
377
+ "uml": 168,
378
+ "ordf": 170,
379
+ "laquo": 171,
380
+ "not": 172,
381
+ "shy": 173,
382
+ "macr": 175,
383
+ "deg": 176,
384
+ "plusmn": 177,
385
+ "sup1": 185,
386
+ "sup2": 178,
387
+ "sup3": 179,
388
+ "acute": 180,
389
+ "micro": 181,
390
+ "para": 182,
391
+ "middot": 183,
392
+ "cedil": 184,
393
+ "ordm": 186,
394
+ "raquo": 187,
395
+ "frac14": 188,
396
+ "frac12": 189,
397
+ "frac34": 190,
398
+ "iquest": 191,
399
+ "times": 215,
400
+ "divide": 247,
401
+ "OElig": 338,
402
+ "oelig": 339,
403
+ "Scaron": 352,
404
+ "scaron": 353,
405
+ "Yuml": 376,
406
+ "fnof": 402,
407
+ "circ": 710,
408
+ "tilde": 732,
409
+ "Alpha": 913,
410
+ "Beta": 914,
411
+ "Gamma": 915,
412
+ "Delta": 916,
413
+ "Epsilon": 917,
414
+ "Zeta": 918,
415
+ "Eta": 919,
416
+ "Theta": 920,
417
+ "Iota": 921,
418
+ "Kappa": 922,
419
+ "Lambda": 923,
420
+ "Mu": 924,
421
+ "Nu": 925,
422
+ "Xi": 926,
423
+ "Omicron": 927,
424
+ "Pi": 928,
425
+ "Rho": 929,
426
+ "Sigma": 931,
427
+ "Tau": 932,
428
+ "Upsilon": 933,
429
+ "Phi": 934,
430
+ "Chi": 935,
431
+ "Psi": 936,
432
+ "Omega": 937,
433
+ "alpha": 945,
434
+ "beta": 946,
435
+ "gamma": 947,
436
+ "delta": 948,
437
+ "epsilon": 949,
438
+ "zeta": 950,
439
+ "eta": 951,
440
+ "theta": 952,
441
+ "iota": 953,
442
+ "kappa": 954,
443
+ "lambda": 955,
444
+ "mu": 956,
445
+ "nu": 957,
446
+ "xi": 958,
447
+ "omicron": 959,
448
+ "pi": 960,
449
+ "rho": 961,
450
+ "sigmaf": 962,
451
+ "sigma": 963,
452
+ "tau": 964,
453
+ "upsilon": 965,
454
+ "phi": 966,
455
+ "chi": 967,
456
+ "psi": 968,
457
+ "omega": 969,
458
+ "thetasym": 977,
459
+ "upsih": 978,
460
+ "piv": 982,
461
+ "ensp": 8194,
462
+ "emsp": 8195,
463
+ "thinsp": 8201,
464
+ "zwnj": 8204,
465
+ "zwj": 8205,
466
+ "lrm": 8206,
467
+ "rlm": 8207,
468
+ "ndash": 8211,
469
+ "mdash": 8212,
470
+ "lsquo": 8216,
471
+ "rsquo": 8217,
472
+ "sbquo": 8218,
473
+ "ldquo": 8220,
474
+ "rdquo": 8221,
475
+ "bdquo": 8222,
476
+ "dagger": 8224,
477
+ "Dagger": 8225,
478
+ "bull": 8226,
479
+ "hellip": 8230,
480
+ "permil": 8240,
481
+ "prime": 8242,
482
+ "Prime": 8243,
483
+ "lsaquo": 8249,
484
+ "rsaquo": 8250,
485
+ "oline": 8254,
486
+ "frasl": 8260,
487
+ "euro": 8364,
488
+ "image": 8465,
489
+ "weierp": 8472,
490
+ "real": 8476,
491
+ "trade": 8482,
492
+ "alefsym": 8501,
493
+ "larr": 8592,
494
+ "uarr": 8593,
495
+ "rarr": 8594,
496
+ "darr": 8595,
497
+ "harr": 8596,
498
+ "crarr": 8629,
499
+ "lArr": 8656,
500
+ "uArr": 8657,
501
+ "rArr": 8658,
502
+ "dArr": 8659,
503
+ "hArr": 8660,
504
+ "forall": 8704,
505
+ "part": 8706,
506
+ "exist": 8707,
507
+ "empty": 8709,
508
+ "nabla": 8711,
509
+ "isin": 8712,
510
+ "notin": 8713,
511
+ "ni": 8715,
512
+ "prod": 8719,
513
+ "sum": 8721,
514
+ "minus": 8722,
515
+ "lowast": 8727,
516
+ "radic": 8730,
517
+ "prop": 8733,
518
+ "infin": 8734,
519
+ "ang": 8736,
520
+ "and": 8743,
521
+ "or": 8744,
522
+ "cap": 8745,
523
+ "cup": 8746,
524
+ "int": 8747,
525
+ "there4": 8756,
526
+ "sim": 8764,
527
+ "cong": 8773,
528
+ "asymp": 8776,
529
+ "ne": 8800,
530
+ "equiv": 8801,
531
+ "le": 8804,
532
+ "ge": 8805,
533
+ "sub": 8834,
534
+ "sup": 8835,
535
+ "nsub": 8836,
536
+ "sube": 8838,
537
+ "supe": 8839,
538
+ "oplus": 8853,
539
+ "otimes": 8855,
540
+ "perp": 8869,
541
+ "sdot": 8901,
542
+ "lceil": 8968,
543
+ "rceil": 8969,
544
+ "lfloor": 8970,
545
+ "rfloor": 8971,
546
+ "lang": 9001,
547
+ "rang": 9002,
548
+ "loz": 9674,
549
+ "spades": 9824,
550
+ "clubs": 9827,
551
+ "hearts": 9829,
552
+ "diams": 9830
553
+ };
554
+ Object.keys(sax.ENTITIES).forEach(function(key) {
555
+ var e = sax.ENTITIES[key];
556
+ var s = typeof e === "number" ? String.fromCharCode(e) : e;
557
+ sax.ENTITIES[key] = s;
558
+ });
559
+ for (var s in sax.STATE) sax.STATE[sax.STATE[s]] = s;
560
+ S = sax.STATE;
561
+ function emit(parser, event, data) {
562
+ parser[event] && parser[event](data);
563
+ }
564
+ function emitNode(parser, nodeType, data) {
565
+ if (parser.textNode) closeText(parser);
566
+ emit(parser, nodeType, data);
567
+ }
568
+ function closeText(parser) {
569
+ parser.textNode = textopts(parser.opt, parser.textNode);
570
+ if (parser.textNode) emit(parser, "ontext", parser.textNode);
571
+ parser.textNode = "";
572
+ }
573
+ function textopts(opt, text) {
574
+ if (opt.trim) text = text.trim();
575
+ if (opt.normalize) text = text.replace(/\s+/g, " ");
576
+ return text;
577
+ }
578
+ function error(parser, er) {
579
+ closeText(parser);
580
+ if (parser.trackPosition) er += "\nLine: " + parser.line + "\nColumn: " + parser.column + "\nChar: " + parser.c;
581
+ er = new Error(er);
582
+ parser.error = er;
583
+ emit(parser, "onerror", er);
584
+ return parser;
585
+ }
586
+ function end(parser) {
587
+ if (parser.sawRoot && !parser.closedRoot) strictFail(parser, "Unclosed root tag");
588
+ if (parser.state !== S.BEGIN && parser.state !== S.BEGIN_WHITESPACE && parser.state !== S.TEXT) error(parser, "Unexpected end");
589
+ closeText(parser);
590
+ parser.c = "";
591
+ parser.closed = true;
592
+ emit(parser, "onend");
593
+ SAXParser.call(parser, parser.strict, parser.opt);
594
+ return parser;
595
+ }
596
+ function strictFail(parser, message) {
597
+ if (typeof parser !== "object" || !(parser instanceof SAXParser)) throw new Error("bad call to strictFail");
598
+ if (parser.strict) error(parser, message);
599
+ }
600
+ function newTag(parser) {
601
+ if (!parser.strict) parser.tagName = parser.tagName[parser.looseCase]();
602
+ var parent = parser.tags[parser.tags.length - 1] || parser;
603
+ var tag = parser.tag = {
604
+ name: parser.tagName,
605
+ attributes: {}
606
+ };
607
+ if (parser.opt.xmlns) tag.ns = parent.ns;
608
+ parser.attribList.length = 0;
609
+ emitNode(parser, "onopentagstart", tag);
610
+ }
611
+ function qname(name, attribute) {
612
+ var qualName = name.indexOf(":") < 0 ? ["", name] : name.split(":");
613
+ var prefix = qualName[0];
614
+ var local = qualName[1];
615
+ if (attribute && name === "xmlns") {
616
+ prefix = "xmlns";
617
+ local = "";
618
+ }
619
+ return {
620
+ prefix,
621
+ local
622
+ };
623
+ }
624
+ function attrib(parser) {
625
+ if (!parser.strict) parser.attribName = parser.attribName[parser.looseCase]();
626
+ if (parser.attribList.indexOf(parser.attribName) !== -1 || parser.tag.attributes.hasOwnProperty(parser.attribName)) {
627
+ parser.attribName = parser.attribValue = "";
628
+ return;
629
+ }
630
+ if (parser.opt.xmlns) {
631
+ var qn = qname(parser.attribName, true);
632
+ var prefix = qn.prefix;
633
+ var local = qn.local;
634
+ if (prefix === "xmlns") if (local === "xml" && parser.attribValue !== XML_NAMESPACE) strictFail(parser, "xml: prefix must be bound to " + XML_NAMESPACE + "\nActual: " + parser.attribValue);
635
+ else if (local === "xmlns" && parser.attribValue !== XMLNS_NAMESPACE) strictFail(parser, "xmlns: prefix must be bound to " + XMLNS_NAMESPACE + "\nActual: " + parser.attribValue);
636
+ else {
637
+ var tag = parser.tag;
638
+ var parent = parser.tags[parser.tags.length - 1] || parser;
639
+ if (tag.ns === parent.ns) tag.ns = Object.create(parent.ns);
640
+ tag.ns[local] = parser.attribValue;
641
+ }
642
+ parser.attribList.push([parser.attribName, parser.attribValue]);
643
+ } else {
644
+ parser.tag.attributes[parser.attribName] = parser.attribValue;
645
+ emitNode(parser, "onattribute", {
646
+ name: parser.attribName,
647
+ value: parser.attribValue
648
+ });
649
+ }
650
+ parser.attribName = parser.attribValue = "";
651
+ }
652
+ function openTag(parser, selfClosing) {
653
+ if (parser.opt.xmlns) {
654
+ var tag = parser.tag;
655
+ var qn = qname(parser.tagName);
656
+ tag.prefix = qn.prefix;
657
+ tag.local = qn.local;
658
+ tag.uri = tag.ns[qn.prefix] || "";
659
+ if (tag.prefix && !tag.uri) {
660
+ strictFail(parser, "Unbound namespace prefix: " + JSON.stringify(parser.tagName));
661
+ tag.uri = qn.prefix;
662
+ }
663
+ var parent = parser.tags[parser.tags.length - 1] || parser;
664
+ if (tag.ns && parent.ns !== tag.ns) Object.keys(tag.ns).forEach(function(p) {
665
+ emitNode(parser, "onopennamespace", {
666
+ prefix: p,
667
+ uri: tag.ns[p]
668
+ });
669
+ });
670
+ for (var i = 0, l = parser.attribList.length; i < l; i++) {
671
+ var nv = parser.attribList[i];
672
+ var name = nv[0];
673
+ var value = nv[1];
674
+ var qualName = qname(name, true);
675
+ var prefix = qualName.prefix;
676
+ var local = qualName.local;
677
+ var uri = prefix === "" ? "" : tag.ns[prefix] || "";
678
+ var a = {
679
+ name,
680
+ value,
681
+ prefix,
682
+ local,
683
+ uri
684
+ };
685
+ if (prefix && prefix !== "xmlns" && !uri) {
686
+ strictFail(parser, "Unbound namespace prefix: " + JSON.stringify(prefix));
687
+ a.uri = prefix;
688
+ }
689
+ parser.tag.attributes[name] = a;
690
+ emitNode(parser, "onattribute", a);
691
+ }
692
+ parser.attribList.length = 0;
693
+ }
694
+ parser.tag.isSelfClosing = !!selfClosing;
695
+ parser.sawRoot = true;
696
+ parser.tags.push(parser.tag);
697
+ emitNode(parser, "onopentag", parser.tag);
698
+ if (!selfClosing) {
699
+ if (!parser.noscript && parser.tagName.toLowerCase() === "script") parser.state = S.SCRIPT;
700
+ else parser.state = S.TEXT;
701
+ parser.tag = null;
702
+ parser.tagName = "";
703
+ }
704
+ parser.attribName = parser.attribValue = "";
705
+ parser.attribList.length = 0;
706
+ }
707
+ function closeTag(parser) {
708
+ if (!parser.tagName) {
709
+ strictFail(parser, "Weird empty close tag.");
710
+ parser.textNode += "</>";
711
+ parser.state = S.TEXT;
712
+ return;
713
+ }
714
+ if (parser.script) {
715
+ if (parser.tagName !== "script") {
716
+ parser.script += "</" + parser.tagName + ">";
717
+ parser.tagName = "";
718
+ parser.state = S.SCRIPT;
719
+ return;
720
+ }
721
+ emitNode(parser, "onscript", parser.script);
722
+ parser.script = "";
723
+ }
724
+ var t = parser.tags.length;
725
+ var tagName = parser.tagName;
726
+ if (!parser.strict) tagName = tagName[parser.looseCase]();
727
+ var closeTo = tagName;
728
+ while (t--) if (parser.tags[t].name !== closeTo) strictFail(parser, "Unexpected close tag");
729
+ else break;
730
+ if (t < 0) {
731
+ strictFail(parser, "Unmatched closing tag: " + parser.tagName);
732
+ parser.textNode += "</" + parser.tagName + ">";
733
+ parser.state = S.TEXT;
734
+ return;
735
+ }
736
+ parser.tagName = tagName;
737
+ var s = parser.tags.length;
738
+ while (s-- > t) {
739
+ var tag = parser.tag = parser.tags.pop();
740
+ parser.tagName = parser.tag.name;
741
+ emitNode(parser, "onclosetag", parser.tagName);
742
+ var x = {};
743
+ for (var i in tag.ns) x[i] = tag.ns[i];
744
+ var parent = parser.tags[parser.tags.length - 1] || parser;
745
+ if (parser.opt.xmlns && tag.ns !== parent.ns) Object.keys(tag.ns).forEach(function(p) {
746
+ var n = tag.ns[p];
747
+ emitNode(parser, "onclosenamespace", {
748
+ prefix: p,
749
+ uri: n
750
+ });
751
+ });
752
+ }
753
+ if (t === 0) parser.closedRoot = true;
754
+ parser.tagName = parser.attribValue = parser.attribName = "";
755
+ parser.attribList.length = 0;
756
+ parser.state = S.TEXT;
757
+ }
758
+ function parseEntity(parser) {
759
+ var entity = parser.entity;
760
+ var entityLC = entity.toLowerCase();
761
+ var num;
762
+ var numStr = "";
763
+ if (parser.ENTITIES[entity]) return parser.ENTITIES[entity];
764
+ if (parser.ENTITIES[entityLC]) return parser.ENTITIES[entityLC];
765
+ entity = entityLC;
766
+ if (entity.charAt(0) === "#") if (entity.charAt(1) === "x") {
767
+ entity = entity.slice(2);
768
+ num = parseInt(entity, 16);
769
+ numStr = num.toString(16);
770
+ } else {
771
+ entity = entity.slice(1);
772
+ num = parseInt(entity, 10);
773
+ numStr = num.toString(10);
774
+ }
775
+ entity = entity.replace(/^0+/, "");
776
+ if (isNaN(num) || numStr.toLowerCase() !== entity) {
777
+ strictFail(parser, "Invalid character entity");
778
+ return "&" + parser.entity + ";";
779
+ }
780
+ return String.fromCodePoint(num);
781
+ }
782
+ function beginWhiteSpace(parser, c) {
783
+ if (c === "<") {
784
+ parser.state = S.OPEN_WAKA;
785
+ parser.startTagPosition = parser.position;
786
+ } else if (!isWhitespace(c)) {
787
+ strictFail(parser, "Non-whitespace before first tag.");
788
+ parser.textNode = c;
789
+ parser.state = S.TEXT;
790
+ }
791
+ }
792
+ function charAt(chunk, i) {
793
+ var result = "";
794
+ if (i < chunk.length) result = chunk.charAt(i);
795
+ return result;
796
+ }
797
+ function write(chunk) {
798
+ var parser = this;
799
+ if (this.error) throw this.error;
800
+ if (parser.closed) return error(parser, "Cannot write after close. Assign an onready handler.");
801
+ if (chunk === null) return end(parser);
802
+ if (typeof chunk === "object") chunk = chunk.toString();
803
+ var i = 0;
804
+ var c = "";
805
+ while (true) {
806
+ c = charAt(chunk, i++);
807
+ parser.c = c;
808
+ if (!c) break;
809
+ if (parser.trackPosition) {
810
+ parser.position++;
811
+ if (c === "\n") {
812
+ parser.line++;
813
+ parser.column = 0;
814
+ } else parser.column++;
815
+ }
816
+ switch (parser.state) {
817
+ case S.BEGIN:
818
+ parser.state = S.BEGIN_WHITESPACE;
819
+ if (c === "") continue;
820
+ beginWhiteSpace(parser, c);
821
+ continue;
822
+ case S.BEGIN_WHITESPACE:
823
+ beginWhiteSpace(parser, c);
824
+ continue;
825
+ case S.TEXT:
826
+ if (parser.sawRoot && !parser.closedRoot) {
827
+ var starti = i - 1;
828
+ while (c && c !== "<" && c !== "&") {
829
+ c = charAt(chunk, i++);
830
+ if (c && parser.trackPosition) {
831
+ parser.position++;
832
+ if (c === "\n") {
833
+ parser.line++;
834
+ parser.column = 0;
835
+ } else parser.column++;
836
+ }
837
+ }
838
+ parser.textNode += chunk.substring(starti, i - 1);
839
+ }
840
+ if (c === "<" && !(parser.sawRoot && parser.closedRoot && !parser.strict)) {
841
+ parser.state = S.OPEN_WAKA;
842
+ parser.startTagPosition = parser.position;
843
+ } else {
844
+ if (!isWhitespace(c) && (!parser.sawRoot || parser.closedRoot)) strictFail(parser, "Text data outside of root node.");
845
+ if (c === "&") parser.state = S.TEXT_ENTITY;
846
+ else parser.textNode += c;
847
+ }
848
+ continue;
849
+ case S.SCRIPT:
850
+ if (c === "<") parser.state = S.SCRIPT_ENDING;
851
+ else parser.script += c;
852
+ continue;
853
+ case S.SCRIPT_ENDING:
854
+ if (c === "/") parser.state = S.CLOSE_TAG;
855
+ else {
856
+ parser.script += "<" + c;
857
+ parser.state = S.SCRIPT;
858
+ }
859
+ continue;
860
+ case S.OPEN_WAKA:
861
+ if (c === "!") {
862
+ parser.state = S.SGML_DECL;
863
+ parser.sgmlDecl = "";
864
+ } else if (isWhitespace(c)) {} else if (isMatch(nameStart, c)) {
865
+ parser.state = S.OPEN_TAG;
866
+ parser.tagName = c;
867
+ } else if (c === "/") {
868
+ parser.state = S.CLOSE_TAG;
869
+ parser.tagName = "";
870
+ } else if (c === "?") {
871
+ parser.state = S.PROC_INST;
872
+ parser.procInstName = parser.procInstBody = "";
873
+ } else {
874
+ strictFail(parser, "Unencoded <");
875
+ if (parser.startTagPosition + 1 < parser.position) {
876
+ var pad = parser.position - parser.startTagPosition;
877
+ c = new Array(pad).join(" ") + c;
878
+ }
879
+ parser.textNode += "<" + c;
880
+ parser.state = S.TEXT;
881
+ }
882
+ continue;
883
+ case S.SGML_DECL:
884
+ if (parser.sgmlDecl + c === "--") {
885
+ parser.state = S.COMMENT;
886
+ parser.comment = "";
887
+ parser.sgmlDecl = "";
888
+ continue;
889
+ }
890
+ if (parser.doctype && parser.doctype !== true && parser.sgmlDecl) {
891
+ parser.state = S.DOCTYPE_DTD;
892
+ parser.doctype += "<!" + parser.sgmlDecl + c;
893
+ parser.sgmlDecl = "";
894
+ } else if ((parser.sgmlDecl + c).toUpperCase() === CDATA) {
895
+ emitNode(parser, "onopencdata");
896
+ parser.state = S.CDATA;
897
+ parser.sgmlDecl = "";
898
+ parser.cdata = "";
899
+ } else if ((parser.sgmlDecl + c).toUpperCase() === DOCTYPE) {
900
+ parser.state = S.DOCTYPE;
901
+ if (parser.doctype || parser.sawRoot) strictFail(parser, "Inappropriately located doctype declaration");
902
+ parser.doctype = "";
903
+ parser.sgmlDecl = "";
904
+ } else if (c === ">") {
905
+ emitNode(parser, "onsgmldeclaration", parser.sgmlDecl);
906
+ parser.sgmlDecl = "";
907
+ parser.state = S.TEXT;
908
+ } else if (isQuote(c)) {
909
+ parser.state = S.SGML_DECL_QUOTED;
910
+ parser.sgmlDecl += c;
911
+ } else parser.sgmlDecl += c;
912
+ continue;
913
+ case S.SGML_DECL_QUOTED:
914
+ if (c === parser.q) {
915
+ parser.state = S.SGML_DECL;
916
+ parser.q = "";
917
+ }
918
+ parser.sgmlDecl += c;
919
+ continue;
920
+ case S.DOCTYPE:
921
+ if (c === ">") {
922
+ parser.state = S.TEXT;
923
+ emitNode(parser, "ondoctype", parser.doctype);
924
+ parser.doctype = true;
925
+ } else {
926
+ parser.doctype += c;
927
+ if (c === "[") parser.state = S.DOCTYPE_DTD;
928
+ else if (isQuote(c)) {
929
+ parser.state = S.DOCTYPE_QUOTED;
930
+ parser.q = c;
931
+ }
932
+ }
933
+ continue;
934
+ case S.DOCTYPE_QUOTED:
935
+ parser.doctype += c;
936
+ if (c === parser.q) {
937
+ parser.q = "";
938
+ parser.state = S.DOCTYPE;
939
+ }
940
+ continue;
941
+ case S.DOCTYPE_DTD:
942
+ if (c === "]") {
943
+ parser.doctype += c;
944
+ parser.state = S.DOCTYPE;
945
+ } else if (c === "<") {
946
+ parser.state = S.OPEN_WAKA;
947
+ parser.startTagPosition = parser.position;
948
+ } else if (isQuote(c)) {
949
+ parser.doctype += c;
950
+ parser.state = S.DOCTYPE_DTD_QUOTED;
951
+ parser.q = c;
952
+ } else parser.doctype += c;
953
+ continue;
954
+ case S.DOCTYPE_DTD_QUOTED:
955
+ parser.doctype += c;
956
+ if (c === parser.q) {
957
+ parser.state = S.DOCTYPE_DTD;
958
+ parser.q = "";
959
+ }
960
+ continue;
961
+ case S.COMMENT:
962
+ if (c === "-") parser.state = S.COMMENT_ENDING;
963
+ else parser.comment += c;
964
+ continue;
965
+ case S.COMMENT_ENDING:
966
+ if (c === "-") {
967
+ parser.state = S.COMMENT_ENDED;
968
+ parser.comment = textopts(parser.opt, parser.comment);
969
+ if (parser.comment) emitNode(parser, "oncomment", parser.comment);
970
+ parser.comment = "";
971
+ } else {
972
+ parser.comment += "-" + c;
973
+ parser.state = S.COMMENT;
974
+ }
975
+ continue;
976
+ case S.COMMENT_ENDED:
977
+ if (c !== ">") {
978
+ strictFail(parser, "Malformed comment");
979
+ parser.comment += "--" + c;
980
+ parser.state = S.COMMENT;
981
+ } else if (parser.doctype && parser.doctype !== true) parser.state = S.DOCTYPE_DTD;
982
+ else parser.state = S.TEXT;
983
+ continue;
984
+ case S.CDATA:
985
+ if (c === "]") parser.state = S.CDATA_ENDING;
986
+ else parser.cdata += c;
987
+ continue;
988
+ case S.CDATA_ENDING:
989
+ if (c === "]") parser.state = S.CDATA_ENDING_2;
990
+ else {
991
+ parser.cdata += "]" + c;
992
+ parser.state = S.CDATA;
993
+ }
994
+ continue;
995
+ case S.CDATA_ENDING_2:
996
+ if (c === ">") {
997
+ if (parser.cdata) emitNode(parser, "oncdata", parser.cdata);
998
+ emitNode(parser, "onclosecdata");
999
+ parser.cdata = "";
1000
+ parser.state = S.TEXT;
1001
+ } else if (c === "]") parser.cdata += "]";
1002
+ else {
1003
+ parser.cdata += "]]" + c;
1004
+ parser.state = S.CDATA;
1005
+ }
1006
+ continue;
1007
+ case S.PROC_INST:
1008
+ if (c === "?") parser.state = S.PROC_INST_ENDING;
1009
+ else if (isWhitespace(c)) parser.state = S.PROC_INST_BODY;
1010
+ else parser.procInstName += c;
1011
+ continue;
1012
+ case S.PROC_INST_BODY:
1013
+ if (!parser.procInstBody && isWhitespace(c)) continue;
1014
+ else if (c === "?") parser.state = S.PROC_INST_ENDING;
1015
+ else parser.procInstBody += c;
1016
+ continue;
1017
+ case S.PROC_INST_ENDING:
1018
+ if (c === ">") {
1019
+ emitNode(parser, "onprocessinginstruction", {
1020
+ name: parser.procInstName,
1021
+ body: parser.procInstBody
1022
+ });
1023
+ parser.procInstName = parser.procInstBody = "";
1024
+ parser.state = S.TEXT;
1025
+ } else {
1026
+ parser.procInstBody += "?" + c;
1027
+ parser.state = S.PROC_INST_BODY;
1028
+ }
1029
+ continue;
1030
+ case S.OPEN_TAG:
1031
+ if (isMatch(nameBody, c)) parser.tagName += c;
1032
+ else {
1033
+ newTag(parser);
1034
+ if (c === ">") openTag(parser);
1035
+ else if (c === "/") parser.state = S.OPEN_TAG_SLASH;
1036
+ else {
1037
+ if (!isWhitespace(c)) strictFail(parser, "Invalid character in tag name");
1038
+ parser.state = S.ATTRIB;
1039
+ }
1040
+ }
1041
+ continue;
1042
+ case S.OPEN_TAG_SLASH:
1043
+ if (c === ">") {
1044
+ openTag(parser, true);
1045
+ closeTag(parser);
1046
+ } else {
1047
+ strictFail(parser, "Forward-slash in opening tag not followed by >");
1048
+ parser.state = S.ATTRIB;
1049
+ }
1050
+ continue;
1051
+ case S.ATTRIB:
1052
+ if (isWhitespace(c)) continue;
1053
+ else if (c === ">") openTag(parser);
1054
+ else if (c === "/") parser.state = S.OPEN_TAG_SLASH;
1055
+ else if (isMatch(nameStart, c)) {
1056
+ parser.attribName = c;
1057
+ parser.attribValue = "";
1058
+ parser.state = S.ATTRIB_NAME;
1059
+ } else strictFail(parser, "Invalid attribute name");
1060
+ continue;
1061
+ case S.ATTRIB_NAME:
1062
+ if (c === "=") parser.state = S.ATTRIB_VALUE;
1063
+ else if (c === ">") {
1064
+ strictFail(parser, "Attribute without value");
1065
+ parser.attribValue = parser.attribName;
1066
+ attrib(parser);
1067
+ openTag(parser);
1068
+ } else if (isWhitespace(c)) parser.state = S.ATTRIB_NAME_SAW_WHITE;
1069
+ else if (isMatch(nameBody, c)) parser.attribName += c;
1070
+ else strictFail(parser, "Invalid attribute name");
1071
+ continue;
1072
+ case S.ATTRIB_NAME_SAW_WHITE:
1073
+ if (c === "=") parser.state = S.ATTRIB_VALUE;
1074
+ else if (isWhitespace(c)) continue;
1075
+ else {
1076
+ strictFail(parser, "Attribute without value");
1077
+ parser.tag.attributes[parser.attribName] = "";
1078
+ parser.attribValue = "";
1079
+ emitNode(parser, "onattribute", {
1080
+ name: parser.attribName,
1081
+ value: ""
1082
+ });
1083
+ parser.attribName = "";
1084
+ if (c === ">") openTag(parser);
1085
+ else if (isMatch(nameStart, c)) {
1086
+ parser.attribName = c;
1087
+ parser.state = S.ATTRIB_NAME;
1088
+ } else {
1089
+ strictFail(parser, "Invalid attribute name");
1090
+ parser.state = S.ATTRIB;
1091
+ }
1092
+ }
1093
+ continue;
1094
+ case S.ATTRIB_VALUE:
1095
+ if (isWhitespace(c)) continue;
1096
+ else if (isQuote(c)) {
1097
+ parser.q = c;
1098
+ parser.state = S.ATTRIB_VALUE_QUOTED;
1099
+ } else {
1100
+ if (!parser.opt.unquotedAttributeValues) error(parser, "Unquoted attribute value");
1101
+ parser.state = S.ATTRIB_VALUE_UNQUOTED;
1102
+ parser.attribValue = c;
1103
+ }
1104
+ continue;
1105
+ case S.ATTRIB_VALUE_QUOTED:
1106
+ if (c !== parser.q) {
1107
+ if (c === "&") parser.state = S.ATTRIB_VALUE_ENTITY_Q;
1108
+ else parser.attribValue += c;
1109
+ continue;
1110
+ }
1111
+ attrib(parser);
1112
+ parser.q = "";
1113
+ parser.state = S.ATTRIB_VALUE_CLOSED;
1114
+ continue;
1115
+ case S.ATTRIB_VALUE_CLOSED:
1116
+ if (isWhitespace(c)) parser.state = S.ATTRIB;
1117
+ else if (c === ">") openTag(parser);
1118
+ else if (c === "/") parser.state = S.OPEN_TAG_SLASH;
1119
+ else if (isMatch(nameStart, c)) {
1120
+ strictFail(parser, "No whitespace between attributes");
1121
+ parser.attribName = c;
1122
+ parser.attribValue = "";
1123
+ parser.state = S.ATTRIB_NAME;
1124
+ } else strictFail(parser, "Invalid attribute name");
1125
+ continue;
1126
+ case S.ATTRIB_VALUE_UNQUOTED:
1127
+ if (!isAttribEnd(c)) {
1128
+ if (c === "&") parser.state = S.ATTRIB_VALUE_ENTITY_U;
1129
+ else parser.attribValue += c;
1130
+ continue;
1131
+ }
1132
+ attrib(parser);
1133
+ if (c === ">") openTag(parser);
1134
+ else parser.state = S.ATTRIB;
1135
+ continue;
1136
+ case S.CLOSE_TAG:
1137
+ if (!parser.tagName) if (isWhitespace(c)) continue;
1138
+ else if (notMatch(nameStart, c)) if (parser.script) {
1139
+ parser.script += "</" + c;
1140
+ parser.state = S.SCRIPT;
1141
+ } else strictFail(parser, "Invalid tagname in closing tag.");
1142
+ else parser.tagName = c;
1143
+ else if (c === ">") closeTag(parser);
1144
+ else if (isMatch(nameBody, c)) parser.tagName += c;
1145
+ else if (parser.script) {
1146
+ parser.script += "</" + parser.tagName;
1147
+ parser.tagName = "";
1148
+ parser.state = S.SCRIPT;
1149
+ } else {
1150
+ if (!isWhitespace(c)) strictFail(parser, "Invalid tagname in closing tag");
1151
+ parser.state = S.CLOSE_TAG_SAW_WHITE;
1152
+ }
1153
+ continue;
1154
+ case S.CLOSE_TAG_SAW_WHITE:
1155
+ if (isWhitespace(c)) continue;
1156
+ if (c === ">") closeTag(parser);
1157
+ else strictFail(parser, "Invalid characters in closing tag");
1158
+ continue;
1159
+ case S.TEXT_ENTITY:
1160
+ case S.ATTRIB_VALUE_ENTITY_Q:
1161
+ case S.ATTRIB_VALUE_ENTITY_U:
1162
+ var returnState;
1163
+ var buffer;
1164
+ switch (parser.state) {
1165
+ case S.TEXT_ENTITY:
1166
+ returnState = S.TEXT;
1167
+ buffer = "textNode";
1168
+ break;
1169
+ case S.ATTRIB_VALUE_ENTITY_Q:
1170
+ returnState = S.ATTRIB_VALUE_QUOTED;
1171
+ buffer = "attribValue";
1172
+ break;
1173
+ case S.ATTRIB_VALUE_ENTITY_U:
1174
+ returnState = S.ATTRIB_VALUE_UNQUOTED;
1175
+ buffer = "attribValue";
1176
+ break;
1177
+ }
1178
+ if (c === ";") {
1179
+ var parsedEntity = parseEntity(parser);
1180
+ if (parser.opt.unparsedEntities && !Object.values(sax.XML_ENTITIES).includes(parsedEntity)) {
1181
+ parser.entity = "";
1182
+ parser.state = returnState;
1183
+ parser.write(parsedEntity);
1184
+ } else {
1185
+ parser[buffer] += parsedEntity;
1186
+ parser.entity = "";
1187
+ parser.state = returnState;
1188
+ }
1189
+ } else if (isMatch(parser.entity.length ? entityBody : entityStart, c)) parser.entity += c;
1190
+ else {
1191
+ strictFail(parser, "Invalid character in entity name");
1192
+ parser[buffer] += "&" + parser.entity + c;
1193
+ parser.entity = "";
1194
+ parser.state = returnState;
1195
+ }
1196
+ continue;
1197
+ default: throw new Error(parser, "Unknown state: " + parser.state);
1198
+ }
1199
+ }
1200
+ if (parser.position >= parser.bufferCheckPosition) checkBufferLength(parser);
1201
+ return parser;
1202
+ }
1203
+ /*! http://mths.be/fromcodepoint v0.1.0 by @mathias */
1204
+ /* istanbul ignore next */
1205
+ if (!String.fromCodePoint) (function() {
1206
+ var stringFromCharCode = String.fromCharCode;
1207
+ var floor = Math.floor;
1208
+ var fromCodePoint = function() {
1209
+ var MAX_SIZE = 16384;
1210
+ var codeUnits = [];
1211
+ var highSurrogate;
1212
+ var lowSurrogate;
1213
+ var index = -1;
1214
+ var length = arguments.length;
1215
+ if (!length) return "";
1216
+ var result = "";
1217
+ while (++index < length) {
1218
+ var codePoint = Number(arguments[index]);
1219
+ if (!isFinite(codePoint) || codePoint < 0 || codePoint > 1114111 || floor(codePoint) !== codePoint) throw RangeError("Invalid code point: " + codePoint);
1220
+ if (codePoint <= 65535) codeUnits.push(codePoint);
1221
+ else {
1222
+ codePoint -= 65536;
1223
+ highSurrogate = (codePoint >> 10) + 55296;
1224
+ lowSurrogate = codePoint % 1024 + 56320;
1225
+ codeUnits.push(highSurrogate, lowSurrogate);
1226
+ }
1227
+ if (index + 1 === length || codeUnits.length > MAX_SIZE) {
1228
+ result += stringFromCharCode.apply(null, codeUnits);
1229
+ codeUnits.length = 0;
1230
+ }
1231
+ }
1232
+ return result;
1233
+ };
1234
+ /* istanbul ignore next */
1235
+ if (Object.defineProperty) Object.defineProperty(String, "fromCodePoint", {
1236
+ value: fromCodePoint,
1237
+ configurable: true,
1238
+ writable: true
1239
+ });
1240
+ else String.fromCodePoint = fromCodePoint;
1241
+ })();
1242
+ })(typeof exports === "undefined" ? exports.sax = {} : exports);
1243
+ }));
1244
+ var require_array_helper = /* @__PURE__ */ __commonJSMin(((exports, module) => {
1245
+ module.exports = { isArray: function(value) {
1246
+ if (Array.isArray) return Array.isArray(value);
1247
+ return Object.prototype.toString.call(value) === "[object Array]";
1248
+ } };
1249
+ }));
1250
+ var require_options_helper = /* @__PURE__ */ __commonJSMin(((exports, module) => {
1251
+ var isArray = require_array_helper().isArray;
1252
+ module.exports = {
1253
+ copyOptions: function(options) {
1254
+ var key, copy = {};
1255
+ for (key in options) if (options.hasOwnProperty(key)) copy[key] = options[key];
1256
+ return copy;
1257
+ },
1258
+ ensureFlagExists: function(item, options) {
1259
+ if (!(item in options) || typeof options[item] !== "boolean") options[item] = false;
1260
+ },
1261
+ ensureSpacesExists: function(options) {
1262
+ if (!("spaces" in options) || typeof options.spaces !== "number" && typeof options.spaces !== "string") options.spaces = 0;
1263
+ },
1264
+ ensureAlwaysArrayExists: function(options) {
1265
+ if (!("alwaysArray" in options) || typeof options.alwaysArray !== "boolean" && !isArray(options.alwaysArray)) options.alwaysArray = false;
1266
+ },
1267
+ ensureKeyExists: function(key, options) {
1268
+ if (!(key + "Key" in options) || typeof options[key + "Key"] !== "string") options[key + "Key"] = options.compact ? "_" + key : key;
1269
+ },
1270
+ checkFnExists: function(key, options) {
1271
+ return key + "Fn" in options;
1272
+ }
1273
+ };
1274
+ }));
1275
+ var require_xml2js = /* @__PURE__ */ __commonJSMin(((exports, module) => {
1276
+ var sax = require_sax();
1277
+ var expat = {
1278
+ on: function() {},
1279
+ parse: function() {}
1280
+ };
1281
+ var helper = require_options_helper();
1282
+ var isArray = require_array_helper().isArray;
1283
+ var options;
1284
+ var pureJsParser = true;
1285
+ var currentElement;
1286
+ function validateOptions(userOptions) {
1287
+ options = helper.copyOptions(userOptions);
1288
+ helper.ensureFlagExists("ignoreDeclaration", options);
1289
+ helper.ensureFlagExists("ignoreInstruction", options);
1290
+ helper.ensureFlagExists("ignoreAttributes", options);
1291
+ helper.ensureFlagExists("ignoreText", options);
1292
+ helper.ensureFlagExists("ignoreComment", options);
1293
+ helper.ensureFlagExists("ignoreCdata", options);
1294
+ helper.ensureFlagExists("ignoreDoctype", options);
1295
+ helper.ensureFlagExists("compact", options);
1296
+ helper.ensureFlagExists("alwaysChildren", options);
1297
+ helper.ensureFlagExists("addParent", options);
1298
+ helper.ensureFlagExists("trim", options);
1299
+ helper.ensureFlagExists("nativeType", options);
1300
+ helper.ensureFlagExists("nativeTypeAttributes", options);
1301
+ helper.ensureFlagExists("sanitize", options);
1302
+ helper.ensureFlagExists("instructionHasAttributes", options);
1303
+ helper.ensureFlagExists("captureSpacesBetweenElements", options);
1304
+ helper.ensureAlwaysArrayExists(options);
1305
+ helper.ensureKeyExists("declaration", options);
1306
+ helper.ensureKeyExists("instruction", options);
1307
+ helper.ensureKeyExists("attributes", options);
1308
+ helper.ensureKeyExists("text", options);
1309
+ helper.ensureKeyExists("comment", options);
1310
+ helper.ensureKeyExists("cdata", options);
1311
+ helper.ensureKeyExists("doctype", options);
1312
+ helper.ensureKeyExists("type", options);
1313
+ helper.ensureKeyExists("name", options);
1314
+ helper.ensureKeyExists("elements", options);
1315
+ helper.ensureKeyExists("parent", options);
1316
+ helper.checkFnExists("doctype", options);
1317
+ helper.checkFnExists("instruction", options);
1318
+ helper.checkFnExists("cdata", options);
1319
+ helper.checkFnExists("comment", options);
1320
+ helper.checkFnExists("text", options);
1321
+ helper.checkFnExists("instructionName", options);
1322
+ helper.checkFnExists("elementName", options);
1323
+ helper.checkFnExists("attributeName", options);
1324
+ helper.checkFnExists("attributeValue", options);
1325
+ helper.checkFnExists("attributes", options);
1326
+ return options;
1327
+ }
1328
+ function nativeType(value) {
1329
+ var nValue = Number(value);
1330
+ if (!isNaN(nValue)) return nValue;
1331
+ var bValue = value.toLowerCase();
1332
+ if (bValue === "true") return true;
1333
+ else if (bValue === "false") return false;
1334
+ return value;
1335
+ }
1336
+ function addField(type, value) {
1337
+ var key;
1338
+ if (options.compact) {
1339
+ if (!currentElement[options[type + "Key"]] && (isArray(options.alwaysArray) ? options.alwaysArray.indexOf(options[type + "Key"]) !== -1 : options.alwaysArray)) currentElement[options[type + "Key"]] = [];
1340
+ if (currentElement[options[type + "Key"]] && !isArray(currentElement[options[type + "Key"]])) currentElement[options[type + "Key"]] = [currentElement[options[type + "Key"]]];
1341
+ if (type + "Fn" in options && typeof value === "string") value = options[type + "Fn"](value, currentElement);
1342
+ if (type === "instruction" && ("instructionFn" in options || "instructionNameFn" in options)) {
1343
+ for (key in value) if (value.hasOwnProperty(key)) if ("instructionFn" in options) value[key] = options.instructionFn(value[key], key, currentElement);
1344
+ else {
1345
+ var temp = value[key];
1346
+ delete value[key];
1347
+ value[options.instructionNameFn(key, temp, currentElement)] = temp;
1348
+ }
1349
+ }
1350
+ if (isArray(currentElement[options[type + "Key"]])) currentElement[options[type + "Key"]].push(value);
1351
+ else currentElement[options[type + "Key"]] = value;
1352
+ } else {
1353
+ if (!currentElement[options.elementsKey]) currentElement[options.elementsKey] = [];
1354
+ var element = {};
1355
+ element[options.typeKey] = type;
1356
+ if (type === "instruction") {
1357
+ for (key in value) if (value.hasOwnProperty(key)) break;
1358
+ element[options.nameKey] = "instructionNameFn" in options ? options.instructionNameFn(key, value, currentElement) : key;
1359
+ if (options.instructionHasAttributes) {
1360
+ element[options.attributesKey] = value[key][options.attributesKey];
1361
+ if ("instructionFn" in options) element[options.attributesKey] = options.instructionFn(element[options.attributesKey], key, currentElement);
1362
+ } else {
1363
+ if ("instructionFn" in options) value[key] = options.instructionFn(value[key], key, currentElement);
1364
+ element[options.instructionKey] = value[key];
1365
+ }
1366
+ } else {
1367
+ if (type + "Fn" in options) value = options[type + "Fn"](value, currentElement);
1368
+ element[options[type + "Key"]] = value;
1369
+ }
1370
+ if (options.addParent) element[options.parentKey] = currentElement;
1371
+ currentElement[options.elementsKey].push(element);
1372
+ }
1373
+ }
1374
+ function manipulateAttributes(attributes) {
1375
+ if ("attributesFn" in options && attributes) attributes = options.attributesFn(attributes, currentElement);
1376
+ if ((options.trim || "attributeValueFn" in options || "attributeNameFn" in options || options.nativeTypeAttributes) && attributes) {
1377
+ var key;
1378
+ for (key in attributes) if (attributes.hasOwnProperty(key)) {
1379
+ if (options.trim) attributes[key] = attributes[key].trim();
1380
+ if (options.nativeTypeAttributes) attributes[key] = nativeType(attributes[key]);
1381
+ if ("attributeValueFn" in options) attributes[key] = options.attributeValueFn(attributes[key], key, currentElement);
1382
+ if ("attributeNameFn" in options) {
1383
+ var temp = attributes[key];
1384
+ delete attributes[key];
1385
+ attributes[options.attributeNameFn(key, attributes[key], currentElement)] = temp;
1386
+ }
1387
+ }
1388
+ }
1389
+ return attributes;
1390
+ }
1391
+ function onInstruction(instruction) {
1392
+ var attributes = {};
1393
+ if (instruction.body && (instruction.name.toLowerCase() === "xml" || options.instructionHasAttributes)) {
1394
+ var attrsRegExp = /([\w:-]+)\s*=\s*(?:"([^"]*)"|'([^']*)'|(\w+))\s*/g;
1395
+ var match;
1396
+ while ((match = attrsRegExp.exec(instruction.body)) !== null) attributes[match[1]] = match[2] || match[3] || match[4];
1397
+ attributes = manipulateAttributes(attributes);
1398
+ }
1399
+ if (instruction.name.toLowerCase() === "xml") {
1400
+ if (options.ignoreDeclaration) return;
1401
+ currentElement[options.declarationKey] = {};
1402
+ if (Object.keys(attributes).length) currentElement[options.declarationKey][options.attributesKey] = attributes;
1403
+ if (options.addParent) currentElement[options.declarationKey][options.parentKey] = currentElement;
1404
+ } else {
1405
+ if (options.ignoreInstruction) return;
1406
+ if (options.trim) instruction.body = instruction.body.trim();
1407
+ var value = {};
1408
+ if (options.instructionHasAttributes && Object.keys(attributes).length) {
1409
+ value[instruction.name] = {};
1410
+ value[instruction.name][options.attributesKey] = attributes;
1411
+ } else value[instruction.name] = instruction.body;
1412
+ addField("instruction", value);
1413
+ }
1414
+ }
1415
+ function onStartElement(name, attributes) {
1416
+ var element;
1417
+ if (typeof name === "object") {
1418
+ attributes = name.attributes;
1419
+ name = name.name;
1420
+ }
1421
+ attributes = manipulateAttributes(attributes);
1422
+ if ("elementNameFn" in options) name = options.elementNameFn(name, currentElement);
1423
+ if (options.compact) {
1424
+ element = {};
1425
+ if (!options.ignoreAttributes && attributes && Object.keys(attributes).length) {
1426
+ element[options.attributesKey] = {};
1427
+ var key;
1428
+ for (key in attributes) if (attributes.hasOwnProperty(key)) element[options.attributesKey][key] = attributes[key];
1429
+ }
1430
+ if (!(name in currentElement) && (isArray(options.alwaysArray) ? options.alwaysArray.indexOf(name) !== -1 : options.alwaysArray)) currentElement[name] = [];
1431
+ if (currentElement[name] && !isArray(currentElement[name])) currentElement[name] = [currentElement[name]];
1432
+ if (isArray(currentElement[name])) currentElement[name].push(element);
1433
+ else currentElement[name] = element;
1434
+ } else {
1435
+ if (!currentElement[options.elementsKey]) currentElement[options.elementsKey] = [];
1436
+ element = {};
1437
+ element[options.typeKey] = "element";
1438
+ element[options.nameKey] = name;
1439
+ if (!options.ignoreAttributes && attributes && Object.keys(attributes).length) element[options.attributesKey] = attributes;
1440
+ if (options.alwaysChildren) element[options.elementsKey] = [];
1441
+ currentElement[options.elementsKey].push(element);
1442
+ }
1443
+ element[options.parentKey] = currentElement;
1444
+ currentElement = element;
1445
+ }
1446
+ function onText(text) {
1447
+ if (options.ignoreText) return;
1448
+ if (!text.trim() && !options.captureSpacesBetweenElements) return;
1449
+ if (options.trim) text = text.trim();
1450
+ if (options.nativeType) text = nativeType(text);
1451
+ if (options.sanitize) text = text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
1452
+ addField("text", text);
1453
+ }
1454
+ function onComment(comment) {
1455
+ if (options.ignoreComment) return;
1456
+ if (options.trim) comment = comment.trim();
1457
+ addField("comment", comment);
1458
+ }
1459
+ function onEndElement(name) {
1460
+ var parentElement = currentElement[options.parentKey];
1461
+ if (!options.addParent) delete currentElement[options.parentKey];
1462
+ currentElement = parentElement;
1463
+ }
1464
+ function onCdata(cdata) {
1465
+ if (options.ignoreCdata) return;
1466
+ if (options.trim) cdata = cdata.trim();
1467
+ addField("cdata", cdata);
1468
+ }
1469
+ function onDoctype(doctype) {
1470
+ if (options.ignoreDoctype) return;
1471
+ doctype = doctype.replace(/^ /, "");
1472
+ if (options.trim) doctype = doctype.trim();
1473
+ addField("doctype", doctype);
1474
+ }
1475
+ function onError(error) {
1476
+ error.note = error;
1477
+ }
1478
+ module.exports = function(xml, userOptions) {
1479
+ var parser = pureJsParser ? sax.parser(true, {}) : parser = new expat.Parser("UTF-8");
1480
+ var result = {};
1481
+ currentElement = result;
1482
+ options = validateOptions(userOptions);
1483
+ if (pureJsParser) {
1484
+ parser.opt = { strictEntities: true };
1485
+ parser.onopentag = onStartElement;
1486
+ parser.ontext = onText;
1487
+ parser.oncomment = onComment;
1488
+ parser.onclosetag = onEndElement;
1489
+ parser.onerror = onError;
1490
+ parser.oncdata = onCdata;
1491
+ parser.ondoctype = onDoctype;
1492
+ parser.onprocessinginstruction = onInstruction;
1493
+ } else {
1494
+ parser.on("startElement", onStartElement);
1495
+ parser.on("text", onText);
1496
+ parser.on("comment", onComment);
1497
+ parser.on("endElement", onEndElement);
1498
+ parser.on("error", onError);
1499
+ }
1500
+ if (pureJsParser) parser.write(xml).close();
1501
+ else if (!parser.parse(xml)) throw new Error("XML parsing error: " + parser.getError());
1502
+ if (result[options.elementsKey]) {
1503
+ var temp = result[options.elementsKey];
1504
+ delete result[options.elementsKey];
1505
+ result[options.elementsKey] = temp;
1506
+ delete result.text;
1507
+ }
1508
+ return result;
1509
+ };
1510
+ }));
1511
+ var require_xml2json = /* @__PURE__ */ __commonJSMin(((exports, module) => {
1512
+ var helper = require_options_helper();
1513
+ var xml2js = require_xml2js();
1514
+ function validateOptions(userOptions) {
1515
+ var options = helper.copyOptions(userOptions);
1516
+ helper.ensureSpacesExists(options);
1517
+ return options;
1518
+ }
1519
+ module.exports = function(xml, userOptions) {
1520
+ var options = validateOptions(userOptions), js = xml2js(xml, options), json, parentKey = "compact" in options && options.compact ? "_parent" : "parent";
1521
+ if ("addParent" in options && options.addParent) json = JSON.stringify(js, function(k, v) {
1522
+ return k === parentKey ? "_" : v;
1523
+ }, options.spaces);
1524
+ else json = JSON.stringify(js, null, options.spaces);
1525
+ return json.replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029");
1526
+ };
1527
+ }));
1528
+ var require_js2xml = /* @__PURE__ */ __commonJSMin(((exports, module) => {
1529
+ var helper = require_options_helper();
1530
+ var isArray = require_array_helper().isArray;
1531
+ var currentElement, currentElementName;
1532
+ function validateOptions(userOptions) {
1533
+ var options = helper.copyOptions(userOptions);
1534
+ helper.ensureFlagExists("ignoreDeclaration", options);
1535
+ helper.ensureFlagExists("ignoreInstruction", options);
1536
+ helper.ensureFlagExists("ignoreAttributes", options);
1537
+ helper.ensureFlagExists("ignoreText", options);
1538
+ helper.ensureFlagExists("ignoreComment", options);
1539
+ helper.ensureFlagExists("ignoreCdata", options);
1540
+ helper.ensureFlagExists("ignoreDoctype", options);
1541
+ helper.ensureFlagExists("compact", options);
1542
+ helper.ensureFlagExists("indentText", options);
1543
+ helper.ensureFlagExists("indentCdata", options);
1544
+ helper.ensureFlagExists("indentAttributes", options);
1545
+ helper.ensureFlagExists("indentInstruction", options);
1546
+ helper.ensureFlagExists("fullTagEmptyElement", options);
1547
+ helper.ensureFlagExists("noQuotesForNativeAttributes", options);
1548
+ helper.ensureSpacesExists(options);
1549
+ if (typeof options.spaces === "number") options.spaces = Array(options.spaces + 1).join(" ");
1550
+ helper.ensureKeyExists("declaration", options);
1551
+ helper.ensureKeyExists("instruction", options);
1552
+ helper.ensureKeyExists("attributes", options);
1553
+ helper.ensureKeyExists("text", options);
1554
+ helper.ensureKeyExists("comment", options);
1555
+ helper.ensureKeyExists("cdata", options);
1556
+ helper.ensureKeyExists("doctype", options);
1557
+ helper.ensureKeyExists("type", options);
1558
+ helper.ensureKeyExists("name", options);
1559
+ helper.ensureKeyExists("elements", options);
1560
+ helper.checkFnExists("doctype", options);
1561
+ helper.checkFnExists("instruction", options);
1562
+ helper.checkFnExists("cdata", options);
1563
+ helper.checkFnExists("comment", options);
1564
+ helper.checkFnExists("text", options);
1565
+ helper.checkFnExists("instructionName", options);
1566
+ helper.checkFnExists("elementName", options);
1567
+ helper.checkFnExists("attributeName", options);
1568
+ helper.checkFnExists("attributeValue", options);
1569
+ helper.checkFnExists("attributes", options);
1570
+ helper.checkFnExists("fullTagEmptyElement", options);
1571
+ return options;
1572
+ }
1573
+ function writeIndentation(options, depth, firstLine) {
1574
+ return (!firstLine && options.spaces ? "\n" : "") + Array(depth + 1).join(options.spaces);
1575
+ }
1576
+ function writeAttributes(attributes, options, depth) {
1577
+ if (options.ignoreAttributes) return "";
1578
+ if ("attributesFn" in options) attributes = options.attributesFn(attributes, currentElementName, currentElement);
1579
+ var key, attr, attrName, quote, result = [];
1580
+ for (key in attributes) if (attributes.hasOwnProperty(key) && attributes[key] !== null && attributes[key] !== void 0) {
1581
+ quote = options.noQuotesForNativeAttributes && typeof attributes[key] !== "string" ? "" : "\"";
1582
+ attr = "" + attributes[key];
1583
+ attr = attr.replace(/"/g, "&quot;");
1584
+ attrName = "attributeNameFn" in options ? options.attributeNameFn(key, attr, currentElementName, currentElement) : key;
1585
+ result.push(options.spaces && options.indentAttributes ? writeIndentation(options, depth + 1, false) : " ");
1586
+ result.push(attrName + "=" + quote + ("attributeValueFn" in options ? options.attributeValueFn(attr, key, currentElementName, currentElement) : attr) + quote);
1587
+ }
1588
+ if (attributes && Object.keys(attributes).length && options.spaces && options.indentAttributes) result.push(writeIndentation(options, depth, false));
1589
+ return result.join("");
1590
+ }
1591
+ function writeDeclaration(declaration, options, depth) {
1592
+ currentElement = declaration;
1593
+ currentElementName = "xml";
1594
+ return options.ignoreDeclaration ? "" : "<?xml" + writeAttributes(declaration[options.attributesKey], options, depth) + "?>";
1595
+ }
1596
+ function writeInstruction(instruction, options, depth) {
1597
+ if (options.ignoreInstruction) return "";
1598
+ var key;
1599
+ for (key in instruction) if (instruction.hasOwnProperty(key)) break;
1600
+ var instructionName = "instructionNameFn" in options ? options.instructionNameFn(key, instruction[key], currentElementName, currentElement) : key;
1601
+ if (typeof instruction[key] === "object") {
1602
+ currentElement = instruction;
1603
+ currentElementName = instructionName;
1604
+ return "<?" + instructionName + writeAttributes(instruction[key][options.attributesKey], options, depth) + "?>";
1605
+ } else {
1606
+ var instructionValue = instruction[key] ? instruction[key] : "";
1607
+ if ("instructionFn" in options) instructionValue = options.instructionFn(instructionValue, key, currentElementName, currentElement);
1608
+ return "<?" + instructionName + (instructionValue ? " " + instructionValue : "") + "?>";
1609
+ }
1610
+ }
1611
+ function writeComment(comment, options) {
1612
+ return options.ignoreComment ? "" : "<!--" + ("commentFn" in options ? options.commentFn(comment, currentElementName, currentElement) : comment) + "-->";
1613
+ }
1614
+ function writeCdata(cdata, options) {
1615
+ return options.ignoreCdata ? "" : "<![CDATA[" + ("cdataFn" in options ? options.cdataFn(cdata, currentElementName, currentElement) : cdata.replace("]]>", "]]]]><![CDATA[>")) + "]]>";
1616
+ }
1617
+ function writeDoctype(doctype, options) {
1618
+ return options.ignoreDoctype ? "" : "<!DOCTYPE " + ("doctypeFn" in options ? options.doctypeFn(doctype, currentElementName, currentElement) : doctype) + ">";
1619
+ }
1620
+ function writeText(text, options) {
1621
+ if (options.ignoreText) return "";
1622
+ text = "" + text;
1623
+ text = text.replace(/&amp;/g, "&");
1624
+ text = text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
1625
+ return "textFn" in options ? options.textFn(text, currentElementName, currentElement) : text;
1626
+ }
1627
+ function hasContent(element, options) {
1628
+ var i;
1629
+ if (element.elements && element.elements.length) for (i = 0; i < element.elements.length; ++i) switch (element.elements[i][options.typeKey]) {
1630
+ case "text":
1631
+ if (options.indentText) return true;
1632
+ break;
1633
+ case "cdata":
1634
+ if (options.indentCdata) return true;
1635
+ break;
1636
+ case "instruction":
1637
+ if (options.indentInstruction) return true;
1638
+ break;
1639
+ case "doctype":
1640
+ case "comment":
1641
+ case "element": return true;
1642
+ default: return true;
1643
+ }
1644
+ return false;
1645
+ }
1646
+ function writeElement(element, options, depth) {
1647
+ currentElement = element;
1648
+ currentElementName = element.name;
1649
+ var xml = [], elementName = "elementNameFn" in options ? options.elementNameFn(element.name, element) : element.name;
1650
+ xml.push("<" + elementName);
1651
+ if (element[options.attributesKey]) xml.push(writeAttributes(element[options.attributesKey], options, depth));
1652
+ var withClosingTag = element[options.elementsKey] && element[options.elementsKey].length || element[options.attributesKey] && element[options.attributesKey]["xml:space"] === "preserve";
1653
+ if (!withClosingTag) if ("fullTagEmptyElementFn" in options) withClosingTag = options.fullTagEmptyElementFn(element.name, element);
1654
+ else withClosingTag = options.fullTagEmptyElement;
1655
+ if (withClosingTag) {
1656
+ xml.push(">");
1657
+ if (element[options.elementsKey] && element[options.elementsKey].length) {
1658
+ xml.push(writeElements(element[options.elementsKey], options, depth + 1));
1659
+ currentElement = element;
1660
+ currentElementName = element.name;
1661
+ }
1662
+ xml.push(options.spaces && hasContent(element, options) ? "\n" + Array(depth + 1).join(options.spaces) : "");
1663
+ xml.push("</" + elementName + ">");
1664
+ } else xml.push("/>");
1665
+ return xml.join("");
1666
+ }
1667
+ function writeElements(elements, options, depth, firstLine) {
1668
+ return elements.reduce(function(xml, element) {
1669
+ var indent = writeIndentation(options, depth, firstLine && !xml);
1670
+ switch (element.type) {
1671
+ case "element": return xml + indent + writeElement(element, options, depth);
1672
+ case "comment": return xml + indent + writeComment(element[options.commentKey], options);
1673
+ case "doctype": return xml + indent + writeDoctype(element[options.doctypeKey], options);
1674
+ case "cdata": return xml + (options.indentCdata ? indent : "") + writeCdata(element[options.cdataKey], options);
1675
+ case "text": return xml + (options.indentText ? indent : "") + writeText(element[options.textKey], options);
1676
+ case "instruction":
1677
+ var instruction = {};
1678
+ instruction[element[options.nameKey]] = element[options.attributesKey] ? element : element[options.instructionKey];
1679
+ return xml + (options.indentInstruction ? indent : "") + writeInstruction(instruction, options, depth);
1680
+ }
1681
+ }, "");
1682
+ }
1683
+ function hasContentCompact(element, options, anyContent) {
1684
+ var key;
1685
+ for (key in element) if (element.hasOwnProperty(key)) switch (key) {
1686
+ case options.parentKey:
1687
+ case options.attributesKey: break;
1688
+ case options.textKey:
1689
+ if (options.indentText || anyContent) return true;
1690
+ break;
1691
+ case options.cdataKey:
1692
+ if (options.indentCdata || anyContent) return true;
1693
+ break;
1694
+ case options.instructionKey:
1695
+ if (options.indentInstruction || anyContent) return true;
1696
+ break;
1697
+ case options.doctypeKey:
1698
+ case options.commentKey: return true;
1699
+ default: return true;
1700
+ }
1701
+ return false;
1702
+ }
1703
+ function writeElementCompact(element, name, options, depth, indent) {
1704
+ currentElement = element;
1705
+ currentElementName = name;
1706
+ var elementName = "elementNameFn" in options ? options.elementNameFn(name, element) : name;
1707
+ if (typeof element === "undefined" || element === null || element === "") return "fullTagEmptyElementFn" in options && options.fullTagEmptyElementFn(name, element) || options.fullTagEmptyElement ? "<" + elementName + "></" + elementName + ">" : "<" + elementName + "/>";
1708
+ var xml = [];
1709
+ if (name) {
1710
+ xml.push("<" + elementName);
1711
+ if (typeof element !== "object") {
1712
+ xml.push(">" + writeText(element, options) + "</" + elementName + ">");
1713
+ return xml.join("");
1714
+ }
1715
+ if (element[options.attributesKey]) xml.push(writeAttributes(element[options.attributesKey], options, depth));
1716
+ var withClosingTag = hasContentCompact(element, options, true) || element[options.attributesKey] && element[options.attributesKey]["xml:space"] === "preserve";
1717
+ if (!withClosingTag) if ("fullTagEmptyElementFn" in options) withClosingTag = options.fullTagEmptyElementFn(name, element);
1718
+ else withClosingTag = options.fullTagEmptyElement;
1719
+ if (withClosingTag) xml.push(">");
1720
+ else {
1721
+ xml.push("/>");
1722
+ return xml.join("");
1723
+ }
1724
+ }
1725
+ xml.push(writeElementsCompact(element, options, depth + 1, false));
1726
+ currentElement = element;
1727
+ currentElementName = name;
1728
+ if (name) xml.push((indent ? writeIndentation(options, depth, false) : "") + "</" + elementName + ">");
1729
+ return xml.join("");
1730
+ }
1731
+ function writeElementsCompact(element, options, depth, firstLine) {
1732
+ var i, key, nodes, xml = [];
1733
+ for (key in element) if (element.hasOwnProperty(key)) {
1734
+ nodes = isArray(element[key]) ? element[key] : [element[key]];
1735
+ for (i = 0; i < nodes.length; ++i) {
1736
+ switch (key) {
1737
+ case options.declarationKey:
1738
+ xml.push(writeDeclaration(nodes[i], options, depth));
1739
+ break;
1740
+ case options.instructionKey:
1741
+ xml.push((options.indentInstruction ? writeIndentation(options, depth, firstLine) : "") + writeInstruction(nodes[i], options, depth));
1742
+ break;
1743
+ case options.attributesKey:
1744
+ case options.parentKey: break;
1745
+ case options.textKey:
1746
+ xml.push((options.indentText ? writeIndentation(options, depth, firstLine) : "") + writeText(nodes[i], options));
1747
+ break;
1748
+ case options.cdataKey:
1749
+ xml.push((options.indentCdata ? writeIndentation(options, depth, firstLine) : "") + writeCdata(nodes[i], options));
1750
+ break;
1751
+ case options.doctypeKey:
1752
+ xml.push(writeIndentation(options, depth, firstLine) + writeDoctype(nodes[i], options));
1753
+ break;
1754
+ case options.commentKey:
1755
+ xml.push(writeIndentation(options, depth, firstLine) + writeComment(nodes[i], options));
1756
+ break;
1757
+ default: xml.push(writeIndentation(options, depth, firstLine) + writeElementCompact(nodes[i], key, options, depth, hasContentCompact(nodes[i], options)));
1758
+ }
1759
+ firstLine = firstLine && !xml.length;
1760
+ }
1761
+ }
1762
+ return xml.join("");
1763
+ }
1764
+ module.exports = function(js, options) {
1765
+ options = validateOptions(options);
1766
+ var xml = [];
1767
+ currentElement = js;
1768
+ currentElementName = "_root_";
1769
+ if (options.compact) xml.push(writeElementsCompact(js, options, 0, true));
1770
+ else {
1771
+ if (js[options.declarationKey]) xml.push(writeDeclaration(js[options.declarationKey], options, 0));
1772
+ if (js[options.elementsKey] && js[options.elementsKey].length) xml.push(writeElements(js[options.elementsKey], options, 0, !xml.length));
1773
+ }
1774
+ return xml.join("");
1775
+ };
1776
+ }));
1777
+ var require_json2xml = /* @__PURE__ */ __commonJSMin(((exports, module) => {
1778
+ var js2xml = require_js2xml();
1779
+ module.exports = function(json, options) {
1780
+ if (json instanceof Buffer) json = json.toString();
1781
+ var js = null;
1782
+ if (typeof json === "string") try {
1783
+ js = JSON.parse(json);
1784
+ } catch (e) {
1785
+ throw new Error("The JSON structure is invalid");
1786
+ }
1787
+ else js = json;
1788
+ return js2xml(js, options);
1789
+ };
1790
+ }));
1791
+ var require_lib = /* @__PURE__ */ __commonJSMin(((exports, module) => {
1792
+ module.exports = {
1793
+ xml2js: require_xml2js(),
1794
+ xml2json: require_xml2json(),
1795
+ js2xml: require_js2xml(),
1796
+ json2xml: require_json2xml()
1797
+ };
1798
+ }));
1799
+ var require_base64_js = /* @__PURE__ */ __commonJSMin(((exports) => {
1800
+ exports.byteLength = byteLength;
1801
+ exports.toByteArray = toByteArray;
1802
+ exports.fromByteArray = fromByteArray;
1803
+ var lookup = [];
1804
+ var revLookup = [];
1805
+ var Arr = typeof Uint8Array !== "undefined" ? Uint8Array : Array;
1806
+ var code = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1807
+ for (var i = 0, len = code.length; i < len; ++i) {
1808
+ lookup[i] = code[i];
1809
+ revLookup[code.charCodeAt(i)] = i;
1810
+ }
1811
+ revLookup["-".charCodeAt(0)] = 62;
1812
+ revLookup["_".charCodeAt(0)] = 63;
1813
+ function getLens(b64) {
1814
+ var len = b64.length;
1815
+ if (len % 4 > 0) throw new Error("Invalid string. Length must be a multiple of 4");
1816
+ var validLen = b64.indexOf("=");
1817
+ if (validLen === -1) validLen = len;
1818
+ var placeHoldersLen = validLen === len ? 0 : 4 - validLen % 4;
1819
+ return [validLen, placeHoldersLen];
1820
+ }
1821
+ function byteLength(b64) {
1822
+ var lens = getLens(b64);
1823
+ var validLen = lens[0];
1824
+ var placeHoldersLen = lens[1];
1825
+ return (validLen + placeHoldersLen) * 3 / 4 - placeHoldersLen;
1826
+ }
1827
+ function _byteLength(b64, validLen, placeHoldersLen) {
1828
+ return (validLen + placeHoldersLen) * 3 / 4 - placeHoldersLen;
1829
+ }
1830
+ function toByteArray(b64) {
1831
+ var tmp;
1832
+ var lens = getLens(b64);
1833
+ var validLen = lens[0];
1834
+ var placeHoldersLen = lens[1];
1835
+ var arr = new Arr(_byteLength(b64, validLen, placeHoldersLen));
1836
+ var curByte = 0;
1837
+ var len = placeHoldersLen > 0 ? validLen - 4 : validLen;
1838
+ var i;
1839
+ for (i = 0; i < len; i += 4) {
1840
+ tmp = revLookup[b64.charCodeAt(i)] << 18 | revLookup[b64.charCodeAt(i + 1)] << 12 | revLookup[b64.charCodeAt(i + 2)] << 6 | revLookup[b64.charCodeAt(i + 3)];
1841
+ arr[curByte++] = tmp >> 16 & 255;
1842
+ arr[curByte++] = tmp >> 8 & 255;
1843
+ arr[curByte++] = tmp & 255;
1844
+ }
1845
+ if (placeHoldersLen === 2) {
1846
+ tmp = revLookup[b64.charCodeAt(i)] << 2 | revLookup[b64.charCodeAt(i + 1)] >> 4;
1847
+ arr[curByte++] = tmp & 255;
1848
+ }
1849
+ if (placeHoldersLen === 1) {
1850
+ tmp = revLookup[b64.charCodeAt(i)] << 10 | revLookup[b64.charCodeAt(i + 1)] << 4 | revLookup[b64.charCodeAt(i + 2)] >> 2;
1851
+ arr[curByte++] = tmp >> 8 & 255;
1852
+ arr[curByte++] = tmp & 255;
1853
+ }
1854
+ return arr;
1855
+ }
1856
+ function tripletToBase64(num) {
1857
+ return lookup[num >> 18 & 63] + lookup[num >> 12 & 63] + lookup[num >> 6 & 63] + lookup[num & 63];
1858
+ }
1859
+ function encodeChunk(uint8, start, end) {
1860
+ var tmp;
1861
+ var output = [];
1862
+ for (var i = start; i < end; i += 3) {
1863
+ tmp = (uint8[i] << 16 & 16711680) + (uint8[i + 1] << 8 & 65280) + (uint8[i + 2] & 255);
1864
+ output.push(tripletToBase64(tmp));
1865
+ }
1866
+ return output.join("");
1867
+ }
1868
+ function fromByteArray(uint8) {
1869
+ var tmp;
1870
+ var len = uint8.length;
1871
+ var extraBytes = len % 3;
1872
+ var parts = [];
1873
+ var maxChunkLength = 16383;
1874
+ for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) parts.push(encodeChunk(uint8, i, i + maxChunkLength > len2 ? len2 : i + maxChunkLength));
1875
+ if (extraBytes === 1) {
1876
+ tmp = uint8[len - 1];
1877
+ parts.push(lookup[tmp >> 2] + lookup[tmp << 4 & 63] + "==");
1878
+ } else if (extraBytes === 2) {
1879
+ tmp = (uint8[len - 2] << 8) + uint8[len - 1];
1880
+ parts.push(lookup[tmp >> 10] + lookup[tmp >> 4 & 63] + lookup[tmp << 2 & 63] + "=");
1881
+ }
1882
+ return parts.join("");
1883
+ }
1884
+ }));
1885
+ var require_ieee754 = /* @__PURE__ */ __commonJSMin(((exports) => {
1886
+ /*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */
1887
+ exports.read = function(buffer, offset, isLE, mLen, nBytes) {
1888
+ var e, m;
1889
+ var eLen = nBytes * 8 - mLen - 1;
1890
+ var eMax = (1 << eLen) - 1;
1891
+ var eBias = eMax >> 1;
1892
+ var nBits = -7;
1893
+ var i = isLE ? nBytes - 1 : 0;
1894
+ var d = isLE ? -1 : 1;
1895
+ var s = buffer[offset + i];
1896
+ i += d;
1897
+ e = s & (1 << -nBits) - 1;
1898
+ s >>= -nBits;
1899
+ nBits += eLen;
1900
+ for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8);
1901
+ m = e & (1 << -nBits) - 1;
1902
+ e >>= -nBits;
1903
+ nBits += mLen;
1904
+ for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8);
1905
+ if (e === 0) e = 1 - eBias;
1906
+ else if (e === eMax) return m ? NaN : (s ? -1 : 1) * Infinity;
1907
+ else {
1908
+ m = m + Math.pow(2, mLen);
1909
+ e = e - eBias;
1910
+ }
1911
+ return (s ? -1 : 1) * m * Math.pow(2, e - mLen);
1912
+ };
1913
+ exports.write = function(buffer, value, offset, isLE, mLen, nBytes) {
1914
+ var e, m, c;
1915
+ var eLen = nBytes * 8 - mLen - 1;
1916
+ var eMax = (1 << eLen) - 1;
1917
+ var eBias = eMax >> 1;
1918
+ var rt = mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0;
1919
+ var i = isLE ? 0 : nBytes - 1;
1920
+ var d = isLE ? 1 : -1;
1921
+ var s = value < 0 || value === 0 && 1 / value < 0 ? 1 : 0;
1922
+ value = Math.abs(value);
1923
+ if (isNaN(value) || value === Infinity) {
1924
+ m = isNaN(value) ? 1 : 0;
1925
+ e = eMax;
1926
+ } else {
1927
+ e = Math.floor(Math.log(value) / Math.LN2);
1928
+ if (value * (c = Math.pow(2, -e)) < 1) {
1929
+ e--;
1930
+ c *= 2;
1931
+ }
1932
+ if (e + eBias >= 1) value += rt / c;
1933
+ else value += rt * Math.pow(2, 1 - eBias);
1934
+ if (value * c >= 2) {
1935
+ e++;
1936
+ c /= 2;
1937
+ }
1938
+ if (e + eBias >= eMax) {
1939
+ m = 0;
1940
+ e = eMax;
1941
+ } else if (e + eBias >= 1) {
1942
+ m = (value * c - 1) * Math.pow(2, mLen);
1943
+ e = e + eBias;
1944
+ } else {
1945
+ m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
1946
+ e = 0;
1947
+ }
1948
+ }
1949
+ for (; mLen >= 8; buffer[offset + i] = m & 255, i += d, m /= 256, mLen -= 8);
1950
+ e = e << mLen | m;
1951
+ eLen += mLen;
1952
+ for (; eLen > 0; buffer[offset + i] = e & 255, i += d, e /= 256, eLen -= 8);
1953
+ buffer[offset + i - d] |= s * 128;
1954
+ };
1955
+ }));
1956
+ /*!
1957
+ * The buffer module from node.js, for the browser.
1958
+ *
1959
+ * @author Feross Aboukhadijeh <https://feross.org>
1960
+ * @license MIT
1961
+ */
1962
+ var require_buffer = /* @__PURE__ */ __commonJSMin(((exports) => {
1963
+ var base64 = require_base64_js();
1964
+ var ieee754 = require_ieee754();
1965
+ var customInspectSymbol = typeof Symbol === "function" && typeof Symbol["for"] === "function" ? Symbol["for"]("nodejs.util.inspect.custom") : null;
1966
+ exports.Buffer = Buffer;
1967
+ exports.SlowBuffer = SlowBuffer;
1968
+ exports.INSPECT_MAX_BYTES = 50;
1969
+ var K_MAX_LENGTH = 2147483647;
1970
+ exports.kMaxLength = K_MAX_LENGTH;
1971
+ /**
1972
+ * If `Buffer.TYPED_ARRAY_SUPPORT`:
1973
+ * === true Use Uint8Array implementation (fastest)
1974
+ * === false Print warning and recommend using `buffer` v4.x which has an Object
1975
+ * implementation (most compatible, even IE6)
1976
+ *
1977
+ * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,
1978
+ * Opera 11.6+, iOS 4.2+.
1979
+ *
1980
+ * We report that the browser does not support typed arrays if the are not subclassable
1981
+ * using __proto__. Firefox 4-29 lacks support for adding new properties to `Uint8Array`
1982
+ * (See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438). IE 10 lacks support
1983
+ * for __proto__ and has a buggy typed array implementation.
1984
+ */
1985
+ Buffer.TYPED_ARRAY_SUPPORT = typedArraySupport();
1986
+ if (!Buffer.TYPED_ARRAY_SUPPORT && typeof console !== "undefined" && typeof console.error === "function") console.error("This browser lacks typed array (Uint8Array) support which is required by `buffer` v5.x. Use `buffer` v4.x if you require old browser support.");
1987
+ function typedArraySupport() {
1988
+ try {
1989
+ const arr = new Uint8Array(1);
1990
+ const proto = { foo: function() {
1991
+ return 42;
1992
+ } };
1993
+ Object.setPrototypeOf(proto, Uint8Array.prototype);
1994
+ Object.setPrototypeOf(arr, proto);
1995
+ return arr.foo() === 42;
1996
+ } catch (e) {
1997
+ return false;
1998
+ }
1999
+ }
2000
+ Object.defineProperty(Buffer.prototype, "parent", {
2001
+ enumerable: true,
2002
+ get: function() {
2003
+ if (!Buffer.isBuffer(this)) return void 0;
2004
+ return this.buffer;
2005
+ }
2006
+ });
2007
+ Object.defineProperty(Buffer.prototype, "offset", {
2008
+ enumerable: true,
2009
+ get: function() {
2010
+ if (!Buffer.isBuffer(this)) return void 0;
2011
+ return this.byteOffset;
2012
+ }
2013
+ });
2014
+ function createBuffer(length) {
2015
+ if (length > K_MAX_LENGTH) throw new RangeError("The value \"" + length + "\" is invalid for option \"size\"");
2016
+ const buf = new Uint8Array(length);
2017
+ Object.setPrototypeOf(buf, Buffer.prototype);
2018
+ return buf;
2019
+ }
2020
+ /**
2021
+ * The Buffer constructor returns instances of `Uint8Array` that have their
2022
+ * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of
2023
+ * `Uint8Array`, so the returned instances will have all the node `Buffer` methods
2024
+ * and the `Uint8Array` methods. Square bracket notation works as expected -- it
2025
+ * returns a single octet.
2026
+ *
2027
+ * The `Uint8Array` prototype remains unmodified.
2028
+ */
2029
+ function Buffer(arg, encodingOrOffset, length) {
2030
+ if (typeof arg === "number") {
2031
+ if (typeof encodingOrOffset === "string") throw new TypeError("The \"string\" argument must be of type string. Received type number");
2032
+ return allocUnsafe(arg);
2033
+ }
2034
+ return from(arg, encodingOrOffset, length);
2035
+ }
2036
+ Buffer.poolSize = 8192;
2037
+ function from(value, encodingOrOffset, length) {
2038
+ if (typeof value === "string") return fromString(value, encodingOrOffset);
2039
+ if (ArrayBuffer.isView(value)) return fromArrayView(value);
2040
+ if (value == null) throw new TypeError("The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type " + typeof value);
2041
+ if (isInstance(value, ArrayBuffer) || value && isInstance(value.buffer, ArrayBuffer)) return fromArrayBuffer(value, encodingOrOffset, length);
2042
+ if (typeof SharedArrayBuffer !== "undefined" && (isInstance(value, SharedArrayBuffer) || value && isInstance(value.buffer, SharedArrayBuffer))) return fromArrayBuffer(value, encodingOrOffset, length);
2043
+ if (typeof value === "number") throw new TypeError("The \"value\" argument must not be of type number. Received type number");
2044
+ const valueOf = value.valueOf && value.valueOf();
2045
+ if (valueOf != null && valueOf !== value) return Buffer.from(valueOf, encodingOrOffset, length);
2046
+ const b = fromObject(value);
2047
+ if (b) return b;
2048
+ if (typeof Symbol !== "undefined" && Symbol.toPrimitive != null && typeof value[Symbol.toPrimitive] === "function") return Buffer.from(value[Symbol.toPrimitive]("string"), encodingOrOffset, length);
2049
+ throw new TypeError("The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type " + typeof value);
2050
+ }
2051
+ /**
2052
+ * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError
2053
+ * if value is a number.
2054
+ * Buffer.from(str[, encoding])
2055
+ * Buffer.from(array)
2056
+ * Buffer.from(buffer)
2057
+ * Buffer.from(arrayBuffer[, byteOffset[, length]])
2058
+ **/
2059
+ Buffer.from = function(value, encodingOrOffset, length) {
2060
+ return from(value, encodingOrOffset, length);
2061
+ };
2062
+ Object.setPrototypeOf(Buffer.prototype, Uint8Array.prototype);
2063
+ Object.setPrototypeOf(Buffer, Uint8Array);
2064
+ function assertSize(size) {
2065
+ if (typeof size !== "number") throw new TypeError("\"size\" argument must be of type number");
2066
+ else if (size < 0) throw new RangeError("The value \"" + size + "\" is invalid for option \"size\"");
2067
+ }
2068
+ function alloc(size, fill, encoding) {
2069
+ assertSize(size);
2070
+ if (size <= 0) return createBuffer(size);
2071
+ if (fill !== void 0) return typeof encoding === "string" ? createBuffer(size).fill(fill, encoding) : createBuffer(size).fill(fill);
2072
+ return createBuffer(size);
2073
+ }
2074
+ /**
2075
+ * Creates a new filled Buffer instance.
2076
+ * alloc(size[, fill[, encoding]])
2077
+ **/
2078
+ Buffer.alloc = function(size, fill, encoding) {
2079
+ return alloc(size, fill, encoding);
2080
+ };
2081
+ function allocUnsafe(size) {
2082
+ assertSize(size);
2083
+ return createBuffer(size < 0 ? 0 : checked(size) | 0);
2084
+ }
2085
+ /**
2086
+ * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance.
2087
+ * */
2088
+ Buffer.allocUnsafe = function(size) {
2089
+ return allocUnsafe(size);
2090
+ };
2091
+ /**
2092
+ * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance.
2093
+ */
2094
+ Buffer.allocUnsafeSlow = function(size) {
2095
+ return allocUnsafe(size);
2096
+ };
2097
+ function fromString(string, encoding) {
2098
+ if (typeof encoding !== "string" || encoding === "") encoding = "utf8";
2099
+ if (!Buffer.isEncoding(encoding)) throw new TypeError("Unknown encoding: " + encoding);
2100
+ const length = byteLength(string, encoding) | 0;
2101
+ let buf = createBuffer(length);
2102
+ const actual = buf.write(string, encoding);
2103
+ if (actual !== length) buf = buf.slice(0, actual);
2104
+ return buf;
2105
+ }
2106
+ function fromArrayLike(array) {
2107
+ const length = array.length < 0 ? 0 : checked(array.length) | 0;
2108
+ const buf = createBuffer(length);
2109
+ for (let i = 0; i < length; i += 1) buf[i] = array[i] & 255;
2110
+ return buf;
2111
+ }
2112
+ function fromArrayView(arrayView) {
2113
+ if (isInstance(arrayView, Uint8Array)) {
2114
+ const copy = new Uint8Array(arrayView);
2115
+ return fromArrayBuffer(copy.buffer, copy.byteOffset, copy.byteLength);
2116
+ }
2117
+ return fromArrayLike(arrayView);
2118
+ }
2119
+ function fromArrayBuffer(array, byteOffset, length) {
2120
+ if (byteOffset < 0 || array.byteLength < byteOffset) throw new RangeError("\"offset\" is outside of buffer bounds");
2121
+ if (array.byteLength < byteOffset + (length || 0)) throw new RangeError("\"length\" is outside of buffer bounds");
2122
+ let buf;
2123
+ if (byteOffset === void 0 && length === void 0) buf = new Uint8Array(array);
2124
+ else if (length === void 0) buf = new Uint8Array(array, byteOffset);
2125
+ else buf = new Uint8Array(array, byteOffset, length);
2126
+ Object.setPrototypeOf(buf, Buffer.prototype);
2127
+ return buf;
2128
+ }
2129
+ function fromObject(obj) {
2130
+ if (Buffer.isBuffer(obj)) {
2131
+ const len = checked(obj.length) | 0;
2132
+ const buf = createBuffer(len);
2133
+ if (buf.length === 0) return buf;
2134
+ obj.copy(buf, 0, 0, len);
2135
+ return buf;
2136
+ }
2137
+ if (obj.length !== void 0) {
2138
+ if (typeof obj.length !== "number" || numberIsNaN(obj.length)) return createBuffer(0);
2139
+ return fromArrayLike(obj);
2140
+ }
2141
+ if (obj.type === "Buffer" && Array.isArray(obj.data)) return fromArrayLike(obj.data);
2142
+ }
2143
+ function checked(length) {
2144
+ if (length >= K_MAX_LENGTH) throw new RangeError("Attempt to allocate Buffer larger than maximum size: 0x" + K_MAX_LENGTH.toString(16) + " bytes");
2145
+ return length | 0;
2146
+ }
2147
+ function SlowBuffer(length) {
2148
+ if (+length != length) length = 0;
2149
+ return Buffer.alloc(+length);
2150
+ }
2151
+ Buffer.isBuffer = function isBuffer(b) {
2152
+ return b != null && b._isBuffer === true && b !== Buffer.prototype;
2153
+ };
2154
+ Buffer.compare = function compare(a, b) {
2155
+ if (isInstance(a, Uint8Array)) a = Buffer.from(a, a.offset, a.byteLength);
2156
+ if (isInstance(b, Uint8Array)) b = Buffer.from(b, b.offset, b.byteLength);
2157
+ if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) throw new TypeError("The \"buf1\", \"buf2\" arguments must be one of type Buffer or Uint8Array");
2158
+ if (a === b) return 0;
2159
+ let x = a.length;
2160
+ let y = b.length;
2161
+ for (let i = 0, len = Math.min(x, y); i < len; ++i) if (a[i] !== b[i]) {
2162
+ x = a[i];
2163
+ y = b[i];
2164
+ break;
2165
+ }
2166
+ if (x < y) return -1;
2167
+ if (y < x) return 1;
2168
+ return 0;
2169
+ };
2170
+ Buffer.isEncoding = function isEncoding(encoding) {
2171
+ switch (String(encoding).toLowerCase()) {
2172
+ case "hex":
2173
+ case "utf8":
2174
+ case "utf-8":
2175
+ case "ascii":
2176
+ case "latin1":
2177
+ case "binary":
2178
+ case "base64":
2179
+ case "ucs2":
2180
+ case "ucs-2":
2181
+ case "utf16le":
2182
+ case "utf-16le": return true;
2183
+ default: return false;
2184
+ }
2185
+ };
2186
+ Buffer.concat = function concat(list, length) {
2187
+ if (!Array.isArray(list)) throw new TypeError("\"list\" argument must be an Array of Buffers");
2188
+ if (list.length === 0) return Buffer.alloc(0);
2189
+ let i;
2190
+ if (length === void 0) {
2191
+ length = 0;
2192
+ for (i = 0; i < list.length; ++i) length += list[i].length;
2193
+ }
2194
+ const buffer = Buffer.allocUnsafe(length);
2195
+ let pos = 0;
2196
+ for (i = 0; i < list.length; ++i) {
2197
+ let buf = list[i];
2198
+ if (isInstance(buf, Uint8Array)) if (pos + buf.length > buffer.length) {
2199
+ if (!Buffer.isBuffer(buf)) buf = Buffer.from(buf);
2200
+ buf.copy(buffer, pos);
2201
+ } else Uint8Array.prototype.set.call(buffer, buf, pos);
2202
+ else if (!Buffer.isBuffer(buf)) throw new TypeError("\"list\" argument must be an Array of Buffers");
2203
+ else buf.copy(buffer, pos);
2204
+ pos += buf.length;
2205
+ }
2206
+ return buffer;
2207
+ };
2208
+ function byteLength(string, encoding) {
2209
+ if (Buffer.isBuffer(string)) return string.length;
2210
+ if (ArrayBuffer.isView(string) || isInstance(string, ArrayBuffer)) return string.byteLength;
2211
+ if (typeof string !== "string") throw new TypeError("The \"string\" argument must be one of type string, Buffer, or ArrayBuffer. Received type " + typeof string);
2212
+ const len = string.length;
2213
+ const mustMatch = arguments.length > 2 && arguments[2] === true;
2214
+ if (!mustMatch && len === 0) return 0;
2215
+ let loweredCase = false;
2216
+ for (;;) switch (encoding) {
2217
+ case "ascii":
2218
+ case "latin1":
2219
+ case "binary": return len;
2220
+ case "utf8":
2221
+ case "utf-8": return utf8ToBytes(string).length;
2222
+ case "ucs2":
2223
+ case "ucs-2":
2224
+ case "utf16le":
2225
+ case "utf-16le": return len * 2;
2226
+ case "hex": return len >>> 1;
2227
+ case "base64": return base64ToBytes(string).length;
2228
+ default:
2229
+ if (loweredCase) return mustMatch ? -1 : utf8ToBytes(string).length;
2230
+ encoding = ("" + encoding).toLowerCase();
2231
+ loweredCase = true;
2232
+ }
2233
+ }
2234
+ Buffer.byteLength = byteLength;
2235
+ function slowToString(encoding, start, end) {
2236
+ let loweredCase = false;
2237
+ if (start === void 0 || start < 0) start = 0;
2238
+ if (start > this.length) return "";
2239
+ if (end === void 0 || end > this.length) end = this.length;
2240
+ if (end <= 0) return "";
2241
+ end >>>= 0;
2242
+ start >>>= 0;
2243
+ if (end <= start) return "";
2244
+ if (!encoding) encoding = "utf8";
2245
+ while (true) switch (encoding) {
2246
+ case "hex": return hexSlice(this, start, end);
2247
+ case "utf8":
2248
+ case "utf-8": return utf8Slice(this, start, end);
2249
+ case "ascii": return asciiSlice(this, start, end);
2250
+ case "latin1":
2251
+ case "binary": return latin1Slice(this, start, end);
2252
+ case "base64": return base64Slice(this, start, end);
2253
+ case "ucs2":
2254
+ case "ucs-2":
2255
+ case "utf16le":
2256
+ case "utf-16le": return utf16leSlice(this, start, end);
2257
+ default:
2258
+ if (loweredCase) throw new TypeError("Unknown encoding: " + encoding);
2259
+ encoding = (encoding + "").toLowerCase();
2260
+ loweredCase = true;
2261
+ }
2262
+ }
2263
+ Buffer.prototype._isBuffer = true;
2264
+ function swap(b, n, m) {
2265
+ const i = b[n];
2266
+ b[n] = b[m];
2267
+ b[m] = i;
2268
+ }
2269
+ Buffer.prototype.swap16 = function swap16() {
2270
+ const len = this.length;
2271
+ if (len % 2 !== 0) throw new RangeError("Buffer size must be a multiple of 16-bits");
2272
+ for (let i = 0; i < len; i += 2) swap(this, i, i + 1);
2273
+ return this;
2274
+ };
2275
+ Buffer.prototype.swap32 = function swap32() {
2276
+ const len = this.length;
2277
+ if (len % 4 !== 0) throw new RangeError("Buffer size must be a multiple of 32-bits");
2278
+ for (let i = 0; i < len; i += 4) {
2279
+ swap(this, i, i + 3);
2280
+ swap(this, i + 1, i + 2);
2281
+ }
2282
+ return this;
2283
+ };
2284
+ Buffer.prototype.swap64 = function swap64() {
2285
+ const len = this.length;
2286
+ if (len % 8 !== 0) throw new RangeError("Buffer size must be a multiple of 64-bits");
2287
+ for (let i = 0; i < len; i += 8) {
2288
+ swap(this, i, i + 7);
2289
+ swap(this, i + 1, i + 6);
2290
+ swap(this, i + 2, i + 5);
2291
+ swap(this, i + 3, i + 4);
2292
+ }
2293
+ return this;
2294
+ };
2295
+ Buffer.prototype.toString = function toString() {
2296
+ const length = this.length;
2297
+ if (length === 0) return "";
2298
+ if (arguments.length === 0) return utf8Slice(this, 0, length);
2299
+ return slowToString.apply(this, arguments);
2300
+ };
2301
+ Buffer.prototype.toLocaleString = Buffer.prototype.toString;
2302
+ Buffer.prototype.equals = function equals(b) {
2303
+ if (!Buffer.isBuffer(b)) throw new TypeError("Argument must be a Buffer");
2304
+ if (this === b) return true;
2305
+ return Buffer.compare(this, b) === 0;
2306
+ };
2307
+ Buffer.prototype.inspect = function inspect() {
2308
+ let str = "";
2309
+ const max = exports.INSPECT_MAX_BYTES;
2310
+ str = this.toString("hex", 0, max).replace(/(.{2})/g, "$1 ").trim();
2311
+ if (this.length > max) str += " ... ";
2312
+ return "<Buffer " + str + ">";
2313
+ };
2314
+ if (customInspectSymbol) Buffer.prototype[customInspectSymbol] = Buffer.prototype.inspect;
2315
+ Buffer.prototype.compare = function compare(target, start, end, thisStart, thisEnd) {
2316
+ if (isInstance(target, Uint8Array)) target = Buffer.from(target, target.offset, target.byteLength);
2317
+ if (!Buffer.isBuffer(target)) throw new TypeError("The \"target\" argument must be one of type Buffer or Uint8Array. Received type " + typeof target);
2318
+ if (start === void 0) start = 0;
2319
+ if (end === void 0) end = target ? target.length : 0;
2320
+ if (thisStart === void 0) thisStart = 0;
2321
+ if (thisEnd === void 0) thisEnd = this.length;
2322
+ if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) throw new RangeError("out of range index");
2323
+ if (thisStart >= thisEnd && start >= end) return 0;
2324
+ if (thisStart >= thisEnd) return -1;
2325
+ if (start >= end) return 1;
2326
+ start >>>= 0;
2327
+ end >>>= 0;
2328
+ thisStart >>>= 0;
2329
+ thisEnd >>>= 0;
2330
+ if (this === target) return 0;
2331
+ let x = thisEnd - thisStart;
2332
+ let y = end - start;
2333
+ const len = Math.min(x, y);
2334
+ const thisCopy = this.slice(thisStart, thisEnd);
2335
+ const targetCopy = target.slice(start, end);
2336
+ for (let i = 0; i < len; ++i) if (thisCopy[i] !== targetCopy[i]) {
2337
+ x = thisCopy[i];
2338
+ y = targetCopy[i];
2339
+ break;
2340
+ }
2341
+ if (x < y) return -1;
2342
+ if (y < x) return 1;
2343
+ return 0;
2344
+ };
2345
+ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
2346
+ if (buffer.length === 0) return -1;
2347
+ if (typeof byteOffset === "string") {
2348
+ encoding = byteOffset;
2349
+ byteOffset = 0;
2350
+ } else if (byteOffset > 2147483647) byteOffset = 2147483647;
2351
+ else if (byteOffset < -2147483648) byteOffset = -2147483648;
2352
+ byteOffset = +byteOffset;
2353
+ if (numberIsNaN(byteOffset)) byteOffset = dir ? 0 : buffer.length - 1;
2354
+ if (byteOffset < 0) byteOffset = buffer.length + byteOffset;
2355
+ if (byteOffset >= buffer.length) if (dir) return -1;
2356
+ else byteOffset = buffer.length - 1;
2357
+ else if (byteOffset < 0) if (dir) byteOffset = 0;
2358
+ else return -1;
2359
+ if (typeof val === "string") val = Buffer.from(val, encoding);
2360
+ if (Buffer.isBuffer(val)) {
2361
+ if (val.length === 0) return -1;
2362
+ return arrayIndexOf(buffer, val, byteOffset, encoding, dir);
2363
+ } else if (typeof val === "number") {
2364
+ val = val & 255;
2365
+ if (typeof Uint8Array.prototype.indexOf === "function") if (dir) return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset);
2366
+ else return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset);
2367
+ return arrayIndexOf(buffer, [val], byteOffset, encoding, dir);
2368
+ }
2369
+ throw new TypeError("val must be string, number or Buffer");
2370
+ }
2371
+ function arrayIndexOf(arr, val, byteOffset, encoding, dir) {
2372
+ let indexSize = 1;
2373
+ let arrLength = arr.length;
2374
+ let valLength = val.length;
2375
+ if (encoding !== void 0) {
2376
+ encoding = String(encoding).toLowerCase();
2377
+ if (encoding === "ucs2" || encoding === "ucs-2" || encoding === "utf16le" || encoding === "utf-16le") {
2378
+ if (arr.length < 2 || val.length < 2) return -1;
2379
+ indexSize = 2;
2380
+ arrLength /= 2;
2381
+ valLength /= 2;
2382
+ byteOffset /= 2;
2383
+ }
2384
+ }
2385
+ function read(buf, i) {
2386
+ if (indexSize === 1) return buf[i];
2387
+ else return buf.readUInt16BE(i * indexSize);
2388
+ }
2389
+ let i;
2390
+ if (dir) {
2391
+ let foundIndex = -1;
2392
+ for (i = byteOffset; i < arrLength; i++) if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
2393
+ if (foundIndex === -1) foundIndex = i;
2394
+ if (i - foundIndex + 1 === valLength) return foundIndex * indexSize;
2395
+ } else {
2396
+ if (foundIndex !== -1) i -= i - foundIndex;
2397
+ foundIndex = -1;
2398
+ }
2399
+ } else {
2400
+ if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength;
2401
+ for (i = byteOffset; i >= 0; i--) {
2402
+ let found = true;
2403
+ for (let j = 0; j < valLength; j++) if (read(arr, i + j) !== read(val, j)) {
2404
+ found = false;
2405
+ break;
2406
+ }
2407
+ if (found) return i;
2408
+ }
2409
+ }
2410
+ return -1;
2411
+ }
2412
+ Buffer.prototype.includes = function includes(val, byteOffset, encoding) {
2413
+ return this.indexOf(val, byteOffset, encoding) !== -1;
2414
+ };
2415
+ Buffer.prototype.indexOf = function indexOf(val, byteOffset, encoding) {
2416
+ return bidirectionalIndexOf(this, val, byteOffset, encoding, true);
2417
+ };
2418
+ Buffer.prototype.lastIndexOf = function lastIndexOf(val, byteOffset, encoding) {
2419
+ return bidirectionalIndexOf(this, val, byteOffset, encoding, false);
2420
+ };
2421
+ function hexWrite(buf, string, offset, length) {
2422
+ offset = Number(offset) || 0;
2423
+ const remaining = buf.length - offset;
2424
+ if (!length) length = remaining;
2425
+ else {
2426
+ length = Number(length);
2427
+ if (length > remaining) length = remaining;
2428
+ }
2429
+ const strLen = string.length;
2430
+ if (length > strLen / 2) length = strLen / 2;
2431
+ let i;
2432
+ for (i = 0; i < length; ++i) {
2433
+ const parsed = parseInt(string.substr(i * 2, 2), 16);
2434
+ if (numberIsNaN(parsed)) return i;
2435
+ buf[offset + i] = parsed;
2436
+ }
2437
+ return i;
2438
+ }
2439
+ function utf8Write(buf, string, offset, length) {
2440
+ return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length);
2441
+ }
2442
+ function asciiWrite(buf, string, offset, length) {
2443
+ return blitBuffer(asciiToBytes(string), buf, offset, length);
2444
+ }
2445
+ function base64Write(buf, string, offset, length) {
2446
+ return blitBuffer(base64ToBytes(string), buf, offset, length);
2447
+ }
2448
+ function ucs2Write(buf, string, offset, length) {
2449
+ return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length);
2450
+ }
2451
+ Buffer.prototype.write = function write(string, offset, length, encoding) {
2452
+ if (offset === void 0) {
2453
+ encoding = "utf8";
2454
+ length = this.length;
2455
+ offset = 0;
2456
+ } else if (length === void 0 && typeof offset === "string") {
2457
+ encoding = offset;
2458
+ length = this.length;
2459
+ offset = 0;
2460
+ } else if (isFinite(offset)) {
2461
+ offset = offset >>> 0;
2462
+ if (isFinite(length)) {
2463
+ length = length >>> 0;
2464
+ if (encoding === void 0) encoding = "utf8";
2465
+ } else {
2466
+ encoding = length;
2467
+ length = void 0;
2468
+ }
2469
+ } else throw new Error("Buffer.write(string, encoding, offset[, length]) is no longer supported");
2470
+ const remaining = this.length - offset;
2471
+ if (length === void 0 || length > remaining) length = remaining;
2472
+ if (string.length > 0 && (length < 0 || offset < 0) || offset > this.length) throw new RangeError("Attempt to write outside buffer bounds");
2473
+ if (!encoding) encoding = "utf8";
2474
+ let loweredCase = false;
2475
+ for (;;) switch (encoding) {
2476
+ case "hex": return hexWrite(this, string, offset, length);
2477
+ case "utf8":
2478
+ case "utf-8": return utf8Write(this, string, offset, length);
2479
+ case "ascii":
2480
+ case "latin1":
2481
+ case "binary": return asciiWrite(this, string, offset, length);
2482
+ case "base64": return base64Write(this, string, offset, length);
2483
+ case "ucs2":
2484
+ case "ucs-2":
2485
+ case "utf16le":
2486
+ case "utf-16le": return ucs2Write(this, string, offset, length);
2487
+ default:
2488
+ if (loweredCase) throw new TypeError("Unknown encoding: " + encoding);
2489
+ encoding = ("" + encoding).toLowerCase();
2490
+ loweredCase = true;
2491
+ }
2492
+ };
2493
+ Buffer.prototype.toJSON = function toJSON() {
2494
+ return {
2495
+ type: "Buffer",
2496
+ data: Array.prototype.slice.call(this._arr || this, 0)
2497
+ };
2498
+ };
2499
+ function base64Slice(buf, start, end) {
2500
+ if (start === 0 && end === buf.length) return base64.fromByteArray(buf);
2501
+ else return base64.fromByteArray(buf.slice(start, end));
2502
+ }
2503
+ function utf8Slice(buf, start, end) {
2504
+ end = Math.min(buf.length, end);
2505
+ const res = [];
2506
+ let i = start;
2507
+ while (i < end) {
2508
+ const firstByte = buf[i];
2509
+ let codePoint = null;
2510
+ let bytesPerSequence = firstByte > 239 ? 4 : firstByte > 223 ? 3 : firstByte > 191 ? 2 : 1;
2511
+ if (i + bytesPerSequence <= end) {
2512
+ let secondByte, thirdByte, fourthByte, tempCodePoint;
2513
+ switch (bytesPerSequence) {
2514
+ case 1:
2515
+ if (firstByte < 128) codePoint = firstByte;
2516
+ break;
2517
+ case 2:
2518
+ secondByte = buf[i + 1];
2519
+ if ((secondByte & 192) === 128) {
2520
+ tempCodePoint = (firstByte & 31) << 6 | secondByte & 63;
2521
+ if (tempCodePoint > 127) codePoint = tempCodePoint;
2522
+ }
2523
+ break;
2524
+ case 3:
2525
+ secondByte = buf[i + 1];
2526
+ thirdByte = buf[i + 2];
2527
+ if ((secondByte & 192) === 128 && (thirdByte & 192) === 128) {
2528
+ tempCodePoint = (firstByte & 15) << 12 | (secondByte & 63) << 6 | thirdByte & 63;
2529
+ if (tempCodePoint > 2047 && (tempCodePoint < 55296 || tempCodePoint > 57343)) codePoint = tempCodePoint;
2530
+ }
2531
+ break;
2532
+ case 4:
2533
+ secondByte = buf[i + 1];
2534
+ thirdByte = buf[i + 2];
2535
+ fourthByte = buf[i + 3];
2536
+ if ((secondByte & 192) === 128 && (thirdByte & 192) === 128 && (fourthByte & 192) === 128) {
2537
+ tempCodePoint = (firstByte & 15) << 18 | (secondByte & 63) << 12 | (thirdByte & 63) << 6 | fourthByte & 63;
2538
+ if (tempCodePoint > 65535 && tempCodePoint < 1114112) codePoint = tempCodePoint;
2539
+ }
2540
+ }
2541
+ }
2542
+ if (codePoint === null) {
2543
+ codePoint = 65533;
2544
+ bytesPerSequence = 1;
2545
+ } else if (codePoint > 65535) {
2546
+ codePoint -= 65536;
2547
+ res.push(codePoint >>> 10 & 1023 | 55296);
2548
+ codePoint = 56320 | codePoint & 1023;
2549
+ }
2550
+ res.push(codePoint);
2551
+ i += bytesPerSequence;
2552
+ }
2553
+ return decodeCodePointsArray(res);
2554
+ }
2555
+ var MAX_ARGUMENTS_LENGTH = 4096;
2556
+ function decodeCodePointsArray(codePoints) {
2557
+ const len = codePoints.length;
2558
+ if (len <= MAX_ARGUMENTS_LENGTH) return String.fromCharCode.apply(String, codePoints);
2559
+ let res = "";
2560
+ let i = 0;
2561
+ while (i < len) res += String.fromCharCode.apply(String, codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH));
2562
+ return res;
2563
+ }
2564
+ function asciiSlice(buf, start, end) {
2565
+ let ret = "";
2566
+ end = Math.min(buf.length, end);
2567
+ for (let i = start; i < end; ++i) ret += String.fromCharCode(buf[i] & 127);
2568
+ return ret;
2569
+ }
2570
+ function latin1Slice(buf, start, end) {
2571
+ let ret = "";
2572
+ end = Math.min(buf.length, end);
2573
+ for (let i = start; i < end; ++i) ret += String.fromCharCode(buf[i]);
2574
+ return ret;
2575
+ }
2576
+ function hexSlice(buf, start, end) {
2577
+ const len = buf.length;
2578
+ if (!start || start < 0) start = 0;
2579
+ if (!end || end < 0 || end > len) end = len;
2580
+ let out = "";
2581
+ for (let i = start; i < end; ++i) out += hexSliceLookupTable[buf[i]];
2582
+ return out;
2583
+ }
2584
+ function utf16leSlice(buf, start, end) {
2585
+ const bytes = buf.slice(start, end);
2586
+ let res = "";
2587
+ for (let i = 0; i < bytes.length - 1; i += 2) res += String.fromCharCode(bytes[i] + bytes[i + 1] * 256);
2588
+ return res;
2589
+ }
2590
+ Buffer.prototype.slice = function slice(start, end) {
2591
+ const len = this.length;
2592
+ start = ~~start;
2593
+ end = end === void 0 ? len : ~~end;
2594
+ if (start < 0) {
2595
+ start += len;
2596
+ if (start < 0) start = 0;
2597
+ } else if (start > len) start = len;
2598
+ if (end < 0) {
2599
+ end += len;
2600
+ if (end < 0) end = 0;
2601
+ } else if (end > len) end = len;
2602
+ if (end < start) end = start;
2603
+ const newBuf = this.subarray(start, end);
2604
+ Object.setPrototypeOf(newBuf, Buffer.prototype);
2605
+ return newBuf;
2606
+ };
2607
+ function checkOffset(offset, ext, length) {
2608
+ if (offset % 1 !== 0 || offset < 0) throw new RangeError("offset is not uint");
2609
+ if (offset + ext > length) throw new RangeError("Trying to access beyond buffer length");
2610
+ }
2611
+ Buffer.prototype.readUintLE = Buffer.prototype.readUIntLE = function readUIntLE(offset, byteLength, noAssert) {
2612
+ offset = offset >>> 0;
2613
+ byteLength = byteLength >>> 0;
2614
+ if (!noAssert) checkOffset(offset, byteLength, this.length);
2615
+ let val = this[offset];
2616
+ let mul = 1;
2617
+ let i = 0;
2618
+ while (++i < byteLength && (mul *= 256)) val += this[offset + i] * mul;
2619
+ return val;
2620
+ };
2621
+ Buffer.prototype.readUintBE = Buffer.prototype.readUIntBE = function readUIntBE(offset, byteLength, noAssert) {
2622
+ offset = offset >>> 0;
2623
+ byteLength = byteLength >>> 0;
2624
+ if (!noAssert) checkOffset(offset, byteLength, this.length);
2625
+ let val = this[offset + --byteLength];
2626
+ let mul = 1;
2627
+ while (byteLength > 0 && (mul *= 256)) val += this[offset + --byteLength] * mul;
2628
+ return val;
2629
+ };
2630
+ Buffer.prototype.readUint8 = Buffer.prototype.readUInt8 = function readUInt8(offset, noAssert) {
2631
+ offset = offset >>> 0;
2632
+ if (!noAssert) checkOffset(offset, 1, this.length);
2633
+ return this[offset];
2634
+ };
2635
+ Buffer.prototype.readUint16LE = Buffer.prototype.readUInt16LE = function readUInt16LE(offset, noAssert) {
2636
+ offset = offset >>> 0;
2637
+ if (!noAssert) checkOffset(offset, 2, this.length);
2638
+ return this[offset] | this[offset + 1] << 8;
2639
+ };
2640
+ Buffer.prototype.readUint16BE = Buffer.prototype.readUInt16BE = function readUInt16BE(offset, noAssert) {
2641
+ offset = offset >>> 0;
2642
+ if (!noAssert) checkOffset(offset, 2, this.length);
2643
+ return this[offset] << 8 | this[offset + 1];
2644
+ };
2645
+ Buffer.prototype.readUint32LE = Buffer.prototype.readUInt32LE = function readUInt32LE(offset, noAssert) {
2646
+ offset = offset >>> 0;
2647
+ if (!noAssert) checkOffset(offset, 4, this.length);
2648
+ return (this[offset] | this[offset + 1] << 8 | this[offset + 2] << 16) + this[offset + 3] * 16777216;
2649
+ };
2650
+ Buffer.prototype.readUint32BE = Buffer.prototype.readUInt32BE = function readUInt32BE(offset, noAssert) {
2651
+ offset = offset >>> 0;
2652
+ if (!noAssert) checkOffset(offset, 4, this.length);
2653
+ return this[offset] * 16777216 + (this[offset + 1] << 16 | this[offset + 2] << 8 | this[offset + 3]);
2654
+ };
2655
+ Buffer.prototype.readBigUInt64LE = defineBigIntMethod(function readBigUInt64LE(offset) {
2656
+ offset = offset >>> 0;
2657
+ validateNumber(offset, "offset");
2658
+ const first = this[offset];
2659
+ const last = this[offset + 7];
2660
+ if (first === void 0 || last === void 0) boundsError(offset, this.length - 8);
2661
+ const lo = first + this[++offset] * 2 ** 8 + this[++offset] * 2 ** 16 + this[++offset] * 2 ** 24;
2662
+ const hi = this[++offset] + this[++offset] * 2 ** 8 + this[++offset] * 2 ** 16 + last * 2 ** 24;
2663
+ return BigInt(lo) + (BigInt(hi) << BigInt(32));
2664
+ });
2665
+ Buffer.prototype.readBigUInt64BE = defineBigIntMethod(function readBigUInt64BE(offset) {
2666
+ offset = offset >>> 0;
2667
+ validateNumber(offset, "offset");
2668
+ const first = this[offset];
2669
+ const last = this[offset + 7];
2670
+ if (first === void 0 || last === void 0) boundsError(offset, this.length - 8);
2671
+ const hi = first * 2 ** 24 + this[++offset] * 2 ** 16 + this[++offset] * 2 ** 8 + this[++offset];
2672
+ const lo = this[++offset] * 2 ** 24 + this[++offset] * 2 ** 16 + this[++offset] * 2 ** 8 + last;
2673
+ return (BigInt(hi) << BigInt(32)) + BigInt(lo);
2674
+ });
2675
+ Buffer.prototype.readIntLE = function readIntLE(offset, byteLength, noAssert) {
2676
+ offset = offset >>> 0;
2677
+ byteLength = byteLength >>> 0;
2678
+ if (!noAssert) checkOffset(offset, byteLength, this.length);
2679
+ let val = this[offset];
2680
+ let mul = 1;
2681
+ let i = 0;
2682
+ while (++i < byteLength && (mul *= 256)) val += this[offset + i] * mul;
2683
+ mul *= 128;
2684
+ if (val >= mul) val -= Math.pow(2, 8 * byteLength);
2685
+ return val;
2686
+ };
2687
+ Buffer.prototype.readIntBE = function readIntBE(offset, byteLength, noAssert) {
2688
+ offset = offset >>> 0;
2689
+ byteLength = byteLength >>> 0;
2690
+ if (!noAssert) checkOffset(offset, byteLength, this.length);
2691
+ let i = byteLength;
2692
+ let mul = 1;
2693
+ let val = this[offset + --i];
2694
+ while (i > 0 && (mul *= 256)) val += this[offset + --i] * mul;
2695
+ mul *= 128;
2696
+ if (val >= mul) val -= Math.pow(2, 8 * byteLength);
2697
+ return val;
2698
+ };
2699
+ Buffer.prototype.readInt8 = function readInt8(offset, noAssert) {
2700
+ offset = offset >>> 0;
2701
+ if (!noAssert) checkOffset(offset, 1, this.length);
2702
+ if (!(this[offset] & 128)) return this[offset];
2703
+ return (255 - this[offset] + 1) * -1;
2704
+ };
2705
+ Buffer.prototype.readInt16LE = function readInt16LE(offset, noAssert) {
2706
+ offset = offset >>> 0;
2707
+ if (!noAssert) checkOffset(offset, 2, this.length);
2708
+ const val = this[offset] | this[offset + 1] << 8;
2709
+ return val & 32768 ? val | 4294901760 : val;
2710
+ };
2711
+ Buffer.prototype.readInt16BE = function readInt16BE(offset, noAssert) {
2712
+ offset = offset >>> 0;
2713
+ if (!noAssert) checkOffset(offset, 2, this.length);
2714
+ const val = this[offset + 1] | this[offset] << 8;
2715
+ return val & 32768 ? val | 4294901760 : val;
2716
+ };
2717
+ Buffer.prototype.readInt32LE = function readInt32LE(offset, noAssert) {
2718
+ offset = offset >>> 0;
2719
+ if (!noAssert) checkOffset(offset, 4, this.length);
2720
+ return this[offset] | this[offset + 1] << 8 | this[offset + 2] << 16 | this[offset + 3] << 24;
2721
+ };
2722
+ Buffer.prototype.readInt32BE = function readInt32BE(offset, noAssert) {
2723
+ offset = offset >>> 0;
2724
+ if (!noAssert) checkOffset(offset, 4, this.length);
2725
+ return this[offset] << 24 | this[offset + 1] << 16 | this[offset + 2] << 8 | this[offset + 3];
2726
+ };
2727
+ Buffer.prototype.readBigInt64LE = defineBigIntMethod(function readBigInt64LE(offset) {
2728
+ offset = offset >>> 0;
2729
+ validateNumber(offset, "offset");
2730
+ const first = this[offset];
2731
+ const last = this[offset + 7];
2732
+ if (first === void 0 || last === void 0) boundsError(offset, this.length - 8);
2733
+ const val = this[offset + 4] + this[offset + 5] * 2 ** 8 + this[offset + 6] * 2 ** 16 + (last << 24);
2734
+ return (BigInt(val) << BigInt(32)) + BigInt(first + this[++offset] * 2 ** 8 + this[++offset] * 2 ** 16 + this[++offset] * 2 ** 24);
2735
+ });
2736
+ Buffer.prototype.readBigInt64BE = defineBigIntMethod(function readBigInt64BE(offset) {
2737
+ offset = offset >>> 0;
2738
+ validateNumber(offset, "offset");
2739
+ const first = this[offset];
2740
+ const last = this[offset + 7];
2741
+ if (first === void 0 || last === void 0) boundsError(offset, this.length - 8);
2742
+ const val = (first << 24) + this[++offset] * 2 ** 16 + this[++offset] * 2 ** 8 + this[++offset];
2743
+ return (BigInt(val) << BigInt(32)) + BigInt(this[++offset] * 2 ** 24 + this[++offset] * 2 ** 16 + this[++offset] * 2 ** 8 + last);
2744
+ });
2745
+ Buffer.prototype.readFloatLE = function readFloatLE(offset, noAssert) {
2746
+ offset = offset >>> 0;
2747
+ if (!noAssert) checkOffset(offset, 4, this.length);
2748
+ return ieee754.read(this, offset, true, 23, 4);
2749
+ };
2750
+ Buffer.prototype.readFloatBE = function readFloatBE(offset, noAssert) {
2751
+ offset = offset >>> 0;
2752
+ if (!noAssert) checkOffset(offset, 4, this.length);
2753
+ return ieee754.read(this, offset, false, 23, 4);
2754
+ };
2755
+ Buffer.prototype.readDoubleLE = function readDoubleLE(offset, noAssert) {
2756
+ offset = offset >>> 0;
2757
+ if (!noAssert) checkOffset(offset, 8, this.length);
2758
+ return ieee754.read(this, offset, true, 52, 8);
2759
+ };
2760
+ Buffer.prototype.readDoubleBE = function readDoubleBE(offset, noAssert) {
2761
+ offset = offset >>> 0;
2762
+ if (!noAssert) checkOffset(offset, 8, this.length);
2763
+ return ieee754.read(this, offset, false, 52, 8);
2764
+ };
2765
+ function checkInt(buf, value, offset, ext, max, min) {
2766
+ if (!Buffer.isBuffer(buf)) throw new TypeError("\"buffer\" argument must be a Buffer instance");
2767
+ if (value > max || value < min) throw new RangeError("\"value\" argument is out of bounds");
2768
+ if (offset + ext > buf.length) throw new RangeError("Index out of range");
2769
+ }
2770
+ Buffer.prototype.writeUintLE = Buffer.prototype.writeUIntLE = function writeUIntLE(value, offset, byteLength, noAssert) {
2771
+ value = +value;
2772
+ offset = offset >>> 0;
2773
+ byteLength = byteLength >>> 0;
2774
+ if (!noAssert) {
2775
+ const maxBytes = Math.pow(2, 8 * byteLength) - 1;
2776
+ checkInt(this, value, offset, byteLength, maxBytes, 0);
2777
+ }
2778
+ let mul = 1;
2779
+ let i = 0;
2780
+ this[offset] = value & 255;
2781
+ while (++i < byteLength && (mul *= 256)) this[offset + i] = value / mul & 255;
2782
+ return offset + byteLength;
2783
+ };
2784
+ Buffer.prototype.writeUintBE = Buffer.prototype.writeUIntBE = function writeUIntBE(value, offset, byteLength, noAssert) {
2785
+ value = +value;
2786
+ offset = offset >>> 0;
2787
+ byteLength = byteLength >>> 0;
2788
+ if (!noAssert) {
2789
+ const maxBytes = Math.pow(2, 8 * byteLength) - 1;
2790
+ checkInt(this, value, offset, byteLength, maxBytes, 0);
2791
+ }
2792
+ let i = byteLength - 1;
2793
+ let mul = 1;
2794
+ this[offset + i] = value & 255;
2795
+ while (--i >= 0 && (mul *= 256)) this[offset + i] = value / mul & 255;
2796
+ return offset + byteLength;
2797
+ };
2798
+ Buffer.prototype.writeUint8 = Buffer.prototype.writeUInt8 = function writeUInt8(value, offset, noAssert) {
2799
+ value = +value;
2800
+ offset = offset >>> 0;
2801
+ if (!noAssert) checkInt(this, value, offset, 1, 255, 0);
2802
+ this[offset] = value & 255;
2803
+ return offset + 1;
2804
+ };
2805
+ Buffer.prototype.writeUint16LE = Buffer.prototype.writeUInt16LE = function writeUInt16LE(value, offset, noAssert) {
2806
+ value = +value;
2807
+ offset = offset >>> 0;
2808
+ if (!noAssert) checkInt(this, value, offset, 2, 65535, 0);
2809
+ this[offset] = value & 255;
2810
+ this[offset + 1] = value >>> 8;
2811
+ return offset + 2;
2812
+ };
2813
+ Buffer.prototype.writeUint16BE = Buffer.prototype.writeUInt16BE = function writeUInt16BE(value, offset, noAssert) {
2814
+ value = +value;
2815
+ offset = offset >>> 0;
2816
+ if (!noAssert) checkInt(this, value, offset, 2, 65535, 0);
2817
+ this[offset] = value >>> 8;
2818
+ this[offset + 1] = value & 255;
2819
+ return offset + 2;
2820
+ };
2821
+ Buffer.prototype.writeUint32LE = Buffer.prototype.writeUInt32LE = function writeUInt32LE(value, offset, noAssert) {
2822
+ value = +value;
2823
+ offset = offset >>> 0;
2824
+ if (!noAssert) checkInt(this, value, offset, 4, 4294967295, 0);
2825
+ this[offset + 3] = value >>> 24;
2826
+ this[offset + 2] = value >>> 16;
2827
+ this[offset + 1] = value >>> 8;
2828
+ this[offset] = value & 255;
2829
+ return offset + 4;
2830
+ };
2831
+ Buffer.prototype.writeUint32BE = Buffer.prototype.writeUInt32BE = function writeUInt32BE(value, offset, noAssert) {
2832
+ value = +value;
2833
+ offset = offset >>> 0;
2834
+ if (!noAssert) checkInt(this, value, offset, 4, 4294967295, 0);
2835
+ this[offset] = value >>> 24;
2836
+ this[offset + 1] = value >>> 16;
2837
+ this[offset + 2] = value >>> 8;
2838
+ this[offset + 3] = value & 255;
2839
+ return offset + 4;
2840
+ };
2841
+ function wrtBigUInt64LE(buf, value, offset, min, max) {
2842
+ checkIntBI(value, min, max, buf, offset, 7);
2843
+ let lo = Number(value & BigInt(4294967295));
2844
+ buf[offset++] = lo;
2845
+ lo = lo >> 8;
2846
+ buf[offset++] = lo;
2847
+ lo = lo >> 8;
2848
+ buf[offset++] = lo;
2849
+ lo = lo >> 8;
2850
+ buf[offset++] = lo;
2851
+ let hi = Number(value >> BigInt(32) & BigInt(4294967295));
2852
+ buf[offset++] = hi;
2853
+ hi = hi >> 8;
2854
+ buf[offset++] = hi;
2855
+ hi = hi >> 8;
2856
+ buf[offset++] = hi;
2857
+ hi = hi >> 8;
2858
+ buf[offset++] = hi;
2859
+ return offset;
2860
+ }
2861
+ function wrtBigUInt64BE(buf, value, offset, min, max) {
2862
+ checkIntBI(value, min, max, buf, offset, 7);
2863
+ let lo = Number(value & BigInt(4294967295));
2864
+ buf[offset + 7] = lo;
2865
+ lo = lo >> 8;
2866
+ buf[offset + 6] = lo;
2867
+ lo = lo >> 8;
2868
+ buf[offset + 5] = lo;
2869
+ lo = lo >> 8;
2870
+ buf[offset + 4] = lo;
2871
+ let hi = Number(value >> BigInt(32) & BigInt(4294967295));
2872
+ buf[offset + 3] = hi;
2873
+ hi = hi >> 8;
2874
+ buf[offset + 2] = hi;
2875
+ hi = hi >> 8;
2876
+ buf[offset + 1] = hi;
2877
+ hi = hi >> 8;
2878
+ buf[offset] = hi;
2879
+ return offset + 8;
2880
+ }
2881
+ Buffer.prototype.writeBigUInt64LE = defineBigIntMethod(function writeBigUInt64LE(value, offset = 0) {
2882
+ return wrtBigUInt64LE(this, value, offset, BigInt(0), BigInt("0xffffffffffffffff"));
2883
+ });
2884
+ Buffer.prototype.writeBigUInt64BE = defineBigIntMethod(function writeBigUInt64BE(value, offset = 0) {
2885
+ return wrtBigUInt64BE(this, value, offset, BigInt(0), BigInt("0xffffffffffffffff"));
2886
+ });
2887
+ Buffer.prototype.writeIntLE = function writeIntLE(value, offset, byteLength, noAssert) {
2888
+ value = +value;
2889
+ offset = offset >>> 0;
2890
+ if (!noAssert) {
2891
+ const limit = Math.pow(2, 8 * byteLength - 1);
2892
+ checkInt(this, value, offset, byteLength, limit - 1, -limit);
2893
+ }
2894
+ let i = 0;
2895
+ let mul = 1;
2896
+ let sub = 0;
2897
+ this[offset] = value & 255;
2898
+ while (++i < byteLength && (mul *= 256)) {
2899
+ if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) sub = 1;
2900
+ this[offset + i] = (value / mul >> 0) - sub & 255;
2901
+ }
2902
+ return offset + byteLength;
2903
+ };
2904
+ Buffer.prototype.writeIntBE = function writeIntBE(value, offset, byteLength, noAssert) {
2905
+ value = +value;
2906
+ offset = offset >>> 0;
2907
+ if (!noAssert) {
2908
+ const limit = Math.pow(2, 8 * byteLength - 1);
2909
+ checkInt(this, value, offset, byteLength, limit - 1, -limit);
2910
+ }
2911
+ let i = byteLength - 1;
2912
+ let mul = 1;
2913
+ let sub = 0;
2914
+ this[offset + i] = value & 255;
2915
+ while (--i >= 0 && (mul *= 256)) {
2916
+ if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) sub = 1;
2917
+ this[offset + i] = (value / mul >> 0) - sub & 255;
2918
+ }
2919
+ return offset + byteLength;
2920
+ };
2921
+ Buffer.prototype.writeInt8 = function writeInt8(value, offset, noAssert) {
2922
+ value = +value;
2923
+ offset = offset >>> 0;
2924
+ if (!noAssert) checkInt(this, value, offset, 1, 127, -128);
2925
+ if (value < 0) value = 255 + value + 1;
2926
+ this[offset] = value & 255;
2927
+ return offset + 1;
2928
+ };
2929
+ Buffer.prototype.writeInt16LE = function writeInt16LE(value, offset, noAssert) {
2930
+ value = +value;
2931
+ offset = offset >>> 0;
2932
+ if (!noAssert) checkInt(this, value, offset, 2, 32767, -32768);
2933
+ this[offset] = value & 255;
2934
+ this[offset + 1] = value >>> 8;
2935
+ return offset + 2;
2936
+ };
2937
+ Buffer.prototype.writeInt16BE = function writeInt16BE(value, offset, noAssert) {
2938
+ value = +value;
2939
+ offset = offset >>> 0;
2940
+ if (!noAssert) checkInt(this, value, offset, 2, 32767, -32768);
2941
+ this[offset] = value >>> 8;
2942
+ this[offset + 1] = value & 255;
2943
+ return offset + 2;
2944
+ };
2945
+ Buffer.prototype.writeInt32LE = function writeInt32LE(value, offset, noAssert) {
2946
+ value = +value;
2947
+ offset = offset >>> 0;
2948
+ if (!noAssert) checkInt(this, value, offset, 4, 2147483647, -2147483648);
2949
+ this[offset] = value & 255;
2950
+ this[offset + 1] = value >>> 8;
2951
+ this[offset + 2] = value >>> 16;
2952
+ this[offset + 3] = value >>> 24;
2953
+ return offset + 4;
2954
+ };
2955
+ Buffer.prototype.writeInt32BE = function writeInt32BE(value, offset, noAssert) {
2956
+ value = +value;
2957
+ offset = offset >>> 0;
2958
+ if (!noAssert) checkInt(this, value, offset, 4, 2147483647, -2147483648);
2959
+ if (value < 0) value = 4294967295 + value + 1;
2960
+ this[offset] = value >>> 24;
2961
+ this[offset + 1] = value >>> 16;
2962
+ this[offset + 2] = value >>> 8;
2963
+ this[offset + 3] = value & 255;
2964
+ return offset + 4;
2965
+ };
2966
+ Buffer.prototype.writeBigInt64LE = defineBigIntMethod(function writeBigInt64LE(value, offset = 0) {
2967
+ return wrtBigUInt64LE(this, value, offset, -BigInt("0x8000000000000000"), BigInt("0x7fffffffffffffff"));
2968
+ });
2969
+ Buffer.prototype.writeBigInt64BE = defineBigIntMethod(function writeBigInt64BE(value, offset = 0) {
2970
+ return wrtBigUInt64BE(this, value, offset, -BigInt("0x8000000000000000"), BigInt("0x7fffffffffffffff"));
2971
+ });
2972
+ function checkIEEE754(buf, value, offset, ext, max, min) {
2973
+ if (offset + ext > buf.length) throw new RangeError("Index out of range");
2974
+ if (offset < 0) throw new RangeError("Index out of range");
2975
+ }
2976
+ function writeFloat(buf, value, offset, littleEndian, noAssert) {
2977
+ value = +value;
2978
+ offset = offset >>> 0;
2979
+ if (!noAssert) checkIEEE754(buf, value, offset, 4, 34028234663852886e22, -34028234663852886e22);
2980
+ ieee754.write(buf, value, offset, littleEndian, 23, 4);
2981
+ return offset + 4;
2982
+ }
2983
+ Buffer.prototype.writeFloatLE = function writeFloatLE(value, offset, noAssert) {
2984
+ return writeFloat(this, value, offset, true, noAssert);
2985
+ };
2986
+ Buffer.prototype.writeFloatBE = function writeFloatBE(value, offset, noAssert) {
2987
+ return writeFloat(this, value, offset, false, noAssert);
2988
+ };
2989
+ function writeDouble(buf, value, offset, littleEndian, noAssert) {
2990
+ value = +value;
2991
+ offset = offset >>> 0;
2992
+ if (!noAssert) checkIEEE754(buf, value, offset, 8, 17976931348623157e292, -17976931348623157e292);
2993
+ ieee754.write(buf, value, offset, littleEndian, 52, 8);
2994
+ return offset + 8;
2995
+ }
2996
+ Buffer.prototype.writeDoubleLE = function writeDoubleLE(value, offset, noAssert) {
2997
+ return writeDouble(this, value, offset, true, noAssert);
2998
+ };
2999
+ Buffer.prototype.writeDoubleBE = function writeDoubleBE(value, offset, noAssert) {
3000
+ return writeDouble(this, value, offset, false, noAssert);
3001
+ };
3002
+ Buffer.prototype.copy = function copy(target, targetStart, start, end) {
3003
+ if (!Buffer.isBuffer(target)) throw new TypeError("argument should be a Buffer");
3004
+ if (!start) start = 0;
3005
+ if (!end && end !== 0) end = this.length;
3006
+ if (targetStart >= target.length) targetStart = target.length;
3007
+ if (!targetStart) targetStart = 0;
3008
+ if (end > 0 && end < start) end = start;
3009
+ if (end === start) return 0;
3010
+ if (target.length === 0 || this.length === 0) return 0;
3011
+ if (targetStart < 0) throw new RangeError("targetStart out of bounds");
3012
+ if (start < 0 || start >= this.length) throw new RangeError("Index out of range");
3013
+ if (end < 0) throw new RangeError("sourceEnd out of bounds");
3014
+ if (end > this.length) end = this.length;
3015
+ if (target.length - targetStart < end - start) end = target.length - targetStart + start;
3016
+ const len = end - start;
3017
+ if (this === target && typeof Uint8Array.prototype.copyWithin === "function") this.copyWithin(targetStart, start, end);
3018
+ else Uint8Array.prototype.set.call(target, this.subarray(start, end), targetStart);
3019
+ return len;
3020
+ };
3021
+ Buffer.prototype.fill = function fill(val, start, end, encoding) {
3022
+ if (typeof val === "string") {
3023
+ if (typeof start === "string") {
3024
+ encoding = start;
3025
+ start = 0;
3026
+ end = this.length;
3027
+ } else if (typeof end === "string") {
3028
+ encoding = end;
3029
+ end = this.length;
3030
+ }
3031
+ if (encoding !== void 0 && typeof encoding !== "string") throw new TypeError("encoding must be a string");
3032
+ if (typeof encoding === "string" && !Buffer.isEncoding(encoding)) throw new TypeError("Unknown encoding: " + encoding);
3033
+ if (val.length === 1) {
3034
+ const code = val.charCodeAt(0);
3035
+ if (encoding === "utf8" && code < 128 || encoding === "latin1") val = code;
3036
+ }
3037
+ } else if (typeof val === "number") val = val & 255;
3038
+ else if (typeof val === "boolean") val = Number(val);
3039
+ if (start < 0 || this.length < start || this.length < end) throw new RangeError("Out of range index");
3040
+ if (end <= start) return this;
3041
+ start = start >>> 0;
3042
+ end = end === void 0 ? this.length : end >>> 0;
3043
+ if (!val) val = 0;
3044
+ let i;
3045
+ if (typeof val === "number") for (i = start; i < end; ++i) this[i] = val;
3046
+ else {
3047
+ const bytes = Buffer.isBuffer(val) ? val : Buffer.from(val, encoding);
3048
+ const len = bytes.length;
3049
+ if (len === 0) throw new TypeError("The value \"" + val + "\" is invalid for argument \"value\"");
3050
+ for (i = 0; i < end - start; ++i) this[i + start] = bytes[i % len];
3051
+ }
3052
+ return this;
3053
+ };
3054
+ var errors = {};
3055
+ function E(sym, getMessage, Base) {
3056
+ errors[sym] = class NodeError extends Base {
3057
+ constructor() {
3058
+ super();
3059
+ Object.defineProperty(this, "message", {
3060
+ value: getMessage.apply(this, arguments),
3061
+ writable: true,
3062
+ configurable: true
3063
+ });
3064
+ this.name = `${this.name} [${sym}]`;
3065
+ this.stack;
3066
+ delete this.name;
3067
+ }
3068
+ get code() {
3069
+ return sym;
3070
+ }
3071
+ set code(value) {
3072
+ Object.defineProperty(this, "code", {
3073
+ configurable: true,
3074
+ enumerable: true,
3075
+ value,
3076
+ writable: true
3077
+ });
3078
+ }
3079
+ toString() {
3080
+ return `${this.name} [${sym}]: ${this.message}`;
3081
+ }
3082
+ };
3083
+ }
3084
+ E("ERR_BUFFER_OUT_OF_BOUNDS", function(name) {
3085
+ if (name) return `${name} is outside of buffer bounds`;
3086
+ return "Attempt to access memory outside buffer bounds";
3087
+ }, RangeError);
3088
+ E("ERR_INVALID_ARG_TYPE", function(name, actual) {
3089
+ return `The "${name}" argument must be of type number. Received type ${typeof actual}`;
3090
+ }, TypeError);
3091
+ E("ERR_OUT_OF_RANGE", function(str, range, input) {
3092
+ let msg = `The value of "${str}" is out of range.`;
3093
+ let received = input;
3094
+ if (Number.isInteger(input) && Math.abs(input) > 2 ** 32) received = addNumericalSeparator(String(input));
3095
+ else if (typeof input === "bigint") {
3096
+ received = String(input);
3097
+ if (input > BigInt(2) ** BigInt(32) || input < -(BigInt(2) ** BigInt(32))) received = addNumericalSeparator(received);
3098
+ received += "n";
3099
+ }
3100
+ msg += ` It must be ${range}. Received ${received}`;
3101
+ return msg;
3102
+ }, RangeError);
3103
+ function addNumericalSeparator(val) {
3104
+ let res = "";
3105
+ let i = val.length;
3106
+ const start = val[0] === "-" ? 1 : 0;
3107
+ for (; i >= start + 4; i -= 3) res = `_${val.slice(i - 3, i)}${res}`;
3108
+ return `${val.slice(0, i)}${res}`;
3109
+ }
3110
+ function checkBounds(buf, offset, byteLength) {
3111
+ validateNumber(offset, "offset");
3112
+ if (buf[offset] === void 0 || buf[offset + byteLength] === void 0) boundsError(offset, buf.length - (byteLength + 1));
3113
+ }
3114
+ function checkIntBI(value, min, max, buf, offset, byteLength) {
3115
+ if (value > max || value < min) {
3116
+ const n = typeof min === "bigint" ? "n" : "";
3117
+ let range;
3118
+ if (byteLength > 3) if (min === 0 || min === BigInt(0)) range = `>= 0${n} and < 2${n} ** ${(byteLength + 1) * 8}${n}`;
3119
+ else range = `>= -(2${n} ** ${(byteLength + 1) * 8 - 1}${n}) and < 2 ** ${(byteLength + 1) * 8 - 1}${n}`;
3120
+ else range = `>= ${min}${n} and <= ${max}${n}`;
3121
+ throw new errors.ERR_OUT_OF_RANGE("value", range, value);
3122
+ }
3123
+ checkBounds(buf, offset, byteLength);
3124
+ }
3125
+ function validateNumber(value, name) {
3126
+ if (typeof value !== "number") throw new errors.ERR_INVALID_ARG_TYPE(name, "number", value);
3127
+ }
3128
+ function boundsError(value, length, type) {
3129
+ if (Math.floor(value) !== value) {
3130
+ validateNumber(value, type);
3131
+ throw new errors.ERR_OUT_OF_RANGE(type || "offset", "an integer", value);
3132
+ }
3133
+ if (length < 0) throw new errors.ERR_BUFFER_OUT_OF_BOUNDS();
3134
+ throw new errors.ERR_OUT_OF_RANGE(type || "offset", `>= ${type ? 1 : 0} and <= ${length}`, value);
3135
+ }
3136
+ var INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g;
3137
+ function base64clean(str) {
3138
+ str = str.split("=")[0];
3139
+ str = str.trim().replace(INVALID_BASE64_RE, "");
3140
+ if (str.length < 2) return "";
3141
+ while (str.length % 4 !== 0) str = str + "=";
3142
+ return str;
3143
+ }
3144
+ function utf8ToBytes(string, units) {
3145
+ units = units || Infinity;
3146
+ let codePoint;
3147
+ const length = string.length;
3148
+ let leadSurrogate = null;
3149
+ const bytes = [];
3150
+ for (let i = 0; i < length; ++i) {
3151
+ codePoint = string.charCodeAt(i);
3152
+ if (codePoint > 55295 && codePoint < 57344) {
3153
+ if (!leadSurrogate) {
3154
+ if (codePoint > 56319) {
3155
+ if ((units -= 3) > -1) bytes.push(239, 191, 189);
3156
+ continue;
3157
+ } else if (i + 1 === length) {
3158
+ if ((units -= 3) > -1) bytes.push(239, 191, 189);
3159
+ continue;
3160
+ }
3161
+ leadSurrogate = codePoint;
3162
+ continue;
3163
+ }
3164
+ if (codePoint < 56320) {
3165
+ if ((units -= 3) > -1) bytes.push(239, 191, 189);
3166
+ leadSurrogate = codePoint;
3167
+ continue;
3168
+ }
3169
+ codePoint = (leadSurrogate - 55296 << 10 | codePoint - 56320) + 65536;
3170
+ } else if (leadSurrogate) {
3171
+ if ((units -= 3) > -1) bytes.push(239, 191, 189);
3172
+ }
3173
+ leadSurrogate = null;
3174
+ if (codePoint < 128) {
3175
+ if ((units -= 1) < 0) break;
3176
+ bytes.push(codePoint);
3177
+ } else if (codePoint < 2048) {
3178
+ if ((units -= 2) < 0) break;
3179
+ bytes.push(codePoint >> 6 | 192, codePoint & 63 | 128);
3180
+ } else if (codePoint < 65536) {
3181
+ if ((units -= 3) < 0) break;
3182
+ bytes.push(codePoint >> 12 | 224, codePoint >> 6 & 63 | 128, codePoint & 63 | 128);
3183
+ } else if (codePoint < 1114112) {
3184
+ if ((units -= 4) < 0) break;
3185
+ bytes.push(codePoint >> 18 | 240, codePoint >> 12 & 63 | 128, codePoint >> 6 & 63 | 128, codePoint & 63 | 128);
3186
+ } else throw new Error("Invalid code point");
3187
+ }
3188
+ return bytes;
3189
+ }
3190
+ function asciiToBytes(str) {
3191
+ const byteArray = [];
3192
+ for (let i = 0; i < str.length; ++i) byteArray.push(str.charCodeAt(i) & 255);
3193
+ return byteArray;
3194
+ }
3195
+ function utf16leToBytes(str, units) {
3196
+ let c, hi, lo;
3197
+ const byteArray = [];
3198
+ for (let i = 0; i < str.length; ++i) {
3199
+ if ((units -= 2) < 0) break;
3200
+ c = str.charCodeAt(i);
3201
+ hi = c >> 8;
3202
+ lo = c % 256;
3203
+ byteArray.push(lo);
3204
+ byteArray.push(hi);
3205
+ }
3206
+ return byteArray;
3207
+ }
3208
+ function base64ToBytes(str) {
3209
+ return base64.toByteArray(base64clean(str));
3210
+ }
3211
+ function blitBuffer(src, dst, offset, length) {
3212
+ let i;
3213
+ for (i = 0; i < length; ++i) {
3214
+ if (i + offset >= dst.length || i >= src.length) break;
3215
+ dst[i + offset] = src[i];
3216
+ }
3217
+ return i;
3218
+ }
3219
+ function isInstance(obj, type) {
3220
+ return obj instanceof type || obj != null && obj.constructor != null && obj.constructor.name != null && obj.constructor.name === type.name;
3221
+ }
3222
+ function numberIsNaN(obj) {
3223
+ return obj !== obj;
3224
+ }
3225
+ var hexSliceLookupTable = (function() {
3226
+ const alphabet = "0123456789abcdef";
3227
+ const table = new Array(256);
3228
+ for (let i = 0; i < 16; ++i) {
3229
+ const i16 = i * 16;
3230
+ for (let j = 0; j < 16; ++j) table[i16 + j] = alphabet[i] + alphabet[j];
3231
+ }
3232
+ return table;
3233
+ })();
3234
+ function defineBigIntMethod(fn) {
3235
+ return typeof BigInt === "undefined" ? BufferBigIntNotDefined : fn;
3236
+ }
3237
+ function BufferBigIntNotDefined() {
3238
+ throw new Error("BigInt not supported");
3239
+ }
3240
+ }));
3241
+ var import_lib = require_lib();
3242
+ var import_buffer = require_buffer();
3243
+ var TiledParser = class TiledParser {
3244
+ constructor(xml, filePath = "", basePath = "") {
3245
+ this.xml = xml;
3246
+ this.filePath = filePath;
3247
+ this.basePath = basePath;
3248
+ this.layers = /* @__PURE__ */ new Map();
3249
+ this.transform = (obj) => {
3250
+ if (!obj) return;
3251
+ const attr = obj.attributes || obj._attributes;
3252
+ if (!attr) return obj;
3253
+ let newObj = {
3254
+ ...obj,
3255
+ ...attr,
3256
+ ...TiledParser.propToNumber(attr, [
3257
+ "version",
3258
+ "width",
3259
+ "height",
3260
+ "tilewidth",
3261
+ "tileheight",
3262
+ "nextlayerid",
3263
+ "nextobjectid",
3264
+ "hexsidelength",
3265
+ "opacity",
3266
+ "x",
3267
+ "y",
3268
+ "offsetx",
3269
+ "offsety",
3270
+ "startx",
3271
+ "starty",
3272
+ "id",
3273
+ "firstgid",
3274
+ "imageheight",
3275
+ "imagewidth",
3276
+ "margin",
3277
+ "columns",
3278
+ "rows",
3279
+ "tilecount",
3280
+ "rotation",
3281
+ "gid",
3282
+ "tileid",
3283
+ "duration",
3284
+ "parallaxx",
3285
+ "parallaxy",
3286
+ "repeatx",
3287
+ "repeaty",
3288
+ "pixelsize"
3289
+ ]),
3290
+ ...TiledParser.propToBool(attr, [
3291
+ "visible",
3292
+ "infinite",
3293
+ "locked",
3294
+ "bold",
3295
+ "italic",
3296
+ "kerning",
3297
+ "strikeout",
3298
+ "underline",
3299
+ "wrap"
3300
+ ])
3301
+ };
3302
+ if (newObj.properties) {
3303
+ const properties = TiledParser.toArray(newObj.properties.property);
3304
+ const propObj = {};
3305
+ for (let prop of properties) {
3306
+ const attr = prop._attributes;
3307
+ if (!attr) continue;
3308
+ let val;
3309
+ switch (attr.type) {
3310
+ case "file":
3311
+ val = this.getImagePath(attr.value);
3312
+ break;
3313
+ case "object":
3314
+ case "float":
3315
+ case "int":
3316
+ val = +attr.value;
3317
+ break;
3318
+ case "bool":
3319
+ val = attr.value == "true" ? true : false;
3320
+ break;
3321
+ case "class":
3322
+ val = {
3323
+ ...this.transform(prop)?.properties ?? {},
3324
+ _classname: attr.propertytype
3325
+ };
3326
+ break;
3327
+ default: val = attr.value;
3328
+ }
3329
+ propObj[attr.name] = val;
3330
+ }
3331
+ newObj.properties = propObj;
3332
+ }
3333
+ if (newObj.polygon) newObj.polygon = this.transform(newObj.polygon);
3334
+ if (newObj.polyline) newObj.polyline = this.transform(newObj.polyline);
3335
+ if (newObj.points) newObj = newObj.points.split(" ").map((point) => {
3336
+ const pos = point.split(",");
3337
+ return {
3338
+ x: +pos[0],
3339
+ y: +pos[1]
3340
+ };
3341
+ });
3342
+ if (newObj.point) newObj.point = true;
3343
+ if (newObj.ellipse) newObj.ellipse = true;
3344
+ if (newObj.text) {
3345
+ newObj.text = {
3346
+ text: newObj.text._text,
3347
+ ...this.transform(newObj.text)
3348
+ };
3349
+ delete newObj.text._text;
3350
+ }
3351
+ if (newObj.image) newObj.image = this.transform(newObj.image);
3352
+ if (newObj.source) {
3353
+ if (!this.isTilesetSource(newObj)) newObj.source = this.getImagePath(newObj.source);
3354
+ }
3355
+ const objectgroup = newObj.object || newObj.objectgroup?.object;
3356
+ if (objectgroup) newObj.objects = TiledParser.toArray(objectgroup).map((object) => {
3357
+ return this.transform(object);
3358
+ });
3359
+ delete newObj._attributes;
3360
+ delete newObj.attributes;
3361
+ delete newObj.object;
3362
+ delete newObj.objectgroup;
3363
+ return newObj;
3364
+ };
3365
+ }
3366
+ static {
3367
+ this.propToNumber = (obj, props) => {
3368
+ for (let key of props) if (obj[key] !== void 0) obj[key] = +obj[key];
3369
+ return obj;
3370
+ };
3371
+ }
3372
+ static {
3373
+ this.propToBool = (obj, props) => {
3374
+ for (let key of props) if (obj[key] !== void 0) obj[key] = obj[key] == "true" || obj[key] == "1";
3375
+ return obj;
3376
+ };
3377
+ }
3378
+ static toArray(prop) {
3379
+ if (!prop) return [];
3380
+ if (!Array.isArray(prop)) return [prop];
3381
+ return prop;
3382
+ }
3383
+ getImagePath(image) {
3384
+ if (this.filePath.startsWith("http")) return new URL(image, this.filePath).href;
3385
+ return joinPath(this.basePath, image);
3386
+ }
3387
+ /**
3388
+ * Check if the object is a tileset source reference
3389
+ * Tileset sources should not have their paths transformed with getImagePath
3390
+ */
3391
+ isTilesetSource(obj) {
3392
+ return obj.firstgid !== void 0 || obj.tilewidth !== void 0 || obj.tileheight !== void 0 || obj.tilecount !== void 0 || obj.columns !== void 0;
3393
+ }
3394
+ static unpackTileBytes(buffer, size) {
3395
+ const expectedCount = size * 4;
3396
+ if (buffer.length !== expectedCount) throw new Error("Expected " + expectedCount + " bytes of tile data; received " + buffer.length);
3397
+ let tileIndex = 0;
3398
+ const array = [];
3399
+ for (let i = 0; i < expectedCount; i += 4) {
3400
+ array[tileIndex] = buffer.readUInt32LE(i);
3401
+ tileIndex++;
3402
+ }
3403
+ return array;
3404
+ }
3405
+ static decode(obj, size) {
3406
+ const { encoding, data } = obj;
3407
+ if (encoding == "base64") return TiledParser.unpackTileBytes(import_buffer.Buffer.from(data.trim(), "base64"), size);
3408
+ else if (encoding == "csv") return data.trim().split(",").map((x) => +x);
3409
+ return data;
3410
+ }
3411
+ parseMap() {
3412
+ const json = (0, import_lib.xml2js)(this.xml, { compact: true });
3413
+ const jsonNoCompact = (0, import_lib.xml2js)(this.xml);
3414
+ const tileset = json.map.tileset;
3415
+ json.map.group;
3416
+ const recursiveObjectGroup = (obj) => {
3417
+ const { objectgroup, group, layer, imagelayer } = obj;
3418
+ const setLayer = (type) => {
3419
+ if (!type) return;
3420
+ TiledParser.toArray(type).forEach((val) => {
3421
+ if (this.layers.has(+val._attributes.id)) throw new Error(`Tiled Parser Error: Layer with id ${val._attributes.id} already exists`);
3422
+ this.layers.set(+val._attributes.id, val);
3423
+ });
3424
+ };
3425
+ setLayer(objectgroup);
3426
+ setLayer(layer);
3427
+ setLayer(group);
3428
+ setLayer(imagelayer);
3429
+ if (group) recursiveObjectGroup(group);
3430
+ };
3431
+ recursiveObjectGroup(json.map);
3432
+ const recursiveLayer = (elements, array = []) => {
3433
+ if (!elements) return array;
3434
+ for (let element of elements) {
3435
+ const { name } = element;
3436
+ if (![
3437
+ "layer",
3438
+ "group",
3439
+ "imagelayer",
3440
+ "objectgroup"
3441
+ ].includes(name)) continue;
3442
+ const data = element.elements?.find((el) => el.name == "data");
3443
+ element.layer = this.layers.get(+element.attributes.id);
3444
+ const obj = {
3445
+ ...this.transform(data) ?? {},
3446
+ ...this.transform(element),
3447
+ ...this.transform(element.layer),
3448
+ layers: recursiveLayer(element.elements),
3449
+ data: data ? data.elements[0].text : void 0,
3450
+ type: name == "layer" ? "tilelayer" : name
3451
+ };
3452
+ delete obj.elements;
3453
+ delete obj.layer;
3454
+ if (obj.data) obj.data = TiledParser.decode(obj, obj.width * obj.height);
3455
+ array.push(obj);
3456
+ }
3457
+ return array;
3458
+ };
3459
+ const layers = recursiveLayer(jsonNoCompact.elements[0].elements);
3460
+ const tilesets = TiledParser.toArray(tileset).map((tileset) => {
3461
+ return this.transform(tileset);
3462
+ });
3463
+ const ret = {
3464
+ ...this.transform(json.map),
3465
+ layers,
3466
+ tilesets
3467
+ };
3468
+ delete ret.layer;
3469
+ delete ret.tileset;
3470
+ delete ret.group;
3471
+ delete ret.imagelayer;
3472
+ return ret;
3473
+ }
3474
+ parseTileset() {
3475
+ const { tileset } = (0, import_lib.xml2js)(this.xml, { compact: true });
3476
+ const ret = {
3477
+ ...this.transform(tileset),
3478
+ image: this.transform(tileset.image),
3479
+ tiles: TiledParser.toArray(tileset.tile).map((tile) => {
3480
+ const ret = this.transform(tile);
3481
+ if (tile.animation) ret.animations = TiledParser.toArray(tile.animation.frame).map(this.transform);
3482
+ delete ret.animation;
3483
+ return ret;
3484
+ })
3485
+ };
3486
+ delete ret.tile;
3487
+ return ret;
3488
+ }
13
3489
  };
3490
+ var TiledProperties = class {
3491
+ constructor(data) {
3492
+ this.properties = {};
3493
+ this.properties = data?.properties ?? {};
3494
+ }
3495
+ getProperty(name, defaultValue) {
3496
+ const val = this.properties[name];
3497
+ if (val === void 0) return defaultValue;
3498
+ return val;
3499
+ }
3500
+ hasProperty(name) {
3501
+ return !!this.properties[name];
3502
+ }
3503
+ setProperty(name, value) {
3504
+ this.properties[name] = value;
3505
+ }
3506
+ getType() {
3507
+ return this.class || this["type"];
3508
+ }
3509
+ };
3510
+ var FLIPPED_HORIZONTALLY_FLAG = 2147483648;
3511
+ var FLIPPED_VERTICALLY_FLAG = 1073741824;
3512
+ var FLIPPED_DIAGONALLY_FLAG = 536870912;
3513
+ var ROTATED_HEXAGONAL_120_FLAG = 268435456;
3514
+ var TileGid = class TileGid extends TiledProperties {
3515
+ constructor(obj) {
3516
+ super(obj);
3517
+ this.obj = obj;
3518
+ this._gid = obj?.gid;
3519
+ }
3520
+ static getRealGid(gid) {
3521
+ return gid & 268435455;
3522
+ }
3523
+ get horizontalFlip() {
3524
+ return !!(this._gid & FLIPPED_HORIZONTALLY_FLAG);
3525
+ }
3526
+ get verticalFlip() {
3527
+ return !!(this._gid & FLIPPED_VERTICALLY_FLAG);
3528
+ }
3529
+ get diagonalFlip() {
3530
+ return !!(this._gid & FLIPPED_DIAGONALLY_FLAG);
3531
+ }
3532
+ get rotatedHex120() {
3533
+ return !!(this._gid & ROTATED_HEXAGONAL_120_FLAG);
3534
+ }
3535
+ get gid() {
3536
+ return TileGid.getRealGid(this._gid);
3537
+ }
3538
+ set gid(val) {
3539
+ this._gid = val;
3540
+ }
3541
+ };
3542
+ var Tile = class extends TileGid {
3543
+ constructor(tile) {
3544
+ super(tile);
3545
+ this.tile = tile;
3546
+ const preservedProperties = this.properties;
3547
+ Reflect.deleteProperty(tile, "gid");
3548
+ Object.assign(this, tile);
3549
+ if (preservedProperties && Object.keys(preservedProperties).length > 0) this.properties = {
3550
+ ...preservedProperties,
3551
+ ...this.properties
3552
+ };
3553
+ }
3554
+ };
3555
+ var TiledObjectClass = class extends TileGid {
3556
+ constructor(object) {
3557
+ super(object);
3558
+ this.layerName = "";
3559
+ Object.assign(this, object);
3560
+ if (object?.gid) this.y -= this.height;
3561
+ }
3562
+ };
3563
+ var Layer = class Layer extends TiledProperties {
3564
+ get size() {
3565
+ return this.data.length;
3566
+ }
3567
+ constructor(layer, tilesets, parent) {
3568
+ super(layer);
3569
+ this.tilesets = tilesets;
3570
+ this.parent = parent;
3571
+ this.cacheTiles = false;
3572
+ this.tiles = [];
3573
+ Object.assign(this, layer);
3574
+ this.mapObjects();
3575
+ this.mergePropertiesWithParent();
3576
+ this.cacheTiles = this.getProperty("cache-tiles", false);
3577
+ if (this.cacheTiles) this.propertiesTiles();
3578
+ }
3579
+ createTile(gid, tileIndex, layerIndex) {
3580
+ if (gid == 0) return;
3581
+ const realGid = TileGid.getRealGid(gid);
3582
+ const tileset = Layer.findTileSet(realGid, this.tilesets);
3583
+ if (!tileset) return;
3584
+ const tile = tileset.getTile(realGid - tileset.firstgid);
3585
+ if (tile) return new Tile({
3586
+ ...tile.tile,
3587
+ gid,
3588
+ index: tileIndex,
3589
+ layerIndex
3590
+ });
3591
+ return new Tile({
3592
+ gid,
3593
+ index: tileIndex,
3594
+ layerIndex
3595
+ });
3596
+ }
3597
+ mergePropertiesWithParent() {
3598
+ const parent = this.getLayerParent();
3599
+ if (!this.properties) this.properties = {};
3600
+ if (!parent) return;
3601
+ for (let key in parent.properties) {
3602
+ const val = parent.properties[key];
3603
+ if (this.properties[key] === void 0) this.properties[key] = val;
3604
+ else if (key == "z") this.properties[key] += val;
3605
+ else continue;
3606
+ }
3607
+ this.opacity = Math.round((parent.opacity ?? 1) * (this.opacity ?? 1) * 100) / 100;
3608
+ this.offsetx = (parent.offsetx ?? 0) + (this.offsetx ?? 0);
3609
+ this.offsety = (parent.offsety ?? 0) + (this.offsety ?? 0);
3610
+ this.locked = parent.locked ?? false;
3611
+ }
3612
+ propertiesTiles() {
3613
+ if (!this.data) return;
3614
+ const data = this.data;
3615
+ for (let i = 0; i < data.length; i++) {
3616
+ const id = data[i];
3617
+ this.tiles.push(this.createTile(id, i));
3618
+ }
3619
+ }
3620
+ mapObjects() {
3621
+ if (this.objects) this.objects = this.objects.map((object) => {
3622
+ const obj = new TiledObjectClass(object);
3623
+ obj.layerName = this.name;
3624
+ return obj;
3625
+ });
3626
+ }
3627
+ getTileByIndex(tileIndex) {
3628
+ if (this.cacheTiles) return this.tiles[tileIndex];
3629
+ return this.createTile(this.data[tileIndex], tileIndex);
3630
+ }
3631
+ static findTileSet(gid, tileSets) {
3632
+ let tileset;
3633
+ for (let i = tileSets.length - 1; i >= 0; i--) {
3634
+ tileset = tileSets[i];
3635
+ if (tileset.firstgid && tileset.firstgid <= gid) break;
3636
+ }
3637
+ return tileset;
3638
+ }
3639
+ getLayerParent() {
3640
+ return this.parent;
3641
+ }
3642
+ tilesForEach(cb) {
3643
+ for (let i = 0; i < this.data.length; i++) {
3644
+ if (this.cacheTiles) {
3645
+ cb(this.tiles[i], i);
3646
+ continue;
3647
+ }
3648
+ cb(this.createTile(this.data[i], i), i);
3649
+ }
3650
+ }
3651
+ setData(tileIndex, gid) {
3652
+ this.data[tileIndex] = gid;
3653
+ }
3654
+ };
3655
+ var Tileset = class extends TiledProperties {
3656
+ constructor(tileset) {
3657
+ super(tileset);
3658
+ this.tileset = tileset;
3659
+ this.cacheTileId = /* @__PURE__ */ new Map();
3660
+ Object.assign(this, tileset);
3661
+ this.margin = this.margin ?? 0;
3662
+ this.spacing = this.spacing ?? 0;
3663
+ const tilesArray = tileset.tiles || tileset.tile || [];
3664
+ for (let tile of tilesArray) this.addTile(tile);
3665
+ Reflect.deleteProperty(this, "tiles");
3666
+ Reflect.deleteProperty(this, "tile");
3667
+ }
3668
+ addTile(tileObj) {
3669
+ const tile = new Tile(tileObj);
3670
+ this.cacheTileId.set(tile.id, tile);
3671
+ return tile;
3672
+ }
3673
+ getTile(id) {
3674
+ return this.cacheTileId.get(+id);
3675
+ }
3676
+ };
3677
+ var bufferTilesets = {};
3678
+ var MapClass = class extends TiledProperties {
3679
+ constructor(map) {
3680
+ super(map ?? {});
3681
+ this.tilesets = [];
3682
+ this.layers = [];
3683
+ this.tmpLayers = [];
3684
+ this.tilesIndex = {};
3685
+ this.allocateMemory = 0;
3686
+ this.lowMemory = false;
3687
+ if (map) this.load(map);
3688
+ }
3689
+ load(map) {
3690
+ Object.assign(this, map);
3691
+ if (this.hasProperty("low-memory")) this.lowMemory = this.getProperty("low-memory", false);
3692
+ this.tmpLayers = [];
3693
+ this.mapTilesets();
3694
+ this.mapLayers(this.layers);
3695
+ this.layers = [...this.tmpLayers];
3696
+ Reflect.deleteProperty(this, "tmpLayers");
3697
+ this.setTilesIndex();
3698
+ this.data = map;
3699
+ }
3700
+ /**
3701
+ * @title Width of the map in pixels
3702
+ * @prop {number} [widthPx]
3703
+ * @readonly
3704
+ * @memberof Map
3705
+ * @memberof RpgSceneMap
3706
+ * */
3707
+ get widthPx() {
3708
+ return this.width * this.tilewidth;
3709
+ }
3710
+ /**
3711
+ * @title Height of the map in pixels
3712
+ * @prop {number} [heightPx]
3713
+ * @readonly
3714
+ * @memberof Map
3715
+ * @memberof RpgSceneMap
3716
+ * */
3717
+ get heightPx() {
3718
+ return this.height * this.tileheight;
3719
+ }
3720
+ /**
3721
+ * @title The depth of the map in pixels (this is the height of a tile ;))
3722
+ * @prop {number} map.zTileHeight
3723
+ * @readonly
3724
+ * @memberof Map
3725
+ * @memberof RpgSceneMap
3726
+ * */
3727
+ get zTileHeight() {
3728
+ return this.tileheight;
3729
+ }
3730
+ /**
3731
+ * Find a layer by name. Returns `undefined` is the layer is not found
3732
+
3733
+ * @title Get Layer by name
3734
+ * @method map.getLayerByName(name)
3735
+ * @param {string} name layer name
3736
+ * @returns {LayerInfo | undefined}
3737
+ * @example
3738
+ * ```ts
3739
+ * const tiles = map.getLayerByName(0, 0)
3740
+ * ```
3741
+ * @memberof Map
3742
+ * @memberof RpgSceneMap
3743
+ */
3744
+ getLayerByName(name) {
3745
+ return this.layers.find((layer) => layer.name == name);
3746
+ }
3747
+ /**
3748
+ * Get the tile index on the tileset
3749
+ *
3750
+ * @title Get index of tile
3751
+ * @method map.getTileIndex(x,y)
3752
+ * @param {number} x Position X
3753
+ * @param {number} x Position Y
3754
+ * @returns {number}
3755
+ * @memberof Map
3756
+ * @memberof RpgSceneMap
3757
+ */
3758
+ getTileIndex(x, y, [z] = [0]) {
3759
+ return this.width * Math.floor((y - z) / this.tileheight) + Math.floor(x / this.tilewidth);
3760
+ }
3761
+ getTilePosition(index) {
3762
+ return {
3763
+ y: Math.floor(index / this.width) * this.tileheight,
3764
+ x: index % this.width * this.tilewidth
3765
+ };
3766
+ }
3767
+ /**
3768
+ * Find the point of origin (top left) of a tile. Of course, its position depends on the size of the tile
3769
+
3770
+ * @title Get origin position of tile
3771
+ * @method map.getTileOriginPosition(x,y)
3772
+ * @param {number} x Position X
3773
+ * @param {number} x Position Y
3774
+ * @returns { {x: number, y: number }}
3775
+ * @example
3776
+ * ```ts
3777
+ * // If the size of a tile is 32x32px
3778
+ * const position = map.getTileOriginPosition(35, 12)
3779
+ * console.log(position) // { x: 32, y: 0 }
3780
+ * ```
3781
+ * @memberof Map
3782
+ * @memberof RpgSceneMap
3783
+ */
3784
+ getTileOriginPosition(x, y) {
3785
+ return {
3786
+ x: Math.floor(x / this.tilewidth) * this.tilewidth,
3787
+ y: Math.floor(y / this.tileheight) * this.tileheight
3788
+ };
3789
+ }
3790
+ /**
3791
+ * Recover tiles according to a position
3792
+
3793
+ * @title Get tile by position
3794
+ * @method map.getTileByPosition(x,y)
3795
+ * @param {number} x Position X
3796
+ * @param {number} x Position Y
3797
+ * @returns {TileInfo}
3798
+ * @example
3799
+ * ```ts
3800
+ * const tiles = map.getTileByPosition(0, 0)
3801
+ * ```
3802
+ * @memberof Map
3803
+ * @memberof RpgSceneMap
3804
+ */
3805
+ getTileByPosition(x, y, z = [0, 0], options = {}) {
3806
+ const tileIndex = this.getTileIndex(x, y, [z[0]]);
3807
+ return this.getTileByIndex(tileIndex, z, options);
3808
+ }
3809
+ /**
3810
+ * Retrieves tiles according to its index
3811
+
3812
+ * @title Get tile by index
3813
+ * @method map.getTileByIndex(tileIndex)
3814
+ * @param {number} tileIndex tile index
3815
+ * @returns {TileInfo}
3816
+ * @example
3817
+ * ```ts
3818
+ * const index = map.getTileIndex(0, 0)
3819
+ * const tiles = map.getTileByIndex(index)
3820
+ * ```
3821
+ * @memberof Map
3822
+ * @memberof RpgSceneMap
3823
+ */
3824
+ getTileByIndex(tileIndex, zPlayer = [0, 0], options = { populateTiles: true }) {
3825
+ const zA = Math.floor(zPlayer[0] / this.zTileHeight);
3826
+ Math.floor(zPlayer[1] / this.zTileHeight);
3827
+ const level = this.tilesIndex[zA];
3828
+ const obj = {
3829
+ tiles: [],
3830
+ hasCollision: false,
3831
+ isOverlay: false,
3832
+ objectGroups: [],
3833
+ tileIndex
3834
+ };
3835
+ if (!level) return obj;
3836
+ const [layer] = this.layers;
3837
+ const getTileByPointer = (pointer = 0) => {
3838
+ const pos = tileIndex * this.realAllocateMemory + pointer;
3839
+ const gid = level[pos];
3840
+ if (gid === 0) return obj;
3841
+ const tile = layer.createTile(gid, tileIndex, level[pos + 1]);
3842
+ if (tile) obj.tiles.push(tile);
3843
+ };
3844
+ if (options.populateTiles) for (let i = 0; i < this.realAllocateMemory; i += 2) getTileByPointer(i);
3845
+ else getTileByPointer();
3846
+ const [tile] = obj.tiles;
3847
+ if (tile) {
3848
+ obj.hasCollision = tile.getProperty("collision", false);
3849
+ obj.objectGroups = tile.objects ?? [];
3850
+ }
3851
+ return obj;
3852
+ }
3853
+ getAllObjects() {
3854
+ return this.layers.reduce((prev, current) => {
3855
+ if (!current.objects) return prev;
3856
+ return prev.concat(...current.objects);
3857
+ }, []);
3858
+ }
3859
+ getData() {
3860
+ return {
3861
+ ...this.data,
3862
+ layers: this.layers
3863
+ };
3864
+ }
3865
+ setTile(x, y, layerFilter, tileInfo) {
3866
+ if (this.lowMemory) throw "Impossible to change a tile with the lowMemory option";
3867
+ const tileIndex = this.getTileIndex(x, y);
3868
+ let fnFilter;
3869
+ let tilesEdited = {};
3870
+ if (typeof layerFilter == "string") fnFilter = (layer) => layer.name == layerFilter;
3871
+ else fnFilter = layerFilter;
3872
+ for (let i = 0; i < this.layers.length; i++) {
3873
+ const layer = this.layers[i];
3874
+ if (!fnFilter(layer)) continue;
3875
+ let tile;
3876
+ const oldTile = this.getTileByIndex(tileIndex);
3877
+ if (tileInfo.gid) tile = layer.createTile(tileInfo.gid, tileIndex);
3878
+ if (!tile) continue;
3879
+ for (let key in tileInfo) {
3880
+ if (key == "gid") continue;
3881
+ tile[key] = tileInfo[key];
3882
+ }
3883
+ tilesEdited[layer.name] = {
3884
+ gid: tile.gid,
3885
+ properties: tile.properties
3886
+ };
3887
+ this.setTileIndex(layer, oldTile.tiles[0], tile, tileIndex, i);
3888
+ layer.setData(tileIndex, tile.gid);
3889
+ }
3890
+ return {
3891
+ x,
3892
+ y,
3893
+ tiles: tilesEdited
3894
+ };
3895
+ }
3896
+ removeCacheTileset(name) {
3897
+ delete bufferTilesets[name];
3898
+ }
3899
+ clearCacheTilesets() {
3900
+ bufferTilesets = {};
3901
+ }
3902
+ mapTilesets() {
3903
+ this.tilesets = this.tilesets.map((tileset) => {
3904
+ if (bufferTilesets[tileset.name]) {
3905
+ const instance = bufferTilesets[tileset.name];
3906
+ instance.firstgid = tileset.firstgid;
3907
+ return instance;
3908
+ }
3909
+ const _tileset = new Tileset(tileset);
3910
+ bufferTilesets[_tileset.name] = _tileset;
3911
+ return _tileset;
3912
+ });
3913
+ }
3914
+ mapLayers(layers = [], parent) {
3915
+ for (let layer of layers) {
3916
+ const layerInstance = new Layer(layer, this.tilesets, parent);
3917
+ this.tmpLayers.push(layerInstance);
3918
+ if (layer.layers) this.mapLayers(layer.layers, layerInstance);
3919
+ }
3920
+ if (this.lowMemory) this.allocateMemory = 1;
3921
+ if (!this.allocateMemory) this.allocateMemory = this.layers.length;
3922
+ }
3923
+ setTileIndex(layer, oldTile, newTile, tileIndex, layerIndex) {
3924
+ const startPos = tileIndex * this.realAllocateMemory;
3925
+ let pointer = startPos + this.realAllocateMemory - 2;
3926
+ let z = layer.getProperty("z", 0) + oldTile.getProperty("z", 0);
3927
+ while (pointer >= startPos) {
3928
+ const zlayer = this.tilesIndex[z];
3929
+ if (zlayer[pointer] === oldTile.gid && zlayer[pointer + 1] === layerIndex) this.tilesIndex[z][pointer] = newTile.gid;
3930
+ pointer -= 2;
3931
+ }
3932
+ }
3933
+ /**
3934
+ * We multiply by 2 because 2 entries are stored for a tile: its GID and the Layer Index
3935
+ *
3936
+ * Example If I have 3 layers, The array will have the following form
3937
+ *
3938
+ * [
3939
+ * GID of Layer 3,
3940
+ * Layer Index of Layer 3,
3941
+ * GID of Layer 2,
3942
+ * Layer Index of Layer 2,
3943
+ * GID of Layer 1,
3944
+ * Layer Index of Layer 1,
3945
+ * ... others tiles
3946
+ * ]
3947
+ *
3948
+ * The size in memory of the map is therefore:
3949
+ *
3950
+ * `(map width * map height * number of layers * 4) bytes`
3951
+ *
3952
+ * > We multiply by 4, because an element takes 2 bytes and has 2 elements for a tile is 4 bytes in all
3953
+ *
3954
+ * Example (a 100x100 map with 5 layers)
3955
+ *
3956
+ * `100 * 100 * 5 * 4 = 200000 bytes = ~195 Kb`
3957
+ *
3958
+ * If we define on lowMemory then the calculation is the following
3959
+ *
3960
+ * `(map width * map height * 4) bytes`
3961
+ *
3962
+ * Example
3963
+ *
3964
+ * `100 * 100 * 4 = 40000 bytes = ~39 Kb`
3965
+ */
3966
+ get realAllocateMemory() {
3967
+ return this.allocateMemory * 2;
3968
+ }
3969
+ /**
3970
+ * We keep each tile in memory classified by z value. The values are ordered from the end to the beginning so that the first element of the array (when retrieved with getTileByIndex() is the tile on the highest layer. This way, the tile search is very fast for collisions
3971
+ *
3972
+ */
3973
+ addTileIndex(layer, tile, tileIndex, layerIndex) {
3974
+ if (!tile || tile && tile.gid == 0) return;
3975
+ let z = layer.getProperty("z", 0) + tile.getProperty("z", 0);
3976
+ if (!this.tilesIndex[z]) {
3977
+ const buffer = /* @__PURE__ */ new ArrayBuffer(layer.size * this.realAllocateMemory * 2);
3978
+ this.tilesIndex[z] = new Uint16Array(buffer);
3979
+ }
3980
+ const startPos = tileIndex * this.realAllocateMemory;
3981
+ let pointer = startPos + this.realAllocateMemory - 2;
3982
+ while (this.tilesIndex[z][pointer] !== 0 && pointer > startPos) pointer -= 2;
3983
+ this.tilesIndex[z][pointer] = tile.gid;
3984
+ this.tilesIndex[z][pointer + 1] = layerIndex;
3985
+ this.tilesIndex[z][startPos] = tile.gid;
3986
+ this.tilesIndex[z][startPos + 1] = layerIndex;
3987
+ }
3988
+ setTilesIndex() {
3989
+ for (let i = 0; i < this.layers.length; i++) {
3990
+ const layer = this.layers[i];
3991
+ if (layer.type != TiledLayerType.Tile) continue;
3992
+ layer.tilesForEach((tile, index) => {
3993
+ this.addTileIndex(layer, tile, index, i);
3994
+ });
3995
+ }
3996
+ }
3997
+ };
3998
+ //#endregion
3999
+ export { MapClass, TiledParser };