dredless 1.3.0 → 1.3.1

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.
@@ -341,6 +341,223 @@
341
341
  }
342
342
  });
343
343
 
344
+ // src/network/bundle-metadata.js
345
+ function quoted(source, start) {
346
+ const quote = source[start];
347
+ let value = "", valid = true, i = start + 1;
348
+ for (; i < source.length; i++) {
349
+ const c = source[i];
350
+ if (c === quote) return { value, valid, end: i + 1 };
351
+ if (c !== "\\") {
352
+ value += c;
353
+ if (c === "\n" || c === "\r") valid = false;
354
+ continue;
355
+ }
356
+ const escape = source[++i];
357
+ const simple = { n: "\n", r: "\r", t: " ", b: "\b", f: "\f", v: "\v", "0": "\0" };
358
+ if (escape === "\n") continue;
359
+ if (escape === "\r") {
360
+ if (source[i + 1] === "\n") i++;
361
+ continue;
362
+ }
363
+ if (escape === "x" || escape === "u") {
364
+ const braced = escape === "u" && source[i + 1] === "{";
365
+ const first = i + (braced ? 2 : 1);
366
+ const end = braced ? source.indexOf("}", first) : first + (escape === "x" ? 2 : 4);
367
+ const digits = source.slice(first, end);
368
+ if (end < first) return { value, valid: false, end: source.length };
369
+ if (!/^[\da-f]+$/i.test(digits) || Number.parseInt(digits, 16) > 1114111) valid = false;
370
+ else value += String.fromCodePoint(Number.parseInt(digits, 16));
371
+ i = braced ? end : end - 1;
372
+ } else if (escape === "0" && /\d/.test(source[i + 1] || "")) valid = false;
373
+ else if (escape in simple) value += simple[escape];
374
+ else if (escape && !/[1-9]/.test(escape)) value += escape;
375
+ else valid = false;
376
+ }
377
+ return { value, valid: false, end: i };
378
+ }
379
+ function skipRegex(source, start) {
380
+ let inClass = false, i = start + 1;
381
+ for (; i < source.length; i++) {
382
+ if (source[i] === "\\") {
383
+ i++;
384
+ continue;
385
+ }
386
+ if (source[i] === "[") inClass = true;
387
+ if (source[i] === "]") inClass = false;
388
+ if (source[i] === "/" && !inClass) {
389
+ while (/[a-z]/i.test(source[i + 1] || "")) i++;
390
+ return i + 1;
391
+ }
392
+ if (source[i] === "\n") break;
393
+ }
394
+ return i;
395
+ }
396
+ function skipTemplate(source, start) {
397
+ let i = start + 1;
398
+ while (i < source.length) {
399
+ if (source[i] === "\\") {
400
+ i += 2;
401
+ continue;
402
+ }
403
+ if (source[i] === "`") return i + 1;
404
+ if (source[i] === "$" && source[i + 1] === "{") {
405
+ const expression = lex(source, i + 2, true);
406
+ i = expression.end;
407
+ } else i++;
408
+ }
409
+ return i;
410
+ }
411
+ function lex(source, start = 0, interpolation = false) {
412
+ const tokens = [];
413
+ let i = start, depth = 0;
414
+ while (i < source.length) {
415
+ const c = source[i];
416
+ if (/\s/.test(c)) {
417
+ i++;
418
+ continue;
419
+ }
420
+ if (source.startsWith("//", i)) {
421
+ const end = source.indexOf("\n", i);
422
+ i = end < 0 ? source.length : end;
423
+ continue;
424
+ }
425
+ if (source.startsWith("/*", i)) {
426
+ const end = source.indexOf("*/", i + 2);
427
+ i = end < 0 ? source.length : end + 2;
428
+ continue;
429
+ }
430
+ let token;
431
+ if (c === '"' || c === "'") {
432
+ const string = quoted(source, i);
433
+ token = { kind: "string", value: string.value, valid: string.valid };
434
+ i = string.end;
435
+ } else if (c === "`") {
436
+ i = skipTemplate(source, i);
437
+ token = { kind: "opaque", value: "template" };
438
+ } else if (c === "/" && (!tokens.length || regexPrefix.has(tokens.at(-1).value))) {
439
+ i = skipRegex(source, i);
440
+ token = { kind: "opaque", value: "regex" };
441
+ } else if (/[A-Za-z_$]/.test(c)) {
442
+ const first = i++;
443
+ while (/[\w$]/.test(source[i] || "")) i++;
444
+ token = { kind: "identifier", value: source.slice(first, i) };
445
+ } else {
446
+ token = { kind: "punctuation", value: source.startsWith("=>", i) ? "=>" : c };
447
+ i += token.value.length;
448
+ }
449
+ if (interpolation && token.kind === "punctuation" && token.value === "}" && depth === 0) return { tokens, end: i };
450
+ if (token.value === "{" && token.kind === "punctuation") depth++;
451
+ if (token.value === "}" && token.kind === "punctuation") depth--;
452
+ tokens.push(token);
453
+ }
454
+ return { tokens, end: i };
455
+ }
456
+ function literal(tokens, start) {
457
+ const token = tokens[start];
458
+ if (token?.kind === "string" && token.valid) return { value: token.value, end: start + 1 };
459
+ if (token?.kind === "identifier" && ["true", "false"].includes(token.value)) return { value: token.value === "true", end: start + 1 };
460
+ if (token?.kind !== "punctuation" || token.value !== "[") return null;
461
+ const value = [];
462
+ let i = start + 1;
463
+ while (!(tokens[i]?.kind === "punctuation" && tokens[i].value === "]")) {
464
+ if (tokens[i]?.kind !== "string" || !tokens[i].valid) return null;
465
+ value.push(tokens[i++].value);
466
+ if (tokens[i]?.kind === "punctuation" && tokens[i].value === "]") break;
467
+ if (tokens[i++]?.value !== ",") return null;
468
+ }
469
+ return tokens[i]?.value === "]" ? { value, end: i + 1 } : null;
470
+ }
471
+ function getter(tokens, start) {
472
+ let i = start;
473
+ if (tokens[i++]?.value !== "(" || tokens[i++]?.value !== ")" || tokens[i++]?.value !== "=>") return null;
474
+ if (tokens[i]?.kind === "identifier" && identifier.test(tokens[i].value) && [",", "}"].includes(tokens[i + 1]?.value)) return tokens[i].value;
475
+ if (tokens[i++]?.value !== "{" || tokens[i++]?.value !== "return") return null;
476
+ const name = tokens[i++];
477
+ if (tokens[i]?.value === ";") i++;
478
+ return name?.kind === "identifier" && tokens[i]?.value === "}" ? name.value : null;
479
+ }
480
+ function readBundleExports(text2, names) {
481
+ const wanted = new Set(names), found = /* @__PURE__ */ new Map();
482
+ const scripts = /^\s*</.test(text2) ? [...text2.matchAll(/<script\b[^>]*>([\s\S]*?)<\/script\s*>/gi)].map((match) => match[1]) : [];
483
+ for (const source of scripts.length ? scripts : [text2]) {
484
+ const { tokens } = lex(source);
485
+ const root = /* @__PURE__ */ new Map(), scopes = [root];
486
+ const brackets = [];
487
+ for (const [index, token] of tokens.entries()) {
488
+ token.scope = scopes.at(-1);
489
+ if (token.kind !== "punctuation") continue;
490
+ if (["(", "[", "{"].includes(token.value)) brackets.push(index);
491
+ if ([")", "]", "}"].includes(token.value)) {
492
+ const opening = brackets.pop();
493
+ if (opening !== void 0) tokens[opening].close = index;
494
+ }
495
+ if (token.value === "{") {
496
+ token.child = /* @__PURE__ */ new Map();
497
+ scopes.push(token.child);
498
+ }
499
+ if (token.value === "}" && scopes.length > 1) scopes.pop();
500
+ }
501
+ for (let i = 0; i < tokens.length; i++) {
502
+ if (tokens[i].kind !== "identifier" || tokens[i].value !== "const") continue;
503
+ const scope = tokens[i].scope;
504
+ let j = i + 1;
505
+ while (tokens[j]?.kind === "identifier" && tokens[j + 1]?.value === "=") {
506
+ const name = tokens[j].value, result = literal(tokens, j + 2);
507
+ let end = j + 2;
508
+ while (end < tokens.length) {
509
+ const token = tokens[end];
510
+ if (token.scope === scope && token.kind === "punctuation" && [";", ",", "}"].includes(token.value)) break;
511
+ end = token.close !== void 0 ? token.close + 1 : end + 1;
512
+ }
513
+ const valid = result && result.end === end;
514
+ scope.set(name, scope.has(name) ? null : valid ? result.value : null);
515
+ if (tokens[end]?.value !== ",") break;
516
+ j = end + 1;
517
+ }
518
+ }
519
+ const record = (name, binding, scope) => {
520
+ if (!wanted.has(name)) return;
521
+ const value = binding ? scope.get(binding) : void 0;
522
+ if (value == null) throw new Error(`Unable to resolve bundle export ${name}: expected a constant literal in the same module`);
523
+ if (found.has(name) && JSON.stringify(found.get(name)) !== JSON.stringify(value)) throw new Error(`Ambiguous bundle export ${name}`);
524
+ found.set(name, value);
525
+ };
526
+ for (let i = 0; i < tokens.length; i++) {
527
+ if (tokens[i].kind !== "identifier") continue;
528
+ const method = tokens[i].value === "Object" && tokens[i + 1]?.value === "." ? tokens[i + 2]?.value : null;
529
+ const call = ["defineProperties", "defineProperty"].includes(method) ? i + 3 : i + 1;
530
+ if (tokens[call]?.value !== "(" || tokens[call + 1]?.kind !== "identifier" || tokens[call + 2]?.value !== ",") continue;
531
+ let j = call + 3;
532
+ if (method === "defineProperty") {
533
+ const name = tokens[j++];
534
+ if (name?.kind !== "string" || !wanted.has(name.value) || tokens[j++]?.value !== ",") continue;
535
+ const object = tokens[j++];
536
+ if (object?.value !== "{" || !object.child) continue;
537
+ for (; j < tokens.length && tokens[j].scope !== object.scope; j++) {
538
+ if (tokens[j].scope === object.child && tokens[j].value === "get" && tokens[j + 1]?.value === ":") record(name.value, getter(tokens, j + 2), tokens[i].scope);
539
+ }
540
+ } else {
541
+ const object = tokens[j++];
542
+ if (object?.value !== "{" || !object.child) continue;
543
+ for (; j < tokens.length && tokens[j].scope !== object.scope; j++) {
544
+ const key = tokens[j];
545
+ if (key.scope === object.child && ["identifier", "string"].includes(key.kind) && wanted.has(key.value) && tokens[j + 1]?.value === ":") record(key.value, getter(tokens, j + 2), tokens[i].scope);
546
+ }
547
+ }
548
+ }
549
+ }
550
+ for (const name of wanted) if (!found.has(name)) throw new Error(`Missing bundle export ${name}`);
551
+ return Object.fromEntries(found);
552
+ }
553
+ var identifier, regexPrefix;
554
+ var init_bundle_metadata = __esm({
555
+ "src/network/bundle-metadata.js"() {
556
+ identifier = /^[A-Za-z_$][\w$]*$/;
557
+ regexPrefix = /* @__PURE__ */ new Set(["(", "[", "{", "=", ":", ",", ";", "!", "?", "&", "|", "=>", "return", "throw", "case", "typeof", "void", "delete"]);
558
+ }
559
+ });
560
+
344
561
  // src/network/servers.js
345
562
  async function fetchServerStatus(server2, baseUrl = DEFAULT_BASE_URL) {
346
563
  const resolved = await resolveServer(server2, baseUrl);
@@ -412,7 +629,9 @@
412
629
  return Number(server2);
413
630
  }
414
631
  function parseGameVersion(text2) {
415
- return text2.match(/Object\.defineProperty\(exports,"GAME_VERSION",\{enumerable:true,get:\(\)=>e\}\);const e="([^"]+)"/)?.[1] || null;
632
+ const { GAME_VERSION } = readBundleExports(text2, ["GAME_VERSION"]);
633
+ if (typeof GAME_VERSION !== "string" || !GAME_VERSION.trim()) throw new Error("Invalid bundle export GAME_VERSION: expected a nonempty string");
634
+ return GAME_VERSION;
416
635
  }
417
636
  function parseNoticeVersion(text2) {
418
637
  const moduleMatch = text2.match(/Object\.defineProperty\(exports,"checkNotice"[\s\S]{0,8000}?setCookie\)\("notice_version",([A-Za-z_$][\w$]*)\)/);
@@ -436,24 +655,46 @@
436
655
  }
437
656
  function parseServerInfo(text2, baseUrl) {
438
657
  const host = hostName(baseUrl);
439
- const match = text2.match(/const n=(true|false);const \$="([^"]+)";const o="([^"]+)";const T="([^"]+)";const t="([^"]+)";const S=\[([^\]]+)\];const c=\[([^\]]+)\];const A="([^"]+)";const I="([^"]+)"/s);
440
- if (!match) return null;
441
- const [localDomain, baseDomain, testDomain, testGameDomain] = match.slice(2, 6);
442
- const domains = [...match[6].matchAll(/"((?:[^"\\]|\\.)*)"/g)].map((m) => m[1]);
443
- const names = [...match[7].matchAll(/"((?:[^"\\]|\\.)*)"/g)].map((m) => m[1]);
658
+ const config = readBundleExports(text2, [
659
+ "LOCAL_DOMAIN",
660
+ "BASE_DOMAIN",
661
+ "TEST_DOMAIN",
662
+ "TEST_GAME_DOMAIN",
663
+ "GAME_DOMAINS",
664
+ "GAME_SERVER_NAMES",
665
+ "MASTER_DOMAIN",
666
+ "TEST_MASTER_DOMAIN"
667
+ ]);
668
+ const domain = (value, name) => {
669
+ if (typeof value !== "string" || value.length > 253 || !/^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)*[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/i.test(value)) {
670
+ throw new Error(`Invalid bundle export ${name}: expected a hostname`);
671
+ }
672
+ return value;
673
+ };
674
+ const localDomain = domain(config.LOCAL_DOMAIN, "LOCAL_DOMAIN");
675
+ const baseDomain = domain(config.BASE_DOMAIN, "BASE_DOMAIN");
676
+ const testDomain = domain(config.TEST_DOMAIN, "TEST_DOMAIN");
677
+ const testGameDomain = domain(config.TEST_GAME_DOMAIN, "TEST_GAME_DOMAIN");
678
+ const domains = config.GAME_DOMAINS;
679
+ const names = config.GAME_SERVER_NAMES;
680
+ if (!Array.isArray(domains) || !domains.length) throw new Error("Invalid bundle export GAME_DOMAINS: expected nonempty hostname array");
681
+ domains.forEach((value) => domain(value, "GAME_DOMAINS"));
682
+ if (!Array.isArray(names) || names.length !== domains.length || names.some((name) => typeof name !== "string")) {
683
+ throw new Error("Invalid bundle export GAME_SERVER_NAMES: expected one string per game domain");
684
+ }
444
685
  const meta = {
445
686
  localDomain,
446
687
  baseDomain,
447
688
  testDomain,
448
689
  testGameDomain,
449
- masterDomain: match[8],
450
- testMasterDomain: match[9]
690
+ masterDomain: domain(config.MASTER_DOMAIN, "MASTER_DOMAIN"),
691
+ testMasterDomain: domain(config.TEST_MASTER_DOMAIN, "TEST_MASTER_DOMAIN")
451
692
  };
452
693
  if (host === localDomain) return { mode: "local", servers: [server(0, localDomain, "Local Test 1")], meta };
453
694
  if (host === testDomain) return { mode: "test", servers: [server(0, testGameDomain, "Public Test")], meta };
454
695
  return {
455
696
  mode: "production",
456
- servers: domains.map((domain, index) => server(index, domain, names[index] || `Server ${index + 1}`)),
697
+ servers: domains.map((domain2, index) => server(index, domain2, names[index] || `Server ${index + 1}`)),
457
698
  meta
458
699
  };
459
700
  }
@@ -473,6 +714,7 @@
473
714
  init_constants();
474
715
  init_runtime();
475
716
  init_http();
717
+ init_bundle_metadata();
476
718
  cache = /* @__PURE__ */ new Map();
477
719
  ServerDirectory = class {
478
720
  constructor(baseUrl = DEFAULT_BASE_URL) {
@@ -480,27 +722,38 @@
480
722
  this.meta = null;
481
723
  }
482
724
  async load() {
483
- if (cache.has(this.baseUrl)) {
484
- this.meta = await cache.get(this.baseUrl);
485
- return this;
725
+ if (!cache.has(this.baseUrl)) cache.set(this.baseUrl, this.#fetchMeta());
726
+ const pending = cache.get(this.baseUrl);
727
+ try {
728
+ this.meta = await pending;
729
+ } catch (error) {
730
+ if (cache.get(this.baseUrl) === pending) cache.delete(this.baseUrl);
731
+ throw error;
486
732
  }
487
- const promise = this.#fetchMeta();
488
- cache.set(this.baseUrl, promise);
489
- this.meta = await promise;
490
733
  return this;
491
734
  }
492
735
  async version() {
493
736
  await this.load();
494
- if (!this.meta.version) throw new Error("Unable to locate GAME_VERSION in drednot bundle");
495
- return this.meta.version;
737
+ try {
738
+ this.meta.version ??= parseGameVersion(this.meta.text);
739
+ return this.meta.version;
740
+ } catch (error) {
741
+ cache.delete(this.baseUrl);
742
+ throw error;
743
+ }
496
744
  }
497
745
  async list() {
498
746
  return this.servers();
499
747
  }
500
748
  async servers() {
501
749
  await this.load();
502
- if (!this.meta.serverInfo) throw new Error("Unable to locate server list in drednot bundle");
503
- return this.meta.serverInfo.servers;
750
+ try {
751
+ this.meta.serverInfo ??= parseServerInfo(this.meta.text, this.baseUrl);
752
+ return this.meta.serverInfo.servers;
753
+ } catch (error) {
754
+ cache.delete(this.baseUrl);
755
+ throw error;
756
+ }
504
757
  }
505
758
  async get(id = null) {
506
759
  return this.pick(id);
@@ -513,7 +766,7 @@
513
766
  const response = await new HttpClient({ baseUrl: this.baseUrl }).request("index.html");
514
767
  if (!response.ok) throw new Error(`Failed to fetch bundle: ${response.status} ${response.statusText}`);
515
768
  const text2 = await response.text();
516
- return { text: text2, version: parseGameVersion(text2), serverInfo: parseServerInfo(text2, this.baseUrl) };
769
+ return { text: text2 };
517
770
  }
518
771
  };
519
772
  fetchGameVersion = (baseUrl = DEFAULT_BASE_URL) => new ServerDirectory(baseUrl).version();