Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 64x 64x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4584x 4584x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 631x 631x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2296x 4350x 4350x 4350x 4350x 4313x 4313x 4313x 4350x 4270x 4270x 4270x 4350x 80x 80x 80x 14x 80x 14x 14x 14x 80x 66x 66x 66x 4350x 2230x 2230x 2296x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 716x 716x 716x 716x 716x 1x 1x 1x 1x 1x 1x 1x 1x 1x 53x 53x 53x 53x 53x 53x 53x 53x 1696x 1696x 1696x 1696x 1696x 2703x 2703x 2703x 1696x 53x 53x 53x 53x 53x 53x 53x 53x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7492x 7492x 7492x 7492x 7492x 7492x 7492x 174910x 174910x 174910x 174910x 174910x 174910x 174910x 174910x 174910x 7492x 7492x 7492x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7492x 7492x 7492x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7204x 7204x 7204x 7204x 13x 13x 7191x 7191x 7191x 7191x 7191x 7191x 7191x 7191x 7204x 1175559x 1175559x 1175559x 1175559x 1175559x 879831x 879831x 879831x 1175559x 7191x 7191x 7191x 7204x 19x 19x 7172x 7172x 7204x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 438x 438x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1335x 1335x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 15997x 15997x 6x 6x 15991x 15991x 15997x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2835x 2835x 2835x 2835x 2835x 271x 271x 2835x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1656x 1656x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1625x 1625x 1625x 2x 2x 1625x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 24363x 24363x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 49192x 49192x 16x 16x 49176x 49176x 49192x 19x 19x 49157x 49157x 49192x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9432x 9432x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3861x 3861x 3861x 3861x 3861x 3580x 3861x 210x 281x 71x 71x 3790x 3790x 3790x 3790x 3790x 3861x 3861x 3861x 3861x 46x 46x 3744x 3744x 3744x 3744x 3744x 3861x 230x 230x 230x 230x 230x 230x 230x 72x 72x 230x 3672x 3672x 3672x 3672x 3672x 3861x 3580x 3580x 3458x 3580x 3861x 212x 212x 3460x 3460x 3460x 3460x 3861x 109x 109x 109x 48x 48x 109x 3412x 3412x 3412x 3861x 126x 126x 126x 52x 52x 126x 3360x 3360x 3360x 3360x 3861x 3861x 3257x 3861x 157x 157x 3203x 3861x 3861x 3861x 3861x 3861x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1916x 1916x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 342x 342x 342x 342x 342x 342x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3970x 3970x 17x 17x 3953x 3953x 3970x 30x 30x 3923x 3923x 3970x 39x 39x 3884x 3884x 3970x 23x 23x 3861x 3861x 3861x 3970x 658x 658x 3203x 3203x 3970x 25x 25x 3178x 3178x 3970x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7296x 7296x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6296x 6296x 6296x 6296x 6296x 6296x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1353x 1353x 1x 1x 1x 1x 1x 1x 1x 1x 1x 359x 359x 1x 1x 1x 1x 1x 1x 1x 1x 2045x 2045x 2045x 2045x 2045x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3325x 3325x 3325x 3325x 3325x 3325x 3325x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 859x 738x 738x 121x 859x 91x 91x 30x 30x 30x 30x 859x 3x 3x 27x 27x 27x 27x 859x 2x 2x 25x 25x 859x 11x 11x 25x 25x 25x 25x 859x 800x 800x 25x 859x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 27x 27x 6858x 918x 918x 6858x 27x 27x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 365x 365x 365x 365x 365x 365x 365x 365x 365x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7155x 14x 14x 7141x 7141x 7141x 7155x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 49279x 49279x 49279x 2725905x 2725905x 2725905x 2718323x 2725905x 2628x 7582x 2543x 2543x 4954x 2411x 2411x 2725905x 49279x 49279x 49279x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1362x 1362x 1362x 1362x 1362x 1362x 1362x 1x 1x 1361x 1361x 1361x 1361x 1362x 19x 19x 1315x 1362x 81x 81x 1234x 1234x 1362x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 15483x 4387x 4387x 11096x 15483x 8315x 8315x 2781x 2781x 15483x 110x 110x 2671x 15483x 9x 9x 2662x 15483x 401x 401x 2261x 2261x 11785x 11778x 11785x 11785x 11743x 2261x 15483x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 19x 19x 19x 19x 49x 19x 19x 19x 19x 19x 19x 23x 17x 23x 19x 19x 19x 19x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1683x 1683x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 23354x 23354x 1x | /*!
* Copyright (c) 2026 The Triauth Authors (https://www.triauth.org/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
import { LIMITS } from './protocol.js';
import { config } from './config.js';
/**
* Static helpers shared across the library.
*
* @class
* @memberof Triauth
*/
export class Helpers {
static #BASE64URL_REGEXP = /^[A-Za-z0-9\-_]+$/;
static #BASE32_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
static #BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static #BASE64URL_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
static #BASE64URL_VALUES = (() => {
const values = new Uint8Array(123); // indexed by char code; 'z' (the highest) is 122
for (let i = 0; i < 64; i++) {
values[Helpers.#BASE64URL_ALPHABET.charCodeAt(i)] = i;
}
return values;
})();
static #DOMAIN_NAME_REGEXP = /^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$/i
static #XN_LABEL_REGEXP = /(^xn--)|(\.xn--)/i; // a Punycode (xn--) label, leading or after a dot
static #URL_HOST_LABEL_REGEXP = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/; // one lowercase LDH host label, 1-63 chars, no edge hyphens
static #URL_NUMERIC_LABEL_REGEXP = /^(?:[0-9]+|0x[0-9a-f]*)$/i; // an all-digits or 0x-hex label, an IPv4 notation when final
static #URL_IPV4_QUAD_REGEXP = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])$/; // a canonical dotted-quad IPv4 host: four decimal octets 0-255, no leading zeros
static #URL_DOT_SEGMENT_REGEXP = /^(?:\.|%2e){1,2}$/i; // a "." or ".." path segment, %2e spellings included
static #URL_PATH_REGEXP = /^[A-Za-z0-9\-._~!$&'()*+,;=:@/%]*$/; // RFC 3986 pchar + "/" ("%" is a plain char here; "|" is not a URL character)
static #URL_QUERY_REGEXP = /^[A-Za-z0-9\-._~!$&'()*+,;=:@/?%]+$/; // the path characters + "?" also the fragment charset
static #NORMAL_STRING_REGEXP = /^[\p{L}\p{N} ~!@#$%^&*()_\-+={}[\]|\\:;"'<,>.?/]+$/u; // the Unicode property escape \p{L} matches any kind of letter from any language, \p{N} any number.
static #UNPAIRED_SURROGATE_REGEXP = /\p{Cs}/u; // matches only UNPAIRED surrogates - the strings UTF-8 cannot encode.
static #LOOKUP_CODE_REGEXP = /^[A-Z0-9]{16}$/;
static #PROTOTYPE_NAMES = ['__proto__', 'constructor', 'prototype']; // the prototype-poisoning names, never a JSON member name
static #P256_P = 0xffffffff00000001000000000000000000000000ffffffffffffffffffffffffn; // the P-256 field prime p
static #P256_B = 0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604bn; // the P-256 curve coefficient b (a = -3)
// eslint-disable-next-line no-restricted-globals
static #ENCODER = new TextEncoder();
// eslint-disable-next-line no-restricted-globals
static #DECODER = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true });
/**
* Returns the current time in milliseconds, for duration measurements.
*
* Monotonic (`performance.now`) whenever the runtime provides it, wall-clock
* (`Date.now`) otherwise.
*
* @returns {number} a millisecond timestamp.
*/
static now() {
return globalThis.performance?.now?.() ?? Date.now();
}
/**
* Returns the earlier of two timestamps, tolerating either being null/undefined.
*
* @param a {?number} - a timestamp, or null/undefined.
* @param b {?number} - a timestamp, or null/undefined.
*
* @returns {?number} the earlier of the two, or whichever one is set.
*/
static earliest(a,b) {
return (a != null && b != null) ? (a < b ? a : b) : (a ?? b);
}
/**
* Verifies DNS record options against a schema of allowed critical options and values.
*
* Non-`x-` prefixed options are critical: each must be whitelisted in the schema and match one of its
* allowed values (strings, regexps, or predicate functions returning exactly `true`). `x-` prefixed
* options are experimental and pass as long as they are normal strings.
*
* @param options {object} - the options as parsed from a DNS record.
* @param optionsSchema {object} - a map of option keys to arrays of allowed values (strings, regexps, or predicates).
*
* @returns {boolean} true when every option is acceptable, false otherwise.
*/
static verifyOptionsSchema(options, optionsSchema) {
for (const [optionKey, optionVal] of Object.entries(options)) {
// whitelisted critical options and values
if (
typeof optionVal === 'string' &&
Object.hasOwn(optionsSchema, optionKey) &&
optionsSchema[optionKey].findIndex(
(v) => (typeof v === 'string' && optionVal === v) || (v.constructor.name === 'RegExp' && optionVal.match(v)) || (typeof v === 'function' && v(optionVal) === true)
) >= 0
) {
continue;
// experimental options prefixed with 'x-' are allowed as long as they are generally sane
} else if (
Helpers.isNormalString(optionKey) &&
Helpers.isNormalString(optionVal) &&
optionKey[0] === 'x' &&
optionKey[1] === '-'
) {
continue;
// unknown or invalid critical option - reject
} else {
return false;
}
}
return true;
}
/**
* Returns a deep copy of the given JSON-shaped value (API results, parsed records).
*
* Uses the runtime's `structuredClone` when present, and an equivalent JSON
* round-trip otherwise.
*
* @param value {*} - A JSON-shaped value to copy.
*
* @returns {*} a deep copy sharing no mutable state with the input.
*/
static clone(value) {
if (typeof globalThis.structuredClone === 'function') {
return structuredClone(value);
}
return value === undefined ? value : JSON.parse(JSON.stringify(value));
}
/**
* Encodes a given byte array into a Base32 encoded string.
*
* @param b {Uint8Array} - A byte array containing the message to be encoded as a base32 string.
*
* @returns {string} base32 representation of the given byte array
*/
static uInt8ArrayToBase32(b) {
const alphabet = this.#BASE32_ALPHABET;
const bytes = b;
let bits = 0;
let value = 0;
let rv = "";
for (let i = 0; i < bytes.length; i++) {
value = (value << 8) | bytes[i];
bits += 8;
// 5-bits per character in base32
while (bits >= 5) {
rv += alphabet[(value >>> (bits - 5)) & 31];
bits -= 5;
}
}
// anything that's left needs to be also encoded
if (bits > 0) {
rv += alphabet[(value << (5 - bits)) & 31];
}
return rv;
}
/**
* Encodes a given byte array buffer into a Base64 encoded string.
*
* @param b {ArrayBuffer|Uint8Array} - An array buffer (or Uint8Array view) that contains the message (bytes) to be encoded as base64 string.
*
* @returns {string} base64 representation of the given array buffer
*/
static arrayBufferToBase64(b) {
const alphabet = this.#BASE64_ALPHABET;
const bytes = new Uint8Array(b);
let rv = '';
// 3 bytes make 4 characters; a final partial group is '='-padded
for (let i = 0; i < bytes.length; i += 3) {
const b0 = bytes[i];
const b1 = i + 1 < bytes.length ? bytes[i + 1] : 0;
const b2 = i + 2 < bytes.length ? bytes[i + 2] : 0;
rv += alphabet[b0 >> 2];
rv += alphabet[((b0 & 0x03) << 4) | (b1 >> 4)];
rv += i + 1 < bytes.length ? alphabet[((b1 & 0x0f) << 2) | (b2 >> 6)] : '=';
rv += i + 2 < bytes.length ? alphabet[b2 & 0x3f] : '=';
}
return rv;
}
/**
* Encodes a given byte array buffer into a Base64URL encoded string.
*
* @param b {ArrayBuffer|Uint8Array} - An array buffer (or Uint8Array view) that contains the message (bytes) to be encoded as base64url string.
*
* @returns {string} base64url representation of the given array buffer
*/
static arrayBufferToBase64Url(b) {
const rv = this.arrayBufferToBase64(b);
return rv.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
}
/**
* Converts the passed base64url encoded string into an Uint8Array of its individual bytes.
*
* @param s {string} - A base64url encoded string.
*
* @returns {Uint8Array} an array with byte values representing the characters of passed string.
*
* @throws {TypeError} when the string is not base64url, or is a non-canonical encoding
* (an impossible length, or payload in the final character's unused bits).
*/
static base64UrlToUint8(s) {
// Canonical decode: only the padding-free base64url alphabet, only
// possible lengths, and no payload in the final character's unused bits —
// every byte sequence has exactly one accepted encoding.
if (typeof s !== 'string' || s.length % 4 === 1 || !/^[A-Za-z0-9\-_]*$/.test(s)) {
throw new TypeError('Invalid base64url input');
}
const values = Helpers.#BASE64URL_VALUES;
const rv = new Uint8Array(Math.floor(s.length * 3 / 4));
let bits = 0;
let value = 0;
let index = 0;
for (let i = 0; i < s.length; i++) {
value = (value << 6) | values[s.charCodeAt(i)];
bits += 6;
// 8 accumulated bits form a byte
if (bits >= 8) {
bits -= 8;
rv[index++] = (value >>> bits) & 0xff;
}
}
// Trailing bits of a final partial group must be zero, or the same bytes
// would have more than one encoding.
if (bits > 0 && (value & ((1 << bits) - 1)) !== 0) {
throw new TypeError('Non-canonical base64url input');
}
return rv;
}
/**
* Encodes the passed string into base64url encoding of its UTF-8 bytes.
*
* Any well-formed string is encodable (emojis, non-latin scripts, etc.). Throws a TypeError
* on an unpaired surrogate — a surrogate code point has no UTF-8 encoding. Unreachable through
* the protocol surface: every internal call site encodes JSON.stringify output, which
* escapes unpaired surrogates.
*
* @param s {string} - A string to encode.
*
* @returns {string} encoded string.
*/
static stringToBase64Url(s) {
return this.arrayBufferToBase64Url(this.stringToUtf8Bytes(s));
}
/**
* Decodes the passed base64url encoded string, interpreting the decoded bytes as UTF-8.
*
* Throws a TypeError on malformed base64url input and on bytes that are not well-formed
* UTF-8. A leading byte-order mark is preserved, not stripped: U+FEFF stays byte-visible,
* so every byte sequence has exactly one decoded spelling.
*
* @param s {string} - A string to decode.
*
* @returns {string} decoded string.
*/
static base64UrlToString(s) {
return this.utf8BytesToString(this.base64UrlToUint8(s));
}
/**
* Encodes the passed string into its UTF-8 bytes.
*
* Throws a TypeError on an unpaired surrogate — a surrogate code point has no UTF-8 encoding,
* and substituting U+FFFD would let two distinct strings share one byte encoding (the
* platform encoder substitutes, so the well-formedness gate lives here).
*
* @param s {string} - A string to encode. Anything else throws.
*
* @returns {Uint8Array} the UTF-8 bytes of the string.
*
* @throws {TypeError} when the value is not a string, or the string carries an unpaired surrogate.
*/
static stringToUtf8Bytes(s) {
if (typeof s !== 'string') {
throw new TypeError('stringToUtf8Bytes expects a string');
}
if (!Helpers.isWellFormedString(s)) {
throw new TypeError('Unpaired surrogate');
}
return this.#ENCODER.encode(s);
}
/**
* Decodes the passed UTF-8 bytes into a string.
*
* Strict: throws a TypeError on any ill-formed sequence — overlong encodings, encoded
* surrogates, code points above U+10FFFF, truncated or stray continuation bytes. A leading
* byte-order mark is preserved, not stripped.
*
* Backed by the runtime's TextDecoder in fatal mode. The strict-decode contract itself is
* pinned by the conformance vectors, so ports use their own stdlib's strict decoder.
*
* @param bytes {Uint8Array} - The UTF-8 bytes to decode. Anything else throws.
*
* @returns {string} the decoded string.
*
* @throws {TypeError} when the value is not a Uint8Array, or the bytes are not well-formed UTF-8.
*/
static utf8BytesToString(bytes) {
if (!(bytes instanceof Uint8Array)) {
throw new TypeError('utf8BytesToString expects a Uint8Array');
}
try {
return this.#DECODER.decode(bytes);
} catch {
throw new TypeError('Ill-formed UTF-8 bytes');
}
}
/**
* Lowercases only the ASCII letters `A`-`Z` of the passed string, applying no other case mapping.
*
* Wire values that are "lowercased" before an ASCII grammar must use this instead of
* String#toLowerCase(): the full Unicode case mapping would fold characters such as
* U+212A KELVIN SIGN into ASCII (`k`), silently widening the wire alphabet in a way
* other implementations (ASCII tolower) would not reproduce.
*
* @param string {string} - A string to map.
*
* @returns {string} the string with `A`-`Z` mapped to `a`-`z` and every other character intact.
*/
static asciiLowercase(string) {
return string.replace(/[A-Z]/g, (c) => c.toLowerCase());
}
/**
* Percent-decodes the passed string into utf-8.
*
* @param string {string} - A string to decode.
*
* @returns {string|null} the decoded string, or null when an escape is malformed (a `%` not
* followed by two hexadecimal digits) or decodes to ill-formed UTF-8.
*/
static percentDecode(string) {
try {
return decodeURIComponent(string);
} catch {
return null;
}
}
/**
* Checks that the passed string carries no surrogate code point (U+D800 to U+DFFF), so that it has a UTF-8 encoding.
*
* Every string takes this check before it is encoded, and every string parsed from JSON text is held to
* the same rule: the `\uXXXX` escape of a surrogate is the one spelling of such a string that passes the
* strict UTF-8 decoder, because the JSON text itself is plain ASCII. The name mirrors the ES2024
* `String.prototype.isWellFormed`, which performs the same test.
*
* @param string {string} - A string to check.
*
* @returns {boolean} true if the value is a string without surrogate code points, false otherwise.
*/
static isWellFormedString(string) {
return typeof string === 'string' && !Helpers.#UNPAIRED_SURROGATE_REGEXP.test(string);
}
/**
* Checks that the passed string does not contain fancy utf-8 characters such as emojis, right-to-left encoding, etc.
*
* This is useful in validation of user-provided identifiers and/or other strings that may be displayed in the UI.
*
* @param string {string} - A string to check.
* @param [bytesizeLimit=255] {number|null} - The size limit for the string (counted in bytes). Null for unbound.
*
* @returns {boolean} true if string contains only 'normal' characters, false otherwise.
*/
static isNormalString(string, bytesizeLimit=255) {
// Ensure that we are dealing with a string
if (typeof string !== 'string') {
return false;
}
// If bytesizeLimit is set, enforce it
if (bytesizeLimit && this.byteSize(string) > bytesizeLimit) {
return false;
}
return this.#NORMAL_STRING_REGEXP.test(string);
}
/**
* Checks whether any label of the given hostname (or domain name) is in Punycode form.
*
* Punycode/IDN labels [RFC 3492] are blocked throughout the protocol, in identifiers, URLs,
* and endpoint hosts alike, to keep look-alike "homograph" domains out of everything a user
* may be shown.
*
* @param string {string} - A hostname or domain name to check.
*
* @returns {boolean} true if the string carries an `xn--` label, false otherwise.
*/
static hasPunycodeLabel(string) {
return typeof string === 'string' && this.#XN_LABEL_REGEXP.test(string);
}
/**
* Parses an absolute http(s) URL given in its canonical spelling, and returns its components.
*
* This is a small subset of RFC 3986 admitting exactly one spelling per resource.
* Owning it makes the accepted set identical on every engine (cross-engine URL parser
* differentials are a classic source of origin confusion).
*
* The grammar:
* - scheme: the literal `https://` or `http://`,
* - host: dot-separated lowercase LDH labels (letters/digits/hyphens, 1–63 chars, no
* leading/trailing hyphen, no empty labels) whose final label is not numeric (all digits,
* or `0x` + hex digits) — or a canonical dotted-quad IPv4 literal (four decimal octets
* 0–255, no leading zeros). Every other IPv4 spelling (hex, octal, dword, short forms)
* matches neither production; `[` is not in the grammar, so IPv6 literals are out as well,
* - port: optional; 1–65535 with no leading zeros, never the scheme's default,
* - userinfo: none (`@` cannot appear before the path),
* - path: required and absolute; characters from the RFC 3986 `pchar` set plus `/` and `%`
* (`|` is not an RFC 3986 URL character and never appears); no `.`/`..` segments in any
* spelling (`%2e` forms included),
* - query and fragment: optional and non-empty, over the path characters plus `?`.
*
* `%` is a plain character at this layer; the policy wrapper below rejects malformed escapes
* before parsing. This parser is reachable only through that wrapper, so grammar and policy
* always travel together.
*
* @param string {string} - The URL string to parse.
*
* @returns {?Readonly<{scheme: string, host: string, port: string, path: string,
* query: ?string, fragment: ?string, origin: string, base: string}>} The URL's components
* (frozen), or null when the string is not a canonical URL. `port` is `''` and
* `query`/`fragment` are null when absent; `origin` carries no trailing slash; `base` is the
* origin plus the path up to and including its last `/`.
*/
static #parseCanonicalUrl(string) {
if (typeof string !== 'string') {
return null;
}
// Scheme — the lowercase spelling only
let scheme;
if (string.startsWith('https://')) {
scheme = 'https';
} else if (string.startsWith('http://')) {
scheme = 'http';
} else {
return null;
}
let tail = string.slice(scheme.length + 3);
// The authority runs up to the first '/', '?' or '#'
const authorityEnd = tail.search(/[/?#]/);
const authority = authorityEnd < 0 ? tail : tail.slice(0, authorityEnd);
tail = authorityEnd < 0 ? '' : tail.slice(authorityEnd);
if (authority === '' || authority.includes('@')) {
return null;
}
// Optional port
let host = authority;
let port = '';
const portAt = authority.lastIndexOf(':');
if (portAt >= 0) {
port = authority.slice(portAt + 1);
host = authority.slice(0, portAt);
if (
!/^[1-9][0-9]{0,4}$/.test(port) || // 1-65535: no leading zeros, and port 0 is not a destination
Number(port) > 65535 ||
port === (scheme === 'https' ? '443' : '80') // an explicit default port is not canonical
) {
return null;
}
}
// Host — a named host (LDH labels, non-numeric final label) or a canonical dotted-quad
// IPv4 literal; every exotic IPv4 spelling matches neither production
const labels = host.split('.');
if (
!Helpers.#URL_IPV4_QUAD_REGEXP.test(host) &&
(
!labels.every((label) => Helpers.#URL_HOST_LABEL_REGEXP.test(label)) ||
Helpers.#URL_NUMERIC_LABEL_REGEXP.test(labels[labels.length - 1])
)
) {
return null;
}
// Fragment and query — split off in that order, so neither can hide in the other
let fragment = null;
const fragmentAt = tail.indexOf('#');
if (fragmentAt >= 0) {
fragment = tail.slice(fragmentAt + 1);
tail = tail.slice(0, fragmentAt);
if (!Helpers.#URL_QUERY_REGEXP.test(fragment)) {
return null;
}
}
let query = null;
const queryAt = tail.indexOf('?');
if (queryAt >= 0) {
query = tail.slice(queryAt + 1);
tail = tail.slice(0, queryAt);
if (!Helpers.#URL_QUERY_REGEXP.test(query)) {
return null;
}
}
// Path — what remains
const path = tail;
if (
path[0] !== '/' ||
!Helpers.#URL_PATH_REGEXP.test(path) ||
path.split('/').some((segment) => Helpers.#URL_DOT_SEGMENT_REGEXP.test(segment))
) {
return null;
}
const origin = `${scheme}://${host}${port ? `:${port}` : ''}`;
const base = origin + path.slice(0, path.lastIndexOf('/') + 1);
return Object.freeze({ scheme, host, port, path, query, fragment, origin, base });
}
/**
* Checks that the passed string is a canonical, safe-to-display URL.
*
* A string passes only when it is:
* - non-empty and within `LIMITS.urlBytesize`,
* - printable ASCII only (no spaces or control characters),
* - free of malformed `%`-escapes (every `%` is followed by two hex digits),
* - a canonical `http` or `https` URL per the closed grammar (a lowercase LDH host with a
* non-numeric final label, or a canonical dotted-quad IPv4 literal — no other IP spelling,
* no IPv6; optional non-default port; absolute path; optional query and fragment; no
* userinfo; the RFC 3986 `pchar` charset, which has no `|`; no `.`/`..`/`%2e` path
* segments), and
* - free of Punycode/IDN `xn--` labels (homograph look-alikes).
*
* Used to validate caller-supplied URLs that are later embedded in signatures or shown to the
* user: the `callbackUrl` (whose base-URL span carries the extra `;` rule at the validator) and
* attachment `sourceUrl`s. URLs that act as verbatim trust anchors — attestation provider URLs —
* use the narrower {@link Triauth.Helpers.isSecureUrl} instead.
*
* @param string {string} - A string to check.
*
* @returns {boolean} true if the string is a canonical URL meeting all the above constraints, false otherwise.
*/
static isCanonicalUrl(string) {
return this.#canonicalUrl(string) !== null;
}
/**
* Checks that the passed string is a canonical URL (see {@link Triauth.Helpers.isCanonicalUrl})
* on a secure origin: an `https` URL with a named (non-IP-literal) host — or, for local
* development, an `http` URL on the literal host `localhost`.
*
* This is the acceptance rule for URLs used verbatim as trust anchors: attestation provider
* URLs, which are displayed to the user as the trust decision and embedded whole as the
* attestation Segment's `via`. Callback URLs take the wider {@link Triauth.Helpers.isCanonicalUrl}
* instead — only their base URL enters the envelope, under its own delimiter rule.
*
* Every string this accepts is also accepted by `isCanonicalUrl`: both are views over the same
* parse, so the two accept-sets cannot diverge.
*
* @param string {string} - A string to check.
*
* @returns {boolean} true if the string is a canonical URL on a secure origin, false otherwise.
*/
static isSecureUrl(string) {
const url = this.#canonicalUrl(string);
return url !== null &&
!this.#URL_IPV4_QUAD_REGEXP.test(url.host) &&
(url.scheme === 'https' || url.host === 'localhost');
}
/**
* The single accept-set behind every canonical-URL helper: the closed grammar of
* {@link Triauth.Helpers.#parseCanonicalUrl} plus the protocol policy — size cap, printable
* ASCII, well-formed `%`-escapes, and no Punycode label.
*
* `isCanonicalUrl`, `isSecureUrl`, `getBaseUrl`, and `urlHost` are all views over this one
* function (`isSecureUrl` a narrowing one), so no URL can ever pass one of them while failing
* another's underlying parse.
*
* @param string {string} - A string to check and parse.
*
* @returns {?Readonly<{scheme: string, host: string, port: string, path: string,
* query: ?string, fragment: ?string, origin: string, base: string}>} The parsed components,
* or null when the string is not a canonical URL under the full protocol rules.
*/
static #canonicalUrl(string) {
// Ensure that we are dealing with a string
if (typeof string !== 'string') {
return null;
}
// Ensure size limits
if (string.length === 0 || this.byteSize(string) > LIMITS.urlBytesize) {
return null;
}
// ASCII only
if (!/^[\x21-\x7E]+$/.test(string)) {
return null;
}
// Reject malformed percent escapes.
if (/%(?![0-9A-Fa-f]{2})/.test(string)) {
return null;
}
// The string must be a canonical URL per the closed grammar
const url = this.#parseCanonicalUrl(string);
if (!url) {
return null;
}
// Block Punycode/IDN homographs (RFC 3492)
if (this.hasPunycodeLabel(url.host)) {
return null;
}
return url;
}
/**
* Checks that the passed string uses only characters from base64URL alphabet.
*
* This method is intended for quick validation and is not bullet-proof - e.g., may not check that the string is properly base64URL encoded.
*
* @param string {string} - A string to check.
*
* @returns {boolean} true if string contains only base64url characters, false otherwise.
*/
static isBase64UrlString(string) {
return typeof string === 'string' && this.#BASE64URL_REGEXP.test(string);
}
/**
* Checks that the passed string is a valid domain name.
*
* Accepts case-insensitive LDH domain names of at least two labels. Labels in Punycode form
* (`xn--`) are rejected (homograph look-alikes), and so is a numeric final label (all digits,
* or `0x` + hex digits) — real TLDs are never numeric, and this keeps every IPv4 spelling,
* exotic notations included, from ever being taken for a domain name.
*
* This method is intended for quick validation and is not bullet-proof - e.g., exotic domain names may not be recognized
*
* @param string {string} - A string to check.
*
* @returns {boolean} true if string is a domain name, false otherwise.
*/
static isDomainName(string) {
return typeof string === 'string' &&
this.byteSize(string) <= LIMITS.domainNameBytesize &&
this.#DOMAIN_NAME_REGEXP.test(string) &&
!this.hasPunycodeLabel(string) &&
!this.#URL_NUMERIC_LABEL_REGEXP.test(string.slice(string.lastIndexOf('.') + 1));
}
/**
* Returns the base URL of the given canonical URL string: its origin plus the path up to and
* including the last `/`, with query and fragment removed.
*
* For example, `https://example.com/foo/bar` returns `https://example.com/foo/`, while
* `https://example.com/foo/` returns `https://example.com/foo/` unchanged.
*
* The base URL is what the authenticator signs into the `via` field of a signature envelope,
* so this accepts only canonical URLs (see {@link Triauth.Helpers.isCanonicalUrl}) — for
* anything else it returns `false` rather than a normalized guess.
*
* @param url {string} - A canonical URL string to process.
*
* @returns {string|false} The base URL with a trailing slash, or `false` if the input is not a canonical URL.
*/
static getBaseUrl(url) {
return this.#canonicalUrl(url)?.base ?? false;
}
/**
* Returns the host (without the port) of the given canonical URL string.
*
* @param url {string} - A canonical URL string to process.
*
* @returns {string|false} The lowercase hostname, or `false` if the input is not a canonical URL.
*/
static urlHost(url) {
return this.#canonicalUrl(url)?.host ?? false;
}
/**
* Calculates the SHA-256 digest of the passed string.
*
* @param message {string} - A message whose SHA-256 digest should be calculated.
* @returns {Promise<string>} a promise that resolves to the sha256 of the given message.
*/
static async sha256(message) {
const data = this.stringToUtf8Bytes(message);
const hash = (await crypto.subtle.digest('SHA-256', /** @type {BufferSource} */ (data)));
return this.arrayBufferToBase64Url(hash);
}
/**
* Computes the HMAC-SHA-256 of a message under a key.
*
* Keys the private-mode identity-domain derivation (`Triauth.IdentityDomain.deriveFullLabel`).
*
* @param key {Uint8Array} - The key bytes.
* @param message {Uint8Array} - The message bytes.
* @returns {Promise<Uint8Array>} a promise that resolves to the 32-byte MAC.
*/
static async hmacSha256(key, message) {
const hmacKey = await crypto.subtle.importKey(
'raw', /** @type {BufferSource} */ (key),
{ name: 'HMAC', hash: 'SHA-256' }, false, ['sign']
);
return new Uint8Array(await crypto.subtle.sign('HMAC', hmacKey, /** @type {BufferSource} */ (message)));
}
/**
* Returns a P-256 public key as the 65-byte uncompressed point (`0x04`, x, y), the form that the Web Crypto
* `raw` import accepts on all runtimes.
*
* Accepts the uncompressed point (65 bytes, prefix `0x04`) and returns it unchanged. Accepts the compressed
* point (33 bytes, prefix `0x02` or `0x03`, then x) and decodes it here, because not all Web Crypto
* implementations import a compressed point. Rejects all other forms, the hybrid form (prefix `0x06` or `0x07`)
* included, also on runtimes whose Web Crypto import accepts them. The result is therefore the same on all runtimes.
*
* @param bytes {Uint8Array} - The public key bytes.
* @returns {Uint8Array} the 65-byte uncompressed point.
*
* @throws {TypeError} when the bytes are not an uncompressed or a compressed point, or when the compressed point
* is not on the curve.
*/
static decodeP256PublicKey(bytes) {
if (bytes.length === 65 && bytes[0] === 0x04) {
return bytes;
}
if (bytes.length !== 33 || (bytes[0] !== 0x02 && bytes[0] !== 0x03)) {
throw new TypeError('Invalid public key');
}
// x must be smaller than p, so that a key has one compressed form only
const p = Helpers.#P256_P;
const x = bytes.subarray(1).reduce((n, byte) => (n << 8n) | BigInt(byte), 0n);
if (x >= p) {
throw new TypeError('Invalid public key');
}
// y² = x³ - 3x + b (mod p). Because p = 3 (mod 4), r^((p + 1) / 4) is a square root of r when r has one.
const r = ((x * x % p) * x + 3n * (p - x) + Helpers.#P256_B) % p;
let y = Helpers.#modPow(r, (p + 1n) >> 2n, p);
if (y * y % p !== r) {
throw new TypeError('Invalid public key');
}
// The prefix gives the parity of y. No point on P-256 has y = 0, so p - y has the other parity.
if (Number(y & 1n) !== (bytes[0] & 1)) {
y = p - y;
}
const point = new Uint8Array(65);
point[0] = 0x04;
point.set(bytes.subarray(1), 1);
for (let i = 64; i > 32; i--, y >>= 8n) {
point[i] = Number(y & 0xffn);
}
return point;
}
/**
* Computes base^exponent mod modulus by square-and-multiply.
*
* @param base {bigint} - The base.
* @param exponent {bigint} - The exponent, zero or more.
* @param modulus {bigint} - The modulus.
* @returns {bigint} the result, from 0 to modulus - 1.
*/
static #modPow(base, exponent, modulus) {
let result = 1n;
for (base %= modulus; exponent > 0n; exponent >>= 1n, base = base * base % modulus) {
if (exponent & 1n) {
result = result * base % modulus;
}
}
return result;
}
/**
* Generates a random string of the given length using the base64 url alphabet.
*
* The underlying byte source is the effective config's `randomSource` when set, otherwise
* `crypto.getRandomValues`. See `Triauth.config.randomSource` for details on overriding
* the entropy source.
*
* @param len {Number} - The character length of the returned string.
* @param [config] {object} - The effective config whose `randomSource` supplies the bytes;
* defaults to the global `Triauth.config`.
* @returns {string} a random string of the given length
*/
static randomString(len, {randomSource} = config) {
// base64url packs 3 random bytes into 4 chars (0.75 bytes/char), so `len` chars need
// ceil(len * 0.75) bytes; the +1 covers the partial final group and guarantees the encoded
// string is at least `len` chars before the substring trim. Every returned char is therefore
// backed by fresh random bytes (no padding/repetition is introduced by the trim).
const array = new Uint8Array(Math.ceil(len * 0.75) + 1);
const source = randomSource || ((arr) => crypto.getRandomValues(arr));
source(array);
return Helpers.arrayBufferToBase64Url(array).substring(0, len);
}
/**
* Verifies that the passed object has only properties that are on the passed whitelist.
*
* This method helps to ensure that the user-passed arguments were not misspelled or come from untrusted sources.
* It also helps to recognize that an argument was passed from the user in the subsequent code, by enforcing (in strict
* mode) that only non-undefined values may be set by the user.
*
* @param obj {Object} - An object which properties should be checked.
* @param whitelist {ReadonlyArray<string>} - An array of allowed properties (whitelist).
* @param [strict=true] {boolean} - When true (default), a whitelisted key whose value is `undefined` is rejected; use this at
* trust boundaries (user-facing options/constraints) to force callers to omit a value rather than pass `undefined`.
*
* @returns {boolean} true if object contains only properties from the whitelist, false otherwise
*/
static hasOnlyKnownProperties(obj, whitelist, strict = true) {
if (obj === null || typeof obj !== 'object' || Array.isArray(obj)) {
return false;
}
const objKeys = Object.keys(obj);
return objKeys.every(key => whitelist.includes(key) && (!strict || obj[key] !== undefined));
}
/**
* Returns the number of bytes it takes to store the given string in UTF-8.
*
* This method is helpful when validating untrusted input, as simply checking .length
* may be not enough for utf-8 encoded strings.
*
* @param str {string} - A string for which its underlying bytesize should be calculated.
*
* @returns {number} The number of bytes.
*/
static byteSize(str) {
let size = 0;
for (let i = 0; i < str.length; i++) {
const c = str.charCodeAt(i);
if (c < 0x80) {
size += 1;
} else if (c < 0x800) {
size += 2;
} else if (c >= 0xD800 && c < 0xDC00 && str.charCodeAt(i + 1) >= 0xDC00 && str.charCodeAt(i + 1) <= 0xDFFF) {
size += 4;
i++;
} else {
size += 3;
}
}
return size;
}
/**
* Parses the provided JSON text and returns the object it encodes.
*
* The native JSON parser reads the text, then one walk over the parsed value applies the rules below, so that
* a value which passes them reads the same in every language:
* - the source is a string of at most 256KB, and its root is an object,
* - a number is a whole number of magnitude at most 2^53 - 1 (`Number.isSafeInteger`), the range that every
* JSON parser reads exactly: `1.0` and `1e2` are the whole numbers 1 and 100, while a fraction, a larger
* integer or a literal that overflows to Infinity is rejected,
* - a string carries no surrogate code point (the JSON escape of a surrogate is the one spelling of such a
* string that passes the strict UTF-8 decoder),
* - a member name is ASCII, at most 255 characters long, and never `__proto__`, `constructor` or `prototype`,
* - objects and arrays nest at most 8 levels deep: the root object is level 1, and other values add no level.
*
* A member name that occurs twice in one object keeps its last value, as the native parser reads it, and the
* rules apply to that last value.
*
* @param source {string} - A source string that contains json encoded data.
*
* @returns {Object} deserialized object
*/
static safeParseJson(source) {
const err = new Error('JSON validation error');
// Pre-validate that source is a string not larger than 256KB
if (
typeof source !== 'string' ||
Helpers.byteSize(source) > LIMITS.jsonMaxBytesize
) {
throw err;
}
const jsonObject = JSON.parse(source);
// We always expect to see JSON encoded objects ({}) at root level.
if (jsonObject === null || typeof jsonObject !== 'object' || Array.isArray(jsonObject)) {
throw err;
}
if (!Helpers.#isBoundedJsonValue(jsonObject, 1)) {
throw err;
}
return jsonObject;
}
/**
* Checks one value produced by `JSON.parse`, and every value inside it, against the rules of `Helpers.safeParseJson`.
*
* The walk goes at most one level past the nesting limit, so its recursion depth stays bounded whatever the parser built.
*
* @param value {*} - A value produced by `JSON.parse`.
* @param depth {number} - The nesting level of the value if it is an object or an array (the root object is level 1).
*
* @returns {boolean} true if the value and every value inside it follow the rules, false otherwise.
*/
static #isBoundedJsonValue(value, depth) {
if (typeof value === 'number') {
return Number.isSafeInteger(value);
}
if (typeof value === 'string') {
return Helpers.isWellFormedString(value);
}
// true, false and null add no nesting level
if (value === null || typeof value !== 'object') {
return true;
}
if (depth > LIMITS.jsonMaxNestingDepth) {
return false;
}
if (Array.isArray(value)) {
return value.every((item) => Helpers.#isBoundedJsonValue(item, depth + 1));
}
return Object.keys(value).every((name) => (
name.length <= LIMITS.jsonMaxKeyLength &&
// eslint-disable-next-line no-control-regex
!/[^\x00-\x7F]/.test(name) &&
!Helpers.#PROTOTYPE_NAMES.includes(name) &&
Helpers.#isBoundedJsonValue(value[name], depth + 1)
));
}
/**
* Builds a minimal in-memory LRU cache - the default answer store of `Triauth.Resolvers.CachingResolver`.
*
* The returned object's `get` and `set` methods are async (return Promises) so a caller can swap in
* a custom remote cache without changing the call sites.
*
* @param [size=1024] {number} - Maximum number of entries. Defaults to 1024.
*
* @returns {{get: (function(string): Promise<*|null>), set: (function(string, *): Promise<void>), resize: (function(number): void)}}
* An LRU cache exposing `get(key)`, `set(key, value)`, and `resize(newSize)`.
*/
static createLRUCache(size = 1024) {
const cache = new Map();
return {
get: async (key) => {
if (!cache.has(key)) return null;
const val = cache.get(key);
cache.delete(key);
cache.set(key, val);
return val;
},
set: async (key, val) => {
if (cache.has(key)) cache.delete(key);
else if (cache.size >= size) cache.delete(cache.keys().next().value);
cache.set(key, val);
},
resize: newSize => {
size = newSize;
while (cache.size > size) cache.delete(cache.keys().next().value);
}
};
}
/**
* Checks whether the given value is a well-formed lookup code - the 16-character token that
* addresses an identity's records in the DNS when resolving in private mode.
*
* @param val {*} - A value to check; anything that is not a string is reported as not a lookup code.
*
* @returns {boolean} true when the value is a string of exactly 16 uppercase letters and digits
*/
static isLookupCode(val) {
return !!(typeof val === 'string' && val.match(this.#LOOKUP_CODE_REGEXP));
}
/**
* Returns the effective settings for one call or instance: the global `Triauth.config`
* with the given per-call overrides merged over it. The global object is read at call
* time, so runtime mutations of `Triauth.config` are picked up.
*
* @param [overrides={}] {object} - Per-call overrides for the global Triauth.config settings.
*
* @returns {typeof config} the merged settings
*/
static mergeConfig(overrides = {}) {
return Object.assign({}, config, overrides);
}
}
|