All files / lib/resources http-client.ts

98.84% Statements 944/955
97% Branches 162/167
100% Functions 37/37
98.84% Lines 944/955

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 9561x 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 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 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 259x 259x 259x 259x 259x 259x 259x 259x 259x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 259x 259x 259x 259x 259x 259x 1x 1x 1x 1x 1x 1x 1x 217x 217x 217x 217x 217x 1x 1x 1x 1x 1x 1x 1x 1x 1x 37x 1x 1x 36x 37x 5x 5x 31x 37x 37x 37x 37x 37x 37x 37x 1x 1x 1x 1x 1x 1x 1x 1x 609x 609x 1x 1x 1x 1x 1x 1x 35300x 1x 1x 1x 1x 1x 1x 1x 1x 1x 35300x 35300x 35300x 35300x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1707x 1707x 1707x 1707x 1707x 1707x 1707x 1707x 1707x 1707x 1707x 1707x 1707x 1707x 1707x 1707x 1707x 1707x 1707x 1707x 1707x 1707x 1x 1x 1x 1x 1x 1x 1x 3027x 3027x 3027x 1x 1x 1x 1x 1x 1x 1x 640x 640x 640x 640x 640x 640x 431x 431x 640x 640x 640x 640x 5493x 640x 640x 640x 640x 1x 1x 1x 1x 1x 1x 1x 266x 3x 3x 3x 266x 266x 266x       266x 266x 6x 9x 9x       9x 9x 9x 6x 6x 266x 266x 266x 1x 1x 1x 1x 1x 1x 1x 3996x 3956x 3956x 40x 40x 142x 20x 20x 20x 20x 20x 20x 1x 1x 1x 1x 1x 1x 1x 1x 13434x 13434x       13434x 13434x 13434x 8434x 8434x 8434x 5000x 5000x 5000x 1x 1x 1x 1x 1x 1x 35431x 35431x 35431x 35431x 1x 1x 1x 1x 1x 1x 11x 11x 1x 1x 1x 1x 1x 1x 578x 5586x 5586x     5586x 5586x 5272x 5586x 314x 314x 5586x 578x 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 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 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 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 4x 4x 4x 4x 4x 12x 13x 3x 3x 12x 12x 13x 1x 1x 13x 1x 1x 1x 1x 1x 1x 1x 37x 13x 13x 24x 24x 24x 39x 24x 24x 1x 1x 1x 1x 1x 1x 1x 1x 16x 16x 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 1616x 1616x 1616x 1616x 1616x 1616x 1616x 1616x 1616x 1616x 1616x 1616x 1616x 1616x 1x 1x 1x 1x 1x 1x 1x 1616x 1616x 1616x 1616x 1616x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 56x 56x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 443x 443x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 24x 24x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 523x 523x 523x 523x 523x 523x 1x 1x 522x 523x 523x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 2x 2x 9x 11x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 11x 3x 3x 3x 2x 2x 1x 1x 1x 11x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 530x 530x 530x 530x 530x 530x 530x 530x 530x 530x 530x 530x 530x 530x 459x 11x 459x 448x 448x 446x 446x 448x 459x 529x 530x 478x 478x 51x 51x 51x 1x 1x 1x 1x 1x 1x 1x 1x 539x 539x 257x 257x 257x 257x 539x 539x 281x 281x 539x 539x 539x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 522x 522x 522x 522x 522x 522x 522x 25x 25x 497x 497x 497x 522x 206x 206x 291x 522x 522x 231x 231x 231x 231x 231x 522x 522x 522x 291x 291x 291x 1x 1x 1x 1x 1x 1x 1x 1x 498x 498x 96x 86x 86x 498x 498x 498x 498x 6x 6x 498x 498x 498x 498x 498x 498x 498x 498x 498x 498x 1x 1x 1x 1x 1x 1x 1x 502x 148x 148x 354x 354x 354x 502x 39x 39x 502x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 1x  
import {createWriteStream} from "fs";
import fs from "fs/promises";
import {randomUUID} from "crypto";
import {Readable} from "stream";
import {pipeline} from "stream/promises";
import {ReadableStream} from "stream/web";
import {Cookie} from "tough-cookie";
import {errorMessage} from "../../app/error/error.js";
import {Resources} from "./main.js";
import {CLIENT_ID, CLIENT_INFO, HEADER_KEYS, USER_AGENT} from "./network-types.js";
 
/**
 * A regex to check if a URL is absolute:
 * ^ - beginning of the string
 * (?: - beginning of a non-captured group
 *   [a-z+]+ - any character of 'a' to 'z' or "+" 1 or more times
 *   : - string (colon character)
 * )? - end of the non-captured group. Group appearing 0 or 1 times
 * // - string (two forward slash characters)
 * 'i' - non case-sensitive flag
 */
const ABSOLUTE_URL_REGEX = /^(?:[a-z+]+:)?\/\//i;
 
/**
 * The HTTP methods used by the application
 */
export type HttpMethod = `GET` | `POST` | `PUT`;
 
/**
 * Per-request configuration
 */
export type HttpRequestConfig = {
    /**
     * Query parameters, appended to the request URL
     */
    params?: Record<string, string>,
    /**
     * Additional request headers - headers from the header jar take precedence
     */
    headers?: Record<string, string>,
    /**
     * Decides, if a response status is accepted - a rejected status results in an HttpError (defaults to accepting 2xx)
     */
    validateStatus?: (status: number) => boolean,
}
 
/**
 * A fully prepared request, as sent to the backend
 */
export type HttpRequest = {
    method: HttpMethod,
    /**
     * The URL as requested - might be relative to the base URL
     */
    url: string,
    /**
     * The base URL of the client at the time of the request
     */
    baseURL?: string,
    /**
     * The resolved, absolute URL including query parameters
     */
    fullURL: string,
    params?: Record<string, string>,
    headers: Record<string, string>,
    /**
     * The serialized request body, if any
     */
    data?: string,
    /**
     * Unix timestamp (in ms) of the moment the request was issued
     */
    startedAt: number,
}
 
/**
 * A received response
 */
export type HttpResponse<T = any> = {
    status: number,
    statusText: string,
    /**
     * Response headers with lower-cased names - values are strings, except 'set-cookie', which is always a string array
     */
    headers: Record<string, any>,
    /**
     * The response body - parsed JSON if possible, the raw string otherwise (an empty body results in an empty string)
     */
    data: T,
    /**
     * The raw response body (an empty body results in an empty string)
     */
    text: string,
    /**
     * The request that lead to this response
     */
    config: HttpRequest,
}
 
/**
 * The raw response, as returned by the transport layer
 */
export type HttpRawResponse = {
    status: number,
    statusText: string,
    headers: Headers,
    body: string,
}
 
/**
 * Brand of response validators - only exists on type level
 */
declare const RESPONSE_VALIDATOR: unique symbol;
 
/**
 * Validates a response against a JSON schema and provides the validated value - throws an iCPSError, if the response is invalid
 * The brand ensures that response validators can only be created by the Validator (see `Validator.response`), so every request's response is schema validated
 */
export type ResponseValidator<T> = ((response: HttpResponse) => T) & {readonly [RESPONSE_VALIDATOR]: true};
 
/**
 * Error thrown by the HttpClient, if a request fails or the response status is not accepted
 * The error code follows the codes previously provided by axios, in order to keep error reports comparable
 */
export class HttpError extends Error {
    /**
     * The error code - ERR_BAD_REQUEST (4xx status), ERR_BAD_RESPONSE (other rejected status), ECONNABORTED (timeout or abort), the underlying network error code (e.g. ECONNREFUSED) or ERR_NETWORK
     */
    code: string;
 
    /**
     * The request that failed
     */
    request: HttpRequest;
 
    /**
     * The response, if one was received
     */
    response?: HttpResponse;
 
    /**
     * Creates a new HttpError
     * @param message - The error message
     * @param code - The error code
     * @param request - The request that failed
     * @param response - The response, if one was received
     * @param cause - The underlying error, if any
     */
    constructor(message: string, code: string, request: HttpRequest, response?: HttpResponse, cause?: unknown) {
        super(message, cause === undefined ? undefined : {cause});
        this.name = `HttpError`;
        this.code = code;
        this.request = request;
        this.response = response;
    }
 
    /**
     * Creates an error for a response with a rejected status
     * @param response - The response
     * @returns The error
     */
    static fromResponse(response: HttpResponse): HttpError {
        const code = response.status >= 400 && response.status < 500
            ? `ERR_BAD_REQUEST`
            : `ERR_BAD_RESPONSE`;
        return new HttpError(`Request failed with status code ${response.status}`, code, response.config, response);
    }
 
    /**
     * Creates an error for a request that did not receive a response
     * Node's fetch rejects with a generic `TypeError('fetch failed')`, the actual network error code is provided by its cause
     * @param err - The error thrown by fetch
     * @param request - The request that failed
     * @returns The error
     */
    static fromNetworkError(err: unknown, request: HttpRequest): HttpError {
        if (err instanceof HttpError) {
            return err;
        }
 
        if (Error.isError(err) && (err.name === `TimeoutError` || err.name === `AbortError`)) {
            return new HttpError(err.message, `ECONNABORTED`, request, undefined, err);
        }
 
        const cause = Error.isError(err) ? err.cause as {code?: unknown, message?: unknown} | undefined : undefined;
        const code = typeof cause?.code === `string` ? cause.code : `ERR_NETWORK`;
        const message = typeof cause?.message === `string` && cause.message.length > 0
            ? cause.message
            : errorMessage(err);
        return new HttpError(message, code, request, undefined, err);
    }
}
 
/**
 * Type guard for HttpErrors
 * @param err - The value to check
 * @returns True, if the value is an HttpError
 */
export function isHttpError(err: unknown): err is HttpError {
    return err instanceof HttpError;
}
 
/**
 * Object holding all necessary information for a specific header value, that needs to be reused across multiple requests
 */
export class Header {
    key: string;
    value: string;
    domain: string;
 
    /**
     * Creates a new header object
     * @param domain - The domain the header should be applied to, or an empty string if it should be applied to all domains
     * @param key - The header key
     * @param value - The header value
     */
    constructor(domain: string, key: string, value: string) {
        this.domain = domain;
        this.key = key;
        this.value = value;
    }
}
 
/**
 * Keeps track of headers and cookies, that need to be applied to requests based on their domain
 */
export class HeaderJar {
    headers: Map<string, Header> = new Map();
    cookies: Map<string, Cookie> = new Map();
 
    /**
     * Creates a new header jar with static header values applied
     */
    constructor() {
        // Default headers
        this.setHeader(new Header(``, `Accept`, `application/json`));
        this.setHeader(new Header(``, `Content-Type`, `application/json`));
        this.setHeader(new Header(``, `Connection`, `keep-alive`));
        this.setHeader(new Header(``, `Accept-Encoding`, `gzip, deflate, br`));
        this.setHeader(new Header(``, `User-Agent`, USER_AGENT));
 
        // Static auth headers
        this.setHeader(new Header(`idmsa.apple.com`, `Origin`, `https://idmsa.apple.com`)); // This should overwrite the default 'Origin' header
        this.setHeader(new Header(`idmsa.apple.com`, `Referer`, `https://idmsa.apple.com/`));
        this.setHeader(new Header(`idmsa.apple.com`, `X-Apple-Widget-Key`, CLIENT_ID));
        this.setHeader(new Header(`idmsa.apple.com`, `X-Apple-OAuth-Client-Id`, CLIENT_ID));
        this.setHeader(new Header(`idmsa.apple.com`, `X-Apple-I-FD-Client-Info`, CLIENT_INFO));
        this.setHeader(new Header(`idmsa.apple.com`, `X-Apple-OAuth-Response-Type`, `code`));
        this.setHeader(new Header(`idmsa.apple.com`, `X-Apple-OAuth-Response-Mode`, `web_message`));
        this.setHeader(new Header(`idmsa.apple.com`, `X-Apple-OAuth-Client-Type`, `firstPartyAuth`));
        this.setHeader(new Header(`idmsa.apple.com`, `X-Apple-OAuth-Redirect-URI`, `https://www.icloud.com`));
        this.setHeader(new Header(`idmsa.apple.com`, `X-Apple-OAuth-Require-Grant-Code`, `true`));
        this.setHeader(new Header(`idmsa.apple.com`, `X-Apple-Offer-Security-Upgrade`, `1`));
        this.setHeader(new Header(`idmsa.apple.com`, `X-Apple-Domain-Id`, `3`));
        this.resetFrameId();
    }
 
    /**
     * Generates a new frame id, identifying the current authentication flow (similar to the iCloud web frontend, which generates one per login iframe)
     * The same value is used as OAuth state
     * @param frameId - The frame id to use - a random UUID is generated by default
     */
    resetFrameId(frameId: string = randomUUID()) {
        this.setHeader(new Header(`idmsa.apple.com`, HEADER_KEYS.FRAME_ID, frameId));
        this.setHeader(new Header(`idmsa.apple.com`, HEADER_KEYS.OAUTH_STATE, frameId));
    }
 
    /**
     * Injects the relevant headers and cookies into the request
     * @param request - The request
     * @returns The adjusted request containing relevant cookies and headers
     */
    _injectHeaders(request: HttpRequest): HttpRequest {
        const requestCookieString = Array.from(this.cookies.values())
            .filter(cookie => this.isApplicable(request, cookie))
            .filter(cookie => this.isNotExpired(cookie))
            .map(cookie => cookie.cookieString()).join(`; `);
 
        if (requestCookieString.length > 0) {
            request.headers[HEADER_KEYS.COOKIE] = requestCookieString;
        }
 
        Array.from(this.headers.values())
            .filter(cookie => this.isApplicable(request, cookie))
            .forEach(header => {
                request.headers[header.key] = header.value;
            });
 
        return request;
    }
 
    /**
     * Extracts general applicable header (scnt) and set-cookies from the response
     * @param response - The response
     * @returns The unmodified response
     */
    _extractHeaders(response: HttpResponse): HttpResponse {
        if (response.headers.scnt && this.isApplicable(response.config, new Header(`idmsa.apple.com`, ``, ``))) {
            Resources.logger(this).debug(`Extracted scnt from response header with length ` + response.headers.scnt.length);
            this.setHeader(new Header(`idmsa.apple.com`, HEADER_KEYS.SCNT, response.headers.scnt));
        }
 
        const authAttributes = response.headers[HEADER_KEYS.AUTH_ATTRIBUTES.toLowerCase()];
        if (authAttributes && this.isApplicable(response.config, new Header(`idmsa.apple.com`, ``, ``))) {
            Resources.logger(this).debug(`Extracted auth attributes from response header with length ` + authAttributes.length);
            this.setHeader(new Header(`idmsa.apple.com`, HEADER_KEYS.AUTH_ATTRIBUTES, authAttributes));
        }
 
        if (response.headers[`set-cookie`] && Array.isArray(response.headers[`set-cookie`])) {
            response.headers[`set-cookie`].forEach(cookie => {
                const parsedCookie = Cookie.parse(cookie);
                if (!parsedCookie) {
                    Resources.logger(this).debug(`Unable to parse cookie from response header, ignoring...`);
                    return;
                }
 
                Resources.logger(this).debug(`Extracted cookie from response header: ${parsedCookie.key} (domain ${parsedCookie.domain}) with length ${parsedCookie.value.length}`);
                this.setCookie(parsedCookie);
            });
        }
 
        return response;
    }
 
    /**
     * Checks metadata of the provided cookie to check if it's still valid
     * @param cookie - The cookie to check
     * @returns False if expired, true otherwise
     */
    isNotExpired(cookie: Cookie): boolean {
        if (cookie.TTL() > 0) {
            return true;
        }
 
        // Cookie.expires could also be the string 'Infinity', but then TTL() would be the number Infinity
        if ((cookie.expires as Date).getTime() === 1000) { // For some reason some Apple Headers have a magic expire unix time of 1000 (X-APPLE-WEBAUTH-HSA-LOGIN), including them for now...
            return true;
        }
 
        Resources.logger(this).debug(`Not applying expired cookie ${cookie.key}`);
        return false;
    }
 
    /**
     * Checks if a given object is applicable to the request. Takes URL and base URL into account
     * @param request - The request
     * @param object - The object to check
     * @returns True if the object is applicable to the request, false otherwise
     */
    isApplicable(request: Pick<HttpRequest, `url` | `baseURL`>, object: Header | Cookie): boolean {
        const objectDomain = object.domain;
        if (objectDomain === null) {
            // Cookies without domain are never applicable
            return false;
        }
 
        const url = request.url ?? ``;
        if (request.baseURL && !ABSOLUTE_URL_REGEX.test(url)) {
            // Base URL is not used if request URL is absolute
            return request.baseURL.includes(objectDomain);
        }
 
        return url.includes(objectDomain);
    }
 
    /**
     * Sets a header object in the header jar - overwrites existing headers with the same key
     * @param header - The header to set
     */
    setHeader(...header: Header[]) {
        for (const h of header) {
            this.headers.set(h.key, h);
        }
    }
 
    /**
     * Clearing a header from the header jar
     * @param key - The key of the header to clear
     */
    clearHeader(key: string) {
        this.headers.delete(key);
    }
 
    /**
     * Sets a cookie object in the header jar - overwrites existing cookies with the same key
     * @param cookie - The cookie to set
     */
    setCookie(...cookie: (Cookie | string)[]) {
        for (const c of cookie) {
            const _cookie = typeof c === `string` ? Cookie.parse(c) : c;
            if (!_cookie) {
                continue;
            }
 
            if (_cookie.value.length > 0) {
                this.cookies.set(_cookie.key, _cookie);
            } else {
                this.cookies.delete(_cookie.key);
            }
        }
    }
}
 
/**
 * A name/value pair, as used for headers in the network capture
 */
type CaptureNameValue = {
    name: string,
    value: string,
}
 
/**
 * A single captured request/response pair, loosely following the HAR 1.2 entry format, so the file can be opened in common HAR viewers
 */
type CaptureEntry = {
    startedDateTime: string,
    /**
     * Duration of the request in milliseconds
     */
    time: number,
    request: {
        method: string,
        url: string,
        httpVersion: string,
        cookies: CaptureNameValue[],
        headers: CaptureNameValue[],
        queryString: CaptureNameValue[],
        postData?: {
            mimeType: string,
            text: string,
        },
        headersSize: number,
        bodySize: number,
    },
    response: {
        /**
         * The HTTP status code, 0 if no response was received
         */
        status: number,
        statusText: string,
        httpVersion: string,
        cookies: CaptureNameValue[],
        headers: CaptureNameValue[],
        content: {
            size: number,
            mimeType: string,
            text?: string,
        },
        redirectURL: string,
        headersSize: number,
        bodySize: number,
    },
    cache: object,
    timings: {
        send: number,
        wait: number,
        receive: number,
    },
    /**
     * The error code and message, if the request failed (e.g. rejected status, timeout or connection error)
     */
    _error?: string,
}
 
/**
 * The captured network log, loosely following the HAR 1.2 format
 */
type CaptureLog = {
    log: {
        version: string,
        creator: {
            name: string,
            version: string,
        },
        pages: [],
        entries: CaptureEntry[],
    },
}
 
/**
 * Captures requests and responses for debugging purposes
 * Each entry is built from the completed request, so concurrent requests are captured independently
 */
export class NetworkCapture {
    /**
     * The captured network log
     */
    log!: CaptureLog;
 
    /**
     * Creates a new, empty network capture
     */
    constructor() {
        this.reset();
    }
 
    /**
     * Clears all captured entries
     */
    reset() {
        this.log = {
            log: {
                version: `1.2`,
                creator: {
                    name: Resources.PackageInfo.name,
                    version: Resources.PackageInfo.version,
                },
                pages: [],
                entries: [],
            },
        };
    }
 
    /**
     * Adds an entry to the network log - failing to capture a request never fails the request itself
     * @param request - The request
     * @param response - The response, if one was received
     * @param error - The error, if the request failed
     */
    addEntry(request: HttpRequest, response?: HttpResponse, error?: HttpError) {
        try {
            const time = Date.now() - request.startedAt;
 
            const requestHeaders = this._toNameValue(request.headers);
            const requestBody = request.data;
            const responseHeaders = this._toNameValue(response?.headers);
            // Empty bodies are captured without text
            const responseBody = response?.text || undefined;
 
            const entry: CaptureEntry = {
                startedDateTime: new Date(request.startedAt).toISOString(),
                time,
                request: {
                    method: request.method,
                    url: request.fullURL,
                    httpVersion: `HTTP/1.1`,
                    cookies: [],
                    headers: requestHeaders,
                    queryString: this._toNameValue(request.params),
                    headersSize: -1,
                    bodySize: requestBody?.length ?? 0,
                },
                response: {
                    status: response?.status ?? 0,
                    statusText: response?.statusText ?? ``,
                    httpVersion: `HTTP/1.1`,
                    cookies: [],
                    headers: responseHeaders,
                    content: {
                        size: responseBody?.length ?? 0,
                        mimeType: this._findValue(responseHeaders, `content-type`),
                        text: responseBody,
                    },
                    redirectURL: ``,
                    headersSize: -1,
                    bodySize: responseBody?.length ?? 0,
                },
                cache: {},
                timings: {
                    send: 0,
                    wait: time,
                    receive: 0,
                },
            };
 
            if (requestBody !== undefined) {
                entry.request.postData = {
                    mimeType: this._findValue(requestHeaders, `content-type`),
                    text: requestBody,
                };
            }
 
            if (error) {
                entry._error = `${error.code}: ${error.message}`;
            }
 
            this.log.log.entries.push(entry);
        } catch (err) {
            Resources.logger(this).debug(`Unable to capture request: ${errorMessage(err)}`);
        }
    }
 
    /**
     * Converts a header object into a list of name/value pairs - multi-value headers (e.g. set-cookie) result in one pair per value
     * @param headers - The headers to convert
     * @returns The list of name/value pairs
     */
    _toNameValue(headers?: Record<string, unknown>): CaptureNameValue[] {
        if (!headers) {
            return [];
        }
 
        return Object.entries(headers)
            .flatMap(([name, value]) => (Array.isArray(value) ? value : [value])
                .filter(v => v !== undefined && v !== null && v !== false)
                .map(v => ({name, value: String(v)})));
    }
 
    /**
     * Finds the value of the first name/value pair matching the name (case-insensitive)
     * @param list - The list to search
     * @param name - The name to look for
     * @returns The value, or an empty string if not found
     */
    _findValue(list: CaptureNameValue[], name: string): string {
        return list.find(item => item.name.toLowerCase() === name)?.value ?? ``;
    }
 
}
 
/**
 * Options for creating an HttpClient
 */
export type HttpClientOptions = {
    /**
     * The base URL relative request URLs are resolved against
     */
    baseURL?: string,
    /**
     * Headers applied to every request
     */
    headers?: Record<string, string>,
    /**
     * Header jar applying (and extracting) headers and cookies - none is used if not provided
     */
    headerJar?: HeaderJar,
    /**
     * Network capture recording all requests - none is used if not provided
     */
    networkCapture?: NetworkCapture,
}
 
/**
 * Minimal HTTP client based on Node's built-in fetch
 * Every request needs to provide a validator for its response, making response validation part of the call signature
 */
export class HttpClient {
    /**
     * The base URL relative request URLs are resolved against
     */
    baseURL?: string;
 
    /**
     * Headers applied to every request
     */
    defaultHeaders: Record<string, string>;
 
    /**
     * Header jar applying headers and cookies to requests and extracting them from accepted responses
     */
    headerJar?: HeaderJar;
 
    /**
     * Network capture recording all requests and responses
     */
    networkCapture?: NetworkCapture;
 
    /**
     * Creates a new HttpClient
     * @param options - The client options
     */
    constructor(options: HttpClientOptions = {}) {
        this.baseURL = options.baseURL;
        this.defaultHeaders = {...options.headers};
        this.headerJar = options.headerJar;
        this.networkCapture = options.networkCapture;
    }
 
    /**
     * Performs a GET request
     * @param url - The URL to request, absolute or relative to the base URL
     * @param validate - Validates the response and provides the return value
     * @param config - Additional request configuration
     * @returns A promise resolving to the validated response
     * @throws An HttpError, if the request failed or the status was rejected - the validator's error, if the response is invalid
     */
    get<T>(url: string, validate: ResponseValidator<T>, config?: HttpRequestConfig): Promise<T> {
        return this.request(`GET`, url, undefined, validate, config);
    }
 
    /**
     * Performs a POST request
     * @param url - The URL to request, absolute or relative to the base URL
     * @param data - The request body - objects are serialized as JSON
     * @param validate - Validates the response and provides the return value
     * @param config - Additional request configuration
     * @returns A promise resolving to the validated response
     * @throws An HttpError, if the request failed or the status was rejected - the validator's error, if the response is invalid
     */
    post<T>(url: string, data: unknown, validate: ResponseValidator<T>, config?: HttpRequestConfig): Promise<T> {
        return this.request(`POST`, url, data, validate, config);
    }
 
    /**
     * Performs a PUT request
     * @param url - The URL to request, absolute or relative to the base URL
     * @param data - The request body - objects are serialized as JSON
     * @param validate - Validates the response and provides the return value
     * @param config - Additional request configuration
     * @returns A promise resolving to the validated response
     * @throws An HttpError, if the request failed or the status was rejected - the validator's error, if the response is invalid
     */
    put<T>(url: string, data: unknown, validate: ResponseValidator<T>, config?: HttpRequestConfig): Promise<T> {
        return this.request(`PUT`, url, data, validate, config);
    }
 
    /**
     * Performs a request: Applies the header jar, sends the request, checks the status, extracts headers and cookies, records the network capture and validates the response
     * The request is handed to the transport synchronously, before the first asynchronous step
     * @param method - The HTTP method
     * @param url - The URL to request, absolute or relative to the base URL
     * @param data - The request body
     * @param validate - Validates the response and provides the return value
     * @param config - Additional request configuration
     * @returns A promise resolving to the validated response
     */
    request<T>(method: HttpMethod, url: string, data: unknown, validate: ResponseValidator<T>, config: HttpRequestConfig = {}): Promise<T> {
        let request: HttpRequest;
        let pending: Promise<HttpRawResponse>;
        try {
            request = this._buildRequest(method, url, data, config);
            pending = this._send(request);
        } catch (err) {
            return Promise.reject(err);
        }
 
        return this._complete(request, pending, validate, config.validateStatus ?? (status => status >= 200 && status < 300));
    }
 
    /**
     * Downloads the provided URL and streams the body to the provided location
     * No headers, cookies or network capture are applied, since these are plain asset downloads
     * The partially written file is removed, if the download fails
     * @param url - The URL to download
     * @param location - The file location to write to (existing files will be overwritten)
     * @param timeout - Timeout in milliseconds, after which the download is aborted
     * @returns A promise that resolves, once the file has been written
     * @throws An HttpError, if the download failed - file system errors are rethrown as is
     */
    async download(url: string, location: string, timeout?: number): Promise<void> {
        const request: HttpRequest = {
            method: `GET`,
            url,
            fullURL: url,
            headers: {},
            startedAt: Date.now(),
        };
        const signal = timeout === undefined ? undefined : AbortSignal.timeout(timeout);
 
        let response: Response;
        try {
            response = await this._fetch(url, {signal});
        } catch (err) {
            throw HttpError.fromNetworkError(err, request);
        }
 
        if (!response.ok || !response.body) {
            await response.body?.cancel();
            throw HttpError.fromResponse(this._parseResponse(request, {
                status: response.status,
                statusText: response.statusText,
                headers: response.headers,
                body: ``,
            }));
        }
 
        try {
            await pipeline(Readable.fromWeb(response.body as ReadableStream), createWriteStream(location, {flags: `w`}));
        } catch (err) {
            await fs.rm(location, {force: true});
            // Errors while receiving the body surface as TypeError (connection terminated) or DOMException (timeout / abort) - file system errors are passed on
            if (Error.isError(err) && [`TypeError`, `TimeoutError`, `AbortError`].includes(err.name)) {
                throw HttpError.fromNetworkError(err, request);
            }
 
            throw err;
        }
    }
 
    /**
     * Builds the request, applying default headers, the request configuration and the header jar
     * @param method - The HTTP method
     * @param url - The URL to request
     * @param data - The request body
     * @param config - The request configuration
     * @returns The prepared request
     */
    _buildRequest(method: HttpMethod, url: string, data: unknown, config: HttpRequestConfig): HttpRequest {
        const request: HttpRequest = {
            method,
            url,
            baseURL: this.baseURL,
            fullURL: this._resolveURL(url, config.params),
            params: config.params,
            headers: {
                ...this.defaultHeaders,
                ...config.headers,
            },
            startedAt: Date.now(),
        };
 
        if (data !== undefined && data !== null) {
            if (typeof data === `string`) {
                request.data = data;
            } else {
                request.data = JSON.stringify(data);
                if (!Object.keys(request.headers).some(key => key.toLowerCase() === `content-type`)) {
                    request.headers[`Content-Type`] = `application/json`;
                }
            }
        }
 
        if (this.headerJar) {
            return this.headerJar._injectHeaders(request);
        }
 
        return request;
    }
 
    /**
     * Resolves the URL against the base URL and appends query parameters
     * @param url - The URL to resolve
     * @param params - The query parameters
     * @returns The resolved URL
     */
    _resolveURL(url: string, params?: Record<string, string>): string {
        let fullURL = url;
        if (this.baseURL && !ABSOLUTE_URL_REGEX.test(url)) {
            fullURL = url.length > 0
                ? this.baseURL.replace(/\/?\/$/, ``) + `/` + url.replace(/^\/+/, ``)
                : this.baseURL;
        }
 
        if (params && Object.keys(params).length > 0) {
            fullURL += (fullURL.includes(`?`) ? `&` : `?`) + new URLSearchParams(params).toString();
        }
 
        return fullURL;
    }
 
    /**
     * Completes a request, once the transport has been invoked
     * @param request - The request
     * @param pending - The pending transport response
     * @param validate - The response validator
     * @param validateStatus - Decides, if the response status is accepted
     * @returns A promise resolving to the validated response
     */
    async _complete<T>(request: HttpRequest, pending: Promise<HttpRawResponse>, validate: ResponseValidator<T>, validateStatus: (status: number) => boolean): Promise<T> {
        let response: HttpResponse | undefined;
        let error: HttpError | undefined;
        try {
            let rawResponse: HttpRawResponse;
            try {
                rawResponse = await pending;
            } catch (err) {
                throw HttpError.fromNetworkError(err, request);
            }
 
            response = this._parseResponse(request, rawResponse);
 
            if (!validateStatus(response.status)) {
                throw HttpError.fromResponse(response);
            }
 
            this.headerJar?._extractHeaders(response);
        } catch (err) {
            if (isHttpError(err)) {
                error = err;
            }
 
            throw err;
        } finally {
            this.networkCapture?.addEntry(request, response, error);
        }
 
        return validate(response);
    }
 
    /**
     * Converts the raw transport response
     * @param request - The request
     * @param rawResponse - The raw response
     * @returns The response with normalized headers and parsed body
     */
    _parseResponse(request: HttpRequest, rawResponse: HttpRawResponse): HttpResponse {
        const headers: Record<string, any> = {};
        rawResponse.headers.forEach((value, key) => {
            if (key !== `set-cookie`) {
                headers[key] = value;
            }
        });
 
        const setCookie = rawResponse.headers.getSetCookie();
        if (setCookie.length > 0) {
            headers[`set-cookie`] = setCookie;
        }
 
        return {
            status: rawResponse.status,
            statusText: rawResponse.statusText,
            headers,
            data: this._parseBody(rawResponse.body),
            text: rawResponse.body,
            config: request,
        };
    }
 
    /**
     * Parses the body as JSON, if possible - an empty body is kept as empty string, a non-JSON body as raw string
     * @param body - The raw body
     * @returns The parsed body
     */
    _parseBody(body: string): any {
        if (body.length === 0) {
            return body;
        }
 
        try {
            return JSON.parse(body);
        } catch {
            return body;
        }
    }
 
    /**
     * Sends the request using fetch - the transport layer of this client
     * @param request - The request to send
     * @returns The raw response
     */
    async _send(request: HttpRequest): Promise<HttpRawResponse> {
        const response = await this._fetch(request.fullURL, {
            method: request.method,
            headers: request.headers,
            body: request.data,
        });
 
        return {
            status: response.status,
            statusText: response.statusText,
            headers: response.headers,
            body: await response.text(),
        };
    }
 
    /**
     * The fetch implementation used by this client
     * @param input - The URL
     * @param init - The request options
     * @returns The fetch response
     */
    _fetch(input: string, init: RequestInit): Promise<Response> {
        return fetch(input, init);
    }
}